1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/netlink.h>
12 #include <linux/rtnetlink.h>
13 #include <netinet/if_ether.h>
14 #include <netinet/ip.h>
15 #include <netinet/ip6.h>
16 #include <netinet/udp.h>
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
29 #define ETH_MAX_MTU 0xFFFFU
33 #define UDP_SEGMENT 103
36 #ifndef UDP_MAX_SEGMENTS
37 #define UDP_MAX_SEGMENTS (1 << 7UL)
40 #define CONST_MTU_TEST 1500
42 #define CONST_HDRLEN_V4 (sizeof(struct iphdr) + sizeof(struct udphdr))
43 #define CONST_HDRLEN_V6 (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
45 #define CONST_MSS_V4 (CONST_MTU_TEST - CONST_HDRLEN_V4)
46 #define CONST_MSS_V6 (CONST_MTU_TEST - CONST_HDRLEN_V6)
48 #define CONST_MAX_SEGS_V4 (ETH_MAX_MTU / CONST_MSS_V4)
49 #define CONST_MAX_SEGS_V6 (ETH_MAX_MTU / CONST_MSS_V6)
51 static bool cfg_do_ipv4;
52 static bool cfg_do_ipv6;
53 static bool cfg_do_connected;
54 static bool cfg_do_connectionless;
55 static bool cfg_do_msgmore;
56 static bool cfg_do_setsockopt;
57 static int cfg_specific_test_id = -1;
59 static unsigned short cfg_port = 9000;
61 static char buf[ETH_MAX_MTU];
64 int tlen; /* send() buffer size, may exceed mss */
65 bool tfail; /* send() call is expected to fail */
66 int gso_len; /* mss after applying gso */
67 int r_num_mss; /* recv(): number of calls of full mss */
68 int r_len_last; /* recv(): size of last non-mss dgram, if any */
71 const struct in6_addr addr6 = {
72 { { 0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }, /* fd00::1 */
75 const struct in_addr addr4 = {
76 __constant_htonl(0x0a000001), /* 10.0.0.1 */
79 struct testcase testcases_v4[] = {
81 /* no GSO: send a single byte */
86 /* no GSO: send a single MSS */
91 /* no GSO: send a single MSS + 1B: fail */
92 .tlen = CONST_MSS_V4 + 1,
96 /* send a single MSS: will fall back to no GSO */
98 .gso_len = CONST_MSS_V4,
102 /* send a single MSS + 1B */
103 .tlen = CONST_MSS_V4 + 1,
104 .gso_len = CONST_MSS_V4,
109 /* send exactly 2 MSS */
110 .tlen = CONST_MSS_V4 * 2,
111 .gso_len = CONST_MSS_V4,
115 /* send 2 MSS + 1B */
116 .tlen = (CONST_MSS_V4 * 2) + 1,
117 .gso_len = CONST_MSS_V4,
123 .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
124 .gso_len = CONST_MSS_V4,
125 .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
130 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
131 .gso_len = CONST_MSS_V4,
132 .r_num_mss = CONST_MAX_SEGS_V4,
133 .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
134 (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
137 /* send MAX + 1: fail */
138 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
139 .gso_len = CONST_MSS_V4,
143 /* send a single 1B MSS: will fall back to no GSO */
149 /* send 2 1B segments */
155 /* send 2B + 2B + 1B segments */
162 /* send max number of min sized segments */
163 .tlen = UDP_MAX_SEGMENTS,
165 .r_num_mss = UDP_MAX_SEGMENTS,
168 /* send max number + 1 of min sized segments: fail */
169 .tlen = UDP_MAX_SEGMENTS + 1,
179 #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
182 struct testcase testcases_v6[] = {
184 /* no GSO: send a single byte */
189 /* no GSO: send a single MSS */
190 .tlen = CONST_MSS_V6,
194 /* no GSO: send a single MSS + 1B: fail */
195 .tlen = CONST_MSS_V6 + 1,
199 /* send a single MSS: will fall back to no GSO */
200 .tlen = CONST_MSS_V6,
201 .gso_len = CONST_MSS_V6,
205 /* send a single MSS + 1B */
206 .tlen = CONST_MSS_V6 + 1,
207 .gso_len = CONST_MSS_V6,
212 /* send exactly 2 MSS */
213 .tlen = CONST_MSS_V6 * 2,
214 .gso_len = CONST_MSS_V6,
218 /* send 2 MSS + 1B */
219 .tlen = (CONST_MSS_V6 * 2) + 1,
220 .gso_len = CONST_MSS_V6,
226 .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
227 .gso_len = CONST_MSS_V6,
228 .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
233 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
234 .gso_len = CONST_MSS_V6,
235 .r_num_mss = CONST_MAX_SEGS_V6,
236 .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
237 (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
240 /* send MAX + 1: fail */
241 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
242 .gso_len = CONST_MSS_V6,
246 /* send a single 1B MSS: will fall back to no GSO */
252 /* send 2 1B segments */
258 /* send 2B + 2B + 1B segments */
265 /* send max number of min sized segments */
266 .tlen = UDP_MAX_SEGMENTS,
268 .r_num_mss = UDP_MAX_SEGMENTS,
271 /* send max number + 1 of min sized segments: fail */
272 .tlen = UDP_MAX_SEGMENTS + 1,
281 static void set_pmtu_discover(int fd, bool is_ipv4)
283 int level, name, val;
287 name = IP_MTU_DISCOVER;
288 val = IP_PMTUDISC_DO;
291 name = IPV6_MTU_DISCOVER;
292 val = IPV6_PMTUDISC_DO;
295 if (setsockopt(fd, level, name, &val, sizeof(val)))
296 error(1, errno, "setsockopt path mtu");
299 static unsigned int get_path_mtu(int fd, bool is_ipv4)
305 vallen = sizeof(mtu);
307 ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
309 ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
312 error(1, errno, "getsockopt mtu");
315 fprintf(stderr, "path mtu (read): %u\n", mtu);
319 static bool __send_one(int fd, struct msghdr *msg, int flags)
323 ret = sendmsg(fd, msg, flags);
325 (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
328 error(1, errno, "sendmsg");
329 if (ret != msg->msg_iov->iov_len)
330 error(1, 0, "sendto: %d != %llu", ret,
331 (unsigned long long)msg->msg_iov->iov_len);
333 error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
338 static bool send_one(int fd, int len, int gso_len,
339 struct sockaddr *addr, socklen_t alen)
341 char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
342 struct msghdr msg = {0};
343 struct iovec iov = {0};
353 msg.msg_namelen = alen;
355 if (gso_len && !cfg_do_setsockopt) {
356 msg.msg_control = control;
357 msg.msg_controllen = sizeof(control);
359 cm = CMSG_FIRSTHDR(&msg);
360 cm->cmsg_level = SOL_UDP;
361 cm->cmsg_type = UDP_SEGMENT;
362 cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
363 *((uint16_t *) CMSG_DATA(cm)) = gso_len;
366 /* If MSG_MORE, send 1 byte followed by remainder */
367 if (cfg_do_msgmore && len > 1) {
369 if (!__send_one(fd, &msg, MSG_MORE))
370 error(1, 0, "send 1B failed");
373 iov.iov_len = len - 1;
376 return __send_one(fd, &msg, 0);
379 static int recv_one(int fd, int flags)
383 ret = recv(fd, buf, sizeof(buf), flags);
384 if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
387 error(1, errno, "recv");
392 static void run_one(struct testcase *test, int fdt, int fdr,
393 struct sockaddr *addr, socklen_t alen)
395 int i, ret, val, mss;
398 fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
399 addr->sa_family == AF_INET ? 4 : 6,
400 test->tlen, test->gso_len,
401 test->tfail ? "(fail)" : "");
404 if (cfg_do_setsockopt) {
405 if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
406 error(1, errno, "setsockopt udp segment");
409 sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
410 if (sent && test->tfail)
411 error(1, 0, "send succeeded while expecting failure");
412 if (!sent && !test->tfail)
413 error(1, 0, "send failed while expecting success");
420 mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
423 /* Recv all full MSS datagrams */
424 for (i = 0; i < test->r_num_mss; i++) {
425 ret = recv_one(fdr, 0);
427 error(1, 0, "recv.%d: %d != %d", i, ret, mss);
430 /* Recv the non-full last datagram, if tlen was not a multiple of mss */
431 if (test->r_len_last) {
432 ret = recv_one(fdr, 0);
433 if (ret != test->r_len_last)
434 error(1, 0, "recv.%d: %d != %d (last)",
435 i, ret, test->r_len_last);
438 /* Verify received all data */
439 ret = recv_one(fdr, MSG_DONTWAIT);
441 error(1, 0, "recv: unexpected datagram");
444 static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
446 struct testcase *tests, *test;
448 tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
450 for (test = tests; test->tlen; test++) {
451 /* if a specific test is given, then skip all others */
452 if (cfg_specific_test_id == -1 ||
453 cfg_specific_test_id == test - tests)
454 run_one(test, fdt, fdr, addr, alen);
458 static void run_test(struct sockaddr *addr, socklen_t alen)
460 struct timeval tv = { .tv_usec = 100 * 1000 };
463 fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
465 error(1, errno, "socket r");
467 if (bind(fdr, addr, alen))
468 error(1, errno, "bind");
470 /* Have tests fail quickly instead of hang */
471 if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
472 error(1, errno, "setsockopt rcv timeout");
474 fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
476 error(1, errno, "socket t");
478 /* Do not fragment these datagrams: only succeed if GSO works */
479 set_pmtu_discover(fdt, addr->sa_family == AF_INET);
481 if (cfg_do_connectionless)
482 run_all(fdt, fdr, addr, alen);
484 if (cfg_do_connected) {
485 if (connect(fdt, addr, alen))
486 error(1, errno, "connect");
488 val = get_path_mtu(fdt, addr->sa_family == AF_INET);
489 if (val != CONST_MTU_TEST)
490 error(1, 0, "bad path mtu %u\n", val);
492 run_all(fdt, fdr, addr, 0 /* use connected addr */);
496 error(1, errno, "close t");
498 error(1, errno, "close r");
501 static void run_test_v4(void)
503 struct sockaddr_in addr = {0};
505 addr.sin_family = AF_INET;
506 addr.sin_port = htons(cfg_port);
507 addr.sin_addr = addr4;
509 run_test((void *)&addr, sizeof(addr));
512 static void run_test_v6(void)
514 struct sockaddr_in6 addr = {0};
516 addr.sin6_family = AF_INET6;
517 addr.sin6_port = htons(cfg_port);
518 addr.sin6_addr = addr6;
520 run_test((void *)&addr, sizeof(addr));
523 static void parse_opts(int argc, char **argv)
527 while ((c = getopt(argc, argv, "46cCmst:")) != -1) {
536 cfg_do_connected = true;
539 cfg_do_connectionless = true;
542 cfg_do_msgmore = true;
545 cfg_do_setsockopt = true;
548 cfg_specific_test_id = strtoul(optarg, NULL, 0);
551 error(1, 0, "%s: parse error", argv[0]);
556 int main(int argc, char **argv)
558 parse_opts(argc, argv);
565 fprintf(stderr, "OK\n");