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