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