csum.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 payload_len;
  550. if (len < sizeof(*ip6h) || ip6h->nexthdr != proto)
  551. return -1;
  552. payload_len = ntohs(ip6h->payload_len);
  553. if (payload_len > len - sizeof(*ip6h))
  554. return -1;
  555. iph_addr_p = &ip6h->saddr;
  556. if (proto == IPPROTO_TCP)
  557. return recv_verify_packet_tcp(ip6h + 1, payload_len);
  558. else
  559. return recv_verify_packet_udp(ip6h + 1, payload_len);
  560. }
  561. /* return whether auxdata includes TP_STATUS_CSUM_VALID */
  562. static uint32_t recv_get_packet_csum_status(struct msghdr *msg)
  563. {
  564. struct tpacket_auxdata *aux = NULL;
  565. struct cmsghdr *cm;
  566. if (msg->msg_flags & MSG_CTRUNC)
  567. error(1, 0, "cmsg: truncated");
  568. for (cm = CMSG_FIRSTHDR(msg); cm; cm = CMSG_NXTHDR(msg, cm)) {
  569. if (cm->cmsg_level != SOL_PACKET ||
  570. cm->cmsg_type != PACKET_AUXDATA)
  571. error(1, 0, "cmsg: level=%d type=%d\n",
  572. cm->cmsg_level, cm->cmsg_type);
  573. if (cm->cmsg_len != CMSG_LEN(sizeof(struct tpacket_auxdata)))
  574. error(1, 0, "cmsg: len=%zu expected=%zu",
  575. cm->cmsg_len, CMSG_LEN(sizeof(struct tpacket_auxdata)));
  576. aux = (void *)CMSG_DATA(cm);
  577. }
  578. if (!aux)
  579. error(1, 0, "cmsg: no auxdata");
  580. return aux->tp_status;
  581. }
  582. static int recv_packet(int fd)
  583. {
  584. static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
  585. unsigned long total = 0, bad_csums = 0, bad_validations = 0;
  586. char ctrl[CMSG_SPACE(sizeof(struct tpacket_auxdata))];
  587. struct pkt *buf = (void *)_buf;
  588. struct msghdr msg = {0};
  589. uint32_t tp_status;
  590. struct iovec iov;
  591. int len, ret;
  592. iov.iov_base = _buf;
  593. iov.iov_len = sizeof(_buf);
  594. msg.msg_iov = &iov;
  595. msg.msg_iovlen = 1;
  596. msg.msg_control = ctrl;
  597. msg.msg_controllen = sizeof(ctrl);
  598. while (1) {
  599. msg.msg_flags = 0;
  600. len = recvmsg(fd, &msg, MSG_DONTWAIT);
  601. if (len == -1 && errno == EAGAIN)
  602. break;
  603. if (len == -1)
  604. error(1, errno, "recv p");
  605. tp_status = recv_get_packet_csum_status(&msg);
  606. /* GRO might coalesce randomized packets. Such GSO packets are
  607. * then reinitialized for csum offload (CHECKSUM_PARTIAL), with
  608. * a pseudo csum. Do not try to validate these checksums.
  609. */
  610. if (tp_status & TP_STATUS_CSUMNOTREADY) {
  611. fprintf(stderr, "cmsg: GSO packet has partial csum: skip\n");
  612. continue;
  613. }
  614. if (cfg_family == PF_INET6)
  615. ret = recv_verify_packet_ipv6(buf, len);
  616. else
  617. ret = recv_verify_packet_ipv4(buf, len);
  618. if (ret == -1 /* skip: non-matching */)
  619. continue;
  620. total++;
  621. if (ret == 1)
  622. bad_csums++;
  623. /* Fail if kernel returns valid for known bad csum.
  624. * Do not fail if kernel does not validate a good csum:
  625. * Absence of validation does not imply invalid.
  626. */
  627. if (tp_status & TP_STATUS_CSUM_VALID && cfg_bad_csum) {
  628. fprintf(stderr, "cmsg: expected bad csum, pf_packet returns valid\n");
  629. bad_validations++;
  630. }
  631. }
  632. if (bad_csums || bad_validations)
  633. error(1, 0, "rx: errors at pf_packet: total=%lu bad_csums=%lu bad_valids=%lu\n",
  634. total, bad_csums, bad_validations);
  635. return total;
  636. }
  637. static void parse_args(int argc, char *const argv[])
  638. {
  639. const char *daddr = NULL, *saddr = NULL;
  640. int c;
  641. while ((c = getopt(argc, argv, "46d:D:eEi:l:L:n:r:PRs:S:tTuUzZ")) != -1) {
  642. switch (c) {
  643. case '4':
  644. cfg_family = PF_INET;
  645. break;
  646. case '6':
  647. cfg_family = PF_INET6;
  648. break;
  649. case 'd':
  650. cfg_mac_dst = optarg;
  651. break;
  652. case 'D':
  653. daddr = optarg;
  654. break;
  655. case 'e':
  656. cfg_encap = true;
  657. break;
  658. case 'E':
  659. cfg_bad_csum = true;
  660. break;
  661. case 'i':
  662. cfg_ifname = optarg;
  663. break;
  664. case 'l':
  665. cfg_payload_len = strtol(optarg, NULL, 0);
  666. break;
  667. case 'L':
  668. cfg_timeout_ms = strtol(optarg, NULL, 0) * 1000;
  669. break;
  670. case 'n':
  671. cfg_num_pkt = strtol(optarg, NULL, 0);
  672. break;
  673. case 'r':
  674. cfg_random_seed = strtol(optarg, NULL, 0);
  675. break;
  676. case 'P':
  677. cfg_send_pfpacket = true;
  678. break;
  679. case 'R':
  680. /* only Rx: used with two machine tests */
  681. cfg_do_tx = false;
  682. break;
  683. case 's':
  684. cfg_mac_src = optarg;
  685. break;
  686. case 'S':
  687. saddr = optarg;
  688. break;
  689. case 't':
  690. cfg_proto = IPPROTO_TCP;
  691. break;
  692. case 'T':
  693. /* only Tx: used with two machine tests */
  694. cfg_do_rx = false;
  695. break;
  696. case 'u':
  697. cfg_proto = IPPROTO_UDP;
  698. break;
  699. case 'U':
  700. /* send using real udp socket,
  701. * to exercise tx checksum offload
  702. */
  703. cfg_send_udp = true;
  704. break;
  705. case 'z':
  706. cfg_zero_disable = true;
  707. break;
  708. case 'Z':
  709. cfg_zero_sum = true;
  710. break;
  711. default:
  712. error(1, 0, "unknown arg %c", c);
  713. }
  714. }
  715. if (!daddr || !saddr)
  716. error(1, 0, "Must pass -D <daddr> and -S <saddr>");
  717. if (cfg_do_tx && cfg_send_pfpacket && (!cfg_mac_src || !cfg_mac_dst))
  718. error(1, 0, "Transmit with pf_packet requires mac addresses");
  719. if (cfg_payload_len > MAX_PAYLOAD_LEN)
  720. error(1, 0, "Payload length exceeds max");
  721. if (cfg_proto != IPPROTO_UDP && (cfg_zero_sum || cfg_zero_disable))
  722. error(1, 0, "Only UDP supports zero csum");
  723. if (cfg_zero_sum && !cfg_send_udp)
  724. error(1, 0, "Zero checksum conversion requires -U for tx csum offload");
  725. if (cfg_zero_sum && cfg_bad_csum)
  726. error(1, 0, "Cannot combine zero checksum conversion and invalid checksum");
  727. if (cfg_zero_sum && cfg_random_seed)
  728. error(1, 0, "Cannot combine zero checksum conversion with randomization");
  729. if (cfg_family == PF_INET6) {
  730. cfg_saddr6.sin6_port = htons(cfg_port_src);
  731. cfg_daddr6.sin6_port = htons(cfg_port_dst);
  732. if (inet_pton(cfg_family, daddr, &cfg_daddr6.sin6_addr) != 1)
  733. error(1, errno, "Cannot parse ipv6 -D");
  734. if (inet_pton(cfg_family, saddr, &cfg_saddr6.sin6_addr) != 1)
  735. error(1, errno, "Cannot parse ipv6 -S");
  736. } else {
  737. cfg_saddr4.sin_port = htons(cfg_port_src);
  738. cfg_daddr4.sin_port = htons(cfg_port_dst);
  739. if (inet_pton(cfg_family, daddr, &cfg_daddr4.sin_addr) != 1)
  740. error(1, errno, "Cannot parse ipv4 -D");
  741. if (inet_pton(cfg_family, saddr, &cfg_saddr4.sin_addr) != 1)
  742. error(1, errno, "Cannot parse ipv4 -S");
  743. }
  744. if (cfg_do_tx && cfg_random_seed) {
  745. /* special case: time-based seed */
  746. if (cfg_random_seed == 1)
  747. cfg_random_seed = (unsigned int)gettimeofday_ms();
  748. srand(cfg_random_seed);
  749. fprintf(stderr, "randomization seed: %u\n", cfg_random_seed);
  750. }
  751. }
  752. static void do_tx(void)
  753. {
  754. static char _buf[MAX_HEADER_LEN + MAX_PAYLOAD_LEN];
  755. char *buf;
  756. int fd, len, i;
  757. buf = build_packet(_buf, sizeof(_buf), &len);
  758. if (cfg_send_pfpacket)
  759. fd = open_packet();
  760. else if (cfg_send_udp)
  761. fd = open_inet(SOCK_DGRAM, 0);
  762. else
  763. fd = open_inet(SOCK_RAW, IPPROTO_RAW);
  764. for (i = 0; i < cfg_num_pkt; i++) {
  765. if (cfg_send_pfpacket)
  766. send_packet(fd, buf, len);
  767. else
  768. send_inet(fd, buf, len);
  769. /* randomize each packet individually to increase coverage */
  770. if (cfg_random_seed) {
  771. cfg_payload_len = rand() % MAX_PAYLOAD_LEN;
  772. buf = build_packet(_buf, sizeof(_buf), &len);
  773. }
  774. }
  775. if (close(fd))
  776. error(1, errno, "close tx");
  777. }
  778. static void do_rx(int fdp, int fdr)
  779. {
  780. unsigned long count_udp = 0, count_pkt = 0;
  781. long tleft, tstop;
  782. struct pollfd pfd;
  783. tstop = gettimeofday_ms() + cfg_timeout_ms;
  784. tleft = cfg_timeout_ms;
  785. do {
  786. pfd.events = POLLIN;
  787. pfd.fd = fdp;
  788. if (poll(&pfd, 1, tleft) == -1)
  789. error(1, errno, "poll");
  790. if (pfd.revents & POLLIN)
  791. count_pkt += recv_packet(fdp);
  792. if (cfg_proto == IPPROTO_UDP)
  793. count_udp += recv_udp(fdr);
  794. tleft = tstop - gettimeofday_ms();
  795. } while (tleft > 0);
  796. if (close(fdr))
  797. error(1, errno, "close r");
  798. if (close(fdp))
  799. error(1, errno, "close p");
  800. if (count_pkt < cfg_num_pkt)
  801. error(1, 0, "rx: missing packets at pf_packet: %lu < %u",
  802. count_pkt, cfg_num_pkt);
  803. if (cfg_proto == IPPROTO_UDP) {
  804. if (cfg_bad_csum && count_udp)
  805. error(1, 0, "rx: unexpected packets at udp");
  806. if (!cfg_bad_csum && !count_udp)
  807. error(1, 0, "rx: missing packets at udp");
  808. }
  809. }
  810. int main(int argc, char *const argv[])
  811. {
  812. int fdp = -1, fdr = -1; /* -1 to silence -Wmaybe-uninitialized */
  813. parse_args(argc, argv);
  814. /* open receive sockets before transmitting */
  815. if (cfg_do_rx) {
  816. fdp = recv_prepare_packet();
  817. fdr = recv_prepare_udp();
  818. }
  819. if (cfg_do_tx)
  820. do_tx();
  821. if (cfg_do_rx)
  822. do_rx(fdp, fdr);
  823. fprintf(stderr, "OK\n");
  824. return 0;
  825. }