udpgso.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <stddef.h>
  4. #include <arpa/inet.h>
  5. #include <error.h>
  6. #include <errno.h>
  7. #include <net/if.h>
  8. #include <linux/in.h>
  9. #include <linux/netlink.h>
  10. #include <linux/rtnetlink.h>
  11. #include <netinet/if_ether.h>
  12. #include <netinet/ip.h>
  13. #include <netinet/ip6.h>
  14. #include <netinet/udp.h>
  15. #include <stdbool.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/socket.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #ifndef ETH_MAX_MTU
  27. #define ETH_MAX_MTU 0xFFFFU
  28. #endif
  29. #ifndef UDP_SEGMENT
  30. #define UDP_SEGMENT 103
  31. #endif
  32. #define CONST_MTU_TEST 1500
  33. #define CONST_HDRLEN_V4 (sizeof(struct iphdr) + sizeof(struct udphdr))
  34. #define CONST_HDRLEN_V6 (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
  35. #define CONST_MSS_V4 (CONST_MTU_TEST - CONST_HDRLEN_V4)
  36. #define CONST_MSS_V6 (CONST_MTU_TEST - CONST_HDRLEN_V6)
  37. #define CONST_MAX_SEGS_V4 (ETH_MAX_MTU / CONST_MSS_V4)
  38. #define CONST_MAX_SEGS_V6 (ETH_MAX_MTU / CONST_MSS_V6)
  39. static bool cfg_do_ipv4;
  40. static bool cfg_do_ipv6;
  41. static bool cfg_do_connected;
  42. static bool cfg_do_connectionless;
  43. static bool cfg_do_msgmore;
  44. static bool cfg_do_setsockopt;
  45. static int cfg_specific_test_id = -1;
  46. static const char cfg_ifname[] = "lo";
  47. static unsigned short cfg_port = 9000;
  48. static char buf[ETH_MAX_MTU];
  49. struct testcase {
  50. int tlen; /* send() buffer size, may exceed mss */
  51. bool tfail; /* send() call is expected to fail */
  52. int gso_len; /* mss after applying gso */
  53. int r_num_mss; /* recv(): number of calls of full mss */
  54. int r_len_last; /* recv(): size of last non-mss dgram, if any */
  55. };
  56. const struct in6_addr addr6 = IN6ADDR_LOOPBACK_INIT;
  57. const struct in_addr addr4 = { .s_addr = __constant_htonl(INADDR_LOOPBACK + 2) };
  58. struct testcase testcases_v4[] = {
  59. {
  60. /* no GSO: send a single byte */
  61. .tlen = 1,
  62. .r_len_last = 1,
  63. },
  64. {
  65. /* no GSO: send a single MSS */
  66. .tlen = CONST_MSS_V4,
  67. .r_num_mss = 1,
  68. },
  69. {
  70. /* no GSO: send a single MSS + 1B: fail */
  71. .tlen = CONST_MSS_V4 + 1,
  72. .tfail = true,
  73. },
  74. {
  75. /* send a single MSS: will fail with GSO, because the segment
  76. * logic in udp4_ufo_fragment demands a gso skb to be > MTU
  77. */
  78. .tlen = CONST_MSS_V4,
  79. .gso_len = CONST_MSS_V4,
  80. .tfail = true,
  81. .r_num_mss = 1,
  82. },
  83. {
  84. /* send a single MSS + 1B */
  85. .tlen = CONST_MSS_V4 + 1,
  86. .gso_len = CONST_MSS_V4,
  87. .r_num_mss = 1,
  88. .r_len_last = 1,
  89. },
  90. {
  91. /* send exactly 2 MSS */
  92. .tlen = CONST_MSS_V4 * 2,
  93. .gso_len = CONST_MSS_V4,
  94. .r_num_mss = 2,
  95. },
  96. {
  97. /* send 2 MSS + 1B */
  98. .tlen = (CONST_MSS_V4 * 2) + 1,
  99. .gso_len = CONST_MSS_V4,
  100. .r_num_mss = 2,
  101. .r_len_last = 1,
  102. },
  103. {
  104. /* send MAX segs */
  105. .tlen = (ETH_MAX_MTU / CONST_MSS_V4) * CONST_MSS_V4,
  106. .gso_len = CONST_MSS_V4,
  107. .r_num_mss = (ETH_MAX_MTU / CONST_MSS_V4),
  108. },
  109. {
  110. /* send MAX bytes */
  111. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4,
  112. .gso_len = CONST_MSS_V4,
  113. .r_num_mss = CONST_MAX_SEGS_V4,
  114. .r_len_last = ETH_MAX_MTU - CONST_HDRLEN_V4 -
  115. (CONST_MAX_SEGS_V4 * CONST_MSS_V4),
  116. },
  117. {
  118. /* send MAX + 1: fail */
  119. .tlen = ETH_MAX_MTU - CONST_HDRLEN_V4 + 1,
  120. .gso_len = CONST_MSS_V4,
  121. .tfail = true,
  122. },
  123. {
  124. /* EOL */
  125. }
  126. };
  127. #ifndef IP6_MAX_MTU
  128. #define IP6_MAX_MTU (ETH_MAX_MTU + sizeof(struct ip6_hdr))
  129. #endif
  130. struct testcase testcases_v6[] = {
  131. {
  132. /* no GSO: send a single byte */
  133. .tlen = 1,
  134. .r_len_last = 1,
  135. },
  136. {
  137. /* no GSO: send a single MSS */
  138. .tlen = CONST_MSS_V6,
  139. .r_num_mss = 1,
  140. },
  141. {
  142. /* no GSO: send a single MSS + 1B: fail */
  143. .tlen = CONST_MSS_V6 + 1,
  144. .tfail = true,
  145. },
  146. {
  147. /* send a single MSS: will fail with GSO, because the segment
  148. * logic in udp4_ufo_fragment demands a gso skb to be > MTU
  149. */
  150. .tlen = CONST_MSS_V6,
  151. .gso_len = CONST_MSS_V6,
  152. .tfail = true,
  153. .r_num_mss = 1,
  154. },
  155. {
  156. /* send a single MSS + 1B */
  157. .tlen = CONST_MSS_V6 + 1,
  158. .gso_len = CONST_MSS_V6,
  159. .r_num_mss = 1,
  160. .r_len_last = 1,
  161. },
  162. {
  163. /* send exactly 2 MSS */
  164. .tlen = CONST_MSS_V6 * 2,
  165. .gso_len = CONST_MSS_V6,
  166. .r_num_mss = 2,
  167. },
  168. {
  169. /* send 2 MSS + 1B */
  170. .tlen = (CONST_MSS_V6 * 2) + 1,
  171. .gso_len = CONST_MSS_V6,
  172. .r_num_mss = 2,
  173. .r_len_last = 1,
  174. },
  175. {
  176. /* send MAX segs */
  177. .tlen = (IP6_MAX_MTU / CONST_MSS_V6) * CONST_MSS_V6,
  178. .gso_len = CONST_MSS_V6,
  179. .r_num_mss = (IP6_MAX_MTU / CONST_MSS_V6),
  180. },
  181. {
  182. /* send MAX bytes */
  183. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6,
  184. .gso_len = CONST_MSS_V6,
  185. .r_num_mss = CONST_MAX_SEGS_V6,
  186. .r_len_last = IP6_MAX_MTU - CONST_HDRLEN_V6 -
  187. (CONST_MAX_SEGS_V6 * CONST_MSS_V6),
  188. },
  189. {
  190. /* send MAX + 1: fail */
  191. .tlen = IP6_MAX_MTU - CONST_HDRLEN_V6 + 1,
  192. .gso_len = CONST_MSS_V6,
  193. .tfail = true,
  194. },
  195. {
  196. /* EOL */
  197. }
  198. };
  199. static unsigned int get_device_mtu(int fd, const char *ifname)
  200. {
  201. struct ifreq ifr;
  202. memset(&ifr, 0, sizeof(ifr));
  203. strcpy(ifr.ifr_name, ifname);
  204. if (ioctl(fd, SIOCGIFMTU, &ifr))
  205. error(1, errno, "ioctl get mtu");
  206. return ifr.ifr_mtu;
  207. }
  208. static void __set_device_mtu(int fd, const char *ifname, unsigned int mtu)
  209. {
  210. struct ifreq ifr;
  211. memset(&ifr, 0, sizeof(ifr));
  212. ifr.ifr_mtu = mtu;
  213. strcpy(ifr.ifr_name, ifname);
  214. if (ioctl(fd, SIOCSIFMTU, &ifr))
  215. error(1, errno, "ioctl set mtu");
  216. }
  217. static void set_device_mtu(int fd, int mtu)
  218. {
  219. int val;
  220. val = get_device_mtu(fd, cfg_ifname);
  221. fprintf(stderr, "device mtu (orig): %u\n", val);
  222. __set_device_mtu(fd, cfg_ifname, mtu);
  223. val = get_device_mtu(fd, cfg_ifname);
  224. if (val != mtu)
  225. error(1, 0, "unable to set device mtu to %u\n", val);
  226. fprintf(stderr, "device mtu (test): %u\n", val);
  227. }
  228. static void set_pmtu_discover(int fd, bool is_ipv4)
  229. {
  230. int level, name, val;
  231. if (is_ipv4) {
  232. level = SOL_IP;
  233. name = IP_MTU_DISCOVER;
  234. val = IP_PMTUDISC_DO;
  235. } else {
  236. level = SOL_IPV6;
  237. name = IPV6_MTU_DISCOVER;
  238. val = IPV6_PMTUDISC_DO;
  239. }
  240. if (setsockopt(fd, level, name, &val, sizeof(val)))
  241. error(1, errno, "setsockopt path mtu");
  242. }
  243. static unsigned int get_path_mtu(int fd, bool is_ipv4)
  244. {
  245. socklen_t vallen;
  246. unsigned int mtu;
  247. int ret;
  248. vallen = sizeof(mtu);
  249. if (is_ipv4)
  250. ret = getsockopt(fd, SOL_IP, IP_MTU, &mtu, &vallen);
  251. else
  252. ret = getsockopt(fd, SOL_IPV6, IPV6_MTU, &mtu, &vallen);
  253. if (ret)
  254. error(1, errno, "getsockopt mtu");
  255. fprintf(stderr, "path mtu (read): %u\n", mtu);
  256. return mtu;
  257. }
  258. /* very wordy version of system("ip route add dev lo mtu 1500 127.0.0.3/32") */
  259. static void set_route_mtu(int mtu, bool is_ipv4)
  260. {
  261. struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
  262. struct nlmsghdr *nh;
  263. struct rtattr *rta;
  264. struct rtmsg *rt;
  265. char data[NLMSG_ALIGN(sizeof(*nh)) +
  266. NLMSG_ALIGN(sizeof(*rt)) +
  267. NLMSG_ALIGN(RTA_LENGTH(sizeof(addr6))) +
  268. NLMSG_ALIGN(RTA_LENGTH(sizeof(int))) +
  269. NLMSG_ALIGN(RTA_LENGTH(0) + RTA_LENGTH(sizeof(int)))];
  270. int fd, ret, alen, off = 0;
  271. alen = is_ipv4 ? sizeof(addr4) : sizeof(addr6);
  272. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  273. if (fd == -1)
  274. error(1, errno, "socket netlink");
  275. memset(data, 0, sizeof(data));
  276. nh = (void *)data;
  277. nh->nlmsg_type = RTM_NEWROUTE;
  278. nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE;
  279. off += NLMSG_ALIGN(sizeof(*nh));
  280. rt = (void *)(data + off);
  281. rt->rtm_family = is_ipv4 ? AF_INET : AF_INET6;
  282. rt->rtm_table = RT_TABLE_MAIN;
  283. rt->rtm_dst_len = alen << 3;
  284. rt->rtm_protocol = RTPROT_BOOT;
  285. rt->rtm_scope = RT_SCOPE_UNIVERSE;
  286. rt->rtm_type = RTN_UNICAST;
  287. off += NLMSG_ALIGN(sizeof(*rt));
  288. rta = (void *)(data + off);
  289. rta->rta_type = RTA_DST;
  290. rta->rta_len = RTA_LENGTH(alen);
  291. if (is_ipv4)
  292. memcpy(RTA_DATA(rta), &addr4, alen);
  293. else
  294. memcpy(RTA_DATA(rta), &addr6, alen);
  295. off += NLMSG_ALIGN(rta->rta_len);
  296. rta = (void *)(data + off);
  297. rta->rta_type = RTA_OIF;
  298. rta->rta_len = RTA_LENGTH(sizeof(int));
  299. *((int *)(RTA_DATA(rta))) = 1; //if_nametoindex("lo");
  300. off += NLMSG_ALIGN(rta->rta_len);
  301. /* MTU is a subtype in a metrics type */
  302. rta = (void *)(data + off);
  303. rta->rta_type = RTA_METRICS;
  304. rta->rta_len = RTA_LENGTH(0) + RTA_LENGTH(sizeof(int));
  305. off += NLMSG_ALIGN(rta->rta_len);
  306. /* now fill MTU subtype. Note that it fits within above rta_len */
  307. rta = (void *)(((char *) rta) + RTA_LENGTH(0));
  308. rta->rta_type = RTAX_MTU;
  309. rta->rta_len = RTA_LENGTH(sizeof(int));
  310. *((int *)(RTA_DATA(rta))) = mtu;
  311. nh->nlmsg_len = off;
  312. ret = sendto(fd, data, off, 0, (void *)&nladdr, sizeof(nladdr));
  313. if (ret != off)
  314. error(1, errno, "send netlink: %uB != %uB\n", ret, off);
  315. if (close(fd))
  316. error(1, errno, "close netlink");
  317. fprintf(stderr, "route mtu (test): %u\n", mtu);
  318. }
  319. static bool __send_one(int fd, struct msghdr *msg, int flags)
  320. {
  321. int ret;
  322. ret = sendmsg(fd, msg, flags);
  323. if (ret == -1 && (errno == EMSGSIZE || errno == ENOMEM))
  324. return false;
  325. if (ret == -1)
  326. error(1, errno, "sendmsg");
  327. if (ret != msg->msg_iov->iov_len)
  328. error(1, 0, "sendto: %d != %lu", ret, msg->msg_iov->iov_len);
  329. if (msg->msg_flags)
  330. error(1, 0, "sendmsg: return flags 0x%x\n", msg->msg_flags);
  331. return true;
  332. }
  333. static bool send_one(int fd, int len, int gso_len,
  334. struct sockaddr *addr, socklen_t alen)
  335. {
  336. char control[CMSG_SPACE(sizeof(uint16_t))] = {0};
  337. struct msghdr msg = {0};
  338. struct iovec iov = {0};
  339. struct cmsghdr *cm;
  340. iov.iov_base = buf;
  341. iov.iov_len = len;
  342. msg.msg_iov = &iov;
  343. msg.msg_iovlen = 1;
  344. msg.msg_name = addr;
  345. msg.msg_namelen = alen;
  346. if (gso_len && !cfg_do_setsockopt) {
  347. msg.msg_control = control;
  348. msg.msg_controllen = sizeof(control);
  349. cm = CMSG_FIRSTHDR(&msg);
  350. cm->cmsg_level = SOL_UDP;
  351. cm->cmsg_type = UDP_SEGMENT;
  352. cm->cmsg_len = CMSG_LEN(sizeof(uint16_t));
  353. *((uint16_t *) CMSG_DATA(cm)) = gso_len;
  354. }
  355. /* If MSG_MORE, send 1 byte followed by remainder */
  356. if (cfg_do_msgmore && len > 1) {
  357. iov.iov_len = 1;
  358. if (!__send_one(fd, &msg, MSG_MORE))
  359. error(1, 0, "send 1B failed");
  360. iov.iov_base++;
  361. iov.iov_len = len - 1;
  362. }
  363. return __send_one(fd, &msg, 0);
  364. }
  365. static int recv_one(int fd, int flags)
  366. {
  367. int ret;
  368. ret = recv(fd, buf, sizeof(buf), flags);
  369. if (ret == -1 && errno == EAGAIN && (flags & MSG_DONTWAIT))
  370. return 0;
  371. if (ret == -1)
  372. error(1, errno, "recv");
  373. return ret;
  374. }
  375. static void run_one(struct testcase *test, int fdt, int fdr,
  376. struct sockaddr *addr, socklen_t alen)
  377. {
  378. int i, ret, val, mss;
  379. bool sent;
  380. fprintf(stderr, "ipv%d tx:%d gso:%d %s\n",
  381. addr->sa_family == AF_INET ? 4 : 6,
  382. test->tlen, test->gso_len,
  383. test->tfail ? "(fail)" : "");
  384. val = test->gso_len;
  385. if (cfg_do_setsockopt) {
  386. if (setsockopt(fdt, SOL_UDP, UDP_SEGMENT, &val, sizeof(val)))
  387. error(1, errno, "setsockopt udp segment");
  388. }
  389. sent = send_one(fdt, test->tlen, test->gso_len, addr, alen);
  390. if (sent && test->tfail)
  391. error(1, 0, "send succeeded while expecting failure");
  392. if (!sent && !test->tfail)
  393. error(1, 0, "send failed while expecting success");
  394. if (!sent)
  395. return;
  396. mss = addr->sa_family == AF_INET ? CONST_MSS_V4 : CONST_MSS_V6;
  397. /* Recv all full MSS datagrams */
  398. for (i = 0; i < test->r_num_mss; i++) {
  399. ret = recv_one(fdr, 0);
  400. if (ret != mss)
  401. error(1, 0, "recv.%d: %d != %d", i, ret, mss);
  402. }
  403. /* Recv the non-full last datagram, if tlen was not a multiple of mss */
  404. if (test->r_len_last) {
  405. ret = recv_one(fdr, 0);
  406. if (ret != test->r_len_last)
  407. error(1, 0, "recv.%d: %d != %d (last)",
  408. i, ret, test->r_len_last);
  409. }
  410. /* Verify received all data */
  411. ret = recv_one(fdr, MSG_DONTWAIT);
  412. if (ret)
  413. error(1, 0, "recv: unexpected datagram");
  414. }
  415. static void run_all(int fdt, int fdr, struct sockaddr *addr, socklen_t alen)
  416. {
  417. struct testcase *tests, *test;
  418. tests = addr->sa_family == AF_INET ? testcases_v4 : testcases_v6;
  419. for (test = tests; test->tlen; test++) {
  420. /* if a specific test is given, then skip all others */
  421. if (cfg_specific_test_id == -1 ||
  422. cfg_specific_test_id == test - tests)
  423. run_one(test, fdt, fdr, addr, alen);
  424. }
  425. }
  426. static void run_test(struct sockaddr *addr, socklen_t alen)
  427. {
  428. struct timeval tv = { .tv_usec = 100 * 1000 };
  429. int fdr, fdt, val;
  430. fdr = socket(addr->sa_family, SOCK_DGRAM, 0);
  431. if (fdr == -1)
  432. error(1, errno, "socket r");
  433. if (bind(fdr, addr, alen))
  434. error(1, errno, "bind");
  435. /* Have tests fail quickly instead of hang */
  436. if (setsockopt(fdr, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)))
  437. error(1, errno, "setsockopt rcv timeout");
  438. fdt = socket(addr->sa_family, SOCK_DGRAM, 0);
  439. if (fdt == -1)
  440. error(1, errno, "socket t");
  441. /* Do not fragment these datagrams: only succeed if GSO works */
  442. set_pmtu_discover(fdt, addr->sa_family == AF_INET);
  443. if (cfg_do_connectionless) {
  444. set_device_mtu(fdt, CONST_MTU_TEST);
  445. run_all(fdt, fdr, addr, alen);
  446. }
  447. if (cfg_do_connected) {
  448. set_device_mtu(fdt, CONST_MTU_TEST + 100);
  449. set_route_mtu(CONST_MTU_TEST, addr->sa_family == AF_INET);
  450. if (connect(fdt, addr, alen))
  451. error(1, errno, "connect");
  452. val = get_path_mtu(fdt, addr->sa_family == AF_INET);
  453. if (val != CONST_MTU_TEST)
  454. error(1, 0, "bad path mtu %u\n", val);
  455. run_all(fdt, fdr, addr, 0 /* use connected addr */);
  456. }
  457. if (close(fdt))
  458. error(1, errno, "close t");
  459. if (close(fdr))
  460. error(1, errno, "close r");
  461. }
  462. static void run_test_v4(void)
  463. {
  464. struct sockaddr_in addr = {0};
  465. addr.sin_family = AF_INET;
  466. addr.sin_port = htons(cfg_port);
  467. addr.sin_addr = addr4;
  468. run_test((void *)&addr, sizeof(addr));
  469. }
  470. static void run_test_v6(void)
  471. {
  472. struct sockaddr_in6 addr = {0};
  473. addr.sin6_family = AF_INET6;
  474. addr.sin6_port = htons(cfg_port);
  475. addr.sin6_addr = addr6;
  476. run_test((void *)&addr, sizeof(addr));
  477. }
  478. static void parse_opts(int argc, char **argv)
  479. {
  480. int c;
  481. while ((c = getopt(argc, argv, "46cCmst:")) != -1) {
  482. switch (c) {
  483. case '4':
  484. cfg_do_ipv4 = true;
  485. break;
  486. case '6':
  487. cfg_do_ipv6 = true;
  488. break;
  489. case 'c':
  490. cfg_do_connected = true;
  491. break;
  492. case 'C':
  493. cfg_do_connectionless = true;
  494. break;
  495. case 'm':
  496. cfg_do_msgmore = true;
  497. break;
  498. case 's':
  499. cfg_do_setsockopt = true;
  500. break;
  501. case 't':
  502. cfg_specific_test_id = strtoul(optarg, NULL, 0);
  503. break;
  504. default:
  505. error(1, 0, "%s: parse error", argv[0]);
  506. }
  507. }
  508. }
  509. int main(int argc, char **argv)
  510. {
  511. parse_opts(argc, argv);
  512. if (cfg_do_ipv4)
  513. run_test_v4();
  514. if (cfg_do_ipv6)
  515. run_test_v6();
  516. fprintf(stderr, "OK\n");
  517. return 0;
  518. }