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_recv = true;
57 static bool cfg_do_setsockopt;
58 static int cfg_specific_test_id = -1;
60 static unsigned short cfg_port = 9000;
62 static char buf[ETH_MAX_MTU];
65 int tlen; /* send() buffer size, may exceed mss */
66 bool tfail; /* send() call is expected to fail */
67 int gso_len; /* mss after applying gso */
68 int r_num_mss; /* recv(): number of calls of full mss */
69 int r_len_last; /* recv(): size of last non-mss dgram, if any */
70 bool v6_ext_hdr; /* send() dgrams with IPv6 extension headers */
73 const struct in6_addr addr6 = {
74 { { 0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }, /* fd00::1 */
77 const struct in_addr addr4 = {
78 __constant_htonl(0x0a000001), /* 10.0.0.1 */
81 static const char ipv6_hopopts_pad1[8] = { 0 };
83 struct testcase testcases_v4[] = {
85 /* no GSO: send a single byte */
90 /* no GSO: send a single MSS */
95 /* no GSO: send a single MSS + 1B: fail */
96 .tlen = CONST_MSS_V4 + 1,
100 /* send a single MSS: will fall back to no GSO */
101 .tlen = CONST_MSS_V4,
102 .gso_len = CONST_MSS_V4,
106 /* datalen <= MSS < gso_len: will fall back to no GSO */
107 .tlen = CONST_MSS_V4,
108 .gso_len = CONST_MSS_V4 + 1,
110 .r_len_last = CONST_MSS_V4,
113 /* MSS < datalen < gso_len: fail */
114 .tlen = CONST_MSS_V4 + 1,
115 .gso_len = CONST_MSS_V4 + 2,
119 /* send a single MSS + 1B */
120 .tlen = CONST_MSS_V4 + 1,
121 .gso_len = CONST_MSS_V4,
126 /* send exactly 2 MSS */
127 .tlen = CONST_MSS_V4 * 2,
128 .gso_len = CONST_MSS_V4,
132 /* send 2 MSS + 1B */
133 .tlen = (CONST_MSS_V4 * 2) + 1,
134 .gso_len = CONST_MSS_V4,
140 .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
141 .gso_len = CONST_MSS_V4,
142 .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
147 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
148 .gso_len = CONST_MSS_V4,
149 .r_num_mss = CONST_MAX_SEGS_V4,
150 .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
151 (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
154 /* send MAX + 1: fail */
155 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
156 .gso_len = CONST_MSS_V4,
160 /* send a single 1B MSS: will fall back to no GSO */
166 /* send 2 1B segments */
172 /* send 2B + 2B + 1B segments */
179 /* send max number of min sized segments */
180 .tlen = UDP_MAX_SEGMENTS,
182 .r_num_mss = UDP_MAX_SEGMENTS,
185 /* send max number + 1 of min sized segments: fail */
186 .tlen = UDP_MAX_SEGMENTS + 1,
196 #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
199 struct testcase testcases_v6[] = {
201 /* no GSO: send a single byte */
206 /* no GSO: send a single MSS */
207 .tlen = CONST_MSS_V6,
211 /* no GSO: send a single MSS + 1B: fail */
212 .tlen = CONST_MSS_V6 + 1,
216 /* send a single MSS: will fall back to no GSO */
217 .tlen = CONST_MSS_V6,
218 .gso_len = CONST_MSS_V6,
222 /* datalen <= MSS < gso_len: will fall back to no GSO */
223 .tlen = CONST_MSS_V6,
224 .gso_len = CONST_MSS_V6 + 1,
226 .r_len_last = CONST_MSS_V6,
229 /* MSS < datalen < gso_len: fail */
230 .tlen = CONST_MSS_V6 + 1,
231 .gso_len = CONST_MSS_V6 + 2,
235 /* send a single MSS + 1B */
236 .tlen = CONST_MSS_V6 + 1,
237 .gso_len = CONST_MSS_V6,
242 /* send exactly 2 MSS */
243 .tlen = CONST_MSS_V6 * 2,
244 .gso_len = CONST_MSS_V6,
248 /* send 2 MSS + 1B */
249 .tlen = (CONST_MSS_V6 * 2) + 1,
250 .gso_len = CONST_MSS_V6,
256 .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
257 .gso_len = CONST_MSS_V6,
258 .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
263 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
264 .gso_len = CONST_MSS_V6,
265 .r_num_mss = CONST_MAX_SEGS_V6,
266 .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
267 (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
270 /* send MAX + 1: fail */
271 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
272 .gso_len = CONST_MSS_V6,
276 /* send a single 1B MSS: will fall back to no GSO */
282 /* send 2 1B segments */
288 /* send 2 1B segments with extension headers */
295 /* send 2B + 2B + 1B segments */
302 /* send max number of min sized segments */
303 .tlen = UDP_MAX_SEGMENTS,
305 .r_num_mss = UDP_MAX_SEGMENTS,
308 /* send max number + 1 of min sized segments: fail */
309 .tlen = UDP_MAX_SEGMENTS + 1,
318 static void set_pmtu_discover(int fd, bool is_ipv4)
320 int level, name, val;
324 name = IP_MTU_DISCOVER;
325 val = IP_PMTUDISC_DO;
328 name = IPV6_MTU_DISCOVER;
329 val = IPV6_PMTUDISC_DO;
332 if (setsockopt(fd, level, name, &val, sizeof(val)))
333 error(1, errno, "setsockopt path mtu");
336 static unsigned int get_path_mtu(int fd, bool is_ipv4)
342 vallen = sizeof(mtu);
344 ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
346 ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
349 error(1, errno, "getsockopt mtu");
352 fprintf(stderr, "path mtu (read): %u\n", mtu);
356 static bool __send_one(int fd, struct msghdr *msg, int flags)
360 ret = sendmsg(fd, msg, flags);
362 (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
365 error(1, errno, "sendmsg");
366 if (ret != msg->msg_iov->iov_len)
367 error(1, 0, "sendto: %d != %llu", ret,
368 (unsigned long long)msg->msg_iov->iov_len);
370 error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
375 static bool send_one(int fd, int len, int gso_len,
376 struct sockaddr *addr, socklen_t alen)
378 char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
379 struct msghdr msg = {0};
380 struct iovec iov = {0};
390 msg.msg_namelen = alen;
392 if (gso_len && !cfg_do_setsockopt) {
393 msg.msg_control = control;
394 msg.msg_controllen = sizeof(control);
396 cm = CMSG_FIRSTHDR(&msg);
397 cm->cmsg_level = SOL_UDP;
398 cm->cmsg_type = UDP_SEGMENT;
399 cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
400 *((uint16_t *) CMSG_DATA(cm)) = gso_len;
403 /* If MSG_MORE, send 1 byte followed by remainder */
404 if (cfg_do_msgmore && len > 1) {
406 if (!__send_one(fd, &msg, MSG_MORE))
407 error(1, 0, "send 1B failed");
410 iov.iov_len = len - 1;
413 return __send_one(fd, &msg, 0);
416 static int recv_one(int fd, int flags)
420 ret = recv(fd, buf, sizeof(buf), flags);
421 if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
424 error(1, errno, "recv");
429 static void run_one(struct testcase *test, int fdt, int fdr,
430 struct sockaddr *addr, socklen_t alen)
432 int i, ret, val, mss;
435 fprintf(stderr, "ipv%d tx:%d gso:%d %s%s\n",
436 addr->sa_family == AF_INET ? 4 : 6,
437 test->tlen, test->gso_len,
438 test->v6_ext_hdr ? "ext-hdr " : "",
439 test->tfail ? "(fail)" : "");
441 if (test->v6_ext_hdr) {
442 if (setsockopt(fdt, IPPROTO_IPV6, IPV6_HOPOPTS,
443 ipv6_hopopts_pad1, sizeof(ipv6_hopopts_pad1)))
444 error(1, errno, "setsockopt ipv6 hopopts");
448 if (cfg_do_setsockopt) {
449 if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
450 error(1, errno, "setsockopt udp segment");
453 sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
454 if (sent && test->tfail)
455 error(1, 0, "send succeeded while expecting failure");
456 if (!sent && !test->tfail)
457 error(1, 0, "send failed while expecting success");
459 if (test->v6_ext_hdr) {
460 if (setsockopt(fdt, IPPROTO_IPV6, IPV6_HOPOPTS, NULL, 0))
461 error(1, errno, "setsockopt ipv6 hopopts clear");
473 mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
476 /* Recv all full MSS datagrams */
477 for (i = 0; i < test->r_num_mss; i++) {
478 ret = recv_one(fdr, 0);
480 error(1, 0, "recv.%d: %d != %d", i, ret, mss);
483 /* Recv the non-full last datagram, if tlen was not a multiple of mss */
484 if (test->r_len_last) {
485 ret = recv_one(fdr, 0);
486 if (ret != test->r_len_last)
487 error(1, 0, "recv.%d: %d != %d (last)",
488 i, ret, test->r_len_last);
491 /* Verify received all data */
492 ret = recv_one(fdr, MSG_DONTWAIT);
494 error(1, 0, "recv: unexpected datagram");
497 static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
499 struct testcase *tests, *test;
501 tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
503 for (test = tests; test->tlen; test++) {
504 /* if a specific test is given, then skip all others */
505 if (cfg_specific_test_id == -1 ||
506 cfg_specific_test_id == test - tests)
507 run_one(test, fdt, fdr, addr, alen);
511 static void run_test(struct sockaddr *addr, socklen_t alen)
513 struct timeval tv = { .tv_usec = 100 * 1000 };
516 fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
518 error(1, errno, "socket r");
521 if (bind(fdr, addr, alen))
522 error(1, errno, "bind");
525 /* Have tests fail quickly instead of hang */
526 if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
527 error(1, errno, "setsockopt rcv timeout");
529 fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
531 error(1, errno, "socket t");
533 /* Do not fragment these datagrams: only succeed if GSO works */
534 set_pmtu_discover(fdt, addr->sa_family == AF_INET);
536 if (cfg_do_connectionless)
537 run_all(fdt, fdr, addr, alen);
539 if (cfg_do_connected) {
540 if (connect(fdt, addr, alen))
541 error(1, errno, "connect");
543 val = get_path_mtu(fdt, addr->sa_family == AF_INET);
544 if (val != CONST_MTU_TEST)
545 error(1, 0, "bad path mtu %u\n", val);
547 run_all(fdt, fdr, addr, 0 /* use connected addr */);
551 error(1, errno, "close t");
553 error(1, errno, "close r");
556 static void run_test_v4(void)
558 struct sockaddr_in addr = {0};
560 addr.sin_family = AF_INET;
561 addr.sin_port = htons(cfg_port);
562 addr.sin_addr = addr4;
564 run_test((void *)&addr, sizeof(addr));
567 static void run_test_v6(void)
569 struct sockaddr_in6 addr = {0};
571 addr.sin6_family = AF_INET6;
572 addr.sin6_port = htons(cfg_port);
573 addr.sin6_addr = addr6;
575 run_test((void *)&addr, sizeof(addr));
578 static void parse_opts(int argc, char **argv)
582 while ((c = getopt(argc, argv, "46cCmRst:")) != -1) {
591 cfg_do_connected = true;
594 cfg_do_connectionless = true;
597 cfg_do_msgmore = true;
603 cfg_do_setsockopt = true;
606 cfg_specific_test_id = strtoul(optarg, NULL, 0);
609 error(1, 0, "%s: parse error", argv[0]);
614 int main(int argc, char **argv)
616 parse_opts(argc, argv);
623 fprintf(stderr, "OK\n");