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