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