1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2020 Intel Corporation. */
5 * Some functions in this program are taken from
6 * Linux kernel samples/bpf/xdpsock* and modified
9 * See test_xsk.sh for detailed information on test topology
10 * and prerequisite network setup.
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.
18 * These selftests test AF_XDP SKB and Native/DRV modes using veth
19 * Virtual Ethernet interfaces.
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
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
32 * Trigger some error conditions and ensure that the appropriate statistics
33 * are incremented. Within this test, the following statistics are tested:
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.
38 * Set the 'len' field of tx descriptors to an invalid value (umem frame
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
54 * - Single process spawns two threads: Tx and Rx
55 * - Each of these two threads attach to a veth interface within their assigned
57 * - Each thread Creates one AF_XDP socket connected to a unique umem for each
59 * - Tx thread Transmits 10k packets from veth<xxxx> to veth<yyyy>
60 * - Rx thread verifies if all 10k packets were received and delivered in-order,
61 * and have the right content
63 * Enable/disable packet dump mode:
64 * --------------------------
65 * To enable L2 - L4 headers and payload dump of each packet on STDOUT, add
66 * parameter -D to params array in test_xsk.sh, i.e. params=("-S" "-D")
73 #include <asm/barrier.h>
74 #include <linux/if_link.h>
75 #include <linux/if_ether.h>
77 #include <linux/udp.h>
78 #include <arpa/inet.h>
90 #include <sys/resource.h>
91 #include <sys/types.h>
92 #include <sys/queue.h>
95 #include <stdatomic.h>
97 #include "xdpxceiver.h"
98 #include "../kselftest.h"
100 static const char *MAC1 = "\x00\x0A\x56\x9E\xEE\x62";
101 static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
102 static const char *IP1 = "192.168.100.162";
103 static const char *IP2 = "192.168.100.161";
104 static const u16 UDP_PORT1 = 2020;
105 static const u16 UDP_PORT2 = 2121;
107 static void __exit_with_error(int error, const char *file, const char *func, int line)
109 ksft_test_result_fail("[%s:%s:%i]: ERROR: %d/\"%s\"\n", file, func, line, error,
114 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
116 #define mode_string(test) (test)->ifobj_tx->xdp_flags & XDP_FLAGS_SKB_MODE ? "SKB" : "DRV"
118 #define print_ksft_result(test) \
119 (ksft_test_result_pass("PASS: %s %s\n", mode_string(test), (test)->name))
121 static void memset32_htonl(void *dest, u32 val, u32 size)
123 u32 *ptr = (u32 *)dest;
128 for (i = 0; i < (size & (~0x3)); i += 4)
133 * Fold a partial checksum
134 * This function code has been taken from
135 * Linux kernel include/asm-generic/checksum.h
137 static __u16 csum_fold(__u32 csum)
139 u32 sum = (__force u32)csum;
141 sum = (sum & 0xffff) + (sum >> 16);
142 sum = (sum & 0xffff) + (sum >> 16);
143 return (__force __u16)~sum;
147 * This function code has been taken from
148 * Linux kernel lib/checksum.c
150 static u32 from64to32(u64 x)
152 /* add up 32-bit and 32-bit for 32+c bit */
153 x = (x & 0xffffffff) + (x >> 32);
155 x = (x & 0xffffffff) + (x >> 32);
160 * This function code has been taken from
161 * Linux kernel lib/checksum.c
163 static __u32 csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __u32 sum)
165 unsigned long long s = (__force u32)sum;
167 s += (__force u32)saddr;
168 s += (__force u32)daddr;
169 #ifdef __BIG_ENDIAN__
172 s += (proto + len) << 8;
174 return (__force __u32)from64to32(s);
178 * This function has been taken from
179 * Linux kernel include/asm-generic/checksum.h
181 static __u16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len, __u8 proto, __u32 sum)
183 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
186 static u16 udp_csum(u32 saddr, u32 daddr, u32 len, u8 proto, u16 *udp_pkt)
191 /* udp hdr and data */
192 for (; cnt < len; cnt += 2)
193 csum += udp_pkt[cnt >> 1];
195 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
198 static void gen_eth_hdr(struct ifobject *ifobject, struct ethhdr *eth_hdr)
200 memcpy(eth_hdr->h_dest, ifobject->dst_mac, ETH_ALEN);
201 memcpy(eth_hdr->h_source, ifobject->src_mac, ETH_ALEN);
202 eth_hdr->h_proto = htons(ETH_P_IP);
205 static void gen_ip_hdr(struct ifobject *ifobject, struct iphdr *ip_hdr)
207 ip_hdr->version = IP_PKT_VER;
209 ip_hdr->tos = IP_PKT_TOS;
210 ip_hdr->tot_len = htons(IP_PKT_SIZE);
212 ip_hdr->frag_off = 0;
213 ip_hdr->ttl = IPDEFTTL;
214 ip_hdr->protocol = IPPROTO_UDP;
215 ip_hdr->saddr = ifobject->src_ip;
216 ip_hdr->daddr = ifobject->dst_ip;
220 static void gen_udp_hdr(u32 payload, void *pkt, struct ifobject *ifobject,
221 struct udphdr *udp_hdr)
223 udp_hdr->source = htons(ifobject->src_port);
224 udp_hdr->dest = htons(ifobject->dst_port);
225 udp_hdr->len = htons(UDP_PKT_SIZE);
226 memset32_htonl(pkt + PKT_HDR_SIZE, payload, UDP_PKT_DATA_SIZE);
229 static void gen_udp_csum(struct udphdr *udp_hdr, struct iphdr *ip_hdr)
233 udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE, IPPROTO_UDP, (u16 *)udp_hdr);
236 static int xsk_configure_umem(struct xsk_umem_info *umem, void *buffer, u64 size)
238 struct xsk_umem_config cfg = {
239 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS,
240 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
241 .frame_size = umem->frame_size,
242 .frame_headroom = umem->frame_headroom,
243 .flags = XSK_UMEM__DEFAULT_FLAGS
247 if (umem->unaligned_mode)
248 cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
250 ret = xsk_umem__create(&umem->umem, buffer, size,
251 &umem->fq, &umem->cq, &cfg);
255 umem->buffer = buffer;
259 static int xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem,
260 struct ifobject *ifobject, u32 qid)
262 struct xsk_socket_config cfg;
263 struct xsk_ring_cons *rxr;
264 struct xsk_ring_prod *txr;
267 cfg.rx_size = xsk->rxqsize;
268 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
269 cfg.libbpf_flags = 0;
270 cfg.xdp_flags = ifobject->xdp_flags;
271 cfg.bind_flags = ifobject->bind_flags;
273 txr = ifobject->tx_on ? &xsk->tx : NULL;
274 rxr = ifobject->rx_on ? &xsk->rx : NULL;
275 return xsk_socket__create(&xsk->xsk, ifobject->ifname, qid, umem->umem, rxr, txr, &cfg);
278 static struct option long_options[] = {
279 {"interface", required_argument, 0, 'i'},
280 {"queue", optional_argument, 0, 'q'},
281 {"dump-pkts", optional_argument, 0, 'D'},
282 {"verbose", no_argument, 0, 'v'},
286 static void usage(const char *prog)
289 " Usage: %s [OPTIONS]\n"
291 " -i, --interface Use interface\n"
292 " -q, --queue=n Use queue n (default 0)\n"
293 " -D, --dump-pkts Dump packets L2 - L5\n"
294 " -v, --verbose Verbose output\n";
296 ksft_print_msg(str, prog);
299 static int switch_namespace(const char *nsname)
301 char fqns[26] = "/var/run/netns/";
304 if (!nsname || strlen(nsname) == 0)
307 strncat(fqns, nsname, sizeof(fqns) - strlen(fqns) - 1);
308 nsfd = open(fqns, O_RDONLY);
311 exit_with_error(errno);
313 if (setns(nsfd, 0) == -1)
314 exit_with_error(errno);
316 print_verbose("NS switched: %s\n", nsname);
321 static bool validate_interface(struct ifobject *ifobj)
323 if (!strcmp(ifobj->ifname, ""))
328 static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc,
331 struct ifobject *ifobj;
332 u32 interface_nb = 0;
340 c = getopt_long(argc, argv, "i:Dv", long_options, &option_index);
346 if (interface_nb == 0)
348 else if (interface_nb == 1)
353 sptr = strndupa(optarg, strlen(optarg));
354 memcpy(ifobj->ifname, strsep(&sptr, ","), MAX_INTERFACE_NAME_CHARS);
355 token = strsep(&sptr, ",");
357 memcpy(ifobj->nsname, token, MAX_INTERFACES_NAMESPACE_CHARS);
367 usage(basename(argv[0]));
373 static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
374 struct ifobject *ifobj_rx)
378 for (i = 0; i < MAX_INTERFACES; i++) {
379 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
381 ifobj->umem = &ifobj->umem_arr[0];
382 ifobj->xsk = &ifobj->xsk_arr[0];
383 ifobj->use_poll = false;
384 ifobj->pkt_stream = test->pkt_stream_default;
387 ifobj->rx_on = false;
391 ifobj->tx_on = false;
394 for (j = 0; j < MAX_SOCKETS; j++) {
395 memset(&ifobj->umem_arr[j], 0, sizeof(ifobj->umem_arr[j]));
396 memset(&ifobj->xsk_arr[j], 0, sizeof(ifobj->xsk_arr[j]));
397 ifobj->umem_arr[j].num_frames = DEFAULT_PKT_CNT / 4;
398 ifobj->umem_arr[j].frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
399 ifobj->xsk_arr[j].rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS;
403 test->ifobj_tx = ifobj_tx;
404 test->ifobj_rx = ifobj_rx;
405 test->current_step = 0;
406 test->total_steps = 1;
407 test->nb_sockets = 1;
410 static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
411 struct ifobject *ifobj_rx, enum test_mode mode)
413 struct pkt_stream *pkt_stream;
416 pkt_stream = test->pkt_stream_default;
417 memset(test, 0, sizeof(*test));
418 test->pkt_stream_default = pkt_stream;
420 for (i = 0; i < MAX_INTERFACES; i++) {
421 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx;
423 ifobj->xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
424 if (mode == TEST_MODE_SKB)
425 ifobj->xdp_flags |= XDP_FLAGS_SKB_MODE;
427 ifobj->xdp_flags |= XDP_FLAGS_DRV_MODE;
429 ifobj->bind_flags = XDP_USE_NEED_WAKEUP | XDP_COPY;
432 __test_spec_init(test, ifobj_tx, ifobj_rx);
435 static void test_spec_reset(struct test_spec *test)
437 __test_spec_init(test, test->ifobj_tx, test->ifobj_rx);
440 static void test_spec_set_name(struct test_spec *test, const char *name)
442 strncpy(test->name, name, MAX_TEST_NAME_SIZE);
445 static struct pkt *pkt_stream_get_pkt(struct pkt_stream *pkt_stream, u32 pkt_nb)
447 if (pkt_nb >= pkt_stream->nb_pkts)
450 return &pkt_stream->pkts[pkt_nb];
453 static void pkt_stream_delete(struct pkt_stream *pkt_stream)
455 free(pkt_stream->pkts);
459 static void pkt_stream_restore_default(struct test_spec *test)
461 pkt_stream_delete(test->ifobj_tx->pkt_stream);
462 test->ifobj_tx->pkt_stream = test->pkt_stream_default;
463 test->ifobj_rx->pkt_stream = test->pkt_stream_default;
466 static struct pkt_stream *pkt_stream_generate(struct xsk_umem_info *umem, u32 nb_pkts, u32 pkt_len)
468 struct pkt_stream *pkt_stream;
471 pkt_stream = calloc(1, sizeof(*pkt_stream));
473 exit_with_error(ENOMEM);
475 pkt_stream->pkts = calloc(nb_pkts, sizeof(*pkt_stream->pkts));
476 if (!pkt_stream->pkts)
477 exit_with_error(ENOMEM);
479 pkt_stream->nb_pkts = nb_pkts;
480 for (i = 0; i < nb_pkts; i++) {
481 pkt_stream->pkts[i].addr = (i % umem->num_frames) * umem->frame_size +
483 pkt_stream->pkts[i].len = pkt_len;
484 pkt_stream->pkts[i].payload = i;
486 if (pkt_len > umem->frame_size)
487 pkt_stream->pkts[i].valid = false;
489 pkt_stream->pkts[i].valid = true;
495 static struct pkt_stream *pkt_stream_clone(struct xsk_umem_info *umem,
496 struct pkt_stream *pkt_stream)
498 return pkt_stream_generate(umem, pkt_stream->nb_pkts, pkt_stream->pkts[0].len);
501 static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len)
503 struct pkt_stream *pkt_stream;
505 pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, nb_pkts, pkt_len);
506 test->ifobj_tx->pkt_stream = pkt_stream;
507 test->ifobj_rx->pkt_stream = pkt_stream;
510 static void pkt_stream_replace_half(struct test_spec *test, u32 pkt_len, u32 offset)
512 struct xsk_umem_info *umem = test->ifobj_tx->umem;
513 struct pkt_stream *pkt_stream;
516 pkt_stream = pkt_stream_clone(umem, test->pkt_stream_default);
517 for (i = 0; i < test->pkt_stream_default->nb_pkts; i += 2) {
518 pkt_stream->pkts[i].addr = (i % umem->num_frames) * umem->frame_size + offset;
519 pkt_stream->pkts[i].len = pkt_len;
522 test->ifobj_tx->pkt_stream = pkt_stream;
523 test->ifobj_rx->pkt_stream = pkt_stream;
526 static struct pkt *pkt_generate(struct ifobject *ifobject, u32 pkt_nb)
528 struct pkt *pkt = pkt_stream_get_pkt(ifobject->pkt_stream, pkt_nb);
529 struct udphdr *udp_hdr;
530 struct ethhdr *eth_hdr;
531 struct iphdr *ip_hdr;
537 data = xsk_umem__get_data(ifobject->umem->buffer, pkt->addr);
538 udp_hdr = (struct udphdr *)(data + sizeof(struct ethhdr) + sizeof(struct iphdr));
539 ip_hdr = (struct iphdr *)(data + sizeof(struct ethhdr));
540 eth_hdr = (struct ethhdr *)data;
542 gen_udp_hdr(pkt_nb, data, ifobject, udp_hdr);
543 gen_ip_hdr(ifobject, ip_hdr);
544 gen_udp_csum(udp_hdr, ip_hdr);
545 gen_eth_hdr(ifobject, eth_hdr);
550 static void pkt_dump(void *pkt, u32 len)
552 char s[INET_ADDRSTRLEN];
553 struct ethhdr *ethhdr;
554 struct udphdr *udphdr;
559 iphdr = pkt + sizeof(*ethhdr);
560 udphdr = pkt + sizeof(*ethhdr) + sizeof(*iphdr);
562 /*extract L2 frame */
563 fprintf(stdout, "DEBUG>> L2: dst mac: ");
564 for (i = 0; i < ETH_ALEN; i++)
565 fprintf(stdout, "%02X", ethhdr->h_dest[i]);
567 fprintf(stdout, "\nDEBUG>> L2: src mac: ");
568 for (i = 0; i < ETH_ALEN; i++)
569 fprintf(stdout, "%02X", ethhdr->h_source[i]);
571 /*extract L3 frame */
572 fprintf(stdout, "\nDEBUG>> L3: ip_hdr->ihl: %02X\n", iphdr->ihl);
573 fprintf(stdout, "DEBUG>> L3: ip_hdr->saddr: %s\n",
574 inet_ntop(AF_INET, &iphdr->saddr, s, sizeof(s)));
575 fprintf(stdout, "DEBUG>> L3: ip_hdr->daddr: %s\n",
576 inet_ntop(AF_INET, &iphdr->daddr, s, sizeof(s)));
577 /*extract L4 frame */
578 fprintf(stdout, "DEBUG>> L4: udp_hdr->src: %d\n", ntohs(udphdr->source));
579 fprintf(stdout, "DEBUG>> L4: udp_hdr->dst: %d\n", ntohs(udphdr->dest));
580 /*extract L5 frame */
581 payload = *((uint32_t *)(pkt + PKT_HDR_SIZE));
583 fprintf(stdout, "DEBUG>> L5: payload: %d\n", payload);
584 fprintf(stdout, "---------------------------------------\n");
587 static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 len)
589 void *data = xsk_umem__get_data(buffer, addr);
590 struct iphdr *iphdr = (struct iphdr *)(data + sizeof(struct ethhdr));
593 ksft_test_result_fail("ERROR: [%s] too many packets received\n", __func__);
597 if (iphdr->version == IP_PKT_VER && iphdr->tos == IP_PKT_TOS) {
598 u32 seqnum = ntohl(*((u32 *)(data + PKT_HDR_SIZE)));
601 pkt_dump(data, PKT_SIZE);
603 if (pkt->len != len) {
604 ksft_test_result_fail
605 ("ERROR: [%s] expected length [%d], got length [%d]\n",
606 __func__, pkt->len, len);
610 if (pkt->payload != seqnum) {
611 ksft_test_result_fail
612 ("ERROR: [%s] expected seqnum [%d], got seqnum [%d]\n",
613 __func__, pkt->payload, seqnum);
617 ksft_print_msg("Invalid frame received: ");
618 ksft_print_msg("[IP_PKT_VER: %02X], [IP_PKT_TOS: %02X]\n", iphdr->version,
626 static void kick_tx(struct xsk_socket_info *xsk)
630 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
631 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN)
633 exit_with_error(errno);
636 static void complete_pkts(struct xsk_socket_info *xsk, int batch_size)
641 if (xsk_ring_prod__needs_wakeup(&xsk->tx))
644 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
646 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
647 xsk->outstanding_tx -= rcvd;
651 static void receive_pkts(struct pkt_stream *pkt_stream, struct xsk_socket_info *xsk,
654 u32 idx_rx = 0, idx_fq = 0, rcvd, i, pkt_count = 0;
658 pkt = pkt_stream_get_pkt(pkt_stream, pkt_count++);
660 rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx);
662 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
663 ret = poll(fds, 1, POLL_TMOUT);
665 exit_with_error(-ret);
670 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
671 while (ret != rcvd) {
673 exit_with_error(-ret);
674 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
675 ret = poll(fds, 1, POLL_TMOUT);
677 exit_with_error(-ret);
679 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
682 for (i = 0; i < rcvd; i++) {
683 const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++);
684 u64 addr = desc->addr, orig;
686 orig = xsk_umem__extract_addr(addr);
687 addr = xsk_umem__add_offset_to_addr(addr);
688 if (!is_pkt_valid(pkt, xsk->umem->buffer, addr, desc->len))
691 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
692 pkt = pkt_stream_get_pkt(pkt_stream, pkt_count++);
695 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
696 xsk_ring_cons__release(&xsk->rx, rcvd);
700 static u32 __send_pkts(struct ifobject *ifobject, u32 pkt_nb)
702 struct xsk_socket_info *xsk = ifobject->xsk;
703 u32 i, idx, valid_pkts = 0;
705 while (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) < BATCH_SIZE)
706 complete_pkts(xsk, BATCH_SIZE);
708 for (i = 0; i < BATCH_SIZE; i++) {
709 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx + i);
710 struct pkt *pkt = pkt_generate(ifobject, pkt_nb);
715 tx_desc->addr = pkt->addr;
716 tx_desc->len = pkt->len;
722 xsk_ring_prod__submit(&xsk->tx, i);
723 xsk->outstanding_tx += valid_pkts;
724 complete_pkts(xsk, BATCH_SIZE);
729 static void wait_for_tx_completion(struct xsk_socket_info *xsk)
731 while (xsk->outstanding_tx)
732 complete_pkts(xsk, BATCH_SIZE);
735 static void send_pkts(struct ifobject *ifobject)
737 struct pollfd fds = { };
740 fds.fd = xsk_socket__fd(ifobject->xsk->xsk);
741 fds.events = POLLOUT;
743 while (pkt_cnt < ifobject->pkt_stream->nb_pkts) {
746 if (ifobject->use_poll) {
749 ret = poll(&fds, 1, POLL_TMOUT);
753 if (!(fds.revents & POLLOUT))
757 sent = __send_pkts(ifobject, pkt_cnt);
762 wait_for_tx_completion(ifobject->xsk);
765 static bool rx_stats_are_valid(struct ifobject *ifobject)
767 u32 xsk_stat = 0, expected_stat = ifobject->pkt_stream->nb_pkts;
768 struct xsk_socket *xsk = ifobject->xsk->xsk;
769 int fd = xsk_socket__fd(xsk);
770 struct xdp_statistics stats;
774 optlen = sizeof(stats);
775 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
777 ksft_test_result_fail("ERROR Rx: [%s] getsockopt(XDP_STATISTICS) error %u %s\n",
778 __func__, -err, strerror(-err));
782 if (optlen == sizeof(struct xdp_statistics)) {
783 switch (stat_test_type) {
784 case STAT_TEST_RX_DROPPED:
785 xsk_stat = stats.rx_dropped;
787 case STAT_TEST_TX_INVALID:
789 case STAT_TEST_RX_FULL:
790 xsk_stat = stats.rx_ring_full;
791 expected_stat -= RX_FULL_RXQSIZE;
793 case STAT_TEST_RX_FILL_EMPTY:
794 xsk_stat = stats.rx_fill_ring_empty_descs;
800 if (xsk_stat == expected_stat)
807 static void tx_stats_validate(struct ifobject *ifobject)
809 struct xsk_socket *xsk = ifobject->xsk->xsk;
810 int fd = xsk_socket__fd(xsk);
811 struct xdp_statistics stats;
815 optlen = sizeof(stats);
816 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
818 ksft_test_result_fail("ERROR Tx: [%s] getsockopt(XDP_STATISTICS) error %u %s\n",
819 __func__, -err, strerror(-err));
823 if (stats.tx_invalid_descs == ifobject->pkt_stream->nb_pkts)
826 ksft_test_result_fail("ERROR: [%s] tx_invalid_descs incorrect. Got [%u] expected [%u]\n",
827 __func__, stats.tx_invalid_descs, ifobject->pkt_stream->nb_pkts);
830 static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject)
832 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
835 ifobject->ns_fd = switch_namespace(ifobject->nsname);
837 if (ifobject->umem->unaligned_mode)
838 mmap_flags |= MAP_HUGETLB;
840 for (i = 0; i < test->nb_sockets; i++) {
841 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size;
845 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
846 if (bufs == MAP_FAILED)
847 exit_with_error(errno);
849 while (ctr++ < SOCK_RECONF_CTR) {
852 ret = xsk_configure_umem(&ifobject->umem_arr[i], bufs, umem_sz);
854 exit_with_error(-ret);
856 ret = xsk_configure_socket(&ifobject->xsk_arr[i], &ifobject->umem_arr[i],
861 /* Retry if it fails as xsk_socket__create() is asynchronous */
862 if (ctr >= SOCK_RECONF_CTR)
863 exit_with_error(-ret);
868 ifobject->umem = &ifobject->umem_arr[0];
869 ifobject->xsk = &ifobject->xsk_arr[0];
872 static void testapp_cleanup_xsk_res(struct ifobject *ifobj)
874 print_verbose("Destroying socket\n");
875 xsk_socket__delete(ifobj->xsk->xsk);
876 xsk_umem__delete(ifobj->umem->umem);
879 static void *worker_testapp_validate_tx(void *arg)
881 struct test_spec *test = (struct test_spec *)arg;
882 struct ifobject *ifobject = test->ifobj_tx;
884 if (test->current_step == 1)
885 thread_common_ops(test, ifobject);
887 print_verbose("Sending %d packets on interface %s\n", ifobject->pkt_stream->nb_pkts,
891 if (stat_test_type == STAT_TEST_TX_INVALID)
892 tx_stats_validate(ifobject);
894 if (test->total_steps == test->current_step)
895 testapp_cleanup_xsk_res(ifobject);
899 static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream)
904 ret = xsk_ring_prod__reserve(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS, &idx);
905 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS)
906 exit_with_error(ENOSPC);
907 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS; i++) {
910 if (pkt_stream->use_addr_for_fill) {
911 struct pkt *pkt = pkt_stream_get_pkt(pkt_stream, i);
917 addr = (i % umem->num_frames) * umem->frame_size + DEFAULT_OFFSET;
920 *xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr;
922 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS);
925 static void *worker_testapp_validate_rx(void *arg)
927 struct test_spec *test = (struct test_spec *)arg;
928 struct ifobject *ifobject = test->ifobj_rx;
929 struct pollfd fds = { };
931 if (test->current_step == 1)
932 thread_common_ops(test, ifobject);
934 if (stat_test_type != STAT_TEST_RX_FILL_EMPTY)
935 xsk_populate_fill_ring(ifobject->umem, ifobject->pkt_stream);
937 fds.fd = xsk_socket__fd(ifobject->xsk->xsk);
940 pthread_barrier_wait(&barr);
942 if (test_type == TEST_TYPE_STATS)
943 while (!rx_stats_are_valid(ifobject))
946 receive_pkts(ifobject->pkt_stream, ifobject->xsk, &fds);
948 if (test->total_steps == test->current_step)
949 testapp_cleanup_xsk_res(ifobject);
953 static void testapp_validate_traffic(struct test_spec *test)
955 struct ifobject *ifobj_tx = test->ifobj_tx;
956 struct ifobject *ifobj_rx = test->ifobj_rx;
959 if (pthread_barrier_init(&barr, NULL, 2))
960 exit_with_error(errno);
962 test->current_step++;
965 pthread_create(&t0, NULL, ifobj_rx->func_ptr, test);
967 pthread_barrier_wait(&barr);
968 if (pthread_barrier_destroy(&barr))
969 exit_with_error(errno);
972 pthread_create(&t1, NULL, ifobj_tx->func_ptr, test);
974 pthread_join(t1, NULL);
975 pthread_join(t0, NULL);
978 static void testapp_teardown(struct test_spec *test)
982 test_spec_set_name(test, "TEARDOWN");
983 for (i = 0; i < MAX_TEARDOWN_ITER; i++) {
984 testapp_validate_traffic(test);
985 test_spec_reset(test);
989 static void swap_directions(struct ifobject **ifobj1, struct ifobject **ifobj2)
991 thread_func_t tmp_func_ptr = (*ifobj1)->func_ptr;
992 struct ifobject *tmp_ifobj = (*ifobj1);
994 (*ifobj1)->func_ptr = (*ifobj2)->func_ptr;
995 (*ifobj2)->func_ptr = tmp_func_ptr;
1001 static void testapp_bidi(struct test_spec *test)
1003 test_spec_set_name(test, "BIDIRECTIONAL");
1004 test->ifobj_tx->rx_on = true;
1005 test->ifobj_rx->tx_on = true;
1006 test->total_steps = 2;
1007 testapp_validate_traffic(test);
1009 print_verbose("Switching Tx/Rx vectors\n");
1010 swap_directions(&test->ifobj_rx, &test->ifobj_tx);
1011 testapp_validate_traffic(test);
1013 swap_directions(&test->ifobj_rx, &test->ifobj_tx);
1016 static void swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx)
1018 xsk_socket__delete(ifobj_tx->xsk->xsk);
1019 xsk_umem__delete(ifobj_tx->umem->umem);
1020 xsk_socket__delete(ifobj_rx->xsk->xsk);
1021 xsk_umem__delete(ifobj_rx->umem->umem);
1022 ifobj_tx->umem = &ifobj_tx->umem_arr[1];
1023 ifobj_tx->xsk = &ifobj_tx->xsk_arr[1];
1024 ifobj_rx->umem = &ifobj_rx->umem_arr[1];
1025 ifobj_rx->xsk = &ifobj_rx->xsk_arr[1];
1028 static void testapp_bpf_res(struct test_spec *test)
1030 test_spec_set_name(test, "BPF_RES");
1031 test->total_steps = 2;
1032 test->nb_sockets = 2;
1033 testapp_validate_traffic(test);
1035 swap_xsk_resources(test->ifobj_tx, test->ifobj_rx);
1036 testapp_validate_traffic(test);
1039 static void testapp_stats(struct test_spec *test)
1043 for (i = 0; i < STAT_TEST_TYPE_MAX; i++) {
1044 test_spec_reset(test);
1047 switch (stat_test_type) {
1048 case STAT_TEST_RX_DROPPED:
1049 test_spec_set_name(test, "STAT_RX_DROPPED");
1050 test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size -
1051 XDP_PACKET_HEADROOM - 1;
1052 testapp_validate_traffic(test);
1054 case STAT_TEST_RX_FULL:
1055 test_spec_set_name(test, "STAT_RX_FULL");
1056 test->ifobj_rx->xsk->rxqsize = RX_FULL_RXQSIZE;
1057 testapp_validate_traffic(test);
1059 case STAT_TEST_TX_INVALID:
1060 test_spec_set_name(test, "STAT_TX_INVALID");
1061 pkt_stream_replace(test, DEFAULT_PKT_CNT, XSK_UMEM__INVALID_FRAME_SIZE);
1062 testapp_validate_traffic(test);
1064 pkt_stream_restore_default(test);
1066 case STAT_TEST_RX_FILL_EMPTY:
1067 test_spec_set_name(test, "STAT_RX_FILL_EMPTY");
1068 testapp_validate_traffic(test);
1075 /* To only see the whole stat set being completed unless an individual test fails. */
1076 test_spec_set_name(test, "STATS");
1080 static bool hugepages_present(struct ifobject *ifobject)
1082 const size_t mmap_sz = 2 * ifobject->umem->num_frames * ifobject->umem->frame_size;
1085 bufs = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE,
1086 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_HUGETLB, -1, 0);
1087 if (bufs == MAP_FAILED)
1090 munmap(bufs, mmap_sz);
1094 static bool testapp_unaligned(struct test_spec *test)
1096 if (!hugepages_present(test->ifobj_tx)) {
1097 ksft_test_result_skip("No 2M huge pages present.\n");
1101 test_spec_set_name(test, "UNALIGNED_MODE");
1102 test->ifobj_tx->umem->unaligned_mode = true;
1103 test->ifobj_rx->umem->unaligned_mode = true;
1104 /* Let half of the packets straddle a buffer boundrary */
1105 pkt_stream_replace_half(test, PKT_SIZE, test->ifobj_tx->umem->frame_size - 32);
1106 test->ifobj_rx->pkt_stream->use_addr_for_fill = true;
1107 testapp_validate_traffic(test);
1109 pkt_stream_restore_default(test);
1113 static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac,
1114 const char *dst_ip, const char *src_ip, const u16 dst_port,
1115 const u16 src_port, thread_func_t func_ptr)
1119 memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN);
1120 memcpy(ifobj->src_mac, src_mac, ETH_ALEN);
1122 inet_aton(dst_ip, &ip);
1123 ifobj->dst_ip = ip.s_addr;
1125 inet_aton(src_ip, &ip);
1126 ifobj->src_ip = ip.s_addr;
1128 ifobj->dst_port = dst_port;
1129 ifobj->src_port = src_port;
1131 ifobj->func_ptr = func_ptr;
1134 static void run_pkt_test(struct test_spec *test, enum test_mode mode, enum test_type type)
1138 /* reset defaults after potential previous test */
1139 stat_test_type = -1;
1141 switch (test_type) {
1142 case TEST_TYPE_STATS:
1143 testapp_stats(test);
1145 case TEST_TYPE_TEARDOWN:
1146 testapp_teardown(test);
1148 case TEST_TYPE_BIDI:
1151 case TEST_TYPE_BPF_RES:
1152 testapp_bpf_res(test);
1154 case TEST_TYPE_NOPOLL:
1155 test_spec_set_name(test, "RUN_TO_COMPLETION");
1156 testapp_validate_traffic(test);
1158 case TEST_TYPE_POLL:
1159 test->ifobj_tx->use_poll = true;
1160 test->ifobj_rx->use_poll = true;
1161 test_spec_set_name(test, "POLL");
1162 testapp_validate_traffic(test);
1164 case TEST_TYPE_UNALIGNED:
1165 if (!testapp_unaligned(test))
1172 print_ksft_result(test);
1175 static struct ifobject *ifobject_create(void)
1177 struct ifobject *ifobj;
1179 ifobj = calloc(1, sizeof(struct ifobject));
1183 ifobj->xsk_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->xsk_arr));
1184 if (!ifobj->xsk_arr)
1187 ifobj->umem_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->umem_arr));
1188 if (!ifobj->umem_arr)
1194 free(ifobj->xsk_arr);
1200 static void ifobject_delete(struct ifobject *ifobj)
1202 free(ifobj->umem_arr);
1203 free(ifobj->xsk_arr);
1207 int main(int argc, char **argv)
1209 struct rlimit _rlim = { RLIM_INFINITY, RLIM_INFINITY };
1210 struct pkt_stream *pkt_stream_default;
1211 struct ifobject *ifobj_tx, *ifobj_rx;
1212 struct test_spec test;
1215 if (setrlimit(RLIMIT_MEMLOCK, &_rlim))
1216 exit_with_error(errno);
1218 ifobj_tx = ifobject_create();
1220 exit_with_error(ENOMEM);
1221 ifobj_rx = ifobject_create();
1223 exit_with_error(ENOMEM);
1225 setlocale(LC_ALL, "");
1227 parse_command_line(ifobj_tx, ifobj_rx, argc, argv);
1229 if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) {
1230 usage(basename(argv[0]));
1234 init_iface(ifobj_tx, MAC1, MAC2, IP1, IP2, UDP_PORT1, UDP_PORT2,
1235 worker_testapp_validate_tx);
1236 init_iface(ifobj_rx, MAC2, MAC1, IP2, IP1, UDP_PORT2, UDP_PORT1,
1237 worker_testapp_validate_rx);
1239 test_spec_init(&test, ifobj_tx, ifobj_rx, 0);
1240 pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, PKT_SIZE);
1241 if (!pkt_stream_default)
1242 exit_with_error(ENOMEM);
1243 test.pkt_stream_default = pkt_stream_default;
1245 ksft_set_plan(TEST_MODE_MAX * TEST_TYPE_MAX);
1247 for (i = 0; i < TEST_MODE_MAX; i++)
1248 for (j = 0; j < TEST_TYPE_MAX; j++) {
1249 test_spec_init(&test, ifobj_tx, ifobj_rx, i);
1250 run_pkt_test(&test, i, j);
1254 pkt_stream_delete(pkt_stream_default);
1255 ifobject_delete(ifobj_tx);
1256 ifobject_delete(ifobj_rx);