1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <stdint.h> 9 #include <inttypes.h> 10 #include <sys/types.h> 11 #include <sys/unistd.h> 12 #include <sys/queue.h> 13 #include <stdarg.h> 14 #include <ctype.h> 15 #include <errno.h> 16 #include <math.h> 17 #include <assert.h> 18 #include <getopt.h> 19 #include <signal.h> 20 21 #include "rte_atomic.h" 22 #include "rte_common.h" 23 #include "rte_eal.h" 24 #include "rte_cycles.h" 25 #include "rte_ether.h" 26 #include "rte_ethdev.h" 27 #include "rte_ip.h" 28 #include "rte_lcore.h" 29 #include "rte_malloc.h" 30 #include "rte_mbuf.h" 31 #include "rte_memory.h" 32 #include "rte_mempool.h" 33 #include "rte_log.h" 34 #include "rte_bbdev.h" 35 #include "rte_bbdev_op.h" 36 37 /* LLR values - negative value for '1' bit */ 38 #define LLR_1_BIT 0x81 39 #define LLR_0_BIT 0x7F 40 41 #define MAX_PKT_BURST 32 42 #define NB_MBUF 8191 43 #define MEMPOOL_CACHE_SIZE 256 44 45 /* Hardcoded K value */ 46 #define K 40 47 #define NCB (3 * RTE_ALIGN_CEIL(K + 4, 32)) 48 49 #define CRC_24B_LEN 3 50 51 /* Configurable number of RX/TX ring descriptors */ 52 #define RTE_TEST_RX_DESC_DEFAULT 128 53 #define RTE_TEST_TX_DESC_DEFAULT 512 54 55 #define BBDEV_ASSERT(a) do { \ 56 if (!(a)) { \ 57 usage(prgname); \ 58 return -1; \ 59 } \ 60 } while (0) 61 62 static const struct rte_eth_conf port_conf = { 63 .rxmode = { 64 .mq_mode = ETH_MQ_RX_NONE, 65 .max_rx_pkt_len = RTE_ETHER_MAX_LEN, 66 .split_hdr_size = 0, 67 }, 68 .txmode = { 69 .mq_mode = ETH_MQ_TX_NONE, 70 }, 71 }; 72 73 struct rte_bbdev_op_turbo_enc def_op_enc = { 74 /* These values are arbitrarily put, and does not map to the real 75 * values for the data received from ethdev ports 76 */ 77 .rv_index = 0, 78 .code_block_mode = 1, 79 .cb_params = { 80 .k = K, 81 }, 82 .op_flags = RTE_BBDEV_TURBO_CRC_24A_ATTACH 83 }; 84 85 struct rte_bbdev_op_turbo_dec def_op_dec = { 86 /* These values are arbitrarily put, and does not map to the real 87 * values for the data received from ethdev ports 88 */ 89 .code_block_mode = 1, 90 .cb_params = { 91 .k = K, 92 }, 93 .rv_index = 0, 94 .iter_max = 8, 95 .iter_min = 4, 96 .ext_scale = 15, 97 .num_maps = 0, 98 .op_flags = RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN 99 }; 100 101 struct app_config_params { 102 /* Placeholders for app params */ 103 uint16_t port_id; 104 uint16_t bbdev_id; 105 uint64_t enc_core_mask; 106 uint64_t dec_core_mask; 107 108 /* Values filled during init time */ 109 uint16_t enc_queue_ids[RTE_MAX_LCORE]; 110 uint16_t dec_queue_ids[RTE_MAX_LCORE]; 111 uint16_t num_enc_cores; 112 uint16_t num_dec_cores; 113 }; 114 115 struct lcore_statistics { 116 unsigned int enqueued; 117 unsigned int dequeued; 118 unsigned int rx_lost_packets; 119 unsigned int enc_to_dec_lost_packets; 120 unsigned int tx_lost_packets; 121 } __rte_cache_aligned; 122 123 /** each lcore configuration */ 124 struct lcore_conf { 125 uint64_t core_type; 126 127 unsigned int port_id; 128 unsigned int rx_queue_id; 129 unsigned int tx_queue_id; 130 131 unsigned int bbdev_id; 132 unsigned int enc_queue_id; 133 unsigned int dec_queue_id; 134 135 uint8_t llr_temp_buf[NCB]; 136 137 struct rte_mempool *bbdev_dec_op_pool; 138 struct rte_mempool *bbdev_enc_op_pool; 139 struct rte_mempool *enc_out_pool; 140 struct rte_ring *enc_to_dec_ring; 141 142 struct lcore_statistics *lcore_stats; 143 } __rte_cache_aligned; 144 145 struct stats_lcore_params { 146 struct lcore_conf *lconf; 147 struct app_config_params *app_params; 148 }; 149 150 151 static const struct app_config_params def_app_config = { 152 .port_id = 0, 153 .bbdev_id = 0, 154 .enc_core_mask = 0x2, 155 .dec_core_mask = 0x4, 156 .num_enc_cores = 1, 157 .num_dec_cores = 1, 158 }; 159 160 static rte_atomic16_t global_exit_flag; 161 162 /* display usage */ 163 static inline void 164 usage(const char *prgname) 165 { 166 printf("%s [EAL options] " 167 " --\n" 168 " --enc_cores - number of encoding cores (default = 0x2)\n" 169 " --dec_cores - number of decoding cores (default = 0x4)\n" 170 " --port_id - Ethernet port ID (default = 0)\n" 171 " --bbdev_id - BBDev ID (default = 0)\n" 172 "\n", prgname); 173 } 174 175 /* parse core mask */ 176 static inline 177 uint16_t bbdev_parse_mask(const char *mask) 178 { 179 char *end = NULL; 180 unsigned long pm; 181 182 /* parse hexadecimal string */ 183 pm = strtoul(mask, &end, 16); 184 if ((mask[0] == '\0') || (end == NULL) || (*end != '\0')) 185 return 0; 186 187 return pm; 188 } 189 190 /* parse core mask */ 191 static inline 192 uint16_t bbdev_parse_number(const char *mask) 193 { 194 char *end = NULL; 195 unsigned long pm; 196 197 /* parse hexadecimal string */ 198 pm = strtoul(mask, &end, 10); 199 if ((mask[0] == '\0') || (end == NULL) || (*end != '\0')) 200 return 0; 201 202 return pm; 203 } 204 205 static int 206 bbdev_parse_args(int argc, char **argv, 207 struct app_config_params *app_params) 208 { 209 int optind = 0; 210 int opt; 211 int opt_indx = 0; 212 char *prgname = argv[0]; 213 214 static struct option lgopts[] = { 215 { "enc_core_mask", required_argument, 0, 'e' }, 216 { "dec_core_mask", required_argument, 0, 'd' }, 217 { "port_id", required_argument, 0, 'p' }, 218 { "bbdev_id", required_argument, 0, 'b' }, 219 { NULL, 0, 0, 0 } 220 }; 221 222 BBDEV_ASSERT(argc != 0); 223 BBDEV_ASSERT(argv != NULL); 224 BBDEV_ASSERT(app_params != NULL); 225 226 while ((opt = getopt_long(argc, argv, "e:d:p:b:", lgopts, &opt_indx)) != 227 EOF) { 228 switch (opt) { 229 case 'e': 230 app_params->enc_core_mask = 231 bbdev_parse_mask(optarg); 232 if (app_params->enc_core_mask == 0) { 233 usage(prgname); 234 return -1; 235 } 236 app_params->num_enc_cores = 237 __builtin_popcount(app_params->enc_core_mask); 238 break; 239 240 case 'd': 241 app_params->dec_core_mask = 242 bbdev_parse_mask(optarg); 243 if (app_params->dec_core_mask == 0) { 244 usage(prgname); 245 return -1; 246 } 247 app_params->num_dec_cores = 248 __builtin_popcount(app_params->dec_core_mask); 249 break; 250 251 case 'p': 252 app_params->port_id = bbdev_parse_number(optarg); 253 break; 254 255 case 'b': 256 app_params->bbdev_id = bbdev_parse_number(optarg); 257 break; 258 259 default: 260 usage(prgname); 261 return -1; 262 } 263 } 264 optind = 0; 265 return optind; 266 } 267 268 static void 269 signal_handler(int signum) 270 { 271 printf("\nSignal %d received\n", signum); 272 rte_atomic16_set(&global_exit_flag, 1); 273 } 274 275 static void 276 print_mac(unsigned int portid, struct rte_ether_addr *bbdev_ports_eth_address) 277 { 278 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n", 279 (unsigned int) portid, 280 bbdev_ports_eth_address->addr_bytes[0], 281 bbdev_ports_eth_address->addr_bytes[1], 282 bbdev_ports_eth_address->addr_bytes[2], 283 bbdev_ports_eth_address->addr_bytes[3], 284 bbdev_ports_eth_address->addr_bytes[4], 285 bbdev_ports_eth_address->addr_bytes[5]); 286 } 287 288 static inline void 289 pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int nb_to_free) 290 { 291 unsigned int i; 292 for (i = 0; i < nb_to_free; ++i) 293 rte_pktmbuf_free(mbufs[i]); 294 } 295 296 static inline void 297 pktmbuf_userdata_free_bulk(struct rte_mbuf **mbufs, unsigned int nb_to_free) 298 { 299 unsigned int i; 300 for (i = 0; i < nb_to_free; ++i) { 301 struct rte_mbuf *rx_pkt = mbufs[i]->userdata; 302 rte_pktmbuf_free(rx_pkt); 303 rte_pktmbuf_free(mbufs[i]); 304 } 305 } 306 307 /* Check the link status of all ports in up to 9s, and print them finally */ 308 static int 309 check_port_link_status(uint16_t port_id) 310 { 311 #define CHECK_INTERVAL 100 /* 100ms */ 312 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 313 uint8_t count; 314 struct rte_eth_link link; 315 int link_get_err = -EINVAL; 316 317 printf("\nChecking link status."); 318 fflush(stdout); 319 320 for (count = 0; count <= MAX_CHECK_TIME && 321 !rte_atomic16_read(&global_exit_flag); count++) { 322 memset(&link, 0, sizeof(link)); 323 link_get_err = rte_eth_link_get_nowait(port_id, &link); 324 325 if (link_get_err >= 0 && link.link_status) { 326 const char *dp = (link.link_duplex == 327 ETH_LINK_FULL_DUPLEX) ? 328 "full-duplex" : "half-duplex"; 329 printf("\nPort %u Link Up - speed %u Mbps - %s\n", 330 port_id, link.link_speed, dp); 331 return 0; 332 } 333 printf("."); 334 fflush(stdout); 335 rte_delay_ms(CHECK_INTERVAL); 336 } 337 338 if (link_get_err >= 0) 339 printf("\nPort %d Link Down\n", port_id); 340 else 341 printf("\nGet link failed (port %d): %s\n", port_id, 342 rte_strerror(-link_get_err)); 343 344 return 0; 345 } 346 347 static inline void 348 add_ether_hdr(struct rte_mbuf *pkt_src, struct rte_mbuf *pkt_dst) 349 { 350 struct rte_ether_hdr *eth_from; 351 struct rte_ether_hdr *eth_to; 352 353 eth_from = rte_pktmbuf_mtod(pkt_src, struct rte_ether_hdr *); 354 eth_to = rte_pktmbuf_mtod(pkt_dst, struct rte_ether_hdr *); 355 356 /* copy header */ 357 rte_memcpy(eth_to, eth_from, sizeof(struct rte_ether_hdr)); 358 } 359 360 static inline void 361 add_awgn(struct rte_mbuf **mbufs, uint16_t num_pkts) 362 { 363 RTE_SET_USED(mbufs); 364 RTE_SET_USED(num_pkts); 365 } 366 367 /* Encoder output to Decoder input adapter. The Decoder accepts only soft input 368 * so each bit of the encoder output must be translated into one byte of LLR. If 369 * Sub-block Deinterleaver is bypassed, which is the case, the padding bytes 370 * must additionally be insterted at the end of each sub-block. 371 */ 372 static inline void 373 transform_enc_out_dec_in(struct rte_mbuf **mbufs, uint8_t *temp_buf, 374 uint16_t num_pkts, uint16_t k) 375 { 376 uint16_t i, l, j; 377 uint16_t start_bit_idx; 378 uint16_t out_idx; 379 uint16_t d = k + 4; 380 uint16_t kpi = RTE_ALIGN_CEIL(d, 32); 381 uint16_t nd = kpi - d; 382 uint16_t ncb = 3 * kpi; 383 384 for (i = 0; i < num_pkts; ++i) { 385 uint16_t pkt_data_len = rte_pktmbuf_data_len(mbufs[i]) - 386 sizeof(struct rte_ether_hdr); 387 388 /* Resize the packet if needed */ 389 if (pkt_data_len < ncb) { 390 char *data = rte_pktmbuf_append(mbufs[i], 391 ncb - pkt_data_len); 392 if (data == NULL) 393 printf( 394 "Not enough space in decoder input packet"); 395 } 396 397 /* Translate each bit into 1 LLR byte. */ 398 start_bit_idx = 0; 399 out_idx = 0; 400 for (j = 0; j < 3; ++j) { 401 for (l = start_bit_idx; l < start_bit_idx + d; ++l) { 402 uint8_t *data = rte_pktmbuf_mtod_offset( 403 mbufs[i], uint8_t *, 404 sizeof(struct rte_ether_hdr) + 405 (l >> 3)); 406 if (*data & (0x80 >> (l & 7))) 407 temp_buf[out_idx] = LLR_1_BIT; 408 else 409 temp_buf[out_idx] = LLR_0_BIT; 410 ++out_idx; 411 } 412 /* Padding bytes should be at the end of the sub-block. 413 */ 414 memset(&temp_buf[out_idx], 0, nd); 415 out_idx += nd; 416 start_bit_idx += d; 417 } 418 419 rte_memcpy(rte_pktmbuf_mtod_offset(mbufs[i], uint8_t *, 420 sizeof(struct rte_ether_hdr)), temp_buf, ncb); 421 } 422 } 423 424 static inline void 425 verify_data(struct rte_mbuf **mbufs, uint16_t num_pkts) 426 { 427 uint16_t i; 428 for (i = 0; i < num_pkts; ++i) { 429 struct rte_mbuf *out = mbufs[i]; 430 struct rte_mbuf *in = out->userdata; 431 432 if (memcmp(rte_pktmbuf_mtod_offset(in, uint8_t *, 433 sizeof(struct rte_ether_hdr)), 434 rte_pktmbuf_mtod_offset(out, uint8_t *, 435 sizeof(struct rte_ether_hdr)), 436 K / 8 - CRC_24B_LEN)) 437 printf("Input and output buffers are not equal!\n"); 438 } 439 } 440 441 static int 442 initialize_ports(struct app_config_params *app_params, 443 struct rte_mempool *ethdev_mbuf_mempool) 444 { 445 int ret; 446 uint16_t port_id = app_params->port_id; 447 uint16_t q; 448 /* ethernet addresses of ports */ 449 struct rte_ether_addr bbdev_port_eth_addr; 450 451 /* initialize ports */ 452 printf("\nInitializing port %u...\n", app_params->port_id); 453 ret = rte_eth_dev_configure(port_id, app_params->num_enc_cores, 454 app_params->num_dec_cores, &port_conf); 455 456 if (ret < 0) { 457 printf("Cannot configure device: err=%d, port=%u\n", 458 ret, port_id); 459 return -1; 460 } 461 462 /* initialize RX queues for encoder */ 463 for (q = 0; q < app_params->num_enc_cores; q++) { 464 ret = rte_eth_rx_queue_setup(port_id, q, 465 RTE_TEST_RX_DESC_DEFAULT, 466 rte_eth_dev_socket_id(port_id), 467 NULL, ethdev_mbuf_mempool); 468 if (ret < 0) { 469 printf("rte_eth_rx_queue_setup: err=%d, queue=%u\n", 470 ret, q); 471 return -1; 472 } 473 } 474 /* initialize TX queues for decoder */ 475 for (q = 0; q < app_params->num_dec_cores; q++) { 476 ret = rte_eth_tx_queue_setup(port_id, q, 477 RTE_TEST_TX_DESC_DEFAULT, 478 rte_eth_dev_socket_id(port_id), NULL); 479 if (ret < 0) { 480 printf("rte_eth_tx_queue_setup: err=%d, queue=%u\n", 481 ret, q); 482 return -1; 483 } 484 } 485 486 ret = rte_eth_promiscuous_enable(port_id); 487 if (ret != 0) { 488 printf("Cannot enable promiscuous mode: err=%s, port=%u\n", 489 rte_strerror(-ret), port_id); 490 return ret; 491 } 492 493 rte_eth_macaddr_get(port_id, &bbdev_port_eth_addr); 494 print_mac(port_id, &bbdev_port_eth_addr); 495 496 return 0; 497 } 498 499 static void 500 lcore_conf_init(struct app_config_params *app_params, 501 struct lcore_conf *lcore_conf, 502 struct rte_mempool **bbdev_op_pools, 503 struct rte_mempool *bbdev_mbuf_mempool, 504 struct rte_ring *enc_to_dec_ring, 505 struct lcore_statistics *lcore_stats) 506 { 507 unsigned int lcore_id; 508 struct lcore_conf *lconf; 509 uint16_t rx_queue_id = 0; 510 uint16_t tx_queue_id = 0; 511 uint16_t enc_q_id = 0; 512 uint16_t dec_q_id = 0; 513 514 /* Configure lcores */ 515 for (lcore_id = 0; lcore_id < 8 * sizeof(uint64_t); ++lcore_id) { 516 lconf = &lcore_conf[lcore_id]; 517 lconf->core_type = 0; 518 519 if ((1ULL << lcore_id) & app_params->enc_core_mask) { 520 lconf->core_type |= (1 << RTE_BBDEV_OP_TURBO_ENC); 521 lconf->rx_queue_id = rx_queue_id++; 522 lconf->enc_queue_id = 523 app_params->enc_queue_ids[enc_q_id++]; 524 } 525 526 if ((1ULL << lcore_id) & app_params->dec_core_mask) { 527 lconf->core_type |= (1 << RTE_BBDEV_OP_TURBO_DEC); 528 lconf->tx_queue_id = tx_queue_id++; 529 lconf->dec_queue_id = 530 app_params->dec_queue_ids[dec_q_id++]; 531 } 532 533 lconf->bbdev_enc_op_pool = 534 bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC]; 535 lconf->bbdev_dec_op_pool = 536 bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC]; 537 lconf->bbdev_id = app_params->bbdev_id; 538 lconf->port_id = app_params->port_id; 539 lconf->enc_out_pool = bbdev_mbuf_mempool; 540 lconf->enc_to_dec_ring = enc_to_dec_ring; 541 lconf->lcore_stats = &lcore_stats[lcore_id]; 542 } 543 } 544 545 static void 546 print_lcore_stats(struct lcore_statistics *lstats, unsigned int lcore_id) 547 { 548 static const char *stats_border = "_______"; 549 550 printf("\nLcore %d: %s enqueued count:\t\t%u\n", 551 lcore_id, stats_border, lstats->enqueued); 552 printf("Lcore %d: %s dequeued count:\t\t%u\n", 553 lcore_id, stats_border, lstats->dequeued); 554 printf("Lcore %d: %s RX lost packets count:\t\t%u\n", 555 lcore_id, stats_border, lstats->rx_lost_packets); 556 printf("Lcore %d: %s encoder-to-decoder lost count:\t%u\n", 557 lcore_id, stats_border, 558 lstats->enc_to_dec_lost_packets); 559 printf("Lcore %d: %s TX lost packets count:\t\t%u\n", 560 lcore_id, stats_border, lstats->tx_lost_packets); 561 } 562 563 static void 564 print_stats(struct stats_lcore_params *stats_lcore) 565 { 566 unsigned int l_id; 567 unsigned int bbdev_id = stats_lcore->app_params->bbdev_id; 568 unsigned int port_id = stats_lcore->app_params->port_id; 569 int len, ret, i; 570 571 struct rte_eth_xstat *xstats; 572 struct rte_eth_xstat_name *xstats_names; 573 struct rte_bbdev_stats bbstats; 574 static const char *stats_border = "_______"; 575 576 const char clr[] = { 27, '[', '2', 'J', '\0' }; 577 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' }; 578 579 /* Clear screen and move to top left */ 580 printf("%s%s", clr, topLeft); 581 582 printf("PORT STATISTICS:\n================\n"); 583 len = rte_eth_xstats_get(port_id, NULL, 0); 584 if (len < 0) 585 rte_exit(EXIT_FAILURE, 586 "rte_eth_xstats_get(%u) failed: %d", port_id, 587 len); 588 589 xstats = calloc(len, sizeof(*xstats)); 590 if (xstats == NULL) 591 rte_exit(EXIT_FAILURE, 592 "Failed to calloc memory for xstats"); 593 594 ret = rte_eth_xstats_get(port_id, xstats, len); 595 if (ret < 0 || ret > len) { 596 free(xstats); 597 rte_exit(EXIT_FAILURE, 598 "rte_eth_xstats_get(%u) len%i failed: %d", 599 port_id, len, ret); 600 } 601 602 xstats_names = calloc(len, sizeof(*xstats_names)); 603 if (xstats_names == NULL) { 604 free(xstats); 605 rte_exit(EXIT_FAILURE, 606 "Failed to calloc memory for xstats_names"); 607 } 608 609 ret = rte_eth_xstats_get_names(port_id, xstats_names, len); 610 if (ret < 0 || ret > len) { 611 free(xstats); 612 free(xstats_names); 613 rte_exit(EXIT_FAILURE, 614 "rte_eth_xstats_get_names(%u) len%i failed: %d", 615 port_id, len, ret); 616 } 617 618 for (i = 0; i < len; i++) { 619 if (xstats[i].value > 0) 620 printf("Port %u: %s %s:\t\t%"PRIu64"\n", 621 port_id, stats_border, 622 xstats_names[i].name, 623 xstats[i].value); 624 } 625 626 ret = rte_bbdev_stats_get(bbdev_id, &bbstats); 627 if (ret < 0) { 628 free(xstats); 629 free(xstats_names); 630 rte_exit(EXIT_FAILURE, 631 "ERROR(%d): Failure to get BBDEV %u statistics\n", 632 ret, bbdev_id); 633 } 634 635 printf("\nBBDEV STATISTICS:\n=================\n"); 636 printf("BBDEV %u: %s enqueue count:\t\t%"PRIu64"\n", 637 bbdev_id, stats_border, 638 bbstats.enqueued_count); 639 printf("BBDEV %u: %s dequeue count:\t\t%"PRIu64"\n", 640 bbdev_id, stats_border, 641 bbstats.dequeued_count); 642 printf("BBDEV %u: %s enqueue error count:\t\t%"PRIu64"\n", 643 bbdev_id, stats_border, 644 bbstats.enqueue_err_count); 645 printf("BBDEV %u: %s dequeue error count:\t\t%"PRIu64"\n\n", 646 bbdev_id, stats_border, 647 bbstats.dequeue_err_count); 648 649 printf("LCORE STATISTICS:\n=================\n"); 650 for (l_id = 0; l_id < RTE_MAX_LCORE; ++l_id) { 651 if (stats_lcore->lconf[l_id].core_type == 0) 652 continue; 653 print_lcore_stats(stats_lcore->lconf[l_id].lcore_stats, l_id); 654 } 655 656 free(xstats); 657 free(xstats_names); 658 } 659 660 static int 661 stats_loop(void *arg) 662 { 663 struct stats_lcore_params *stats_lcore = arg; 664 665 while (!rte_atomic16_read(&global_exit_flag)) { 666 print_stats(stats_lcore); 667 rte_delay_ms(500); 668 } 669 670 return 0; 671 } 672 673 static inline void 674 run_encoding(struct lcore_conf *lcore_conf) 675 { 676 uint16_t i; 677 uint16_t port_id, rx_queue_id; 678 uint16_t bbdev_id, enc_queue_id; 679 uint16_t nb_rx, nb_enq, nb_deq, nb_sent; 680 struct rte_mbuf *rx_pkts_burst[MAX_PKT_BURST]; 681 struct rte_mbuf *enc_out_pkts[MAX_PKT_BURST]; 682 struct rte_bbdev_enc_op *bbdev_ops_burst[MAX_PKT_BURST]; 683 struct lcore_statistics *lcore_stats; 684 struct rte_mempool *bbdev_op_pool, *enc_out_pool; 685 struct rte_ring *enc_to_dec_ring; 686 const int in_data_len = (def_op_enc.cb_params.k / 8) - CRC_24B_LEN; 687 688 lcore_stats = lcore_conf->lcore_stats; 689 port_id = lcore_conf->port_id; 690 rx_queue_id = lcore_conf->rx_queue_id; 691 bbdev_id = lcore_conf->bbdev_id; 692 enc_queue_id = lcore_conf->enc_queue_id; 693 bbdev_op_pool = lcore_conf->bbdev_enc_op_pool; 694 enc_out_pool = lcore_conf->enc_out_pool; 695 enc_to_dec_ring = lcore_conf->enc_to_dec_ring; 696 697 /* Read packet from RX queues*/ 698 nb_rx = rte_eth_rx_burst(port_id, rx_queue_id, rx_pkts_burst, 699 MAX_PKT_BURST); 700 if (!nb_rx) 701 return; 702 703 if (unlikely(rte_mempool_get_bulk(enc_out_pool, (void **)enc_out_pkts, 704 nb_rx) != 0)) { 705 pktmbuf_free_bulk(rx_pkts_burst, nb_rx); 706 lcore_stats->rx_lost_packets += nb_rx; 707 return; 708 } 709 710 if (unlikely(rte_bbdev_enc_op_alloc_bulk(bbdev_op_pool, bbdev_ops_burst, 711 nb_rx) != 0)) { 712 pktmbuf_free_bulk(enc_out_pkts, nb_rx); 713 pktmbuf_free_bulk(rx_pkts_burst, nb_rx); 714 lcore_stats->rx_lost_packets += nb_rx; 715 return; 716 } 717 718 for (i = 0; i < nb_rx; i++) { 719 char *data; 720 const uint16_t pkt_data_len = 721 rte_pktmbuf_data_len(rx_pkts_burst[i]) - 722 sizeof(struct rte_ether_hdr); 723 /* save input mbuf pointer for later comparison */ 724 enc_out_pkts[i]->userdata = rx_pkts_burst[i]; 725 726 /* copy ethernet header */ 727 rte_pktmbuf_reset(enc_out_pkts[i]); 728 data = rte_pktmbuf_append(enc_out_pkts[i], 729 sizeof(struct rte_ether_hdr)); 730 if (data == NULL) { 731 printf( 732 "Not enough space for ethernet header in encoder output mbuf\n"); 733 continue; 734 } 735 add_ether_hdr(rx_pkts_burst[i], enc_out_pkts[i]); 736 737 /* set op */ 738 bbdev_ops_burst[i]->turbo_enc = def_op_enc; 739 740 bbdev_ops_burst[i]->turbo_enc.input.data = 741 rx_pkts_burst[i]; 742 bbdev_ops_burst[i]->turbo_enc.input.offset = 743 sizeof(struct rte_ether_hdr); 744 /* Encoder will attach the CRC24B, adjust the length */ 745 bbdev_ops_burst[i]->turbo_enc.input.length = in_data_len; 746 747 if (in_data_len < pkt_data_len) 748 rte_pktmbuf_trim(rx_pkts_burst[i], pkt_data_len - 749 in_data_len); 750 else if (in_data_len > pkt_data_len) { 751 data = rte_pktmbuf_append(rx_pkts_burst[i], 752 in_data_len - pkt_data_len); 753 if (data == NULL) 754 printf( 755 "Not enough storage in mbuf to perform the encoding\n"); 756 } 757 758 bbdev_ops_burst[i]->turbo_enc.output.data = 759 enc_out_pkts[i]; 760 bbdev_ops_burst[i]->turbo_enc.output.offset = 761 sizeof(struct rte_ether_hdr); 762 } 763 764 /* Enqueue packets on BBDevice */ 765 nb_enq = rte_bbdev_enqueue_enc_ops(bbdev_id, enc_queue_id, 766 bbdev_ops_burst, nb_rx); 767 if (unlikely(nb_enq < nb_rx)) { 768 pktmbuf_userdata_free_bulk(&enc_out_pkts[nb_enq], 769 nb_rx - nb_enq); 770 rte_bbdev_enc_op_free_bulk(&bbdev_ops_burst[nb_enq], 771 nb_rx - nb_enq); 772 lcore_stats->rx_lost_packets += nb_rx - nb_enq; 773 774 if (!nb_enq) 775 return; 776 } 777 778 lcore_stats->enqueued += nb_enq; 779 780 /* Dequeue packets from bbdev device*/ 781 nb_deq = 0; 782 do { 783 nb_deq += rte_bbdev_dequeue_enc_ops(bbdev_id, enc_queue_id, 784 &bbdev_ops_burst[nb_deq], nb_enq - nb_deq); 785 } while (unlikely(nb_deq < nb_enq)); 786 787 lcore_stats->dequeued += nb_deq; 788 789 /* Generate and add AWGN */ 790 add_awgn(enc_out_pkts, nb_deq); 791 792 rte_bbdev_enc_op_free_bulk(bbdev_ops_burst, nb_deq); 793 794 /* Enqueue packets to encoder-to-decoder ring */ 795 nb_sent = rte_ring_enqueue_burst(enc_to_dec_ring, (void **)enc_out_pkts, 796 nb_deq, NULL); 797 if (unlikely(nb_sent < nb_deq)) { 798 pktmbuf_userdata_free_bulk(&enc_out_pkts[nb_sent], 799 nb_deq - nb_sent); 800 lcore_stats->enc_to_dec_lost_packets += nb_deq - nb_sent; 801 } 802 } 803 804 static void 805 run_decoding(struct lcore_conf *lcore_conf) 806 { 807 uint16_t i; 808 uint16_t port_id, tx_queue_id; 809 uint16_t bbdev_id, bbdev_queue_id; 810 uint16_t nb_recv, nb_enq, nb_deq, nb_tx; 811 uint8_t *llr_temp_buf; 812 struct rte_mbuf *recv_pkts_burst[MAX_PKT_BURST]; 813 struct rte_bbdev_dec_op *bbdev_ops_burst[MAX_PKT_BURST]; 814 struct lcore_statistics *lcore_stats; 815 struct rte_mempool *bbdev_op_pool; 816 struct rte_ring *enc_to_dec_ring; 817 818 lcore_stats = lcore_conf->lcore_stats; 819 port_id = lcore_conf->port_id; 820 tx_queue_id = lcore_conf->tx_queue_id; 821 bbdev_id = lcore_conf->bbdev_id; 822 bbdev_queue_id = lcore_conf->dec_queue_id; 823 bbdev_op_pool = lcore_conf->bbdev_dec_op_pool; 824 enc_to_dec_ring = lcore_conf->enc_to_dec_ring; 825 llr_temp_buf = lcore_conf->llr_temp_buf; 826 827 /* Dequeue packets from the ring */ 828 nb_recv = rte_ring_dequeue_burst(enc_to_dec_ring, 829 (void **)recv_pkts_burst, MAX_PKT_BURST, NULL); 830 if (!nb_recv) 831 return; 832 833 if (unlikely(rte_bbdev_dec_op_alloc_bulk(bbdev_op_pool, bbdev_ops_burst, 834 nb_recv) != 0)) { 835 pktmbuf_userdata_free_bulk(recv_pkts_burst, nb_recv); 836 lcore_stats->rx_lost_packets += nb_recv; 837 return; 838 } 839 840 transform_enc_out_dec_in(recv_pkts_burst, llr_temp_buf, nb_recv, 841 def_op_dec.cb_params.k); 842 843 for (i = 0; i < nb_recv; i++) { 844 /* set op */ 845 bbdev_ops_burst[i]->turbo_dec = def_op_dec; 846 847 bbdev_ops_burst[i]->turbo_dec.input.data = recv_pkts_burst[i]; 848 bbdev_ops_burst[i]->turbo_dec.input.offset = 849 sizeof(struct rte_ether_hdr); 850 bbdev_ops_burst[i]->turbo_dec.input.length = 851 rte_pktmbuf_data_len(recv_pkts_burst[i]) 852 - sizeof(struct rte_ether_hdr); 853 854 bbdev_ops_burst[i]->turbo_dec.hard_output.data = 855 recv_pkts_burst[i]; 856 bbdev_ops_burst[i]->turbo_dec.hard_output.offset = 857 sizeof(struct rte_ether_hdr); 858 } 859 860 /* Enqueue packets on BBDevice */ 861 nb_enq = rte_bbdev_enqueue_dec_ops(bbdev_id, bbdev_queue_id, 862 bbdev_ops_burst, nb_recv); 863 if (unlikely(nb_enq < nb_recv)) { 864 pktmbuf_userdata_free_bulk(&recv_pkts_burst[nb_enq], 865 nb_recv - nb_enq); 866 rte_bbdev_dec_op_free_bulk(&bbdev_ops_burst[nb_enq], 867 nb_recv - nb_enq); 868 lcore_stats->rx_lost_packets += nb_recv - nb_enq; 869 870 if (!nb_enq) 871 return; 872 } 873 874 lcore_stats->enqueued += nb_enq; 875 876 /* Dequeue packets from BBDevice */ 877 nb_deq = 0; 878 do { 879 nb_deq += rte_bbdev_dequeue_dec_ops(bbdev_id, bbdev_queue_id, 880 &bbdev_ops_burst[nb_deq], nb_enq - nb_deq); 881 } while (unlikely(nb_deq < nb_enq)); 882 883 lcore_stats->dequeued += nb_deq; 884 885 rte_bbdev_dec_op_free_bulk(bbdev_ops_burst, nb_deq); 886 887 verify_data(recv_pkts_burst, nb_deq); 888 889 /* Free the RX mbufs after verification */ 890 for (i = 0; i < nb_deq; ++i) 891 rte_pktmbuf_free(recv_pkts_burst[i]->userdata); 892 893 /* Transmit the packets */ 894 nb_tx = rte_eth_tx_burst(port_id, tx_queue_id, recv_pkts_burst, nb_deq); 895 if (unlikely(nb_tx < nb_deq)) { 896 pktmbuf_userdata_free_bulk(&recv_pkts_burst[nb_tx], 897 nb_deq - nb_tx); 898 lcore_stats->tx_lost_packets += nb_deq - nb_tx; 899 } 900 } 901 902 static int 903 processing_loop(void *arg) 904 { 905 struct lcore_conf *lcore_conf = arg; 906 const bool run_encoder = (lcore_conf->core_type & 907 (1 << RTE_BBDEV_OP_TURBO_ENC)); 908 const bool run_decoder = (lcore_conf->core_type & 909 (1 << RTE_BBDEV_OP_TURBO_DEC)); 910 911 while (!rte_atomic16_read(&global_exit_flag)) { 912 if (run_encoder) 913 run_encoding(lcore_conf); 914 if (run_decoder) 915 run_decoding(lcore_conf); 916 } 917 918 return 0; 919 } 920 921 static int 922 prepare_bbdev_device(unsigned int dev_id, struct rte_bbdev_info *info, 923 struct app_config_params *app_params) 924 { 925 int ret; 926 unsigned int q_id, dec_q_id, enc_q_id; 927 struct rte_bbdev_queue_conf qconf = {0}; 928 uint16_t dec_qs_nb = app_params->num_dec_cores; 929 uint16_t enc_qs_nb = app_params->num_enc_cores; 930 uint16_t tot_qs = dec_qs_nb + enc_qs_nb; 931 932 ret = rte_bbdev_setup_queues(dev_id, tot_qs, info->socket_id); 933 if (ret < 0) 934 rte_exit(EXIT_FAILURE, 935 "ERROR(%d): BBDEV %u not configured properly\n", 936 ret, dev_id); 937 938 /* setup device DEC queues */ 939 qconf.socket = info->socket_id; 940 qconf.queue_size = info->drv.queue_size_lim; 941 qconf.op_type = RTE_BBDEV_OP_TURBO_DEC; 942 943 for (q_id = 0, dec_q_id = 0; q_id < dec_qs_nb; q_id++) { 944 ret = rte_bbdev_queue_configure(dev_id, q_id, &qconf); 945 if (ret < 0) 946 rte_exit(EXIT_FAILURE, 947 "ERROR(%d): BBDEV %u DEC queue %u not configured properly\n", 948 ret, dev_id, q_id); 949 app_params->dec_queue_ids[dec_q_id++] = q_id; 950 } 951 952 /* setup device ENC queues */ 953 qconf.op_type = RTE_BBDEV_OP_TURBO_ENC; 954 955 for (q_id = dec_qs_nb, enc_q_id = 0; q_id < tot_qs; q_id++) { 956 ret = rte_bbdev_queue_configure(dev_id, q_id, &qconf); 957 if (ret < 0) 958 rte_exit(EXIT_FAILURE, 959 "ERROR(%d): BBDEV %u ENC queue %u not configured properly\n", 960 ret, dev_id, q_id); 961 app_params->enc_queue_ids[enc_q_id++] = q_id; 962 } 963 964 ret = rte_bbdev_start(dev_id); 965 966 if (ret != 0) 967 rte_exit(EXIT_FAILURE, "ERROR(%d): BBDEV %u not started\n", 968 ret, dev_id); 969 970 printf("BBdev %u started\n", dev_id); 971 972 return 0; 973 } 974 975 static inline bool 976 check_matching_capabilities(uint64_t mask, uint64_t required_mask) 977 { 978 return (mask & required_mask) == required_mask; 979 } 980 981 static void 982 enable_bbdev(struct app_config_params *app_params) 983 { 984 struct rte_bbdev_info dev_info; 985 const struct rte_bbdev_op_cap *op_cap; 986 uint16_t bbdev_id = app_params->bbdev_id; 987 bool encoder_capable = false; 988 bool decoder_capable = false; 989 990 rte_bbdev_info_get(bbdev_id, &dev_info); 991 op_cap = dev_info.drv.capabilities; 992 993 while (op_cap->type != RTE_BBDEV_OP_NONE) { 994 if (op_cap->type == RTE_BBDEV_OP_TURBO_ENC) { 995 if (check_matching_capabilities( 996 op_cap->cap.turbo_enc.capability_flags, 997 def_op_enc.op_flags)) 998 encoder_capable = true; 999 } 1000 1001 if (op_cap->type == RTE_BBDEV_OP_TURBO_DEC) { 1002 if (check_matching_capabilities( 1003 op_cap->cap.turbo_dec.capability_flags, 1004 def_op_dec.op_flags)) 1005 decoder_capable = true; 1006 } 1007 1008 op_cap++; 1009 } 1010 1011 if (encoder_capable == false) 1012 rte_exit(EXIT_FAILURE, 1013 "The specified BBDev %u doesn't have required encoder capabilities!\n", 1014 bbdev_id); 1015 if (decoder_capable == false) 1016 rte_exit(EXIT_FAILURE, 1017 "The specified BBDev %u doesn't have required decoder capabilities!\n", 1018 bbdev_id); 1019 1020 prepare_bbdev_device(bbdev_id, &dev_info, app_params); 1021 } 1022 1023 int 1024 main(int argc, char **argv) 1025 { 1026 int ret; 1027 unsigned int nb_bbdevs, flags, lcore_id; 1028 void *sigret; 1029 struct app_config_params app_params = def_app_config; 1030 struct rte_mempool *ethdev_mbuf_mempool, *bbdev_mbuf_mempool; 1031 struct rte_mempool *bbdev_op_pools[RTE_BBDEV_OP_TYPE_COUNT]; 1032 struct lcore_conf lcore_conf[RTE_MAX_LCORE] = { {0} }; 1033 struct lcore_statistics lcore_stats[RTE_MAX_LCORE] = { {0} }; 1034 struct stats_lcore_params stats_lcore; 1035 struct rte_ring *enc_to_dec_ring; 1036 bool stats_thread_started = false; 1037 unsigned int master_lcore_id = rte_get_master_lcore(); 1038 1039 rte_atomic16_init(&global_exit_flag); 1040 1041 sigret = signal(SIGTERM, signal_handler); 1042 if (sigret == SIG_ERR) 1043 rte_exit(EXIT_FAILURE, "signal(%d, ...) failed", SIGTERM); 1044 1045 sigret = signal(SIGINT, signal_handler); 1046 if (sigret == SIG_ERR) 1047 rte_exit(EXIT_FAILURE, "signal(%d, ...) failed", SIGINT); 1048 1049 ret = rte_eal_init(argc, argv); 1050 if (ret < 0) 1051 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); 1052 1053 argc -= ret; 1054 argv += ret; 1055 1056 /* parse application arguments (after the EAL ones) */ 1057 ret = bbdev_parse_args(argc, argv, &app_params); 1058 if (ret < 0) 1059 rte_exit(EXIT_FAILURE, "Invalid BBDEV arguments\n"); 1060 1061 /*create bbdev op pools*/ 1062 bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC] = 1063 rte_bbdev_op_pool_create("bbdev_op_pool_dec", 1064 RTE_BBDEV_OP_TURBO_DEC, NB_MBUF, 128, rte_socket_id()); 1065 bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC] = 1066 rte_bbdev_op_pool_create("bbdev_op_pool_enc", 1067 RTE_BBDEV_OP_TURBO_ENC, NB_MBUF, 128, rte_socket_id()); 1068 1069 if ((bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC] == NULL) || 1070 (bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC] == NULL)) 1071 rte_exit(EXIT_FAILURE, "Cannot create bbdev op pools\n"); 1072 1073 /* Create encoder to decoder ring */ 1074 flags = (app_params.num_enc_cores == 1) ? RING_F_SP_ENQ : 0; 1075 if (app_params.num_dec_cores == 1) 1076 flags |= RING_F_SC_DEQ; 1077 1078 enc_to_dec_ring = rte_ring_create("enc_to_dec_ring", 1079 rte_align32pow2(NB_MBUF), rte_socket_id(), flags); 1080 1081 /* Get the number of available bbdev devices */ 1082 nb_bbdevs = rte_bbdev_count(); 1083 if (nb_bbdevs <= app_params.bbdev_id) 1084 rte_exit(EXIT_FAILURE, 1085 "%u BBDevs detected, cannot use BBDev with ID %u!\n", 1086 nb_bbdevs, app_params.bbdev_id); 1087 printf("Number of bbdevs detected: %d\n", nb_bbdevs); 1088 1089 if (!rte_eth_dev_is_valid_port(app_params.port_id)) 1090 rte_exit(EXIT_FAILURE, 1091 "cannot use port with ID %u!\n", 1092 app_params.port_id); 1093 1094 /* create the mbuf mempool for ethdev pkts */ 1095 ethdev_mbuf_mempool = rte_pktmbuf_pool_create("ethdev_mbuf_pool", 1096 NB_MBUF, MEMPOOL_CACHE_SIZE, 0, 1097 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 1098 if (ethdev_mbuf_mempool == NULL) 1099 rte_exit(EXIT_FAILURE, "Cannot create ethdev mbuf mempool\n"); 1100 1101 /* create the mbuf mempool for encoder output */ 1102 bbdev_mbuf_mempool = rte_pktmbuf_pool_create("bbdev_mbuf_pool", 1103 NB_MBUF, MEMPOOL_CACHE_SIZE, 0, 1104 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); 1105 if (bbdev_mbuf_mempool == NULL) 1106 rte_exit(EXIT_FAILURE, "Cannot create ethdev mbuf mempool\n"); 1107 1108 /* initialize ports */ 1109 ret = initialize_ports(&app_params, ethdev_mbuf_mempool); 1110 1111 /* Check if all requested lcores are available */ 1112 for (lcore_id = 0; lcore_id < 8 * sizeof(uint64_t); ++lcore_id) 1113 if (((1ULL << lcore_id) & app_params.enc_core_mask) || 1114 ((1ULL << lcore_id) & app_params.dec_core_mask)) 1115 if (!rte_lcore_is_enabled(lcore_id)) 1116 rte_exit(EXIT_FAILURE, 1117 "Requested lcore_id %u is not enabled!\n", 1118 lcore_id); 1119 1120 /* Start ethernet port */ 1121 ret = rte_eth_dev_start(app_params.port_id); 1122 if (ret < 0) 1123 rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n", 1124 ret, app_params.port_id); 1125 1126 ret = check_port_link_status(app_params.port_id); 1127 if (ret < 0) 1128 exit(EXIT_FAILURE); 1129 1130 /* start BBDevice and save BBDev queue IDs */ 1131 enable_bbdev(&app_params); 1132 1133 /* Initialize the port/queue configuration of each logical core */ 1134 lcore_conf_init(&app_params, lcore_conf, bbdev_op_pools, 1135 bbdev_mbuf_mempool, enc_to_dec_ring, lcore_stats); 1136 1137 stats_lcore.app_params = &app_params; 1138 stats_lcore.lconf = lcore_conf; 1139 1140 RTE_LCORE_FOREACH_SLAVE(lcore_id) { 1141 if (lcore_conf[lcore_id].core_type != 0) 1142 /* launch per-lcore processing loop on slave lcores */ 1143 rte_eal_remote_launch(processing_loop, 1144 &lcore_conf[lcore_id], lcore_id); 1145 else if (!stats_thread_started) { 1146 /* launch statistics printing loop */ 1147 rte_eal_remote_launch(stats_loop, &stats_lcore, 1148 lcore_id); 1149 stats_thread_started = true; 1150 } 1151 } 1152 1153 if (!stats_thread_started && 1154 lcore_conf[master_lcore_id].core_type != 0) 1155 rte_exit(EXIT_FAILURE, 1156 "Not enough lcores to run the statistics printing loop!"); 1157 else if (lcore_conf[master_lcore_id].core_type != 0) 1158 processing_loop(&lcore_conf[master_lcore_id]); 1159 else if (!stats_thread_started) 1160 stats_loop(&stats_lcore); 1161 1162 RTE_LCORE_FOREACH_SLAVE(lcore_id) { 1163 ret |= rte_eal_wait_lcore(lcore_id); 1164 } 1165 1166 return ret; 1167 } 1168