xref: /linux/tools/testing/selftests/net/lib/gro.c (revision 9a84a4047df79f7f58a4915abaf09d1089103233)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This testsuite provides conformance testing for GRO coalescing.
4  *
5  * Test cases:
6  *
7  * data_*:
8  *  Data packets of the same size and same header setup with correct
9  *  sequence numbers coalesce. The one exception being the last data
10  *  packet coalesced: it can be smaller than the rest and coalesced
11  *  as long as it is in the same flow.
12  *   - data_same:    same size packets coalesce
13  *   - data_lrg_sml:   large then small coalesces
14  *   - data_lrg_1byte: large then 1 byte coalesces (Ethernet padding)
15  *   - data_sml_lrg:   small then large doesn't coalesce
16  *   - data_burst:   two bursts of two, separated by 100ms
17  *
18  * ack:
19  *  Pure ACK does not coalesce.
20  *
21  * flags_*:
22  *  No packets with PSH, SYN, URG, RST, CWR set will be coalesced.
23  *   - flags_psh, flags_syn, flags_rst, flags_urg, flags_cwr
24  *
25  * tcp_*:
26  *  Packets with incorrect checksum, non-consecutive seqno and
27  *  different TCP header options shouldn't coalesce. Nit: given that
28  *  some extension headers have paddings, such as timestamp, headers
29  *  that are padded differently would not be coalesced.
30  *   - tcp_csum: incorrect checksum
31  *   - tcp_seq:  non-consecutive sequence numbers
32  *   - tcp_ts:   different timestamps
33  *   - tcp_opt:  different TCP options
34  *
35  * ip_*:
36  *  Packets with different (ECN, TTL, TOS) header, IP options or
37  *  IP fragments shouldn't coalesce.
38  *   - ip_ecn, ip_tos:            shared between IPv4/IPv6
39  *   - ip_ttl, ip_opt, ip_frag4:  IPv4 only
40  *   - ip_id_df*:                 IPv4 IP ID field coalescing tests
41  *   - ip_frag6, ip_v6ext_*:      IPv6 only
42  *
43  * large_*:
44  *  Packets larger than GRO_MAX_SIZE packets shouldn't coalesce.
45  *   - large_max: exceeding max size
46  *   - large_rem: remainder handling
47  *
48  * single, capacity:
49  *  Boring cases used to test coalescing machinery itself and stats
50  *  more than protocol behavior.
51  *
52  * MSS is defined as 4096 - header because if it is too small
53  * (i.e. 1500 MTU - header), it will result in many packets,
54  * increasing the "large" test case's flakiness. This is because
55  * due to time sensitivity in the coalescing window, the receiver
56  * may not coalesce all of the packets.
57  *
58  * Note the timing issue applies to all of the test cases, so some
59  * flakiness is to be expected.
60  *
61  */
62 
63 #define _GNU_SOURCE
64 
65 #include <arpa/inet.h>
66 #include <errno.h>
67 #include <error.h>
68 #include <getopt.h>
69 #include <linux/filter.h>
70 #include <linux/if_packet.h>
71 #include <linux/ipv6.h>
72 #include <linux/net_tstamp.h>
73 #include <net/ethernet.h>
74 #include <net/if.h>
75 #include <netinet/in.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
78 #include <netinet/tcp.h>
79 #include <stdbool.h>
80 #include <stddef.h>
81 #include <stdio.h>
82 #include <stdarg.h>
83 #include <string.h>
84 #include <time.h>
85 #include <unistd.h>
86 
87 #include "kselftest.h"
88 #include "ksft.h"
89 
90 #define DPORT 8000
91 #define SPORT 1500
92 #define PAYLOAD_LEN 100
93 #define NUM_PACKETS 4
94 #define START_SEQ 100
95 #define START_ACK 100
96 #define ETH_P_NONE 0
97 #define ASSUMED_MTU 4096
98 #define MAX_MSS (ASSUMED_MTU - sizeof(struct iphdr) - sizeof(struct tcphdr))
99 #define MAX_HDR_LEN \
100 	(ETH_HLEN + sizeof(struct ipv6hdr) * 2 + sizeof(struct tcphdr))
101 #define MAX_LARGE_PKT_CNT ((IP_MAXPACKET - (MAX_HDR_LEN - ETH_HLEN)) /	\
102 			   (ASSUMED_MTU - (MAX_HDR_LEN - ETH_HLEN)))
103 #define MIN_EXTHDR_SIZE 8
104 #define EXT_PAYLOAD_1 "\x00\x00\x00\x00\x00\x00"
105 #define EXT_PAYLOAD_2 "\x11\x11\x11\x11\x11\x11"
106 
107 #define ipv6_optlen(p)  (((p)->hdrlen+1) << 3) /* calculate IPv6 extension header len */
108 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
109 
110 enum flush_id_case {
111 	FLUSH_ID_DF1_INC,
112 	FLUSH_ID_DF1_FIXED,
113 	FLUSH_ID_DF0_INC,
114 	FLUSH_ID_DF0_FIXED,
115 	FLUSH_ID_DF1_INC_FIXED,
116 	FLUSH_ID_DF1_FIXED_INC,
117 };
118 
119 static const char *addr6_src = "fdaa::2";
120 static const char *addr6_dst = "fdaa::1";
121 static const char *addr4_src = "192.168.1.200";
122 static const char *addr4_dst = "192.168.1.100";
123 static int proto = -1;
124 static uint8_t src_mac[ETH_ALEN], dst_mac[ETH_ALEN];
125 static char *testname = "data";
126 static char *ifname = "eth0";
127 static char *smac = "aa:00:00:00:00:02";
128 static char *dmac = "aa:00:00:00:00:01";
129 static bool verbose;
130 static bool tx_socket = true;
131 static int tcp_offset = -1;
132 static int total_hdr_len = -1;
133 static int ethhdr_proto = -1;
134 static bool ipip;
135 static bool ip6ip6;
136 static uint64_t txtime_ns;
137 static int num_flows = 4;
138 static bool order_check;
139 
140 #define CAPACITY_PAYLOAD_LEN 200
141 
142 #define TXTIME_DELAY_MS 5
143 
144 /* Max TCP payload that GRO will coalesce. The outer header overhead
145  * varies by encapsulation, reducing the effective max payload.
146  */
147 static int max_payload(void)
148 {
149 	return IP_MAXPACKET - (total_hdr_len - ETH_HLEN);
150 }
151 
152 static int calc_mss(void)
153 {
154 	return ASSUMED_MTU - (total_hdr_len - ETH_HLEN);
155 }
156 
157 static int num_large_pkt(void)
158 {
159 	return max_payload() / calc_mss();
160 }
161 
162 static void vlog(const char *fmt, ...)
163 {
164 	va_list args;
165 
166 	if (verbose) {
167 		va_start(args, fmt);
168 		vfprintf(stderr, fmt, args);
169 		va_end(args);
170 	}
171 }
172 
173 static void setup_sock_filter(int fd)
174 {
175 	const int dport_off = tcp_offset + offsetof(struct tcphdr, dest);
176 	const int ethproto_off = offsetof(struct ethhdr, h_proto);
177 	int optlen = 0;
178 	int ipproto_off, opt_ipproto_off;
179 
180 	if (proto == PF_INET)
181 		ipproto_off = tcp_offset - sizeof(struct iphdr) +
182 			      offsetof(struct iphdr, protocol);
183 	else
184 		ipproto_off = tcp_offset - sizeof(struct ipv6hdr) +
185 			      offsetof(struct ipv6hdr, nexthdr);
186 
187 	/* Overridden later if exthdrs are used: */
188 	opt_ipproto_off = ipproto_off;
189 
190 	if (strcmp(testname, "ip_opt") == 0) {
191 		optlen = sizeof(struct ip_timestamp);
192 	} else if (strcmp(testname, "ip_frag6") == 0 ||
193 		   strcmp(testname, "ip_v6ext_same") == 0 ||
194 		   strcmp(testname, "ip_v6ext_diff") == 0) {
195 		BUILD_BUG_ON(sizeof(struct ip6_hbh) > MIN_EXTHDR_SIZE);
196 		BUILD_BUG_ON(sizeof(struct ip6_dest) > MIN_EXTHDR_SIZE);
197 		BUILD_BUG_ON(sizeof(struct ip6_frag) > MIN_EXTHDR_SIZE);
198 
199 		/* same size for HBH and Fragment extension header types */
200 		optlen = MIN_EXTHDR_SIZE;
201 		opt_ipproto_off = ETH_HLEN + sizeof(struct ipv6hdr)
202 			+ offsetof(struct ip6_ext, ip6e_nxt);
203 	}
204 
205 	/* this filter validates the following:
206 	 *	- packet is IPv4/IPv6 according to the running test.
207 	 *	- packet is TCP. Also handles the case of one extension header and then TCP.
208 	 *	- checks the packet tcp dport equals to DPORT. Also handles the case of one
209 	 *	  extension header and then TCP.
210 	 */
211 	struct sock_filter filter[] = {
212 			BPF_STMT(BPF_LD  + BPF_H   + BPF_ABS, ethproto_off),
213 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ntohs(ethhdr_proto), 0, 9),
214 			BPF_STMT(BPF_LD  + BPF_B   + BPF_ABS, ipproto_off),
215 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 2, 0),
216 			BPF_STMT(BPF_LD  + BPF_B   + BPF_ABS, opt_ipproto_off),
217 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 0, 5),
218 			BPF_STMT(BPF_LD  + BPF_H   + BPF_ABS, dport_off),
219 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 2, 0),
220 			BPF_STMT(BPF_LD  + BPF_H   + BPF_ABS, dport_off + optlen),
221 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 0, 1),
222 			BPF_STMT(BPF_RET + BPF_K, 0xFFFFFFFF),
223 			BPF_STMT(BPF_RET + BPF_K, 0),
224 	};
225 
226 	struct sock_fprog bpf = {
227 		.len = ARRAY_SIZE(filter),
228 		.filter = filter,
229 	};
230 
231 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) < 0)
232 		error(1, errno, "error setting filter");
233 }
234 
235 static uint32_t checksum_nofold(void *data, size_t len, uint32_t sum)
236 {
237 	uint16_t *words = data;
238 	int i;
239 
240 	for (i = 0; i < len / 2; i++)
241 		sum += words[i];
242 	if (len & 1)
243 		sum += ((char *)data)[len - 1];
244 	return sum;
245 }
246 
247 static uint16_t checksum_fold(void *data, size_t len, uint32_t sum)
248 {
249 	sum = checksum_nofold(data, len, sum);
250 	while (sum > 0xFFFF)
251 		sum = (sum & 0xFFFF) + (sum >> 16);
252 	return ~sum;
253 }
254 
255 static uint16_t tcp_checksum(void *buf, int payload_len)
256 {
257 	struct pseudo_header6 {
258 		struct in6_addr saddr;
259 		struct in6_addr daddr;
260 		uint16_t protocol;
261 		uint16_t payload_len;
262 	} ph6;
263 	struct pseudo_header4 {
264 		struct in_addr saddr;
265 		struct in_addr daddr;
266 		uint16_t protocol;
267 		uint16_t payload_len;
268 	} ph4;
269 	uint32_t sum = 0;
270 
271 	if (proto == PF_INET6) {
272 		if (inet_pton(AF_INET6, addr6_src, &ph6.saddr) != 1)
273 			error(1, errno, "inet_pton6 source ip pseudo");
274 		if (inet_pton(AF_INET6, addr6_dst, &ph6.daddr) != 1)
275 			error(1, errno, "inet_pton6 dest ip pseudo");
276 		ph6.protocol = htons(IPPROTO_TCP);
277 		ph6.payload_len = htons(sizeof(struct tcphdr) + payload_len);
278 
279 		sum = checksum_nofold(&ph6, sizeof(ph6), 0);
280 	} else if (proto == PF_INET) {
281 		if (inet_pton(AF_INET, addr4_src, &ph4.saddr) != 1)
282 			error(1, errno, "inet_pton source ip pseudo");
283 		if (inet_pton(AF_INET, addr4_dst, &ph4.daddr) != 1)
284 			error(1, errno, "inet_pton dest ip pseudo");
285 		ph4.protocol = htons(IPPROTO_TCP);
286 		ph4.payload_len = htons(sizeof(struct tcphdr) + payload_len);
287 
288 		sum = checksum_nofold(&ph4, sizeof(ph4), 0);
289 	}
290 
291 	return checksum_fold(buf, sizeof(struct tcphdr) + payload_len, sum);
292 }
293 
294 static void read_MAC(uint8_t *mac_addr, char *mac)
295 {
296 	if (sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
297 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
298 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6)
299 		error(1, 0, "sscanf");
300 }
301 
302 static void fill_datalinklayer(void *buf)
303 {
304 	struct ethhdr *eth = buf;
305 
306 	memcpy(eth->h_dest, dst_mac, ETH_ALEN);
307 	memcpy(eth->h_source, src_mac, ETH_ALEN);
308 	eth->h_proto = ethhdr_proto;
309 }
310 
311 static void fill_networklayer(void *buf, int payload_len, int protocol)
312 {
313 	struct ipv6hdr *ip6h = buf;
314 	struct iphdr *iph = buf;
315 
316 	if (proto == PF_INET6) {
317 		memset(ip6h, 0, sizeof(*ip6h));
318 
319 		ip6h->version = 6;
320 		ip6h->payload_len = htons(sizeof(struct tcphdr) + payload_len);
321 		ip6h->nexthdr = protocol;
322 		ip6h->hop_limit = 8;
323 		if (inet_pton(AF_INET6, addr6_src, &ip6h->saddr) != 1)
324 			error(1, errno, "inet_pton source ip6");
325 		if (inet_pton(AF_INET6, addr6_dst, &ip6h->daddr) != 1)
326 			error(1, errno, "inet_pton dest ip6");
327 	} else if (proto == PF_INET) {
328 		memset(iph, 0, sizeof(*iph));
329 
330 		iph->version = 4;
331 		iph->ihl = 5;
332 		iph->ttl = 8;
333 		iph->protocol	= protocol;
334 		iph->tot_len = htons(sizeof(struct tcphdr) +
335 				payload_len + sizeof(struct iphdr));
336 		iph->frag_off = htons(0x4000); /* DF = 1, MF = 0 */
337 		if (inet_pton(AF_INET, addr4_src, &iph->saddr) != 1)
338 			error(1, errno, "inet_pton source ip");
339 		if (inet_pton(AF_INET, addr4_dst, &iph->daddr) != 1)
340 			error(1, errno, "inet_pton dest ip");
341 		iph->check = checksum_fold(buf, sizeof(struct iphdr), 0);
342 	}
343 }
344 
345 static void fill_transportlayer(void *buf, int seq_offset, int ack_offset,
346 				int payload_len, int fin)
347 {
348 	struct tcphdr *tcph = buf;
349 
350 	memset(tcph, 0, sizeof(*tcph));
351 
352 	tcph->source = htons(SPORT);
353 	tcph->dest = htons(DPORT);
354 	tcph->seq = ntohl(START_SEQ + seq_offset);
355 	tcph->ack_seq = ntohl(START_ACK + ack_offset);
356 	tcph->ack = 1;
357 	tcph->fin = fin;
358 	tcph->doff = 5;
359 	tcph->window = htons(TCP_MAXWIN);
360 	tcph->urg_ptr = 0;
361 	tcph->check = tcp_checksum(tcph, payload_len);
362 }
363 
364 static void write_packet(int fd, char *buf, int len, struct sockaddr_ll *daddr)
365 {
366 	char control[CMSG_SPACE(sizeof(uint64_t))];
367 	struct msghdr msg = {};
368 	struct iovec iov = {};
369 	struct cmsghdr *cm;
370 	int ret = -1;
371 
372 	iov.iov_base = buf;
373 	iov.iov_len = len;
374 
375 	msg.msg_iov = &iov;
376 	msg.msg_iovlen = 1;
377 	msg.msg_name = daddr;
378 	msg.msg_namelen = sizeof(*daddr);
379 
380 	if (txtime_ns) {
381 		memset(control, 0, sizeof(control));
382 		msg.msg_control = control;
383 		msg.msg_controllen = sizeof(control);
384 
385 		cm = CMSG_FIRSTHDR(&msg);
386 		cm->cmsg_level = SOL_SOCKET;
387 		cm->cmsg_type = SCM_TXTIME;
388 		cm->cmsg_len = CMSG_LEN(sizeof(uint64_t));
389 		memcpy(CMSG_DATA(cm), &txtime_ns, sizeof(txtime_ns));
390 	}
391 
392 	ret = sendmsg(fd, &msg, 0);
393 	if (ret == -1)
394 		error(1, errno, "sendmsg failure");
395 	if (ret != len)
396 		error(1, 0, "sendmsg wrong length: %d vs %d", ret, len);
397 }
398 
399 static void create_packet(void *buf, int seq_offset, int ack_offset,
400 			  int payload_len, int fin)
401 {
402 	int ip_hdr_len = (proto == PF_INET) ?
403 			 sizeof(struct iphdr) : sizeof(struct ipv6hdr);
404 	int inner_ip_off = tcp_offset - ip_hdr_len;
405 
406 	memset(buf, 0, total_hdr_len);
407 	memset(buf + total_hdr_len, 'a', payload_len);
408 
409 	fill_transportlayer(buf + tcp_offset, seq_offset, ack_offset,
410 			    payload_len, fin);
411 
412 	fill_networklayer(buf + inner_ip_off, payload_len, IPPROTO_TCP);
413 	if (inner_ip_off > ETH_HLEN) {
414 		int encap_proto = (proto == PF_INET) ?
415 				  IPPROTO_IPIP : IPPROTO_IPV6;
416 
417 		fill_networklayer(buf + ETH_HLEN,
418 				  payload_len + ip_hdr_len, encap_proto);
419 	}
420 
421 	fill_datalinklayer(buf);
422 }
423 
424 static void create_capacity_packet(void *buf, int flow_id, int pkt_idx, int psh)
425 {
426 	int seq_offset = pkt_idx * CAPACITY_PAYLOAD_LEN;
427 	struct tcphdr *tcph;
428 
429 	create_packet(buf, seq_offset, 0, CAPACITY_PAYLOAD_LEN, 0);
430 
431 	/* Customize for this flow id */
432 	memset(buf + total_hdr_len, 'a' + flow_id, CAPACITY_PAYLOAD_LEN);
433 
434 	tcph = buf + tcp_offset;
435 	tcph->source = htons(SPORT + flow_id);
436 	tcph->psh = psh;
437 	tcph->check = 0;
438 	tcph->check = tcp_checksum(tcph, CAPACITY_PAYLOAD_LEN);
439 }
440 
441 /* Send a capacity test, 2 packets per flow, all first packets then all second:
442  *  A1 B1 C1 D1 ... A2 B2 C2 D2 ...
443  */
444 static void send_capacity(int fd, struct sockaddr_ll *daddr)
445 {
446 	static char buf[MAX_HDR_LEN + CAPACITY_PAYLOAD_LEN];
447 	int pkt_size = total_hdr_len + CAPACITY_PAYLOAD_LEN;
448 	int i;
449 
450 	/* Send first packet of each flow (no PSH) */
451 	for (i = 0; i < num_flows; i++) {
452 		create_capacity_packet(buf, i, 0, 0);
453 		write_packet(fd, buf, pkt_size, daddr);
454 	}
455 
456 	/* Send second packet of each flow (with PSH to flush) */
457 	for (i = 0; i < num_flows; i++) {
458 		create_capacity_packet(buf, i, 1, 1);
459 		write_packet(fd, buf, pkt_size, daddr);
460 	}
461 }
462 
463 #ifndef TH_CWR
464 #define TH_CWR 0x80
465 #endif
466 static void set_flags(struct tcphdr *tcph, int payload_len, int psh, int syn,
467 		      int rst, int urg, int cwr)
468 {
469 	tcph->psh = psh;
470 	tcph->syn = syn;
471 	tcph->rst = rst;
472 	tcph->urg = urg;
473 	if (cwr)
474 		tcph->th_flags |= TH_CWR;
475 	else
476 		tcph->th_flags &= ~TH_CWR;
477 	tcph->check = 0;
478 	tcph->check = tcp_checksum(tcph, payload_len);
479 }
480 
481 /* send extra flags of the (NUM_PACKETS / 2) and (NUM_PACKETS / 2 - 1)
482  * pkts, not first and not last pkt
483  */
484 static void send_flags(int fd, struct sockaddr_ll *daddr, int psh, int syn,
485 		       int rst, int urg, int cwr)
486 {
487 	static char flag_buf[2][MAX_HDR_LEN + PAYLOAD_LEN];
488 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
489 	int payload_len, pkt_size, i;
490 	struct tcphdr *tcph;
491 	int flag[2];
492 
493 	payload_len = PAYLOAD_LEN * (psh || cwr);
494 	pkt_size = total_hdr_len + payload_len;
495 	flag[0] = NUM_PACKETS / 2;
496 	flag[1] = NUM_PACKETS / 2 - 1;
497 
498 	/* Create and configure packets with flags
499 	 */
500 	for (i = 0; i < 2; i++) {
501 		if (flag[i] > 0) {
502 			create_packet(flag_buf[i], flag[i] * payload_len, 0,
503 				      payload_len, 0);
504 			tcph = (struct tcphdr *)(flag_buf[i] + tcp_offset);
505 			set_flags(tcph, payload_len, psh, syn, rst, urg, cwr);
506 		}
507 	}
508 
509 	for (i = 0; i < NUM_PACKETS + 1; i++) {
510 		if (i == flag[0]) {
511 			write_packet(fd, flag_buf[0], pkt_size, daddr);
512 			continue;
513 		} else if (i == flag[1] && cwr) {
514 			write_packet(fd, flag_buf[1], pkt_size, daddr);
515 			continue;
516 		}
517 		create_packet(buf, i * PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
518 		write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
519 	}
520 }
521 
522 /* Test for data of same length, smaller than previous
523  * and of different lengths
524  */
525 static void send_data_pkts(int fd, struct sockaddr_ll *daddr,
526 			   int payload_len1, int payload_len2)
527 {
528 	static char buf[ETH_HLEN + IP_MAXPACKET];
529 
530 	create_packet(buf, 0, 0, payload_len1, 0);
531 	write_packet(fd, buf, total_hdr_len + payload_len1, daddr);
532 	create_packet(buf, payload_len1, 0, payload_len2, 0);
533 	write_packet(fd, buf, total_hdr_len + payload_len2, daddr);
534 }
535 
536 /* If incoming segments make tracked segment length exceed
537  * legal IP datagram length, do not coalesce
538  */
539 static void send_large(int fd, struct sockaddr_ll *daddr, int remainder)
540 {
541 	static char pkts[MAX_LARGE_PKT_CNT][MAX_HDR_LEN + MAX_MSS];
542 	static char new_seg[MAX_HDR_LEN + MAX_MSS];
543 	static char last[MAX_HDR_LEN + MAX_MSS];
544 	const int num_pkt = num_large_pkt();
545 	const int mss = calc_mss();
546 	int i;
547 
548 	for (i = 0; i < num_pkt; i++)
549 		create_packet(pkts[i], i * mss, 0, mss, 0);
550 	create_packet(last, num_pkt * mss, 0, remainder, 0);
551 	create_packet(new_seg, (num_pkt + 1) * mss, 0, remainder, 0);
552 
553 	for (i = 0; i < num_pkt; i++)
554 		write_packet(fd, pkts[i], total_hdr_len + mss, daddr);
555 	write_packet(fd, last, total_hdr_len + remainder, daddr);
556 	write_packet(fd, new_seg, total_hdr_len + remainder, daddr);
557 }
558 
559 /* Pure acks and dup acks don't coalesce */
560 static void send_ack(int fd, struct sockaddr_ll *daddr)
561 {
562 	static char buf[MAX_HDR_LEN];
563 
564 	create_packet(buf, 0, 0, 0, 0);
565 	write_packet(fd, buf, total_hdr_len, daddr);
566 	write_packet(fd, buf, total_hdr_len, daddr);
567 	create_packet(buf, 0, 1, 0, 0);
568 	write_packet(fd, buf, total_hdr_len, daddr);
569 }
570 
571 static void recompute_packet(char *buf, char *no_ext, int extlen)
572 {
573 	struct tcphdr *tcphdr = (struct tcphdr *)(buf + tcp_offset);
574 	int off;
575 
576 	memmove(buf, no_ext, total_hdr_len);
577 	memmove(buf + total_hdr_len + extlen,
578 		no_ext + total_hdr_len, PAYLOAD_LEN);
579 
580 	tcphdr->doff = tcphdr->doff + (extlen / 4);
581 	tcphdr->check = 0;
582 	tcphdr->check = tcp_checksum(tcphdr, PAYLOAD_LEN + extlen);
583 	if (proto == PF_INET) {
584 		for (off = ETH_HLEN; off < tcp_offset;
585 		     off += sizeof(struct iphdr)) {
586 			struct iphdr *iph = (struct iphdr *)(buf + off);
587 
588 			iph->tot_len = htons(ntohs(iph->tot_len) + extlen);
589 			iph->check = 0;
590 			iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
591 		}
592 	} else {
593 		for (off = ETH_HLEN; off < tcp_offset;
594 		     off += sizeof(struct ipv6hdr)) {
595 			struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + off);
596 
597 			ip6h->payload_len =
598 				htons(ntohs(ip6h->payload_len) + extlen);
599 		}
600 	}
601 }
602 
603 static void tcp_write_options(char *buf, int kind, int ts)
604 {
605 	struct tcp_option_ts {
606 		uint8_t kind;
607 		uint8_t len;
608 		uint32_t tsval;
609 		uint32_t tsecr;
610 	} *opt_ts = (void *)buf;
611 	struct tcp_option_window {
612 		uint8_t kind;
613 		uint8_t len;
614 		uint8_t shift;
615 	} *opt_window = (void *)buf;
616 
617 	switch (kind) {
618 	case TCPOPT_NOP:
619 		buf[0] = TCPOPT_NOP;
620 		break;
621 	case TCPOPT_WINDOW:
622 		memset(opt_window, 0, sizeof(struct tcp_option_window));
623 		opt_window->kind = TCPOPT_WINDOW;
624 		opt_window->len = TCPOLEN_WINDOW;
625 		opt_window->shift = 0;
626 		break;
627 	case TCPOPT_TIMESTAMP:
628 		memset(opt_ts, 0, sizeof(struct tcp_option_ts));
629 		opt_ts->kind = TCPOPT_TIMESTAMP;
630 		opt_ts->len = TCPOLEN_TIMESTAMP;
631 		opt_ts->tsval = ts;
632 		opt_ts->tsecr = 0;
633 		break;
634 	default:
635 		error(1, 0, "unimplemented TCP option");
636 		break;
637 	}
638 }
639 
640 /* TCP with options is always a permutation of {TS, NOP, NOP}.
641  * Implement different orders to verify coalescing stops.
642  */
643 static void add_standard_tcp_options(char *buf, char *no_ext, int ts, int order)
644 {
645 	switch (order) {
646 	case 0:
647 		tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);
648 		tcp_write_options(buf + total_hdr_len + 1, TCPOPT_NOP, 0);
649 		tcp_write_options(buf + total_hdr_len + 2 /* two NOP opts */,
650 				  TCPOPT_TIMESTAMP, ts);
651 		break;
652 	case 1:
653 		tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);
654 		tcp_write_options(buf + total_hdr_len + 1,
655 				  TCPOPT_TIMESTAMP, ts);
656 		tcp_write_options(buf + total_hdr_len + 1 + TCPOLEN_TIMESTAMP,
657 				  TCPOPT_NOP, 0);
658 		break;
659 	case 2:
660 		tcp_write_options(buf + total_hdr_len, TCPOPT_TIMESTAMP, ts);
661 		tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 1,
662 				  TCPOPT_NOP, 0);
663 		tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 2,
664 				  TCPOPT_NOP, 0);
665 		break;
666 	default:
667 		error(1, 0, "unknown order");
668 		break;
669 	}
670 	recompute_packet(buf, no_ext, TCPOLEN_TSTAMP_APPA);
671 }
672 
673 /* Packets with invalid checksum don't coalesce. */
674 static void send_changed_checksum(int fd, struct sockaddr_ll *daddr)
675 {
676 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
677 	struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);
678 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
679 
680 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
681 	write_packet(fd, buf, pkt_size, daddr);
682 
683 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
684 	tcph->check = tcph->check - 1;
685 	write_packet(fd, buf, pkt_size, daddr);
686 }
687 
688  /* Packets with non-consecutive sequence number don't coalesce.*/
689 static void send_changed_seq(int fd, struct sockaddr_ll *daddr)
690 {
691 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
692 	struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);
693 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
694 
695 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
696 	write_packet(fd, buf, pkt_size, daddr);
697 
698 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
699 	tcph->seq = ntohl(htonl(tcph->seq) + 1);
700 	tcph->check = 0;
701 	tcph->check = tcp_checksum(tcph, PAYLOAD_LEN);
702 	write_packet(fd, buf, pkt_size, daddr);
703 }
704 
705  /* Packet with different timestamp option or different timestamps
706   * don't coalesce.
707   */
708 static void send_changed_ts(int fd, struct sockaddr_ll *daddr)
709 {
710 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
711 	static char extpkt[sizeof(buf) + TCPOLEN_TSTAMP_APPA];
712 	int pkt_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;
713 
714 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
715 	add_standard_tcp_options(extpkt, buf, 0, 0);
716 	write_packet(fd, extpkt, pkt_size, daddr);
717 
718 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
719 	add_standard_tcp_options(extpkt, buf, 0, 0);
720 	write_packet(fd, extpkt, pkt_size, daddr);
721 
722 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
723 	add_standard_tcp_options(extpkt, buf, 100, 0);
724 	write_packet(fd, extpkt, pkt_size, daddr);
725 
726 	create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
727 	add_standard_tcp_options(extpkt, buf, 100, 1);
728 	write_packet(fd, extpkt, pkt_size, daddr);
729 
730 	create_packet(buf, PAYLOAD_LEN * 4, 0, PAYLOAD_LEN, 0);
731 	add_standard_tcp_options(extpkt, buf, 100, 2);
732 	write_packet(fd, extpkt, pkt_size, daddr);
733 }
734 
735 /* Packet with different tcp options don't coalesce. */
736 static void send_diff_opt(int fd, struct sockaddr_ll *daddr)
737 {
738 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
739 	static char extpkt1[sizeof(buf) + TCPOLEN_TSTAMP_APPA];
740 	static char extpkt2[sizeof(buf) + TCPOLEN_MAXSEG];
741 	int extpkt1_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;
742 	int extpkt2_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_MAXSEG;
743 
744 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
745 	add_standard_tcp_options(extpkt1, buf, 0, 0);
746 	write_packet(fd, extpkt1, extpkt1_size, daddr);
747 
748 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
749 	add_standard_tcp_options(extpkt1, buf, 0, 0);
750 	write_packet(fd, extpkt1, extpkt1_size, daddr);
751 
752 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
753 	tcp_write_options(extpkt2 + MAX_HDR_LEN, TCPOPT_NOP, 0);
754 	tcp_write_options(extpkt2 + MAX_HDR_LEN + 1, TCPOPT_WINDOW, 0);
755 	recompute_packet(extpkt2, buf, TCPOLEN_WINDOW + 1);
756 	write_packet(fd, extpkt2, extpkt2_size, daddr);
757 }
758 
759 static void add_ipv4_ts_option(void *buf, void *optpkt)
760 {
761 	struct ip_timestamp *ts = (struct ip_timestamp *)(optpkt + tcp_offset);
762 	int optlen = sizeof(struct ip_timestamp);
763 	struct iphdr *iph;
764 
765 	if (optlen % 4)
766 		error(1, 0, "ipv4 timestamp length is not a multiple of 4B");
767 
768 	ts->ipt_code = IPOPT_TS;
769 	ts->ipt_len = optlen;
770 	ts->ipt_ptr = 5;
771 	ts->ipt_flg = IPOPT_TS_TSONLY;
772 
773 	memcpy(optpkt, buf, tcp_offset);
774 	memcpy(optpkt + tcp_offset + optlen, buf + tcp_offset,
775 	       sizeof(struct tcphdr) + PAYLOAD_LEN);
776 
777 	iph = (struct iphdr *)(optpkt + ETH_HLEN);
778 	iph->ihl = 5 + (optlen / 4);
779 	iph->tot_len = htons(ntohs(iph->tot_len) + optlen);
780 	iph->check = 0;
781 	iph->check = checksum_fold(iph, sizeof(struct iphdr) + optlen, 0);
782 }
783 
784 static void add_ipv6_exthdr(void *buf, void *optpkt, __u8 exthdr_type, char *ext_payload)
785 {
786 	struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr *)(optpkt + tcp_offset);
787 	struct ipv6hdr *iph = (struct ipv6hdr *)(optpkt + ETH_HLEN);
788 	char *exthdr_payload_start = (char *)(exthdr + 1);
789 
790 	exthdr->hdrlen = 0;
791 	exthdr->nexthdr = IPPROTO_TCP;
792 
793 	memcpy(exthdr_payload_start, ext_payload, MIN_EXTHDR_SIZE - sizeof(*exthdr));
794 
795 	memcpy(optpkt, buf, tcp_offset);
796 	memcpy(optpkt + tcp_offset + MIN_EXTHDR_SIZE, buf + tcp_offset,
797 		sizeof(struct tcphdr) + PAYLOAD_LEN);
798 
799 	iph->nexthdr = exthdr_type;
800 	iph->payload_len = htons(ntohs(iph->payload_len) + MIN_EXTHDR_SIZE);
801 }
802 
803 static void fix_ip4_checksum(struct iphdr *iph)
804 {
805 	iph->check = 0;
806 	iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
807 }
808 
809 static void send_flush_id_case(int fd, struct sockaddr_ll *daddr,
810 			       enum flush_id_case tcase)
811 {
812 	static char buf1[MAX_HDR_LEN + PAYLOAD_LEN];
813 	static char buf2[MAX_HDR_LEN + PAYLOAD_LEN];
814 	static char buf3[MAX_HDR_LEN + PAYLOAD_LEN];
815 	bool send_three = false;
816 	struct iphdr *iph1;
817 	struct iphdr *iph2;
818 	struct iphdr *iph3;
819 
820 	iph1 = (struct iphdr *)(buf1 + ETH_HLEN);
821 	iph2 = (struct iphdr *)(buf2 + ETH_HLEN);
822 	iph3 = (struct iphdr *)(buf3 + ETH_HLEN);
823 
824 	create_packet(buf1, 0, 0, PAYLOAD_LEN, 0);
825 	create_packet(buf2, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
826 	create_packet(buf3, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
827 
828 	switch (tcase) {
829 	case FLUSH_ID_DF1_INC: /* DF=1, Incrementing - should coalesce */
830 		iph1->frag_off |= htons(IP_DF);
831 		iph1->id = htons(8);
832 
833 		iph2->frag_off |= htons(IP_DF);
834 		iph2->id = htons(9);
835 		break;
836 
837 	case FLUSH_ID_DF1_FIXED: /* DF=1, Fixed - should coalesce */
838 		iph1->frag_off |= htons(IP_DF);
839 		iph1->id = htons(8);
840 
841 		iph2->frag_off |= htons(IP_DF);
842 		iph2->id = htons(8);
843 		break;
844 
845 	case FLUSH_ID_DF0_INC: /* DF=0, Incrementing - should coalesce */
846 		iph1->frag_off &= ~htons(IP_DF);
847 		iph1->id = htons(8);
848 
849 		iph2->frag_off &= ~htons(IP_DF);
850 		iph2->id = htons(9);
851 		break;
852 
853 	case FLUSH_ID_DF0_FIXED: /* DF=0, Fixed - should coalesce */
854 		iph1->frag_off &= ~htons(IP_DF);
855 		iph1->id = htons(8);
856 
857 		iph2->frag_off &= ~htons(IP_DF);
858 		iph2->id = htons(8);
859 		break;
860 
861 	case FLUSH_ID_DF1_INC_FIXED: /* DF=1, two packets incrementing, and
862 				      * one fixed - should coalesce only the
863 				      * first two packets
864 				      */
865 		iph1->frag_off |= htons(IP_DF);
866 		iph1->id = htons(8);
867 
868 		iph2->frag_off |= htons(IP_DF);
869 		iph2->id = htons(9);
870 
871 		iph3->frag_off |= htons(IP_DF);
872 		iph3->id = htons(9);
873 		send_three = true;
874 		break;
875 
876 	case FLUSH_ID_DF1_FIXED_INC: /* DF=1, two packets fixed, and one
877 				      * incrementing - should coalesce only
878 				      * the first two packets
879 				      */
880 		iph1->frag_off |= htons(IP_DF);
881 		iph1->id = htons(8);
882 
883 		iph2->frag_off |= htons(IP_DF);
884 		iph2->id = htons(8);
885 
886 		iph3->frag_off |= htons(IP_DF);
887 		iph3->id = htons(9);
888 		send_three = true;
889 		break;
890 	}
891 
892 	fix_ip4_checksum(iph1);
893 	fix_ip4_checksum(iph2);
894 	write_packet(fd, buf1, total_hdr_len + PAYLOAD_LEN, daddr);
895 	write_packet(fd, buf2, total_hdr_len + PAYLOAD_LEN, daddr);
896 
897 	if (send_three) {
898 		fix_ip4_checksum(iph3);
899 		write_packet(fd, buf3, total_hdr_len + PAYLOAD_LEN, daddr);
900 	}
901 }
902 
903 static void send_ipv6_exthdr(int fd, struct sockaddr_ll *daddr, char *ext_data1, char *ext_data2)
904 {
905 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
906 	static char exthdr_pck[sizeof(buf) + MIN_EXTHDR_SIZE];
907 
908 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
909 	add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_DSTOPTS, ext_data1);
910 	write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr);
911 
912 	create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);
913 	add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_DSTOPTS, ext_data2);
914 	write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr);
915 }
916 
917 /* IPv4 options shouldn't coalesce */
918 static void send_ip_options(int fd, struct sockaddr_ll *daddr)
919 {
920 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
921 	static char optpkt[sizeof(buf) + sizeof(struct ip_timestamp)];
922 	int optlen = sizeof(struct ip_timestamp);
923 	int pkt_size = total_hdr_len + PAYLOAD_LEN + optlen;
924 
925 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
926 	write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
927 
928 	create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);
929 	add_ipv4_ts_option(buf, optpkt);
930 	write_packet(fd, optpkt, pkt_size, daddr);
931 
932 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
933 	write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
934 }
935 
936 /*  IPv4 fragments shouldn't coalesce */
937 static void send_fragment4(int fd, struct sockaddr_ll *daddr)
938 {
939 	static char buf[IP_MAXPACKET];
940 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
941 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
942 
943 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
944 	write_packet(fd, buf, pkt_size, daddr);
945 
946 	/* Once fragmented, packet would retain the total_len.
947 	 * Tcp header is prepared as if rest of data is in follow-up frags,
948 	 * but follow up frags aren't actually sent.
949 	 */
950 	memset(buf + total_hdr_len, 'a', PAYLOAD_LEN * 2);
951 	fill_transportlayer(buf + tcp_offset, PAYLOAD_LEN, 0, PAYLOAD_LEN * 2, 0);
952 	fill_networklayer(buf + ETH_HLEN, PAYLOAD_LEN, IPPROTO_TCP);
953 	fill_datalinklayer(buf);
954 
955 	iph->frag_off = htons(0x6000); // DF = 1, MF = 1
956 	iph->check = 0;
957 	iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
958 	write_packet(fd, buf, pkt_size, daddr);
959 }
960 
961 /* IPv4 packets with different ttl don't coalesce.*/
962 static void send_changed_ttl(int fd, struct sockaddr_ll *daddr)
963 {
964 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
965 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
966 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
967 
968 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
969 	write_packet(fd, buf, pkt_size, daddr);
970 
971 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
972 	iph->ttl = 7;
973 	iph->check = 0;
974 	iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
975 	write_packet(fd, buf, pkt_size, daddr);
976 }
977 
978 /* Packets with different tos don't coalesce.*/
979 static void send_changed_tos(int fd, struct sockaddr_ll *daddr)
980 {
981 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
982 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
983 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
984 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
985 
986 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
987 	write_packet(fd, buf, pkt_size, daddr);
988 
989 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
990 	if (proto == PF_INET) {
991 		iph->tos = 1;
992 		iph->check = 0;
993 		iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
994 	} else if (proto == PF_INET6) {
995 		ip6h->priority = 0xf;
996 	}
997 	write_packet(fd, buf, pkt_size, daddr);
998 }
999 
1000 /* Packets with different ECN don't coalesce.*/
1001 static void send_changed_ECN(int fd, struct sockaddr_ll *daddr)
1002 {
1003 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
1004 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
1005 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
1006 
1007 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
1008 	write_packet(fd, buf, pkt_size, daddr);
1009 
1010 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
1011 	if (proto == PF_INET) {
1012 		buf[ETH_HLEN + 1] ^= 0x2; // ECN set to 10
1013 		iph->check = 0;
1014 		iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
1015 	} else {
1016 		buf[ETH_HLEN + 1] ^= 0x20; // ECN set to 10
1017 	}
1018 	write_packet(fd, buf, pkt_size, daddr);
1019 }
1020 
1021 /* IPv6 fragments and packets with extensions don't coalesce.*/
1022 static void send_fragment6(int fd, struct sockaddr_ll *daddr)
1023 {
1024 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
1025 	static char extpkt[MAX_HDR_LEN + PAYLOAD_LEN +
1026 			   sizeof(struct ip6_frag)];
1027 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
1028 	struct ip6_frag *frag = (void *)(extpkt + tcp_offset);
1029 	int extlen = sizeof(struct ip6_frag);
1030 	int bufpkt_len = total_hdr_len + PAYLOAD_LEN;
1031 	int extpkt_len = bufpkt_len + extlen;
1032 	int i;
1033 
1034 	for (i = 0; i < 2; i++) {
1035 		create_packet(buf, PAYLOAD_LEN * i, 0, PAYLOAD_LEN, 0);
1036 		write_packet(fd, buf, bufpkt_len, daddr);
1037 	}
1038 	sleep(1);
1039 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
1040 	memset(extpkt, 0, extpkt_len);
1041 
1042 	ip6h->nexthdr = IPPROTO_FRAGMENT;
1043 	ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);
1044 	frag->ip6f_nxt = IPPROTO_TCP;
1045 
1046 	memcpy(extpkt, buf, tcp_offset);
1047 	memcpy(extpkt + tcp_offset + extlen, buf + tcp_offset,
1048 	       sizeof(struct tcphdr) + PAYLOAD_LEN);
1049 	write_packet(fd, extpkt, extpkt_len, daddr);
1050 
1051 	create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
1052 	write_packet(fd, buf, bufpkt_len, daddr);
1053 }
1054 
1055 static void bind_packetsocket(int fd)
1056 {
1057 	struct sockaddr_ll daddr = {};
1058 
1059 	daddr.sll_family = AF_PACKET;
1060 	daddr.sll_protocol = ethhdr_proto;
1061 	daddr.sll_ifindex = if_nametoindex(ifname);
1062 	if (daddr.sll_ifindex == 0)
1063 		error(1, errno, "if_nametoindex");
1064 
1065 	if (bind(fd, (void *)&daddr, sizeof(daddr)) < 0)
1066 		error(1, errno, "could not bind socket");
1067 }
1068 
1069 static void set_timeout(int fd)
1070 {
1071 	struct timeval timeout;
1072 
1073 	timeout.tv_sec = 3;
1074 	timeout.tv_usec = 0;
1075 	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
1076 		       sizeof(timeout)) < 0)
1077 		error(1, errno, "cannot set timeout, setsockopt failed");
1078 }
1079 
1080 static void set_rcvbuf(int fd)
1081 {
1082 	int bufsize = 1 * 1024 * 1024; /* 1 MB */
1083 
1084 	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize)))
1085 		error(1, errno, "cannot set rcvbuf size, setsockopt failed");
1086 }
1087 
1088 static void recv_error(int fd, int rcv_errno)
1089 {
1090 	struct tpacket_stats stats;
1091 	socklen_t len;
1092 
1093 	len = sizeof(stats);
1094 	if (getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &stats, &len))
1095 		error(1, errno, "can't get stats");
1096 
1097 	fprintf(stderr, "Socket stats: packets=%u, drops=%u\n",
1098 		stats.tp_packets, stats.tp_drops);
1099 	error(1, rcv_errno, "could not receive");
1100 }
1101 
1102 static void check_recv_pkts(int fd, int *correct_payload,
1103 			    int correct_num_pkts)
1104 {
1105 	static char buffer[IP_MAXPACKET + ETH_HLEN + 1];
1106 	struct iphdr *iph = (struct iphdr *)(buffer + ETH_HLEN);
1107 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + ETH_HLEN);
1108 	struct tcphdr *tcph;
1109 	bool bad_packet = false;
1110 	int tcp_ext_len = 0;
1111 	int ip_ext_len = 0;
1112 	int pkt_size = -1;
1113 	int data_len = 0;
1114 	int num_pkt = 0;
1115 	int i;
1116 
1117 	vlog("Expected {");
1118 	for (i = 0; i < correct_num_pkts; i++)
1119 		vlog("%d ", correct_payload[i]);
1120 	vlog("}, Total %d packets\nReceived {", correct_num_pkts);
1121 
1122 	while (1) {
1123 		ip_ext_len = 0;
1124 		pkt_size = recv(fd, buffer, IP_MAXPACKET + ETH_HLEN + 1, 0);
1125 		if (pkt_size < 0)
1126 			recv_error(fd, errno);
1127 
1128 		if (iph->version == 4)
1129 			ip_ext_len = (iph->ihl - 5) * 4;
1130 		else if (ip6h->version == 6 && !ip6ip6 &&
1131 			 ip6h->nexthdr != IPPROTO_TCP)
1132 			ip_ext_len = MIN_EXTHDR_SIZE;
1133 
1134 		tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len);
1135 
1136 		if (tcph->fin)
1137 			break;
1138 
1139 		tcp_ext_len = (tcph->doff - 5) * 4;
1140 		data_len = pkt_size - total_hdr_len - tcp_ext_len - ip_ext_len;
1141 		/* Min ethernet frame payload is 46(ETH_ZLEN - ETH_HLEN) by RFC 802.3.
1142 		 * Ipv4/tcp packets without at least 6 bytes of data will be padded.
1143 		 * Packet sockets are protocol agnostic, and will not trim the padding.
1144 		 */
1145 		if (pkt_size == ETH_ZLEN && iph->version == 4) {
1146 			data_len = ntohs(iph->tot_len)
1147 				- sizeof(struct tcphdr) - sizeof(struct iphdr);
1148 		}
1149 		vlog("%d ", data_len);
1150 		if (data_len != correct_payload[num_pkt]) {
1151 			vlog("[!=%d]", correct_payload[num_pkt]);
1152 			bad_packet = true;
1153 		}
1154 		num_pkt++;
1155 	}
1156 	vlog("}, Total %d packets.\n", num_pkt);
1157 	if (num_pkt != correct_num_pkts)
1158 		error(1, 0, "incorrect number of packets");
1159 	if (bad_packet)
1160 		error(1, 0, "incorrect packet geometry");
1161 
1162 	printf("Test succeeded\n\n");
1163 }
1164 
1165 static void check_capacity_pkts(int fd)
1166 {
1167 	static char buffer[IP_MAXPACKET + ETH_HLEN + 1];
1168 	struct iphdr *iph = (struct iphdr *)(buffer + ETH_HLEN);
1169 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + ETH_HLEN);
1170 	int num_pkt = 0, num_coal = 0, pkt_idx;
1171 	const char *fail_reason = NULL;
1172 	int flow_order[num_flows * 2];
1173 	int coalesced[num_flows];
1174 	struct tcphdr *tcph;
1175 	int ip_ext_len = 0;
1176 	int total_data = 0;
1177 	int pkt_size = -1;
1178 	int data_len = 0;
1179 	int flow_id;
1180 	int sport;
1181 
1182 	memset(coalesced, 0, sizeof(coalesced));
1183 	memset(flow_order, -1, sizeof(flow_order));
1184 
1185 	while (1) {
1186 		ip_ext_len = 0;
1187 		pkt_size = recv(fd, buffer, IP_MAXPACKET + ETH_HLEN + 1, 0);
1188 		if (pkt_size < 0)
1189 			recv_error(fd, errno);
1190 
1191 		if (iph->version == 4)
1192 			ip_ext_len = (iph->ihl - 5) * 4;
1193 		else if (ip6h->version == 6 && !ip6ip6 &&
1194 			 ip6h->nexthdr != IPPROTO_TCP)
1195 			ip_ext_len = MIN_EXTHDR_SIZE;
1196 
1197 		tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len);
1198 
1199 		if (tcph->fin)
1200 			break;
1201 
1202 		sport = ntohs(tcph->source);
1203 		flow_id = sport - SPORT;
1204 
1205 		if (flow_id < 0 || flow_id >= num_flows) {
1206 			vlog("Invalid flow_id %d from sport %d\n",
1207 			     flow_id, sport);
1208 			fail_reason = fail_reason ?: "invalid packet";
1209 			continue;
1210 		}
1211 
1212 		/* Calculate payload length */
1213 		if (pkt_size == ETH_ZLEN && iph->version == 4) {
1214 			data_len = ntohs(iph->tot_len)
1215 				- sizeof(struct tcphdr) - sizeof(struct iphdr);
1216 		} else {
1217 			data_len = pkt_size - total_hdr_len - ip_ext_len;
1218 		}
1219 
1220 		if (num_pkt < num_flows * 2) {
1221 			flow_order[num_pkt] = flow_id;
1222 		} else if (num_pkt == num_flows * 2) {
1223 			vlog("More packets than expected (%d)\n",
1224 			     num_flows * 2);
1225 			fail_reason = fail_reason ?: "too many packets";
1226 		}
1227 		coalesced[flow_id] = data_len;
1228 
1229 		if (data_len == CAPACITY_PAYLOAD_LEN * 2) {
1230 			num_coal++;
1231 		} else {
1232 			vlog("Pkt %d: flow %d, sport %d, len %d (expected %d)\n",
1233 			     num_pkt, flow_id, sport, data_len,
1234 			     CAPACITY_PAYLOAD_LEN * 2);
1235 			fail_reason = fail_reason ?: "not coalesced";
1236 		}
1237 
1238 		num_pkt++;
1239 		total_data += data_len;
1240 	}
1241 
1242 	/* Check flow ordering. We expect to see all non-coalesced first segs
1243 	 * then interleaved coalesced and non-coalesced second frames.
1244 	 */
1245 	pkt_idx = 0;
1246 	for (flow_id = 0; order_check && flow_id < num_flows; flow_id++) {
1247 		bool coaled = coalesced[flow_id] > CAPACITY_PAYLOAD_LEN;
1248 
1249 		if (coaled)
1250 			continue;
1251 
1252 		if (flow_order[pkt_idx] != flow_id) {
1253 			vlog("Flow order mismatch (non-coalesced) at position %d: expected flow %d, got flow %d\n",
1254 			     pkt_idx, flow_id, flow_order[pkt_idx]);
1255 			fail_reason = fail_reason ?: "bad packet order (1)";
1256 		}
1257 		pkt_idx++;
1258 	}
1259 	for (flow_id = 0; order_check && flow_id < num_flows; flow_id++) {
1260 		bool coaled = coalesced[flow_id] > CAPACITY_PAYLOAD_LEN;
1261 
1262 		if (flow_order[pkt_idx] != flow_id) {
1263 			vlog("Flow order mismatch at position %d: expected flow %d, got flow %d, coalesced: %d\n",
1264 			     pkt_idx, flow_id, flow_order[pkt_idx], coaled);
1265 			fail_reason = fail_reason ?: "bad packet order (2)";
1266 		}
1267 		pkt_idx++;
1268 	}
1269 
1270 	if (!fail_reason) {
1271 		vlog("All %d flows coalesced correctly\n", num_flows);
1272 		printf("Test succeeded\n\n");
1273 	} else {
1274 		printf("FAILED\n");
1275 	}
1276 
1277 	/* Always print stats for external validation */
1278 	printf("STATS: received=%d wire=%d coalesced=%d\n",
1279 	       num_pkt, num_pkt + num_coal, num_coal);
1280 
1281 	if (fail_reason)
1282 		error(1, 0, "capacity test failed %s", fail_reason);
1283 }
1284 
1285 static void gro_sender(void)
1286 {
1287 	int bufsize = 4 * 1024 * 1024; /* 4 MB */
1288 	const int fin_delay_us = 100 * 1000;
1289 	static char fin_pkt[MAX_HDR_LEN];
1290 	struct sockaddr_ll daddr = {};
1291 	int txfd = -1;
1292 
1293 	txfd = socket(PF_PACKET, SOCK_RAW, IPPROTO_RAW);
1294 	if (txfd < 0)
1295 		error(1, errno, "socket creation");
1296 
1297 	if (setsockopt(txfd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)))
1298 		error(1, errno, "cannot set sndbuf size, setsockopt failed");
1299 
1300 	/* Enable SO_TXTIME unless test case generates more than one flow
1301 	 * SO_TXTIME could result in qdisc layer sorting the packets at sender.
1302 	 */
1303 	if (strcmp(testname, "single") && strcmp(testname, "capacity")) {
1304 		struct sock_txtime so_txtime = { .clockid = CLOCK_MONOTONIC, };
1305 		struct timespec ts;
1306 
1307 		if (setsockopt(txfd, SOL_SOCKET, SO_TXTIME,
1308 			       &so_txtime, sizeof(so_txtime)))
1309 			error(1, errno, "setsockopt SO_TXTIME");
1310 
1311 		if (clock_gettime(CLOCK_MONOTONIC, &ts))
1312 			error(1, errno, "clock_gettime");
1313 
1314 		txtime_ns = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
1315 		txtime_ns += TXTIME_DELAY_MS * 1000000ULL;
1316 	}
1317 
1318 	memset(&daddr, 0, sizeof(daddr));
1319 	daddr.sll_ifindex = if_nametoindex(ifname);
1320 	if (daddr.sll_ifindex == 0)
1321 		error(1, errno, "if_nametoindex");
1322 	daddr.sll_family = AF_PACKET;
1323 	memcpy(daddr.sll_addr, dst_mac, ETH_ALEN);
1324 	daddr.sll_halen = ETH_ALEN;
1325 	create_packet(fin_pkt, PAYLOAD_LEN * 2, 0, 0, 1);
1326 
1327 	/* data sub-tests */
1328 	if (strcmp(testname, "data_same") == 0) {
1329 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN);
1330 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1331 	} else if (strcmp(testname, "data_lrg_sml") == 0) {
1332 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN / 2);
1333 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1334 	} else if (strcmp(testname, "data_lrg_1byte") == 0) {
1335 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN, 1);
1336 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1337 	} else if (strcmp(testname, "data_sml_lrg") == 0) {
1338 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN / 2, PAYLOAD_LEN);
1339 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1340 	} else if (strcmp(testname, "data_burst") == 0) {
1341 		static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
1342 
1343 		create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
1344 		write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr);
1345 		create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
1346 		write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr);
1347 
1348 		usleep(100 * 1000); /* 100ms */
1349 		create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
1350 		write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr);
1351 		create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
1352 		write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr);
1353 
1354 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1355 
1356 	/* ack test */
1357 	} else if (strcmp(testname, "ack") == 0) {
1358 		send_ack(txfd, &daddr);
1359 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1360 
1361 	/* flags sub-tests */
1362 	} else if (strcmp(testname, "flags_psh") == 0) {
1363 		send_flags(txfd, &daddr, 1, 0, 0, 0, 0);
1364 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1365 	} else if (strcmp(testname, "flags_syn") == 0) {
1366 		send_flags(txfd, &daddr, 0, 1, 0, 0, 0);
1367 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1368 	} else if (strcmp(testname, "flags_rst") == 0) {
1369 		send_flags(txfd, &daddr, 0, 0, 1, 0, 0);
1370 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1371 	} else if (strcmp(testname, "flags_urg") == 0) {
1372 		send_flags(txfd, &daddr, 0, 0, 0, 1, 0);
1373 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1374 	} else if (strcmp(testname, "flags_cwr") == 0) {
1375 		send_flags(txfd, &daddr, 0, 0, 0, 0, 1);
1376 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1377 
1378 	/* tcp sub-tests */
1379 	} else if (strcmp(testname, "tcp_csum") == 0) {
1380 		send_changed_checksum(txfd, &daddr);
1381 		usleep(fin_delay_us);
1382 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1383 	} else if (strcmp(testname, "tcp_seq") == 0) {
1384 		send_changed_seq(txfd, &daddr);
1385 		usleep(fin_delay_us);
1386 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1387 	} else if (strcmp(testname, "tcp_ts") == 0) {
1388 		send_changed_ts(txfd, &daddr);
1389 		usleep(fin_delay_us);
1390 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1391 	} else if (strcmp(testname, "tcp_opt") == 0) {
1392 		send_diff_opt(txfd, &daddr);
1393 		usleep(fin_delay_us);
1394 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1395 
1396 	/* ip sub-tests - shared between IPv4 and IPv6 */
1397 	} else if (strcmp(testname, "ip_ecn") == 0) {
1398 		send_changed_ECN(txfd, &daddr);
1399 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1400 	} else if (strcmp(testname, "ip_tos") == 0) {
1401 		send_changed_tos(txfd, &daddr);
1402 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1403 
1404 	/* ip sub-tests - IPv4 only */
1405 	} else if (strcmp(testname, "ip_ttl") == 0) {
1406 		send_changed_ttl(txfd, &daddr);
1407 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1408 	} else if (strcmp(testname, "ip_opt") == 0) {
1409 		send_ip_options(txfd, &daddr);
1410 		usleep(fin_delay_us);
1411 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1412 	} else if (strcmp(testname, "ip_frag4") == 0) {
1413 		send_fragment4(txfd, &daddr);
1414 		usleep(fin_delay_us);
1415 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1416 	} else if (strcmp(testname, "ip_id_df1_inc") == 0) {
1417 		send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_INC);
1418 		usleep(fin_delay_us);
1419 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1420 	} else if (strcmp(testname, "ip_id_df1_fixed") == 0) {
1421 		send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_FIXED);
1422 		usleep(fin_delay_us);
1423 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1424 	} else if (strcmp(testname, "ip_id_df0_inc") == 0) {
1425 		send_flush_id_case(txfd, &daddr, FLUSH_ID_DF0_INC);
1426 		usleep(fin_delay_us);
1427 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1428 	} else if (strcmp(testname, "ip_id_df0_fixed") == 0) {
1429 		send_flush_id_case(txfd, &daddr, FLUSH_ID_DF0_FIXED);
1430 		usleep(fin_delay_us);
1431 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1432 	} else if (strcmp(testname, "ip_id_df1_inc_fixed") == 0) {
1433 		send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_INC_FIXED);
1434 		usleep(fin_delay_us);
1435 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1436 	} else if (strcmp(testname, "ip_id_df1_fixed_inc") == 0) {
1437 		send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_FIXED_INC);
1438 		usleep(fin_delay_us);
1439 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1440 
1441 	/* ip sub-tests - IPv6 only */
1442 	} else if (strcmp(testname, "ip_frag6") == 0) {
1443 		send_fragment6(txfd, &daddr);
1444 		usleep(fin_delay_us);
1445 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1446 	} else if (strcmp(testname, "ip_v6ext_same") == 0) {
1447 		send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_1);
1448 		usleep(fin_delay_us);
1449 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1450 	} else if (strcmp(testname, "ip_v6ext_diff") == 0) {
1451 		send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_2);
1452 		usleep(fin_delay_us);
1453 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1454 
1455 	/* large sub-tests */
1456 	} else if (strcmp(testname, "large_max") == 0) {
1457 		int remainder = max_payload() % calc_mss();
1458 
1459 		send_large(txfd, &daddr, remainder);
1460 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1461 	} else if (strcmp(testname, "large_rem") == 0) {
1462 		int remainder = max_payload() % calc_mss();
1463 
1464 		send_large(txfd, &daddr, remainder + 1);
1465 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1466 
1467 	/* machinery sub-tests */
1468 	} else if (strcmp(testname, "single") == 0) {
1469 		static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
1470 
1471 		create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
1472 		write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr);
1473 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1474 	} else if (strcmp(testname, "capacity") == 0) {
1475 		send_capacity(txfd, &daddr);
1476 		usleep(fin_delay_us);
1477 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
1478 
1479 	} else {
1480 		error(1, 0, "Unknown testcase: %s", testname);
1481 	}
1482 
1483 	if (close(txfd))
1484 		error(1, errno, "socket close");
1485 }
1486 
1487 static void gro_receiver(void)
1488 {
1489 	static int correct_payload[NUM_PACKETS];
1490 	int rxfd = -1;
1491 
1492 	rxfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_NONE));
1493 	if (rxfd < 0)
1494 		error(1, 0, "socket creation");
1495 	setup_sock_filter(rxfd);
1496 	set_timeout(rxfd);
1497 	set_rcvbuf(rxfd);
1498 	bind_packetsocket(rxfd);
1499 
1500 	ksft_ready();
1501 
1502 	memset(correct_payload, 0, sizeof(correct_payload));
1503 
1504 	/* data sub-tests */
1505 	if (strcmp(testname, "data_same") == 0) {
1506 		printf("pure data packet of same size: ");
1507 		correct_payload[0] = PAYLOAD_LEN * 2;
1508 		check_recv_pkts(rxfd, correct_payload, 1);
1509 	} else if (strcmp(testname, "data_lrg_sml") == 0) {
1510 		printf("large data packets followed by a smaller one: ");
1511 		correct_payload[0] = PAYLOAD_LEN * 1.5;
1512 		check_recv_pkts(rxfd, correct_payload, 1);
1513 	} else if (strcmp(testname, "data_lrg_1byte") == 0) {
1514 		printf("large data packet followed by a 1 byte one: ");
1515 		correct_payload[0] = PAYLOAD_LEN + 1;
1516 		check_recv_pkts(rxfd, correct_payload, 1);
1517 	} else if (strcmp(testname, "data_sml_lrg") == 0) {
1518 		printf("small data packets followed by a larger one: ");
1519 		correct_payload[0] = PAYLOAD_LEN / 2;
1520 		correct_payload[1] = PAYLOAD_LEN;
1521 		check_recv_pkts(rxfd, correct_payload, 2);
1522 	} else if (strcmp(testname, "data_burst") == 0) {
1523 		printf("two bursts of two data packets: ");
1524 		correct_payload[0] = PAYLOAD_LEN * 2;
1525 		correct_payload[1] = PAYLOAD_LEN * 2;
1526 		check_recv_pkts(rxfd, correct_payload, 2);
1527 
1528 	/* ack test */
1529 	} else if (strcmp(testname, "ack") == 0) {
1530 		printf("duplicate ack and pure ack: ");
1531 		check_recv_pkts(rxfd, correct_payload, 3);
1532 
1533 	/* flags sub-tests */
1534 	} else if (strcmp(testname, "flags_psh") == 0) {
1535 		correct_payload[0] = PAYLOAD_LEN * 3;
1536 		correct_payload[1] = PAYLOAD_LEN * 2;
1537 		printf("psh flag ends coalescing: ");
1538 		check_recv_pkts(rxfd, correct_payload, 2);
1539 	} else if (strcmp(testname, "flags_syn") == 0) {
1540 		correct_payload[0] = PAYLOAD_LEN * 2;
1541 		correct_payload[1] = 0;
1542 		correct_payload[2] = PAYLOAD_LEN * 2;
1543 		printf("syn flag ends coalescing: ");
1544 		check_recv_pkts(rxfd, correct_payload, 3);
1545 	} else if (strcmp(testname, "flags_rst") == 0) {
1546 		correct_payload[0] = PAYLOAD_LEN * 2;
1547 		correct_payload[1] = 0;
1548 		correct_payload[2] = PAYLOAD_LEN * 2;
1549 		printf("rst flag ends coalescing: ");
1550 		check_recv_pkts(rxfd, correct_payload, 3);
1551 	} else if (strcmp(testname, "flags_urg") == 0) {
1552 		correct_payload[0] = PAYLOAD_LEN * 2;
1553 		correct_payload[1] = 0;
1554 		correct_payload[2] = PAYLOAD_LEN * 2;
1555 		printf("urg flag ends coalescing: ");
1556 		check_recv_pkts(rxfd, correct_payload, 3);
1557 	} else if (strcmp(testname, "flags_cwr") == 0) {
1558 		correct_payload[0] = PAYLOAD_LEN;
1559 		correct_payload[1] = PAYLOAD_LEN * 2;
1560 		correct_payload[2] = PAYLOAD_LEN * 2;
1561 		printf("cwr flag ends coalescing: ");
1562 		check_recv_pkts(rxfd, correct_payload, 3);
1563 
1564 	/* tcp sub-tests */
1565 	} else if (strcmp(testname, "tcp_csum") == 0) {
1566 		correct_payload[0] = PAYLOAD_LEN;
1567 		correct_payload[1] = PAYLOAD_LEN;
1568 		printf("changed checksum does not coalesce: ");
1569 		check_recv_pkts(rxfd, correct_payload, 2);
1570 	} else if (strcmp(testname, "tcp_seq") == 0) {
1571 		correct_payload[0] = PAYLOAD_LEN;
1572 		correct_payload[1] = PAYLOAD_LEN;
1573 		printf("Wrong Seq number doesn't coalesce: ");
1574 		check_recv_pkts(rxfd, correct_payload, 2);
1575 	} else if (strcmp(testname, "tcp_ts") == 0) {
1576 		correct_payload[0] = PAYLOAD_LEN * 2;
1577 		correct_payload[1] = PAYLOAD_LEN;
1578 		correct_payload[2] = PAYLOAD_LEN;
1579 		correct_payload[3] = PAYLOAD_LEN;
1580 		printf("Different timestamp doesn't coalesce: ");
1581 		check_recv_pkts(rxfd, correct_payload, 4);
1582 	} else if (strcmp(testname, "tcp_opt") == 0) {
1583 		correct_payload[0] = PAYLOAD_LEN * 2;
1584 		correct_payload[1] = PAYLOAD_LEN;
1585 		printf("Different options doesn't coalesce: ");
1586 		check_recv_pkts(rxfd, correct_payload, 2);
1587 
1588 	/* ip sub-tests - shared between IPv4 and IPv6 */
1589 	} else if (strcmp(testname, "ip_ecn") == 0) {
1590 		correct_payload[0] = PAYLOAD_LEN;
1591 		correct_payload[1] = PAYLOAD_LEN;
1592 		printf("different ECN doesn't coalesce: ");
1593 		check_recv_pkts(rxfd, correct_payload, 2);
1594 	} else if (strcmp(testname, "ip_tos") == 0) {
1595 		correct_payload[0] = PAYLOAD_LEN;
1596 		correct_payload[1] = PAYLOAD_LEN;
1597 		printf("different tos doesn't coalesce: ");
1598 		check_recv_pkts(rxfd, correct_payload, 2);
1599 
1600 	/* ip sub-tests - IPv4 only */
1601 	} else if (strcmp(testname, "ip_ttl") == 0) {
1602 		correct_payload[0] = PAYLOAD_LEN;
1603 		correct_payload[1] = PAYLOAD_LEN;
1604 		printf("different ttl doesn't coalesce: ");
1605 		check_recv_pkts(rxfd, correct_payload, 2);
1606 	} else if (strcmp(testname, "ip_opt") == 0) {
1607 		correct_payload[0] = PAYLOAD_LEN;
1608 		correct_payload[1] = PAYLOAD_LEN;
1609 		correct_payload[2] = PAYLOAD_LEN;
1610 		printf("ip options doesn't coalesce: ");
1611 		check_recv_pkts(rxfd, correct_payload, 3);
1612 	} else if (strcmp(testname, "ip_frag4") == 0) {
1613 		correct_payload[0] = PAYLOAD_LEN;
1614 		correct_payload[1] = PAYLOAD_LEN;
1615 		printf("fragmented ip4 doesn't coalesce: ");
1616 		check_recv_pkts(rxfd, correct_payload, 2);
1617 	} else if (strcmp(testname, "ip_id_df1_inc") == 0) {
1618 		printf("DF=1, Incrementing - should coalesce: ");
1619 		correct_payload[0] = PAYLOAD_LEN * 2;
1620 		check_recv_pkts(rxfd, correct_payload, 1);
1621 	} else if (strcmp(testname, "ip_id_df1_fixed") == 0) {
1622 		printf("DF=1, Fixed - should coalesce: ");
1623 		correct_payload[0] = PAYLOAD_LEN * 2;
1624 		check_recv_pkts(rxfd, correct_payload, 1);
1625 	} else if (strcmp(testname, "ip_id_df0_inc") == 0) {
1626 		printf("DF=0, Incrementing - should coalesce: ");
1627 		correct_payload[0] = PAYLOAD_LEN * 2;
1628 		check_recv_pkts(rxfd, correct_payload, 1);
1629 	} else if (strcmp(testname, "ip_id_df0_fixed") == 0) {
1630 		printf("DF=0, Fixed - should coalesce: ");
1631 		correct_payload[0] = PAYLOAD_LEN * 2;
1632 		check_recv_pkts(rxfd, correct_payload, 1);
1633 	} else if (strcmp(testname, "ip_id_df1_inc_fixed") == 0) {
1634 		printf("DF=1, 2 Incrementing and one fixed - should coalesce only first 2 packets: ");
1635 		correct_payload[0] = PAYLOAD_LEN * 2;
1636 		correct_payload[1] = PAYLOAD_LEN;
1637 		check_recv_pkts(rxfd, correct_payload, 2);
1638 	} else if (strcmp(testname, "ip_id_df1_fixed_inc") == 0) {
1639 		printf("DF=1, 2 Fixed and one incrementing - should coalesce only first 2 packets: ");
1640 		correct_payload[0] = PAYLOAD_LEN * 2;
1641 		correct_payload[1] = PAYLOAD_LEN;
1642 		check_recv_pkts(rxfd, correct_payload, 2);
1643 
1644 	/* ip sub-tests - IPv6 only */
1645 	} else if (strcmp(testname, "ip_frag6") == 0) {
1646 		/* GRO doesn't check for ipv6 hop limit when flushing.
1647 		 * Hence no corresponding test to the ipv4 case.
1648 		 */
1649 		printf("fragmented ip6 doesn't coalesce: ");
1650 		correct_payload[0] = PAYLOAD_LEN * 2;
1651 		correct_payload[1] = PAYLOAD_LEN;
1652 		correct_payload[2] = PAYLOAD_LEN;
1653 		check_recv_pkts(rxfd, correct_payload, 3);
1654 	} else if (strcmp(testname, "ip_v6ext_same") == 0) {
1655 		printf("ipv6 with ext header does coalesce: ");
1656 		correct_payload[0] = PAYLOAD_LEN * 2;
1657 		check_recv_pkts(rxfd, correct_payload, 1);
1658 	} else if (strcmp(testname, "ip_v6ext_diff") == 0) {
1659 		printf("ipv6 with ext header with different payloads doesn't coalesce: ");
1660 		correct_payload[0] = PAYLOAD_LEN;
1661 		correct_payload[1] = PAYLOAD_LEN;
1662 		check_recv_pkts(rxfd, correct_payload, 2);
1663 
1664 	/* large sub-tests */
1665 	} else if (strcmp(testname, "large_max") == 0) {
1666 		int remainder = max_payload() % calc_mss();
1667 
1668 		correct_payload[0] = max_payload();
1669 		correct_payload[1] = remainder;
1670 		printf("Shouldn't coalesce if exceed IP max pkt size: ");
1671 		check_recv_pkts(rxfd, correct_payload, 2);
1672 	} else if (strcmp(testname, "large_rem") == 0) {
1673 		int remainder = max_payload() % calc_mss();
1674 
1675 		/* last segment sent individually, doesn't start new segment */
1676 		correct_payload[0] = max_payload() - remainder;
1677 		correct_payload[1] = remainder + 1;
1678 		correct_payload[2] = remainder + 1;
1679 		printf("last segment sent individually: ");
1680 		check_recv_pkts(rxfd, correct_payload, 3);
1681 
1682 	/* machinery sub-tests */
1683 	} else if (strcmp(testname, "single") == 0) {
1684 		printf("single data packet: ");
1685 		correct_payload[0] = PAYLOAD_LEN;
1686 		check_recv_pkts(rxfd, correct_payload, 1);
1687 	} else if (strcmp(testname, "capacity") == 0) {
1688 		check_capacity_pkts(rxfd);
1689 
1690 	} else {
1691 		error(1, 0, "Test case error: unknown testname %s", testname);
1692 	}
1693 
1694 	if (close(rxfd))
1695 		error(1, 0, "socket close");
1696 }
1697 
1698 static void parse_args(int argc, char **argv)
1699 {
1700 	static const struct option opts[] = {
1701 		{ "daddr", required_argument, NULL, 'd' },
1702 		{ "dmac", required_argument, NULL, 'D' },
1703 		{ "iface", required_argument, NULL, 'i' },
1704 		{ "ipv4", no_argument, NULL, '4' },
1705 		{ "ipv6", no_argument, NULL, '6' },
1706 		{ "ipip", no_argument, NULL, 'e' },
1707 		{ "ip6ip6", no_argument, NULL, 'E' },
1708 		{ "num-flows", required_argument, NULL, 'n' },
1709 		{ "rx", no_argument, NULL, 'r' },
1710 		{ "saddr", required_argument, NULL, 's' },
1711 		{ "smac", required_argument, NULL, 'S' },
1712 		{ "test", required_argument, NULL, 't' },
1713 		{ "order-check", no_argument, NULL, 'o' },
1714 		{ "verbose", no_argument, NULL, 'v' },
1715 		{ 0, 0, 0, 0 }
1716 	};
1717 	int c;
1718 
1719 	while ((c = getopt_long(argc, argv, "46d:D:eEi:n:rs:S:t:ov", opts, NULL)) != -1) {
1720 		switch (c) {
1721 		case '4':
1722 			proto = PF_INET;
1723 			ethhdr_proto = htons(ETH_P_IP);
1724 			break;
1725 		case '6':
1726 			proto = PF_INET6;
1727 			ethhdr_proto = htons(ETH_P_IPV6);
1728 			break;
1729 		case 'e':
1730 			ipip = true;
1731 			proto = PF_INET;
1732 			ethhdr_proto = htons(ETH_P_IP);
1733 			break;
1734 		case 'E':
1735 			ip6ip6 = true;
1736 			proto = PF_INET6;
1737 			ethhdr_proto = htons(ETH_P_IPV6);
1738 			break;
1739 		case 'd':
1740 			addr4_dst = addr6_dst = optarg;
1741 			break;
1742 		case 'D':
1743 			dmac = optarg;
1744 			break;
1745 		case 'i':
1746 			ifname = optarg;
1747 			break;
1748 		case 'n':
1749 			num_flows = atoi(optarg);
1750 			break;
1751 		case 'r':
1752 			tx_socket = false;
1753 			break;
1754 		case 's':
1755 			addr4_src = addr6_src = optarg;
1756 			break;
1757 		case 'S':
1758 			smac = optarg;
1759 			break;
1760 		case 't':
1761 			testname = optarg;
1762 			break;
1763 		case 'o':
1764 			order_check = true;
1765 			break;
1766 		case 'v':
1767 			verbose = true;
1768 			break;
1769 		default:
1770 			error(1, 0, "%s invalid option %c\n", __func__, c);
1771 			break;
1772 		}
1773 	}
1774 }
1775 
1776 int main(int argc, char **argv)
1777 {
1778 	parse_args(argc, argv);
1779 
1780 	if (ipip) {
1781 		tcp_offset = ETH_HLEN + sizeof(struct iphdr) * 2;
1782 		total_hdr_len = tcp_offset + sizeof(struct tcphdr);
1783 	} else if (ip6ip6) {
1784 		tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr) * 2;
1785 		total_hdr_len = tcp_offset + sizeof(struct tcphdr);
1786 	} else if (proto == PF_INET) {
1787 		tcp_offset = ETH_HLEN + sizeof(struct iphdr);
1788 		total_hdr_len = tcp_offset + sizeof(struct tcphdr);
1789 	} else if (proto == PF_INET6) {
1790 		tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr);
1791 		total_hdr_len = tcp_offset + sizeof(struct tcphdr);
1792 	} else {
1793 		error(1, 0, "Protocol family is not ipv4 or ipv6");
1794 	}
1795 
1796 	read_MAC(src_mac, smac);
1797 	read_MAC(dst_mac, dmac);
1798 
1799 	if (tx_socket) {
1800 		gro_sender();
1801 	} else {
1802 		/* Only the receiver exit status determines test success. */
1803 		gro_receiver();
1804 		fprintf(stderr, "Gro::%s test passed.\n", testname);
1805 	}
1806 
1807 	return 0;
1808 }
1809