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