xref: /dpdk/examples/l2fwd-crypto/main.c (revision db4e81351fb85ff623bd0438d1b5a8fb55fe9fee)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2015-2016 Intel Corporation
3387259bdSDeclan Doherty  */
4387259bdSDeclan Doherty 
5387259bdSDeclan Doherty #include <time.h>
6387259bdSDeclan Doherty #include <stdio.h>
7387259bdSDeclan Doherty #include <stdlib.h>
8387259bdSDeclan Doherty #include <string.h>
9387259bdSDeclan Doherty #include <stdint.h>
10387259bdSDeclan Doherty #include <inttypes.h>
11387259bdSDeclan Doherty #include <sys/types.h>
12387259bdSDeclan Doherty #include <sys/queue.h>
13387259bdSDeclan Doherty #include <netinet/in.h>
14387259bdSDeclan Doherty #include <setjmp.h>
15387259bdSDeclan Doherty #include <stdarg.h>
16387259bdSDeclan Doherty #include <ctype.h>
17387259bdSDeclan Doherty #include <errno.h>
18387259bdSDeclan Doherty #include <getopt.h>
196dad6e69SPiotr Azarewicz #include <fcntl.h>
206dad6e69SPiotr Azarewicz #include <unistd.h>
21387259bdSDeclan Doherty 
226723c0fcSBruce Richardson #include <rte_string_fns.h>
23387259bdSDeclan Doherty #include <rte_atomic.h>
24387259bdSDeclan Doherty #include <rte_branch_prediction.h>
25387259bdSDeclan Doherty #include <rte_common.h>
26387259bdSDeclan Doherty #include <rte_cryptodev.h>
27387259bdSDeclan Doherty #include <rte_cycles.h>
28387259bdSDeclan Doherty #include <rte_debug.h>
29387259bdSDeclan Doherty #include <rte_eal.h>
30387259bdSDeclan Doherty #include <rte_ether.h>
31387259bdSDeclan Doherty #include <rte_ethdev.h>
32387259bdSDeclan Doherty #include <rte_interrupts.h>
33387259bdSDeclan Doherty #include <rte_ip.h>
34387259bdSDeclan Doherty #include <rte_launch.h>
35387259bdSDeclan Doherty #include <rte_lcore.h>
36387259bdSDeclan Doherty #include <rte_log.h>
37387259bdSDeclan Doherty #include <rte_malloc.h>
38387259bdSDeclan Doherty #include <rte_mbuf.h>
39387259bdSDeclan Doherty #include <rte_memcpy.h>
40387259bdSDeclan Doherty #include <rte_memory.h>
41387259bdSDeclan Doherty #include <rte_mempool.h>
42387259bdSDeclan Doherty #include <rte_per_lcore.h>
43387259bdSDeclan Doherty #include <rte_prefetch.h>
44387259bdSDeclan Doherty #include <rte_random.h>
4541e97c2eSPablo de Lara #include <rte_hexdump.h>
46e3bcb99aSPablo de Lara #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
47e3bcb99aSPablo de Lara #include <rte_cryptodev_scheduler.h>
48e3bcb99aSPablo de Lara #endif
49387259bdSDeclan Doherty 
5027cf2d1bSPablo de Lara enum cdev_type {
5127cf2d1bSPablo de Lara 	CDEV_TYPE_ANY,
5227cf2d1bSPablo de Lara 	CDEV_TYPE_HW,
5327cf2d1bSPablo de Lara 	CDEV_TYPE_SW
5427cf2d1bSPablo de Lara };
5527cf2d1bSPablo de Lara 
56387259bdSDeclan Doherty #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
57387259bdSDeclan Doherty 
58387259bdSDeclan Doherty #define NB_MBUF   8192
59387259bdSDeclan Doherty 
6027cf2d1bSPablo de Lara #define MAX_STR_LEN 32
611df9c010SPablo de Lara #define MAX_KEY_SIZE 128
62ff5d5b01SPablo de Lara #define MAX_IV_SIZE 16
63ff5d5b01SPablo de Lara #define MAX_AAD_SIZE 65535
64387259bdSDeclan Doherty #define MAX_PKT_BURST 32
65387259bdSDeclan Doherty #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
662c59bd32SSlawomir Mrozowicz #define SESSION_POOL_CACHE_SIZE 0
67387259bdSDeclan Doherty 
68e636243eSPablo de Lara #define MAXIMUM_IV_LENGTH	16
69e636243eSPablo de Lara #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
70e636243eSPablo de Lara 				sizeof(struct rte_crypto_sym_op))
71e636243eSPablo de Lara 
72387259bdSDeclan Doherty /*
73387259bdSDeclan Doherty  * Configurable number of RX/TX ring descriptors
74387259bdSDeclan Doherty  */
75867a6c66SKevin Laatz #define RTE_TEST_RX_DESC_DEFAULT 1024
76867a6c66SKevin Laatz #define RTE_TEST_TX_DESC_DEFAULT 1024
773c96262cSPablo de Lara 
78387259bdSDeclan Doherty static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
79387259bdSDeclan Doherty static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
80387259bdSDeclan Doherty 
81387259bdSDeclan Doherty /* ethernet addresses of ports */
826d13ea8eSOlivier Matz static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
83387259bdSDeclan Doherty 
84387259bdSDeclan Doherty /* mask of enabled ports */
85387259bdSDeclan Doherty static uint64_t l2fwd_enabled_port_mask;
86387259bdSDeclan Doherty static uint64_t l2fwd_enabled_crypto_mask;
87387259bdSDeclan Doherty 
88387259bdSDeclan Doherty /* list of enabled ports */
89e2cdfbd0SZhiyong Yang static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
90387259bdSDeclan Doherty 
91387259bdSDeclan Doherty 
92387259bdSDeclan Doherty struct pkt_buffer {
93387259bdSDeclan Doherty 	unsigned len;
94387259bdSDeclan Doherty 	struct rte_mbuf *buffer[MAX_PKT_BURST];
95387259bdSDeclan Doherty };
96387259bdSDeclan Doherty 
97c0f87eb5SDeclan Doherty struct op_buffer {
98c0f87eb5SDeclan Doherty 	unsigned len;
99c0f87eb5SDeclan Doherty 	struct rte_crypto_op *buffer[MAX_PKT_BURST];
100c0f87eb5SDeclan Doherty };
101c0f87eb5SDeclan Doherty 
102387259bdSDeclan Doherty #define MAX_RX_QUEUE_PER_LCORE 16
103387259bdSDeclan Doherty #define MAX_TX_QUEUE_PER_PORT 16
104387259bdSDeclan Doherty 
105387259bdSDeclan Doherty enum l2fwd_crypto_xform_chain {
106387259bdSDeclan Doherty 	L2FWD_CRYPTO_CIPHER_HASH,
1071a75e9f3SPablo de Lara 	L2FWD_CRYPTO_HASH_CIPHER,
1081a75e9f3SPablo de Lara 	L2FWD_CRYPTO_CIPHER_ONLY,
1092661f4fbSPablo de Lara 	L2FWD_CRYPTO_HASH_ONLY,
1102661f4fbSPablo de Lara 	L2FWD_CRYPTO_AEAD
111387259bdSDeclan Doherty };
112387259bdSDeclan Doherty 
113a7f4562bSFiona Trahe struct l2fwd_key {
114a7f4562bSFiona Trahe 	uint8_t *data;
115a7f4562bSFiona Trahe 	uint32_t length;
116c4509373SSantosh Shukla 	rte_iova_t phys_addr;
117a7f4562bSFiona Trahe };
118a7f4562bSFiona Trahe 
1190fbd75a9SPablo de Lara struct l2fwd_iv {
1200fbd75a9SPablo de Lara 	uint8_t *data;
1210fbd75a9SPablo de Lara 	uint16_t length;
1220fbd75a9SPablo de Lara };
1230fbd75a9SPablo de Lara 
124387259bdSDeclan Doherty /** l2fwd crypto application command line options */
125387259bdSDeclan Doherty struct l2fwd_crypto_options {
126387259bdSDeclan Doherty 	unsigned portmask;
127387259bdSDeclan Doherty 	unsigned nb_ports_per_lcore;
128387259bdSDeclan Doherty 	unsigned refresh_period;
129387259bdSDeclan Doherty 	unsigned single_lcore:1;
130387259bdSDeclan Doherty 
13127cf2d1bSPablo de Lara 	enum cdev_type type;
132387259bdSDeclan Doherty 	unsigned sessionless:1;
133387259bdSDeclan Doherty 
134387259bdSDeclan Doherty 	enum l2fwd_crypto_xform_chain xform_chain;
135387259bdSDeclan Doherty 
1361bd407faSFiona Trahe 	struct rte_crypto_sym_xform cipher_xform;
1371df9c010SPablo de Lara 	unsigned ckey_param;
138a061e50aSPablo de Lara 	int ckey_random_size;
139186b14d6SFan Zhang 	uint8_t cipher_key[MAX_KEY_SIZE];
140387259bdSDeclan Doherty 
141acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
142acf86169SPablo de Lara 	unsigned int cipher_iv_param;
143acf86169SPablo de Lara 	int cipher_iv_random_size;
144387259bdSDeclan Doherty 
1451bd407faSFiona Trahe 	struct rte_crypto_sym_xform auth_xform;
1461df9c010SPablo de Lara 	uint8_t akey_param;
147a061e50aSPablo de Lara 	int akey_random_size;
148186b14d6SFan Zhang 	uint8_t auth_key[MAX_KEY_SIZE];
149617a7949SPablo de Lara 
150acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
151acf86169SPablo de Lara 	unsigned int auth_iv_param;
152acf86169SPablo de Lara 	int auth_iv_random_size;
153acf86169SPablo de Lara 
1542661f4fbSPablo de Lara 	struct rte_crypto_sym_xform aead_xform;
1552661f4fbSPablo de Lara 	unsigned int aead_key_param;
1562661f4fbSPablo de Lara 	int aead_key_random_size;
157186b14d6SFan Zhang 	uint8_t aead_key[MAX_KEY_SIZE];
1582661f4fbSPablo de Lara 
1592661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
1602661f4fbSPablo de Lara 	unsigned int aead_iv_param;
1612661f4fbSPablo de Lara 	int aead_iv_random_size;
1622661f4fbSPablo de Lara 
163617a7949SPablo de Lara 	struct l2fwd_key aad;
164617a7949SPablo de Lara 	unsigned aad_param;
165a061e50aSPablo de Lara 	int aad_random_size;
166a061e50aSPablo de Lara 
167a061e50aSPablo de Lara 	int digest_size;
16827cf2d1bSPablo de Lara 
16927cf2d1bSPablo de Lara 	uint16_t block_size;
17027cf2d1bSPablo de Lara 	char string_type[MAX_STR_LEN];
171d2797f51SFan Zhang 
172d2797f51SFan Zhang 	uint64_t cryptodev_mask;
173acdfecbaSKuba Kozak 
174acdfecbaSKuba Kozak 	unsigned int mac_updating;
175387259bdSDeclan Doherty };
176387259bdSDeclan Doherty 
177387259bdSDeclan Doherty /** l2fwd crypto lcore params */
178387259bdSDeclan Doherty struct l2fwd_crypto_params {
179387259bdSDeclan Doherty 	uint8_t dev_id;
180387259bdSDeclan Doherty 	uint8_t qp_id;
181387259bdSDeclan Doherty 
182387259bdSDeclan Doherty 	unsigned digest_length;
183387259bdSDeclan Doherty 	unsigned block_size;
18427cf2d1bSPablo de Lara 
185acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
186acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
1872661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
188617a7949SPablo de Lara 	struct l2fwd_key aad;
1891bd407faSFiona Trahe 	struct rte_cryptodev_sym_session *session;
1901a75e9f3SPablo de Lara 
1911a75e9f3SPablo de Lara 	uint8_t do_cipher;
1921a75e9f3SPablo de Lara 	uint8_t do_hash;
1932661f4fbSPablo de Lara 	uint8_t do_aead;
19427cf2d1bSPablo de Lara 	uint8_t hash_verify;
195d29ea843SPablo de Lara 
196d29ea843SPablo de Lara 	enum rte_crypto_cipher_algorithm cipher_algo;
197d29ea843SPablo de Lara 	enum rte_crypto_auth_algorithm auth_algo;
1982661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm aead_algo;
199387259bdSDeclan Doherty };
200387259bdSDeclan Doherty 
201387259bdSDeclan Doherty /** lcore configuration */
202387259bdSDeclan Doherty struct lcore_queue_conf {
203387259bdSDeclan Doherty 	unsigned nb_rx_ports;
204e2cdfbd0SZhiyong Yang 	uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
205387259bdSDeclan Doherty 
206387259bdSDeclan Doherty 	unsigned nb_crypto_devs;
207387259bdSDeclan Doherty 	unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
208387259bdSDeclan Doherty 
209ad476dd3SPablo de Lara 	struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
210c0f87eb5SDeclan Doherty 	struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
211387259bdSDeclan Doherty } __rte_cache_aligned;
212387259bdSDeclan Doherty 
213387259bdSDeclan Doherty struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
214387259bdSDeclan Doherty 
215f2b713e3SShahaf Shuler static struct rte_eth_conf port_conf = {
216387259bdSDeclan Doherty 	.rxmode = {
2171df9c010SPablo de Lara 		.mq_mode = ETH_MQ_RX_NONE,
21835b2d13fSOlivier Matz 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
219387259bdSDeclan Doherty 		.split_hdr_size = 0,
220387259bdSDeclan Doherty 	},
221387259bdSDeclan Doherty 	.txmode = {
222387259bdSDeclan Doherty 		.mq_mode = ETH_MQ_TX_NONE,
223387259bdSDeclan Doherty 	},
224387259bdSDeclan Doherty };
225387259bdSDeclan Doherty 
226387259bdSDeclan Doherty struct rte_mempool *l2fwd_pktmbuf_pool;
227c0f87eb5SDeclan Doherty struct rte_mempool *l2fwd_crypto_op_pool;
228261bbff7SFan Zhang static struct {
229261bbff7SFan Zhang 	struct rte_mempool *sess_mp;
230261bbff7SFan Zhang 	struct rte_mempool *priv_mp;
231261bbff7SFan Zhang } session_pool_socket[RTE_MAX_NUMA_NODES];
232387259bdSDeclan Doherty 
233387259bdSDeclan Doherty /* Per-port statistics struct */
234387259bdSDeclan Doherty struct l2fwd_port_statistics {
235387259bdSDeclan Doherty 	uint64_t tx;
236387259bdSDeclan Doherty 	uint64_t rx;
237387259bdSDeclan Doherty 
238387259bdSDeclan Doherty 	uint64_t crypto_enqueued;
239387259bdSDeclan Doherty 	uint64_t crypto_dequeued;
240387259bdSDeclan Doherty 
241387259bdSDeclan Doherty 	uint64_t dropped;
242387259bdSDeclan Doherty } __rte_cache_aligned;
243387259bdSDeclan Doherty 
244387259bdSDeclan Doherty struct l2fwd_crypto_statistics {
245387259bdSDeclan Doherty 	uint64_t enqueued;
246387259bdSDeclan Doherty 	uint64_t dequeued;
247387259bdSDeclan Doherty 
248387259bdSDeclan Doherty 	uint64_t errors;
249387259bdSDeclan Doherty } __rte_cache_aligned;
250387259bdSDeclan Doherty 
251387259bdSDeclan Doherty struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
252572f0779SSlawomir Mrozowicz struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
253387259bdSDeclan Doherty 
254387259bdSDeclan Doherty /* A tsc-based timer responsible for triggering statistics printout */
255387259bdSDeclan Doherty #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
2563c96262cSPablo de Lara #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
257387259bdSDeclan Doherty 
258387259bdSDeclan Doherty /* default period is 10 seconds */
259387259bdSDeclan Doherty static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
260387259bdSDeclan Doherty 
261387259bdSDeclan Doherty /* Print out statistics on packets dropped */
262387259bdSDeclan Doherty static void
263387259bdSDeclan Doherty print_stats(void)
264387259bdSDeclan Doherty {
26528523d9aSPablo de Lara 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
26628523d9aSPablo de Lara 	uint64_t total_packets_enqueued, total_packets_dequeued,
26728523d9aSPablo de Lara 		total_packets_errors;
268e2cdfbd0SZhiyong Yang 	uint16_t portid;
269387259bdSDeclan Doherty 	uint64_t cdevid;
270387259bdSDeclan Doherty 
27128523d9aSPablo de Lara 	total_packets_dropped = 0;
27228523d9aSPablo de Lara 	total_packets_tx = 0;
27328523d9aSPablo de Lara 	total_packets_rx = 0;
27428523d9aSPablo de Lara 	total_packets_enqueued = 0;
27528523d9aSPablo de Lara 	total_packets_dequeued = 0;
27628523d9aSPablo de Lara 	total_packets_errors = 0;
277387259bdSDeclan Doherty 
278387259bdSDeclan Doherty 	const char clr[] = { 27, '[', '2', 'J', '\0' };
279387259bdSDeclan Doherty 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
280387259bdSDeclan Doherty 
281387259bdSDeclan Doherty 		/* Clear screen and move to top left */
282387259bdSDeclan Doherty 	printf("%s%s", clr, topLeft);
283387259bdSDeclan Doherty 
284387259bdSDeclan Doherty 	printf("\nPort statistics ====================================");
285387259bdSDeclan Doherty 
286387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
287387259bdSDeclan Doherty 		/* skip disabled ports */
288387259bdSDeclan Doherty 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
289387259bdSDeclan Doherty 			continue;
290387259bdSDeclan Doherty 		printf("\nStatistics for port %u ------------------------------"
291387259bdSDeclan Doherty 			   "\nPackets sent: %32"PRIu64
292387259bdSDeclan Doherty 			   "\nPackets received: %28"PRIu64
293387259bdSDeclan Doherty 			   "\nPackets dropped: %29"PRIu64,
294387259bdSDeclan Doherty 			   portid,
295387259bdSDeclan Doherty 			   port_statistics[portid].tx,
296387259bdSDeclan Doherty 			   port_statistics[portid].rx,
297387259bdSDeclan Doherty 			   port_statistics[portid].dropped);
298387259bdSDeclan Doherty 
299387259bdSDeclan Doherty 		total_packets_dropped += port_statistics[portid].dropped;
300387259bdSDeclan Doherty 		total_packets_tx += port_statistics[portid].tx;
301387259bdSDeclan Doherty 		total_packets_rx += port_statistics[portid].rx;
302387259bdSDeclan Doherty 	}
303387259bdSDeclan Doherty 	printf("\nCrypto statistics ==================================");
304387259bdSDeclan Doherty 
305387259bdSDeclan Doherty 	for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
306387259bdSDeclan Doherty 		/* skip disabled ports */
307ad476dd3SPablo de Lara 		if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
308387259bdSDeclan Doherty 			continue;
309387259bdSDeclan Doherty 		printf("\nStatistics for cryptodev %"PRIu64
310387259bdSDeclan Doherty 				" -------------------------"
311387259bdSDeclan Doherty 			   "\nPackets enqueued: %28"PRIu64
312387259bdSDeclan Doherty 			   "\nPackets dequeued: %28"PRIu64
313387259bdSDeclan Doherty 			   "\nPackets errors: %30"PRIu64,
314387259bdSDeclan Doherty 			   cdevid,
315387259bdSDeclan Doherty 			   crypto_statistics[cdevid].enqueued,
316387259bdSDeclan Doherty 			   crypto_statistics[cdevid].dequeued,
317387259bdSDeclan Doherty 			   crypto_statistics[cdevid].errors);
318387259bdSDeclan Doherty 
319387259bdSDeclan Doherty 		total_packets_enqueued += crypto_statistics[cdevid].enqueued;
320387259bdSDeclan Doherty 		total_packets_dequeued += crypto_statistics[cdevid].dequeued;
321387259bdSDeclan Doherty 		total_packets_errors += crypto_statistics[cdevid].errors;
322387259bdSDeclan Doherty 	}
323387259bdSDeclan Doherty 	printf("\nAggregate statistics ==============================="
324387259bdSDeclan Doherty 		   "\nTotal packets received: %22"PRIu64
325387259bdSDeclan Doherty 		   "\nTotal packets enqueued: %22"PRIu64
326387259bdSDeclan Doherty 		   "\nTotal packets dequeued: %22"PRIu64
327387259bdSDeclan Doherty 		   "\nTotal packets sent: %26"PRIu64
328387259bdSDeclan Doherty 		   "\nTotal packets dropped: %23"PRIu64
329387259bdSDeclan Doherty 		   "\nTotal packets crypto errors: %17"PRIu64,
330387259bdSDeclan Doherty 		   total_packets_rx,
331387259bdSDeclan Doherty 		   total_packets_enqueued,
332387259bdSDeclan Doherty 		   total_packets_dequeued,
333387259bdSDeclan Doherty 		   total_packets_tx,
334387259bdSDeclan Doherty 		   total_packets_dropped,
335387259bdSDeclan Doherty 		   total_packets_errors);
336387259bdSDeclan Doherty 	printf("\n====================================================\n");
3373ee6f706SGeorgiy Levashov 
3383ee6f706SGeorgiy Levashov 	fflush(stdout);
339387259bdSDeclan Doherty }
340387259bdSDeclan Doherty 
341387259bdSDeclan Doherty static int
342387259bdSDeclan Doherty l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
343387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
344387259bdSDeclan Doherty {
345c0f87eb5SDeclan Doherty 	struct rte_crypto_op **op_buffer;
346387259bdSDeclan Doherty 	unsigned ret;
347387259bdSDeclan Doherty 
348c0f87eb5SDeclan Doherty 	op_buffer = (struct rte_crypto_op **)
349c0f87eb5SDeclan Doherty 			qconf->op_buf[cparams->dev_id].buffer;
350387259bdSDeclan Doherty 
351c0f87eb5SDeclan Doherty 	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
352c0f87eb5SDeclan Doherty 			cparams->qp_id,	op_buffer, (uint16_t) n);
353c0f87eb5SDeclan Doherty 
354387259bdSDeclan Doherty 	crypto_statistics[cparams->dev_id].enqueued += ret;
355387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
356387259bdSDeclan Doherty 		crypto_statistics[cparams->dev_id].errors += (n - ret);
357387259bdSDeclan Doherty 		do {
358c0f87eb5SDeclan Doherty 			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
359c0f87eb5SDeclan Doherty 			rte_crypto_op_free(op_buffer[ret]);
360387259bdSDeclan Doherty 		} while (++ret < n);
361387259bdSDeclan Doherty 	}
362387259bdSDeclan Doherty 
363387259bdSDeclan Doherty 	return 0;
364387259bdSDeclan Doherty }
365387259bdSDeclan Doherty 
366387259bdSDeclan Doherty static int
367c0f87eb5SDeclan Doherty l2fwd_crypto_enqueue(struct rte_crypto_op *op,
368c0f87eb5SDeclan Doherty 		struct l2fwd_crypto_params *cparams)
369387259bdSDeclan Doherty {
370387259bdSDeclan Doherty 	unsigned lcore_id, len;
371387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
372387259bdSDeclan Doherty 
373387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
374387259bdSDeclan Doherty 
375387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
376c0f87eb5SDeclan Doherty 	len = qconf->op_buf[cparams->dev_id].len;
377c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].buffer[len] = op;
378387259bdSDeclan Doherty 	len++;
379387259bdSDeclan Doherty 
380c0f87eb5SDeclan Doherty 	/* enough ops to be sent */
381387259bdSDeclan Doherty 	if (len == MAX_PKT_BURST) {
382387259bdSDeclan Doherty 		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
383387259bdSDeclan Doherty 		len = 0;
384387259bdSDeclan Doherty 	}
385387259bdSDeclan Doherty 
386c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].len = len;
387387259bdSDeclan Doherty 	return 0;
388387259bdSDeclan Doherty }
389387259bdSDeclan Doherty 
390387259bdSDeclan Doherty static int
391387259bdSDeclan Doherty l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
392c0f87eb5SDeclan Doherty 		struct rte_crypto_op *op,
393387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
394387259bdSDeclan Doherty {
3956d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_hdr;
396a7c528e5SOlivier Matz 	struct rte_ipv4_hdr *ip_hdr;
397387259bdSDeclan Doherty 
3985839fd20SPablo de Lara 	uint32_t ipdata_offset, data_len;
3995839fd20SPablo de Lara 	uint32_t pad_len = 0;
400387259bdSDeclan Doherty 	char *padding;
401387259bdSDeclan Doherty 
4026d13ea8eSOlivier Matz 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
403387259bdSDeclan Doherty 
4040c9da755SDavid Marchand 	if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
405387259bdSDeclan Doherty 		return -1;
406387259bdSDeclan Doherty 
4076d13ea8eSOlivier Matz 	ipdata_offset = sizeof(struct rte_ether_hdr);
408387259bdSDeclan Doherty 
409a7c528e5SOlivier Matz 	ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
410387259bdSDeclan Doherty 			ipdata_offset);
411387259bdSDeclan Doherty 
41224ac604eSOlivier Matz 	ipdata_offset += (ip_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK)
41324ac604eSOlivier Matz 			* RTE_IPV4_IHL_MULTIPLIER;
414387259bdSDeclan Doherty 
415387259bdSDeclan Doherty 
416387259bdSDeclan Doherty 	/* Zero pad data to be crypto'd so it is block aligned */
417387259bdSDeclan Doherty 	data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
418893fbab0SPiotr Azarewicz 
4196df38301SPablo de Lara 	if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify)
420893fbab0SPiotr Azarewicz 		data_len -= cparams->digest_length;
421893fbab0SPiotr Azarewicz 
4225839fd20SPablo de Lara 	if (cparams->do_cipher) {
4235839fd20SPablo de Lara 		/*
4245839fd20SPablo de Lara 		 * Following algorithms are block cipher algorithms,
4255839fd20SPablo de Lara 		 * and might need padding
4265839fd20SPablo de Lara 		 */
4275839fd20SPablo de Lara 		switch (cparams->cipher_algo) {
4285839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_CBC:
4295839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_ECB:
4305839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_DES_CBC:
4315839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_CBC:
4325839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_ECB:
4335839fd20SPablo de Lara 			if (data_len % cparams->block_size)
4345839fd20SPablo de Lara 				pad_len = cparams->block_size -
4355839fd20SPablo de Lara 					(data_len % cparams->block_size);
4365839fd20SPablo de Lara 			break;
4375839fd20SPablo de Lara 		default:
4385839fd20SPablo de Lara 			pad_len = 0;
4395839fd20SPablo de Lara 		}
440387259bdSDeclan Doherty 
441387259bdSDeclan Doherty 		if (pad_len) {
442387259bdSDeclan Doherty 			padding = rte_pktmbuf_append(m, pad_len);
443387259bdSDeclan Doherty 			if (unlikely(!padding))
444387259bdSDeclan Doherty 				return -1;
445387259bdSDeclan Doherty 
446387259bdSDeclan Doherty 			data_len += pad_len;
447387259bdSDeclan Doherty 			memset(padding, 0, pad_len);
448387259bdSDeclan Doherty 		}
4495839fd20SPablo de Lara 	}
450387259bdSDeclan Doherty 
451387259bdSDeclan Doherty 	/* Set crypto operation data parameters */
452c0f87eb5SDeclan Doherty 	rte_crypto_op_attach_sym_session(op, cparams->session);
453387259bdSDeclan Doherty 
4541a75e9f3SPablo de Lara 	if (cparams->do_hash) {
455acf86169SPablo de Lara 		if (cparams->auth_iv.length) {
456acf86169SPablo de Lara 			uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
457acf86169SPablo de Lara 						uint8_t *,
458acf86169SPablo de Lara 						IV_OFFSET +
459acf86169SPablo de Lara 						cparams->cipher_iv.length);
460acf86169SPablo de Lara 			/*
461acf86169SPablo de Lara 			 * Copy IV at the end of the crypto operation,
462acf86169SPablo de Lara 			 * after the cipher IV, if added
463acf86169SPablo de Lara 			 */
464acf86169SPablo de Lara 			rte_memcpy(iv_ptr, cparams->auth_iv.data,
465acf86169SPablo de Lara 					cparams->auth_iv.length);
466acf86169SPablo de Lara 		}
46727cf2d1bSPablo de Lara 		if (!cparams->hash_verify) {
468387259bdSDeclan Doherty 			/* Append space for digest to end of packet */
469c0f87eb5SDeclan Doherty 			op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
470387259bdSDeclan Doherty 				cparams->digest_length);
47127cf2d1bSPablo de Lara 		} else {
472893fbab0SPiotr Azarewicz 			op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
473893fbab0SPiotr Azarewicz 				uint8_t *) + ipdata_offset + data_len;
47427cf2d1bSPablo de Lara 		}
47527cf2d1bSPablo de Lara 
476bfa9a8a4SThomas Monjalon 		op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
477387259bdSDeclan Doherty 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
478387259bdSDeclan Doherty 
4791f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
4802773c86dSPablo de Lara 		if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
4811f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
4821f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
483d29ea843SPablo de Lara 			op->sym->auth.data.offset = ipdata_offset << 3;
484d29ea843SPablo de Lara 			op->sym->auth.data.length = data_len << 3;
485d29ea843SPablo de Lara 		} else {
486c0f87eb5SDeclan Doherty 			op->sym->auth.data.offset = ipdata_offset;
487c0f87eb5SDeclan Doherty 			op->sym->auth.data.length = data_len;
488d29ea843SPablo de Lara 		}
4891a75e9f3SPablo de Lara 	}
4901a75e9f3SPablo de Lara 
4911a75e9f3SPablo de Lara 	if (cparams->do_cipher) {
492e636243eSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
493e636243eSPablo de Lara 							IV_OFFSET);
494e636243eSPablo de Lara 		/* Copy IV at the end of the crypto operation */
495acf86169SPablo de Lara 		rte_memcpy(iv_ptr, cparams->cipher_iv.data,
496acf86169SPablo de Lara 				cparams->cipher_iv.length);
497e636243eSPablo de Lara 
4981f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
4992773c86dSPablo de Lara 		if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
5001f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
5011f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
502d29ea843SPablo de Lara 			op->sym->cipher.data.offset = ipdata_offset << 3;
503d29ea843SPablo de Lara 			op->sym->cipher.data.length = data_len << 3;
504d29ea843SPablo de Lara 		} else {
505c0f87eb5SDeclan Doherty 			op->sym->cipher.data.offset = ipdata_offset;
506c0f87eb5SDeclan Doherty 			op->sym->cipher.data.length = data_len;
5071a75e9f3SPablo de Lara 		}
508d29ea843SPablo de Lara 	}
509387259bdSDeclan Doherty 
5102661f4fbSPablo de Lara 	if (cparams->do_aead) {
5112661f4fbSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
5122661f4fbSPablo de Lara 							IV_OFFSET);
5132661f4fbSPablo de Lara 		/* Copy IV at the end of the crypto operation */
514ff5d5b01SPablo de Lara 		/*
515ff5d5b01SPablo de Lara 		 * If doing AES-CCM, nonce is copied one byte
516ff5d5b01SPablo de Lara 		 * after the start of IV field
517ff5d5b01SPablo de Lara 		 */
518ff5d5b01SPablo de Lara 		if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
519ff5d5b01SPablo de Lara 			rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
520ff5d5b01SPablo de Lara 					cparams->aead_iv.length);
521ff5d5b01SPablo de Lara 		else
522ff5d5b01SPablo de Lara 			rte_memcpy(iv_ptr, cparams->aead_iv.data,
523ff5d5b01SPablo de Lara 					cparams->aead_iv.length);
5242661f4fbSPablo de Lara 
5252661f4fbSPablo de Lara 		op->sym->aead.data.offset = ipdata_offset;
5262661f4fbSPablo de Lara 		op->sym->aead.data.length = data_len;
5272661f4fbSPablo de Lara 
5282661f4fbSPablo de Lara 		if (!cparams->hash_verify) {
5292661f4fbSPablo de Lara 			/* Append space for digest to end of packet */
5302661f4fbSPablo de Lara 			op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
5312661f4fbSPablo de Lara 				cparams->digest_length);
5322661f4fbSPablo de Lara 		} else {
5332661f4fbSPablo de Lara 			op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
5342661f4fbSPablo de Lara 				uint8_t *) + ipdata_offset + data_len;
5352661f4fbSPablo de Lara 		}
5362661f4fbSPablo de Lara 
537bfa9a8a4SThomas Monjalon 		op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
5382661f4fbSPablo de Lara 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
5392661f4fbSPablo de Lara 
5402661f4fbSPablo de Lara 		if (cparams->aad.length) {
5412661f4fbSPablo de Lara 			op->sym->aead.aad.data = cparams->aad.data;
5422661f4fbSPablo de Lara 			op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
5432661f4fbSPablo de Lara 		}
5442661f4fbSPablo de Lara 	}
5452661f4fbSPablo de Lara 
546c0f87eb5SDeclan Doherty 	op->sym->m_src = m;
547c0f87eb5SDeclan Doherty 
548c0f87eb5SDeclan Doherty 	return l2fwd_crypto_enqueue(op, cparams);
549387259bdSDeclan Doherty }
550387259bdSDeclan Doherty 
551387259bdSDeclan Doherty 
552387259bdSDeclan Doherty /* Send the burst of packets on an output interface */
553387259bdSDeclan Doherty static int
554c0f87eb5SDeclan Doherty l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
55547523597SZhiyong Yang 		uint16_t port)
556387259bdSDeclan Doherty {
557387259bdSDeclan Doherty 	struct rte_mbuf **pkt_buffer;
558387259bdSDeclan Doherty 	unsigned ret;
559387259bdSDeclan Doherty 
560c0f87eb5SDeclan Doherty 	pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
561387259bdSDeclan Doherty 
562c0f87eb5SDeclan Doherty 	ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
563387259bdSDeclan Doherty 	port_statistics[port].tx += ret;
564387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
565387259bdSDeclan Doherty 		port_statistics[port].dropped += (n - ret);
566387259bdSDeclan Doherty 		do {
567387259bdSDeclan Doherty 			rte_pktmbuf_free(pkt_buffer[ret]);
568387259bdSDeclan Doherty 		} while (++ret < n);
569387259bdSDeclan Doherty 	}
570387259bdSDeclan Doherty 
571387259bdSDeclan Doherty 	return 0;
572387259bdSDeclan Doherty }
573387259bdSDeclan Doherty 
574387259bdSDeclan Doherty /* Enqueue packets for TX and prepare them to be sent */
575387259bdSDeclan Doherty static int
57647523597SZhiyong Yang l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
577387259bdSDeclan Doherty {
578387259bdSDeclan Doherty 	unsigned lcore_id, len;
579387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
580387259bdSDeclan Doherty 
581387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
582387259bdSDeclan Doherty 
583387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
584c0f87eb5SDeclan Doherty 	len = qconf->pkt_buf[port].len;
585c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].buffer[len] = m;
586387259bdSDeclan Doherty 	len++;
587387259bdSDeclan Doherty 
588387259bdSDeclan Doherty 	/* enough pkts to be sent */
589387259bdSDeclan Doherty 	if (unlikely(len == MAX_PKT_BURST)) {
590387259bdSDeclan Doherty 		l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
591387259bdSDeclan Doherty 		len = 0;
592387259bdSDeclan Doherty 	}
593387259bdSDeclan Doherty 
594c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].len = len;
595387259bdSDeclan Doherty 	return 0;
596387259bdSDeclan Doherty }
597387259bdSDeclan Doherty 
598387259bdSDeclan Doherty static void
599e2cdfbd0SZhiyong Yang l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
600387259bdSDeclan Doherty {
6016d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth;
602387259bdSDeclan Doherty 	void *tmp;
603387259bdSDeclan Doherty 
6046d13ea8eSOlivier Matz 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
605387259bdSDeclan Doherty 
606387259bdSDeclan Doherty 	/* 02:00:00:00:00:xx */
607387259bdSDeclan Doherty 	tmp = &eth->d_addr.addr_bytes[0];
608acdfecbaSKuba Kozak 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
609387259bdSDeclan Doherty 
610387259bdSDeclan Doherty 	/* src addr */
611538da7a1SOlivier Matz 	rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
612acdfecbaSKuba Kozak }
613acdfecbaSKuba Kozak 
614acdfecbaSKuba Kozak static void
615e2cdfbd0SZhiyong Yang l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
616acdfecbaSKuba Kozak 		struct l2fwd_crypto_options *options)
617acdfecbaSKuba Kozak {
618e2cdfbd0SZhiyong Yang 	uint16_t dst_port;
619acdfecbaSKuba Kozak 
620acdfecbaSKuba Kozak 	dst_port = l2fwd_dst_ports[portid];
621acdfecbaSKuba Kozak 
622acdfecbaSKuba Kozak 	if (options->mac_updating)
623acdfecbaSKuba Kozak 		l2fwd_mac_updating(m, dst_port);
624387259bdSDeclan Doherty 
625e2cdfbd0SZhiyong Yang 	l2fwd_send_packet(m, dst_port);
626387259bdSDeclan Doherty }
627387259bdSDeclan Doherty 
628387259bdSDeclan Doherty /** Generate random key */
629387259bdSDeclan Doherty static void
630387259bdSDeclan Doherty generate_random_key(uint8_t *key, unsigned length)
631387259bdSDeclan Doherty {
6326dad6e69SPiotr Azarewicz 	int fd;
6336dad6e69SPiotr Azarewicz 	int ret;
634387259bdSDeclan Doherty 
6356dad6e69SPiotr Azarewicz 	fd = open("/dev/urandom", O_RDONLY);
6366dad6e69SPiotr Azarewicz 	if (fd < 0)
6376dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
6386dad6e69SPiotr Azarewicz 
6396dad6e69SPiotr Azarewicz 	ret = read(fd, key, length);
6406dad6e69SPiotr Azarewicz 	close(fd);
6416dad6e69SPiotr Azarewicz 
6426dad6e69SPiotr Azarewicz 	if (ret != (signed)length)
6436dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
644387259bdSDeclan Doherty }
645387259bdSDeclan Doherty 
6461bd407faSFiona Trahe static struct rte_cryptodev_sym_session *
6472c59bd32SSlawomir Mrozowicz initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
648387259bdSDeclan Doherty {
6491bd407faSFiona Trahe 	struct rte_crypto_sym_xform *first_xform;
650b3bbd9e5SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
6518dbc9bbfSPablo de Lara 	int retval = rte_cryptodev_socket_id(cdev_id);
6528dbc9bbfSPablo de Lara 
6538dbc9bbfSPablo de Lara 	if (retval < 0)
6548dbc9bbfSPablo de Lara 		return NULL;
6558dbc9bbfSPablo de Lara 
6568dbc9bbfSPablo de Lara 	uint8_t socket_id = (uint8_t) retval;
657387259bdSDeclan Doherty 
6582661f4fbSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
6592661f4fbSPablo de Lara 		first_xform = &options->aead_xform;
6602661f4fbSPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
661387259bdSDeclan Doherty 		first_xform = &options->cipher_xform;
662387259bdSDeclan Doherty 		first_xform->next = &options->auth_xform;
6631a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
664387259bdSDeclan Doherty 		first_xform = &options->auth_xform;
665387259bdSDeclan Doherty 		first_xform->next = &options->cipher_xform;
6661a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
6671a75e9f3SPablo de Lara 		first_xform = &options->cipher_xform;
6681a75e9f3SPablo de Lara 	} else {
6691a75e9f3SPablo de Lara 		first_xform = &options->auth_xform;
670387259bdSDeclan Doherty 	}
671387259bdSDeclan Doherty 
672261bbff7SFan Zhang 	session = rte_cryptodev_sym_session_create(
673261bbff7SFan Zhang 			session_pool_socket[socket_id].sess_mp);
674b3bbd9e5SSlawomir Mrozowicz 	if (session == NULL)
675b3bbd9e5SSlawomir Mrozowicz 		return NULL;
676b3bbd9e5SSlawomir Mrozowicz 
677b3bbd9e5SSlawomir Mrozowicz 	if (rte_cryptodev_sym_session_init(cdev_id, session,
678261bbff7SFan Zhang 				first_xform,
679261bbff7SFan Zhang 				session_pool_socket[socket_id].priv_mp) < 0)
680b3bbd9e5SSlawomir Mrozowicz 		return NULL;
681b3bbd9e5SSlawomir Mrozowicz 
682b3bbd9e5SSlawomir Mrozowicz 	return session;
683387259bdSDeclan Doherty }
684387259bdSDeclan Doherty 
685387259bdSDeclan Doherty static void
686387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
687387259bdSDeclan Doherty 
688387259bdSDeclan Doherty /* main processing loop */
689387259bdSDeclan Doherty static void
690387259bdSDeclan Doherty l2fwd_main_loop(struct l2fwd_crypto_options *options)
691387259bdSDeclan Doherty {
692387259bdSDeclan Doherty 	struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
693c0f87eb5SDeclan Doherty 	struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
694c0f87eb5SDeclan Doherty 
695387259bdSDeclan Doherty 	unsigned lcore_id = rte_lcore_id();
696387259bdSDeclan Doherty 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
697e2cdfbd0SZhiyong Yang 	unsigned int i, j, nb_rx, len;
698e2cdfbd0SZhiyong Yang 	uint16_t portid;
699387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
700387259bdSDeclan Doherty 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
701387259bdSDeclan Doherty 			US_PER_S * BURST_TX_DRAIN_US;
702387259bdSDeclan Doherty 	struct l2fwd_crypto_params *cparams;
703387259bdSDeclan Doherty 	struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
7042c59bd32SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
705387259bdSDeclan Doherty 
706387259bdSDeclan Doherty 	if (qconf->nb_rx_ports == 0) {
707387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
708387259bdSDeclan Doherty 		return;
709387259bdSDeclan Doherty 	}
710387259bdSDeclan Doherty 
711387259bdSDeclan Doherty 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
712387259bdSDeclan Doherty 
713387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_rx_ports; i++) {
714387259bdSDeclan Doherty 
715387259bdSDeclan Doherty 		portid = qconf->rx_port_list[i];
716387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
717387259bdSDeclan Doherty 			portid);
718387259bdSDeclan Doherty 	}
719387259bdSDeclan Doherty 
720387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_crypto_devs; i++) {
7211a75e9f3SPablo de Lara 		port_cparams[i].do_cipher = 0;
7221a75e9f3SPablo de Lara 		port_cparams[i].do_hash = 0;
7232661f4fbSPablo de Lara 		port_cparams[i].do_aead = 0;
7241a75e9f3SPablo de Lara 
7251a75e9f3SPablo de Lara 		switch (options->xform_chain) {
7262661f4fbSPablo de Lara 		case L2FWD_CRYPTO_AEAD:
7272661f4fbSPablo de Lara 			port_cparams[i].do_aead = 1;
7282661f4fbSPablo de Lara 			break;
7291a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_HASH:
7301a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_CIPHER:
7311a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7321a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7331a75e9f3SPablo de Lara 			break;
7341a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_ONLY:
7351a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7361a75e9f3SPablo de Lara 			break;
7371a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_ONLY:
7381a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7391a75e9f3SPablo de Lara 			break;
7401a75e9f3SPablo de Lara 		}
7411a75e9f3SPablo de Lara 
742387259bdSDeclan Doherty 		port_cparams[i].dev_id = qconf->cryptodev_list[i];
743387259bdSDeclan Doherty 		port_cparams[i].qp_id = 0;
744387259bdSDeclan Doherty 
74527cf2d1bSPablo de Lara 		port_cparams[i].block_size = options->block_size;
746387259bdSDeclan Doherty 
7471a75e9f3SPablo de Lara 		if (port_cparams[i].do_hash) {
748acf86169SPablo de Lara 			port_cparams[i].auth_iv.data = options->auth_iv.data;
749acf86169SPablo de Lara 			port_cparams[i].auth_iv.length = options->auth_iv.length;
750acf86169SPablo de Lara 			if (!options->auth_iv_param)
751acf86169SPablo de Lara 				generate_random_key(port_cparams[i].auth_iv.data,
752acf86169SPablo de Lara 						port_cparams[i].auth_iv.length);
7532661f4fbSPablo de Lara 			if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
7542661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 1;
7552661f4fbSPablo de Lara 			else
7562661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 0;
7572661f4fbSPablo de Lara 
7582661f4fbSPablo de Lara 			port_cparams[i].auth_algo = options->auth_xform.auth.algo;
7592c023071SPablo de Lara 			port_cparams[i].digest_length =
7602c023071SPablo de Lara 					options->auth_xform.auth.digest_length;
761acf86169SPablo de Lara 			/* Set IV parameters */
762acf86169SPablo de Lara 			if (options->auth_iv.length) {
763acf86169SPablo de Lara 				options->auth_xform.auth.iv.offset =
764acf86169SPablo de Lara 					IV_OFFSET + options->cipher_iv.length;
765acf86169SPablo de Lara 				options->auth_xform.auth.iv.length =
766acf86169SPablo de Lara 					options->auth_iv.length;
767acf86169SPablo de Lara 			}
7682661f4fbSPablo de Lara 		}
7692661f4fbSPablo de Lara 
7702661f4fbSPablo de Lara 		if (port_cparams[i].do_aead) {
771ef896e92SPablo de Lara 			port_cparams[i].aead_iv.data = options->aead_iv.data;
772ef896e92SPablo de Lara 			port_cparams[i].aead_iv.length = options->aead_iv.length;
773ef896e92SPablo de Lara 			if (!options->aead_iv_param)
774ef896e92SPablo de Lara 				generate_random_key(port_cparams[i].aead_iv.data,
775ef896e92SPablo de Lara 						port_cparams[i].aead_iv.length);
7762661f4fbSPablo de Lara 			port_cparams[i].aead_algo = options->aead_xform.aead.algo;
77727cf2d1bSPablo de Lara 			port_cparams[i].digest_length =
7782661f4fbSPablo de Lara 					options->aead_xform.aead.digest_length;
77946a0547fSPablo de Lara 			if (options->aead_xform.aead.aad_length) {
78027cf2d1bSPablo de Lara 				port_cparams[i].aad.data = options->aad.data;
781617a7949SPablo de Lara 				port_cparams[i].aad.phys_addr = options->aad.phys_addr;
7822661f4fbSPablo de Lara 				port_cparams[i].aad.length = options->aad.length;
783617a7949SPablo de Lara 				if (!options->aad_param)
78427cf2d1bSPablo de Lara 					generate_random_key(port_cparams[i].aad.data,
785a158899aSPablo de Lara 						port_cparams[i].aad.length);
786ff5d5b01SPablo de Lara 				/*
787ff5d5b01SPablo de Lara 				 * If doing AES-CCM, first 18 bytes has to be reserved,
788ff5d5b01SPablo de Lara 				 * and actual AAD should start from byte 18
789ff5d5b01SPablo de Lara 				 */
790ff5d5b01SPablo de Lara 				if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
791ff5d5b01SPablo de Lara 					memmove(port_cparams[i].aad.data + 18,
792ff5d5b01SPablo de Lara 							port_cparams[i].aad.data,
793ff5d5b01SPablo de Lara 							port_cparams[i].aad.length);
79427cf2d1bSPablo de Lara 
79518f421f6SPablo de Lara 			} else
79618f421f6SPablo de Lara 				port_cparams[i].aad.length = 0;
79727cf2d1bSPablo de Lara 
7982661f4fbSPablo de Lara 			if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
79927cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 1;
80027cf2d1bSPablo de Lara 			else
80127cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 0;
802d29ea843SPablo de Lara 
8032661f4fbSPablo de Lara 			/* Set IV parameters */
8042661f4fbSPablo de Lara 			options->aead_xform.aead.iv.offset = IV_OFFSET;
8052661f4fbSPablo de Lara 			options->aead_xform.aead.iv.length = options->aead_iv.length;
8061a75e9f3SPablo de Lara 		}
8071a75e9f3SPablo de Lara 
8081a75e9f3SPablo de Lara 		if (port_cparams[i].do_cipher) {
809acf86169SPablo de Lara 			port_cparams[i].cipher_iv.data = options->cipher_iv.data;
810acf86169SPablo de Lara 			port_cparams[i].cipher_iv.length = options->cipher_iv.length;
811acf86169SPablo de Lara 			if (!options->cipher_iv_param)
812acf86169SPablo de Lara 				generate_random_key(port_cparams[i].cipher_iv.data,
813acf86169SPablo de Lara 						port_cparams[i].cipher_iv.length);
814617a7949SPablo de Lara 
815d29ea843SPablo de Lara 			port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
8160fbd75a9SPablo de Lara 			/* Set IV parameters */
8170fbd75a9SPablo de Lara 			options->cipher_xform.cipher.iv.offset = IV_OFFSET;
818acf86169SPablo de Lara 			options->cipher_xform.cipher.iv.length =
819acf86169SPablo de Lara 						options->cipher_iv.length;
82027cf2d1bSPablo de Lara 		}
821617a7949SPablo de Lara 
8222c59bd32SSlawomir Mrozowicz 		session = initialize_crypto_session(options,
823387259bdSDeclan Doherty 				port_cparams[i].dev_id);
8242c59bd32SSlawomir Mrozowicz 		if (session == NULL)
8252c59bd32SSlawomir Mrozowicz 			rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
826387259bdSDeclan Doherty 
8272c59bd32SSlawomir Mrozowicz 		port_cparams[i].session = session;
8282c59bd32SSlawomir Mrozowicz 
829387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
830387259bdSDeclan Doherty 				port_cparams[i].dev_id);
831387259bdSDeclan Doherty 	}
832387259bdSDeclan Doherty 
83341e97c2eSPablo de Lara 	l2fwd_crypto_options_print(options);
83441e97c2eSPablo de Lara 
83541e97c2eSPablo de Lara 	/*
83641e97c2eSPablo de Lara 	 * Initialize previous tsc timestamp before the loop,
83741e97c2eSPablo de Lara 	 * to avoid showing the port statistics immediately,
83841e97c2eSPablo de Lara 	 * so user can see the crypto information.
83941e97c2eSPablo de Lara 	 */
84041e97c2eSPablo de Lara 	prev_tsc = rte_rdtsc();
841387259bdSDeclan Doherty 	while (1) {
842387259bdSDeclan Doherty 
843387259bdSDeclan Doherty 		cur_tsc = rte_rdtsc();
844387259bdSDeclan Doherty 
845387259bdSDeclan Doherty 		/*
846268ca735SPablo de Lara 		 * Crypto device/TX burst queue drain
847387259bdSDeclan Doherty 		 */
848387259bdSDeclan Doherty 		diff_tsc = cur_tsc - prev_tsc;
849387259bdSDeclan Doherty 		if (unlikely(diff_tsc > drain_tsc)) {
850268ca735SPablo de Lara 			/* Enqueue all crypto ops remaining in buffers */
851268ca735SPablo de Lara 			for (i = 0; i < qconf->nb_crypto_devs; i++) {
852268ca735SPablo de Lara 				cparams = &port_cparams[i];
853268ca735SPablo de Lara 				len = qconf->op_buf[cparams->dev_id].len;
854268ca735SPablo de Lara 				l2fwd_crypto_send_burst(qconf, len, cparams);
855268ca735SPablo de Lara 				qconf->op_buf[cparams->dev_id].len = 0;
856268ca735SPablo de Lara 			}
857268ca735SPablo de Lara 			/* Transmit all packets remaining in buffers */
858387259bdSDeclan Doherty 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
859c0f87eb5SDeclan Doherty 				if (qconf->pkt_buf[portid].len == 0)
860387259bdSDeclan Doherty 					continue;
861387259bdSDeclan Doherty 				l2fwd_send_burst(&lcore_queue_conf[lcore_id],
862c0f87eb5SDeclan Doherty 						 qconf->pkt_buf[portid].len,
863e2cdfbd0SZhiyong Yang 						 portid);
864c0f87eb5SDeclan Doherty 				qconf->pkt_buf[portid].len = 0;
865387259bdSDeclan Doherty 			}
866387259bdSDeclan Doherty 
867387259bdSDeclan Doherty 			/* if timer is enabled */
868387259bdSDeclan Doherty 			if (timer_period > 0) {
869387259bdSDeclan Doherty 
870387259bdSDeclan Doherty 				/* advance the timer */
871387259bdSDeclan Doherty 				timer_tsc += diff_tsc;
872387259bdSDeclan Doherty 
873387259bdSDeclan Doherty 				/* if timer has reached its timeout */
874387259bdSDeclan Doherty 				if (unlikely(timer_tsc >=
875387259bdSDeclan Doherty 						(uint64_t)timer_period)) {
876387259bdSDeclan Doherty 
877387259bdSDeclan Doherty 					/* do this only on master core */
878ad509b4aSDeclan Doherty 					if (lcore_id == rte_get_master_lcore()
879ad509b4aSDeclan Doherty 						&& options->refresh_period) {
880387259bdSDeclan Doherty 						print_stats();
881387259bdSDeclan Doherty 						timer_tsc = 0;
882387259bdSDeclan Doherty 					}
883387259bdSDeclan Doherty 				}
884387259bdSDeclan Doherty 			}
885387259bdSDeclan Doherty 
886387259bdSDeclan Doherty 			prev_tsc = cur_tsc;
887387259bdSDeclan Doherty 		}
888387259bdSDeclan Doherty 
889387259bdSDeclan Doherty 		/*
890387259bdSDeclan Doherty 		 * Read packet from RX queues
891387259bdSDeclan Doherty 		 */
892387259bdSDeclan Doherty 		for (i = 0; i < qconf->nb_rx_ports; i++) {
893387259bdSDeclan Doherty 			portid = qconf->rx_port_list[i];
894387259bdSDeclan Doherty 
895387259bdSDeclan Doherty 			cparams = &port_cparams[i];
896387259bdSDeclan Doherty 
897e2cdfbd0SZhiyong Yang 			nb_rx = rte_eth_rx_burst(portid, 0,
898387259bdSDeclan Doherty 						 pkts_burst, MAX_PKT_BURST);
899387259bdSDeclan Doherty 
900387259bdSDeclan Doherty 			port_statistics[portid].rx += nb_rx;
901387259bdSDeclan Doherty 
902c0f87eb5SDeclan Doherty 			if (nb_rx) {
903387259bdSDeclan Doherty 				/*
904c0f87eb5SDeclan Doherty 				 * If we can't allocate a crypto_ops, then drop
905387259bdSDeclan Doherty 				 * the rest of the burst and dequeue and
906387259bdSDeclan Doherty 				 * process the packets to free offload structs
907387259bdSDeclan Doherty 				 */
908c0f87eb5SDeclan Doherty 				if (rte_crypto_op_bulk_alloc(
909c0f87eb5SDeclan Doherty 						l2fwd_crypto_op_pool,
910c0f87eb5SDeclan Doherty 						RTE_CRYPTO_OP_TYPE_SYMMETRIC,
911c0f87eb5SDeclan Doherty 						ops_burst, nb_rx) !=
912c0f87eb5SDeclan Doherty 								nb_rx) {
913c0f87eb5SDeclan Doherty 					for (j = 0; j < nb_rx; j++)
914d7acf6baSPablo de Lara 						rte_pktmbuf_free(pkts_burst[j]);
915c0f87eb5SDeclan Doherty 
916c0f87eb5SDeclan Doherty 					nb_rx = 0;
917387259bdSDeclan Doherty 				}
918387259bdSDeclan Doherty 
919c0f87eb5SDeclan Doherty 				/* Enqueue packets from Crypto device*/
920c0f87eb5SDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
921c0f87eb5SDeclan Doherty 					m = pkts_burst[j];
922387259bdSDeclan Doherty 
923c0f87eb5SDeclan Doherty 					l2fwd_simple_crypto_enqueue(m,
924c0f87eb5SDeclan Doherty 							ops_burst[j], cparams);
925c0f87eb5SDeclan Doherty 				}
926387259bdSDeclan Doherty 			}
927387259bdSDeclan Doherty 
928387259bdSDeclan Doherty 			/* Dequeue packets from Crypto device */
929c0f87eb5SDeclan Doherty 			do {
930387259bdSDeclan Doherty 				nb_rx = rte_cryptodev_dequeue_burst(
931387259bdSDeclan Doherty 						cparams->dev_id, cparams->qp_id,
932c0f87eb5SDeclan Doherty 						ops_burst, MAX_PKT_BURST);
933c0f87eb5SDeclan Doherty 
934c0f87eb5SDeclan Doherty 				crypto_statistics[cparams->dev_id].dequeued +=
935c0f87eb5SDeclan Doherty 						nb_rx;
936387259bdSDeclan Doherty 
937387259bdSDeclan Doherty 				/* Forward crypto'd packets */
938387259bdSDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
939c0f87eb5SDeclan Doherty 					m = ops_burst[j]->sym->m_src;
940c0f87eb5SDeclan Doherty 
941c0f87eb5SDeclan Doherty 					rte_crypto_op_free(ops_burst[j]);
942acdfecbaSKuba Kozak 					l2fwd_simple_forward(m, portid,
943acdfecbaSKuba Kozak 							options);
944387259bdSDeclan Doherty 				}
945c0f87eb5SDeclan Doherty 			} while (nb_rx == MAX_PKT_BURST);
946387259bdSDeclan Doherty 		}
947387259bdSDeclan Doherty 	}
948387259bdSDeclan Doherty }
949387259bdSDeclan Doherty 
950387259bdSDeclan Doherty static int
951387259bdSDeclan Doherty l2fwd_launch_one_lcore(void *arg)
952387259bdSDeclan Doherty {
953387259bdSDeclan Doherty 	l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
954387259bdSDeclan Doherty 	return 0;
955387259bdSDeclan Doherty }
956387259bdSDeclan Doherty 
957387259bdSDeclan Doherty /* Display command line arguments usage */
958387259bdSDeclan Doherty static void
959387259bdSDeclan Doherty l2fwd_crypto_usage(const char *prgname)
960387259bdSDeclan Doherty {
961912b3a0aSPablo de Lara 	printf("%s [EAL options] --\n"
962387259bdSDeclan Doherty 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
963387259bdSDeclan Doherty 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
96469a558e3SPablo de Lara 		"  -s manage all ports from single lcore\n"
965a3380989SPablo de Lara 		"  -T PERIOD: statistics will be refreshed each PERIOD seconds"
966387259bdSDeclan Doherty 		" (0 to disable, 10 default, 86400 maximum)\n"
967387259bdSDeclan Doherty 
968912b3a0aSPablo de Lara 		"  --cdev_type HW / SW / ANY\n"
969a3c2e34bSPablo de Lara 		"  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
9702661f4fbSPablo de Lara 		" HASH_ONLY / AEAD\n"
971387259bdSDeclan Doherty 
972387259bdSDeclan Doherty 		"  --cipher_algo ALGO\n"
973387259bdSDeclan Doherty 		"  --cipher_op ENCRYPT / DECRYPT\n"
974fcdbb3d5SPablo de Lara 		"  --cipher_key KEY (bytes separated with \":\")\n"
975a061e50aSPablo de Lara 		"  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
976acf86169SPablo de Lara 		"  --cipher_iv IV (bytes separated with \":\")\n"
977acf86169SPablo de Lara 		"  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
978387259bdSDeclan Doherty 
9793b98cbaaSPablo de Lara 		"  --auth_algo ALGO\n"
980387259bdSDeclan Doherty 		"  --auth_op GENERATE / VERIFY\n"
981fcdbb3d5SPablo de Lara 		"  --auth_key KEY (bytes separated with \":\")\n"
982a061e50aSPablo de Lara 		"  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
983acf86169SPablo de Lara 		"  --auth_iv IV (bytes separated with \":\")\n"
984acf86169SPablo de Lara 		"  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
9852661f4fbSPablo de Lara 
9862661f4fbSPablo de Lara 		"  --aead_algo ALGO\n"
9872661f4fbSPablo de Lara 		"  --aead_op ENCRYPT / DECRYPT\n"
9882661f4fbSPablo de Lara 		"  --aead_key KEY (bytes separated with \":\")\n"
9892661f4fbSPablo de Lara 		"  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
9902661f4fbSPablo de Lara 		"  --aead_iv IV (bytes separated with \":\")\n"
9912661f4fbSPablo de Lara 		"  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
992fcdbb3d5SPablo de Lara 		"  --aad AAD (bytes separated with \":\")\n"
993a061e50aSPablo de Lara 		"  --aad_random_size SIZE: size of AAD when generated randomly\n"
9942661f4fbSPablo de Lara 
995a061e50aSPablo de Lara 		"  --digest_size SIZE: size of digest to be generated/verified\n"
996387259bdSDeclan Doherty 
997d2797f51SFan Zhang 		"  --sessionless\n"
998acdfecbaSKuba Kozak 		"  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
999acdfecbaSKuba Kozak 
1000acdfecbaSKuba Kozak 		"  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1001acdfecbaSKuba Kozak 		"      When enabled:\n"
1002acdfecbaSKuba Kozak 		"       - The source MAC address is replaced by the TX port MAC address\n"
1003acdfecbaSKuba Kozak 		"       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1004387259bdSDeclan Doherty 	       prgname);
1005387259bdSDeclan Doherty }
1006387259bdSDeclan Doherty 
1007387259bdSDeclan Doherty /** Parse crypto device type command line argument */
1008387259bdSDeclan Doherty static int
100927cf2d1bSPablo de Lara parse_cryptodev_type(enum cdev_type *type, char *optarg)
1010387259bdSDeclan Doherty {
101127cf2d1bSPablo de Lara 	if (strcmp("HW", optarg) == 0) {
101227cf2d1bSPablo de Lara 		*type = CDEV_TYPE_HW;
1013387259bdSDeclan Doherty 		return 0;
101427cf2d1bSPablo de Lara 	} else if (strcmp("SW", optarg) == 0) {
101527cf2d1bSPablo de Lara 		*type = CDEV_TYPE_SW;
101627cf2d1bSPablo de Lara 		return 0;
101727cf2d1bSPablo de Lara 	} else if (strcmp("ANY", optarg) == 0) {
101827cf2d1bSPablo de Lara 		*type = CDEV_TYPE_ANY;
1019387259bdSDeclan Doherty 		return 0;
1020387259bdSDeclan Doherty 	}
1021387259bdSDeclan Doherty 
1022387259bdSDeclan Doherty 	return -1;
1023387259bdSDeclan Doherty }
1024387259bdSDeclan Doherty 
1025387259bdSDeclan Doherty /** Parse crypto chain xform command line argument */
1026387259bdSDeclan Doherty static int
1027387259bdSDeclan Doherty parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1028387259bdSDeclan Doherty {
1029387259bdSDeclan Doherty 	if (strcmp("CIPHER_HASH", optarg) == 0) {
1030387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1031387259bdSDeclan Doherty 		return 0;
1032387259bdSDeclan Doherty 	} else if (strcmp("HASH_CIPHER", optarg) == 0) {
1033387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1034387259bdSDeclan Doherty 		return 0;
10351a75e9f3SPablo de Lara 	} else if (strcmp("CIPHER_ONLY", optarg) == 0) {
10361a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
10371a75e9f3SPablo de Lara 		return 0;
10381a75e9f3SPablo de Lara 	} else if (strcmp("HASH_ONLY", optarg) == 0) {
10391a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
10401a75e9f3SPablo de Lara 		return 0;
10412661f4fbSPablo de Lara 	} else if (strcmp("AEAD", optarg) == 0) {
10422661f4fbSPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_AEAD;
10432661f4fbSPablo de Lara 		return 0;
1044387259bdSDeclan Doherty 	}
1045387259bdSDeclan Doherty 
1046387259bdSDeclan Doherty 	return -1;
1047387259bdSDeclan Doherty }
1048387259bdSDeclan Doherty 
1049387259bdSDeclan Doherty /** Parse crypto cipher algo option command line argument */
1050387259bdSDeclan Doherty static int
1051387259bdSDeclan Doherty parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1052387259bdSDeclan Doherty {
105300c58901SPablo de Lara 
10544790f99dSPablo de Lara 	if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
10554790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Cipher algorithm specified "
10564790f99dSPablo de Lara 				"not supported!\n");
1057387259bdSDeclan Doherty 		return -1;
1058387259bdSDeclan Doherty 	}
1059387259bdSDeclan Doherty 
10604790f99dSPablo de Lara 	return 0;
10614790f99dSPablo de Lara }
10624790f99dSPablo de Lara 
1063387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1064387259bdSDeclan Doherty static int
1065387259bdSDeclan Doherty parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1066387259bdSDeclan Doherty {
1067387259bdSDeclan Doherty 	if (strcmp("ENCRYPT", optarg) == 0) {
1068387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1069387259bdSDeclan Doherty 		return 0;
1070387259bdSDeclan Doherty 	} else if (strcmp("DECRYPT", optarg) == 0) {
1071387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1072387259bdSDeclan Doherty 		return 0;
1073387259bdSDeclan Doherty 	}
1074387259bdSDeclan Doherty 
1075387259bdSDeclan Doherty 	printf("Cipher operation not supported!\n");
1076387259bdSDeclan Doherty 	return -1;
1077387259bdSDeclan Doherty }
1078387259bdSDeclan Doherty 
1079ff5d5b01SPablo de Lara /** Parse bytes from command line argument */
1080387259bdSDeclan Doherty static int
1081ff5d5b01SPablo de Lara parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1082387259bdSDeclan Doherty {
10831df9c010SPablo de Lara 	unsigned byte_count;
10841df9c010SPablo de Lara 	char *token;
10851df9c010SPablo de Lara 
108645544627SHemant Agrawal 	errno = 0;
10871df9c010SPablo de Lara 	for (byte_count = 0, token = strtok(input_arg, ":");
1088ff5d5b01SPablo de Lara 			(byte_count < max_size) && (token != NULL);
10891df9c010SPablo de Lara 			token = strtok(NULL, ":")) {
10901df9c010SPablo de Lara 
10911df9c010SPablo de Lara 		int number = (int)strtol(token, NULL, 16);
10921df9c010SPablo de Lara 
10931df9c010SPablo de Lara 		if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1094387259bdSDeclan Doherty 			return -1;
10951df9c010SPablo de Lara 
10961df9c010SPablo de Lara 		data[byte_count++] = (uint8_t)number;
10971df9c010SPablo de Lara 	}
10981df9c010SPablo de Lara 
1099a061e50aSPablo de Lara 	return byte_count;
1100a061e50aSPablo de Lara }
1101a061e50aSPablo de Lara 
1102a061e50aSPablo de Lara /** Parse size param*/
1103a061e50aSPablo de Lara static int
1104a061e50aSPablo de Lara parse_size(int *size, const char *q_arg)
1105a061e50aSPablo de Lara {
1106a061e50aSPablo de Lara 	char *end = NULL;
1107a061e50aSPablo de Lara 	unsigned long n;
1108a061e50aSPablo de Lara 
1109a061e50aSPablo de Lara 	/* parse hexadecimal string */
1110a061e50aSPablo de Lara 	n = strtoul(q_arg, &end, 10);
1111a061e50aSPablo de Lara 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1112a061e50aSPablo de Lara 		n = 0;
1113a061e50aSPablo de Lara 
1114a061e50aSPablo de Lara 	if (n == 0) {
1115a061e50aSPablo de Lara 		printf("invalid size\n");
1116a061e50aSPablo de Lara 		return -1;
1117a061e50aSPablo de Lara 	}
1118a061e50aSPablo de Lara 
1119a061e50aSPablo de Lara 	*size = n;
11201df9c010SPablo de Lara 	return 0;
1121387259bdSDeclan Doherty }
1122387259bdSDeclan Doherty 
1123387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1124387259bdSDeclan Doherty static int
1125387259bdSDeclan Doherty parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1126387259bdSDeclan Doherty {
11274790f99dSPablo de Lara 	if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
11284790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Authentication algorithm specified "
11294790f99dSPablo de Lara 				"not supported!\n");
1130387259bdSDeclan Doherty 		return -1;
1131387259bdSDeclan Doherty 	}
1132387259bdSDeclan Doherty 
11334790f99dSPablo de Lara 	return 0;
11344790f99dSPablo de Lara }
11354790f99dSPablo de Lara 
1136387259bdSDeclan Doherty static int
1137387259bdSDeclan Doherty parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1138387259bdSDeclan Doherty {
1139387259bdSDeclan Doherty 	if (strcmp("VERIFY", optarg) == 0) {
1140387259bdSDeclan Doherty 		*op = RTE_CRYPTO_AUTH_OP_VERIFY;
1141387259bdSDeclan Doherty 		return 0;
1142387259bdSDeclan Doherty 	} else if (strcmp("GENERATE", optarg) == 0) {
114372169087SPablo de Lara 		*op = RTE_CRYPTO_AUTH_OP_GENERATE;
1144387259bdSDeclan Doherty 		return 0;
1145387259bdSDeclan Doherty 	}
1146387259bdSDeclan Doherty 
1147387259bdSDeclan Doherty 	printf("Authentication operation specified not supported!\n");
1148387259bdSDeclan Doherty 	return -1;
1149387259bdSDeclan Doherty }
1150387259bdSDeclan Doherty 
1151d2797f51SFan Zhang static int
11522661f4fbSPablo de Lara parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
11532661f4fbSPablo de Lara {
11542661f4fbSPablo de Lara 	if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
11552661f4fbSPablo de Lara 		RTE_LOG(ERR, USER1, "AEAD algorithm specified "
11562661f4fbSPablo de Lara 				"not supported!\n");
11572661f4fbSPablo de Lara 		return -1;
11582661f4fbSPablo de Lara 	}
11592661f4fbSPablo de Lara 
11602661f4fbSPablo de Lara 	return 0;
11612661f4fbSPablo de Lara }
11622661f4fbSPablo de Lara 
11632661f4fbSPablo de Lara static int
11642661f4fbSPablo de Lara parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
11652661f4fbSPablo de Lara {
11662661f4fbSPablo de Lara 	if (strcmp("ENCRYPT", optarg) == 0) {
11672661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
11682661f4fbSPablo de Lara 		return 0;
11692661f4fbSPablo de Lara 	} else if (strcmp("DECRYPT", optarg) == 0) {
11702661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_DECRYPT;
11712661f4fbSPablo de Lara 		return 0;
11722661f4fbSPablo de Lara 	}
11732661f4fbSPablo de Lara 
11742661f4fbSPablo de Lara 	printf("AEAD operation specified not supported!\n");
11752661f4fbSPablo de Lara 	return -1;
11762661f4fbSPablo de Lara }
11772661f4fbSPablo de Lara static int
1178d2797f51SFan Zhang parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1179d2797f51SFan Zhang 		const char *q_arg)
1180d2797f51SFan Zhang {
1181d2797f51SFan Zhang 	char *end = NULL;
1182d2797f51SFan Zhang 	uint64_t pm;
1183d2797f51SFan Zhang 
1184d2797f51SFan Zhang 	/* parse hexadecimal string */
1185d2797f51SFan Zhang 	pm = strtoul(q_arg, &end, 16);
1186d2797f51SFan Zhang 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1187d2797f51SFan Zhang 		pm = 0;
1188d2797f51SFan Zhang 
1189d2797f51SFan Zhang 	options->cryptodev_mask = pm;
1190d2797f51SFan Zhang 	if (options->cryptodev_mask == 0) {
1191d2797f51SFan Zhang 		printf("invalid cryptodev_mask specified\n");
1192d2797f51SFan Zhang 		return -1;
1193d2797f51SFan Zhang 	}
1194d2797f51SFan Zhang 
1195d2797f51SFan Zhang 	return 0;
1196d2797f51SFan Zhang }
1197d2797f51SFan Zhang 
1198387259bdSDeclan Doherty /** Parse long options */
1199387259bdSDeclan Doherty static int
1200387259bdSDeclan Doherty l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1201387259bdSDeclan Doherty 		struct option *lgopts, int option_index)
1202387259bdSDeclan Doherty {
120327cf2d1bSPablo de Lara 	int retval;
120427cf2d1bSPablo de Lara 
120549f79e86SPablo de Lara 	if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
120649f79e86SPablo de Lara 		retval = parse_cryptodev_type(&options->type, optarg);
120749f79e86SPablo de Lara 		if (retval == 0)
12086723c0fcSBruce Richardson 			strlcpy(options->string_type, optarg, MAX_STR_LEN);
120949f79e86SPablo de Lara 		return retval;
121049f79e86SPablo de Lara 	}
1211387259bdSDeclan Doherty 
1212387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "chain") == 0)
1213387259bdSDeclan Doherty 		return parse_crypto_opt_chain(options, optarg);
1214387259bdSDeclan Doherty 
1215387259bdSDeclan Doherty 	/* Cipher options */
121600c58901SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
121700c58901SPablo de Lara 		return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1218387259bdSDeclan Doherty 				optarg);
1219387259bdSDeclan Doherty 
1220387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1221387259bdSDeclan Doherty 		return parse_cipher_op(&options->cipher_xform.cipher.op,
1222387259bdSDeclan Doherty 				optarg);
1223387259bdSDeclan Doherty 
1224a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
12251df9c010SPablo de Lara 		options->ckey_param = 1;
1226a061e50aSPablo de Lara 		options->cipher_xform.cipher.key.length =
1227186b14d6SFan Zhang 			parse_bytes(options->cipher_key, optarg, MAX_KEY_SIZE);
1228a061e50aSPablo de Lara 		if (options->cipher_xform.cipher.key.length > 0)
1229a061e50aSPablo de Lara 			return 0;
1230a061e50aSPablo de Lara 		else
1231a061e50aSPablo de Lara 			return -1;
12321df9c010SPablo de Lara 	}
1233387259bdSDeclan Doherty 
1234a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1235a061e50aSPablo de Lara 		return parse_size(&options->ckey_random_size, optarg);
1236a061e50aSPablo de Lara 
1237acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1238acf86169SPablo de Lara 		options->cipher_iv_param = 1;
1239acf86169SPablo de Lara 		options->cipher_iv.length =
1240ff5d5b01SPablo de Lara 			parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1241acf86169SPablo de Lara 		if (options->cipher_iv.length > 0)
1242a061e50aSPablo de Lara 			return 0;
1243a061e50aSPablo de Lara 		else
1244a061e50aSPablo de Lara 			return -1;
12451df9c010SPablo de Lara 	}
1246387259bdSDeclan Doherty 
1247acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1248acf86169SPablo de Lara 		return parse_size(&options->cipher_iv_random_size, optarg);
1249a061e50aSPablo de Lara 
1250387259bdSDeclan Doherty 	/* Authentication options */
125127cf2d1bSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
125200c58901SPablo de Lara 		return parse_auth_algo(&options->auth_xform.auth.algo,
1253387259bdSDeclan Doherty 				optarg);
125427cf2d1bSPablo de Lara 	}
1255387259bdSDeclan Doherty 
1256387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
125772169087SPablo de Lara 		return parse_auth_op(&options->auth_xform.auth.op,
1258387259bdSDeclan Doherty 				optarg);
1259387259bdSDeclan Doherty 
1260a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
12611df9c010SPablo de Lara 		options->akey_param = 1;
1262a061e50aSPablo de Lara 		options->auth_xform.auth.key.length =
1263186b14d6SFan Zhang 			parse_bytes(options->auth_key, optarg, MAX_KEY_SIZE);
1264a061e50aSPablo de Lara 		if (options->auth_xform.auth.key.length > 0)
1265a061e50aSPablo de Lara 			return 0;
1266a061e50aSPablo de Lara 		else
1267a061e50aSPablo de Lara 			return -1;
1268a061e50aSPablo de Lara 	}
1269a061e50aSPablo de Lara 
1270a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1271a061e50aSPablo de Lara 		return parse_size(&options->akey_random_size, optarg);
12721df9c010SPablo de Lara 	}
1273387259bdSDeclan Doherty 
1274acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1275acf86169SPablo de Lara 		options->auth_iv_param = 1;
1276acf86169SPablo de Lara 		options->auth_iv.length =
1277ff5d5b01SPablo de Lara 			parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1278acf86169SPablo de Lara 		if (options->auth_iv.length > 0)
1279acf86169SPablo de Lara 			return 0;
1280acf86169SPablo de Lara 		else
1281acf86169SPablo de Lara 			return -1;
1282acf86169SPablo de Lara 	}
1283acf86169SPablo de Lara 
1284acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1285acf86169SPablo de Lara 		return parse_size(&options->auth_iv_random_size, optarg);
1286acf86169SPablo de Lara 
12872661f4fbSPablo de Lara 	/* AEAD options */
12882661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
12892661f4fbSPablo de Lara 		return parse_aead_algo(&options->aead_xform.aead.algo,
12902661f4fbSPablo de Lara 				optarg);
12912661f4fbSPablo de Lara 	}
12922661f4fbSPablo de Lara 
12932661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
12942661f4fbSPablo de Lara 		return parse_aead_op(&options->aead_xform.aead.op,
12952661f4fbSPablo de Lara 				optarg);
12962661f4fbSPablo de Lara 
12972661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
12982661f4fbSPablo de Lara 		options->aead_key_param = 1;
12992661f4fbSPablo de Lara 		options->aead_xform.aead.key.length =
1300186b14d6SFan Zhang 			parse_bytes(options->aead_key, optarg, MAX_KEY_SIZE);
13012661f4fbSPablo de Lara 		if (options->aead_xform.aead.key.length > 0)
13022661f4fbSPablo de Lara 			return 0;
13032661f4fbSPablo de Lara 		else
13042661f4fbSPablo de Lara 			return -1;
13052661f4fbSPablo de Lara 	}
13062661f4fbSPablo de Lara 
13072661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
13082661f4fbSPablo de Lara 		return parse_size(&options->aead_key_random_size, optarg);
13092661f4fbSPablo de Lara 
13102661f4fbSPablo de Lara 
13112661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
13122661f4fbSPablo de Lara 		options->aead_iv_param = 1;
13132661f4fbSPablo de Lara 		options->aead_iv.length =
1314ff5d5b01SPablo de Lara 			parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
13152661f4fbSPablo de Lara 		if (options->aead_iv.length > 0)
13162661f4fbSPablo de Lara 			return 0;
13172661f4fbSPablo de Lara 		else
13182661f4fbSPablo de Lara 			return -1;
13192661f4fbSPablo de Lara 	}
13202661f4fbSPablo de Lara 
13212661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
13222661f4fbSPablo de Lara 		return parse_size(&options->aead_iv_random_size, optarg);
13232661f4fbSPablo de Lara 
1324617a7949SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1325617a7949SPablo de Lara 		options->aad_param = 1;
1326a061e50aSPablo de Lara 		options->aad.length =
1327ff5d5b01SPablo de Lara 			parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1328a061e50aSPablo de Lara 		if (options->aad.length > 0)
1329a061e50aSPablo de Lara 			return 0;
1330a061e50aSPablo de Lara 		else
1331a061e50aSPablo de Lara 			return -1;
1332a061e50aSPablo de Lara 	}
1333a061e50aSPablo de Lara 
1334a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1335a061e50aSPablo de Lara 		return parse_size(&options->aad_random_size, optarg);
1336a061e50aSPablo de Lara 	}
1337a061e50aSPablo de Lara 
1338a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1339a061e50aSPablo de Lara 		return parse_size(&options->digest_size, optarg);
1340617a7949SPablo de Lara 	}
1341617a7949SPablo de Lara 
13421df9c010SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1343387259bdSDeclan Doherty 		options->sessionless = 1;
1344387259bdSDeclan Doherty 		return 0;
1345387259bdSDeclan Doherty 	}
1346387259bdSDeclan Doherty 
1347d2797f51SFan Zhang 	else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1348d2797f51SFan Zhang 		return parse_cryptodev_mask(options, optarg);
1349d2797f51SFan Zhang 
1350acdfecbaSKuba Kozak 	else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1351acdfecbaSKuba Kozak 		options->mac_updating = 1;
1352acdfecbaSKuba Kozak 		return 0;
1353acdfecbaSKuba Kozak 	}
1354acdfecbaSKuba Kozak 
1355acdfecbaSKuba Kozak 	else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1356acdfecbaSKuba Kozak 		options->mac_updating = 0;
1357acdfecbaSKuba Kozak 		return 0;
1358acdfecbaSKuba Kozak 	}
1359acdfecbaSKuba Kozak 
1360387259bdSDeclan Doherty 	return -1;
1361387259bdSDeclan Doherty }
1362387259bdSDeclan Doherty 
1363387259bdSDeclan Doherty /** Parse port mask */
1364387259bdSDeclan Doherty static int
1365387259bdSDeclan Doherty l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1366387259bdSDeclan Doherty 		const char *q_arg)
1367387259bdSDeclan Doherty {
1368387259bdSDeclan Doherty 	char *end = NULL;
1369387259bdSDeclan Doherty 	unsigned long pm;
1370387259bdSDeclan Doherty 
1371387259bdSDeclan Doherty 	/* parse hexadecimal string */
1372387259bdSDeclan Doherty 	pm = strtoul(q_arg, &end, 16);
1373387259bdSDeclan Doherty 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1374387259bdSDeclan Doherty 		pm = 0;
1375387259bdSDeclan Doherty 
1376387259bdSDeclan Doherty 	options->portmask = pm;
1377387259bdSDeclan Doherty 	if (options->portmask == 0) {
1378387259bdSDeclan Doherty 		printf("invalid portmask specified\n");
1379387259bdSDeclan Doherty 		return -1;
1380387259bdSDeclan Doherty 	}
1381387259bdSDeclan Doherty 
1382387259bdSDeclan Doherty 	return pm;
1383387259bdSDeclan Doherty }
1384387259bdSDeclan Doherty 
1385387259bdSDeclan Doherty /** Parse number of queues */
1386387259bdSDeclan Doherty static int
1387387259bdSDeclan Doherty l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1388387259bdSDeclan Doherty 		const char *q_arg)
1389387259bdSDeclan Doherty {
1390387259bdSDeclan Doherty 	char *end = NULL;
1391387259bdSDeclan Doherty 	unsigned long n;
1392387259bdSDeclan Doherty 
1393387259bdSDeclan Doherty 	/* parse hexadecimal string */
1394387259bdSDeclan Doherty 	n = strtoul(q_arg, &end, 10);
1395387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1396387259bdSDeclan Doherty 		n = 0;
1397387259bdSDeclan Doherty 	else if (n >= MAX_RX_QUEUE_PER_LCORE)
1398387259bdSDeclan Doherty 		n = 0;
1399387259bdSDeclan Doherty 
1400387259bdSDeclan Doherty 	options->nb_ports_per_lcore = n;
1401387259bdSDeclan Doherty 	if (options->nb_ports_per_lcore == 0) {
1402387259bdSDeclan Doherty 		printf("invalid number of ports selected\n");
1403387259bdSDeclan Doherty 		return -1;
1404387259bdSDeclan Doherty 	}
1405387259bdSDeclan Doherty 
1406387259bdSDeclan Doherty 	return 0;
1407387259bdSDeclan Doherty }
1408387259bdSDeclan Doherty 
1409387259bdSDeclan Doherty /** Parse timer period */
1410387259bdSDeclan Doherty static int
1411387259bdSDeclan Doherty l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1412387259bdSDeclan Doherty 		const char *q_arg)
1413387259bdSDeclan Doherty {
1414387259bdSDeclan Doherty 	char *end = NULL;
14153c96262cSPablo de Lara 	unsigned long n;
1416387259bdSDeclan Doherty 
1417387259bdSDeclan Doherty 	/* parse number string */
14183c96262cSPablo de Lara 	n = (unsigned)strtol(q_arg, &end, 10);
1419387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1420387259bdSDeclan Doherty 		n = 0;
1421387259bdSDeclan Doherty 
1422ad509b4aSDeclan Doherty 	if (n >= MAX_TIMER_PERIOD) {
14233c96262cSPablo de Lara 		printf("Warning refresh period specified %lu is greater than "
14243c96262cSPablo de Lara 				"max value %lu! using max value",
1425ad509b4aSDeclan Doherty 				n, MAX_TIMER_PERIOD);
1426ad509b4aSDeclan Doherty 		n = MAX_TIMER_PERIOD;
1427ad509b4aSDeclan Doherty 	}
1428387259bdSDeclan Doherty 
1429387259bdSDeclan Doherty 	options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1430387259bdSDeclan Doherty 
1431387259bdSDeclan Doherty 	return 0;
1432387259bdSDeclan Doherty }
1433387259bdSDeclan Doherty 
1434387259bdSDeclan Doherty /** Generate default options for application */
1435387259bdSDeclan Doherty static void
1436387259bdSDeclan Doherty l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1437387259bdSDeclan Doherty {
1438387259bdSDeclan Doherty 	options->portmask = 0xffffffff;
1439387259bdSDeclan Doherty 	options->nb_ports_per_lcore = 1;
1440387259bdSDeclan Doherty 	options->refresh_period = 10000;
1441387259bdSDeclan Doherty 	options->single_lcore = 0;
14423c96262cSPablo de Lara 	options->sessionless = 0;
1443387259bdSDeclan Doherty 
1444387259bdSDeclan Doherty 	options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1445387259bdSDeclan Doherty 
1446387259bdSDeclan Doherty 	/* Cipher Data */
14471bd407faSFiona Trahe 	options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1448387259bdSDeclan Doherty 	options->cipher_xform.next = NULL;
14491df9c010SPablo de Lara 	options->ckey_param = 0;
1450a061e50aSPablo de Lara 	options->ckey_random_size = -1;
1451a061e50aSPablo de Lara 	options->cipher_xform.cipher.key.length = 0;
1452acf86169SPablo de Lara 	options->cipher_iv_param = 0;
1453acf86169SPablo de Lara 	options->cipher_iv_random_size = -1;
1454acf86169SPablo de Lara 	options->cipher_iv.length = 0;
1455387259bdSDeclan Doherty 
1456387259bdSDeclan Doherty 	options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1457387259bdSDeclan Doherty 	options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1458387259bdSDeclan Doherty 
1459387259bdSDeclan Doherty 	/* Authentication Data */
14601bd407faSFiona Trahe 	options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1461387259bdSDeclan Doherty 	options->auth_xform.next = NULL;
14621df9c010SPablo de Lara 	options->akey_param = 0;
1463a061e50aSPablo de Lara 	options->akey_random_size = -1;
1464a061e50aSPablo de Lara 	options->auth_xform.auth.key.length = 0;
1465acf86169SPablo de Lara 	options->auth_iv_param = 0;
1466acf86169SPablo de Lara 	options->auth_iv_random_size = -1;
1467acf86169SPablo de Lara 	options->auth_iv.length = 0;
1468387259bdSDeclan Doherty 
1469387259bdSDeclan Doherty 	options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
147027cf2d1bSPablo de Lara 	options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1471387259bdSDeclan Doherty 
14722661f4fbSPablo de Lara 	/* AEAD Data */
14732661f4fbSPablo de Lara 	options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
14742661f4fbSPablo de Lara 	options->aead_xform.next = NULL;
14752661f4fbSPablo de Lara 	options->aead_key_param = 0;
14762661f4fbSPablo de Lara 	options->aead_key_random_size = -1;
14772661f4fbSPablo de Lara 	options->aead_xform.aead.key.length = 0;
14782661f4fbSPablo de Lara 	options->aead_iv_param = 0;
14792661f4fbSPablo de Lara 	options->aead_iv_random_size = -1;
14802661f4fbSPablo de Lara 	options->aead_iv.length = 0;
14812661f4fbSPablo de Lara 
1482d2c4b7d3SHemant Agrawal 	options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1483d2c4b7d3SHemant Agrawal 	options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1484b79e4c00SPablo de Lara 
14852661f4fbSPablo de Lara 	options->aad_param = 0;
14862661f4fbSPablo de Lara 	options->aad_random_size = -1;
14872661f4fbSPablo de Lara 	options->aad.length = 0;
14882661f4fbSPablo de Lara 
14892661f4fbSPablo de Lara 	options->digest_size = -1;
14902661f4fbSPablo de Lara 
149127cf2d1bSPablo de Lara 	options->type = CDEV_TYPE_ANY;
1492d2797f51SFan Zhang 	options->cryptodev_mask = UINT64_MAX;
1493acdfecbaSKuba Kozak 
1494acdfecbaSKuba Kozak 	options->mac_updating = 1;
1495387259bdSDeclan Doherty }
1496387259bdSDeclan Doherty 
1497387259bdSDeclan Doherty static void
149841e97c2eSPablo de Lara display_cipher_info(struct l2fwd_crypto_options *options)
149941e97c2eSPablo de Lara {
150041e97c2eSPablo de Lara 	printf("\n---- Cipher information ---\n");
150141e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
15024790f99dSPablo de Lara 		rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
150341e97c2eSPablo de Lara 	rte_hexdump(stdout, "Cipher key:",
150441e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.data,
150541e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.length);
1506acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
150741e97c2eSPablo de Lara }
150841e97c2eSPablo de Lara 
150941e97c2eSPablo de Lara static void
151041e97c2eSPablo de Lara display_auth_info(struct l2fwd_crypto_options *options)
151141e97c2eSPablo de Lara {
151241e97c2eSPablo de Lara 	printf("\n---- Authentication information ---\n");
151341e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
15144eb45d2dSPablo de Lara 		rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
151541e97c2eSPablo de Lara 	rte_hexdump(stdout, "Auth key:",
151641e97c2eSPablo de Lara 			options->auth_xform.auth.key.data,
151741e97c2eSPablo de Lara 			options->auth_xform.auth.key.length);
1518acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
15192661f4fbSPablo de Lara }
15202661f4fbSPablo de Lara 
15212661f4fbSPablo de Lara static void
15222661f4fbSPablo de Lara display_aead_info(struct l2fwd_crypto_options *options)
15232661f4fbSPablo de Lara {
15242661f4fbSPablo de Lara 	printf("\n---- AEAD information ---\n");
15252661f4fbSPablo de Lara 	printf("Algorithm: %s\n",
15262661f4fbSPablo de Lara 		rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
15272661f4fbSPablo de Lara 	rte_hexdump(stdout, "AEAD key:",
15282661f4fbSPablo de Lara 			options->aead_xform.aead.key.data,
15292661f4fbSPablo de Lara 			options->aead_xform.aead.key.length);
15302661f4fbSPablo de Lara 	rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
153141e97c2eSPablo de Lara 	rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
153241e97c2eSPablo de Lara }
153341e97c2eSPablo de Lara 
153441e97c2eSPablo de Lara static void
1535387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1536387259bdSDeclan Doherty {
153741e97c2eSPablo de Lara 	char string_cipher_op[MAX_STR_LEN];
153841e97c2eSPablo de Lara 	char string_auth_op[MAX_STR_LEN];
15392661f4fbSPablo de Lara 	char string_aead_op[MAX_STR_LEN];
154041e97c2eSPablo de Lara 
154141e97c2eSPablo de Lara 	if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
154241e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Encrypt");
154341e97c2eSPablo de Lara 	else
154441e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Decrypt");
154541e97c2eSPablo de Lara 
154641e97c2eSPablo de Lara 	if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
154741e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth generate");
154841e97c2eSPablo de Lara 	else
154941e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth verify");
155041e97c2eSPablo de Lara 
15512661f4fbSPablo de Lara 	if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
15522661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated encryption");
15532661f4fbSPablo de Lara 	else
15542661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated decryption");
15552661f4fbSPablo de Lara 
15562661f4fbSPablo de Lara 
1557387259bdSDeclan Doherty 	printf("Options:-\nn");
1558387259bdSDeclan Doherty 	printf("portmask: %x\n", options->portmask);
1559387259bdSDeclan Doherty 	printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1560387259bdSDeclan Doherty 	printf("refresh period : %u\n", options->refresh_period);
1561387259bdSDeclan Doherty 	printf("single lcore mode: %s\n",
1562387259bdSDeclan Doherty 			options->single_lcore ? "enabled" : "disabled");
1563387259bdSDeclan Doherty 	printf("stats_printing: %s\n",
1564ad509b4aSDeclan Doherty 			options->refresh_period == 0 ? "disabled" : "enabled");
1565387259bdSDeclan Doherty 
1566387259bdSDeclan Doherty 	printf("sessionless crypto: %s\n",
1567387259bdSDeclan Doherty 			options->sessionless ? "enabled" : "disabled");
156841e97c2eSPablo de Lara 
156941e97c2eSPablo de Lara 	if (options->ckey_param && (options->ckey_random_size != -1))
157041e97c2eSPablo de Lara 		printf("Cipher key already parsed, ignoring size of random key\n");
157141e97c2eSPablo de Lara 
157241e97c2eSPablo de Lara 	if (options->akey_param && (options->akey_random_size != -1))
157341e97c2eSPablo de Lara 		printf("Auth key already parsed, ignoring size of random key\n");
157441e97c2eSPablo de Lara 
1575acf86169SPablo de Lara 	if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1576acf86169SPablo de Lara 		printf("Cipher IV already parsed, ignoring size of random IV\n");
1577acf86169SPablo de Lara 
1578acf86169SPablo de Lara 	if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1579acf86169SPablo de Lara 		printf("Auth IV already parsed, ignoring size of random IV\n");
158041e97c2eSPablo de Lara 
158141e97c2eSPablo de Lara 	if (options->aad_param && (options->aad_random_size != -1))
158241e97c2eSPablo de Lara 		printf("AAD already parsed, ignoring size of random AAD\n");
158341e97c2eSPablo de Lara 
158441e97c2eSPablo de Lara 	printf("\nCrypto chain: ");
158541e97c2eSPablo de Lara 	switch (options->xform_chain) {
15862661f4fbSPablo de Lara 	case L2FWD_CRYPTO_AEAD:
15872661f4fbSPablo de Lara 		printf("Input --> %s --> Output\n", string_aead_op);
15882661f4fbSPablo de Lara 		display_aead_info(options);
15892661f4fbSPablo de Lara 		break;
159041e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_HASH:
159141e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
159241e97c2eSPablo de Lara 			string_cipher_op, string_auth_op);
159341e97c2eSPablo de Lara 		display_cipher_info(options);
159441e97c2eSPablo de Lara 		display_auth_info(options);
159541e97c2eSPablo de Lara 		break;
159641e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_CIPHER:
159741e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
159841e97c2eSPablo de Lara 			string_auth_op, string_cipher_op);
159941e97c2eSPablo de Lara 		display_cipher_info(options);
160041e97c2eSPablo de Lara 		display_auth_info(options);
160141e97c2eSPablo de Lara 		break;
160241e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_ONLY:
160341e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_auth_op);
160441e97c2eSPablo de Lara 		display_auth_info(options);
160541e97c2eSPablo de Lara 		break;
160641e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_ONLY:
160741e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_cipher_op);
160841e97c2eSPablo de Lara 		display_cipher_info(options);
160941e97c2eSPablo de Lara 		break;
161041e97c2eSPablo de Lara 	}
1611387259bdSDeclan Doherty }
1612387259bdSDeclan Doherty 
1613387259bdSDeclan Doherty /* Parse the argument given in the command line of the application */
1614387259bdSDeclan Doherty static int
1615387259bdSDeclan Doherty l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1616387259bdSDeclan Doherty 		int argc, char **argv)
1617387259bdSDeclan Doherty {
1618387259bdSDeclan Doherty 	int opt, retval, option_index;
1619387259bdSDeclan Doherty 	char **argvopt = argv, *prgname = argv[0];
1620387259bdSDeclan Doherty 
1621387259bdSDeclan Doherty 	static struct option lgopts[] = {
1622387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1623387259bdSDeclan Doherty 
1624387259bdSDeclan Doherty 			{ "cdev_type", required_argument, 0, 0 },
1625387259bdSDeclan Doherty 			{ "chain", required_argument, 0, 0 },
1626387259bdSDeclan Doherty 
1627387259bdSDeclan Doherty 			{ "cipher_algo", required_argument, 0, 0 },
1628387259bdSDeclan Doherty 			{ "cipher_op", required_argument, 0, 0 },
1629387259bdSDeclan Doherty 			{ "cipher_key", required_argument, 0, 0 },
1630a061e50aSPablo de Lara 			{ "cipher_key_random_size", required_argument, 0, 0 },
1631acf86169SPablo de Lara 			{ "cipher_iv", required_argument, 0, 0 },
1632acf86169SPablo de Lara 			{ "cipher_iv_random_size", required_argument, 0, 0 },
1633387259bdSDeclan Doherty 
1634387259bdSDeclan Doherty 			{ "auth_algo", required_argument, 0, 0 },
1635387259bdSDeclan Doherty 			{ "auth_op", required_argument, 0, 0 },
1636387259bdSDeclan Doherty 			{ "auth_key", required_argument, 0, 0 },
1637a061e50aSPablo de Lara 			{ "auth_key_random_size", required_argument, 0, 0 },
1638acf86169SPablo de Lara 			{ "auth_iv", required_argument, 0, 0 },
1639acf86169SPablo de Lara 			{ "auth_iv_random_size", required_argument, 0, 0 },
1640387259bdSDeclan Doherty 
16412661f4fbSPablo de Lara 			{ "aead_algo", required_argument, 0, 0 },
16422661f4fbSPablo de Lara 			{ "aead_op", required_argument, 0, 0 },
16432661f4fbSPablo de Lara 			{ "aead_key", required_argument, 0, 0 },
16442661f4fbSPablo de Lara 			{ "aead_key_random_size", required_argument, 0, 0 },
16452661f4fbSPablo de Lara 			{ "aead_iv", required_argument, 0, 0 },
16462661f4fbSPablo de Lara 			{ "aead_iv_random_size", required_argument, 0, 0 },
16472661f4fbSPablo de Lara 
1648617a7949SPablo de Lara 			{ "aad", required_argument, 0, 0 },
1649a061e50aSPablo de Lara 			{ "aad_random_size", required_argument, 0, 0 },
16502661f4fbSPablo de Lara 
1651a061e50aSPablo de Lara 			{ "digest_size", required_argument, 0, 0 },
1652387259bdSDeclan Doherty 
1653387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1654d2797f51SFan Zhang 			{ "cryptodev_mask", required_argument, 0, 0},
16553c96262cSPablo de Lara 
1656acdfecbaSKuba Kozak 			{ "mac-updating", no_argument, 0, 0},
1657acdfecbaSKuba Kozak 			{ "no-mac-updating", no_argument, 0, 0},
1658acdfecbaSKuba Kozak 
1659387259bdSDeclan Doherty 			{ NULL, 0, 0, 0 }
1660387259bdSDeclan Doherty 	};
1661387259bdSDeclan Doherty 
1662387259bdSDeclan Doherty 	l2fwd_crypto_default_options(options);
1663387259bdSDeclan Doherty 
1664f1ae15baSPablo de Lara 	while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1665387259bdSDeclan Doherty 			&option_index)) != EOF) {
1666387259bdSDeclan Doherty 		switch (opt) {
1667387259bdSDeclan Doherty 		/* long options */
1668387259bdSDeclan Doherty 		case 0:
1669387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_args_long_options(options,
1670387259bdSDeclan Doherty 					lgopts, option_index);
1671387259bdSDeclan Doherty 			if (retval < 0) {
1672387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1673387259bdSDeclan Doherty 				return -1;
1674387259bdSDeclan Doherty 			}
1675387259bdSDeclan Doherty 			break;
1676387259bdSDeclan Doherty 
1677387259bdSDeclan Doherty 		/* portmask */
1678387259bdSDeclan Doherty 		case 'p':
1679387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_portmask(options, optarg);
1680387259bdSDeclan Doherty 			if (retval < 0) {
1681387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1682387259bdSDeclan Doherty 				return -1;
1683387259bdSDeclan Doherty 			}
1684387259bdSDeclan Doherty 			break;
1685387259bdSDeclan Doherty 
1686387259bdSDeclan Doherty 		/* nqueue */
1687387259bdSDeclan Doherty 		case 'q':
1688387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_nqueue(options, optarg);
1689387259bdSDeclan Doherty 			if (retval < 0) {
1690387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1691387259bdSDeclan Doherty 				return -1;
1692387259bdSDeclan Doherty 			}
1693387259bdSDeclan Doherty 			break;
1694387259bdSDeclan Doherty 
1695387259bdSDeclan Doherty 		/* single  */
1696387259bdSDeclan Doherty 		case 's':
1697387259bdSDeclan Doherty 			options->single_lcore = 1;
1698387259bdSDeclan Doherty 
1699387259bdSDeclan Doherty 			break;
1700387259bdSDeclan Doherty 
1701387259bdSDeclan Doherty 		/* timer period */
1702a3380989SPablo de Lara 		case 'T':
1703387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_timer_period(options,
1704387259bdSDeclan Doherty 					optarg);
1705387259bdSDeclan Doherty 			if (retval < 0) {
1706387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1707387259bdSDeclan Doherty 				return -1;
1708387259bdSDeclan Doherty 			}
1709387259bdSDeclan Doherty 			break;
1710387259bdSDeclan Doherty 
1711387259bdSDeclan Doherty 		default:
1712387259bdSDeclan Doherty 			l2fwd_crypto_usage(prgname);
1713387259bdSDeclan Doherty 			return -1;
1714387259bdSDeclan Doherty 		}
1715387259bdSDeclan Doherty 	}
1716387259bdSDeclan Doherty 
1717387259bdSDeclan Doherty 
1718387259bdSDeclan Doherty 	if (optind >= 0)
1719387259bdSDeclan Doherty 		argv[optind-1] = prgname;
1720387259bdSDeclan Doherty 
1721387259bdSDeclan Doherty 	retval = optind-1;
17229d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
1723387259bdSDeclan Doherty 
1724387259bdSDeclan Doherty 	return retval;
1725387259bdSDeclan Doherty }
1726387259bdSDeclan Doherty 
1727387259bdSDeclan Doherty /* Check the link status of all ports in up to 9s, and print them finally */
1728387259bdSDeclan Doherty static void
17298728ccf3SThomas Monjalon check_all_ports_link_status(uint32_t port_mask)
1730387259bdSDeclan Doherty {
1731387259bdSDeclan Doherty #define CHECK_INTERVAL 100 /* 100ms */
1732387259bdSDeclan Doherty #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
173347523597SZhiyong Yang 	uint16_t portid;
173447523597SZhiyong Yang 	uint8_t count, all_ports_up, print_flag = 0;
1735387259bdSDeclan Doherty 	struct rte_eth_link link;
173622e5c73bSIgor Romanov 	int ret;
1737*db4e8135SIvan Dyukov 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1738387259bdSDeclan Doherty 
1739387259bdSDeclan Doherty 	printf("\nChecking link status");
1740387259bdSDeclan Doherty 	fflush(stdout);
1741387259bdSDeclan Doherty 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1742387259bdSDeclan Doherty 		all_ports_up = 1;
17438728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(portid) {
1744387259bdSDeclan Doherty 			if ((port_mask & (1 << portid)) == 0)
1745387259bdSDeclan Doherty 				continue;
1746387259bdSDeclan Doherty 			memset(&link, 0, sizeof(link));
174722e5c73bSIgor Romanov 			ret = rte_eth_link_get_nowait(portid, &link);
174822e5c73bSIgor Romanov 			if (ret < 0) {
174922e5c73bSIgor Romanov 				all_ports_up = 0;
175022e5c73bSIgor Romanov 				if (print_flag == 1)
175122e5c73bSIgor Romanov 					printf("Port %u link get failed: %s\n",
175222e5c73bSIgor Romanov 						portid, rte_strerror(-ret));
175322e5c73bSIgor Romanov 				continue;
175422e5c73bSIgor Romanov 			}
1755387259bdSDeclan Doherty 			/* print link status if flag set */
1756387259bdSDeclan Doherty 			if (print_flag == 1) {
1757*db4e8135SIvan Dyukov 				rte_eth_link_to_str(link_status_text,
1758*db4e8135SIvan Dyukov 					sizeof(link_status_text), &link);
1759*db4e8135SIvan Dyukov 				printf("Port %d %s\n", portid,
1760*db4e8135SIvan Dyukov 					link_status_text);
1761387259bdSDeclan Doherty 				continue;
1762387259bdSDeclan Doherty 			}
1763387259bdSDeclan Doherty 			/* clear all_ports_up flag if any link down */
176409419f23SThomas Monjalon 			if (link.link_status == ETH_LINK_DOWN) {
1765387259bdSDeclan Doherty 				all_ports_up = 0;
1766387259bdSDeclan Doherty 				break;
1767387259bdSDeclan Doherty 			}
1768387259bdSDeclan Doherty 		}
1769387259bdSDeclan Doherty 		/* after finally printing all link status, get out */
1770387259bdSDeclan Doherty 		if (print_flag == 1)
1771387259bdSDeclan Doherty 			break;
1772387259bdSDeclan Doherty 
1773387259bdSDeclan Doherty 		if (all_ports_up == 0) {
1774387259bdSDeclan Doherty 			printf(".");
1775387259bdSDeclan Doherty 			fflush(stdout);
1776387259bdSDeclan Doherty 			rte_delay_ms(CHECK_INTERVAL);
1777387259bdSDeclan Doherty 		}
1778387259bdSDeclan Doherty 
1779387259bdSDeclan Doherty 		/* set the print_flag if all ports up or timeout */
1780387259bdSDeclan Doherty 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1781387259bdSDeclan Doherty 			print_flag = 1;
1782387259bdSDeclan Doherty 			printf("done\n");
1783387259bdSDeclan Doherty 		}
1784387259bdSDeclan Doherty 	}
1785387259bdSDeclan Doherty }
1786387259bdSDeclan Doherty 
178727cf2d1bSPablo de Lara /* Check if device has to be HW/SW or any */
1788387259bdSDeclan Doherty static int
17894baea68dSPablo de Lara check_type(const struct l2fwd_crypto_options *options,
17904baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info)
179127cf2d1bSPablo de Lara {
179227cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_HW &&
179327cf2d1bSPablo de Lara 			(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
179427cf2d1bSPablo de Lara 		return 0;
179527cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_SW &&
179627cf2d1bSPablo de Lara 			!(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
179727cf2d1bSPablo de Lara 		return 0;
179827cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_ANY)
179927cf2d1bSPablo de Lara 		return 0;
180027cf2d1bSPablo de Lara 
180127cf2d1bSPablo de Lara 	return -1;
180227cf2d1bSPablo de Lara }
180327cf2d1bSPablo de Lara 
18044baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
18054baea68dSPablo de Lara check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
18064baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18074baea68dSPablo de Lara 		uint8_t cdev_id)
18084baea68dSPablo de Lara {
18094baea68dSPablo de Lara 	unsigned int i = 0;
18104baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18114baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm cap_cipher_algo;
18124baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm opt_cipher_algo =
18134baea68dSPablo de Lara 					options->cipher_xform.cipher.algo;
18144baea68dSPablo de Lara 
18154baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18164baea68dSPablo de Lara 		cap_cipher_algo = cap->sym.cipher.algo;
18174baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
18184baea68dSPablo de Lara 			if (cap_cipher_algo == opt_cipher_algo) {
18194baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
18204baea68dSPablo de Lara 					break;
18214baea68dSPablo de Lara 			}
18224baea68dSPablo de Lara 		}
18234baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
18244baea68dSPablo de Lara 	}
18254baea68dSPablo de Lara 
18264baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18274baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18284baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
18294baea68dSPablo de Lara 			rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
18304baea68dSPablo de Lara 			cdev_id,
18314baea68dSPablo de Lara 			options->string_type);
18324baea68dSPablo de Lara 		return NULL;
18334baea68dSPablo de Lara 	}
18344baea68dSPablo de Lara 
18354baea68dSPablo de Lara 	return cap;
18364baea68dSPablo de Lara }
18374baea68dSPablo de Lara 
18384baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
18394baea68dSPablo de Lara check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
18404baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18414baea68dSPablo de Lara 		uint8_t cdev_id)
18424baea68dSPablo de Lara {
18434baea68dSPablo de Lara 	unsigned int i = 0;
18444baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18454baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm cap_auth_algo;
18464baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm opt_auth_algo =
18474baea68dSPablo de Lara 					options->auth_xform.auth.algo;
18484baea68dSPablo de Lara 
18494baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18504baea68dSPablo de Lara 		cap_auth_algo = cap->sym.auth.algo;
18514baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
18524baea68dSPablo de Lara 			if (cap_auth_algo == opt_auth_algo) {
18534baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
18544baea68dSPablo de Lara 					break;
18554baea68dSPablo de Lara 			}
18564baea68dSPablo de Lara 		}
18574baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
18584baea68dSPablo de Lara 	}
18594baea68dSPablo de Lara 
18604baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18614baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18624baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
18634baea68dSPablo de Lara 			rte_crypto_auth_algorithm_strings[opt_auth_algo],
18644baea68dSPablo de Lara 			cdev_id,
18654baea68dSPablo de Lara 			options->string_type);
18664baea68dSPablo de Lara 		return NULL;
18674baea68dSPablo de Lara 	}
18684baea68dSPablo de Lara 
18694baea68dSPablo de Lara 	return cap;
18704baea68dSPablo de Lara }
18714baea68dSPablo de Lara 
18722661f4fbSPablo de Lara static const struct rte_cryptodev_capabilities *
18732661f4fbSPablo de Lara check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
18742661f4fbSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18752661f4fbSPablo de Lara 		uint8_t cdev_id)
18762661f4fbSPablo de Lara {
18772661f4fbSPablo de Lara 	unsigned int i = 0;
18782661f4fbSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18792661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm cap_aead_algo;
18802661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm opt_aead_algo =
18812661f4fbSPablo de Lara 					options->aead_xform.aead.algo;
18822661f4fbSPablo de Lara 
18832661f4fbSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18842661f4fbSPablo de Lara 		cap_aead_algo = cap->sym.aead.algo;
18852661f4fbSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
18862661f4fbSPablo de Lara 			if (cap_aead_algo == opt_aead_algo) {
18872661f4fbSPablo de Lara 				if (check_type(options, dev_info) == 0)
18882661f4fbSPablo de Lara 					break;
18892661f4fbSPablo de Lara 			}
18902661f4fbSPablo de Lara 		}
18912661f4fbSPablo de Lara 		cap = &dev_info->capabilities[++i];
18922661f4fbSPablo de Lara 	}
18932661f4fbSPablo de Lara 
18942661f4fbSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18952661f4fbSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18962661f4fbSPablo de Lara 			" or device not of preferred type (%s)\n",
18972661f4fbSPablo de Lara 			rte_crypto_aead_algorithm_strings[opt_aead_algo],
18982661f4fbSPablo de Lara 			cdev_id,
18992661f4fbSPablo de Lara 			options->string_type);
19002661f4fbSPablo de Lara 		return NULL;
19012661f4fbSPablo de Lara 	}
19022661f4fbSPablo de Lara 
19032661f4fbSPablo de Lara 	return cap;
19042661f4fbSPablo de Lara }
19052661f4fbSPablo de Lara 
1906d2797f51SFan Zhang /* Check if the device is enabled by cryptodev_mask */
1907d2797f51SFan Zhang static int
1908d2797f51SFan Zhang check_cryptodev_mask(struct l2fwd_crypto_options *options,
1909d2797f51SFan Zhang 		uint8_t cdev_id)
1910d2797f51SFan Zhang {
1911d2797f51SFan Zhang 	if (options->cryptodev_mask & (1 << cdev_id))
1912d2797f51SFan Zhang 		return 0;
1913d2797f51SFan Zhang 
1914d2797f51SFan Zhang 	return -1;
1915d2797f51SFan Zhang }
1916d2797f51SFan Zhang 
1917a061e50aSPablo de Lara static inline int
1918a061e50aSPablo de Lara check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1919a061e50aSPablo de Lara 		uint16_t increment)
1920a061e50aSPablo de Lara {
1921a061e50aSPablo de Lara 	uint16_t supp_size;
1922a061e50aSPablo de Lara 
192337ebd9e1SPablo de Lara 	/* Single value */
192437ebd9e1SPablo de Lara 	if (increment == 0) {
192537ebd9e1SPablo de Lara 		if (length == min)
192637ebd9e1SPablo de Lara 			return 0;
192737ebd9e1SPablo de Lara 		else
192837ebd9e1SPablo de Lara 			return -1;
192937ebd9e1SPablo de Lara 	}
193037ebd9e1SPablo de Lara 
193137ebd9e1SPablo de Lara 	/* Range of values */
1932a061e50aSPablo de Lara 	for (supp_size = min; supp_size <= max; supp_size += increment) {
1933a061e50aSPablo de Lara 		if (length == supp_size)
1934a061e50aSPablo de Lara 			return 0;
1935a061e50aSPablo de Lara 	}
1936a061e50aSPablo de Lara 
1937a061e50aSPablo de Lara 	return -1;
1938a061e50aSPablo de Lara }
19390fbd75a9SPablo de Lara 
19400fbd75a9SPablo de Lara static int
19410fbd75a9SPablo de Lara check_iv_param(const struct rte_crypto_param_range *iv_range_size,
19420fbd75a9SPablo de Lara 		unsigned int iv_param, int iv_random_size,
1943a6fde4f1SPablo de Lara 		uint16_t iv_length)
19440fbd75a9SPablo de Lara {
19450fbd75a9SPablo de Lara 	/*
19460fbd75a9SPablo de Lara 	 * Check if length of provided IV is supported
19470fbd75a9SPablo de Lara 	 * by the algorithm chosen.
19480fbd75a9SPablo de Lara 	 */
19490fbd75a9SPablo de Lara 	if (iv_param) {
1950a6fde4f1SPablo de Lara 		if (check_supported_size(iv_length,
19510fbd75a9SPablo de Lara 				iv_range_size->min,
19520fbd75a9SPablo de Lara 				iv_range_size->max,
19530fbd75a9SPablo de Lara 				iv_range_size->increment)
1954a6fde4f1SPablo de Lara 					!= 0)
19550fbd75a9SPablo de Lara 			return -1;
19560fbd75a9SPablo de Lara 	/*
19570fbd75a9SPablo de Lara 	 * Check if length of IV to be randomly generated
19580fbd75a9SPablo de Lara 	 * is supported by the algorithm chosen.
19590fbd75a9SPablo de Lara 	 */
19600fbd75a9SPablo de Lara 	} else if (iv_random_size != -1) {
19610fbd75a9SPablo de Lara 		if (check_supported_size(iv_random_size,
19620fbd75a9SPablo de Lara 				iv_range_size->min,
19630fbd75a9SPablo de Lara 				iv_range_size->max,
19640fbd75a9SPablo de Lara 				iv_range_size->increment)
1965a6fde4f1SPablo de Lara 					!= 0)
19660fbd75a9SPablo de Lara 			return -1;
19670fbd75a9SPablo de Lara 	}
19680fbd75a9SPablo de Lara 
19690fbd75a9SPablo de Lara 	return 0;
19700fbd75a9SPablo de Lara }
19710fbd75a9SPablo de Lara 
197227cf2d1bSPablo de Lara static int
19736ae3fb9dSPablo de Lara check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id)
19746ae3fb9dSPablo de Lara {
19756ae3fb9dSPablo de Lara 	struct rte_cryptodev_info dev_info;
19766ae3fb9dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
19776ae3fb9dSPablo de Lara 
19786ae3fb9dSPablo de Lara 	rte_cryptodev_info_get(cdev_id, &dev_info);
19796ae3fb9dSPablo de Lara 
19806ae3fb9dSPablo de Lara 	/* Set AEAD parameters */
19816ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
19826ae3fb9dSPablo de Lara 		/* Check if device supports AEAD algo */
19836ae3fb9dSPablo de Lara 		cap = check_device_support_aead_algo(options, &dev_info,
19846ae3fb9dSPablo de Lara 						cdev_id);
19856ae3fb9dSPablo de Lara 		if (cap == NULL)
19866ae3fb9dSPablo de Lara 			return -1;
19876ae3fb9dSPablo de Lara 
19886ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.aead.iv_size,
19896ae3fb9dSPablo de Lara 				options->aead_iv_param,
19906ae3fb9dSPablo de Lara 				options->aead_iv_random_size,
19916ae3fb9dSPablo de Lara 				options->aead_iv.length) != 0) {
19926ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
19936ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
19946ae3fb9dSPablo de Lara 				cdev_id);
19956ae3fb9dSPablo de Lara 			return -1;
19966ae3fb9dSPablo de Lara 		}
19976ae3fb9dSPablo de Lara 
19986ae3fb9dSPablo de Lara 		/*
19996ae3fb9dSPablo de Lara 		 * Check if length of provided AEAD key is supported
20006ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
20016ae3fb9dSPablo de Lara 		 */
20026ae3fb9dSPablo de Lara 		if (options->aead_key_param) {
20036ae3fb9dSPablo de Lara 			if (check_supported_size(
20046ae3fb9dSPablo de Lara 					options->aead_xform.aead.key.length,
20056ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.min,
20066ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.max,
20076ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.increment)
20086ae3fb9dSPablo de Lara 						!= 0) {
20096ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20106ae3fb9dSPablo de Lara 					"Device %u does not support "
20116ae3fb9dSPablo de Lara 					"AEAD key length\n",
20126ae3fb9dSPablo de Lara 					cdev_id);
20136ae3fb9dSPablo de Lara 				return -1;
20146ae3fb9dSPablo de Lara 			}
20156ae3fb9dSPablo de Lara 		/*
20166ae3fb9dSPablo de Lara 		 * Check if length of the aead key to be randomly generated
20176ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
20186ae3fb9dSPablo de Lara 		 */
20196ae3fb9dSPablo de Lara 		} else if (options->aead_key_random_size != -1) {
20206ae3fb9dSPablo de Lara 			if (check_supported_size(options->aead_key_random_size,
20216ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.min,
20226ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.max,
20236ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.increment)
20246ae3fb9dSPablo de Lara 						!= 0) {
20256ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20266ae3fb9dSPablo de Lara 					"Device %u does not support "
20276ae3fb9dSPablo de Lara 					"AEAD key length\n",
20286ae3fb9dSPablo de Lara 					cdev_id);
20296ae3fb9dSPablo de Lara 				return -1;
20306ae3fb9dSPablo de Lara 			}
20316ae3fb9dSPablo de Lara 		}
20326ae3fb9dSPablo de Lara 
20336ae3fb9dSPablo de Lara 
20346ae3fb9dSPablo de Lara 		/*
20356ae3fb9dSPablo de Lara 		 * Check if length of provided AAD is supported
20366ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
20376ae3fb9dSPablo de Lara 		 */
20386ae3fb9dSPablo de Lara 		if (options->aad_param) {
20396ae3fb9dSPablo de Lara 			if (check_supported_size(options->aad.length,
20406ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.min,
20416ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.max,
20426ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.increment)
20436ae3fb9dSPablo de Lara 						!= 0) {
20446ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20456ae3fb9dSPablo de Lara 					"Device %u does not support "
20466ae3fb9dSPablo de Lara 					"AAD length\n",
20476ae3fb9dSPablo de Lara 					cdev_id);
20486ae3fb9dSPablo de Lara 				return -1;
20496ae3fb9dSPablo de Lara 			}
20506ae3fb9dSPablo de Lara 		/*
20516ae3fb9dSPablo de Lara 		 * Check if length of AAD to be randomly generated
20526ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
20536ae3fb9dSPablo de Lara 		 */
20546ae3fb9dSPablo de Lara 		} else if (options->aad_random_size != -1) {
20556ae3fb9dSPablo de Lara 			if (check_supported_size(options->aad_random_size,
20566ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.min,
20576ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.max,
20586ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.increment)
20596ae3fb9dSPablo de Lara 						!= 0) {
20606ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20616ae3fb9dSPablo de Lara 					"Device %u does not support "
20626ae3fb9dSPablo de Lara 					"AAD length\n",
20636ae3fb9dSPablo de Lara 					cdev_id);
20646ae3fb9dSPablo de Lara 				return -1;
20656ae3fb9dSPablo de Lara 			}
20666ae3fb9dSPablo de Lara 		}
20676ae3fb9dSPablo de Lara 
20686ae3fb9dSPablo de Lara 		/* Check if digest size is supported by the algorithm. */
20696ae3fb9dSPablo de Lara 		if (options->digest_size != -1) {
20706ae3fb9dSPablo de Lara 			if (check_supported_size(options->digest_size,
20716ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.min,
20726ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.max,
20736ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.increment)
20746ae3fb9dSPablo de Lara 						!= 0) {
20756ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20766ae3fb9dSPablo de Lara 					"Device %u does not support "
20776ae3fb9dSPablo de Lara 					"digest length\n",
20786ae3fb9dSPablo de Lara 					cdev_id);
20796ae3fb9dSPablo de Lara 				return -1;
20806ae3fb9dSPablo de Lara 			}
20816ae3fb9dSPablo de Lara 		}
20826ae3fb9dSPablo de Lara 	}
20836ae3fb9dSPablo de Lara 
20846ae3fb9dSPablo de Lara 	/* Set cipher parameters */
20856ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
20866ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
20876ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
20886ae3fb9dSPablo de Lara 		/* Check if device supports cipher algo */
20896ae3fb9dSPablo de Lara 		cap = check_device_support_cipher_algo(options, &dev_info,
20906ae3fb9dSPablo de Lara 						cdev_id);
20916ae3fb9dSPablo de Lara 		if (cap == NULL)
20926ae3fb9dSPablo de Lara 			return -1;
20936ae3fb9dSPablo de Lara 
20946ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.cipher.iv_size,
20956ae3fb9dSPablo de Lara 				options->cipher_iv_param,
20966ae3fb9dSPablo de Lara 				options->cipher_iv_random_size,
20976ae3fb9dSPablo de Lara 				options->cipher_iv.length) != 0) {
20986ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
20996ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
21006ae3fb9dSPablo de Lara 				cdev_id);
21016ae3fb9dSPablo de Lara 			return -1;
21026ae3fb9dSPablo de Lara 		}
21036ae3fb9dSPablo de Lara 
21046ae3fb9dSPablo de Lara 		/*
21056ae3fb9dSPablo de Lara 		 * Check if length of provided cipher key is supported
21066ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
21076ae3fb9dSPablo de Lara 		 */
21086ae3fb9dSPablo de Lara 		if (options->ckey_param) {
21096ae3fb9dSPablo de Lara 			if (check_supported_size(
21106ae3fb9dSPablo de Lara 					options->cipher_xform.cipher.key.length,
21116ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.min,
21126ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.max,
21136ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.increment)
21146ae3fb9dSPablo de Lara 						!= 0) {
21156ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21166ae3fb9dSPablo de Lara 					"Device %u does not support cipher "
21176ae3fb9dSPablo de Lara 					"key length\n",
21186ae3fb9dSPablo de Lara 					cdev_id);
21196ae3fb9dSPablo de Lara 				return -1;
21206ae3fb9dSPablo de Lara 			}
21216ae3fb9dSPablo de Lara 		/*
21226ae3fb9dSPablo de Lara 		 * Check if length of the cipher key to be randomly generated
21236ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
21246ae3fb9dSPablo de Lara 		 */
21256ae3fb9dSPablo de Lara 		} else if (options->ckey_random_size != -1) {
21266ae3fb9dSPablo de Lara 			if (check_supported_size(options->ckey_random_size,
21276ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.min,
21286ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.max,
21296ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.increment)
21306ae3fb9dSPablo de Lara 						!= 0) {
21316ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21326ae3fb9dSPablo de Lara 					"Device %u does not support cipher "
21336ae3fb9dSPablo de Lara 					"key length\n",
21346ae3fb9dSPablo de Lara 					cdev_id);
21356ae3fb9dSPablo de Lara 				return -1;
21366ae3fb9dSPablo de Lara 			}
21376ae3fb9dSPablo de Lara 		}
21386ae3fb9dSPablo de Lara 	}
21396ae3fb9dSPablo de Lara 
21406ae3fb9dSPablo de Lara 	/* Set auth parameters */
21416ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
21426ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
21436ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
21446ae3fb9dSPablo de Lara 		/* Check if device supports auth algo */
21456ae3fb9dSPablo de Lara 		cap = check_device_support_auth_algo(options, &dev_info,
21466ae3fb9dSPablo de Lara 						cdev_id);
21476ae3fb9dSPablo de Lara 		if (cap == NULL)
21486ae3fb9dSPablo de Lara 			return -1;
21496ae3fb9dSPablo de Lara 
21506ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.auth.iv_size,
21516ae3fb9dSPablo de Lara 				options->auth_iv_param,
21526ae3fb9dSPablo de Lara 				options->auth_iv_random_size,
21536ae3fb9dSPablo de Lara 				options->auth_iv.length) != 0) {
21546ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
21556ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
21566ae3fb9dSPablo de Lara 				cdev_id);
21576ae3fb9dSPablo de Lara 			return -1;
21586ae3fb9dSPablo de Lara 		}
21596ae3fb9dSPablo de Lara 		/*
21606ae3fb9dSPablo de Lara 		 * Check if length of provided auth key is supported
21616ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
21626ae3fb9dSPablo de Lara 		 */
21636ae3fb9dSPablo de Lara 		if (options->akey_param) {
21646ae3fb9dSPablo de Lara 			if (check_supported_size(
21656ae3fb9dSPablo de Lara 					options->auth_xform.auth.key.length,
21666ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.min,
21676ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.max,
21686ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.increment)
21696ae3fb9dSPablo de Lara 						!= 0) {
21706ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21716ae3fb9dSPablo de Lara 					"Device %u does not support auth "
21726ae3fb9dSPablo de Lara 					"key length\n",
21736ae3fb9dSPablo de Lara 					cdev_id);
21746ae3fb9dSPablo de Lara 				return -1;
21756ae3fb9dSPablo de Lara 			}
21766ae3fb9dSPablo de Lara 		/*
21776ae3fb9dSPablo de Lara 		 * Check if length of the auth key to be randomly generated
21786ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
21796ae3fb9dSPablo de Lara 		 */
21806ae3fb9dSPablo de Lara 		} else if (options->akey_random_size != -1) {
21816ae3fb9dSPablo de Lara 			if (check_supported_size(options->akey_random_size,
21826ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.min,
21836ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.max,
21846ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.increment)
21856ae3fb9dSPablo de Lara 						!= 0) {
21866ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21876ae3fb9dSPablo de Lara 					"Device %u does not support auth "
21886ae3fb9dSPablo de Lara 					"key length\n",
21896ae3fb9dSPablo de Lara 					cdev_id);
21906ae3fb9dSPablo de Lara 				return -1;
21916ae3fb9dSPablo de Lara 			}
21926ae3fb9dSPablo de Lara 		}
21936ae3fb9dSPablo de Lara 
21946ae3fb9dSPablo de Lara 		/* Check if digest size is supported by the algorithm. */
21956ae3fb9dSPablo de Lara 		if (options->digest_size != -1) {
21966ae3fb9dSPablo de Lara 			if (check_supported_size(options->digest_size,
21976ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.min,
21986ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.max,
21996ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.increment)
22006ae3fb9dSPablo de Lara 						!= 0) {
22016ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
22026ae3fb9dSPablo de Lara 					"Device %u does not support "
22036ae3fb9dSPablo de Lara 					"digest length\n",
22046ae3fb9dSPablo de Lara 					cdev_id);
22056ae3fb9dSPablo de Lara 				return -1;
22066ae3fb9dSPablo de Lara 			}
22076ae3fb9dSPablo de Lara 		}
22086ae3fb9dSPablo de Lara 	}
22096ae3fb9dSPablo de Lara 
22106ae3fb9dSPablo de Lara 	return 0;
22116ae3fb9dSPablo de Lara }
22126ae3fb9dSPablo de Lara 
22136ae3fb9dSPablo de Lara static int
221427cf2d1bSPablo de Lara initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
221527cf2d1bSPablo de Lara 		uint8_t *enabled_cdevs)
2216387259bdSDeclan Doherty {
22176ae3fb9dSPablo de Lara 	uint8_t cdev_id, cdev_count, enabled_cdev_count = 0;
221827cf2d1bSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
22192c59bd32SSlawomir Mrozowicz 	unsigned int sess_sz, max_sess_sz = 0;
2220e3bcb99aSPablo de Lara 	uint32_t sessions_needed = 0;
2221387259bdSDeclan Doherty 	int retval;
2222387259bdSDeclan Doherty 
222327cf2d1bSPablo de Lara 	cdev_count = rte_cryptodev_count();
222427cf2d1bSPablo de Lara 	if (cdev_count == 0) {
222527cf2d1bSPablo de Lara 		printf("No crypto devices available\n");
2226387259bdSDeclan Doherty 		return -1;
2227387259bdSDeclan Doherty 	}
2228387259bdSDeclan Doherty 
22296ae3fb9dSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
22306ae3fb9dSPablo de Lara 			cdev_id++) {
22316ae3fb9dSPablo de Lara 		if (check_cryptodev_mask(options, cdev_id) < 0)
22326ae3fb9dSPablo de Lara 			continue;
22336ae3fb9dSPablo de Lara 
22346ae3fb9dSPablo de Lara 		if (check_capabilities(options, cdev_id) < 0)
22356ae3fb9dSPablo de Lara 			continue;
22366ae3fb9dSPablo de Lara 
2237a106fcceSPablo de Lara 		sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
22382c59bd32SSlawomir Mrozowicz 		if (sess_sz > max_sess_sz)
22392c59bd32SSlawomir Mrozowicz 			max_sess_sz = sess_sz;
22406ae3fb9dSPablo de Lara 
22416ae3fb9dSPablo de Lara 		l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
22426ae3fb9dSPablo de Lara 
22436ae3fb9dSPablo de Lara 		enabled_cdevs[cdev_id] = 1;
22446ae3fb9dSPablo de Lara 		enabled_cdev_count++;
22452c59bd32SSlawomir Mrozowicz 	}
22462c59bd32SSlawomir Mrozowicz 
22476ae3fb9dSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2248387259bdSDeclan Doherty 		struct rte_cryptodev_qp_conf qp_conf;
2249387259bdSDeclan Doherty 		struct rte_cryptodev_info dev_info;
22506ae3fb9dSPablo de Lara 
22516ae3fb9dSPablo de Lara 		if (enabled_cdevs[cdev_id] == 0)
22526ae3fb9dSPablo de Lara 			continue;
22536ae3fb9dSPablo de Lara 
22548dbc9bbfSPablo de Lara 		retval = rte_cryptodev_socket_id(cdev_id);
22558dbc9bbfSPablo de Lara 
22568dbc9bbfSPablo de Lara 		if (retval < 0) {
22578dbc9bbfSPablo de Lara 			printf("Invalid crypto device id used\n");
22588dbc9bbfSPablo de Lara 			return -1;
22598dbc9bbfSPablo de Lara 		}
22608dbc9bbfSPablo de Lara 
22618dbc9bbfSPablo de Lara 		uint8_t socket_id = (uint8_t) retval;
2262387259bdSDeclan Doherty 
2263387259bdSDeclan Doherty 		struct rte_cryptodev_config conf = {
2264387259bdSDeclan Doherty 			.nb_queue_pairs = 1,
22652c59bd32SSlawomir Mrozowicz 			.socket_id = socket_id,
2266c9030ae3SAnoob Joseph 			.ff_disable = RTE_CRYPTODEV_FF_SECURITY,
2267387259bdSDeclan Doherty 		};
2268387259bdSDeclan Doherty 
2269387259bdSDeclan Doherty 		rte_cryptodev_info_get(cdev_id, &dev_info);
2270387259bdSDeclan Doherty 
2271e3bcb99aSPablo de Lara 		/*
2272e3bcb99aSPablo de Lara 		 * Two sessions objects are required for each session
2273e3bcb99aSPablo de Lara 		 * (one for the header, one for the private data)
2274e3bcb99aSPablo de Lara 		 */
2275e3bcb99aSPablo de Lara 		if (!strcmp(dev_info.driver_name, "crypto_scheduler")) {
2276e3bcb99aSPablo de Lara #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
2277e3bcb99aSPablo de Lara 			uint32_t nb_slaves =
2278e3bcb99aSPablo de Lara 				rte_cryptodev_scheduler_slaves_get(cdev_id,
2279e3bcb99aSPablo de Lara 								NULL);
2280e3bcb99aSPablo de Lara 
2281261bbff7SFan Zhang 			sessions_needed = enabled_cdev_count * nb_slaves;
2282e3bcb99aSPablo de Lara #endif
2283e3bcb99aSPablo de Lara 		} else
2284261bbff7SFan Zhang 			sessions_needed = enabled_cdev_count;
2285e3bcb99aSPablo de Lara 
2286261bbff7SFan Zhang 		if (session_pool_socket[socket_id].priv_mp == NULL) {
22872c59bd32SSlawomir Mrozowicz 			char mp_name[RTE_MEMPOOL_NAMESIZE];
22882c59bd32SSlawomir Mrozowicz 
22892c59bd32SSlawomir Mrozowicz 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2290261bbff7SFan Zhang 				"priv_sess_mp_%u", socket_id);
22912c59bd32SSlawomir Mrozowicz 
2292261bbff7SFan Zhang 			session_pool_socket[socket_id].priv_mp =
2293261bbff7SFan Zhang 					rte_mempool_create(mp_name,
2294e3bcb99aSPablo de Lara 						sessions_needed,
22952c59bd32SSlawomir Mrozowicz 						max_sess_sz,
2296261bbff7SFan Zhang 						0, 0, NULL, NULL, NULL,
22972c59bd32SSlawomir Mrozowicz 						NULL, socket_id,
22982c59bd32SSlawomir Mrozowicz 						0);
22992c59bd32SSlawomir Mrozowicz 
2300261bbff7SFan Zhang 			if (session_pool_socket[socket_id].priv_mp == NULL) {
2301261bbff7SFan Zhang 				printf("Cannot create pool on socket %d\n",
23022c59bd32SSlawomir Mrozowicz 					socket_id);
23032c59bd32SSlawomir Mrozowicz 				return -ENOMEM;
23042c59bd32SSlawomir Mrozowicz 			}
23052c59bd32SSlawomir Mrozowicz 
2306261bbff7SFan Zhang 			printf("Allocated pool \"%s\" on socket %d\n",
2307261bbff7SFan Zhang 				mp_name, socket_id);
2308261bbff7SFan Zhang 		}
2309261bbff7SFan Zhang 
2310261bbff7SFan Zhang 		if (session_pool_socket[socket_id].sess_mp == NULL) {
2311261bbff7SFan Zhang 			char mp_name[RTE_MEMPOOL_NAMESIZE];
2312261bbff7SFan Zhang 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2313261bbff7SFan Zhang 				"sess_mp_%u", socket_id);
2314261bbff7SFan Zhang 
2315261bbff7SFan Zhang 			session_pool_socket[socket_id].sess_mp =
2316261bbff7SFan Zhang 					rte_cryptodev_sym_session_pool_create(
2317261bbff7SFan Zhang 							mp_name,
2318261bbff7SFan Zhang 							sessions_needed,
2319261bbff7SFan Zhang 							0, 0, 0, socket_id);
2320261bbff7SFan Zhang 
2321261bbff7SFan Zhang 			if (session_pool_socket[socket_id].sess_mp == NULL) {
2322261bbff7SFan Zhang 				printf("Cannot create pool on socket %d\n",
2323261bbff7SFan Zhang 					socket_id);
2324261bbff7SFan Zhang 				return -ENOMEM;
2325261bbff7SFan Zhang 			}
2326261bbff7SFan Zhang 
2327261bbff7SFan Zhang 			printf("Allocated pool \"%s\" on socket %d\n",
2328261bbff7SFan Zhang 				mp_name, socket_id);
23292c59bd32SSlawomir Mrozowicz 		}
23302c59bd32SSlawomir Mrozowicz 
23312661f4fbSPablo de Lara 		/* Set AEAD parameters */
23322661f4fbSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
23332661f4fbSPablo de Lara 			cap = check_device_support_aead_algo(options, &dev_info,
23342661f4fbSPablo de Lara 							cdev_id);
23352661f4fbSPablo de Lara 
23362661f4fbSPablo de Lara 			options->block_size = cap->sym.aead.block_size;
23372661f4fbSPablo de Lara 
2338a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2339a6fde4f1SPablo de Lara 			if (options->aead_iv_param == 0) {
2340a6fde4f1SPablo de Lara 				if (options->aead_iv_random_size != -1)
2341a6fde4f1SPablo de Lara 					options->aead_iv.length =
2342a6fde4f1SPablo de Lara 						options->aead_iv_random_size;
2343a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2344a6fde4f1SPablo de Lara 				else
2345a6fde4f1SPablo de Lara 					options->aead_iv.length =
2346a6fde4f1SPablo de Lara 						cap->sym.aead.iv_size.min;
2347a6fde4f1SPablo de Lara 			}
23482661f4fbSPablo de Lara 
23490b920a5fSPablo de Lara 			/* Set key if not provided from command line */
23500b920a5fSPablo de Lara 			if (options->aead_key_param == 0) {
23510b920a5fSPablo de Lara 				if (options->aead_key_random_size != -1)
23522661f4fbSPablo de Lara 					options->aead_xform.aead.key.length =
2353459369fcSPablo de Lara 						options->aead_key_random_size;
23542661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
23550b920a5fSPablo de Lara 				else
23562661f4fbSPablo de Lara 					options->aead_xform.aead.key.length =
23572661f4fbSPablo de Lara 						cap->sym.aead.key_size.min;
23582661f4fbSPablo de Lara 
2359186b14d6SFan Zhang 				generate_random_key(options->aead_key,
23602661f4fbSPablo de Lara 					options->aead_xform.aead.key.length);
23610b920a5fSPablo de Lara 			}
23622661f4fbSPablo de Lara 
23630b920a5fSPablo de Lara 			/* Set AAD if not provided from command line */
23640b920a5fSPablo de Lara 			if (options->aad_param == 0) {
23650b920a5fSPablo de Lara 				if (options->aad_random_size != -1)
23660b920a5fSPablo de Lara 					options->aad.length =
23670b920a5fSPablo de Lara 						options->aad_random_size;
23682661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
23690b920a5fSPablo de Lara 				else
23700b920a5fSPablo de Lara 					options->aad.length =
23710b920a5fSPablo de Lara 						cap->sym.auth.aad_size.min;
23720b920a5fSPablo de Lara 			}
23732661f4fbSPablo de Lara 
237446a0547fSPablo de Lara 			options->aead_xform.aead.aad_length =
23752661f4fbSPablo de Lara 						options->aad.length;
23762661f4fbSPablo de Lara 
23770b920a5fSPablo de Lara 			/* Set digest size if not provided from command line */
23780b920a5fSPablo de Lara 			if (options->digest_size != -1)
23792661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
23802661f4fbSPablo de Lara 							options->digest_size;
23812661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
23820b920a5fSPablo de Lara 			else
23832661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
23842661f4fbSPablo de Lara 						cap->sym.aead.digest_size.min;
23852661f4fbSPablo de Lara 		}
23862661f4fbSPablo de Lara 
238727cf2d1bSPablo de Lara 		/* Set cipher parameters */
238827cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
238927cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
239027cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
23914baea68dSPablo de Lara 			cap = check_device_support_cipher_algo(options, &dev_info,
23924baea68dSPablo de Lara 							cdev_id);
239327cf2d1bSPablo de Lara 			options->block_size = cap->sym.cipher.block_size;
23940fbd75a9SPablo de Lara 
2395a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2396a6fde4f1SPablo de Lara 			if (options->cipher_iv_param == 0) {
2397a6fde4f1SPablo de Lara 				if (options->cipher_iv_random_size != -1)
2398a6fde4f1SPablo de Lara 					options->cipher_iv.length =
2399a6fde4f1SPablo de Lara 						options->cipher_iv_random_size;
2400a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2401a6fde4f1SPablo de Lara 				else
2402a6fde4f1SPablo de Lara 					options->cipher_iv.length =
2403a6fde4f1SPablo de Lara 						cap->sym.cipher.iv_size.min;
2404a6fde4f1SPablo de Lara 			}
2405a061e50aSPablo de Lara 
24060b920a5fSPablo de Lara 			/* Set key if not provided from command line */
24070b920a5fSPablo de Lara 			if (options->ckey_param == 0) {
24080b920a5fSPablo de Lara 				if (options->ckey_random_size != -1)
2409a061e50aSPablo de Lara 					options->cipher_xform.cipher.key.length =
2410a061e50aSPablo de Lara 						options->ckey_random_size;
2411a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
24120b920a5fSPablo de Lara 				else
241327cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length =
241427cf2d1bSPablo de Lara 						cap->sym.cipher.key_size.min;
2415a061e50aSPablo de Lara 
2416186b14d6SFan Zhang 				generate_random_key(options->cipher_key,
241727cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length);
24180b920a5fSPablo de Lara 			}
241927cf2d1bSPablo de Lara 		}
242027cf2d1bSPablo de Lara 
242127cf2d1bSPablo de Lara 		/* Set auth parameters */
242227cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
242327cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
242427cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
24254baea68dSPablo de Lara 			cap = check_device_support_auth_algo(options, &dev_info,
24264baea68dSPablo de Lara 							cdev_id);
2427a6fde4f1SPablo de Lara 
2428a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2429a6fde4f1SPablo de Lara 			if (options->auth_iv_param == 0) {
2430a6fde4f1SPablo de Lara 				if (options->auth_iv_random_size != -1)
2431a6fde4f1SPablo de Lara 					options->auth_iv.length =
2432a6fde4f1SPablo de Lara 						options->auth_iv_random_size;
2433a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2434a6fde4f1SPablo de Lara 				else
2435a6fde4f1SPablo de Lara 					options->auth_iv.length =
2436a6fde4f1SPablo de Lara 						cap->sym.auth.iv_size.min;
2437a6fde4f1SPablo de Lara 			}
2438a6fde4f1SPablo de Lara 
24390b920a5fSPablo de Lara 			/* Set key if not provided from command line */
24400b920a5fSPablo de Lara 			if (options->akey_param == 0) {
24410b920a5fSPablo de Lara 				if (options->akey_random_size != -1)
2442a061e50aSPablo de Lara 					options->auth_xform.auth.key.length =
2443a061e50aSPablo de Lara 						options->akey_random_size;
2444a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
24450b920a5fSPablo de Lara 				else
244627cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length =
244727cf2d1bSPablo de Lara 						cap->sym.auth.key_size.min;
244827cf2d1bSPablo de Lara 
2449186b14d6SFan Zhang 				generate_random_key(options->auth_key,
245027cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length);
2451a061e50aSPablo de Lara 			}
24520b920a5fSPablo de Lara 
24530b920a5fSPablo de Lara 			/* Set digest size if not provided from command line */
24540b920a5fSPablo de Lara 			if (options->digest_size != -1)
2455a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2456a061e50aSPablo de Lara 							options->digest_size;
2457a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
24580b920a5fSPablo de Lara 			else
2459a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2460a061e50aSPablo de Lara 						cap->sym.auth.digest_size.min;
246127cf2d1bSPablo de Lara 		}
2462387259bdSDeclan Doherty 
2463f7db6f82SPablo de Lara 		retval = rte_cryptodev_configure(cdev_id, &conf);
2464387259bdSDeclan Doherty 		if (retval < 0) {
2465387259bdSDeclan Doherty 			printf("Failed to configure cryptodev %u", cdev_id);
2466387259bdSDeclan Doherty 			return -1;
2467387259bdSDeclan Doherty 		}
2468387259bdSDeclan Doherty 
2469387259bdSDeclan Doherty 		qp_conf.nb_descriptors = 2048;
2470261bbff7SFan Zhang 		qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
2471261bbff7SFan Zhang 		qp_conf.mp_session_private =
2472261bbff7SFan Zhang 				session_pool_socket[socket_id].priv_mp;
2473387259bdSDeclan Doherty 
2474387259bdSDeclan Doherty 		retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2475725d2a7fSFan Zhang 				socket_id);
2476387259bdSDeclan Doherty 		if (retval < 0) {
2477387259bdSDeclan Doherty 			printf("Failed to setup queue pair %u on cryptodev %u",
2478387259bdSDeclan Doherty 					0, cdev_id);
2479387259bdSDeclan Doherty 			return -1;
2480387259bdSDeclan Doherty 		}
2481387259bdSDeclan Doherty 
2482800386e6SHemant Agrawal 		retval = rte_cryptodev_start(cdev_id);
2483800386e6SHemant Agrawal 		if (retval < 0) {
2484800386e6SHemant Agrawal 			printf("Failed to start device %u: error %d\n",
2485800386e6SHemant Agrawal 					cdev_id, retval);
2486800386e6SHemant Agrawal 			return -1;
2487800386e6SHemant Agrawal 		}
2488387259bdSDeclan Doherty 	}
2489387259bdSDeclan Doherty 
2490387259bdSDeclan Doherty 	return enabled_cdev_count;
2491387259bdSDeclan Doherty }
2492387259bdSDeclan Doherty 
2493387259bdSDeclan Doherty static int
2494387259bdSDeclan Doherty initialize_ports(struct l2fwd_crypto_options *options)
2495387259bdSDeclan Doherty {
24968728ccf3SThomas Monjalon 	uint16_t last_portid = 0, portid;
2497387259bdSDeclan Doherty 	unsigned enabled_portcount = 0;
2498d9a42a69SThomas Monjalon 	unsigned nb_ports = rte_eth_dev_count_avail();
2499387259bdSDeclan Doherty 
2500387259bdSDeclan Doherty 	if (nb_ports == 0) {
2501387259bdSDeclan Doherty 		printf("No Ethernet ports - bye\n");
2502387259bdSDeclan Doherty 		return -1;
2503387259bdSDeclan Doherty 	}
2504387259bdSDeclan Doherty 
2505387259bdSDeclan Doherty 	/* Reset l2fwd_dst_ports */
2506387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2507387259bdSDeclan Doherty 		l2fwd_dst_ports[portid] = 0;
2508387259bdSDeclan Doherty 
25098728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
2510387259bdSDeclan Doherty 		int retval;
2511f2b713e3SShahaf Shuler 		struct rte_eth_dev_info dev_info;
2512f2b713e3SShahaf Shuler 		struct rte_eth_rxconf rxq_conf;
2513f2b713e3SShahaf Shuler 		struct rte_eth_txconf txq_conf;
2514f2b713e3SShahaf Shuler 		struct rte_eth_conf local_port_conf = port_conf;
2515387259bdSDeclan Doherty 
2516387259bdSDeclan Doherty 		/* Skip ports that are not enabled */
2517387259bdSDeclan Doherty 		if ((options->portmask & (1 << portid)) == 0)
2518387259bdSDeclan Doherty 			continue;
2519387259bdSDeclan Doherty 
2520387259bdSDeclan Doherty 		/* init port */
2521e2cdfbd0SZhiyong Yang 		printf("Initializing port %u... ", portid);
2522387259bdSDeclan Doherty 		fflush(stdout);
2523089e5ed7SIvan Ilchenko 
2524089e5ed7SIvan Ilchenko 		retval = rte_eth_dev_info_get(portid, &dev_info);
2525089e5ed7SIvan Ilchenko 		if (retval != 0) {
2526089e5ed7SIvan Ilchenko 			printf("Error during getting device (port %u) info: %s\n",
2527089e5ed7SIvan Ilchenko 					portid, strerror(-retval));
2528089e5ed7SIvan Ilchenko 			return retval;
2529089e5ed7SIvan Ilchenko 		}
2530089e5ed7SIvan Ilchenko 
2531f2b713e3SShahaf Shuler 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2532f2b713e3SShahaf Shuler 			local_port_conf.txmode.offloads |=
2533f2b713e3SShahaf Shuler 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2534f2b713e3SShahaf Shuler 		retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2535387259bdSDeclan Doherty 		if (retval < 0) {
2536387259bdSDeclan Doherty 			printf("Cannot configure device: err=%d, port=%u\n",
2537e2cdfbd0SZhiyong Yang 				  retval, portid);
2538387259bdSDeclan Doherty 			return -1;
2539387259bdSDeclan Doherty 		}
2540387259bdSDeclan Doherty 
254160efb44fSRoman Zhukov 		retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
254260efb44fSRoman Zhukov 							  &nb_txd);
254360efb44fSRoman Zhukov 		if (retval < 0) {
254460efb44fSRoman Zhukov 			printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2545e2cdfbd0SZhiyong Yang 				retval, portid);
254660efb44fSRoman Zhukov 			return -1;
254760efb44fSRoman Zhukov 		}
254860efb44fSRoman Zhukov 
2549387259bdSDeclan Doherty 		/* init one RX queue */
2550387259bdSDeclan Doherty 		fflush(stdout);
2551f2b713e3SShahaf Shuler 		rxq_conf = dev_info.default_rxconf;
2552f2b713e3SShahaf Shuler 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
2553387259bdSDeclan Doherty 		retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2554387259bdSDeclan Doherty 					     rte_eth_dev_socket_id(portid),
2555f2b713e3SShahaf Shuler 					     &rxq_conf, l2fwd_pktmbuf_pool);
2556387259bdSDeclan Doherty 		if (retval < 0) {
2557387259bdSDeclan Doherty 			printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2558e2cdfbd0SZhiyong Yang 					retval, portid);
2559387259bdSDeclan Doherty 			return -1;
2560387259bdSDeclan Doherty 		}
2561387259bdSDeclan Doherty 
2562387259bdSDeclan Doherty 		/* init one TX queue on each port */
2563387259bdSDeclan Doherty 		fflush(stdout);
2564f2b713e3SShahaf Shuler 		txq_conf = dev_info.default_txconf;
2565f2b713e3SShahaf Shuler 		txq_conf.offloads = local_port_conf.txmode.offloads;
2566387259bdSDeclan Doherty 		retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2567387259bdSDeclan Doherty 				rte_eth_dev_socket_id(portid),
2568f2b713e3SShahaf Shuler 				&txq_conf);
2569387259bdSDeclan Doherty 		if (retval < 0) {
2570387259bdSDeclan Doherty 			printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2571e2cdfbd0SZhiyong Yang 				retval, portid);
2572387259bdSDeclan Doherty 
2573387259bdSDeclan Doherty 			return -1;
2574387259bdSDeclan Doherty 		}
2575387259bdSDeclan Doherty 
2576387259bdSDeclan Doherty 		/* Start device */
2577387259bdSDeclan Doherty 		retval = rte_eth_dev_start(portid);
2578387259bdSDeclan Doherty 		if (retval < 0) {
2579387259bdSDeclan Doherty 			printf("rte_eth_dev_start:err=%d, port=%u\n",
2580e2cdfbd0SZhiyong Yang 					retval, portid);
2581387259bdSDeclan Doherty 			return -1;
2582387259bdSDeclan Doherty 		}
2583387259bdSDeclan Doherty 
2584f430bbceSIvan Ilchenko 		retval = rte_eth_promiscuous_enable(portid);
2585f430bbceSIvan Ilchenko 		if (retval != 0) {
2586f430bbceSIvan Ilchenko 			printf("rte_eth_promiscuous_enable:err=%s, port=%u\n",
2587f430bbceSIvan Ilchenko 				rte_strerror(-retval), portid);
2588f430bbceSIvan Ilchenko 			return -1;
2589f430bbceSIvan Ilchenko 		}
2590387259bdSDeclan Doherty 
259170febdcfSIgor Romanov 		retval = rte_eth_macaddr_get(portid,
259270febdcfSIgor Romanov 					     &l2fwd_ports_eth_addr[portid]);
259370febdcfSIgor Romanov 		if (retval < 0) {
259470febdcfSIgor Romanov 			printf("rte_eth_macaddr_get :err=%d, port=%u\n",
259570febdcfSIgor Romanov 					retval, portid);
259670febdcfSIgor Romanov 			return -1;
259770febdcfSIgor Romanov 		}
2598387259bdSDeclan Doherty 
2599387259bdSDeclan Doherty 		printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2600e2cdfbd0SZhiyong Yang 				portid,
2601387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[0],
2602387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[1],
2603387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[2],
2604387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[3],
2605387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[4],
2606387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2607387259bdSDeclan Doherty 
2608387259bdSDeclan Doherty 		/* initialize port stats */
2609387259bdSDeclan Doherty 		memset(&port_statistics, 0, sizeof(port_statistics));
2610387259bdSDeclan Doherty 
2611387259bdSDeclan Doherty 		/* Setup port forwarding table */
2612387259bdSDeclan Doherty 		if (enabled_portcount % 2) {
2613387259bdSDeclan Doherty 			l2fwd_dst_ports[portid] = last_portid;
2614387259bdSDeclan Doherty 			l2fwd_dst_ports[last_portid] = portid;
2615387259bdSDeclan Doherty 		} else {
2616387259bdSDeclan Doherty 			last_portid = portid;
2617387259bdSDeclan Doherty 		}
2618387259bdSDeclan Doherty 
2619387259bdSDeclan Doherty 		l2fwd_enabled_port_mask |= (1 << portid);
2620387259bdSDeclan Doherty 		enabled_portcount++;
2621387259bdSDeclan Doherty 	}
2622387259bdSDeclan Doherty 
2623387259bdSDeclan Doherty 	if (enabled_portcount == 1) {
2624387259bdSDeclan Doherty 		l2fwd_dst_ports[last_portid] = last_portid;
2625387259bdSDeclan Doherty 	} else if (enabled_portcount % 2) {
2626387259bdSDeclan Doherty 		printf("odd number of ports in portmask- bye\n");
2627387259bdSDeclan Doherty 		return -1;
2628387259bdSDeclan Doherty 	}
2629387259bdSDeclan Doherty 
26308728ccf3SThomas Monjalon 	check_all_ports_link_status(l2fwd_enabled_port_mask);
2631387259bdSDeclan Doherty 
2632387259bdSDeclan Doherty 	return enabled_portcount;
2633387259bdSDeclan Doherty }
2634387259bdSDeclan Doherty 
26351df9c010SPablo de Lara static void
26361df9c010SPablo de Lara reserve_key_memory(struct l2fwd_crypto_options *options)
26371df9c010SPablo de Lara {
2638186b14d6SFan Zhang 	options->cipher_xform.cipher.key.data = options->cipher_key;
26391df9c010SPablo de Lara 
2640186b14d6SFan Zhang 	options->auth_xform.auth.key.data = options->auth_key;
26411df9c010SPablo de Lara 
2642186b14d6SFan Zhang 	options->aead_xform.aead.key.data = options->aead_key;
26432661f4fbSPablo de Lara 
2644acf86169SPablo de Lara 	options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2645acf86169SPablo de Lara 	if (options->cipher_iv.data == NULL)
2646acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2647acf86169SPablo de Lara 
2648acf86169SPablo de Lara 	options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2649acf86169SPablo de Lara 	if (options->auth_iv.data == NULL)
2650acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2651617a7949SPablo de Lara 
26522661f4fbSPablo de Lara 	options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
26532661f4fbSPablo de Lara 	if (options->aead_iv.data == NULL)
26542661f4fbSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
26552661f4fbSPablo de Lara 
2656617a7949SPablo de Lara 	options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2657617a7949SPablo de Lara 	if (options->aad.data == NULL)
2658617a7949SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
265987cf4c6cSThomas Monjalon 	options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
26601df9c010SPablo de Lara }
26611df9c010SPablo de Lara 
2662387259bdSDeclan Doherty int
2663387259bdSDeclan Doherty main(int argc, char **argv)
2664387259bdSDeclan Doherty {
26658728ccf3SThomas Monjalon 	struct lcore_queue_conf *qconf = NULL;
2666387259bdSDeclan Doherty 	struct l2fwd_crypto_options options;
2667387259bdSDeclan Doherty 
2668e2cdfbd0SZhiyong Yang 	uint8_t nb_cryptodevs, cdev_id;
26698728ccf3SThomas Monjalon 	uint16_t portid;
26708728ccf3SThomas Monjalon 	unsigned lcore_id, rx_lcore_id = 0;
2671387259bdSDeclan Doherty 	int ret, enabled_cdevcount, enabled_portcount;
267227cf2d1bSPablo de Lara 	uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2673387259bdSDeclan Doherty 
2674387259bdSDeclan Doherty 	/* init EAL */
2675387259bdSDeclan Doherty 	ret = rte_eal_init(argc, argv);
2676387259bdSDeclan Doherty 	if (ret < 0)
2677387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2678387259bdSDeclan Doherty 	argc -= ret;
2679387259bdSDeclan Doherty 	argv += ret;
2680387259bdSDeclan Doherty 
26811df9c010SPablo de Lara 	/* reserve memory for Cipher/Auth key and IV */
26821df9c010SPablo de Lara 	reserve_key_memory(&options);
26831df9c010SPablo de Lara 
2684387259bdSDeclan Doherty 	/* parse application arguments (after the EAL ones) */
2685387259bdSDeclan Doherty 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
2686387259bdSDeclan Doherty 	if (ret < 0)
2687387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2688387259bdSDeclan Doherty 
2689acdfecbaSKuba Kozak 	printf("MAC updating %s\n",
2690acdfecbaSKuba Kozak 			options.mac_updating ? "enabled" : "disabled");
2691acdfecbaSKuba Kozak 
2692387259bdSDeclan Doherty 	/* create the mbuf pool */
2693c0f87eb5SDeclan Doherty 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2694c0f87eb5SDeclan Doherty 			sizeof(struct rte_crypto_op),
2695c0f87eb5SDeclan Doherty 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2696387259bdSDeclan Doherty 	if (l2fwd_pktmbuf_pool == NULL)
2697387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2698387259bdSDeclan Doherty 
2699387259bdSDeclan Doherty 	/* create crypto op pool */
2700c0f87eb5SDeclan Doherty 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2701e636243eSPablo de Lara 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2702c0f87eb5SDeclan Doherty 			rte_socket_id());
2703c0f87eb5SDeclan Doherty 	if (l2fwd_crypto_op_pool == NULL)
2704387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2705387259bdSDeclan Doherty 
2706387259bdSDeclan Doherty 	/* Enable Ethernet ports */
2707387259bdSDeclan Doherty 	enabled_portcount = initialize_ports(&options);
2708387259bdSDeclan Doherty 	if (enabled_portcount < 1)
2709387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2710387259bdSDeclan Doherty 
2711387259bdSDeclan Doherty 	/* Initialize the port/queue configuration of each logical core */
27128728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
2713387259bdSDeclan Doherty 
2714387259bdSDeclan Doherty 		/* skip ports that are not enabled */
2715387259bdSDeclan Doherty 		if ((options.portmask & (1 << portid)) == 0)
2716387259bdSDeclan Doherty 			continue;
2717387259bdSDeclan Doherty 
2718387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2719387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2720387259bdSDeclan Doherty 				rx_lcore_id++;
2721387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2722387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2723387259bdSDeclan Doherty 							"Not enough cores\n");
2724387259bdSDeclan Doherty 			}
2725387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2726387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2727387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2728387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2729387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2730387259bdSDeclan Doherty 				rx_lcore_id++;
2731387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2732387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2733387259bdSDeclan Doherty 							"Not enough cores\n");
2734387259bdSDeclan Doherty 			}
2735387259bdSDeclan Doherty 		}
2736387259bdSDeclan Doherty 
2737387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2738387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2739387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2740387259bdSDeclan Doherty 
2741387259bdSDeclan Doherty 		qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2742387259bdSDeclan Doherty 		qconf->nb_rx_ports++;
2743387259bdSDeclan Doherty 
2744e2cdfbd0SZhiyong Yang 		printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2745387259bdSDeclan Doherty 	}
2746387259bdSDeclan Doherty 
2747387259bdSDeclan Doherty 	/* Enable Crypto devices */
274827cf2d1bSPablo de Lara 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
274927cf2d1bSPablo de Lara 			enabled_cdevs);
275027cf2d1bSPablo de Lara 	if (enabled_cdevcount < 0)
275127cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
275227cf2d1bSPablo de Lara 
275327cf2d1bSPablo de Lara 	if (enabled_cdevcount < enabled_portcount)
275427cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
275527cf2d1bSPablo de Lara 				"has to be more or equal to number of ports (%d)\n",
275627cf2d1bSPablo de Lara 				enabled_cdevcount, enabled_portcount);
2757387259bdSDeclan Doherty 
2758387259bdSDeclan Doherty 	nb_cryptodevs = rte_cryptodev_count();
275927cf2d1bSPablo de Lara 
276027cf2d1bSPablo de Lara 	/* Initialize the port/cryptodev configuration of each logical core */
2761387259bdSDeclan Doherty 	for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2762387259bdSDeclan Doherty 			cdev_id < nb_cryptodevs && enabled_cdevcount;
2763387259bdSDeclan Doherty 			cdev_id++) {
276427cf2d1bSPablo de Lara 		/* Crypto op not supported by crypto device */
276527cf2d1bSPablo de Lara 		if (!enabled_cdevs[cdev_id])
2766387259bdSDeclan Doherty 			continue;
2767387259bdSDeclan Doherty 
2768387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2769387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2770387259bdSDeclan Doherty 				rx_lcore_id++;
2771387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2772387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2773387259bdSDeclan Doherty 							"Not enough cores\n");
2774387259bdSDeclan Doherty 			}
2775387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2776387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2777387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2778387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2779387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2780387259bdSDeclan Doherty 				rx_lcore_id++;
2781387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2782387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2783387259bdSDeclan Doherty 							"Not enough cores\n");
2784387259bdSDeclan Doherty 			}
2785387259bdSDeclan Doherty 		}
2786387259bdSDeclan Doherty 
2787387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2788387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2789387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2790387259bdSDeclan Doherty 
2791387259bdSDeclan Doherty 		qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2792387259bdSDeclan Doherty 		qconf->nb_crypto_devs++;
2793387259bdSDeclan Doherty 
2794387259bdSDeclan Doherty 		enabled_cdevcount--;
2795387259bdSDeclan Doherty 
2796387259bdSDeclan Doherty 		printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2797387259bdSDeclan Doherty 				(unsigned)cdev_id);
2798387259bdSDeclan Doherty 	}
2799387259bdSDeclan Doherty 
2800387259bdSDeclan Doherty 	/* launch per-lcore init on every lcore */
2801387259bdSDeclan Doherty 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2802387259bdSDeclan Doherty 			CALL_MASTER);
2803387259bdSDeclan Doherty 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2804387259bdSDeclan Doherty 		if (rte_eal_wait_lcore(lcore_id) < 0)
2805387259bdSDeclan Doherty 			return -1;
2806387259bdSDeclan Doherty 	}
2807387259bdSDeclan Doherty 
2808387259bdSDeclan Doherty 	return 0;
2809387259bdSDeclan Doherty }
2810