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