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 * j. If multi-buffer is supported, send 9k packets divided into 3 frames 53 * k. If multi-buffer and huge pages are supported, send 9k packets in a single frame 54 * using unaligned mode 55 * l. If multi-buffer is supported, try various nasty combinations of descriptors to 56 * check if they pass the validation or not 57 * 58 * Flow: 59 * ----- 60 * - Single process spawns two threads: Tx and Rx 61 * - Each of these two threads attach to a veth interface 62 * - Each thread creates one AF_XDP socket connected to a unique umem for each 63 * veth interface 64 * - Tx thread Transmits a number of packets from veth<xxxx> to veth<yyyy> 65 * - Rx thread verifies if all packets were received and delivered in-order, 66 * and have the right content 67 * 68 * Enable/disable packet dump mode: 69 * -------------------------- 70 * To enable L2 - L4 headers and payload dump of each packet on STDOUT, add 71 * parameter -D to params array in test_xsk.sh, i.e. params=("-S" "-D") 72 */ 73 74 #define _GNU_SOURCE 75 #include <assert.h> 76 #include <fcntl.h> 77 #include <errno.h> 78 #include <getopt.h> 79 #include <linux/if_link.h> 80 #include <linux/if_ether.h> 81 #include <linux/mman.h> 82 #include <linux/netdev.h> 83 #include <arpa/inet.h> 84 #include <net/if.h> 85 #include <locale.h> 86 #include <poll.h> 87 #include <pthread.h> 88 #include <signal.h> 89 #include <stdio.h> 90 #include <stdlib.h> 91 #include <string.h> 92 #include <stddef.h> 93 #include <sys/mman.h> 94 #include <sys/socket.h> 95 #include <sys/time.h> 96 #include <sys/types.h> 97 #include <unistd.h> 98 99 #include "xsk_xdp_progs.skel.h" 100 #include "xsk.h" 101 #include "xskxceiver.h" 102 #include <bpf/bpf.h> 103 #include <linux/filter.h> 104 #include "../kselftest.h" 105 #include "xsk_xdp_metadata.h" 106 107 static const char *MAC1 = "\x00\x0A\x56\x9E\xEE\x62"; 108 static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61"; 109 110 static bool opt_verbose; 111 static bool opt_print_tests; 112 static enum test_mode opt_mode = TEST_MODE_ALL; 113 static u32 opt_run_test = RUN_ALL_TESTS; 114 115 static void __exit_with_error(int error, const char *file, const char *func, int line) 116 { 117 ksft_test_result_fail("[%s:%s:%i]: ERROR: %d/\"%s\"\n", file, func, line, error, 118 strerror(error)); 119 ksft_exit_xfail(); 120 } 121 122 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__) 123 #define busy_poll_string(test) (test)->ifobj_tx->busy_poll ? "BUSY-POLL " : "" 124 static char *mode_string(struct test_spec *test) 125 { 126 switch (test->mode) { 127 case TEST_MODE_SKB: 128 return "SKB"; 129 case TEST_MODE_DRV: 130 return "DRV"; 131 case TEST_MODE_ZC: 132 return "ZC"; 133 default: 134 return "BOGUS"; 135 } 136 } 137 138 static void report_failure(struct test_spec *test) 139 { 140 if (test->fail) 141 return; 142 143 ksft_test_result_fail("FAIL: %s %s%s\n", mode_string(test), busy_poll_string(test), 144 test->name); 145 test->fail = true; 146 } 147 148 /* The payload is a word consisting of a packet sequence number in the upper 149 * 16-bits and a intra packet data sequence number in the lower 16 bits. So the 3rd packet's 150 * 5th word of data will contain the number (2<<16) | 4 as they are numbered from 0. 151 */ 152 static void write_payload(void *dest, u32 pkt_nb, u32 start, u32 size) 153 { 154 u32 *ptr = (u32 *)dest, i; 155 156 start /= sizeof(*ptr); 157 size /= sizeof(*ptr); 158 for (i = 0; i < size; i++) 159 ptr[i] = htonl(pkt_nb << 16 | (i + start)); 160 } 161 162 static void gen_eth_hdr(struct ifobject *ifobject, struct ethhdr *eth_hdr) 163 { 164 memcpy(eth_hdr->h_dest, ifobject->dst_mac, ETH_ALEN); 165 memcpy(eth_hdr->h_source, ifobject->src_mac, ETH_ALEN); 166 eth_hdr->h_proto = htons(ETH_P_LOOPBACK); 167 } 168 169 static bool is_umem_valid(struct ifobject *ifobj) 170 { 171 return !!ifobj->umem->umem; 172 } 173 174 static u32 mode_to_xdp_flags(enum test_mode mode) 175 { 176 return (mode == TEST_MODE_SKB) ? XDP_FLAGS_SKB_MODE : XDP_FLAGS_DRV_MODE; 177 } 178 179 static u64 umem_size(struct xsk_umem_info *umem) 180 { 181 return umem->num_frames * umem->frame_size; 182 } 183 184 static int xsk_configure_umem(struct ifobject *ifobj, struct xsk_umem_info *umem, void *buffer, 185 u64 size) 186 { 187 struct xsk_umem_config cfg = { 188 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, 189 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, 190 .frame_size = umem->frame_size, 191 .frame_headroom = umem->frame_headroom, 192 .flags = XSK_UMEM__DEFAULT_FLAGS 193 }; 194 int ret; 195 196 if (umem->unaligned_mode) 197 cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG; 198 199 ret = xsk_umem__create(&umem->umem, buffer, size, 200 &umem->fq, &umem->cq, &cfg); 201 if (ret) 202 return ret; 203 204 umem->buffer = buffer; 205 if (ifobj->shared_umem && ifobj->rx_on) { 206 umem->base_addr = umem_size(umem); 207 umem->next_buffer = umem_size(umem); 208 } 209 210 return 0; 211 } 212 213 static u64 umem_alloc_buffer(struct xsk_umem_info *umem) 214 { 215 u64 addr; 216 217 addr = umem->next_buffer; 218 umem->next_buffer += umem->frame_size; 219 if (umem->next_buffer >= umem->base_addr + umem_size(umem)) 220 umem->next_buffer = umem->base_addr; 221 222 return addr; 223 } 224 225 static void umem_reset_alloc(struct xsk_umem_info *umem) 226 { 227 umem->next_buffer = 0; 228 } 229 230 static void enable_busy_poll(struct xsk_socket_info *xsk) 231 { 232 int sock_opt; 233 234 sock_opt = 1; 235 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL, 236 (void *)&sock_opt, sizeof(sock_opt)) < 0) 237 exit_with_error(errno); 238 239 sock_opt = 20; 240 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL, 241 (void *)&sock_opt, sizeof(sock_opt)) < 0) 242 exit_with_error(errno); 243 244 sock_opt = BATCH_SIZE; 245 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET, 246 (void *)&sock_opt, sizeof(sock_opt)) < 0) 247 exit_with_error(errno); 248 } 249 250 static int __xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem, 251 struct ifobject *ifobject, bool shared) 252 { 253 struct xsk_socket_config cfg = {}; 254 struct xsk_ring_cons *rxr; 255 struct xsk_ring_prod *txr; 256 257 xsk->umem = umem; 258 cfg.rx_size = xsk->rxqsize; 259 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS; 260 cfg.bind_flags = ifobject->bind_flags; 261 if (shared) 262 cfg.bind_flags |= XDP_SHARED_UMEM; 263 if (ifobject->pkt_stream && ifobject->mtu > MAX_ETH_PKT_SIZE) 264 cfg.bind_flags |= XDP_USE_SG; 265 266 txr = ifobject->tx_on ? &xsk->tx : NULL; 267 rxr = ifobject->rx_on ? &xsk->rx : NULL; 268 return xsk_socket__create(&xsk->xsk, ifobject->ifindex, 0, umem->umem, rxr, txr, &cfg); 269 } 270 271 static bool ifobj_zc_avail(struct ifobject *ifobject) 272 { 273 size_t umem_sz = DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE; 274 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 275 struct xsk_socket_info *xsk; 276 struct xsk_umem_info *umem; 277 bool zc_avail = false; 278 void *bufs; 279 int ret; 280 281 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); 282 if (bufs == MAP_FAILED) 283 exit_with_error(errno); 284 285 umem = calloc(1, sizeof(struct xsk_umem_info)); 286 if (!umem) { 287 munmap(bufs, umem_sz); 288 exit_with_error(ENOMEM); 289 } 290 umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE; 291 ret = xsk_configure_umem(ifobject, umem, bufs, umem_sz); 292 if (ret) 293 exit_with_error(-ret); 294 295 xsk = calloc(1, sizeof(struct xsk_socket_info)); 296 if (!xsk) 297 goto out; 298 ifobject->bind_flags = XDP_USE_NEED_WAKEUP | XDP_ZEROCOPY; 299 ifobject->rx_on = true; 300 xsk->rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS; 301 ret = __xsk_configure_socket(xsk, umem, ifobject, false); 302 if (!ret) 303 zc_avail = true; 304 305 xsk_socket__delete(xsk->xsk); 306 free(xsk); 307 out: 308 munmap(umem->buffer, umem_sz); 309 xsk_umem__delete(umem->umem); 310 free(umem); 311 return zc_avail; 312 } 313 314 static struct option long_options[] = { 315 {"interface", required_argument, 0, 'i'}, 316 {"busy-poll", no_argument, 0, 'b'}, 317 {"verbose", no_argument, 0, 'v'}, 318 {"mode", required_argument, 0, 'm'}, 319 {"list", no_argument, 0, 'l'}, 320 {"test", required_argument, 0, 't'}, 321 {"help", no_argument, 0, 'h'}, 322 {0, 0, 0, 0} 323 }; 324 325 static void print_usage(char **argv) 326 { 327 const char *str = 328 " Usage: xskxceiver [OPTIONS]\n" 329 " Options:\n" 330 " -i, --interface Use interface\n" 331 " -v, --verbose Verbose output\n" 332 " -b, --busy-poll Enable busy poll\n" 333 " -m, --mode Run only mode skb, drv, or zc\n" 334 " -l, --list List all available tests\n" 335 " -t, --test Run a specific test. Enter number from -l option.\n" 336 " -h, --help Display this help and exit\n"; 337 338 ksft_print_msg(str, basename(argv[0])); 339 ksft_exit_xfail(); 340 } 341 342 static bool validate_interface(struct ifobject *ifobj) 343 { 344 if (!strcmp(ifobj->ifname, "")) 345 return false; 346 return true; 347 } 348 349 static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc, 350 char **argv) 351 { 352 struct ifobject *ifobj; 353 u32 interface_nb = 0; 354 int option_index, c; 355 356 opterr = 0; 357 358 for (;;) { 359 c = getopt_long(argc, argv, "i:vbm:lt:", long_options, &option_index); 360 if (c == -1) 361 break; 362 363 switch (c) { 364 case 'i': 365 if (interface_nb == 0) 366 ifobj = ifobj_tx; 367 else if (interface_nb == 1) 368 ifobj = ifobj_rx; 369 else 370 break; 371 372 memcpy(ifobj->ifname, optarg, 373 min_t(size_t, MAX_INTERFACE_NAME_CHARS, strlen(optarg))); 374 375 ifobj->ifindex = if_nametoindex(ifobj->ifname); 376 if (!ifobj->ifindex) 377 exit_with_error(errno); 378 379 interface_nb++; 380 break; 381 case 'v': 382 opt_verbose = true; 383 break; 384 case 'b': 385 ifobj_tx->busy_poll = true; 386 ifobj_rx->busy_poll = true; 387 break; 388 case 'm': 389 if (!strncmp("skb", optarg, strlen(optarg))) 390 opt_mode = TEST_MODE_SKB; 391 else if (!strncmp("drv", optarg, strlen(optarg))) 392 opt_mode = TEST_MODE_DRV; 393 else if (!strncmp("zc", optarg, strlen(optarg))) 394 opt_mode = TEST_MODE_ZC; 395 else 396 print_usage(argv); 397 break; 398 case 'l': 399 opt_print_tests = true; 400 break; 401 case 't': 402 errno = 0; 403 opt_run_test = strtol(optarg, NULL, 0); 404 if (errno) 405 print_usage(argv); 406 break; 407 case 'h': 408 default: 409 print_usage(argv); 410 } 411 } 412 } 413 414 static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx, 415 struct ifobject *ifobj_rx) 416 { 417 u32 i, j; 418 419 for (i = 0; i < MAX_INTERFACES; i++) { 420 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx; 421 422 ifobj->xsk = &ifobj->xsk_arr[0]; 423 ifobj->use_poll = false; 424 ifobj->use_fill_ring = true; 425 ifobj->release_rx = true; 426 ifobj->validation_func = NULL; 427 ifobj->use_metadata = false; 428 429 if (i == 0) { 430 ifobj->rx_on = false; 431 ifobj->tx_on = true; 432 ifobj->pkt_stream = test->tx_pkt_stream_default; 433 } else { 434 ifobj->rx_on = true; 435 ifobj->tx_on = false; 436 ifobj->pkt_stream = test->rx_pkt_stream_default; 437 } 438 439 memset(ifobj->umem, 0, sizeof(*ifobj->umem)); 440 ifobj->umem->num_frames = DEFAULT_UMEM_BUFFERS; 441 ifobj->umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE; 442 443 for (j = 0; j < MAX_SOCKETS; j++) { 444 memset(&ifobj->xsk_arr[j], 0, sizeof(ifobj->xsk_arr[j])); 445 ifobj->xsk_arr[j].rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS; 446 } 447 } 448 449 test->ifobj_tx = ifobj_tx; 450 test->ifobj_rx = ifobj_rx; 451 test->current_step = 0; 452 test->total_steps = 1; 453 test->nb_sockets = 1; 454 test->fail = false; 455 test->mtu = MAX_ETH_PKT_SIZE; 456 test->xdp_prog_rx = ifobj_rx->xdp_progs->progs.xsk_def_prog; 457 test->xskmap_rx = ifobj_rx->xdp_progs->maps.xsk; 458 test->xdp_prog_tx = ifobj_tx->xdp_progs->progs.xsk_def_prog; 459 test->xskmap_tx = ifobj_tx->xdp_progs->maps.xsk; 460 } 461 462 static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx, 463 struct ifobject *ifobj_rx, enum test_mode mode, 464 const struct test_spec *test_to_run) 465 { 466 struct pkt_stream *tx_pkt_stream; 467 struct pkt_stream *rx_pkt_stream; 468 u32 i; 469 470 tx_pkt_stream = test->tx_pkt_stream_default; 471 rx_pkt_stream = test->rx_pkt_stream_default; 472 memset(test, 0, sizeof(*test)); 473 test->tx_pkt_stream_default = tx_pkt_stream; 474 test->rx_pkt_stream_default = rx_pkt_stream; 475 476 for (i = 0; i < MAX_INTERFACES; i++) { 477 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx; 478 479 ifobj->bind_flags = XDP_USE_NEED_WAKEUP; 480 if (mode == TEST_MODE_ZC) 481 ifobj->bind_flags |= XDP_ZEROCOPY; 482 else 483 ifobj->bind_flags |= XDP_COPY; 484 } 485 486 strncpy(test->name, test_to_run->name, MAX_TEST_NAME_SIZE); 487 test->test_func = test_to_run->test_func; 488 test->mode = mode; 489 __test_spec_init(test, ifobj_tx, ifobj_rx); 490 } 491 492 static void test_spec_reset(struct test_spec *test) 493 { 494 __test_spec_init(test, test->ifobj_tx, test->ifobj_rx); 495 } 496 497 static void test_spec_set_xdp_prog(struct test_spec *test, struct bpf_program *xdp_prog_rx, 498 struct bpf_program *xdp_prog_tx, struct bpf_map *xskmap_rx, 499 struct bpf_map *xskmap_tx) 500 { 501 test->xdp_prog_rx = xdp_prog_rx; 502 test->xdp_prog_tx = xdp_prog_tx; 503 test->xskmap_rx = xskmap_rx; 504 test->xskmap_tx = xskmap_tx; 505 } 506 507 static int test_spec_set_mtu(struct test_spec *test, int mtu) 508 { 509 int err; 510 511 if (test->ifobj_rx->mtu != mtu) { 512 err = xsk_set_mtu(test->ifobj_rx->ifindex, mtu); 513 if (err) 514 return err; 515 test->ifobj_rx->mtu = mtu; 516 } 517 if (test->ifobj_tx->mtu != mtu) { 518 err = xsk_set_mtu(test->ifobj_tx->ifindex, mtu); 519 if (err) 520 return err; 521 test->ifobj_tx->mtu = mtu; 522 } 523 524 return 0; 525 } 526 527 static void pkt_stream_reset(struct pkt_stream *pkt_stream) 528 { 529 if (pkt_stream) 530 pkt_stream->current_pkt_nb = 0; 531 } 532 533 static struct pkt *pkt_stream_get_next_tx_pkt(struct pkt_stream *pkt_stream) 534 { 535 if (pkt_stream->current_pkt_nb >= pkt_stream->nb_pkts) 536 return NULL; 537 538 return &pkt_stream->pkts[pkt_stream->current_pkt_nb++]; 539 } 540 541 static struct pkt *pkt_stream_get_next_rx_pkt(struct pkt_stream *pkt_stream, u32 *pkts_sent) 542 { 543 while (pkt_stream->current_pkt_nb < pkt_stream->nb_pkts) { 544 (*pkts_sent)++; 545 if (pkt_stream->pkts[pkt_stream->current_pkt_nb].valid) 546 return &pkt_stream->pkts[pkt_stream->current_pkt_nb++]; 547 pkt_stream->current_pkt_nb++; 548 } 549 return NULL; 550 } 551 552 static void pkt_stream_delete(struct pkt_stream *pkt_stream) 553 { 554 free(pkt_stream->pkts); 555 free(pkt_stream); 556 } 557 558 static void pkt_stream_restore_default(struct test_spec *test) 559 { 560 struct pkt_stream *tx_pkt_stream = test->ifobj_tx->pkt_stream; 561 struct pkt_stream *rx_pkt_stream = test->ifobj_rx->pkt_stream; 562 563 if (tx_pkt_stream != test->tx_pkt_stream_default) { 564 pkt_stream_delete(test->ifobj_tx->pkt_stream); 565 test->ifobj_tx->pkt_stream = test->tx_pkt_stream_default; 566 } 567 568 if (rx_pkt_stream != test->rx_pkt_stream_default) { 569 pkt_stream_delete(test->ifobj_rx->pkt_stream); 570 test->ifobj_rx->pkt_stream = test->rx_pkt_stream_default; 571 } 572 } 573 574 static struct pkt_stream *__pkt_stream_alloc(u32 nb_pkts) 575 { 576 struct pkt_stream *pkt_stream; 577 578 pkt_stream = calloc(1, sizeof(*pkt_stream)); 579 if (!pkt_stream) 580 return NULL; 581 582 pkt_stream->pkts = calloc(nb_pkts, sizeof(*pkt_stream->pkts)); 583 if (!pkt_stream->pkts) { 584 free(pkt_stream); 585 return NULL; 586 } 587 588 pkt_stream->nb_pkts = nb_pkts; 589 return pkt_stream; 590 } 591 592 static bool pkt_continues(u32 options) 593 { 594 return options & XDP_PKT_CONTD; 595 } 596 597 static u32 ceil_u32(u32 a, u32 b) 598 { 599 return (a + b - 1) / b; 600 } 601 602 static u32 pkt_nb_frags(u32 frame_size, struct pkt_stream *pkt_stream, struct pkt *pkt) 603 { 604 u32 nb_frags = 1, next_frag; 605 606 if (!pkt) 607 return 1; 608 609 if (!pkt_stream->verbatim) { 610 if (!pkt->valid || !pkt->len) 611 return 1; 612 return ceil_u32(pkt->len, frame_size); 613 } 614 615 /* Search for the end of the packet in verbatim mode */ 616 if (!pkt_continues(pkt->options)) 617 return nb_frags; 618 619 next_frag = pkt_stream->current_pkt_nb; 620 pkt++; 621 while (next_frag++ < pkt_stream->nb_pkts) { 622 nb_frags++; 623 if (!pkt_continues(pkt->options) || !pkt->valid) 624 break; 625 pkt++; 626 } 627 return nb_frags; 628 } 629 630 static void pkt_set(struct xsk_umem_info *umem, struct pkt *pkt, int offset, u32 len) 631 { 632 pkt->offset = offset; 633 pkt->len = len; 634 if (len > MAX_ETH_JUMBO_SIZE) 635 pkt->valid = false; 636 else 637 pkt->valid = true; 638 } 639 640 static u32 pkt_get_buffer_len(struct xsk_umem_info *umem, u32 len) 641 { 642 return ceil_u32(len, umem->frame_size) * umem->frame_size; 643 } 644 645 static struct pkt_stream *pkt_stream_generate(struct xsk_umem_info *umem, u32 nb_pkts, u32 pkt_len) 646 { 647 struct pkt_stream *pkt_stream; 648 u32 i; 649 650 pkt_stream = __pkt_stream_alloc(nb_pkts); 651 if (!pkt_stream) 652 exit_with_error(ENOMEM); 653 654 pkt_stream->nb_pkts = nb_pkts; 655 pkt_stream->max_pkt_len = pkt_len; 656 for (i = 0; i < nb_pkts; i++) { 657 struct pkt *pkt = &pkt_stream->pkts[i]; 658 659 pkt_set(umem, pkt, 0, pkt_len); 660 pkt->pkt_nb = i; 661 } 662 663 return pkt_stream; 664 } 665 666 static struct pkt_stream *pkt_stream_clone(struct xsk_umem_info *umem, 667 struct pkt_stream *pkt_stream) 668 { 669 return pkt_stream_generate(umem, pkt_stream->nb_pkts, pkt_stream->pkts[0].len); 670 } 671 672 static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len) 673 { 674 struct pkt_stream *pkt_stream; 675 676 pkt_stream = pkt_stream_generate(test->ifobj_tx->umem, nb_pkts, pkt_len); 677 test->ifobj_tx->pkt_stream = pkt_stream; 678 pkt_stream = pkt_stream_generate(test->ifobj_rx->umem, nb_pkts, pkt_len); 679 test->ifobj_rx->pkt_stream = pkt_stream; 680 } 681 682 static void __pkt_stream_replace_half(struct ifobject *ifobj, u32 pkt_len, 683 int offset) 684 { 685 struct xsk_umem_info *umem = ifobj->umem; 686 struct pkt_stream *pkt_stream; 687 u32 i; 688 689 pkt_stream = pkt_stream_clone(umem, ifobj->pkt_stream); 690 for (i = 1; i < ifobj->pkt_stream->nb_pkts; i += 2) 691 pkt_set(umem, &pkt_stream->pkts[i], offset, pkt_len); 692 693 ifobj->pkt_stream = pkt_stream; 694 } 695 696 static void pkt_stream_replace_half(struct test_spec *test, u32 pkt_len, int offset) 697 { 698 __pkt_stream_replace_half(test->ifobj_tx, pkt_len, offset); 699 __pkt_stream_replace_half(test->ifobj_rx, pkt_len, offset); 700 } 701 702 static void pkt_stream_receive_half(struct test_spec *test) 703 { 704 struct xsk_umem_info *umem = test->ifobj_rx->umem; 705 struct pkt_stream *pkt_stream = test->ifobj_tx->pkt_stream; 706 u32 i; 707 708 test->ifobj_rx->pkt_stream = pkt_stream_generate(umem, pkt_stream->nb_pkts, 709 pkt_stream->pkts[0].len); 710 pkt_stream = test->ifobj_rx->pkt_stream; 711 for (i = 1; i < pkt_stream->nb_pkts; i += 2) 712 pkt_stream->pkts[i].valid = false; 713 } 714 715 static u64 pkt_get_addr(struct pkt *pkt, struct xsk_umem_info *umem) 716 { 717 if (!pkt->valid) 718 return pkt->offset; 719 return pkt->offset + umem_alloc_buffer(umem); 720 } 721 722 static void pkt_stream_cancel(struct pkt_stream *pkt_stream) 723 { 724 pkt_stream->current_pkt_nb--; 725 } 726 727 static void pkt_generate(struct ifobject *ifobject, u64 addr, u32 len, u32 pkt_nb, 728 u32 bytes_written) 729 { 730 void *data = xsk_umem__get_data(ifobject->umem->buffer, addr); 731 732 if (len < MIN_PKT_SIZE) 733 return; 734 735 if (!bytes_written) { 736 gen_eth_hdr(ifobject, data); 737 738 len -= PKT_HDR_SIZE; 739 data += PKT_HDR_SIZE; 740 } else { 741 bytes_written -= PKT_HDR_SIZE; 742 } 743 744 write_payload(data, pkt_nb, bytes_written, len); 745 } 746 747 static struct pkt_stream *__pkt_stream_generate_custom(struct ifobject *ifobj, struct pkt *frames, 748 u32 nb_frames, bool verbatim) 749 { 750 u32 i, len = 0, pkt_nb = 0, payload = 0; 751 struct pkt_stream *pkt_stream; 752 753 pkt_stream = __pkt_stream_alloc(nb_frames); 754 if (!pkt_stream) 755 exit_with_error(ENOMEM); 756 757 for (i = 0; i < nb_frames; i++) { 758 struct pkt *pkt = &pkt_stream->pkts[pkt_nb]; 759 struct pkt *frame = &frames[i]; 760 761 pkt->offset = frame->offset; 762 if (verbatim) { 763 *pkt = *frame; 764 pkt->pkt_nb = payload; 765 if (!frame->valid || !pkt_continues(frame->options)) 766 payload++; 767 } else { 768 if (frame->valid) 769 len += frame->len; 770 if (frame->valid && pkt_continues(frame->options)) 771 continue; 772 773 pkt->pkt_nb = pkt_nb; 774 pkt->len = len; 775 pkt->valid = frame->valid; 776 pkt->options = 0; 777 778 len = 0; 779 } 780 781 print_verbose("offset: %d len: %u valid: %u options: %u pkt_nb: %u\n", 782 pkt->offset, pkt->len, pkt->valid, pkt->options, pkt->pkt_nb); 783 784 if (pkt->valid && pkt->len > pkt_stream->max_pkt_len) 785 pkt_stream->max_pkt_len = pkt->len; 786 pkt_nb++; 787 } 788 789 pkt_stream->nb_pkts = pkt_nb; 790 pkt_stream->verbatim = verbatim; 791 return pkt_stream; 792 } 793 794 static void pkt_stream_generate_custom(struct test_spec *test, struct pkt *pkts, u32 nb_pkts) 795 { 796 struct pkt_stream *pkt_stream; 797 798 pkt_stream = __pkt_stream_generate_custom(test->ifobj_tx, pkts, nb_pkts, true); 799 test->ifobj_tx->pkt_stream = pkt_stream; 800 801 pkt_stream = __pkt_stream_generate_custom(test->ifobj_rx, pkts, nb_pkts, false); 802 test->ifobj_rx->pkt_stream = pkt_stream; 803 } 804 805 static void pkt_print_data(u32 *data, u32 cnt) 806 { 807 u32 i; 808 809 for (i = 0; i < cnt; i++) { 810 u32 seqnum, pkt_nb; 811 812 seqnum = ntohl(*data) & 0xffff; 813 pkt_nb = ntohl(*data) >> 16; 814 ksft_print_msg("%u:%u ", pkt_nb, seqnum); 815 data++; 816 } 817 } 818 819 static void pkt_dump(void *pkt, u32 len, bool eth_header) 820 { 821 struct ethhdr *ethhdr = pkt; 822 u32 i, *data; 823 824 if (eth_header) { 825 /*extract L2 frame */ 826 ksft_print_msg("DEBUG>> L2: dst mac: "); 827 for (i = 0; i < ETH_ALEN; i++) 828 ksft_print_msg("%02X", ethhdr->h_dest[i]); 829 830 ksft_print_msg("\nDEBUG>> L2: src mac: "); 831 for (i = 0; i < ETH_ALEN; i++) 832 ksft_print_msg("%02X", ethhdr->h_source[i]); 833 834 data = pkt + PKT_HDR_SIZE; 835 } else { 836 data = pkt; 837 } 838 839 /*extract L5 frame */ 840 ksft_print_msg("\nDEBUG>> L5: seqnum: "); 841 pkt_print_data(data, PKT_DUMP_NB_TO_PRINT); 842 ksft_print_msg("...."); 843 if (len > PKT_DUMP_NB_TO_PRINT * sizeof(u32)) { 844 ksft_print_msg("\n.... "); 845 pkt_print_data(data + len / sizeof(u32) - PKT_DUMP_NB_TO_PRINT, 846 PKT_DUMP_NB_TO_PRINT); 847 } 848 ksft_print_msg("\n---------------------------------------\n"); 849 } 850 851 static bool is_offset_correct(struct xsk_umem_info *umem, struct pkt *pkt, u64 addr) 852 { 853 u32 headroom = umem->unaligned_mode ? 0 : umem->frame_headroom; 854 u32 offset = addr % umem->frame_size, expected_offset; 855 int pkt_offset = pkt->valid ? pkt->offset : 0; 856 857 if (!umem->unaligned_mode) 858 pkt_offset = 0; 859 860 expected_offset = (pkt_offset + headroom + XDP_PACKET_HEADROOM) % umem->frame_size; 861 862 if (offset == expected_offset) 863 return true; 864 865 ksft_print_msg("[%s] expected [%u], got [%u]\n", __func__, expected_offset, offset); 866 return false; 867 } 868 869 static bool is_metadata_correct(struct pkt *pkt, void *buffer, u64 addr) 870 { 871 void *data = xsk_umem__get_data(buffer, addr); 872 struct xdp_info *meta = data - sizeof(struct xdp_info); 873 874 if (meta->count != pkt->pkt_nb) { 875 ksft_print_msg("[%s] expected meta_count [%d], got meta_count [%d]\n", 876 __func__, pkt->pkt_nb, meta->count); 877 return false; 878 } 879 880 return true; 881 } 882 883 static bool is_frag_valid(struct xsk_umem_info *umem, u64 addr, u32 len, u32 expected_pkt_nb, 884 u32 bytes_processed) 885 { 886 u32 seqnum, pkt_nb, *pkt_data, words_to_end, expected_seqnum; 887 void *data = xsk_umem__get_data(umem->buffer, addr); 888 889 addr -= umem->base_addr; 890 891 if (addr >= umem->num_frames * umem->frame_size || 892 addr + len > umem->num_frames * umem->frame_size) { 893 ksft_print_msg("Frag invalid addr: %llx len: %u\n", addr, len); 894 return false; 895 } 896 if (!umem->unaligned_mode && addr % umem->frame_size + len > umem->frame_size) { 897 ksft_print_msg("Frag crosses frame boundary addr: %llx len: %u\n", addr, len); 898 return false; 899 } 900 901 pkt_data = data; 902 if (!bytes_processed) { 903 pkt_data += PKT_HDR_SIZE / sizeof(*pkt_data); 904 len -= PKT_HDR_SIZE; 905 } else { 906 bytes_processed -= PKT_HDR_SIZE; 907 } 908 909 expected_seqnum = bytes_processed / sizeof(*pkt_data); 910 seqnum = ntohl(*pkt_data) & 0xffff; 911 pkt_nb = ntohl(*pkt_data) >> 16; 912 913 if (expected_pkt_nb != pkt_nb) { 914 ksft_print_msg("[%s] expected pkt_nb [%u], got pkt_nb [%u]\n", 915 __func__, expected_pkt_nb, pkt_nb); 916 goto error; 917 } 918 if (expected_seqnum != seqnum) { 919 ksft_print_msg("[%s] expected seqnum at start [%u], got seqnum [%u]\n", 920 __func__, expected_seqnum, seqnum); 921 goto error; 922 } 923 924 words_to_end = len / sizeof(*pkt_data) - 1; 925 pkt_data += words_to_end; 926 seqnum = ntohl(*pkt_data) & 0xffff; 927 expected_seqnum += words_to_end; 928 if (expected_seqnum != seqnum) { 929 ksft_print_msg("[%s] expected seqnum at end [%u], got seqnum [%u]\n", 930 __func__, expected_seqnum, seqnum); 931 goto error; 932 } 933 934 return true; 935 936 error: 937 pkt_dump(data, len, !bytes_processed); 938 return false; 939 } 940 941 static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 len) 942 { 943 if (pkt->len != len) { 944 ksft_print_msg("[%s] expected packet length [%d], got length [%d]\n", 945 __func__, pkt->len, len); 946 pkt_dump(xsk_umem__get_data(buffer, addr), len, true); 947 return false; 948 } 949 950 return true; 951 } 952 953 static int kick_tx(struct xsk_socket_info *xsk) 954 { 955 int ret; 956 957 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0); 958 if (ret >= 0) 959 return TEST_PASS; 960 if (errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN) { 961 usleep(100); 962 return TEST_PASS; 963 } 964 return TEST_FAILURE; 965 } 966 967 static int kick_rx(struct xsk_socket_info *xsk) 968 { 969 int ret; 970 971 ret = recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL); 972 if (ret < 0) 973 return TEST_FAILURE; 974 975 return TEST_PASS; 976 } 977 978 static int complete_pkts(struct xsk_socket_info *xsk, int batch_size) 979 { 980 unsigned int rcvd; 981 u32 idx; 982 int ret; 983 984 if (xsk_ring_prod__needs_wakeup(&xsk->tx)) { 985 ret = kick_tx(xsk); 986 if (ret) 987 return TEST_FAILURE; 988 } 989 990 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx); 991 if (rcvd) { 992 if (rcvd > xsk->outstanding_tx) { 993 u64 addr = *xsk_ring_cons__comp_addr(&xsk->umem->cq, idx + rcvd - 1); 994 995 ksft_print_msg("[%s] Too many packets completed\n", __func__); 996 ksft_print_msg("Last completion address: %llx\n", addr); 997 return TEST_FAILURE; 998 } 999 1000 xsk_ring_cons__release(&xsk->umem->cq, rcvd); 1001 xsk->outstanding_tx -= rcvd; 1002 } 1003 1004 return TEST_PASS; 1005 } 1006 1007 static int receive_pkts(struct test_spec *test, struct pollfd *fds) 1008 { 1009 struct timeval tv_end, tv_now, tv_timeout = {THREAD_TMOUT, 0}; 1010 struct pkt_stream *pkt_stream = test->ifobj_rx->pkt_stream; 1011 struct xsk_socket_info *xsk = test->ifobj_rx->xsk; 1012 u32 idx_rx = 0, idx_fq = 0, rcvd, pkts_sent = 0; 1013 struct ifobject *ifobj = test->ifobj_rx; 1014 struct xsk_umem_info *umem = xsk->umem; 1015 struct pkt *pkt; 1016 int ret; 1017 1018 ret = gettimeofday(&tv_now, NULL); 1019 if (ret) 1020 exit_with_error(errno); 1021 timeradd(&tv_now, &tv_timeout, &tv_end); 1022 1023 pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &pkts_sent); 1024 while (pkt) { 1025 u32 frags_processed = 0, nb_frags = 0, pkt_len = 0; 1026 u64 first_addr; 1027 1028 ret = gettimeofday(&tv_now, NULL); 1029 if (ret) 1030 exit_with_error(errno); 1031 if (timercmp(&tv_now, &tv_end, >)) { 1032 ksft_print_msg("ERROR: [%s] Receive loop timed out\n", __func__); 1033 return TEST_FAILURE; 1034 } 1035 1036 ret = kick_rx(xsk); 1037 if (ret) 1038 return TEST_FAILURE; 1039 1040 if (ifobj->use_poll) { 1041 ret = poll(fds, 1, POLL_TMOUT); 1042 if (ret < 0) 1043 return TEST_FAILURE; 1044 1045 if (!ret) { 1046 if (!is_umem_valid(test->ifobj_tx)) 1047 return TEST_PASS; 1048 1049 ksft_print_msg("ERROR: [%s] Poll timed out\n", __func__); 1050 return TEST_FAILURE; 1051 } 1052 1053 if (!(fds->revents & POLLIN)) 1054 continue; 1055 } 1056 1057 rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx); 1058 if (!rcvd) 1059 continue; 1060 1061 if (ifobj->use_fill_ring) { 1062 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq); 1063 while (ret != rcvd) { 1064 if (xsk_ring_prod__needs_wakeup(&umem->fq)) { 1065 ret = poll(fds, 1, POLL_TMOUT); 1066 if (ret < 0) 1067 return TEST_FAILURE; 1068 } 1069 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq); 1070 } 1071 } 1072 1073 while (frags_processed < rcvd) { 1074 const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++); 1075 u64 addr = desc->addr, orig; 1076 1077 orig = xsk_umem__extract_addr(addr); 1078 addr = xsk_umem__add_offset_to_addr(addr); 1079 1080 if (!pkt) { 1081 ksft_print_msg("[%s] received too many packets addr: %lx len %u\n", 1082 __func__, addr, desc->len); 1083 return TEST_FAILURE; 1084 } 1085 1086 print_verbose("Rx: addr: %lx len: %u options: %u pkt_nb: %u valid: %u\n", 1087 addr, desc->len, desc->options, pkt->pkt_nb, pkt->valid); 1088 1089 if (!is_frag_valid(umem, addr, desc->len, pkt->pkt_nb, pkt_len) || 1090 !is_offset_correct(umem, pkt, addr) || 1091 (ifobj->use_metadata && !is_metadata_correct(pkt, umem->buffer, addr))) 1092 return TEST_FAILURE; 1093 1094 if (!nb_frags++) 1095 first_addr = addr; 1096 frags_processed++; 1097 pkt_len += desc->len; 1098 if (ifobj->use_fill_ring) 1099 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = orig; 1100 1101 if (pkt_continues(desc->options)) 1102 continue; 1103 1104 /* The complete packet has been received */ 1105 if (!is_pkt_valid(pkt, umem->buffer, first_addr, pkt_len) || 1106 !is_offset_correct(umem, pkt, addr)) 1107 return TEST_FAILURE; 1108 1109 pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &pkts_sent); 1110 nb_frags = 0; 1111 pkt_len = 0; 1112 } 1113 1114 if (nb_frags) { 1115 /* In the middle of a packet. Start over from beginning of packet. */ 1116 idx_rx -= nb_frags; 1117 xsk_ring_cons__cancel(&xsk->rx, nb_frags); 1118 if (ifobj->use_fill_ring) { 1119 idx_fq -= nb_frags; 1120 xsk_ring_prod__cancel(&umem->fq, nb_frags); 1121 } 1122 frags_processed -= nb_frags; 1123 } 1124 1125 if (ifobj->use_fill_ring) 1126 xsk_ring_prod__submit(&umem->fq, frags_processed); 1127 if (ifobj->release_rx) 1128 xsk_ring_cons__release(&xsk->rx, frags_processed); 1129 1130 pthread_mutex_lock(&pacing_mutex); 1131 pkts_in_flight -= pkts_sent; 1132 pthread_mutex_unlock(&pacing_mutex); 1133 pkts_sent = 0; 1134 } 1135 1136 return TEST_PASS; 1137 } 1138 1139 static int __send_pkts(struct ifobject *ifobject, struct pollfd *fds, bool timeout) 1140 { 1141 u32 i, idx = 0, valid_pkts = 0, valid_frags = 0, buffer_len; 1142 struct pkt_stream *pkt_stream = ifobject->pkt_stream; 1143 struct xsk_socket_info *xsk = ifobject->xsk; 1144 struct xsk_umem_info *umem = ifobject->umem; 1145 bool use_poll = ifobject->use_poll; 1146 int ret; 1147 1148 buffer_len = pkt_get_buffer_len(umem, pkt_stream->max_pkt_len); 1149 /* pkts_in_flight might be negative if many invalid packets are sent */ 1150 if (pkts_in_flight >= (int)((umem_size(umem) - BATCH_SIZE * buffer_len) / buffer_len)) { 1151 ret = kick_tx(xsk); 1152 if (ret) 1153 return TEST_FAILURE; 1154 return TEST_CONTINUE; 1155 } 1156 1157 while (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) < BATCH_SIZE) { 1158 if (use_poll) { 1159 ret = poll(fds, 1, POLL_TMOUT); 1160 if (timeout) { 1161 if (ret < 0) { 1162 ksft_print_msg("ERROR: [%s] Poll error %d\n", 1163 __func__, errno); 1164 return TEST_FAILURE; 1165 } 1166 if (ret == 0) 1167 return TEST_PASS; 1168 break; 1169 } 1170 if (ret <= 0) { 1171 ksft_print_msg("ERROR: [%s] Poll error %d\n", 1172 __func__, errno); 1173 return TEST_FAILURE; 1174 } 1175 } 1176 1177 complete_pkts(xsk, BATCH_SIZE); 1178 } 1179 1180 for (i = 0; i < BATCH_SIZE; i++) { 1181 struct pkt *pkt = pkt_stream_get_next_tx_pkt(pkt_stream); 1182 u32 nb_frags_left, nb_frags, bytes_written = 0; 1183 1184 if (!pkt) 1185 break; 1186 1187 nb_frags = pkt_nb_frags(umem->frame_size, pkt_stream, pkt); 1188 if (nb_frags > BATCH_SIZE - i) { 1189 pkt_stream_cancel(pkt_stream); 1190 xsk_ring_prod__cancel(&xsk->tx, BATCH_SIZE - i); 1191 break; 1192 } 1193 nb_frags_left = nb_frags; 1194 1195 while (nb_frags_left--) { 1196 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx + i); 1197 1198 tx_desc->addr = pkt_get_addr(pkt, ifobject->umem); 1199 if (pkt_stream->verbatim) { 1200 tx_desc->len = pkt->len; 1201 tx_desc->options = pkt->options; 1202 } else if (nb_frags_left) { 1203 tx_desc->len = umem->frame_size; 1204 tx_desc->options = XDP_PKT_CONTD; 1205 } else { 1206 tx_desc->len = pkt->len - bytes_written; 1207 tx_desc->options = 0; 1208 } 1209 if (pkt->valid) 1210 pkt_generate(ifobject, tx_desc->addr, tx_desc->len, pkt->pkt_nb, 1211 bytes_written); 1212 bytes_written += tx_desc->len; 1213 1214 print_verbose("Tx addr: %llx len: %u options: %u pkt_nb: %u\n", 1215 tx_desc->addr, tx_desc->len, tx_desc->options, pkt->pkt_nb); 1216 1217 if (nb_frags_left) { 1218 i++; 1219 if (pkt_stream->verbatim) 1220 pkt = pkt_stream_get_next_tx_pkt(pkt_stream); 1221 } 1222 } 1223 1224 if (pkt && pkt->valid) { 1225 valid_pkts++; 1226 valid_frags += nb_frags; 1227 } 1228 } 1229 1230 pthread_mutex_lock(&pacing_mutex); 1231 pkts_in_flight += valid_pkts; 1232 pthread_mutex_unlock(&pacing_mutex); 1233 1234 xsk_ring_prod__submit(&xsk->tx, i); 1235 xsk->outstanding_tx += valid_frags; 1236 1237 if (use_poll) { 1238 ret = poll(fds, 1, POLL_TMOUT); 1239 if (ret <= 0) { 1240 if (ret == 0 && timeout) 1241 return TEST_PASS; 1242 1243 ksft_print_msg("ERROR: [%s] Poll error %d\n", __func__, ret); 1244 return TEST_FAILURE; 1245 } 1246 } 1247 1248 if (!timeout) { 1249 if (complete_pkts(xsk, i)) 1250 return TEST_FAILURE; 1251 1252 usleep(10); 1253 return TEST_PASS; 1254 } 1255 1256 return TEST_CONTINUE; 1257 } 1258 1259 static int wait_for_tx_completion(struct xsk_socket_info *xsk) 1260 { 1261 struct timeval tv_end, tv_now, tv_timeout = {THREAD_TMOUT, 0}; 1262 int ret; 1263 1264 ret = gettimeofday(&tv_now, NULL); 1265 if (ret) 1266 exit_with_error(errno); 1267 timeradd(&tv_now, &tv_timeout, &tv_end); 1268 1269 while (xsk->outstanding_tx) { 1270 ret = gettimeofday(&tv_now, NULL); 1271 if (ret) 1272 exit_with_error(errno); 1273 if (timercmp(&tv_now, &tv_end, >)) { 1274 ksft_print_msg("ERROR: [%s] Transmission loop timed out\n", __func__); 1275 return TEST_FAILURE; 1276 } 1277 1278 complete_pkts(xsk, BATCH_SIZE); 1279 } 1280 1281 return TEST_PASS; 1282 } 1283 1284 static int send_pkts(struct test_spec *test, struct ifobject *ifobject) 1285 { 1286 struct pkt_stream *pkt_stream = ifobject->pkt_stream; 1287 bool timeout = !is_umem_valid(test->ifobj_rx); 1288 struct pollfd fds = { }; 1289 u32 ret; 1290 1291 fds.fd = xsk_socket__fd(ifobject->xsk->xsk); 1292 fds.events = POLLOUT; 1293 1294 while (pkt_stream->current_pkt_nb < pkt_stream->nb_pkts) { 1295 ret = __send_pkts(ifobject, &fds, timeout); 1296 if (ret == TEST_CONTINUE && !test->fail) 1297 continue; 1298 if ((ret || test->fail) && !timeout) 1299 return TEST_FAILURE; 1300 if (ret == TEST_PASS && timeout) 1301 return ret; 1302 } 1303 1304 return wait_for_tx_completion(ifobject->xsk); 1305 } 1306 1307 static int get_xsk_stats(struct xsk_socket *xsk, struct xdp_statistics *stats) 1308 { 1309 int fd = xsk_socket__fd(xsk), err; 1310 socklen_t optlen, expected_len; 1311 1312 optlen = sizeof(*stats); 1313 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, stats, &optlen); 1314 if (err) { 1315 ksft_print_msg("[%s] getsockopt(XDP_STATISTICS) error %u %s\n", 1316 __func__, -err, strerror(-err)); 1317 return TEST_FAILURE; 1318 } 1319 1320 expected_len = sizeof(struct xdp_statistics); 1321 if (optlen != expected_len) { 1322 ksft_print_msg("[%s] getsockopt optlen error. Expected: %u got: %u\n", 1323 __func__, expected_len, optlen); 1324 return TEST_FAILURE; 1325 } 1326 1327 return TEST_PASS; 1328 } 1329 1330 static int validate_rx_dropped(struct ifobject *ifobject) 1331 { 1332 struct xsk_socket *xsk = ifobject->xsk->xsk; 1333 struct xdp_statistics stats; 1334 int err; 1335 1336 err = kick_rx(ifobject->xsk); 1337 if (err) 1338 return TEST_FAILURE; 1339 1340 err = get_xsk_stats(xsk, &stats); 1341 if (err) 1342 return TEST_FAILURE; 1343 1344 /* The receiver calls getsockopt after receiving the last (valid) 1345 * packet which is not the final packet sent in this test (valid and 1346 * invalid packets are sent in alternating fashion with the final 1347 * packet being invalid). Since the last packet may or may not have 1348 * been dropped already, both outcomes must be allowed. 1349 */ 1350 if (stats.rx_dropped == ifobject->pkt_stream->nb_pkts / 2 || 1351 stats.rx_dropped == ifobject->pkt_stream->nb_pkts / 2 - 1) 1352 return TEST_PASS; 1353 1354 return TEST_FAILURE; 1355 } 1356 1357 static int validate_rx_full(struct ifobject *ifobject) 1358 { 1359 struct xsk_socket *xsk = ifobject->xsk->xsk; 1360 struct xdp_statistics stats; 1361 int err; 1362 1363 usleep(1000); 1364 err = kick_rx(ifobject->xsk); 1365 if (err) 1366 return TEST_FAILURE; 1367 1368 err = get_xsk_stats(xsk, &stats); 1369 if (err) 1370 return TEST_FAILURE; 1371 1372 if (stats.rx_ring_full) 1373 return TEST_PASS; 1374 1375 return TEST_FAILURE; 1376 } 1377 1378 static int validate_fill_empty(struct ifobject *ifobject) 1379 { 1380 struct xsk_socket *xsk = ifobject->xsk->xsk; 1381 struct xdp_statistics stats; 1382 int err; 1383 1384 usleep(1000); 1385 err = kick_rx(ifobject->xsk); 1386 if (err) 1387 return TEST_FAILURE; 1388 1389 err = get_xsk_stats(xsk, &stats); 1390 if (err) 1391 return TEST_FAILURE; 1392 1393 if (stats.rx_fill_ring_empty_descs) 1394 return TEST_PASS; 1395 1396 return TEST_FAILURE; 1397 } 1398 1399 static int validate_tx_invalid_descs(struct ifobject *ifobject) 1400 { 1401 struct xsk_socket *xsk = ifobject->xsk->xsk; 1402 int fd = xsk_socket__fd(xsk); 1403 struct xdp_statistics stats; 1404 socklen_t optlen; 1405 int err; 1406 1407 optlen = sizeof(stats); 1408 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen); 1409 if (err) { 1410 ksft_print_msg("[%s] getsockopt(XDP_STATISTICS) error %u %s\n", 1411 __func__, -err, strerror(-err)); 1412 return TEST_FAILURE; 1413 } 1414 1415 if (stats.tx_invalid_descs != ifobject->pkt_stream->nb_pkts / 2) { 1416 ksft_print_msg("[%s] tx_invalid_descs incorrect. Got [%u] expected [%u]\n", 1417 __func__, stats.tx_invalid_descs, ifobject->pkt_stream->nb_pkts); 1418 return TEST_FAILURE; 1419 } 1420 1421 return TEST_PASS; 1422 } 1423 1424 static void xsk_configure_socket(struct test_spec *test, struct ifobject *ifobject, 1425 struct xsk_umem_info *umem, bool tx) 1426 { 1427 int i, ret; 1428 1429 for (i = 0; i < test->nb_sockets; i++) { 1430 bool shared = (ifobject->shared_umem && tx) ? true : !!i; 1431 u32 ctr = 0; 1432 1433 while (ctr++ < SOCK_RECONF_CTR) { 1434 ret = __xsk_configure_socket(&ifobject->xsk_arr[i], umem, 1435 ifobject, shared); 1436 if (!ret) 1437 break; 1438 1439 /* Retry if it fails as xsk_socket__create() is asynchronous */ 1440 if (ctr >= SOCK_RECONF_CTR) 1441 exit_with_error(-ret); 1442 usleep(USLEEP_MAX); 1443 } 1444 if (ifobject->busy_poll) 1445 enable_busy_poll(&ifobject->xsk_arr[i]); 1446 } 1447 } 1448 1449 static void thread_common_ops_tx(struct test_spec *test, struct ifobject *ifobject) 1450 { 1451 xsk_configure_socket(test, ifobject, test->ifobj_rx->umem, true); 1452 ifobject->xsk = &ifobject->xsk_arr[0]; 1453 ifobject->xskmap = test->ifobj_rx->xskmap; 1454 memcpy(ifobject->umem, test->ifobj_rx->umem, sizeof(struct xsk_umem_info)); 1455 ifobject->umem->base_addr = 0; 1456 } 1457 1458 static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream, 1459 bool fill_up) 1460 { 1461 u32 rx_frame_size = umem->frame_size - XDP_PACKET_HEADROOM; 1462 u32 idx = 0, filled = 0, buffers_to_fill, nb_pkts; 1463 int ret; 1464 1465 if (umem->num_frames < XSK_RING_PROD__DEFAULT_NUM_DESCS) 1466 buffers_to_fill = umem->num_frames; 1467 else 1468 buffers_to_fill = XSK_RING_PROD__DEFAULT_NUM_DESCS; 1469 1470 ret = xsk_ring_prod__reserve(&umem->fq, buffers_to_fill, &idx); 1471 if (ret != buffers_to_fill) 1472 exit_with_error(ENOSPC); 1473 1474 while (filled < buffers_to_fill) { 1475 struct pkt *pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &nb_pkts); 1476 u64 addr; 1477 u32 i; 1478 1479 for (i = 0; i < pkt_nb_frags(rx_frame_size, pkt_stream, pkt); i++) { 1480 if (!pkt) { 1481 if (!fill_up) 1482 break; 1483 addr = filled * umem->frame_size + umem->base_addr; 1484 } else if (pkt->offset >= 0) { 1485 addr = pkt->offset % umem->frame_size + umem_alloc_buffer(umem); 1486 } else { 1487 addr = pkt->offset + umem_alloc_buffer(umem); 1488 } 1489 1490 *xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr; 1491 if (++filled >= buffers_to_fill) 1492 break; 1493 } 1494 } 1495 xsk_ring_prod__submit(&umem->fq, filled); 1496 xsk_ring_prod__cancel(&umem->fq, buffers_to_fill - filled); 1497 1498 pkt_stream_reset(pkt_stream); 1499 umem_reset_alloc(umem); 1500 } 1501 1502 static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject) 1503 { 1504 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size; 1505 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 1506 LIBBPF_OPTS(bpf_xdp_query_opts, opts); 1507 void *bufs; 1508 int ret; 1509 1510 if (ifobject->umem->unaligned_mode) 1511 mmap_flags |= MAP_HUGETLB | MAP_HUGE_2MB; 1512 1513 if (ifobject->shared_umem) 1514 umem_sz *= 2; 1515 1516 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); 1517 if (bufs == MAP_FAILED) 1518 exit_with_error(errno); 1519 1520 ret = xsk_configure_umem(ifobject, ifobject->umem, bufs, umem_sz); 1521 if (ret) 1522 exit_with_error(-ret); 1523 1524 xsk_configure_socket(test, ifobject, ifobject->umem, false); 1525 1526 ifobject->xsk = &ifobject->xsk_arr[0]; 1527 1528 if (!ifobject->rx_on) 1529 return; 1530 1531 xsk_populate_fill_ring(ifobject->umem, ifobject->pkt_stream, ifobject->use_fill_ring); 1532 1533 ret = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk); 1534 if (ret) 1535 exit_with_error(errno); 1536 } 1537 1538 static void *worker_testapp_validate_tx(void *arg) 1539 { 1540 struct test_spec *test = (struct test_spec *)arg; 1541 struct ifobject *ifobject = test->ifobj_tx; 1542 int err; 1543 1544 if (test->current_step == 1) { 1545 if (!ifobject->shared_umem) 1546 thread_common_ops(test, ifobject); 1547 else 1548 thread_common_ops_tx(test, ifobject); 1549 } 1550 1551 err = send_pkts(test, ifobject); 1552 1553 if (!err && ifobject->validation_func) 1554 err = ifobject->validation_func(ifobject); 1555 if (err) 1556 report_failure(test); 1557 1558 pthread_exit(NULL); 1559 } 1560 1561 static void *worker_testapp_validate_rx(void *arg) 1562 { 1563 struct test_spec *test = (struct test_spec *)arg; 1564 struct ifobject *ifobject = test->ifobj_rx; 1565 struct pollfd fds = { }; 1566 int err; 1567 1568 if (test->current_step == 1) { 1569 thread_common_ops(test, ifobject); 1570 } else { 1571 xsk_clear_xskmap(ifobject->xskmap); 1572 err = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk); 1573 if (err) { 1574 ksft_print_msg("Error: Failed to update xskmap, error %s\n", 1575 strerror(-err)); 1576 exit_with_error(-err); 1577 } 1578 } 1579 1580 fds.fd = xsk_socket__fd(ifobject->xsk->xsk); 1581 fds.events = POLLIN; 1582 1583 pthread_barrier_wait(&barr); 1584 1585 err = receive_pkts(test, &fds); 1586 1587 if (!err && ifobject->validation_func) 1588 err = ifobject->validation_func(ifobject); 1589 if (err) 1590 report_failure(test); 1591 1592 pthread_exit(NULL); 1593 } 1594 1595 static u64 ceil_u64(u64 a, u64 b) 1596 { 1597 return (a + b - 1) / b; 1598 } 1599 1600 static void testapp_clean_xsk_umem(struct ifobject *ifobj) 1601 { 1602 u64 umem_sz = ifobj->umem->num_frames * ifobj->umem->frame_size; 1603 1604 if (ifobj->shared_umem) 1605 umem_sz *= 2; 1606 1607 umem_sz = ceil_u64(umem_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE; 1608 xsk_umem__delete(ifobj->umem->umem); 1609 munmap(ifobj->umem->buffer, umem_sz); 1610 } 1611 1612 static void handler(int signum) 1613 { 1614 pthread_exit(NULL); 1615 } 1616 1617 static bool xdp_prog_changed_rx(struct test_spec *test) 1618 { 1619 struct ifobject *ifobj = test->ifobj_rx; 1620 1621 return ifobj->xdp_prog != test->xdp_prog_rx || ifobj->mode != test->mode; 1622 } 1623 1624 static bool xdp_prog_changed_tx(struct test_spec *test) 1625 { 1626 struct ifobject *ifobj = test->ifobj_tx; 1627 1628 return ifobj->xdp_prog != test->xdp_prog_tx || ifobj->mode != test->mode; 1629 } 1630 1631 static void xsk_reattach_xdp(struct ifobject *ifobj, struct bpf_program *xdp_prog, 1632 struct bpf_map *xskmap, enum test_mode mode) 1633 { 1634 int err; 1635 1636 xsk_detach_xdp_program(ifobj->ifindex, mode_to_xdp_flags(ifobj->mode)); 1637 err = xsk_attach_xdp_program(xdp_prog, ifobj->ifindex, mode_to_xdp_flags(mode)); 1638 if (err) { 1639 ksft_print_msg("Error attaching XDP program\n"); 1640 exit_with_error(-err); 1641 } 1642 1643 if (ifobj->mode != mode && (mode == TEST_MODE_DRV || mode == TEST_MODE_ZC)) 1644 if (!xsk_is_in_mode(ifobj->ifindex, XDP_FLAGS_DRV_MODE)) { 1645 ksft_print_msg("ERROR: XDP prog not in DRV mode\n"); 1646 exit_with_error(EINVAL); 1647 } 1648 1649 ifobj->xdp_prog = xdp_prog; 1650 ifobj->xskmap = xskmap; 1651 ifobj->mode = mode; 1652 } 1653 1654 static void xsk_attach_xdp_progs(struct test_spec *test, struct ifobject *ifobj_rx, 1655 struct ifobject *ifobj_tx) 1656 { 1657 if (xdp_prog_changed_rx(test)) 1658 xsk_reattach_xdp(ifobj_rx, test->xdp_prog_rx, test->xskmap_rx, test->mode); 1659 1660 if (!ifobj_tx || ifobj_tx->shared_umem) 1661 return; 1662 1663 if (xdp_prog_changed_tx(test)) 1664 xsk_reattach_xdp(ifobj_tx, test->xdp_prog_tx, test->xskmap_tx, test->mode); 1665 } 1666 1667 static int __testapp_validate_traffic(struct test_spec *test, struct ifobject *ifobj1, 1668 struct ifobject *ifobj2) 1669 { 1670 pthread_t t0, t1; 1671 int err; 1672 1673 if (test->mtu > MAX_ETH_PKT_SIZE) { 1674 if (test->mode == TEST_MODE_ZC && (!ifobj1->multi_buff_zc_supp || 1675 (ifobj2 && !ifobj2->multi_buff_zc_supp))) { 1676 ksft_test_result_skip("Multi buffer for zero-copy not supported.\n"); 1677 return TEST_SKIP; 1678 } 1679 if (test->mode != TEST_MODE_ZC && (!ifobj1->multi_buff_supp || 1680 (ifobj2 && !ifobj2->multi_buff_supp))) { 1681 ksft_test_result_skip("Multi buffer not supported.\n"); 1682 return TEST_SKIP; 1683 } 1684 } 1685 err = test_spec_set_mtu(test, test->mtu); 1686 if (err) { 1687 ksft_print_msg("Error, could not set mtu.\n"); 1688 exit_with_error(err); 1689 } 1690 1691 if (ifobj2) { 1692 if (pthread_barrier_init(&barr, NULL, 2)) 1693 exit_with_error(errno); 1694 pkt_stream_reset(ifobj2->pkt_stream); 1695 } 1696 1697 test->current_step++; 1698 pkt_stream_reset(ifobj1->pkt_stream); 1699 pkts_in_flight = 0; 1700 1701 signal(SIGUSR1, handler); 1702 /*Spawn RX thread */ 1703 pthread_create(&t0, NULL, ifobj1->func_ptr, test); 1704 1705 if (ifobj2) { 1706 pthread_barrier_wait(&barr); 1707 if (pthread_barrier_destroy(&barr)) 1708 exit_with_error(errno); 1709 1710 /*Spawn TX thread */ 1711 pthread_create(&t1, NULL, ifobj2->func_ptr, test); 1712 1713 pthread_join(t1, NULL); 1714 } 1715 1716 if (!ifobj2) 1717 pthread_kill(t0, SIGUSR1); 1718 else 1719 pthread_join(t0, NULL); 1720 1721 if (test->total_steps == test->current_step || test->fail) { 1722 if (ifobj2) 1723 xsk_socket__delete(ifobj2->xsk->xsk); 1724 xsk_socket__delete(ifobj1->xsk->xsk); 1725 testapp_clean_xsk_umem(ifobj1); 1726 if (ifobj2 && !ifobj2->shared_umem) 1727 testapp_clean_xsk_umem(ifobj2); 1728 } 1729 1730 return !!test->fail; 1731 } 1732 1733 static int testapp_validate_traffic(struct test_spec *test) 1734 { 1735 struct ifobject *ifobj_rx = test->ifobj_rx; 1736 struct ifobject *ifobj_tx = test->ifobj_tx; 1737 1738 if ((ifobj_rx->umem->unaligned_mode && !ifobj_rx->unaligned_supp) || 1739 (ifobj_tx->umem->unaligned_mode && !ifobj_tx->unaligned_supp)) { 1740 ksft_test_result_skip("No huge pages present.\n"); 1741 return TEST_SKIP; 1742 } 1743 1744 xsk_attach_xdp_progs(test, ifobj_rx, ifobj_tx); 1745 return __testapp_validate_traffic(test, ifobj_rx, ifobj_tx); 1746 } 1747 1748 static int testapp_validate_traffic_single_thread(struct test_spec *test, struct ifobject *ifobj) 1749 { 1750 return __testapp_validate_traffic(test, ifobj, NULL); 1751 } 1752 1753 static int testapp_teardown(struct test_spec *test) 1754 { 1755 int i; 1756 1757 for (i = 0; i < MAX_TEARDOWN_ITER; i++) { 1758 if (testapp_validate_traffic(test)) 1759 return TEST_FAILURE; 1760 test_spec_reset(test); 1761 } 1762 1763 return TEST_PASS; 1764 } 1765 1766 static void swap_directions(struct ifobject **ifobj1, struct ifobject **ifobj2) 1767 { 1768 thread_func_t tmp_func_ptr = (*ifobj1)->func_ptr; 1769 struct ifobject *tmp_ifobj = (*ifobj1); 1770 1771 (*ifobj1)->func_ptr = (*ifobj2)->func_ptr; 1772 (*ifobj2)->func_ptr = tmp_func_ptr; 1773 1774 *ifobj1 = *ifobj2; 1775 *ifobj2 = tmp_ifobj; 1776 } 1777 1778 static int testapp_bidirectional(struct test_spec *test) 1779 { 1780 int res; 1781 1782 test->ifobj_tx->rx_on = true; 1783 test->ifobj_rx->tx_on = true; 1784 test->total_steps = 2; 1785 if (testapp_validate_traffic(test)) 1786 return TEST_FAILURE; 1787 1788 print_verbose("Switching Tx/Rx direction\n"); 1789 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 1790 res = __testapp_validate_traffic(test, test->ifobj_rx, test->ifobj_tx); 1791 1792 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 1793 return res; 1794 } 1795 1796 static int swap_xsk_resources(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx) 1797 { 1798 int ret; 1799 1800 xsk_socket__delete(ifobj_tx->xsk->xsk); 1801 xsk_socket__delete(ifobj_rx->xsk->xsk); 1802 ifobj_tx->xsk = &ifobj_tx->xsk_arr[1]; 1803 ifobj_rx->xsk = &ifobj_rx->xsk_arr[1]; 1804 1805 ret = xsk_update_xskmap(ifobj_rx->xskmap, ifobj_rx->xsk->xsk); 1806 if (ret) 1807 return TEST_FAILURE; 1808 1809 return TEST_PASS; 1810 } 1811 1812 static int testapp_xdp_prog_cleanup(struct test_spec *test) 1813 { 1814 test->total_steps = 2; 1815 test->nb_sockets = 2; 1816 if (testapp_validate_traffic(test)) 1817 return TEST_FAILURE; 1818 1819 if (swap_xsk_resources(test->ifobj_tx, test->ifobj_rx)) 1820 return TEST_FAILURE; 1821 return testapp_validate_traffic(test); 1822 } 1823 1824 static int testapp_headroom(struct test_spec *test) 1825 { 1826 test->ifobj_rx->umem->frame_headroom = UMEM_HEADROOM_TEST_SIZE; 1827 return testapp_validate_traffic(test); 1828 } 1829 1830 static int testapp_stats_rx_dropped(struct test_spec *test) 1831 { 1832 if (test->mode == TEST_MODE_ZC) { 1833 ksft_test_result_skip("Can not run RX_DROPPED test for ZC mode\n"); 1834 return TEST_SKIP; 1835 } 1836 1837 pkt_stream_replace_half(test, MIN_PKT_SIZE * 4, 0); 1838 test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size - 1839 XDP_PACKET_HEADROOM - MIN_PKT_SIZE * 3; 1840 pkt_stream_receive_half(test); 1841 test->ifobj_rx->validation_func = validate_rx_dropped; 1842 return testapp_validate_traffic(test); 1843 } 1844 1845 static int testapp_stats_tx_invalid_descs(struct test_spec *test) 1846 { 1847 pkt_stream_replace_half(test, XSK_UMEM__INVALID_FRAME_SIZE, 0); 1848 test->ifobj_tx->validation_func = validate_tx_invalid_descs; 1849 return testapp_validate_traffic(test); 1850 } 1851 1852 static int testapp_stats_rx_full(struct test_spec *test) 1853 { 1854 pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE); 1855 test->ifobj_rx->pkt_stream = pkt_stream_generate(test->ifobj_rx->umem, 1856 DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE); 1857 1858 test->ifobj_rx->xsk->rxqsize = DEFAULT_UMEM_BUFFERS; 1859 test->ifobj_rx->release_rx = false; 1860 test->ifobj_rx->validation_func = validate_rx_full; 1861 return testapp_validate_traffic(test); 1862 } 1863 1864 static int testapp_stats_fill_empty(struct test_spec *test) 1865 { 1866 pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE); 1867 test->ifobj_rx->pkt_stream = pkt_stream_generate(test->ifobj_rx->umem, 1868 DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE); 1869 1870 test->ifobj_rx->use_fill_ring = false; 1871 test->ifobj_rx->validation_func = validate_fill_empty; 1872 return testapp_validate_traffic(test); 1873 } 1874 1875 static int testapp_send_receive_unaligned(struct test_spec *test) 1876 { 1877 test->ifobj_tx->umem->unaligned_mode = true; 1878 test->ifobj_rx->umem->unaligned_mode = true; 1879 /* Let half of the packets straddle a 4K buffer boundary */ 1880 pkt_stream_replace_half(test, MIN_PKT_SIZE, -MIN_PKT_SIZE / 2); 1881 1882 return testapp_validate_traffic(test); 1883 } 1884 1885 static int testapp_send_receive_unaligned_mb(struct test_spec *test) 1886 { 1887 test->mtu = MAX_ETH_JUMBO_SIZE; 1888 test->ifobj_tx->umem->unaligned_mode = true; 1889 test->ifobj_rx->umem->unaligned_mode = true; 1890 pkt_stream_replace(test, DEFAULT_PKT_CNT, MAX_ETH_JUMBO_SIZE); 1891 return testapp_validate_traffic(test); 1892 } 1893 1894 static int testapp_single_pkt(struct test_spec *test) 1895 { 1896 struct pkt pkts[] = {{0, MIN_PKT_SIZE, 0, true}}; 1897 1898 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 1899 return testapp_validate_traffic(test); 1900 } 1901 1902 static int testapp_send_receive_mb(struct test_spec *test) 1903 { 1904 test->mtu = MAX_ETH_JUMBO_SIZE; 1905 pkt_stream_replace(test, DEFAULT_PKT_CNT, MAX_ETH_JUMBO_SIZE); 1906 1907 return testapp_validate_traffic(test); 1908 } 1909 1910 static int testapp_invalid_desc_mb(struct test_spec *test) 1911 { 1912 struct xsk_umem_info *umem = test->ifobj_tx->umem; 1913 u64 umem_size = umem->num_frames * umem->frame_size; 1914 struct pkt pkts[] = { 1915 /* Valid packet for synch to start with */ 1916 {0, MIN_PKT_SIZE, 0, true, 0}, 1917 /* Zero frame len is not legal */ 1918 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1919 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1920 {0, 0, 0, false, 0}, 1921 /* Invalid address in the second frame */ 1922 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1923 {umem_size, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1924 /* Invalid len in the middle */ 1925 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1926 {0, XSK_UMEM__INVALID_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1927 /* Invalid options in the middle */ 1928 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1929 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XSK_DESC__INVALID_OPTION}, 1930 /* Transmit 2 frags, receive 3 */ 1931 {0, XSK_UMEM__MAX_FRAME_SIZE, 0, true, XDP_PKT_CONTD}, 1932 {0, XSK_UMEM__MAX_FRAME_SIZE, 0, true, 0}, 1933 /* Middle frame crosses chunk boundary with small length */ 1934 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 1935 {-MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false, 0}, 1936 /* Valid packet for synch so that something is received */ 1937 {0, MIN_PKT_SIZE, 0, true, 0}}; 1938 1939 if (umem->unaligned_mode) { 1940 /* Crossing a chunk boundary allowed */ 1941 pkts[12].valid = true; 1942 pkts[13].valid = true; 1943 } 1944 1945 test->mtu = MAX_ETH_JUMBO_SIZE; 1946 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 1947 return testapp_validate_traffic(test); 1948 } 1949 1950 static int testapp_invalid_desc(struct test_spec *test) 1951 { 1952 struct xsk_umem_info *umem = test->ifobj_tx->umem; 1953 u64 umem_size = umem->num_frames * umem->frame_size; 1954 struct pkt pkts[] = { 1955 /* Zero packet address allowed */ 1956 {0, MIN_PKT_SIZE, 0, true}, 1957 /* Allowed packet */ 1958 {0, MIN_PKT_SIZE, 0, true}, 1959 /* Straddling the start of umem */ 1960 {-2, MIN_PKT_SIZE, 0, false}, 1961 /* Packet too large */ 1962 {0, XSK_UMEM__INVALID_FRAME_SIZE, 0, false}, 1963 /* Up to end of umem allowed */ 1964 {umem_size - MIN_PKT_SIZE - 2 * umem->frame_size, MIN_PKT_SIZE, 0, true}, 1965 /* After umem ends */ 1966 {umem_size, MIN_PKT_SIZE, 0, false}, 1967 /* Straddle the end of umem */ 1968 {umem_size - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false}, 1969 /* Straddle a 4K boundary */ 1970 {0x1000 - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false}, 1971 /* Straddle a 2K boundary */ 1972 {0x800 - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, true}, 1973 /* Valid packet for synch so that something is received */ 1974 {0, MIN_PKT_SIZE, 0, true}}; 1975 1976 if (umem->unaligned_mode) { 1977 /* Crossing a page boundary allowed */ 1978 pkts[7].valid = true; 1979 } 1980 if (umem->frame_size == XSK_UMEM__DEFAULT_FRAME_SIZE / 2) { 1981 /* Crossing a 2K frame size boundary not allowed */ 1982 pkts[8].valid = false; 1983 } 1984 1985 if (test->ifobj_tx->shared_umem) { 1986 pkts[4].offset += umem_size; 1987 pkts[5].offset += umem_size; 1988 pkts[6].offset += umem_size; 1989 } 1990 1991 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 1992 return testapp_validate_traffic(test); 1993 } 1994 1995 static int testapp_xdp_drop(struct test_spec *test) 1996 { 1997 struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs; 1998 struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; 1999 2000 test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_drop, skel_tx->progs.xsk_xdp_drop, 2001 skel_rx->maps.xsk, skel_tx->maps.xsk); 2002 2003 pkt_stream_receive_half(test); 2004 return testapp_validate_traffic(test); 2005 } 2006 2007 static int testapp_xdp_metadata_copy(struct test_spec *test) 2008 { 2009 struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs; 2010 struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; 2011 struct bpf_map *data_map; 2012 int count = 0; 2013 int key = 0; 2014 2015 test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_populate_metadata, 2016 skel_tx->progs.xsk_xdp_populate_metadata, 2017 skel_rx->maps.xsk, skel_tx->maps.xsk); 2018 test->ifobj_rx->use_metadata = true; 2019 2020 data_map = bpf_object__find_map_by_name(skel_rx->obj, "xsk_xdp_.bss"); 2021 if (!data_map || !bpf_map__is_internal(data_map)) { 2022 ksft_print_msg("Error: could not find bss section of XDP program\n"); 2023 return TEST_FAILURE; 2024 } 2025 2026 if (bpf_map_update_elem(bpf_map__fd(data_map), &key, &count, BPF_ANY)) { 2027 ksft_print_msg("Error: could not update count element\n"); 2028 return TEST_FAILURE; 2029 } 2030 2031 return testapp_validate_traffic(test); 2032 } 2033 2034 static int testapp_poll_txq_tmout(struct test_spec *test) 2035 { 2036 test->ifobj_tx->use_poll = true; 2037 /* create invalid frame by set umem frame_size and pkt length equal to 2048 */ 2038 test->ifobj_tx->umem->frame_size = 2048; 2039 pkt_stream_replace(test, 2 * DEFAULT_PKT_CNT, 2048); 2040 return testapp_validate_traffic_single_thread(test, test->ifobj_tx); 2041 } 2042 2043 static int testapp_poll_rxq_tmout(struct test_spec *test) 2044 { 2045 test->ifobj_rx->use_poll = true; 2046 return testapp_validate_traffic_single_thread(test, test->ifobj_rx); 2047 } 2048 2049 static int testapp_too_many_frags(struct test_spec *test) 2050 { 2051 struct pkt pkts[2 * XSK_DESC__MAX_SKB_FRAGS + 2] = {}; 2052 u32 max_frags, i; 2053 2054 if (test->mode == TEST_MODE_ZC) 2055 max_frags = test->ifobj_tx->xdp_zc_max_segs; 2056 else 2057 max_frags = XSK_DESC__MAX_SKB_FRAGS; 2058 2059 test->mtu = MAX_ETH_JUMBO_SIZE; 2060 2061 /* Valid packet for synch */ 2062 pkts[0].len = MIN_PKT_SIZE; 2063 pkts[0].valid = true; 2064 2065 /* One valid packet with the max amount of frags */ 2066 for (i = 1; i < max_frags + 1; i++) { 2067 pkts[i].len = MIN_PKT_SIZE; 2068 pkts[i].options = XDP_PKT_CONTD; 2069 pkts[i].valid = true; 2070 } 2071 pkts[max_frags].options = 0; 2072 2073 /* An invalid packet with the max amount of frags but signals packet 2074 * continues on the last frag 2075 */ 2076 for (i = max_frags + 1; i < 2 * max_frags + 1; i++) { 2077 pkts[i].len = MIN_PKT_SIZE; 2078 pkts[i].options = XDP_PKT_CONTD; 2079 pkts[i].valid = false; 2080 } 2081 2082 /* Valid packet for synch */ 2083 pkts[2 * max_frags + 1].len = MIN_PKT_SIZE; 2084 pkts[2 * max_frags + 1].valid = true; 2085 2086 pkt_stream_generate_custom(test, pkts, 2 * max_frags + 2); 2087 return testapp_validate_traffic(test); 2088 } 2089 2090 static int xsk_load_xdp_programs(struct ifobject *ifobj) 2091 { 2092 ifobj->xdp_progs = xsk_xdp_progs__open_and_load(); 2093 if (libbpf_get_error(ifobj->xdp_progs)) 2094 return libbpf_get_error(ifobj->xdp_progs); 2095 2096 return 0; 2097 } 2098 2099 static void xsk_unload_xdp_programs(struct ifobject *ifobj) 2100 { 2101 xsk_xdp_progs__destroy(ifobj->xdp_progs); 2102 } 2103 2104 /* Simple test */ 2105 static bool hugepages_present(void) 2106 { 2107 size_t mmap_sz = 2 * DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE; 2108 void *bufs; 2109 2110 bufs = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, 2111 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, MAP_HUGE_2MB); 2112 if (bufs == MAP_FAILED) 2113 return false; 2114 2115 mmap_sz = ceil_u64(mmap_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE; 2116 munmap(bufs, mmap_sz); 2117 return true; 2118 } 2119 2120 static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac, 2121 thread_func_t func_ptr) 2122 { 2123 LIBBPF_OPTS(bpf_xdp_query_opts, query_opts); 2124 int err; 2125 2126 memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN); 2127 memcpy(ifobj->src_mac, src_mac, ETH_ALEN); 2128 2129 ifobj->func_ptr = func_ptr; 2130 2131 err = xsk_load_xdp_programs(ifobj); 2132 if (err) { 2133 ksft_print_msg("Error loading XDP program\n"); 2134 exit_with_error(err); 2135 } 2136 2137 if (hugepages_present()) 2138 ifobj->unaligned_supp = true; 2139 2140 err = bpf_xdp_query(ifobj->ifindex, XDP_FLAGS_DRV_MODE, &query_opts); 2141 if (err) { 2142 ksft_print_msg("Error querying XDP capabilities\n"); 2143 exit_with_error(-err); 2144 } 2145 if (query_opts.feature_flags & NETDEV_XDP_ACT_RX_SG) 2146 ifobj->multi_buff_supp = true; 2147 if (query_opts.feature_flags & NETDEV_XDP_ACT_XSK_ZEROCOPY) { 2148 if (query_opts.xdp_zc_max_segs > 1) { 2149 ifobj->multi_buff_zc_supp = true; 2150 ifobj->xdp_zc_max_segs = query_opts.xdp_zc_max_segs; 2151 } else { 2152 ifobj->xdp_zc_max_segs = 0; 2153 } 2154 } 2155 } 2156 2157 static int testapp_send_receive(struct test_spec *test) 2158 { 2159 return testapp_validate_traffic(test); 2160 } 2161 2162 static int testapp_send_receive_2k_frame(struct test_spec *test) 2163 { 2164 test->ifobj_tx->umem->frame_size = 2048; 2165 test->ifobj_rx->umem->frame_size = 2048; 2166 pkt_stream_replace(test, DEFAULT_PKT_CNT, MIN_PKT_SIZE); 2167 return testapp_validate_traffic(test); 2168 } 2169 2170 static int testapp_poll_rx(struct test_spec *test) 2171 { 2172 test->ifobj_rx->use_poll = true; 2173 return testapp_validate_traffic(test); 2174 } 2175 2176 static int testapp_poll_tx(struct test_spec *test) 2177 { 2178 test->ifobj_tx->use_poll = true; 2179 return testapp_validate_traffic(test); 2180 } 2181 2182 static int testapp_aligned_inv_desc(struct test_spec *test) 2183 { 2184 return testapp_invalid_desc(test); 2185 } 2186 2187 static int testapp_aligned_inv_desc_2k_frame(struct test_spec *test) 2188 { 2189 test->ifobj_tx->umem->frame_size = 2048; 2190 test->ifobj_rx->umem->frame_size = 2048; 2191 return testapp_invalid_desc(test); 2192 } 2193 2194 static int testapp_unaligned_inv_desc(struct test_spec *test) 2195 { 2196 test->ifobj_tx->umem->unaligned_mode = true; 2197 test->ifobj_rx->umem->unaligned_mode = true; 2198 return testapp_invalid_desc(test); 2199 } 2200 2201 static int testapp_unaligned_inv_desc_4001_frame(struct test_spec *test) 2202 { 2203 u64 page_size, umem_size; 2204 2205 /* Odd frame size so the UMEM doesn't end near a page boundary. */ 2206 test->ifobj_tx->umem->frame_size = 4001; 2207 test->ifobj_rx->umem->frame_size = 4001; 2208 test->ifobj_tx->umem->unaligned_mode = true; 2209 test->ifobj_rx->umem->unaligned_mode = true; 2210 /* This test exists to test descriptors that staddle the end of 2211 * the UMEM but not a page. 2212 */ 2213 page_size = sysconf(_SC_PAGESIZE); 2214 umem_size = test->ifobj_tx->umem->num_frames * test->ifobj_tx->umem->frame_size; 2215 assert(umem_size % page_size > MIN_PKT_SIZE); 2216 assert(umem_size % page_size < page_size - MIN_PKT_SIZE); 2217 2218 return testapp_invalid_desc(test); 2219 } 2220 2221 static int testapp_aligned_inv_desc_mb(struct test_spec *test) 2222 { 2223 return testapp_invalid_desc_mb(test); 2224 } 2225 2226 static int testapp_unaligned_inv_desc_mb(struct test_spec *test) 2227 { 2228 test->ifobj_tx->umem->unaligned_mode = true; 2229 test->ifobj_rx->umem->unaligned_mode = true; 2230 return testapp_invalid_desc_mb(test); 2231 } 2232 2233 static int testapp_xdp_metadata(struct test_spec *test) 2234 { 2235 return testapp_xdp_metadata_copy(test); 2236 } 2237 2238 static int testapp_xdp_metadata_mb(struct test_spec *test) 2239 { 2240 test->mtu = MAX_ETH_JUMBO_SIZE; 2241 return testapp_xdp_metadata_copy(test); 2242 } 2243 2244 static void run_pkt_test(struct test_spec *test) 2245 { 2246 int ret; 2247 2248 ret = test->test_func(test); 2249 2250 if (ret == TEST_PASS) 2251 ksft_test_result_pass("PASS: %s %s%s\n", mode_string(test), busy_poll_string(test), 2252 test->name); 2253 pkt_stream_restore_default(test); 2254 } 2255 2256 static struct ifobject *ifobject_create(void) 2257 { 2258 struct ifobject *ifobj; 2259 2260 ifobj = calloc(1, sizeof(struct ifobject)); 2261 if (!ifobj) 2262 return NULL; 2263 2264 ifobj->xsk_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->xsk_arr)); 2265 if (!ifobj->xsk_arr) 2266 goto out_xsk_arr; 2267 2268 ifobj->umem = calloc(1, sizeof(*ifobj->umem)); 2269 if (!ifobj->umem) 2270 goto out_umem; 2271 2272 return ifobj; 2273 2274 out_umem: 2275 free(ifobj->xsk_arr); 2276 out_xsk_arr: 2277 free(ifobj); 2278 return NULL; 2279 } 2280 2281 static void ifobject_delete(struct ifobject *ifobj) 2282 { 2283 free(ifobj->umem); 2284 free(ifobj->xsk_arr); 2285 free(ifobj); 2286 } 2287 2288 static bool is_xdp_supported(int ifindex) 2289 { 2290 int flags = XDP_FLAGS_DRV_MODE; 2291 2292 LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = flags); 2293 struct bpf_insn insns[2] = { 2294 BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), 2295 BPF_EXIT_INSN() 2296 }; 2297 int prog_fd, insn_cnt = ARRAY_SIZE(insns); 2298 int err; 2299 2300 prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL); 2301 if (prog_fd < 0) 2302 return false; 2303 2304 err = bpf_xdp_attach(ifindex, prog_fd, flags, NULL); 2305 if (err) { 2306 close(prog_fd); 2307 return false; 2308 } 2309 2310 bpf_xdp_detach(ifindex, flags, NULL); 2311 close(prog_fd); 2312 2313 return true; 2314 } 2315 2316 static const struct test_spec tests[] = { 2317 {.name = "SEND_RECEIVE", .test_func = testapp_send_receive}, 2318 {.name = "SEND_RECEIVE_2K_FRAME", .test_func = testapp_send_receive_2k_frame}, 2319 {.name = "SEND_RECEIVE_SINGLE_PKT", .test_func = testapp_single_pkt}, 2320 {.name = "POLL_RX", .test_func = testapp_poll_rx}, 2321 {.name = "POLL_TX", .test_func = testapp_poll_tx}, 2322 {.name = "POLL_RXQ_FULL", .test_func = testapp_poll_rxq_tmout}, 2323 {.name = "POLL_TXQ_FULL", .test_func = testapp_poll_txq_tmout}, 2324 {.name = "SEND_RECEIVE_UNALIGNED", .test_func = testapp_send_receive_unaligned}, 2325 {.name = "ALIGNED_INV_DESC", .test_func = testapp_aligned_inv_desc}, 2326 {.name = "ALIGNED_INV_DESC_2K_FRAME_SIZE", .test_func = testapp_aligned_inv_desc_2k_frame}, 2327 {.name = "UNALIGNED_INV_DESC", .test_func = testapp_unaligned_inv_desc}, 2328 {.name = "UNALIGNED_INV_DESC_4001_FRAME_SIZE", 2329 .test_func = testapp_unaligned_inv_desc_4001_frame}, 2330 {.name = "UMEM_HEADROOM", .test_func = testapp_headroom}, 2331 {.name = "TEARDOWN", .test_func = testapp_teardown}, 2332 {.name = "BIDIRECTIONAL", .test_func = testapp_bidirectional}, 2333 {.name = "STAT_RX_DROPPED", .test_func = testapp_stats_rx_dropped}, 2334 {.name = "STAT_TX_INVALID", .test_func = testapp_stats_tx_invalid_descs}, 2335 {.name = "STAT_RX_FULL", .test_func = testapp_stats_rx_full}, 2336 {.name = "STAT_FILL_EMPTY", .test_func = testapp_stats_fill_empty}, 2337 {.name = "XDP_PROG_CLEANUP", .test_func = testapp_xdp_prog_cleanup}, 2338 {.name = "XDP_DROP_HALF", .test_func = testapp_xdp_drop}, 2339 {.name = "XDP_METADATA_COPY", .test_func = testapp_xdp_metadata}, 2340 {.name = "XDP_METADATA_COPY_MULTI_BUFF", .test_func = testapp_xdp_metadata_mb}, 2341 {.name = "SEND_RECEIVE_9K_PACKETS", .test_func = testapp_send_receive_mb}, 2342 {.name = "SEND_RECEIVE_UNALIGNED_9K_PACKETS", 2343 .test_func = testapp_send_receive_unaligned_mb}, 2344 {.name = "ALIGNED_INV_DESC_MULTI_BUFF", .test_func = testapp_aligned_inv_desc_mb}, 2345 {.name = "UNALIGNED_INV_DESC_MULTI_BUFF", .test_func = testapp_unaligned_inv_desc_mb}, 2346 {.name = "TOO_MANY_FRAGS", .test_func = testapp_too_many_frags}, 2347 }; 2348 2349 static void print_tests(void) 2350 { 2351 u32 i; 2352 2353 printf("Tests:\n"); 2354 for (i = 0; i < ARRAY_SIZE(tests); i++) 2355 printf("%u: %s\n", i, tests[i].name); 2356 } 2357 2358 int main(int argc, char **argv) 2359 { 2360 struct pkt_stream *rx_pkt_stream_default; 2361 struct pkt_stream *tx_pkt_stream_default; 2362 struct ifobject *ifobj_tx, *ifobj_rx; 2363 u32 i, j, failed_tests = 0, nb_tests; 2364 int modes = TEST_MODE_SKB + 1; 2365 struct test_spec test; 2366 bool shared_netdev; 2367 2368 /* Use libbpf 1.0 API mode */ 2369 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 2370 2371 ifobj_tx = ifobject_create(); 2372 if (!ifobj_tx) 2373 exit_with_error(ENOMEM); 2374 ifobj_rx = ifobject_create(); 2375 if (!ifobj_rx) 2376 exit_with_error(ENOMEM); 2377 2378 setlocale(LC_ALL, ""); 2379 2380 parse_command_line(ifobj_tx, ifobj_rx, argc, argv); 2381 2382 if (opt_print_tests) { 2383 print_tests(); 2384 ksft_exit_xpass(); 2385 } 2386 if (opt_run_test != RUN_ALL_TESTS && opt_run_test >= ARRAY_SIZE(tests)) { 2387 ksft_print_msg("Error: test %u does not exist.\n", opt_run_test); 2388 ksft_exit_xfail(); 2389 } 2390 2391 shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex); 2392 ifobj_tx->shared_umem = shared_netdev; 2393 ifobj_rx->shared_umem = shared_netdev; 2394 2395 if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) 2396 print_usage(argv); 2397 2398 if (is_xdp_supported(ifobj_tx->ifindex)) { 2399 modes++; 2400 if (ifobj_zc_avail(ifobj_tx)) 2401 modes++; 2402 } 2403 2404 init_iface(ifobj_rx, MAC1, MAC2, worker_testapp_validate_rx); 2405 init_iface(ifobj_tx, MAC2, MAC1, worker_testapp_validate_tx); 2406 2407 test_spec_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]); 2408 tx_pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, MIN_PKT_SIZE); 2409 rx_pkt_stream_default = pkt_stream_generate(ifobj_rx->umem, DEFAULT_PKT_CNT, MIN_PKT_SIZE); 2410 if (!tx_pkt_stream_default || !rx_pkt_stream_default) 2411 exit_with_error(ENOMEM); 2412 test.tx_pkt_stream_default = tx_pkt_stream_default; 2413 test.rx_pkt_stream_default = rx_pkt_stream_default; 2414 2415 if (opt_run_test == RUN_ALL_TESTS) 2416 nb_tests = ARRAY_SIZE(tests); 2417 else 2418 nb_tests = 1; 2419 if (opt_mode == TEST_MODE_ALL) { 2420 ksft_set_plan(modes * nb_tests); 2421 } else { 2422 if (opt_mode == TEST_MODE_DRV && modes <= TEST_MODE_DRV) { 2423 ksft_print_msg("Error: XDP_DRV mode not supported.\n"); 2424 ksft_exit_xfail(); 2425 } 2426 if (opt_mode == TEST_MODE_ZC && modes <= TEST_MODE_ZC) { 2427 ksft_print_msg("Error: zero-copy mode not supported.\n"); 2428 ksft_exit_xfail(); 2429 } 2430 2431 ksft_set_plan(nb_tests); 2432 } 2433 2434 for (i = 0; i < modes; i++) { 2435 if (opt_mode != TEST_MODE_ALL && i != opt_mode) 2436 continue; 2437 2438 for (j = 0; j < ARRAY_SIZE(tests); j++) { 2439 if (opt_run_test != RUN_ALL_TESTS && j != opt_run_test) 2440 continue; 2441 2442 test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]); 2443 run_pkt_test(&test); 2444 usleep(USLEEP_MAX); 2445 2446 if (test.fail) 2447 failed_tests++; 2448 } 2449 } 2450 2451 pkt_stream_delete(tx_pkt_stream_default); 2452 pkt_stream_delete(rx_pkt_stream_default); 2453 xsk_unload_xdp_programs(ifobj_tx); 2454 xsk_unload_xdp_programs(ifobj_rx); 2455 ifobject_delete(ifobj_tx); 2456 ifobject_delete(ifobj_rx); 2457 2458 if (failed_tests) 2459 ksft_exit_fail(); 2460 else 2461 ksft_exit_pass(); 2462 } 2463