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