xref: /dpdk/examples/bbdev_app/main.c (revision a7db3afce75346832059d8bfe54a8f81945fb213)
11ffee690SAmr Mokhtar /* SPDX-License-Identifier: BSD-3-Clause
21ffee690SAmr Mokhtar  * Copyright(c) 2017 Intel Corporation
31ffee690SAmr Mokhtar  */
41ffee690SAmr Mokhtar 
51ffee690SAmr Mokhtar #include <stdio.h>
61ffee690SAmr Mokhtar #include <stdlib.h>
71ffee690SAmr Mokhtar #include <string.h>
81ffee690SAmr Mokhtar #include <stdint.h>
91ffee690SAmr Mokhtar #include <inttypes.h>
101ffee690SAmr Mokhtar #include <sys/types.h>
11960f28b2SThomas Monjalon #include <unistd.h>
121ffee690SAmr Mokhtar #include <sys/queue.h>
131ffee690SAmr Mokhtar #include <stdarg.h>
141ffee690SAmr Mokhtar #include <ctype.h>
151ffee690SAmr Mokhtar #include <errno.h>
161ffee690SAmr Mokhtar #include <math.h>
171ffee690SAmr Mokhtar #include <assert.h>
181ffee690SAmr Mokhtar #include <getopt.h>
191ffee690SAmr Mokhtar #include <signal.h>
201ffee690SAmr Mokhtar 
217b0de673SThomas Monjalon #include <rte_atomic.h>
227b0de673SThomas Monjalon #include <rte_common.h>
237b0de673SThomas Monjalon #include <rte_eal.h>
247b0de673SThomas Monjalon #include <rte_cycles.h>
257b0de673SThomas Monjalon #include <rte_ether.h>
267b0de673SThomas Monjalon #include <rte_ethdev.h>
277b0de673SThomas Monjalon #include <rte_ip.h>
287b0de673SThomas Monjalon #include <rte_lcore.h>
297b0de673SThomas Monjalon #include <rte_malloc.h>
307b0de673SThomas Monjalon #include <rte_mbuf.h>
3161bf499dSThomas Monjalon #include <rte_mbuf_dyn.h>
327b0de673SThomas Monjalon #include <rte_memory.h>
337b0de673SThomas Monjalon #include <rte_mempool.h>
347b0de673SThomas Monjalon #include <rte_log.h>
357b0de673SThomas Monjalon #include <rte_bbdev.h>
367b0de673SThomas Monjalon #include <rte_bbdev_op.h>
371ffee690SAmr Mokhtar 
381ffee690SAmr Mokhtar /* LLR values - negative value for '1' bit */
391ffee690SAmr Mokhtar #define LLR_1_BIT 0x81
401ffee690SAmr Mokhtar #define LLR_0_BIT 0x7F
411ffee690SAmr Mokhtar 
421ffee690SAmr Mokhtar #define MAX_PKT_BURST 32
431ffee690SAmr Mokhtar #define NB_MBUF 8191
441ffee690SAmr Mokhtar #define MEMPOOL_CACHE_SIZE 256
451ffee690SAmr Mokhtar 
461ffee690SAmr Mokhtar /* Hardcoded K value */
471ffee690SAmr Mokhtar #define K 40
481ffee690SAmr Mokhtar #define NCB (3 * RTE_ALIGN_CEIL(K + 4, 32))
491ffee690SAmr Mokhtar 
501ffee690SAmr Mokhtar #define CRC_24B_LEN 3
511ffee690SAmr Mokhtar 
521ffee690SAmr Mokhtar /* Configurable number of RX/TX ring descriptors */
531ffee690SAmr Mokhtar #define RTE_TEST_RX_DESC_DEFAULT 128
541ffee690SAmr Mokhtar #define RTE_TEST_TX_DESC_DEFAULT 512
551ffee690SAmr Mokhtar 
561ffee690SAmr Mokhtar #define BBDEV_ASSERT(a) do { \
571ffee690SAmr Mokhtar 	if (!(a)) { \
581ffee690SAmr Mokhtar 		usage(prgname); \
591ffee690SAmr Mokhtar 		return -1; \
601ffee690SAmr Mokhtar 	} \
611ffee690SAmr Mokhtar } while (0)
621ffee690SAmr Mokhtar 
6361bf499dSThomas Monjalon static int input_dynfield_offset = -1;
6461bf499dSThomas Monjalon 
6561bf499dSThomas Monjalon static inline struct rte_mbuf **
6661bf499dSThomas Monjalon mbuf_input(struct rte_mbuf *mbuf)
6761bf499dSThomas Monjalon {
6861bf499dSThomas Monjalon 	return RTE_MBUF_DYNFIELD(mbuf,
6961bf499dSThomas Monjalon 			input_dynfield_offset, struct rte_mbuf **);
7061bf499dSThomas Monjalon }
7161bf499dSThomas Monjalon 
721ffee690SAmr Mokhtar static const struct rte_eth_conf port_conf = {
731ffee690SAmr Mokhtar 	.rxmode = {
741ffee690SAmr Mokhtar 		.mq_mode = ETH_MQ_RX_NONE,
7535b2d13fSOlivier Matz 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
761ffee690SAmr Mokhtar 		.split_hdr_size = 0,
771ffee690SAmr Mokhtar 	},
781ffee690SAmr Mokhtar 	.txmode = {
791ffee690SAmr Mokhtar 		.mq_mode = ETH_MQ_TX_NONE,
801ffee690SAmr Mokhtar 	},
811ffee690SAmr Mokhtar };
821ffee690SAmr Mokhtar 
831ffee690SAmr Mokhtar struct rte_bbdev_op_turbo_enc def_op_enc = {
841ffee690SAmr Mokhtar 	/* These values are arbitrarily put, and does not map to the real
851ffee690SAmr Mokhtar 	 * values for the data received from ethdev ports
861ffee690SAmr Mokhtar 	 */
871ffee690SAmr Mokhtar 	.rv_index = 0,
881ffee690SAmr Mokhtar 	.code_block_mode = 1,
891ffee690SAmr Mokhtar 	.cb_params = {
901ffee690SAmr Mokhtar 		.k = K,
911ffee690SAmr Mokhtar 	},
921ffee690SAmr Mokhtar 	.op_flags = RTE_BBDEV_TURBO_CRC_24A_ATTACH
931ffee690SAmr Mokhtar };
941ffee690SAmr Mokhtar 
951ffee690SAmr Mokhtar struct rte_bbdev_op_turbo_dec def_op_dec = {
961ffee690SAmr Mokhtar 	/* These values are arbitrarily put, and does not map to the real
971ffee690SAmr Mokhtar 	 * values for the data received from ethdev ports
981ffee690SAmr Mokhtar 	 */
991ffee690SAmr Mokhtar 	.code_block_mode = 1,
1001ffee690SAmr Mokhtar 	.cb_params = {
1011ffee690SAmr Mokhtar 		.k = K,
1021ffee690SAmr Mokhtar 	},
1031ffee690SAmr Mokhtar 	.rv_index = 0,
1041ffee690SAmr Mokhtar 	.iter_max = 8,
1051ffee690SAmr Mokhtar 	.iter_min = 4,
1061ffee690SAmr Mokhtar 	.ext_scale = 15,
1071ffee690SAmr Mokhtar 	.num_maps = 0,
1081ffee690SAmr Mokhtar 	.op_flags = RTE_BBDEV_TURBO_NEG_LLR_1_BIT_IN
1091ffee690SAmr Mokhtar };
1101ffee690SAmr Mokhtar 
1111ffee690SAmr Mokhtar struct app_config_params {
1121ffee690SAmr Mokhtar 	/* Placeholders for app params */
1131ffee690SAmr Mokhtar 	uint16_t port_id;
1141ffee690SAmr Mokhtar 	uint16_t bbdev_id;
1151ffee690SAmr Mokhtar 	uint64_t enc_core_mask;
1161ffee690SAmr Mokhtar 	uint64_t dec_core_mask;
1171ffee690SAmr Mokhtar 
1181ffee690SAmr Mokhtar 	/* Values filled during init time */
1191ffee690SAmr Mokhtar 	uint16_t enc_queue_ids[RTE_MAX_LCORE];
1201ffee690SAmr Mokhtar 	uint16_t dec_queue_ids[RTE_MAX_LCORE];
1211ffee690SAmr Mokhtar 	uint16_t num_enc_cores;
1221ffee690SAmr Mokhtar 	uint16_t num_dec_cores;
1231ffee690SAmr Mokhtar };
1241ffee690SAmr Mokhtar 
1251ffee690SAmr Mokhtar struct lcore_statistics {
1261ffee690SAmr Mokhtar 	unsigned int enqueued;
1271ffee690SAmr Mokhtar 	unsigned int dequeued;
1281ffee690SAmr Mokhtar 	unsigned int rx_lost_packets;
1291ffee690SAmr Mokhtar 	unsigned int enc_to_dec_lost_packets;
1301ffee690SAmr Mokhtar 	unsigned int tx_lost_packets;
1311ffee690SAmr Mokhtar } __rte_cache_aligned;
1321ffee690SAmr Mokhtar 
1331ffee690SAmr Mokhtar /** each lcore configuration */
1341ffee690SAmr Mokhtar struct lcore_conf {
1351ffee690SAmr Mokhtar 	uint64_t core_type;
1361ffee690SAmr Mokhtar 
1371ffee690SAmr Mokhtar 	unsigned int port_id;
1381ffee690SAmr Mokhtar 	unsigned int rx_queue_id;
1391ffee690SAmr Mokhtar 	unsigned int tx_queue_id;
1401ffee690SAmr Mokhtar 
1411ffee690SAmr Mokhtar 	unsigned int bbdev_id;
1421ffee690SAmr Mokhtar 	unsigned int enc_queue_id;
1431ffee690SAmr Mokhtar 	unsigned int dec_queue_id;
1441ffee690SAmr Mokhtar 
1451ffee690SAmr Mokhtar 	uint8_t llr_temp_buf[NCB];
1461ffee690SAmr Mokhtar 
1471ffee690SAmr Mokhtar 	struct rte_mempool *bbdev_dec_op_pool;
1481ffee690SAmr Mokhtar 	struct rte_mempool *bbdev_enc_op_pool;
1491ffee690SAmr Mokhtar 	struct rte_mempool *enc_out_pool;
1501ffee690SAmr Mokhtar 	struct rte_ring *enc_to_dec_ring;
1511ffee690SAmr Mokhtar 
1521ffee690SAmr Mokhtar 	struct lcore_statistics *lcore_stats;
1531ffee690SAmr Mokhtar } __rte_cache_aligned;
1541ffee690SAmr Mokhtar 
1551ffee690SAmr Mokhtar struct stats_lcore_params {
1561ffee690SAmr Mokhtar 	struct lcore_conf *lconf;
1571ffee690SAmr Mokhtar 	struct app_config_params *app_params;
1581ffee690SAmr Mokhtar };
1591ffee690SAmr Mokhtar 
1601ffee690SAmr Mokhtar 
1611ffee690SAmr Mokhtar static const struct app_config_params def_app_config = {
1621ffee690SAmr Mokhtar 	.port_id = 0,
1631ffee690SAmr Mokhtar 	.bbdev_id = 0,
1641ffee690SAmr Mokhtar 	.enc_core_mask = 0x2,
1651ffee690SAmr Mokhtar 	.dec_core_mask = 0x4,
1661ffee690SAmr Mokhtar 	.num_enc_cores = 1,
1671ffee690SAmr Mokhtar 	.num_dec_cores = 1,
1681ffee690SAmr Mokhtar };
1691ffee690SAmr Mokhtar 
1701ffee690SAmr Mokhtar static rte_atomic16_t global_exit_flag;
1711ffee690SAmr Mokhtar 
1721ffee690SAmr Mokhtar /* display usage */
1731ffee690SAmr Mokhtar static inline void
1741ffee690SAmr Mokhtar usage(const char *prgname)
1751ffee690SAmr Mokhtar {
1761ffee690SAmr Mokhtar 	printf("%s [EAL options] "
1771ffee690SAmr Mokhtar 			"  --\n"
1781ffee690SAmr Mokhtar 			"  --enc_cores - number of encoding cores (default = 0x2)\n"
1791ffee690SAmr Mokhtar 			"  --dec_cores - number of decoding cores (default = 0x4)\n"
1801ffee690SAmr Mokhtar 			"  --port_id - Ethernet port ID (default = 0)\n"
1811ffee690SAmr Mokhtar 			"  --bbdev_id - BBDev ID (default = 0)\n"
1821ffee690SAmr Mokhtar 			"\n", prgname);
1831ffee690SAmr Mokhtar }
1841ffee690SAmr Mokhtar 
1851ffee690SAmr Mokhtar /* parse core mask */
1861ffee690SAmr Mokhtar static inline
1871ffee690SAmr Mokhtar uint16_t bbdev_parse_mask(const char *mask)
1881ffee690SAmr Mokhtar {
1891ffee690SAmr Mokhtar 	char *end = NULL;
1901ffee690SAmr Mokhtar 	unsigned long pm;
1911ffee690SAmr Mokhtar 
1921ffee690SAmr Mokhtar 	/* parse hexadecimal string */
1931ffee690SAmr Mokhtar 	pm = strtoul(mask, &end, 16);
1941ffee690SAmr Mokhtar 	if ((mask[0] == '\0') || (end == NULL) || (*end != '\0'))
1951ffee690SAmr Mokhtar 		return 0;
1961ffee690SAmr Mokhtar 
1971ffee690SAmr Mokhtar 	return pm;
1981ffee690SAmr Mokhtar }
1991ffee690SAmr Mokhtar 
2001ffee690SAmr Mokhtar /* parse core mask */
2011ffee690SAmr Mokhtar static inline
2021ffee690SAmr Mokhtar uint16_t bbdev_parse_number(const char *mask)
2031ffee690SAmr Mokhtar {
2041ffee690SAmr Mokhtar 	char *end = NULL;
2051ffee690SAmr Mokhtar 	unsigned long pm;
2061ffee690SAmr Mokhtar 
2071ffee690SAmr Mokhtar 	/* parse hexadecimal string */
2081ffee690SAmr Mokhtar 	pm = strtoul(mask, &end, 10);
2091ffee690SAmr Mokhtar 	if ((mask[0] == '\0') || (end == NULL) || (*end != '\0'))
2101ffee690SAmr Mokhtar 		return 0;
2111ffee690SAmr Mokhtar 
2121ffee690SAmr Mokhtar 	return pm;
2131ffee690SAmr Mokhtar }
2141ffee690SAmr Mokhtar 
2151ffee690SAmr Mokhtar static int
2161ffee690SAmr Mokhtar bbdev_parse_args(int argc, char **argv,
2171ffee690SAmr Mokhtar 		struct app_config_params *app_params)
2181ffee690SAmr Mokhtar {
2191ffee690SAmr Mokhtar 	int optind = 0;
2201ffee690SAmr Mokhtar 	int opt;
2211ffee690SAmr Mokhtar 	int opt_indx = 0;
2221ffee690SAmr Mokhtar 	char *prgname = argv[0];
2231ffee690SAmr Mokhtar 
2241ffee690SAmr Mokhtar 	static struct option lgopts[] = {
2251ffee690SAmr Mokhtar 		{ "enc_core_mask", required_argument, 0, 'e' },
2261ffee690SAmr Mokhtar 		{ "dec_core_mask", required_argument, 0, 'd' },
2271ffee690SAmr Mokhtar 		{ "port_id", required_argument, 0, 'p' },
2281ffee690SAmr Mokhtar 		{ "bbdev_id", required_argument, 0, 'b' },
2291ffee690SAmr Mokhtar 		{ NULL, 0, 0, 0 }
2301ffee690SAmr Mokhtar 	};
2311ffee690SAmr Mokhtar 
2321ffee690SAmr Mokhtar 	BBDEV_ASSERT(argc != 0);
2331ffee690SAmr Mokhtar 	BBDEV_ASSERT(argv != NULL);
2341ffee690SAmr Mokhtar 	BBDEV_ASSERT(app_params != NULL);
2351ffee690SAmr Mokhtar 
2361ffee690SAmr Mokhtar 	while ((opt = getopt_long(argc, argv, "e:d:p:b:", lgopts, &opt_indx)) !=
2371ffee690SAmr Mokhtar 		EOF) {
2381ffee690SAmr Mokhtar 		switch (opt) {
2391ffee690SAmr Mokhtar 		case 'e':
2401ffee690SAmr Mokhtar 			app_params->enc_core_mask =
2411ffee690SAmr Mokhtar 				bbdev_parse_mask(optarg);
2421ffee690SAmr Mokhtar 			if (app_params->enc_core_mask == 0) {
2431ffee690SAmr Mokhtar 				usage(prgname);
2441ffee690SAmr Mokhtar 				return -1;
2451ffee690SAmr Mokhtar 			}
2461ffee690SAmr Mokhtar 			app_params->num_enc_cores =
2471ffee690SAmr Mokhtar 				__builtin_popcount(app_params->enc_core_mask);
2481ffee690SAmr Mokhtar 			break;
2491ffee690SAmr Mokhtar 
2501ffee690SAmr Mokhtar 		case 'd':
2511ffee690SAmr Mokhtar 			app_params->dec_core_mask =
2521ffee690SAmr Mokhtar 				bbdev_parse_mask(optarg);
2531ffee690SAmr Mokhtar 			if (app_params->dec_core_mask == 0) {
2541ffee690SAmr Mokhtar 				usage(prgname);
2551ffee690SAmr Mokhtar 				return -1;
2561ffee690SAmr Mokhtar 			}
2571ffee690SAmr Mokhtar 			app_params->num_dec_cores =
2581ffee690SAmr Mokhtar 				__builtin_popcount(app_params->dec_core_mask);
2591ffee690SAmr Mokhtar 			break;
2601ffee690SAmr Mokhtar 
2611ffee690SAmr Mokhtar 		case 'p':
2621ffee690SAmr Mokhtar 			app_params->port_id = bbdev_parse_number(optarg);
2631ffee690SAmr Mokhtar 			break;
2641ffee690SAmr Mokhtar 
2651ffee690SAmr Mokhtar 		case 'b':
2661ffee690SAmr Mokhtar 			app_params->bbdev_id = bbdev_parse_number(optarg);
2671ffee690SAmr Mokhtar 			break;
2681ffee690SAmr Mokhtar 
2691ffee690SAmr Mokhtar 		default:
2701ffee690SAmr Mokhtar 			usage(prgname);
2711ffee690SAmr Mokhtar 			return -1;
2721ffee690SAmr Mokhtar 		}
2731ffee690SAmr Mokhtar 	}
2741ffee690SAmr Mokhtar 	optind = 0;
2751ffee690SAmr Mokhtar 	return optind;
2761ffee690SAmr Mokhtar }
2771ffee690SAmr Mokhtar 
2781ffee690SAmr Mokhtar static void
2791ffee690SAmr Mokhtar signal_handler(int signum)
2801ffee690SAmr Mokhtar {
2811ffee690SAmr Mokhtar 	printf("\nSignal %d received\n", signum);
2821ffee690SAmr Mokhtar 	rte_atomic16_set(&global_exit_flag, 1);
2831ffee690SAmr Mokhtar }
2841ffee690SAmr Mokhtar 
2851ffee690SAmr Mokhtar static void
2866d13ea8eSOlivier Matz print_mac(unsigned int portid, struct rte_ether_addr *bbdev_ports_eth_address)
2871ffee690SAmr Mokhtar {
288c2c4f87bSAman Deep Singh 	printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
2891ffee690SAmr Mokhtar 			(unsigned int) portid,
290*a7db3afcSAman Deep Singh 			RTE_ETHER_ADDR_BYTES(bbdev_ports_eth_address));
2911ffee690SAmr Mokhtar }
2921ffee690SAmr Mokhtar 
2931ffee690SAmr Mokhtar static inline void
2941ffee690SAmr Mokhtar pktmbuf_free_bulk(struct rte_mbuf **mbufs, unsigned int nb_to_free)
2951ffee690SAmr Mokhtar {
2961ffee690SAmr Mokhtar 	unsigned int i;
2971ffee690SAmr Mokhtar 	for (i = 0; i < nb_to_free; ++i)
2981ffee690SAmr Mokhtar 		rte_pktmbuf_free(mbufs[i]);
2991ffee690SAmr Mokhtar }
3001ffee690SAmr Mokhtar 
3011ffee690SAmr Mokhtar static inline void
30261bf499dSThomas Monjalon pktmbuf_input_free_bulk(struct rte_mbuf **mbufs, unsigned int nb_to_free)
3031ffee690SAmr Mokhtar {
3041ffee690SAmr Mokhtar 	unsigned int i;
3051ffee690SAmr Mokhtar 	for (i = 0; i < nb_to_free; ++i) {
30661bf499dSThomas Monjalon 		struct rte_mbuf *rx_pkt = *mbuf_input(mbufs[i]);
3071ffee690SAmr Mokhtar 		rte_pktmbuf_free(rx_pkt);
3081ffee690SAmr Mokhtar 		rte_pktmbuf_free(mbufs[i]);
3091ffee690SAmr Mokhtar 	}
3101ffee690SAmr Mokhtar }
3111ffee690SAmr Mokhtar 
3121ffee690SAmr Mokhtar /* Check the link status of all ports in up to 9s, and print them finally */
3131ffee690SAmr Mokhtar static int
3141ffee690SAmr Mokhtar check_port_link_status(uint16_t port_id)
3151ffee690SAmr Mokhtar {
3161ffee690SAmr Mokhtar #define CHECK_INTERVAL 100 /* 100ms */
3171ffee690SAmr Mokhtar #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3181ffee690SAmr Mokhtar 	uint8_t count;
3191ffee690SAmr Mokhtar 	struct rte_eth_link link;
32022e5c73bSIgor Romanov 	int link_get_err = -EINVAL;
3211ffee690SAmr Mokhtar 
3221ffee690SAmr Mokhtar 	printf("\nChecking link status.");
3231ffee690SAmr Mokhtar 	fflush(stdout);
3241ffee690SAmr Mokhtar 
3251ffee690SAmr Mokhtar 	for (count = 0; count <= MAX_CHECK_TIME &&
3261ffee690SAmr Mokhtar 			!rte_atomic16_read(&global_exit_flag); count++) {
3271ffee690SAmr Mokhtar 		memset(&link, 0, sizeof(link));
32822e5c73bSIgor Romanov 		link_get_err = rte_eth_link_get_nowait(port_id, &link);
3291ffee690SAmr Mokhtar 
33022e5c73bSIgor Romanov 		if (link_get_err >= 0 && link.link_status) {
3311ffee690SAmr Mokhtar 			const char *dp = (link.link_duplex ==
3321ffee690SAmr Mokhtar 				ETH_LINK_FULL_DUPLEX) ?
3331ffee690SAmr Mokhtar 				"full-duplex" : "half-duplex";
334db4e8135SIvan Dyukov 			printf("\nPort %u Link Up - speed %s - %s\n",
335db4e8135SIvan Dyukov 				port_id,
336db4e8135SIvan Dyukov 				rte_eth_link_speed_to_str(link.link_speed),
337db4e8135SIvan Dyukov 				dp);
3381ffee690SAmr Mokhtar 			return 0;
3391ffee690SAmr Mokhtar 		}
3401ffee690SAmr Mokhtar 		printf(".");
3411ffee690SAmr Mokhtar 		fflush(stdout);
3421ffee690SAmr Mokhtar 		rte_delay_ms(CHECK_INTERVAL);
3431ffee690SAmr Mokhtar 	}
3441ffee690SAmr Mokhtar 
34522e5c73bSIgor Romanov 	if (link_get_err >= 0)
3461ffee690SAmr Mokhtar 		printf("\nPort %d Link Down\n", port_id);
34722e5c73bSIgor Romanov 	else
34822e5c73bSIgor Romanov 		printf("\nGet link failed (port %d): %s\n", port_id,
34922e5c73bSIgor Romanov 		       rte_strerror(-link_get_err));
35022e5c73bSIgor Romanov 
3511ffee690SAmr Mokhtar 	return 0;
3521ffee690SAmr Mokhtar }
3531ffee690SAmr Mokhtar 
3541ffee690SAmr Mokhtar static inline void
3551ffee690SAmr Mokhtar add_ether_hdr(struct rte_mbuf *pkt_src, struct rte_mbuf *pkt_dst)
3561ffee690SAmr Mokhtar {
3576d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_from;
3586d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_to;
3591ffee690SAmr Mokhtar 
3606d13ea8eSOlivier Matz 	eth_from = rte_pktmbuf_mtod(pkt_src, struct rte_ether_hdr *);
3616d13ea8eSOlivier Matz 	eth_to = rte_pktmbuf_mtod(pkt_dst, struct rte_ether_hdr *);
3621ffee690SAmr Mokhtar 
3631ffee690SAmr Mokhtar 	/* copy header */
3646d13ea8eSOlivier Matz 	rte_memcpy(eth_to, eth_from, sizeof(struct rte_ether_hdr));
3651ffee690SAmr Mokhtar }
3661ffee690SAmr Mokhtar 
3671ffee690SAmr Mokhtar static inline void
3681ffee690SAmr Mokhtar add_awgn(struct rte_mbuf **mbufs, uint16_t num_pkts)
3691ffee690SAmr Mokhtar {
3701ffee690SAmr Mokhtar 	RTE_SET_USED(mbufs);
3711ffee690SAmr Mokhtar 	RTE_SET_USED(num_pkts);
3721ffee690SAmr Mokhtar }
3731ffee690SAmr Mokhtar 
3741ffee690SAmr Mokhtar /* Encoder output to Decoder input adapter. The Decoder accepts only soft input
3751ffee690SAmr Mokhtar  * so each bit of the encoder output must be translated into one byte of LLR. If
3761ffee690SAmr Mokhtar  * Sub-block Deinterleaver is bypassed, which is the case, the padding bytes
3771ffee690SAmr Mokhtar  * must additionally be insterted at the end of each sub-block.
3781ffee690SAmr Mokhtar  */
3791ffee690SAmr Mokhtar static inline void
3801ffee690SAmr Mokhtar transform_enc_out_dec_in(struct rte_mbuf **mbufs, uint8_t *temp_buf,
3811ffee690SAmr Mokhtar 		uint16_t num_pkts, uint16_t k)
3821ffee690SAmr Mokhtar {
3831ffee690SAmr Mokhtar 	uint16_t i, l, j;
3841ffee690SAmr Mokhtar 	uint16_t start_bit_idx;
3851ffee690SAmr Mokhtar 	uint16_t out_idx;
3861ffee690SAmr Mokhtar 	uint16_t d = k + 4;
3871ffee690SAmr Mokhtar 	uint16_t kpi = RTE_ALIGN_CEIL(d, 32);
3881ffee690SAmr Mokhtar 	uint16_t nd = kpi - d;
3891ffee690SAmr Mokhtar 	uint16_t ncb = 3 * kpi;
3901ffee690SAmr Mokhtar 
3911ffee690SAmr Mokhtar 	for (i = 0; i < num_pkts; ++i) {
3921ffee690SAmr Mokhtar 		uint16_t pkt_data_len = rte_pktmbuf_data_len(mbufs[i]) -
3936d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr);
3941ffee690SAmr Mokhtar 
3951ffee690SAmr Mokhtar 		/* Resize the packet if needed */
3961ffee690SAmr Mokhtar 		if (pkt_data_len < ncb) {
3971ffee690SAmr Mokhtar 			char *data = rte_pktmbuf_append(mbufs[i],
3981ffee690SAmr Mokhtar 					ncb - pkt_data_len);
3991ffee690SAmr Mokhtar 			if (data == NULL)
4001ffee690SAmr Mokhtar 				printf(
4011ffee690SAmr Mokhtar 					"Not enough space in decoder input packet");
4021ffee690SAmr Mokhtar 		}
4031ffee690SAmr Mokhtar 
4041ffee690SAmr Mokhtar 		/* Translate each bit into 1 LLR byte. */
4051ffee690SAmr Mokhtar 		start_bit_idx = 0;
4061ffee690SAmr Mokhtar 		out_idx = 0;
4071ffee690SAmr Mokhtar 		for (j = 0; j < 3; ++j) {
4081ffee690SAmr Mokhtar 			for (l = start_bit_idx; l < start_bit_idx + d; ++l) {
4091ffee690SAmr Mokhtar 				uint8_t *data = rte_pktmbuf_mtod_offset(
4101ffee690SAmr Mokhtar 					mbufs[i], uint8_t *,
4116d13ea8eSOlivier Matz 					sizeof(struct rte_ether_hdr) +
4126d13ea8eSOlivier Matz 					(l >> 3));
4131ffee690SAmr Mokhtar 				if (*data & (0x80 >> (l & 7)))
4141ffee690SAmr Mokhtar 					temp_buf[out_idx] = LLR_1_BIT;
4151ffee690SAmr Mokhtar 				else
4161ffee690SAmr Mokhtar 					temp_buf[out_idx] = LLR_0_BIT;
4171ffee690SAmr Mokhtar 				++out_idx;
4181ffee690SAmr Mokhtar 			}
4191ffee690SAmr Mokhtar 			/* Padding bytes should be at the end of the sub-block.
4201ffee690SAmr Mokhtar 			 */
4211ffee690SAmr Mokhtar 			memset(&temp_buf[out_idx], 0, nd);
4221ffee690SAmr Mokhtar 			out_idx += nd;
4231ffee690SAmr Mokhtar 			start_bit_idx += d;
4241ffee690SAmr Mokhtar 		}
4251ffee690SAmr Mokhtar 
4261ffee690SAmr Mokhtar 		rte_memcpy(rte_pktmbuf_mtod_offset(mbufs[i], uint8_t *,
4276d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr)), temp_buf, ncb);
4281ffee690SAmr Mokhtar 	}
4291ffee690SAmr Mokhtar }
4301ffee690SAmr Mokhtar 
4311ffee690SAmr Mokhtar static inline void
4321ffee690SAmr Mokhtar verify_data(struct rte_mbuf **mbufs, uint16_t num_pkts)
4331ffee690SAmr Mokhtar {
4341ffee690SAmr Mokhtar 	uint16_t i;
4351ffee690SAmr Mokhtar 	for (i = 0; i < num_pkts; ++i) {
4361ffee690SAmr Mokhtar 		struct rte_mbuf *out = mbufs[i];
43761bf499dSThomas Monjalon 		struct rte_mbuf *in = *mbuf_input(out);
4381ffee690SAmr Mokhtar 
4391ffee690SAmr Mokhtar 		if (memcmp(rte_pktmbuf_mtod_offset(in, uint8_t *,
4406d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr)),
4411ffee690SAmr Mokhtar 				rte_pktmbuf_mtod_offset(out, uint8_t *,
4426d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr)),
4431ffee690SAmr Mokhtar 				K / 8 - CRC_24B_LEN))
4441ffee690SAmr Mokhtar 			printf("Input and output buffers are not equal!\n");
4451ffee690SAmr Mokhtar 	}
4461ffee690SAmr Mokhtar }
4471ffee690SAmr Mokhtar 
4481ffee690SAmr Mokhtar static int
4491ffee690SAmr Mokhtar initialize_ports(struct app_config_params *app_params,
4501ffee690SAmr Mokhtar 		struct rte_mempool *ethdev_mbuf_mempool)
4511ffee690SAmr Mokhtar {
4521ffee690SAmr Mokhtar 	int ret;
4531ffee690SAmr Mokhtar 	uint16_t port_id = app_params->port_id;
4541ffee690SAmr Mokhtar 	uint16_t q;
4551ffee690SAmr Mokhtar 	/* ethernet addresses of ports */
4566d13ea8eSOlivier Matz 	struct rte_ether_addr bbdev_port_eth_addr;
4571ffee690SAmr Mokhtar 
4581ffee690SAmr Mokhtar 	/* initialize ports */
4591ffee690SAmr Mokhtar 	printf("\nInitializing port %u...\n", app_params->port_id);
4601ffee690SAmr Mokhtar 	ret = rte_eth_dev_configure(port_id, app_params->num_enc_cores,
4611ffee690SAmr Mokhtar 		app_params->num_dec_cores, &port_conf);
4621ffee690SAmr Mokhtar 
4631ffee690SAmr Mokhtar 	if (ret < 0) {
4641ffee690SAmr Mokhtar 		printf("Cannot configure device: err=%d, port=%u\n",
4651ffee690SAmr Mokhtar 			ret, port_id);
4661ffee690SAmr Mokhtar 		return -1;
4671ffee690SAmr Mokhtar 	}
4681ffee690SAmr Mokhtar 
4691ffee690SAmr Mokhtar 	/* initialize RX queues for encoder */
4701ffee690SAmr Mokhtar 	for (q = 0; q < app_params->num_enc_cores; q++) {
4711ffee690SAmr Mokhtar 		ret = rte_eth_rx_queue_setup(port_id, q,
4721ffee690SAmr Mokhtar 			RTE_TEST_RX_DESC_DEFAULT,
4731ffee690SAmr Mokhtar 			rte_eth_dev_socket_id(port_id),
4741ffee690SAmr Mokhtar 			NULL, ethdev_mbuf_mempool);
4751ffee690SAmr Mokhtar 		if (ret < 0) {
4761ffee690SAmr Mokhtar 			printf("rte_eth_rx_queue_setup: err=%d, queue=%u\n",
4771ffee690SAmr Mokhtar 				ret, q);
4781ffee690SAmr Mokhtar 			return -1;
4791ffee690SAmr Mokhtar 		}
4801ffee690SAmr Mokhtar 	}
4811ffee690SAmr Mokhtar 	/* initialize TX queues for decoder */
4821ffee690SAmr Mokhtar 	for (q = 0; q < app_params->num_dec_cores; q++) {
4831ffee690SAmr Mokhtar 		ret = rte_eth_tx_queue_setup(port_id, q,
4841ffee690SAmr Mokhtar 			RTE_TEST_TX_DESC_DEFAULT,
4851ffee690SAmr Mokhtar 			rte_eth_dev_socket_id(port_id), NULL);
4861ffee690SAmr Mokhtar 		if (ret < 0) {
4871ffee690SAmr Mokhtar 			printf("rte_eth_tx_queue_setup: err=%d, queue=%u\n",
4881ffee690SAmr Mokhtar 				ret, q);
4891ffee690SAmr Mokhtar 			return -1;
4901ffee690SAmr Mokhtar 		}
4911ffee690SAmr Mokhtar 	}
4921ffee690SAmr Mokhtar 
493f430bbceSIvan Ilchenko 	ret = rte_eth_promiscuous_enable(port_id);
494f430bbceSIvan Ilchenko 	if (ret != 0) {
495f430bbceSIvan Ilchenko 		printf("Cannot enable promiscuous mode: err=%s, port=%u\n",
496f430bbceSIvan Ilchenko 			rte_strerror(-ret), port_id);
497f430bbceSIvan Ilchenko 		return ret;
498f430bbceSIvan Ilchenko 	}
4991ffee690SAmr Mokhtar 
50070febdcfSIgor Romanov 	ret = rte_eth_macaddr_get(port_id, &bbdev_port_eth_addr);
50170febdcfSIgor Romanov 	if (ret < 0) {
50270febdcfSIgor Romanov 		printf("rte_eth_macaddr_get: err=%d, queue=%u\n",
50370febdcfSIgor Romanov 			ret, q);
50470febdcfSIgor Romanov 		return -1;
50570febdcfSIgor Romanov 	}
50670febdcfSIgor Romanov 
5071ffee690SAmr Mokhtar 	print_mac(port_id, &bbdev_port_eth_addr);
5081ffee690SAmr Mokhtar 
5091ffee690SAmr Mokhtar 	return 0;
5101ffee690SAmr Mokhtar }
5111ffee690SAmr Mokhtar 
5121ffee690SAmr Mokhtar static void
5131ffee690SAmr Mokhtar lcore_conf_init(struct app_config_params *app_params,
5141ffee690SAmr Mokhtar 		struct lcore_conf *lcore_conf,
5151ffee690SAmr Mokhtar 		struct rte_mempool **bbdev_op_pools,
5161ffee690SAmr Mokhtar 		struct rte_mempool *bbdev_mbuf_mempool,
5171ffee690SAmr Mokhtar 		struct rte_ring *enc_to_dec_ring,
5181ffee690SAmr Mokhtar 		struct lcore_statistics *lcore_stats)
5191ffee690SAmr Mokhtar {
5201ffee690SAmr Mokhtar 	unsigned int lcore_id;
5211ffee690SAmr Mokhtar 	struct lcore_conf *lconf;
5221ffee690SAmr Mokhtar 	uint16_t rx_queue_id = 0;
5231ffee690SAmr Mokhtar 	uint16_t tx_queue_id = 0;
5241ffee690SAmr Mokhtar 	uint16_t enc_q_id = 0;
5251ffee690SAmr Mokhtar 	uint16_t dec_q_id = 0;
5261ffee690SAmr Mokhtar 
5271ffee690SAmr Mokhtar 	/* Configure lcores */
5281ffee690SAmr Mokhtar 	for (lcore_id = 0; lcore_id < 8 * sizeof(uint64_t); ++lcore_id) {
5291ffee690SAmr Mokhtar 		lconf = &lcore_conf[lcore_id];
5301ffee690SAmr Mokhtar 		lconf->core_type = 0;
5311ffee690SAmr Mokhtar 
5321ffee690SAmr Mokhtar 		if ((1ULL << lcore_id) & app_params->enc_core_mask) {
5331ffee690SAmr Mokhtar 			lconf->core_type |= (1 << RTE_BBDEV_OP_TURBO_ENC);
5341ffee690SAmr Mokhtar 			lconf->rx_queue_id = rx_queue_id++;
5351ffee690SAmr Mokhtar 			lconf->enc_queue_id =
5361ffee690SAmr Mokhtar 					app_params->enc_queue_ids[enc_q_id++];
5371ffee690SAmr Mokhtar 		}
5381ffee690SAmr Mokhtar 
5391ffee690SAmr Mokhtar 		if ((1ULL << lcore_id) & app_params->dec_core_mask) {
5401ffee690SAmr Mokhtar 			lconf->core_type |= (1 << RTE_BBDEV_OP_TURBO_DEC);
5411ffee690SAmr Mokhtar 			lconf->tx_queue_id = tx_queue_id++;
5421ffee690SAmr Mokhtar 			lconf->dec_queue_id =
5431ffee690SAmr Mokhtar 					app_params->dec_queue_ids[dec_q_id++];
5441ffee690SAmr Mokhtar 		}
5451ffee690SAmr Mokhtar 
5461ffee690SAmr Mokhtar 		lconf->bbdev_enc_op_pool =
5471ffee690SAmr Mokhtar 				bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC];
5481ffee690SAmr Mokhtar 		lconf->bbdev_dec_op_pool =
5491ffee690SAmr Mokhtar 				bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC];
5501ffee690SAmr Mokhtar 		lconf->bbdev_id = app_params->bbdev_id;
5511ffee690SAmr Mokhtar 		lconf->port_id = app_params->port_id;
5521ffee690SAmr Mokhtar 		lconf->enc_out_pool = bbdev_mbuf_mempool;
5531ffee690SAmr Mokhtar 		lconf->enc_to_dec_ring = enc_to_dec_ring;
5541ffee690SAmr Mokhtar 		lconf->lcore_stats = &lcore_stats[lcore_id];
5551ffee690SAmr Mokhtar 	}
5561ffee690SAmr Mokhtar }
5571ffee690SAmr Mokhtar 
5581ffee690SAmr Mokhtar static void
5591ffee690SAmr Mokhtar print_lcore_stats(struct lcore_statistics *lstats, unsigned int lcore_id)
5601ffee690SAmr Mokhtar {
5611ffee690SAmr Mokhtar 	static const char *stats_border = "_______";
5621ffee690SAmr Mokhtar 
5631ffee690SAmr Mokhtar 	printf("\nLcore %d: %s enqueued count:\t\t%u\n",
5641ffee690SAmr Mokhtar 			lcore_id, stats_border, lstats->enqueued);
5651ffee690SAmr Mokhtar 	printf("Lcore %d: %s dequeued count:\t\t%u\n",
5661ffee690SAmr Mokhtar 			lcore_id, stats_border, lstats->dequeued);
5671ffee690SAmr Mokhtar 	printf("Lcore %d: %s RX lost packets count:\t\t%u\n",
5681ffee690SAmr Mokhtar 			lcore_id, stats_border, lstats->rx_lost_packets);
5691ffee690SAmr Mokhtar 	printf("Lcore %d: %s encoder-to-decoder lost count:\t%u\n",
5701ffee690SAmr Mokhtar 			lcore_id, stats_border,
5711ffee690SAmr Mokhtar 			lstats->enc_to_dec_lost_packets);
5721ffee690SAmr Mokhtar 	printf("Lcore %d: %s TX lost packets count:\t\t%u\n",
5731ffee690SAmr Mokhtar 			lcore_id, stats_border, lstats->tx_lost_packets);
5741ffee690SAmr Mokhtar }
5751ffee690SAmr Mokhtar 
5761ffee690SAmr Mokhtar static void
5771ffee690SAmr Mokhtar print_stats(struct stats_lcore_params *stats_lcore)
5781ffee690SAmr Mokhtar {
5791ffee690SAmr Mokhtar 	unsigned int l_id;
5801ffee690SAmr Mokhtar 	unsigned int bbdev_id = stats_lcore->app_params->bbdev_id;
5811ffee690SAmr Mokhtar 	unsigned int port_id = stats_lcore->app_params->port_id;
5821ffee690SAmr Mokhtar 	int len, ret, i;
5831ffee690SAmr Mokhtar 
5841ffee690SAmr Mokhtar 	struct rte_eth_xstat *xstats;
5851ffee690SAmr Mokhtar 	struct rte_eth_xstat_name *xstats_names;
5861ffee690SAmr Mokhtar 	struct rte_bbdev_stats bbstats;
5871ffee690SAmr Mokhtar 	static const char *stats_border = "_______";
5881ffee690SAmr Mokhtar 
5891ffee690SAmr Mokhtar 	const char clr[] = { 27, '[', '2', 'J', '\0' };
5901ffee690SAmr Mokhtar 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
5911ffee690SAmr Mokhtar 
5921ffee690SAmr Mokhtar 	/* Clear screen and move to top left */
5931ffee690SAmr Mokhtar 	printf("%s%s", clr, topLeft);
5941ffee690SAmr Mokhtar 
5951ffee690SAmr Mokhtar 	printf("PORT STATISTICS:\n================\n");
5961ffee690SAmr Mokhtar 	len = rte_eth_xstats_get(port_id, NULL, 0);
5971ffee690SAmr Mokhtar 	if (len < 0)
5981ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
5991ffee690SAmr Mokhtar 				"rte_eth_xstats_get(%u) failed: %d", port_id,
6001ffee690SAmr Mokhtar 				len);
6011ffee690SAmr Mokhtar 
6021ffee690SAmr Mokhtar 	xstats = calloc(len, sizeof(*xstats));
6031ffee690SAmr Mokhtar 	if (xstats == NULL)
6041ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
6051ffee690SAmr Mokhtar 				"Failed to calloc memory for xstats");
6061ffee690SAmr Mokhtar 
6071ffee690SAmr Mokhtar 	ret = rte_eth_xstats_get(port_id, xstats, len);
6082a5aa6e7SAmr Mokhtar 	if (ret < 0 || ret > len) {
6092a5aa6e7SAmr Mokhtar 		free(xstats);
6101ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
6111ffee690SAmr Mokhtar 				"rte_eth_xstats_get(%u) len%i failed: %d",
6121ffee690SAmr Mokhtar 				port_id, len, ret);
6132a5aa6e7SAmr Mokhtar 	}
6141ffee690SAmr Mokhtar 
6151ffee690SAmr Mokhtar 	xstats_names = calloc(len, sizeof(*xstats_names));
6162a5aa6e7SAmr Mokhtar 	if (xstats_names == NULL) {
6172a5aa6e7SAmr Mokhtar 		free(xstats);
6181ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
6191ffee690SAmr Mokhtar 				"Failed to calloc memory for xstats_names");
6202a5aa6e7SAmr Mokhtar 	}
6211ffee690SAmr Mokhtar 
6221ffee690SAmr Mokhtar 	ret = rte_eth_xstats_get_names(port_id, xstats_names, len);
6232a5aa6e7SAmr Mokhtar 	if (ret < 0 || ret > len) {
6242a5aa6e7SAmr Mokhtar 		free(xstats);
6252a5aa6e7SAmr Mokhtar 		free(xstats_names);
6261ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
6271ffee690SAmr Mokhtar 				"rte_eth_xstats_get_names(%u) len%i failed: %d",
6281ffee690SAmr Mokhtar 				port_id, len, ret);
6292a5aa6e7SAmr Mokhtar 	}
6301ffee690SAmr Mokhtar 
6311ffee690SAmr Mokhtar 	for (i = 0; i < len; i++) {
6321ffee690SAmr Mokhtar 		if (xstats[i].value > 0)
6331ffee690SAmr Mokhtar 			printf("Port %u: %s %s:\t\t%"PRIu64"\n",
6341ffee690SAmr Mokhtar 					port_id, stats_border,
6351ffee690SAmr Mokhtar 					xstats_names[i].name,
6361ffee690SAmr Mokhtar 					xstats[i].value);
6371ffee690SAmr Mokhtar 	}
6381ffee690SAmr Mokhtar 
639200d0e7aSAmr Mokhtar 	ret = rte_bbdev_stats_get(bbdev_id, &bbstats);
640200d0e7aSAmr Mokhtar 	if (ret < 0) {
641200d0e7aSAmr Mokhtar 		free(xstats);
642200d0e7aSAmr Mokhtar 		free(xstats_names);
643200d0e7aSAmr Mokhtar 		rte_exit(EXIT_FAILURE,
644200d0e7aSAmr Mokhtar 				"ERROR(%d): Failure to get BBDEV %u statistics\n",
645200d0e7aSAmr Mokhtar 				ret, bbdev_id);
646200d0e7aSAmr Mokhtar 	}
647200d0e7aSAmr Mokhtar 
6481ffee690SAmr Mokhtar 	printf("\nBBDEV STATISTICS:\n=================\n");
6491ffee690SAmr Mokhtar 	printf("BBDEV %u: %s enqueue count:\t\t%"PRIu64"\n",
6501ffee690SAmr Mokhtar 			bbdev_id, stats_border,
6511ffee690SAmr Mokhtar 			bbstats.enqueued_count);
6521ffee690SAmr Mokhtar 	printf("BBDEV %u: %s dequeue count:\t\t%"PRIu64"\n",
6531ffee690SAmr Mokhtar 			bbdev_id, stats_border,
6541ffee690SAmr Mokhtar 			bbstats.dequeued_count);
6551ffee690SAmr Mokhtar 	printf("BBDEV %u: %s enqueue error count:\t\t%"PRIu64"\n",
6561ffee690SAmr Mokhtar 			bbdev_id, stats_border,
6571ffee690SAmr Mokhtar 			bbstats.enqueue_err_count);
6581ffee690SAmr Mokhtar 	printf("BBDEV %u: %s dequeue error count:\t\t%"PRIu64"\n\n",
6591ffee690SAmr Mokhtar 			bbdev_id, stats_border,
6601ffee690SAmr Mokhtar 			bbstats.dequeue_err_count);
6611ffee690SAmr Mokhtar 
6621ffee690SAmr Mokhtar 	printf("LCORE STATISTICS:\n=================\n");
6631ffee690SAmr Mokhtar 	for (l_id = 0; l_id < RTE_MAX_LCORE; ++l_id) {
6641ffee690SAmr Mokhtar 		if (stats_lcore->lconf[l_id].core_type == 0)
6651ffee690SAmr Mokhtar 			continue;
6661ffee690SAmr Mokhtar 		print_lcore_stats(stats_lcore->lconf[l_id].lcore_stats, l_id);
6671ffee690SAmr Mokhtar 	}
6682a5aa6e7SAmr Mokhtar 
6693ee6f706SGeorgiy Levashov 	fflush(stdout);
6703ee6f706SGeorgiy Levashov 
6712a5aa6e7SAmr Mokhtar 	free(xstats);
6722a5aa6e7SAmr Mokhtar 	free(xstats_names);
6731ffee690SAmr Mokhtar }
6741ffee690SAmr Mokhtar 
6751ffee690SAmr Mokhtar static int
6761ffee690SAmr Mokhtar stats_loop(void *arg)
6771ffee690SAmr Mokhtar {
6781ffee690SAmr Mokhtar 	struct stats_lcore_params *stats_lcore = arg;
6791ffee690SAmr Mokhtar 
6801ffee690SAmr Mokhtar 	while (!rte_atomic16_read(&global_exit_flag)) {
6811ffee690SAmr Mokhtar 		print_stats(stats_lcore);
6821ffee690SAmr Mokhtar 		rte_delay_ms(500);
6831ffee690SAmr Mokhtar 	}
6841ffee690SAmr Mokhtar 
6851ffee690SAmr Mokhtar 	return 0;
6861ffee690SAmr Mokhtar }
6871ffee690SAmr Mokhtar 
6881ffee690SAmr Mokhtar static inline void
6891ffee690SAmr Mokhtar run_encoding(struct lcore_conf *lcore_conf)
6901ffee690SAmr Mokhtar {
6911ffee690SAmr Mokhtar 	uint16_t i;
6921ffee690SAmr Mokhtar 	uint16_t port_id, rx_queue_id;
6931ffee690SAmr Mokhtar 	uint16_t bbdev_id, enc_queue_id;
6941ffee690SAmr Mokhtar 	uint16_t nb_rx, nb_enq, nb_deq, nb_sent;
6951ffee690SAmr Mokhtar 	struct rte_mbuf *rx_pkts_burst[MAX_PKT_BURST];
6961ffee690SAmr Mokhtar 	struct rte_mbuf *enc_out_pkts[MAX_PKT_BURST];
6971ffee690SAmr Mokhtar 	struct rte_bbdev_enc_op *bbdev_ops_burst[MAX_PKT_BURST];
6981ffee690SAmr Mokhtar 	struct lcore_statistics *lcore_stats;
6991ffee690SAmr Mokhtar 	struct rte_mempool *bbdev_op_pool, *enc_out_pool;
7001ffee690SAmr Mokhtar 	struct rte_ring *enc_to_dec_ring;
7011ffee690SAmr Mokhtar 	const int in_data_len = (def_op_enc.cb_params.k / 8) - CRC_24B_LEN;
7021ffee690SAmr Mokhtar 
7031ffee690SAmr Mokhtar 	lcore_stats = lcore_conf->lcore_stats;
7041ffee690SAmr Mokhtar 	port_id = lcore_conf->port_id;
7051ffee690SAmr Mokhtar 	rx_queue_id = lcore_conf->rx_queue_id;
7061ffee690SAmr Mokhtar 	bbdev_id = lcore_conf->bbdev_id;
7071ffee690SAmr Mokhtar 	enc_queue_id = lcore_conf->enc_queue_id;
7081ffee690SAmr Mokhtar 	bbdev_op_pool = lcore_conf->bbdev_enc_op_pool;
7091ffee690SAmr Mokhtar 	enc_out_pool = lcore_conf->enc_out_pool;
7101ffee690SAmr Mokhtar 	enc_to_dec_ring = lcore_conf->enc_to_dec_ring;
7111ffee690SAmr Mokhtar 
7121ffee690SAmr Mokhtar 	/* Read packet from RX queues*/
7131ffee690SAmr Mokhtar 	nb_rx = rte_eth_rx_burst(port_id, rx_queue_id, rx_pkts_burst,
7141ffee690SAmr Mokhtar 			MAX_PKT_BURST);
7151ffee690SAmr Mokhtar 	if (!nb_rx)
7161ffee690SAmr Mokhtar 		return;
7171ffee690SAmr Mokhtar 
7181ffee690SAmr Mokhtar 	if (unlikely(rte_mempool_get_bulk(enc_out_pool, (void **)enc_out_pkts,
7191ffee690SAmr Mokhtar 			nb_rx) != 0)) {
7201ffee690SAmr Mokhtar 		pktmbuf_free_bulk(rx_pkts_burst, nb_rx);
7211ffee690SAmr Mokhtar 		lcore_stats->rx_lost_packets += nb_rx;
7221ffee690SAmr Mokhtar 		return;
7231ffee690SAmr Mokhtar 	}
7241ffee690SAmr Mokhtar 
7251ffee690SAmr Mokhtar 	if (unlikely(rte_bbdev_enc_op_alloc_bulk(bbdev_op_pool, bbdev_ops_burst,
7261ffee690SAmr Mokhtar 			nb_rx) != 0)) {
7271ffee690SAmr Mokhtar 		pktmbuf_free_bulk(enc_out_pkts, nb_rx);
7281ffee690SAmr Mokhtar 		pktmbuf_free_bulk(rx_pkts_burst, nb_rx);
7291ffee690SAmr Mokhtar 		lcore_stats->rx_lost_packets += nb_rx;
7301ffee690SAmr Mokhtar 		return;
7311ffee690SAmr Mokhtar 	}
7321ffee690SAmr Mokhtar 
7331ffee690SAmr Mokhtar 	for (i = 0; i < nb_rx; i++) {
7341ffee690SAmr Mokhtar 		char *data;
7351ffee690SAmr Mokhtar 		const uint16_t pkt_data_len =
7361ffee690SAmr Mokhtar 				rte_pktmbuf_data_len(rx_pkts_burst[i]) -
7376d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr);
7381ffee690SAmr Mokhtar 		/* save input mbuf pointer for later comparison */
73961bf499dSThomas Monjalon 		*mbuf_input(enc_out_pkts[i]) = rx_pkts_burst[i];
7401ffee690SAmr Mokhtar 
7411ffee690SAmr Mokhtar 		/* copy ethernet header */
7421ffee690SAmr Mokhtar 		rte_pktmbuf_reset(enc_out_pkts[i]);
7431ffee690SAmr Mokhtar 		data = rte_pktmbuf_append(enc_out_pkts[i],
7446d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr));
7451ffee690SAmr Mokhtar 		if (data == NULL) {
7461ffee690SAmr Mokhtar 			printf(
7471ffee690SAmr Mokhtar 				"Not enough space for ethernet header in encoder output mbuf\n");
7481ffee690SAmr Mokhtar 			continue;
7491ffee690SAmr Mokhtar 		}
7501ffee690SAmr Mokhtar 		add_ether_hdr(rx_pkts_burst[i], enc_out_pkts[i]);
7511ffee690SAmr Mokhtar 
7521ffee690SAmr Mokhtar 		/* set op */
7531ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_enc = def_op_enc;
7541ffee690SAmr Mokhtar 
7551ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_enc.input.data =
7561ffee690SAmr Mokhtar 				rx_pkts_burst[i];
7571ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_enc.input.offset =
7586d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr);
7591ffee690SAmr Mokhtar 		/* Encoder will attach the CRC24B, adjust the length */
7601ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_enc.input.length = in_data_len;
7611ffee690SAmr Mokhtar 
7621ffee690SAmr Mokhtar 		if (in_data_len < pkt_data_len)
7631ffee690SAmr Mokhtar 			rte_pktmbuf_trim(rx_pkts_burst[i], pkt_data_len -
7641ffee690SAmr Mokhtar 					in_data_len);
7651ffee690SAmr Mokhtar 		else if (in_data_len > pkt_data_len) {
7661ffee690SAmr Mokhtar 			data = rte_pktmbuf_append(rx_pkts_burst[i],
7671ffee690SAmr Mokhtar 					in_data_len - pkt_data_len);
7681ffee690SAmr Mokhtar 			if (data == NULL)
7691ffee690SAmr Mokhtar 				printf(
7701ffee690SAmr Mokhtar 					"Not enough storage in mbuf to perform the encoding\n");
7711ffee690SAmr Mokhtar 		}
7721ffee690SAmr Mokhtar 
7731ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_enc.output.data =
7741ffee690SAmr Mokhtar 				enc_out_pkts[i];
7751ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_enc.output.offset =
7766d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr);
7771ffee690SAmr Mokhtar 	}
7781ffee690SAmr Mokhtar 
7791ffee690SAmr Mokhtar 	/* Enqueue packets on BBDevice */
7801ffee690SAmr Mokhtar 	nb_enq = rte_bbdev_enqueue_enc_ops(bbdev_id, enc_queue_id,
7811ffee690SAmr Mokhtar 			bbdev_ops_burst, nb_rx);
7821ffee690SAmr Mokhtar 	if (unlikely(nb_enq < nb_rx)) {
78361bf499dSThomas Monjalon 		pktmbuf_input_free_bulk(&enc_out_pkts[nb_enq],
7841ffee690SAmr Mokhtar 				nb_rx - nb_enq);
7851ffee690SAmr Mokhtar 		rte_bbdev_enc_op_free_bulk(&bbdev_ops_burst[nb_enq],
7861ffee690SAmr Mokhtar 				nb_rx - nb_enq);
7871ffee690SAmr Mokhtar 		lcore_stats->rx_lost_packets += nb_rx - nb_enq;
7881ffee690SAmr Mokhtar 
7891ffee690SAmr Mokhtar 		if (!nb_enq)
7901ffee690SAmr Mokhtar 			return;
7911ffee690SAmr Mokhtar 	}
7921ffee690SAmr Mokhtar 
7931ffee690SAmr Mokhtar 	lcore_stats->enqueued += nb_enq;
7941ffee690SAmr Mokhtar 
7951ffee690SAmr Mokhtar 	/* Dequeue packets from bbdev device*/
7961ffee690SAmr Mokhtar 	nb_deq = 0;
7971ffee690SAmr Mokhtar 	do {
7981ffee690SAmr Mokhtar 		nb_deq += rte_bbdev_dequeue_enc_ops(bbdev_id, enc_queue_id,
7991ffee690SAmr Mokhtar 				&bbdev_ops_burst[nb_deq], nb_enq - nb_deq);
8001ffee690SAmr Mokhtar 	} while (unlikely(nb_deq < nb_enq));
8011ffee690SAmr Mokhtar 
8021ffee690SAmr Mokhtar 	lcore_stats->dequeued += nb_deq;
8031ffee690SAmr Mokhtar 
8041ffee690SAmr Mokhtar 	/* Generate and add AWGN */
8051ffee690SAmr Mokhtar 	add_awgn(enc_out_pkts, nb_deq);
8061ffee690SAmr Mokhtar 
8071ffee690SAmr Mokhtar 	rte_bbdev_enc_op_free_bulk(bbdev_ops_burst, nb_deq);
8081ffee690SAmr Mokhtar 
8091ffee690SAmr Mokhtar 	/* Enqueue packets to encoder-to-decoder ring */
8101ffee690SAmr Mokhtar 	nb_sent = rte_ring_enqueue_burst(enc_to_dec_ring, (void **)enc_out_pkts,
8111ffee690SAmr Mokhtar 			nb_deq, NULL);
8121ffee690SAmr Mokhtar 	if (unlikely(nb_sent < nb_deq)) {
81361bf499dSThomas Monjalon 		pktmbuf_input_free_bulk(&enc_out_pkts[nb_sent],
8141ffee690SAmr Mokhtar 				nb_deq - nb_sent);
8151ffee690SAmr Mokhtar 		lcore_stats->enc_to_dec_lost_packets += nb_deq - nb_sent;
8161ffee690SAmr Mokhtar 	}
8171ffee690SAmr Mokhtar }
8181ffee690SAmr Mokhtar 
8191ffee690SAmr Mokhtar static void
8201ffee690SAmr Mokhtar run_decoding(struct lcore_conf *lcore_conf)
8211ffee690SAmr Mokhtar {
8221ffee690SAmr Mokhtar 	uint16_t i;
8231ffee690SAmr Mokhtar 	uint16_t port_id, tx_queue_id;
8241ffee690SAmr Mokhtar 	uint16_t bbdev_id, bbdev_queue_id;
8251ffee690SAmr Mokhtar 	uint16_t nb_recv, nb_enq, nb_deq, nb_tx;
8261ffee690SAmr Mokhtar 	uint8_t *llr_temp_buf;
8271ffee690SAmr Mokhtar 	struct rte_mbuf *recv_pkts_burst[MAX_PKT_BURST];
8281ffee690SAmr Mokhtar 	struct rte_bbdev_dec_op *bbdev_ops_burst[MAX_PKT_BURST];
8291ffee690SAmr Mokhtar 	struct lcore_statistics *lcore_stats;
8301ffee690SAmr Mokhtar 	struct rte_mempool *bbdev_op_pool;
8311ffee690SAmr Mokhtar 	struct rte_ring *enc_to_dec_ring;
8321ffee690SAmr Mokhtar 
8331ffee690SAmr Mokhtar 	lcore_stats = lcore_conf->lcore_stats;
8341ffee690SAmr Mokhtar 	port_id = lcore_conf->port_id;
8351ffee690SAmr Mokhtar 	tx_queue_id = lcore_conf->tx_queue_id;
8361ffee690SAmr Mokhtar 	bbdev_id = lcore_conf->bbdev_id;
8371ffee690SAmr Mokhtar 	bbdev_queue_id = lcore_conf->dec_queue_id;
8381ffee690SAmr Mokhtar 	bbdev_op_pool = lcore_conf->bbdev_dec_op_pool;
8391ffee690SAmr Mokhtar 	enc_to_dec_ring = lcore_conf->enc_to_dec_ring;
8401ffee690SAmr Mokhtar 	llr_temp_buf = lcore_conf->llr_temp_buf;
8411ffee690SAmr Mokhtar 
8421ffee690SAmr Mokhtar 	/* Dequeue packets from the ring */
8431ffee690SAmr Mokhtar 	nb_recv = rte_ring_dequeue_burst(enc_to_dec_ring,
8441ffee690SAmr Mokhtar 			(void **)recv_pkts_burst, MAX_PKT_BURST, NULL);
8451ffee690SAmr Mokhtar 	if (!nb_recv)
8461ffee690SAmr Mokhtar 		return;
8471ffee690SAmr Mokhtar 
8481ffee690SAmr Mokhtar 	if (unlikely(rte_bbdev_dec_op_alloc_bulk(bbdev_op_pool, bbdev_ops_burst,
8491ffee690SAmr Mokhtar 			nb_recv) != 0)) {
85061bf499dSThomas Monjalon 		pktmbuf_input_free_bulk(recv_pkts_burst, nb_recv);
8511ffee690SAmr Mokhtar 		lcore_stats->rx_lost_packets += nb_recv;
8521ffee690SAmr Mokhtar 		return;
8531ffee690SAmr Mokhtar 	}
8541ffee690SAmr Mokhtar 
8551ffee690SAmr Mokhtar 	transform_enc_out_dec_in(recv_pkts_burst, llr_temp_buf, nb_recv,
8561ffee690SAmr Mokhtar 			def_op_dec.cb_params.k);
8571ffee690SAmr Mokhtar 
8581ffee690SAmr Mokhtar 	for (i = 0; i < nb_recv; i++) {
8591ffee690SAmr Mokhtar 		/* set op */
8601ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_dec = def_op_dec;
8611ffee690SAmr Mokhtar 
8621ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_dec.input.data = recv_pkts_burst[i];
8631ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_dec.input.offset =
8646d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr);
8651ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_dec.input.length =
8661ffee690SAmr Mokhtar 				rte_pktmbuf_data_len(recv_pkts_burst[i])
8676d13ea8eSOlivier Matz 				- sizeof(struct rte_ether_hdr);
8681ffee690SAmr Mokhtar 
8691ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_dec.hard_output.data =
8701ffee690SAmr Mokhtar 				recv_pkts_burst[i];
8711ffee690SAmr Mokhtar 		bbdev_ops_burst[i]->turbo_dec.hard_output.offset =
8726d13ea8eSOlivier Matz 				sizeof(struct rte_ether_hdr);
8731ffee690SAmr Mokhtar 	}
8741ffee690SAmr Mokhtar 
8751ffee690SAmr Mokhtar 	/* Enqueue packets on BBDevice */
8761ffee690SAmr Mokhtar 	nb_enq = rte_bbdev_enqueue_dec_ops(bbdev_id, bbdev_queue_id,
8771ffee690SAmr Mokhtar 			bbdev_ops_burst, nb_recv);
8781ffee690SAmr Mokhtar 	if (unlikely(nb_enq < nb_recv)) {
87961bf499dSThomas Monjalon 		pktmbuf_input_free_bulk(&recv_pkts_burst[nb_enq],
8801ffee690SAmr Mokhtar 				nb_recv - nb_enq);
8811ffee690SAmr Mokhtar 		rte_bbdev_dec_op_free_bulk(&bbdev_ops_burst[nb_enq],
8821ffee690SAmr Mokhtar 				nb_recv - nb_enq);
8831ffee690SAmr Mokhtar 		lcore_stats->rx_lost_packets += nb_recv - nb_enq;
8841ffee690SAmr Mokhtar 
8851ffee690SAmr Mokhtar 		if (!nb_enq)
8861ffee690SAmr Mokhtar 			return;
8871ffee690SAmr Mokhtar 	}
8881ffee690SAmr Mokhtar 
8891ffee690SAmr Mokhtar 	lcore_stats->enqueued += nb_enq;
8901ffee690SAmr Mokhtar 
8911ffee690SAmr Mokhtar 	/* Dequeue packets from BBDevice */
8921ffee690SAmr Mokhtar 	nb_deq = 0;
8931ffee690SAmr Mokhtar 	do {
8941ffee690SAmr Mokhtar 		nb_deq += rte_bbdev_dequeue_dec_ops(bbdev_id, bbdev_queue_id,
8951ffee690SAmr Mokhtar 				&bbdev_ops_burst[nb_deq], nb_enq - nb_deq);
8961ffee690SAmr Mokhtar 	} while (unlikely(nb_deq < nb_enq));
8971ffee690SAmr Mokhtar 
8981ffee690SAmr Mokhtar 	lcore_stats->dequeued += nb_deq;
8991ffee690SAmr Mokhtar 
9001ffee690SAmr Mokhtar 	rte_bbdev_dec_op_free_bulk(bbdev_ops_burst, nb_deq);
9011ffee690SAmr Mokhtar 
9021ffee690SAmr Mokhtar 	verify_data(recv_pkts_burst, nb_deq);
9031ffee690SAmr Mokhtar 
9041ffee690SAmr Mokhtar 	/* Free the RX mbufs after verification */
9051ffee690SAmr Mokhtar 	for (i = 0; i < nb_deq; ++i)
90661bf499dSThomas Monjalon 		rte_pktmbuf_free(*mbuf_input(recv_pkts_burst[i]));
9071ffee690SAmr Mokhtar 
9081ffee690SAmr Mokhtar 	/* Transmit the packets */
9091ffee690SAmr Mokhtar 	nb_tx = rte_eth_tx_burst(port_id, tx_queue_id, recv_pkts_burst, nb_deq);
9101ffee690SAmr Mokhtar 	if (unlikely(nb_tx < nb_deq)) {
91161bf499dSThomas Monjalon 		pktmbuf_input_free_bulk(&recv_pkts_burst[nb_tx],
9121ffee690SAmr Mokhtar 				nb_deq - nb_tx);
9131ffee690SAmr Mokhtar 		lcore_stats->tx_lost_packets += nb_deq - nb_tx;
9141ffee690SAmr Mokhtar 	}
9151ffee690SAmr Mokhtar }
9161ffee690SAmr Mokhtar 
9171ffee690SAmr Mokhtar static int
9181ffee690SAmr Mokhtar processing_loop(void *arg)
9191ffee690SAmr Mokhtar {
9201ffee690SAmr Mokhtar 	struct lcore_conf *lcore_conf = arg;
9211ffee690SAmr Mokhtar 	const bool run_encoder = (lcore_conf->core_type &
9221ffee690SAmr Mokhtar 			(1 << RTE_BBDEV_OP_TURBO_ENC));
9231ffee690SAmr Mokhtar 	const bool run_decoder = (lcore_conf->core_type &
9241ffee690SAmr Mokhtar 			(1 << RTE_BBDEV_OP_TURBO_DEC));
9251ffee690SAmr Mokhtar 
9261ffee690SAmr Mokhtar 	while (!rte_atomic16_read(&global_exit_flag)) {
9271ffee690SAmr Mokhtar 		if (run_encoder)
9281ffee690SAmr Mokhtar 			run_encoding(lcore_conf);
9291ffee690SAmr Mokhtar 		if (run_decoder)
9301ffee690SAmr Mokhtar 			run_decoding(lcore_conf);
9311ffee690SAmr Mokhtar 	}
9321ffee690SAmr Mokhtar 
9331ffee690SAmr Mokhtar 	return 0;
9341ffee690SAmr Mokhtar }
9351ffee690SAmr Mokhtar 
9361ffee690SAmr Mokhtar static int
9371ffee690SAmr Mokhtar prepare_bbdev_device(unsigned int dev_id, struct rte_bbdev_info *info,
9381ffee690SAmr Mokhtar 		struct app_config_params *app_params)
9391ffee690SAmr Mokhtar {
9401ffee690SAmr Mokhtar 	int ret;
9411ffee690SAmr Mokhtar 	unsigned int q_id, dec_q_id, enc_q_id;
9421ffee690SAmr Mokhtar 	struct rte_bbdev_queue_conf qconf = {0};
9431ffee690SAmr Mokhtar 	uint16_t dec_qs_nb = app_params->num_dec_cores;
9441ffee690SAmr Mokhtar 	uint16_t enc_qs_nb = app_params->num_enc_cores;
9451ffee690SAmr Mokhtar 	uint16_t tot_qs = dec_qs_nb + enc_qs_nb;
9461ffee690SAmr Mokhtar 
9471ffee690SAmr Mokhtar 	ret = rte_bbdev_setup_queues(dev_id, tot_qs, info->socket_id);
9481ffee690SAmr Mokhtar 	if (ret < 0)
9491ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
9501ffee690SAmr Mokhtar 				"ERROR(%d): BBDEV %u not configured properly\n",
9511ffee690SAmr Mokhtar 				ret, dev_id);
9521ffee690SAmr Mokhtar 
9531ffee690SAmr Mokhtar 	/* setup device DEC queues */
9541ffee690SAmr Mokhtar 	qconf.socket = info->socket_id;
9551ffee690SAmr Mokhtar 	qconf.queue_size = info->drv.queue_size_lim;
9561ffee690SAmr Mokhtar 	qconf.op_type = RTE_BBDEV_OP_TURBO_DEC;
9571ffee690SAmr Mokhtar 
9581ffee690SAmr Mokhtar 	for (q_id = 0, dec_q_id = 0; q_id < dec_qs_nb; q_id++) {
9591ffee690SAmr Mokhtar 		ret = rte_bbdev_queue_configure(dev_id, q_id, &qconf);
9601ffee690SAmr Mokhtar 		if (ret < 0)
9611ffee690SAmr Mokhtar 			rte_exit(EXIT_FAILURE,
9621ffee690SAmr Mokhtar 					"ERROR(%d): BBDEV %u DEC queue %u not configured properly\n",
9631ffee690SAmr Mokhtar 					ret, dev_id, q_id);
9641ffee690SAmr Mokhtar 		app_params->dec_queue_ids[dec_q_id++] = q_id;
9651ffee690SAmr Mokhtar 	}
9661ffee690SAmr Mokhtar 
9671ffee690SAmr Mokhtar 	/* setup device ENC queues */
9681ffee690SAmr Mokhtar 	qconf.op_type = RTE_BBDEV_OP_TURBO_ENC;
9691ffee690SAmr Mokhtar 
9701ffee690SAmr Mokhtar 	for (q_id = dec_qs_nb, enc_q_id = 0; q_id < tot_qs; q_id++) {
9711ffee690SAmr Mokhtar 		ret = rte_bbdev_queue_configure(dev_id, q_id, &qconf);
9721ffee690SAmr Mokhtar 		if (ret < 0)
9731ffee690SAmr Mokhtar 			rte_exit(EXIT_FAILURE,
9741ffee690SAmr Mokhtar 					"ERROR(%d): BBDEV %u ENC queue %u not configured properly\n",
9751ffee690SAmr Mokhtar 					ret, dev_id, q_id);
9761ffee690SAmr Mokhtar 		app_params->enc_queue_ids[enc_q_id++] = q_id;
9771ffee690SAmr Mokhtar 	}
9781ffee690SAmr Mokhtar 
9791ffee690SAmr Mokhtar 	ret = rte_bbdev_start(dev_id);
9801ffee690SAmr Mokhtar 
9811ffee690SAmr Mokhtar 	if (ret != 0)
9821ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "ERROR(%d): BBDEV %u not started\n",
9831ffee690SAmr Mokhtar 			ret, dev_id);
9841ffee690SAmr Mokhtar 
9851ffee690SAmr Mokhtar 	printf("BBdev %u started\n", dev_id);
9861ffee690SAmr Mokhtar 
9871ffee690SAmr Mokhtar 	return 0;
9881ffee690SAmr Mokhtar }
9891ffee690SAmr Mokhtar 
9901ffee690SAmr Mokhtar static inline bool
9911ffee690SAmr Mokhtar check_matching_capabilities(uint64_t mask, uint64_t required_mask)
9921ffee690SAmr Mokhtar {
9931ffee690SAmr Mokhtar 	return (mask & required_mask) == required_mask;
9941ffee690SAmr Mokhtar }
9951ffee690SAmr Mokhtar 
9961ffee690SAmr Mokhtar static void
9971ffee690SAmr Mokhtar enable_bbdev(struct app_config_params *app_params)
9981ffee690SAmr Mokhtar {
9991ffee690SAmr Mokhtar 	struct rte_bbdev_info dev_info;
10001ffee690SAmr Mokhtar 	const struct rte_bbdev_op_cap *op_cap;
10011ffee690SAmr Mokhtar 	uint16_t bbdev_id = app_params->bbdev_id;
10021ffee690SAmr Mokhtar 	bool encoder_capable = false;
10031ffee690SAmr Mokhtar 	bool decoder_capable = false;
10041ffee690SAmr Mokhtar 
10051ffee690SAmr Mokhtar 	rte_bbdev_info_get(bbdev_id, &dev_info);
10061ffee690SAmr Mokhtar 	op_cap = dev_info.drv.capabilities;
10071ffee690SAmr Mokhtar 
10081ffee690SAmr Mokhtar 	while (op_cap->type != RTE_BBDEV_OP_NONE) {
10091ffee690SAmr Mokhtar 		if (op_cap->type == RTE_BBDEV_OP_TURBO_ENC) {
10101ffee690SAmr Mokhtar 			if (check_matching_capabilities(
10111ffee690SAmr Mokhtar 					op_cap->cap.turbo_enc.capability_flags,
10121ffee690SAmr Mokhtar 					def_op_enc.op_flags))
10131ffee690SAmr Mokhtar 				encoder_capable = true;
10141ffee690SAmr Mokhtar 		}
10151ffee690SAmr Mokhtar 
10161ffee690SAmr Mokhtar 		if (op_cap->type == RTE_BBDEV_OP_TURBO_DEC) {
10171ffee690SAmr Mokhtar 			if (check_matching_capabilities(
10181ffee690SAmr Mokhtar 					op_cap->cap.turbo_dec.capability_flags,
10191ffee690SAmr Mokhtar 					def_op_dec.op_flags))
10201ffee690SAmr Mokhtar 				decoder_capable = true;
10211ffee690SAmr Mokhtar 		}
10221ffee690SAmr Mokhtar 
10231ffee690SAmr Mokhtar 		op_cap++;
10241ffee690SAmr Mokhtar 	}
10251ffee690SAmr Mokhtar 
10261ffee690SAmr Mokhtar 	if (encoder_capable == false)
10271ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
10281ffee690SAmr Mokhtar 			"The specified BBDev %u doesn't have required encoder capabilities!\n",
10291ffee690SAmr Mokhtar 			bbdev_id);
10301ffee690SAmr Mokhtar 	if (decoder_capable == false)
10311ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
10321ffee690SAmr Mokhtar 			"The specified BBDev %u doesn't have required decoder capabilities!\n",
10331ffee690SAmr Mokhtar 			bbdev_id);
10341ffee690SAmr Mokhtar 
10351ffee690SAmr Mokhtar 	prepare_bbdev_device(bbdev_id, &dev_info, app_params);
10361ffee690SAmr Mokhtar }
10371ffee690SAmr Mokhtar 
10381ffee690SAmr Mokhtar int
10391ffee690SAmr Mokhtar main(int argc, char **argv)
10401ffee690SAmr Mokhtar {
10411ffee690SAmr Mokhtar 	int ret;
1042a9dbe180SThomas Monjalon 	unsigned int nb_bbdevs, flags, lcore_id;
10431ffee690SAmr Mokhtar 	void *sigret;
10441ffee690SAmr Mokhtar 	struct app_config_params app_params = def_app_config;
10451ffee690SAmr Mokhtar 	struct rte_mempool *ethdev_mbuf_mempool, *bbdev_mbuf_mempool;
10461ffee690SAmr Mokhtar 	struct rte_mempool *bbdev_op_pools[RTE_BBDEV_OP_TYPE_COUNT];
10471ffee690SAmr Mokhtar 	struct lcore_conf lcore_conf[RTE_MAX_LCORE] = { {0} };
10481ffee690SAmr Mokhtar 	struct lcore_statistics lcore_stats[RTE_MAX_LCORE] = { {0} };
10491ffee690SAmr Mokhtar 	struct stats_lcore_params stats_lcore;
10501ffee690SAmr Mokhtar 	struct rte_ring *enc_to_dec_ring;
10511ffee690SAmr Mokhtar 	bool stats_thread_started = false;
1052cb056611SStephen Hemminger 	unsigned int main_lcore_id = rte_get_main_lcore();
10531ffee690SAmr Mokhtar 
105461bf499dSThomas Monjalon 	static const struct rte_mbuf_dynfield input_dynfield_desc = {
105561bf499dSThomas Monjalon 		.name = "example_bbdev_dynfield_input",
105661bf499dSThomas Monjalon 		.size = sizeof(struct rte_mbuf *),
105761bf499dSThomas Monjalon 		.align = __alignof__(struct rte_mbuf *),
105861bf499dSThomas Monjalon 	};
105961bf499dSThomas Monjalon 
10601ffee690SAmr Mokhtar 	rte_atomic16_init(&global_exit_flag);
10611ffee690SAmr Mokhtar 
10621ffee690SAmr Mokhtar 	sigret = signal(SIGTERM, signal_handler);
10631ffee690SAmr Mokhtar 	if (sigret == SIG_ERR)
10641ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "signal(%d, ...) failed", SIGTERM);
10651ffee690SAmr Mokhtar 
10661ffee690SAmr Mokhtar 	sigret = signal(SIGINT, signal_handler);
10671ffee690SAmr Mokhtar 	if (sigret == SIG_ERR)
10681ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "signal(%d, ...) failed", SIGINT);
10691ffee690SAmr Mokhtar 
10701ffee690SAmr Mokhtar 	ret = rte_eal_init(argc, argv);
10711ffee690SAmr Mokhtar 	if (ret < 0)
10721ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
10731ffee690SAmr Mokhtar 
10741ffee690SAmr Mokhtar 	argc -= ret;
10751ffee690SAmr Mokhtar 	argv += ret;
10761ffee690SAmr Mokhtar 
10771ffee690SAmr Mokhtar 	/* parse application arguments (after the EAL ones) */
10781ffee690SAmr Mokhtar 	ret = bbdev_parse_args(argc, argv, &app_params);
10791ffee690SAmr Mokhtar 	if (ret < 0)
10801ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "Invalid BBDEV arguments\n");
10811ffee690SAmr Mokhtar 
10821ffee690SAmr Mokhtar 	/*create bbdev op pools*/
10831ffee690SAmr Mokhtar 	bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC] =
10841ffee690SAmr Mokhtar 			rte_bbdev_op_pool_create("bbdev_op_pool_dec",
10851ffee690SAmr Mokhtar 			RTE_BBDEV_OP_TURBO_DEC, NB_MBUF, 128, rte_socket_id());
10861ffee690SAmr Mokhtar 	bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC] =
10871ffee690SAmr Mokhtar 			rte_bbdev_op_pool_create("bbdev_op_pool_enc",
10881ffee690SAmr Mokhtar 			RTE_BBDEV_OP_TURBO_ENC, NB_MBUF, 128, rte_socket_id());
10891ffee690SAmr Mokhtar 
10901ffee690SAmr Mokhtar 	if ((bbdev_op_pools[RTE_BBDEV_OP_TURBO_DEC] == NULL) ||
10911ffee690SAmr Mokhtar 			(bbdev_op_pools[RTE_BBDEV_OP_TURBO_ENC] == NULL))
10921ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "Cannot create bbdev op pools\n");
10931ffee690SAmr Mokhtar 
10941ffee690SAmr Mokhtar 	/* Create encoder to decoder ring */
10951ffee690SAmr Mokhtar 	flags = (app_params.num_enc_cores == 1) ? RING_F_SP_ENQ : 0;
10961ffee690SAmr Mokhtar 	if (app_params.num_dec_cores == 1)
10971ffee690SAmr Mokhtar 		flags |= RING_F_SC_DEQ;
10981ffee690SAmr Mokhtar 
10991ffee690SAmr Mokhtar 	enc_to_dec_ring = rte_ring_create("enc_to_dec_ring",
11001ffee690SAmr Mokhtar 		rte_align32pow2(NB_MBUF), rte_socket_id(), flags);
11011ffee690SAmr Mokhtar 
11021ffee690SAmr Mokhtar 	/* Get the number of available bbdev devices */
11031ffee690SAmr Mokhtar 	nb_bbdevs = rte_bbdev_count();
11041ffee690SAmr Mokhtar 	if (nb_bbdevs <= app_params.bbdev_id)
11051ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
11061ffee690SAmr Mokhtar 				"%u BBDevs detected, cannot use BBDev with ID %u!\n",
11071ffee690SAmr Mokhtar 				nb_bbdevs, app_params.bbdev_id);
11081ffee690SAmr Mokhtar 	printf("Number of bbdevs detected: %d\n", nb_bbdevs);
11091ffee690SAmr Mokhtar 
1110a9dbe180SThomas Monjalon 	if (!rte_eth_dev_is_valid_port(app_params.port_id))
11111ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
1112a9dbe180SThomas Monjalon 				"cannot use port with ID %u!\n",
1113a9dbe180SThomas Monjalon 				app_params.port_id);
11141ffee690SAmr Mokhtar 
11151ffee690SAmr Mokhtar 	/* create the mbuf mempool for ethdev pkts */
11161ffee690SAmr Mokhtar 	ethdev_mbuf_mempool = rte_pktmbuf_pool_create("ethdev_mbuf_pool",
11171ffee690SAmr Mokhtar 			NB_MBUF, MEMPOOL_CACHE_SIZE, 0,
11181ffee690SAmr Mokhtar 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
11191ffee690SAmr Mokhtar 	if (ethdev_mbuf_mempool == NULL)
11201ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "Cannot create ethdev mbuf mempool\n");
11211ffee690SAmr Mokhtar 
11221ffee690SAmr Mokhtar 	/* create the mbuf mempool for encoder output */
11231ffee690SAmr Mokhtar 	bbdev_mbuf_mempool = rte_pktmbuf_pool_create("bbdev_mbuf_pool",
11241ffee690SAmr Mokhtar 			NB_MBUF, MEMPOOL_CACHE_SIZE, 0,
11251ffee690SAmr Mokhtar 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
11261ffee690SAmr Mokhtar 	if (bbdev_mbuf_mempool == NULL)
11271ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "Cannot create ethdev mbuf mempool\n");
11281ffee690SAmr Mokhtar 
112961bf499dSThomas Monjalon 	/* register mbuf field to store input pointer */
113061bf499dSThomas Monjalon 	input_dynfield_offset =
113161bf499dSThomas Monjalon 		rte_mbuf_dynfield_register(&input_dynfield_desc);
113261bf499dSThomas Monjalon 	if (input_dynfield_offset < 0)
113361bf499dSThomas Monjalon 		rte_exit(EXIT_FAILURE, "Cannot register mbuf field\n");
113461bf499dSThomas Monjalon 
11351ffee690SAmr Mokhtar 	/* initialize ports */
11361ffee690SAmr Mokhtar 	ret = initialize_ports(&app_params, ethdev_mbuf_mempool);
11371ffee690SAmr Mokhtar 
11381ffee690SAmr Mokhtar 	/* Check if all requested lcores are available */
11391ffee690SAmr Mokhtar 	for (lcore_id = 0; lcore_id < 8 * sizeof(uint64_t); ++lcore_id)
11401ffee690SAmr Mokhtar 		if (((1ULL << lcore_id) & app_params.enc_core_mask) ||
11411ffee690SAmr Mokhtar 				((1ULL << lcore_id) & app_params.dec_core_mask))
11421ffee690SAmr Mokhtar 			if (!rte_lcore_is_enabled(lcore_id))
11431ffee690SAmr Mokhtar 				rte_exit(EXIT_FAILURE,
11441ffee690SAmr Mokhtar 						"Requested lcore_id %u is not enabled!\n",
11451ffee690SAmr Mokhtar 						lcore_id);
11461ffee690SAmr Mokhtar 
11471ffee690SAmr Mokhtar 	/* Start ethernet port */
11481ffee690SAmr Mokhtar 	ret = rte_eth_dev_start(app_params.port_id);
11491ffee690SAmr Mokhtar 	if (ret < 0)
11501ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
11511ffee690SAmr Mokhtar 				ret, app_params.port_id);
11521ffee690SAmr Mokhtar 
11531ffee690SAmr Mokhtar 	ret = check_port_link_status(app_params.port_id);
11541ffee690SAmr Mokhtar 	if (ret < 0)
11551ffee690SAmr Mokhtar 		exit(EXIT_FAILURE);
11561ffee690SAmr Mokhtar 
11571ffee690SAmr Mokhtar 	/* start BBDevice and save BBDev queue IDs */
11581ffee690SAmr Mokhtar 	enable_bbdev(&app_params);
11591ffee690SAmr Mokhtar 
11601ffee690SAmr Mokhtar 	/* Initialize the port/queue configuration of each logical core */
11611ffee690SAmr Mokhtar 	lcore_conf_init(&app_params, lcore_conf, bbdev_op_pools,
11621ffee690SAmr Mokhtar 			bbdev_mbuf_mempool, enc_to_dec_ring, lcore_stats);
11631ffee690SAmr Mokhtar 
11641ffee690SAmr Mokhtar 	stats_lcore.app_params = &app_params;
11651ffee690SAmr Mokhtar 	stats_lcore.lconf = lcore_conf;
11661ffee690SAmr Mokhtar 
1167cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
11681ffee690SAmr Mokhtar 		if (lcore_conf[lcore_id].core_type != 0)
1169cb056611SStephen Hemminger 			/* launch per-lcore processing loop on worker lcores */
11701ffee690SAmr Mokhtar 			rte_eal_remote_launch(processing_loop,
11711ffee690SAmr Mokhtar 					&lcore_conf[lcore_id], lcore_id);
11721ffee690SAmr Mokhtar 		else if (!stats_thread_started) {
11731ffee690SAmr Mokhtar 			/* launch statistics printing loop */
11741ffee690SAmr Mokhtar 			rte_eal_remote_launch(stats_loop, &stats_lcore,
11751ffee690SAmr Mokhtar 					lcore_id);
11761ffee690SAmr Mokhtar 			stats_thread_started = true;
11771ffee690SAmr Mokhtar 		}
11781ffee690SAmr Mokhtar 	}
11791ffee690SAmr Mokhtar 
11801ffee690SAmr Mokhtar 	if (!stats_thread_started &&
1181cb056611SStephen Hemminger 			lcore_conf[main_lcore_id].core_type != 0)
11821ffee690SAmr Mokhtar 		rte_exit(EXIT_FAILURE,
11831ffee690SAmr Mokhtar 				"Not enough lcores to run the statistics printing loop!");
1184cb056611SStephen Hemminger 	else if (lcore_conf[main_lcore_id].core_type != 0)
1185cb056611SStephen Hemminger 		processing_loop(&lcore_conf[main_lcore_id]);
11861ffee690SAmr Mokhtar 	else if (!stats_thread_started)
11871ffee690SAmr Mokhtar 		stats_loop(&stats_lcore);
11881ffee690SAmr Mokhtar 
1189cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
11901ffee690SAmr Mokhtar 		ret |= rte_eal_wait_lcore(lcore_id);
11911ffee690SAmr Mokhtar 	}
11921ffee690SAmr Mokhtar 
119310aa3757SChengchang Tang 	/* clean up the EAL */
119410aa3757SChengchang Tang 	rte_eal_cleanup();
119510aa3757SChengchang Tang 
11961ffee690SAmr Mokhtar 	return ret;
11971ffee690SAmr Mokhtar }
1198