csum.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Test hardware checksum offload: Rx + Tx, IPv4 + IPv6, TCP + UDP.
  3. *
  4. * The test runs on two machines to exercise the NIC. For this reason it
  5. * is not integrated in kselftests.
  6. *
  7. * CMD=$((./csum -[46] -[tu] -S $SADDR -D $DADDR -[RT] -r 1 $EXTRA_ARGS))
  8. *
  9. * Rx:
  10. *
  11. * The sender sends packets with a known checksum field using PF_INET(6)
  12. * SOCK_RAW sockets.
  13. *
  14. * good packet: $CMD [-t]
  15. * bad packet: $CMD [-t] -E
  16. *
  17. * The receiver reads UDP packets with a UDP socket. This is not an
  18. * option for TCP packets ('-t'). Optionally insert an iptables filter
  19. * to avoid these entering the real protocol stack.
  20. *
  21. * The receiver also reads all packets with a PF_PACKET socket, to
  22. * observe whether both good and bad packets arrive on the host. And to
  23. * read the optional TP_STATUS_CSUM_VALID bit. This requires setting
  24. * option PACKET_AUXDATA, and works only for CHECKSUM_UNNECESSARY.
  25. *
  26. * Tx:
  27. *
  28. * The sender needs to build CHECKSUM_PARTIAL packets to exercise tx
  29. * checksum offload.
  30. *
  31. * The sender can sends packets with a UDP socket.
  32. *
  33. * Optionally crafts a packet that sums up to zero to verify that the
  34. * device writes negative zero 0xFFFF in this case to distinguish from
  35. * 0x0000 (checksum disabled), as required by RFC 768. Hit this case
  36. * by choosing a specific source port.
  37. *
  38. * good packet: $CMD -U
  39. * zero csum: $CMD -U -Z
  40. *
  41. * The sender can also build packets with PF_PACKET with PACKET_VNET_HDR,
  42. * to cover more protocols. PF_PACKET requires passing src and dst mac
  43. * addresses.
  44. *
  45. * good packet: $CMD -s $smac -d $dmac -p [-t]
  46. *
  47. * Argument '-z' sends UDP packets with a 0x000 checksum disabled field,
  48. * to verify that the NIC passes these packets unmodified.
  49. *
  50. * Argument '-e' adds a transport mode encapsulation header between
  51. * network and transport header. This will fail for devices that parse
  52. * headers. Should work on devices that implement protocol agnostic tx
  53. * checksum offload (NETIF_F_HW_CSUM).
  54. *
  55. * Argument '-r $SEED' optionally randomizes header, payload and length
  56. * to increase coverage between packets sent. SEED 1 further chooses a
  57. * different seed for each run (and logs this for reproducibility). It
  58. * is advised to enable this for extra coverage in continuous testing.
  59. */
  60. #define _GNU_SOURCE
  61. #include <arpa/inet.h>
  62. #include <asm/byteorder.h>
  63. #include <errno.h>
  64. #include <error.h>
  65. #include <linux/filter.h>
  66. #include <linux/if_packet.h>
  67. #include <linux/ipv6.h>
  68. #include <linux/virtio_net.h>
  69. #include <net/ethernet.h>
  70. #include <net/if.h>
  71. #include <netinet/if_ether.h>
  72. #include <netinet/in.h>
  73. #include <netinet/ip.h>
  74. #include <netinet/ip6.h>
  75. #include <netinet/tcp.h>
  76. #include <netinet/udp.h>
  77. #include <poll.h>
  78. #include <sched.h>
  79. #include <stdbool.h>
  80. #include <stddef.h>
  81. #include <stdint.h>
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <sys/socket.h>
  86. #include <sys/stat.h>
  87. #include <sys/time.h>
  88. #include <sys/types.h>
  89. #include <unistd.h>
  90. #include "kselftest.h"
  91. static bool cfg_bad_csum;
  92. static int cfg_family = PF_INET6;
  93. static int cfg_num_pkt = 4;
  94. static bool cfg_do_rx = true;
  95. static bool cfg_do_tx = true;
  96. static bool cfg_encap;
  97. static char *cfg_ifname = "eth0";
  98. static char *cfg_mac_dst;
  99. static char *cfg_mac_src;
  100. static int cfg_proto = IPPROTO_UDP;
  101. static int cfg_payload_char = 'a';
  102. static int cfg_payload_len = 100;
  103. static uint16_t cfg_port_dst = 34000;
  104. static uint16_t cfg_port_src = 33000;
  105. static uint16_t cfg_port_src_encap = 33001;
  106. static unsigned int cfg_random_seed;
  107. static int cfg_rcvbuf = 1 << 22; /* be able to queue large cfg_num_pkt */
  108. static bool cfg_send_pfpacket;
  109. static bool cfg_send_udp;
  110. static int cfg_timeout_ms = 2000;
  111. static bool cfg_zero_disable; /* skip checksum: set to zero (udp only) */
  112. static bool cfg_zero_sum; /* create packet that adds up to zero */
  113. static struct sockaddr_in cfg_daddr4 = {.sin_family = AF_INET};
  114. static struct sockaddr_in cfg_saddr4 = {.sin_family = AF_INET};
  115. static struct sockaddr_in6 cfg_daddr6 = {.sin6_family = AF_INET6};
  116. static struct sockaddr_in6 cfg_saddr6 = {.sin6_family = AF_INET6};
  117. #define ENC_HEADER_LEN (sizeof(struct udphdr) + sizeof(struct udp_encap_hdr))
  118. #define MAX_HEADER_LEN (sizeof(struct ipv6hdr) + ENC_HEADER_LEN + sizeof(struct tcphdr))
  119. #define MAX_PAYLOAD_LEN 1024
  120. /* Trivial demo encap. Stand-in for transport layer protocols like ESP or PSP */
  121. struct udp_encap_hdr {
  122. uint8_t nexthdr;
  123. uint8_t padding[3];
  124. };
  125. /* Ipaddrs, for pseudo csum. Global var is ugly, pass through funcs was worse */
  126. static void *iph_addr_p;
  127. static unsigned long gettimeofday_ms(void)
  128. {
  129. struct timeval tv;
  130. gettimeofday(&tv, NULL);
  131. return (tv.tv_sec * 1000UL) + (tv.tv_usec / 1000UL);
  132. }
  133. static uint32_t checksum_nofold(char *data, size_t len, uint32_t sum)
  134. {
  135. uint16_t *words = (uint16_t *)data;
  136. int i;
  137. for (i = 0; i < len / 2; i++)
  138. sum += words[i];
  139. if (len & 1)
  140. sum += ((unsigned char *)data)[len - 1];
  141. return sum;
  142. }
  143. static uint16_t checksum_fold(void *data, size_t len, uint32_t sum)
  144. {
  145. sum = checksum_nofold(data, len, sum);
  146. while (sum > 0xFFFF)
  147. sum = (sum & 0xFFFF) + (sum >> 16);
  148. return ~sum;
  149. }
  150. static uint16_t checksum(void *th, uint16_t proto, size_t len)
  151. {
  152. uint32_t sum;
  153. int alen;
  154. alen = cfg_family == PF_INET6 ? 32 : 8;
  155. sum = checksum_nofold(iph_addr_p, alen, 0);
  156. sum += htons(proto);
  157. sum += htons(len);
  158. /* With CHECKSUM_PARTIAL kernel expects non-inverted pseudo csum */
  159. if (cfg_do_tx && cfg_send_pfpacket)
  160. return ~checksum_fold(NULL, 0, sum);
  161. else
  162. return checksum_fold(th, len, sum);
  163. }
  164. static void *build_packet_ipv4(void *_iph, uint8_t proto, unsigned int len)
  165. {
  166. struct iphdr *iph = _iph;
  167. memset(iph, 0, sizeof(*iph));
  168. iph->version = 4;
  169. iph->ihl = 5;
  170. iph->ttl = 8;
  171. iph->protocol = proto;
  172. iph->saddr = cfg_saddr4.sin_addr.s_addr;
  173. iph->daddr = cfg_daddr4.sin_addr.s_addr;
  174. iph->tot_len = htons(sizeof(*iph) + len);
  175. iph->check = checksum_fold(iph, sizeof(*iph), 0);
  176. iph_addr_p = &iph->saddr;
  177. return iph + 1;
  178. }
  179. static void *build_packet_ipv6(void *_ip6h, uint8_t proto, unsigned int len)
  180. {
  181. struct ipv6hdr *ip6h = _ip6h;
  182. memset(ip6h, 0, sizeof(*ip6h));
  183. ip6h->version = 6;
  184. ip6h->payload_len = htons(len);
  185. ip6h->nexthdr = proto;
  186. ip6h->hop_limit = 64;
  187. ip6h->saddr = cfg_saddr6.sin6_addr;
  188. ip6h->daddr = cfg_daddr6.sin6_addr;
  189. iph_addr_p = &ip6h->saddr;
  190. return ip6h + 1;
  191. }
  192. static void *build_packet_udp(void *_uh)
  193. {
  194. struct udphdr *uh = _uh;
  195. uh->source = htons(cfg_port_src);
  196. uh->dest = htons(cfg_port_dst);
  197. uh->len = htons(sizeof(*uh) + cfg_payload_len);
  198. uh->check = 0;
  199. /* choose source port so that uh->check adds up to zero */
  200. if (cfg_zero_sum) {
  201. uh->source = 0;
  202. uh->source = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len);
  203. fprintf(stderr, "tx: changing sport: %hu -> %hu\n",
  204. cfg_port_src, ntohs(uh->source));
  205. cfg_port_src = ntohs(uh->source);
  206. }
  207. if (cfg_zero_disable)
  208. uh->check = 0;
  209. else
  210. uh->check = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len);
  211. if (cfg_bad_csum)
  212. uh->check = ~uh->check;
  213. fprintf(stderr, "tx: sending checksum: 0x%x\n", uh->check);
  214. return uh + 1;
  215. }
  216. static void *build_packet_tcp(void *_th)
  217. {
  218. struct tcphdr *th = _th;
  219. th->source = htons(cfg_port_src);
  220. th->dest = htons(cfg_port_dst);
  221. th->doff = 5;
  222. th->check = 0;
  223. th->check = checksum(th, IPPROTO_TCP, sizeof(*th) + cfg_payload_len);
  224. if (cfg_bad_csum)
  225. th->check = ~th->check;
  226. fprintf(stderr, "tx: sending checksum: 0x%x\n", th->check);
  227. return th + 1;
  228. }
  229. static char *build_packet_udp_encap(void *_uh)
  230. {
  231. struct udphdr *uh = _uh;
  232. struct udp_encap_hdr *eh = _uh + sizeof(*uh);
  233. /* outer dst == inner dst, to simplify BPF filter
  234. * outer src != inner src, to demultiplex on recv
  235. */
  236. uh->dest = htons(cfg_port_dst);
  237. uh->source = htons(cfg_port_src_encap);
  238. uh->check = 0;
  239. uh->len = htons(sizeof(*uh) +
  240. sizeof(*eh) +
  241. sizeof(struct tcphdr) +
  242. cfg_payload_len);
  243. eh->nexthdr = IPPROTO_TCP;
  244. return build_packet_tcp(eh + 1);
  245. }
  246. static char *build_packet(char *buf, int max_len, int *len)
  247. {
  248. uint8_t proto;
  249. char *off;
  250. int tlen;
  251. if (cfg_random_seed) {
  252. int *buf32 = (void *)buf;
  253. int i;
  254. for (i = 0; i < (max_len / sizeof(int)); i++)
  255. buf32[i] = rand();
  256. } else {
  257. memset(buf, cfg_payload_char, max_len);
  258. }
  259. if (cfg_proto == IPPROTO_UDP)
  260. tlen = sizeof(struct udphdr) + cfg_payload_len;
  261. else
  262. tlen = sizeof(struct tcphdr) + cfg_payload_len;
  263. if (cfg_encap) {
  264. proto = IPPROTO_UDP;
  265. tlen += ENC_HEADER_LEN;
  266. } else {
  267. proto = cfg_proto;
  268. }
  269. if (cfg_family == PF_INET)
  270. off = build_packet_ipv4(buf, proto, tlen);
  271. else
  272. off = build_packet_ipv6(buf, proto, tlen);
  273. if (cfg_encap)
  274. off = build_packet_udp_encap(off);
  275. else if (cfg_proto == IPPROTO_UDP)
  276. off = build_packet_udp(off);
  277. else
  278. off = build_packet_tcp(off);
  279. /* only pass the payload, but still compute headers for cfg_zero_sum */
  280. if (cfg_send_udp) {
  281. *len = cfg_payload_len;
  282. return off;
  283. }
  284. *len = off - buf + cfg_payload_len;
  285. return buf;
  286. }
  287. static int open_inet(int ipproto, int protocol)
  288. {
  289. int fd;
  290. fd = socket(cfg_family, ipproto, protocol);
  291. if (fd == -1)
  292. error(1, errno, "socket inet");
  293. if (cfg_family == PF_INET6) {
  294. /* may have been updated by cfg_zero_sum */
  295. cfg_saddr6.sin6_port = htons(cfg_port_src);
  296. if (bind(fd, (void *)&cfg_saddr6, sizeof(cfg_saddr6)))
  297. error(1, errno, "bind dgram 6");
  298. if (connect(fd, (void *)&cfg_daddr6, sizeof(cfg_daddr6)))
  299. error(1, errno, "connect dgram 6");
  300. } else {
  301. /* may have been updated by cfg_zero_sum */
  302. cfg_saddr4.sin_port = htons(cfg_port_src);
  303. if (bind(fd, (void *)&cfg_saddr4, sizeof(cfg_saddr4)))
  304. error(1, errno, "bind dgram 4");
  305. if (connect(fd, (void *)&cfg_daddr4, sizeof(cfg_daddr4)))
  306. error(1, errno, "connect dgram 4");
  307. }
  308. return fd;
  309. }
  310. static int open_packet(void)
  311. {
  312. int fd, one = 1;
  313. fd = socket(PF_PACKET, SOCK_RAW, 0);
  314. if (fd == -1)
  315. error(1, errno, "socket packet");
  316. if (setsockopt(fd, SOL_PACKET, PACKET_VNET_HDR, &one, sizeof(one)))
  317. error(1, errno, "setsockopt packet_vnet_ndr");
  318. return fd;
  319. }
  320. static void send_inet(int fd, const char *buf, int len)
  321. {
  322. int ret;
  323. ret = write(fd, buf, len);
  324. if (ret == -1)
  325. error(1, errno, "write");
  326. if (ret != len)
  327. error(1, 0, "write: %d", ret);
  328. }
  329. static void eth_str_to_addr(const char *str, unsigned char *eth)
  330. {
  331. if (sscanf(str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  332. &eth[0], &eth[1], &eth[2], &eth[3], &eth[4], &eth[5]) != 6)
  333. error(1, 0, "cannot parse mac addr %s", str);
  334. }
  335. static void send_packet(int fd, const char *buf, int len)
  336. {
  337. struct virtio_net_hdr vh = {0};
  338. struct sockaddr_ll addr = {0};
  339. struct msghdr msg = {0};
  340. struct ethhdr eth;
  341. struct iovec iov[3];
  342. int ret;
  343. addr.sll_family = AF_PACKET;
  344. addr.sll_halen = ETH_ALEN;
  345. addr.sll_ifindex = if_nametoindex(cfg_ifname);
  346. if (!addr.sll_ifindex)
  347. error(1, errno, "if_nametoindex %s", cfg_ifname);
  348. vh.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  349. if (cfg_family == PF_INET6) {
  350. vh.csum_start = sizeof(struct ethhdr) + sizeof(struct ipv6hdr);
  351. addr.sll_protocol = htons(ETH_P_IPV6);
  352. } else {
  353. vh.csum_start = sizeof(struct ethhdr) + sizeof(struct iphdr);
  354. addr.sll_protocol = htons(ETH_P_IP);
  355. }
  356. if (cfg_encap)
  357. vh.csum_start += ENC_HEADER_LEN;
  358. if (cfg_proto == IPPROTO_TCP) {
  359. vh.csum_offset = __builtin_offsetof(struct tcphdr, check);
  360. vh.hdr_len = vh.csum_start + sizeof(struct tcphdr);
  361. } else {
  362. vh.csum_offset = __builtin_offsetof(struct udphdr, check);
  363. vh.hdr_len = vh.csum_start + sizeof(struct udphdr);
  364. }
  365. eth_str_to_addr(cfg_mac_src, eth.h_source);
  366. eth_str_to_addr(cfg_mac_dst, eth.h_dest);
  367. eth.h_proto = addr.sll_protocol;
  368. iov[0].iov_base = &vh;
  369. iov[0].iov_len = sizeof(vh);
  370. iov[1].iov_base = &eth;
  371. iov[1].iov_len = sizeof(eth);
  372. iov[2].iov_base = (void *)buf;
  373. iov[2].iov_len = len;
  374. msg.msg_iov = iov;
  375. msg.msg_iovlen = ARRAY_SIZE(iov);
  376. msg.msg_name = &addr;
  377. msg.msg_namelen = sizeof(addr);
  378. ret = sendmsg(fd, &msg, 0);
  379. if (ret == -1)
  380. error(1, errno, "sendmsg packet");
  381. if (ret != sizeof(vh) + sizeof(eth) + len)
  382. error(1, errno, "sendmsg packet: %u", ret);
  383. }
  384. static int recv_prepare_udp(void)
  385. {
  386. int fd;
  387. fd = socket(cfg_family, SOCK_DGRAM, 0);
  388. if (fd == -1)
  389. error(1, errno, "socket r");
  390. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
  391. &cfg_rcvbuf, sizeof(cfg_rcvbuf)))
  392. error(1, errno, "setsockopt SO_RCVBUF r");
  393. if (cfg_family == PF_INET6) {
  394. if (bind(fd, (void *)&cfg_daddr6, sizeof(cfg_daddr6)))
  395. error(1, errno, "bind r");
  396. } else {
  397. if (bind(fd, (void *)&cfg_daddr4, sizeof(cfg_daddr4)))
  398. error(1, errno, "bind r");
  399. }
  400. return fd;
  401. }
  402. /* Filter out all traffic that is not cfg_proto with our destination port.
  403. *
  404. * Otherwise background noise may cause PF_PACKET receive queue overflow,
  405. * dropping the expected packets and failing the test.
  406. */
  407. static void __recv_prepare_packet_filter(int fd, int off_nexthdr, int off_dport)
  408. {
  409. struct sock_filter filter[] = {
  410. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, SKF_AD_OFF + SKF_AD_PKTTYPE),
  411. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, PACKET_HOST, 0, 4),
  412. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, off_nexthdr),
  413. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_encap ? IPPROTO_UDP : cfg_proto, 0, 2),
  414. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, off_dport),
  415. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, cfg_port_dst, 1, 0),
  416. BPF_STMT(BPF_RET + BPF_K, 0),
  417. BPF_STMT(BPF_RET + BPF_K, 0xFFFF),
  418. };
  419. struct sock_fprog prog = {};
  420. prog.filter = filter;
  421. prog.len = ARRAY_SIZE(filter);
  422. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)))
  423. error(1, errno, "setsockopt filter");
  424. }
  425. static void recv_prepare_packet_filter(int fd)
  426. {
  427. const int off_dport = offsetof(struct tcphdr, dest); /* same for udp */
  428. if (cfg_family == AF_INET)
  429. __recv_prepare_packet_filter(fd, offsetof(struct iphdr, protocol),
  430. sizeof(struct iphdr) + off_dport);
  431. else
  432. __recv_prepare_packet_filter(fd, offsetof(struct ipv6hdr, nexthdr),
  433. sizeof(struct ipv6hdr) + off_dport);
  434. }
  435. static void recv_prepare_packet_bind(int fd)
  436. {
  437. struct sockaddr_ll laddr = {0};
  438. laddr.sll_family = AF_PACKET;
  439. if (cfg_family == PF_INET)
  440. laddr.sll_protocol = htons(ETH_P_IP);
  441. else
  442. laddr.sll_protocol = htons(ETH_P_IPV6);
  443. laddr.sll_ifindex = if_nametoindex(cfg_ifname);
  444. if (!laddr.sll_ifindex)
  445. error(1, 0, "if_nametoindex %s", cfg_ifname);
  446. if (bind(fd, (void *)&laddr, sizeof(laddr)))
  447. error(1, errno, "bind pf_packet");
  448. }
  449. static int recv_prepare_packet(void)
  450. {
  451. int fd, one = 1;
  452. fd = socket(PF_PACKET, SOCK_DGRAM, 0);
  453. if (fd == -1)
  454. error(1, errno, "socket p");
  455. if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
  456. &cfg_rcvbuf, sizeof(cfg_rcvbuf)))
  457. error(1, errno, "setsockopt SO_RCVBUF p");
  458. /* enable auxdata to recv checksum status (valid vs unknown) */
  459. if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one)))
  460. error(1, errno, "setsockopt auxdata");
  461. /* install filter to restrict packet flow to match */
  462. recv_prepare_packet_filter(fd);
  463. /* bind to address family to start packet flow */
  464. recv_prepare_packet_bind(fd);
  465. return fd;
  466. }
  467. static int recv_udp(int fd)
  468. {
  469. static char buf[MAX_PAYLOAD_LEN];
  470. int ret, count = 0;
  471. while (1) {
  472. ret = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
  473. if (ret == -1 && errno == EAGAIN)
  474. break;
  475. if (ret == -1)
  476. error(1, errno, "recv r");
  477. fprintf(stderr, "rx: udp: len=%u\n", ret);
  478. count++;
  479. }
  480. return count;
  481. }
  482. static int recv_verify_csum(void *th, int len, uint16_t sport, uint16_t csum_field)
  483. {
  484. uint16_t csum;
  485. csum = checksum(th, cfg_proto, len);
  486. fprintf(stderr, "rx: pkt: sport=%hu len=%u csum=0x%hx verify=0x%hx\n",
  487. sport, len, csum_field, csum);
  488. /* csum must be zero unless cfg_bad_csum indicates bad csum */
  489. if (csum && !cfg_bad_csum) {
  490. fprintf(stderr, "pkt: bad csum\n");
  491. return 1;
  492. } else if (cfg_bad_csum && !csum) {
  493. fprintf(stderr, "pkt: good csum, while bad expected\n");
  494. return 1;
  495. }
  496. if (cfg_zero_sum && csum_field != 0xFFFF) {
  497. fprintf(stderr, "pkt: zero csum: field should be 0xFFFF, is 0x%hx\n", csum_field);
  498. return 1;
  499. }
  500. return 0;
  501. }
  502. static int recv_verify_packet_tcp(void *th, int len)
  503. {
  504. struct tcphdr *tcph = th;
  505. if (len < sizeof(*tcph) || tcph->dest != htons(cfg_port_dst))
  506. return -1;
  507. return recv_verify_csum(th, len, ntohs(tcph->source), tcph->check);
  508. }
  509. static int recv_verify_packet_udp_encap(void *th, int len)
  510. {
  511. struct udp_encap_hdr *eh = th;
  512. if (len < sizeof(*eh) || eh->nexthdr != IPPROTO_TCP)
  513. return -1;
  514. return recv_verify_packet_tcp(eh + 1, len - sizeof(*eh));
  515. }
  516. static int recv_verify_packet_udp(void *th, int len)
  517. {
  518. struct udphdr *udph = th;
  519. if (len < sizeof(*udph))
  520. return -1;
  521. if (udph->dest != htons(cfg_port_dst))
  522. return -1;
  523. if (udph->source == htons(cfg_port_src_encap))
  524. return recv_verify_packet_udp_encap(udph + 1,
  525. len - sizeof(*udph));
  526. return recv_verify_csum(th, len, ntohs(udph->source), udph->check);
  527. }
  528. static int recv_verify_packet_ipv4(void *nh, int len)
  529. {
  530. struct iphdr *iph = nh;
  531. uint16_t proto = cfg_encap ? IPPROTO_UDP : cfg_proto;
  532. uint16_t ip_len;
  533. if (len < sizeof(*iph) || iph->protocol != proto)
  534. return -1;
  535. ip_len = ntohs(iph->tot_len);
  536. if (ip_len > len || ip_len < sizeof(*iph))
  537. return -1;
  538. len = ip_len;
  539. iph_addr_p = &iph->saddr;
  540. if (proto == IPPROTO_TCP)
  541. return recv_verify_packet_tcp(iph + 1, len - sizeof(*iph));
  542. else
  543. return recv_verify_packet_udp(iph + 1, len - sizeof(*iph));
  544. }
  545. static int recv_verify_packet_ipv6(void *nh, int len)
  546. {
  547. struct ipv6hdr *ip6h = nh;
  548. uint16_t proto = cfg_encap ? IPPROTO_UDP : cfg_proto;
  549. uint16_t ip_len;
  550. if (len < sizeof(*ip6h) || ip6h->nexthdr != proto)
  551. return -1;
  552. ip_len = ntohs(ip6h->payload_len);
  553. if (ip_len > len - sizeof(*ip6h))
  554. return -1;
  555. len = ip_len;
  556. iph_addr_p = &ip6h->saddr;
  557. if (proto == IPPROTO_TCP)
  558. return recv_verify_packet_tcp(ip6h + 1, len);
  559. else
  560. return recv_verify_packet_udp(ip6h + 1, len);
  561. }
  562. /* return whether auxdata includes TP_STATUS_CSUM_VALID */
  563. static uint32_t recv_get_packet_csum_status(struct msghdr *msg)
  564. {
  565. struct tpacket_auxdata *aux = NULL;
  566. struct cmsghdr *cm;
  567. if (msg->msg_flags & MSG_CTRUNC)
  568. error(1, 0, "cmsg: truncated");
  569. for (cm = CMSG_FIRSTHDR(msg); cm; cm = CMSG_NXTHDR(msg, cm)) {
  570. if (cm->cmsg_level != SOL_PACKET ||
  571. cm->cmsg_type != PACKET_AUXDATA)
  572. error(1, 0, "cmsg: level=%d type=%d\n",
  573. cm->cmsg_level, cm->cmsg_type);
  574. if (cm->cmsg_len != CMSG_LEN(sizeof(struct tpacket_auxdata)))
  575. error(1, 0, "cmsg: len=%lu expected=%lu",
  576. cm->cmsg_len, CMSG_LEN(sizeof(struct tpacket_auxdata)));
  577. aux = (void *)CMSG_DATA(cm);
  578. }
  579. if (!aux)
  580. error(1, 0, "cmsg: no auxdata");
  581. return aux->tp_status;
  582. }
  583. static int recv_packet(int fd)
  584. {
  585. static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
  586. unsigned long total = 0, bad_csums = 0, bad_validations = 0;
  587. char ctrl[CMSG_SPACE(sizeof(struct tpacket_auxdata))];
  588. struct pkt *buf = (void *)_buf;
  589. struct msghdr msg = {0};
  590. uint32_t tp_status;
  591. struct iovec iov;
  592. int len, ret;
  593. iov.iov_base = _buf;
  594. iov.iov_len = sizeof(_buf);
  595. msg.msg_iov = &iov;
  596. msg.msg_iovlen = 1;
  597. msg.msg_control = ctrl;
  598. msg.msg_controllen = sizeof(ctrl);
  599. while (1) {
  600. msg.msg_flags = 0;
  601. len = recvmsg(fd, &msg, MSG_DONTWAIT);
  602. if (len == -1 && errno == EAGAIN)
  603. break;
  604. if (len == -1)
  605. error(1, errno, "recv p");
  606. tp_status = recv_get_packet_csum_status(&msg);
  607. /* GRO might coalesce randomized packets. Such GSO packets are
  608. * then reinitialized for csum offload (CHECKSUM_PARTIAL), with
  609. * a pseudo csum. Do not try to validate these checksums.
  610. */
  611. if (tp_status & TP_STATUS_CSUMNOTREADY) {
  612. fprintf(stderr, "cmsg: GSO packet has partial csum: skip\n");
  613. continue;
  614. }
  615. if (cfg_family == PF_INET6)
  616. ret = recv_verify_packet_ipv6(buf, len);
  617. else
  618. ret = recv_verify_packet_ipv4(buf, len);
  619. if (ret == -1 /* skip: non-matching */)
  620. continue;
  621. total++;
  622. if (ret == 1)
  623. bad_csums++;
  624. /* Fail if kernel returns valid for known bad csum.
  625. * Do not fail if kernel does not validate a good csum:
  626. * Absence of validation does not imply invalid.
  627. */
  628. if (tp_status & TP_STATUS_CSUM_VALID && cfg_bad_csum) {
  629. fprintf(stderr, "cmsg: expected bad csum, pf_packet returns valid\n");
  630. bad_validations++;
  631. }
  632. }
  633. if (bad_csums || bad_validations)
  634. error(1, 0, "rx: errors at pf_packet: total=%lu bad_csums=%lu bad_valids=%lu\n",
  635. total, bad_csums, bad_validations);
  636. return total;
  637. }
  638. static void parse_args(int argc, char *const argv[])
  639. {
  640. const char *daddr = NULL, *saddr = NULL;
  641. int c;
  642. while ((c = getopt(argc, argv, "46d:D:eEi:l:L:n:r:PRs:S:tTuUzZ")) != -1) {
  643. switch (c) {
  644. case '4':
  645. cfg_family = PF_INET;
  646. break;
  647. case '6':
  648. cfg_family = PF_INET6;
  649. break;
  650. case 'd':
  651. cfg_mac_dst = optarg;
  652. break;
  653. case 'D':
  654. daddr = optarg;
  655. break;
  656. case 'e':
  657. cfg_encap = true;
  658. break;
  659. case 'E':
  660. cfg_bad_csum = true;
  661. break;
  662. case 'i':
  663. cfg_ifname = optarg;
  664. break;
  665. case 'l':
  666. cfg_payload_len = strtol(optarg, NULL, 0);
  667. break;
  668. case 'L':
  669. cfg_timeout_ms = strtol(optarg, NULL, 0) * 1000;
  670. break;
  671. case 'n':
  672. cfg_num_pkt = strtol(optarg, NULL, 0);
  673. break;
  674. case 'r':
  675. cfg_random_seed = strtol(optarg, NULL, 0);
  676. break;
  677. case 'P':
  678. cfg_send_pfpacket = true;
  679. break;
  680. case 'R':
  681. /* only Rx: used with two machine tests */
  682. cfg_do_tx = false;
  683. break;
  684. case 's':
  685. cfg_mac_src = optarg;
  686. break;
  687. case 'S':
  688. saddr = optarg;
  689. break;
  690. case 't':
  691. cfg_proto = IPPROTO_TCP;
  692. break;
  693. case 'T':
  694. /* only Tx: used with two machine tests */
  695. cfg_do_rx = false;
  696. break;
  697. case 'u':
  698. cfg_proto = IPPROTO_UDP;
  699. break;
  700. case 'U':
  701. /* send using real udp socket,
  702. * to exercise tx checksum offload
  703. */
  704. cfg_send_udp = true;
  705. break;
  706. case 'z':
  707. cfg_zero_disable = true;
  708. break;
  709. case 'Z':
  710. cfg_zero_sum = true;
  711. break;
  712. default:
  713. error(1, 0, "unknown arg %c", c);
  714. }
  715. }
  716. if (!daddr || !saddr)
  717. error(1, 0, "Must pass -D <daddr> and -S <saddr>");
  718. if (cfg_do_tx && cfg_send_pfpacket && (!cfg_mac_src || !cfg_mac_dst))
  719. error(1, 0, "Transmit with pf_packet requires mac addresses");
  720. if (cfg_payload_len > MAX_PAYLOAD_LEN)
  721. error(1, 0, "Payload length exceeds max");
  722. if (cfg_proto != IPPROTO_UDP && (cfg_zero_sum || cfg_zero_disable))
  723. error(1, 0, "Only UDP supports zero csum");
  724. if (cfg_zero_sum && !cfg_send_udp)
  725. error(1, 0, "Zero checksum conversion requires -U for tx csum offload");
  726. if (cfg_zero_sum && cfg_bad_csum)
  727. error(1, 0, "Cannot combine zero checksum conversion and invalid checksum");
  728. if (cfg_zero_sum && cfg_random_seed)
  729. error(1, 0, "Cannot combine zero checksum conversion with randomization");
  730. if (cfg_family == PF_INET6) {
  731. cfg_saddr6.sin6_port = htons(cfg_port_src);
  732. cfg_daddr6.sin6_port = htons(cfg_port_dst);
  733. if (inet_pton(cfg_family, daddr, &cfg_daddr6.sin6_addr) != 1)
  734. error(1, errno, "Cannot parse ipv6 -D");
  735. if (inet_pton(cfg_family, saddr, &cfg_saddr6.sin6_addr) != 1)
  736. error(1, errno, "Cannot parse ipv6 -S");
  737. } else {
  738. cfg_saddr4.sin_port = htons(cfg_port_src);
  739. cfg_daddr4.sin_port = htons(cfg_port_dst);
  740. if (inet_pton(cfg_family, daddr, &cfg_daddr4.sin_addr) != 1)
  741. error(1, errno, "Cannot parse ipv4 -D");
  742. if (inet_pton(cfg_family, saddr, &cfg_saddr4.sin_addr) != 1)
  743. error(1, errno, "Cannot parse ipv4 -S");
  744. }
  745. if (cfg_do_tx && cfg_random_seed) {
  746. /* special case: time-based seed */
  747. if (cfg_random_seed == 1)
  748. cfg_random_seed = (unsigned int)gettimeofday_ms();
  749. srand(cfg_random_seed);
  750. fprintf(stderr, "randomization seed: %u\n", cfg_random_seed);
  751. }
  752. }
  753. static void do_tx(void)
  754. {
  755. static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
  756. char *buf;
  757. int fd, len, i;
  758. buf = build_packet(_buf, sizeof(_buf), &len);
  759. if (cfg_send_pfpacket)
  760. fd = open_packet();
  761. else if (cfg_send_udp)
  762. fd = open_inet(SOCK_DGRAM, 0);
  763. else
  764. fd = open_inet(SOCK_RAW, IPPROTO_RAW);
  765. for (i = 0; i < cfg_num_pkt; i++) {
  766. if (cfg_send_pfpacket)
  767. send_packet(fd, buf, len);
  768. else
  769. send_inet(fd, buf, len);
  770. /* randomize each packet individually to increase coverage */
  771. if (cfg_random_seed) {
  772. cfg_payload_len = rand() % MAX_PAYLOAD_LEN;
  773. buf = build_packet(_buf, sizeof(_buf), &len);
  774. }
  775. }
  776. if (close(fd))
  777. error(1, errno, "close tx");
  778. }
  779. static void do_rx(int fdp, int fdr)
  780. {
  781. unsigned long count_udp = 0, count_pkt = 0;
  782. long tleft, tstop;
  783. struct pollfd pfd;
  784. tstop = gettimeofday_ms() + cfg_timeout_ms;
  785. tleft = cfg_timeout_ms;
  786. do {
  787. pfd.events = POLLIN;
  788. pfd.fd = fdp;
  789. if (poll(&pfd, 1, tleft) == -1)
  790. error(1, errno, "poll");
  791. if (pfd.revents & POLLIN)
  792. count_pkt += recv_packet(fdp);
  793. if (cfg_proto == IPPROTO_UDP)
  794. count_udp += recv_udp(fdr);
  795. tleft = tstop - gettimeofday_ms();
  796. } while (tleft > 0);
  797. if (close(fdr))
  798. error(1, errno, "close r");
  799. if (close(fdp))
  800. error(1, errno, "close p");
  801. if (count_pkt < cfg_num_pkt)
  802. error(1, 0, "rx: missing packets at pf_packet: %lu < %u",
  803. count_pkt, cfg_num_pkt);
  804. if (cfg_proto == IPPROTO_UDP) {
  805. if (cfg_bad_csum && count_udp)
  806. error(1, 0, "rx: unexpected packets at udp");
  807. if (!cfg_bad_csum && !count_udp)
  808. error(1, 0, "rx: missing packets at udp");
  809. }
  810. }
  811. int main(int argc, char *const argv[])
  812. {
  813. int fdp = -1, fdr = -1; /* -1 to silence -Wmaybe-uninitialized */
  814. parse_args(argc, argv);
  815. /* open receive sockets before transmitting */
  816. if (cfg_do_rx) {
  817. fdp = recv_prepare_packet();
  818. fdr = recv_prepare_udp();
  819. }
  820. if (cfg_do_tx)
  821. do_tx();
  822. if (cfg_do_rx)
  823. do_rx(fdp, fdr);
  824. fprintf(stderr, "OK\n");
  825. return 0;
  826. }