1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This testsuite provides conformance testing for GRO coalescing. 4 * 5 * Test cases: 6 * 7 * data_*: 8 * Data packets of the same size and same header setup with correct 9 * sequence numbers coalesce. The one exception being the last data 10 * packet coalesced: it can be smaller than the rest and coalesced 11 * as long as it is in the same flow. 12 * - data_same: same size packets coalesce 13 * - data_lrg_sml: large then small coalesces 14 * - data_lrg_1byte: large then 1 byte coalesces (Ethernet padding) 15 * - data_sml_lrg: small then large doesn't coalesce 16 * - data_burst: two bursts of two, separated by 100ms 17 * 18 * ack: 19 * Pure ACK does not coalesce. 20 * 21 * flags_*: 22 * No packets with PSH, SYN, URG, RST, CWR set will be coalesced. 23 * - flags_psh, flags_syn, flags_rst, flags_urg, flags_cwr 24 * 25 * tcp_*: 26 * Packets with incorrect checksum, non-consecutive seqno and 27 * different TCP header options shouldn't coalesce. Nit: given that 28 * some extension headers have paddings, such as timestamp, headers 29 * that are padded differently would not be coalesced. 30 * - tcp_csum: incorrect checksum 31 * - tcp_seq: non-consecutive sequence numbers 32 * - tcp_ts: different timestamps 33 * - tcp_opt: different TCP options 34 * 35 * ip_*: 36 * Packets with different (ECN, TTL, TOS) header, IP options or 37 * IP fragments shouldn't coalesce. 38 * - ip_ecn, ip_tos: shared between IPv4/IPv6 39 * - ip_csum: IPv4 only, bad IP header checksum 40 * - ip_ttl, ip_opt, ip_frag4: IPv4 only 41 * - ip_id_df*: IPv4 IP ID field coalescing tests 42 * - ip_frag6, ip_v6ext_*: IPv6 only 43 * 44 * large_*: 45 * Packets larger than GRO_MAX_SIZE packets shouldn't coalesce. 46 * - large_max: exceeding max size 47 * - large_rem: remainder handling 48 * 49 * single, capacity: 50 * Boring cases used to test coalescing machinery itself and stats 51 * more than protocol behavior. 52 * 53 * MSS is defined as 4096 - header because if it is too small 54 * (i.e. 1500 MTU - header), it will result in many packets, 55 * increasing the "large" test case's flakiness. This is because 56 * due to time sensitivity in the coalescing window, the receiver 57 * may not coalesce all of the packets. 58 * 59 * Note the timing issue applies to all of the test cases, so some 60 * flakiness is to be expected. 61 * 62 */ 63 64 #define _GNU_SOURCE 65 66 #include <arpa/inet.h> 67 #include <errno.h> 68 #include <error.h> 69 #include <getopt.h> 70 #include <net/ethernet.h> 71 #include <net/if.h> 72 #include <linux/filter.h> 73 #include <linux/if_packet.h> 74 #include <linux/if_pppox.h> 75 #include <linux/ipv6.h> 76 #include <linux/net_tstamp.h> 77 #include <linux/ppp_defs.h> 78 #include <netinet/in.h> 79 #include <netinet/ip.h> 80 #include <netinet/ip6.h> 81 #include <netinet/tcp.h> 82 #include <stdbool.h> 83 #include <stddef.h> 84 #include <stdio.h> 85 #include <stdarg.h> 86 #include <string.h> 87 #include <time.h> 88 #include <unistd.h> 89 90 #include "kselftest.h" 91 #include "ksft.h" 92 93 #define DPORT 8000 94 #define SPORT 1500 95 #define PAYLOAD_LEN 100 96 #define NUM_PACKETS 4 97 #define START_SEQ 100 98 #define START_ACK 100 99 #define ETH_P_NONE 0 100 #define ASSUMED_MTU 4096 101 #define MAX_MSS (ASSUMED_MTU - sizeof(struct iphdr) - sizeof(struct tcphdr)) 102 #define MAX_HDR_LEN \ 103 (ETH_HLEN + sizeof(struct ipv6hdr) * 2 + sizeof(struct tcphdr)) 104 #define MAX_LARGE_PKT_CNT ((IP_MAXPACKET - (MAX_HDR_LEN - ETH_HLEN)) / \ 105 (ASSUMED_MTU - (MAX_HDR_LEN - ETH_HLEN))) 106 #define MIN_EXTHDR_SIZE 8 107 #define L2_HLEN_MAX (ETH_HLEN + PPPOE_SES_HLEN) 108 #define EXT_PAYLOAD_1 "\x00\x00\x00\x00\x00\x00" 109 #define EXT_PAYLOAD_2 "\x11\x11\x11\x11\x11\x11" 110 111 #define ipv6_optlen(p) (((p)->hdrlen+1) << 3) /* calculate IPv6 extension header len */ 112 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 113 114 enum flush_id_case { 115 FLUSH_ID_DF1_INC, 116 FLUSH_ID_DF1_FIXED, 117 FLUSH_ID_DF0_INC, 118 FLUSH_ID_DF0_FIXED, 119 FLUSH_ID_DF1_INC_FIXED, 120 FLUSH_ID_DF1_FIXED_INC, 121 }; 122 123 static const char *addr6_src = "fdaa::2"; 124 static const char *addr6_dst = "fdaa::1"; 125 static const char *addr4_src = "192.168.1.200"; 126 static const char *addr4_dst = "192.168.1.100"; 127 static int proto = -1; 128 static uint8_t src_mac[ETH_ALEN], dst_mac[ETH_ALEN]; 129 static char *testname = "data"; 130 static char *ifname = "eth0"; 131 static char *smac = "aa:00:00:00:00:02"; 132 static char *dmac = "aa:00:00:00:00:01"; 133 static bool verbose; 134 static bool tx_socket = true; 135 static int tcp_offset = -1; 136 static int total_hdr_len = -1; 137 static int ethhdr_proto = -1; 138 static bool ipip; 139 static bool ip6ip6; 140 static bool pppoe; 141 static uint64_t txtime_ns; 142 static int num_flows = 4; 143 static bool order_check; 144 145 #define CAPACITY_PAYLOAD_LEN 200 146 147 #define TXTIME_DELAY_MS 5 148 149 /* Max TCP payload that GRO will coalesce. The outer header overhead 150 * varies by encapsulation, reducing the effective max payload. 151 */ 152 static int max_payload(void) 153 { 154 return IP_MAXPACKET - (total_hdr_len - ETH_HLEN); 155 } 156 157 static int calc_mss(void) 158 { 159 return ASSUMED_MTU - (total_hdr_len - ETH_HLEN); 160 } 161 162 static int num_large_pkt(void) 163 { 164 return max_payload() / calc_mss(); 165 } 166 167 static void vlog(const char *fmt, ...) 168 { 169 va_list args; 170 171 if (verbose) { 172 va_start(args, fmt); 173 vfprintf(stderr, fmt, args); 174 va_end(args); 175 } 176 } 177 178 static void fill_pppoelayer(void *buf, int payload_len, uint16_t sid) 179 { 180 struct pppoe_ppp_hdr { 181 struct pppoe_hdr eh; 182 __be16 proto; 183 } *ph = buf; 184 185 payload_len += sizeof(struct tcphdr); 186 ph->eh.type = 1; 187 ph->eh.ver = 1; 188 ph->eh.code = 0; 189 ph->eh.sid = htons(sid); 190 ph->eh.length = htons(payload_len + sizeof(ph->proto)); 191 ph->proto = htons(proto == PF_INET ? PPP_IP : PPP_IPV6); 192 } 193 194 static void setup_sock_filter(int fd) 195 { 196 const int dport_off = tcp_offset + offsetof(struct tcphdr, dest); 197 const int ethproto_off = offsetof(struct ethhdr, h_proto); 198 int optlen = 0; 199 int ipproto_off, opt_ipproto_off; 200 201 if (proto == PF_INET) 202 ipproto_off = tcp_offset - sizeof(struct iphdr) + 203 offsetof(struct iphdr, protocol); 204 else 205 ipproto_off = tcp_offset - sizeof(struct ipv6hdr) + 206 offsetof(struct ipv6hdr, nexthdr); 207 208 /* Overridden later if exthdrs are used: */ 209 opt_ipproto_off = ipproto_off; 210 211 if (strcmp(testname, "ip_opt") == 0) { 212 optlen = sizeof(struct ip_timestamp); 213 } else if (strcmp(testname, "ip_frag6") == 0 || 214 strcmp(testname, "ip_v6ext_same") == 0 || 215 strcmp(testname, "ip_v6ext_diff") == 0) { 216 BUILD_BUG_ON(sizeof(struct ip6_hbh) > MIN_EXTHDR_SIZE); 217 BUILD_BUG_ON(sizeof(struct ip6_dest) > MIN_EXTHDR_SIZE); 218 BUILD_BUG_ON(sizeof(struct ip6_frag) > MIN_EXTHDR_SIZE); 219 220 /* same size for HBH and Fragment extension header types */ 221 optlen = MIN_EXTHDR_SIZE; 222 opt_ipproto_off = ETH_HLEN + sizeof(struct ipv6hdr) 223 + offsetof(struct ip6_ext, ip6e_nxt); 224 } 225 226 /* this filter validates the following: 227 * - packet is IPv4/IPv6 according to the running test. 228 * - packet is TCP. Also handles the case of one extension header and then TCP. 229 * - checks the packet tcp dport equals to DPORT. Also handles the case of one 230 * extension header and then TCP. 231 */ 232 struct sock_filter filter[] = { 233 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, ethproto_off), 234 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ntohs(ethhdr_proto), 0, 9), 235 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, ipproto_off), 236 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 2, 0), 237 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, opt_ipproto_off), 238 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_TCP, 0, 5), 239 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off), 240 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 2, 0), 241 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, dport_off + optlen), 242 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DPORT, 0, 1), 243 BPF_STMT(BPF_RET + BPF_K, 0xFFFFFFFF), 244 BPF_STMT(BPF_RET + BPF_K, 0), 245 }; 246 247 struct sock_fprog bpf = { 248 .len = ARRAY_SIZE(filter), 249 .filter = filter, 250 }; 251 252 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)) < 0) 253 error(1, errno, "error setting filter"); 254 } 255 256 static uint32_t checksum_nofold(void *data, size_t len, uint32_t sum) 257 { 258 uint16_t *words = data; 259 int i; 260 261 for (i = 0; i < len / 2; i++) 262 sum += words[i]; 263 if (len & 1) 264 sum += ((char *)data)[len - 1]; 265 return sum; 266 } 267 268 static uint16_t checksum_fold(void *data, size_t len, uint32_t sum) 269 { 270 sum = checksum_nofold(data, len, sum); 271 while (sum > 0xFFFF) 272 sum = (sum & 0xFFFF) + (sum >> 16); 273 return ~sum; 274 } 275 276 static uint16_t tcp_checksum(void *buf, int payload_len) 277 { 278 struct pseudo_header6 { 279 struct in6_addr saddr; 280 struct in6_addr daddr; 281 uint16_t protocol; 282 uint16_t payload_len; 283 } ph6; 284 struct pseudo_header4 { 285 struct in_addr saddr; 286 struct in_addr daddr; 287 uint16_t protocol; 288 uint16_t payload_len; 289 } ph4; 290 uint32_t sum = 0; 291 292 if (proto == PF_INET6) { 293 if (inet_pton(AF_INET6, addr6_src, &ph6.saddr) != 1) 294 error(1, errno, "inet_pton6 source ip pseudo"); 295 if (inet_pton(AF_INET6, addr6_dst, &ph6.daddr) != 1) 296 error(1, errno, "inet_pton6 dest ip pseudo"); 297 ph6.protocol = htons(IPPROTO_TCP); 298 ph6.payload_len = htons(sizeof(struct tcphdr) + payload_len); 299 300 sum = checksum_nofold(&ph6, sizeof(ph6), 0); 301 } else if (proto == PF_INET) { 302 if (inet_pton(AF_INET, addr4_src, &ph4.saddr) != 1) 303 error(1, errno, "inet_pton source ip pseudo"); 304 if (inet_pton(AF_INET, addr4_dst, &ph4.daddr) != 1) 305 error(1, errno, "inet_pton dest ip pseudo"); 306 ph4.protocol = htons(IPPROTO_TCP); 307 ph4.payload_len = htons(sizeof(struct tcphdr) + payload_len); 308 309 sum = checksum_nofold(&ph4, sizeof(ph4), 0); 310 } 311 312 return checksum_fold(buf, sizeof(struct tcphdr) + payload_len, sum); 313 } 314 315 static void read_MAC(uint8_t *mac_addr, char *mac) 316 { 317 if (sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", 318 &mac_addr[0], &mac_addr[1], &mac_addr[2], 319 &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) 320 error(1, 0, "sscanf"); 321 } 322 323 static void fill_datalinklayer(void *buf) 324 { 325 struct ethhdr *eth = buf; 326 327 memcpy(eth->h_dest, dst_mac, ETH_ALEN); 328 memcpy(eth->h_source, src_mac, ETH_ALEN); 329 eth->h_proto = ethhdr_proto; 330 } 331 332 static void fill_networklayer(void *buf, int payload_len, int protocol) 333 { 334 struct ipv6hdr *ip6h = buf; 335 struct iphdr *iph = buf; 336 337 if (proto == PF_INET6) { 338 memset(ip6h, 0, sizeof(*ip6h)); 339 340 ip6h->version = 6; 341 ip6h->payload_len = htons(sizeof(struct tcphdr) + payload_len); 342 ip6h->nexthdr = protocol; 343 ip6h->hop_limit = 8; 344 if (inet_pton(AF_INET6, addr6_src, &ip6h->saddr) != 1) 345 error(1, errno, "inet_pton source ip6"); 346 if (inet_pton(AF_INET6, addr6_dst, &ip6h->daddr) != 1) 347 error(1, errno, "inet_pton dest ip6"); 348 } else if (proto == PF_INET) { 349 memset(iph, 0, sizeof(*iph)); 350 351 iph->version = 4; 352 iph->ihl = 5; 353 iph->ttl = 8; 354 iph->protocol = protocol; 355 iph->tot_len = htons(sizeof(struct tcphdr) + 356 payload_len + sizeof(struct iphdr)); 357 iph->frag_off = htons(0x4000); /* DF = 1, MF = 0 */ 358 if (inet_pton(AF_INET, addr4_src, &iph->saddr) != 1) 359 error(1, errno, "inet_pton source ip"); 360 if (inet_pton(AF_INET, addr4_dst, &iph->daddr) != 1) 361 error(1, errno, "inet_pton dest ip"); 362 iph->check = checksum_fold(buf, sizeof(struct iphdr), 0); 363 } 364 } 365 366 static void fill_transportlayer(void *buf, int seq_offset, int ack_offset, 367 int payload_len, int fin) 368 { 369 struct tcphdr *tcph = buf; 370 371 memset(tcph, 0, sizeof(*tcph)); 372 373 tcph->source = htons(SPORT); 374 tcph->dest = htons(DPORT); 375 tcph->seq = ntohl(START_SEQ + seq_offset); 376 tcph->ack_seq = ntohl(START_ACK + ack_offset); 377 tcph->ack = 1; 378 tcph->fin = fin; 379 tcph->doff = 5; 380 tcph->window = htons(TCP_MAXWIN); 381 tcph->urg_ptr = 0; 382 tcph->check = tcp_checksum(tcph, payload_len); 383 } 384 385 static void write_packet(int fd, char *buf, int len, struct sockaddr_ll *daddr) 386 { 387 char control[CMSG_SPACE(sizeof(uint64_t))]; 388 struct msghdr msg = {}; 389 struct iovec iov = {}; 390 struct cmsghdr *cm; 391 int ret = -1; 392 393 iov.iov_base = buf; 394 iov.iov_len = len; 395 396 msg.msg_iov = &iov; 397 msg.msg_iovlen = 1; 398 msg.msg_name = daddr; 399 msg.msg_namelen = sizeof(*daddr); 400 401 if (txtime_ns) { 402 memset(control, 0, sizeof(control)); 403 msg.msg_control = control; 404 msg.msg_controllen = sizeof(control); 405 406 cm = CMSG_FIRSTHDR(&msg); 407 cm->cmsg_level = SOL_SOCKET; 408 cm->cmsg_type = SCM_TXTIME; 409 cm->cmsg_len = CMSG_LEN(sizeof(uint64_t)); 410 memcpy(CMSG_DATA(cm), &txtime_ns, sizeof(txtime_ns)); 411 } 412 413 ret = sendmsg(fd, &msg, 0); 414 if (ret == -1) 415 error(1, errno, "sendmsg failure"); 416 if (ret != len) 417 error(1, 0, "sendmsg wrong length: %d vs %d", ret, len); 418 } 419 420 static void create_packet(void *buf, int seq_offset, int ack_offset, 421 int payload_len, int fin) 422 { 423 int ip_hdr_len = (proto == PF_INET) ? 424 sizeof(struct iphdr) : sizeof(struct ipv6hdr); 425 int inner_ip_off = tcp_offset - ip_hdr_len; 426 427 memset(buf, 0, total_hdr_len); 428 memset(buf + total_hdr_len, 'a', payload_len); 429 430 fill_transportlayer(buf + tcp_offset, seq_offset, ack_offset, 431 payload_len, fin); 432 433 fill_networklayer(buf + inner_ip_off, payload_len, IPPROTO_TCP); 434 if (inner_ip_off > ETH_HLEN) { 435 if (pppoe) { 436 fill_pppoelayer(buf + ETH_HLEN, payload_len + ip_hdr_len, 0x1234); 437 } else { 438 int encap_proto = (proto == PF_INET) ? 439 IPPROTO_IPIP : IPPROTO_IPV6; 440 441 fill_networklayer(buf + ETH_HLEN, 442 payload_len + ip_hdr_len, encap_proto); 443 } 444 } 445 446 fill_datalinklayer(buf); 447 } 448 449 static void create_capacity_packet(void *buf, int flow_id, int pkt_idx, int psh) 450 { 451 int seq_offset = pkt_idx * CAPACITY_PAYLOAD_LEN; 452 struct tcphdr *tcph; 453 454 create_packet(buf, seq_offset, 0, CAPACITY_PAYLOAD_LEN, 0); 455 456 /* Customize for this flow id */ 457 memset(buf + total_hdr_len, 'a' + flow_id, CAPACITY_PAYLOAD_LEN); 458 459 tcph = buf + tcp_offset; 460 tcph->source = htons(SPORT + flow_id); 461 tcph->psh = psh; 462 tcph->check = 0; 463 tcph->check = tcp_checksum(tcph, CAPACITY_PAYLOAD_LEN); 464 } 465 466 /* Send a capacity test, 2 packets per flow, all first packets then all second: 467 * A1 B1 C1 D1 ... A2 B2 C2 D2 ... 468 */ 469 static void send_capacity(int fd, struct sockaddr_ll *daddr) 470 { 471 static char buf[MAX_HDR_LEN + CAPACITY_PAYLOAD_LEN]; 472 int pkt_size = total_hdr_len + CAPACITY_PAYLOAD_LEN; 473 int i; 474 475 /* Send first packet of each flow (no PSH) */ 476 for (i = 0; i < num_flows; i++) { 477 create_capacity_packet(buf, i, 0, 0); 478 write_packet(fd, buf, pkt_size, daddr); 479 } 480 481 /* Send second packet of each flow (with PSH to flush) */ 482 for (i = 0; i < num_flows; i++) { 483 create_capacity_packet(buf, i, 1, 1); 484 write_packet(fd, buf, pkt_size, daddr); 485 } 486 } 487 488 #ifndef TH_CWR 489 #define TH_CWR 0x80 490 #endif 491 static void set_flags(struct tcphdr *tcph, int payload_len, int psh, int syn, 492 int rst, int urg, int cwr) 493 { 494 tcph->psh = psh; 495 tcph->syn = syn; 496 tcph->rst = rst; 497 tcph->urg = urg; 498 if (cwr) 499 tcph->th_flags |= TH_CWR; 500 else 501 tcph->th_flags &= ~TH_CWR; 502 tcph->check = 0; 503 tcph->check = tcp_checksum(tcph, payload_len); 504 } 505 506 /* send extra flags of the (NUM_PACKETS / 2) and (NUM_PACKETS / 2 - 1) 507 * pkts, not first and not last pkt 508 */ 509 static void send_flags(int fd, struct sockaddr_ll *daddr, int psh, int syn, 510 int rst, int urg, int cwr) 511 { 512 static char flag_buf[2][MAX_HDR_LEN + PAYLOAD_LEN]; 513 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 514 int payload_len, pkt_size, i; 515 struct tcphdr *tcph; 516 int flag[2]; 517 518 payload_len = PAYLOAD_LEN * (psh || cwr); 519 pkt_size = total_hdr_len + payload_len; 520 flag[0] = NUM_PACKETS / 2; 521 flag[1] = NUM_PACKETS / 2 - 1; 522 523 /* Create and configure packets with flags 524 */ 525 for (i = 0; i < 2; i++) { 526 if (flag[i] > 0) { 527 create_packet(flag_buf[i], flag[i] * payload_len, 0, 528 payload_len, 0); 529 tcph = (struct tcphdr *)(flag_buf[i] + tcp_offset); 530 set_flags(tcph, payload_len, psh, syn, rst, urg, cwr); 531 } 532 } 533 534 for (i = 0; i < NUM_PACKETS + 1; i++) { 535 if (i == flag[0]) { 536 write_packet(fd, flag_buf[0], pkt_size, daddr); 537 continue; 538 } else if (i == flag[1] && cwr) { 539 write_packet(fd, flag_buf[1], pkt_size, daddr); 540 continue; 541 } 542 create_packet(buf, i * PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 543 write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr); 544 } 545 } 546 547 /* Test for data of same length, smaller than previous 548 * and of different lengths 549 */ 550 static void send_data_pkts(int fd, struct sockaddr_ll *daddr, 551 int payload_len1, int payload_len2) 552 { 553 static char buf[L2_HLEN_MAX + IP_MAXPACKET]; 554 555 create_packet(buf, 0, 0, payload_len1, 0); 556 write_packet(fd, buf, total_hdr_len + payload_len1, daddr); 557 create_packet(buf, payload_len1, 0, payload_len2, 0); 558 write_packet(fd, buf, total_hdr_len + payload_len2, daddr); 559 } 560 561 /* If incoming segments make tracked segment length exceed 562 * legal IP datagram length, do not coalesce 563 */ 564 static void send_large(int fd, struct sockaddr_ll *daddr, int remainder) 565 { 566 static char pkts[MAX_LARGE_PKT_CNT][MAX_HDR_LEN + MAX_MSS]; 567 static char new_seg[MAX_HDR_LEN + MAX_MSS]; 568 static char last[MAX_HDR_LEN + MAX_MSS]; 569 const int num_pkt = num_large_pkt(); 570 const int mss = calc_mss(); 571 int i; 572 573 for (i = 0; i < num_pkt; i++) 574 create_packet(pkts[i], i * mss, 0, mss, 0); 575 create_packet(last, num_pkt * mss, 0, remainder, 0); 576 create_packet(new_seg, (num_pkt + 1) * mss, 0, remainder, 0); 577 578 for (i = 0; i < num_pkt; i++) 579 write_packet(fd, pkts[i], total_hdr_len + mss, daddr); 580 write_packet(fd, last, total_hdr_len + remainder, daddr); 581 write_packet(fd, new_seg, total_hdr_len + remainder, daddr); 582 } 583 584 /* Pure acks and dup acks don't coalesce */ 585 static void send_ack(int fd, struct sockaddr_ll *daddr) 586 { 587 static char buf[MAX_HDR_LEN]; 588 589 create_packet(buf, 0, 0, 0, 0); 590 write_packet(fd, buf, total_hdr_len, daddr); 591 write_packet(fd, buf, total_hdr_len, daddr); 592 create_packet(buf, 0, 1, 0, 0); 593 write_packet(fd, buf, total_hdr_len, daddr); 594 } 595 596 static void recompute_packet(char *buf, char *no_ext, int extlen) 597 { 598 struct tcphdr *tcphdr = (struct tcphdr *)(buf + tcp_offset); 599 int off; 600 601 memmove(buf, no_ext, total_hdr_len); 602 memmove(buf + total_hdr_len + extlen, 603 no_ext + total_hdr_len, PAYLOAD_LEN); 604 605 tcphdr->doff = tcphdr->doff + (extlen / 4); 606 tcphdr->check = 0; 607 tcphdr->check = tcp_checksum(tcphdr, PAYLOAD_LEN + extlen); 608 if (proto == PF_INET) { 609 for (off = ETH_HLEN; off < tcp_offset; 610 off += sizeof(struct iphdr)) { 611 struct iphdr *iph = (struct iphdr *)(buf + off); 612 613 iph->tot_len = htons(ntohs(iph->tot_len) + extlen); 614 iph->check = 0; 615 iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 616 } 617 } else { 618 for (off = ETH_HLEN; off < tcp_offset; 619 off += sizeof(struct ipv6hdr)) { 620 struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + off); 621 622 ip6h->payload_len = 623 htons(ntohs(ip6h->payload_len) + extlen); 624 } 625 } 626 } 627 628 static void tcp_write_options(char *buf, int kind, int ts) 629 { 630 struct tcp_option_ts { 631 uint8_t kind; 632 uint8_t len; 633 uint32_t tsval; 634 uint32_t tsecr; 635 } *opt_ts = (void *)buf; 636 struct tcp_option_window { 637 uint8_t kind; 638 uint8_t len; 639 uint8_t shift; 640 } *opt_window = (void *)buf; 641 642 switch (kind) { 643 case TCPOPT_NOP: 644 buf[0] = TCPOPT_NOP; 645 break; 646 case TCPOPT_WINDOW: 647 memset(opt_window, 0, sizeof(struct tcp_option_window)); 648 opt_window->kind = TCPOPT_WINDOW; 649 opt_window->len = TCPOLEN_WINDOW; 650 opt_window->shift = 0; 651 break; 652 case TCPOPT_TIMESTAMP: 653 memset(opt_ts, 0, sizeof(struct tcp_option_ts)); 654 opt_ts->kind = TCPOPT_TIMESTAMP; 655 opt_ts->len = TCPOLEN_TIMESTAMP; 656 opt_ts->tsval = ts; 657 opt_ts->tsecr = 0; 658 break; 659 default: 660 error(1, 0, "unimplemented TCP option"); 661 break; 662 } 663 } 664 665 /* TCP with options is always a permutation of {TS, NOP, NOP}. 666 * Implement different orders to verify coalescing stops. 667 */ 668 static void add_standard_tcp_options(char *buf, char *no_ext, int ts, int order) 669 { 670 switch (order) { 671 case 0: 672 tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0); 673 tcp_write_options(buf + total_hdr_len + 1, TCPOPT_NOP, 0); 674 tcp_write_options(buf + total_hdr_len + 2 /* two NOP opts */, 675 TCPOPT_TIMESTAMP, ts); 676 break; 677 case 1: 678 tcp_write_options(buf + total_hdr_len, TCPOPT_NOP, 0); 679 tcp_write_options(buf + total_hdr_len + 1, 680 TCPOPT_TIMESTAMP, ts); 681 tcp_write_options(buf + total_hdr_len + 1 + TCPOLEN_TIMESTAMP, 682 TCPOPT_NOP, 0); 683 break; 684 case 2: 685 tcp_write_options(buf + total_hdr_len, TCPOPT_TIMESTAMP, ts); 686 tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 1, 687 TCPOPT_NOP, 0); 688 tcp_write_options(buf + total_hdr_len + TCPOLEN_TIMESTAMP + 2, 689 TCPOPT_NOP, 0); 690 break; 691 default: 692 error(1, 0, "unknown order"); 693 break; 694 } 695 recompute_packet(buf, no_ext, TCPOLEN_TSTAMP_APPA); 696 } 697 698 /* Packets with invalid checksum don't coalesce. */ 699 static void send_changed_checksum(int fd, struct sockaddr_ll *daddr) 700 { 701 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 702 struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset); 703 int pkt_size = total_hdr_len + PAYLOAD_LEN; 704 705 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 706 write_packet(fd, buf, pkt_size, daddr); 707 708 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 709 tcph->check = tcph->check - 1; 710 write_packet(fd, buf, pkt_size, daddr); 711 } 712 713 /* Packets with incorrect IPv4 header checksum don't coalesce. */ 714 static void send_changed_ip_checksum(int fd, struct sockaddr_ll *daddr) 715 { 716 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 717 struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 718 int pkt_size = total_hdr_len + PAYLOAD_LEN; 719 720 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 721 write_packet(fd, buf, pkt_size, daddr); 722 723 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 724 iph->check = iph->check - 1; 725 write_packet(fd, buf, pkt_size, daddr); 726 727 create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 728 write_packet(fd, buf, pkt_size, daddr); 729 } 730 731 /* Packets with non-consecutive sequence number don't coalesce.*/ 732 static void send_changed_seq(int fd, struct sockaddr_ll *daddr) 733 { 734 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 735 struct tcphdr *tcph = (struct tcphdr *)(buf + tcp_offset); 736 int pkt_size = total_hdr_len + PAYLOAD_LEN; 737 738 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 739 write_packet(fd, buf, pkt_size, daddr); 740 741 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 742 tcph->seq = ntohl(htonl(tcph->seq) + 1); 743 tcph->check = 0; 744 tcph->check = tcp_checksum(tcph, PAYLOAD_LEN); 745 write_packet(fd, buf, pkt_size, daddr); 746 } 747 748 /* Packet with different timestamp option or different timestamps 749 * don't coalesce. 750 */ 751 static void send_changed_ts(int fd, struct sockaddr_ll *daddr) 752 { 753 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 754 static char extpkt[sizeof(buf) + TCPOLEN_TSTAMP_APPA]; 755 int pkt_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA; 756 757 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 758 add_standard_tcp_options(extpkt, buf, 0, 0); 759 write_packet(fd, extpkt, pkt_size, daddr); 760 761 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 762 add_standard_tcp_options(extpkt, buf, 0, 0); 763 write_packet(fd, extpkt, pkt_size, daddr); 764 765 create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 766 add_standard_tcp_options(extpkt, buf, 100, 0); 767 write_packet(fd, extpkt, pkt_size, daddr); 768 769 create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0); 770 add_standard_tcp_options(extpkt, buf, 100, 1); 771 write_packet(fd, extpkt, pkt_size, daddr); 772 773 create_packet(buf, PAYLOAD_LEN * 4, 0, PAYLOAD_LEN, 0); 774 add_standard_tcp_options(extpkt, buf, 100, 2); 775 write_packet(fd, extpkt, pkt_size, daddr); 776 } 777 778 /* Packet with different tcp options don't coalesce. */ 779 static void send_diff_opt(int fd, struct sockaddr_ll *daddr) 780 { 781 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 782 static char extpkt1[sizeof(buf) + TCPOLEN_TSTAMP_APPA]; 783 static char extpkt2[sizeof(buf) + TCPOLEN_MAXSEG]; 784 int extpkt1_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_TSTAMP_APPA; 785 int extpkt2_size = total_hdr_len + PAYLOAD_LEN + TCPOLEN_MAXSEG; 786 787 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 788 add_standard_tcp_options(extpkt1, buf, 0, 0); 789 write_packet(fd, extpkt1, extpkt1_size, daddr); 790 791 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 792 add_standard_tcp_options(extpkt1, buf, 0, 0); 793 write_packet(fd, extpkt1, extpkt1_size, daddr); 794 795 create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 796 tcp_write_options(extpkt2 + MAX_HDR_LEN, TCPOPT_NOP, 0); 797 tcp_write_options(extpkt2 + MAX_HDR_LEN + 1, TCPOPT_WINDOW, 0); 798 recompute_packet(extpkt2, buf, TCPOLEN_WINDOW + 1); 799 write_packet(fd, extpkt2, extpkt2_size, daddr); 800 } 801 802 static void add_ipv4_ts_option(void *buf, void *optpkt) 803 { 804 struct ip_timestamp *ts = (struct ip_timestamp *)(optpkt + tcp_offset); 805 int optlen = sizeof(struct ip_timestamp); 806 struct iphdr *iph; 807 808 if (optlen % 4) 809 error(1, 0, "ipv4 timestamp length is not a multiple of 4B"); 810 811 ts->ipt_code = IPOPT_TS; 812 ts->ipt_len = optlen; 813 ts->ipt_ptr = 5; 814 ts->ipt_flg = IPOPT_TS_TSONLY; 815 816 memcpy(optpkt, buf, tcp_offset); 817 memcpy(optpkt + tcp_offset + optlen, buf + tcp_offset, 818 sizeof(struct tcphdr) + PAYLOAD_LEN); 819 820 iph = (struct iphdr *)(optpkt + ETH_HLEN); 821 iph->ihl = 5 + (optlen / 4); 822 iph->tot_len = htons(ntohs(iph->tot_len) + optlen); 823 iph->check = 0; 824 iph->check = checksum_fold(iph, sizeof(struct iphdr) + optlen, 0); 825 } 826 827 static void add_ipv6_exthdr(void *buf, void *optpkt, __u8 exthdr_type, char *ext_payload) 828 { 829 struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr *)(optpkt + tcp_offset); 830 struct ipv6hdr *iph = (struct ipv6hdr *)(optpkt + ETH_HLEN); 831 char *exthdr_payload_start = (char *)(exthdr + 1); 832 833 exthdr->hdrlen = 0; 834 exthdr->nexthdr = IPPROTO_TCP; 835 836 memcpy(exthdr_payload_start, ext_payload, MIN_EXTHDR_SIZE - sizeof(*exthdr)); 837 838 memcpy(optpkt, buf, tcp_offset); 839 memcpy(optpkt + tcp_offset + MIN_EXTHDR_SIZE, buf + tcp_offset, 840 sizeof(struct tcphdr) + PAYLOAD_LEN); 841 842 iph->nexthdr = exthdr_type; 843 iph->payload_len = htons(ntohs(iph->payload_len) + MIN_EXTHDR_SIZE); 844 } 845 846 static void fix_ip4_checksum(struct iphdr *iph) 847 { 848 iph->check = 0; 849 iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 850 } 851 852 static void send_flush_id_case(int fd, struct sockaddr_ll *daddr, 853 enum flush_id_case tcase) 854 { 855 static char buf1[MAX_HDR_LEN + PAYLOAD_LEN]; 856 static char buf2[MAX_HDR_LEN + PAYLOAD_LEN]; 857 static char buf3[MAX_HDR_LEN + PAYLOAD_LEN]; 858 bool send_three = false; 859 struct iphdr *iph1; 860 struct iphdr *iph2; 861 struct iphdr *iph3; 862 863 iph1 = (struct iphdr *)(buf1 + ETH_HLEN); 864 iph2 = (struct iphdr *)(buf2 + ETH_HLEN); 865 iph3 = (struct iphdr *)(buf3 + ETH_HLEN); 866 867 create_packet(buf1, 0, 0, PAYLOAD_LEN, 0); 868 create_packet(buf2, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 869 create_packet(buf3, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 870 871 switch (tcase) { 872 case FLUSH_ID_DF1_INC: /* DF=1, Incrementing - should coalesce */ 873 iph1->frag_off |= htons(IP_DF); 874 iph1->id = htons(8); 875 876 iph2->frag_off |= htons(IP_DF); 877 iph2->id = htons(9); 878 break; 879 880 case FLUSH_ID_DF1_FIXED: /* DF=1, Fixed - should coalesce */ 881 iph1->frag_off |= htons(IP_DF); 882 iph1->id = htons(8); 883 884 iph2->frag_off |= htons(IP_DF); 885 iph2->id = htons(8); 886 break; 887 888 case FLUSH_ID_DF0_INC: /* DF=0, Incrementing - should coalesce */ 889 iph1->frag_off &= ~htons(IP_DF); 890 iph1->id = htons(8); 891 892 iph2->frag_off &= ~htons(IP_DF); 893 iph2->id = htons(9); 894 break; 895 896 case FLUSH_ID_DF0_FIXED: /* DF=0, Fixed - should coalesce */ 897 iph1->frag_off &= ~htons(IP_DF); 898 iph1->id = htons(8); 899 900 iph2->frag_off &= ~htons(IP_DF); 901 iph2->id = htons(8); 902 break; 903 904 case FLUSH_ID_DF1_INC_FIXED: /* DF=1, two packets incrementing, and 905 * one fixed - should coalesce only the 906 * first two packets 907 */ 908 iph1->frag_off |= htons(IP_DF); 909 iph1->id = htons(8); 910 911 iph2->frag_off |= htons(IP_DF); 912 iph2->id = htons(9); 913 914 iph3->frag_off |= htons(IP_DF); 915 iph3->id = htons(9); 916 send_three = true; 917 break; 918 919 case FLUSH_ID_DF1_FIXED_INC: /* DF=1, two packets fixed, and one 920 * incrementing - should coalesce only 921 * the first two packets 922 */ 923 iph1->frag_off |= htons(IP_DF); 924 iph1->id = htons(8); 925 926 iph2->frag_off |= htons(IP_DF); 927 iph2->id = htons(8); 928 929 iph3->frag_off |= htons(IP_DF); 930 iph3->id = htons(9); 931 send_three = true; 932 break; 933 } 934 935 fix_ip4_checksum(iph1); 936 fix_ip4_checksum(iph2); 937 write_packet(fd, buf1, total_hdr_len + PAYLOAD_LEN, daddr); 938 write_packet(fd, buf2, total_hdr_len + PAYLOAD_LEN, daddr); 939 940 if (send_three) { 941 fix_ip4_checksum(iph3); 942 write_packet(fd, buf3, total_hdr_len + PAYLOAD_LEN, daddr); 943 } 944 } 945 946 static void send_ipv6_exthdr(int fd, struct sockaddr_ll *daddr, char *ext_data1, char *ext_data2) 947 { 948 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 949 static char exthdr_pck[sizeof(buf) + MIN_EXTHDR_SIZE]; 950 951 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 952 add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_DSTOPTS, ext_data1); 953 write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr); 954 955 create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0); 956 add_ipv6_exthdr(buf, exthdr_pck, IPPROTO_DSTOPTS, ext_data2); 957 write_packet(fd, exthdr_pck, total_hdr_len + PAYLOAD_LEN + MIN_EXTHDR_SIZE, daddr); 958 } 959 960 /* IPv4 options shouldn't coalesce */ 961 static void send_ip_options(int fd, struct sockaddr_ll *daddr) 962 { 963 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 964 static char optpkt[sizeof(buf) + sizeof(struct ip_timestamp)]; 965 int optlen = sizeof(struct ip_timestamp); 966 int pkt_size = total_hdr_len + PAYLOAD_LEN + optlen; 967 968 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 969 write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr); 970 971 create_packet(buf, PAYLOAD_LEN * 1, 0, PAYLOAD_LEN, 0); 972 add_ipv4_ts_option(buf, optpkt); 973 write_packet(fd, optpkt, pkt_size, daddr); 974 975 create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 976 write_packet(fd, buf, total_hdr_len + PAYLOAD_LEN, daddr); 977 } 978 979 /* IPv4 fragments shouldn't coalesce */ 980 static void send_fragment4(int fd, struct sockaddr_ll *daddr) 981 { 982 static char buf[IP_MAXPACKET]; 983 struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 984 int pkt_size = total_hdr_len + PAYLOAD_LEN; 985 986 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 987 write_packet(fd, buf, pkt_size, daddr); 988 989 /* Once fragmented, packet would retain the total_len. 990 * Tcp header is prepared as if rest of data is in follow-up frags, 991 * but follow up frags aren't actually sent. 992 */ 993 memset(buf + total_hdr_len, 'a', PAYLOAD_LEN * 2); 994 fill_transportlayer(buf + tcp_offset, PAYLOAD_LEN, 0, PAYLOAD_LEN * 2, 0); 995 fill_networklayer(buf + ETH_HLEN, PAYLOAD_LEN, IPPROTO_TCP); 996 fill_datalinklayer(buf); 997 998 iph->frag_off = htons(0x6000); // DF = 1, MF = 1 999 iph->check = 0; 1000 iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 1001 write_packet(fd, buf, pkt_size, daddr); 1002 } 1003 1004 /* IPv4 packets with different ttl don't coalesce.*/ 1005 static void send_changed_ttl(int fd, struct sockaddr_ll *daddr) 1006 { 1007 int pkt_size = total_hdr_len + PAYLOAD_LEN; 1008 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1009 struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 1010 1011 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 1012 write_packet(fd, buf, pkt_size, daddr); 1013 1014 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 1015 iph->ttl = 7; 1016 iph->check = 0; 1017 iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 1018 write_packet(fd, buf, pkt_size, daddr); 1019 } 1020 1021 /* Packets with different tos don't coalesce.*/ 1022 static void send_changed_tos(int fd, struct sockaddr_ll *daddr) 1023 { 1024 int pkt_size = total_hdr_len + PAYLOAD_LEN; 1025 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1026 struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 1027 struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN); 1028 1029 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 1030 write_packet(fd, buf, pkt_size, daddr); 1031 1032 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 1033 if (proto == PF_INET) { 1034 iph->tos = 1; 1035 iph->check = 0; 1036 iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 1037 } else if (proto == PF_INET6) { 1038 ip6h->priority = 0xf; 1039 } 1040 write_packet(fd, buf, pkt_size, daddr); 1041 } 1042 1043 /* Packets with different ECN don't coalesce.*/ 1044 static void send_changed_ECN(int fd, struct sockaddr_ll *daddr) 1045 { 1046 int pkt_size = total_hdr_len + PAYLOAD_LEN; 1047 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1048 struct iphdr *iph = (struct iphdr *)(buf + ETH_HLEN); 1049 1050 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 1051 write_packet(fd, buf, pkt_size, daddr); 1052 1053 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 1054 if (proto == PF_INET) { 1055 buf[ETH_HLEN + 1] ^= 0x2; // ECN set to 10 1056 iph->check = 0; 1057 iph->check = checksum_fold(iph, sizeof(struct iphdr), 0); 1058 } else { 1059 buf[ETH_HLEN + 1] ^= 0x20; // ECN set to 10 1060 } 1061 write_packet(fd, buf, pkt_size, daddr); 1062 } 1063 1064 /* IPv6 fragments and packets with extensions don't coalesce.*/ 1065 static void send_fragment6(int fd, struct sockaddr_ll *daddr) 1066 { 1067 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1068 static char extpkt[MAX_HDR_LEN + PAYLOAD_LEN + 1069 sizeof(struct ip6_frag)]; 1070 struct ipv6hdr *ip6h = (struct ipv6hdr *)(buf + ETH_HLEN); 1071 struct ip6_frag *frag = (void *)(extpkt + tcp_offset); 1072 int extlen = sizeof(struct ip6_frag); 1073 int bufpkt_len = total_hdr_len + PAYLOAD_LEN; 1074 int extpkt_len = bufpkt_len + extlen; 1075 int i; 1076 1077 for (i = 0; i < 2; i++) { 1078 create_packet(buf, PAYLOAD_LEN * i, 0, PAYLOAD_LEN, 0); 1079 write_packet(fd, buf, bufpkt_len, daddr); 1080 } 1081 sleep(1); 1082 create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 1083 memset(extpkt, 0, extpkt_len); 1084 1085 ip6h->nexthdr = IPPROTO_FRAGMENT; 1086 ip6h->payload_len = htons(ntohs(ip6h->payload_len) + extlen); 1087 frag->ip6f_nxt = IPPROTO_TCP; 1088 1089 memcpy(extpkt, buf, tcp_offset); 1090 memcpy(extpkt + tcp_offset + extlen, buf + tcp_offset, 1091 sizeof(struct tcphdr) + PAYLOAD_LEN); 1092 write_packet(fd, extpkt, extpkt_len, daddr); 1093 1094 create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0); 1095 write_packet(fd, buf, bufpkt_len, daddr); 1096 } 1097 1098 static void send_changed_pppoe_sid(int fd, struct sockaddr_ll *daddr) 1099 { 1100 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1101 int pkt_size = total_hdr_len + PAYLOAD_LEN; 1102 struct pppoe_hdr *hdr = (struct pppoe_hdr *)(buf + ETH_HLEN); 1103 1104 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 1105 write_packet(fd, buf, pkt_size, daddr); 1106 1107 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 1108 hdr->sid = htons(0x4321); 1109 write_packet(fd, buf, pkt_size, daddr); 1110 } 1111 1112 static void bind_packetsocket(int fd) 1113 { 1114 struct sockaddr_ll daddr = {}; 1115 1116 daddr.sll_family = AF_PACKET; 1117 daddr.sll_protocol = ethhdr_proto; 1118 daddr.sll_ifindex = if_nametoindex(ifname); 1119 if (daddr.sll_ifindex == 0) 1120 error(1, errno, "if_nametoindex"); 1121 1122 if (bind(fd, (void *)&daddr, sizeof(daddr)) < 0) 1123 error(1, errno, "could not bind socket"); 1124 } 1125 1126 static void set_timeout(int fd) 1127 { 1128 struct timeval timeout; 1129 1130 timeout.tv_sec = 3; 1131 timeout.tv_usec = 0; 1132 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, 1133 sizeof(timeout)) < 0) 1134 error(1, errno, "cannot set timeout, setsockopt failed"); 1135 } 1136 1137 static void set_rcvbuf(int fd) 1138 { 1139 int bufsize = 1 * 1024 * 1024; /* 1 MB */ 1140 1141 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize))) 1142 error(1, errno, "cannot set rcvbuf size, setsockopt failed"); 1143 } 1144 1145 static void recv_error(int fd, int rcv_errno) 1146 { 1147 struct tpacket_stats stats; 1148 socklen_t len; 1149 1150 len = sizeof(stats); 1151 if (getsockopt(fd, SOL_PACKET, PACKET_STATISTICS, &stats, &len)) 1152 error(1, errno, "can't get stats"); 1153 1154 fprintf(stderr, "Socket stats: packets=%u, drops=%u\n", 1155 stats.tp_packets, stats.tp_drops); 1156 error(1, rcv_errno, "could not receive"); 1157 } 1158 1159 static void check_recv_pkts(int fd, int *correct_payload, 1160 int correct_num_pkts) 1161 { 1162 static char buffer[IP_MAXPACKET + L2_HLEN_MAX + 1]; 1163 int nhoff = ETH_HLEN + (pppoe ? PPPOE_SES_HLEN : 0); 1164 struct iphdr *iph = (struct iphdr *)(buffer + nhoff); 1165 struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + nhoff); 1166 struct tcphdr *tcph; 1167 bool bad_packet = false; 1168 int tcp_ext_len = 0; 1169 int ip_ext_len = 0; 1170 int pkt_size = -1; 1171 int data_len = 0; 1172 int num_pkt = 0; 1173 int i; 1174 1175 vlog("Expected {"); 1176 for (i = 0; i < correct_num_pkts; i++) 1177 vlog("%d ", correct_payload[i]); 1178 vlog("}, Total %d packets\nReceived {", correct_num_pkts); 1179 1180 while (1) { 1181 ip_ext_len = 0; 1182 pkt_size = recv(fd, buffer, sizeof(buffer), 0); 1183 if (pkt_size < 0) 1184 recv_error(fd, errno); 1185 1186 if (iph->version == 4) 1187 ip_ext_len = (iph->ihl - 5) * 4; 1188 else if (ip6h->version == 6 && !ip6ip6 && 1189 ip6h->nexthdr != IPPROTO_TCP) 1190 ip_ext_len = MIN_EXTHDR_SIZE; 1191 1192 tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len); 1193 1194 if (tcph->fin) 1195 break; 1196 1197 tcp_ext_len = (tcph->doff - 5) * 4; 1198 data_len = pkt_size - total_hdr_len - tcp_ext_len - ip_ext_len; 1199 /* Min ethernet frame payload is 46(ETH_ZLEN - ETH_HLEN) by RFC 802.3. 1200 * Ipv4/tcp packets without at least 6 bytes of data will be padded. 1201 * Packet sockets are protocol agnostic, and will not trim the padding. 1202 */ 1203 if (pkt_size == ETH_ZLEN && iph->version == 4) { 1204 data_len = ntohs(iph->tot_len) 1205 - sizeof(struct tcphdr) - sizeof(struct iphdr); 1206 } 1207 vlog("%d ", data_len); 1208 if (data_len != correct_payload[num_pkt]) { 1209 vlog("[!=%d]", correct_payload[num_pkt]); 1210 bad_packet = true; 1211 } 1212 num_pkt++; 1213 } 1214 vlog("}, Total %d packets.\n", num_pkt); 1215 if (num_pkt != correct_num_pkts) 1216 error(1, 0, "incorrect number of packets"); 1217 if (bad_packet) 1218 error(1, 0, "incorrect packet geometry"); 1219 1220 printf("Test succeeded\n\n"); 1221 } 1222 1223 static void check_capacity_pkts(int fd) 1224 { 1225 static char buffer[IP_MAXPACKET + L2_HLEN_MAX + 1]; 1226 int nhoff = ETH_HLEN + (pppoe ? PPPOE_SES_HLEN : 0); 1227 struct iphdr *iph = (struct iphdr *)(buffer + nhoff); 1228 struct ipv6hdr *ip6h = (struct ipv6hdr *)(buffer + nhoff); 1229 int num_pkt = 0, num_coal = 0, pkt_idx; 1230 const char *fail_reason = NULL; 1231 int flow_order[num_flows * 2]; 1232 int coalesced[num_flows]; 1233 struct tcphdr *tcph; 1234 int ip_ext_len = 0; 1235 int total_data = 0; 1236 int pkt_size = -1; 1237 int data_len = 0; 1238 int flow_id; 1239 int sport; 1240 1241 memset(coalesced, 0, sizeof(coalesced)); 1242 memset(flow_order, -1, sizeof(flow_order)); 1243 1244 while (1) { 1245 ip_ext_len = 0; 1246 pkt_size = recv(fd, buffer, sizeof(buffer), 0); 1247 if (pkt_size < 0) 1248 recv_error(fd, errno); 1249 1250 if (iph->version == 4) 1251 ip_ext_len = (iph->ihl - 5) * 4; 1252 else if (ip6h->version == 6 && !ip6ip6 && 1253 ip6h->nexthdr != IPPROTO_TCP) 1254 ip_ext_len = MIN_EXTHDR_SIZE; 1255 1256 tcph = (struct tcphdr *)(buffer + tcp_offset + ip_ext_len); 1257 1258 if (tcph->fin) 1259 break; 1260 1261 sport = ntohs(tcph->source); 1262 flow_id = sport - SPORT; 1263 1264 if (flow_id < 0 || flow_id >= num_flows) { 1265 vlog("Invalid flow_id %d from sport %d\n", 1266 flow_id, sport); 1267 fail_reason = fail_reason ?: "invalid packet"; 1268 continue; 1269 } 1270 1271 /* Calculate payload length */ 1272 if (pkt_size == ETH_ZLEN && iph->version == 4) { 1273 data_len = ntohs(iph->tot_len) 1274 - sizeof(struct tcphdr) - sizeof(struct iphdr); 1275 } else { 1276 data_len = pkt_size - total_hdr_len - ip_ext_len; 1277 } 1278 1279 if (num_pkt < num_flows * 2) { 1280 flow_order[num_pkt] = flow_id; 1281 } else if (num_pkt == num_flows * 2) { 1282 vlog("More packets than expected (%d)\n", 1283 num_flows * 2); 1284 fail_reason = fail_reason ?: "too many packets"; 1285 } 1286 coalesced[flow_id] = data_len; 1287 1288 if (data_len == CAPACITY_PAYLOAD_LEN * 2) { 1289 num_coal++; 1290 } else { 1291 vlog("Pkt %d: flow %d, sport %d, len %d (expected %d)\n", 1292 num_pkt, flow_id, sport, data_len, 1293 CAPACITY_PAYLOAD_LEN * 2); 1294 fail_reason = fail_reason ?: "not coalesced"; 1295 } 1296 1297 num_pkt++; 1298 total_data += data_len; 1299 } 1300 1301 /* Check flow ordering. We expect to see all non-coalesced first segs 1302 * then interleaved coalesced and non-coalesced second frames. 1303 */ 1304 pkt_idx = 0; 1305 for (flow_id = 0; order_check && flow_id < num_flows; flow_id++) { 1306 bool coaled = coalesced[flow_id] > CAPACITY_PAYLOAD_LEN; 1307 1308 if (coaled) 1309 continue; 1310 1311 if (flow_order[pkt_idx] != flow_id) { 1312 vlog("Flow order mismatch (non-coalesced) at position %d: expected flow %d, got flow %d\n", 1313 pkt_idx, flow_id, flow_order[pkt_idx]); 1314 fail_reason = fail_reason ?: "bad packet order (1)"; 1315 } 1316 pkt_idx++; 1317 } 1318 for (flow_id = 0; order_check && flow_id < num_flows; flow_id++) { 1319 bool coaled = coalesced[flow_id] > CAPACITY_PAYLOAD_LEN; 1320 1321 if (flow_order[pkt_idx] != flow_id) { 1322 vlog("Flow order mismatch at position %d: expected flow %d, got flow %d, coalesced: %d\n", 1323 pkt_idx, flow_id, flow_order[pkt_idx], coaled); 1324 fail_reason = fail_reason ?: "bad packet order (2)"; 1325 } 1326 pkt_idx++; 1327 } 1328 1329 if (!fail_reason) { 1330 vlog("All %d flows coalesced correctly\n", num_flows); 1331 printf("Test succeeded\n\n"); 1332 } else { 1333 printf("FAILED\n"); 1334 } 1335 1336 /* Always print stats for external validation */ 1337 printf("STATS: received=%d wire=%d coalesced=%d\n", 1338 num_pkt, num_pkt + num_coal, num_coal); 1339 1340 if (fail_reason) 1341 error(1, 0, "capacity test failed %s", fail_reason); 1342 } 1343 1344 static void gro_sender(void) 1345 { 1346 int bufsize = 4 * 1024 * 1024; /* 4 MB */ 1347 const int fin_delay_us = 100 * 1000; 1348 static char fin_pkt[MAX_HDR_LEN]; 1349 struct sockaddr_ll daddr = {}; 1350 int txfd = -1; 1351 1352 txfd = socket(PF_PACKET, SOCK_RAW, IPPROTO_RAW); 1353 if (txfd < 0) 1354 error(1, errno, "socket creation"); 1355 1356 if (setsockopt(txfd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize))) 1357 error(1, errno, "cannot set sndbuf size, setsockopt failed"); 1358 1359 /* Enable SO_TXTIME unless test case generates more than one flow 1360 * SO_TXTIME could result in qdisc layer sorting the packets at sender. 1361 */ 1362 if (strcmp(testname, "single") && strcmp(testname, "capacity")) { 1363 struct sock_txtime so_txtime = { .clockid = CLOCK_MONOTONIC, }; 1364 struct timespec ts; 1365 1366 if (setsockopt(txfd, SOL_SOCKET, SO_TXTIME, 1367 &so_txtime, sizeof(so_txtime))) 1368 error(1, errno, "setsockopt SO_TXTIME"); 1369 1370 if (clock_gettime(CLOCK_MONOTONIC, &ts)) 1371 error(1, errno, "clock_gettime"); 1372 1373 txtime_ns = ts.tv_sec * 1000000000ULL + ts.tv_nsec; 1374 txtime_ns += TXTIME_DELAY_MS * 1000000ULL; 1375 } 1376 1377 memset(&daddr, 0, sizeof(daddr)); 1378 daddr.sll_ifindex = if_nametoindex(ifname); 1379 if (daddr.sll_ifindex == 0) 1380 error(1, errno, "if_nametoindex"); 1381 daddr.sll_family = AF_PACKET; 1382 memcpy(daddr.sll_addr, dst_mac, ETH_ALEN); 1383 daddr.sll_halen = ETH_ALEN; 1384 create_packet(fin_pkt, PAYLOAD_LEN * 2, 0, 0, 1); 1385 1386 /* data sub-tests */ 1387 if (strcmp(testname, "data_same") == 0) { 1388 send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN); 1389 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1390 } else if (strcmp(testname, "data_lrg_sml") == 0) { 1391 send_data_pkts(txfd, &daddr, PAYLOAD_LEN, PAYLOAD_LEN / 2); 1392 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1393 } else if (strcmp(testname, "data_lrg_1byte") == 0) { 1394 send_data_pkts(txfd, &daddr, PAYLOAD_LEN, 1); 1395 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1396 } else if (strcmp(testname, "data_sml_lrg") == 0) { 1397 send_data_pkts(txfd, &daddr, PAYLOAD_LEN / 2, PAYLOAD_LEN); 1398 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1399 } else if (strcmp(testname, "data_burst") == 0) { 1400 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1401 1402 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 1403 write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr); 1404 create_packet(buf, PAYLOAD_LEN, 0, PAYLOAD_LEN, 0); 1405 write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr); 1406 1407 usleep(100 * 1000); /* 100ms */ 1408 create_packet(buf, PAYLOAD_LEN * 2, 0, PAYLOAD_LEN, 0); 1409 write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr); 1410 create_packet(buf, PAYLOAD_LEN * 3, 0, PAYLOAD_LEN, 0); 1411 write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr); 1412 1413 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1414 1415 /* ack test */ 1416 } else if (strcmp(testname, "ack") == 0) { 1417 send_ack(txfd, &daddr); 1418 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1419 1420 /* flags sub-tests */ 1421 } else if (strcmp(testname, "flags_psh") == 0) { 1422 send_flags(txfd, &daddr, 1, 0, 0, 0, 0); 1423 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1424 } else if (strcmp(testname, "flags_syn") == 0) { 1425 send_flags(txfd, &daddr, 0, 1, 0, 0, 0); 1426 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1427 } else if (strcmp(testname, "flags_rst") == 0) { 1428 send_flags(txfd, &daddr, 0, 0, 1, 0, 0); 1429 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1430 } else if (strcmp(testname, "flags_urg") == 0) { 1431 send_flags(txfd, &daddr, 0, 0, 0, 1, 0); 1432 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1433 } else if (strcmp(testname, "flags_cwr") == 0) { 1434 send_flags(txfd, &daddr, 0, 0, 0, 0, 1); 1435 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1436 1437 /* tcp sub-tests */ 1438 } else if (strcmp(testname, "tcp_csum") == 0) { 1439 send_changed_checksum(txfd, &daddr); 1440 usleep(fin_delay_us); 1441 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1442 } else if (strcmp(testname, "tcp_seq") == 0) { 1443 send_changed_seq(txfd, &daddr); 1444 usleep(fin_delay_us); 1445 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1446 } else if (strcmp(testname, "tcp_ts") == 0) { 1447 send_changed_ts(txfd, &daddr); 1448 usleep(fin_delay_us); 1449 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1450 } else if (strcmp(testname, "tcp_opt") == 0) { 1451 send_diff_opt(txfd, &daddr); 1452 usleep(fin_delay_us); 1453 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1454 1455 /* ip sub-tests - shared between IPv4 and IPv6 */ 1456 } else if (strcmp(testname, "ip_ecn") == 0) { 1457 send_changed_ECN(txfd, &daddr); 1458 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1459 } else if (strcmp(testname, "ip_tos") == 0) { 1460 send_changed_tos(txfd, &daddr); 1461 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1462 1463 /* ip sub-tests - IPv4 only */ 1464 } else if (strcmp(testname, "ip_csum") == 0) { 1465 send_changed_ip_checksum(txfd, &daddr); 1466 usleep(fin_delay_us); 1467 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1468 } else if (strcmp(testname, "ip_ttl") == 0) { 1469 send_changed_ttl(txfd, &daddr); 1470 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1471 } else if (strcmp(testname, "ip_opt") == 0) { 1472 send_ip_options(txfd, &daddr); 1473 usleep(fin_delay_us); 1474 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1475 } else if (strcmp(testname, "ip_frag4") == 0) { 1476 send_fragment4(txfd, &daddr); 1477 usleep(fin_delay_us); 1478 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1479 } else if (strcmp(testname, "ip_id_df1_inc") == 0) { 1480 send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_INC); 1481 usleep(fin_delay_us); 1482 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1483 } else if (strcmp(testname, "ip_id_df1_fixed") == 0) { 1484 send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_FIXED); 1485 usleep(fin_delay_us); 1486 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1487 } else if (strcmp(testname, "ip_id_df0_inc") == 0) { 1488 send_flush_id_case(txfd, &daddr, FLUSH_ID_DF0_INC); 1489 usleep(fin_delay_us); 1490 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1491 } else if (strcmp(testname, "ip_id_df0_fixed") == 0) { 1492 send_flush_id_case(txfd, &daddr, FLUSH_ID_DF0_FIXED); 1493 usleep(fin_delay_us); 1494 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1495 } else if (strcmp(testname, "ip_id_df1_inc_fixed") == 0) { 1496 send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_INC_FIXED); 1497 usleep(fin_delay_us); 1498 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1499 } else if (strcmp(testname, "ip_id_df1_fixed_inc") == 0) { 1500 send_flush_id_case(txfd, &daddr, FLUSH_ID_DF1_FIXED_INC); 1501 usleep(fin_delay_us); 1502 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1503 1504 /* ip sub-tests - IPv6 only */ 1505 } else if (strcmp(testname, "ip_frag6") == 0) { 1506 send_fragment6(txfd, &daddr); 1507 usleep(fin_delay_us); 1508 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1509 } else if (strcmp(testname, "ip_v6ext_same") == 0) { 1510 send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_1); 1511 usleep(fin_delay_us); 1512 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1513 } else if (strcmp(testname, "ip_v6ext_diff") == 0) { 1514 send_ipv6_exthdr(txfd, &daddr, EXT_PAYLOAD_1, EXT_PAYLOAD_2); 1515 usleep(fin_delay_us); 1516 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1517 1518 /* large sub-tests */ 1519 } else if (strcmp(testname, "large_max") == 0) { 1520 int remainder = max_payload() % calc_mss(); 1521 1522 send_large(txfd, &daddr, remainder); 1523 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1524 } else if (strcmp(testname, "large_rem") == 0) { 1525 int remainder = max_payload() % calc_mss(); 1526 1527 send_large(txfd, &daddr, remainder + 1); 1528 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1529 1530 /* machinery sub-tests */ 1531 } else if (strcmp(testname, "single") == 0) { 1532 static char buf[MAX_HDR_LEN + PAYLOAD_LEN]; 1533 1534 create_packet(buf, 0, 0, PAYLOAD_LEN, 0); 1535 write_packet(txfd, buf, total_hdr_len + PAYLOAD_LEN, &daddr); 1536 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1537 } else if (strcmp(testname, "capacity") == 0) { 1538 send_capacity(txfd, &daddr); 1539 usleep(fin_delay_us); 1540 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1541 1542 /* PPPoE sub-tests */ 1543 } else if (strcmp(testname, "pppoe_sid") == 0) { 1544 send_changed_pppoe_sid(txfd, &daddr); 1545 usleep(fin_delay_us); 1546 write_packet(txfd, fin_pkt, total_hdr_len, &daddr); 1547 1548 } else { 1549 error(1, 0, "Unknown testcase: %s", testname); 1550 } 1551 1552 if (close(txfd)) 1553 error(1, errno, "socket close"); 1554 } 1555 1556 static void gro_receiver(void) 1557 { 1558 static int correct_payload[NUM_PACKETS]; 1559 int rxfd = -1; 1560 1561 rxfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_NONE)); 1562 if (rxfd < 0) 1563 error(1, 0, "socket creation"); 1564 setup_sock_filter(rxfd); 1565 set_timeout(rxfd); 1566 set_rcvbuf(rxfd); 1567 bind_packetsocket(rxfd); 1568 1569 ksft_ready(); 1570 1571 memset(correct_payload, 0, sizeof(correct_payload)); 1572 1573 /* data sub-tests */ 1574 if (strcmp(testname, "data_same") == 0) { 1575 printf("pure data packet of same size: "); 1576 correct_payload[0] = PAYLOAD_LEN * 2; 1577 check_recv_pkts(rxfd, correct_payload, 1); 1578 } else if (strcmp(testname, "data_lrg_sml") == 0) { 1579 printf("large data packets followed by a smaller one: "); 1580 correct_payload[0] = PAYLOAD_LEN * 1.5; 1581 check_recv_pkts(rxfd, correct_payload, 1); 1582 } else if (strcmp(testname, "data_lrg_1byte") == 0) { 1583 printf("large data packet followed by a 1 byte one: "); 1584 correct_payload[0] = PAYLOAD_LEN + 1; 1585 check_recv_pkts(rxfd, correct_payload, 1); 1586 } else if (strcmp(testname, "data_sml_lrg") == 0) { 1587 printf("small data packets followed by a larger one: "); 1588 correct_payload[0] = PAYLOAD_LEN / 2; 1589 correct_payload[1] = PAYLOAD_LEN; 1590 check_recv_pkts(rxfd, correct_payload, 2); 1591 } else if (strcmp(testname, "data_burst") == 0) { 1592 printf("two bursts of two data packets: "); 1593 correct_payload[0] = PAYLOAD_LEN * 2; 1594 correct_payload[1] = PAYLOAD_LEN * 2; 1595 check_recv_pkts(rxfd, correct_payload, 2); 1596 1597 /* ack test */ 1598 } else if (strcmp(testname, "ack") == 0) { 1599 printf("duplicate ack and pure ack: "); 1600 check_recv_pkts(rxfd, correct_payload, 3); 1601 1602 /* flags sub-tests */ 1603 } else if (strcmp(testname, "flags_psh") == 0) { 1604 correct_payload[0] = PAYLOAD_LEN * 3; 1605 correct_payload[1] = PAYLOAD_LEN * 2; 1606 printf("psh flag ends coalescing: "); 1607 check_recv_pkts(rxfd, correct_payload, 2); 1608 } else if (strcmp(testname, "flags_syn") == 0) { 1609 correct_payload[0] = PAYLOAD_LEN * 2; 1610 correct_payload[1] = 0; 1611 correct_payload[2] = PAYLOAD_LEN * 2; 1612 printf("syn flag ends coalescing: "); 1613 check_recv_pkts(rxfd, correct_payload, 3); 1614 } else if (strcmp(testname, "flags_rst") == 0) { 1615 correct_payload[0] = PAYLOAD_LEN * 2; 1616 correct_payload[1] = 0; 1617 correct_payload[2] = PAYLOAD_LEN * 2; 1618 printf("rst flag ends coalescing: "); 1619 check_recv_pkts(rxfd, correct_payload, 3); 1620 } else if (strcmp(testname, "flags_urg") == 0) { 1621 correct_payload[0] = PAYLOAD_LEN * 2; 1622 correct_payload[1] = 0; 1623 correct_payload[2] = PAYLOAD_LEN * 2; 1624 printf("urg flag ends coalescing: "); 1625 check_recv_pkts(rxfd, correct_payload, 3); 1626 } else if (strcmp(testname, "flags_cwr") == 0) { 1627 correct_payload[0] = PAYLOAD_LEN; 1628 correct_payload[1] = PAYLOAD_LEN * 2; 1629 correct_payload[2] = PAYLOAD_LEN * 2; 1630 printf("cwr flag ends coalescing: "); 1631 check_recv_pkts(rxfd, correct_payload, 3); 1632 1633 /* tcp sub-tests */ 1634 } else if (strcmp(testname, "tcp_csum") == 0) { 1635 correct_payload[0] = PAYLOAD_LEN; 1636 correct_payload[1] = PAYLOAD_LEN; 1637 printf("changed checksum does not coalesce: "); 1638 check_recv_pkts(rxfd, correct_payload, 2); 1639 } else if (strcmp(testname, "tcp_seq") == 0) { 1640 correct_payload[0] = PAYLOAD_LEN; 1641 correct_payload[1] = PAYLOAD_LEN; 1642 printf("Wrong Seq number doesn't coalesce: "); 1643 check_recv_pkts(rxfd, correct_payload, 2); 1644 } else if (strcmp(testname, "tcp_ts") == 0) { 1645 correct_payload[0] = PAYLOAD_LEN * 2; 1646 correct_payload[1] = PAYLOAD_LEN; 1647 correct_payload[2] = PAYLOAD_LEN; 1648 correct_payload[3] = PAYLOAD_LEN; 1649 printf("Different timestamp doesn't coalesce: "); 1650 check_recv_pkts(rxfd, correct_payload, 4); 1651 } else if (strcmp(testname, "tcp_opt") == 0) { 1652 correct_payload[0] = PAYLOAD_LEN * 2; 1653 correct_payload[1] = PAYLOAD_LEN; 1654 printf("Different options doesn't coalesce: "); 1655 check_recv_pkts(rxfd, correct_payload, 2); 1656 1657 /* ip sub-tests - shared between IPv4 and IPv6 */ 1658 } else if (strcmp(testname, "ip_ecn") == 0) { 1659 correct_payload[0] = PAYLOAD_LEN; 1660 correct_payload[1] = PAYLOAD_LEN; 1661 printf("different ECN doesn't coalesce: "); 1662 check_recv_pkts(rxfd, correct_payload, 2); 1663 } else if (strcmp(testname, "ip_tos") == 0) { 1664 correct_payload[0] = PAYLOAD_LEN; 1665 correct_payload[1] = PAYLOAD_LEN; 1666 printf("different tos doesn't coalesce: "); 1667 check_recv_pkts(rxfd, correct_payload, 2); 1668 1669 /* ip sub-tests - IPv4 only */ 1670 } else if (strcmp(testname, "ip_csum") == 0) { 1671 correct_payload[0] = PAYLOAD_LEN; 1672 correct_payload[1] = PAYLOAD_LEN; 1673 correct_payload[2] = PAYLOAD_LEN; 1674 printf("bad ip checksum doesn't coalesce: "); 1675 check_recv_pkts(rxfd, correct_payload, 3); 1676 } else if (strcmp(testname, "ip_ttl") == 0) { 1677 correct_payload[0] = PAYLOAD_LEN; 1678 correct_payload[1] = PAYLOAD_LEN; 1679 printf("different ttl doesn't coalesce: "); 1680 check_recv_pkts(rxfd, correct_payload, 2); 1681 } else if (strcmp(testname, "ip_opt") == 0) { 1682 correct_payload[0] = PAYLOAD_LEN; 1683 correct_payload[1] = PAYLOAD_LEN; 1684 correct_payload[2] = PAYLOAD_LEN; 1685 printf("ip options doesn't coalesce: "); 1686 check_recv_pkts(rxfd, correct_payload, 3); 1687 } else if (strcmp(testname, "ip_frag4") == 0) { 1688 correct_payload[0] = PAYLOAD_LEN; 1689 correct_payload[1] = PAYLOAD_LEN; 1690 printf("fragmented ip4 doesn't coalesce: "); 1691 check_recv_pkts(rxfd, correct_payload, 2); 1692 } else if (strcmp(testname, "ip_id_df1_inc") == 0) { 1693 printf("DF=1, Incrementing - should coalesce: "); 1694 correct_payload[0] = PAYLOAD_LEN * 2; 1695 check_recv_pkts(rxfd, correct_payload, 1); 1696 } else if (strcmp(testname, "ip_id_df1_fixed") == 0) { 1697 printf("DF=1, Fixed - should coalesce: "); 1698 correct_payload[0] = PAYLOAD_LEN * 2; 1699 check_recv_pkts(rxfd, correct_payload, 1); 1700 } else if (strcmp(testname, "ip_id_df0_inc") == 0) { 1701 printf("DF=0, Incrementing - should coalesce: "); 1702 correct_payload[0] = PAYLOAD_LEN * 2; 1703 check_recv_pkts(rxfd, correct_payload, 1); 1704 } else if (strcmp(testname, "ip_id_df0_fixed") == 0) { 1705 printf("DF=0, Fixed - should coalesce: "); 1706 correct_payload[0] = PAYLOAD_LEN * 2; 1707 check_recv_pkts(rxfd, correct_payload, 1); 1708 } else if (strcmp(testname, "ip_id_df1_inc_fixed") == 0) { 1709 printf("DF=1, 2 Incrementing and one fixed - should coalesce only first 2 packets: "); 1710 correct_payload[0] = PAYLOAD_LEN * 2; 1711 correct_payload[1] = PAYLOAD_LEN; 1712 check_recv_pkts(rxfd, correct_payload, 2); 1713 } else if (strcmp(testname, "ip_id_df1_fixed_inc") == 0) { 1714 printf("DF=1, 2 Fixed and one incrementing - should coalesce only first 2 packets: "); 1715 correct_payload[0] = PAYLOAD_LEN * 2; 1716 correct_payload[1] = PAYLOAD_LEN; 1717 check_recv_pkts(rxfd, correct_payload, 2); 1718 1719 /* ip sub-tests - IPv6 only */ 1720 } else if (strcmp(testname, "ip_frag6") == 0) { 1721 /* GRO doesn't check for ipv6 hop limit when flushing. 1722 * Hence no corresponding test to the ipv4 case. 1723 */ 1724 printf("fragmented ip6 doesn't coalesce: "); 1725 correct_payload[0] = PAYLOAD_LEN * 2; 1726 correct_payload[1] = PAYLOAD_LEN; 1727 correct_payload[2] = PAYLOAD_LEN; 1728 check_recv_pkts(rxfd, correct_payload, 3); 1729 } else if (strcmp(testname, "ip_v6ext_same") == 0) { 1730 printf("ipv6 with ext header does coalesce: "); 1731 correct_payload[0] = PAYLOAD_LEN * 2; 1732 check_recv_pkts(rxfd, correct_payload, 1); 1733 } else if (strcmp(testname, "ip_v6ext_diff") == 0) { 1734 printf("ipv6 with ext header with different payloads doesn't coalesce: "); 1735 correct_payload[0] = PAYLOAD_LEN; 1736 correct_payload[1] = PAYLOAD_LEN; 1737 check_recv_pkts(rxfd, correct_payload, 2); 1738 1739 /* large sub-tests */ 1740 } else if (strcmp(testname, "large_max") == 0) { 1741 int remainder = max_payload() % calc_mss(); 1742 1743 correct_payload[0] = max_payload(); 1744 correct_payload[1] = remainder; 1745 printf("Shouldn't coalesce if exceed IP max pkt size: "); 1746 check_recv_pkts(rxfd, correct_payload, 2); 1747 } else if (strcmp(testname, "large_rem") == 0) { 1748 int remainder = max_payload() % calc_mss(); 1749 1750 /* last segment sent individually, doesn't start new segment */ 1751 correct_payload[0] = max_payload() - remainder; 1752 correct_payload[1] = remainder + 1; 1753 correct_payload[2] = remainder + 1; 1754 printf("last segment sent individually: "); 1755 check_recv_pkts(rxfd, correct_payload, 3); 1756 1757 /* machinery sub-tests */ 1758 } else if (strcmp(testname, "single") == 0) { 1759 printf("single data packet: "); 1760 correct_payload[0] = PAYLOAD_LEN; 1761 check_recv_pkts(rxfd, correct_payload, 1); 1762 } else if (strcmp(testname, "capacity") == 0) { 1763 check_capacity_pkts(rxfd); 1764 1765 } else if (strcmp(testname, "pppoe_sid") == 0) { 1766 correct_payload[0] = PAYLOAD_LEN; 1767 correct_payload[1] = PAYLOAD_LEN; 1768 printf("different PPPoE session ID doesn't coalesce: "); 1769 check_recv_pkts(rxfd, correct_payload, 2); 1770 1771 } else { 1772 error(1, 0, "Test case error: unknown testname %s", testname); 1773 } 1774 1775 if (close(rxfd)) 1776 error(1, 0, "socket close"); 1777 } 1778 1779 static void parse_args(int argc, char **argv) 1780 { 1781 static const struct option opts[] = { 1782 { "daddr", required_argument, NULL, 'd' }, 1783 { "dmac", required_argument, NULL, 'D' }, 1784 { "iface", required_argument, NULL, 'i' }, 1785 { "ipv4", no_argument, NULL, '4' }, 1786 { "ipv6", no_argument, NULL, '6' }, 1787 { "ipip", no_argument, NULL, 'e' }, 1788 { "ip6ip6", no_argument, NULL, 'E' }, 1789 { "pppoev4", no_argument, NULL, 'p' }, 1790 { "pppoev6", no_argument, NULL, 'P' }, 1791 { "num-flows", required_argument, NULL, 'n' }, 1792 { "rx", no_argument, NULL, 'r' }, 1793 { "saddr", required_argument, NULL, 's' }, 1794 { "smac", required_argument, NULL, 'S' }, 1795 { "test", required_argument, NULL, 't' }, 1796 { "order-check", no_argument, NULL, 'o' }, 1797 { "verbose", no_argument, NULL, 'v' }, 1798 { 0, 0, 0, 0 } 1799 }; 1800 int c; 1801 1802 while ((c = getopt_long(argc, argv, "46d:D:eEi:n:pPrs:S:t:ov", opts, NULL)) != -1) { 1803 switch (c) { 1804 case '4': 1805 proto = PF_INET; 1806 ethhdr_proto = htons(ETH_P_IP); 1807 break; 1808 case '6': 1809 proto = PF_INET6; 1810 ethhdr_proto = htons(ETH_P_IPV6); 1811 break; 1812 case 'e': 1813 ipip = true; 1814 proto = PF_INET; 1815 ethhdr_proto = htons(ETH_P_IP); 1816 break; 1817 case 'E': 1818 ip6ip6 = true; 1819 proto = PF_INET6; 1820 ethhdr_proto = htons(ETH_P_IPV6); 1821 break; 1822 case 'p': 1823 pppoe = true; 1824 proto = PF_INET; 1825 ethhdr_proto = htons(ETH_P_PPP_SES); 1826 break; 1827 case 'P': 1828 pppoe = true; 1829 proto = PF_INET6; 1830 ethhdr_proto = htons(ETH_P_PPP_SES); 1831 break; 1832 case 'd': 1833 addr4_dst = addr6_dst = optarg; 1834 break; 1835 case 'D': 1836 dmac = optarg; 1837 break; 1838 case 'i': 1839 ifname = optarg; 1840 break; 1841 case 'n': 1842 num_flows = atoi(optarg); 1843 break; 1844 case 'r': 1845 tx_socket = false; 1846 break; 1847 case 's': 1848 addr4_src = addr6_src = optarg; 1849 break; 1850 case 'S': 1851 smac = optarg; 1852 break; 1853 case 't': 1854 testname = optarg; 1855 break; 1856 case 'o': 1857 order_check = true; 1858 break; 1859 case 'v': 1860 verbose = true; 1861 break; 1862 default: 1863 error(1, 0, "%s invalid option %c\n", __func__, c); 1864 break; 1865 } 1866 } 1867 } 1868 1869 int main(int argc, char **argv) 1870 { 1871 parse_args(argc, argv); 1872 1873 if (ipip) { 1874 tcp_offset = ETH_HLEN + sizeof(struct iphdr) * 2; 1875 total_hdr_len = tcp_offset + sizeof(struct tcphdr); 1876 } else if (ip6ip6) { 1877 tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr) * 2; 1878 total_hdr_len = tcp_offset + sizeof(struct tcphdr); 1879 } else if (pppoe) { 1880 tcp_offset = ETH_HLEN + PPPOE_SES_HLEN + 1881 (proto == PF_INET ? sizeof(struct iphdr) : sizeof(struct ipv6hdr)); 1882 total_hdr_len = tcp_offset + sizeof(struct tcphdr); 1883 } else if (proto == PF_INET) { 1884 tcp_offset = ETH_HLEN + sizeof(struct iphdr); 1885 total_hdr_len = tcp_offset + sizeof(struct tcphdr); 1886 } else if (proto == PF_INET6) { 1887 tcp_offset = ETH_HLEN + sizeof(struct ipv6hdr); 1888 total_hdr_len = tcp_offset + sizeof(struct tcphdr); 1889 } else { 1890 error(1, 0, "Protocol family is not ipv4 or ipv6"); 1891 } 1892 1893 read_MAC(src_mac, smac); 1894 read_MAC(dst_mac, dmac); 1895 1896 if (tx_socket) { 1897 gro_sender(); 1898 } else { 1899 /* Only the receiver exit status determines test success. */ 1900 gro_receiver(); 1901 fprintf(stderr, "Gro::%s test passed.\n", testname); 1902 } 1903 1904 return 0; 1905 } 1906