1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2020 Intel Corporation. */
3
4 /*
5 * Some functions in this program are taken from
6 * Linux kernel samples/bpf/xdpsock* and modified
7 * for use.
8 *
9 * See test_xsk.sh for detailed information on test topology
10 * and prerequisite network setup.
11 *
12 * This test program contains two threads, each thread is single socket with
13 * a unique UMEM. It validates in-order packet delivery and packet content
14 * by sending packets to each other.
15 *
16 * Tests Information:
17 * ------------------
18 * These selftests test AF_XDP SKB and Native/DRV modes using veth
19 * Virtual Ethernet interfaces.
20 *
21 * For each mode, the following tests are run:
22 * a. nopoll - soft-irq processing in run-to-completion mode
23 * b. poll - using poll() syscall
24 * c. Socket Teardown
25 * Create a Tx and a Rx socket, Tx from one socket, Rx on another. Destroy
26 * both sockets, then repeat multiple times. Only nopoll mode is used
27 * d. Bi-directional sockets
28 * Configure sockets as bi-directional tx/rx sockets, sets up fill and
29 * completion rings on each socket, tx/rx in both directions. Only nopoll
30 * mode is used
31 * e. Statistics
32 * Trigger some error conditions and ensure that the appropriate statistics
33 * are incremented. Within this test, the following statistics are tested:
34 * i. rx dropped
35 * Increase the UMEM frame headroom to a value which results in
36 * insufficient space in the rx buffer for both the packet and the headroom.
37 * ii. tx invalid
38 * Set the 'len' field of tx descriptors to an invalid value (umem frame
39 * size + 1).
40 * iii. rx ring full
41 * Reduce the size of the RX ring to a fraction of the fill ring size.
42 * iv. fill queue empty
43 * Do not populate the fill queue and then try to receive pkts.
44 * f. bpf_link resource persistence
45 * Configure sockets at indexes 0 and 1, run a traffic on queue ids 0,
46 * then remove xsk sockets from queue 0 on both veth interfaces and
47 * finally run a traffic on queues ids 1
48 * g. unaligned mode
49 * h. tests for invalid and corner case Tx descriptors so that the correct ones
50 * are discarded and let through, respectively.
51 * i. 2K frame size tests
52 *
53 * Total tests: 12
54 *
55 * Flow:
56 * -----
57 * - Single process spawns two threads: Tx and Rx
58 * - Each of these two threads attach to a veth interface within their assigned
59 * namespaces
60 * - Each thread Creates one AF_XDP socket connected to a unique umem for each
61 * veth interface
62 * - Tx thread Transmits 10k packets from veth<xxxx> to veth<yyyy>
63 * - Rx thread verifies if all 10k packets were received and delivered in-order,
64 * and have the right content
65 *
66 * Enable/disable packet dump mode:
67 * --------------------------
68 * To enable L2 - L4 headers and payload dump of each packet on STDOUT, add
69 * parameter -D to params array in test_xsk.sh, i.e. params=("-S" "-D")
70 */
71
72 #define _GNU_SOURCE
73 #include <fcntl.h>
74 #include <errno.h>
75 #include <getopt.h>
76 #include <asm/barrier.h>
77 #include <linux/if_link.h>
78 #include <linux/if_ether.h>
79 #include <linux/ip.h>
80 #include <linux/udp.h>
81 #include <arpa/inet.h>
82 #include <net/if.h>
83 #include <locale.h>
84 #include <poll.h>
85 #include <pthread.h>
86 #include <signal.h>
87 #include <stdbool.h>
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <stddef.h>
92 #include <sys/mman.h>
93 #include <sys/resource.h>
94 #include <sys/types.h>
95 #include <sys/queue.h>
96 #include <time.h>
97 #include <unistd.h>
98 #include <stdatomic.h>
99 #include <bpf/xsk.h>
100 #include "xdpxceiver.h"
101 #include "../kselftest.h"
102
103 /* AF_XDP APIs were moved into libxdp and marked as deprecated in libbpf.
104 * Until xdpxceiver is either moved or re-writed into libxdp, suppress
105 * deprecation warnings in this file
106 */
107 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
108
109 static const char *MAC1 = "\x00\x0A\x56\x9E\xEE\x62";
110 static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
111 static const char *IP1 = "192.168.100.162";
112 static const char *IP2 = "192.168.100.161";
113 static const u16 UDP_PORT1 = 2020;
114 static const u16 UDP_PORT2 = 2121;
115
__exit_with_error(int error,const char * file,const char * func,int line)116 static void __exit_with_error(int error, const char *file, const char *func, int line)
117 {
118 ksft_test_result_fail("[%s:%s:%i]: ERROR: %d/\"%s\"\n", file, func, line, error,
119 strerror(error));
120 ksft_exit_xfail();
121 }
122
123 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
124
125 #define mode_string(test) (test)->ifobj_tx->xdp_flags & XDP_FLAGS_SKB_MODE ? "SKB" : "DRV"
126
127 #define print_ksft_result(test) \
128 (ksft_test_result_pass("PASS: %s %s\n", mode_string(test), (test)->name))
129
memset32_htonl(void * dest,u32 val,u32 size)130 static void memset32_htonl(void *dest, u32 val, u32 size)
131 {
132 u32 *ptr = (u32 *)dest;
133 int i;
134
135 val = htonl(val);
136
137 for (i = 0; i < (size & (~0x3)); i += 4)
138 ptr[i >> 2] = val;
139 }
140
141 /*
142 * Fold a partial checksum
143 * This function code has been taken from
144 * Linux kernel include/asm-generic/checksum.h
145 */
csum_fold(__u32 csum)146 static __u16 csum_fold(__u32 csum)
147 {
148 u32 sum = (__force u32)csum;
149
150 sum = (sum & 0xffff) + (sum >> 16);
151 sum = (sum & 0xffff) + (sum >> 16);
152 return (__force __u16)~sum;
153 }
154
155 /*
156 * This function code has been taken from
157 * Linux kernel lib/checksum.c
158 */
from64to32(u64 x)159 static u32 from64to32(u64 x)
160 {
161 /* add up 32-bit and 32-bit for 32+c bit */
162 x = (x & 0xffffffff) + (x >> 32);
163 /* add up carry.. */
164 x = (x & 0xffffffff) + (x >> 32);
165 return (u32)x;
166 }
167
168 /*
169 * This function code has been taken from
170 * Linux kernel lib/checksum.c
171 */
csum_tcpudp_nofold(__be32 saddr,__be32 daddr,__u32 len,__u8 proto,__u32 sum)172 static __u32 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __u32 sum)
173 {
174 unsigned long long s = (__force u32)sum;
175
176 s += (__force u32)saddr;
177 s += (__force u32)daddr;
178 #ifdef __BIG_ENDIAN__
179 s += proto + len;
180 #else
181 s += (proto + len) << 8;
182 #endif
183 return (__force __u32)from64to32(s);
184 }
185
186 /*
187 * This function has been taken from
188 * Linux kernel include/asm-generic/checksum.h
189 */
csum_tcpudp_magic(__be32 saddr,__be32 daddr,__u32 len,__u8 proto,__u32 sum)190 static __u16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __u32 sum)
191 {
192 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
193 }
194
udp_csum(u32 saddr,u32 daddr,u32 len,u8 proto,u16 * udp_pkt)195 static u16 udp_csum(u32 saddr, u32 daddr, u32 len, u8 proto, u16 *udp_pkt)
196 {
197 u32 csum = 0;
198 u32 cnt = 0;
199
200 /* udp hdr and data */
201 for (; cnt < len; cnt += 2)
202 csum += udp_pkt[cnt >> 1];
203
204 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
205 }
206
gen_eth_hdr(struct ifobject * ifobject,struct ethhdr * eth_hdr)207 static void gen_eth_hdr(struct ifobject *ifobject, struct ethhdr *eth_hdr)
208 {
209 memcpy(eth_hdr->h_dest, ifobject->dst_mac, ETH_ALEN);
210 memcpy(eth_hdr->h_source, ifobject->src_mac, ETH_ALEN);
211 eth_hdr->h_proto = htons(ETH_P_IP);
212 }
213
gen_ip_hdr(struct ifobject * ifobject,struct iphdr * ip_hdr)214 static void gen_ip_hdr(struct ifobject *ifobject, struct iphdr *ip_hdr)
215 {
216 ip_hdr->version = IP_PKT_VER;
217 ip_hdr->ihl = 0x5;
218 ip_hdr->tos = IP_PKT_TOS;
219 ip_hdr->tot_len = htons(IP_PKT_SIZE);
220 ip_hdr->id = 0;
221 ip_hdr->frag_off = 0;
222 ip_hdr->ttl = IPDEFTTL;
223 ip_hdr->protocol = IPPROTO_UDP;
224 ip_hdr->saddr = ifobject->src_ip;
225 ip_hdr->daddr = ifobject->dst_ip;
226 ip_hdr->check = 0;
227 }
228
gen_udp_hdr(u32 payload,void * pkt,struct ifobject * ifobject,struct udphdr * udp_hdr)229 static void gen_udp_hdr(u32 payload, void *pkt, struct ifobject *ifobject,
230 struct udphdr *udp_hdr)
231 {
232 udp_hdr->source = htons(ifobject->src_port);
233 udp_hdr->dest = htons(ifobject->dst_port);
234 udp_hdr->len = htons(UDP_PKT_SIZE);
235 memset32_htonl(pkt + PKT_HDR_SIZE, payload, UDP_PKT_DATA_SIZE);
236 }
237
gen_udp_csum(struct udphdr * udp_hdr,struct iphdr * ip_hdr)238 static void gen_udp_csum(struct udphdr *udp_hdr, struct iphdr *ip_hdr)
239 {
240 udp_hdr->check = 0;
241 udp_hdr->check =
242 udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE, IPPROTO_UDP, (u16 *)udp_hdr);
243 }
244
xsk_configure_umem(struct xsk_umem_info * umem,void * buffer,u64 size)245 static int xsk_configure_umem(struct xsk_umem_info *umem, void *buffer, u64 size)
246 {
247 struct xsk_umem_config cfg = {
248 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
249 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
250 .frame_size = umem->frame_size,
251 .frame_headroom = umem->frame_headroom,
252 .flags = XSK_UMEM__DEFAULT_FLAGS
253 };
254 int ret;
255
256 if (umem->unaligned_mode)
257 cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
258
259 ret = xsk_umem__create(&umem->umem, buffer, size,
260 &umem->fq, &umem->cq, &cfg);
261 if (ret)
262 return ret;
263
264 umem->buffer = buffer;
265 return 0;
266 }
267
xsk_configure_socket(struct xsk_socket_info * xsk,struct xsk_umem_info * umem,struct ifobject * ifobject,u32 qid)268 static int xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem,
269 struct ifobject *ifobject, u32 qid)
270 {
271 struct xsk_socket_config cfg;
272 struct xsk_ring_cons *rxr;
273 struct xsk_ring_prod *txr;
274
275 xsk->umem = umem;
276 cfg.rx_size = xsk->rxqsize;
277 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
278 cfg.libbpf_flags = 0;
279 cfg.xdp_flags = ifobject->xdp_flags;
280 cfg.bind_flags = ifobject->bind_flags;
281
282 txr = ifobject->tx_on ? &xsk->tx : NULL;
283 rxr = ifobject->rx_on ? &xsk->rx : NULL;
284 return xsk_socket__create(&xsk->xsk, ifobject->ifname, qid, umem->umem, rxr, txr, &cfg);
285 }
286
287 static struct option long_options[] = {
288 {"interface", required_argument, 0, 'i'},
289 {"queue", optional_argument, 0, 'q'},
290 {"dump-pkts", optional_argument, 0, 'D'},
291 {"verbose", no_argument, 0, 'v'},
292 {0, 0, 0, 0}
293 };
294
usage(const char * prog)295 static void usage(const char *prog)
296 {
297 const char *str =
298 " Usage: %s [OPTIONS]\n"
299 " Options:\n"
300 " -i, --interface Use interface\n"
301 " -q, --queue=n Use queue n (default 0)\n"
302 " -D, --dump-pkts Dump packets L2 - L5\n"
303 " -v, --verbose Verbose output\n";
304
305 ksft_print_msg(str, prog);
306 }
307
switch_namespace(const char * nsname)308 static int switch_namespace(const char *nsname)
309 {
310 char fqns[26] = "/var/run/netns/";
311 int nsfd;
312
313 if (!nsname || strlen(nsname) == 0)
314 return -1;
315
316 strncat(fqns, nsname, sizeof(fqns) - strlen(fqns) - 1);
317 nsfd = open(fqns, O_RDONLY);
318
319 if (nsfd == -1)
320 exit_with_error(errno);
321
322 if (setns(nsfd, 0) == -1)
323 exit_with_error(errno);
324
325 print_verbose("NS switched: %s\n", nsname);
326
327 return nsfd;
328 }
329
validate_interface(struct ifobject * ifobj)330 static bool validate_interface(struct ifobject *ifobj)
331 {
332 if (!strcmp(ifobj->ifname, ""))
333 return false;
334 return true;
335 }
336
parse_command_line(struct ifobject * ifobj_tx,struct ifobject * ifobj_rx,int argc,char ** argv)337 static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc,
338 char **argv)
339 {
340 struct ifobject *ifobj;
341 u32 interface_nb = 0;
342 int option_index, c;
343
344 opterr = 0;
345
346 for (;;) {
347 char *sptr, *token;
348
349 c = getopt_long(argc, argv, "i:Dv", long_options, &option_index);
350 if (c == -1)
351 break;
352
353 switch (c) {
354 case 'i':
355 if (interface_nb == 0)
356 ifobj = ifobj_tx;
357 else if (interface_nb == 1)
358 ifobj = ifobj_rx;
359 else
360 break;
361
362 sptr = strndupa(optarg, strlen(optarg));
363 memcpy(ifobj->ifname, strsep(&sptr, ","), MAX_INTERFACE_NAME_CHARS);
364 token = strsep(&sptr, ",");
365 if (token)
366 memcpy(ifobj->nsname, token, MAX_INTERFACES_NAMESPACE_CHARS);
367 interface_nb++;
368 break;
369 case 'D':
370 opt_pkt_dump = true;
371 break;
372 case 'v':
373 opt_verbose = true;
374 break;
375 default:
376 usage(basename(argv[0]));
377 ksft_exit_xfail();
378 }
379 }
380 }
381
__test_spec_init(struct test_spec * test,struct ifobject * ifobj_tx,struct ifobject * ifobj_rx)382 static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
383 struct ifobject *ifobj_rx)
384 {
385 u32 i, j;
386
387 for (i = 0; i < MAX_INTERFACES; i++) {
388 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
389
390 ifobj->umem = &ifobj->umem_arr[0];
391 ifobj->xsk = &ifobj->xsk_arr[0];
392 ifobj->use_poll = false;
393 ifobj->pacing_on = true;
394 ifobj->pkt_stream = test->pkt_stream_default;
395
396 if (i == 0) {
397 ifobj->rx_on = false;
398 ifobj->tx_on = true;
399 } else {
400 ifobj->rx_on = true;
401 ifobj->tx_on = false;
402 }
403
404 for (j = 0; j < MAX_SOCKETS; j++) {
405 memset(&ifobj->umem_arr[j], 0, sizeof(ifobj->umem_arr[j]));
406 memset(&ifobj->xsk_arr[j], 0, sizeof(ifobj->xsk_arr[j]));
407 ifobj->umem_arr[j].num_frames = DEFAULT_UMEM_BUFFERS;
408 ifobj->umem_arr[j].frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
409 ifobj->xsk_arr[j].rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS;
410 }
411 }
412
413 test->ifobj_tx = ifobj_tx;
414 test->ifobj_rx = ifobj_rx;
415 test->current_step = 0;
416 test->total_steps = 1;
417 test->nb_sockets = 1;
418 }
419
test_spec_init(struct test_spec * test,struct ifobject * ifobj_tx,struct ifobject * ifobj_rx,enum test_mode mode)420 static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
421 struct ifobject *ifobj_rx, enum test_mode mode)
422 {
423 struct pkt_stream *pkt_stream;
424 u32 i;
425
426 pkt_stream = test->pkt_stream_default;
427 memset(test, 0, sizeof(*test));
428 test->pkt_stream_default = pkt_stream;
429
430 for (i = 0; i < MAX_INTERFACES; i++) {
431 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
432
433 ifobj->xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
434 if (mode == TEST_MODE_SKB)
435 ifobj->xdp_flags |= XDP_FLAGS_SKB_MODE;
436 else
437 ifobj->xdp_flags |= XDP_FLAGS_DRV_MODE;
438
439 ifobj->bind_flags = XDP_USE_NEED_WAKEUP | XDP_COPY;
440 }
441
442 __test_spec_init(test, ifobj_tx, ifobj_rx);
443 }
444
test_spec_reset(struct test_spec * test)445 static void test_spec_reset(struct test_spec *test)
446 {
447 __test_spec_init(test, test->ifobj_tx, test->ifobj_rx);
448 }
449
test_spec_set_name(struct test_spec * test,const char * name)450 static void test_spec_set_name(struct test_spec *test, const char *name)
451 {
452 strncpy(test->name, name, MAX_TEST_NAME_SIZE);
453 }
454
pkt_stream_reset(struct pkt_stream * pkt_stream)455 static void pkt_stream_reset(struct pkt_stream *pkt_stream)
456 {
457 if (pkt_stream)
458 pkt_stream->rx_pkt_nb = 0;
459 }
460
pkt_stream_get_pkt(struct pkt_stream * pkt_stream,u32 pkt_nb)461 static struct pkt *pkt_stream_get_pkt(struct pkt_stream *pkt_stream, u32 pkt_nb)
462 {
463 if (pkt_nb >= pkt_stream->nb_pkts)
464 return NULL;
465
466 return &pkt_stream->pkts[pkt_nb];
467 }
468
pkt_stream_get_next_rx_pkt(struct pkt_stream * pkt_stream)469 static struct pkt *pkt_stream_get_next_rx_pkt(struct pkt_stream *pkt_stream)
470 {
471 while (pkt_stream->rx_pkt_nb < pkt_stream->nb_pkts) {
472 if (pkt_stream->pkts[pkt_stream->rx_pkt_nb].valid)
473 return &pkt_stream->pkts[pkt_stream->rx_pkt_nb++];
474 pkt_stream->rx_pkt_nb++;
475 }
476 return NULL;
477 }
478
pkt_stream_delete(struct pkt_stream * pkt_stream)479 static void pkt_stream_delete(struct pkt_stream *pkt_stream)
480 {
481 free(pkt_stream->pkts);
482 free(pkt_stream);
483 }
484
pkt_stream_restore_default(struct test_spec * test)485 static void pkt_stream_restore_default(struct test_spec *test)
486 {
487 if (test->ifobj_tx->pkt_stream != test->pkt_stream_default) {
488 pkt_stream_delete(test->ifobj_tx->pkt_stream);
489 test->ifobj_tx->pkt_stream = test->pkt_stream_default;
490 }
491 test->ifobj_rx->pkt_stream = test->pkt_stream_default;
492 }
493
__pkt_stream_alloc(u32 nb_pkts)494 static struct pkt_stream *__pkt_stream_alloc(u32 nb_pkts)
495 {
496 struct pkt_stream *pkt_stream;
497
498 pkt_stream = calloc(1, sizeof(*pkt_stream));
499 if (!pkt_stream)
500 return NULL;
501
502 pkt_stream->pkts = calloc(nb_pkts, sizeof(*pkt_stream->pkts));
503 if (!pkt_stream->pkts) {
504 free(pkt_stream);
505 return NULL;
506 }
507
508 pkt_stream->nb_pkts = nb_pkts;
509 return pkt_stream;
510 }
511
pkt_stream_generate(struct xsk_umem_info * umem,u32 nb_pkts,u32 pkt_len)512 static struct pkt_stream *pkt_stream_generate(struct xsk_umem_info *umem, u32 nb_pkts, u32 pkt_len)
513 {
514 struct pkt_stream *pkt_stream;
515 u32 i;
516
517 pkt_stream = __pkt_stream_alloc(nb_pkts);
518 if (!pkt_stream)
519 exit_with_error(ENOMEM);
520
521 pkt_stream->nb_pkts = nb_pkts;
522 for (i = 0; i < nb_pkts; i++) {
523 pkt_stream->pkts[i].addr = (i % umem->num_frames) * umem->frame_size;
524 pkt_stream->pkts[i].len = pkt_len;
525 pkt_stream->pkts[i].payload = i;
526
527 if (pkt_len > umem->frame_size)
528 pkt_stream->pkts[i].valid = false;
529 else
530 pkt_stream->pkts[i].valid = true;
531 }
532
533 return pkt_stream;
534 }
535
pkt_stream_clone(struct xsk_umem_info * umem,struct pkt_stream * pkt_stream)536 static struct pkt_stream *pkt_stream_clone(struct xsk_umem_info *umem,
537 struct pkt_stream *pkt_stream)
538 {
539 return pkt_stream_generate(umem, pkt_stream->nb_pkts, pkt_stream->pkts[0].len);
540 }
541
pkt_stream_replace(struct test_spec * test,u32 nb_pkts,u32 pkt_len)542 static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
543 {
544 struct pkt_stream *pkt_stream;
545
546 pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, nb_pkts, pkt_len);
547 test->ifobj_tx->pkt_stream = pkt_stream;
548 test->ifobj_rx->pkt_stream = pkt_stream;
549 }
550
pkt_stream_replace_half(struct test_spec * test,u32 pkt_len,int offset)551 static void pkt_stream_replace_half(struct test_spec *test, u32 pkt_len, int offset)
552 {
553 struct xsk_umem_info *umem = test->ifobj_tx->umem;
554 struct pkt_stream *pkt_stream;
555 u32 i;
556
557 pkt_stream = pkt_stream_clone(umem, test->pkt_stream_default);
558 for (i = 1; i < test->pkt_stream_default->nb_pkts; i += 2) {
559 pkt_stream->pkts[i].addr = (i % umem->num_frames) * umem->frame_size + offset;
560 pkt_stream->pkts[i].len = pkt_len;
561 }
562
563 test->ifobj_tx->pkt_stream = pkt_stream;
564 test->ifobj_rx->pkt_stream = pkt_stream;
565 }
566
pkt_generate(struct ifobject * ifobject,u32 pkt_nb)567 static struct pkt *pkt_generate(struct ifobject *ifobject, u32 pkt_nb)
568 {
569 struct pkt *pkt = pkt_stream_get_pkt(ifobject->pkt_stream, pkt_nb);
570 struct udphdr *udp_hdr;
571 struct ethhdr *eth_hdr;
572 struct iphdr *ip_hdr;
573 void *data;
574
575 if (!pkt)
576 return NULL;
577 if (!pkt->valid || pkt->len < PKT_SIZE)
578 return pkt;
579
580 data = xsk_umem__get_data(ifobject->umem->buffer, pkt->addr);
581 udp_hdr = (struct udphdr *)(data + sizeof(struct ethhdr) + sizeof(struct iphdr));
582 ip_hdr = (struct iphdr *)(data + sizeof(struct ethhdr));
583 eth_hdr = (struct ethhdr *)data;
584
585 gen_udp_hdr(pkt_nb, data, ifobject, udp_hdr);
586 gen_ip_hdr(ifobject, ip_hdr);
587 gen_udp_csum(udp_hdr, ip_hdr);
588 gen_eth_hdr(ifobject, eth_hdr);
589
590 return pkt;
591 }
592
pkt_stream_generate_custom(struct test_spec * test,struct pkt * pkts,u32 nb_pkts)593 static void pkt_stream_generate_custom(struct test_spec *test, struct pkt *pkts, u32 nb_pkts)
594 {
595 struct pkt_stream *pkt_stream;
596 u32 i;
597
598 pkt_stream = __pkt_stream_alloc(nb_pkts);
599 if (!pkt_stream)
600 exit_with_error(ENOMEM);
601
602 test->ifobj_tx->pkt_stream = pkt_stream;
603 test->ifobj_rx->pkt_stream = pkt_stream;
604
605 for (i = 0; i < nb_pkts; i++) {
606 pkt_stream->pkts[i].addr = pkts[i].addr;
607 pkt_stream->pkts[i].len = pkts[i].len;
608 pkt_stream->pkts[i].payload = i;
609 pkt_stream->pkts[i].valid = pkts[i].valid;
610 }
611 }
612
pkt_dump(void * pkt,u32 len)613 static void pkt_dump(void *pkt, u32 len)
614 {
615 char s[INET_ADDRSTRLEN];
616 struct ethhdr *ethhdr;
617 struct udphdr *udphdr;
618 struct iphdr *iphdr;
619 int payload, i;
620
621 ethhdr = pkt;
622 iphdr = pkt + sizeof(*ethhdr);
623 udphdr = pkt + sizeof(*ethhdr) + sizeof(*iphdr);
624
625 /*extract L2 frame */
626 fprintf(stdout, "DEBUG>> L2: dst mac: ");
627 for (i = 0; i < ETH_ALEN; i++)
628 fprintf(stdout, "%02X", ethhdr->h_dest[i]);
629
630 fprintf(stdout, "\nDEBUG>> L2: src mac: ");
631 for (i = 0; i < ETH_ALEN; i++)
632 fprintf(stdout, "%02X", ethhdr->h_source[i]);
633
634 /*extract L3 frame */
635 fprintf(stdout, "\nDEBUG>> L3: ip_hdr->ihl: %02X\n", iphdr->ihl);
636 fprintf(stdout, "DEBUG>> L3: ip_hdr->saddr: %s\n",
637 inet_ntop(AF_INET, &iphdr->saddr, s, sizeof(s)));
638 fprintf(stdout, "DEBUG>> L3: ip_hdr->daddr: %s\n",
639 inet_ntop(AF_INET, &iphdr->daddr, s, sizeof(s)));
640 /*extract L4 frame */
641 fprintf(stdout, "DEBUG>> L4: udp_hdr->src: %d\n", ntohs(udphdr->source));
642 fprintf(stdout, "DEBUG>> L4: udp_hdr->dst: %d\n", ntohs(udphdr->dest));
643 /*extract L5 frame */
644 payload = *((uint32_t *)(pkt + PKT_HDR_SIZE));
645
646 fprintf(stdout, "DEBUG>> L5: payload: %d\n", payload);
647 fprintf(stdout, "---------------------------------------\n");
648 }
649
is_offset_correct(struct xsk_umem_info * umem,struct pkt_stream * pkt_stream,u64 addr,u64 pkt_stream_addr)650 static bool is_offset_correct(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream, u64 addr,
651 u64 pkt_stream_addr)
652 {
653 u32 headroom = umem->unaligned_mode ? 0 : umem->frame_headroom;
654 u32 offset = addr % umem->frame_size, expected_offset = 0;
655
656 if (!pkt_stream->use_addr_for_fill)
657 pkt_stream_addr = 0;
658
659 expected_offset += (pkt_stream_addr + headroom + XDP_PACKET_HEADROOM) % umem->frame_size;
660
661 if (offset == expected_offset)
662 return true;
663
664 ksft_test_result_fail("ERROR: [%s] expected [%u], got [%u]\n", __func__, expected_offset,
665 offset);
666 return false;
667 }
668
is_pkt_valid(struct pkt * pkt,void * buffer,u64 addr,u32 len)669 static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 len)
670 {
671 void *data = xsk_umem__get_data(buffer, addr);
672 struct iphdr *iphdr = (struct iphdr *)(data + sizeof(struct ethhdr));
673
674 if (!pkt) {
675 ksft_test_result_fail("ERROR: [%s] too many packets received\n", __func__);
676 return false;
677 }
678
679 if (len < PKT_SIZE) {
680 /*Do not try to verify packets that are smaller than minimum size. */
681 return true;
682 }
683
684 if (pkt->len != len) {
685 ksft_test_result_fail
686 ("ERROR: [%s] expected length [%d], got length [%d]\n",
687 __func__, pkt->len, len);
688 return false;
689 }
690
691 if (iphdr->version == IP_PKT_VER && iphdr->tos == IP_PKT_TOS) {
692 u32 seqnum = ntohl(*((u32 *)(data + PKT_HDR_SIZE)));
693
694 if (opt_pkt_dump)
695 pkt_dump(data, PKT_SIZE);
696
697 if (pkt->payload != seqnum) {
698 ksft_test_result_fail
699 ("ERROR: [%s] expected seqnum [%d], got seqnum [%d]\n",
700 __func__, pkt->payload, seqnum);
701 return false;
702 }
703 } else {
704 ksft_print_msg("Invalid frame received: ");
705 ksft_print_msg("[IP_PKT_VER: %02X], [IP_PKT_TOS: %02X]\n", iphdr->version,
706 iphdr->tos);
707 return false;
708 }
709
710 return true;
711 }
712
kick_tx(struct xsk_socket_info * xsk)713 static void kick_tx(struct xsk_socket_info *xsk)
714 {
715 int ret;
716
717 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
718 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN)
719 return;
720 exit_with_error(errno);
721 }
722
complete_pkts(struct xsk_socket_info * xsk,int batch_size)723 static void complete_pkts(struct xsk_socket_info *xsk, int batch_size)
724 {
725 unsigned int rcvd;
726 u32 idx;
727
728 if (xsk_ring_prod__needs_wakeup(&xsk->tx))
729 kick_tx(xsk);
730
731 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
732 if (rcvd) {
733 if (rcvd > xsk->outstanding_tx) {
734 u64 addr = *xsk_ring_cons__comp_addr(&xsk->umem->cq, idx + rcvd - 1);
735
736 ksft_test_result_fail("ERROR: [%s] Too many packets completed\n",
737 __func__);
738 ksft_print_msg("Last completion address: %llx\n", addr);
739 return;
740 }
741
742 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
743 xsk->outstanding_tx -= rcvd;
744 }
745 }
746
receive_pkts(struct pkt_stream * pkt_stream,struct xsk_socket_info * xsk,struct pollfd * fds)747 static void receive_pkts(struct pkt_stream *pkt_stream, struct xsk_socket_info *xsk,
748 struct pollfd *fds)
749 {
750 struct pkt *pkt = pkt_stream_get_next_rx_pkt(pkt_stream);
751 struct xsk_umem_info *umem = xsk->umem;
752 u32 idx_rx = 0, idx_fq = 0, rcvd, i;
753 int ret;
754
755 while (pkt) {
756 rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx);
757 if (!rcvd) {
758 if (xsk_ring_prod__needs_wakeup(&umem->fq)) {
759 ret = poll(fds, 1, POLL_TMOUT);
760 if (ret < 0)
761 exit_with_error(-ret);
762 }
763 continue;
764 }
765
766 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
767 while (ret != rcvd) {
768 if (ret < 0)
769 exit_with_error(-ret);
770 if (xsk_ring_prod__needs_wakeup(&umem->fq)) {
771 ret = poll(fds, 1, POLL_TMOUT);
772 if (ret < 0)
773 exit_with_error(-ret);
774 }
775 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
776 }
777
778 for (i = 0; i < rcvd; i++) {
779 const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++);
780 u64 addr = desc->addr, orig;
781
782 if (!pkt) {
783 ksft_test_result_fail("ERROR: [%s] Received too many packets.\n",
784 __func__);
785 ksft_print_msg("Last packet has addr: %llx len: %u\n",
786 addr, desc->len);
787 return;
788 }
789
790 orig = xsk_umem__extract_addr(addr);
791 addr = xsk_umem__add_offset_to_addr(addr);
792
793 if (!is_pkt_valid(pkt, umem->buffer, addr, desc->len))
794 return;
795 if (!is_offset_correct(umem, pkt_stream, addr, pkt->addr))
796 return;
797
798 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = orig;
799 pkt = pkt_stream_get_next_rx_pkt(pkt_stream);
800 }
801
802 xsk_ring_prod__submit(&umem->fq, rcvd);
803 xsk_ring_cons__release(&xsk->rx, rcvd);
804
805 pthread_mutex_lock(&pacing_mutex);
806 pkts_in_flight -= rcvd;
807 if (pkts_in_flight < umem->num_frames)
808 pthread_cond_signal(&pacing_cond);
809 pthread_mutex_unlock(&pacing_mutex);
810 }
811 }
812
__send_pkts(struct ifobject * ifobject,u32 pkt_nb)813 static u32 __send_pkts(struct ifobject *ifobject, u32 pkt_nb)
814 {
815 struct xsk_socket_info *xsk = ifobject->xsk;
816 u32 i, idx, valid_pkts = 0;
817
818 while (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) < BATCH_SIZE)
819 complete_pkts(xsk, BATCH_SIZE);
820
821 for (i = 0; i < BATCH_SIZE; i++) {
822 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx + i);
823 struct pkt *pkt = pkt_generate(ifobject, pkt_nb);
824
825 if (!pkt)
826 break;
827
828 tx_desc->addr = pkt->addr;
829 tx_desc->len = pkt->len;
830 pkt_nb++;
831 if (pkt->valid)
832 valid_pkts++;
833 }
834
835 pthread_mutex_lock(&pacing_mutex);
836 pkts_in_flight += valid_pkts;
837 if (ifobject->pacing_on && pkts_in_flight >= ifobject->umem->num_frames - BATCH_SIZE) {
838 kick_tx(xsk);
839 pthread_cond_wait(&pacing_cond, &pacing_mutex);
840 }
841 pthread_mutex_unlock(&pacing_mutex);
842
843 xsk_ring_prod__submit(&xsk->tx, i);
844 xsk->outstanding_tx += valid_pkts;
845 complete_pkts(xsk, i);
846
847 usleep(10);
848 return i;
849 }
850
wait_for_tx_completion(struct xsk_socket_info * xsk)851 static void wait_for_tx_completion(struct xsk_socket_info *xsk)
852 {
853 while (xsk->outstanding_tx)
854 complete_pkts(xsk, BATCH_SIZE);
855 }
856
send_pkts(struct ifobject * ifobject)857 static void send_pkts(struct ifobject *ifobject)
858 {
859 struct pollfd fds = { };
860 u32 pkt_cnt = 0;
861
862 fds.fd = xsk_socket__fd(ifobject->xsk->xsk);
863 fds.events = POLLOUT;
864
865 while (pkt_cnt < ifobject->pkt_stream->nb_pkts) {
866 if (ifobject->use_poll) {
867 int ret;
868
869 ret = poll(&fds, 1, POLL_TMOUT);
870 if (ret <= 0)
871 continue;
872
873 if (!(fds.revents & POLLOUT))
874 continue;
875 }
876
877 pkt_cnt += __send_pkts(ifobject, pkt_cnt);
878 }
879
880 wait_for_tx_completion(ifobject->xsk);
881 }
882
rx_stats_are_valid(struct ifobject * ifobject)883 static bool rx_stats_are_valid(struct ifobject *ifobject)
884 {
885 u32 xsk_stat = 0, expected_stat = ifobject->pkt_stream->nb_pkts;
886 struct xsk_socket *xsk = ifobject->xsk->xsk;
887 int fd = xsk_socket__fd(xsk);
888 struct xdp_statistics stats;
889 socklen_t optlen;
890 int err;
891
892 optlen = sizeof(stats);
893 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
894 if (err) {
895 ksft_test_result_fail("ERROR Rx: [%s] getsockopt(XDP_STATISTICS) error %u %s\n",
896 __func__, -err, strerror(-err));
897 return true;
898 }
899
900 if (optlen == sizeof(struct xdp_statistics)) {
901 switch (stat_test_type) {
902 case STAT_TEST_RX_DROPPED:
903 xsk_stat = stats.rx_dropped;
904 break;
905 case STAT_TEST_TX_INVALID:
906 return true;
907 case STAT_TEST_RX_FULL:
908 xsk_stat = stats.rx_ring_full;
909 expected_stat -= RX_FULL_RXQSIZE;
910 break;
911 case STAT_TEST_RX_FILL_EMPTY:
912 xsk_stat = stats.rx_fill_ring_empty_descs;
913 break;
914 default:
915 break;
916 }
917
918 if (xsk_stat == expected_stat)
919 return true;
920 }
921
922 return false;
923 }
924
tx_stats_validate(struct ifobject * ifobject)925 static void tx_stats_validate(struct ifobject *ifobject)
926 {
927 struct xsk_socket *xsk = ifobject->xsk->xsk;
928 int fd = xsk_socket__fd(xsk);
929 struct xdp_statistics stats;
930 socklen_t optlen;
931 int err;
932
933 optlen = sizeof(stats);
934 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
935 if (err) {
936 ksft_test_result_fail("ERROR Tx: [%s] getsockopt(XDP_STATISTICS) error %u %s\n",
937 __func__, -err, strerror(-err));
938 return;
939 }
940
941 if (stats.tx_invalid_descs == ifobject->pkt_stream->nb_pkts)
942 return;
943
944 ksft_test_result_fail("ERROR: [%s] tx_invalid_descs incorrect. Got [%u] expected [%u]\n",
945 __func__, stats.tx_invalid_descs, ifobject->pkt_stream->nb_pkts);
946 }
947
thread_common_ops(struct test_spec * test,struct ifobject * ifobject)948 static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
949 {
950 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
951 u32 i;
952
953 ifobject->ns_fd = switch_namespace(ifobject->nsname);
954
955 if (ifobject->umem->unaligned_mode)
956 mmap_flags |= MAP_HUGETLB;
957
958 for (i = 0; i < test->nb_sockets; i++) {
959 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size;
960 u32 ctr = 0;
961 void *bufs;
962 int ret;
963
964 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
965 if (bufs == MAP_FAILED)
966 exit_with_error(errno);
967
968 ret = xsk_configure_umem(&ifobject->umem_arr[i], bufs, umem_sz);
969 if (ret)
970 exit_with_error(-ret);
971
972 while (ctr++ < SOCK_RECONF_CTR) {
973 ret = xsk_configure_socket(&ifobject->xsk_arr[i], &ifobject->umem_arr[i],
974 ifobject, i);
975 if (!ret)
976 break;
977
978 /* Retry if it fails as xsk_socket__create() is asynchronous */
979 if (ctr >= SOCK_RECONF_CTR)
980 exit_with_error(-ret);
981 usleep(USLEEP_MAX);
982 }
983 }
984
985 ifobject->umem = &ifobject->umem_arr[0];
986 ifobject->xsk = &ifobject->xsk_arr[0];
987 }
988
testapp_cleanup_xsk_res(struct ifobject * ifobj)989 static void testapp_cleanup_xsk_res(struct ifobject *ifobj)
990 {
991 print_verbose("Destroying socket\n");
992 xsk_socket__delete(ifobj->xsk->xsk);
993 munmap(ifobj->umem->buffer, ifobj->umem->num_frames * ifobj->umem->frame_size);
994 xsk_umem__delete(ifobj->umem->umem);
995 }
996
worker_testapp_validate_tx(void * arg)997 static void *worker_testapp_validate_tx(void *arg)
998 {
999 struct test_spec *test = (struct test_spec *)arg;
1000 struct ifobject *ifobject = test->ifobj_tx;
1001
1002 if (test->current_step == 1)
1003 thread_common_ops(test, ifobject);
1004
1005 print_verbose("Sending %d packets on interface %s\n", ifobject->pkt_stream->nb_pkts,
1006 ifobject->ifname);
1007 send_pkts(ifobject);
1008
1009 if (stat_test_type == STAT_TEST_TX_INVALID)
1010 tx_stats_validate(ifobject);
1011
1012 if (test->total_steps == test->current_step)
1013 testapp_cleanup_xsk_res(ifobject);
1014 pthread_exit(NULL);
1015 }
1016
xsk_populate_fill_ring(struct xsk_umem_info * umem,struct pkt_stream * pkt_stream)1017 static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream)
1018 {
1019 u32 idx = 0, i, buffers_to_fill;
1020 int ret;
1021
1022 if (umem->num_frames < XSK_RING_PROD__DEFAULT_NUM_DESCS)
1023 buffers_to_fill = umem->num_frames;
1024 else
1025 buffers_to_fill = XSK_RING_PROD__DEFAULT_NUM_DESCS;
1026
1027 ret = xsk_ring_prod__reserve(&umem->fq, buffers_to_fill, &idx);
1028 if (ret != buffers_to_fill)
1029 exit_with_error(ENOSPC);
1030 for (i = 0; i < buffers_to_fill; i++) {
1031 u64 addr;
1032
1033 if (pkt_stream->use_addr_for_fill) {
1034 struct pkt *pkt = pkt_stream_get_pkt(pkt_stream, i);
1035
1036 if (!pkt)
1037 break;
1038 addr = pkt->addr;
1039 } else {
1040 addr = i * umem->frame_size;
1041 }
1042
1043 *xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr;
1044 }
1045 xsk_ring_prod__submit(&umem->fq, buffers_to_fill);
1046 }
1047
worker_testapp_validate_rx(void * arg)1048 static void *worker_testapp_validate_rx(void *arg)
1049 {
1050 struct test_spec *test = (struct test_spec *)arg;
1051 struct ifobject *ifobject = test->ifobj_rx;
1052 struct pollfd fds = { };
1053
1054 if (test->current_step == 1)
1055 thread_common_ops(test, ifobject);
1056
1057 xsk_populate_fill_ring(ifobject->umem, ifobject->pkt_stream);
1058
1059 fds.fd = xsk_socket__fd(ifobject->xsk->xsk);
1060 fds.events = POLLIN;
1061
1062 pthread_barrier_wait(&barr);
1063
1064 if (test_type == TEST_TYPE_STATS)
1065 while (!rx_stats_are_valid(ifobject))
1066 continue;
1067 else
1068 receive_pkts(ifobject->pkt_stream, ifobject->xsk, &fds);
1069
1070 if (test->total_steps == test->current_step)
1071 testapp_cleanup_xsk_res(ifobject);
1072 pthread_exit(NULL);
1073 }
1074
testapp_validate_traffic(struct test_spec * test)1075 static void testapp_validate_traffic(struct test_spec *test)
1076 {
1077 struct ifobject *ifobj_tx = test->ifobj_tx;
1078 struct ifobject *ifobj_rx = test->ifobj_rx;
1079 pthread_t t0, t1;
1080
1081 if (pthread_barrier_init(&barr, NULL, 2))
1082 exit_with_error(errno);
1083
1084 test->current_step++;
1085 pkt_stream_reset(ifobj_rx->pkt_stream);
1086 pkts_in_flight = 0;
1087
1088 /*Spawn RX thread */
1089 pthread_create(&t0, NULL, ifobj_rx->func_ptr, test);
1090
1091 pthread_barrier_wait(&barr);
1092 if (pthread_barrier_destroy(&barr))
1093 exit_with_error(errno);
1094
1095 /*Spawn TX thread */
1096 pthread_create(&t1, NULL, ifobj_tx->func_ptr, test);
1097
1098 pthread_join(t1, NULL);
1099 pthread_join(t0, NULL);
1100 }
1101
testapp_teardown(struct test_spec * test)1102 static void testapp_teardown(struct test_spec *test)
1103 {
1104 int i;
1105
1106 test_spec_set_name(test, "TEARDOWN");
1107 for (i = 0; i < MAX_TEARDOWN_ITER; i++) {
1108 testapp_validate_traffic(test);
1109 test_spec_reset(test);
1110 }
1111 }
1112
swap_directions(struct ifobject ** ifobj1,struct ifobject ** ifobj2)1113 static void swap_directions(struct ifobject **ifobj1, struct ifobject **ifobj2)
1114 {
1115 thread_func_t tmp_func_ptr = (*ifobj1)->func_ptr;
1116 struct ifobject *tmp_ifobj = (*ifobj1);
1117
1118 (*ifobj1)->func_ptr = (*ifobj2)->func_ptr;
1119 (*ifobj2)->func_ptr = tmp_func_ptr;
1120
1121 *ifobj1 = *ifobj2;
1122 *ifobj2 = tmp_ifobj;
1123 }
1124
testapp_bidi(struct test_spec * test)1125 static void testapp_bidi(struct test_spec *test)
1126 {
1127 test_spec_set_name(test, "BIDIRECTIONAL");
1128 test->ifobj_tx->rx_on = true;
1129 test->ifobj_rx->tx_on = true;
1130 test->total_steps = 2;
1131 testapp_validate_traffic(test);
1132
1133 print_verbose("Switching Tx/Rx vectors\n");
1134 swap_directions(&test->ifobj_rx, &test->ifobj_tx);
1135 testapp_validate_traffic(test);
1136
1137 swap_directions(&test->ifobj_rx, &test->ifobj_tx);
1138 }
1139
swap_xsk_resources(struct ifobject * ifobj_tx,struct ifobject * ifobj_rx)1140 static void swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx)
1141 {
1142 xsk_socket__delete(ifobj_tx->xsk->xsk);
1143 xsk_umem__delete(ifobj_tx->umem->umem);
1144 xsk_socket__delete(ifobj_rx->xsk->xsk);
1145 xsk_umem__delete(ifobj_rx->umem->umem);
1146 ifobj_tx->umem = &ifobj_tx->umem_arr[1];
1147 ifobj_tx->xsk = &ifobj_tx->xsk_arr[1];
1148 ifobj_rx->umem = &ifobj_rx->umem_arr[1];
1149 ifobj_rx->xsk = &ifobj_rx->xsk_arr[1];
1150 }
1151
testapp_bpf_res(struct test_spec * test)1152 static void testapp_bpf_res(struct test_spec *test)
1153 {
1154 test_spec_set_name(test, "BPF_RES");
1155 test->total_steps = 2;
1156 test->nb_sockets = 2;
1157 testapp_validate_traffic(test);
1158
1159 swap_xsk_resources(test->ifobj_tx, test->ifobj_rx);
1160 testapp_validate_traffic(test);
1161 }
1162
testapp_headroom(struct test_spec * test)1163 static void testapp_headroom(struct test_spec *test)
1164 {
1165 test_spec_set_name(test, "UMEM_HEADROOM");
1166 test->ifobj_rx->umem->frame_headroom = UMEM_HEADROOM_TEST_SIZE;
1167 testapp_validate_traffic(test);
1168 }
1169
testapp_stats(struct test_spec * test)1170 static void testapp_stats(struct test_spec *test)
1171 {
1172 int i;
1173
1174 for (i = 0; i < STAT_TEST_TYPE_MAX; i++) {
1175 test_spec_reset(test);
1176 stat_test_type = i;
1177 /* No or few packets will be received so cannot pace packets */
1178 test->ifobj_tx->pacing_on = false;
1179
1180 switch (stat_test_type) {
1181 case STAT_TEST_RX_DROPPED:
1182 test_spec_set_name(test, "STAT_RX_DROPPED");
1183 test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size -
1184 XDP_PACKET_HEADROOM - 1;
1185 testapp_validate_traffic(test);
1186 break;
1187 case STAT_TEST_RX_FULL:
1188 test_spec_set_name(test, "STAT_RX_FULL");
1189 test->ifobj_rx->xsk->rxqsize = RX_FULL_RXQSIZE;
1190 testapp_validate_traffic(test);
1191 break;
1192 case STAT_TEST_TX_INVALID:
1193 test_spec_set_name(test, "STAT_TX_INVALID");
1194 pkt_stream_replace(test, DEFAULT_PKT_CNT, XSK_UMEM__INVALID_FRAME_SIZE);
1195 testapp_validate_traffic(test);
1196
1197 pkt_stream_restore_default(test);
1198 break;
1199 case STAT_TEST_RX_FILL_EMPTY:
1200 test_spec_set_name(test, "STAT_RX_FILL_EMPTY");
1201 test->ifobj_rx->pkt_stream = pkt_stream_generate(test->ifobj_rx->umem, 0,
1202 MIN_PKT_SIZE);
1203 if (!test->ifobj_rx->pkt_stream)
1204 exit_with_error(ENOMEM);
1205 test->ifobj_rx->pkt_stream->use_addr_for_fill = true;
1206 testapp_validate_traffic(test);
1207
1208 pkt_stream_restore_default(test);
1209 break;
1210 default:
1211 break;
1212 }
1213 }
1214
1215 /* To only see the whole stat set being completed unless an individual test fails. */
1216 test_spec_set_name(test, "STATS");
1217 }
1218
1219 /* Simple test */
hugepages_present(struct ifobject * ifobject)1220 static bool hugepages_present(struct ifobject *ifobject)
1221 {
1222 const size_t mmap_sz = 2 * ifobject->umem->num_frames * ifobject->umem->frame_size;
1223 void *bufs;
1224
1225 bufs = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1226 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
1227 if (bufs == MAP_FAILED)
1228 return false;
1229
1230 munmap(bufs, mmap_sz);
1231 return true;
1232 }
1233
testapp_unaligned(struct test_spec * test)1234 static bool testapp_unaligned(struct test_spec *test)
1235 {
1236 if (!hugepages_present(test->ifobj_tx)) {
1237 ksft_test_result_skip("No 2M huge pages present.\n");
1238 return false;
1239 }
1240
1241 test_spec_set_name(test, "UNALIGNED_MODE");
1242 test->ifobj_tx->umem->unaligned_mode = true;
1243 test->ifobj_rx->umem->unaligned_mode = true;
1244 /* Let half of the packets straddle a buffer boundrary */
1245 pkt_stream_replace_half(test, PKT_SIZE, -PKT_SIZE / 2);
1246 test->ifobj_rx->pkt_stream->use_addr_for_fill = true;
1247 testapp_validate_traffic(test);
1248
1249 pkt_stream_restore_default(test);
1250 return true;
1251 }
1252
testapp_single_pkt(struct test_spec * test)1253 static void testapp_single_pkt(struct test_spec *test)
1254 {
1255 struct pkt pkts[] = {{0x1000, PKT_SIZE, 0, true}};
1256
1257 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts));
1258 testapp_validate_traffic(test);
1259 pkt_stream_restore_default(test);
1260 }
1261
testapp_invalid_desc(struct test_spec * test)1262 static void testapp_invalid_desc(struct test_spec *test)
1263 {
1264 struct pkt pkts[] = {
1265 /* Zero packet length at address zero allowed */
1266 {0, 0, 0, true},
1267 /* Zero packet length allowed */
1268 {0x1000, 0, 0, true},
1269 /* Straddling the start of umem */
1270 {-2, PKT_SIZE, 0, false},
1271 /* Packet too large */
1272 {0x2000, XSK_UMEM__INVALID_FRAME_SIZE, 0, false},
1273 /* After umem ends */
1274 {UMEM_SIZE, PKT_SIZE, 0, false},
1275 /* Straddle the end of umem */
1276 {UMEM_SIZE - PKT_SIZE / 2, PKT_SIZE, 0, false},
1277 /* Straddle a page boundrary */
1278 {0x3000 - PKT_SIZE / 2, PKT_SIZE, 0, false},
1279 /* Straddle a 2K boundrary */
1280 {0x3800 - PKT_SIZE / 2, PKT_SIZE, 0, true},
1281 /* Valid packet for synch so that something is received */
1282 {0x4000, PKT_SIZE, 0, true}};
1283
1284 if (test->ifobj_tx->umem->unaligned_mode) {
1285 /* Crossing a page boundrary allowed */
1286 pkts[6].valid = true;
1287 }
1288 if (test->ifobj_tx->umem->frame_size == XSK_UMEM__DEFAULT_FRAME_SIZE / 2) {
1289 /* Crossing a 2K frame size boundrary not allowed */
1290 pkts[7].valid = false;
1291 }
1292
1293 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts));
1294 testapp_validate_traffic(test);
1295 pkt_stream_restore_default(test);
1296 }
1297
init_iface(struct ifobject * ifobj,const char * dst_mac,const char * src_mac,const char * dst_ip,const char * src_ip,const u16 dst_port,const u16 src_port,thread_func_t func_ptr)1298 static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac,
1299 const char *dst_ip, const char *src_ip, const u16 dst_port,
1300 const u16 src_port, thread_func_t func_ptr)
1301 {
1302 struct in_addr ip;
1303
1304 memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN);
1305 memcpy(ifobj->src_mac, src_mac, ETH_ALEN);
1306
1307 inet_aton(dst_ip, &ip);
1308 ifobj->dst_ip = ip.s_addr;
1309
1310 inet_aton(src_ip, &ip);
1311 ifobj->src_ip = ip.s_addr;
1312
1313 ifobj->dst_port = dst_port;
1314 ifobj->src_port = src_port;
1315
1316 ifobj->func_ptr = func_ptr;
1317 }
1318
run_pkt_test(struct test_spec * test,enum test_mode mode,enum test_type type)1319 static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_type type)
1320 {
1321 test_type = type;
1322
1323 /* reset defaults after potential previous test */
1324 stat_test_type = -1;
1325
1326 switch (test_type) {
1327 case TEST_TYPE_STATS:
1328 testapp_stats(test);
1329 break;
1330 case TEST_TYPE_TEARDOWN:
1331 testapp_teardown(test);
1332 break;
1333 case TEST_TYPE_BIDI:
1334 testapp_bidi(test);
1335 break;
1336 case TEST_TYPE_BPF_RES:
1337 testapp_bpf_res(test);
1338 break;
1339 case TEST_TYPE_RUN_TO_COMPLETION:
1340 test_spec_set_name(test, "RUN_TO_COMPLETION");
1341 testapp_validate_traffic(test);
1342 break;
1343 case TEST_TYPE_RUN_TO_COMPLETION_SINGLE_PKT:
1344 test_spec_set_name(test, "RUN_TO_COMPLETION_SINGLE_PKT");
1345 testapp_single_pkt(test);
1346 break;
1347 case TEST_TYPE_RUN_TO_COMPLETION_2K_FRAME:
1348 test_spec_set_name(test, "RUN_TO_COMPLETION_2K_FRAME_SIZE");
1349 test->ifobj_tx->umem->frame_size = 2048;
1350 test->ifobj_rx->umem->frame_size = 2048;
1351 pkt_stream_replace(test, DEFAULT_PKT_CNT, MIN_PKT_SIZE);
1352 testapp_validate_traffic(test);
1353
1354 pkt_stream_restore_default(test);
1355 break;
1356 case TEST_TYPE_POLL:
1357 test->ifobj_tx->use_poll = true;
1358 test->ifobj_rx->use_poll = true;
1359 test_spec_set_name(test, "POLL");
1360 testapp_validate_traffic(test);
1361 break;
1362 case TEST_TYPE_ALIGNED_INV_DESC:
1363 test_spec_set_name(test, "ALIGNED_INV_DESC");
1364 testapp_invalid_desc(test);
1365 break;
1366 case TEST_TYPE_ALIGNED_INV_DESC_2K_FRAME:
1367 test_spec_set_name(test, "ALIGNED_INV_DESC_2K_FRAME_SIZE");
1368 test->ifobj_tx->umem->frame_size = 2048;
1369 test->ifobj_rx->umem->frame_size = 2048;
1370 testapp_invalid_desc(test);
1371 break;
1372 case TEST_TYPE_UNALIGNED_INV_DESC:
1373 if (!hugepages_present(test->ifobj_tx)) {
1374 ksft_test_result_skip("No 2M huge pages present.\n");
1375 return;
1376 }
1377 test_spec_set_name(test, "UNALIGNED_INV_DESC");
1378 test->ifobj_tx->umem->unaligned_mode = true;
1379 test->ifobj_rx->umem->unaligned_mode = true;
1380 testapp_invalid_desc(test);
1381 break;
1382 case TEST_TYPE_UNALIGNED:
1383 if (!testapp_unaligned(test))
1384 return;
1385 break;
1386 case TEST_TYPE_HEADROOM:
1387 testapp_headroom(test);
1388 break;
1389 default:
1390 break;
1391 }
1392
1393 print_ksft_result(test);
1394 }
1395
ifobject_create(void)1396 static struct ifobject *ifobject_create(void)
1397 {
1398 struct ifobject *ifobj;
1399
1400 ifobj = calloc(1, sizeof(struct ifobject));
1401 if (!ifobj)
1402 return NULL;
1403
1404 ifobj->xsk_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->xsk_arr));
1405 if (!ifobj->xsk_arr)
1406 goto out_xsk_arr;
1407
1408 ifobj->umem_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->umem_arr));
1409 if (!ifobj->umem_arr)
1410 goto out_umem_arr;
1411
1412 return ifobj;
1413
1414 out_umem_arr:
1415 free(ifobj->xsk_arr);
1416 out_xsk_arr:
1417 free(ifobj);
1418 return NULL;
1419 }
1420
ifobject_delete(struct ifobject * ifobj)1421 static void ifobject_delete(struct ifobject *ifobj)
1422 {
1423 free(ifobj->umem_arr);
1424 free(ifobj->xsk_arr);
1425 free(ifobj);
1426 }
1427
main(int argc,char ** argv)1428 int main(int argc, char **argv)
1429 {
1430 struct rlimit _rlim = { RLIM_INFINITY, RLIM_INFINITY };
1431 struct pkt_stream *pkt_stream_default;
1432 struct ifobject *ifobj_tx, *ifobj_rx;
1433 struct test_spec test;
1434 u32 i, j;
1435
1436 if (setrlimit(RLIMIT_MEMLOCK, &_rlim))
1437 exit_with_error(errno);
1438
1439 ifobj_tx = ifobject_create();
1440 if (!ifobj_tx)
1441 exit_with_error(ENOMEM);
1442 ifobj_rx = ifobject_create();
1443 if (!ifobj_rx)
1444 exit_with_error(ENOMEM);
1445
1446 setlocale(LC_ALL, "");
1447
1448 parse_command_line(ifobj_tx, ifobj_rx, argc, argv);
1449
1450 if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) {
1451 usage(basename(argv[0]));
1452 ksft_exit_xfail();
1453 }
1454
1455 init_iface(ifobj_tx, MAC1, MAC2, IP1, IP2, UDP_PORT1, UDP_PORT2,
1456 worker_testapp_validate_tx);
1457 init_iface(ifobj_rx, MAC2, MAC1, IP2, IP1, UDP_PORT2, UDP_PORT1,
1458 worker_testapp_validate_rx);
1459
1460 test_spec_init(&test, ifobj_tx, ifobj_rx, 0);
1461 pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, PKT_SIZE);
1462 if (!pkt_stream_default)
1463 exit_with_error(ENOMEM);
1464 test.pkt_stream_default = pkt_stream_default;
1465
1466 ksft_set_plan(TEST_MODE_MAX * TEST_TYPE_MAX);
1467
1468 for (i = 0; i < TEST_MODE_MAX; i++)
1469 for (j = 0; j < TEST_TYPE_MAX; j++) {
1470 test_spec_init(&test, ifobj_tx, ifobj_rx, i);
1471 run_pkt_test(&test, i, j);
1472 usleep(USLEEP_MAX);
1473 }
1474
1475 pkt_stream_delete(pkt_stream_default);
1476 ifobject_delete(ifobj_tx);
1477 ifobject_delete(ifobj_rx);
1478
1479 ksft_exit_pass();
1480 return 0;
1481 }
1482