xref: /openbmc/linux/tools/testing/selftests/net/gro.c (revision 87f7282e76bef97d775a7cd61dc1988fc5ed8639)
17d157501SCoco Li // SPDX-License-Identifier: GPL-2.0
27d157501SCoco Li /*
37d157501SCoco Li  * This testsuite provides conformance testing for GRO coalescing.
47d157501SCoco Li  *
57d157501SCoco Li  * Test cases:
67d157501SCoco Li  * 1.data
77d157501SCoco Li  *  Data packets of the same size and same header setup with correct
87d157501SCoco Li  *  sequence numbers coalesce. The one exception being the last data
97d157501SCoco Li  *  packet coalesced: it can be smaller than the rest and coalesced
107d157501SCoco Li  *  as long as it is in the same flow.
117d157501SCoco Li  * 2.ack
127d157501SCoco Li  *  Pure ACK does not coalesce.
137d157501SCoco Li  * 3.flags
147d157501SCoco Li  *  Specific test cases: no packets with PSH, SYN, URG, RST set will
157d157501SCoco Li  *  be coalesced.
167d157501SCoco Li  * 4.tcp
177d157501SCoco Li  *  Packets with incorrect checksum, non-consecutive seqno and
187d157501SCoco Li  *  different TCP header options shouldn't coalesce. Nit: given that
197d157501SCoco Li  *  some extension headers have paddings, such as timestamp, headers
207d157501SCoco Li  *  that are padding differently would not be coalesced.
217d157501SCoco Li  * 5.ip:
227d157501SCoco Li  *  Packets with different (ECN, TTL, TOS) header, ip options or
237d157501SCoco Li  *  ip fragments (ipv6) shouldn't coalesce.
247d157501SCoco Li  * 6.large:
257d157501SCoco Li  *  Packets larger than GRO_MAX_SIZE packets shouldn't coalesce.
267d157501SCoco Li  *
277d157501SCoco Li  * MSS is defined as 4096 - header because if it is too small
287d157501SCoco Li  * (i.e. 1500 MTU - header), it will result in many packets,
297d157501SCoco Li  * increasing the "large" test case's flakiness. This is because
307d157501SCoco Li  * due to time sensitivity in the coalescing window, the receiver
317d157501SCoco Li  * may not coalesce all of the packets.
327d157501SCoco Li  *
337d157501SCoco Li  * Note the timing issue applies to all of the test cases, so some
347d157501SCoco Li  * flakiness is to be expected.
357d157501SCoco Li  *
367d157501SCoco Li  */
377d157501SCoco Li 
387d157501SCoco Li #define _GNU_SOURCE
397d157501SCoco Li 
407d157501SCoco Li #include <arpa/inet.h>
417d157501SCoco Li #include <errno.h>
427d157501SCoco Li #include <error.h>
437d157501SCoco Li #include <getopt.h>
447d157501SCoco Li #include <linux/filter.h>
457d157501SCoco Li #include <linux/if_packet.h>
467d157501SCoco Li #include <linux/ipv6.h>
477d157501SCoco Li #include <net/ethernet.h>
487d157501SCoco Li #include <net/if.h>
497d157501SCoco Li #include <netinet/in.h>
507d157501SCoco Li #include <netinet/ip.h>
517d157501SCoco Li #include <netinet/ip6.h>
527d157501SCoco Li #include <netinet/tcp.h>
537d157501SCoco Li #include <stdbool.h>
547d157501SCoco Li #include <stddef.h>
557d157501SCoco Li #include <stdio.h>
567d157501SCoco Li #include <stdarg.h>
577d157501SCoco Li #include <string.h>
587d157501SCoco Li #include <unistd.h>
597d157501SCoco Li 
607d157501SCoco Li #define DPORT 8000
617d157501SCoco Li #define SPORT 1500
627d157501SCoco Li #define PAYLOAD_LEN 100
637d157501SCoco Li #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
647d157501SCoco Li #define NUM_PACKETS 4
657d157501SCoco Li #define START_SEQ 100
667d157501SCoco Li #define START_ACK 100
677d157501SCoco Li #define ETH_P_NONE 0
687d157501SCoco Li #define TOTAL_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
697d157501SCoco Li #define MSS (4096 - sizeof(struct tcphdr) - sizeof(struct ipv6hdr))
707d157501SCoco Li #define MAX_PAYLOAD (IP_MAXPACKET - sizeof(struct tcphdr) - sizeof(struct ipv6hdr))
717d157501SCoco Li #define NUM_LARGE_PKT (MAX_PAYLOAD / MSS)
727d157501SCoco Li #define MAX_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
737d157501SCoco Li 
74*87f7282eSWillem de Bruijn static const char *addr6_src = "fdaa::2";
75*87f7282eSWillem de Bruijn static const char *addr6_dst = "fdaa::1";
76*87f7282eSWillem de Bruijn static const char *addr4_src = "192.168.1.200";
77*87f7282eSWillem de Bruijn static const char *addr4_dst = "192.168.1.100";
787d157501SCoco Li static int proto = -1;
797d157501SCoco Li static uint8_t src_mac[ETH_ALEN], dst_mac[ETH_ALEN];
807d157501SCoco Li static char *testname = "data";
817d157501SCoco Li static char *ifname = "eth0";
827d157501SCoco Li static char *smac = "aa:00:00:00:00:02";
837d157501SCoco Li static char *dmac = "aa:00:00:00:00:01";
847d157501SCoco Li static bool verbose;
857d157501SCoco Li static bool tx_socket = true;
867d157501SCoco Li static int tcp_offset = -1;
877d157501SCoco Li static int total_hdr_len = -1;
887d157501SCoco Li static int ethhdr_proto = -1;
897d157501SCoco Li 
907d157501SCoco Li static void vlog(const char *fmt, ...)
917d157501SCoco Li {
927d157501SCoco Li 	va_list args;
937d157501SCoco Li 
947d157501SCoco Li 	if (verbose) {
957d157501SCoco Li 		va_start(args, fmt);
967d157501SCoco Li 		vfprintf(stderr, fmt, args);
977d157501SCoco Li 		va_end(args);
987d157501SCoco Li 	}
997d157501SCoco Li }
1007d157501SCoco Li 
1017d157501SCoco Li static void setup_sock_filter(int fd)
1027d157501SCoco Li {
1037d157501SCoco Li 	const int dport_off = tcp_offset + offsetof(struct tcphdr, dest);
1047d157501SCoco Li 	const int ethproto_off = offsetof(struct ethhdr, h_proto);
1057d157501SCoco Li 	int optlen = 0;
1067d157501SCoco Li 	int ipproto_off;
1077d157501SCoco Li 	int next_off;
1087d157501SCoco Li 
1097d157501SCoco Li 	if (proto == PF_INET)
1107d157501SCoco Li 		next_off = offsetof(struct iphdr, protocol);
1117d157501SCoco Li 	else
1127d157501SCoco Li 		next_off = offsetof(struct ipv6hdr, nexthdr);
1137d157501SCoco Li 	ipproto_off = ETH_HLEN + next_off;
1147d157501SCoco Li 
1157d157501SCoco Li 	if (strcmp(testname, "ip") == 0) {
1167d157501SCoco Li 		if (proto == PF_INET)
1177d157501SCoco Li 			optlen = sizeof(struct ip_timestamp);
1187d157501SCoco Li 		else
1197d157501SCoco Li 			optlen = sizeof(struct ip6_frag);
1207d157501SCoco Li 	}
1217d157501SCoco Li 
1227d157501SCoco Li 	struct sock_filter filter[] = {
1237d157501SCoco Li 			BPF_STMT(BPF_LD  + BPF_H   + BPF_ABS, ethproto_off),
1247d157501SCoco Li 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ntohs(ethhdr_proto), 0, 7),
1257d157501SCoco Li 			BPF_STMT(BPF_LD  + BPF_B   + BPF_ABS, ipproto_off),
1267d157501SCoco Li 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 0, 5),
1277d157501SCoco Li 			BPF_STMT(BPF_LD  + BPF_H   + BPF_ABS, dport_off),
1287d157501SCoco Li 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 2, 0),
1297d157501SCoco Li 			BPF_STMT(BPF_LD  + BPF_H   + BPF_ABS, dport_off + optlen),
1307d157501SCoco Li 			BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 0, 1),
1317d157501SCoco Li 			BPF_STMT(BPF_RET + BPF_K, 0xFFFFFFFF),
1327d157501SCoco Li 			BPF_STMT(BPF_RET + BPF_K, 0),
1337d157501SCoco Li 	};
1347d157501SCoco Li 
1357d157501SCoco Li 	struct sock_fprog bpf = {
1367d157501SCoco Li 		.len = ARRAY_SIZE(filter),
1377d157501SCoco Li 		.filter = filter,
1387d157501SCoco Li 	};
1397d157501SCoco Li 
1407d157501SCoco Li 	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) < 0)
1417d157501SCoco Li 		error(1, errno, "error setting filter");
1427d157501SCoco Li }
1437d157501SCoco Li 
1447d157501SCoco Li static uint32_t checksum_nofold(void *data, size_t len, uint32_t sum)
1457d157501SCoco Li {
1467d157501SCoco Li 	uint16_t *words = data;
1477d157501SCoco Li 	int i;
1487d157501SCoco Li 
1497d157501SCoco Li 	for (i = 0; i < len / 2; i++)
1507d157501SCoco Li 		sum += words[i];
1517d157501SCoco Li 	if (len & 1)
1527d157501SCoco Li 		sum += ((char *)data)[len - 1];
1537d157501SCoco Li 	return sum;
1547d157501SCoco Li }
1557d157501SCoco Li 
1567d157501SCoco Li static uint16_t checksum_fold(void *data, size_t len, uint32_t sum)
1577d157501SCoco Li {
1587d157501SCoco Li 	sum = checksum_nofold(data, len, sum);
1597d157501SCoco Li 	while (sum > 0xFFFF)
1607d157501SCoco Li 		sum = (sum & 0xFFFF) + (sum >> 16);
1617d157501SCoco Li 	return ~sum;
1627d157501SCoco Li }
1637d157501SCoco Li 
1647d157501SCoco Li static uint16_t tcp_checksum(void *buf, int payload_len)
1657d157501SCoco Li {
1667d157501SCoco Li 	struct pseudo_header6 {
1677d157501SCoco Li 		struct in6_addr saddr;
1687d157501SCoco Li 		struct in6_addr daddr;
1697d157501SCoco Li 		uint16_t protocol;
1707d157501SCoco Li 		uint16_t payload_len;
1717d157501SCoco Li 	} ph6;
1727d157501SCoco Li 	struct pseudo_header4 {
1737d157501SCoco Li 		struct in_addr saddr;
1747d157501SCoco Li 		struct in_addr daddr;
1757d157501SCoco Li 		uint16_t protocol;
1767d157501SCoco Li 		uint16_t payload_len;
1777d157501SCoco Li 	} ph4;
1787d157501SCoco Li 	uint32_t sum = 0;
1797d157501SCoco Li 
1807d157501SCoco Li 	if (proto == PF_INET6) {
181*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET6, addr6_src, &ph6.saddr) != 1)
1827d157501SCoco Li 			error(1, errno, "inet_pton6 source ip pseudo");
183*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET6, addr6_dst, &ph6.daddr) != 1)
1847d157501SCoco Li 			error(1, errno, "inet_pton6 dest ip pseudo");
1857d157501SCoco Li 		ph6.protocol = htons(IPPROTO_TCP);
1867d157501SCoco Li 		ph6.payload_len = htons(sizeof(struct tcphdr) + payload_len);
1877d157501SCoco Li 
1887d157501SCoco Li 		sum = checksum_nofold(&ph6, sizeof(ph6), 0);
1897d157501SCoco Li 	} else if (proto == PF_INET) {
190*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET, addr4_src, &ph4.saddr) != 1)
1917d157501SCoco Li 			error(1, errno, "inet_pton source ip pseudo");
192*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET, addr4_dst, &ph4.daddr) != 1)
1937d157501SCoco Li 			error(1, errno, "inet_pton dest ip pseudo");
1947d157501SCoco Li 		ph4.protocol = htons(IPPROTO_TCP);
1957d157501SCoco Li 		ph4.payload_len = htons(sizeof(struct tcphdr) + payload_len);
1967d157501SCoco Li 
1977d157501SCoco Li 		sum = checksum_nofold(&ph4, sizeof(ph4), 0);
1987d157501SCoco Li 	}
1997d157501SCoco Li 
2007d157501SCoco Li 	return checksum_fold(buf, sizeof(struct tcphdr) + payload_len, sum);
2017d157501SCoco Li }
2027d157501SCoco Li 
2037d157501SCoco Li static void read_MAC(uint8_t *mac_addr, char *mac)
2047d157501SCoco Li {
2057d157501SCoco Li 	if (sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
2067d157501SCoco Li 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
2077d157501SCoco Li 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6)
2087d157501SCoco Li 		error(1, 0, "sscanf");
2097d157501SCoco Li }
2107d157501SCoco Li 
2117d157501SCoco Li static void fill_datalinklayer(void *buf)
2127d157501SCoco Li {
2137d157501SCoco Li 	struct ethhdr *eth = buf;
2147d157501SCoco Li 
2157d157501SCoco Li 	memcpy(eth->h_dest, dst_mac, ETH_ALEN);
2167d157501SCoco Li 	memcpy(eth->h_source, src_mac, ETH_ALEN);
2177d157501SCoco Li 	eth->h_proto = ethhdr_proto;
2187d157501SCoco Li }
2197d157501SCoco Li 
2207d157501SCoco Li static void fill_networklayer(void *buf, int payload_len)
2217d157501SCoco Li {
2227d157501SCoco Li 	struct ipv6hdr *ip6h = buf;
2237d157501SCoco Li 	struct iphdr *iph = buf;
2247d157501SCoco Li 
2257d157501SCoco Li 	if (proto == PF_INET6) {
2267d157501SCoco Li 		memset(ip6h, 0, sizeof(*ip6h));
2277d157501SCoco Li 
2287d157501SCoco Li 		ip6h->version = 6;
2297d157501SCoco Li 		ip6h->payload_len = htons(sizeof(struct tcphdr) + payload_len);
2307d157501SCoco Li 		ip6h->nexthdr = IPPROTO_TCP;
2317d157501SCoco Li 		ip6h->hop_limit = 8;
232*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET6, addr6_src, &ip6h->saddr) != 1)
2337d157501SCoco Li 			error(1, errno, "inet_pton source ip6");
234*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET6, addr6_dst, &ip6h->daddr) != 1)
2357d157501SCoco Li 			error(1, errno, "inet_pton dest ip6");
2367d157501SCoco Li 	} else if (proto == PF_INET) {
2377d157501SCoco Li 		memset(iph, 0, sizeof(*iph));
2387d157501SCoco Li 
2397d157501SCoco Li 		iph->version = 4;
2407d157501SCoco Li 		iph->ihl = 5;
2417d157501SCoco Li 		iph->ttl = 8;
2427d157501SCoco Li 		iph->protocol	= IPPROTO_TCP;
2437d157501SCoco Li 		iph->tot_len = htons(sizeof(struct tcphdr) +
2447d157501SCoco Li 				payload_len + sizeof(struct iphdr));
2457d157501SCoco Li 		iph->frag_off = htons(0x4000); /* DF = 1, MF = 0 */
246*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET, addr4_src, &iph->saddr) != 1)
2477d157501SCoco Li 			error(1, errno, "inet_pton source ip");
248*87f7282eSWillem de Bruijn 		if (inet_pton(AF_INET, addr4_dst, &iph->daddr) != 1)
2497d157501SCoco Li 			error(1, errno, "inet_pton dest ip");
2507d157501SCoco Li 		iph->check = checksum_fold(buf, sizeof(struct iphdr), 0);
2517d157501SCoco Li 	}
2527d157501SCoco Li }
2537d157501SCoco Li 
2547d157501SCoco Li static void fill_transportlayer(void *buf, int seq_offset, int ack_offset,
2557d157501SCoco Li 				int payload_len, int fin)
2567d157501SCoco Li {
2577d157501SCoco Li 	struct tcphdr *tcph = buf;
2587d157501SCoco Li 
2597d157501SCoco Li 	memset(tcph, 0, sizeof(*tcph));
2607d157501SCoco Li 
2617d157501SCoco Li 	tcph->source = htons(SPORT);
2627d157501SCoco Li 	tcph->dest = htons(DPORT);
2637d157501SCoco Li 	tcph->seq = ntohl(START_SEQ + seq_offset);
2647d157501SCoco Li 	tcph->ack_seq = ntohl(START_ACK + ack_offset);
2657d157501SCoco Li 	tcph->ack = 1;
2667d157501SCoco Li 	tcph->fin = fin;
2677d157501SCoco Li 	tcph->doff = 5;
2687d157501SCoco Li 	tcph->window = htons(TCP_MAXWIN);
2697d157501SCoco Li 	tcph->urg_ptr = 0;
2707d157501SCoco Li 	tcph->check = tcp_checksum(tcph, payload_len);
2717d157501SCoco Li }
2727d157501SCoco Li 
2737d157501SCoco Li static void write_packet(int fd, char *buf, int len, struct sockaddr_ll *daddr)
2747d157501SCoco Li {
2757d157501SCoco Li 	int ret = -1;
2767d157501SCoco Li 
2777d157501SCoco Li 	ret = sendto(fd, buf, len, 0, (struct sockaddr *)daddr, sizeof(*daddr));
2787d157501SCoco Li 	if (ret == -1)
2797d157501SCoco Li 		error(1, errno, "sendto failure");
2807d157501SCoco Li 	if (ret != len)
2817d157501SCoco Li 		error(1, errno, "sendto wrong length");
2827d157501SCoco Li }
2837d157501SCoco Li 
2847d157501SCoco Li static void create_packet(void *buf, int seq_offset, int ack_offset,
2857d157501SCoco Li 			  int payload_len, int fin)
2867d157501SCoco Li {
2877d157501SCoco Li 	memset(buf, 0, total_hdr_len);
2887d157501SCoco Li 	memset(buf + total_hdr_len, 'a', payload_len);
2897d157501SCoco Li 	fill_transportlayer(buf + tcp_offset, seq_offset, ack_offset,
2907d157501SCoco Li 			    payload_len, fin);
2917d157501SCoco Li 	fill_networklayer(buf + ETH_HLEN, payload_len);
2927d157501SCoco Li 	fill_datalinklayer(buf);
2937d157501SCoco Li }
2947d157501SCoco Li 
2957d157501SCoco Li /* send one extra flag, not first and not last pkt */
2967d157501SCoco Li static void send_flags(int fd, struct sockaddr_ll *daddr, int psh, int syn,
2977d157501SCoco Li 		       int rst, int urg)
2987d157501SCoco Li {
2997d157501SCoco Li 	static char flag_buf[MAX_HDR_LEN + PAYLOAD_LEN];
3007d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
3017d157501SCoco Li 	int payload_len, pkt_size, flag, i;
3027d157501SCoco Li 	struct tcphdr *tcph;
3037d157501SCoco Li 
3047d157501SCoco Li 	payload_len = PAYLOAD_LEN * psh;
3057d157501SCoco Li 	pkt_size = total_hdr_len + payload_len;
3067d157501SCoco Li 	flag = NUM_PACKETS / 2;
3077d157501SCoco Li 
3087d157501SCoco Li 	create_packet(flag_buf, flag * payload_len, 0, payload_len, 0);
3097d157501SCoco Li 
3107d157501SCoco Li 	tcph = (struct tcphdr *)(flag_buf + tcp_offset);
3117d157501SCoco Li 	tcph->psh = psh;
3127d157501SCoco Li 	tcph->syn = syn;
3137d157501SCoco Li 	tcph->rst = rst;
3147d157501SCoco Li 	tcph->urg = urg;
3157d157501SCoco Li 	tcph->check = 0;
3167d157501SCoco Li 	tcph->check = tcp_checksum(tcph, payload_len);
3177d157501SCoco Li 
3187d157501SCoco Li 	for (i = 0; i < NUM_PACKETS + 1; i++) {
3197d157501SCoco Li 		if (i == flag) {
3207d157501SCoco Li 			write_packet(fd, flag_buf, pkt_size, daddr);
3217d157501SCoco Li 			continue;
3227d157501SCoco Li 		}
3237d157501SCoco Li 		create_packet(buf, i * PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
3247d157501SCoco Li 		write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
3257d157501SCoco Li 	}
3267d157501SCoco Li }
3277d157501SCoco Li 
3287d157501SCoco Li /* Test for data of same length, smaller than previous
3297d157501SCoco Li  * and of different lengths
3307d157501SCoco Li  */
3317d157501SCoco Li static void send_data_pkts(int fd, struct sockaddr_ll *daddr,
3327d157501SCoco Li 			   int payload_len1, int payload_len2)
3337d157501SCoco Li {
3347d157501SCoco Li 	static char buf[ETH_HLEN + IP_MAXPACKET];
3357d157501SCoco Li 
3367d157501SCoco Li 	create_packet(buf, 0, 0, payload_len1, 0);
3377d157501SCoco Li 	write_packet(fd, buf, total_hdr_len + payload_len1, daddr);
3387d157501SCoco Li 	create_packet(buf, payload_len1, 0, payload_len2, 0);
3397d157501SCoco Li 	write_packet(fd, buf, total_hdr_len + payload_len2, daddr);
3407d157501SCoco Li }
3417d157501SCoco Li 
3427d157501SCoco Li /* If incoming segments make tracked segment length exceed
3437d157501SCoco Li  * legal IP datagram length, do not coalesce
3447d157501SCoco Li  */
3457d157501SCoco Li static void send_large(int fd, struct sockaddr_ll *daddr, int remainder)
3467d157501SCoco Li {
3477d157501SCoco Li 	static char pkts[NUM_LARGE_PKT][TOTAL_HDR_LEN + MSS];
3487d157501SCoco Li 	static char last[TOTAL_HDR_LEN + MSS];
3497d157501SCoco Li 	static char new_seg[TOTAL_HDR_LEN + MSS];
3507d157501SCoco Li 	int i;
3517d157501SCoco Li 
3527d157501SCoco Li 	for (i = 0; i < NUM_LARGE_PKT; i++)
3537d157501SCoco Li 		create_packet(pkts[i], i * MSS, 0, MSS, 0);
3547d157501SCoco Li 	create_packet(last, NUM_LARGE_PKT * MSS, 0, remainder, 0);
3557d157501SCoco Li 	create_packet(new_seg, (NUM_LARGE_PKT + 1) * MSS, 0, remainder, 0);
3567d157501SCoco Li 
3577d157501SCoco Li 	for (i = 0; i < NUM_LARGE_PKT; i++)
3587d157501SCoco Li 		write_packet(fd, pkts[i], total_hdr_len + MSS, daddr);
3597d157501SCoco Li 	write_packet(fd, last, total_hdr_len + remainder, daddr);
3607d157501SCoco Li 	write_packet(fd, new_seg, total_hdr_len + remainder, daddr);
3617d157501SCoco Li }
3627d157501SCoco Li 
3637d157501SCoco Li /* Pure acks and dup acks don't coalesce */
3647d157501SCoco Li static void send_ack(int fd, struct sockaddr_ll *daddr)
3657d157501SCoco Li {
3667d157501SCoco Li 	static char buf[MAX_HDR_LEN];
3677d157501SCoco Li 
3687d157501SCoco Li 	create_packet(buf, 0, 0, 0, 0);
3697d157501SCoco Li 	write_packet(fd, buf, total_hdr_len, daddr);
3707d157501SCoco Li 	write_packet(fd, buf, total_hdr_len, daddr);
3717d157501SCoco Li 	create_packet(buf, 0, 1, 0, 0);
3727d157501SCoco Li 	write_packet(fd, buf, total_hdr_len, daddr);
3737d157501SCoco Li }
3747d157501SCoco Li 
3757d157501SCoco Li static void recompute_packet(char *buf, char *no_ext, int extlen)
3767d157501SCoco Li {
3777d157501SCoco Li 	struct tcphdr *tcphdr = (struct tcphdr *)(buf + tcp_offset);
3787d157501SCoco Li 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
3797d157501SCoco Li 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
3807d157501SCoco Li 
3817d157501SCoco Li 	memmove(buf, no_ext, total_hdr_len);
3827d157501SCoco Li 	memmove(buf + total_hdr_len + extlen,
3837d157501SCoco Li 		no_ext + total_hdr_len, PAYLOAD_LEN);
3847d157501SCoco Li 
3857d157501SCoco Li 	tcphdr->doff = tcphdr->doff + (extlen / 4);
3867d157501SCoco Li 	tcphdr->check = 0;
3877d157501SCoco Li 	tcphdr->check = tcp_checksum(tcphdr, PAYLOAD_LEN + extlen);
3887d157501SCoco Li 	if (proto == PF_INET) {
3897d157501SCoco Li 		iph->tot_len = htons(ntohs(iph->tot_len) + extlen);
3907d157501SCoco Li 		iph->check = 0;
3917d157501SCoco Li 		iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
3927d157501SCoco Li 	} else {
3937d157501SCoco Li 		ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);
3947d157501SCoco Li 	}
3957d157501SCoco Li }
3967d157501SCoco Li 
3977d157501SCoco Li static void tcp_write_options(char *buf, int kind, int ts)
3987d157501SCoco Li {
3997d157501SCoco Li 	struct tcp_option_ts {
4007d157501SCoco Li 		uint8_t kind;
4017d157501SCoco Li 		uint8_t len;
4027d157501SCoco Li 		uint32_t tsval;
4037d157501SCoco Li 		uint32_t tsecr;
4047d157501SCoco Li 	} *opt_ts = (void *)buf;
4057d157501SCoco Li 	struct tcp_option_window {
4067d157501SCoco Li 		uint8_t kind;
4077d157501SCoco Li 		uint8_t len;
4087d157501SCoco Li 		uint8_t shift;
4097d157501SCoco Li 	} *opt_window = (void *)buf;
4107d157501SCoco Li 
4117d157501SCoco Li 	switch (kind) {
4127d157501SCoco Li 	case TCPOPT_NOP:
4137d157501SCoco Li 		buf[0] = TCPOPT_NOP;
4147d157501SCoco Li 		break;
4157d157501SCoco Li 	case TCPOPT_WINDOW:
4167d157501SCoco Li 		memset(opt_window, 0, sizeof(struct tcp_option_window));
4177d157501SCoco Li 		opt_window->kind = TCPOPT_WINDOW;
4187d157501SCoco Li 		opt_window->len = TCPOLEN_WINDOW;
4197d157501SCoco Li 		opt_window->shift = 0;
4207d157501SCoco Li 		break;
4217d157501SCoco Li 	case TCPOPT_TIMESTAMP:
4227d157501SCoco Li 		memset(opt_ts, 0, sizeof(struct tcp_option_ts));
4237d157501SCoco Li 		opt_ts->kind = TCPOPT_TIMESTAMP;
4247d157501SCoco Li 		opt_ts->len = TCPOLEN_TIMESTAMP;
4257d157501SCoco Li 		opt_ts->tsval = ts;
4267d157501SCoco Li 		opt_ts->tsecr = 0;
4277d157501SCoco Li 		break;
4287d157501SCoco Li 	default:
4297d157501SCoco Li 		error(1, 0, "unimplemented TCP option");
4307d157501SCoco Li 		break;
4317d157501SCoco Li 	}
4327d157501SCoco Li }
4337d157501SCoco Li 
4347d157501SCoco Li /* TCP with options is always a permutation of {TS, NOP, NOP}.
4357d157501SCoco Li  * Implement different orders to verify coalescing stops.
4367d157501SCoco Li  */
4377d157501SCoco Li static void add_standard_tcp_options(char *buf, char *no_ext, int ts, int order)
4387d157501SCoco Li {
4397d157501SCoco Li 	switch (order) {
4407d157501SCoco Li 	case 0:
4417d157501SCoco Li 		tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);
4427d157501SCoco Li 		tcp_write_options(buf + total_hdr_len + 1, TCPOPT_NOP, 0);
4437d157501SCoco Li 		tcp_write_options(buf + total_hdr_len + 2 /* two NOP opts */,
4447d157501SCoco Li 				  TCPOPT_TIMESTAMP, ts);
4457d157501SCoco Li 		break;
4467d157501SCoco Li 	case 1:
4477d157501SCoco Li 		tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);
4487d157501SCoco Li 		tcp_write_options(buf + total_hdr_len + 1,
4497d157501SCoco Li 				  TCPOPT_TIMESTAMP, ts);
4507d157501SCoco Li 		tcp_write_options(buf + total_hdr_len + 1 + TCPOLEN_TIMESTAMP,
4517d157501SCoco Li 				  TCPOPT_NOP, 0);
4527d157501SCoco Li 		break;
4537d157501SCoco Li 	case 2:
4547d157501SCoco Li 		tcp_write_options(buf + total_hdr_len, TCPOPT_TIMESTAMP, ts);
4557d157501SCoco Li 		tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 1,
4567d157501SCoco Li 				  TCPOPT_NOP, 0);
4577d157501SCoco Li 		tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 2,
4587d157501SCoco Li 				  TCPOPT_NOP, 0);
4597d157501SCoco Li 		break;
4607d157501SCoco Li 	default:
4617d157501SCoco Li 		error(1, 0, "unknown order");
4627d157501SCoco Li 		break;
4637d157501SCoco Li 	}
4647d157501SCoco Li 	recompute_packet(buf, no_ext, TCPOLEN_TSTAMP_APPA);
4657d157501SCoco Li }
4667d157501SCoco Li 
4677d157501SCoco Li /* Packets with invalid checksum don't coalesce. */
4687d157501SCoco Li static void send_changed_checksum(int fd, struct sockaddr_ll *daddr)
4697d157501SCoco Li {
4707d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
4717d157501SCoco Li 	struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);
4727d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
4737d157501SCoco Li 
4747d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
4757d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
4767d157501SCoco Li 
4777d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
4787d157501SCoco Li 	tcph->check = tcph->check - 1;
4797d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
4807d157501SCoco Li }
4817d157501SCoco Li 
4827d157501SCoco Li  /* Packets with non-consecutive sequence number don't coalesce.*/
4837d157501SCoco Li static void send_changed_seq(int fd, struct sockaddr_ll *daddr)
4847d157501SCoco Li {
4857d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
4867d157501SCoco Li 	struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);
4877d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
4887d157501SCoco Li 
4897d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
4907d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
4917d157501SCoco Li 
4927d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
4937d157501SCoco Li 	tcph->seq = ntohl(htonl(tcph->seq) + 1);
4947d157501SCoco Li 	tcph->check = 0;
4957d157501SCoco Li 	tcph->check = tcp_checksum(tcph, PAYLOAD_LEN);
4967d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
4977d157501SCoco Li }
4987d157501SCoco Li 
4997d157501SCoco Li  /* Packet with different timestamp option or different timestamps
5007d157501SCoco Li   * don't coalesce.
5017d157501SCoco Li   */
5027d157501SCoco Li static void send_changed_ts(int fd, struct sockaddr_ll *daddr)
5037d157501SCoco Li {
5047d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
5057d157501SCoco Li 	static char extpkt[sizeof(buf) + TCPOLEN_TSTAMP_APPA];
5067d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;
5077d157501SCoco Li 
5087d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
5097d157501SCoco Li 	add_standard_tcp_options(extpkt, buf, 0, 0);
5107d157501SCoco Li 	write_packet(fd, extpkt, pkt_size, daddr);
5117d157501SCoco Li 
5127d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
5137d157501SCoco Li 	add_standard_tcp_options(extpkt, buf, 0, 0);
5147d157501SCoco Li 	write_packet(fd, extpkt, pkt_size, daddr);
5157d157501SCoco Li 
5167d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
5177d157501SCoco Li 	add_standard_tcp_options(extpkt, buf, 100, 0);
5187d157501SCoco Li 	write_packet(fd, extpkt, pkt_size, daddr);
5197d157501SCoco Li 
5207d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
5217d157501SCoco Li 	add_standard_tcp_options(extpkt, buf, 100, 1);
5227d157501SCoco Li 	write_packet(fd, extpkt, pkt_size, daddr);
5237d157501SCoco Li 
5247d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 4, 0, PAYLOAD_LEN, 0);
5257d157501SCoco Li 	add_standard_tcp_options(extpkt, buf, 100, 2);
5267d157501SCoco Li 	write_packet(fd, extpkt, pkt_size, daddr);
5277d157501SCoco Li }
5287d157501SCoco Li 
5297d157501SCoco Li /* Packet with different tcp options don't coalesce. */
5307d157501SCoco Li static void send_diff_opt(int fd, struct sockaddr_ll *daddr)
5317d157501SCoco Li {
5327d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
5337d157501SCoco Li 	static char extpkt1[sizeof(buf) + TCPOLEN_TSTAMP_APPA];
5347d157501SCoco Li 	static char extpkt2[sizeof(buf) + TCPOLEN_MAXSEG];
5357d157501SCoco Li 	int extpkt1_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;
5367d157501SCoco Li 	int extpkt2_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_MAXSEG;
5377d157501SCoco Li 
5387d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
5397d157501SCoco Li 	add_standard_tcp_options(extpkt1, buf, 0, 0);
5407d157501SCoco Li 	write_packet(fd, extpkt1, extpkt1_size, daddr);
5417d157501SCoco Li 
5427d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
5437d157501SCoco Li 	add_standard_tcp_options(extpkt1, buf, 0, 0);
5447d157501SCoco Li 	write_packet(fd, extpkt1, extpkt1_size, daddr);
5457d157501SCoco Li 
5467d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
5477d157501SCoco Li 	tcp_write_options(extpkt2 + MAX_HDR_LEN, TCPOPT_NOP, 0);
5487d157501SCoco Li 	tcp_write_options(extpkt2 + MAX_HDR_LEN + 1, TCPOPT_WINDOW, 0);
5497d157501SCoco Li 	recompute_packet(extpkt2, buf, TCPOLEN_WINDOW + 1);
5507d157501SCoco Li 	write_packet(fd, extpkt2, extpkt2_size, daddr);
5517d157501SCoco Li }
5527d157501SCoco Li 
5537d157501SCoco Li static void add_ipv4_ts_option(void *buf, void *optpkt)
5547d157501SCoco Li {
5557d157501SCoco Li 	struct ip_timestamp *ts = (struct ip_timestamp *)(optpkt + tcp_offset);
5567d157501SCoco Li 	int optlen = sizeof(struct ip_timestamp);
5577d157501SCoco Li 	struct iphdr *iph;
5587d157501SCoco Li 
5597d157501SCoco Li 	if (optlen % 4)
5607d157501SCoco Li 		error(1, 0, "ipv4 timestamp length is not a multiple of 4B");
5617d157501SCoco Li 
5627d157501SCoco Li 	ts->ipt_code = IPOPT_TS;
5637d157501SCoco Li 	ts->ipt_len = optlen;
5647d157501SCoco Li 	ts->ipt_ptr = 5;
5657d157501SCoco Li 	ts->ipt_flg = IPOPT_TS_TSONLY;
5667d157501SCoco Li 
5677d157501SCoco Li 	memcpy(optpkt, buf, tcp_offset);
5687d157501SCoco Li 	memcpy(optpkt + tcp_offset + optlen, buf + tcp_offset,
5697d157501SCoco Li 	       sizeof(struct tcphdr) + PAYLOAD_LEN);
5707d157501SCoco Li 
5717d157501SCoco Li 	iph = (struct iphdr *)(optpkt + ETH_HLEN);
5727d157501SCoco Li 	iph->ihl = 5 + (optlen / 4);
5737d157501SCoco Li 	iph->tot_len = htons(ntohs(iph->tot_len) + optlen);
5747d157501SCoco Li 	iph->check = 0;
5757d157501SCoco Li 	iph->check = checksum_fold(iph, sizeof(struct iphdr) + optlen, 0);
5767d157501SCoco Li }
5777d157501SCoco Li 
5787d157501SCoco Li /* IPv4 options shouldn't coalesce */
5797d157501SCoco Li static void send_ip_options(int fd, struct sockaddr_ll *daddr)
5807d157501SCoco Li {
5817d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
5827d157501SCoco Li 	static char optpkt[sizeof(buf) + sizeof(struct ip_timestamp)];
5837d157501SCoco Li 	int optlen = sizeof(struct ip_timestamp);
5847d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN + optlen;
5857d157501SCoco Li 
5867d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
5877d157501SCoco Li 	write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
5887d157501SCoco Li 
5897d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);
5907d157501SCoco Li 	add_ipv4_ts_option(buf, optpkt);
5917d157501SCoco Li 	write_packet(fd, optpkt, pkt_size, daddr);
5927d157501SCoco Li 
5937d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
5947d157501SCoco Li 	write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);
5957d157501SCoco Li }
5967d157501SCoco Li 
5977d157501SCoco Li /*  IPv4 fragments shouldn't coalesce */
5987d157501SCoco Li static void send_fragment4(int fd, struct sockaddr_ll *daddr)
5997d157501SCoco Li {
6007d157501SCoco Li 	static char buf[IP_MAXPACKET];
6017d157501SCoco Li 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
6027d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
6037d157501SCoco Li 
6047d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
6057d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6067d157501SCoco Li 
6077d157501SCoco Li 	/* Once fragmented, packet would retain the total_len.
6087d157501SCoco Li 	 * Tcp header is prepared as if rest of data is in follow-up frags,
6097d157501SCoco Li 	 * but follow up frags aren't actually sent.
6107d157501SCoco Li 	 */
6117d157501SCoco Li 	memset(buf + total_hdr_len, 'a', PAYLOAD_LEN * 2);
6127d157501SCoco Li 	fill_transportlayer(buf + tcp_offset, PAYLOAD_LEN, 0, PAYLOAD_LEN * 2, 0);
6137d157501SCoco Li 	fill_networklayer(buf + ETH_HLEN, PAYLOAD_LEN);
6147d157501SCoco Li 	fill_datalinklayer(buf);
6157d157501SCoco Li 
6167d157501SCoco Li 	iph->frag_off = htons(0x6000); // DF = 1, MF = 1
6177d157501SCoco Li 	iph->check = 0;
6187d157501SCoco Li 	iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
6197d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6207d157501SCoco Li }
6217d157501SCoco Li 
6227d157501SCoco Li /* IPv4 packets with different ttl don't coalesce.*/
6237d157501SCoco Li static void send_changed_ttl(int fd, struct sockaddr_ll *daddr)
6247d157501SCoco Li {
6257d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
6267d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
6277d157501SCoco Li 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
6287d157501SCoco Li 
6297d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
6307d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6317d157501SCoco Li 
6327d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
6337d157501SCoco Li 	iph->ttl = 7;
6347d157501SCoco Li 	iph->check = 0;
6357d157501SCoco Li 	iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
6367d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6377d157501SCoco Li }
6387d157501SCoco Li 
6397d157501SCoco Li /* Packets with different tos don't coalesce.*/
6407d157501SCoco Li static void send_changed_tos(int fd, struct sockaddr_ll *daddr)
6417d157501SCoco Li {
6427d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
6437d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
6447d157501SCoco Li 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
6457d157501SCoco Li 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
6467d157501SCoco Li 
6477d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
6487d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6497d157501SCoco Li 
6507d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
6517d157501SCoco Li 	if (proto == PF_INET) {
6527d157501SCoco Li 		iph->tos = 1;
6537d157501SCoco Li 		iph->check = 0;
6547d157501SCoco Li 		iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
6557d157501SCoco Li 	} else if (proto == PF_INET6) {
6567d157501SCoco Li 		ip6h->priority = 0xf;
6577d157501SCoco Li 	}
6587d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6597d157501SCoco Li }
6607d157501SCoco Li 
6617d157501SCoco Li /* Packets with different ECN don't coalesce.*/
6627d157501SCoco Li static void send_changed_ECN(int fd, struct sockaddr_ll *daddr)
6637d157501SCoco Li {
6647d157501SCoco Li 	int pkt_size = total_hdr_len + PAYLOAD_LEN;
6657d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
6667d157501SCoco Li 	struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);
6677d157501SCoco Li 
6687d157501SCoco Li 	create_packet(buf, 0, 0, PAYLOAD_LEN, 0);
6697d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6707d157501SCoco Li 
6717d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);
6727d157501SCoco Li 	if (proto == PF_INET) {
6737d157501SCoco Li 		buf[ETH_HLEN + 1] ^= 0x2; // ECN set to 10
6747d157501SCoco Li 		iph->check = 0;
6757d157501SCoco Li 		iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);
6767d157501SCoco Li 	} else {
6777d157501SCoco Li 		buf[ETH_HLEN + 1] ^= 0x20; // ECN set to 10
6787d157501SCoco Li 	}
6797d157501SCoco Li 	write_packet(fd, buf, pkt_size, daddr);
6807d157501SCoco Li }
6817d157501SCoco Li 
6827d157501SCoco Li /* IPv6 fragments and packets with extensions don't coalesce.*/
6837d157501SCoco Li static void send_fragment6(int fd, struct sockaddr_ll *daddr)
6847d157501SCoco Li {
6857d157501SCoco Li 	static char buf[MAX_HDR_LEN + PAYLOAD_LEN];
6867d157501SCoco Li 	static char extpkt[MAX_HDR_LEN + PAYLOAD_LEN +
6877d157501SCoco Li 			   sizeof(struct ip6_frag)];
6887d157501SCoco Li 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);
6897d157501SCoco Li 	struct ip6_frag *frag = (void *)(extpkt + tcp_offset);
6907d157501SCoco Li 	int extlen = sizeof(struct ip6_frag);
6917d157501SCoco Li 	int bufpkt_len = total_hdr_len + PAYLOAD_LEN;
6927d157501SCoco Li 	int extpkt_len = bufpkt_len + extlen;
6937d157501SCoco Li 	int i;
6947d157501SCoco Li 
6957d157501SCoco Li 	for (i = 0; i < 2; i++) {
6967d157501SCoco Li 		create_packet(buf, PAYLOAD_LEN * i, 0, PAYLOAD_LEN, 0);
6977d157501SCoco Li 		write_packet(fd, buf, bufpkt_len, daddr);
6987d157501SCoco Li 	}
6997d157501SCoco Li 
7007d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);
7017d157501SCoco Li 	memset(extpkt, 0, extpkt_len);
7027d157501SCoco Li 
7037d157501SCoco Li 	ip6h->nexthdr = IPPROTO_FRAGMENT;
7047d157501SCoco Li 	ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);
7057d157501SCoco Li 	frag->ip6f_nxt = IPPROTO_TCP;
7067d157501SCoco Li 
7077d157501SCoco Li 	memcpy(extpkt, buf, tcp_offset);
7087d157501SCoco Li 	memcpy(extpkt + tcp_offset + extlen, buf + tcp_offset,
7097d157501SCoco Li 	       sizeof(struct tcphdr) + PAYLOAD_LEN);
7107d157501SCoco Li 	write_packet(fd, extpkt, extpkt_len, daddr);
7117d157501SCoco Li 
7127d157501SCoco Li 	create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);
7137d157501SCoco Li 	write_packet(fd, buf, bufpkt_len, daddr);
7147d157501SCoco Li }
7157d157501SCoco Li 
7167d157501SCoco Li static void bind_packetsocket(int fd)
7177d157501SCoco Li {
7187d157501SCoco Li 	struct sockaddr_ll daddr = {};
7197d157501SCoco Li 
7207d157501SCoco Li 	daddr.sll_family = AF_PACKET;
7217d157501SCoco Li 	daddr.sll_protocol = ethhdr_proto;
7227d157501SCoco Li 	daddr.sll_ifindex = if_nametoindex(ifname);
7237d157501SCoco Li 	if (daddr.sll_ifindex == 0)
7247d157501SCoco Li 		error(1, errno, "if_nametoindex");
7257d157501SCoco Li 
7267d157501SCoco Li 	if (bind(fd, (void *)&daddr, sizeof(daddr)) < 0)
7277d157501SCoco Li 		error(1, errno, "could not bind socket");
7287d157501SCoco Li }
7297d157501SCoco Li 
7307d157501SCoco Li static void set_timeout(int fd)
7317d157501SCoco Li {
7327d157501SCoco Li 	struct timeval timeout;
7337d157501SCoco Li 
734*87f7282eSWillem de Bruijn 	timeout.tv_sec = 3;
7357d157501SCoco Li 	timeout.tv_usec = 0;
7367d157501SCoco Li 	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
7377d157501SCoco Li 		       sizeof(timeout)) < 0)
7387d157501SCoco Li 		error(1, errno, "cannot set timeout, setsockopt failed");
7397d157501SCoco Li }
7407d157501SCoco Li 
7417d157501SCoco Li static void check_recv_pkts(int fd, int *correct_payload,
7427d157501SCoco Li 			    int correct_num_pkts)
7437d157501SCoco Li {
7447d157501SCoco Li 	static char buffer[IP_MAXPACKET + ETH_HLEN + 1];
7457d157501SCoco Li 	struct iphdr *iph = (struct iphdr *)(buffer + ETH_HLEN);
7467d157501SCoco Li 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + ETH_HLEN);
7477d157501SCoco Li 	struct tcphdr *tcph;
7487d157501SCoco Li 	bool bad_packet = false;
7497d157501SCoco Li 	int tcp_ext_len = 0;
7507d157501SCoco Li 	int ip_ext_len = 0;
7517d157501SCoco Li 	int pkt_size = -1;
7527d157501SCoco Li 	int data_len = 0;
7537d157501SCoco Li 	int num_pkt = 0;
7547d157501SCoco Li 	int i;
7557d157501SCoco Li 
7567d157501SCoco Li 	vlog("Expected {");
7577d157501SCoco Li 	for (i = 0; i < correct_num_pkts; i++)
7587d157501SCoco Li 		vlog("%d ", correct_payload[i]);
7597d157501SCoco Li 	vlog("}, Total %d packets\nReceived {", correct_num_pkts);
7607d157501SCoco Li 
7617d157501SCoco Li 	while (1) {
7627d157501SCoco Li 		pkt_size = recv(fd, buffer, IP_MAXPACKET + ETH_HLEN + 1, 0);
7637d157501SCoco Li 		if (pkt_size < 0)
7647d157501SCoco Li 			error(1, errno, "could not receive");
7657d157501SCoco Li 
7667d157501SCoco Li 		if (iph->version == 4)
7677d157501SCoco Li 			ip_ext_len = (iph->ihl - 5) * 4;
7687d157501SCoco Li 		else if (ip6h->version == 6 && ip6h->nexthdr != IPPROTO_TCP)
7697d157501SCoco Li 			ip_ext_len = sizeof(struct ip6_frag);
7707d157501SCoco Li 
7717d157501SCoco Li 		tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len);
7727d157501SCoco Li 
7737d157501SCoco Li 		if (tcph->fin)
7747d157501SCoco Li 			break;
7757d157501SCoco Li 
7767d157501SCoco Li 		tcp_ext_len = (tcph->doff - 5) * 4;
7777d157501SCoco Li 		data_len = pkt_size - total_hdr_len - tcp_ext_len - ip_ext_len;
7787d157501SCoco Li 		/* Min ethernet frame payload is 46(ETH_ZLEN - ETH_HLEN) by RFC 802.3.
7797d157501SCoco Li 		 * Ipv4/tcp packets without at least 6 bytes of data will be padded.
7807d157501SCoco Li 		 * Packet sockets are protocol agnostic, and will not trim the padding.
7817d157501SCoco Li 		 */
7827d157501SCoco Li 		if (pkt_size == ETH_ZLEN && iph->version == 4) {
7837d157501SCoco Li 			data_len = ntohs(iph->tot_len)
7847d157501SCoco Li 				- sizeof(struct tcphdr) - sizeof(struct iphdr);
7857d157501SCoco Li 		}
7867d157501SCoco Li 		vlog("%d ", data_len);
7877d157501SCoco Li 		if (data_len != correct_payload[num_pkt]) {
7887d157501SCoco Li 			vlog("[!=%d]", correct_payload[num_pkt]);
7897d157501SCoco Li 			bad_packet = true;
7907d157501SCoco Li 		}
7917d157501SCoco Li 		num_pkt++;
7927d157501SCoco Li 	}
7937d157501SCoco Li 	vlog("}, Total %d packets.\n", num_pkt);
7947d157501SCoco Li 	if (num_pkt != correct_num_pkts)
7957d157501SCoco Li 		error(1, 0, "incorrect number of packets");
7967d157501SCoco Li 	if (bad_packet)
7977d157501SCoco Li 		error(1, 0, "incorrect packet geometry");
7987d157501SCoco Li 
7997d157501SCoco Li 	printf("Test succeeded\n\n");
8007d157501SCoco Li }
8017d157501SCoco Li 
8027d157501SCoco Li static void gro_sender(void)
8037d157501SCoco Li {
8047d157501SCoco Li 	static char fin_pkt[MAX_HDR_LEN];
8057d157501SCoco Li 	struct sockaddr_ll daddr = {};
8067d157501SCoco Li 	int txfd = -1;
8077d157501SCoco Li 
8087d157501SCoco Li 	txfd = socket(PF_PACKET, SOCK_RAW, IPPROTO_RAW);
8097d157501SCoco Li 	if (txfd < 0)
8107d157501SCoco Li 		error(1, errno, "socket creation");
8117d157501SCoco Li 
8127d157501SCoco Li 	memset(&daddr, 0, sizeof(daddr));
8137d157501SCoco Li 	daddr.sll_ifindex = if_nametoindex(ifname);
8147d157501SCoco Li 	if (daddr.sll_ifindex == 0)
8157d157501SCoco Li 		error(1, errno, "if_nametoindex");
8167d157501SCoco Li 	daddr.sll_family = AF_PACKET;
8177d157501SCoco Li 	memcpy(daddr.sll_addr, dst_mac, ETH_ALEN);
8187d157501SCoco Li 	daddr.sll_halen = ETH_ALEN;
8197d157501SCoco Li 	create_packet(fin_pkt, PAYLOAD_LEN * 2, 0, 0, 1);
8207d157501SCoco Li 
8217d157501SCoco Li 	if (strcmp(testname, "data") == 0) {
8227d157501SCoco Li 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN);
8237d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8247d157501SCoco Li 
8257d157501SCoco Li 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN / 2);
8267d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8277d157501SCoco Li 
8287d157501SCoco Li 		send_data_pkts(txfd, &daddr, PAYLOAD_LEN / 2, PAYLOAD_LEN);
8297d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8307d157501SCoco Li 	} else if (strcmp(testname, "ack") == 0) {
8317d157501SCoco Li 		send_ack(txfd, &daddr);
8327d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8337d157501SCoco Li 	} else if (strcmp(testname, "flags") == 0) {
8347d157501SCoco Li 		send_flags(txfd, &daddr, 1, 0, 0, 0);
8357d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8367d157501SCoco Li 
8377d157501SCoco Li 		send_flags(txfd, &daddr, 0, 1, 0, 0);
8387d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8397d157501SCoco Li 
8407d157501SCoco Li 		send_flags(txfd, &daddr, 0, 0, 1, 0);
8417d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8427d157501SCoco Li 
8437d157501SCoco Li 		send_flags(txfd, &daddr, 0, 0, 0, 1);
8447d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8457d157501SCoco Li 	} else if (strcmp(testname, "tcp") == 0) {
8467d157501SCoco Li 		send_changed_checksum(txfd, &daddr);
8477d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8487d157501SCoco Li 
8497d157501SCoco Li 		send_changed_seq(txfd, &daddr);
8507d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8517d157501SCoco Li 
8527d157501SCoco Li 		send_changed_ts(txfd, &daddr);
8537d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8547d157501SCoco Li 
8557d157501SCoco Li 		send_diff_opt(txfd, &daddr);
8567d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8577d157501SCoco Li 	} else if (strcmp(testname, "ip") == 0) {
8587d157501SCoco Li 		send_changed_ECN(txfd, &daddr);
8597d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8607d157501SCoco Li 
8617d157501SCoco Li 		send_changed_tos(txfd, &daddr);
8627d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8637d157501SCoco Li 		if (proto == PF_INET) {
8647d157501SCoco Li 			/* Modified packets may be received out of order.
8657d157501SCoco Li 			 * Sleep function added to enforce test boundaries
8667d157501SCoco Li 			 * so that fin pkts are not received prior to other pkts.
8677d157501SCoco Li 			 */
8687d157501SCoco Li 			sleep(1);
8697d157501SCoco Li 			send_changed_ttl(txfd, &daddr);
8707d157501SCoco Li 			write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8717d157501SCoco Li 
8727d157501SCoco Li 			sleep(1);
8737d157501SCoco Li 			send_ip_options(txfd, &daddr);
8747d157501SCoco Li 			sleep(1);
8757d157501SCoco Li 			write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8767d157501SCoco Li 
8777d157501SCoco Li 			sleep(1);
8787d157501SCoco Li 			send_fragment4(txfd, &daddr);
8797d157501SCoco Li 			sleep(1);
8807d157501SCoco Li 			write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8817d157501SCoco Li 		} else if (proto == PF_INET6) {
8827d157501SCoco Li 			send_fragment6(txfd, &daddr);
8837d157501SCoco Li 			write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8847d157501SCoco Li 		}
8857d157501SCoco Li 	} else if (strcmp(testname, "large") == 0) {
8867d157501SCoco Li 		/* 20 is the difference between min iphdr size
8877d157501SCoco Li 		 * and min ipv6hdr size. Like MAX_HDR_SIZE,
8887d157501SCoco Li 		 * MAX_PAYLOAD is defined with the larger header of the two.
8897d157501SCoco Li 		 */
8907d157501SCoco Li 		int offset = proto == PF_INET ? 20 : 0;
8917d157501SCoco Li 		int remainder = (MAX_PAYLOAD + offset) % MSS;
8927d157501SCoco Li 
8937d157501SCoco Li 		send_large(txfd, &daddr, remainder);
8947d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8957d157501SCoco Li 
8967d157501SCoco Li 		send_large(txfd, &daddr, remainder + 1);
8977d157501SCoco Li 		write_packet(txfd, fin_pkt, total_hdr_len, &daddr);
8987d157501SCoco Li 	} else {
8997d157501SCoco Li 		error(1, 0, "Unknown testcase");
9007d157501SCoco Li 	}
9017d157501SCoco Li 
9027d157501SCoco Li 	if (close(txfd))
9037d157501SCoco Li 		error(1, errno, "socket close");
9047d157501SCoco Li }
9057d157501SCoco Li 
9067d157501SCoco Li static void gro_receiver(void)
9077d157501SCoco Li {
9087d157501SCoco Li 	static int correct_payload[NUM_PACKETS];
9097d157501SCoco Li 	int rxfd = -1;
9107d157501SCoco Li 
9117d157501SCoco Li 	rxfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_NONE));
9127d157501SCoco Li 	if (rxfd < 0)
9137d157501SCoco Li 		error(1, 0, "socket creation");
9147d157501SCoco Li 	setup_sock_filter(rxfd);
9157d157501SCoco Li 	set_timeout(rxfd);
9167d157501SCoco Li 	bind_packetsocket(rxfd);
9177d157501SCoco Li 
9187d157501SCoco Li 	memset(correct_payload, 0, sizeof(correct_payload));
9197d157501SCoco Li 
9207d157501SCoco Li 	if (strcmp(testname, "data") == 0) {
9217d157501SCoco Li 		printf("pure data packet of same size: ");
9227d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN * 2;
9237d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 1);
9247d157501SCoco Li 
9257d157501SCoco Li 		printf("large data packets followed by a smaller one: ");
9267d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN * 1.5;
9277d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 1);
9287d157501SCoco Li 
9297d157501SCoco Li 		printf("small data packets followed by a larger one: ");
9307d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN / 2;
9317d157501SCoco Li 		correct_payload[1] = PAYLOAD_LEN;
9327d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9337d157501SCoco Li 	} else if (strcmp(testname, "ack") == 0) {
9347d157501SCoco Li 		printf("duplicate ack and pure ack: ");
9357d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 3);
9367d157501SCoco Li 	} else if (strcmp(testname, "flags") == 0) {
9377d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN * 3;
9387d157501SCoco Li 		correct_payload[1] = PAYLOAD_LEN * 2;
9397d157501SCoco Li 
9407d157501SCoco Li 		printf("psh flag ends coalescing: ");
9417d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9427d157501SCoco Li 
9437d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN * 2;
9447d157501SCoco Li 		correct_payload[1] = 0;
9457d157501SCoco Li 		correct_payload[2] = PAYLOAD_LEN * 2;
9467d157501SCoco Li 		printf("syn flag ends coalescing: ");
9477d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 3);
9487d157501SCoco Li 
9497d157501SCoco Li 		printf("rst flag ends coalescing: ");
9507d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 3);
9517d157501SCoco Li 
9527d157501SCoco Li 		printf("urg flag ends coalescing: ");
9537d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 3);
9547d157501SCoco Li 	} else if (strcmp(testname, "tcp") == 0) {
9557d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN;
9567d157501SCoco Li 		correct_payload[1] = PAYLOAD_LEN;
9577d157501SCoco Li 		correct_payload[2] = PAYLOAD_LEN;
9587d157501SCoco Li 		correct_payload[3] = PAYLOAD_LEN;
9597d157501SCoco Li 
9607d157501SCoco Li 		printf("changed checksum does not coalesce: ");
9617d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9627d157501SCoco Li 
9637d157501SCoco Li 		printf("Wrong Seq number doesn't coalesce: ");
9647d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9657d157501SCoco Li 
9667d157501SCoco Li 		printf("Different timestamp doesn't coalesce: ");
9677d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN * 2;
9687d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 4);
9697d157501SCoco Li 
9707d157501SCoco Li 		printf("Different options doesn't coalesce: ");
9717d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN * 2;
9727d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9737d157501SCoco Li 	} else if (strcmp(testname, "ip") == 0) {
9747d157501SCoco Li 		correct_payload[0] = PAYLOAD_LEN;
9757d157501SCoco Li 		correct_payload[1] = PAYLOAD_LEN;
9767d157501SCoco Li 
9777d157501SCoco Li 		printf("different ECN doesn't coalesce: ");
9787d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9797d157501SCoco Li 
9807d157501SCoco Li 		printf("different tos doesn't coalesce: ");
9817d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
9827d157501SCoco Li 
9837d157501SCoco Li 		if (proto == PF_INET) {
9847d157501SCoco Li 			printf("different ttl doesn't coalesce: ");
9857d157501SCoco Li 			check_recv_pkts(rxfd, correct_payload, 2);
9867d157501SCoco Li 
9877d157501SCoco Li 			printf("ip options doesn't coalesce: ");
9887d157501SCoco Li 			correct_payload[2] = PAYLOAD_LEN;
9897d157501SCoco Li 			check_recv_pkts(rxfd, correct_payload, 3);
9907d157501SCoco Li 
9917d157501SCoco Li 			printf("fragmented ip4 doesn't coalesce: ");
9927d157501SCoco Li 			check_recv_pkts(rxfd, correct_payload, 2);
9937d157501SCoco Li 		} else if (proto == PF_INET6) {
9947d157501SCoco Li 			/* GRO doesn't check for ipv6 hop limit when flushing.
9957d157501SCoco Li 			 * Hence no corresponding test to the ipv4 case.
9967d157501SCoco Li 			 */
9977d157501SCoco Li 			printf("fragmented ip6 doesn't coalesce: ");
9987d157501SCoco Li 			correct_payload[0] = PAYLOAD_LEN * 2;
9997d157501SCoco Li 			check_recv_pkts(rxfd, correct_payload, 2);
10007d157501SCoco Li 		}
10017d157501SCoco Li 	} else if (strcmp(testname, "large") == 0) {
10027d157501SCoco Li 		int offset = proto == PF_INET ? 20 : 0;
10037d157501SCoco Li 		int remainder = (MAX_PAYLOAD + offset) % MSS;
10047d157501SCoco Li 
10057d157501SCoco Li 		correct_payload[0] = (MAX_PAYLOAD + offset);
10067d157501SCoco Li 		correct_payload[1] = remainder;
10077d157501SCoco Li 		printf("Shouldn't coalesce if exceed IP max pkt size: ");
10087d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 2);
10097d157501SCoco Li 
10107d157501SCoco Li 		/* last segment sent individually, doesn't start new segment */
10117d157501SCoco Li 		correct_payload[0] = correct_payload[0] - remainder;
10127d157501SCoco Li 		correct_payload[1] = remainder + 1;
10137d157501SCoco Li 		correct_payload[2] = remainder + 1;
10147d157501SCoco Li 		check_recv_pkts(rxfd, correct_payload, 3);
10157d157501SCoco Li 	} else {
10167d157501SCoco Li 		error(1, 0, "Test case error, should never trigger");
10177d157501SCoco Li 	}
10187d157501SCoco Li 
10197d157501SCoco Li 	if (close(rxfd))
10207d157501SCoco Li 		error(1, 0, "socket close");
10217d157501SCoco Li }
10227d157501SCoco Li 
10237d157501SCoco Li static void parse_args(int argc, char **argv)
10247d157501SCoco Li {
10257d157501SCoco Li 	static const struct option opts[] = {
1026*87f7282eSWillem de Bruijn 		{ "daddr", required_argument, NULL, 'd' },
10277d157501SCoco Li 		{ "dmac", required_argument, NULL, 'D' },
10287d157501SCoco Li 		{ "iface", required_argument, NULL, 'i' },
10297d157501SCoco Li 		{ "ipv4", no_argument, NULL, '4' },
10307d157501SCoco Li 		{ "ipv6", no_argument, NULL, '6' },
10317d157501SCoco Li 		{ "rx", no_argument, NULL, 'r' },
1032*87f7282eSWillem de Bruijn 		{ "saddr", required_argument, NULL, 's' },
10337d157501SCoco Li 		{ "smac", required_argument, NULL, 'S' },
10347d157501SCoco Li 		{ "test", required_argument, NULL, 't' },
10357d157501SCoco Li 		{ "verbose", no_argument, NULL, 'v' },
10367d157501SCoco Li 		{ 0, 0, 0, 0 }
10377d157501SCoco Li 	};
10387d157501SCoco Li 	int c;
10397d157501SCoco Li 
1040*87f7282eSWillem de Bruijn 	while ((c = getopt_long(argc, argv, "46d:D:i:rs:S:t:v", opts, NULL)) != -1) {
10417d157501SCoco Li 		switch (c) {
10427d157501SCoco Li 		case '4':
10437d157501SCoco Li 			proto = PF_INET;
10447d157501SCoco Li 			ethhdr_proto = htons(ETH_P_IP);
10457d157501SCoco Li 			break;
10467d157501SCoco Li 		case '6':
10477d157501SCoco Li 			proto = PF_INET6;
10487d157501SCoco Li 			ethhdr_proto = htons(ETH_P_IPV6);
10497d157501SCoco Li 			break;
1050*87f7282eSWillem de Bruijn 		case 'd':
1051*87f7282eSWillem de Bruijn 			addr4_dst = addr6_dst = optarg;
1052*87f7282eSWillem de Bruijn 			break;
10537d157501SCoco Li 		case 'D':
10547d157501SCoco Li 			dmac = optarg;
10557d157501SCoco Li 			break;
10567d157501SCoco Li 		case 'i':
10577d157501SCoco Li 			ifname = optarg;
10587d157501SCoco Li 			break;
10597d157501SCoco Li 		case 'r':
10607d157501SCoco Li 			tx_socket = false;
10617d157501SCoco Li 			break;
1062*87f7282eSWillem de Bruijn 		case 's':
1063*87f7282eSWillem de Bruijn 			addr4_src = addr6_src = optarg;
1064*87f7282eSWillem de Bruijn 			break;
10657d157501SCoco Li 		case 'S':
10667d157501SCoco Li 			smac = optarg;
10677d157501SCoco Li 			break;
10687d157501SCoco Li 		case 't':
10697d157501SCoco Li 			testname = optarg;
10707d157501SCoco Li 			break;
10717d157501SCoco Li 		case 'v':
10727d157501SCoco Li 			verbose = true;
10737d157501SCoco Li 			break;
10747d157501SCoco Li 		default:
10757d157501SCoco Li 			error(1, 0, "%s invalid option %c\n", __func__, c);
10767d157501SCoco Li 			break;
10777d157501SCoco Li 		}
10787d157501SCoco Li 	}
10797d157501SCoco Li }
10807d157501SCoco Li 
10817d157501SCoco Li int main(int argc, char **argv)
10827d157501SCoco Li {
10837d157501SCoco Li 	parse_args(argc, argv);
10847d157501SCoco Li 
10857d157501SCoco Li 	if (proto == PF_INET) {
10867d157501SCoco Li 		tcp_offset = ETH_HLEN + sizeof(struct iphdr);
10877d157501SCoco Li 		total_hdr_len = tcp_offset + sizeof(struct tcphdr);
10887d157501SCoco Li 	} else if (proto == PF_INET6) {
10897d157501SCoco Li 		tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr);
10907d157501SCoco Li 		total_hdr_len = MAX_HDR_LEN;
10917d157501SCoco Li 	} else {
10927d157501SCoco Li 		error(1, 0, "Protocol family is not ipv4 or ipv6");
10937d157501SCoco Li 	}
10947d157501SCoco Li 
10957d157501SCoco Li 	read_MAC(src_mac, smac);
10967d157501SCoco Li 	read_MAC(dst_mac, dmac);
10977d157501SCoco Li 
10987d157501SCoco Li 	if (tx_socket)
10997d157501SCoco Li 		gro_sender();
11007d157501SCoco Li 	else
11017d157501SCoco Li 		gro_receiver();
1102*87f7282eSWillem de Bruijn 
1103*87f7282eSWillem de Bruijn 	fprintf(stderr, "Gro::%s test passed.\n", testname);
11047d157501SCoco Li 	return 0;
11057d157501SCoco Li }
1106