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