Path: blob/master/tools/testing/selftests/drivers/net/gro.c
38237 views
// SPDX-License-Identifier: GPL-2.01/*2* This testsuite provides conformance testing for GRO coalescing.3*4* Test cases:5* 1.data6* Data packets of the same size and same header setup with correct7* sequence numbers coalesce. The one exception being the last data8* packet coalesced: it can be smaller than the rest and coalesced9* as long as it is in the same flow.10* 2.ack11* Pure ACK does not coalesce.12* 3.flags13* Specific test cases: no packets with PSH, SYN, URG, RST set will14* be coalesced.15* 4.tcp16* Packets with incorrect checksum, non-consecutive seqno and17* different TCP header options shouldn't coalesce. Nit: given that18* some extension headers have paddings, such as timestamp, headers19* that are padding differently would not be coalesced.20* 5.ip:21* Packets with different (ECN, TTL, TOS) header, ip options or22* ip fragments (ipv6) shouldn't coalesce.23* 6.large:24* Packets larger than GRO_MAX_SIZE packets shouldn't coalesce.25*26* MSS is defined as 4096 - header because if it is too small27* (i.e. 1500 MTU - header), it will result in many packets,28* increasing the "large" test case's flakiness. This is because29* due to time sensitivity in the coalescing window, the receiver30* may not coalesce all of the packets.31*32* Note the timing issue applies to all of the test cases, so some33* flakiness is to be expected.34*35*/3637#define _GNU_SOURCE3839#include <arpa/inet.h>40#include <errno.h>41#include <error.h>42#include <getopt.h>43#include <linux/filter.h>44#include <linux/if_packet.h>45#include <linux/ipv6.h>46#include <net/ethernet.h>47#include <net/if.h>48#include <netinet/in.h>49#include <netinet/ip.h>50#include <netinet/ip6.h>51#include <netinet/tcp.h>52#include <stdbool.h>53#include <stddef.h>54#include <stdio.h>55#include <stdarg.h>56#include <string.h>57#include <unistd.h>5859#include "kselftest.h"60#include "../../net/lib/ksft.h"6162#define DPORT 800063#define SPORT 150064#define PAYLOAD_LEN 10065#define NUM_PACKETS 466#define START_SEQ 10067#define START_ACK 10068#define ETH_P_NONE 069#define TOTAL_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr))70#define MSS (4096 - sizeof(struct tcphdr) - sizeof(struct ipv6hdr))71#define MAX_PAYLOAD (IP_MAXPACKET - sizeof(struct tcphdr) - sizeof(struct ipv6hdr))72#define NUM_LARGE_PKT (MAX_PAYLOAD / MSS)73#define MAX_HDR_LEN (ETH_HLEN + sizeof(struct ipv6hdr) + sizeof(struct tcphdr))74#define MIN_EXTHDR_SIZE 875#define EXT_PAYLOAD_1 "\x00\x00\x00\x00\x00\x00"76#define EXT_PAYLOAD_2 "\x11\x11\x11\x11\x11\x11"7778#define ipv6_optlen(p) (((p)->hdrlen+1) << 3) /* calculate IPv6 extension header len */79#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))8081static const char *addr6_src = "fdaa::2";82static const char *addr6_dst = "fdaa::1";83static const char *addr4_src = "192.168.1.200";84static const char *addr4_dst = "192.168.1.100";85static int proto = -1;86static uint8_t src_mac[ETH_ALEN], dst_mac[ETH_ALEN];87static char *testname = "data";88static char *ifname = "eth0";89static char *smac = "aa:00:00:00:00:02";90static char *dmac = "aa:00:00:00:00:01";91static bool verbose;92static bool tx_socket = true;93static int tcp_offset = -1;94static int total_hdr_len = -1;95static int ethhdr_proto = -1;96static bool ipip;97static const int num_flush_id_cases = 6;9899static void vlog(const char *fmt, ...)100{101va_list args;102103if (verbose) {104va_start(args, fmt);105vfprintf(stderr, fmt, args);106va_end(args);107}108}109110static void setup_sock_filter(int fd)111{112const int dport_off = tcp_offset + offsetof(struct tcphdr, dest);113const int ethproto_off = offsetof(struct ethhdr, h_proto);114int optlen = 0;115int ipproto_off, opt_ipproto_off;116int next_off;117118if (ipip)119next_off = sizeof(struct iphdr) + offsetof(struct iphdr, protocol);120else if (proto == PF_INET)121next_off = offsetof(struct iphdr, protocol);122else123next_off = offsetof(struct ipv6hdr, nexthdr);124ipproto_off = ETH_HLEN + next_off;125126/* Overridden later if exthdrs are used: */127opt_ipproto_off = ipproto_off;128129if (strcmp(testname, "ip") == 0) {130if (proto == PF_INET)131optlen = sizeof(struct ip_timestamp);132else {133BUILD_BUG_ON(sizeof(struct ip6_hbh) > MIN_EXTHDR_SIZE);134BUILD_BUG_ON(sizeof(struct ip6_dest) > MIN_EXTHDR_SIZE);135BUILD_BUG_ON(sizeof(struct ip6_frag) > MIN_EXTHDR_SIZE);136137/* same size for HBH and Fragment extension header types */138optlen = MIN_EXTHDR_SIZE;139opt_ipproto_off = ETH_HLEN + sizeof(struct ipv6hdr)140+ offsetof(struct ip6_ext, ip6e_nxt);141}142}143144/* this filter validates the following:145* - packet is IPv4/IPv6 according to the running test.146* - packet is TCP. Also handles the case of one extension header and then TCP.147* - checks the packet tcp dport equals to DPORT. Also handles the case of one148* extension header and then TCP.149*/150struct sock_filter filter[] = {151BPF_STMT(BPF_LD + BPF_H + BPF_ABS, ethproto_off),152BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ntohs(ethhdr_proto), 0, 9),153BPF_STMT(BPF_LD + BPF_B + BPF_ABS, ipproto_off),154BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 2, 0),155BPF_STMT(BPF_LD + BPF_B + BPF_ABS, opt_ipproto_off),156BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 0, 5),157BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off),158BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 2, 0),159BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off + optlen),160BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 0, 1),161BPF_STMT(BPF_RET + BPF_K, 0xFFFFFFFF),162BPF_STMT(BPF_RET + BPF_K, 0),163};164165struct sock_fprog bpf = {166.len = ARRAY_SIZE(filter),167.filter = filter,168};169170if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) < 0)171error(1, errno, "error setting filter");172}173174static uint32_t checksum_nofold(void *data, size_t len, uint32_t sum)175{176uint16_t *words = data;177int i;178179for (i = 0; i < len / 2; i++)180sum += words[i];181if (len & 1)182sum += ((char *)data)[len - 1];183return sum;184}185186static uint16_t checksum_fold(void *data, size_t len, uint32_t sum)187{188sum = checksum_nofold(data, len, sum);189while (sum > 0xFFFF)190sum = (sum & 0xFFFF) + (sum >> 16);191return ~sum;192}193194static uint16_t tcp_checksum(void *buf, int payload_len)195{196struct pseudo_header6 {197struct in6_addr saddr;198struct in6_addr daddr;199uint16_t protocol;200uint16_t payload_len;201} ph6;202struct pseudo_header4 {203struct in_addr saddr;204struct in_addr daddr;205uint16_t protocol;206uint16_t payload_len;207} ph4;208uint32_t sum = 0;209210if (proto == PF_INET6) {211if (inet_pton(AF_INET6, addr6_src, &ph6.saddr) != 1)212error(1, errno, "inet_pton6 source ip pseudo");213if (inet_pton(AF_INET6, addr6_dst, &ph6.daddr) != 1)214error(1, errno, "inet_pton6 dest ip pseudo");215ph6.protocol = htons(IPPROTO_TCP);216ph6.payload_len = htons(sizeof(struct tcphdr) + payload_len);217218sum = checksum_nofold(&ph6, sizeof(ph6), 0);219} else if (proto == PF_INET) {220if (inet_pton(AF_INET, addr4_src, &ph4.saddr) != 1)221error(1, errno, "inet_pton source ip pseudo");222if (inet_pton(AF_INET, addr4_dst, &ph4.daddr) != 1)223error(1, errno, "inet_pton dest ip pseudo");224ph4.protocol = htons(IPPROTO_TCP);225ph4.payload_len = htons(sizeof(struct tcphdr) + payload_len);226227sum = checksum_nofold(&ph4, sizeof(ph4), 0);228}229230return checksum_fold(buf, sizeof(struct tcphdr) + payload_len, sum);231}232233static void read_MAC(uint8_t *mac_addr, char *mac)234{235if (sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",236&mac_addr[0], &mac_addr[1], &mac_addr[2],237&mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6)238error(1, 0, "sscanf");239}240241static void fill_datalinklayer(void *buf)242{243struct ethhdr *eth = buf;244245memcpy(eth->h_dest, dst_mac, ETH_ALEN);246memcpy(eth->h_source, src_mac, ETH_ALEN);247eth->h_proto = ethhdr_proto;248}249250static void fill_networklayer(void *buf, int payload_len, int protocol)251{252struct ipv6hdr *ip6h = buf;253struct iphdr *iph = buf;254255if (proto == PF_INET6) {256memset(ip6h, 0, sizeof(*ip6h));257258ip6h->version = 6;259ip6h->payload_len = htons(sizeof(struct tcphdr) + payload_len);260ip6h->nexthdr = protocol;261ip6h->hop_limit = 8;262if (inet_pton(AF_INET6, addr6_src, &ip6h->saddr) != 1)263error(1, errno, "inet_pton source ip6");264if (inet_pton(AF_INET6, addr6_dst, &ip6h->daddr) != 1)265error(1, errno, "inet_pton dest ip6");266} else if (proto == PF_INET) {267memset(iph, 0, sizeof(*iph));268269iph->version = 4;270iph->ihl = 5;271iph->ttl = 8;272iph->protocol = protocol;273iph->tot_len = htons(sizeof(struct tcphdr) +274payload_len + sizeof(struct iphdr));275iph->frag_off = htons(0x4000); /* DF = 1, MF = 0 */276if (inet_pton(AF_INET, addr4_src, &iph->saddr) != 1)277error(1, errno, "inet_pton source ip");278if (inet_pton(AF_INET, addr4_dst, &iph->daddr) != 1)279error(1, errno, "inet_pton dest ip");280iph->check = checksum_fold(buf, sizeof(struct iphdr), 0);281}282}283284static void fill_transportlayer(void *buf, int seq_offset, int ack_offset,285int payload_len, int fin)286{287struct tcphdr *tcph = buf;288289memset(tcph, 0, sizeof(*tcph));290291tcph->source = htons(SPORT);292tcph->dest = htons(DPORT);293tcph->seq = ntohl(START_SEQ + seq_offset);294tcph->ack_seq = ntohl(START_ACK + ack_offset);295tcph->ack = 1;296tcph->fin = fin;297tcph->doff = 5;298tcph->window = htons(TCP_MAXWIN);299tcph->urg_ptr = 0;300tcph->check = tcp_checksum(tcph, payload_len);301}302303static void write_packet(int fd, char *buf, int len, struct sockaddr_ll *daddr)304{305int ret = -1;306307ret = sendto(fd, buf, len, 0, (struct sockaddr *)daddr, sizeof(*daddr));308if (ret == -1)309error(1, errno, "sendto failure");310if (ret != len)311error(1, errno, "sendto wrong length");312}313314static void create_packet(void *buf, int seq_offset, int ack_offset,315int payload_len, int fin)316{317memset(buf, 0, total_hdr_len);318memset(buf + total_hdr_len, 'a', payload_len);319320fill_transportlayer(buf + tcp_offset, seq_offset, ack_offset,321payload_len, fin);322323if (ipip) {324fill_networklayer(buf + ETH_HLEN, payload_len + sizeof(struct iphdr),325IPPROTO_IPIP);326fill_networklayer(buf + ETH_HLEN + sizeof(struct iphdr),327payload_len, IPPROTO_TCP);328} else {329fill_networklayer(buf + ETH_HLEN, payload_len, IPPROTO_TCP);330}331332fill_datalinklayer(buf);333}334335/* send one extra flag, not first and not last pkt */336static void send_flags(int fd, struct sockaddr_ll *daddr, int psh, int syn,337int rst, int urg)338{339static char flag_buf[MAX_HDR_LEN + PAYLOAD_LEN];340static char buf[MAX_HDR_LEN + PAYLOAD_LEN];341int payload_len, pkt_size, flag, i;342struct tcphdr *tcph;343344payload_len = PAYLOAD_LEN * psh;345pkt_size = total_hdr_len + payload_len;346flag = NUM_PACKETS / 2;347348create_packet(flag_buf, flag * payload_len, 0, payload_len, 0);349350tcph = (struct tcphdr *)(flag_buf + tcp_offset);351tcph->psh = psh;352tcph->syn = syn;353tcph->rst = rst;354tcph->urg = urg;355tcph->check = 0;356tcph->check = tcp_checksum(tcph, payload_len);357358for (i = 0; i < NUM_PACKETS + 1; i++) {359if (i == flag) {360write_packet(fd, flag_buf, pkt_size, daddr);361continue;362}363create_packet(buf, i * PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);364write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);365}366}367368/* Test for data of same length, smaller than previous369* and of different lengths370*/371static void send_data_pkts(int fd, struct sockaddr_ll *daddr,372int payload_len1, int payload_len2)373{374static char buf[ETH_HLEN + IP_MAXPACKET];375376create_packet(buf, 0, 0, payload_len1, 0);377write_packet(fd, buf, total_hdr_len + payload_len1, daddr);378create_packet(buf, payload_len1, 0, payload_len2, 0);379write_packet(fd, buf, total_hdr_len + payload_len2, daddr);380}381382/* If incoming segments make tracked segment length exceed383* legal IP datagram length, do not coalesce384*/385static void send_large(int fd, struct sockaddr_ll *daddr, int remainder)386{387static char pkts[NUM_LARGE_PKT][TOTAL_HDR_LEN + MSS];388static char last[TOTAL_HDR_LEN + MSS];389static char new_seg[TOTAL_HDR_LEN + MSS];390int i;391392for (i = 0; i < NUM_LARGE_PKT; i++)393create_packet(pkts[i], i * MSS, 0, MSS, 0);394create_packet(last, NUM_LARGE_PKT * MSS, 0, remainder, 0);395create_packet(new_seg, (NUM_LARGE_PKT + 1) * MSS, 0, remainder, 0);396397for (i = 0; i < NUM_LARGE_PKT; i++)398write_packet(fd, pkts[i], total_hdr_len + MSS, daddr);399write_packet(fd, last, total_hdr_len + remainder, daddr);400write_packet(fd, new_seg, total_hdr_len + remainder, daddr);401}402403/* Pure acks and dup acks don't coalesce */404static void send_ack(int fd, struct sockaddr_ll *daddr)405{406static char buf[MAX_HDR_LEN];407408create_packet(buf, 0, 0, 0, 0);409write_packet(fd, buf, total_hdr_len, daddr);410write_packet(fd, buf, total_hdr_len, daddr);411create_packet(buf, 0, 1, 0, 0);412write_packet(fd, buf, total_hdr_len, daddr);413}414415static void recompute_packet(char *buf, char *no_ext, int extlen)416{417struct tcphdr *tcphdr = (struct tcphdr *)(buf + tcp_offset);418struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);419struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);420421memmove(buf, no_ext, total_hdr_len);422memmove(buf + total_hdr_len + extlen,423no_ext + total_hdr_len, PAYLOAD_LEN);424425tcphdr->doff = tcphdr->doff + (extlen / 4);426tcphdr->check = 0;427tcphdr->check = tcp_checksum(tcphdr, PAYLOAD_LEN + extlen);428if (proto == PF_INET) {429iph->tot_len = htons(ntohs(iph->tot_len) + extlen);430iph->check = 0;431iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);432433if (ipip) {434iph += 1;435iph->tot_len = htons(ntohs(iph->tot_len) + extlen);436iph->check = 0;437iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);438}439} else {440ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);441}442}443444static void tcp_write_options(char *buf, int kind, int ts)445{446struct tcp_option_ts {447uint8_t kind;448uint8_t len;449uint32_t tsval;450uint32_t tsecr;451} *opt_ts = (void *)buf;452struct tcp_option_window {453uint8_t kind;454uint8_t len;455uint8_t shift;456} *opt_window = (void *)buf;457458switch (kind) {459case TCPOPT_NOP:460buf[0] = TCPOPT_NOP;461break;462case TCPOPT_WINDOW:463memset(opt_window, 0, sizeof(struct tcp_option_window));464opt_window->kind = TCPOPT_WINDOW;465opt_window->len = TCPOLEN_WINDOW;466opt_window->shift = 0;467break;468case TCPOPT_TIMESTAMP:469memset(opt_ts, 0, sizeof(struct tcp_option_ts));470opt_ts->kind = TCPOPT_TIMESTAMP;471opt_ts->len = TCPOLEN_TIMESTAMP;472opt_ts->tsval = ts;473opt_ts->tsecr = 0;474break;475default:476error(1, 0, "unimplemented TCP option");477break;478}479}480481/* TCP with options is always a permutation of {TS, NOP, NOP}.482* Implement different orders to verify coalescing stops.483*/484static void add_standard_tcp_options(char *buf, char *no_ext, int ts, int order)485{486switch (order) {487case 0:488tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);489tcp_write_options(buf + total_hdr_len + 1, TCPOPT_NOP, 0);490tcp_write_options(buf + total_hdr_len + 2 /* two NOP opts */,491TCPOPT_TIMESTAMP, ts);492break;493case 1:494tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0);495tcp_write_options(buf + total_hdr_len + 1,496TCPOPT_TIMESTAMP, ts);497tcp_write_options(buf + total_hdr_len + 1 + TCPOLEN_TIMESTAMP,498TCPOPT_NOP, 0);499break;500case 2:501tcp_write_options(buf + total_hdr_len, TCPOPT_TIMESTAMP, ts);502tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 1,503TCPOPT_NOP, 0);504tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 2,505TCPOPT_NOP, 0);506break;507default:508error(1, 0, "unknown order");509break;510}511recompute_packet(buf, no_ext, TCPOLEN_TSTAMP_APPA);512}513514/* Packets with invalid checksum don't coalesce. */515static void send_changed_checksum(int fd, struct sockaddr_ll *daddr)516{517static char buf[MAX_HDR_LEN + PAYLOAD_LEN];518struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);519int pkt_size = total_hdr_len + PAYLOAD_LEN;520521create_packet(buf, 0, 0, PAYLOAD_LEN, 0);522write_packet(fd, buf, pkt_size, daddr);523524create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);525tcph->check = tcph->check - 1;526write_packet(fd, buf, pkt_size, daddr);527}528529/* Packets with non-consecutive sequence number don't coalesce.*/530static void send_changed_seq(int fd, struct sockaddr_ll *daddr)531{532static char buf[MAX_HDR_LEN + PAYLOAD_LEN];533struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset);534int pkt_size = total_hdr_len + PAYLOAD_LEN;535536create_packet(buf, 0, 0, PAYLOAD_LEN, 0);537write_packet(fd, buf, pkt_size, daddr);538539create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);540tcph->seq = ntohl(htonl(tcph->seq) + 1);541tcph->check = 0;542tcph->check = tcp_checksum(tcph, PAYLOAD_LEN);543write_packet(fd, buf, pkt_size, daddr);544}545546/* Packet with different timestamp option or different timestamps547* don't coalesce.548*/549static void send_changed_ts(int fd, struct sockaddr_ll *daddr)550{551static char buf[MAX_HDR_LEN + PAYLOAD_LEN];552static char extpkt[sizeof(buf) + TCPOLEN_TSTAMP_APPA];553int pkt_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;554555create_packet(buf, 0, 0, PAYLOAD_LEN, 0);556add_standard_tcp_options(extpkt, buf, 0, 0);557write_packet(fd, extpkt, pkt_size, daddr);558559create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);560add_standard_tcp_options(extpkt, buf, 0, 0);561write_packet(fd, extpkt, pkt_size, daddr);562563create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);564add_standard_tcp_options(extpkt, buf, 100, 0);565write_packet(fd, extpkt, pkt_size, daddr);566567create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);568add_standard_tcp_options(extpkt, buf, 100, 1);569write_packet(fd, extpkt, pkt_size, daddr);570571create_packet(buf, PAYLOAD_LEN * 4, 0, PAYLOAD_LEN, 0);572add_standard_tcp_options(extpkt, buf, 100, 2);573write_packet(fd, extpkt, pkt_size, daddr);574}575576/* Packet with different tcp options don't coalesce. */577static void send_diff_opt(int fd, struct sockaddr_ll *daddr)578{579static char buf[MAX_HDR_LEN + PAYLOAD_LEN];580static char extpkt1[sizeof(buf) + TCPOLEN_TSTAMP_APPA];581static char extpkt2[sizeof(buf) + TCPOLEN_MAXSEG];582int extpkt1_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA;583int extpkt2_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_MAXSEG;584585create_packet(buf, 0, 0, PAYLOAD_LEN, 0);586add_standard_tcp_options(extpkt1, buf, 0, 0);587write_packet(fd, extpkt1, extpkt1_size, daddr);588589create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);590add_standard_tcp_options(extpkt1, buf, 0, 0);591write_packet(fd, extpkt1, extpkt1_size, daddr);592593create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);594tcp_write_options(extpkt2 + MAX_HDR_LEN, TCPOPT_NOP, 0);595tcp_write_options(extpkt2 + MAX_HDR_LEN + 1, TCPOPT_WINDOW, 0);596recompute_packet(extpkt2, buf, TCPOLEN_WINDOW + 1);597write_packet(fd, extpkt2, extpkt2_size, daddr);598}599600static void add_ipv4_ts_option(void *buf, void *optpkt)601{602struct ip_timestamp *ts = (struct ip_timestamp *)(optpkt + tcp_offset);603int optlen = sizeof(struct ip_timestamp);604struct iphdr *iph;605606if (optlen % 4)607error(1, 0, "ipv4 timestamp length is not a multiple of 4B");608609ts->ipt_code = IPOPT_TS;610ts->ipt_len = optlen;611ts->ipt_ptr = 5;612ts->ipt_flg = IPOPT_TS_TSONLY;613614memcpy(optpkt, buf, tcp_offset);615memcpy(optpkt + tcp_offset + optlen, buf + tcp_offset,616sizeof(struct tcphdr) + PAYLOAD_LEN);617618iph = (struct iphdr *)(optpkt + ETH_HLEN);619iph->ihl = 5 + (optlen / 4);620iph->tot_len = htons(ntohs(iph->tot_len) + optlen);621iph->check = 0;622iph->check = checksum_fold(iph, sizeof(struct iphdr) + optlen, 0);623}624625static void add_ipv6_exthdr(void *buf, void *optpkt, __u8 exthdr_type, char *ext_payload)626{627struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr *)(optpkt + tcp_offset);628struct ipv6hdr *iph = (struct ipv6hdr *)(optpkt + ETH_HLEN);629char *exthdr_payload_start = (char *)(exthdr + 1);630631exthdr->hdrlen = 0;632exthdr->nexthdr = IPPROTO_TCP;633634memcpy(exthdr_payload_start, ext_payload, MIN_EXTHDR_SIZE - sizeof(*exthdr));635636memcpy(optpkt, buf, tcp_offset);637memcpy(optpkt + tcp_offset + MIN_EXTHDR_SIZE, buf + tcp_offset,638sizeof(struct tcphdr) + PAYLOAD_LEN);639640iph->nexthdr = exthdr_type;641iph->payload_len = htons(ntohs(iph->payload_len) + MIN_EXTHDR_SIZE);642}643644static void fix_ip4_checksum(struct iphdr *iph)645{646iph->check = 0;647iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);648}649650static void send_flush_id_case(int fd, struct sockaddr_ll *daddr, int tcase)651{652static char buf1[MAX_HDR_LEN + PAYLOAD_LEN];653static char buf2[MAX_HDR_LEN + PAYLOAD_LEN];654static char buf3[MAX_HDR_LEN + PAYLOAD_LEN];655bool send_three = false;656struct iphdr *iph1;657struct iphdr *iph2;658struct iphdr *iph3;659660iph1 = (struct iphdr *)(buf1 + ETH_HLEN);661iph2 = (struct iphdr *)(buf2 + ETH_HLEN);662iph3 = (struct iphdr *)(buf3 + ETH_HLEN);663664create_packet(buf1, 0, 0, PAYLOAD_LEN, 0);665create_packet(buf2, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);666create_packet(buf3, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);667668switch (tcase) {669case 0: /* DF=1, Incrementing - should coalesce */670iph1->frag_off |= htons(IP_DF);671iph1->id = htons(8);672673iph2->frag_off |= htons(IP_DF);674iph2->id = htons(9);675break;676677case 1: /* DF=1, Fixed - should coalesce */678iph1->frag_off |= htons(IP_DF);679iph1->id = htons(8);680681iph2->frag_off |= htons(IP_DF);682iph2->id = htons(8);683break;684685case 2: /* DF=0, Incrementing - should coalesce */686iph1->frag_off &= ~htons(IP_DF);687iph1->id = htons(8);688689iph2->frag_off &= ~htons(IP_DF);690iph2->id = htons(9);691break;692693case 3: /* DF=0, Fixed - should coalesce */694iph1->frag_off &= ~htons(IP_DF);695iph1->id = htons(8);696697iph2->frag_off &= ~htons(IP_DF);698iph2->id = htons(8);699break;700701case 4: /* DF=1, two packets incrementing, and one fixed - should702* coalesce only the first two packets703*/704iph1->frag_off |= htons(IP_DF);705iph1->id = htons(8);706707iph2->frag_off |= htons(IP_DF);708iph2->id = htons(9);709710iph3->frag_off |= htons(IP_DF);711iph3->id = htons(9);712send_three = true;713break;714715case 5: /* DF=1, two packets fixed, and one incrementing - should716* coalesce only the first two packets717*/718iph1->frag_off |= htons(IP_DF);719iph1->id = htons(8);720721iph2->frag_off |= htons(IP_DF);722iph2->id = htons(8);723724iph3->frag_off |= htons(IP_DF);725iph3->id = htons(9);726send_three = true;727break;728}729730fix_ip4_checksum(iph1);731fix_ip4_checksum(iph2);732write_packet(fd, buf1, total_hdr_len + PAYLOAD_LEN, daddr);733write_packet(fd, buf2, total_hdr_len + PAYLOAD_LEN, daddr);734735if (send_three) {736fix_ip4_checksum(iph3);737write_packet(fd, buf3, total_hdr_len + PAYLOAD_LEN, daddr);738}739}740741static void test_flush_id(int fd, struct sockaddr_ll *daddr, char *fin_pkt)742{743for (int i = 0; i < num_flush_id_cases; i++) {744sleep(1);745send_flush_id_case(fd, daddr, i);746sleep(1);747write_packet(fd, fin_pkt, total_hdr_len, daddr);748}749}750751static void send_ipv6_exthdr(int fd, struct sockaddr_ll *daddr, char *ext_data1, char *ext_data2)752{753static char buf[MAX_HDR_LEN + PAYLOAD_LEN];754static char exthdr_pck[sizeof(buf) + MIN_EXTHDR_SIZE];755756create_packet(buf, 0, 0, PAYLOAD_LEN, 0);757add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_DSTOPTS, ext_data1);758write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr);759760create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);761add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_DSTOPTS, ext_data2);762write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr);763}764765/* IPv4 options shouldn't coalesce */766static void send_ip_options(int fd, struct sockaddr_ll *daddr)767{768static char buf[MAX_HDR_LEN + PAYLOAD_LEN];769static char optpkt[sizeof(buf) + sizeof(struct ip_timestamp)];770int optlen = sizeof(struct ip_timestamp);771int pkt_size = total_hdr_len + PAYLOAD_LEN + optlen;772773create_packet(buf, 0, 0, PAYLOAD_LEN, 0);774write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);775776create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0);777add_ipv4_ts_option(buf, optpkt);778write_packet(fd, optpkt, pkt_size, daddr);779780create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);781write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr);782}783784/* IPv4 fragments shouldn't coalesce */785static void send_fragment4(int fd, struct sockaddr_ll *daddr)786{787static char buf[IP_MAXPACKET];788struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);789int pkt_size = total_hdr_len + PAYLOAD_LEN;790791create_packet(buf, 0, 0, PAYLOAD_LEN, 0);792write_packet(fd, buf, pkt_size, daddr);793794/* Once fragmented, packet would retain the total_len.795* Tcp header is prepared as if rest of data is in follow-up frags,796* but follow up frags aren't actually sent.797*/798memset(buf + total_hdr_len, 'a', PAYLOAD_LEN * 2);799fill_transportlayer(buf + tcp_offset, PAYLOAD_LEN, 0, PAYLOAD_LEN * 2, 0);800fill_networklayer(buf + ETH_HLEN, PAYLOAD_LEN, IPPROTO_TCP);801fill_datalinklayer(buf);802803iph->frag_off = htons(0x6000); // DF = 1, MF = 1804iph->check = 0;805iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);806write_packet(fd, buf, pkt_size, daddr);807}808809/* IPv4 packets with different ttl don't coalesce.*/810static void send_changed_ttl(int fd, struct sockaddr_ll *daddr)811{812int pkt_size = total_hdr_len + PAYLOAD_LEN;813static char buf[MAX_HDR_LEN + PAYLOAD_LEN];814struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);815816create_packet(buf, 0, 0, PAYLOAD_LEN, 0);817write_packet(fd, buf, pkt_size, daddr);818819create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);820iph->ttl = 7;821iph->check = 0;822iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);823write_packet(fd, buf, pkt_size, daddr);824}825826/* Packets with different tos don't coalesce.*/827static void send_changed_tos(int fd, struct sockaddr_ll *daddr)828{829int pkt_size = total_hdr_len + PAYLOAD_LEN;830static char buf[MAX_HDR_LEN + PAYLOAD_LEN];831struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);832struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);833834create_packet(buf, 0, 0, PAYLOAD_LEN, 0);835write_packet(fd, buf, pkt_size, daddr);836837create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);838if (proto == PF_INET) {839iph->tos = 1;840iph->check = 0;841iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);842} else if (proto == PF_INET6) {843ip6h->priority = 0xf;844}845write_packet(fd, buf, pkt_size, daddr);846}847848/* Packets with different ECN don't coalesce.*/849static void send_changed_ECN(int fd, struct sockaddr_ll *daddr)850{851int pkt_size = total_hdr_len + PAYLOAD_LEN;852static char buf[MAX_HDR_LEN + PAYLOAD_LEN];853struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN);854855create_packet(buf, 0, 0, PAYLOAD_LEN, 0);856write_packet(fd, buf, pkt_size, daddr);857858create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0);859if (proto == PF_INET) {860buf[ETH_HLEN + 1] ^= 0x2; // ECN set to 10861iph->check = 0;862iph->check = checksum_fold(iph, sizeof(struct iphdr), 0);863} else {864buf[ETH_HLEN + 1] ^= 0x20; // ECN set to 10865}866write_packet(fd, buf, pkt_size, daddr);867}868869/* IPv6 fragments and packets with extensions don't coalesce.*/870static void send_fragment6(int fd, struct sockaddr_ll *daddr)871{872static char buf[MAX_HDR_LEN + PAYLOAD_LEN];873static char extpkt[MAX_HDR_LEN + PAYLOAD_LEN +874sizeof(struct ip6_frag)];875struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN);876struct ip6_frag *frag = (void *)(extpkt + tcp_offset);877int extlen = sizeof(struct ip6_frag);878int bufpkt_len = total_hdr_len + PAYLOAD_LEN;879int extpkt_len = bufpkt_len + extlen;880int i;881882for (i = 0; i < 2; i++) {883create_packet(buf, PAYLOAD_LEN * i, 0, PAYLOAD_LEN, 0);884write_packet(fd, buf, bufpkt_len, daddr);885}886sleep(1);887create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0);888memset(extpkt, 0, extpkt_len);889890ip6h->nexthdr = IPPROTO_FRAGMENT;891ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen);892frag->ip6f_nxt = IPPROTO_TCP;893894memcpy(extpkt, buf, tcp_offset);895memcpy(extpkt + tcp_offset + extlen, buf + tcp_offset,896sizeof(struct tcphdr) + PAYLOAD_LEN);897write_packet(fd, extpkt, extpkt_len, daddr);898899create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0);900write_packet(fd, buf, bufpkt_len, daddr);901}902903static void bind_packetsocket(int fd)904{905struct sockaddr_ll daddr = {};906907daddr.sll_family = AF_PACKET;908daddr.sll_protocol = ethhdr_proto;909daddr.sll_ifindex = if_nametoindex(ifname);910if (daddr.sll_ifindex == 0)911error(1, errno, "if_nametoindex");912913if (bind(fd, (void *)&daddr, sizeof(daddr)) < 0)914error(1, errno, "could not bind socket");915}916917static void set_timeout(int fd)918{919struct timeval timeout;920921timeout.tv_sec = 3;922timeout.tv_usec = 0;923if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,924sizeof(timeout)) < 0)925error(1, errno, "cannot set timeout, setsockopt failed");926}927928static void check_recv_pkts(int fd, int *correct_payload,929int correct_num_pkts)930{931static char buffer[IP_MAXPACKET + ETH_HLEN + 1];932struct iphdr *iph = (struct iphdr *)(buffer + ETH_HLEN);933struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + ETH_HLEN);934struct tcphdr *tcph;935bool bad_packet = false;936int tcp_ext_len = 0;937int ip_ext_len = 0;938int pkt_size = -1;939int data_len = 0;940int num_pkt = 0;941int i;942943vlog("Expected {");944for (i = 0; i < correct_num_pkts; i++)945vlog("%d ", correct_payload[i]);946vlog("}, Total %d packets\nReceived {", correct_num_pkts);947948while (1) {949ip_ext_len = 0;950pkt_size = recv(fd, buffer, IP_MAXPACKET + ETH_HLEN + 1, 0);951if (pkt_size < 0)952error(1, errno, "could not receive");953954if (iph->version == 4)955ip_ext_len = (iph->ihl - 5) * 4;956else if (ip6h->version == 6 && ip6h->nexthdr != IPPROTO_TCP)957ip_ext_len = MIN_EXTHDR_SIZE;958959tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len);960961if (tcph->fin)962break;963964tcp_ext_len = (tcph->doff - 5) * 4;965data_len = pkt_size - total_hdr_len - tcp_ext_len - ip_ext_len;966/* Min ethernet frame payload is 46(ETH_ZLEN - ETH_HLEN) by RFC 802.3.967* Ipv4/tcp packets without at least 6 bytes of data will be padded.968* Packet sockets are protocol agnostic, and will not trim the padding.969*/970if (pkt_size == ETH_ZLEN && iph->version == 4) {971data_len = ntohs(iph->tot_len)972- sizeof(struct tcphdr) - sizeof(struct iphdr);973}974vlog("%d ", data_len);975if (data_len != correct_payload[num_pkt]) {976vlog("[!=%d]", correct_payload[num_pkt]);977bad_packet = true;978}979num_pkt++;980}981vlog("}, Total %d packets.\n", num_pkt);982if (num_pkt != correct_num_pkts)983error(1, 0, "incorrect number of packets");984if (bad_packet)985error(1, 0, "incorrect packet geometry");986987printf("Test succeeded\n\n");988}989990static void gro_sender(void)991{992const int fin_delay_us = 100 * 1000;993static char fin_pkt[MAX_HDR_LEN];994struct sockaddr_ll daddr = {};995int txfd = -1;996997txfd = socket(PF_PACKET, SOCK_RAW, IPPROTO_RAW);998if (txfd < 0)999error(1, errno, "socket creation");10001001memset(&daddr, 0, sizeof(daddr));1002daddr.sll_ifindex = if_nametoindex(ifname);1003if (daddr.sll_ifindex == 0)1004error(1, errno, "if_nametoindex");1005daddr.sll_family = AF_PACKET;1006memcpy(daddr.sll_addr, dst_mac, ETH_ALEN);1007daddr.sll_halen = ETH_ALEN;1008create_packet(fin_pkt, PAYLOAD_LEN * 2, 0, 0, 1);10091010if (strcmp(testname, "data") == 0) {1011send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN);1012write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10131014send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN / 2);1015write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10161017send_data_pkts(txfd, &daddr, PAYLOAD_LEN / 2, PAYLOAD_LEN);1018write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1019} else if (strcmp(testname, "ack") == 0) {1020send_ack(txfd, &daddr);1021write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1022} else if (strcmp(testname, "flags") == 0) {1023send_flags(txfd, &daddr, 1, 0, 0, 0);1024write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10251026send_flags(txfd, &daddr, 0, 1, 0, 0);1027write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10281029send_flags(txfd, &daddr, 0, 0, 1, 0);1030write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10311032send_flags(txfd, &daddr, 0, 0, 0, 1);1033write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1034} else if (strcmp(testname, "tcp") == 0) {1035send_changed_checksum(txfd, &daddr);1036/* Adding sleep before sending FIN so that it is not1037* received prior to other packets.1038*/1039usleep(fin_delay_us);1040write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10411042send_changed_seq(txfd, &daddr);1043usleep(fin_delay_us);1044write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10451046send_changed_ts(txfd, &daddr);1047usleep(fin_delay_us);1048write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10491050send_diff_opt(txfd, &daddr);1051usleep(fin_delay_us);1052write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1053} else if (strcmp(testname, "ip") == 0) {1054send_changed_ECN(txfd, &daddr);1055write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10561057send_changed_tos(txfd, &daddr);1058write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1059if (proto == PF_INET) {1060/* Modified packets may be received out of order.1061* Sleep function added to enforce test boundaries1062* so that fin pkts are not received prior to other pkts.1063*/1064sleep(1);1065send_changed_ttl(txfd, &daddr);1066write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10671068sleep(1);1069send_ip_options(txfd, &daddr);1070sleep(1);1071write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10721073sleep(1);1074send_fragment4(txfd, &daddr);1075sleep(1);1076write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10771078test_flush_id(txfd, &daddr, fin_pkt);1079} else if (proto == PF_INET6) {1080sleep(1);1081send_fragment6(txfd, &daddr);1082sleep(1);1083write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10841085sleep(1);1086/* send IPv6 packets with ext header with same payload */1087send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_1);1088sleep(1);1089write_packet(txfd, fin_pkt, total_hdr_len, &daddr);10901091sleep(1);1092/* send IPv6 packets with ext header with different payload */1093send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_2);1094sleep(1);1095write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1096}1097} else if (strcmp(testname, "large") == 0) {1098/* 20 is the difference between min iphdr size1099* and min ipv6hdr size. Like MAX_HDR_SIZE,1100* MAX_PAYLOAD is defined with the larger header of the two.1101*/1102int offset = (proto == PF_INET && !ipip) ? 20 : 0;1103int remainder = (MAX_PAYLOAD + offset) % MSS;11041105send_large(txfd, &daddr, remainder);1106write_packet(txfd, fin_pkt, total_hdr_len, &daddr);11071108send_large(txfd, &daddr, remainder + 1);1109write_packet(txfd, fin_pkt, total_hdr_len, &daddr);1110} else {1111error(1, 0, "Unknown testcase");1112}11131114if (close(txfd))1115error(1, errno, "socket close");1116}11171118static void gro_receiver(void)1119{1120static int correct_payload[NUM_PACKETS];1121int rxfd = -1;11221123rxfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_NONE));1124if (rxfd < 0)1125error(1, 0, "socket creation");1126setup_sock_filter(rxfd);1127set_timeout(rxfd);1128bind_packetsocket(rxfd);11291130ksft_ready();11311132memset(correct_payload, 0, sizeof(correct_payload));11331134if (strcmp(testname, "data") == 0) {1135printf("pure data packet of same size: ");1136correct_payload[0] = PAYLOAD_LEN * 2;1137check_recv_pkts(rxfd, correct_payload, 1);11381139printf("large data packets followed by a smaller one: ");1140correct_payload[0] = PAYLOAD_LEN * 1.5;1141check_recv_pkts(rxfd, correct_payload, 1);11421143printf("small data packets followed by a larger one: ");1144correct_payload[0] = PAYLOAD_LEN / 2;1145correct_payload[1] = PAYLOAD_LEN;1146check_recv_pkts(rxfd, correct_payload, 2);1147} else if (strcmp(testname, "ack") == 0) {1148printf("duplicate ack and pure ack: ");1149check_recv_pkts(rxfd, correct_payload, 3);1150} else if (strcmp(testname, "flags") == 0) {1151correct_payload[0] = PAYLOAD_LEN * 3;1152correct_payload[1] = PAYLOAD_LEN * 2;11531154printf("psh flag ends coalescing: ");1155check_recv_pkts(rxfd, correct_payload, 2);11561157correct_payload[0] = PAYLOAD_LEN * 2;1158correct_payload[1] = 0;1159correct_payload[2] = PAYLOAD_LEN * 2;1160printf("syn flag ends coalescing: ");1161check_recv_pkts(rxfd, correct_payload, 3);11621163printf("rst flag ends coalescing: ");1164check_recv_pkts(rxfd, correct_payload, 3);11651166printf("urg flag ends coalescing: ");1167check_recv_pkts(rxfd, correct_payload, 3);1168} else if (strcmp(testname, "tcp") == 0) {1169correct_payload[0] = PAYLOAD_LEN;1170correct_payload[1] = PAYLOAD_LEN;1171correct_payload[2] = PAYLOAD_LEN;1172correct_payload[3] = PAYLOAD_LEN;11731174printf("changed checksum does not coalesce: ");1175check_recv_pkts(rxfd, correct_payload, 2);11761177printf("Wrong Seq number doesn't coalesce: ");1178check_recv_pkts(rxfd, correct_payload, 2);11791180printf("Different timestamp doesn't coalesce: ");1181correct_payload[0] = PAYLOAD_LEN * 2;1182check_recv_pkts(rxfd, correct_payload, 4);11831184printf("Different options doesn't coalesce: ");1185correct_payload[0] = PAYLOAD_LEN * 2;1186check_recv_pkts(rxfd, correct_payload, 2);1187} else if (strcmp(testname, "ip") == 0) {1188correct_payload[0] = PAYLOAD_LEN;1189correct_payload[1] = PAYLOAD_LEN;11901191printf("different ECN doesn't coalesce: ");1192check_recv_pkts(rxfd, correct_payload, 2);11931194printf("different tos doesn't coalesce: ");1195check_recv_pkts(rxfd, correct_payload, 2);11961197if (proto == PF_INET) {1198printf("different ttl doesn't coalesce: ");1199check_recv_pkts(rxfd, correct_payload, 2);12001201printf("ip options doesn't coalesce: ");1202correct_payload[2] = PAYLOAD_LEN;1203check_recv_pkts(rxfd, correct_payload, 3);12041205printf("fragmented ip4 doesn't coalesce: ");1206check_recv_pkts(rxfd, correct_payload, 2);12071208/* is_atomic checks */1209printf("DF=1, Incrementing - should coalesce: ");1210correct_payload[0] = PAYLOAD_LEN * 2;1211check_recv_pkts(rxfd, correct_payload, 1);12121213printf("DF=1, Fixed - should coalesce: ");1214correct_payload[0] = PAYLOAD_LEN * 2;1215check_recv_pkts(rxfd, correct_payload, 1);12161217printf("DF=0, Incrementing - should coalesce: ");1218correct_payload[0] = PAYLOAD_LEN * 2;1219check_recv_pkts(rxfd, correct_payload, 1);12201221printf("DF=0, Fixed - should coalesce: ");1222correct_payload[0] = PAYLOAD_LEN * 2;1223check_recv_pkts(rxfd, correct_payload, 1);12241225printf("DF=1, 2 Incrementing and one fixed - should coalesce only first 2 packets: ");1226correct_payload[0] = PAYLOAD_LEN * 2;1227correct_payload[1] = PAYLOAD_LEN;1228check_recv_pkts(rxfd, correct_payload, 2);12291230printf("DF=1, 2 Fixed and one incrementing - should coalesce only first 2 packets: ");1231correct_payload[0] = PAYLOAD_LEN * 2;1232correct_payload[1] = PAYLOAD_LEN;1233check_recv_pkts(rxfd, correct_payload, 2);1234} else if (proto == PF_INET6) {1235/* GRO doesn't check for ipv6 hop limit when flushing.1236* Hence no corresponding test to the ipv4 case.1237*/1238printf("fragmented ip6 doesn't coalesce: ");1239correct_payload[0] = PAYLOAD_LEN * 2;1240correct_payload[1] = PAYLOAD_LEN;1241correct_payload[2] = PAYLOAD_LEN;1242check_recv_pkts(rxfd, correct_payload, 3);12431244printf("ipv6 with ext header does coalesce: ");1245correct_payload[0] = PAYLOAD_LEN * 2;1246check_recv_pkts(rxfd, correct_payload, 1);12471248printf("ipv6 with ext header with different payloads doesn't coalesce: ");1249correct_payload[0] = PAYLOAD_LEN;1250correct_payload[1] = PAYLOAD_LEN;1251check_recv_pkts(rxfd, correct_payload, 2);1252}1253} else if (strcmp(testname, "large") == 0) {1254int offset = (proto == PF_INET && !ipip) ? 20 : 0;1255int remainder = (MAX_PAYLOAD + offset) % MSS;12561257correct_payload[0] = (MAX_PAYLOAD + offset);1258correct_payload[1] = remainder;1259printf("Shouldn't coalesce if exceed IP max pkt size: ");1260check_recv_pkts(rxfd, correct_payload, 2);12611262/* last segment sent individually, doesn't start new segment */1263correct_payload[0] = correct_payload[0] - remainder;1264correct_payload[1] = remainder + 1;1265correct_payload[2] = remainder + 1;1266check_recv_pkts(rxfd, correct_payload, 3);1267} else {1268error(1, 0, "Test case error, should never trigger");1269}12701271if (close(rxfd))1272error(1, 0, "socket close");1273}12741275static void parse_args(int argc, char **argv)1276{1277static const struct option opts[] = {1278{ "daddr", required_argument, NULL, 'd' },1279{ "dmac", required_argument, NULL, 'D' },1280{ "iface", required_argument, NULL, 'i' },1281{ "ipv4", no_argument, NULL, '4' },1282{ "ipv6", no_argument, NULL, '6' },1283{ "ipip", no_argument, NULL, 'e' },1284{ "rx", no_argument, NULL, 'r' },1285{ "saddr", required_argument, NULL, 's' },1286{ "smac", required_argument, NULL, 'S' },1287{ "test", required_argument, NULL, 't' },1288{ "verbose", no_argument, NULL, 'v' },1289{ 0, 0, 0, 0 }1290};1291int c;12921293while ((c = getopt_long(argc, argv, "46d:D:ei:rs:S:t:v", opts, NULL)) != -1) {1294switch (c) {1295case '4':1296proto = PF_INET;1297ethhdr_proto = htons(ETH_P_IP);1298break;1299case '6':1300proto = PF_INET6;1301ethhdr_proto = htons(ETH_P_IPV6);1302break;1303case 'e':1304ipip = true;1305proto = PF_INET;1306ethhdr_proto = htons(ETH_P_IP);1307break;1308case 'd':1309addr4_dst = addr6_dst = optarg;1310break;1311case 'D':1312dmac = optarg;1313break;1314case 'i':1315ifname = optarg;1316break;1317case 'r':1318tx_socket = false;1319break;1320case 's':1321addr4_src = addr6_src = optarg;1322break;1323case 'S':1324smac = optarg;1325break;1326case 't':1327testname = optarg;1328break;1329case 'v':1330verbose = true;1331break;1332default:1333error(1, 0, "%s invalid option %c\n", __func__, c);1334break;1335}1336}1337}13381339int main(int argc, char **argv)1340{1341parse_args(argc, argv);13421343if (ipip) {1344tcp_offset = ETH_HLEN + sizeof(struct iphdr) * 2;1345total_hdr_len = tcp_offset + sizeof(struct tcphdr);1346} else if (proto == PF_INET) {1347tcp_offset = ETH_HLEN + sizeof(struct iphdr);1348total_hdr_len = tcp_offset + sizeof(struct tcphdr);1349} else if (proto == PF_INET6) {1350tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr);1351total_hdr_len = MAX_HDR_LEN;1352} else {1353error(1, 0, "Protocol family is not ipv4 or ipv6");1354}13551356read_MAC(src_mac, smac);1357read_MAC(dst_mac, dmac);13581359if (tx_socket) {1360gro_sender();1361} else {1362/* Only the receiver exit status determines test success. */1363gro_receiver();1364fprintf(stderr, "Gro::%s test passed.\n", testname);1365}13661367return 0;1368}136913701371