1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
10 #include <sys/types.h>
12 #include <sys/queue.h>
21 #include <rte_atomic.h>
22 #include <rte_common.h>
24 #include <rte_cycles.h>
25 #include <rte_ether.h>
26 #include <rte_ethdev.h>
28 #include <rte_lcore.h>
29 #include <rte_malloc.h>
31 #include <rte_mbuf_dyn.h>
32 #include <rte_memory.h>
33 #include <rte_mempool.h>
35 #include <rte_bbdev.h>
36 #include <rte_bbdev_op.h>
38 /* LLR values - negative value for '1' bit */
39 #define LLR_1_BIT 0x81
40 #define LLR_0_BIT 0x7F
42 #define MAX_PKT_BURST 32
44 #define MEMPOOL_CACHE_SIZE 256
46 /* Hardcoded K value */
48 #define NCB (3 * RTE_ALIGN_CEIL(K + 4, 32))
52 /* Configurable number of RX/TX ring descriptors */
53 #define RTE_TEST_RX_DESC_DEFAULT 128
54 #define RTE_TEST_TX_DESC_DEFAULT 512
56 #define BBDEV_ASSERT(a) do { \
63 static int input_dynfield_offset = -1;
65 static inline struct rte_mbuf **
66 mbuf_input(struct rte_mbuf *mbuf)
68 return RTE_MBUF_DYNFIELD(mbuf,
69 input_dynfield_offset, struct rte_mbuf **);
72 static const struct rte_eth_conf port_conf = {
74 .mq_mode = ETH_MQ_RX_NONE,
75 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
79 .mq_mode = ETH_MQ_TX_NONE,
83 struct rte_bbdev_op_turbo_enc def_op_enc = {
84 /* These values are arbitrarily put, and does not map to the real
85 * values for the data received from ethdev ports
92 .op_flags = RTE_BBDEV_TURBO_CRC_24A_ATTACH
95 struct rte_bbdev_op_turbo_dec def_op_dec = {
96 /* These values are arbitrarily put, and does not map to the real
97 * values for the data received from ethdev ports
108 .op_flags = RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN
111 struct app_config_params {
112 /* Placeholders for app params */
115 uint64_t enc_core_mask;
116 uint64_t dec_core_mask;
118 /* Values filled during init time */
119 uint16_t enc_queue_ids[RTE_MAX_LCORE];
120 uint16_t dec_queue_ids[RTE_MAX_LCORE];
121 uint16_t num_enc_cores;
122 uint16_t num_dec_cores;
125 struct lcore_statistics {
126 unsigned int enqueued;
127 unsigned int dequeued;
128 unsigned int rx_lost_packets;
129 unsigned int enc_to_dec_lost_packets;
130 unsigned int tx_lost_packets;
131 } __rte_cache_aligned;
133 /** each lcore configuration */
137 unsigned int port_id;
138 unsigned int rx_queue_id;
139 unsigned int tx_queue_id;
141 unsigned int bbdev_id;
142 unsigned int enc_queue_id;
143 unsigned int dec_queue_id;
145 uint8_t llr_temp_buf[NCB];
147 struct rte_mempool *bbdev_dec_op_pool;
148 struct rte_mempool *bbdev_enc_op_pool;
149 struct rte_mempool *enc_out_pool;
150 struct rte_ring *enc_to_dec_ring;
152 struct lcore_statistics *lcore_stats;
153 } __rte_cache_aligned;
155 struct stats_lcore_params {
156 struct lcore_conf *lconf;
157 struct app_config_params *app_params;
161 static const struct app_config_params def_app_config = {
164 .enc_core_mask = 0x2,
165 .dec_core_mask = 0x4,
170 static rte_atomic16_t global_exit_flag;
174 usage(const char *prgname)
176 printf("%s [EAL options] "
178 " --enc_cores - number of encoding cores (default = 0x2)\n"
179 " --dec_cores - number of decoding cores (default = 0x4)\n"
180 " --port_id - Ethernet port ID (default = 0)\n"
181 " --bbdev_id - BBDev ID (default = 0)\n"
185 /* parse core mask */
187 uint16_t bbdev_parse_mask(const char *mask)
192 /* parse hexadecimal string */
193 pm = strtoul(mask, &end, 16);
194 if ((mask[0] == '\0') || (end == NULL) || (*end != '\0'))
200 /* parse core mask */
202 uint16_t bbdev_parse_number(const char *mask)
207 /* parse hexadecimal string */
208 pm = strtoul(mask, &end, 10);
209 if ((mask[0] == '\0') || (end == NULL) || (*end != '\0'))
216 bbdev_parse_args(int argc, char **argv,
217 struct app_config_params *app_params)
222 char *prgname = argv[0];
224 static struct option lgopts[] = {
225 { "enc_core_mask", required_argument, 0, 'e' },
226 { "dec_core_mask", required_argument, 0, 'd' },
227 { "port_id", required_argument, 0, 'p' },
228 { "bbdev_id", required_argument, 0, 'b' },
232 BBDEV_ASSERT(argc != 0);
233 BBDEV_ASSERT(argv != NULL);
234 BBDEV_ASSERT(app_params != NULL);
236 while ((opt = getopt_long(argc, argv, "e:d:p:b:", lgopts, &opt_indx)) !=
240 app_params->enc_core_mask =
241 bbdev_parse_mask(optarg);
242 if (app_params->enc_core_mask == 0) {
246 app_params->num_enc_cores =
247 __builtin_popcount(app_params->enc_core_mask);
251 app_params->dec_core_mask =
252 bbdev_parse_mask(optarg);
253 if (app_params->dec_core_mask == 0) {
257 app_params->num_dec_cores =
258 __builtin_popcount(app_params->dec_core_mask);
262 app_params->port_id = bbdev_parse_number(optarg);
266 app_params->bbdev_id = bbdev_parse_number(optarg);
279 signal_handler(int signum)
281 printf("\nSignal %d received\n", signum);
282 rte_atomic16_set(&global_exit_flag, 1);
286 print_mac(unsigned int portid, struct rte_ether_addr *bbdev_ports_eth_address)
288 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
289 (unsigned int) portid,
290 bbdev_ports_eth_address->addr_bytes[0],
291 bbdev_ports_eth_address->addr_bytes[1],
292 bbdev_ports_eth_address->addr_bytes[2],
293 bbdev_ports_eth_address->addr_bytes[3],
294 bbdev_ports_eth_address->addr_bytes[4],
295 bbdev_ports_eth_address->addr_bytes[5]);
299 pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int nb_to_free)
302 for (i = 0; i < nb_to_free; ++i)
303 rte_pktmbuf_free(mbufs[i]);
307 pktmbuf_input_free_bulk(struct rte_mbuf **mbufs, unsigned int nb_to_free)
310 for (i = 0; i < nb_to_free; ++i) {
311 struct rte_mbuf *rx_pkt = *mbuf_input(mbufs[i]);
312 rte_pktmbuf_free(rx_pkt);
313 rte_pktmbuf_free(mbufs[i]);
317 /* Check the link status of all ports in up to 9s, and print them finally */
319 check_port_link_status(uint16_t port_id)
321 #define CHECK_INTERVAL 100 /* 100ms */
322 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
324 struct rte_eth_link link;
325 int link_get_err = -EINVAL;
327 printf("\nChecking link status.");
330 for (count = 0; count <= MAX_CHECK_TIME &&
331 !rte_atomic16_read(&global_exit_flag); count++) {
332 memset(&link, 0, sizeof(link));
333 link_get_err = rte_eth_link_get_nowait(port_id, &link);
335 if (link_get_err >= 0 && link.link_status) {
336 const char *dp = (link.link_duplex ==
337 ETH_LINK_FULL_DUPLEX) ?
338 "full-duplex" : "half-duplex";
339 printf("\nPort %u Link Up - speed %s - %s\n",
341 rte_eth_link_speed_to_str(link.link_speed),
347 rte_delay_ms(CHECK_INTERVAL);
350 if (link_get_err >= 0)
351 printf("\nPort %d Link Down\n", port_id);
353 printf("\nGet link failed (port %d): %s\n", port_id,
354 rte_strerror(-link_get_err));
360 add_ether_hdr(struct rte_mbuf *pkt_src, struct rte_mbuf *pkt_dst)
362 struct rte_ether_hdr *eth_from;
363 struct rte_ether_hdr *eth_to;
365 eth_from = rte_pktmbuf_mtod(pkt_src, struct rte_ether_hdr *);
366 eth_to = rte_pktmbuf_mtod(pkt_dst, struct rte_ether_hdr *);
369 rte_memcpy(eth_to, eth_from, sizeof(struct rte_ether_hdr));
373 add_awgn(struct rte_mbuf **mbufs, uint16_t num_pkts)
376 RTE_SET_USED(num_pkts);
379 /* Encoder output to Decoder input adapter. The Decoder accepts only soft input
380 * so each bit of the encoder output must be translated into one byte of LLR. If
381 * Sub-block Deinterleaver is bypassed, which is the case, the padding bytes
382 * must additionally be insterted at the end of each sub-block.
385 transform_enc_out_dec_in(struct rte_mbuf **mbufs, uint8_t *temp_buf,
386 uint16_t num_pkts, uint16_t k)
389 uint16_t start_bit_idx;
392 uint16_t kpi = RTE_ALIGN_CEIL(d, 32);
393 uint16_t nd = kpi - d;
394 uint16_t ncb = 3 * kpi;
396 for (i = 0; i < num_pkts; ++i) {
397 uint16_t pkt_data_len = rte_pktmbuf_data_len(mbufs[i]) -
398 sizeof(struct rte_ether_hdr);
400 /* Resize the packet if needed */
401 if (pkt_data_len < ncb) {
402 char *data = rte_pktmbuf_append(mbufs[i],
406 "Not enough space in decoder input packet");
409 /* Translate each bit into 1 LLR byte. */
412 for (j = 0; j < 3; ++j) {
413 for (l = start_bit_idx; l < start_bit_idx + d; ++l) {
414 uint8_t *data = rte_pktmbuf_mtod_offset(
416 sizeof(struct rte_ether_hdr) +
418 if (*data & (0x80 >> (l & 7)))
419 temp_buf[out_idx] = LLR_1_BIT;
421 temp_buf[out_idx] = LLR_0_BIT;
424 /* Padding bytes should be at the end of the sub-block.
426 memset(&temp_buf[out_idx], 0, nd);
431 rte_memcpy(rte_pktmbuf_mtod_offset(mbufs[i], uint8_t *,
432 sizeof(struct rte_ether_hdr)), temp_buf, ncb);
437 verify_data(struct rte_mbuf **mbufs, uint16_t num_pkts)
440 for (i = 0; i < num_pkts; ++i) {
441 struct rte_mbuf *out = mbufs[i];
442 struct rte_mbuf *in = *mbuf_input(out);
444 if (memcmp(rte_pktmbuf_mtod_offset(in, uint8_t *,
445 sizeof(struct rte_ether_hdr)),
446 rte_pktmbuf_mtod_offset(out, uint8_t *,
447 sizeof(struct rte_ether_hdr)),
448 K / 8 - CRC_24B_LEN))
449 printf("Input and output buffers are not equal!\n");
454 initialize_ports(struct app_config_params *app_params,
455 struct rte_mempool *ethdev_mbuf_mempool)
458 uint16_t port_id = app_params->port_id;
460 /* ethernet addresses of ports */
461 struct rte_ether_addr bbdev_port_eth_addr;
463 /* initialize ports */
464 printf("\nInitializing port %u...\n", app_params->port_id);
465 ret = rte_eth_dev_configure(port_id, app_params->num_enc_cores,
466 app_params->num_dec_cores, &port_conf);
469 printf("Cannot configure device: err=%d, port=%u\n",
474 /* initialize RX queues for encoder */
475 for (q = 0; q < app_params->num_enc_cores; q++) {
476 ret = rte_eth_rx_queue_setup(port_id, q,
477 RTE_TEST_RX_DESC_DEFAULT,
478 rte_eth_dev_socket_id(port_id),
479 NULL, ethdev_mbuf_mempool);
481 printf("rte_eth_rx_queue_setup: err=%d, queue=%u\n",
486 /* initialize TX queues for decoder */
487 for (q = 0; q < app_params->num_dec_cores; q++) {
488 ret = rte_eth_tx_queue_setup(port_id, q,
489 RTE_TEST_TX_DESC_DEFAULT,
490 rte_eth_dev_socket_id(port_id), NULL);
492 printf("rte_eth_tx_queue_setup: err=%d, queue=%u\n",
498 ret = rte_eth_promiscuous_enable(port_id);
500 printf("Cannot enable promiscuous mode: err=%s, port=%u\n",
501 rte_strerror(-ret), port_id);
505 ret = rte_eth_macaddr_get(port_id, &bbdev_port_eth_addr);
507 printf("rte_eth_macaddr_get: err=%d, queue=%u\n",
512 print_mac(port_id, &bbdev_port_eth_addr);
518 lcore_conf_init(struct app_config_params *app_params,
519 struct lcore_conf *lcore_conf,
520 struct rte_mempool **bbdev_op_pools,
521 struct rte_mempool *bbdev_mbuf_mempool,
522 struct rte_ring *enc_to_dec_ring,
523 struct lcore_statistics *lcore_stats)
525 unsigned int lcore_id;
526 struct lcore_conf *lconf;
527 uint16_t rx_queue_id = 0;
528 uint16_t tx_queue_id = 0;
529 uint16_t enc_q_id = 0;
530 uint16_t dec_q_id = 0;
532 /* Configure lcores */
533 for (lcore_id = 0; lcore_id < 8 * sizeof(uint64_t); ++lcore_id) {
534 lconf = &lcore_conf[lcore_id];
535 lconf->core_type = 0;
537 if ((1ULL << lcore_id) & app_params->enc_core_mask) {
538 lconf->core_type |= (1 << RTE_BBDEV_OP_TURBO_ENC);
539 lconf->rx_queue_id = rx_queue_id++;
540 lconf->enc_queue_id =
541 app_params->enc_queue_ids[enc_q_id++];
544 if ((1ULL << lcore_id) & app_params->dec_core_mask) {
545 lconf->core_type |= (1 << RTE_BBDEV_OP_TURBO_DEC);
546 lconf->tx_queue_id = tx_queue_id++;
547 lconf->dec_queue_id =
548 app_params->dec_queue_ids[dec_q_id++];
551 lconf->bbdev_enc_op_pool =
552 bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC];
553 lconf->bbdev_dec_op_pool =
554 bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC];
555 lconf->bbdev_id = app_params->bbdev_id;
556 lconf->port_id = app_params->port_id;
557 lconf->enc_out_pool = bbdev_mbuf_mempool;
558 lconf->enc_to_dec_ring = enc_to_dec_ring;
559 lconf->lcore_stats = &lcore_stats[lcore_id];
564 print_lcore_stats(struct lcore_statistics *lstats, unsigned int lcore_id)
566 static const char *stats_border = "_______";
568 printf("\nLcore %d: %s enqueued count:\t\t%u\n",
569 lcore_id, stats_border, lstats->enqueued);
570 printf("Lcore %d: %s dequeued count:\t\t%u\n",
571 lcore_id, stats_border, lstats->dequeued);
572 printf("Lcore %d: %s RX lost packets count:\t\t%u\n",
573 lcore_id, stats_border, lstats->rx_lost_packets);
574 printf("Lcore %d: %s encoder-to-decoder lost count:\t%u\n",
575 lcore_id, stats_border,
576 lstats->enc_to_dec_lost_packets);
577 printf("Lcore %d: %s TX lost packets count:\t\t%u\n",
578 lcore_id, stats_border, lstats->tx_lost_packets);
582 print_stats(struct stats_lcore_params *stats_lcore)
585 unsigned int bbdev_id = stats_lcore->app_params->bbdev_id;
586 unsigned int port_id = stats_lcore->app_params->port_id;
589 struct rte_eth_xstat *xstats;
590 struct rte_eth_xstat_name *xstats_names;
591 struct rte_bbdev_stats bbstats;
592 static const char *stats_border = "_______";
594 const char clr[] = { 27, '[', '2', 'J', '\0' };
595 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
597 /* Clear screen and move to top left */
598 printf("%s%s", clr, topLeft);
600 printf("PORT STATISTICS:\n================\n");
601 len = rte_eth_xstats_get(port_id, NULL, 0);
603 rte_exit(EXIT_FAILURE,
604 "rte_eth_xstats_get(%u) failed: %d", port_id,
607 xstats = calloc(len, sizeof(*xstats));
609 rte_exit(EXIT_FAILURE,
610 "Failed to calloc memory for xstats");
612 ret = rte_eth_xstats_get(port_id, xstats, len);
613 if (ret < 0 || ret > len) {
615 rte_exit(EXIT_FAILURE,
616 "rte_eth_xstats_get(%u) len%i failed: %d",
620 xstats_names = calloc(len, sizeof(*xstats_names));
621 if (xstats_names == NULL) {
623 rte_exit(EXIT_FAILURE,
624 "Failed to calloc memory for xstats_names");
627 ret = rte_eth_xstats_get_names(port_id, xstats_names, len);
628 if (ret < 0 || ret > len) {
631 rte_exit(EXIT_FAILURE,
632 "rte_eth_xstats_get_names(%u) len%i failed: %d",
636 for (i = 0; i < len; i++) {
637 if (xstats[i].value > 0)
638 printf("Port %u: %s %s:\t\t%"PRIu64"\n",
639 port_id, stats_border,
640 xstats_names[i].name,
644 ret = rte_bbdev_stats_get(bbdev_id, &bbstats);
648 rte_exit(EXIT_FAILURE,
649 "ERROR(%d): Failure to get BBDEV %u statistics\n",
653 printf("\nBBDEV STATISTICS:\n=================\n");
654 printf("BBDEV %u: %s enqueue count:\t\t%"PRIu64"\n",
655 bbdev_id, stats_border,
656 bbstats.enqueued_count);
657 printf("BBDEV %u: %s dequeue count:\t\t%"PRIu64"\n",
658 bbdev_id, stats_border,
659 bbstats.dequeued_count);
660 printf("BBDEV %u: %s enqueue error count:\t\t%"PRIu64"\n",
661 bbdev_id, stats_border,
662 bbstats.enqueue_err_count);
663 printf("BBDEV %u: %s dequeue error count:\t\t%"PRIu64"\n\n",
664 bbdev_id, stats_border,
665 bbstats.dequeue_err_count);
667 printf("LCORE STATISTICS:\n=================\n");
668 for (l_id = 0; l_id < RTE_MAX_LCORE; ++l_id) {
669 if (stats_lcore->lconf[l_id].core_type == 0)
671 print_lcore_stats(stats_lcore->lconf[l_id].lcore_stats, l_id);
681 stats_loop(void *arg)
683 struct stats_lcore_params *stats_lcore = arg;
685 while (!rte_atomic16_read(&global_exit_flag)) {
686 print_stats(stats_lcore);
694 run_encoding(struct lcore_conf *lcore_conf)
697 uint16_t port_id, rx_queue_id;
698 uint16_t bbdev_id, enc_queue_id;
699 uint16_t nb_rx, nb_enq, nb_deq, nb_sent;
700 struct rte_mbuf *rx_pkts_burst[MAX_PKT_BURST];
701 struct rte_mbuf *enc_out_pkts[MAX_PKT_BURST];
702 struct rte_bbdev_enc_op *bbdev_ops_burst[MAX_PKT_BURST];
703 struct lcore_statistics *lcore_stats;
704 struct rte_mempool *bbdev_op_pool, *enc_out_pool;
705 struct rte_ring *enc_to_dec_ring;
706 const int in_data_len = (def_op_enc.cb_params.k / 8) - CRC_24B_LEN;
708 lcore_stats = lcore_conf->lcore_stats;
709 port_id = lcore_conf->port_id;
710 rx_queue_id = lcore_conf->rx_queue_id;
711 bbdev_id = lcore_conf->bbdev_id;
712 enc_queue_id = lcore_conf->enc_queue_id;
713 bbdev_op_pool = lcore_conf->bbdev_enc_op_pool;
714 enc_out_pool = lcore_conf->enc_out_pool;
715 enc_to_dec_ring = lcore_conf->enc_to_dec_ring;
717 /* Read packet from RX queues*/
718 nb_rx = rte_eth_rx_burst(port_id, rx_queue_id, rx_pkts_burst,
723 if (unlikely(rte_mempool_get_bulk(enc_out_pool, (void **)enc_out_pkts,
725 pktmbuf_free_bulk(rx_pkts_burst, nb_rx);
726 lcore_stats->rx_lost_packets += nb_rx;
730 if (unlikely(rte_bbdev_enc_op_alloc_bulk(bbdev_op_pool, bbdev_ops_burst,
732 pktmbuf_free_bulk(enc_out_pkts, nb_rx);
733 pktmbuf_free_bulk(rx_pkts_burst, nb_rx);
734 lcore_stats->rx_lost_packets += nb_rx;
738 for (i = 0; i < nb_rx; i++) {
740 const uint16_t pkt_data_len =
741 rte_pktmbuf_data_len(rx_pkts_burst[i]) -
742 sizeof(struct rte_ether_hdr);
743 /* save input mbuf pointer for later comparison */
744 *mbuf_input(enc_out_pkts[i]) = rx_pkts_burst[i];
746 /* copy ethernet header */
747 rte_pktmbuf_reset(enc_out_pkts[i]);
748 data = rte_pktmbuf_append(enc_out_pkts[i],
749 sizeof(struct rte_ether_hdr));
752 "Not enough space for ethernet header in encoder output mbuf\n");
755 add_ether_hdr(rx_pkts_burst[i], enc_out_pkts[i]);
758 bbdev_ops_burst[i]->turbo_enc = def_op_enc;
760 bbdev_ops_burst[i]->turbo_enc.input.data =
762 bbdev_ops_burst[i]->turbo_enc.input.offset =
763 sizeof(struct rte_ether_hdr);
764 /* Encoder will attach the CRC24B, adjust the length */
765 bbdev_ops_burst[i]->turbo_enc.input.length = in_data_len;
767 if (in_data_len < pkt_data_len)
768 rte_pktmbuf_trim(rx_pkts_burst[i], pkt_data_len -
770 else if (in_data_len > pkt_data_len) {
771 data = rte_pktmbuf_append(rx_pkts_burst[i],
772 in_data_len - pkt_data_len);
775 "Not enough storage in mbuf to perform the encoding\n");
778 bbdev_ops_burst[i]->turbo_enc.output.data =
780 bbdev_ops_burst[i]->turbo_enc.output.offset =
781 sizeof(struct rte_ether_hdr);
784 /* Enqueue packets on BBDevice */
785 nb_enq = rte_bbdev_enqueue_enc_ops(bbdev_id, enc_queue_id,
786 bbdev_ops_burst, nb_rx);
787 if (unlikely(nb_enq < nb_rx)) {
788 pktmbuf_input_free_bulk(&enc_out_pkts[nb_enq],
790 rte_bbdev_enc_op_free_bulk(&bbdev_ops_burst[nb_enq],
792 lcore_stats->rx_lost_packets += nb_rx - nb_enq;
798 lcore_stats->enqueued += nb_enq;
800 /* Dequeue packets from bbdev device*/
803 nb_deq += rte_bbdev_dequeue_enc_ops(bbdev_id, enc_queue_id,
804 &bbdev_ops_burst[nb_deq], nb_enq - nb_deq);
805 } while (unlikely(nb_deq < nb_enq));
807 lcore_stats->dequeued += nb_deq;
809 /* Generate and add AWGN */
810 add_awgn(enc_out_pkts, nb_deq);
812 rte_bbdev_enc_op_free_bulk(bbdev_ops_burst, nb_deq);
814 /* Enqueue packets to encoder-to-decoder ring */
815 nb_sent = rte_ring_enqueue_burst(enc_to_dec_ring, (void **)enc_out_pkts,
817 if (unlikely(nb_sent < nb_deq)) {
818 pktmbuf_input_free_bulk(&enc_out_pkts[nb_sent],
820 lcore_stats->enc_to_dec_lost_packets += nb_deq - nb_sent;
825 run_decoding(struct lcore_conf *lcore_conf)
828 uint16_t port_id, tx_queue_id;
829 uint16_t bbdev_id, bbdev_queue_id;
830 uint16_t nb_recv, nb_enq, nb_deq, nb_tx;
831 uint8_t *llr_temp_buf;
832 struct rte_mbuf *recv_pkts_burst[MAX_PKT_BURST];
833 struct rte_bbdev_dec_op *bbdev_ops_burst[MAX_PKT_BURST];
834 struct lcore_statistics *lcore_stats;
835 struct rte_mempool *bbdev_op_pool;
836 struct rte_ring *enc_to_dec_ring;
838 lcore_stats = lcore_conf->lcore_stats;
839 port_id = lcore_conf->port_id;
840 tx_queue_id = lcore_conf->tx_queue_id;
841 bbdev_id = lcore_conf->bbdev_id;
842 bbdev_queue_id = lcore_conf->dec_queue_id;
843 bbdev_op_pool = lcore_conf->bbdev_dec_op_pool;
844 enc_to_dec_ring = lcore_conf->enc_to_dec_ring;
845 llr_temp_buf = lcore_conf->llr_temp_buf;
847 /* Dequeue packets from the ring */
848 nb_recv = rte_ring_dequeue_burst(enc_to_dec_ring,
849 (void **)recv_pkts_burst, MAX_PKT_BURST, NULL);
853 if (unlikely(rte_bbdev_dec_op_alloc_bulk(bbdev_op_pool, bbdev_ops_burst,
855 pktmbuf_input_free_bulk(recv_pkts_burst, nb_recv);
856 lcore_stats->rx_lost_packets += nb_recv;
860 transform_enc_out_dec_in(recv_pkts_burst, llr_temp_buf, nb_recv,
861 def_op_dec.cb_params.k);
863 for (i = 0; i < nb_recv; i++) {
865 bbdev_ops_burst[i]->turbo_dec = def_op_dec;
867 bbdev_ops_burst[i]->turbo_dec.input.data = recv_pkts_burst[i];
868 bbdev_ops_burst[i]->turbo_dec.input.offset =
869 sizeof(struct rte_ether_hdr);
870 bbdev_ops_burst[i]->turbo_dec.input.length =
871 rte_pktmbuf_data_len(recv_pkts_burst[i])
872 - sizeof(struct rte_ether_hdr);
874 bbdev_ops_burst[i]->turbo_dec.hard_output.data =
876 bbdev_ops_burst[i]->turbo_dec.hard_output.offset =
877 sizeof(struct rte_ether_hdr);
880 /* Enqueue packets on BBDevice */
881 nb_enq = rte_bbdev_enqueue_dec_ops(bbdev_id, bbdev_queue_id,
882 bbdev_ops_burst, nb_recv);
883 if (unlikely(nb_enq < nb_recv)) {
884 pktmbuf_input_free_bulk(&recv_pkts_burst[nb_enq],
886 rte_bbdev_dec_op_free_bulk(&bbdev_ops_burst[nb_enq],
888 lcore_stats->rx_lost_packets += nb_recv - nb_enq;
894 lcore_stats->enqueued += nb_enq;
896 /* Dequeue packets from BBDevice */
899 nb_deq += rte_bbdev_dequeue_dec_ops(bbdev_id, bbdev_queue_id,
900 &bbdev_ops_burst[nb_deq], nb_enq - nb_deq);
901 } while (unlikely(nb_deq < nb_enq));
903 lcore_stats->dequeued += nb_deq;
905 rte_bbdev_dec_op_free_bulk(bbdev_ops_burst, nb_deq);
907 verify_data(recv_pkts_burst, nb_deq);
909 /* Free the RX mbufs after verification */
910 for (i = 0; i < nb_deq; ++i)
911 rte_pktmbuf_free(*mbuf_input(recv_pkts_burst[i]));
913 /* Transmit the packets */
914 nb_tx = rte_eth_tx_burst(port_id, tx_queue_id, recv_pkts_burst, nb_deq);
915 if (unlikely(nb_tx < nb_deq)) {
916 pktmbuf_input_free_bulk(&recv_pkts_burst[nb_tx],
918 lcore_stats->tx_lost_packets += nb_deq - nb_tx;
923 processing_loop(void *arg)
925 struct lcore_conf *lcore_conf = arg;
926 const bool run_encoder = (lcore_conf->core_type &
927 (1 << RTE_BBDEV_OP_TURBO_ENC));
928 const bool run_decoder = (lcore_conf->core_type &
929 (1 << RTE_BBDEV_OP_TURBO_DEC));
931 while (!rte_atomic16_read(&global_exit_flag)) {
933 run_encoding(lcore_conf);
935 run_decoding(lcore_conf);
942 prepare_bbdev_device(unsigned int dev_id, struct rte_bbdev_info *info,
943 struct app_config_params *app_params)
946 unsigned int q_id, dec_q_id, enc_q_id;
947 struct rte_bbdev_queue_conf qconf = {0};
948 uint16_t dec_qs_nb = app_params->num_dec_cores;
949 uint16_t enc_qs_nb = app_params->num_enc_cores;
950 uint16_t tot_qs = dec_qs_nb + enc_qs_nb;
952 ret = rte_bbdev_setup_queues(dev_id, tot_qs, info->socket_id);
954 rte_exit(EXIT_FAILURE,
955 "ERROR(%d): BBDEV %u not configured properly\n",
958 /* setup device DEC queues */
959 qconf.socket = info->socket_id;
960 qconf.queue_size = info->drv.queue_size_lim;
961 qconf.op_type = RTE_BBDEV_OP_TURBO_DEC;
963 for (q_id = 0, dec_q_id = 0; q_id < dec_qs_nb; q_id++) {
964 ret = rte_bbdev_queue_configure(dev_id, q_id, &qconf);
966 rte_exit(EXIT_FAILURE,
967 "ERROR(%d): BBDEV %u DEC queue %u not configured properly\n",
969 app_params->dec_queue_ids[dec_q_id++] = q_id;
972 /* setup device ENC queues */
973 qconf.op_type = RTE_BBDEV_OP_TURBO_ENC;
975 for (q_id = dec_qs_nb, enc_q_id = 0; q_id < tot_qs; q_id++) {
976 ret = rte_bbdev_queue_configure(dev_id, q_id, &qconf);
978 rte_exit(EXIT_FAILURE,
979 "ERROR(%d): BBDEV %u ENC queue %u not configured properly\n",
981 app_params->enc_queue_ids[enc_q_id++] = q_id;
984 ret = rte_bbdev_start(dev_id);
987 rte_exit(EXIT_FAILURE, "ERROR(%d): BBDEV %u not started\n",
990 printf("BBdev %u started\n", dev_id);
996 check_matching_capabilities(uint64_t mask, uint64_t required_mask)
998 return (mask & required_mask) == required_mask;
1002 enable_bbdev(struct app_config_params *app_params)
1004 struct rte_bbdev_info dev_info;
1005 const struct rte_bbdev_op_cap *op_cap;
1006 uint16_t bbdev_id = app_params->bbdev_id;
1007 bool encoder_capable = false;
1008 bool decoder_capable = false;
1010 rte_bbdev_info_get(bbdev_id, &dev_info);
1011 op_cap = dev_info.drv.capabilities;
1013 while (op_cap->type != RTE_BBDEV_OP_NONE) {
1014 if (op_cap->type == RTE_BBDEV_OP_TURBO_ENC) {
1015 if (check_matching_capabilities(
1016 op_cap->cap.turbo_enc.capability_flags,
1017 def_op_enc.op_flags))
1018 encoder_capable = true;
1021 if (op_cap->type == RTE_BBDEV_OP_TURBO_DEC) {
1022 if (check_matching_capabilities(
1023 op_cap->cap.turbo_dec.capability_flags,
1024 def_op_dec.op_flags))
1025 decoder_capable = true;
1031 if (encoder_capable == false)
1032 rte_exit(EXIT_FAILURE,
1033 "The specified BBDev %u doesn't have required encoder capabilities!\n",
1035 if (decoder_capable == false)
1036 rte_exit(EXIT_FAILURE,
1037 "The specified BBDev %u doesn't have required decoder capabilities!\n",
1040 prepare_bbdev_device(bbdev_id, &dev_info, app_params);
1044 main(int argc, char **argv)
1047 unsigned int nb_bbdevs, flags, lcore_id;
1049 struct app_config_params app_params = def_app_config;
1050 struct rte_mempool *ethdev_mbuf_mempool, *bbdev_mbuf_mempool;
1051 struct rte_mempool *bbdev_op_pools[RTE_BBDEV_OP_TYPE_COUNT];
1052 struct lcore_conf lcore_conf[RTE_MAX_LCORE] = { {0} };
1053 struct lcore_statistics lcore_stats[RTE_MAX_LCORE] = { {0} };
1054 struct stats_lcore_params stats_lcore;
1055 struct rte_ring *enc_to_dec_ring;
1056 bool stats_thread_started = false;
1057 unsigned int main_lcore_id = rte_get_main_lcore();
1059 static const struct rte_mbuf_dynfield input_dynfield_desc = {
1060 .name = "example_bbdev_dynfield_input",
1061 .size = sizeof(struct rte_mbuf *),
1062 .align = __alignof__(struct rte_mbuf *),
1065 rte_atomic16_init(&global_exit_flag);
1067 sigret = signal(SIGTERM, signal_handler);
1068 if (sigret == SIG_ERR)
1069 rte_exit(EXIT_FAILURE, "signal(%d, ...) failed", SIGTERM);
1071 sigret = signal(SIGINT, signal_handler);
1072 if (sigret == SIG_ERR)
1073 rte_exit(EXIT_FAILURE, "signal(%d, ...) failed", SIGINT);
1075 ret = rte_eal_init(argc, argv);
1077 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1082 /* parse application arguments (after the EAL ones) */
1083 ret = bbdev_parse_args(argc, argv, &app_params);
1085 rte_exit(EXIT_FAILURE, "Invalid BBDEV arguments\n");
1087 /*create bbdev op pools*/
1088 bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC] =
1089 rte_bbdev_op_pool_create("bbdev_op_pool_dec",
1090 RTE_BBDEV_OP_TURBO_DEC, NB_MBUF, 128, rte_socket_id());
1091 bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC] =
1092 rte_bbdev_op_pool_create("bbdev_op_pool_enc",
1093 RTE_BBDEV_OP_TURBO_ENC, NB_MBUF, 128, rte_socket_id());
1095 if ((bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC] == NULL) ||
1096 (bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC] == NULL))
1097 rte_exit(EXIT_FAILURE, "Cannot create bbdev op pools\n");
1099 /* Create encoder to decoder ring */
1100 flags = (app_params.num_enc_cores == 1) ? RING_F_SP_ENQ : 0;
1101 if (app_params.num_dec_cores == 1)
1102 flags |= RING_F_SC_DEQ;
1104 enc_to_dec_ring = rte_ring_create("enc_to_dec_ring",
1105 rte_align32pow2(NB_MBUF), rte_socket_id(), flags);
1107 /* Get the number of available bbdev devices */
1108 nb_bbdevs = rte_bbdev_count();
1109 if (nb_bbdevs <= app_params.bbdev_id)
1110 rte_exit(EXIT_FAILURE,
1111 "%u BBDevs detected, cannot use BBDev with ID %u!\n",
1112 nb_bbdevs, app_params.bbdev_id);
1113 printf("Number of bbdevs detected: %d\n", nb_bbdevs);
1115 if (!rte_eth_dev_is_valid_port(app_params.port_id))
1116 rte_exit(EXIT_FAILURE,
1117 "cannot use port with ID %u!\n",
1118 app_params.port_id);
1120 /* create the mbuf mempool for ethdev pkts */
1121 ethdev_mbuf_mempool = rte_pktmbuf_pool_create("ethdev_mbuf_pool",
1122 NB_MBUF, MEMPOOL_CACHE_SIZE, 0,
1123 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1124 if (ethdev_mbuf_mempool == NULL)
1125 rte_exit(EXIT_FAILURE, "Cannot create ethdev mbuf mempool\n");
1127 /* create the mbuf mempool for encoder output */
1128 bbdev_mbuf_mempool = rte_pktmbuf_pool_create("bbdev_mbuf_pool",
1129 NB_MBUF, MEMPOOL_CACHE_SIZE, 0,
1130 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1131 if (bbdev_mbuf_mempool == NULL)
1132 rte_exit(EXIT_FAILURE, "Cannot create ethdev mbuf mempool\n");
1134 /* register mbuf field to store input pointer */
1135 input_dynfield_offset =
1136 rte_mbuf_dynfield_register(&input_dynfield_desc);
1137 if (input_dynfield_offset < 0)
1138 rte_exit(EXIT_FAILURE, "Cannot register mbuf field\n");
1140 /* initialize ports */
1141 ret = initialize_ports(&app_params, ethdev_mbuf_mempool);
1143 /* Check if all requested lcores are available */
1144 for (lcore_id = 0; lcore_id < 8 * sizeof(uint64_t); ++lcore_id)
1145 if (((1ULL << lcore_id) & app_params.enc_core_mask) ||
1146 ((1ULL << lcore_id) & app_params.dec_core_mask))
1147 if (!rte_lcore_is_enabled(lcore_id))
1148 rte_exit(EXIT_FAILURE,
1149 "Requested lcore_id %u is not enabled!\n",
1152 /* Start ethernet port */
1153 ret = rte_eth_dev_start(app_params.port_id);
1155 rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
1156 ret, app_params.port_id);
1158 ret = check_port_link_status(app_params.port_id);
1162 /* start BBDevice and save BBDev queue IDs */
1163 enable_bbdev(&app_params);
1165 /* Initialize the port/queue configuration of each logical core */
1166 lcore_conf_init(&app_params, lcore_conf, bbdev_op_pools,
1167 bbdev_mbuf_mempool, enc_to_dec_ring, lcore_stats);
1169 stats_lcore.app_params = &app_params;
1170 stats_lcore.lconf = lcore_conf;
1172 RTE_LCORE_FOREACH_WORKER(lcore_id) {
1173 if (lcore_conf[lcore_id].core_type != 0)
1174 /* launch per-lcore processing loop on worker lcores */
1175 rte_eal_remote_launch(processing_loop,
1176 &lcore_conf[lcore_id], lcore_id);
1177 else if (!stats_thread_started) {
1178 /* launch statistics printing loop */
1179 rte_eal_remote_launch(stats_loop, &stats_lcore,
1181 stats_thread_started = true;
1185 if (!stats_thread_started &&
1186 lcore_conf[main_lcore_id].core_type != 0)
1187 rte_exit(EXIT_FAILURE,
1188 "Not enough lcores to run the statistics printing loop!");
1189 else if (lcore_conf[main_lcore_id].core_type != 0)
1190 processing_loop(&lcore_conf[main_lcore_id]);
1191 else if (!stats_thread_started)
1192 stats_loop(&stats_lcore);
1194 RTE_LCORE_FOREACH_WORKER(lcore_id) {
1195 ret |= rte_eal_wait_lcore(lcore_id);
1198 /* clean up the EAL */