gro.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This testsuite provides conformance testing for GRO coalescing.
  4. *
  5. * Test cases:
  6. * 1.data
  7. * Data packets of the same size and same header setup with correct
  8. * sequence numbers coalesce. The one exception being the last data
  9. * packet coalesced: it can be smaller than the rest and coalesced
  10. * as long as it is in the same flow.
  11. * 2.ack
  12. * Pure ACK does not coalesce.
  13. * 3.flags
  14. * Specific test cases: no packets with PSH, SYN, URG, RST set will
  15. * be coalesced.
  16. * 4.tcp
  17. * Packets with incorrect checksum, non-consecutive seqno and
  18. * different TCP header options shouldn't coalesce. Nit: given that
  19. * some extension headers have paddings, such as timestamp, headers
  20. * that are padding differently would not be coalesced.
  21. * 5.ip:
  22. * Packets with different (ECN, TTL, TOS) header, ip options or
  23. * ip fragments (ipv6) shouldn't coalesce.
  24. * 6.large:
  25. * Packets larger than GRO_MAX_SIZE packets shouldn't coalesce.
  26. *
  27. * MSS is defined as 4096 - header because if it is too small
  28. * (i.e. 1500 MTU - header), it will result in many packets,
  29. * increasing the "large" test case's flakiness. This is because
  30. * due to time sensitivity in the coalescing window, the receiver
  31. * may not coalesce all of the packets.
  32. *
  33. * Note the timing issue applies to all of the test cases, so some
  34. * flakiness is to be expected.
  35. *
  36. */
  37. #define _GNU_SOURCE
  38. #include <arpa/inet.h>
  39. #include <errno.h>
  40. #include <error.h>
  41. #include <getopt.h>
  42. #include <linux/filter.h>
  43. #include <linux/if_packet.h>
  44. #include <linux/ipv6.h>
  45. #include <net/ethernet.h>
  46. #include <net/if.h>
  47. #include <netinet/in.h>
  48. #include <netinet/ip.h>
  49. #include <netinet/ip6.h>
  50. #include <netinet/tcp.h>
  51. #include <stdbool.h>
  52. #include <stddef.h>
  53. #include <stdio.h>
  54. #include <stdarg.h>
  55. #include <string.h>
  56. #include <unistd.h>
  57. #include "../kselftest.h"
  58. #define DPORT 8000
  59. #define SPORT 1500
  60. #define PAYLOAD_LEN 100
  61. #define NUM_PACKETS 4
  62. #define START_SEQ 100
  63. #define START_ACK 100
  64. #define ETH_P_NONE 0
  65. #define TOTAL_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
  66. #define MSS (4096 - sizeof(struct tcphdr) - sizeof(struct ipv6hdr))
  67. #define MAX_PAYLOAD (IP_MAXPACKET - sizeof(struct tcphdr) - sizeof(struct ipv6hdr))
  68. #define NUM_LARGE_PKT (MAX_PAYLOAD / MSS)
  69. #define MAX_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
  70. #define MIN_EXTHDR_SIZE 8
  71. #define EXT_PAYLOAD_1 "\x00\x00\x00\x00\x00\x00"
  72. #define EXT_PAYLOAD_2 "\x11\x11\x11\x11\x11\x11"
  73. #define ipv6_optlen(p) (((p)->hdrlen+1) << 3) /* calculate IPv6 extension header len */
  74. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  75. static const char *addr6_src = "fdaa::2";
  76. static const char *addr6_dst = "fdaa::1";
  77. static const char *addr4_src = "192.168.1.200";
  78. static const char *addr4_dst = "192.168.1.100";
  79. static int proto = -1;
  80. static uint8_t src_mac[ETH_ALEN], dst_mac[ETH_ALEN];
  81. static char *testname = "data";
  82. static char *ifname = "eth0";
  83. static char *smac = "aa:00:00:00:00:02";
  84. static char *dmac = "aa:00:00:00:00:01";
  85. static bool verbose;
  86. static bool tx_socket = true;
  87. static int tcp_offset = -1;
  88. static int total_hdr_len = -1;
  89. static int ethhdr_proto = -1;
  90. static const int num_flush_id_cases = 6;
  91. static void vlog(const char *fmt, ...)
  92. {
  93. va_list args;
  94. if (verbose) {
  95. va_start(args, fmt);
  96. vfprintf(stderr, fmt, args);
  97. va_end(args);
  98. }
  99. }
  100. static void setup_sock_filter(int fd)
  101. {
  102. const int dport_off = tcp_offset + offsetof(struct tcphdr, dest);
  103. const int ethproto_off = offsetof(struct ethhdr, h_proto);
  104. int optlen = 0;
  105. int ipproto_off, opt_ipproto_off;
  106. int next_off;
  107. if (proto == PF_INET)
  108. next_off = offsetof(struct iphdr, protocol);
  109. else
  110. next_off = offsetof(struct ipv6hdr, nexthdr);
  111. ipproto_off = ETH_HLEN + next_off;
  112. /* Overridden later if exthdrs are used: */
  113. opt_ipproto_off = ipproto_off;
  114. if (strcmp(testname, "ip") == 0) {
  115. if (proto == PF_INET)
  116. optlen = sizeof(struct ip_timestamp);
  117. else {
  118. BUILD_BUG_ON(sizeof(struct ip6_hbh) > MIN_EXTHDR_SIZE);
  119. BUILD_BUG_ON(sizeof(struct ip6_dest) > MIN_EXTHDR_SIZE);
  120. BUILD_BUG_ON(sizeof(struct ip6_frag) > MIN_EXTHDR_SIZE);
  121. /* same size for HBH and Fragment extension header types */
  122. optlen = MIN_EXTHDR_SIZE;
  123. opt_ipproto_off = ETH_HLEN + sizeof(struct ipv6hdr)
  124. + offsetof(struct ip6_ext, ip6e_nxt);
  125. }
  126. }
  127. /* this filter validates the following:
  128. * - packet is IPv4/IPv6 according to the running test.
  129. * - packet is TCP. Also handles the case of one extension header and then TCP.
  130. * - checks the packet tcp dport equals to DPORT. Also handles the case of one
  131. * extension header and then TCP.
  132. */
  133. struct sock_filter filter[] = {
  134. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, ethproto_off),
  135. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ntohs(ethhdr_proto), 0, 9),
  136. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, ipproto_off),
  137. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 2, 0),
  138. BPF_STMT(BPF_LD + BPF_B + BPF_ABS, opt_ipproto_off),
  139. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 0, 5),
  140. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off),
  141. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 2, 0),
  142. BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off + optlen),
  143. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 0, 1),
  144. BPF_STMT(BPF_RET + BPF_K, 0xFFFFFFFF),
  145. BPF_STMT(BPF_RET + BPF_K, 0),
  146. };
  147. struct sock_fprog bpf = {
  148. .len = ARRAY_SIZE(filter),
  149. .filter = filter,
  150. };
  151. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) < 0)
  152. error(1, errno, "error setting filter");
  153. }
  154. static uint32_t checksum_nofold(void *data, size_t len, uint32_t sum)
  155. {
  156. uint16_t *words = data;
  157. int i;
  158. for (i = 0; i < len / 2; i++)
  159. sum += words[i];
  160. if (len & 1)
  161. sum += ((char *)data)[len - 1];
  162. return sum;
  163. }
  164. static uint16_t checksum_fold(void *data, size_t len, uint32_t sum)
  165. {
  166. sum = checksum_nofold(data, len, sum);
  167. while (sum > 0xFFFF)
  168. sum = (sum & 0xFFFF) + (sum >> 16);
  169. return ~sum;
  170. }
  171. static uint16_t tcp_checksum(void *buf, int payload_len)
  172. {
  173. struct pseudo_header6 {
  174. struct in6_addr saddr;
  175. struct in6_addr daddr;
  176. uint16_t protocol;
  177. uint16_t payload_len;
  178. } ph6;
  179. struct pseudo_header4 {
  180. struct in_addr saddr;
  181. struct in_addr daddr;
  182. uint16_t protocol;
  183. uint16_t payload_len;
  184. } ph4;
  185. uint32_t sum = 0;
  186. if (proto == PF_INET6) {
  187. if (inet_pton(AF_INET6, addr6_src, &ph6.saddr) != 1)
  188. error(1, errno, "inet_pton6 source ip pseudo");
  189. if (inet_pton(AF_INET6, addr6_dst, &ph6.daddr) != 1)
  190. error(1, errno, "inet_pton6 dest ip pseudo");
  191. ph6.protocol = htons(IPPROTO_TCP);
  192. ph6.payload_len = htons(sizeof(struct tcphdr) + payload_len);
  193. sum = checksum_nofold(&ph6, sizeof(ph6), 0);
  194. } else if (proto == PF_INET) {
  195. if (inet_pton(AF_INET, addr4_src, &ph4.saddr) != 1)
  196. error(1, errno, "inet_pton source ip pseudo");
  197. if (inet_pton(AF_INET, addr4_dst, &ph4.daddr) != 1)
  198. error(1, errno, "inet_pton dest ip pseudo");
  199. ph4.protocol = htons(IPPROTO_TCP);
  200. ph4.payload_len = htons(sizeof(struct tcphdr) + payload_len);
  201. sum = checksum_nofold(&ph4, sizeof(ph4), 0);
  202. }
  203. return checksum_fold(buf, sizeof(struct tcphdr) + payload_len, sum);
  204. }
  205. static void read_MAC(uint8_t *mac_addr, char *mac)
  206. {
  207. if (sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  208. &mac_addr[0], &mac_addr[1], &mac_addr[2],
  209. &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6)
  210. error(1, 0, "sscanf");
  211. }
  212. static void fill_datalinklayer(void *buf)
  213. {
  214. struct ethhdr *eth = buf;
  215. memcpy(eth->h_dest, dst_mac, ETH_ALEN);
  216. memcpy(eth->h_source, src_mac, ETH_ALEN);
  217. eth->h_proto = ethhdr_proto;
  218. }
  219. static void fill_networklayer(void *buf, int payload_len)
  220. {
  221. struct ipv6hdr *ip6h = buf;
  222. struct iphdr *iph = buf;
  223. if (proto == PF_INET6) {
  224. memset(ip6h, 0, sizeof(*ip6h));
  225. ip6h->version = 6;
  226. ip6h->payload_len = htons(sizeof(struct tcphdr) + payload_len);
  227. ip6h->nexthdr = IPPROTO_TCP;
  228. ip6h->hop_limit = 8;
  229. if (inet_pton(AF_INET6, addr6_src, &ip6h->saddr) != 1)
  230. error(1, errno, "inet_pton source ip6");
  231. if (inet_pton(AF_INET6, addr6_dst, &ip6h->daddr) != 1)
  232. error(1, errno, "inet_pton dest ip6");
  233. } else if (proto == PF_INET) {
  234. memset(iph, 0, sizeof(*iph));
  235. iph->version = 4;
  236. iph->ihl = 5;
  237. iph->ttl = 8;
  238. iph->protocol = IPPROTO_TCP;
  239. iph->tot_len = htons(sizeof(struct tcphdr) +
  240. payload_len + sizeof(struct iphdr));
  241. iph->frag_off = htons(0x4000); /* DF = 1, MF = 0 */
  242. if (inet_pton(AF_INET, addr4_src, &iph->saddr) != 1)
  243. error(1, errno, "inet_pton source ip");
  244. if (inet_pton(AF_INET, addr4_dst, &iph->daddr) != 1)
  245. error(1, errno, "inet_pton dest ip");
  246. iph->check = checksum_fold(buf, sizeof(struct iphdr), 0);
  247. }
  248. }
  249. static void fill_transportlayer(void *buf, int seq_offset, int ack_offset,
  250. int payload_len, int fin)
  251. {
  252. struct tcphdr *tcph = buf;
  253. memset(tcph, 0, sizeof(*tcph));
  254. tcph->source = htons(SPORT);
  255. tcph->dest = htons(DPORT);
  256. tcph->seq = ntohl(START_SEQ + seq_offset);
  257. tcph->ack_seq = ntohl(START_ACK + ack_offset);
  258. tcph->ack = 1;
  259. tcph->fin = fin;
  260. tcph->doff = 5;
  261. tcph->window = htons(TCP_MAXWIN);
  262. tcph->urg_ptr = 0;
  263. tcph->check = tcp_checksum(tcph, payload_len);
  264. }
  265. static void write_packet(int fd, char *buf, int len, struct sockaddr_ll *daddr)
  266. {
  267. int ret = -1;
  268. ret = sendto(fd, buf, len, 0, (struct sockaddr *)daddr, sizeof(*daddr));
  269. if (ret == -1)
  270. error(1, errno, "sendto failure");
  271. if (ret != len)
  272. error(1, errno, "sendto wrong length");
  273. }
  274. static void create_packet(void *buf, int seq_offset, int ack_offset,
  275. int payload_len, int fin)
  276. {
  277. memset(buf, 0, total_hdr_len);
  278. memset(buf + total_hdr_len, 'a', payload_len);
  279. fill_transportlayer(buf + tcp_offset, seq_offset, ack_offset,
  280. payload_len, fin);
  281. fill_networklayer(buf + ETH_HLEN, payload_len);
  282. fill_datalinklayer(buf);
  283. }
  284. /* send one extra flag, not first and not last pkt */
  285. static void send_flags(int fd, struct sockaddr_ll *daddr, int psh, int syn,
  286. int rst, int urg)
  287. {
  288. static char flag_buf[MAX_HDR_LEN + PAYLOAD_LEN];
  289. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  290. int payload_len, pkt_size, flag, i;
  291. struct tcphdr *tcph;
  292. payload_len = PAYLOAD_LEN * psh;
  293. pkt_size = total_hdr_len + payload_len;
  294. flag = NUM_PACKETS / 2;
  295. create_packet(flag_buf, flag * payload_len, 0, payload_len, 0);
  296. tcph = (struct tcphdr *)(flag_buf + tcp_offset);
  297. tcph->psh = psh;
  298. tcph->syn = syn;
  299. tcph->rst = rst;
  300. tcph->urg = urg;
  301. tcph->check = 0;
  302. tcph->check = tcp_checksum(tcph, payload_len);
  303. for (i = 0; i < NUM_PACKETS + 1; i++) {
  304. if (i == flag) {
  305. write_packet(fd, flag_buf, pkt_size, daddr);
  306. continue;
  307. }
  308. create_packet(buf, i * PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  309. write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
  310. }
  311. }
  312. /* Test for data of same length, smaller than previous
  313. * and of different lengths
  314. */
  315. static void send_data_pkts(int fd, struct sockaddr_ll *daddr,
  316. int payload_len1, int payload_len2)
  317. {
  318. static char buf[ETH_HLEN + IP_MAXPACKET];
  319. create_packet(buf, 0, 0, payload_len1, 0);
  320. write_packet(fd, buf, total_hdr_len + payload_len1, daddr);
  321. create_packet(buf, payload_len1, 0, payload_len2, 0);
  322. write_packet(fd, buf, total_hdr_len + payload_len2, daddr);
  323. }
  324. /* If incoming segments make tracked segment length exceed
  325. * legal IP datagram length, do not coalesce
  326. */
  327. static void send_large(int fd, struct sockaddr_ll *daddr, int remainder)
  328. {
  329. static char pkts[NUM_LARGE_PKT][TOTAL_HDR_LEN + MSS];
  330. static char last[TOTAL_HDR_LEN + MSS];
  331. static char new_seg[TOTAL_HDR_LEN + MSS];
  332. int i;
  333. for (i = 0; i < NUM_LARGE_PKT; i++)
  334. create_packet(pkts[i], i * MSS, 0, MSS, 0);
  335. create_packet(last, NUM_LARGE_PKT * MSS, 0, remainder, 0);
  336. create_packet(new_seg, (NUM_LARGE_PKT + 1) * MSS, 0, remainder, 0);
  337. for (i = 0; i < NUM_LARGE_PKT; i++)
  338. write_packet(fd, pkts[i], total_hdr_len + MSS, daddr);
  339. write_packet(fd, last, total_hdr_len + remainder, daddr);
  340. write_packet(fd, new_seg, total_hdr_len + remainder, daddr);
  341. }
  342. /* Pure acks and dup acks don't coalesce */
  343. static void send_ack(int fd, struct sockaddr_ll *daddr)
  344. {
  345. static char buf[MAX_HDR_LEN];
  346. create_packet(buf, 0, 0, 0, 0);
  347. write_packet(fd, buf, total_hdr_len, daddr);
  348. write_packet(fd, buf, total_hdr_len, daddr);
  349. create_packet(buf, 0, 1, 0, 0);
  350. write_packet(fd, buf, total_hdr_len, daddr);
  351. }
  352. static void recompute_packet(char *buf, char *no_ext, int extlen)
  353. {
  354. struct tcphdr *tcphdr = (struct tcphdr *)(buf + tcp_offset);
  355. struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
  356. struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
  357. memmove(buf, no_ext, total_hdr_len);
  358. memmove(buf + total_hdr_len + extlen,
  359. no_ext + total_hdr_len, PAYLOAD_LEN);
  360. tcphdr->doff = tcphdr->doff + (extlen / 4);
  361. tcphdr->check = 0;
  362. tcphdr->check = tcp_checksum(tcphdr, PAYLOAD_LEN + extlen);
  363. if (proto == PF_INET) {
  364. iph->tot_len = htons(ntohs(iph->tot_len) + extlen);
  365. iph->check = 0;
  366. iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
  367. } else {
  368. ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);
  369. }
  370. }
  371. static void tcp_write_options(char *buf, int kind, int ts)
  372. {
  373. struct tcp_option_ts {
  374. uint8_t kind;
  375. uint8_t len;
  376. uint32_t tsval;
  377. uint32_t tsecr;
  378. } *opt_ts = (void *)buf;
  379. struct tcp_option_window {
  380. uint8_t kind;
  381. uint8_t len;
  382. uint8_t shift;
  383. } *opt_window = (void *)buf;
  384. switch (kind) {
  385. case TCPOPT_NOP:
  386. buf[0] = TCPOPT_NOP;
  387. break;
  388. case TCPOPT_WINDOW:
  389. memset(opt_window, 0, sizeof(struct tcp_option_window));
  390. opt_window->kind = TCPOPT_WINDOW;
  391. opt_window->len = TCPOLEN_WINDOW;
  392. opt_window->shift = 0;
  393. break;
  394. case TCPOPT_TIMESTAMP:
  395. memset(opt_ts, 0, sizeof(struct tcp_option_ts));
  396. opt_ts->kind = TCPOPT_TIMESTAMP;
  397. opt_ts->len = TCPOLEN_TIMESTAMP;
  398. opt_ts->tsval = ts;
  399. opt_ts->tsecr = 0;
  400. break;
  401. default:
  402. error(1, 0, "unimplemented TCP option");
  403. break;
  404. }
  405. }
  406. /* TCP with options is always a permutation of {TS, NOP, NOP}.
  407. * Implement different orders to verify coalescing stops.
  408. */
  409. static void add_standard_tcp_options(char *buf, char *no_ext, int ts, int order)
  410. {
  411. switch (order) {
  412. case 0:
  413. tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);
  414. tcp_write_options(buf + total_hdr_len + 1, TCPOPT_NOP, 0);
  415. tcp_write_options(buf + total_hdr_len + 2 /* two NOP opts */,
  416. TCPOPT_TIMESTAMP, ts);
  417. break;
  418. case 1:
  419. tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);
  420. tcp_write_options(buf + total_hdr_len + 1,
  421. TCPOPT_TIMESTAMP, ts);
  422. tcp_write_options(buf + total_hdr_len + 1 + TCPOLEN_TIMESTAMP,
  423. TCPOPT_NOP, 0);
  424. break;
  425. case 2:
  426. tcp_write_options(buf + total_hdr_len, TCPOPT_TIMESTAMP, ts);
  427. tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 1,
  428. TCPOPT_NOP, 0);
  429. tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 2,
  430. TCPOPT_NOP, 0);
  431. break;
  432. default:
  433. error(1, 0, "unknown order");
  434. break;
  435. }
  436. recompute_packet(buf, no_ext, TCPOLEN_TSTAMP_APPA);
  437. }
  438. /* Packets with invalid checksum don't coalesce. */
  439. static void send_changed_checksum(int fd, struct sockaddr_ll *daddr)
  440. {
  441. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  442. struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);
  443. int pkt_size = total_hdr_len + PAYLOAD_LEN;
  444. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  445. write_packet(fd, buf, pkt_size, daddr);
  446. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  447. tcph->check = tcph->check - 1;
  448. write_packet(fd, buf, pkt_size, daddr);
  449. }
  450. /* Packets with non-consecutive sequence number don't coalesce.*/
  451. static void send_changed_seq(int fd, struct sockaddr_ll *daddr)
  452. {
  453. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  454. struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);
  455. int pkt_size = total_hdr_len + PAYLOAD_LEN;
  456. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  457. write_packet(fd, buf, pkt_size, daddr);
  458. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  459. tcph->seq = ntohl(htonl(tcph->seq) + 1);
  460. tcph->check = 0;
  461. tcph->check = tcp_checksum(tcph, PAYLOAD_LEN);
  462. write_packet(fd, buf, pkt_size, daddr);
  463. }
  464. /* Packet with different timestamp option or different timestamps
  465. * don't coalesce.
  466. */
  467. static void send_changed_ts(int fd, struct sockaddr_ll *daddr)
  468. {
  469. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  470. static char extpkt[sizeof(buf) + TCPOLEN_TSTAMP_APPA];
  471. int pkt_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;
  472. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  473. add_standard_tcp_options(extpkt, buf, 0, 0);
  474. write_packet(fd, extpkt, pkt_size, daddr);
  475. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  476. add_standard_tcp_options(extpkt, buf, 0, 0);
  477. write_packet(fd, extpkt, pkt_size, daddr);
  478. create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
  479. add_standard_tcp_options(extpkt, buf, 100, 0);
  480. write_packet(fd, extpkt, pkt_size, daddr);
  481. create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
  482. add_standard_tcp_options(extpkt, buf, 100, 1);
  483. write_packet(fd, extpkt, pkt_size, daddr);
  484. create_packet(buf, PAYLOAD_LEN * 4, 0, PAYLOAD_LEN, 0);
  485. add_standard_tcp_options(extpkt, buf, 100, 2);
  486. write_packet(fd, extpkt, pkt_size, daddr);
  487. }
  488. /* Packet with different tcp options don't coalesce. */
  489. static void send_diff_opt(int fd, struct sockaddr_ll *daddr)
  490. {
  491. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  492. static char extpkt1[sizeof(buf) + TCPOLEN_TSTAMP_APPA];
  493. static char extpkt2[sizeof(buf) + TCPOLEN_MAXSEG];
  494. int extpkt1_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;
  495. int extpkt2_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_MAXSEG;
  496. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  497. add_standard_tcp_options(extpkt1, buf, 0, 0);
  498. write_packet(fd, extpkt1, extpkt1_size, daddr);
  499. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  500. add_standard_tcp_options(extpkt1, buf, 0, 0);
  501. write_packet(fd, extpkt1, extpkt1_size, daddr);
  502. create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
  503. tcp_write_options(extpkt2 + MAX_HDR_LEN, TCPOPT_NOP, 0);
  504. tcp_write_options(extpkt2 + MAX_HDR_LEN + 1, TCPOPT_WINDOW, 0);
  505. recompute_packet(extpkt2, buf, TCPOLEN_WINDOW + 1);
  506. write_packet(fd, extpkt2, extpkt2_size, daddr);
  507. }
  508. static void add_ipv4_ts_option(void *buf, void *optpkt)
  509. {
  510. struct ip_timestamp *ts = (struct ip_timestamp *)(optpkt + tcp_offset);
  511. int optlen = sizeof(struct ip_timestamp);
  512. struct iphdr *iph;
  513. if (optlen % 4)
  514. error(1, 0, "ipv4 timestamp length is not a multiple of 4B");
  515. ts->ipt_code = IPOPT_TS;
  516. ts->ipt_len = optlen;
  517. ts->ipt_ptr = 5;
  518. ts->ipt_flg = IPOPT_TS_TSONLY;
  519. memcpy(optpkt, buf, tcp_offset);
  520. memcpy(optpkt + tcp_offset + optlen, buf + tcp_offset,
  521. sizeof(struct tcphdr) + PAYLOAD_LEN);
  522. iph = (struct iphdr *)(optpkt + ETH_HLEN);
  523. iph->ihl = 5 + (optlen / 4);
  524. iph->tot_len = htons(ntohs(iph->tot_len) + optlen);
  525. iph->check = 0;
  526. iph->check = checksum_fold(iph, sizeof(struct iphdr) + optlen, 0);
  527. }
  528. static void add_ipv6_exthdr(void *buf, void *optpkt, __u8 exthdr_type, char *ext_payload)
  529. {
  530. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr *)(optpkt + tcp_offset);
  531. struct ipv6hdr *iph = (struct ipv6hdr *)(optpkt + ETH_HLEN);
  532. char *exthdr_payload_start = (char *)(exthdr + 1);
  533. exthdr->hdrlen = 0;
  534. exthdr->nexthdr = IPPROTO_TCP;
  535. memcpy(exthdr_payload_start, ext_payload, MIN_EXTHDR_SIZE - sizeof(*exthdr));
  536. memcpy(optpkt, buf, tcp_offset);
  537. memcpy(optpkt + tcp_offset + MIN_EXTHDR_SIZE, buf + tcp_offset,
  538. sizeof(struct tcphdr) + PAYLOAD_LEN);
  539. iph->nexthdr = exthdr_type;
  540. iph->payload_len = htons(ntohs(iph->payload_len) + MIN_EXTHDR_SIZE);
  541. }
  542. static void fix_ip4_checksum(struct iphdr *iph)
  543. {
  544. iph->check = 0;
  545. iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
  546. }
  547. static void send_flush_id_case(int fd, struct sockaddr_ll *daddr, int tcase)
  548. {
  549. static char buf1[MAX_HDR_LEN + PAYLOAD_LEN];
  550. static char buf2[MAX_HDR_LEN + PAYLOAD_LEN];
  551. static char buf3[MAX_HDR_LEN + PAYLOAD_LEN];
  552. bool send_three = false;
  553. struct iphdr *iph1;
  554. struct iphdr *iph2;
  555. struct iphdr *iph3;
  556. iph1 = (struct iphdr *)(buf1 + ETH_HLEN);
  557. iph2 = (struct iphdr *)(buf2 + ETH_HLEN);
  558. iph3 = (struct iphdr *)(buf3 + ETH_HLEN);
  559. create_packet(buf1, 0, 0, PAYLOAD_LEN, 0);
  560. create_packet(buf2, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  561. create_packet(buf3, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
  562. switch (tcase) {
  563. case 0: /* DF=1, Incrementing - should coalesce */
  564. iph1->frag_off |= htons(IP_DF);
  565. iph1->id = htons(8);
  566. iph2->frag_off |= htons(IP_DF);
  567. iph2->id = htons(9);
  568. break;
  569. case 1: /* DF=1, Fixed - should coalesce */
  570. iph1->frag_off |= htons(IP_DF);
  571. iph1->id = htons(8);
  572. iph2->frag_off |= htons(IP_DF);
  573. iph2->id = htons(8);
  574. break;
  575. case 2: /* DF=0, Incrementing - should coalesce */
  576. iph1->frag_off &= ~htons(IP_DF);
  577. iph1->id = htons(8);
  578. iph2->frag_off &= ~htons(IP_DF);
  579. iph2->id = htons(9);
  580. break;
  581. case 3: /* DF=0, Fixed - should not coalesce */
  582. iph1->frag_off &= ~htons(IP_DF);
  583. iph1->id = htons(8);
  584. iph2->frag_off &= ~htons(IP_DF);
  585. iph2->id = htons(8);
  586. break;
  587. case 4: /* DF=1, two packets incrementing, and one fixed - should
  588. * coalesce only the first two packets
  589. */
  590. iph1->frag_off |= htons(IP_DF);
  591. iph1->id = htons(8);
  592. iph2->frag_off |= htons(IP_DF);
  593. iph2->id = htons(9);
  594. iph3->frag_off |= htons(IP_DF);
  595. iph3->id = htons(9);
  596. send_three = true;
  597. break;
  598. case 5: /* DF=1, two packets fixed, and one incrementing - should
  599. * coalesce only the first two packets
  600. */
  601. iph1->frag_off |= htons(IP_DF);
  602. iph1->id = htons(8);
  603. iph2->frag_off |= htons(IP_DF);
  604. iph2->id = htons(8);
  605. iph3->frag_off |= htons(IP_DF);
  606. iph3->id = htons(9);
  607. send_three = true;
  608. break;
  609. }
  610. fix_ip4_checksum(iph1);
  611. fix_ip4_checksum(iph2);
  612. write_packet(fd, buf1, total_hdr_len + PAYLOAD_LEN, daddr);
  613. write_packet(fd, buf2, total_hdr_len + PAYLOAD_LEN, daddr);
  614. if (send_three) {
  615. fix_ip4_checksum(iph3);
  616. write_packet(fd, buf3, total_hdr_len + PAYLOAD_LEN, daddr);
  617. }
  618. }
  619. static void test_flush_id(int fd, struct sockaddr_ll *daddr, char *fin_pkt)
  620. {
  621. for (int i = 0; i < num_flush_id_cases; i++) {
  622. sleep(1);
  623. send_flush_id_case(fd, daddr, i);
  624. sleep(1);
  625. write_packet(fd, fin_pkt, total_hdr_len, daddr);
  626. }
  627. }
  628. static void send_ipv6_exthdr(int fd, struct sockaddr_ll *daddr, char *ext_data1, char *ext_data2)
  629. {
  630. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  631. static char exthdr_pck[sizeof(buf) + MIN_EXTHDR_SIZE];
  632. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  633. add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_HOPOPTS, ext_data1);
  634. write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr);
  635. create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);
  636. add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_HOPOPTS, ext_data2);
  637. write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr);
  638. }
  639. /* IPv4 options shouldn't coalesce */
  640. static void send_ip_options(int fd, struct sockaddr_ll *daddr)
  641. {
  642. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  643. static char optpkt[sizeof(buf) + sizeof(struct ip_timestamp)];
  644. int optlen = sizeof(struct ip_timestamp);
  645. int pkt_size = total_hdr_len + PAYLOAD_LEN + optlen;
  646. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  647. write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
  648. create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);
  649. add_ipv4_ts_option(buf, optpkt);
  650. write_packet(fd, optpkt, pkt_size, daddr);
  651. create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
  652. write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
  653. }
  654. /* IPv4 fragments shouldn't coalesce */
  655. static void send_fragment4(int fd, struct sockaddr_ll *daddr)
  656. {
  657. static char buf[IP_MAXPACKET];
  658. struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
  659. int pkt_size = total_hdr_len + PAYLOAD_LEN;
  660. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  661. write_packet(fd, buf, pkt_size, daddr);
  662. /* Once fragmented, packet would retain the total_len.
  663. * Tcp header is prepared as if rest of data is in follow-up frags,
  664. * but follow up frags aren't actually sent.
  665. */
  666. memset(buf + total_hdr_len, 'a', PAYLOAD_LEN * 2);
  667. fill_transportlayer(buf + tcp_offset, PAYLOAD_LEN, 0, PAYLOAD_LEN * 2, 0);
  668. fill_networklayer(buf + ETH_HLEN, PAYLOAD_LEN);
  669. fill_datalinklayer(buf);
  670. iph->frag_off = htons(0x6000); // DF = 1, MF = 1
  671. iph->check = 0;
  672. iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
  673. write_packet(fd, buf, pkt_size, daddr);
  674. }
  675. /* IPv4 packets with different ttl don't coalesce.*/
  676. static void send_changed_ttl(int fd, struct sockaddr_ll *daddr)
  677. {
  678. int pkt_size = total_hdr_len + PAYLOAD_LEN;
  679. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  680. struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
  681. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  682. write_packet(fd, buf, pkt_size, daddr);
  683. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  684. iph->ttl = 7;
  685. iph->check = 0;
  686. iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
  687. write_packet(fd, buf, pkt_size, daddr);
  688. }
  689. /* Packets with different tos don't coalesce.*/
  690. static void send_changed_tos(int fd, struct sockaddr_ll *daddr)
  691. {
  692. int pkt_size = total_hdr_len + PAYLOAD_LEN;
  693. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  694. struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
  695. struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
  696. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  697. write_packet(fd, buf, pkt_size, daddr);
  698. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  699. if (proto == PF_INET) {
  700. iph->tos = 1;
  701. iph->check = 0;
  702. iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
  703. } else if (proto == PF_INET6) {
  704. ip6h->priority = 0xf;
  705. }
  706. write_packet(fd, buf, pkt_size, daddr);
  707. }
  708. /* Packets with different ECN don't coalesce.*/
  709. static void send_changed_ECN(int fd, struct sockaddr_ll *daddr)
  710. {
  711. int pkt_size = total_hdr_len + PAYLOAD_LEN;
  712. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  713. struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
  714. create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
  715. write_packet(fd, buf, pkt_size, daddr);
  716. create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
  717. if (proto == PF_INET) {
  718. buf[ETH_HLEN + 1] ^= 0x2; // ECN set to 10
  719. iph->check = 0;
  720. iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
  721. } else {
  722. buf[ETH_HLEN + 1] ^= 0x20; // ECN set to 10
  723. }
  724. write_packet(fd, buf, pkt_size, daddr);
  725. }
  726. /* IPv6 fragments and packets with extensions don't coalesce.*/
  727. static void send_fragment6(int fd, struct sockaddr_ll *daddr)
  728. {
  729. static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
  730. static char extpkt[MAX_HDR_LEN + PAYLOAD_LEN +
  731. sizeof(struct ip6_frag)];
  732. struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
  733. struct ip6_frag *frag = (void *)(extpkt + tcp_offset);
  734. int extlen = sizeof(struct ip6_frag);
  735. int bufpkt_len = total_hdr_len + PAYLOAD_LEN;
  736. int extpkt_len = bufpkt_len + extlen;
  737. int i;
  738. for (i = 0; i < 2; i++) {
  739. create_packet(buf, PAYLOAD_LEN * i, 0, PAYLOAD_LEN, 0);
  740. write_packet(fd, buf, bufpkt_len, daddr);
  741. }
  742. sleep(1);
  743. create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
  744. memset(extpkt, 0, extpkt_len);
  745. ip6h->nexthdr = IPPROTO_FRAGMENT;
  746. ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);
  747. frag->ip6f_nxt = IPPROTO_TCP;
  748. memcpy(extpkt, buf, tcp_offset);
  749. memcpy(extpkt + tcp_offset + extlen, buf + tcp_offset,
  750. sizeof(struct tcphdr) + PAYLOAD_LEN);
  751. write_packet(fd, extpkt, extpkt_len, daddr);
  752. create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
  753. write_packet(fd, buf, bufpkt_len, daddr);
  754. }
  755. static void bind_packetsocket(int fd)
  756. {
  757. struct sockaddr_ll daddr = {};
  758. daddr.sll_family = AF_PACKET;
  759. daddr.sll_protocol = ethhdr_proto;
  760. daddr.sll_ifindex = if_nametoindex(ifname);
  761. if (daddr.sll_ifindex == 0)
  762. error(1, errno, "if_nametoindex");
  763. if (bind(fd, (void *)&daddr, sizeof(daddr)) < 0)
  764. error(1, errno, "could not bind socket");
  765. }
  766. static void set_timeout(int fd)
  767. {
  768. struct timeval timeout;
  769. timeout.tv_sec = 3;
  770. timeout.tv_usec = 0;
  771. if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
  772. sizeof(timeout)) < 0)
  773. error(1, errno, "cannot set timeout, setsockopt failed");
  774. }
  775. static void check_recv_pkts(int fd, int *correct_payload,
  776. int correct_num_pkts)
  777. {
  778. static char buffer[IP_MAXPACKET + ETH_HLEN + 1];
  779. struct iphdr *iph = (struct iphdr *)(buffer + ETH_HLEN);
  780. struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + ETH_HLEN);
  781. struct tcphdr *tcph;
  782. bool bad_packet = false;
  783. int tcp_ext_len = 0;
  784. int ip_ext_len = 0;
  785. int pkt_size = -1;
  786. int data_len = 0;
  787. int num_pkt = 0;
  788. int i;
  789. vlog("Expected {");
  790. for (i = 0; i < correct_num_pkts; i++)
  791. vlog("%d ", correct_payload[i]);
  792. vlog("}, Total %d packets\nReceived {", correct_num_pkts);
  793. while (1) {
  794. ip_ext_len = 0;
  795. pkt_size = recv(fd, buffer, IP_MAXPACKET + ETH_HLEN + 1, 0);
  796. if (pkt_size < 0)
  797. error(1, errno, "could not receive");
  798. if (iph->version == 4)
  799. ip_ext_len = (iph->ihl - 5) * 4;
  800. else if (ip6h->version == 6 && ip6h->nexthdr != IPPROTO_TCP)
  801. ip_ext_len = MIN_EXTHDR_SIZE;
  802. tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len);
  803. if (tcph->fin)
  804. break;
  805. tcp_ext_len = (tcph->doff - 5) * 4;
  806. data_len = pkt_size - total_hdr_len - tcp_ext_len - ip_ext_len;
  807. /* Min ethernet frame payload is 46(ETH_ZLEN - ETH_HLEN) by RFC 802.3.
  808. * Ipv4/tcp packets without at least 6 bytes of data will be padded.
  809. * Packet sockets are protocol agnostic, and will not trim the padding.
  810. */
  811. if (pkt_size == ETH_ZLEN && iph->version == 4) {
  812. data_len = ntohs(iph->tot_len)
  813. - sizeof(struct tcphdr) - sizeof(struct iphdr);
  814. }
  815. vlog("%d ", data_len);
  816. if (data_len != correct_payload[num_pkt]) {
  817. vlog("[!=%d]", correct_payload[num_pkt]);
  818. bad_packet = true;
  819. }
  820. num_pkt++;
  821. }
  822. vlog("}, Total %d packets.\n", num_pkt);
  823. if (num_pkt != correct_num_pkts)
  824. error(1, 0, "incorrect number of packets");
  825. if (bad_packet)
  826. error(1, 0, "incorrect packet geometry");
  827. printf("Test succeeded\n\n");
  828. }
  829. static void gro_sender(void)
  830. {
  831. static char fin_pkt[MAX_HDR_LEN];
  832. struct sockaddr_ll daddr = {};
  833. int txfd = -1;
  834. txfd = socket(PF_PACKET, SOCK_RAW, IPPROTO_RAW);
  835. if (txfd < 0)
  836. error(1, errno, "socket creation");
  837. memset(&daddr, 0, sizeof(daddr));
  838. daddr.sll_ifindex = if_nametoindex(ifname);
  839. if (daddr.sll_ifindex == 0)
  840. error(1, errno, "if_nametoindex");
  841. daddr.sll_family = AF_PACKET;
  842. memcpy(daddr.sll_addr, dst_mac, ETH_ALEN);
  843. daddr.sll_halen = ETH_ALEN;
  844. create_packet(fin_pkt, PAYLOAD_LEN * 2, 0, 0, 1);
  845. if (strcmp(testname, "data") == 0) {
  846. send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN);
  847. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  848. send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN / 2);
  849. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  850. send_data_pkts(txfd, &daddr, PAYLOAD_LEN / 2, PAYLOAD_LEN);
  851. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  852. } else if (strcmp(testname, "ack") == 0) {
  853. send_ack(txfd, &daddr);
  854. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  855. } else if (strcmp(testname, "flags") == 0) {
  856. send_flags(txfd, &daddr, 1, 0, 0, 0);
  857. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  858. send_flags(txfd, &daddr, 0, 1, 0, 0);
  859. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  860. send_flags(txfd, &daddr, 0, 0, 1, 0);
  861. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  862. send_flags(txfd, &daddr, 0, 0, 0, 1);
  863. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  864. } else if (strcmp(testname, "tcp") == 0) {
  865. send_changed_checksum(txfd, &daddr);
  866. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  867. send_changed_seq(txfd, &daddr);
  868. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  869. send_changed_ts(txfd, &daddr);
  870. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  871. send_diff_opt(txfd, &daddr);
  872. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  873. } else if (strcmp(testname, "ip") == 0) {
  874. send_changed_ECN(txfd, &daddr);
  875. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  876. send_changed_tos(txfd, &daddr);
  877. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  878. if (proto == PF_INET) {
  879. /* Modified packets may be received out of order.
  880. * Sleep function added to enforce test boundaries
  881. * so that fin pkts are not received prior to other pkts.
  882. */
  883. sleep(1);
  884. send_changed_ttl(txfd, &daddr);
  885. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  886. sleep(1);
  887. send_ip_options(txfd, &daddr);
  888. sleep(1);
  889. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  890. sleep(1);
  891. send_fragment4(txfd, &daddr);
  892. sleep(1);
  893. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  894. test_flush_id(txfd, &daddr, fin_pkt);
  895. } else if (proto == PF_INET6) {
  896. sleep(1);
  897. send_fragment6(txfd, &daddr);
  898. sleep(1);
  899. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  900. sleep(1);
  901. /* send IPv6 packets with ext header with same payload */
  902. send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_1);
  903. sleep(1);
  904. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  905. sleep(1);
  906. /* send IPv6 packets with ext header with different payload */
  907. send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_2);
  908. sleep(1);
  909. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  910. }
  911. } else if (strcmp(testname, "large") == 0) {
  912. /* 20 is the difference between min iphdr size
  913. * and min ipv6hdr size. Like MAX_HDR_SIZE,
  914. * MAX_PAYLOAD is defined with the larger header of the two.
  915. */
  916. int offset = proto == PF_INET ? 20 : 0;
  917. int remainder = (MAX_PAYLOAD + offset) % MSS;
  918. send_large(txfd, &daddr, remainder);
  919. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  920. send_large(txfd, &daddr, remainder + 1);
  921. write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
  922. } else {
  923. error(1, 0, "Unknown testcase");
  924. }
  925. if (close(txfd))
  926. error(1, errno, "socket close");
  927. }
  928. static void gro_receiver(void)
  929. {
  930. static int correct_payload[NUM_PACKETS];
  931. int rxfd = -1;
  932. rxfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_NONE));
  933. if (rxfd < 0)
  934. error(1, 0, "socket creation");
  935. setup_sock_filter(rxfd);
  936. set_timeout(rxfd);
  937. bind_packetsocket(rxfd);
  938. memset(correct_payload, 0, sizeof(correct_payload));
  939. if (strcmp(testname, "data") == 0) {
  940. printf("pure data packet of same size: ");
  941. correct_payload[0] = PAYLOAD_LEN * 2;
  942. check_recv_pkts(rxfd, correct_payload, 1);
  943. printf("large data packets followed by a smaller one: ");
  944. correct_payload[0] = PAYLOAD_LEN * 1.5;
  945. check_recv_pkts(rxfd, correct_payload, 1);
  946. printf("small data packets followed by a larger one: ");
  947. correct_payload[0] = PAYLOAD_LEN / 2;
  948. correct_payload[1] = PAYLOAD_LEN;
  949. check_recv_pkts(rxfd, correct_payload, 2);
  950. } else if (strcmp(testname, "ack") == 0) {
  951. printf("duplicate ack and pure ack: ");
  952. check_recv_pkts(rxfd, correct_payload, 3);
  953. } else if (strcmp(testname, "flags") == 0) {
  954. correct_payload[0] = PAYLOAD_LEN * 3;
  955. correct_payload[1] = PAYLOAD_LEN * 2;
  956. printf("psh flag ends coalescing: ");
  957. check_recv_pkts(rxfd, correct_payload, 2);
  958. correct_payload[0] = PAYLOAD_LEN * 2;
  959. correct_payload[1] = 0;
  960. correct_payload[2] = PAYLOAD_LEN * 2;
  961. printf("syn flag ends coalescing: ");
  962. check_recv_pkts(rxfd, correct_payload, 3);
  963. printf("rst flag ends coalescing: ");
  964. check_recv_pkts(rxfd, correct_payload, 3);
  965. printf("urg flag ends coalescing: ");
  966. check_recv_pkts(rxfd, correct_payload, 3);
  967. } else if (strcmp(testname, "tcp") == 0) {
  968. correct_payload[0] = PAYLOAD_LEN;
  969. correct_payload[1] = PAYLOAD_LEN;
  970. correct_payload[2] = PAYLOAD_LEN;
  971. correct_payload[3] = PAYLOAD_LEN;
  972. printf("changed checksum does not coalesce: ");
  973. check_recv_pkts(rxfd, correct_payload, 2);
  974. printf("Wrong Seq number doesn't coalesce: ");
  975. check_recv_pkts(rxfd, correct_payload, 2);
  976. printf("Different timestamp doesn't coalesce: ");
  977. correct_payload[0] = PAYLOAD_LEN * 2;
  978. check_recv_pkts(rxfd, correct_payload, 4);
  979. printf("Different options doesn't coalesce: ");
  980. correct_payload[0] = PAYLOAD_LEN * 2;
  981. check_recv_pkts(rxfd, correct_payload, 2);
  982. } else if (strcmp(testname, "ip") == 0) {
  983. correct_payload[0] = PAYLOAD_LEN;
  984. correct_payload[1] = PAYLOAD_LEN;
  985. printf("different ECN doesn't coalesce: ");
  986. check_recv_pkts(rxfd, correct_payload, 2);
  987. printf("different tos doesn't coalesce: ");
  988. check_recv_pkts(rxfd, correct_payload, 2);
  989. if (proto == PF_INET) {
  990. printf("different ttl doesn't coalesce: ");
  991. check_recv_pkts(rxfd, correct_payload, 2);
  992. printf("ip options doesn't coalesce: ");
  993. correct_payload[2] = PAYLOAD_LEN;
  994. check_recv_pkts(rxfd, correct_payload, 3);
  995. printf("fragmented ip4 doesn't coalesce: ");
  996. check_recv_pkts(rxfd, correct_payload, 2);
  997. /* is_atomic checks */
  998. printf("DF=1, Incrementing - should coalesce: ");
  999. correct_payload[0] = PAYLOAD_LEN * 2;
  1000. check_recv_pkts(rxfd, correct_payload, 1);
  1001. printf("DF=1, Fixed - should coalesce: ");
  1002. correct_payload[0] = PAYLOAD_LEN * 2;
  1003. check_recv_pkts(rxfd, correct_payload, 1);
  1004. printf("DF=0, Incrementing - should coalesce: ");
  1005. correct_payload[0] = PAYLOAD_LEN * 2;
  1006. check_recv_pkts(rxfd, correct_payload, 1);
  1007. printf("DF=0, Fixed - should not coalesce: ");
  1008. correct_payload[0] = PAYLOAD_LEN;
  1009. correct_payload[1] = PAYLOAD_LEN;
  1010. check_recv_pkts(rxfd, correct_payload, 2);
  1011. printf("DF=1, 2 Incrementing and one fixed - should coalesce only first 2 packets: ");
  1012. correct_payload[0] = PAYLOAD_LEN * 2;
  1013. correct_payload[1] = PAYLOAD_LEN;
  1014. check_recv_pkts(rxfd, correct_payload, 2);
  1015. printf("DF=1, 2 Fixed and one incrementing - should coalesce only first 2 packets: ");
  1016. correct_payload[0] = PAYLOAD_LEN * 2;
  1017. correct_payload[1] = PAYLOAD_LEN;
  1018. check_recv_pkts(rxfd, correct_payload, 2);
  1019. } else if (proto == PF_INET6) {
  1020. /* GRO doesn't check for ipv6 hop limit when flushing.
  1021. * Hence no corresponding test to the ipv4 case.
  1022. */
  1023. printf("fragmented ip6 doesn't coalesce: ");
  1024. correct_payload[0] = PAYLOAD_LEN * 2;
  1025. correct_payload[1] = PAYLOAD_LEN;
  1026. correct_payload[2] = PAYLOAD_LEN;
  1027. check_recv_pkts(rxfd, correct_payload, 3);
  1028. printf("ipv6 with ext header does coalesce: ");
  1029. correct_payload[0] = PAYLOAD_LEN * 2;
  1030. check_recv_pkts(rxfd, correct_payload, 1);
  1031. printf("ipv6 with ext header with different payloads doesn't coalesce: ");
  1032. correct_payload[0] = PAYLOAD_LEN;
  1033. correct_payload[1] = PAYLOAD_LEN;
  1034. check_recv_pkts(rxfd, correct_payload, 2);
  1035. }
  1036. } else if (strcmp(testname, "large") == 0) {
  1037. int offset = proto == PF_INET ? 20 : 0;
  1038. int remainder = (MAX_PAYLOAD + offset) % MSS;
  1039. correct_payload[0] = (MAX_PAYLOAD + offset);
  1040. correct_payload[1] = remainder;
  1041. printf("Shouldn't coalesce if exceed IP max pkt size: ");
  1042. check_recv_pkts(rxfd, correct_payload, 2);
  1043. /* last segment sent individually, doesn't start new segment */
  1044. correct_payload[0] = correct_payload[0] - remainder;
  1045. correct_payload[1] = remainder + 1;
  1046. correct_payload[2] = remainder + 1;
  1047. check_recv_pkts(rxfd, correct_payload, 3);
  1048. } else {
  1049. error(1, 0, "Test case error, should never trigger");
  1050. }
  1051. if (close(rxfd))
  1052. error(1, 0, "socket close");
  1053. }
  1054. static void parse_args(int argc, char **argv)
  1055. {
  1056. static const struct option opts[] = {
  1057. { "daddr", required_argument, NULL, 'd' },
  1058. { "dmac", required_argument, NULL, 'D' },
  1059. { "iface", required_argument, NULL, 'i' },
  1060. { "ipv4", no_argument, NULL, '4' },
  1061. { "ipv6", no_argument, NULL, '6' },
  1062. { "rx", no_argument, NULL, 'r' },
  1063. { "saddr", required_argument, NULL, 's' },
  1064. { "smac", required_argument, NULL, 'S' },
  1065. { "test", required_argument, NULL, 't' },
  1066. { "verbose", no_argument, NULL, 'v' },
  1067. { 0, 0, 0, 0 }
  1068. };
  1069. int c;
  1070. while ((c = getopt_long(argc, argv, "46d:D:i:rs:S:t:v", opts, NULL)) != -1) {
  1071. switch (c) {
  1072. case '4':
  1073. proto = PF_INET;
  1074. ethhdr_proto = htons(ETH_P_IP);
  1075. break;
  1076. case '6':
  1077. proto = PF_INET6;
  1078. ethhdr_proto = htons(ETH_P_IPV6);
  1079. break;
  1080. case 'd':
  1081. addr4_dst = addr6_dst = optarg;
  1082. break;
  1083. case 'D':
  1084. dmac = optarg;
  1085. break;
  1086. case 'i':
  1087. ifname = optarg;
  1088. break;
  1089. case 'r':
  1090. tx_socket = false;
  1091. break;
  1092. case 's':
  1093. addr4_src = addr6_src = optarg;
  1094. break;
  1095. case 'S':
  1096. smac = optarg;
  1097. break;
  1098. case 't':
  1099. testname = optarg;
  1100. break;
  1101. case 'v':
  1102. verbose = true;
  1103. break;
  1104. default:
  1105. error(1, 0, "%s invalid option %c\n", __func__, c);
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. int main(int argc, char **argv)
  1111. {
  1112. parse_args(argc, argv);
  1113. if (proto == PF_INET) {
  1114. tcp_offset = ETH_HLEN + sizeof(struct iphdr);
  1115. total_hdr_len = tcp_offset + sizeof(struct tcphdr);
  1116. } else if (proto == PF_INET6) {
  1117. tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr);
  1118. total_hdr_len = MAX_HDR_LEN;
  1119. } else {
  1120. error(1, 0, "Protocol family is not ipv4 or ipv6");
  1121. }
  1122. read_MAC(src_mac, smac);
  1123. read_MAC(dst_mac, dmac);
  1124. if (tx_socket)
  1125. gro_sender();
  1126. else
  1127. gro_receiver();
  1128. fprintf(stderr, "Gro::%s test passed.\n", testname);
  1129. return 0;
  1130. }