xref: /dpdk/examples/l2fwd-crypto/main.c (revision a7c528e5d71ff3f569898d268f9de129fdfc152b)
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;
139387259bdSDeclan Doherty 
140acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
141acf86169SPablo de Lara 	unsigned int cipher_iv_param;
142acf86169SPablo de Lara 	int cipher_iv_random_size;
143387259bdSDeclan Doherty 
1441bd407faSFiona Trahe 	struct rte_crypto_sym_xform auth_xform;
1451df9c010SPablo de Lara 	uint8_t akey_param;
146a061e50aSPablo de Lara 	int akey_random_size;
147617a7949SPablo de Lara 
148acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
149acf86169SPablo de Lara 	unsigned int auth_iv_param;
150acf86169SPablo de Lara 	int auth_iv_random_size;
151acf86169SPablo de Lara 
1522661f4fbSPablo de Lara 	struct rte_crypto_sym_xform aead_xform;
1532661f4fbSPablo de Lara 	unsigned int aead_key_param;
1542661f4fbSPablo de Lara 	int aead_key_random_size;
1552661f4fbSPablo de Lara 
1562661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
1572661f4fbSPablo de Lara 	unsigned int aead_iv_param;
1582661f4fbSPablo de Lara 	int aead_iv_random_size;
1592661f4fbSPablo de Lara 
160617a7949SPablo de Lara 	struct l2fwd_key aad;
161617a7949SPablo de Lara 	unsigned aad_param;
162a061e50aSPablo de Lara 	int aad_random_size;
163a061e50aSPablo de Lara 
164a061e50aSPablo de Lara 	int digest_size;
16527cf2d1bSPablo de Lara 
16627cf2d1bSPablo de Lara 	uint16_t block_size;
16727cf2d1bSPablo de Lara 	char string_type[MAX_STR_LEN];
168d2797f51SFan Zhang 
169d2797f51SFan Zhang 	uint64_t cryptodev_mask;
170acdfecbaSKuba Kozak 
171acdfecbaSKuba Kozak 	unsigned int mac_updating;
172387259bdSDeclan Doherty };
173387259bdSDeclan Doherty 
174387259bdSDeclan Doherty /** l2fwd crypto lcore params */
175387259bdSDeclan Doherty struct l2fwd_crypto_params {
176387259bdSDeclan Doherty 	uint8_t dev_id;
177387259bdSDeclan Doherty 	uint8_t qp_id;
178387259bdSDeclan Doherty 
179387259bdSDeclan Doherty 	unsigned digest_length;
180387259bdSDeclan Doherty 	unsigned block_size;
18127cf2d1bSPablo de Lara 
182acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
183acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
1842661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
185617a7949SPablo de Lara 	struct l2fwd_key aad;
1861bd407faSFiona Trahe 	struct rte_cryptodev_sym_session *session;
1871a75e9f3SPablo de Lara 
1881a75e9f3SPablo de Lara 	uint8_t do_cipher;
1891a75e9f3SPablo de Lara 	uint8_t do_hash;
1902661f4fbSPablo de Lara 	uint8_t do_aead;
19127cf2d1bSPablo de Lara 	uint8_t hash_verify;
192d29ea843SPablo de Lara 
193d29ea843SPablo de Lara 	enum rte_crypto_cipher_algorithm cipher_algo;
194d29ea843SPablo de Lara 	enum rte_crypto_auth_algorithm auth_algo;
1952661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm aead_algo;
196387259bdSDeclan Doherty };
197387259bdSDeclan Doherty 
198387259bdSDeclan Doherty /** lcore configuration */
199387259bdSDeclan Doherty struct lcore_queue_conf {
200387259bdSDeclan Doherty 	unsigned nb_rx_ports;
201e2cdfbd0SZhiyong Yang 	uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
202387259bdSDeclan Doherty 
203387259bdSDeclan Doherty 	unsigned nb_crypto_devs;
204387259bdSDeclan Doherty 	unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
205387259bdSDeclan Doherty 
206ad476dd3SPablo de Lara 	struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
207c0f87eb5SDeclan Doherty 	struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
208387259bdSDeclan Doherty } __rte_cache_aligned;
209387259bdSDeclan Doherty 
210387259bdSDeclan Doherty struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
211387259bdSDeclan Doherty 
212f2b713e3SShahaf Shuler static struct rte_eth_conf port_conf = {
213387259bdSDeclan Doherty 	.rxmode = {
2141df9c010SPablo de Lara 		.mq_mode = ETH_MQ_RX_NONE,
21535b2d13fSOlivier Matz 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
216387259bdSDeclan Doherty 		.split_hdr_size = 0,
217387259bdSDeclan Doherty 	},
218387259bdSDeclan Doherty 	.txmode = {
219387259bdSDeclan Doherty 		.mq_mode = ETH_MQ_TX_NONE,
220387259bdSDeclan Doherty 	},
221387259bdSDeclan Doherty };
222387259bdSDeclan Doherty 
223387259bdSDeclan Doherty struct rte_mempool *l2fwd_pktmbuf_pool;
224c0f87eb5SDeclan Doherty struct rte_mempool *l2fwd_crypto_op_pool;
225261bbff7SFan Zhang static struct {
226261bbff7SFan Zhang 	struct rte_mempool *sess_mp;
227261bbff7SFan Zhang 	struct rte_mempool *priv_mp;
228261bbff7SFan Zhang } session_pool_socket[RTE_MAX_NUMA_NODES];
229387259bdSDeclan Doherty 
230387259bdSDeclan Doherty /* Per-port statistics struct */
231387259bdSDeclan Doherty struct l2fwd_port_statistics {
232387259bdSDeclan Doherty 	uint64_t tx;
233387259bdSDeclan Doherty 	uint64_t rx;
234387259bdSDeclan Doherty 
235387259bdSDeclan Doherty 	uint64_t crypto_enqueued;
236387259bdSDeclan Doherty 	uint64_t crypto_dequeued;
237387259bdSDeclan Doherty 
238387259bdSDeclan Doherty 	uint64_t dropped;
239387259bdSDeclan Doherty } __rte_cache_aligned;
240387259bdSDeclan Doherty 
241387259bdSDeclan Doherty struct l2fwd_crypto_statistics {
242387259bdSDeclan Doherty 	uint64_t enqueued;
243387259bdSDeclan Doherty 	uint64_t dequeued;
244387259bdSDeclan Doherty 
245387259bdSDeclan Doherty 	uint64_t errors;
246387259bdSDeclan Doherty } __rte_cache_aligned;
247387259bdSDeclan Doherty 
248387259bdSDeclan Doherty struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
249572f0779SSlawomir Mrozowicz struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
250387259bdSDeclan Doherty 
251387259bdSDeclan Doherty /* A tsc-based timer responsible for triggering statistics printout */
252387259bdSDeclan Doherty #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
2533c96262cSPablo de Lara #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
254387259bdSDeclan Doherty 
255387259bdSDeclan Doherty /* default period is 10 seconds */
256387259bdSDeclan Doherty static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
257387259bdSDeclan Doherty 
258387259bdSDeclan Doherty /* Print out statistics on packets dropped */
259387259bdSDeclan Doherty static void
260387259bdSDeclan Doherty print_stats(void)
261387259bdSDeclan Doherty {
26228523d9aSPablo de Lara 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
26328523d9aSPablo de Lara 	uint64_t total_packets_enqueued, total_packets_dequeued,
26428523d9aSPablo de Lara 		total_packets_errors;
265e2cdfbd0SZhiyong Yang 	uint16_t portid;
266387259bdSDeclan Doherty 	uint64_t cdevid;
267387259bdSDeclan Doherty 
26828523d9aSPablo de Lara 	total_packets_dropped = 0;
26928523d9aSPablo de Lara 	total_packets_tx = 0;
27028523d9aSPablo de Lara 	total_packets_rx = 0;
27128523d9aSPablo de Lara 	total_packets_enqueued = 0;
27228523d9aSPablo de Lara 	total_packets_dequeued = 0;
27328523d9aSPablo de Lara 	total_packets_errors = 0;
274387259bdSDeclan Doherty 
275387259bdSDeclan Doherty 	const char clr[] = { 27, '[', '2', 'J', '\0' };
276387259bdSDeclan Doherty 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
277387259bdSDeclan Doherty 
278387259bdSDeclan Doherty 		/* Clear screen and move to top left */
279387259bdSDeclan Doherty 	printf("%s%s", clr, topLeft);
280387259bdSDeclan Doherty 
281387259bdSDeclan Doherty 	printf("\nPort statistics ====================================");
282387259bdSDeclan Doherty 
283387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
284387259bdSDeclan Doherty 		/* skip disabled ports */
285387259bdSDeclan Doherty 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
286387259bdSDeclan Doherty 			continue;
287387259bdSDeclan Doherty 		printf("\nStatistics for port %u ------------------------------"
288387259bdSDeclan Doherty 			   "\nPackets sent: %32"PRIu64
289387259bdSDeclan Doherty 			   "\nPackets received: %28"PRIu64
290387259bdSDeclan Doherty 			   "\nPackets dropped: %29"PRIu64,
291387259bdSDeclan Doherty 			   portid,
292387259bdSDeclan Doherty 			   port_statistics[portid].tx,
293387259bdSDeclan Doherty 			   port_statistics[portid].rx,
294387259bdSDeclan Doherty 			   port_statistics[portid].dropped);
295387259bdSDeclan Doherty 
296387259bdSDeclan Doherty 		total_packets_dropped += port_statistics[portid].dropped;
297387259bdSDeclan Doherty 		total_packets_tx += port_statistics[portid].tx;
298387259bdSDeclan Doherty 		total_packets_rx += port_statistics[portid].rx;
299387259bdSDeclan Doherty 	}
300387259bdSDeclan Doherty 	printf("\nCrypto statistics ==================================");
301387259bdSDeclan Doherty 
302387259bdSDeclan Doherty 	for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
303387259bdSDeclan Doherty 		/* skip disabled ports */
304ad476dd3SPablo de Lara 		if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
305387259bdSDeclan Doherty 			continue;
306387259bdSDeclan Doherty 		printf("\nStatistics for cryptodev %"PRIu64
307387259bdSDeclan Doherty 				" -------------------------"
308387259bdSDeclan Doherty 			   "\nPackets enqueued: %28"PRIu64
309387259bdSDeclan Doherty 			   "\nPackets dequeued: %28"PRIu64
310387259bdSDeclan Doherty 			   "\nPackets errors: %30"PRIu64,
311387259bdSDeclan Doherty 			   cdevid,
312387259bdSDeclan Doherty 			   crypto_statistics[cdevid].enqueued,
313387259bdSDeclan Doherty 			   crypto_statistics[cdevid].dequeued,
314387259bdSDeclan Doherty 			   crypto_statistics[cdevid].errors);
315387259bdSDeclan Doherty 
316387259bdSDeclan Doherty 		total_packets_enqueued += crypto_statistics[cdevid].enqueued;
317387259bdSDeclan Doherty 		total_packets_dequeued += crypto_statistics[cdevid].dequeued;
318387259bdSDeclan Doherty 		total_packets_errors += crypto_statistics[cdevid].errors;
319387259bdSDeclan Doherty 	}
320387259bdSDeclan Doherty 	printf("\nAggregate statistics ==============================="
321387259bdSDeclan Doherty 		   "\nTotal packets received: %22"PRIu64
322387259bdSDeclan Doherty 		   "\nTotal packets enqueued: %22"PRIu64
323387259bdSDeclan Doherty 		   "\nTotal packets dequeued: %22"PRIu64
324387259bdSDeclan Doherty 		   "\nTotal packets sent: %26"PRIu64
325387259bdSDeclan Doherty 		   "\nTotal packets dropped: %23"PRIu64
326387259bdSDeclan Doherty 		   "\nTotal packets crypto errors: %17"PRIu64,
327387259bdSDeclan Doherty 		   total_packets_rx,
328387259bdSDeclan Doherty 		   total_packets_enqueued,
329387259bdSDeclan Doherty 		   total_packets_dequeued,
330387259bdSDeclan Doherty 		   total_packets_tx,
331387259bdSDeclan Doherty 		   total_packets_dropped,
332387259bdSDeclan Doherty 		   total_packets_errors);
333387259bdSDeclan Doherty 	printf("\n====================================================\n");
334387259bdSDeclan Doherty }
335387259bdSDeclan Doherty 
336387259bdSDeclan Doherty static int
337387259bdSDeclan Doherty l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
338387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
339387259bdSDeclan Doherty {
340c0f87eb5SDeclan Doherty 	struct rte_crypto_op **op_buffer;
341387259bdSDeclan Doherty 	unsigned ret;
342387259bdSDeclan Doherty 
343c0f87eb5SDeclan Doherty 	op_buffer = (struct rte_crypto_op **)
344c0f87eb5SDeclan Doherty 			qconf->op_buf[cparams->dev_id].buffer;
345387259bdSDeclan Doherty 
346c0f87eb5SDeclan Doherty 	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
347c0f87eb5SDeclan Doherty 			cparams->qp_id,	op_buffer, (uint16_t) n);
348c0f87eb5SDeclan Doherty 
349387259bdSDeclan Doherty 	crypto_statistics[cparams->dev_id].enqueued += ret;
350387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
351387259bdSDeclan Doherty 		crypto_statistics[cparams->dev_id].errors += (n - ret);
352387259bdSDeclan Doherty 		do {
353c0f87eb5SDeclan Doherty 			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
354c0f87eb5SDeclan Doherty 			rte_crypto_op_free(op_buffer[ret]);
355387259bdSDeclan Doherty 		} while (++ret < n);
356387259bdSDeclan Doherty 	}
357387259bdSDeclan Doherty 
358387259bdSDeclan Doherty 	return 0;
359387259bdSDeclan Doherty }
360387259bdSDeclan Doherty 
361387259bdSDeclan Doherty static int
362c0f87eb5SDeclan Doherty l2fwd_crypto_enqueue(struct rte_crypto_op *op,
363c0f87eb5SDeclan Doherty 		struct l2fwd_crypto_params *cparams)
364387259bdSDeclan Doherty {
365387259bdSDeclan Doherty 	unsigned lcore_id, len;
366387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
367387259bdSDeclan Doherty 
368387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
369387259bdSDeclan Doherty 
370387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
371c0f87eb5SDeclan Doherty 	len = qconf->op_buf[cparams->dev_id].len;
372c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].buffer[len] = op;
373387259bdSDeclan Doherty 	len++;
374387259bdSDeclan Doherty 
375c0f87eb5SDeclan Doherty 	/* enough ops to be sent */
376387259bdSDeclan Doherty 	if (len == MAX_PKT_BURST) {
377387259bdSDeclan Doherty 		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
378387259bdSDeclan Doherty 		len = 0;
379387259bdSDeclan Doherty 	}
380387259bdSDeclan Doherty 
381c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].len = len;
382387259bdSDeclan Doherty 	return 0;
383387259bdSDeclan Doherty }
384387259bdSDeclan Doherty 
385387259bdSDeclan Doherty static int
386387259bdSDeclan Doherty l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
387c0f87eb5SDeclan Doherty 		struct rte_crypto_op *op,
388387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
389387259bdSDeclan Doherty {
3906d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_hdr;
391*a7c528e5SOlivier Matz 	struct rte_ipv4_hdr *ip_hdr;
392387259bdSDeclan Doherty 
3935839fd20SPablo de Lara 	uint32_t ipdata_offset, data_len;
3945839fd20SPablo de Lara 	uint32_t pad_len = 0;
395387259bdSDeclan Doherty 	char *padding;
396387259bdSDeclan Doherty 
3976d13ea8eSOlivier Matz 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
398387259bdSDeclan Doherty 
39935b2d13fSOlivier Matz 	if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPv4))
400387259bdSDeclan Doherty 		return -1;
401387259bdSDeclan Doherty 
4026d13ea8eSOlivier Matz 	ipdata_offset = sizeof(struct rte_ether_hdr);
403387259bdSDeclan Doherty 
404*a7c528e5SOlivier Matz 	ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
405387259bdSDeclan Doherty 			ipdata_offset);
406387259bdSDeclan Doherty 
407387259bdSDeclan Doherty 	ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
408387259bdSDeclan Doherty 			* IPV4_IHL_MULTIPLIER;
409387259bdSDeclan Doherty 
410387259bdSDeclan Doherty 
411387259bdSDeclan Doherty 	/* Zero pad data to be crypto'd so it is block aligned */
412387259bdSDeclan Doherty 	data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
413893fbab0SPiotr Azarewicz 
4146df38301SPablo de Lara 	if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify)
415893fbab0SPiotr Azarewicz 		data_len -= cparams->digest_length;
416893fbab0SPiotr Azarewicz 
4175839fd20SPablo de Lara 	if (cparams->do_cipher) {
4185839fd20SPablo de Lara 		/*
4195839fd20SPablo de Lara 		 * Following algorithms are block cipher algorithms,
4205839fd20SPablo de Lara 		 * and might need padding
4215839fd20SPablo de Lara 		 */
4225839fd20SPablo de Lara 		switch (cparams->cipher_algo) {
4235839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_CBC:
4245839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_ECB:
4255839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_DES_CBC:
4265839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_CBC:
4275839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_ECB:
4285839fd20SPablo de Lara 			if (data_len % cparams->block_size)
4295839fd20SPablo de Lara 				pad_len = cparams->block_size -
4305839fd20SPablo de Lara 					(data_len % cparams->block_size);
4315839fd20SPablo de Lara 			break;
4325839fd20SPablo de Lara 		default:
4335839fd20SPablo de Lara 			pad_len = 0;
4345839fd20SPablo de Lara 		}
435387259bdSDeclan Doherty 
436387259bdSDeclan Doherty 		if (pad_len) {
437387259bdSDeclan Doherty 			padding = rte_pktmbuf_append(m, pad_len);
438387259bdSDeclan Doherty 			if (unlikely(!padding))
439387259bdSDeclan Doherty 				return -1;
440387259bdSDeclan Doherty 
441387259bdSDeclan Doherty 			data_len += pad_len;
442387259bdSDeclan Doherty 			memset(padding, 0, pad_len);
443387259bdSDeclan Doherty 		}
4445839fd20SPablo de Lara 	}
445387259bdSDeclan Doherty 
446387259bdSDeclan Doherty 	/* Set crypto operation data parameters */
447c0f87eb5SDeclan Doherty 	rte_crypto_op_attach_sym_session(op, cparams->session);
448387259bdSDeclan Doherty 
4491a75e9f3SPablo de Lara 	if (cparams->do_hash) {
450acf86169SPablo de Lara 		if (cparams->auth_iv.length) {
451acf86169SPablo de Lara 			uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
452acf86169SPablo de Lara 						uint8_t *,
453acf86169SPablo de Lara 						IV_OFFSET +
454acf86169SPablo de Lara 						cparams->cipher_iv.length);
455acf86169SPablo de Lara 			/*
456acf86169SPablo de Lara 			 * Copy IV at the end of the crypto operation,
457acf86169SPablo de Lara 			 * after the cipher IV, if added
458acf86169SPablo de Lara 			 */
459acf86169SPablo de Lara 			rte_memcpy(iv_ptr, cparams->auth_iv.data,
460acf86169SPablo de Lara 					cparams->auth_iv.length);
461acf86169SPablo de Lara 		}
46227cf2d1bSPablo de Lara 		if (!cparams->hash_verify) {
463387259bdSDeclan Doherty 			/* Append space for digest to end of packet */
464c0f87eb5SDeclan Doherty 			op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
465387259bdSDeclan Doherty 				cparams->digest_length);
46627cf2d1bSPablo de Lara 		} else {
467893fbab0SPiotr Azarewicz 			op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
468893fbab0SPiotr Azarewicz 				uint8_t *) + ipdata_offset + data_len;
46927cf2d1bSPablo de Lara 		}
47027cf2d1bSPablo de Lara 
471bfa9a8a4SThomas Monjalon 		op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
472387259bdSDeclan Doherty 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
473387259bdSDeclan Doherty 
4741f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
4752773c86dSPablo de Lara 		if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
4761f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
4771f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
478d29ea843SPablo de Lara 			op->sym->auth.data.offset = ipdata_offset << 3;
479d29ea843SPablo de Lara 			op->sym->auth.data.length = data_len << 3;
480d29ea843SPablo de Lara 		} else {
481c0f87eb5SDeclan Doherty 			op->sym->auth.data.offset = ipdata_offset;
482c0f87eb5SDeclan Doherty 			op->sym->auth.data.length = data_len;
483d29ea843SPablo de Lara 		}
4841a75e9f3SPablo de Lara 	}
4851a75e9f3SPablo de Lara 
4861a75e9f3SPablo de Lara 	if (cparams->do_cipher) {
487e636243eSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
488e636243eSPablo de Lara 							IV_OFFSET);
489e636243eSPablo de Lara 		/* Copy IV at the end of the crypto operation */
490acf86169SPablo de Lara 		rte_memcpy(iv_ptr, cparams->cipher_iv.data,
491acf86169SPablo de Lara 				cparams->cipher_iv.length);
492e636243eSPablo de Lara 
4931f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
4942773c86dSPablo de Lara 		if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
4951f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
4961f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
497d29ea843SPablo de Lara 			op->sym->cipher.data.offset = ipdata_offset << 3;
498d29ea843SPablo de Lara 			op->sym->cipher.data.length = data_len << 3;
499d29ea843SPablo de Lara 		} else {
500c0f87eb5SDeclan Doherty 			op->sym->cipher.data.offset = ipdata_offset;
501c0f87eb5SDeclan Doherty 			op->sym->cipher.data.length = data_len;
5021a75e9f3SPablo de Lara 		}
503d29ea843SPablo de Lara 	}
504387259bdSDeclan Doherty 
5052661f4fbSPablo de Lara 	if (cparams->do_aead) {
5062661f4fbSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
5072661f4fbSPablo de Lara 							IV_OFFSET);
5082661f4fbSPablo de Lara 		/* Copy IV at the end of the crypto operation */
509ff5d5b01SPablo de Lara 		/*
510ff5d5b01SPablo de Lara 		 * If doing AES-CCM, nonce is copied one byte
511ff5d5b01SPablo de Lara 		 * after the start of IV field
512ff5d5b01SPablo de Lara 		 */
513ff5d5b01SPablo de Lara 		if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
514ff5d5b01SPablo de Lara 			rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
515ff5d5b01SPablo de Lara 					cparams->aead_iv.length);
516ff5d5b01SPablo de Lara 		else
517ff5d5b01SPablo de Lara 			rte_memcpy(iv_ptr, cparams->aead_iv.data,
518ff5d5b01SPablo de Lara 					cparams->aead_iv.length);
5192661f4fbSPablo de Lara 
5202661f4fbSPablo de Lara 		op->sym->aead.data.offset = ipdata_offset;
5212661f4fbSPablo de Lara 		op->sym->aead.data.length = data_len;
5222661f4fbSPablo de Lara 
5232661f4fbSPablo de Lara 		if (!cparams->hash_verify) {
5242661f4fbSPablo de Lara 			/* Append space for digest to end of packet */
5252661f4fbSPablo de Lara 			op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
5262661f4fbSPablo de Lara 				cparams->digest_length);
5272661f4fbSPablo de Lara 		} else {
5282661f4fbSPablo de Lara 			op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
5292661f4fbSPablo de Lara 				uint8_t *) + ipdata_offset + data_len;
5302661f4fbSPablo de Lara 		}
5312661f4fbSPablo de Lara 
532bfa9a8a4SThomas Monjalon 		op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
5332661f4fbSPablo de Lara 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
5342661f4fbSPablo de Lara 
5352661f4fbSPablo de Lara 		if (cparams->aad.length) {
5362661f4fbSPablo de Lara 			op->sym->aead.aad.data = cparams->aad.data;
5372661f4fbSPablo de Lara 			op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
5382661f4fbSPablo de Lara 		}
5392661f4fbSPablo de Lara 	}
5402661f4fbSPablo de Lara 
541c0f87eb5SDeclan Doherty 	op->sym->m_src = m;
542c0f87eb5SDeclan Doherty 
543c0f87eb5SDeclan Doherty 	return l2fwd_crypto_enqueue(op, cparams);
544387259bdSDeclan Doherty }
545387259bdSDeclan Doherty 
546387259bdSDeclan Doherty 
547387259bdSDeclan Doherty /* Send the burst of packets on an output interface */
548387259bdSDeclan Doherty static int
549c0f87eb5SDeclan Doherty l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
55047523597SZhiyong Yang 		uint16_t port)
551387259bdSDeclan Doherty {
552387259bdSDeclan Doherty 	struct rte_mbuf **pkt_buffer;
553387259bdSDeclan Doherty 	unsigned ret;
554387259bdSDeclan Doherty 
555c0f87eb5SDeclan Doherty 	pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
556387259bdSDeclan Doherty 
557c0f87eb5SDeclan Doherty 	ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
558387259bdSDeclan Doherty 	port_statistics[port].tx += ret;
559387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
560387259bdSDeclan Doherty 		port_statistics[port].dropped += (n - ret);
561387259bdSDeclan Doherty 		do {
562387259bdSDeclan Doherty 			rte_pktmbuf_free(pkt_buffer[ret]);
563387259bdSDeclan Doherty 		} while (++ret < n);
564387259bdSDeclan Doherty 	}
565387259bdSDeclan Doherty 
566387259bdSDeclan Doherty 	return 0;
567387259bdSDeclan Doherty }
568387259bdSDeclan Doherty 
569387259bdSDeclan Doherty /* Enqueue packets for TX and prepare them to be sent */
570387259bdSDeclan Doherty static int
57147523597SZhiyong Yang l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
572387259bdSDeclan Doherty {
573387259bdSDeclan Doherty 	unsigned lcore_id, len;
574387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
575387259bdSDeclan Doherty 
576387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
577387259bdSDeclan Doherty 
578387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
579c0f87eb5SDeclan Doherty 	len = qconf->pkt_buf[port].len;
580c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].buffer[len] = m;
581387259bdSDeclan Doherty 	len++;
582387259bdSDeclan Doherty 
583387259bdSDeclan Doherty 	/* enough pkts to be sent */
584387259bdSDeclan Doherty 	if (unlikely(len == MAX_PKT_BURST)) {
585387259bdSDeclan Doherty 		l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
586387259bdSDeclan Doherty 		len = 0;
587387259bdSDeclan Doherty 	}
588387259bdSDeclan Doherty 
589c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].len = len;
590387259bdSDeclan Doherty 	return 0;
591387259bdSDeclan Doherty }
592387259bdSDeclan Doherty 
593387259bdSDeclan Doherty static void
594e2cdfbd0SZhiyong Yang l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
595387259bdSDeclan Doherty {
5966d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth;
597387259bdSDeclan Doherty 	void *tmp;
598387259bdSDeclan Doherty 
5996d13ea8eSOlivier Matz 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
600387259bdSDeclan Doherty 
601387259bdSDeclan Doherty 	/* 02:00:00:00:00:xx */
602387259bdSDeclan Doherty 	tmp = &eth->d_addr.addr_bytes[0];
603acdfecbaSKuba Kozak 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
604387259bdSDeclan Doherty 
605387259bdSDeclan Doherty 	/* src addr */
606538da7a1SOlivier Matz 	rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
607acdfecbaSKuba Kozak }
608acdfecbaSKuba Kozak 
609acdfecbaSKuba Kozak static void
610e2cdfbd0SZhiyong Yang l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
611acdfecbaSKuba Kozak 		struct l2fwd_crypto_options *options)
612acdfecbaSKuba Kozak {
613e2cdfbd0SZhiyong Yang 	uint16_t dst_port;
614acdfecbaSKuba Kozak 
615acdfecbaSKuba Kozak 	dst_port = l2fwd_dst_ports[portid];
616acdfecbaSKuba Kozak 
617acdfecbaSKuba Kozak 	if (options->mac_updating)
618acdfecbaSKuba Kozak 		l2fwd_mac_updating(m, dst_port);
619387259bdSDeclan Doherty 
620e2cdfbd0SZhiyong Yang 	l2fwd_send_packet(m, dst_port);
621387259bdSDeclan Doherty }
622387259bdSDeclan Doherty 
623387259bdSDeclan Doherty /** Generate random key */
624387259bdSDeclan Doherty static void
625387259bdSDeclan Doherty generate_random_key(uint8_t *key, unsigned length)
626387259bdSDeclan Doherty {
6276dad6e69SPiotr Azarewicz 	int fd;
6286dad6e69SPiotr Azarewicz 	int ret;
629387259bdSDeclan Doherty 
6306dad6e69SPiotr Azarewicz 	fd = open("/dev/urandom", O_RDONLY);
6316dad6e69SPiotr Azarewicz 	if (fd < 0)
6326dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
6336dad6e69SPiotr Azarewicz 
6346dad6e69SPiotr Azarewicz 	ret = read(fd, key, length);
6356dad6e69SPiotr Azarewicz 	close(fd);
6366dad6e69SPiotr Azarewicz 
6376dad6e69SPiotr Azarewicz 	if (ret != (signed)length)
6386dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
639387259bdSDeclan Doherty }
640387259bdSDeclan Doherty 
6411bd407faSFiona Trahe static struct rte_cryptodev_sym_session *
6422c59bd32SSlawomir Mrozowicz initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
643387259bdSDeclan Doherty {
6441bd407faSFiona Trahe 	struct rte_crypto_sym_xform *first_xform;
645b3bbd9e5SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
6468dbc9bbfSPablo de Lara 	int retval = rte_cryptodev_socket_id(cdev_id);
6478dbc9bbfSPablo de Lara 
6488dbc9bbfSPablo de Lara 	if (retval < 0)
6498dbc9bbfSPablo de Lara 		return NULL;
6508dbc9bbfSPablo de Lara 
6518dbc9bbfSPablo de Lara 	uint8_t socket_id = (uint8_t) retval;
652387259bdSDeclan Doherty 
6532661f4fbSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
6542661f4fbSPablo de Lara 		first_xform = &options->aead_xform;
6552661f4fbSPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
656387259bdSDeclan Doherty 		first_xform = &options->cipher_xform;
657387259bdSDeclan Doherty 		first_xform->next = &options->auth_xform;
6581a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
659387259bdSDeclan Doherty 		first_xform = &options->auth_xform;
660387259bdSDeclan Doherty 		first_xform->next = &options->cipher_xform;
6611a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
6621a75e9f3SPablo de Lara 		first_xform = &options->cipher_xform;
6631a75e9f3SPablo de Lara 	} else {
6641a75e9f3SPablo de Lara 		first_xform = &options->auth_xform;
665387259bdSDeclan Doherty 	}
666387259bdSDeclan Doherty 
667261bbff7SFan Zhang 	session = rte_cryptodev_sym_session_create(
668261bbff7SFan Zhang 			session_pool_socket[socket_id].sess_mp);
669b3bbd9e5SSlawomir Mrozowicz 	if (session == NULL)
670b3bbd9e5SSlawomir Mrozowicz 		return NULL;
671b3bbd9e5SSlawomir Mrozowicz 
672b3bbd9e5SSlawomir Mrozowicz 	if (rte_cryptodev_sym_session_init(cdev_id, session,
673261bbff7SFan Zhang 				first_xform,
674261bbff7SFan Zhang 				session_pool_socket[socket_id].priv_mp) < 0)
675b3bbd9e5SSlawomir Mrozowicz 		return NULL;
676b3bbd9e5SSlawomir Mrozowicz 
677b3bbd9e5SSlawomir Mrozowicz 	return session;
678387259bdSDeclan Doherty }
679387259bdSDeclan Doherty 
680387259bdSDeclan Doherty static void
681387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
682387259bdSDeclan Doherty 
683387259bdSDeclan Doherty /* main processing loop */
684387259bdSDeclan Doherty static void
685387259bdSDeclan Doherty l2fwd_main_loop(struct l2fwd_crypto_options *options)
686387259bdSDeclan Doherty {
687387259bdSDeclan Doherty 	struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
688c0f87eb5SDeclan Doherty 	struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
689c0f87eb5SDeclan Doherty 
690387259bdSDeclan Doherty 	unsigned lcore_id = rte_lcore_id();
691387259bdSDeclan Doherty 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
692e2cdfbd0SZhiyong Yang 	unsigned int i, j, nb_rx, len;
693e2cdfbd0SZhiyong Yang 	uint16_t portid;
694387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
695387259bdSDeclan Doherty 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
696387259bdSDeclan Doherty 			US_PER_S * BURST_TX_DRAIN_US;
697387259bdSDeclan Doherty 	struct l2fwd_crypto_params *cparams;
698387259bdSDeclan Doherty 	struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
6992c59bd32SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
700387259bdSDeclan Doherty 
701387259bdSDeclan Doherty 	if (qconf->nb_rx_ports == 0) {
702387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
703387259bdSDeclan Doherty 		return;
704387259bdSDeclan Doherty 	}
705387259bdSDeclan Doherty 
706387259bdSDeclan Doherty 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
707387259bdSDeclan Doherty 
708387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_rx_ports; i++) {
709387259bdSDeclan Doherty 
710387259bdSDeclan Doherty 		portid = qconf->rx_port_list[i];
711387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
712387259bdSDeclan Doherty 			portid);
713387259bdSDeclan Doherty 	}
714387259bdSDeclan Doherty 
715387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_crypto_devs; i++) {
7161a75e9f3SPablo de Lara 		port_cparams[i].do_cipher = 0;
7171a75e9f3SPablo de Lara 		port_cparams[i].do_hash = 0;
7182661f4fbSPablo de Lara 		port_cparams[i].do_aead = 0;
7191a75e9f3SPablo de Lara 
7201a75e9f3SPablo de Lara 		switch (options->xform_chain) {
7212661f4fbSPablo de Lara 		case L2FWD_CRYPTO_AEAD:
7222661f4fbSPablo de Lara 			port_cparams[i].do_aead = 1;
7232661f4fbSPablo de Lara 			break;
7241a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_HASH:
7251a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_CIPHER:
7261a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7271a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7281a75e9f3SPablo de Lara 			break;
7291a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_ONLY:
7301a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7311a75e9f3SPablo de Lara 			break;
7321a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_ONLY:
7331a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7341a75e9f3SPablo de Lara 			break;
7351a75e9f3SPablo de Lara 		}
7361a75e9f3SPablo de Lara 
737387259bdSDeclan Doherty 		port_cparams[i].dev_id = qconf->cryptodev_list[i];
738387259bdSDeclan Doherty 		port_cparams[i].qp_id = 0;
739387259bdSDeclan Doherty 
74027cf2d1bSPablo de Lara 		port_cparams[i].block_size = options->block_size;
741387259bdSDeclan Doherty 
7421a75e9f3SPablo de Lara 		if (port_cparams[i].do_hash) {
743acf86169SPablo de Lara 			port_cparams[i].auth_iv.data = options->auth_iv.data;
744acf86169SPablo de Lara 			port_cparams[i].auth_iv.length = options->auth_iv.length;
745acf86169SPablo de Lara 			if (!options->auth_iv_param)
746acf86169SPablo de Lara 				generate_random_key(port_cparams[i].auth_iv.data,
747acf86169SPablo de Lara 						port_cparams[i].auth_iv.length);
7482661f4fbSPablo de Lara 			if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
7492661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 1;
7502661f4fbSPablo de Lara 			else
7512661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 0;
7522661f4fbSPablo de Lara 
7532661f4fbSPablo de Lara 			port_cparams[i].auth_algo = options->auth_xform.auth.algo;
7542c023071SPablo de Lara 			port_cparams[i].digest_length =
7552c023071SPablo de Lara 					options->auth_xform.auth.digest_length;
756acf86169SPablo de Lara 			/* Set IV parameters */
757acf86169SPablo de Lara 			if (options->auth_iv.length) {
758acf86169SPablo de Lara 				options->auth_xform.auth.iv.offset =
759acf86169SPablo de Lara 					IV_OFFSET + options->cipher_iv.length;
760acf86169SPablo de Lara 				options->auth_xform.auth.iv.length =
761acf86169SPablo de Lara 					options->auth_iv.length;
762acf86169SPablo de Lara 			}
7632661f4fbSPablo de Lara 		}
7642661f4fbSPablo de Lara 
7652661f4fbSPablo de Lara 		if (port_cparams[i].do_aead) {
766ef896e92SPablo de Lara 			port_cparams[i].aead_iv.data = options->aead_iv.data;
767ef896e92SPablo de Lara 			port_cparams[i].aead_iv.length = options->aead_iv.length;
768ef896e92SPablo de Lara 			if (!options->aead_iv_param)
769ef896e92SPablo de Lara 				generate_random_key(port_cparams[i].aead_iv.data,
770ef896e92SPablo de Lara 						port_cparams[i].aead_iv.length);
7712661f4fbSPablo de Lara 			port_cparams[i].aead_algo = options->aead_xform.aead.algo;
77227cf2d1bSPablo de Lara 			port_cparams[i].digest_length =
7732661f4fbSPablo de Lara 					options->aead_xform.aead.digest_length;
77446a0547fSPablo de Lara 			if (options->aead_xform.aead.aad_length) {
77527cf2d1bSPablo de Lara 				port_cparams[i].aad.data = options->aad.data;
776617a7949SPablo de Lara 				port_cparams[i].aad.phys_addr = options->aad.phys_addr;
7772661f4fbSPablo de Lara 				port_cparams[i].aad.length = options->aad.length;
778617a7949SPablo de Lara 				if (!options->aad_param)
77927cf2d1bSPablo de Lara 					generate_random_key(port_cparams[i].aad.data,
780a158899aSPablo de Lara 						port_cparams[i].aad.length);
781ff5d5b01SPablo de Lara 				/*
782ff5d5b01SPablo de Lara 				 * If doing AES-CCM, first 18 bytes has to be reserved,
783ff5d5b01SPablo de Lara 				 * and actual AAD should start from byte 18
784ff5d5b01SPablo de Lara 				 */
785ff5d5b01SPablo de Lara 				if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
786ff5d5b01SPablo de Lara 					memmove(port_cparams[i].aad.data + 18,
787ff5d5b01SPablo de Lara 							port_cparams[i].aad.data,
788ff5d5b01SPablo de Lara 							port_cparams[i].aad.length);
78927cf2d1bSPablo de Lara 
79018f421f6SPablo de Lara 			} else
79118f421f6SPablo de Lara 				port_cparams[i].aad.length = 0;
79227cf2d1bSPablo de Lara 
7932661f4fbSPablo de Lara 			if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
79427cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 1;
79527cf2d1bSPablo de Lara 			else
79627cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 0;
797d29ea843SPablo de Lara 
7982661f4fbSPablo de Lara 			/* Set IV parameters */
7992661f4fbSPablo de Lara 			options->aead_xform.aead.iv.offset = IV_OFFSET;
8002661f4fbSPablo de Lara 			options->aead_xform.aead.iv.length = options->aead_iv.length;
8011a75e9f3SPablo de Lara 		}
8021a75e9f3SPablo de Lara 
8031a75e9f3SPablo de Lara 		if (port_cparams[i].do_cipher) {
804acf86169SPablo de Lara 			port_cparams[i].cipher_iv.data = options->cipher_iv.data;
805acf86169SPablo de Lara 			port_cparams[i].cipher_iv.length = options->cipher_iv.length;
806acf86169SPablo de Lara 			if (!options->cipher_iv_param)
807acf86169SPablo de Lara 				generate_random_key(port_cparams[i].cipher_iv.data,
808acf86169SPablo de Lara 						port_cparams[i].cipher_iv.length);
809617a7949SPablo de Lara 
810d29ea843SPablo de Lara 			port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
8110fbd75a9SPablo de Lara 			/* Set IV parameters */
8120fbd75a9SPablo de Lara 			options->cipher_xform.cipher.iv.offset = IV_OFFSET;
813acf86169SPablo de Lara 			options->cipher_xform.cipher.iv.length =
814acf86169SPablo de Lara 						options->cipher_iv.length;
81527cf2d1bSPablo de Lara 		}
816617a7949SPablo de Lara 
8172c59bd32SSlawomir Mrozowicz 		session = initialize_crypto_session(options,
818387259bdSDeclan Doherty 				port_cparams[i].dev_id);
8192c59bd32SSlawomir Mrozowicz 		if (session == NULL)
8202c59bd32SSlawomir Mrozowicz 			rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
821387259bdSDeclan Doherty 
8222c59bd32SSlawomir Mrozowicz 		port_cparams[i].session = session;
8232c59bd32SSlawomir Mrozowicz 
824387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
825387259bdSDeclan Doherty 				port_cparams[i].dev_id);
826387259bdSDeclan Doherty 	}
827387259bdSDeclan Doherty 
82841e97c2eSPablo de Lara 	l2fwd_crypto_options_print(options);
82941e97c2eSPablo de Lara 
83041e97c2eSPablo de Lara 	/*
83141e97c2eSPablo de Lara 	 * Initialize previous tsc timestamp before the loop,
83241e97c2eSPablo de Lara 	 * to avoid showing the port statistics immediately,
83341e97c2eSPablo de Lara 	 * so user can see the crypto information.
83441e97c2eSPablo de Lara 	 */
83541e97c2eSPablo de Lara 	prev_tsc = rte_rdtsc();
836387259bdSDeclan Doherty 	while (1) {
837387259bdSDeclan Doherty 
838387259bdSDeclan Doherty 		cur_tsc = rte_rdtsc();
839387259bdSDeclan Doherty 
840387259bdSDeclan Doherty 		/*
841268ca735SPablo de Lara 		 * Crypto device/TX burst queue drain
842387259bdSDeclan Doherty 		 */
843387259bdSDeclan Doherty 		diff_tsc = cur_tsc - prev_tsc;
844387259bdSDeclan Doherty 		if (unlikely(diff_tsc > drain_tsc)) {
845268ca735SPablo de Lara 			/* Enqueue all crypto ops remaining in buffers */
846268ca735SPablo de Lara 			for (i = 0; i < qconf->nb_crypto_devs; i++) {
847268ca735SPablo de Lara 				cparams = &port_cparams[i];
848268ca735SPablo de Lara 				len = qconf->op_buf[cparams->dev_id].len;
849268ca735SPablo de Lara 				l2fwd_crypto_send_burst(qconf, len, cparams);
850268ca735SPablo de Lara 				qconf->op_buf[cparams->dev_id].len = 0;
851268ca735SPablo de Lara 			}
852268ca735SPablo de Lara 			/* Transmit all packets remaining in buffers */
853387259bdSDeclan Doherty 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
854c0f87eb5SDeclan Doherty 				if (qconf->pkt_buf[portid].len == 0)
855387259bdSDeclan Doherty 					continue;
856387259bdSDeclan Doherty 				l2fwd_send_burst(&lcore_queue_conf[lcore_id],
857c0f87eb5SDeclan Doherty 						 qconf->pkt_buf[portid].len,
858e2cdfbd0SZhiyong Yang 						 portid);
859c0f87eb5SDeclan Doherty 				qconf->pkt_buf[portid].len = 0;
860387259bdSDeclan Doherty 			}
861387259bdSDeclan Doherty 
862387259bdSDeclan Doherty 			/* if timer is enabled */
863387259bdSDeclan Doherty 			if (timer_period > 0) {
864387259bdSDeclan Doherty 
865387259bdSDeclan Doherty 				/* advance the timer */
866387259bdSDeclan Doherty 				timer_tsc += diff_tsc;
867387259bdSDeclan Doherty 
868387259bdSDeclan Doherty 				/* if timer has reached its timeout */
869387259bdSDeclan Doherty 				if (unlikely(timer_tsc >=
870387259bdSDeclan Doherty 						(uint64_t)timer_period)) {
871387259bdSDeclan Doherty 
872387259bdSDeclan Doherty 					/* do this only on master core */
873ad509b4aSDeclan Doherty 					if (lcore_id == rte_get_master_lcore()
874ad509b4aSDeclan Doherty 						&& options->refresh_period) {
875387259bdSDeclan Doherty 						print_stats();
876387259bdSDeclan Doherty 						timer_tsc = 0;
877387259bdSDeclan Doherty 					}
878387259bdSDeclan Doherty 				}
879387259bdSDeclan Doherty 			}
880387259bdSDeclan Doherty 
881387259bdSDeclan Doherty 			prev_tsc = cur_tsc;
882387259bdSDeclan Doherty 		}
883387259bdSDeclan Doherty 
884387259bdSDeclan Doherty 		/*
885387259bdSDeclan Doherty 		 * Read packet from RX queues
886387259bdSDeclan Doherty 		 */
887387259bdSDeclan Doherty 		for (i = 0; i < qconf->nb_rx_ports; i++) {
888387259bdSDeclan Doherty 			portid = qconf->rx_port_list[i];
889387259bdSDeclan Doherty 
890387259bdSDeclan Doherty 			cparams = &port_cparams[i];
891387259bdSDeclan Doherty 
892e2cdfbd0SZhiyong Yang 			nb_rx = rte_eth_rx_burst(portid, 0,
893387259bdSDeclan Doherty 						 pkts_burst, MAX_PKT_BURST);
894387259bdSDeclan Doherty 
895387259bdSDeclan Doherty 			port_statistics[portid].rx += nb_rx;
896387259bdSDeclan Doherty 
897c0f87eb5SDeclan Doherty 			if (nb_rx) {
898387259bdSDeclan Doherty 				/*
899c0f87eb5SDeclan Doherty 				 * If we can't allocate a crypto_ops, then drop
900387259bdSDeclan Doherty 				 * the rest of the burst and dequeue and
901387259bdSDeclan Doherty 				 * process the packets to free offload structs
902387259bdSDeclan Doherty 				 */
903c0f87eb5SDeclan Doherty 				if (rte_crypto_op_bulk_alloc(
904c0f87eb5SDeclan Doherty 						l2fwd_crypto_op_pool,
905c0f87eb5SDeclan Doherty 						RTE_CRYPTO_OP_TYPE_SYMMETRIC,
906c0f87eb5SDeclan Doherty 						ops_burst, nb_rx) !=
907c0f87eb5SDeclan Doherty 								nb_rx) {
908c0f87eb5SDeclan Doherty 					for (j = 0; j < nb_rx; j++)
909d7acf6baSPablo de Lara 						rte_pktmbuf_free(pkts_burst[j]);
910c0f87eb5SDeclan Doherty 
911c0f87eb5SDeclan Doherty 					nb_rx = 0;
912387259bdSDeclan Doherty 				}
913387259bdSDeclan Doherty 
914c0f87eb5SDeclan Doherty 				/* Enqueue packets from Crypto device*/
915c0f87eb5SDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
916c0f87eb5SDeclan Doherty 					m = pkts_burst[j];
917387259bdSDeclan Doherty 
918c0f87eb5SDeclan Doherty 					l2fwd_simple_crypto_enqueue(m,
919c0f87eb5SDeclan Doherty 							ops_burst[j], cparams);
920c0f87eb5SDeclan Doherty 				}
921387259bdSDeclan Doherty 			}
922387259bdSDeclan Doherty 
923387259bdSDeclan Doherty 			/* Dequeue packets from Crypto device */
924c0f87eb5SDeclan Doherty 			do {
925387259bdSDeclan Doherty 				nb_rx = rte_cryptodev_dequeue_burst(
926387259bdSDeclan Doherty 						cparams->dev_id, cparams->qp_id,
927c0f87eb5SDeclan Doherty 						ops_burst, MAX_PKT_BURST);
928c0f87eb5SDeclan Doherty 
929c0f87eb5SDeclan Doherty 				crypto_statistics[cparams->dev_id].dequeued +=
930c0f87eb5SDeclan Doherty 						nb_rx;
931387259bdSDeclan Doherty 
932387259bdSDeclan Doherty 				/* Forward crypto'd packets */
933387259bdSDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
934c0f87eb5SDeclan Doherty 					m = ops_burst[j]->sym->m_src;
935c0f87eb5SDeclan Doherty 
936c0f87eb5SDeclan Doherty 					rte_crypto_op_free(ops_burst[j]);
937acdfecbaSKuba Kozak 					l2fwd_simple_forward(m, portid,
938acdfecbaSKuba Kozak 							options);
939387259bdSDeclan Doherty 				}
940c0f87eb5SDeclan Doherty 			} while (nb_rx == MAX_PKT_BURST);
941387259bdSDeclan Doherty 		}
942387259bdSDeclan Doherty 	}
943387259bdSDeclan Doherty }
944387259bdSDeclan Doherty 
945387259bdSDeclan Doherty static int
946387259bdSDeclan Doherty l2fwd_launch_one_lcore(void *arg)
947387259bdSDeclan Doherty {
948387259bdSDeclan Doherty 	l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
949387259bdSDeclan Doherty 	return 0;
950387259bdSDeclan Doherty }
951387259bdSDeclan Doherty 
952387259bdSDeclan Doherty /* Display command line arguments usage */
953387259bdSDeclan Doherty static void
954387259bdSDeclan Doherty l2fwd_crypto_usage(const char *prgname)
955387259bdSDeclan Doherty {
956912b3a0aSPablo de Lara 	printf("%s [EAL options] --\n"
957387259bdSDeclan Doherty 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
958387259bdSDeclan Doherty 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
95969a558e3SPablo de Lara 		"  -s manage all ports from single lcore\n"
960a3380989SPablo de Lara 		"  -T PERIOD: statistics will be refreshed each PERIOD seconds"
961387259bdSDeclan Doherty 		" (0 to disable, 10 default, 86400 maximum)\n"
962387259bdSDeclan Doherty 
963912b3a0aSPablo de Lara 		"  --cdev_type HW / SW / ANY\n"
964a3c2e34bSPablo de Lara 		"  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
9652661f4fbSPablo de Lara 		" HASH_ONLY / AEAD\n"
966387259bdSDeclan Doherty 
967387259bdSDeclan Doherty 		"  --cipher_algo ALGO\n"
968387259bdSDeclan Doherty 		"  --cipher_op ENCRYPT / DECRYPT\n"
969fcdbb3d5SPablo de Lara 		"  --cipher_key KEY (bytes separated with \":\")\n"
970a061e50aSPablo de Lara 		"  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
971acf86169SPablo de Lara 		"  --cipher_iv IV (bytes separated with \":\")\n"
972acf86169SPablo de Lara 		"  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
973387259bdSDeclan Doherty 
9743b98cbaaSPablo de Lara 		"  --auth_algo ALGO\n"
975387259bdSDeclan Doherty 		"  --auth_op GENERATE / VERIFY\n"
976fcdbb3d5SPablo de Lara 		"  --auth_key KEY (bytes separated with \":\")\n"
977a061e50aSPablo de Lara 		"  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
978acf86169SPablo de Lara 		"  --auth_iv IV (bytes separated with \":\")\n"
979acf86169SPablo de Lara 		"  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
9802661f4fbSPablo de Lara 
9812661f4fbSPablo de Lara 		"  --aead_algo ALGO\n"
9822661f4fbSPablo de Lara 		"  --aead_op ENCRYPT / DECRYPT\n"
9832661f4fbSPablo de Lara 		"  --aead_key KEY (bytes separated with \":\")\n"
9842661f4fbSPablo de Lara 		"  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
9852661f4fbSPablo de Lara 		"  --aead_iv IV (bytes separated with \":\")\n"
9862661f4fbSPablo de Lara 		"  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
987fcdbb3d5SPablo de Lara 		"  --aad AAD (bytes separated with \":\")\n"
988a061e50aSPablo de Lara 		"  --aad_random_size SIZE: size of AAD when generated randomly\n"
9892661f4fbSPablo de Lara 
990a061e50aSPablo de Lara 		"  --digest_size SIZE: size of digest to be generated/verified\n"
991387259bdSDeclan Doherty 
992d2797f51SFan Zhang 		"  --sessionless\n"
993acdfecbaSKuba Kozak 		"  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
994acdfecbaSKuba Kozak 
995acdfecbaSKuba Kozak 		"  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
996acdfecbaSKuba Kozak 		"      When enabled:\n"
997acdfecbaSKuba Kozak 		"       - The source MAC address is replaced by the TX port MAC address\n"
998acdfecbaSKuba Kozak 		"       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
999387259bdSDeclan Doherty 	       prgname);
1000387259bdSDeclan Doherty }
1001387259bdSDeclan Doherty 
1002387259bdSDeclan Doherty /** Parse crypto device type command line argument */
1003387259bdSDeclan Doherty static int
100427cf2d1bSPablo de Lara parse_cryptodev_type(enum cdev_type *type, char *optarg)
1005387259bdSDeclan Doherty {
100627cf2d1bSPablo de Lara 	if (strcmp("HW", optarg) == 0) {
100727cf2d1bSPablo de Lara 		*type = CDEV_TYPE_HW;
1008387259bdSDeclan Doherty 		return 0;
100927cf2d1bSPablo de Lara 	} else if (strcmp("SW", optarg) == 0) {
101027cf2d1bSPablo de Lara 		*type = CDEV_TYPE_SW;
101127cf2d1bSPablo de Lara 		return 0;
101227cf2d1bSPablo de Lara 	} else if (strcmp("ANY", optarg) == 0) {
101327cf2d1bSPablo de Lara 		*type = CDEV_TYPE_ANY;
1014387259bdSDeclan Doherty 		return 0;
1015387259bdSDeclan Doherty 	}
1016387259bdSDeclan Doherty 
1017387259bdSDeclan Doherty 	return -1;
1018387259bdSDeclan Doherty }
1019387259bdSDeclan Doherty 
1020387259bdSDeclan Doherty /** Parse crypto chain xform command line argument */
1021387259bdSDeclan Doherty static int
1022387259bdSDeclan Doherty parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1023387259bdSDeclan Doherty {
1024387259bdSDeclan Doherty 	if (strcmp("CIPHER_HASH", optarg) == 0) {
1025387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1026387259bdSDeclan Doherty 		return 0;
1027387259bdSDeclan Doherty 	} else if (strcmp("HASH_CIPHER", optarg) == 0) {
1028387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1029387259bdSDeclan Doherty 		return 0;
10301a75e9f3SPablo de Lara 	} else if (strcmp("CIPHER_ONLY", optarg) == 0) {
10311a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
10321a75e9f3SPablo de Lara 		return 0;
10331a75e9f3SPablo de Lara 	} else if (strcmp("HASH_ONLY", optarg) == 0) {
10341a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
10351a75e9f3SPablo de Lara 		return 0;
10362661f4fbSPablo de Lara 	} else if (strcmp("AEAD", optarg) == 0) {
10372661f4fbSPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_AEAD;
10382661f4fbSPablo de Lara 		return 0;
1039387259bdSDeclan Doherty 	}
1040387259bdSDeclan Doherty 
1041387259bdSDeclan Doherty 	return -1;
1042387259bdSDeclan Doherty }
1043387259bdSDeclan Doherty 
1044387259bdSDeclan Doherty /** Parse crypto cipher algo option command line argument */
1045387259bdSDeclan Doherty static int
1046387259bdSDeclan Doherty parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1047387259bdSDeclan Doherty {
104800c58901SPablo de Lara 
10494790f99dSPablo de Lara 	if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
10504790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Cipher algorithm specified "
10514790f99dSPablo de Lara 				"not supported!\n");
1052387259bdSDeclan Doherty 		return -1;
1053387259bdSDeclan Doherty 	}
1054387259bdSDeclan Doherty 
10554790f99dSPablo de Lara 	return 0;
10564790f99dSPablo de Lara }
10574790f99dSPablo de Lara 
1058387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1059387259bdSDeclan Doherty static int
1060387259bdSDeclan Doherty parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1061387259bdSDeclan Doherty {
1062387259bdSDeclan Doherty 	if (strcmp("ENCRYPT", optarg) == 0) {
1063387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1064387259bdSDeclan Doherty 		return 0;
1065387259bdSDeclan Doherty 	} else if (strcmp("DECRYPT", optarg) == 0) {
1066387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1067387259bdSDeclan Doherty 		return 0;
1068387259bdSDeclan Doherty 	}
1069387259bdSDeclan Doherty 
1070387259bdSDeclan Doherty 	printf("Cipher operation not supported!\n");
1071387259bdSDeclan Doherty 	return -1;
1072387259bdSDeclan Doherty }
1073387259bdSDeclan Doherty 
1074ff5d5b01SPablo de Lara /** Parse bytes from command line argument */
1075387259bdSDeclan Doherty static int
1076ff5d5b01SPablo de Lara parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1077387259bdSDeclan Doherty {
10781df9c010SPablo de Lara 	unsigned byte_count;
10791df9c010SPablo de Lara 	char *token;
10801df9c010SPablo de Lara 
108145544627SHemant Agrawal 	errno = 0;
10821df9c010SPablo de Lara 	for (byte_count = 0, token = strtok(input_arg, ":");
1083ff5d5b01SPablo de Lara 			(byte_count < max_size) && (token != NULL);
10841df9c010SPablo de Lara 			token = strtok(NULL, ":")) {
10851df9c010SPablo de Lara 
10861df9c010SPablo de Lara 		int number = (int)strtol(token, NULL, 16);
10871df9c010SPablo de Lara 
10881df9c010SPablo de Lara 		if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1089387259bdSDeclan Doherty 			return -1;
10901df9c010SPablo de Lara 
10911df9c010SPablo de Lara 		data[byte_count++] = (uint8_t)number;
10921df9c010SPablo de Lara 	}
10931df9c010SPablo de Lara 
1094a061e50aSPablo de Lara 	return byte_count;
1095a061e50aSPablo de Lara }
1096a061e50aSPablo de Lara 
1097a061e50aSPablo de Lara /** Parse size param*/
1098a061e50aSPablo de Lara static int
1099a061e50aSPablo de Lara parse_size(int *size, const char *q_arg)
1100a061e50aSPablo de Lara {
1101a061e50aSPablo de Lara 	char *end = NULL;
1102a061e50aSPablo de Lara 	unsigned long n;
1103a061e50aSPablo de Lara 
1104a061e50aSPablo de Lara 	/* parse hexadecimal string */
1105a061e50aSPablo de Lara 	n = strtoul(q_arg, &end, 10);
1106a061e50aSPablo de Lara 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1107a061e50aSPablo de Lara 		n = 0;
1108a061e50aSPablo de Lara 
1109a061e50aSPablo de Lara 	if (n == 0) {
1110a061e50aSPablo de Lara 		printf("invalid size\n");
1111a061e50aSPablo de Lara 		return -1;
1112a061e50aSPablo de Lara 	}
1113a061e50aSPablo de Lara 
1114a061e50aSPablo de Lara 	*size = n;
11151df9c010SPablo de Lara 	return 0;
1116387259bdSDeclan Doherty }
1117387259bdSDeclan Doherty 
1118387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1119387259bdSDeclan Doherty static int
1120387259bdSDeclan Doherty parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1121387259bdSDeclan Doherty {
11224790f99dSPablo de Lara 	if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
11234790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Authentication algorithm specified "
11244790f99dSPablo de Lara 				"not supported!\n");
1125387259bdSDeclan Doherty 		return -1;
1126387259bdSDeclan Doherty 	}
1127387259bdSDeclan Doherty 
11284790f99dSPablo de Lara 	return 0;
11294790f99dSPablo de Lara }
11304790f99dSPablo de Lara 
1131387259bdSDeclan Doherty static int
1132387259bdSDeclan Doherty parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1133387259bdSDeclan Doherty {
1134387259bdSDeclan Doherty 	if (strcmp("VERIFY", optarg) == 0) {
1135387259bdSDeclan Doherty 		*op = RTE_CRYPTO_AUTH_OP_VERIFY;
1136387259bdSDeclan Doherty 		return 0;
1137387259bdSDeclan Doherty 	} else if (strcmp("GENERATE", optarg) == 0) {
113872169087SPablo de Lara 		*op = RTE_CRYPTO_AUTH_OP_GENERATE;
1139387259bdSDeclan Doherty 		return 0;
1140387259bdSDeclan Doherty 	}
1141387259bdSDeclan Doherty 
1142387259bdSDeclan Doherty 	printf("Authentication operation specified not supported!\n");
1143387259bdSDeclan Doherty 	return -1;
1144387259bdSDeclan Doherty }
1145387259bdSDeclan Doherty 
1146d2797f51SFan Zhang static int
11472661f4fbSPablo de Lara parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
11482661f4fbSPablo de Lara {
11492661f4fbSPablo de Lara 	if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
11502661f4fbSPablo de Lara 		RTE_LOG(ERR, USER1, "AEAD algorithm specified "
11512661f4fbSPablo de Lara 				"not supported!\n");
11522661f4fbSPablo de Lara 		return -1;
11532661f4fbSPablo de Lara 	}
11542661f4fbSPablo de Lara 
11552661f4fbSPablo de Lara 	return 0;
11562661f4fbSPablo de Lara }
11572661f4fbSPablo de Lara 
11582661f4fbSPablo de Lara static int
11592661f4fbSPablo de Lara parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
11602661f4fbSPablo de Lara {
11612661f4fbSPablo de Lara 	if (strcmp("ENCRYPT", optarg) == 0) {
11622661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
11632661f4fbSPablo de Lara 		return 0;
11642661f4fbSPablo de Lara 	} else if (strcmp("DECRYPT", optarg) == 0) {
11652661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_DECRYPT;
11662661f4fbSPablo de Lara 		return 0;
11672661f4fbSPablo de Lara 	}
11682661f4fbSPablo de Lara 
11692661f4fbSPablo de Lara 	printf("AEAD operation specified not supported!\n");
11702661f4fbSPablo de Lara 	return -1;
11712661f4fbSPablo de Lara }
11722661f4fbSPablo de Lara static int
1173d2797f51SFan Zhang parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1174d2797f51SFan Zhang 		const char *q_arg)
1175d2797f51SFan Zhang {
1176d2797f51SFan Zhang 	char *end = NULL;
1177d2797f51SFan Zhang 	uint64_t pm;
1178d2797f51SFan Zhang 
1179d2797f51SFan Zhang 	/* parse hexadecimal string */
1180d2797f51SFan Zhang 	pm = strtoul(q_arg, &end, 16);
1181d2797f51SFan Zhang 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1182d2797f51SFan Zhang 		pm = 0;
1183d2797f51SFan Zhang 
1184d2797f51SFan Zhang 	options->cryptodev_mask = pm;
1185d2797f51SFan Zhang 	if (options->cryptodev_mask == 0) {
1186d2797f51SFan Zhang 		printf("invalid cryptodev_mask specified\n");
1187d2797f51SFan Zhang 		return -1;
1188d2797f51SFan Zhang 	}
1189d2797f51SFan Zhang 
1190d2797f51SFan Zhang 	return 0;
1191d2797f51SFan Zhang }
1192d2797f51SFan Zhang 
1193387259bdSDeclan Doherty /** Parse long options */
1194387259bdSDeclan Doherty static int
1195387259bdSDeclan Doherty l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1196387259bdSDeclan Doherty 		struct option *lgopts, int option_index)
1197387259bdSDeclan Doherty {
119827cf2d1bSPablo de Lara 	int retval;
119927cf2d1bSPablo de Lara 
120049f79e86SPablo de Lara 	if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
120149f79e86SPablo de Lara 		retval = parse_cryptodev_type(&options->type, optarg);
120249f79e86SPablo de Lara 		if (retval == 0)
12036723c0fcSBruce Richardson 			strlcpy(options->string_type, optarg, MAX_STR_LEN);
120449f79e86SPablo de Lara 		return retval;
120549f79e86SPablo de Lara 	}
1206387259bdSDeclan Doherty 
1207387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "chain") == 0)
1208387259bdSDeclan Doherty 		return parse_crypto_opt_chain(options, optarg);
1209387259bdSDeclan Doherty 
1210387259bdSDeclan Doherty 	/* Cipher options */
121100c58901SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
121200c58901SPablo de Lara 		return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1213387259bdSDeclan Doherty 				optarg);
1214387259bdSDeclan Doherty 
1215387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1216387259bdSDeclan Doherty 		return parse_cipher_op(&options->cipher_xform.cipher.op,
1217387259bdSDeclan Doherty 				optarg);
1218387259bdSDeclan Doherty 
1219a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
12201df9c010SPablo de Lara 		options->ckey_param = 1;
1221a061e50aSPablo de Lara 		options->cipher_xform.cipher.key.length =
1222ff5d5b01SPablo de Lara 			parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1223ff5d5b01SPablo de Lara 					MAX_KEY_SIZE);
1224a061e50aSPablo de Lara 		if (options->cipher_xform.cipher.key.length > 0)
1225a061e50aSPablo de Lara 			return 0;
1226a061e50aSPablo de Lara 		else
1227a061e50aSPablo de Lara 			return -1;
12281df9c010SPablo de Lara 	}
1229387259bdSDeclan Doherty 
1230a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1231a061e50aSPablo de Lara 		return parse_size(&options->ckey_random_size, optarg);
1232a061e50aSPablo de Lara 
1233acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1234acf86169SPablo de Lara 		options->cipher_iv_param = 1;
1235acf86169SPablo de Lara 		options->cipher_iv.length =
1236ff5d5b01SPablo de Lara 			parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1237acf86169SPablo de Lara 		if (options->cipher_iv.length > 0)
1238a061e50aSPablo de Lara 			return 0;
1239a061e50aSPablo de Lara 		else
1240a061e50aSPablo de Lara 			return -1;
12411df9c010SPablo de Lara 	}
1242387259bdSDeclan Doherty 
1243acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1244acf86169SPablo de Lara 		return parse_size(&options->cipher_iv_random_size, optarg);
1245a061e50aSPablo de Lara 
1246387259bdSDeclan Doherty 	/* Authentication options */
124727cf2d1bSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
124800c58901SPablo de Lara 		return parse_auth_algo(&options->auth_xform.auth.algo,
1249387259bdSDeclan Doherty 				optarg);
125027cf2d1bSPablo de Lara 	}
1251387259bdSDeclan Doherty 
1252387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
125372169087SPablo de Lara 		return parse_auth_op(&options->auth_xform.auth.op,
1254387259bdSDeclan Doherty 				optarg);
1255387259bdSDeclan Doherty 
1256a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
12571df9c010SPablo de Lara 		options->akey_param = 1;
1258a061e50aSPablo de Lara 		options->auth_xform.auth.key.length =
1259ff5d5b01SPablo de Lara 			parse_bytes(options->auth_xform.auth.key.data, optarg,
1260ff5d5b01SPablo de Lara 					MAX_KEY_SIZE);
1261a061e50aSPablo de Lara 		if (options->auth_xform.auth.key.length > 0)
1262a061e50aSPablo de Lara 			return 0;
1263a061e50aSPablo de Lara 		else
1264a061e50aSPablo de Lara 			return -1;
1265a061e50aSPablo de Lara 	}
1266a061e50aSPablo de Lara 
1267a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1268a061e50aSPablo de Lara 		return parse_size(&options->akey_random_size, optarg);
12691df9c010SPablo de Lara 	}
1270387259bdSDeclan Doherty 
1271acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1272acf86169SPablo de Lara 		options->auth_iv_param = 1;
1273acf86169SPablo de Lara 		options->auth_iv.length =
1274ff5d5b01SPablo de Lara 			parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1275acf86169SPablo de Lara 		if (options->auth_iv.length > 0)
1276acf86169SPablo de Lara 			return 0;
1277acf86169SPablo de Lara 		else
1278acf86169SPablo de Lara 			return -1;
1279acf86169SPablo de Lara 	}
1280acf86169SPablo de Lara 
1281acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1282acf86169SPablo de Lara 		return parse_size(&options->auth_iv_random_size, optarg);
1283acf86169SPablo de Lara 
12842661f4fbSPablo de Lara 	/* AEAD options */
12852661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
12862661f4fbSPablo de Lara 		return parse_aead_algo(&options->aead_xform.aead.algo,
12872661f4fbSPablo de Lara 				optarg);
12882661f4fbSPablo de Lara 	}
12892661f4fbSPablo de Lara 
12902661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
12912661f4fbSPablo de Lara 		return parse_aead_op(&options->aead_xform.aead.op,
12922661f4fbSPablo de Lara 				optarg);
12932661f4fbSPablo de Lara 
12942661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
12952661f4fbSPablo de Lara 		options->aead_key_param = 1;
12962661f4fbSPablo de Lara 		options->aead_xform.aead.key.length =
1297ff5d5b01SPablo de Lara 			parse_bytes(options->aead_xform.aead.key.data, optarg,
1298ff5d5b01SPablo de Lara 					MAX_KEY_SIZE);
12992661f4fbSPablo de Lara 		if (options->aead_xform.aead.key.length > 0)
13002661f4fbSPablo de Lara 			return 0;
13012661f4fbSPablo de Lara 		else
13022661f4fbSPablo de Lara 			return -1;
13032661f4fbSPablo de Lara 	}
13042661f4fbSPablo de Lara 
13052661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
13062661f4fbSPablo de Lara 		return parse_size(&options->aead_key_random_size, optarg);
13072661f4fbSPablo de Lara 
13082661f4fbSPablo de Lara 
13092661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
13102661f4fbSPablo de Lara 		options->aead_iv_param = 1;
13112661f4fbSPablo de Lara 		options->aead_iv.length =
1312ff5d5b01SPablo de Lara 			parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
13132661f4fbSPablo de Lara 		if (options->aead_iv.length > 0)
13142661f4fbSPablo de Lara 			return 0;
13152661f4fbSPablo de Lara 		else
13162661f4fbSPablo de Lara 			return -1;
13172661f4fbSPablo de Lara 	}
13182661f4fbSPablo de Lara 
13192661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
13202661f4fbSPablo de Lara 		return parse_size(&options->aead_iv_random_size, optarg);
13212661f4fbSPablo de Lara 
1322617a7949SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1323617a7949SPablo de Lara 		options->aad_param = 1;
1324a061e50aSPablo de Lara 		options->aad.length =
1325ff5d5b01SPablo de Lara 			parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1326a061e50aSPablo de Lara 		if (options->aad.length > 0)
1327a061e50aSPablo de Lara 			return 0;
1328a061e50aSPablo de Lara 		else
1329a061e50aSPablo de Lara 			return -1;
1330a061e50aSPablo de Lara 	}
1331a061e50aSPablo de Lara 
1332a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1333a061e50aSPablo de Lara 		return parse_size(&options->aad_random_size, optarg);
1334a061e50aSPablo de Lara 	}
1335a061e50aSPablo de Lara 
1336a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1337a061e50aSPablo de Lara 		return parse_size(&options->digest_size, optarg);
1338617a7949SPablo de Lara 	}
1339617a7949SPablo de Lara 
13401df9c010SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1341387259bdSDeclan Doherty 		options->sessionless = 1;
1342387259bdSDeclan Doherty 		return 0;
1343387259bdSDeclan Doherty 	}
1344387259bdSDeclan Doherty 
1345d2797f51SFan Zhang 	else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1346d2797f51SFan Zhang 		return parse_cryptodev_mask(options, optarg);
1347d2797f51SFan Zhang 
1348acdfecbaSKuba Kozak 	else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1349acdfecbaSKuba Kozak 		options->mac_updating = 1;
1350acdfecbaSKuba Kozak 		return 0;
1351acdfecbaSKuba Kozak 	}
1352acdfecbaSKuba Kozak 
1353acdfecbaSKuba Kozak 	else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1354acdfecbaSKuba Kozak 		options->mac_updating = 0;
1355acdfecbaSKuba Kozak 		return 0;
1356acdfecbaSKuba Kozak 	}
1357acdfecbaSKuba Kozak 
1358387259bdSDeclan Doherty 	return -1;
1359387259bdSDeclan Doherty }
1360387259bdSDeclan Doherty 
1361387259bdSDeclan Doherty /** Parse port mask */
1362387259bdSDeclan Doherty static int
1363387259bdSDeclan Doherty l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1364387259bdSDeclan Doherty 		const char *q_arg)
1365387259bdSDeclan Doherty {
1366387259bdSDeclan Doherty 	char *end = NULL;
1367387259bdSDeclan Doherty 	unsigned long pm;
1368387259bdSDeclan Doherty 
1369387259bdSDeclan Doherty 	/* parse hexadecimal string */
1370387259bdSDeclan Doherty 	pm = strtoul(q_arg, &end, 16);
1371387259bdSDeclan Doherty 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1372387259bdSDeclan Doherty 		pm = 0;
1373387259bdSDeclan Doherty 
1374387259bdSDeclan Doherty 	options->portmask = pm;
1375387259bdSDeclan Doherty 	if (options->portmask == 0) {
1376387259bdSDeclan Doherty 		printf("invalid portmask specified\n");
1377387259bdSDeclan Doherty 		return -1;
1378387259bdSDeclan Doherty 	}
1379387259bdSDeclan Doherty 
1380387259bdSDeclan Doherty 	return pm;
1381387259bdSDeclan Doherty }
1382387259bdSDeclan Doherty 
1383387259bdSDeclan Doherty /** Parse number of queues */
1384387259bdSDeclan Doherty static int
1385387259bdSDeclan Doherty l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1386387259bdSDeclan Doherty 		const char *q_arg)
1387387259bdSDeclan Doherty {
1388387259bdSDeclan Doherty 	char *end = NULL;
1389387259bdSDeclan Doherty 	unsigned long n;
1390387259bdSDeclan Doherty 
1391387259bdSDeclan Doherty 	/* parse hexadecimal string */
1392387259bdSDeclan Doherty 	n = strtoul(q_arg, &end, 10);
1393387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1394387259bdSDeclan Doherty 		n = 0;
1395387259bdSDeclan Doherty 	else if (n >= MAX_RX_QUEUE_PER_LCORE)
1396387259bdSDeclan Doherty 		n = 0;
1397387259bdSDeclan Doherty 
1398387259bdSDeclan Doherty 	options->nb_ports_per_lcore = n;
1399387259bdSDeclan Doherty 	if (options->nb_ports_per_lcore == 0) {
1400387259bdSDeclan Doherty 		printf("invalid number of ports selected\n");
1401387259bdSDeclan Doherty 		return -1;
1402387259bdSDeclan Doherty 	}
1403387259bdSDeclan Doherty 
1404387259bdSDeclan Doherty 	return 0;
1405387259bdSDeclan Doherty }
1406387259bdSDeclan Doherty 
1407387259bdSDeclan Doherty /** Parse timer period */
1408387259bdSDeclan Doherty static int
1409387259bdSDeclan Doherty l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1410387259bdSDeclan Doherty 		const char *q_arg)
1411387259bdSDeclan Doherty {
1412387259bdSDeclan Doherty 	char *end = NULL;
14133c96262cSPablo de Lara 	unsigned long n;
1414387259bdSDeclan Doherty 
1415387259bdSDeclan Doherty 	/* parse number string */
14163c96262cSPablo de Lara 	n = (unsigned)strtol(q_arg, &end, 10);
1417387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1418387259bdSDeclan Doherty 		n = 0;
1419387259bdSDeclan Doherty 
1420ad509b4aSDeclan Doherty 	if (n >= MAX_TIMER_PERIOD) {
14213c96262cSPablo de Lara 		printf("Warning refresh period specified %lu is greater than "
14223c96262cSPablo de Lara 				"max value %lu! using max value",
1423ad509b4aSDeclan Doherty 				n, MAX_TIMER_PERIOD);
1424ad509b4aSDeclan Doherty 		n = MAX_TIMER_PERIOD;
1425ad509b4aSDeclan Doherty 	}
1426387259bdSDeclan Doherty 
1427387259bdSDeclan Doherty 	options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1428387259bdSDeclan Doherty 
1429387259bdSDeclan Doherty 	return 0;
1430387259bdSDeclan Doherty }
1431387259bdSDeclan Doherty 
1432387259bdSDeclan Doherty /** Generate default options for application */
1433387259bdSDeclan Doherty static void
1434387259bdSDeclan Doherty l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1435387259bdSDeclan Doherty {
1436387259bdSDeclan Doherty 	options->portmask = 0xffffffff;
1437387259bdSDeclan Doherty 	options->nb_ports_per_lcore = 1;
1438387259bdSDeclan Doherty 	options->refresh_period = 10000;
1439387259bdSDeclan Doherty 	options->single_lcore = 0;
14403c96262cSPablo de Lara 	options->sessionless = 0;
1441387259bdSDeclan Doherty 
1442387259bdSDeclan Doherty 	options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1443387259bdSDeclan Doherty 
1444387259bdSDeclan Doherty 	/* Cipher Data */
14451bd407faSFiona Trahe 	options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1446387259bdSDeclan Doherty 	options->cipher_xform.next = NULL;
14471df9c010SPablo de Lara 	options->ckey_param = 0;
1448a061e50aSPablo de Lara 	options->ckey_random_size = -1;
1449a061e50aSPablo de Lara 	options->cipher_xform.cipher.key.length = 0;
1450acf86169SPablo de Lara 	options->cipher_iv_param = 0;
1451acf86169SPablo de Lara 	options->cipher_iv_random_size = -1;
1452acf86169SPablo de Lara 	options->cipher_iv.length = 0;
1453387259bdSDeclan Doherty 
1454387259bdSDeclan Doherty 	options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1455387259bdSDeclan Doherty 	options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1456387259bdSDeclan Doherty 
1457387259bdSDeclan Doherty 	/* Authentication Data */
14581bd407faSFiona Trahe 	options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1459387259bdSDeclan Doherty 	options->auth_xform.next = NULL;
14601df9c010SPablo de Lara 	options->akey_param = 0;
1461a061e50aSPablo de Lara 	options->akey_random_size = -1;
1462a061e50aSPablo de Lara 	options->auth_xform.auth.key.length = 0;
1463acf86169SPablo de Lara 	options->auth_iv_param = 0;
1464acf86169SPablo de Lara 	options->auth_iv_random_size = -1;
1465acf86169SPablo de Lara 	options->auth_iv.length = 0;
1466387259bdSDeclan Doherty 
1467387259bdSDeclan Doherty 	options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
146827cf2d1bSPablo de Lara 	options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1469387259bdSDeclan Doherty 
14702661f4fbSPablo de Lara 	/* AEAD Data */
14712661f4fbSPablo de Lara 	options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
14722661f4fbSPablo de Lara 	options->aead_xform.next = NULL;
14732661f4fbSPablo de Lara 	options->aead_key_param = 0;
14742661f4fbSPablo de Lara 	options->aead_key_random_size = -1;
14752661f4fbSPablo de Lara 	options->aead_xform.aead.key.length = 0;
14762661f4fbSPablo de Lara 	options->aead_iv_param = 0;
14772661f4fbSPablo de Lara 	options->aead_iv_random_size = -1;
14782661f4fbSPablo de Lara 	options->aead_iv.length = 0;
14792661f4fbSPablo de Lara 
1480d2c4b7d3SHemant Agrawal 	options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1481d2c4b7d3SHemant Agrawal 	options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1482b79e4c00SPablo de Lara 
14832661f4fbSPablo de Lara 	options->aad_param = 0;
14842661f4fbSPablo de Lara 	options->aad_random_size = -1;
14852661f4fbSPablo de Lara 	options->aad.length = 0;
14862661f4fbSPablo de Lara 
14872661f4fbSPablo de Lara 	options->digest_size = -1;
14882661f4fbSPablo de Lara 
148927cf2d1bSPablo de Lara 	options->type = CDEV_TYPE_ANY;
1490d2797f51SFan Zhang 	options->cryptodev_mask = UINT64_MAX;
1491acdfecbaSKuba Kozak 
1492acdfecbaSKuba Kozak 	options->mac_updating = 1;
1493387259bdSDeclan Doherty }
1494387259bdSDeclan Doherty 
1495387259bdSDeclan Doherty static void
149641e97c2eSPablo de Lara display_cipher_info(struct l2fwd_crypto_options *options)
149741e97c2eSPablo de Lara {
149841e97c2eSPablo de Lara 	printf("\n---- Cipher information ---\n");
149941e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
15004790f99dSPablo de Lara 		rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
150141e97c2eSPablo de Lara 	rte_hexdump(stdout, "Cipher key:",
150241e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.data,
150341e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.length);
1504acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
150541e97c2eSPablo de Lara }
150641e97c2eSPablo de Lara 
150741e97c2eSPablo de Lara static void
150841e97c2eSPablo de Lara display_auth_info(struct l2fwd_crypto_options *options)
150941e97c2eSPablo de Lara {
151041e97c2eSPablo de Lara 	printf("\n---- Authentication information ---\n");
151141e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
15124eb45d2dSPablo de Lara 		rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
151341e97c2eSPablo de Lara 	rte_hexdump(stdout, "Auth key:",
151441e97c2eSPablo de Lara 			options->auth_xform.auth.key.data,
151541e97c2eSPablo de Lara 			options->auth_xform.auth.key.length);
1516acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
15172661f4fbSPablo de Lara }
15182661f4fbSPablo de Lara 
15192661f4fbSPablo de Lara static void
15202661f4fbSPablo de Lara display_aead_info(struct l2fwd_crypto_options *options)
15212661f4fbSPablo de Lara {
15222661f4fbSPablo de Lara 	printf("\n---- AEAD information ---\n");
15232661f4fbSPablo de Lara 	printf("Algorithm: %s\n",
15242661f4fbSPablo de Lara 		rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
15252661f4fbSPablo de Lara 	rte_hexdump(stdout, "AEAD key:",
15262661f4fbSPablo de Lara 			options->aead_xform.aead.key.data,
15272661f4fbSPablo de Lara 			options->aead_xform.aead.key.length);
15282661f4fbSPablo de Lara 	rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
152941e97c2eSPablo de Lara 	rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
153041e97c2eSPablo de Lara }
153141e97c2eSPablo de Lara 
153241e97c2eSPablo de Lara static void
1533387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1534387259bdSDeclan Doherty {
153541e97c2eSPablo de Lara 	char string_cipher_op[MAX_STR_LEN];
153641e97c2eSPablo de Lara 	char string_auth_op[MAX_STR_LEN];
15372661f4fbSPablo de Lara 	char string_aead_op[MAX_STR_LEN];
153841e97c2eSPablo de Lara 
153941e97c2eSPablo de Lara 	if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
154041e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Encrypt");
154141e97c2eSPablo de Lara 	else
154241e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Decrypt");
154341e97c2eSPablo de Lara 
154441e97c2eSPablo de Lara 	if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
154541e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth generate");
154641e97c2eSPablo de Lara 	else
154741e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth verify");
154841e97c2eSPablo de Lara 
15492661f4fbSPablo de Lara 	if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
15502661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated encryption");
15512661f4fbSPablo de Lara 	else
15522661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated decryption");
15532661f4fbSPablo de Lara 
15542661f4fbSPablo de Lara 
1555387259bdSDeclan Doherty 	printf("Options:-\nn");
1556387259bdSDeclan Doherty 	printf("portmask: %x\n", options->portmask);
1557387259bdSDeclan Doherty 	printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1558387259bdSDeclan Doherty 	printf("refresh period : %u\n", options->refresh_period);
1559387259bdSDeclan Doherty 	printf("single lcore mode: %s\n",
1560387259bdSDeclan Doherty 			options->single_lcore ? "enabled" : "disabled");
1561387259bdSDeclan Doherty 	printf("stats_printing: %s\n",
1562ad509b4aSDeclan Doherty 			options->refresh_period == 0 ? "disabled" : "enabled");
1563387259bdSDeclan Doherty 
1564387259bdSDeclan Doherty 	printf("sessionless crypto: %s\n",
1565387259bdSDeclan Doherty 			options->sessionless ? "enabled" : "disabled");
156641e97c2eSPablo de Lara 
156741e97c2eSPablo de Lara 	if (options->ckey_param && (options->ckey_random_size != -1))
156841e97c2eSPablo de Lara 		printf("Cipher key already parsed, ignoring size of random key\n");
156941e97c2eSPablo de Lara 
157041e97c2eSPablo de Lara 	if (options->akey_param && (options->akey_random_size != -1))
157141e97c2eSPablo de Lara 		printf("Auth key already parsed, ignoring size of random key\n");
157241e97c2eSPablo de Lara 
1573acf86169SPablo de Lara 	if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1574acf86169SPablo de Lara 		printf("Cipher IV already parsed, ignoring size of random IV\n");
1575acf86169SPablo de Lara 
1576acf86169SPablo de Lara 	if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1577acf86169SPablo de Lara 		printf("Auth IV already parsed, ignoring size of random IV\n");
157841e97c2eSPablo de Lara 
157941e97c2eSPablo de Lara 	if (options->aad_param && (options->aad_random_size != -1))
158041e97c2eSPablo de Lara 		printf("AAD already parsed, ignoring size of random AAD\n");
158141e97c2eSPablo de Lara 
158241e97c2eSPablo de Lara 	printf("\nCrypto chain: ");
158341e97c2eSPablo de Lara 	switch (options->xform_chain) {
15842661f4fbSPablo de Lara 	case L2FWD_CRYPTO_AEAD:
15852661f4fbSPablo de Lara 		printf("Input --> %s --> Output\n", string_aead_op);
15862661f4fbSPablo de Lara 		display_aead_info(options);
15872661f4fbSPablo de Lara 		break;
158841e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_HASH:
158941e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
159041e97c2eSPablo de Lara 			string_cipher_op, string_auth_op);
159141e97c2eSPablo de Lara 		display_cipher_info(options);
159241e97c2eSPablo de Lara 		display_auth_info(options);
159341e97c2eSPablo de Lara 		break;
159441e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_CIPHER:
159541e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
159641e97c2eSPablo de Lara 			string_auth_op, string_cipher_op);
159741e97c2eSPablo de Lara 		display_cipher_info(options);
159841e97c2eSPablo de Lara 		display_auth_info(options);
159941e97c2eSPablo de Lara 		break;
160041e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_ONLY:
160141e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_auth_op);
160241e97c2eSPablo de Lara 		display_auth_info(options);
160341e97c2eSPablo de Lara 		break;
160441e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_ONLY:
160541e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_cipher_op);
160641e97c2eSPablo de Lara 		display_cipher_info(options);
160741e97c2eSPablo de Lara 		break;
160841e97c2eSPablo de Lara 	}
1609387259bdSDeclan Doherty }
1610387259bdSDeclan Doherty 
1611387259bdSDeclan Doherty /* Parse the argument given in the command line of the application */
1612387259bdSDeclan Doherty static int
1613387259bdSDeclan Doherty l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1614387259bdSDeclan Doherty 		int argc, char **argv)
1615387259bdSDeclan Doherty {
1616387259bdSDeclan Doherty 	int opt, retval, option_index;
1617387259bdSDeclan Doherty 	char **argvopt = argv, *prgname = argv[0];
1618387259bdSDeclan Doherty 
1619387259bdSDeclan Doherty 	static struct option lgopts[] = {
1620387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1621387259bdSDeclan Doherty 
1622387259bdSDeclan Doherty 			{ "cdev_type", required_argument, 0, 0 },
1623387259bdSDeclan Doherty 			{ "chain", required_argument, 0, 0 },
1624387259bdSDeclan Doherty 
1625387259bdSDeclan Doherty 			{ "cipher_algo", required_argument, 0, 0 },
1626387259bdSDeclan Doherty 			{ "cipher_op", required_argument, 0, 0 },
1627387259bdSDeclan Doherty 			{ "cipher_key", required_argument, 0, 0 },
1628a061e50aSPablo de Lara 			{ "cipher_key_random_size", required_argument, 0, 0 },
1629acf86169SPablo de Lara 			{ "cipher_iv", required_argument, 0, 0 },
1630acf86169SPablo de Lara 			{ "cipher_iv_random_size", required_argument, 0, 0 },
1631387259bdSDeclan Doherty 
1632387259bdSDeclan Doherty 			{ "auth_algo", required_argument, 0, 0 },
1633387259bdSDeclan Doherty 			{ "auth_op", required_argument, 0, 0 },
1634387259bdSDeclan Doherty 			{ "auth_key", required_argument, 0, 0 },
1635a061e50aSPablo de Lara 			{ "auth_key_random_size", required_argument, 0, 0 },
1636acf86169SPablo de Lara 			{ "auth_iv", required_argument, 0, 0 },
1637acf86169SPablo de Lara 			{ "auth_iv_random_size", required_argument, 0, 0 },
1638387259bdSDeclan Doherty 
16392661f4fbSPablo de Lara 			{ "aead_algo", required_argument, 0, 0 },
16402661f4fbSPablo de Lara 			{ "aead_op", required_argument, 0, 0 },
16412661f4fbSPablo de Lara 			{ "aead_key", required_argument, 0, 0 },
16422661f4fbSPablo de Lara 			{ "aead_key_random_size", required_argument, 0, 0 },
16432661f4fbSPablo de Lara 			{ "aead_iv", required_argument, 0, 0 },
16442661f4fbSPablo de Lara 			{ "aead_iv_random_size", required_argument, 0, 0 },
16452661f4fbSPablo de Lara 
1646617a7949SPablo de Lara 			{ "aad", required_argument, 0, 0 },
1647a061e50aSPablo de Lara 			{ "aad_random_size", required_argument, 0, 0 },
16482661f4fbSPablo de Lara 
1649a061e50aSPablo de Lara 			{ "digest_size", required_argument, 0, 0 },
1650387259bdSDeclan Doherty 
1651387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1652d2797f51SFan Zhang 			{ "cryptodev_mask", required_argument, 0, 0},
16533c96262cSPablo de Lara 
1654acdfecbaSKuba Kozak 			{ "mac-updating", no_argument, 0, 0},
1655acdfecbaSKuba Kozak 			{ "no-mac-updating", no_argument, 0, 0},
1656acdfecbaSKuba Kozak 
1657387259bdSDeclan Doherty 			{ NULL, 0, 0, 0 }
1658387259bdSDeclan Doherty 	};
1659387259bdSDeclan Doherty 
1660387259bdSDeclan Doherty 	l2fwd_crypto_default_options(options);
1661387259bdSDeclan Doherty 
1662f1ae15baSPablo de Lara 	while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1663387259bdSDeclan Doherty 			&option_index)) != EOF) {
1664387259bdSDeclan Doherty 		switch (opt) {
1665387259bdSDeclan Doherty 		/* long options */
1666387259bdSDeclan Doherty 		case 0:
1667387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_args_long_options(options,
1668387259bdSDeclan Doherty 					lgopts, option_index);
1669387259bdSDeclan Doherty 			if (retval < 0) {
1670387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1671387259bdSDeclan Doherty 				return -1;
1672387259bdSDeclan Doherty 			}
1673387259bdSDeclan Doherty 			break;
1674387259bdSDeclan Doherty 
1675387259bdSDeclan Doherty 		/* portmask */
1676387259bdSDeclan Doherty 		case 'p':
1677387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_portmask(options, optarg);
1678387259bdSDeclan Doherty 			if (retval < 0) {
1679387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1680387259bdSDeclan Doherty 				return -1;
1681387259bdSDeclan Doherty 			}
1682387259bdSDeclan Doherty 			break;
1683387259bdSDeclan Doherty 
1684387259bdSDeclan Doherty 		/* nqueue */
1685387259bdSDeclan Doherty 		case 'q':
1686387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_nqueue(options, optarg);
1687387259bdSDeclan Doherty 			if (retval < 0) {
1688387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1689387259bdSDeclan Doherty 				return -1;
1690387259bdSDeclan Doherty 			}
1691387259bdSDeclan Doherty 			break;
1692387259bdSDeclan Doherty 
1693387259bdSDeclan Doherty 		/* single  */
1694387259bdSDeclan Doherty 		case 's':
1695387259bdSDeclan Doherty 			options->single_lcore = 1;
1696387259bdSDeclan Doherty 
1697387259bdSDeclan Doherty 			break;
1698387259bdSDeclan Doherty 
1699387259bdSDeclan Doherty 		/* timer period */
1700a3380989SPablo de Lara 		case 'T':
1701387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_timer_period(options,
1702387259bdSDeclan Doherty 					optarg);
1703387259bdSDeclan Doherty 			if (retval < 0) {
1704387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1705387259bdSDeclan Doherty 				return -1;
1706387259bdSDeclan Doherty 			}
1707387259bdSDeclan Doherty 			break;
1708387259bdSDeclan Doherty 
1709387259bdSDeclan Doherty 		default:
1710387259bdSDeclan Doherty 			l2fwd_crypto_usage(prgname);
1711387259bdSDeclan Doherty 			return -1;
1712387259bdSDeclan Doherty 		}
1713387259bdSDeclan Doherty 	}
1714387259bdSDeclan Doherty 
1715387259bdSDeclan Doherty 
1716387259bdSDeclan Doherty 	if (optind >= 0)
1717387259bdSDeclan Doherty 		argv[optind-1] = prgname;
1718387259bdSDeclan Doherty 
1719387259bdSDeclan Doherty 	retval = optind-1;
17209d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
1721387259bdSDeclan Doherty 
1722387259bdSDeclan Doherty 	return retval;
1723387259bdSDeclan Doherty }
1724387259bdSDeclan Doherty 
1725387259bdSDeclan Doherty /* Check the link status of all ports in up to 9s, and print them finally */
1726387259bdSDeclan Doherty static void
17278728ccf3SThomas Monjalon check_all_ports_link_status(uint32_t port_mask)
1728387259bdSDeclan Doherty {
1729387259bdSDeclan Doherty #define CHECK_INTERVAL 100 /* 100ms */
1730387259bdSDeclan Doherty #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
173147523597SZhiyong Yang 	uint16_t portid;
173247523597SZhiyong Yang 	uint8_t count, all_ports_up, print_flag = 0;
1733387259bdSDeclan Doherty 	struct rte_eth_link link;
1734387259bdSDeclan Doherty 
1735387259bdSDeclan Doherty 	printf("\nChecking link status");
1736387259bdSDeclan Doherty 	fflush(stdout);
1737387259bdSDeclan Doherty 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1738387259bdSDeclan Doherty 		all_ports_up = 1;
17398728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(portid) {
1740387259bdSDeclan Doherty 			if ((port_mask & (1 << portid)) == 0)
1741387259bdSDeclan Doherty 				continue;
1742387259bdSDeclan Doherty 			memset(&link, 0, sizeof(link));
1743387259bdSDeclan Doherty 			rte_eth_link_get_nowait(portid, &link);
1744387259bdSDeclan Doherty 			/* print link status if flag set */
1745387259bdSDeclan Doherty 			if (print_flag == 1) {
1746387259bdSDeclan Doherty 				if (link.link_status)
174747523597SZhiyong Yang 					printf(
174847523597SZhiyong Yang 					"Port%d Link Up. Speed %u Mbps - %s\n",
174947523597SZhiyong Yang 						portid, link.link_speed,
1750387259bdSDeclan Doherty 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1751387259bdSDeclan Doherty 					("full-duplex") : ("half-duplex\n"));
1752387259bdSDeclan Doherty 				else
175347523597SZhiyong Yang 					printf("Port %d Link Down\n", portid);
1754387259bdSDeclan Doherty 				continue;
1755387259bdSDeclan Doherty 			}
1756387259bdSDeclan Doherty 			/* clear all_ports_up flag if any link down */
175709419f23SThomas Monjalon 			if (link.link_status == ETH_LINK_DOWN) {
1758387259bdSDeclan Doherty 				all_ports_up = 0;
1759387259bdSDeclan Doherty 				break;
1760387259bdSDeclan Doherty 			}
1761387259bdSDeclan Doherty 		}
1762387259bdSDeclan Doherty 		/* after finally printing all link status, get out */
1763387259bdSDeclan Doherty 		if (print_flag == 1)
1764387259bdSDeclan Doherty 			break;
1765387259bdSDeclan Doherty 
1766387259bdSDeclan Doherty 		if (all_ports_up == 0) {
1767387259bdSDeclan Doherty 			printf(".");
1768387259bdSDeclan Doherty 			fflush(stdout);
1769387259bdSDeclan Doherty 			rte_delay_ms(CHECK_INTERVAL);
1770387259bdSDeclan Doherty 		}
1771387259bdSDeclan Doherty 
1772387259bdSDeclan Doherty 		/* set the print_flag if all ports up or timeout */
1773387259bdSDeclan Doherty 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1774387259bdSDeclan Doherty 			print_flag = 1;
1775387259bdSDeclan Doherty 			printf("done\n");
1776387259bdSDeclan Doherty 		}
1777387259bdSDeclan Doherty 	}
1778387259bdSDeclan Doherty }
1779387259bdSDeclan Doherty 
178027cf2d1bSPablo de Lara /* Check if device has to be HW/SW or any */
1781387259bdSDeclan Doherty static int
17824baea68dSPablo de Lara check_type(const struct l2fwd_crypto_options *options,
17834baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info)
178427cf2d1bSPablo de Lara {
178527cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_HW &&
178627cf2d1bSPablo de Lara 			(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
178727cf2d1bSPablo de Lara 		return 0;
178827cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_SW &&
178927cf2d1bSPablo de Lara 			!(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
179027cf2d1bSPablo de Lara 		return 0;
179127cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_ANY)
179227cf2d1bSPablo de Lara 		return 0;
179327cf2d1bSPablo de Lara 
179427cf2d1bSPablo de Lara 	return -1;
179527cf2d1bSPablo de Lara }
179627cf2d1bSPablo de Lara 
17974baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
17984baea68dSPablo de Lara check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
17994baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18004baea68dSPablo de Lara 		uint8_t cdev_id)
18014baea68dSPablo de Lara {
18024baea68dSPablo de Lara 	unsigned int i = 0;
18034baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18044baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm cap_cipher_algo;
18054baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm opt_cipher_algo =
18064baea68dSPablo de Lara 					options->cipher_xform.cipher.algo;
18074baea68dSPablo de Lara 
18084baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18094baea68dSPablo de Lara 		cap_cipher_algo = cap->sym.cipher.algo;
18104baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
18114baea68dSPablo de Lara 			if (cap_cipher_algo == opt_cipher_algo) {
18124baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
18134baea68dSPablo de Lara 					break;
18144baea68dSPablo de Lara 			}
18154baea68dSPablo de Lara 		}
18164baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
18174baea68dSPablo de Lara 	}
18184baea68dSPablo de Lara 
18194baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18204baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18214baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
18224baea68dSPablo de Lara 			rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
18234baea68dSPablo de Lara 			cdev_id,
18244baea68dSPablo de Lara 			options->string_type);
18254baea68dSPablo de Lara 		return NULL;
18264baea68dSPablo de Lara 	}
18274baea68dSPablo de Lara 
18284baea68dSPablo de Lara 	return cap;
18294baea68dSPablo de Lara }
18304baea68dSPablo de Lara 
18314baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
18324baea68dSPablo de Lara check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
18334baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18344baea68dSPablo de Lara 		uint8_t cdev_id)
18354baea68dSPablo de Lara {
18364baea68dSPablo de Lara 	unsigned int i = 0;
18374baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18384baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm cap_auth_algo;
18394baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm opt_auth_algo =
18404baea68dSPablo de Lara 					options->auth_xform.auth.algo;
18414baea68dSPablo de Lara 
18424baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18434baea68dSPablo de Lara 		cap_auth_algo = cap->sym.auth.algo;
18444baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
18454baea68dSPablo de Lara 			if (cap_auth_algo == opt_auth_algo) {
18464baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
18474baea68dSPablo de Lara 					break;
18484baea68dSPablo de Lara 			}
18494baea68dSPablo de Lara 		}
18504baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
18514baea68dSPablo de Lara 	}
18524baea68dSPablo de Lara 
18534baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18544baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18554baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
18564baea68dSPablo de Lara 			rte_crypto_auth_algorithm_strings[opt_auth_algo],
18574baea68dSPablo de Lara 			cdev_id,
18584baea68dSPablo de Lara 			options->string_type);
18594baea68dSPablo de Lara 		return NULL;
18604baea68dSPablo de Lara 	}
18614baea68dSPablo de Lara 
18624baea68dSPablo de Lara 	return cap;
18634baea68dSPablo de Lara }
18644baea68dSPablo de Lara 
18652661f4fbSPablo de Lara static const struct rte_cryptodev_capabilities *
18662661f4fbSPablo de Lara check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
18672661f4fbSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18682661f4fbSPablo de Lara 		uint8_t cdev_id)
18692661f4fbSPablo de Lara {
18702661f4fbSPablo de Lara 	unsigned int i = 0;
18712661f4fbSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18722661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm cap_aead_algo;
18732661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm opt_aead_algo =
18742661f4fbSPablo de Lara 					options->aead_xform.aead.algo;
18752661f4fbSPablo de Lara 
18762661f4fbSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18772661f4fbSPablo de Lara 		cap_aead_algo = cap->sym.aead.algo;
18782661f4fbSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
18792661f4fbSPablo de Lara 			if (cap_aead_algo == opt_aead_algo) {
18802661f4fbSPablo de Lara 				if (check_type(options, dev_info) == 0)
18812661f4fbSPablo de Lara 					break;
18822661f4fbSPablo de Lara 			}
18832661f4fbSPablo de Lara 		}
18842661f4fbSPablo de Lara 		cap = &dev_info->capabilities[++i];
18852661f4fbSPablo de Lara 	}
18862661f4fbSPablo de Lara 
18872661f4fbSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18882661f4fbSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18892661f4fbSPablo de Lara 			" or device not of preferred type (%s)\n",
18902661f4fbSPablo de Lara 			rte_crypto_aead_algorithm_strings[opt_aead_algo],
18912661f4fbSPablo de Lara 			cdev_id,
18922661f4fbSPablo de Lara 			options->string_type);
18932661f4fbSPablo de Lara 		return NULL;
18942661f4fbSPablo de Lara 	}
18952661f4fbSPablo de Lara 
18962661f4fbSPablo de Lara 	return cap;
18972661f4fbSPablo de Lara }
18982661f4fbSPablo de Lara 
1899d2797f51SFan Zhang /* Check if the device is enabled by cryptodev_mask */
1900d2797f51SFan Zhang static int
1901d2797f51SFan Zhang check_cryptodev_mask(struct l2fwd_crypto_options *options,
1902d2797f51SFan Zhang 		uint8_t cdev_id)
1903d2797f51SFan Zhang {
1904d2797f51SFan Zhang 	if (options->cryptodev_mask & (1 << cdev_id))
1905d2797f51SFan Zhang 		return 0;
1906d2797f51SFan Zhang 
1907d2797f51SFan Zhang 	return -1;
1908d2797f51SFan Zhang }
1909d2797f51SFan Zhang 
1910a061e50aSPablo de Lara static inline int
1911a061e50aSPablo de Lara check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1912a061e50aSPablo de Lara 		uint16_t increment)
1913a061e50aSPablo de Lara {
1914a061e50aSPablo de Lara 	uint16_t supp_size;
1915a061e50aSPablo de Lara 
191637ebd9e1SPablo de Lara 	/* Single value */
191737ebd9e1SPablo de Lara 	if (increment == 0) {
191837ebd9e1SPablo de Lara 		if (length == min)
191937ebd9e1SPablo de Lara 			return 0;
192037ebd9e1SPablo de Lara 		else
192137ebd9e1SPablo de Lara 			return -1;
192237ebd9e1SPablo de Lara 	}
192337ebd9e1SPablo de Lara 
192437ebd9e1SPablo de Lara 	/* Range of values */
1925a061e50aSPablo de Lara 	for (supp_size = min; supp_size <= max; supp_size += increment) {
1926a061e50aSPablo de Lara 		if (length == supp_size)
1927a061e50aSPablo de Lara 			return 0;
1928a061e50aSPablo de Lara 	}
1929a061e50aSPablo de Lara 
1930a061e50aSPablo de Lara 	return -1;
1931a061e50aSPablo de Lara }
19320fbd75a9SPablo de Lara 
19330fbd75a9SPablo de Lara static int
19340fbd75a9SPablo de Lara check_iv_param(const struct rte_crypto_param_range *iv_range_size,
19350fbd75a9SPablo de Lara 		unsigned int iv_param, int iv_random_size,
1936a6fde4f1SPablo de Lara 		uint16_t iv_length)
19370fbd75a9SPablo de Lara {
19380fbd75a9SPablo de Lara 	/*
19390fbd75a9SPablo de Lara 	 * Check if length of provided IV is supported
19400fbd75a9SPablo de Lara 	 * by the algorithm chosen.
19410fbd75a9SPablo de Lara 	 */
19420fbd75a9SPablo de Lara 	if (iv_param) {
1943a6fde4f1SPablo de Lara 		if (check_supported_size(iv_length,
19440fbd75a9SPablo de Lara 				iv_range_size->min,
19450fbd75a9SPablo de Lara 				iv_range_size->max,
19460fbd75a9SPablo de Lara 				iv_range_size->increment)
1947a6fde4f1SPablo de Lara 					!= 0)
19480fbd75a9SPablo de Lara 			return -1;
19490fbd75a9SPablo de Lara 	/*
19500fbd75a9SPablo de Lara 	 * Check if length of IV to be randomly generated
19510fbd75a9SPablo de Lara 	 * is supported by the algorithm chosen.
19520fbd75a9SPablo de Lara 	 */
19530fbd75a9SPablo de Lara 	} else if (iv_random_size != -1) {
19540fbd75a9SPablo de Lara 		if (check_supported_size(iv_random_size,
19550fbd75a9SPablo de Lara 				iv_range_size->min,
19560fbd75a9SPablo de Lara 				iv_range_size->max,
19570fbd75a9SPablo de Lara 				iv_range_size->increment)
1958a6fde4f1SPablo de Lara 					!= 0)
19590fbd75a9SPablo de Lara 			return -1;
19600fbd75a9SPablo de Lara 	}
19610fbd75a9SPablo de Lara 
19620fbd75a9SPablo de Lara 	return 0;
19630fbd75a9SPablo de Lara }
19640fbd75a9SPablo de Lara 
196527cf2d1bSPablo de Lara static int
19666ae3fb9dSPablo de Lara check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id)
19676ae3fb9dSPablo de Lara {
19686ae3fb9dSPablo de Lara 	struct rte_cryptodev_info dev_info;
19696ae3fb9dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
19706ae3fb9dSPablo de Lara 
19716ae3fb9dSPablo de Lara 	rte_cryptodev_info_get(cdev_id, &dev_info);
19726ae3fb9dSPablo de Lara 
19736ae3fb9dSPablo de Lara 	/* Set AEAD parameters */
19746ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
19756ae3fb9dSPablo de Lara 		/* Check if device supports AEAD algo */
19766ae3fb9dSPablo de Lara 		cap = check_device_support_aead_algo(options, &dev_info,
19776ae3fb9dSPablo de Lara 						cdev_id);
19786ae3fb9dSPablo de Lara 		if (cap == NULL)
19796ae3fb9dSPablo de Lara 			return -1;
19806ae3fb9dSPablo de Lara 
19816ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.aead.iv_size,
19826ae3fb9dSPablo de Lara 				options->aead_iv_param,
19836ae3fb9dSPablo de Lara 				options->aead_iv_random_size,
19846ae3fb9dSPablo de Lara 				options->aead_iv.length) != 0) {
19856ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
19866ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
19876ae3fb9dSPablo de Lara 				cdev_id);
19886ae3fb9dSPablo de Lara 			return -1;
19896ae3fb9dSPablo de Lara 		}
19906ae3fb9dSPablo de Lara 
19916ae3fb9dSPablo de Lara 		/*
19926ae3fb9dSPablo de Lara 		 * Check if length of provided AEAD key is supported
19936ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
19946ae3fb9dSPablo de Lara 		 */
19956ae3fb9dSPablo de Lara 		if (options->aead_key_param) {
19966ae3fb9dSPablo de Lara 			if (check_supported_size(
19976ae3fb9dSPablo de Lara 					options->aead_xform.aead.key.length,
19986ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.min,
19996ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.max,
20006ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.increment)
20016ae3fb9dSPablo de Lara 						!= 0) {
20026ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20036ae3fb9dSPablo de Lara 					"Device %u does not support "
20046ae3fb9dSPablo de Lara 					"AEAD key length\n",
20056ae3fb9dSPablo de Lara 					cdev_id);
20066ae3fb9dSPablo de Lara 				return -1;
20076ae3fb9dSPablo de Lara 			}
20086ae3fb9dSPablo de Lara 		/*
20096ae3fb9dSPablo de Lara 		 * Check if length of the aead key to be randomly generated
20106ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
20116ae3fb9dSPablo de Lara 		 */
20126ae3fb9dSPablo de Lara 		} else if (options->aead_key_random_size != -1) {
20136ae3fb9dSPablo de Lara 			if (check_supported_size(options->aead_key_random_size,
20146ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.min,
20156ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.max,
20166ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.increment)
20176ae3fb9dSPablo de Lara 						!= 0) {
20186ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20196ae3fb9dSPablo de Lara 					"Device %u does not support "
20206ae3fb9dSPablo de Lara 					"AEAD key length\n",
20216ae3fb9dSPablo de Lara 					cdev_id);
20226ae3fb9dSPablo de Lara 				return -1;
20236ae3fb9dSPablo de Lara 			}
20246ae3fb9dSPablo de Lara 		}
20256ae3fb9dSPablo de Lara 
20266ae3fb9dSPablo de Lara 
20276ae3fb9dSPablo de Lara 		/*
20286ae3fb9dSPablo de Lara 		 * Check if length of provided AAD is supported
20296ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
20306ae3fb9dSPablo de Lara 		 */
20316ae3fb9dSPablo de Lara 		if (options->aad_param) {
20326ae3fb9dSPablo de Lara 			if (check_supported_size(options->aad.length,
20336ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.min,
20346ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.max,
20356ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.increment)
20366ae3fb9dSPablo de Lara 						!= 0) {
20376ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20386ae3fb9dSPablo de Lara 					"Device %u does not support "
20396ae3fb9dSPablo de Lara 					"AAD length\n",
20406ae3fb9dSPablo de Lara 					cdev_id);
20416ae3fb9dSPablo de Lara 				return -1;
20426ae3fb9dSPablo de Lara 			}
20436ae3fb9dSPablo de Lara 		/*
20446ae3fb9dSPablo de Lara 		 * Check if length of AAD to be randomly generated
20456ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
20466ae3fb9dSPablo de Lara 		 */
20476ae3fb9dSPablo de Lara 		} else if (options->aad_random_size != -1) {
20486ae3fb9dSPablo de Lara 			if (check_supported_size(options->aad_random_size,
20496ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.min,
20506ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.max,
20516ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.increment)
20526ae3fb9dSPablo de Lara 						!= 0) {
20536ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20546ae3fb9dSPablo de Lara 					"Device %u does not support "
20556ae3fb9dSPablo de Lara 					"AAD length\n",
20566ae3fb9dSPablo de Lara 					cdev_id);
20576ae3fb9dSPablo de Lara 				return -1;
20586ae3fb9dSPablo de Lara 			}
20596ae3fb9dSPablo de Lara 		}
20606ae3fb9dSPablo de Lara 
20616ae3fb9dSPablo de Lara 		/* Check if digest size is supported by the algorithm. */
20626ae3fb9dSPablo de Lara 		if (options->digest_size != -1) {
20636ae3fb9dSPablo de Lara 			if (check_supported_size(options->digest_size,
20646ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.min,
20656ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.max,
20666ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.increment)
20676ae3fb9dSPablo de Lara 						!= 0) {
20686ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20696ae3fb9dSPablo de Lara 					"Device %u does not support "
20706ae3fb9dSPablo de Lara 					"digest length\n",
20716ae3fb9dSPablo de Lara 					cdev_id);
20726ae3fb9dSPablo de Lara 				return -1;
20736ae3fb9dSPablo de Lara 			}
20746ae3fb9dSPablo de Lara 		}
20756ae3fb9dSPablo de Lara 	}
20766ae3fb9dSPablo de Lara 
20776ae3fb9dSPablo de Lara 	/* Set cipher parameters */
20786ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
20796ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
20806ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
20816ae3fb9dSPablo de Lara 		/* Check if device supports cipher algo */
20826ae3fb9dSPablo de Lara 		cap = check_device_support_cipher_algo(options, &dev_info,
20836ae3fb9dSPablo de Lara 						cdev_id);
20846ae3fb9dSPablo de Lara 		if (cap == NULL)
20856ae3fb9dSPablo de Lara 			return -1;
20866ae3fb9dSPablo de Lara 
20876ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.cipher.iv_size,
20886ae3fb9dSPablo de Lara 				options->cipher_iv_param,
20896ae3fb9dSPablo de Lara 				options->cipher_iv_random_size,
20906ae3fb9dSPablo de Lara 				options->cipher_iv.length) != 0) {
20916ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
20926ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
20936ae3fb9dSPablo de Lara 				cdev_id);
20946ae3fb9dSPablo de Lara 			return -1;
20956ae3fb9dSPablo de Lara 		}
20966ae3fb9dSPablo de Lara 
20976ae3fb9dSPablo de Lara 		/*
20986ae3fb9dSPablo de Lara 		 * Check if length of provided cipher key is supported
20996ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
21006ae3fb9dSPablo de Lara 		 */
21016ae3fb9dSPablo de Lara 		if (options->ckey_param) {
21026ae3fb9dSPablo de Lara 			if (check_supported_size(
21036ae3fb9dSPablo de Lara 					options->cipher_xform.cipher.key.length,
21046ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.min,
21056ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.max,
21066ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.increment)
21076ae3fb9dSPablo de Lara 						!= 0) {
21086ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21096ae3fb9dSPablo de Lara 					"Device %u does not support cipher "
21106ae3fb9dSPablo de Lara 					"key length\n",
21116ae3fb9dSPablo de Lara 					cdev_id);
21126ae3fb9dSPablo de Lara 				return -1;
21136ae3fb9dSPablo de Lara 			}
21146ae3fb9dSPablo de Lara 		/*
21156ae3fb9dSPablo de Lara 		 * Check if length of the cipher key to be randomly generated
21166ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
21176ae3fb9dSPablo de Lara 		 */
21186ae3fb9dSPablo de Lara 		} else if (options->ckey_random_size != -1) {
21196ae3fb9dSPablo de Lara 			if (check_supported_size(options->ckey_random_size,
21206ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.min,
21216ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.max,
21226ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.increment)
21236ae3fb9dSPablo de Lara 						!= 0) {
21246ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21256ae3fb9dSPablo de Lara 					"Device %u does not support cipher "
21266ae3fb9dSPablo de Lara 					"key length\n",
21276ae3fb9dSPablo de Lara 					cdev_id);
21286ae3fb9dSPablo de Lara 				return -1;
21296ae3fb9dSPablo de Lara 			}
21306ae3fb9dSPablo de Lara 		}
21316ae3fb9dSPablo de Lara 	}
21326ae3fb9dSPablo de Lara 
21336ae3fb9dSPablo de Lara 	/* Set auth parameters */
21346ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
21356ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
21366ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
21376ae3fb9dSPablo de Lara 		/* Check if device supports auth algo */
21386ae3fb9dSPablo de Lara 		cap = check_device_support_auth_algo(options, &dev_info,
21396ae3fb9dSPablo de Lara 						cdev_id);
21406ae3fb9dSPablo de Lara 		if (cap == NULL)
21416ae3fb9dSPablo de Lara 			return -1;
21426ae3fb9dSPablo de Lara 
21436ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.auth.iv_size,
21446ae3fb9dSPablo de Lara 				options->auth_iv_param,
21456ae3fb9dSPablo de Lara 				options->auth_iv_random_size,
21466ae3fb9dSPablo de Lara 				options->auth_iv.length) != 0) {
21476ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
21486ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
21496ae3fb9dSPablo de Lara 				cdev_id);
21506ae3fb9dSPablo de Lara 			return -1;
21516ae3fb9dSPablo de Lara 		}
21526ae3fb9dSPablo de Lara 		/*
21536ae3fb9dSPablo de Lara 		 * Check if length of provided auth key is supported
21546ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
21556ae3fb9dSPablo de Lara 		 */
21566ae3fb9dSPablo de Lara 		if (options->akey_param) {
21576ae3fb9dSPablo de Lara 			if (check_supported_size(
21586ae3fb9dSPablo de Lara 					options->auth_xform.auth.key.length,
21596ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.min,
21606ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.max,
21616ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.increment)
21626ae3fb9dSPablo de Lara 						!= 0) {
21636ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21646ae3fb9dSPablo de Lara 					"Device %u does not support auth "
21656ae3fb9dSPablo de Lara 					"key length\n",
21666ae3fb9dSPablo de Lara 					cdev_id);
21676ae3fb9dSPablo de Lara 				return -1;
21686ae3fb9dSPablo de Lara 			}
21696ae3fb9dSPablo de Lara 		/*
21706ae3fb9dSPablo de Lara 		 * Check if length of the auth key to be randomly generated
21716ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
21726ae3fb9dSPablo de Lara 		 */
21736ae3fb9dSPablo de Lara 		} else if (options->akey_random_size != -1) {
21746ae3fb9dSPablo de Lara 			if (check_supported_size(options->akey_random_size,
21756ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.min,
21766ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.max,
21776ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.increment)
21786ae3fb9dSPablo de Lara 						!= 0) {
21796ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21806ae3fb9dSPablo de Lara 					"Device %u does not support auth "
21816ae3fb9dSPablo de Lara 					"key length\n",
21826ae3fb9dSPablo de Lara 					cdev_id);
21836ae3fb9dSPablo de Lara 				return -1;
21846ae3fb9dSPablo de Lara 			}
21856ae3fb9dSPablo de Lara 		}
21866ae3fb9dSPablo de Lara 
21876ae3fb9dSPablo de Lara 		/* Check if digest size is supported by the algorithm. */
21886ae3fb9dSPablo de Lara 		if (options->digest_size != -1) {
21896ae3fb9dSPablo de Lara 			if (check_supported_size(options->digest_size,
21906ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.min,
21916ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.max,
21926ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.increment)
21936ae3fb9dSPablo de Lara 						!= 0) {
21946ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21956ae3fb9dSPablo de Lara 					"Device %u does not support "
21966ae3fb9dSPablo de Lara 					"digest length\n",
21976ae3fb9dSPablo de Lara 					cdev_id);
21986ae3fb9dSPablo de Lara 				return -1;
21996ae3fb9dSPablo de Lara 			}
22006ae3fb9dSPablo de Lara 		}
22016ae3fb9dSPablo de Lara 	}
22026ae3fb9dSPablo de Lara 
22036ae3fb9dSPablo de Lara 	return 0;
22046ae3fb9dSPablo de Lara }
22056ae3fb9dSPablo de Lara 
22066ae3fb9dSPablo de Lara static int
220727cf2d1bSPablo de Lara initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
220827cf2d1bSPablo de Lara 		uint8_t *enabled_cdevs)
2209387259bdSDeclan Doherty {
22106ae3fb9dSPablo de Lara 	uint8_t cdev_id, cdev_count, enabled_cdev_count = 0;
221127cf2d1bSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
22122c59bd32SSlawomir Mrozowicz 	unsigned int sess_sz, max_sess_sz = 0;
2213e3bcb99aSPablo de Lara 	uint32_t sessions_needed = 0;
2214387259bdSDeclan Doherty 	int retval;
2215387259bdSDeclan Doherty 
221627cf2d1bSPablo de Lara 	cdev_count = rte_cryptodev_count();
221727cf2d1bSPablo de Lara 	if (cdev_count == 0) {
221827cf2d1bSPablo de Lara 		printf("No crypto devices available\n");
2219387259bdSDeclan Doherty 		return -1;
2220387259bdSDeclan Doherty 	}
2221387259bdSDeclan Doherty 
22226ae3fb9dSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
22236ae3fb9dSPablo de Lara 			cdev_id++) {
22246ae3fb9dSPablo de Lara 		if (check_cryptodev_mask(options, cdev_id) < 0)
22256ae3fb9dSPablo de Lara 			continue;
22266ae3fb9dSPablo de Lara 
22276ae3fb9dSPablo de Lara 		if (check_capabilities(options, cdev_id) < 0)
22286ae3fb9dSPablo de Lara 			continue;
22296ae3fb9dSPablo de Lara 
2230a106fcceSPablo de Lara 		sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
22312c59bd32SSlawomir Mrozowicz 		if (sess_sz > max_sess_sz)
22322c59bd32SSlawomir Mrozowicz 			max_sess_sz = sess_sz;
22336ae3fb9dSPablo de Lara 
22346ae3fb9dSPablo de Lara 		l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
22356ae3fb9dSPablo de Lara 
22366ae3fb9dSPablo de Lara 		enabled_cdevs[cdev_id] = 1;
22376ae3fb9dSPablo de Lara 		enabled_cdev_count++;
22382c59bd32SSlawomir Mrozowicz 	}
22392c59bd32SSlawomir Mrozowicz 
22406ae3fb9dSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2241387259bdSDeclan Doherty 		struct rte_cryptodev_qp_conf qp_conf;
2242387259bdSDeclan Doherty 		struct rte_cryptodev_info dev_info;
22436ae3fb9dSPablo de Lara 
22446ae3fb9dSPablo de Lara 		if (enabled_cdevs[cdev_id] == 0)
22456ae3fb9dSPablo de Lara 			continue;
22466ae3fb9dSPablo de Lara 
22478dbc9bbfSPablo de Lara 		retval = rte_cryptodev_socket_id(cdev_id);
22488dbc9bbfSPablo de Lara 
22498dbc9bbfSPablo de Lara 		if (retval < 0) {
22508dbc9bbfSPablo de Lara 			printf("Invalid crypto device id used\n");
22518dbc9bbfSPablo de Lara 			return -1;
22528dbc9bbfSPablo de Lara 		}
22538dbc9bbfSPablo de Lara 
22548dbc9bbfSPablo de Lara 		uint8_t socket_id = (uint8_t) retval;
2255387259bdSDeclan Doherty 
2256387259bdSDeclan Doherty 		struct rte_cryptodev_config conf = {
2257387259bdSDeclan Doherty 			.nb_queue_pairs = 1,
22582c59bd32SSlawomir Mrozowicz 			.socket_id = socket_id,
2259387259bdSDeclan Doherty 		};
2260387259bdSDeclan Doherty 
2261387259bdSDeclan Doherty 		rte_cryptodev_info_get(cdev_id, &dev_info);
2262387259bdSDeclan Doherty 
2263e3bcb99aSPablo de Lara 		/*
2264e3bcb99aSPablo de Lara 		 * Two sessions objects are required for each session
2265e3bcb99aSPablo de Lara 		 * (one for the header, one for the private data)
2266e3bcb99aSPablo de Lara 		 */
2267e3bcb99aSPablo de Lara 		if (!strcmp(dev_info.driver_name, "crypto_scheduler")) {
2268e3bcb99aSPablo de Lara #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
2269e3bcb99aSPablo de Lara 			uint32_t nb_slaves =
2270e3bcb99aSPablo de Lara 				rte_cryptodev_scheduler_slaves_get(cdev_id,
2271e3bcb99aSPablo de Lara 								NULL);
2272e3bcb99aSPablo de Lara 
2273261bbff7SFan Zhang 			sessions_needed = enabled_cdev_count * nb_slaves;
2274e3bcb99aSPablo de Lara #endif
2275e3bcb99aSPablo de Lara 		} else
2276261bbff7SFan Zhang 			sessions_needed = enabled_cdev_count;
2277e3bcb99aSPablo de Lara 
2278261bbff7SFan Zhang 		if (session_pool_socket[socket_id].priv_mp == NULL) {
22792c59bd32SSlawomir Mrozowicz 			char mp_name[RTE_MEMPOOL_NAMESIZE];
22802c59bd32SSlawomir Mrozowicz 
22812c59bd32SSlawomir Mrozowicz 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2282261bbff7SFan Zhang 				"priv_sess_mp_%u", socket_id);
22832c59bd32SSlawomir Mrozowicz 
2284261bbff7SFan Zhang 			session_pool_socket[socket_id].priv_mp =
2285261bbff7SFan Zhang 					rte_mempool_create(mp_name,
2286e3bcb99aSPablo de Lara 						sessions_needed,
22872c59bd32SSlawomir Mrozowicz 						max_sess_sz,
2288261bbff7SFan Zhang 						0, 0, NULL, NULL, NULL,
22892c59bd32SSlawomir Mrozowicz 						NULL, socket_id,
22902c59bd32SSlawomir Mrozowicz 						0);
22912c59bd32SSlawomir Mrozowicz 
2292261bbff7SFan Zhang 			if (session_pool_socket[socket_id].priv_mp == NULL) {
2293261bbff7SFan Zhang 				printf("Cannot create pool on socket %d\n",
22942c59bd32SSlawomir Mrozowicz 					socket_id);
22952c59bd32SSlawomir Mrozowicz 				return -ENOMEM;
22962c59bd32SSlawomir Mrozowicz 			}
22972c59bd32SSlawomir Mrozowicz 
2298261bbff7SFan Zhang 			printf("Allocated pool \"%s\" on socket %d\n",
2299261bbff7SFan Zhang 				mp_name, socket_id);
2300261bbff7SFan Zhang 		}
2301261bbff7SFan Zhang 
2302261bbff7SFan Zhang 		if (session_pool_socket[socket_id].sess_mp == NULL) {
2303261bbff7SFan Zhang 			char mp_name[RTE_MEMPOOL_NAMESIZE];
2304261bbff7SFan Zhang 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2305261bbff7SFan Zhang 				"sess_mp_%u", socket_id);
2306261bbff7SFan Zhang 
2307261bbff7SFan Zhang 			session_pool_socket[socket_id].sess_mp =
2308261bbff7SFan Zhang 					rte_cryptodev_sym_session_pool_create(
2309261bbff7SFan Zhang 							mp_name,
2310261bbff7SFan Zhang 							sessions_needed,
2311261bbff7SFan Zhang 							0, 0, 0, socket_id);
2312261bbff7SFan Zhang 
2313261bbff7SFan Zhang 			if (session_pool_socket[socket_id].sess_mp == NULL) {
2314261bbff7SFan Zhang 				printf("Cannot create pool on socket %d\n",
2315261bbff7SFan Zhang 					socket_id);
2316261bbff7SFan Zhang 				return -ENOMEM;
2317261bbff7SFan Zhang 			}
2318261bbff7SFan Zhang 
2319261bbff7SFan Zhang 			printf("Allocated pool \"%s\" on socket %d\n",
2320261bbff7SFan Zhang 				mp_name, socket_id);
23212c59bd32SSlawomir Mrozowicz 		}
23222c59bd32SSlawomir Mrozowicz 
23232661f4fbSPablo de Lara 		/* Set AEAD parameters */
23242661f4fbSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
23252661f4fbSPablo de Lara 			cap = check_device_support_aead_algo(options, &dev_info,
23262661f4fbSPablo de Lara 							cdev_id);
23272661f4fbSPablo de Lara 
23282661f4fbSPablo de Lara 			options->block_size = cap->sym.aead.block_size;
23292661f4fbSPablo de Lara 
2330a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2331a6fde4f1SPablo de Lara 			if (options->aead_iv_param == 0) {
2332a6fde4f1SPablo de Lara 				if (options->aead_iv_random_size != -1)
2333a6fde4f1SPablo de Lara 					options->aead_iv.length =
2334a6fde4f1SPablo de Lara 						options->aead_iv_random_size;
2335a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2336a6fde4f1SPablo de Lara 				else
2337a6fde4f1SPablo de Lara 					options->aead_iv.length =
2338a6fde4f1SPablo de Lara 						cap->sym.aead.iv_size.min;
2339a6fde4f1SPablo de Lara 			}
23402661f4fbSPablo de Lara 
23410b920a5fSPablo de Lara 			/* Set key if not provided from command line */
23420b920a5fSPablo de Lara 			if (options->aead_key_param == 0) {
23430b920a5fSPablo de Lara 				if (options->aead_key_random_size != -1)
23442661f4fbSPablo de Lara 					options->aead_xform.aead.key.length =
2345459369fcSPablo de Lara 						options->aead_key_random_size;
23462661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
23470b920a5fSPablo de Lara 				else
23482661f4fbSPablo de Lara 					options->aead_xform.aead.key.length =
23492661f4fbSPablo de Lara 						cap->sym.aead.key_size.min;
23502661f4fbSPablo de Lara 
23512661f4fbSPablo de Lara 				generate_random_key(
23522661f4fbSPablo de Lara 					options->aead_xform.aead.key.data,
23532661f4fbSPablo de Lara 					options->aead_xform.aead.key.length);
23540b920a5fSPablo de Lara 			}
23552661f4fbSPablo de Lara 
23560b920a5fSPablo de Lara 			/* Set AAD if not provided from command line */
23570b920a5fSPablo de Lara 			if (options->aad_param == 0) {
23580b920a5fSPablo de Lara 				if (options->aad_random_size != -1)
23590b920a5fSPablo de Lara 					options->aad.length =
23600b920a5fSPablo de Lara 						options->aad_random_size;
23612661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
23620b920a5fSPablo de Lara 				else
23630b920a5fSPablo de Lara 					options->aad.length =
23640b920a5fSPablo de Lara 						cap->sym.auth.aad_size.min;
23650b920a5fSPablo de Lara 			}
23662661f4fbSPablo de Lara 
236746a0547fSPablo de Lara 			options->aead_xform.aead.aad_length =
23682661f4fbSPablo de Lara 						options->aad.length;
23692661f4fbSPablo de Lara 
23700b920a5fSPablo de Lara 			/* Set digest size if not provided from command line */
23710b920a5fSPablo de Lara 			if (options->digest_size != -1)
23722661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
23732661f4fbSPablo de Lara 							options->digest_size;
23742661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
23750b920a5fSPablo de Lara 			else
23762661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
23772661f4fbSPablo de Lara 						cap->sym.aead.digest_size.min;
23782661f4fbSPablo de Lara 		}
23792661f4fbSPablo de Lara 
238027cf2d1bSPablo de Lara 		/* Set cipher parameters */
238127cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
238227cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
238327cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
23844baea68dSPablo de Lara 			cap = check_device_support_cipher_algo(options, &dev_info,
23854baea68dSPablo de Lara 							cdev_id);
238627cf2d1bSPablo de Lara 			options->block_size = cap->sym.cipher.block_size;
23870fbd75a9SPablo de Lara 
2388a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2389a6fde4f1SPablo de Lara 			if (options->cipher_iv_param == 0) {
2390a6fde4f1SPablo de Lara 				if (options->cipher_iv_random_size != -1)
2391a6fde4f1SPablo de Lara 					options->cipher_iv.length =
2392a6fde4f1SPablo de Lara 						options->cipher_iv_random_size;
2393a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2394a6fde4f1SPablo de Lara 				else
2395a6fde4f1SPablo de Lara 					options->cipher_iv.length =
2396a6fde4f1SPablo de Lara 						cap->sym.cipher.iv_size.min;
2397a6fde4f1SPablo de Lara 			}
2398a061e50aSPablo de Lara 
23990b920a5fSPablo de Lara 			/* Set key if not provided from command line */
24000b920a5fSPablo de Lara 			if (options->ckey_param == 0) {
24010b920a5fSPablo de Lara 				if (options->ckey_random_size != -1)
2402a061e50aSPablo de Lara 					options->cipher_xform.cipher.key.length =
2403a061e50aSPablo de Lara 						options->ckey_random_size;
2404a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
24050b920a5fSPablo de Lara 				else
240627cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length =
240727cf2d1bSPablo de Lara 						cap->sym.cipher.key_size.min;
2408a061e50aSPablo de Lara 
240927cf2d1bSPablo de Lara 				generate_random_key(
241027cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.data,
241127cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length);
24120b920a5fSPablo de Lara 			}
241327cf2d1bSPablo de Lara 		}
241427cf2d1bSPablo de Lara 
241527cf2d1bSPablo de Lara 		/* Set auth parameters */
241627cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
241727cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
241827cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
24194baea68dSPablo de Lara 			cap = check_device_support_auth_algo(options, &dev_info,
24204baea68dSPablo de Lara 							cdev_id);
2421a6fde4f1SPablo de Lara 
2422a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2423a6fde4f1SPablo de Lara 			if (options->auth_iv_param == 0) {
2424a6fde4f1SPablo de Lara 				if (options->auth_iv_random_size != -1)
2425a6fde4f1SPablo de Lara 					options->auth_iv.length =
2426a6fde4f1SPablo de Lara 						options->auth_iv_random_size;
2427a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2428a6fde4f1SPablo de Lara 				else
2429a6fde4f1SPablo de Lara 					options->auth_iv.length =
2430a6fde4f1SPablo de Lara 						cap->sym.auth.iv_size.min;
2431a6fde4f1SPablo de Lara 			}
2432a6fde4f1SPablo de Lara 
24330b920a5fSPablo de Lara 			/* Set key if not provided from command line */
24340b920a5fSPablo de Lara 			if (options->akey_param == 0) {
24350b920a5fSPablo de Lara 				if (options->akey_random_size != -1)
2436a061e50aSPablo de Lara 					options->auth_xform.auth.key.length =
2437a061e50aSPablo de Lara 						options->akey_random_size;
2438a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
24390b920a5fSPablo de Lara 				else
244027cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length =
244127cf2d1bSPablo de Lara 						cap->sym.auth.key_size.min;
244227cf2d1bSPablo de Lara 
244327cf2d1bSPablo de Lara 				generate_random_key(
244427cf2d1bSPablo de Lara 					options->auth_xform.auth.key.data,
244527cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length);
2446a061e50aSPablo de Lara 			}
24470b920a5fSPablo de Lara 
24480b920a5fSPablo de Lara 			/* Set digest size if not provided from command line */
24490b920a5fSPablo de Lara 			if (options->digest_size != -1)
2450a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2451a061e50aSPablo de Lara 							options->digest_size;
2452a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
24530b920a5fSPablo de Lara 			else
2454a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2455a061e50aSPablo de Lara 						cap->sym.auth.digest_size.min;
245627cf2d1bSPablo de Lara 		}
2457387259bdSDeclan Doherty 
2458f7db6f82SPablo de Lara 		retval = rte_cryptodev_configure(cdev_id, &conf);
2459387259bdSDeclan Doherty 		if (retval < 0) {
2460387259bdSDeclan Doherty 			printf("Failed to configure cryptodev %u", cdev_id);
2461387259bdSDeclan Doherty 			return -1;
2462387259bdSDeclan Doherty 		}
2463387259bdSDeclan Doherty 
2464387259bdSDeclan Doherty 		qp_conf.nb_descriptors = 2048;
2465261bbff7SFan Zhang 		qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
2466261bbff7SFan Zhang 		qp_conf.mp_session_private =
2467261bbff7SFan Zhang 				session_pool_socket[socket_id].priv_mp;
2468387259bdSDeclan Doherty 
2469387259bdSDeclan Doherty 		retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2470725d2a7fSFan Zhang 				socket_id);
2471387259bdSDeclan Doherty 		if (retval < 0) {
2472387259bdSDeclan Doherty 			printf("Failed to setup queue pair %u on cryptodev %u",
2473387259bdSDeclan Doherty 					0, cdev_id);
2474387259bdSDeclan Doherty 			return -1;
2475387259bdSDeclan Doherty 		}
2476387259bdSDeclan Doherty 
2477800386e6SHemant Agrawal 		retval = rte_cryptodev_start(cdev_id);
2478800386e6SHemant Agrawal 		if (retval < 0) {
2479800386e6SHemant Agrawal 			printf("Failed to start device %u: error %d\n",
2480800386e6SHemant Agrawal 					cdev_id, retval);
2481800386e6SHemant Agrawal 			return -1;
2482800386e6SHemant Agrawal 		}
2483387259bdSDeclan Doherty 	}
2484387259bdSDeclan Doherty 
2485387259bdSDeclan Doherty 	return enabled_cdev_count;
2486387259bdSDeclan Doherty }
2487387259bdSDeclan Doherty 
2488387259bdSDeclan Doherty static int
2489387259bdSDeclan Doherty initialize_ports(struct l2fwd_crypto_options *options)
2490387259bdSDeclan Doherty {
24918728ccf3SThomas Monjalon 	uint16_t last_portid = 0, portid;
2492387259bdSDeclan Doherty 	unsigned enabled_portcount = 0;
2493d9a42a69SThomas Monjalon 	unsigned nb_ports = rte_eth_dev_count_avail();
2494387259bdSDeclan Doherty 
2495387259bdSDeclan Doherty 	if (nb_ports == 0) {
2496387259bdSDeclan Doherty 		printf("No Ethernet ports - bye\n");
2497387259bdSDeclan Doherty 		return -1;
2498387259bdSDeclan Doherty 	}
2499387259bdSDeclan Doherty 
2500387259bdSDeclan Doherty 	/* Reset l2fwd_dst_ports */
2501387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2502387259bdSDeclan Doherty 		l2fwd_dst_ports[portid] = 0;
2503387259bdSDeclan Doherty 
25048728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
2505387259bdSDeclan Doherty 		int retval;
2506f2b713e3SShahaf Shuler 		struct rte_eth_dev_info dev_info;
2507f2b713e3SShahaf Shuler 		struct rte_eth_rxconf rxq_conf;
2508f2b713e3SShahaf Shuler 		struct rte_eth_txconf txq_conf;
2509f2b713e3SShahaf Shuler 		struct rte_eth_conf local_port_conf = port_conf;
2510387259bdSDeclan Doherty 
2511387259bdSDeclan Doherty 		/* Skip ports that are not enabled */
2512387259bdSDeclan Doherty 		if ((options->portmask & (1 << portid)) == 0)
2513387259bdSDeclan Doherty 			continue;
2514387259bdSDeclan Doherty 
2515387259bdSDeclan Doherty 		/* init port */
2516e2cdfbd0SZhiyong Yang 		printf("Initializing port %u... ", portid);
2517387259bdSDeclan Doherty 		fflush(stdout);
2518f2b713e3SShahaf Shuler 		rte_eth_dev_info_get(portid, &dev_info);
2519f2b713e3SShahaf Shuler 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2520f2b713e3SShahaf Shuler 			local_port_conf.txmode.offloads |=
2521f2b713e3SShahaf Shuler 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2522f2b713e3SShahaf Shuler 		retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2523387259bdSDeclan Doherty 		if (retval < 0) {
2524387259bdSDeclan Doherty 			printf("Cannot configure device: err=%d, port=%u\n",
2525e2cdfbd0SZhiyong Yang 				  retval, portid);
2526387259bdSDeclan Doherty 			return -1;
2527387259bdSDeclan Doherty 		}
2528387259bdSDeclan Doherty 
252960efb44fSRoman Zhukov 		retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
253060efb44fSRoman Zhukov 							  &nb_txd);
253160efb44fSRoman Zhukov 		if (retval < 0) {
253260efb44fSRoman Zhukov 			printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2533e2cdfbd0SZhiyong Yang 				retval, portid);
253460efb44fSRoman Zhukov 			return -1;
253560efb44fSRoman Zhukov 		}
253660efb44fSRoman Zhukov 
2537387259bdSDeclan Doherty 		/* init one RX queue */
2538387259bdSDeclan Doherty 		fflush(stdout);
2539f2b713e3SShahaf Shuler 		rxq_conf = dev_info.default_rxconf;
2540f2b713e3SShahaf Shuler 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
2541387259bdSDeclan Doherty 		retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2542387259bdSDeclan Doherty 					     rte_eth_dev_socket_id(portid),
2543f2b713e3SShahaf Shuler 					     &rxq_conf, l2fwd_pktmbuf_pool);
2544387259bdSDeclan Doherty 		if (retval < 0) {
2545387259bdSDeclan Doherty 			printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2546e2cdfbd0SZhiyong Yang 					retval, portid);
2547387259bdSDeclan Doherty 			return -1;
2548387259bdSDeclan Doherty 		}
2549387259bdSDeclan Doherty 
2550387259bdSDeclan Doherty 		/* init one TX queue on each port */
2551387259bdSDeclan Doherty 		fflush(stdout);
2552f2b713e3SShahaf Shuler 		txq_conf = dev_info.default_txconf;
2553f2b713e3SShahaf Shuler 		txq_conf.offloads = local_port_conf.txmode.offloads;
2554387259bdSDeclan Doherty 		retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2555387259bdSDeclan Doherty 				rte_eth_dev_socket_id(portid),
2556f2b713e3SShahaf Shuler 				&txq_conf);
2557387259bdSDeclan Doherty 		if (retval < 0) {
2558387259bdSDeclan Doherty 			printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2559e2cdfbd0SZhiyong Yang 				retval, portid);
2560387259bdSDeclan Doherty 
2561387259bdSDeclan Doherty 			return -1;
2562387259bdSDeclan Doherty 		}
2563387259bdSDeclan Doherty 
2564387259bdSDeclan Doherty 		/* Start device */
2565387259bdSDeclan Doherty 		retval = rte_eth_dev_start(portid);
2566387259bdSDeclan Doherty 		if (retval < 0) {
2567387259bdSDeclan Doherty 			printf("rte_eth_dev_start:err=%d, port=%u\n",
2568e2cdfbd0SZhiyong Yang 					retval, portid);
2569387259bdSDeclan Doherty 			return -1;
2570387259bdSDeclan Doherty 		}
2571387259bdSDeclan Doherty 
2572387259bdSDeclan Doherty 		rte_eth_promiscuous_enable(portid);
2573387259bdSDeclan Doherty 
2574387259bdSDeclan Doherty 		rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2575387259bdSDeclan Doherty 
2576387259bdSDeclan Doherty 		printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2577e2cdfbd0SZhiyong Yang 				portid,
2578387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[0],
2579387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[1],
2580387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[2],
2581387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[3],
2582387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[4],
2583387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2584387259bdSDeclan Doherty 
2585387259bdSDeclan Doherty 		/* initialize port stats */
2586387259bdSDeclan Doherty 		memset(&port_statistics, 0, sizeof(port_statistics));
2587387259bdSDeclan Doherty 
2588387259bdSDeclan Doherty 		/* Setup port forwarding table */
2589387259bdSDeclan Doherty 		if (enabled_portcount % 2) {
2590387259bdSDeclan Doherty 			l2fwd_dst_ports[portid] = last_portid;
2591387259bdSDeclan Doherty 			l2fwd_dst_ports[last_portid] = portid;
2592387259bdSDeclan Doherty 		} else {
2593387259bdSDeclan Doherty 			last_portid = portid;
2594387259bdSDeclan Doherty 		}
2595387259bdSDeclan Doherty 
2596387259bdSDeclan Doherty 		l2fwd_enabled_port_mask |= (1 << portid);
2597387259bdSDeclan Doherty 		enabled_portcount++;
2598387259bdSDeclan Doherty 	}
2599387259bdSDeclan Doherty 
2600387259bdSDeclan Doherty 	if (enabled_portcount == 1) {
2601387259bdSDeclan Doherty 		l2fwd_dst_ports[last_portid] = last_portid;
2602387259bdSDeclan Doherty 	} else if (enabled_portcount % 2) {
2603387259bdSDeclan Doherty 		printf("odd number of ports in portmask- bye\n");
2604387259bdSDeclan Doherty 		return -1;
2605387259bdSDeclan Doherty 	}
2606387259bdSDeclan Doherty 
26078728ccf3SThomas Monjalon 	check_all_ports_link_status(l2fwd_enabled_port_mask);
2608387259bdSDeclan Doherty 
2609387259bdSDeclan Doherty 	return enabled_portcount;
2610387259bdSDeclan Doherty }
2611387259bdSDeclan Doherty 
26121df9c010SPablo de Lara static void
26131df9c010SPablo de Lara reserve_key_memory(struct l2fwd_crypto_options *options)
26141df9c010SPablo de Lara {
26151df9c010SPablo de Lara 	options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
26161df9c010SPablo de Lara 						MAX_KEY_SIZE, 0);
26171df9c010SPablo de Lara 	if (options->cipher_xform.cipher.key.data == NULL)
26181df9c010SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
26191df9c010SPablo de Lara 
26201df9c010SPablo de Lara 	options->auth_xform.auth.key.data = rte_malloc("auth key",
26211df9c010SPablo de Lara 						MAX_KEY_SIZE, 0);
26221df9c010SPablo de Lara 	if (options->auth_xform.auth.key.data == NULL)
26231df9c010SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
26241df9c010SPablo de Lara 
26252661f4fbSPablo de Lara 	options->aead_xform.aead.key.data = rte_malloc("aead key",
26262661f4fbSPablo de Lara 						MAX_KEY_SIZE, 0);
26272661f4fbSPablo de Lara 	if (options->aead_xform.aead.key.data == NULL)
26282661f4fbSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
26292661f4fbSPablo de Lara 
2630acf86169SPablo de Lara 	options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2631acf86169SPablo de Lara 	if (options->cipher_iv.data == NULL)
2632acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2633acf86169SPablo de Lara 
2634acf86169SPablo de Lara 	options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2635acf86169SPablo de Lara 	if (options->auth_iv.data == NULL)
2636acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2637617a7949SPablo de Lara 
26382661f4fbSPablo de Lara 	options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
26392661f4fbSPablo de Lara 	if (options->aead_iv.data == NULL)
26402661f4fbSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
26412661f4fbSPablo de Lara 
2642617a7949SPablo de Lara 	options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2643617a7949SPablo de Lara 	if (options->aad.data == NULL)
2644617a7949SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
264587cf4c6cSThomas Monjalon 	options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
26461df9c010SPablo de Lara }
26471df9c010SPablo de Lara 
2648387259bdSDeclan Doherty int
2649387259bdSDeclan Doherty main(int argc, char **argv)
2650387259bdSDeclan Doherty {
26518728ccf3SThomas Monjalon 	struct lcore_queue_conf *qconf = NULL;
2652387259bdSDeclan Doherty 	struct l2fwd_crypto_options options;
2653387259bdSDeclan Doherty 
2654e2cdfbd0SZhiyong Yang 	uint8_t nb_cryptodevs, cdev_id;
26558728ccf3SThomas Monjalon 	uint16_t portid;
26568728ccf3SThomas Monjalon 	unsigned lcore_id, rx_lcore_id = 0;
2657387259bdSDeclan Doherty 	int ret, enabled_cdevcount, enabled_portcount;
265827cf2d1bSPablo de Lara 	uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2659387259bdSDeclan Doherty 
2660387259bdSDeclan Doherty 	/* init EAL */
2661387259bdSDeclan Doherty 	ret = rte_eal_init(argc, argv);
2662387259bdSDeclan Doherty 	if (ret < 0)
2663387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2664387259bdSDeclan Doherty 	argc -= ret;
2665387259bdSDeclan Doherty 	argv += ret;
2666387259bdSDeclan Doherty 
26671df9c010SPablo de Lara 	/* reserve memory for Cipher/Auth key and IV */
26681df9c010SPablo de Lara 	reserve_key_memory(&options);
26691df9c010SPablo de Lara 
2670387259bdSDeclan Doherty 	/* parse application arguments (after the EAL ones) */
2671387259bdSDeclan Doherty 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
2672387259bdSDeclan Doherty 	if (ret < 0)
2673387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2674387259bdSDeclan Doherty 
2675acdfecbaSKuba Kozak 	printf("MAC updating %s\n",
2676acdfecbaSKuba Kozak 			options.mac_updating ? "enabled" : "disabled");
2677acdfecbaSKuba Kozak 
2678387259bdSDeclan Doherty 	/* create the mbuf pool */
2679c0f87eb5SDeclan Doherty 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2680c0f87eb5SDeclan Doherty 			sizeof(struct rte_crypto_op),
2681c0f87eb5SDeclan Doherty 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2682387259bdSDeclan Doherty 	if (l2fwd_pktmbuf_pool == NULL)
2683387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2684387259bdSDeclan Doherty 
2685387259bdSDeclan Doherty 	/* create crypto op pool */
2686c0f87eb5SDeclan Doherty 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2687e636243eSPablo de Lara 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2688c0f87eb5SDeclan Doherty 			rte_socket_id());
2689c0f87eb5SDeclan Doherty 	if (l2fwd_crypto_op_pool == NULL)
2690387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2691387259bdSDeclan Doherty 
2692387259bdSDeclan Doherty 	/* Enable Ethernet ports */
2693387259bdSDeclan Doherty 	enabled_portcount = initialize_ports(&options);
2694387259bdSDeclan Doherty 	if (enabled_portcount < 1)
2695387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2696387259bdSDeclan Doherty 
2697387259bdSDeclan Doherty 	/* Initialize the port/queue configuration of each logical core */
26988728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
2699387259bdSDeclan Doherty 
2700387259bdSDeclan Doherty 		/* skip ports that are not enabled */
2701387259bdSDeclan Doherty 		if ((options.portmask & (1 << portid)) == 0)
2702387259bdSDeclan Doherty 			continue;
2703387259bdSDeclan Doherty 
2704387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2705387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2706387259bdSDeclan Doherty 				rx_lcore_id++;
2707387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2708387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2709387259bdSDeclan Doherty 							"Not enough cores\n");
2710387259bdSDeclan Doherty 			}
2711387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2712387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2713387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2714387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2715387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2716387259bdSDeclan Doherty 				rx_lcore_id++;
2717387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2718387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2719387259bdSDeclan Doherty 							"Not enough cores\n");
2720387259bdSDeclan Doherty 			}
2721387259bdSDeclan Doherty 		}
2722387259bdSDeclan Doherty 
2723387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2724387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2725387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2726387259bdSDeclan Doherty 
2727387259bdSDeclan Doherty 		qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2728387259bdSDeclan Doherty 		qconf->nb_rx_ports++;
2729387259bdSDeclan Doherty 
2730e2cdfbd0SZhiyong Yang 		printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2731387259bdSDeclan Doherty 	}
2732387259bdSDeclan Doherty 
2733387259bdSDeclan Doherty 	/* Enable Crypto devices */
273427cf2d1bSPablo de Lara 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
273527cf2d1bSPablo de Lara 			enabled_cdevs);
273627cf2d1bSPablo de Lara 	if (enabled_cdevcount < 0)
273727cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
273827cf2d1bSPablo de Lara 
273927cf2d1bSPablo de Lara 	if (enabled_cdevcount < enabled_portcount)
274027cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
274127cf2d1bSPablo de Lara 				"has to be more or equal to number of ports (%d)\n",
274227cf2d1bSPablo de Lara 				enabled_cdevcount, enabled_portcount);
2743387259bdSDeclan Doherty 
2744387259bdSDeclan Doherty 	nb_cryptodevs = rte_cryptodev_count();
274527cf2d1bSPablo de Lara 
274627cf2d1bSPablo de Lara 	/* Initialize the port/cryptodev configuration of each logical core */
2747387259bdSDeclan Doherty 	for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2748387259bdSDeclan Doherty 			cdev_id < nb_cryptodevs && enabled_cdevcount;
2749387259bdSDeclan Doherty 			cdev_id++) {
275027cf2d1bSPablo de Lara 		/* Crypto op not supported by crypto device */
275127cf2d1bSPablo de Lara 		if (!enabled_cdevs[cdev_id])
2752387259bdSDeclan Doherty 			continue;
2753387259bdSDeclan Doherty 
2754387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2755387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2756387259bdSDeclan Doherty 				rx_lcore_id++;
2757387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2758387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2759387259bdSDeclan Doherty 							"Not enough cores\n");
2760387259bdSDeclan Doherty 			}
2761387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2762387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2763387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2764387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2765387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2766387259bdSDeclan Doherty 				rx_lcore_id++;
2767387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2768387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2769387259bdSDeclan Doherty 							"Not enough cores\n");
2770387259bdSDeclan Doherty 			}
2771387259bdSDeclan Doherty 		}
2772387259bdSDeclan Doherty 
2773387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2774387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2775387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2776387259bdSDeclan Doherty 
2777387259bdSDeclan Doherty 		qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2778387259bdSDeclan Doherty 		qconf->nb_crypto_devs++;
2779387259bdSDeclan Doherty 
2780387259bdSDeclan Doherty 		enabled_cdevcount--;
2781387259bdSDeclan Doherty 
2782387259bdSDeclan Doherty 		printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2783387259bdSDeclan Doherty 				(unsigned)cdev_id);
2784387259bdSDeclan Doherty 	}
2785387259bdSDeclan Doherty 
2786387259bdSDeclan Doherty 	/* launch per-lcore init on every lcore */
2787387259bdSDeclan Doherty 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2788387259bdSDeclan Doherty 			CALL_MASTER);
2789387259bdSDeclan Doherty 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2790387259bdSDeclan Doherty 		if (rte_eal_wait_lcore(lcore_id) < 0)
2791387259bdSDeclan Doherty 			return -1;
2792387259bdSDeclan Doherty 	}
2793387259bdSDeclan Doherty 
2794387259bdSDeclan Doherty 	return 0;
2795387259bdSDeclan Doherty }
2796