File Index
Files and directories
Sign in
FILE

gro.c

File preview with syntax highlighting.

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