projects/linux·blob master · refs

1919 lines · 58.7 KiB

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