udpgso.c 13 KB

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