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