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