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 */
72 const struct in6_addr addr6 = {
73 { { 0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }, /* fd00::1 */
76 const struct in_addr addr4 = {
77 __constant_htonl(0x0a000001), /* 10.0.0.1 */
80 struct testcase testcases_v4[] = {
82 /* no GSO: send a single byte */
87 /* no GSO: send a single MSS */
92 /* no GSO: send a single MSS + 1B: fail */
93 .tlen = CONST_MSS_V4 + 1,
97 /* send a single MSS: will fall back to no GSO */
99 .gso_len = CONST_MSS_V4,
103 /* send a single MSS + 1B */
104 .tlen = CONST_MSS_V4 + 1,
105 .gso_len = CONST_MSS_V4,
110 /* send exactly 2 MSS */
111 .tlen = CONST_MSS_V4 * 2,
112 .gso_len = CONST_MSS_V4,
116 /* send 2 MSS + 1B */
117 .tlen = (CONST_MSS_V4 * 2) + 1,
118 .gso_len = CONST_MSS_V4,
124 .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
125 .gso_len = CONST_MSS_V4,
126 .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
131 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
132 .gso_len = CONST_MSS_V4,
133 .r_num_mss = CONST_MAX_SEGS_V4,
134 .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
135 (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
138 /* send MAX + 1: fail */
139 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
140 .gso_len = CONST_MSS_V4,
144 /* send a single 1B MSS: will fall back to no GSO */
150 /* send 2 1B segments */
156 /* send 2B + 2B + 1B segments */
163 /* send max number of min sized segments */
164 .tlen = UDP_MAX_SEGMENTS,
166 .r_num_mss = UDP_MAX_SEGMENTS,
169 /* send max number + 1 of min sized segments: fail */
170 .tlen = UDP_MAX_SEGMENTS + 1,
180 #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
183 struct testcase testcases_v6[] = {
185 /* no GSO: send a single byte */
190 /* no GSO: send a single MSS */
191 .tlen = CONST_MSS_V6,
195 /* no GSO: send a single MSS + 1B: fail */
196 .tlen = CONST_MSS_V6 + 1,
200 /* send a single MSS: will fall back to no GSO */
201 .tlen = CONST_MSS_V6,
202 .gso_len = CONST_MSS_V6,
206 /* send a single MSS + 1B */
207 .tlen = CONST_MSS_V6 + 1,
208 .gso_len = CONST_MSS_V6,
213 /* send exactly 2 MSS */
214 .tlen = CONST_MSS_V6 * 2,
215 .gso_len = CONST_MSS_V6,
219 /* send 2 MSS + 1B */
220 .tlen = (CONST_MSS_V6 * 2) + 1,
221 .gso_len = CONST_MSS_V6,
227 .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
228 .gso_len = CONST_MSS_V6,
229 .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
234 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
235 .gso_len = CONST_MSS_V6,
236 .r_num_mss = CONST_MAX_SEGS_V6,
237 .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
238 (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
241 /* send MAX + 1: fail */
242 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
243 .gso_len = CONST_MSS_V6,
247 /* send a single 1B MSS: will fall back to no GSO */
253 /* send 2 1B segments */
259 /* send 2B + 2B + 1B segments */
266 /* send max number of min sized segments */
267 .tlen = UDP_MAX_SEGMENTS,
269 .r_num_mss = UDP_MAX_SEGMENTS,
272 /* send max number + 1 of min sized segments: fail */
273 .tlen = UDP_MAX_SEGMENTS + 1,
282 static void set_pmtu_discover(int fd, bool is_ipv4)
284 int level, name, val;
288 name = IP_MTU_DISCOVER;
289 val = IP_PMTUDISC_DO;
292 name = IPV6_MTU_DISCOVER;
293 val = IPV6_PMTUDISC_DO;
296 if (setsockopt(fd, level, name, &val, sizeof(val)))
297 error(1, errno, "setsockopt path mtu");
300 static unsigned int get_path_mtu(int fd, bool is_ipv4)
306 vallen = sizeof(mtu);
308 ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
310 ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
313 error(1, errno, "getsockopt mtu");
316 fprintf(stderr, "path mtu (read): %u\n", mtu);
320 static bool __send_one(int fd, struct msghdr *msg, int flags)
324 ret = sendmsg(fd, msg, flags);
326 (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
329 error(1, errno, "sendmsg");
330 if (ret != msg->msg_iov->iov_len)
331 error(1, 0, "sendto: %d != %llu", ret,
332 (unsigned long long)msg->msg_iov->iov_len);
334 error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
339 static bool send_one(int fd, int len, int gso_len,
340 struct sockaddr *addr, socklen_t alen)
342 char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
343 struct msghdr msg = {0};
344 struct iovec iov = {0};
354 msg.msg_namelen = alen;
356 if (gso_len && !cfg_do_setsockopt) {
357 msg.msg_control = control;
358 msg.msg_controllen = sizeof(control);
360 cm = CMSG_FIRSTHDR(&msg);
361 cm->cmsg_level = SOL_UDP;
362 cm->cmsg_type = UDP_SEGMENT;
363 cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
364 *((uint16_t *) CMSG_DATA(cm)) = gso_len;
367 /* If MSG_MORE, send 1 byte followed by remainder */
368 if (cfg_do_msgmore && len > 1) {
370 if (!__send_one(fd, &msg, MSG_MORE))
371 error(1, 0, "send 1B failed");
374 iov.iov_len = len - 1;
377 return __send_one(fd, &msg, 0);
380 static int recv_one(int fd, int flags)
384 ret = recv(fd, buf, sizeof(buf), flags);
385 if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
388 error(1, errno, "recv");
393 static void run_one(struct testcase *test, int fdt, int fdr,
394 struct sockaddr *addr, socklen_t alen)
396 int i, ret, val, mss;
399 fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
400 addr->sa_family == AF_INET ? 4 : 6,
401 test->tlen, test->gso_len,
402 test->tfail ? "(fail)" : "");
405 if (cfg_do_setsockopt) {
406 if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
407 error(1, errno, "setsockopt udp segment");
410 sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
411 if (sent && test->tfail)
412 error(1, 0, "send succeeded while expecting failure");
413 if (!sent && !test->tfail)
414 error(1, 0, "send failed while expecting success");
424 mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
427 /* Recv all full MSS datagrams */
428 for (i = 0; i < test->r_num_mss; i++) {
429 ret = recv_one(fdr, 0);
431 error(1, 0, "recv.%d: %d != %d", i, ret, mss);
434 /* Recv the non-full last datagram, if tlen was not a multiple of mss */
435 if (test->r_len_last) {
436 ret = recv_one(fdr, 0);
437 if (ret != test->r_len_last)
438 error(1, 0, "recv.%d: %d != %d (last)",
439 i, ret, test->r_len_last);
442 /* Verify received all data */
443 ret = recv_one(fdr, MSG_DONTWAIT);
445 error(1, 0, "recv: unexpected datagram");
448 static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
450 struct testcase *tests, *test;
452 tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
454 for (test = tests; test->tlen; test++) {
455 /* if a specific test is given, then skip all others */
456 if (cfg_specific_test_id == -1 ||
457 cfg_specific_test_id == test - tests)
458 run_one(test, fdt, fdr, addr, alen);
462 static void run_test(struct sockaddr *addr, socklen_t alen)
464 struct timeval tv = { .tv_usec = 100 * 1000 };
467 fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
469 error(1, errno, "socket r");
472 if (bind(fdr, addr, alen))
473 error(1, errno, "bind");
476 /* Have tests fail quickly instead of hang */
477 if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
478 error(1, errno, "setsockopt rcv timeout");
480 fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
482 error(1, errno, "socket t");
484 /* Do not fragment these datagrams: only succeed if GSO works */
485 set_pmtu_discover(fdt, addr->sa_family == AF_INET);
487 if (cfg_do_connectionless)
488 run_all(fdt, fdr, addr, alen);
490 if (cfg_do_connected) {
491 if (connect(fdt, addr, alen))
492 error(1, errno, "connect");
494 val = get_path_mtu(fdt, addr->sa_family == AF_INET);
495 if (val != CONST_MTU_TEST)
496 error(1, 0, "bad path mtu %u\n", val);
498 run_all(fdt, fdr, addr, 0 /* use connected addr */);
502 error(1, errno, "close t");
504 error(1, errno, "close r");
507 static void run_test_v4(void)
509 struct sockaddr_in addr = {0};
511 addr.sin_family = AF_INET;
512 addr.sin_port = htons(cfg_port);
513 addr.sin_addr = addr4;
515 run_test((void *)&addr, sizeof(addr));
518 static void run_test_v6(void)
520 struct sockaddr_in6 addr = {0};
522 addr.sin6_family = AF_INET6;
523 addr.sin6_port = htons(cfg_port);
524 addr.sin6_addr = addr6;
526 run_test((void *)&addr, sizeof(addr));
529 static void parse_opts(int argc, char **argv)
533 while ((c = getopt(argc, argv, "46cCmRst:")) != -1) {
542 cfg_do_connected = true;
545 cfg_do_connectionless = true;
548 cfg_do_msgmore = true;
554 cfg_do_setsockopt = true;
557 cfg_specific_test_id = strtoul(optarg, NULL, 0);
560 error(1, 0, "%s: parse error", argv[0]);
565 int main(int argc, char **argv)
567 parse_opts(argc, argv);
574 fprintf(stderr, "OK\n");