Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / net / udpgso.c
blob270c17ab071e46506f9a705ecdac4c4994c62484
1 // SPDX-License-Identifier: GPL-2.0
3 #define _GNU_SOURCE
5 #include <stddef.h>
6 #include <arpa/inet.h>
7 #include <error.h>
8 #include <errno.h>
9 #include <net/if.h>
10 #include <linux/in.h>
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>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <sys/stat.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <unistd.h>
29 #ifndef ETH_MAX_MTU
30 #define ETH_MAX_MTU 0xFFFFU
31 #endif
33 #ifndef UDP_SEGMENT
34 #define UDP_SEGMENT 103
35 #endif
37 #ifndef UDP_MAX_SEGMENTS
38 #define UDP_MAX_SEGMENTS (1 << 6UL)
39 #endif
41 #define CONST_MTU_TEST 1500
43 #define CONST_HDRLEN_V4 (sizeof(struct iphdr) + sizeof(struct udphdr))
44 #define CONST_HDRLEN_V6 (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
46 #define CONST_MSS_V4 (CONST_MTU_TEST - CONST_HDRLEN_V4)
47 #define CONST_MSS_V6 (CONST_MTU_TEST - CONST_HDRLEN_V6)
49 #define CONST_MAX_SEGS_V4 (ETH_MAX_MTU / CONST_MSS_V4)
50 #define CONST_MAX_SEGS_V6 (ETH_MAX_MTU / CONST_MSS_V6)
52 static bool cfg_do_ipv4;
53 static bool cfg_do_ipv6;
54 static bool cfg_do_connected;
55 static bool cfg_do_connectionless;
56 static bool cfg_do_msgmore;
57 static bool cfg_do_setsockopt;
58 static int cfg_specific_test_id = -1;
60 static const char cfg_ifname[] = "lo";
61 static unsigned short cfg_port = 9000;
63 static char buf[ETH_MAX_MTU];
65 struct testcase {
66 int tlen; /* send() buffer size, may exceed mss */
67 bool tfail; /* send() call is expected to fail */
68 int gso_len; /* mss after applying gso */
69 int r_num_mss; /* recv(): number of calls of full mss */
70 int r_len_last; /* recv(): size of last non-mss dgram, if any */
73 const struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
74 const struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) };
76 struct testcase testcases_v4[] = {
78 /* no GSO: send a single byte */
79 .tlen = 1,
80 .r_len_last = 1,
83 /* no GSO: send a single MSS */
84 .tlen = CONST_MSS_V4,
85 .r_num_mss = 1,
88 /* no GSO: send a single MSS + 1B: fail */
89 .tlen = CONST_MSS_V4 + 1,
90 .tfail = true,
93 /* send a single MSS: will fall back to no GSO */
94 .tlen = CONST_MSS_V4,
95 .gso_len = CONST_MSS_V4,
96 .r_num_mss = 1,
99 /* send a single MSS + 1B */
100 .tlen = CONST_MSS_V4 + 1,
101 .gso_len = CONST_MSS_V4,
102 .r_num_mss = 1,
103 .r_len_last = 1,
106 /* send exactly 2 MSS */
107 .tlen = CONST_MSS_V4 * 2,
108 .gso_len = CONST_MSS_V4,
109 .r_num_mss = 2,
112 /* send 2 MSS + 1B */
113 .tlen = (CONST_MSS_V4 * 2) + 1,
114 .gso_len = CONST_MSS_V4,
115 .r_num_mss = 2,
116 .r_len_last = 1,
119 /* send MAX segs */
120 .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
121 .gso_len = CONST_MSS_V4,
122 .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
126 /* send MAX bytes */
127 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
128 .gso_len = CONST_MSS_V4,
129 .r_num_mss = CONST_MAX_SEGS_V4,
130 .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
131 (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
134 /* send MAX + 1: fail */
135 .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
136 .gso_len = CONST_MSS_V4,
137 .tfail = true,
140 /* send a single 1B MSS: will fall back to no GSO */
141 .tlen = 1,
142 .gso_len = 1,
143 .r_num_mss = 1,
146 /* send 2 1B segments */
147 .tlen = 2,
148 .gso_len = 1,
149 .r_num_mss = 2,
152 /* send 2B + 2B + 1B segments */
153 .tlen = 5,
154 .gso_len = 2,
155 .r_num_mss = 2,
156 .r_len_last = 1,
159 /* send max number of min sized segments */
160 .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4,
161 .gso_len = 1,
162 .r_num_mss = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4,
165 /* send max number + 1 of min sized segments: fail */
166 .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V4 + 1,
167 .gso_len = 1,
168 .tfail = true,
171 /* EOL */
175 #ifndef IP6_MAX_MTU
176 #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
177 #endif
179 struct testcase testcases_v6[] = {
181 /* no GSO: send a single byte */
182 .tlen = 1,
183 .r_len_last = 1,
186 /* no GSO: send a single MSS */
187 .tlen = CONST_MSS_V6,
188 .r_num_mss = 1,
191 /* no GSO: send a single MSS + 1B: fail */
192 .tlen = CONST_MSS_V6 + 1,
193 .tfail = true,
196 /* send a single MSS: will fall back to no GSO */
197 .tlen = CONST_MSS_V6,
198 .gso_len = CONST_MSS_V6,
199 .r_num_mss = 1,
202 /* send a single MSS + 1B */
203 .tlen = CONST_MSS_V6 + 1,
204 .gso_len = CONST_MSS_V6,
205 .r_num_mss = 1,
206 .r_len_last = 1,
209 /* send exactly 2 MSS */
210 .tlen = CONST_MSS_V6 * 2,
211 .gso_len = CONST_MSS_V6,
212 .r_num_mss = 2,
215 /* send 2 MSS + 1B */
216 .tlen = (CONST_MSS_V6 * 2) + 1,
217 .gso_len = CONST_MSS_V6,
218 .r_num_mss = 2,
219 .r_len_last = 1,
222 /* send MAX segs */
223 .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
224 .gso_len = CONST_MSS_V6,
225 .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
229 /* send MAX bytes */
230 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
231 .gso_len = CONST_MSS_V6,
232 .r_num_mss = CONST_MAX_SEGS_V6,
233 .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
234 (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
237 /* send MAX + 1: fail */
238 .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
239 .gso_len = CONST_MSS_V6,
240 .tfail = true,
243 /* send a single 1B MSS: will fall back to no GSO */
244 .tlen = 1,
245 .gso_len = 1,
246 .r_num_mss = 1,
249 /* send 2 1B segments */
250 .tlen = 2,
251 .gso_len = 1,
252 .r_num_mss = 2,
255 /* send 2B + 2B + 1B segments */
256 .tlen = 5,
257 .gso_len = 2,
258 .r_num_mss = 2,
259 .r_len_last = 1,
262 /* send max number of min sized segments */
263 .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6,
264 .gso_len = 1,
265 .r_num_mss = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6,
268 /* send max number + 1 of min sized segments: fail */
269 .tlen = UDP_MAX_SEGMENTS - CONST_HDRLEN_V6 + 1,
270 .gso_len = 1,
271 .tfail = true,
274 /* EOL */
278 static unsigned int get_device_mtu(int fd, const char *ifname)
280 struct ifreq ifr;
282 memset(&ifr, 0, sizeof(ifr));
284 strcpy(ifr.ifr_name, ifname);
286 if (ioctl(fd, SIOCGIFMTU, &ifr))
287 error(1, errno, "ioctl get mtu");
289 return ifr.ifr_mtu;
292 static void __set_device_mtu(int fd, const char *ifname, unsigned int mtu)
294 struct ifreq ifr;
296 memset(&ifr, 0, sizeof(ifr));
298 ifr.ifr_mtu = mtu;
299 strcpy(ifr.ifr_name, ifname);
301 if (ioctl(fd, SIOCSIFMTU, &ifr))
302 error(1, errno, "ioctl set mtu");
305 static void set_device_mtu(int fd, int mtu)
307 int val;
309 val = get_device_mtu(fd, cfg_ifname);
310 fprintf(stderr, "device mtu (orig): %u\n", val);
312 __set_device_mtu(fd, cfg_ifname, mtu);
313 val = get_device_mtu(fd, cfg_ifname);
314 if (val != mtu)
315 error(1, 0, "unable to set device mtu to %u\n", val);
317 fprintf(stderr, "device mtu (test): %u\n", val);
320 static void set_pmtu_discover(int fd, bool is_ipv4)
322 int level, name, val;
324 if (is_ipv4) {
325 level = SOL_IP;
326 name = IP_MTU_DISCOVER;
327 val = IP_PMTUDISC_DO;
328 } else {
329 level = SOL_IPV6;
330 name = IPV6_MTU_DISCOVER;
331 val = IPV6_PMTUDISC_DO;
334 if (setsockopt(fd, level, name, &val, sizeof(val)))
335 error(1, errno, "setsockopt path mtu");
338 static unsigned int get_path_mtu(int fd, bool is_ipv4)
340 socklen_t vallen;
341 unsigned int mtu;
342 int ret;
344 vallen = sizeof(mtu);
345 if (is_ipv4)
346 ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
347 else
348 ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
350 if (ret)
351 error(1, errno, "getsockopt mtu");
354 fprintf(stderr, "path mtu (read): %u\n", mtu);
355 return mtu;
358 /* very wordy version of system("ip route add dev lo mtu 1500 127.0.0.3/32") */
359 static void set_route_mtu(int mtu, bool is_ipv4)
361 struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
362 struct nlmsghdr *nh;
363 struct rtattr *rta;
364 struct rtmsg *rt;
365 char data[NLMSG_ALIGN(sizeof(*nh)) +
366 NLMSG_ALIGN(sizeof(*rt)) +
367 NLMSG_ALIGN(RTA_LENGTH(sizeof(addr6))) +
368 NLMSG_ALIGN(RTA_LENGTH(sizeof(int))) +
369 NLMSG_ALIGN(RTA_LENGTH(0) + RTA_LENGTH(sizeof(int)))];
370 int fd, ret, alen, off = 0;
372 alen = is_ipv4 ? sizeof(addr4) : sizeof(addr6);
374 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
375 if (fd == -1)
376 error(1, errno, "socket netlink");
378 memset(data, 0, sizeof(data));
380 nh = (void *)data;
381 nh->nlmsg_type = RTM_NEWROUTE;
382 nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
383 off += NLMSG_ALIGN(sizeof(*nh));
385 rt = (void *)(data + off);
386 rt->rtm_family = is_ipv4 ? AF_INET : AF_INET6;
387 rt->rtm_table = RT_TABLE_MAIN;
388 rt->rtm_dst_len = alen << 3;
389 rt->rtm_protocol = RTPROT_BOOT;
390 rt->rtm_scope = RT_SCOPE_UNIVERSE;
391 rt->rtm_type = RTN_UNICAST;
392 off += NLMSG_ALIGN(sizeof(*rt));
394 rta = (void *)(data + off);
395 rta->rta_type = RTA_DST;
396 rta->rta_len = RTA_LENGTH(alen);
397 if (is_ipv4)
398 memcpy(RTA_DATA(rta), &addr4, alen);
399 else
400 memcpy(RTA_DATA(rta), &addr6, alen);
401 off += NLMSG_ALIGN(rta->rta_len);
403 rta = (void *)(data + off);
404 rta->rta_type = RTA_OIF;
405 rta->rta_len = RTA_LENGTH(sizeof(int));
406 *((int *)(RTA_DATA(rta))) = 1; //if_nametoindex("lo");
407 off += NLMSG_ALIGN(rta->rta_len);
409 /* MTU is a subtype in a metrics type */
410 rta = (void *)(data + off);
411 rta->rta_type = RTA_METRICS;
412 rta->rta_len = RTA_LENGTH(0) + RTA_LENGTH(sizeof(int));
413 off += NLMSG_ALIGN(rta->rta_len);
415 /* now fill MTU subtype. Note that it fits within above rta_len */
416 rta = (void *)(((char *) rta) + RTA_LENGTH(0));
417 rta->rta_type = RTAX_MTU;
418 rta->rta_len = RTA_LENGTH(sizeof(int));
419 *((int *)(RTA_DATA(rta))) = mtu;
421 nh->nlmsg_len = off;
423 ret = sendto(fd, data, off, 0, (void *)&nladdr, sizeof(nladdr));
424 if (ret != off)
425 error(1, errno, "send netlink: %uB != %uB\n", ret, off);
427 if (close(fd))
428 error(1, errno, "close netlink");
430 fprintf(stderr, "route mtu (test): %u\n", mtu);
433 static bool __send_one(int fd, struct msghdr *msg, int flags)
435 int ret;
437 ret = sendmsg(fd, msg, flags);
438 if (ret == -1 &&
439 (errno == EMSGSIZE || errno == ENOMEM || errno == EINVAL))
440 return false;
441 if (ret == -1)
442 error(1, errno, "sendmsg");
443 if (ret != msg->msg_iov->iov_len)
444 error(1, 0, "sendto: %d != %lu", ret, msg->msg_iov->iov_len);
445 if (msg->msg_flags)
446 error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
448 return true;
451 static bool send_one(int fd, int len, int gso_len,
452 struct sockaddr *addr, socklen_t alen)
454 char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
455 struct msghdr msg = {0};
456 struct iovec iov = {0};
457 struct cmsghdr *cm;
459 iov.iov_base = buf;
460 iov.iov_len = len;
462 msg.msg_iov = &iov;
463 msg.msg_iovlen = 1;
465 msg.msg_name = addr;
466 msg.msg_namelen = alen;
468 if (gso_len && !cfg_do_setsockopt) {
469 msg.msg_control = control;
470 msg.msg_controllen = sizeof(control);
472 cm = CMSG_FIRSTHDR(&msg);
473 cm->cmsg_level = SOL_UDP;
474 cm->cmsg_type = UDP_SEGMENT;
475 cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
476 *((uint16_t *) CMSG_DATA(cm)) = gso_len;
479 /* If MSG_MORE, send 1 byte followed by remainder */
480 if (cfg_do_msgmore && len > 1) {
481 iov.iov_len = 1;
482 if (!__send_one(fd, &msg, MSG_MORE))
483 error(1, 0, "send 1B failed");
485 iov.iov_base++;
486 iov.iov_len = len - 1;
489 return __send_one(fd, &msg, 0);
492 static int recv_one(int fd, int flags)
494 int ret;
496 ret = recv(fd, buf, sizeof(buf), flags);
497 if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
498 return 0;
499 if (ret == -1)
500 error(1, errno, "recv");
502 return ret;
505 static void run_one(struct testcase *test, int fdt, int fdr,
506 struct sockaddr *addr, socklen_t alen)
508 int i, ret, val, mss;
509 bool sent;
511 fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
512 addr->sa_family == AF_INET ? 4 : 6,
513 test->tlen, test->gso_len,
514 test->tfail ? "(fail)" : "");
516 val = test->gso_len;
517 if (cfg_do_setsockopt) {
518 if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
519 error(1, errno, "setsockopt udp segment");
522 sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
523 if (sent && test->tfail)
524 error(1, 0, "send succeeded while expecting failure");
525 if (!sent && !test->tfail)
526 error(1, 0, "send failed while expecting success");
527 if (!sent)
528 return;
530 if (test->gso_len)
531 mss = test->gso_len;
532 else
533 mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
536 /* Recv all full MSS datagrams */
537 for (i = 0; i < test->r_num_mss; i++) {
538 ret = recv_one(fdr, 0);
539 if (ret != mss)
540 error(1, 0, "recv.%d: %d != %d", i, ret, mss);
543 /* Recv the non-full last datagram, if tlen was not a multiple of mss */
544 if (test->r_len_last) {
545 ret = recv_one(fdr, 0);
546 if (ret != test->r_len_last)
547 error(1, 0, "recv.%d: %d != %d (last)",
548 i, ret, test->r_len_last);
551 /* Verify received all data */
552 ret = recv_one(fdr, MSG_DONTWAIT);
553 if (ret)
554 error(1, 0, "recv: unexpected datagram");
557 static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
559 struct testcase *tests, *test;
561 tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
563 for (test = tests; test->tlen; test++) {
564 /* if a specific test is given, then skip all others */
565 if (cfg_specific_test_id == -1 ||
566 cfg_specific_test_id == test - tests)
567 run_one(test, fdt, fdr, addr, alen);
571 static void run_test(struct sockaddr *addr, socklen_t alen)
573 struct timeval tv = { .tv_usec = 100 * 1000 };
574 int fdr, fdt, val;
576 fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
577 if (fdr == -1)
578 error(1, errno, "socket r");
580 if (bind(fdr, addr, alen))
581 error(1, errno, "bind");
583 /* Have tests fail quickly instead of hang */
584 if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
585 error(1, errno, "setsockopt rcv timeout");
587 fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
588 if (fdt == -1)
589 error(1, errno, "socket t");
591 /* Do not fragment these datagrams: only succeed if GSO works */
592 set_pmtu_discover(fdt, addr->sa_family == AF_INET);
594 if (cfg_do_connectionless) {
595 set_device_mtu(fdt, CONST_MTU_TEST);
596 run_all(fdt, fdr, addr, alen);
599 if (cfg_do_connected) {
600 set_device_mtu(fdt, CONST_MTU_TEST + 100);
601 set_route_mtu(CONST_MTU_TEST, addr->sa_family == AF_INET);
603 if (connect(fdt, addr, alen))
604 error(1, errno, "connect");
606 val = get_path_mtu(fdt, addr->sa_family == AF_INET);
607 if (val != CONST_MTU_TEST)
608 error(1, 0, "bad path mtu %u\n", val);
610 run_all(fdt, fdr, addr, 0 /* use connected addr */);
613 if (close(fdt))
614 error(1, errno, "close t");
615 if (close(fdr))
616 error(1, errno, "close r");
619 static void run_test_v4(void)
621 struct sockaddr_in addr = {0};
623 addr.sin_family = AF_INET;
624 addr.sin_port = htons(cfg_port);
625 addr.sin_addr = addr4;
627 run_test((void *)&addr, sizeof(addr));
630 static void run_test_v6(void)
632 struct sockaddr_in6 addr = {0};
634 addr.sin6_family = AF_INET6;
635 addr.sin6_port = htons(cfg_port);
636 addr.sin6_addr = addr6;
638 run_test((void *)&addr, sizeof(addr));
641 static void parse_opts(int argc, char **argv)
643 int c;
645 while ((c = getopt(argc, argv, "46cCmst:")) != -1) {
646 switch (c) {
647 case '4':
648 cfg_do_ipv4 = true;
649 break;
650 case '6':
651 cfg_do_ipv6 = true;
652 break;
653 case 'c':
654 cfg_do_connected = true;
655 break;
656 case 'C':
657 cfg_do_connectionless = true;
658 break;
659 case 'm':
660 cfg_do_msgmore = true;
661 break;
662 case 's':
663 cfg_do_setsockopt = true;
664 break;
665 case 't':
666 cfg_specific_test_id = strtoul(optarg, NULL, 0);
667 break;
668 default:
669 error(1, 0, "%s: parse error", argv[0]);
674 int main(int argc, char **argv)
676 parse_opts(argc, argv);
678 if (cfg_do_ipv4)
679 run_test_v4();
680 if (cfg_do_ipv6)
681 run_test_v6();
683 fprintf(stderr, "OK\n");
684 return 0;