xref: /dpdk/examples/l2fwd-crypto/main.c (revision cab0c8f3c09ee9fd8fdd9bb73d24ea50df61698d)
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_branch_prediction.h>
24387259bdSDeclan Doherty #include <rte_common.h>
25387259bdSDeclan Doherty #include <rte_cryptodev.h>
26387259bdSDeclan Doherty #include <rte_cycles.h>
27387259bdSDeclan Doherty #include <rte_debug.h>
28387259bdSDeclan Doherty #include <rte_eal.h>
29387259bdSDeclan Doherty #include <rte_ether.h>
30387259bdSDeclan Doherty #include <rte_ethdev.h>
31387259bdSDeclan Doherty #include <rte_interrupts.h>
32387259bdSDeclan Doherty #include <rte_ip.h>
33387259bdSDeclan Doherty #include <rte_launch.h>
34387259bdSDeclan Doherty #include <rte_lcore.h>
35387259bdSDeclan Doherty #include <rte_log.h>
36387259bdSDeclan Doherty #include <rte_malloc.h>
37387259bdSDeclan Doherty #include <rte_mbuf.h>
38387259bdSDeclan Doherty #include <rte_memcpy.h>
39387259bdSDeclan Doherty #include <rte_memory.h>
40387259bdSDeclan Doherty #include <rte_mempool.h>
41387259bdSDeclan Doherty #include <rte_per_lcore.h>
42387259bdSDeclan Doherty #include <rte_prefetch.h>
43387259bdSDeclan Doherty #include <rte_random.h>
4441e97c2eSPablo de Lara #include <rte_hexdump.h>
45a8d0d473SBruce Richardson #ifdef RTE_CRYPTO_SCHEDULER
46e3bcb99aSPablo de Lara #include <rte_cryptodev_scheduler.h>
47e3bcb99aSPablo de Lara #endif
48387259bdSDeclan Doherty 
4927cf2d1bSPablo de Lara enum cdev_type {
5027cf2d1bSPablo de Lara 	CDEV_TYPE_ANY,
5127cf2d1bSPablo de Lara 	CDEV_TYPE_HW,
5227cf2d1bSPablo de Lara 	CDEV_TYPE_SW
5327cf2d1bSPablo de Lara };
5427cf2d1bSPablo de Lara 
55387259bdSDeclan Doherty #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
56387259bdSDeclan Doherty 
57387259bdSDeclan Doherty #define NB_MBUF   8192
58387259bdSDeclan Doherty 
5927cf2d1bSPablo de Lara #define MAX_STR_LEN 32
601df9c010SPablo de Lara #define MAX_KEY_SIZE 128
61ff5d5b01SPablo de Lara #define MAX_IV_SIZE 16
62ff5d5b01SPablo de Lara #define MAX_AAD_SIZE 65535
63387259bdSDeclan Doherty #define MAX_PKT_BURST 32
64387259bdSDeclan Doherty #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
652c59bd32SSlawomir Mrozowicz #define SESSION_POOL_CACHE_SIZE 0
66387259bdSDeclan Doherty 
67e636243eSPablo de Lara #define MAXIMUM_IV_LENGTH	16
68e636243eSPablo de Lara #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
69e636243eSPablo de Lara 				sizeof(struct rte_crypto_sym_op))
70e636243eSPablo de Lara 
71387259bdSDeclan Doherty /*
72387259bdSDeclan Doherty  * Configurable number of RX/TX ring descriptors
73387259bdSDeclan Doherty  */
74867a6c66SKevin Laatz #define RTE_TEST_RX_DESC_DEFAULT 1024
75867a6c66SKevin Laatz #define RTE_TEST_TX_DESC_DEFAULT 1024
763c96262cSPablo de Lara 
77387259bdSDeclan Doherty static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
78387259bdSDeclan Doherty static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
79387259bdSDeclan Doherty 
80387259bdSDeclan Doherty /* ethernet addresses of ports */
816d13ea8eSOlivier Matz static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
82387259bdSDeclan Doherty 
83387259bdSDeclan Doherty /* mask of enabled ports */
84387259bdSDeclan Doherty static uint64_t l2fwd_enabled_port_mask;
85387259bdSDeclan Doherty static uint64_t l2fwd_enabled_crypto_mask;
86387259bdSDeclan Doherty 
87387259bdSDeclan Doherty /* list of enabled ports */
88e2cdfbd0SZhiyong Yang static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
89387259bdSDeclan Doherty 
90387259bdSDeclan Doherty 
91387259bdSDeclan Doherty struct pkt_buffer {
92387259bdSDeclan Doherty 	unsigned len;
93387259bdSDeclan Doherty 	struct rte_mbuf *buffer[MAX_PKT_BURST];
94387259bdSDeclan Doherty };
95387259bdSDeclan Doherty 
96c0f87eb5SDeclan Doherty struct op_buffer {
97c0f87eb5SDeclan Doherty 	unsigned len;
98c0f87eb5SDeclan Doherty 	struct rte_crypto_op *buffer[MAX_PKT_BURST];
99c0f87eb5SDeclan Doherty };
100c0f87eb5SDeclan Doherty 
101387259bdSDeclan Doherty #define MAX_RX_QUEUE_PER_LCORE 16
102387259bdSDeclan Doherty #define MAX_TX_QUEUE_PER_PORT 16
103387259bdSDeclan Doherty 
104387259bdSDeclan Doherty enum l2fwd_crypto_xform_chain {
105387259bdSDeclan Doherty 	L2FWD_CRYPTO_CIPHER_HASH,
1061a75e9f3SPablo de Lara 	L2FWD_CRYPTO_HASH_CIPHER,
1071a75e9f3SPablo de Lara 	L2FWD_CRYPTO_CIPHER_ONLY,
1082661f4fbSPablo de Lara 	L2FWD_CRYPTO_HASH_ONLY,
1092661f4fbSPablo de Lara 	L2FWD_CRYPTO_AEAD
110387259bdSDeclan Doherty };
111387259bdSDeclan Doherty 
112a7f4562bSFiona Trahe struct l2fwd_key {
113a7f4562bSFiona Trahe 	uint8_t *data;
114a7f4562bSFiona Trahe 	uint32_t length;
115c4509373SSantosh Shukla 	rte_iova_t phys_addr;
116a7f4562bSFiona Trahe };
117a7f4562bSFiona Trahe 
1180fbd75a9SPablo de Lara struct l2fwd_iv {
1190fbd75a9SPablo de Lara 	uint8_t *data;
1200fbd75a9SPablo de Lara 	uint16_t length;
1210fbd75a9SPablo de Lara };
1220fbd75a9SPablo de Lara 
123387259bdSDeclan Doherty /** l2fwd crypto application command line options */
124387259bdSDeclan Doherty struct l2fwd_crypto_options {
125387259bdSDeclan Doherty 	unsigned portmask;
126387259bdSDeclan Doherty 	unsigned nb_ports_per_lcore;
127387259bdSDeclan Doherty 	unsigned refresh_period;
128387259bdSDeclan Doherty 	unsigned single_lcore:1;
129387259bdSDeclan Doherty 
13027cf2d1bSPablo de Lara 	enum cdev_type type;
131387259bdSDeclan Doherty 	unsigned sessionless:1;
132387259bdSDeclan Doherty 
133387259bdSDeclan Doherty 	enum l2fwd_crypto_xform_chain xform_chain;
134387259bdSDeclan Doherty 
1351bd407faSFiona Trahe 	struct rte_crypto_sym_xform cipher_xform;
1361df9c010SPablo de Lara 	unsigned ckey_param;
137a061e50aSPablo de Lara 	int ckey_random_size;
138186b14d6SFan Zhang 	uint8_t cipher_key[MAX_KEY_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;
147186b14d6SFan Zhang 	uint8_t auth_key[MAX_KEY_SIZE];
148617a7949SPablo de Lara 
149acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
150acf86169SPablo de Lara 	unsigned int auth_iv_param;
151acf86169SPablo de Lara 	int auth_iv_random_size;
152acf86169SPablo de Lara 
1532661f4fbSPablo de Lara 	struct rte_crypto_sym_xform aead_xform;
1542661f4fbSPablo de Lara 	unsigned int aead_key_param;
1552661f4fbSPablo de Lara 	int aead_key_random_size;
156186b14d6SFan Zhang 	uint8_t aead_key[MAX_KEY_SIZE];
1572661f4fbSPablo de Lara 
1582661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
1592661f4fbSPablo de Lara 	unsigned int aead_iv_param;
1602661f4fbSPablo de Lara 	int aead_iv_random_size;
1612661f4fbSPablo de Lara 
162617a7949SPablo de Lara 	struct l2fwd_key aad;
163617a7949SPablo de Lara 	unsigned aad_param;
164a061e50aSPablo de Lara 	int aad_random_size;
165a061e50aSPablo de Lara 
166a061e50aSPablo de Lara 	int digest_size;
16727cf2d1bSPablo de Lara 
16827cf2d1bSPablo de Lara 	uint16_t block_size;
16927cf2d1bSPablo de Lara 	char string_type[MAX_STR_LEN];
170d2797f51SFan Zhang 
171d2797f51SFan Zhang 	uint64_t cryptodev_mask;
172acdfecbaSKuba Kozak 
173acdfecbaSKuba Kozak 	unsigned int mac_updating;
174387259bdSDeclan Doherty };
175387259bdSDeclan Doherty 
176387259bdSDeclan Doherty /** l2fwd crypto lcore params */
177387259bdSDeclan Doherty struct l2fwd_crypto_params {
178387259bdSDeclan Doherty 	uint8_t dev_id;
179387259bdSDeclan Doherty 	uint8_t qp_id;
180387259bdSDeclan Doherty 
181387259bdSDeclan Doherty 	unsigned digest_length;
182387259bdSDeclan Doherty 	unsigned block_size;
18327cf2d1bSPablo de Lara 
184*cab0c8f3SMatan Azrad 	uint32_t cipher_dataunit_len;
185dcf384b4SMatan Azrad 
186acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
187acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
1882661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
189617a7949SPablo de Lara 	struct l2fwd_key aad;
1901bd407faSFiona Trahe 	struct rte_cryptodev_sym_session *session;
1911a75e9f3SPablo de Lara 
1921a75e9f3SPablo de Lara 	uint8_t do_cipher;
1931a75e9f3SPablo de Lara 	uint8_t do_hash;
1942661f4fbSPablo de Lara 	uint8_t do_aead;
19527cf2d1bSPablo de Lara 	uint8_t hash_verify;
196d29ea843SPablo de Lara 
197d29ea843SPablo de Lara 	enum rte_crypto_cipher_algorithm cipher_algo;
198d29ea843SPablo de Lara 	enum rte_crypto_auth_algorithm auth_algo;
1992661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm aead_algo;
200387259bdSDeclan Doherty };
201387259bdSDeclan Doherty 
202387259bdSDeclan Doherty /** lcore configuration */
203387259bdSDeclan Doherty struct lcore_queue_conf {
204387259bdSDeclan Doherty 	unsigned nb_rx_ports;
205e2cdfbd0SZhiyong Yang 	uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
206387259bdSDeclan Doherty 
207387259bdSDeclan Doherty 	unsigned nb_crypto_devs;
208387259bdSDeclan Doherty 	unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
209387259bdSDeclan Doherty 
210ad476dd3SPablo de Lara 	struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
211c0f87eb5SDeclan Doherty 	struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
212387259bdSDeclan Doherty } __rte_cache_aligned;
213387259bdSDeclan Doherty 
214387259bdSDeclan Doherty struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
215387259bdSDeclan Doherty 
216f2b713e3SShahaf Shuler static struct rte_eth_conf port_conf = {
217387259bdSDeclan Doherty 	.rxmode = {
2181df9c010SPablo de Lara 		.mq_mode = ETH_MQ_RX_NONE,
21935b2d13fSOlivier Matz 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
220387259bdSDeclan Doherty 		.split_hdr_size = 0,
221387259bdSDeclan Doherty 	},
222387259bdSDeclan Doherty 	.txmode = {
223387259bdSDeclan Doherty 		.mq_mode = ETH_MQ_TX_NONE,
224387259bdSDeclan Doherty 	},
225387259bdSDeclan Doherty };
226387259bdSDeclan Doherty 
227387259bdSDeclan Doherty struct rte_mempool *l2fwd_pktmbuf_pool;
228c0f87eb5SDeclan Doherty struct rte_mempool *l2fwd_crypto_op_pool;
229261bbff7SFan Zhang static struct {
230261bbff7SFan Zhang 	struct rte_mempool *sess_mp;
231261bbff7SFan Zhang 	struct rte_mempool *priv_mp;
232261bbff7SFan Zhang } session_pool_socket[RTE_MAX_NUMA_NODES];
233387259bdSDeclan Doherty 
234387259bdSDeclan Doherty /* Per-port statistics struct */
235387259bdSDeclan Doherty struct l2fwd_port_statistics {
236387259bdSDeclan Doherty 	uint64_t tx;
237387259bdSDeclan Doherty 	uint64_t rx;
238387259bdSDeclan Doherty 
239387259bdSDeclan Doherty 	uint64_t crypto_enqueued;
240387259bdSDeclan Doherty 	uint64_t crypto_dequeued;
241387259bdSDeclan Doherty 
242387259bdSDeclan Doherty 	uint64_t dropped;
243387259bdSDeclan Doherty } __rte_cache_aligned;
244387259bdSDeclan Doherty 
245387259bdSDeclan Doherty struct l2fwd_crypto_statistics {
246387259bdSDeclan Doherty 	uint64_t enqueued;
247387259bdSDeclan Doherty 	uint64_t dequeued;
248387259bdSDeclan Doherty 
249387259bdSDeclan Doherty 	uint64_t errors;
250387259bdSDeclan Doherty } __rte_cache_aligned;
251387259bdSDeclan Doherty 
252387259bdSDeclan Doherty struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
253572f0779SSlawomir Mrozowicz struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
254387259bdSDeclan Doherty 
255387259bdSDeclan Doherty /* A tsc-based timer responsible for triggering statistics printout */
256387259bdSDeclan Doherty #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
2573c96262cSPablo de Lara #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
258387259bdSDeclan Doherty 
259387259bdSDeclan Doherty /* default period is 10 seconds */
260387259bdSDeclan Doherty static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
261387259bdSDeclan Doherty 
262387259bdSDeclan Doherty /* Print out statistics on packets dropped */
263387259bdSDeclan Doherty static void
264387259bdSDeclan Doherty print_stats(void)
265387259bdSDeclan Doherty {
26628523d9aSPablo de Lara 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
26728523d9aSPablo de Lara 	uint64_t total_packets_enqueued, total_packets_dequeued,
26828523d9aSPablo de Lara 		total_packets_errors;
269e2cdfbd0SZhiyong Yang 	uint16_t portid;
270387259bdSDeclan Doherty 	uint64_t cdevid;
271387259bdSDeclan Doherty 
27228523d9aSPablo de Lara 	total_packets_dropped = 0;
27328523d9aSPablo de Lara 	total_packets_tx = 0;
27428523d9aSPablo de Lara 	total_packets_rx = 0;
27528523d9aSPablo de Lara 	total_packets_enqueued = 0;
27628523d9aSPablo de Lara 	total_packets_dequeued = 0;
27728523d9aSPablo de Lara 	total_packets_errors = 0;
278387259bdSDeclan Doherty 
279387259bdSDeclan Doherty 	const char clr[] = { 27, '[', '2', 'J', '\0' };
280387259bdSDeclan Doherty 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
281387259bdSDeclan Doherty 
282387259bdSDeclan Doherty 		/* Clear screen and move to top left */
283387259bdSDeclan Doherty 	printf("%s%s", clr, topLeft);
284387259bdSDeclan Doherty 
285387259bdSDeclan Doherty 	printf("\nPort statistics ====================================");
286387259bdSDeclan Doherty 
287387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
288387259bdSDeclan Doherty 		/* skip disabled ports */
289387259bdSDeclan Doherty 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
290387259bdSDeclan Doherty 			continue;
291387259bdSDeclan Doherty 		printf("\nStatistics for port %u ------------------------------"
292387259bdSDeclan Doherty 			   "\nPackets sent: %32"PRIu64
293387259bdSDeclan Doherty 			   "\nPackets received: %28"PRIu64
294387259bdSDeclan Doherty 			   "\nPackets dropped: %29"PRIu64,
295387259bdSDeclan Doherty 			   portid,
296387259bdSDeclan Doherty 			   port_statistics[portid].tx,
297387259bdSDeclan Doherty 			   port_statistics[portid].rx,
298387259bdSDeclan Doherty 			   port_statistics[portid].dropped);
299387259bdSDeclan Doherty 
300387259bdSDeclan Doherty 		total_packets_dropped += port_statistics[portid].dropped;
301387259bdSDeclan Doherty 		total_packets_tx += port_statistics[portid].tx;
302387259bdSDeclan Doherty 		total_packets_rx += port_statistics[portid].rx;
303387259bdSDeclan Doherty 	}
304387259bdSDeclan Doherty 	printf("\nCrypto statistics ==================================");
305387259bdSDeclan Doherty 
306387259bdSDeclan Doherty 	for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
307387259bdSDeclan Doherty 		/* skip disabled ports */
308ad476dd3SPablo de Lara 		if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
309387259bdSDeclan Doherty 			continue;
310387259bdSDeclan Doherty 		printf("\nStatistics for cryptodev %"PRIu64
311387259bdSDeclan Doherty 				" -------------------------"
312387259bdSDeclan Doherty 			   "\nPackets enqueued: %28"PRIu64
313387259bdSDeclan Doherty 			   "\nPackets dequeued: %28"PRIu64
314387259bdSDeclan Doherty 			   "\nPackets errors: %30"PRIu64,
315387259bdSDeclan Doherty 			   cdevid,
316387259bdSDeclan Doherty 			   crypto_statistics[cdevid].enqueued,
317387259bdSDeclan Doherty 			   crypto_statistics[cdevid].dequeued,
318387259bdSDeclan Doherty 			   crypto_statistics[cdevid].errors);
319387259bdSDeclan Doherty 
320387259bdSDeclan Doherty 		total_packets_enqueued += crypto_statistics[cdevid].enqueued;
321387259bdSDeclan Doherty 		total_packets_dequeued += crypto_statistics[cdevid].dequeued;
322387259bdSDeclan Doherty 		total_packets_errors += crypto_statistics[cdevid].errors;
323387259bdSDeclan Doherty 	}
324387259bdSDeclan Doherty 	printf("\nAggregate statistics ==============================="
325387259bdSDeclan Doherty 		   "\nTotal packets received: %22"PRIu64
326387259bdSDeclan Doherty 		   "\nTotal packets enqueued: %22"PRIu64
327387259bdSDeclan Doherty 		   "\nTotal packets dequeued: %22"PRIu64
328387259bdSDeclan Doherty 		   "\nTotal packets sent: %26"PRIu64
329387259bdSDeclan Doherty 		   "\nTotal packets dropped: %23"PRIu64
330387259bdSDeclan Doherty 		   "\nTotal packets crypto errors: %17"PRIu64,
331387259bdSDeclan Doherty 		   total_packets_rx,
332387259bdSDeclan Doherty 		   total_packets_enqueued,
333387259bdSDeclan Doherty 		   total_packets_dequeued,
334387259bdSDeclan Doherty 		   total_packets_tx,
335387259bdSDeclan Doherty 		   total_packets_dropped,
336387259bdSDeclan Doherty 		   total_packets_errors);
337387259bdSDeclan Doherty 	printf("\n====================================================\n");
3383ee6f706SGeorgiy Levashov 
3393ee6f706SGeorgiy Levashov 	fflush(stdout);
340387259bdSDeclan Doherty }
341387259bdSDeclan Doherty 
3429a212dc0SConor Fogarty /* l2fwd_crypto_send_burst 8< */
343387259bdSDeclan Doherty static int
344387259bdSDeclan Doherty l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
345387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
346387259bdSDeclan Doherty {
347c0f87eb5SDeclan Doherty 	struct rte_crypto_op **op_buffer;
348387259bdSDeclan Doherty 	unsigned ret;
349387259bdSDeclan Doherty 
350c0f87eb5SDeclan Doherty 	op_buffer = (struct rte_crypto_op **)
351c0f87eb5SDeclan Doherty 			qconf->op_buf[cparams->dev_id].buffer;
352387259bdSDeclan Doherty 
353c0f87eb5SDeclan Doherty 	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
354c0f87eb5SDeclan Doherty 			cparams->qp_id,	op_buffer, (uint16_t) n);
355c0f87eb5SDeclan Doherty 
356387259bdSDeclan Doherty 	crypto_statistics[cparams->dev_id].enqueued += ret;
357387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
358387259bdSDeclan Doherty 		crypto_statistics[cparams->dev_id].errors += (n - ret);
359387259bdSDeclan Doherty 		do {
360c0f87eb5SDeclan Doherty 			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
361c0f87eb5SDeclan Doherty 			rte_crypto_op_free(op_buffer[ret]);
362387259bdSDeclan Doherty 		} while (++ret < n);
363387259bdSDeclan Doherty 	}
364387259bdSDeclan Doherty 
365387259bdSDeclan Doherty 	return 0;
366387259bdSDeclan Doherty }
3679a212dc0SConor Fogarty /* >8 End of l2fwd_crypto_send_burst. */
368387259bdSDeclan Doherty 
3699a212dc0SConor Fogarty /* Crypto enqueue. 8< */
370387259bdSDeclan Doherty static int
371c0f87eb5SDeclan Doherty l2fwd_crypto_enqueue(struct rte_crypto_op *op,
372c0f87eb5SDeclan Doherty 		struct l2fwd_crypto_params *cparams)
373387259bdSDeclan Doherty {
374387259bdSDeclan Doherty 	unsigned lcore_id, len;
375387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
376387259bdSDeclan Doherty 
377387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
378387259bdSDeclan Doherty 
379387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
380c0f87eb5SDeclan Doherty 	len = qconf->op_buf[cparams->dev_id].len;
381c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].buffer[len] = op;
382387259bdSDeclan Doherty 	len++;
383387259bdSDeclan Doherty 
384c0f87eb5SDeclan Doherty 	/* enough ops to be sent */
385387259bdSDeclan Doherty 	if (len == MAX_PKT_BURST) {
386387259bdSDeclan Doherty 		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
387387259bdSDeclan Doherty 		len = 0;
388387259bdSDeclan Doherty 	}
389387259bdSDeclan Doherty 
390c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].len = len;
391387259bdSDeclan Doherty 	return 0;
392387259bdSDeclan Doherty }
3939a212dc0SConor Fogarty /* >8 End of crypto enqueue. */
394387259bdSDeclan Doherty 
395387259bdSDeclan Doherty static int
396387259bdSDeclan Doherty l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
397c0f87eb5SDeclan Doherty 		struct rte_crypto_op *op,
398387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
399387259bdSDeclan Doherty {
4006d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth_hdr;
401a7c528e5SOlivier Matz 	struct rte_ipv4_hdr *ip_hdr;
402387259bdSDeclan Doherty 
4035839fd20SPablo de Lara 	uint32_t ipdata_offset, data_len;
4045839fd20SPablo de Lara 	uint32_t pad_len = 0;
405387259bdSDeclan Doherty 	char *padding;
406387259bdSDeclan Doherty 
4076d13ea8eSOlivier Matz 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
408387259bdSDeclan Doherty 
4090c9da755SDavid Marchand 	if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
410387259bdSDeclan Doherty 		return -1;
411387259bdSDeclan Doherty 
4126d13ea8eSOlivier Matz 	ipdata_offset = sizeof(struct rte_ether_hdr);
413387259bdSDeclan Doherty 
414a7c528e5SOlivier Matz 	ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
415387259bdSDeclan Doherty 			ipdata_offset);
416387259bdSDeclan Doherty 
41724ac604eSOlivier Matz 	ipdata_offset += (ip_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK)
41824ac604eSOlivier Matz 			* RTE_IPV4_IHL_MULTIPLIER;
419387259bdSDeclan Doherty 
420387259bdSDeclan Doherty 
421387259bdSDeclan Doherty 	/* Zero pad data to be crypto'd so it is block aligned */
422387259bdSDeclan Doherty 	data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
423893fbab0SPiotr Azarewicz 
4246df38301SPablo de Lara 	if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify)
425893fbab0SPiotr Azarewicz 		data_len -= cparams->digest_length;
426893fbab0SPiotr Azarewicz 
4275839fd20SPablo de Lara 	if (cparams->do_cipher) {
4285839fd20SPablo de Lara 		/*
4295839fd20SPablo de Lara 		 * Following algorithms are block cipher algorithms,
4305839fd20SPablo de Lara 		 * and might need padding
4315839fd20SPablo de Lara 		 */
4325839fd20SPablo de Lara 		switch (cparams->cipher_algo) {
4335839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_CBC:
4345839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_ECB:
4355839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_DES_CBC:
4365839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_CBC:
4375839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_ECB:
4385839fd20SPablo de Lara 			if (data_len % cparams->block_size)
4395839fd20SPablo de Lara 				pad_len = cparams->block_size -
4405839fd20SPablo de Lara 					(data_len % cparams->block_size);
4415839fd20SPablo de Lara 			break;
442dcf384b4SMatan Azrad 		case RTE_CRYPTO_CIPHER_AES_XTS:
443dcf384b4SMatan Azrad 			if (cparams->cipher_dataunit_len != 0 &&
444dcf384b4SMatan Azrad 			    (data_len % cparams->cipher_dataunit_len))
445dcf384b4SMatan Azrad 				pad_len = cparams->cipher_dataunit_len -
446dcf384b4SMatan Azrad 					(data_len % cparams->cipher_dataunit_len);
447dcf384b4SMatan Azrad 			break;
4485839fd20SPablo de Lara 		default:
4495839fd20SPablo de Lara 			pad_len = 0;
4505839fd20SPablo de Lara 		}
451387259bdSDeclan Doherty 
452387259bdSDeclan Doherty 		if (pad_len) {
453387259bdSDeclan Doherty 			padding = rte_pktmbuf_append(m, pad_len);
454387259bdSDeclan Doherty 			if (unlikely(!padding))
455387259bdSDeclan Doherty 				return -1;
456387259bdSDeclan Doherty 
457387259bdSDeclan Doherty 			data_len += pad_len;
458387259bdSDeclan Doherty 			memset(padding, 0, pad_len);
459387259bdSDeclan Doherty 		}
4605839fd20SPablo de Lara 	}
461387259bdSDeclan Doherty 
462387259bdSDeclan Doherty 	/* Set crypto operation data parameters */
463c0f87eb5SDeclan Doherty 	rte_crypto_op_attach_sym_session(op, cparams->session);
464387259bdSDeclan Doherty 
4651a75e9f3SPablo de Lara 	if (cparams->do_hash) {
466acf86169SPablo de Lara 		if (cparams->auth_iv.length) {
467acf86169SPablo de Lara 			uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
468acf86169SPablo de Lara 						uint8_t *,
469acf86169SPablo de Lara 						IV_OFFSET +
470acf86169SPablo de Lara 						cparams->cipher_iv.length);
471acf86169SPablo de Lara 			/*
472acf86169SPablo de Lara 			 * Copy IV at the end of the crypto operation,
473acf86169SPablo de Lara 			 * after the cipher IV, if added
474acf86169SPablo de Lara 			 */
475acf86169SPablo de Lara 			rte_memcpy(iv_ptr, cparams->auth_iv.data,
476acf86169SPablo de Lara 					cparams->auth_iv.length);
477acf86169SPablo de Lara 		}
47827cf2d1bSPablo de Lara 		if (!cparams->hash_verify) {
479387259bdSDeclan Doherty 			/* Append space for digest to end of packet */
480c0f87eb5SDeclan Doherty 			op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
481387259bdSDeclan Doherty 				cparams->digest_length);
48227cf2d1bSPablo de Lara 		} else {
483893fbab0SPiotr Azarewicz 			op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
484893fbab0SPiotr Azarewicz 				uint8_t *) + ipdata_offset + data_len;
48527cf2d1bSPablo de Lara 		}
48627cf2d1bSPablo de Lara 
487bfa9a8a4SThomas Monjalon 		op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
488387259bdSDeclan Doherty 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
489387259bdSDeclan Doherty 
4901f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
4912773c86dSPablo de Lara 		if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
4921f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
4931f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
494d29ea843SPablo de Lara 			op->sym->auth.data.offset = ipdata_offset << 3;
495d29ea843SPablo de Lara 			op->sym->auth.data.length = data_len << 3;
496d29ea843SPablo de Lara 		} else {
497c0f87eb5SDeclan Doherty 			op->sym->auth.data.offset = ipdata_offset;
498c0f87eb5SDeclan Doherty 			op->sym->auth.data.length = data_len;
499d29ea843SPablo de Lara 		}
5001a75e9f3SPablo de Lara 	}
5011a75e9f3SPablo de Lara 
5021a75e9f3SPablo de Lara 	if (cparams->do_cipher) {
503e636243eSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
504e636243eSPablo de Lara 							IV_OFFSET);
505e636243eSPablo de Lara 		/* Copy IV at the end of the crypto operation */
506acf86169SPablo de Lara 		rte_memcpy(iv_ptr, cparams->cipher_iv.data,
507acf86169SPablo de Lara 				cparams->cipher_iv.length);
508e636243eSPablo de Lara 
5091f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
5102773c86dSPablo de Lara 		if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
5111f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
5121f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
513d29ea843SPablo de Lara 			op->sym->cipher.data.offset = ipdata_offset << 3;
514d29ea843SPablo de Lara 			op->sym->cipher.data.length = data_len << 3;
515d29ea843SPablo de Lara 		} else {
516c0f87eb5SDeclan Doherty 			op->sym->cipher.data.offset = ipdata_offset;
517c0f87eb5SDeclan Doherty 			op->sym->cipher.data.length = data_len;
5181a75e9f3SPablo de Lara 		}
519d29ea843SPablo de Lara 	}
520387259bdSDeclan Doherty 
5212661f4fbSPablo de Lara 	if (cparams->do_aead) {
5222661f4fbSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
5232661f4fbSPablo de Lara 							IV_OFFSET);
5242661f4fbSPablo de Lara 		/* Copy IV at the end of the crypto operation */
525ff5d5b01SPablo de Lara 		/*
526ff5d5b01SPablo de Lara 		 * If doing AES-CCM, nonce is copied one byte
527ff5d5b01SPablo de Lara 		 * after the start of IV field
528ff5d5b01SPablo de Lara 		 */
529ff5d5b01SPablo de Lara 		if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
530ff5d5b01SPablo de Lara 			rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
531ff5d5b01SPablo de Lara 					cparams->aead_iv.length);
532ff5d5b01SPablo de Lara 		else
533ff5d5b01SPablo de Lara 			rte_memcpy(iv_ptr, cparams->aead_iv.data,
534ff5d5b01SPablo de Lara 					cparams->aead_iv.length);
5352661f4fbSPablo de Lara 
5362661f4fbSPablo de Lara 		op->sym->aead.data.offset = ipdata_offset;
5372661f4fbSPablo de Lara 		op->sym->aead.data.length = data_len;
5382661f4fbSPablo de Lara 
5392661f4fbSPablo de Lara 		if (!cparams->hash_verify) {
5402661f4fbSPablo de Lara 			/* Append space for digest to end of packet */
5412661f4fbSPablo de Lara 			op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
5422661f4fbSPablo de Lara 				cparams->digest_length);
5432661f4fbSPablo de Lara 		} else {
5442661f4fbSPablo de Lara 			op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
5452661f4fbSPablo de Lara 				uint8_t *) + ipdata_offset + data_len;
5462661f4fbSPablo de Lara 		}
5472661f4fbSPablo de Lara 
548bfa9a8a4SThomas Monjalon 		op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
5492661f4fbSPablo de Lara 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
5502661f4fbSPablo de Lara 
5512661f4fbSPablo de Lara 		if (cparams->aad.length) {
5522661f4fbSPablo de Lara 			op->sym->aead.aad.data = cparams->aad.data;
5532661f4fbSPablo de Lara 			op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
5542661f4fbSPablo de Lara 		}
5552661f4fbSPablo de Lara 	}
5562661f4fbSPablo de Lara 
557c0f87eb5SDeclan Doherty 	op->sym->m_src = m;
558c0f87eb5SDeclan Doherty 
559c0f87eb5SDeclan Doherty 	return l2fwd_crypto_enqueue(op, cparams);
560387259bdSDeclan Doherty }
561387259bdSDeclan Doherty 
562387259bdSDeclan Doherty 
563387259bdSDeclan Doherty /* Send the burst of packets on an output interface */
564387259bdSDeclan Doherty static int
565c0f87eb5SDeclan Doherty l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
56647523597SZhiyong Yang 		uint16_t port)
567387259bdSDeclan Doherty {
568387259bdSDeclan Doherty 	struct rte_mbuf **pkt_buffer;
569387259bdSDeclan Doherty 	unsigned ret;
570387259bdSDeclan Doherty 
571c0f87eb5SDeclan Doherty 	pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
572387259bdSDeclan Doherty 
573c0f87eb5SDeclan Doherty 	ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
574387259bdSDeclan Doherty 	port_statistics[port].tx += ret;
575387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
576387259bdSDeclan Doherty 		port_statistics[port].dropped += (n - ret);
577387259bdSDeclan Doherty 		do {
578387259bdSDeclan Doherty 			rte_pktmbuf_free(pkt_buffer[ret]);
579387259bdSDeclan Doherty 		} while (++ret < n);
580387259bdSDeclan Doherty 	}
581387259bdSDeclan Doherty 
582387259bdSDeclan Doherty 	return 0;
583387259bdSDeclan Doherty }
584387259bdSDeclan Doherty 
5859a212dc0SConor Fogarty /* Enqueue packets for TX and prepare them to be sent. 8< */
586387259bdSDeclan Doherty static int
58747523597SZhiyong Yang l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
588387259bdSDeclan Doherty {
589387259bdSDeclan Doherty 	unsigned lcore_id, len;
590387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
591387259bdSDeclan Doherty 
592387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
593387259bdSDeclan Doherty 
594387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
595c0f87eb5SDeclan Doherty 	len = qconf->pkt_buf[port].len;
596c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].buffer[len] = m;
597387259bdSDeclan Doherty 	len++;
598387259bdSDeclan Doherty 
599387259bdSDeclan Doherty 	/* enough pkts to be sent */
600387259bdSDeclan Doherty 	if (unlikely(len == MAX_PKT_BURST)) {
601387259bdSDeclan Doherty 		l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
602387259bdSDeclan Doherty 		len = 0;
603387259bdSDeclan Doherty 	}
604387259bdSDeclan Doherty 
605c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].len = len;
606387259bdSDeclan Doherty 	return 0;
607387259bdSDeclan Doherty }
6089a212dc0SConor Fogarty /* >8 End of Enqueuing packets for TX. */
609387259bdSDeclan Doherty 
610387259bdSDeclan Doherty static void
611e2cdfbd0SZhiyong Yang l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
612387259bdSDeclan Doherty {
6136d13ea8eSOlivier Matz 	struct rte_ether_hdr *eth;
614387259bdSDeclan Doherty 	void *tmp;
615387259bdSDeclan Doherty 
6166d13ea8eSOlivier Matz 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
617387259bdSDeclan Doherty 
618387259bdSDeclan Doherty 	/* 02:00:00:00:00:xx */
61904d43857SDmitry Kozlyuk 	tmp = &eth->dst_addr.addr_bytes[0];
620acdfecbaSKuba Kozak 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
621387259bdSDeclan Doherty 
622387259bdSDeclan Doherty 	/* src addr */
62304d43857SDmitry Kozlyuk 	rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->src_addr);
624acdfecbaSKuba Kozak }
625acdfecbaSKuba Kozak 
626acdfecbaSKuba Kozak static void
627e2cdfbd0SZhiyong Yang l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
628acdfecbaSKuba Kozak 		struct l2fwd_crypto_options *options)
629acdfecbaSKuba Kozak {
630e2cdfbd0SZhiyong Yang 	uint16_t dst_port;
63162c0b448SRohit Raj 	uint32_t pad_len;
63262c0b448SRohit Raj 	struct rte_ipv4_hdr *ip_hdr;
63362c0b448SRohit Raj 	uint32_t ipdata_offset = sizeof(struct rte_ether_hdr);
634acdfecbaSKuba Kozak 
63562c0b448SRohit Raj 	ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
63662c0b448SRohit Raj 					 ipdata_offset);
637acdfecbaSKuba Kozak 	dst_port = l2fwd_dst_ports[portid];
638acdfecbaSKuba Kozak 
639acdfecbaSKuba Kozak 	if (options->mac_updating)
640acdfecbaSKuba Kozak 		l2fwd_mac_updating(m, dst_port);
641387259bdSDeclan Doherty 
64262c0b448SRohit Raj 	if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
64362c0b448SRohit Raj 		rte_pktmbuf_trim(m, options->auth_xform.auth.digest_length);
64462c0b448SRohit Raj 
64562c0b448SRohit Raj 	if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
64662c0b448SRohit Raj 		pad_len = m->pkt_len - rte_be_to_cpu_16(ip_hdr->total_length) -
64762c0b448SRohit Raj 			  ipdata_offset;
64862c0b448SRohit Raj 		rte_pktmbuf_trim(m, pad_len);
64962c0b448SRohit Raj 	}
65062c0b448SRohit Raj 
651e2cdfbd0SZhiyong Yang 	l2fwd_send_packet(m, dst_port);
652387259bdSDeclan Doherty }
653387259bdSDeclan Doherty 
654387259bdSDeclan Doherty /** Generate random key */
655387259bdSDeclan Doherty static void
656387259bdSDeclan Doherty generate_random_key(uint8_t *key, unsigned length)
657387259bdSDeclan Doherty {
6586dad6e69SPiotr Azarewicz 	int fd;
6596dad6e69SPiotr Azarewicz 	int ret;
660387259bdSDeclan Doherty 
6616dad6e69SPiotr Azarewicz 	fd = open("/dev/urandom", O_RDONLY);
6626dad6e69SPiotr Azarewicz 	if (fd < 0)
6636dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
6646dad6e69SPiotr Azarewicz 
6656dad6e69SPiotr Azarewicz 	ret = read(fd, key, length);
6666dad6e69SPiotr Azarewicz 	close(fd);
6676dad6e69SPiotr Azarewicz 
6686dad6e69SPiotr Azarewicz 	if (ret != (signed)length)
6696dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
670387259bdSDeclan Doherty }
671387259bdSDeclan Doherty 
6729a212dc0SConor Fogarty /* Session is created and is later attached to the crypto operation. 8< */
6731bd407faSFiona Trahe static struct rte_cryptodev_sym_session *
6742c59bd32SSlawomir Mrozowicz initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
675387259bdSDeclan Doherty {
6761bd407faSFiona Trahe 	struct rte_crypto_sym_xform *first_xform;
677b3bbd9e5SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
6788dbc9bbfSPablo de Lara 	int retval = rte_cryptodev_socket_id(cdev_id);
6798dbc9bbfSPablo de Lara 
6808dbc9bbfSPablo de Lara 	if (retval < 0)
6818dbc9bbfSPablo de Lara 		return NULL;
6828dbc9bbfSPablo de Lara 
6838dbc9bbfSPablo de Lara 	uint8_t socket_id = (uint8_t) retval;
684387259bdSDeclan Doherty 
6852661f4fbSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
6862661f4fbSPablo de Lara 		first_xform = &options->aead_xform;
6872661f4fbSPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
688387259bdSDeclan Doherty 		first_xform = &options->cipher_xform;
689387259bdSDeclan Doherty 		first_xform->next = &options->auth_xform;
6901a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
691387259bdSDeclan Doherty 		first_xform = &options->auth_xform;
692387259bdSDeclan Doherty 		first_xform->next = &options->cipher_xform;
6931a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
6941a75e9f3SPablo de Lara 		first_xform = &options->cipher_xform;
6951a75e9f3SPablo de Lara 	} else {
6961a75e9f3SPablo de Lara 		first_xform = &options->auth_xform;
697387259bdSDeclan Doherty 	}
698387259bdSDeclan Doherty 
699261bbff7SFan Zhang 	session = rte_cryptodev_sym_session_create(
700261bbff7SFan Zhang 			session_pool_socket[socket_id].sess_mp);
701b3bbd9e5SSlawomir Mrozowicz 	if (session == NULL)
702b3bbd9e5SSlawomir Mrozowicz 		return NULL;
703b3bbd9e5SSlawomir Mrozowicz 
704b3bbd9e5SSlawomir Mrozowicz 	if (rte_cryptodev_sym_session_init(cdev_id, session,
705261bbff7SFan Zhang 				first_xform,
706261bbff7SFan Zhang 				session_pool_socket[socket_id].priv_mp) < 0)
707b3bbd9e5SSlawomir Mrozowicz 		return NULL;
708b3bbd9e5SSlawomir Mrozowicz 
709b3bbd9e5SSlawomir Mrozowicz 	return session;
710387259bdSDeclan Doherty }
7119a212dc0SConor Fogarty /* >8 End of creation of session. */
712387259bdSDeclan Doherty 
713387259bdSDeclan Doherty static void
714387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
715387259bdSDeclan Doherty 
716387259bdSDeclan Doherty /* main processing loop */
717387259bdSDeclan Doherty static void
718387259bdSDeclan Doherty l2fwd_main_loop(struct l2fwd_crypto_options *options)
719387259bdSDeclan Doherty {
720387259bdSDeclan Doherty 	struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
721c0f87eb5SDeclan Doherty 	struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
722c0f87eb5SDeclan Doherty 
723387259bdSDeclan Doherty 	unsigned lcore_id = rte_lcore_id();
724387259bdSDeclan Doherty 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
725e2cdfbd0SZhiyong Yang 	unsigned int i, j, nb_rx, len;
726e2cdfbd0SZhiyong Yang 	uint16_t portid;
727387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
728387259bdSDeclan Doherty 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
729387259bdSDeclan Doherty 			US_PER_S * BURST_TX_DRAIN_US;
730387259bdSDeclan Doherty 	struct l2fwd_crypto_params *cparams;
731387259bdSDeclan Doherty 	struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
7322c59bd32SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
733387259bdSDeclan Doherty 
734387259bdSDeclan Doherty 	if (qconf->nb_rx_ports == 0) {
735387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
736387259bdSDeclan Doherty 		return;
737387259bdSDeclan Doherty 	}
738387259bdSDeclan Doherty 
739387259bdSDeclan Doherty 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
740387259bdSDeclan Doherty 
741387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_rx_ports; i++) {
742387259bdSDeclan Doherty 
743387259bdSDeclan Doherty 		portid = qconf->rx_port_list[i];
744387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
745387259bdSDeclan Doherty 			portid);
746387259bdSDeclan Doherty 	}
747387259bdSDeclan Doherty 
748387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_crypto_devs; i++) {
7491a75e9f3SPablo de Lara 		port_cparams[i].do_cipher = 0;
7501a75e9f3SPablo de Lara 		port_cparams[i].do_hash = 0;
7512661f4fbSPablo de Lara 		port_cparams[i].do_aead = 0;
7521a75e9f3SPablo de Lara 
7531a75e9f3SPablo de Lara 		switch (options->xform_chain) {
7542661f4fbSPablo de Lara 		case L2FWD_CRYPTO_AEAD:
7552661f4fbSPablo de Lara 			port_cparams[i].do_aead = 1;
7562661f4fbSPablo de Lara 			break;
7571a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_HASH:
7581a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_CIPHER:
7591a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7601a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7611a75e9f3SPablo de Lara 			break;
7621a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_ONLY:
7631a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7641a75e9f3SPablo de Lara 			break;
7651a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_ONLY:
7661a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7671a75e9f3SPablo de Lara 			break;
7681a75e9f3SPablo de Lara 		}
7691a75e9f3SPablo de Lara 
770387259bdSDeclan Doherty 		port_cparams[i].dev_id = qconf->cryptodev_list[i];
771387259bdSDeclan Doherty 		port_cparams[i].qp_id = 0;
772387259bdSDeclan Doherty 
77327cf2d1bSPablo de Lara 		port_cparams[i].block_size = options->block_size;
774387259bdSDeclan Doherty 
7751a75e9f3SPablo de Lara 		if (port_cparams[i].do_hash) {
776acf86169SPablo de Lara 			port_cparams[i].auth_iv.data = options->auth_iv.data;
777acf86169SPablo de Lara 			port_cparams[i].auth_iv.length = options->auth_iv.length;
778acf86169SPablo de Lara 			if (!options->auth_iv_param)
779acf86169SPablo de Lara 				generate_random_key(port_cparams[i].auth_iv.data,
780acf86169SPablo de Lara 						port_cparams[i].auth_iv.length);
7812661f4fbSPablo de Lara 			if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
7822661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 1;
7832661f4fbSPablo de Lara 			else
7842661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 0;
7852661f4fbSPablo de Lara 
7862661f4fbSPablo de Lara 			port_cparams[i].auth_algo = options->auth_xform.auth.algo;
7872c023071SPablo de Lara 			port_cparams[i].digest_length =
7882c023071SPablo de Lara 					options->auth_xform.auth.digest_length;
789acf86169SPablo de Lara 			/* Set IV parameters */
790acf86169SPablo de Lara 			if (options->auth_iv.length) {
791acf86169SPablo de Lara 				options->auth_xform.auth.iv.offset =
792acf86169SPablo de Lara 					IV_OFFSET + options->cipher_iv.length;
793acf86169SPablo de Lara 				options->auth_xform.auth.iv.length =
794acf86169SPablo de Lara 					options->auth_iv.length;
795acf86169SPablo de Lara 			}
7962661f4fbSPablo de Lara 		}
7972661f4fbSPablo de Lara 
7982661f4fbSPablo de Lara 		if (port_cparams[i].do_aead) {
799ef896e92SPablo de Lara 			port_cparams[i].aead_iv.data = options->aead_iv.data;
800ef896e92SPablo de Lara 			port_cparams[i].aead_iv.length = options->aead_iv.length;
801ef896e92SPablo de Lara 			if (!options->aead_iv_param)
802ef896e92SPablo de Lara 				generate_random_key(port_cparams[i].aead_iv.data,
803ef896e92SPablo de Lara 						port_cparams[i].aead_iv.length);
8042661f4fbSPablo de Lara 			port_cparams[i].aead_algo = options->aead_xform.aead.algo;
80527cf2d1bSPablo de Lara 			port_cparams[i].digest_length =
8062661f4fbSPablo de Lara 					options->aead_xform.aead.digest_length;
80746a0547fSPablo de Lara 			if (options->aead_xform.aead.aad_length) {
80827cf2d1bSPablo de Lara 				port_cparams[i].aad.data = options->aad.data;
809617a7949SPablo de Lara 				port_cparams[i].aad.phys_addr = options->aad.phys_addr;
8102661f4fbSPablo de Lara 				port_cparams[i].aad.length = options->aad.length;
811617a7949SPablo de Lara 				if (!options->aad_param)
81227cf2d1bSPablo de Lara 					generate_random_key(port_cparams[i].aad.data,
813a158899aSPablo de Lara 						port_cparams[i].aad.length);
814ff5d5b01SPablo de Lara 				/*
815ff5d5b01SPablo de Lara 				 * If doing AES-CCM, first 18 bytes has to be reserved,
816ff5d5b01SPablo de Lara 				 * and actual AAD should start from byte 18
817ff5d5b01SPablo de Lara 				 */
818ff5d5b01SPablo de Lara 				if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
819ff5d5b01SPablo de Lara 					memmove(port_cparams[i].aad.data + 18,
820ff5d5b01SPablo de Lara 							port_cparams[i].aad.data,
821ff5d5b01SPablo de Lara 							port_cparams[i].aad.length);
82227cf2d1bSPablo de Lara 
82318f421f6SPablo de Lara 			} else
82418f421f6SPablo de Lara 				port_cparams[i].aad.length = 0;
82527cf2d1bSPablo de Lara 
8262661f4fbSPablo de Lara 			if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
82727cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 1;
82827cf2d1bSPablo de Lara 			else
82927cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 0;
830d29ea843SPablo de Lara 
8312661f4fbSPablo de Lara 			/* Set IV parameters */
8322661f4fbSPablo de Lara 			options->aead_xform.aead.iv.offset = IV_OFFSET;
8332661f4fbSPablo de Lara 			options->aead_xform.aead.iv.length = options->aead_iv.length;
8341a75e9f3SPablo de Lara 		}
8351a75e9f3SPablo de Lara 
8361a75e9f3SPablo de Lara 		if (port_cparams[i].do_cipher) {
837acf86169SPablo de Lara 			port_cparams[i].cipher_iv.data = options->cipher_iv.data;
838acf86169SPablo de Lara 			port_cparams[i].cipher_iv.length = options->cipher_iv.length;
839acf86169SPablo de Lara 			if (!options->cipher_iv_param)
840acf86169SPablo de Lara 				generate_random_key(port_cparams[i].cipher_iv.data,
841acf86169SPablo de Lara 						port_cparams[i].cipher_iv.length);
842617a7949SPablo de Lara 
843d29ea843SPablo de Lara 			port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
844dcf384b4SMatan Azrad 			port_cparams[i].cipher_dataunit_len =
845dcf384b4SMatan Azrad 				options->cipher_xform.cipher.dataunit_len;
8460fbd75a9SPablo de Lara 			/* Set IV parameters */
8470fbd75a9SPablo de Lara 			options->cipher_xform.cipher.iv.offset = IV_OFFSET;
848acf86169SPablo de Lara 			options->cipher_xform.cipher.iv.length =
849acf86169SPablo de Lara 						options->cipher_iv.length;
85027cf2d1bSPablo de Lara 		}
851617a7949SPablo de Lara 
8522c59bd32SSlawomir Mrozowicz 		session = initialize_crypto_session(options,
853387259bdSDeclan Doherty 				port_cparams[i].dev_id);
8542c59bd32SSlawomir Mrozowicz 		if (session == NULL)
8552c59bd32SSlawomir Mrozowicz 			rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
856387259bdSDeclan Doherty 
8572c59bd32SSlawomir Mrozowicz 		port_cparams[i].session = session;
8582c59bd32SSlawomir Mrozowicz 
859387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
860387259bdSDeclan Doherty 				port_cparams[i].dev_id);
861387259bdSDeclan Doherty 	}
862387259bdSDeclan Doherty 
86341e97c2eSPablo de Lara 	l2fwd_crypto_options_print(options);
86441e97c2eSPablo de Lara 
86541e97c2eSPablo de Lara 	/*
86641e97c2eSPablo de Lara 	 * Initialize previous tsc timestamp before the loop,
86741e97c2eSPablo de Lara 	 * to avoid showing the port statistics immediately,
86841e97c2eSPablo de Lara 	 * so user can see the crypto information.
86941e97c2eSPablo de Lara 	 */
87041e97c2eSPablo de Lara 	prev_tsc = rte_rdtsc();
871387259bdSDeclan Doherty 	while (1) {
872387259bdSDeclan Doherty 
873387259bdSDeclan Doherty 		cur_tsc = rte_rdtsc();
874387259bdSDeclan Doherty 
875387259bdSDeclan Doherty 		/*
876268ca735SPablo de Lara 		 * Crypto device/TX burst queue drain
877387259bdSDeclan Doherty 		 */
878387259bdSDeclan Doherty 		diff_tsc = cur_tsc - prev_tsc;
879387259bdSDeclan Doherty 		if (unlikely(diff_tsc > drain_tsc)) {
880268ca735SPablo de Lara 			/* Enqueue all crypto ops remaining in buffers */
881268ca735SPablo de Lara 			for (i = 0; i < qconf->nb_crypto_devs; i++) {
882268ca735SPablo de Lara 				cparams = &port_cparams[i];
883268ca735SPablo de Lara 				len = qconf->op_buf[cparams->dev_id].len;
884268ca735SPablo de Lara 				l2fwd_crypto_send_burst(qconf, len, cparams);
885268ca735SPablo de Lara 				qconf->op_buf[cparams->dev_id].len = 0;
886268ca735SPablo de Lara 			}
887268ca735SPablo de Lara 			/* Transmit all packets remaining in buffers */
888387259bdSDeclan Doherty 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
889c0f87eb5SDeclan Doherty 				if (qconf->pkt_buf[portid].len == 0)
890387259bdSDeclan Doherty 					continue;
891387259bdSDeclan Doherty 				l2fwd_send_burst(&lcore_queue_conf[lcore_id],
892c0f87eb5SDeclan Doherty 						 qconf->pkt_buf[portid].len,
893e2cdfbd0SZhiyong Yang 						 portid);
894c0f87eb5SDeclan Doherty 				qconf->pkt_buf[portid].len = 0;
895387259bdSDeclan Doherty 			}
896387259bdSDeclan Doherty 
897387259bdSDeclan Doherty 			/* if timer is enabled */
898387259bdSDeclan Doherty 			if (timer_period > 0) {
899387259bdSDeclan Doherty 
900387259bdSDeclan Doherty 				/* advance the timer */
901387259bdSDeclan Doherty 				timer_tsc += diff_tsc;
902387259bdSDeclan Doherty 
903387259bdSDeclan Doherty 				/* if timer has reached its timeout */
904387259bdSDeclan Doherty 				if (unlikely(timer_tsc >=
905387259bdSDeclan Doherty 						(uint64_t)timer_period)) {
906387259bdSDeclan Doherty 
907cb056611SStephen Hemminger 					/* do this only on main core */
908cb056611SStephen Hemminger 					if (lcore_id == rte_get_main_lcore()
909ad509b4aSDeclan Doherty 						&& options->refresh_period) {
910387259bdSDeclan Doherty 						print_stats();
911387259bdSDeclan Doherty 						timer_tsc = 0;
912387259bdSDeclan Doherty 					}
913387259bdSDeclan Doherty 				}
914387259bdSDeclan Doherty 			}
915387259bdSDeclan Doherty 
916387259bdSDeclan Doherty 			prev_tsc = cur_tsc;
917387259bdSDeclan Doherty 		}
918387259bdSDeclan Doherty 
919387259bdSDeclan Doherty 		/*
920387259bdSDeclan Doherty 		 * Read packet from RX queues
921387259bdSDeclan Doherty 		 */
922387259bdSDeclan Doherty 		for (i = 0; i < qconf->nb_rx_ports; i++) {
923387259bdSDeclan Doherty 			portid = qconf->rx_port_list[i];
924387259bdSDeclan Doherty 
925387259bdSDeclan Doherty 			cparams = &port_cparams[i];
926387259bdSDeclan Doherty 
927e2cdfbd0SZhiyong Yang 			nb_rx = rte_eth_rx_burst(portid, 0,
928387259bdSDeclan Doherty 						 pkts_burst, MAX_PKT_BURST);
929387259bdSDeclan Doherty 
930387259bdSDeclan Doherty 			port_statistics[portid].rx += nb_rx;
931387259bdSDeclan Doherty 
9329a212dc0SConor Fogarty 			/* Allocate and fillcrypto operations. 8< */
933c0f87eb5SDeclan Doherty 			if (nb_rx) {
934387259bdSDeclan Doherty 				/*
935c0f87eb5SDeclan Doherty 				 * If we can't allocate a crypto_ops, then drop
936387259bdSDeclan Doherty 				 * the rest of the burst and dequeue and
937387259bdSDeclan Doherty 				 * process the packets to free offload structs
938387259bdSDeclan Doherty 				 */
939c0f87eb5SDeclan Doherty 				if (rte_crypto_op_bulk_alloc(
940c0f87eb5SDeclan Doherty 						l2fwd_crypto_op_pool,
941c0f87eb5SDeclan Doherty 						RTE_CRYPTO_OP_TYPE_SYMMETRIC,
942c0f87eb5SDeclan Doherty 						ops_burst, nb_rx) !=
943c0f87eb5SDeclan Doherty 								nb_rx) {
944c0f87eb5SDeclan Doherty 					for (j = 0; j < nb_rx; j++)
945d7acf6baSPablo de Lara 						rte_pktmbuf_free(pkts_burst[j]);
946c0f87eb5SDeclan Doherty 
947c0f87eb5SDeclan Doherty 					nb_rx = 0;
948387259bdSDeclan Doherty 				}
9499a212dc0SConor Fogarty 				/* >8 End of crypto operation allocated and filled. */
950387259bdSDeclan Doherty 
951c0f87eb5SDeclan Doherty 				/* Enqueue packets from Crypto device*/
952c0f87eb5SDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
953c0f87eb5SDeclan Doherty 					m = pkts_burst[j];
954387259bdSDeclan Doherty 
955c0f87eb5SDeclan Doherty 					l2fwd_simple_crypto_enqueue(m,
956c0f87eb5SDeclan Doherty 							ops_burst[j], cparams);
957c0f87eb5SDeclan Doherty 				}
958387259bdSDeclan Doherty 			}
959387259bdSDeclan Doherty 
9609a212dc0SConor Fogarty 			/* Dequeue packets from Crypto device. 8< */
961c0f87eb5SDeclan Doherty 			do {
962387259bdSDeclan Doherty 				nb_rx = rte_cryptodev_dequeue_burst(
963387259bdSDeclan Doherty 						cparams->dev_id, cparams->qp_id,
964c0f87eb5SDeclan Doherty 						ops_burst, MAX_PKT_BURST);
965c0f87eb5SDeclan Doherty 
966c0f87eb5SDeclan Doherty 				crypto_statistics[cparams->dev_id].dequeued +=
967c0f87eb5SDeclan Doherty 						nb_rx;
968387259bdSDeclan Doherty 
969387259bdSDeclan Doherty 				/* Forward crypto'd packets */
970387259bdSDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
971c0f87eb5SDeclan Doherty 					m = ops_burst[j]->sym->m_src;
972c0f87eb5SDeclan Doherty 
973c0f87eb5SDeclan Doherty 					rte_crypto_op_free(ops_burst[j]);
974acdfecbaSKuba Kozak 					l2fwd_simple_forward(m, portid,
975acdfecbaSKuba Kozak 							options);
976387259bdSDeclan Doherty 				}
977c0f87eb5SDeclan Doherty 			} while (nb_rx == MAX_PKT_BURST);
9789a212dc0SConor Fogarty 			/* >8 End of dequeue packets from crypto device. */
979387259bdSDeclan Doherty 		}
980387259bdSDeclan Doherty 	}
981387259bdSDeclan Doherty }
982387259bdSDeclan Doherty 
983387259bdSDeclan Doherty static int
984387259bdSDeclan Doherty l2fwd_launch_one_lcore(void *arg)
985387259bdSDeclan Doherty {
986387259bdSDeclan Doherty 	l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
987387259bdSDeclan Doherty 	return 0;
988387259bdSDeclan Doherty }
989387259bdSDeclan Doherty 
990387259bdSDeclan Doherty /* Display command line arguments usage */
991387259bdSDeclan Doherty static void
992387259bdSDeclan Doherty l2fwd_crypto_usage(const char *prgname)
993387259bdSDeclan Doherty {
994912b3a0aSPablo de Lara 	printf("%s [EAL options] --\n"
995387259bdSDeclan Doherty 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
996387259bdSDeclan Doherty 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
99769a558e3SPablo de Lara 		"  -s manage all ports from single lcore\n"
998a3380989SPablo de Lara 		"  -T PERIOD: statistics will be refreshed each PERIOD seconds"
999387259bdSDeclan Doherty 		" (0 to disable, 10 default, 86400 maximum)\n"
1000387259bdSDeclan Doherty 
1001912b3a0aSPablo de Lara 		"  --cdev_type HW / SW / ANY\n"
1002a3c2e34bSPablo de Lara 		"  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
10032661f4fbSPablo de Lara 		" HASH_ONLY / AEAD\n"
1004387259bdSDeclan Doherty 
1005387259bdSDeclan Doherty 		"  --cipher_algo ALGO\n"
1006387259bdSDeclan Doherty 		"  --cipher_op ENCRYPT / DECRYPT\n"
1007fcdbb3d5SPablo de Lara 		"  --cipher_key KEY (bytes separated with \":\")\n"
1008a061e50aSPablo de Lara 		"  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
1009acf86169SPablo de Lara 		"  --cipher_iv IV (bytes separated with \":\")\n"
1010acf86169SPablo de Lara 		"  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1011dcf384b4SMatan Azrad 		"  --cipher_dataunit_len SIZE: length of the algorithm data-unit\n"
1012387259bdSDeclan Doherty 
10133b98cbaaSPablo de Lara 		"  --auth_algo ALGO\n"
1014387259bdSDeclan Doherty 		"  --auth_op GENERATE / VERIFY\n"
1015fcdbb3d5SPablo de Lara 		"  --auth_key KEY (bytes separated with \":\")\n"
1016a061e50aSPablo de Lara 		"  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1017acf86169SPablo de Lara 		"  --auth_iv IV (bytes separated with \":\")\n"
1018acf86169SPablo de Lara 		"  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
10192661f4fbSPablo de Lara 
10202661f4fbSPablo de Lara 		"  --aead_algo ALGO\n"
10212661f4fbSPablo de Lara 		"  --aead_op ENCRYPT / DECRYPT\n"
10222661f4fbSPablo de Lara 		"  --aead_key KEY (bytes separated with \":\")\n"
10232661f4fbSPablo de Lara 		"  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
10242661f4fbSPablo de Lara 		"  --aead_iv IV (bytes separated with \":\")\n"
10252661f4fbSPablo de Lara 		"  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1026fcdbb3d5SPablo de Lara 		"  --aad AAD (bytes separated with \":\")\n"
1027a061e50aSPablo de Lara 		"  --aad_random_size SIZE: size of AAD when generated randomly\n"
10282661f4fbSPablo de Lara 
1029a061e50aSPablo de Lara 		"  --digest_size SIZE: size of digest to be generated/verified\n"
1030387259bdSDeclan Doherty 
1031d2797f51SFan Zhang 		"  --sessionless\n"
1032acdfecbaSKuba Kozak 		"  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1033acdfecbaSKuba Kozak 
1034acdfecbaSKuba Kozak 		"  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1035acdfecbaSKuba Kozak 		"      When enabled:\n"
1036acdfecbaSKuba Kozak 		"       - The source MAC address is replaced by the TX port MAC address\n"
1037acdfecbaSKuba Kozak 		"       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1038387259bdSDeclan Doherty 	       prgname);
1039387259bdSDeclan Doherty }
1040387259bdSDeclan Doherty 
1041387259bdSDeclan Doherty /** Parse crypto device type command line argument */
1042387259bdSDeclan Doherty static int
104327cf2d1bSPablo de Lara parse_cryptodev_type(enum cdev_type *type, char *optarg)
1044387259bdSDeclan Doherty {
104527cf2d1bSPablo de Lara 	if (strcmp("HW", optarg) == 0) {
104627cf2d1bSPablo de Lara 		*type = CDEV_TYPE_HW;
1047387259bdSDeclan Doherty 		return 0;
104827cf2d1bSPablo de Lara 	} else if (strcmp("SW", optarg) == 0) {
104927cf2d1bSPablo de Lara 		*type = CDEV_TYPE_SW;
105027cf2d1bSPablo de Lara 		return 0;
105127cf2d1bSPablo de Lara 	} else if (strcmp("ANY", optarg) == 0) {
105227cf2d1bSPablo de Lara 		*type = CDEV_TYPE_ANY;
1053387259bdSDeclan Doherty 		return 0;
1054387259bdSDeclan Doherty 	}
1055387259bdSDeclan Doherty 
1056387259bdSDeclan Doherty 	return -1;
1057387259bdSDeclan Doherty }
1058387259bdSDeclan Doherty 
1059387259bdSDeclan Doherty /** Parse crypto chain xform command line argument */
1060387259bdSDeclan Doherty static int
1061387259bdSDeclan Doherty parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1062387259bdSDeclan Doherty {
1063387259bdSDeclan Doherty 	if (strcmp("CIPHER_HASH", optarg) == 0) {
1064387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1065387259bdSDeclan Doherty 		return 0;
1066387259bdSDeclan Doherty 	} else if (strcmp("HASH_CIPHER", optarg) == 0) {
1067387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1068387259bdSDeclan Doherty 		return 0;
10691a75e9f3SPablo de Lara 	} else if (strcmp("CIPHER_ONLY", optarg) == 0) {
10701a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
10711a75e9f3SPablo de Lara 		return 0;
10721a75e9f3SPablo de Lara 	} else if (strcmp("HASH_ONLY", optarg) == 0) {
10731a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
10741a75e9f3SPablo de Lara 		return 0;
10752661f4fbSPablo de Lara 	} else if (strcmp("AEAD", optarg) == 0) {
10762661f4fbSPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_AEAD;
10772661f4fbSPablo de Lara 		return 0;
1078387259bdSDeclan Doherty 	}
1079387259bdSDeclan Doherty 
1080387259bdSDeclan Doherty 	return -1;
1081387259bdSDeclan Doherty }
1082387259bdSDeclan Doherty 
1083387259bdSDeclan Doherty /** Parse crypto cipher algo option command line argument */
1084387259bdSDeclan Doherty static int
1085387259bdSDeclan Doherty parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1086387259bdSDeclan Doherty {
108700c58901SPablo de Lara 
10884790f99dSPablo de Lara 	if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
10894790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Cipher algorithm specified "
10904790f99dSPablo de Lara 				"not supported!\n");
1091387259bdSDeclan Doherty 		return -1;
1092387259bdSDeclan Doherty 	}
1093387259bdSDeclan Doherty 
10944790f99dSPablo de Lara 	return 0;
10954790f99dSPablo de Lara }
10964790f99dSPablo de Lara 
1097387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1098387259bdSDeclan Doherty static int
1099387259bdSDeclan Doherty parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1100387259bdSDeclan Doherty {
1101387259bdSDeclan Doherty 	if (strcmp("ENCRYPT", optarg) == 0) {
1102387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1103387259bdSDeclan Doherty 		return 0;
1104387259bdSDeclan Doherty 	} else if (strcmp("DECRYPT", optarg) == 0) {
1105387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1106387259bdSDeclan Doherty 		return 0;
1107387259bdSDeclan Doherty 	}
1108387259bdSDeclan Doherty 
1109387259bdSDeclan Doherty 	printf("Cipher operation not supported!\n");
1110387259bdSDeclan Doherty 	return -1;
1111387259bdSDeclan Doherty }
1112387259bdSDeclan Doherty 
1113ff5d5b01SPablo de Lara /** Parse bytes from command line argument */
1114387259bdSDeclan Doherty static int
1115ff5d5b01SPablo de Lara parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1116387259bdSDeclan Doherty {
11171df9c010SPablo de Lara 	unsigned byte_count;
11181df9c010SPablo de Lara 	char *token;
11191df9c010SPablo de Lara 
112045544627SHemant Agrawal 	errno = 0;
11211df9c010SPablo de Lara 	for (byte_count = 0, token = strtok(input_arg, ":");
1122ff5d5b01SPablo de Lara 			(byte_count < max_size) && (token != NULL);
11231df9c010SPablo de Lara 			token = strtok(NULL, ":")) {
11241df9c010SPablo de Lara 
11251df9c010SPablo de Lara 		int number = (int)strtol(token, NULL, 16);
11261df9c010SPablo de Lara 
11271df9c010SPablo de Lara 		if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1128387259bdSDeclan Doherty 			return -1;
11291df9c010SPablo de Lara 
11301df9c010SPablo de Lara 		data[byte_count++] = (uint8_t)number;
11311df9c010SPablo de Lara 	}
11321df9c010SPablo de Lara 
1133a061e50aSPablo de Lara 	return byte_count;
1134a061e50aSPablo de Lara }
1135a061e50aSPablo de Lara 
1136a061e50aSPablo de Lara /** Parse size param*/
1137a061e50aSPablo de Lara static int
1138a061e50aSPablo de Lara parse_size(int *size, const char *q_arg)
1139a061e50aSPablo de Lara {
1140a061e50aSPablo de Lara 	char *end = NULL;
1141a061e50aSPablo de Lara 	unsigned long n;
1142a061e50aSPablo de Lara 
1143a061e50aSPablo de Lara 	/* parse hexadecimal string */
1144a061e50aSPablo de Lara 	n = strtoul(q_arg, &end, 10);
1145a061e50aSPablo de Lara 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1146a061e50aSPablo de Lara 		n = 0;
1147a061e50aSPablo de Lara 
1148a061e50aSPablo de Lara 	if (n == 0) {
1149a061e50aSPablo de Lara 		printf("invalid size\n");
1150a061e50aSPablo de Lara 		return -1;
1151a061e50aSPablo de Lara 	}
1152a061e50aSPablo de Lara 
1153a061e50aSPablo de Lara 	*size = n;
11541df9c010SPablo de Lara 	return 0;
1155387259bdSDeclan Doherty }
1156387259bdSDeclan Doherty 
1157387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1158387259bdSDeclan Doherty static int
1159387259bdSDeclan Doherty parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1160387259bdSDeclan Doherty {
11614790f99dSPablo de Lara 	if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
11624790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Authentication algorithm specified "
11634790f99dSPablo de Lara 				"not supported!\n");
1164387259bdSDeclan Doherty 		return -1;
1165387259bdSDeclan Doherty 	}
1166387259bdSDeclan Doherty 
11674790f99dSPablo de Lara 	return 0;
11684790f99dSPablo de Lara }
11694790f99dSPablo de Lara 
1170387259bdSDeclan Doherty static int
1171387259bdSDeclan Doherty parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1172387259bdSDeclan Doherty {
1173387259bdSDeclan Doherty 	if (strcmp("VERIFY", optarg) == 0) {
1174387259bdSDeclan Doherty 		*op = RTE_CRYPTO_AUTH_OP_VERIFY;
1175387259bdSDeclan Doherty 		return 0;
1176387259bdSDeclan Doherty 	} else if (strcmp("GENERATE", optarg) == 0) {
117772169087SPablo de Lara 		*op = RTE_CRYPTO_AUTH_OP_GENERATE;
1178387259bdSDeclan Doherty 		return 0;
1179387259bdSDeclan Doherty 	}
1180387259bdSDeclan Doherty 
1181387259bdSDeclan Doherty 	printf("Authentication operation specified not supported!\n");
1182387259bdSDeclan Doherty 	return -1;
1183387259bdSDeclan Doherty }
1184387259bdSDeclan Doherty 
1185d2797f51SFan Zhang static int
11862661f4fbSPablo de Lara parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
11872661f4fbSPablo de Lara {
11882661f4fbSPablo de Lara 	if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
11892661f4fbSPablo de Lara 		RTE_LOG(ERR, USER1, "AEAD algorithm specified "
11902661f4fbSPablo de Lara 				"not supported!\n");
11912661f4fbSPablo de Lara 		return -1;
11922661f4fbSPablo de Lara 	}
11932661f4fbSPablo de Lara 
11942661f4fbSPablo de Lara 	return 0;
11952661f4fbSPablo de Lara }
11962661f4fbSPablo de Lara 
11972661f4fbSPablo de Lara static int
11982661f4fbSPablo de Lara parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
11992661f4fbSPablo de Lara {
12002661f4fbSPablo de Lara 	if (strcmp("ENCRYPT", optarg) == 0) {
12012661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
12022661f4fbSPablo de Lara 		return 0;
12032661f4fbSPablo de Lara 	} else if (strcmp("DECRYPT", optarg) == 0) {
12042661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_DECRYPT;
12052661f4fbSPablo de Lara 		return 0;
12062661f4fbSPablo de Lara 	}
12072661f4fbSPablo de Lara 
12082661f4fbSPablo de Lara 	printf("AEAD operation specified not supported!\n");
12092661f4fbSPablo de Lara 	return -1;
12102661f4fbSPablo de Lara }
12112661f4fbSPablo de Lara static int
1212d2797f51SFan Zhang parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1213d2797f51SFan Zhang 		const char *q_arg)
1214d2797f51SFan Zhang {
1215d2797f51SFan Zhang 	char *end = NULL;
1216d2797f51SFan Zhang 	uint64_t pm;
1217d2797f51SFan Zhang 
1218d2797f51SFan Zhang 	/* parse hexadecimal string */
1219d2797f51SFan Zhang 	pm = strtoul(q_arg, &end, 16);
1220d2797f51SFan Zhang 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1221d2797f51SFan Zhang 		pm = 0;
1222d2797f51SFan Zhang 
1223d2797f51SFan Zhang 	options->cryptodev_mask = pm;
1224d2797f51SFan Zhang 	if (options->cryptodev_mask == 0) {
1225d2797f51SFan Zhang 		printf("invalid cryptodev_mask specified\n");
1226d2797f51SFan Zhang 		return -1;
1227d2797f51SFan Zhang 	}
1228d2797f51SFan Zhang 
1229d2797f51SFan Zhang 	return 0;
1230d2797f51SFan Zhang }
1231d2797f51SFan Zhang 
1232387259bdSDeclan Doherty /** Parse long options */
1233387259bdSDeclan Doherty static int
1234387259bdSDeclan Doherty l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1235387259bdSDeclan Doherty 		struct option *lgopts, int option_index)
1236387259bdSDeclan Doherty {
123727cf2d1bSPablo de Lara 	int retval;
1238dcf384b4SMatan Azrad 	int val;
123927cf2d1bSPablo de Lara 
124049f79e86SPablo de Lara 	if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
124149f79e86SPablo de Lara 		retval = parse_cryptodev_type(&options->type, optarg);
124249f79e86SPablo de Lara 		if (retval == 0)
12436723c0fcSBruce Richardson 			strlcpy(options->string_type, optarg, MAX_STR_LEN);
124449f79e86SPablo de Lara 		return retval;
124549f79e86SPablo de Lara 	}
1246387259bdSDeclan Doherty 
1247387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "chain") == 0)
1248387259bdSDeclan Doherty 		return parse_crypto_opt_chain(options, optarg);
1249387259bdSDeclan Doherty 
1250387259bdSDeclan Doherty 	/* Cipher options */
125100c58901SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
125200c58901SPablo de Lara 		return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1253387259bdSDeclan Doherty 				optarg);
1254387259bdSDeclan Doherty 
1255387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1256387259bdSDeclan Doherty 		return parse_cipher_op(&options->cipher_xform.cipher.op,
1257387259bdSDeclan Doherty 				optarg);
1258387259bdSDeclan Doherty 
1259a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
12601df9c010SPablo de Lara 		options->ckey_param = 1;
1261a061e50aSPablo de Lara 		options->cipher_xform.cipher.key.length =
1262186b14d6SFan Zhang 			parse_bytes(options->cipher_key, optarg, MAX_KEY_SIZE);
1263a061e50aSPablo de Lara 		if (options->cipher_xform.cipher.key.length > 0)
1264a061e50aSPablo de Lara 			return 0;
1265a061e50aSPablo de Lara 		else
1266a061e50aSPablo de Lara 			return -1;
12671df9c010SPablo de Lara 	}
1268387259bdSDeclan Doherty 
1269dcf384b4SMatan Azrad 	else if (strcmp(lgopts[option_index].name, "cipher_dataunit_len") == 0) {
1270dcf384b4SMatan Azrad 		retval = parse_size(&val, optarg);
1271*cab0c8f3SMatan Azrad 		if (retval == 0 && val >= 0) {
1272dcf384b4SMatan Azrad 			options->cipher_xform.cipher.dataunit_len =
1273*cab0c8f3SMatan Azrad 								(uint32_t)val;
1274dcf384b4SMatan Azrad 			return 0;
1275dcf384b4SMatan Azrad 		} else
1276dcf384b4SMatan Azrad 			return -1;
1277dcf384b4SMatan Azrad 	}
1278dcf384b4SMatan Azrad 
1279a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1280a061e50aSPablo de Lara 		return parse_size(&options->ckey_random_size, optarg);
1281a061e50aSPablo de Lara 
1282acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1283acf86169SPablo de Lara 		options->cipher_iv_param = 1;
1284acf86169SPablo de Lara 		options->cipher_iv.length =
1285ff5d5b01SPablo de Lara 			parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1286acf86169SPablo de Lara 		if (options->cipher_iv.length > 0)
1287a061e50aSPablo de Lara 			return 0;
1288a061e50aSPablo de Lara 		else
1289a061e50aSPablo de Lara 			return -1;
12901df9c010SPablo de Lara 	}
1291387259bdSDeclan Doherty 
1292acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1293acf86169SPablo de Lara 		return parse_size(&options->cipher_iv_random_size, optarg);
1294a061e50aSPablo de Lara 
1295387259bdSDeclan Doherty 	/* Authentication options */
129627cf2d1bSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
129700c58901SPablo de Lara 		return parse_auth_algo(&options->auth_xform.auth.algo,
1298387259bdSDeclan Doherty 				optarg);
129927cf2d1bSPablo de Lara 	}
1300387259bdSDeclan Doherty 
1301387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
130272169087SPablo de Lara 		return parse_auth_op(&options->auth_xform.auth.op,
1303387259bdSDeclan Doherty 				optarg);
1304387259bdSDeclan Doherty 
1305a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
13061df9c010SPablo de Lara 		options->akey_param = 1;
1307a061e50aSPablo de Lara 		options->auth_xform.auth.key.length =
1308186b14d6SFan Zhang 			parse_bytes(options->auth_key, optarg, MAX_KEY_SIZE);
1309a061e50aSPablo de Lara 		if (options->auth_xform.auth.key.length > 0)
1310a061e50aSPablo de Lara 			return 0;
1311a061e50aSPablo de Lara 		else
1312a061e50aSPablo de Lara 			return -1;
1313a061e50aSPablo de Lara 	}
1314a061e50aSPablo de Lara 
1315a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1316a061e50aSPablo de Lara 		return parse_size(&options->akey_random_size, optarg);
13171df9c010SPablo de Lara 	}
1318387259bdSDeclan Doherty 
1319acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1320acf86169SPablo de Lara 		options->auth_iv_param = 1;
1321acf86169SPablo de Lara 		options->auth_iv.length =
1322ff5d5b01SPablo de Lara 			parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1323acf86169SPablo de Lara 		if (options->auth_iv.length > 0)
1324acf86169SPablo de Lara 			return 0;
1325acf86169SPablo de Lara 		else
1326acf86169SPablo de Lara 			return -1;
1327acf86169SPablo de Lara 	}
1328acf86169SPablo de Lara 
1329acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1330acf86169SPablo de Lara 		return parse_size(&options->auth_iv_random_size, optarg);
1331acf86169SPablo de Lara 
13322661f4fbSPablo de Lara 	/* AEAD options */
13332661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
13342661f4fbSPablo de Lara 		return parse_aead_algo(&options->aead_xform.aead.algo,
13352661f4fbSPablo de Lara 				optarg);
13362661f4fbSPablo de Lara 	}
13372661f4fbSPablo de Lara 
13382661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
13392661f4fbSPablo de Lara 		return parse_aead_op(&options->aead_xform.aead.op,
13402661f4fbSPablo de Lara 				optarg);
13412661f4fbSPablo de Lara 
13422661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
13432661f4fbSPablo de Lara 		options->aead_key_param = 1;
13442661f4fbSPablo de Lara 		options->aead_xform.aead.key.length =
1345186b14d6SFan Zhang 			parse_bytes(options->aead_key, optarg, MAX_KEY_SIZE);
13462661f4fbSPablo de Lara 		if (options->aead_xform.aead.key.length > 0)
13472661f4fbSPablo de Lara 			return 0;
13482661f4fbSPablo de Lara 		else
13492661f4fbSPablo de Lara 			return -1;
13502661f4fbSPablo de Lara 	}
13512661f4fbSPablo de Lara 
13522661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
13532661f4fbSPablo de Lara 		return parse_size(&options->aead_key_random_size, optarg);
13542661f4fbSPablo de Lara 
13552661f4fbSPablo de Lara 
13562661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
13572661f4fbSPablo de Lara 		options->aead_iv_param = 1;
13582661f4fbSPablo de Lara 		options->aead_iv.length =
1359ff5d5b01SPablo de Lara 			parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
13602661f4fbSPablo de Lara 		if (options->aead_iv.length > 0)
13612661f4fbSPablo de Lara 			return 0;
13622661f4fbSPablo de Lara 		else
13632661f4fbSPablo de Lara 			return -1;
13642661f4fbSPablo de Lara 	}
13652661f4fbSPablo de Lara 
13662661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
13672661f4fbSPablo de Lara 		return parse_size(&options->aead_iv_random_size, optarg);
13682661f4fbSPablo de Lara 
1369617a7949SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1370617a7949SPablo de Lara 		options->aad_param = 1;
1371a061e50aSPablo de Lara 		options->aad.length =
1372ff5d5b01SPablo de Lara 			parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1373a061e50aSPablo de Lara 		if (options->aad.length > 0)
1374a061e50aSPablo de Lara 			return 0;
1375a061e50aSPablo de Lara 		else
1376a061e50aSPablo de Lara 			return -1;
1377a061e50aSPablo de Lara 	}
1378a061e50aSPablo de Lara 
1379a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1380a061e50aSPablo de Lara 		return parse_size(&options->aad_random_size, optarg);
1381a061e50aSPablo de Lara 	}
1382a061e50aSPablo de Lara 
1383a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1384a061e50aSPablo de Lara 		return parse_size(&options->digest_size, optarg);
1385617a7949SPablo de Lara 	}
1386617a7949SPablo de Lara 
13871df9c010SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1388387259bdSDeclan Doherty 		options->sessionless = 1;
1389387259bdSDeclan Doherty 		return 0;
1390387259bdSDeclan Doherty 	}
1391387259bdSDeclan Doherty 
1392d2797f51SFan Zhang 	else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1393d2797f51SFan Zhang 		return parse_cryptodev_mask(options, optarg);
1394d2797f51SFan Zhang 
1395acdfecbaSKuba Kozak 	else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1396acdfecbaSKuba Kozak 		options->mac_updating = 1;
1397acdfecbaSKuba Kozak 		return 0;
1398acdfecbaSKuba Kozak 	}
1399acdfecbaSKuba Kozak 
1400acdfecbaSKuba Kozak 	else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1401acdfecbaSKuba Kozak 		options->mac_updating = 0;
1402acdfecbaSKuba Kozak 		return 0;
1403acdfecbaSKuba Kozak 	}
1404acdfecbaSKuba Kozak 
1405387259bdSDeclan Doherty 	return -1;
1406387259bdSDeclan Doherty }
1407387259bdSDeclan Doherty 
1408387259bdSDeclan Doherty /** Parse port mask */
1409387259bdSDeclan Doherty static int
1410387259bdSDeclan Doherty l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1411387259bdSDeclan Doherty 		const char *q_arg)
1412387259bdSDeclan Doherty {
1413387259bdSDeclan Doherty 	char *end = NULL;
1414387259bdSDeclan Doherty 	unsigned long pm;
1415387259bdSDeclan Doherty 
1416387259bdSDeclan Doherty 	/* parse hexadecimal string */
1417387259bdSDeclan Doherty 	pm = strtoul(q_arg, &end, 16);
1418387259bdSDeclan Doherty 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1419387259bdSDeclan Doherty 		pm = 0;
1420387259bdSDeclan Doherty 
1421387259bdSDeclan Doherty 	options->portmask = pm;
1422387259bdSDeclan Doherty 	if (options->portmask == 0) {
1423387259bdSDeclan Doherty 		printf("invalid portmask specified\n");
1424387259bdSDeclan Doherty 		return -1;
1425387259bdSDeclan Doherty 	}
1426387259bdSDeclan Doherty 
1427387259bdSDeclan Doherty 	return pm;
1428387259bdSDeclan Doherty }
1429387259bdSDeclan Doherty 
1430387259bdSDeclan Doherty /** Parse number of queues */
1431387259bdSDeclan Doherty static int
1432387259bdSDeclan Doherty l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1433387259bdSDeclan Doherty 		const char *q_arg)
1434387259bdSDeclan Doherty {
1435387259bdSDeclan Doherty 	char *end = NULL;
1436387259bdSDeclan Doherty 	unsigned long n;
1437387259bdSDeclan Doherty 
1438387259bdSDeclan Doherty 	/* parse hexadecimal string */
1439387259bdSDeclan Doherty 	n = strtoul(q_arg, &end, 10);
1440387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1441387259bdSDeclan Doherty 		n = 0;
1442387259bdSDeclan Doherty 	else if (n >= MAX_RX_QUEUE_PER_LCORE)
1443387259bdSDeclan Doherty 		n = 0;
1444387259bdSDeclan Doherty 
1445387259bdSDeclan Doherty 	options->nb_ports_per_lcore = n;
1446387259bdSDeclan Doherty 	if (options->nb_ports_per_lcore == 0) {
1447387259bdSDeclan Doherty 		printf("invalid number of ports selected\n");
1448387259bdSDeclan Doherty 		return -1;
1449387259bdSDeclan Doherty 	}
1450387259bdSDeclan Doherty 
1451387259bdSDeclan Doherty 	return 0;
1452387259bdSDeclan Doherty }
1453387259bdSDeclan Doherty 
1454387259bdSDeclan Doherty /** Parse timer period */
1455387259bdSDeclan Doherty static int
1456387259bdSDeclan Doherty l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1457387259bdSDeclan Doherty 		const char *q_arg)
1458387259bdSDeclan Doherty {
1459387259bdSDeclan Doherty 	char *end = NULL;
14603c96262cSPablo de Lara 	unsigned long n;
1461387259bdSDeclan Doherty 
1462387259bdSDeclan Doherty 	/* parse number string */
14633c96262cSPablo de Lara 	n = (unsigned)strtol(q_arg, &end, 10);
1464387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1465387259bdSDeclan Doherty 		n = 0;
1466387259bdSDeclan Doherty 
1467ad509b4aSDeclan Doherty 	if (n >= MAX_TIMER_PERIOD) {
14683c96262cSPablo de Lara 		printf("Warning refresh period specified %lu is greater than "
14693c96262cSPablo de Lara 				"max value %lu! using max value",
1470ad509b4aSDeclan Doherty 				n, MAX_TIMER_PERIOD);
1471ad509b4aSDeclan Doherty 		n = MAX_TIMER_PERIOD;
1472ad509b4aSDeclan Doherty 	}
1473387259bdSDeclan Doherty 
1474387259bdSDeclan Doherty 	options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1475387259bdSDeclan Doherty 
1476387259bdSDeclan Doherty 	return 0;
1477387259bdSDeclan Doherty }
1478387259bdSDeclan Doherty 
1479387259bdSDeclan Doherty /** Generate default options for application */
1480387259bdSDeclan Doherty static void
1481387259bdSDeclan Doherty l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1482387259bdSDeclan Doherty {
1483387259bdSDeclan Doherty 	options->portmask = 0xffffffff;
1484387259bdSDeclan Doherty 	options->nb_ports_per_lcore = 1;
1485387259bdSDeclan Doherty 	options->refresh_period = 10000;
1486387259bdSDeclan Doherty 	options->single_lcore = 0;
14873c96262cSPablo de Lara 	options->sessionless = 0;
1488387259bdSDeclan Doherty 
1489387259bdSDeclan Doherty 	options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1490387259bdSDeclan Doherty 
1491387259bdSDeclan Doherty 	/* Cipher Data */
14921bd407faSFiona Trahe 	options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1493387259bdSDeclan Doherty 	options->cipher_xform.next = NULL;
14941df9c010SPablo de Lara 	options->ckey_param = 0;
1495a061e50aSPablo de Lara 	options->ckey_random_size = -1;
1496a061e50aSPablo de Lara 	options->cipher_xform.cipher.key.length = 0;
1497acf86169SPablo de Lara 	options->cipher_iv_param = 0;
1498acf86169SPablo de Lara 	options->cipher_iv_random_size = -1;
1499acf86169SPablo de Lara 	options->cipher_iv.length = 0;
1500387259bdSDeclan Doherty 
1501387259bdSDeclan Doherty 	options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1502387259bdSDeclan Doherty 	options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1503dcf384b4SMatan Azrad 	options->cipher_xform.cipher.dataunit_len = 0;
1504387259bdSDeclan Doherty 
1505387259bdSDeclan Doherty 	/* Authentication Data */
15061bd407faSFiona Trahe 	options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1507387259bdSDeclan Doherty 	options->auth_xform.next = NULL;
15081df9c010SPablo de Lara 	options->akey_param = 0;
1509a061e50aSPablo de Lara 	options->akey_random_size = -1;
1510a061e50aSPablo de Lara 	options->auth_xform.auth.key.length = 0;
1511acf86169SPablo de Lara 	options->auth_iv_param = 0;
1512acf86169SPablo de Lara 	options->auth_iv_random_size = -1;
1513acf86169SPablo de Lara 	options->auth_iv.length = 0;
1514387259bdSDeclan Doherty 
1515387259bdSDeclan Doherty 	options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
151627cf2d1bSPablo de Lara 	options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1517387259bdSDeclan Doherty 
15182661f4fbSPablo de Lara 	/* AEAD Data */
15192661f4fbSPablo de Lara 	options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
15202661f4fbSPablo de Lara 	options->aead_xform.next = NULL;
15212661f4fbSPablo de Lara 	options->aead_key_param = 0;
15222661f4fbSPablo de Lara 	options->aead_key_random_size = -1;
15232661f4fbSPablo de Lara 	options->aead_xform.aead.key.length = 0;
15242661f4fbSPablo de Lara 	options->aead_iv_param = 0;
15252661f4fbSPablo de Lara 	options->aead_iv_random_size = -1;
15262661f4fbSPablo de Lara 	options->aead_iv.length = 0;
15272661f4fbSPablo de Lara 
1528d2c4b7d3SHemant Agrawal 	options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1529d2c4b7d3SHemant Agrawal 	options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1530b79e4c00SPablo de Lara 
15312661f4fbSPablo de Lara 	options->aad_param = 0;
15322661f4fbSPablo de Lara 	options->aad_random_size = -1;
15332661f4fbSPablo de Lara 	options->aad.length = 0;
15342661f4fbSPablo de Lara 
15352661f4fbSPablo de Lara 	options->digest_size = -1;
15362661f4fbSPablo de Lara 
153727cf2d1bSPablo de Lara 	options->type = CDEV_TYPE_ANY;
1538d2797f51SFan Zhang 	options->cryptodev_mask = UINT64_MAX;
1539acdfecbaSKuba Kozak 
1540acdfecbaSKuba Kozak 	options->mac_updating = 1;
1541387259bdSDeclan Doherty }
1542387259bdSDeclan Doherty 
1543387259bdSDeclan Doherty static void
154441e97c2eSPablo de Lara display_cipher_info(struct l2fwd_crypto_options *options)
154541e97c2eSPablo de Lara {
154641e97c2eSPablo de Lara 	printf("\n---- Cipher information ---\n");
154741e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
15484790f99dSPablo de Lara 		rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
154941e97c2eSPablo de Lara 	rte_hexdump(stdout, "Cipher key:",
155041e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.data,
155141e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.length);
1552acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
155341e97c2eSPablo de Lara }
155441e97c2eSPablo de Lara 
155541e97c2eSPablo de Lara static void
155641e97c2eSPablo de Lara display_auth_info(struct l2fwd_crypto_options *options)
155741e97c2eSPablo de Lara {
155841e97c2eSPablo de Lara 	printf("\n---- Authentication information ---\n");
155941e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
15604eb45d2dSPablo de Lara 		rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
156141e97c2eSPablo de Lara 	rte_hexdump(stdout, "Auth key:",
156241e97c2eSPablo de Lara 			options->auth_xform.auth.key.data,
156341e97c2eSPablo de Lara 			options->auth_xform.auth.key.length);
1564acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
15652661f4fbSPablo de Lara }
15662661f4fbSPablo de Lara 
15672661f4fbSPablo de Lara static void
15682661f4fbSPablo de Lara display_aead_info(struct l2fwd_crypto_options *options)
15692661f4fbSPablo de Lara {
15702661f4fbSPablo de Lara 	printf("\n---- AEAD information ---\n");
15712661f4fbSPablo de Lara 	printf("Algorithm: %s\n",
15722661f4fbSPablo de Lara 		rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
15732661f4fbSPablo de Lara 	rte_hexdump(stdout, "AEAD key:",
15742661f4fbSPablo de Lara 			options->aead_xform.aead.key.data,
15752661f4fbSPablo de Lara 			options->aead_xform.aead.key.length);
15762661f4fbSPablo de Lara 	rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
157741e97c2eSPablo de Lara 	rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
157841e97c2eSPablo de Lara }
157941e97c2eSPablo de Lara 
158041e97c2eSPablo de Lara static void
1581387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1582387259bdSDeclan Doherty {
158341e97c2eSPablo de Lara 	char string_cipher_op[MAX_STR_LEN];
158441e97c2eSPablo de Lara 	char string_auth_op[MAX_STR_LEN];
15852661f4fbSPablo de Lara 	char string_aead_op[MAX_STR_LEN];
158641e97c2eSPablo de Lara 
158741e97c2eSPablo de Lara 	if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
158841e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Encrypt");
158941e97c2eSPablo de Lara 	else
159041e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Decrypt");
159141e97c2eSPablo de Lara 
159241e97c2eSPablo de Lara 	if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
159341e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth generate");
159441e97c2eSPablo de Lara 	else
159541e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth verify");
159641e97c2eSPablo de Lara 
15972661f4fbSPablo de Lara 	if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
15982661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated encryption");
15992661f4fbSPablo de Lara 	else
16002661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated decryption");
16012661f4fbSPablo de Lara 
16022661f4fbSPablo de Lara 
1603387259bdSDeclan Doherty 	printf("Options:-\nn");
1604387259bdSDeclan Doherty 	printf("portmask: %x\n", options->portmask);
1605387259bdSDeclan Doherty 	printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1606387259bdSDeclan Doherty 	printf("refresh period : %u\n", options->refresh_period);
1607387259bdSDeclan Doherty 	printf("single lcore mode: %s\n",
1608387259bdSDeclan Doherty 			options->single_lcore ? "enabled" : "disabled");
1609387259bdSDeclan Doherty 	printf("stats_printing: %s\n",
1610ad509b4aSDeclan Doherty 			options->refresh_period == 0 ? "disabled" : "enabled");
1611387259bdSDeclan Doherty 
1612387259bdSDeclan Doherty 	printf("sessionless crypto: %s\n",
1613387259bdSDeclan Doherty 			options->sessionless ? "enabled" : "disabled");
161441e97c2eSPablo de Lara 
161541e97c2eSPablo de Lara 	if (options->ckey_param && (options->ckey_random_size != -1))
161641e97c2eSPablo de Lara 		printf("Cipher key already parsed, ignoring size of random key\n");
161741e97c2eSPablo de Lara 
161841e97c2eSPablo de Lara 	if (options->akey_param && (options->akey_random_size != -1))
161941e97c2eSPablo de Lara 		printf("Auth key already parsed, ignoring size of random key\n");
162041e97c2eSPablo de Lara 
1621acf86169SPablo de Lara 	if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1622acf86169SPablo de Lara 		printf("Cipher IV already parsed, ignoring size of random IV\n");
1623acf86169SPablo de Lara 
1624acf86169SPablo de Lara 	if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1625acf86169SPablo de Lara 		printf("Auth IV already parsed, ignoring size of random IV\n");
162641e97c2eSPablo de Lara 
162741e97c2eSPablo de Lara 	if (options->aad_param && (options->aad_random_size != -1))
162841e97c2eSPablo de Lara 		printf("AAD already parsed, ignoring size of random AAD\n");
162941e97c2eSPablo de Lara 
163041e97c2eSPablo de Lara 	printf("\nCrypto chain: ");
163141e97c2eSPablo de Lara 	switch (options->xform_chain) {
16322661f4fbSPablo de Lara 	case L2FWD_CRYPTO_AEAD:
16332661f4fbSPablo de Lara 		printf("Input --> %s --> Output\n", string_aead_op);
16342661f4fbSPablo de Lara 		display_aead_info(options);
16352661f4fbSPablo de Lara 		break;
163641e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_HASH:
163741e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
163841e97c2eSPablo de Lara 			string_cipher_op, string_auth_op);
163941e97c2eSPablo de Lara 		display_cipher_info(options);
164041e97c2eSPablo de Lara 		display_auth_info(options);
164141e97c2eSPablo de Lara 		break;
164241e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_CIPHER:
164341e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
164441e97c2eSPablo de Lara 			string_auth_op, string_cipher_op);
164541e97c2eSPablo de Lara 		display_cipher_info(options);
164641e97c2eSPablo de Lara 		display_auth_info(options);
164741e97c2eSPablo de Lara 		break;
164841e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_ONLY:
164941e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_auth_op);
165041e97c2eSPablo de Lara 		display_auth_info(options);
165141e97c2eSPablo de Lara 		break;
165241e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_ONLY:
165341e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_cipher_op);
165441e97c2eSPablo de Lara 		display_cipher_info(options);
165541e97c2eSPablo de Lara 		break;
165641e97c2eSPablo de Lara 	}
1657387259bdSDeclan Doherty }
1658387259bdSDeclan Doherty 
1659387259bdSDeclan Doherty /* Parse the argument given in the command line of the application */
1660387259bdSDeclan Doherty static int
1661387259bdSDeclan Doherty l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1662387259bdSDeclan Doherty 		int argc, char **argv)
1663387259bdSDeclan Doherty {
1664387259bdSDeclan Doherty 	int opt, retval, option_index;
1665387259bdSDeclan Doherty 	char **argvopt = argv, *prgname = argv[0];
1666387259bdSDeclan Doherty 
1667387259bdSDeclan Doherty 	static struct option lgopts[] = {
1668387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1669387259bdSDeclan Doherty 
1670387259bdSDeclan Doherty 			{ "cdev_type", required_argument, 0, 0 },
1671387259bdSDeclan Doherty 			{ "chain", required_argument, 0, 0 },
1672387259bdSDeclan Doherty 
1673387259bdSDeclan Doherty 			{ "cipher_algo", required_argument, 0, 0 },
1674387259bdSDeclan Doherty 			{ "cipher_op", required_argument, 0, 0 },
1675387259bdSDeclan Doherty 			{ "cipher_key", required_argument, 0, 0 },
1676a061e50aSPablo de Lara 			{ "cipher_key_random_size", required_argument, 0, 0 },
1677acf86169SPablo de Lara 			{ "cipher_iv", required_argument, 0, 0 },
1678acf86169SPablo de Lara 			{ "cipher_iv_random_size", required_argument, 0, 0 },
1679dcf384b4SMatan Azrad 			{ "cipher_dataunit_len", required_argument, 0, 0},
1680387259bdSDeclan Doherty 
1681387259bdSDeclan Doherty 			{ "auth_algo", required_argument, 0, 0 },
1682387259bdSDeclan Doherty 			{ "auth_op", required_argument, 0, 0 },
1683387259bdSDeclan Doherty 			{ "auth_key", required_argument, 0, 0 },
1684a061e50aSPablo de Lara 			{ "auth_key_random_size", required_argument, 0, 0 },
1685acf86169SPablo de Lara 			{ "auth_iv", required_argument, 0, 0 },
1686acf86169SPablo de Lara 			{ "auth_iv_random_size", required_argument, 0, 0 },
1687387259bdSDeclan Doherty 
16882661f4fbSPablo de Lara 			{ "aead_algo", required_argument, 0, 0 },
16892661f4fbSPablo de Lara 			{ "aead_op", required_argument, 0, 0 },
16902661f4fbSPablo de Lara 			{ "aead_key", required_argument, 0, 0 },
16912661f4fbSPablo de Lara 			{ "aead_key_random_size", required_argument, 0, 0 },
16922661f4fbSPablo de Lara 			{ "aead_iv", required_argument, 0, 0 },
16932661f4fbSPablo de Lara 			{ "aead_iv_random_size", required_argument, 0, 0 },
16942661f4fbSPablo de Lara 
1695617a7949SPablo de Lara 			{ "aad", required_argument, 0, 0 },
1696a061e50aSPablo de Lara 			{ "aad_random_size", required_argument, 0, 0 },
16972661f4fbSPablo de Lara 
1698a061e50aSPablo de Lara 			{ "digest_size", required_argument, 0, 0 },
1699387259bdSDeclan Doherty 
1700387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1701d2797f51SFan Zhang 			{ "cryptodev_mask", required_argument, 0, 0},
17023c96262cSPablo de Lara 
1703acdfecbaSKuba Kozak 			{ "mac-updating", no_argument, 0, 0},
1704acdfecbaSKuba Kozak 			{ "no-mac-updating", no_argument, 0, 0},
1705acdfecbaSKuba Kozak 
1706387259bdSDeclan Doherty 			{ NULL, 0, 0, 0 }
1707387259bdSDeclan Doherty 	};
1708387259bdSDeclan Doherty 
1709387259bdSDeclan Doherty 	l2fwd_crypto_default_options(options);
1710387259bdSDeclan Doherty 
1711f1ae15baSPablo de Lara 	while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1712387259bdSDeclan Doherty 			&option_index)) != EOF) {
1713387259bdSDeclan Doherty 		switch (opt) {
1714387259bdSDeclan Doherty 		/* long options */
1715387259bdSDeclan Doherty 		case 0:
1716387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_args_long_options(options,
1717387259bdSDeclan Doherty 					lgopts, option_index);
1718387259bdSDeclan Doherty 			if (retval < 0) {
1719387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1720387259bdSDeclan Doherty 				return -1;
1721387259bdSDeclan Doherty 			}
1722387259bdSDeclan Doherty 			break;
1723387259bdSDeclan Doherty 
1724387259bdSDeclan Doherty 		/* portmask */
1725387259bdSDeclan Doherty 		case 'p':
1726387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_portmask(options, optarg);
1727387259bdSDeclan Doherty 			if (retval < 0) {
1728387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1729387259bdSDeclan Doherty 				return -1;
1730387259bdSDeclan Doherty 			}
1731387259bdSDeclan Doherty 			break;
1732387259bdSDeclan Doherty 
1733387259bdSDeclan Doherty 		/* nqueue */
1734387259bdSDeclan Doherty 		case 'q':
1735387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_nqueue(options, optarg);
1736387259bdSDeclan Doherty 			if (retval < 0) {
1737387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1738387259bdSDeclan Doherty 				return -1;
1739387259bdSDeclan Doherty 			}
1740387259bdSDeclan Doherty 			break;
1741387259bdSDeclan Doherty 
1742387259bdSDeclan Doherty 		/* single  */
1743387259bdSDeclan Doherty 		case 's':
1744387259bdSDeclan Doherty 			options->single_lcore = 1;
1745387259bdSDeclan Doherty 
1746387259bdSDeclan Doherty 			break;
1747387259bdSDeclan Doherty 
1748387259bdSDeclan Doherty 		/* timer period */
1749a3380989SPablo de Lara 		case 'T':
1750387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_timer_period(options,
1751387259bdSDeclan Doherty 					optarg);
1752387259bdSDeclan Doherty 			if (retval < 0) {
1753387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1754387259bdSDeclan Doherty 				return -1;
1755387259bdSDeclan Doherty 			}
1756387259bdSDeclan Doherty 			break;
1757387259bdSDeclan Doherty 
1758387259bdSDeclan Doherty 		default:
1759387259bdSDeclan Doherty 			l2fwd_crypto_usage(prgname);
1760387259bdSDeclan Doherty 			return -1;
1761387259bdSDeclan Doherty 		}
1762387259bdSDeclan Doherty 	}
1763387259bdSDeclan Doherty 
1764387259bdSDeclan Doherty 
1765387259bdSDeclan Doherty 	if (optind >= 0)
1766387259bdSDeclan Doherty 		argv[optind-1] = prgname;
1767387259bdSDeclan Doherty 
1768387259bdSDeclan Doherty 	retval = optind-1;
17699d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
1770387259bdSDeclan Doherty 
1771387259bdSDeclan Doherty 	return retval;
1772387259bdSDeclan Doherty }
1773387259bdSDeclan Doherty 
1774387259bdSDeclan Doherty /* Check the link status of all ports in up to 9s, and print them finally */
1775387259bdSDeclan Doherty static void
17768728ccf3SThomas Monjalon check_all_ports_link_status(uint32_t port_mask)
1777387259bdSDeclan Doherty {
1778387259bdSDeclan Doherty #define CHECK_INTERVAL 100 /* 100ms */
1779387259bdSDeclan Doherty #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
178047523597SZhiyong Yang 	uint16_t portid;
178147523597SZhiyong Yang 	uint8_t count, all_ports_up, print_flag = 0;
1782387259bdSDeclan Doherty 	struct rte_eth_link link;
178322e5c73bSIgor Romanov 	int ret;
1784db4e8135SIvan Dyukov 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1785387259bdSDeclan Doherty 
1786387259bdSDeclan Doherty 	printf("\nChecking link status");
1787387259bdSDeclan Doherty 	fflush(stdout);
1788387259bdSDeclan Doherty 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1789387259bdSDeclan Doherty 		all_ports_up = 1;
17908728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(portid) {
1791387259bdSDeclan Doherty 			if ((port_mask & (1 << portid)) == 0)
1792387259bdSDeclan Doherty 				continue;
1793387259bdSDeclan Doherty 			memset(&link, 0, sizeof(link));
179422e5c73bSIgor Romanov 			ret = rte_eth_link_get_nowait(portid, &link);
179522e5c73bSIgor Romanov 			if (ret < 0) {
179622e5c73bSIgor Romanov 				all_ports_up = 0;
179722e5c73bSIgor Romanov 				if (print_flag == 1)
179822e5c73bSIgor Romanov 					printf("Port %u link get failed: %s\n",
179922e5c73bSIgor Romanov 						portid, rte_strerror(-ret));
180022e5c73bSIgor Romanov 				continue;
180122e5c73bSIgor Romanov 			}
1802387259bdSDeclan Doherty 			/* print link status if flag set */
1803387259bdSDeclan Doherty 			if (print_flag == 1) {
1804db4e8135SIvan Dyukov 				rte_eth_link_to_str(link_status_text,
1805db4e8135SIvan Dyukov 					sizeof(link_status_text), &link);
1806db4e8135SIvan Dyukov 				printf("Port %d %s\n", portid,
1807db4e8135SIvan Dyukov 					link_status_text);
1808387259bdSDeclan Doherty 				continue;
1809387259bdSDeclan Doherty 			}
1810387259bdSDeclan Doherty 			/* clear all_ports_up flag if any link down */
181109419f23SThomas Monjalon 			if (link.link_status == ETH_LINK_DOWN) {
1812387259bdSDeclan Doherty 				all_ports_up = 0;
1813387259bdSDeclan Doherty 				break;
1814387259bdSDeclan Doherty 			}
1815387259bdSDeclan Doherty 		}
1816387259bdSDeclan Doherty 		/* after finally printing all link status, get out */
1817387259bdSDeclan Doherty 		if (print_flag == 1)
1818387259bdSDeclan Doherty 			break;
1819387259bdSDeclan Doherty 
1820387259bdSDeclan Doherty 		if (all_ports_up == 0) {
1821387259bdSDeclan Doherty 			printf(".");
1822387259bdSDeclan Doherty 			fflush(stdout);
1823387259bdSDeclan Doherty 			rte_delay_ms(CHECK_INTERVAL);
1824387259bdSDeclan Doherty 		}
1825387259bdSDeclan Doherty 
1826387259bdSDeclan Doherty 		/* set the print_flag if all ports up or timeout */
1827387259bdSDeclan Doherty 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1828387259bdSDeclan Doherty 			print_flag = 1;
1829387259bdSDeclan Doherty 			printf("done\n");
1830387259bdSDeclan Doherty 		}
1831387259bdSDeclan Doherty 	}
1832387259bdSDeclan Doherty }
1833387259bdSDeclan Doherty 
183427cf2d1bSPablo de Lara /* Check if device has to be HW/SW or any */
1835387259bdSDeclan Doherty static int
18364baea68dSPablo de Lara check_type(const struct l2fwd_crypto_options *options,
18374baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info)
183827cf2d1bSPablo de Lara {
183927cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_HW &&
184027cf2d1bSPablo de Lara 			(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
184127cf2d1bSPablo de Lara 		return 0;
184227cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_SW &&
184327cf2d1bSPablo de Lara 			!(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
184427cf2d1bSPablo de Lara 		return 0;
184527cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_ANY)
184627cf2d1bSPablo de Lara 		return 0;
184727cf2d1bSPablo de Lara 
184827cf2d1bSPablo de Lara 	return -1;
184927cf2d1bSPablo de Lara }
185027cf2d1bSPablo de Lara 
18514baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
18524baea68dSPablo de Lara check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
18534baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18544baea68dSPablo de Lara 		uint8_t cdev_id)
18554baea68dSPablo de Lara {
18564baea68dSPablo de Lara 	unsigned int i = 0;
18574baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18584baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm cap_cipher_algo;
18594baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm opt_cipher_algo =
18604baea68dSPablo de Lara 					options->cipher_xform.cipher.algo;
18614baea68dSPablo de Lara 
18624baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18634baea68dSPablo de Lara 		cap_cipher_algo = cap->sym.cipher.algo;
18644baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
18654baea68dSPablo de Lara 			if (cap_cipher_algo == opt_cipher_algo) {
18664baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
18674baea68dSPablo de Lara 					break;
18684baea68dSPablo de Lara 			}
18694baea68dSPablo de Lara 		}
18704baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
18714baea68dSPablo de Lara 	}
18724baea68dSPablo de Lara 
18734baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18744baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18754baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
18764baea68dSPablo de Lara 			rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
18774baea68dSPablo de Lara 			cdev_id,
18784baea68dSPablo de Lara 			options->string_type);
18794baea68dSPablo de Lara 		return NULL;
18804baea68dSPablo de Lara 	}
18814baea68dSPablo de Lara 
18824baea68dSPablo de Lara 	return cap;
18834baea68dSPablo de Lara }
18844baea68dSPablo de Lara 
18854baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
18864baea68dSPablo de Lara check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
18874baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18884baea68dSPablo de Lara 		uint8_t cdev_id)
18894baea68dSPablo de Lara {
18904baea68dSPablo de Lara 	unsigned int i = 0;
18914baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18924baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm cap_auth_algo;
18934baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm opt_auth_algo =
18944baea68dSPablo de Lara 					options->auth_xform.auth.algo;
18954baea68dSPablo de Lara 
18964baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18974baea68dSPablo de Lara 		cap_auth_algo = cap->sym.auth.algo;
18984baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
18994baea68dSPablo de Lara 			if (cap_auth_algo == opt_auth_algo) {
19004baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
19014baea68dSPablo de Lara 					break;
19024baea68dSPablo de Lara 			}
19034baea68dSPablo de Lara 		}
19044baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
19054baea68dSPablo de Lara 	}
19064baea68dSPablo de Lara 
19074baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
19084baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
19094baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
19104baea68dSPablo de Lara 			rte_crypto_auth_algorithm_strings[opt_auth_algo],
19114baea68dSPablo de Lara 			cdev_id,
19124baea68dSPablo de Lara 			options->string_type);
19134baea68dSPablo de Lara 		return NULL;
19144baea68dSPablo de Lara 	}
19154baea68dSPablo de Lara 
19164baea68dSPablo de Lara 	return cap;
19174baea68dSPablo de Lara }
19184baea68dSPablo de Lara 
19192661f4fbSPablo de Lara static const struct rte_cryptodev_capabilities *
19202661f4fbSPablo de Lara check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
19212661f4fbSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
19222661f4fbSPablo de Lara 		uint8_t cdev_id)
19232661f4fbSPablo de Lara {
19242661f4fbSPablo de Lara 	unsigned int i = 0;
19252661f4fbSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
19262661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm cap_aead_algo;
19272661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm opt_aead_algo =
19282661f4fbSPablo de Lara 					options->aead_xform.aead.algo;
19292661f4fbSPablo de Lara 
19302661f4fbSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
19312661f4fbSPablo de Lara 		cap_aead_algo = cap->sym.aead.algo;
19322661f4fbSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
19332661f4fbSPablo de Lara 			if (cap_aead_algo == opt_aead_algo) {
19342661f4fbSPablo de Lara 				if (check_type(options, dev_info) == 0)
19352661f4fbSPablo de Lara 					break;
19362661f4fbSPablo de Lara 			}
19372661f4fbSPablo de Lara 		}
19382661f4fbSPablo de Lara 		cap = &dev_info->capabilities[++i];
19392661f4fbSPablo de Lara 	}
19402661f4fbSPablo de Lara 
19412661f4fbSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
19422661f4fbSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
19432661f4fbSPablo de Lara 			" or device not of preferred type (%s)\n",
19442661f4fbSPablo de Lara 			rte_crypto_aead_algorithm_strings[opt_aead_algo],
19452661f4fbSPablo de Lara 			cdev_id,
19462661f4fbSPablo de Lara 			options->string_type);
19472661f4fbSPablo de Lara 		return NULL;
19482661f4fbSPablo de Lara 	}
19492661f4fbSPablo de Lara 
19502661f4fbSPablo de Lara 	return cap;
19512661f4fbSPablo de Lara }
19522661f4fbSPablo de Lara 
1953d2797f51SFan Zhang /* Check if the device is enabled by cryptodev_mask */
1954d2797f51SFan Zhang static int
1955d2797f51SFan Zhang check_cryptodev_mask(struct l2fwd_crypto_options *options,
1956d2797f51SFan Zhang 		uint8_t cdev_id)
1957d2797f51SFan Zhang {
1958d2797f51SFan Zhang 	if (options->cryptodev_mask & (1 << cdev_id))
1959d2797f51SFan Zhang 		return 0;
1960d2797f51SFan Zhang 
1961d2797f51SFan Zhang 	return -1;
1962d2797f51SFan Zhang }
1963d2797f51SFan Zhang 
1964a061e50aSPablo de Lara static inline int
1965a061e50aSPablo de Lara check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1966a061e50aSPablo de Lara 		uint16_t increment)
1967a061e50aSPablo de Lara {
1968a061e50aSPablo de Lara 	uint16_t supp_size;
1969a061e50aSPablo de Lara 
197037ebd9e1SPablo de Lara 	/* Single value */
197137ebd9e1SPablo de Lara 	if (increment == 0) {
197237ebd9e1SPablo de Lara 		if (length == min)
197337ebd9e1SPablo de Lara 			return 0;
197437ebd9e1SPablo de Lara 		else
197537ebd9e1SPablo de Lara 			return -1;
197637ebd9e1SPablo de Lara 	}
197737ebd9e1SPablo de Lara 
197837ebd9e1SPablo de Lara 	/* Range of values */
1979a061e50aSPablo de Lara 	for (supp_size = min; supp_size <= max; supp_size += increment) {
1980a061e50aSPablo de Lara 		if (length == supp_size)
1981a061e50aSPablo de Lara 			return 0;
1982a061e50aSPablo de Lara 	}
1983a061e50aSPablo de Lara 
1984a061e50aSPablo de Lara 	return -1;
1985a061e50aSPablo de Lara }
19860fbd75a9SPablo de Lara 
19870fbd75a9SPablo de Lara static int
19880fbd75a9SPablo de Lara check_iv_param(const struct rte_crypto_param_range *iv_range_size,
19890fbd75a9SPablo de Lara 		unsigned int iv_param, int iv_random_size,
1990a6fde4f1SPablo de Lara 		uint16_t iv_length)
19910fbd75a9SPablo de Lara {
19920fbd75a9SPablo de Lara 	/*
19930fbd75a9SPablo de Lara 	 * Check if length of provided IV is supported
19940fbd75a9SPablo de Lara 	 * by the algorithm chosen.
19950fbd75a9SPablo de Lara 	 */
19960fbd75a9SPablo de Lara 	if (iv_param) {
1997a6fde4f1SPablo de Lara 		if (check_supported_size(iv_length,
19980fbd75a9SPablo de Lara 				iv_range_size->min,
19990fbd75a9SPablo de Lara 				iv_range_size->max,
20000fbd75a9SPablo de Lara 				iv_range_size->increment)
2001a6fde4f1SPablo de Lara 					!= 0)
20020fbd75a9SPablo de Lara 			return -1;
20030fbd75a9SPablo de Lara 	/*
20040fbd75a9SPablo de Lara 	 * Check if length of IV to be randomly generated
20050fbd75a9SPablo de Lara 	 * is supported by the algorithm chosen.
20060fbd75a9SPablo de Lara 	 */
20070fbd75a9SPablo de Lara 	} else if (iv_random_size != -1) {
20080fbd75a9SPablo de Lara 		if (check_supported_size(iv_random_size,
20090fbd75a9SPablo de Lara 				iv_range_size->min,
20100fbd75a9SPablo de Lara 				iv_range_size->max,
20110fbd75a9SPablo de Lara 				iv_range_size->increment)
2012a6fde4f1SPablo de Lara 					!= 0)
20130fbd75a9SPablo de Lara 			return -1;
20140fbd75a9SPablo de Lara 	}
20150fbd75a9SPablo de Lara 
20160fbd75a9SPablo de Lara 	return 0;
20170fbd75a9SPablo de Lara }
20180fbd75a9SPablo de Lara 
201927cf2d1bSPablo de Lara static int
20206ae3fb9dSPablo de Lara check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id)
20216ae3fb9dSPablo de Lara {
20226ae3fb9dSPablo de Lara 	struct rte_cryptodev_info dev_info;
20236ae3fb9dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
20246ae3fb9dSPablo de Lara 
20256ae3fb9dSPablo de Lara 	rte_cryptodev_info_get(cdev_id, &dev_info);
20266ae3fb9dSPablo de Lara 
20276ae3fb9dSPablo de Lara 	/* Set AEAD parameters */
20286ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
20296ae3fb9dSPablo de Lara 		/* Check if device supports AEAD algo */
20306ae3fb9dSPablo de Lara 		cap = check_device_support_aead_algo(options, &dev_info,
20316ae3fb9dSPablo de Lara 						cdev_id);
20326ae3fb9dSPablo de Lara 		if (cap == NULL)
20336ae3fb9dSPablo de Lara 			return -1;
20346ae3fb9dSPablo de Lara 
20356ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.aead.iv_size,
20366ae3fb9dSPablo de Lara 				options->aead_iv_param,
20376ae3fb9dSPablo de Lara 				options->aead_iv_random_size,
20386ae3fb9dSPablo de Lara 				options->aead_iv.length) != 0) {
20396ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
20406ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
20416ae3fb9dSPablo de Lara 				cdev_id);
20426ae3fb9dSPablo de Lara 			return -1;
20436ae3fb9dSPablo de Lara 		}
20446ae3fb9dSPablo de Lara 
20456ae3fb9dSPablo de Lara 		/*
20466ae3fb9dSPablo de Lara 		 * Check if length of provided AEAD key is supported
20476ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
20486ae3fb9dSPablo de Lara 		 */
20496ae3fb9dSPablo de Lara 		if (options->aead_key_param) {
20506ae3fb9dSPablo de Lara 			if (check_supported_size(
20516ae3fb9dSPablo de Lara 					options->aead_xform.aead.key.length,
20526ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.min,
20536ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.max,
20546ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.increment)
20556ae3fb9dSPablo de Lara 						!= 0) {
20566ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20576ae3fb9dSPablo de Lara 					"Device %u does not support "
20586ae3fb9dSPablo de Lara 					"AEAD key length\n",
20596ae3fb9dSPablo de Lara 					cdev_id);
20606ae3fb9dSPablo de Lara 				return -1;
20616ae3fb9dSPablo de Lara 			}
20626ae3fb9dSPablo de Lara 		/*
20636ae3fb9dSPablo de Lara 		 * Check if length of the aead key to be randomly generated
20646ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
20656ae3fb9dSPablo de Lara 		 */
20666ae3fb9dSPablo de Lara 		} else if (options->aead_key_random_size != -1) {
20676ae3fb9dSPablo de Lara 			if (check_supported_size(options->aead_key_random_size,
20686ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.min,
20696ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.max,
20706ae3fb9dSPablo de Lara 					cap->sym.aead.key_size.increment)
20716ae3fb9dSPablo de Lara 						!= 0) {
20726ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20736ae3fb9dSPablo de Lara 					"Device %u does not support "
20746ae3fb9dSPablo de Lara 					"AEAD key length\n",
20756ae3fb9dSPablo de Lara 					cdev_id);
20766ae3fb9dSPablo de Lara 				return -1;
20776ae3fb9dSPablo de Lara 			}
20786ae3fb9dSPablo de Lara 		}
20796ae3fb9dSPablo de Lara 
20806ae3fb9dSPablo de Lara 
20816ae3fb9dSPablo de Lara 		/*
20826ae3fb9dSPablo de Lara 		 * Check if length of provided AAD is supported
20836ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
20846ae3fb9dSPablo de Lara 		 */
20856ae3fb9dSPablo de Lara 		if (options->aad_param) {
20866ae3fb9dSPablo de Lara 			if (check_supported_size(options->aad.length,
20876ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.min,
20886ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.max,
20896ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.increment)
20906ae3fb9dSPablo de Lara 						!= 0) {
20916ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
20926ae3fb9dSPablo de Lara 					"Device %u does not support "
20936ae3fb9dSPablo de Lara 					"AAD length\n",
20946ae3fb9dSPablo de Lara 					cdev_id);
20956ae3fb9dSPablo de Lara 				return -1;
20966ae3fb9dSPablo de Lara 			}
20976ae3fb9dSPablo de Lara 		/*
20986ae3fb9dSPablo de Lara 		 * Check if length of AAD to be randomly generated
20996ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
21006ae3fb9dSPablo de Lara 		 */
21016ae3fb9dSPablo de Lara 		} else if (options->aad_random_size != -1) {
21026ae3fb9dSPablo de Lara 			if (check_supported_size(options->aad_random_size,
21036ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.min,
21046ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.max,
21056ae3fb9dSPablo de Lara 					cap->sym.aead.aad_size.increment)
21066ae3fb9dSPablo de Lara 						!= 0) {
21076ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21086ae3fb9dSPablo de Lara 					"Device %u does not support "
21096ae3fb9dSPablo de Lara 					"AAD length\n",
21106ae3fb9dSPablo de Lara 					cdev_id);
21116ae3fb9dSPablo de Lara 				return -1;
21126ae3fb9dSPablo de Lara 			}
21136ae3fb9dSPablo de Lara 		}
21146ae3fb9dSPablo de Lara 
21156ae3fb9dSPablo de Lara 		/* Check if digest size is supported by the algorithm. */
21166ae3fb9dSPablo de Lara 		if (options->digest_size != -1) {
21176ae3fb9dSPablo de Lara 			if (check_supported_size(options->digest_size,
21186ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.min,
21196ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.max,
21206ae3fb9dSPablo de Lara 					cap->sym.aead.digest_size.increment)
21216ae3fb9dSPablo de Lara 						!= 0) {
21226ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21236ae3fb9dSPablo de Lara 					"Device %u does not support "
21246ae3fb9dSPablo de Lara 					"digest length\n",
21256ae3fb9dSPablo de Lara 					cdev_id);
21266ae3fb9dSPablo de Lara 				return -1;
21276ae3fb9dSPablo de Lara 			}
21286ae3fb9dSPablo de Lara 		}
21296ae3fb9dSPablo de Lara 	}
21306ae3fb9dSPablo de Lara 
21316ae3fb9dSPablo de Lara 	/* Set cipher parameters */
21326ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
21336ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
21346ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
21359a212dc0SConor Fogarty 
21369a212dc0SConor Fogarty 		/* Check if device supports cipher algo. 8< */
21376ae3fb9dSPablo de Lara 		cap = check_device_support_cipher_algo(options, &dev_info,
21386ae3fb9dSPablo de Lara 						cdev_id);
21396ae3fb9dSPablo de Lara 		if (cap == NULL)
21406ae3fb9dSPablo de Lara 			return -1;
21416ae3fb9dSPablo de Lara 
21426ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.cipher.iv_size,
21436ae3fb9dSPablo de Lara 				options->cipher_iv_param,
21446ae3fb9dSPablo de Lara 				options->cipher_iv_random_size,
21456ae3fb9dSPablo de Lara 				options->cipher_iv.length) != 0) {
21466ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
21476ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
21486ae3fb9dSPablo de Lara 				cdev_id);
21496ae3fb9dSPablo de Lara 			return -1;
21506ae3fb9dSPablo de Lara 		}
21519a212dc0SConor Fogarty 		/* >8 End of check if device supports cipher algo. */
21529a212dc0SConor Fogarty 
21539a212dc0SConor Fogarty 		/* Check if capable cipher is supported. 8< */
21546ae3fb9dSPablo de Lara 
21556ae3fb9dSPablo de Lara 		/*
21566ae3fb9dSPablo de Lara 		 * Check if length of provided cipher key is supported
21576ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
21586ae3fb9dSPablo de Lara 		 */
21596ae3fb9dSPablo de Lara 		if (options->ckey_param) {
21606ae3fb9dSPablo de Lara 			if (check_supported_size(
21616ae3fb9dSPablo de Lara 					options->cipher_xform.cipher.key.length,
21626ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.min,
21636ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.max,
21646ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.increment)
21656ae3fb9dSPablo de Lara 						!= 0) {
21663ea6ee38SShiri Kuzin 				if (dev_info.feature_flags &
21673ea6ee38SShiri Kuzin 				    RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY) {
21686ae3fb9dSPablo de Lara 					RTE_LOG(DEBUG, USER1,
21693ea6ee38SShiri Kuzin 					"Key length does not match the device "
21703ea6ee38SShiri Kuzin 					"%u capability. Key may be wrapped\n",
21713ea6ee38SShiri Kuzin 					cdev_id);
21723ea6ee38SShiri Kuzin 				} else {
21733ea6ee38SShiri Kuzin 					RTE_LOG(DEBUG, USER1,
21743ea6ee38SShiri Kuzin 					"Key length does not match the device "
21753ea6ee38SShiri Kuzin 					"%u capability\n",
21766ae3fb9dSPablo de Lara 					cdev_id);
21776ae3fb9dSPablo de Lara 					return -1;
21786ae3fb9dSPablo de Lara 				}
21793ea6ee38SShiri Kuzin 			}
21803ea6ee38SShiri Kuzin 
21816ae3fb9dSPablo de Lara 		/*
21826ae3fb9dSPablo de Lara 		 * Check if length of the cipher key to be randomly generated
21836ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
21846ae3fb9dSPablo de Lara 		 */
21856ae3fb9dSPablo de Lara 		} else if (options->ckey_random_size != -1) {
21866ae3fb9dSPablo de Lara 			if (check_supported_size(options->ckey_random_size,
21876ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.min,
21886ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.max,
21896ae3fb9dSPablo de Lara 					cap->sym.cipher.key_size.increment)
21906ae3fb9dSPablo de Lara 						!= 0) {
21916ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
21926ae3fb9dSPablo de Lara 					"Device %u does not support cipher "
21936ae3fb9dSPablo de Lara 					"key length\n",
21946ae3fb9dSPablo de Lara 					cdev_id);
21956ae3fb9dSPablo de Lara 				return -1;
21966ae3fb9dSPablo de Lara 			}
21976ae3fb9dSPablo de Lara 		}
2198dcf384b4SMatan Azrad 
2199dcf384b4SMatan Azrad 		if (options->cipher_xform.cipher.dataunit_len > 0) {
2200dcf384b4SMatan Azrad 			if (!(dev_info.feature_flags &
2201dcf384b4SMatan Azrad 				RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS)) {
2202dcf384b4SMatan Azrad 				RTE_LOG(DEBUG, USER1,
2203dcf384b4SMatan Azrad 					"Device %u does not support "
2204dcf384b4SMatan Azrad 					"cipher multiple data units\n",
2205dcf384b4SMatan Azrad 					cdev_id);
2206dcf384b4SMatan Azrad 				return -1;
2207dcf384b4SMatan Azrad 			}
2208dcf384b4SMatan Azrad 			if (cap->sym.cipher.dataunit_set != 0) {
2209dcf384b4SMatan Azrad 				int ret = 0;
2210dcf384b4SMatan Azrad 
2211dcf384b4SMatan Azrad 				switch (options->cipher_xform.cipher.dataunit_len) {
2212dcf384b4SMatan Azrad 				case 512:
2213dcf384b4SMatan Azrad 					if (!(cap->sym.cipher.dataunit_set &
2214dcf384b4SMatan Azrad 						RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES))
2215dcf384b4SMatan Azrad 						ret = -1;
2216dcf384b4SMatan Azrad 					break;
2217dcf384b4SMatan Azrad 				case 4096:
2218dcf384b4SMatan Azrad 					if (!(cap->sym.cipher.dataunit_set &
2219dcf384b4SMatan Azrad 						RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_4096_BYTES))
2220dcf384b4SMatan Azrad 						ret = -1;
2221dcf384b4SMatan Azrad 					break;
2222dcf384b4SMatan Azrad 				default:
2223dcf384b4SMatan Azrad 					ret = -1;
2224dcf384b4SMatan Azrad 				}
2225dcf384b4SMatan Azrad 				if (ret == -1) {
2226dcf384b4SMatan Azrad 					RTE_LOG(DEBUG, USER1,
2227dcf384b4SMatan Azrad 						"Device %u does not support "
2228dcf384b4SMatan Azrad 						"data-unit length %u\n",
2229dcf384b4SMatan Azrad 						cdev_id,
2230dcf384b4SMatan Azrad 						options->cipher_xform.cipher.dataunit_len);
2231dcf384b4SMatan Azrad 					return -1;
2232dcf384b4SMatan Azrad 				}
2233dcf384b4SMatan Azrad 			}
2234dcf384b4SMatan Azrad 		}
22359a212dc0SConor Fogarty 		/* >8 End of checking if cipher is supported. */
22366ae3fb9dSPablo de Lara 	}
22376ae3fb9dSPablo de Lara 
22386ae3fb9dSPablo de Lara 	/* Set auth parameters */
22396ae3fb9dSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
22406ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
22416ae3fb9dSPablo de Lara 			options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
22426ae3fb9dSPablo de Lara 		/* Check if device supports auth algo */
22436ae3fb9dSPablo de Lara 		cap = check_device_support_auth_algo(options, &dev_info,
22446ae3fb9dSPablo de Lara 						cdev_id);
22456ae3fb9dSPablo de Lara 		if (cap == NULL)
22466ae3fb9dSPablo de Lara 			return -1;
22476ae3fb9dSPablo de Lara 
22486ae3fb9dSPablo de Lara 		if (check_iv_param(&cap->sym.auth.iv_size,
22496ae3fb9dSPablo de Lara 				options->auth_iv_param,
22506ae3fb9dSPablo de Lara 				options->auth_iv_random_size,
22516ae3fb9dSPablo de Lara 				options->auth_iv.length) != 0) {
22526ae3fb9dSPablo de Lara 			RTE_LOG(DEBUG, USER1,
22536ae3fb9dSPablo de Lara 				"Device %u does not support IV length\n",
22546ae3fb9dSPablo de Lara 				cdev_id);
22556ae3fb9dSPablo de Lara 			return -1;
22566ae3fb9dSPablo de Lara 		}
22576ae3fb9dSPablo de Lara 		/*
22586ae3fb9dSPablo de Lara 		 * Check if length of provided auth key is supported
22596ae3fb9dSPablo de Lara 		 * by the algorithm chosen.
22606ae3fb9dSPablo de Lara 		 */
22616ae3fb9dSPablo de Lara 		if (options->akey_param) {
22626ae3fb9dSPablo de Lara 			if (check_supported_size(
22636ae3fb9dSPablo de Lara 					options->auth_xform.auth.key.length,
22646ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.min,
22656ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.max,
22666ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.increment)
22676ae3fb9dSPablo de Lara 						!= 0) {
22686ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
22696ae3fb9dSPablo de Lara 					"Device %u does not support auth "
22706ae3fb9dSPablo de Lara 					"key length\n",
22716ae3fb9dSPablo de Lara 					cdev_id);
22726ae3fb9dSPablo de Lara 				return -1;
22736ae3fb9dSPablo de Lara 			}
22746ae3fb9dSPablo de Lara 		/*
22756ae3fb9dSPablo de Lara 		 * Check if length of the auth key to be randomly generated
22766ae3fb9dSPablo de Lara 		 * is supported by the algorithm chosen.
22776ae3fb9dSPablo de Lara 		 */
22786ae3fb9dSPablo de Lara 		} else if (options->akey_random_size != -1) {
22796ae3fb9dSPablo de Lara 			if (check_supported_size(options->akey_random_size,
22806ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.min,
22816ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.max,
22826ae3fb9dSPablo de Lara 					cap->sym.auth.key_size.increment)
22836ae3fb9dSPablo de Lara 						!= 0) {
22846ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
22856ae3fb9dSPablo de Lara 					"Device %u does not support auth "
22866ae3fb9dSPablo de Lara 					"key length\n",
22876ae3fb9dSPablo de Lara 					cdev_id);
22886ae3fb9dSPablo de Lara 				return -1;
22896ae3fb9dSPablo de Lara 			}
22906ae3fb9dSPablo de Lara 		}
22916ae3fb9dSPablo de Lara 
22926ae3fb9dSPablo de Lara 		/* Check if digest size is supported by the algorithm. */
22936ae3fb9dSPablo de Lara 		if (options->digest_size != -1) {
22946ae3fb9dSPablo de Lara 			if (check_supported_size(options->digest_size,
22956ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.min,
22966ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.max,
22976ae3fb9dSPablo de Lara 					cap->sym.auth.digest_size.increment)
22986ae3fb9dSPablo de Lara 						!= 0) {
22996ae3fb9dSPablo de Lara 				RTE_LOG(DEBUG, USER1,
23006ae3fb9dSPablo de Lara 					"Device %u does not support "
23016ae3fb9dSPablo de Lara 					"digest length\n",
23026ae3fb9dSPablo de Lara 					cdev_id);
23036ae3fb9dSPablo de Lara 				return -1;
23046ae3fb9dSPablo de Lara 			}
23056ae3fb9dSPablo de Lara 		}
23066ae3fb9dSPablo de Lara 	}
23076ae3fb9dSPablo de Lara 
23086ae3fb9dSPablo de Lara 	return 0;
23096ae3fb9dSPablo de Lara }
23106ae3fb9dSPablo de Lara 
23116ae3fb9dSPablo de Lara static int
231227cf2d1bSPablo de Lara initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
231327cf2d1bSPablo de Lara 		uint8_t *enabled_cdevs)
2314387259bdSDeclan Doherty {
23156ae3fb9dSPablo de Lara 	uint8_t cdev_id, cdev_count, enabled_cdev_count = 0;
231627cf2d1bSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
23172c59bd32SSlawomir Mrozowicz 	unsigned int sess_sz, max_sess_sz = 0;
2318e3bcb99aSPablo de Lara 	uint32_t sessions_needed = 0;
2319387259bdSDeclan Doherty 	int retval;
2320387259bdSDeclan Doherty 
232127cf2d1bSPablo de Lara 	cdev_count = rte_cryptodev_count();
232227cf2d1bSPablo de Lara 	if (cdev_count == 0) {
232327cf2d1bSPablo de Lara 		printf("No crypto devices available\n");
2324387259bdSDeclan Doherty 		return -1;
2325387259bdSDeclan Doherty 	}
2326387259bdSDeclan Doherty 
23276ae3fb9dSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
23286ae3fb9dSPablo de Lara 			cdev_id++) {
23296ae3fb9dSPablo de Lara 		if (check_cryptodev_mask(options, cdev_id) < 0)
23306ae3fb9dSPablo de Lara 			continue;
23316ae3fb9dSPablo de Lara 
23326ae3fb9dSPablo de Lara 		if (check_capabilities(options, cdev_id) < 0)
23336ae3fb9dSPablo de Lara 			continue;
23346ae3fb9dSPablo de Lara 
2335a106fcceSPablo de Lara 		sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
23362c59bd32SSlawomir Mrozowicz 		if (sess_sz > max_sess_sz)
23372c59bd32SSlawomir Mrozowicz 			max_sess_sz = sess_sz;
23386ae3fb9dSPablo de Lara 
23396ae3fb9dSPablo de Lara 		l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
23406ae3fb9dSPablo de Lara 
23416ae3fb9dSPablo de Lara 		enabled_cdevs[cdev_id] = 1;
23426ae3fb9dSPablo de Lara 		enabled_cdev_count++;
23432c59bd32SSlawomir Mrozowicz 	}
23442c59bd32SSlawomir Mrozowicz 
23456ae3fb9dSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2346387259bdSDeclan Doherty 		struct rte_cryptodev_qp_conf qp_conf;
2347387259bdSDeclan Doherty 		struct rte_cryptodev_info dev_info;
23486ae3fb9dSPablo de Lara 
23496ae3fb9dSPablo de Lara 		if (enabled_cdevs[cdev_id] == 0)
23506ae3fb9dSPablo de Lara 			continue;
23516ae3fb9dSPablo de Lara 
235258266cc9SApeksha Gupta 		if (check_cryptodev_mask(options, cdev_id) < 0)
235358266cc9SApeksha Gupta 			continue;
235458266cc9SApeksha Gupta 
235558266cc9SApeksha Gupta 		if (check_capabilities(options, cdev_id) < 0)
235658266cc9SApeksha Gupta 			continue;
235758266cc9SApeksha Gupta 
23588dbc9bbfSPablo de Lara 		retval = rte_cryptodev_socket_id(cdev_id);
23598dbc9bbfSPablo de Lara 
23608dbc9bbfSPablo de Lara 		if (retval < 0) {
23618dbc9bbfSPablo de Lara 			printf("Invalid crypto device id used\n");
23628dbc9bbfSPablo de Lara 			return -1;
23638dbc9bbfSPablo de Lara 		}
23648dbc9bbfSPablo de Lara 
23658dbc9bbfSPablo de Lara 		uint8_t socket_id = (uint8_t) retval;
2366387259bdSDeclan Doherty 
2367387259bdSDeclan Doherty 		struct rte_cryptodev_config conf = {
2368387259bdSDeclan Doherty 			.nb_queue_pairs = 1,
23692c59bd32SSlawomir Mrozowicz 			.socket_id = socket_id,
2370c9030ae3SAnoob Joseph 			.ff_disable = RTE_CRYPTODEV_FF_SECURITY,
2371387259bdSDeclan Doherty 		};
2372387259bdSDeclan Doherty 
2373387259bdSDeclan Doherty 		rte_cryptodev_info_get(cdev_id, &dev_info);
2374387259bdSDeclan Doherty 
2375e3bcb99aSPablo de Lara 		/*
2376e3bcb99aSPablo de Lara 		 * Two sessions objects are required for each session
2377e3bcb99aSPablo de Lara 		 * (one for the header, one for the private data)
2378e3bcb99aSPablo de Lara 		 */
2379e3bcb99aSPablo de Lara 		if (!strcmp(dev_info.driver_name, "crypto_scheduler")) {
2380a8d0d473SBruce Richardson #ifdef RTE_CRYPTO_SCHEDULER
238185b00824SAdam Dybkowski 			uint32_t nb_workers =
238285b00824SAdam Dybkowski 				rte_cryptodev_scheduler_workers_get(cdev_id,
2383e3bcb99aSPablo de Lara 								NULL);
2384e3bcb99aSPablo de Lara 
238585b00824SAdam Dybkowski 			sessions_needed = enabled_cdev_count * nb_workers;
2386e3bcb99aSPablo de Lara #endif
2387e3bcb99aSPablo de Lara 		} else
2388261bbff7SFan Zhang 			sessions_needed = enabled_cdev_count;
2389e3bcb99aSPablo de Lara 
2390261bbff7SFan Zhang 		if (session_pool_socket[socket_id].priv_mp == NULL) {
23912c59bd32SSlawomir Mrozowicz 			char mp_name[RTE_MEMPOOL_NAMESIZE];
23922c59bd32SSlawomir Mrozowicz 
23932c59bd32SSlawomir Mrozowicz 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2394261bbff7SFan Zhang 				"priv_sess_mp_%u", socket_id);
23952c59bd32SSlawomir Mrozowicz 
2396261bbff7SFan Zhang 			session_pool_socket[socket_id].priv_mp =
2397261bbff7SFan Zhang 					rte_mempool_create(mp_name,
2398e3bcb99aSPablo de Lara 						sessions_needed,
23992c59bd32SSlawomir Mrozowicz 						max_sess_sz,
2400261bbff7SFan Zhang 						0, 0, NULL, NULL, NULL,
24012c59bd32SSlawomir Mrozowicz 						NULL, socket_id,
24022c59bd32SSlawomir Mrozowicz 						0);
24032c59bd32SSlawomir Mrozowicz 
2404261bbff7SFan Zhang 			if (session_pool_socket[socket_id].priv_mp == NULL) {
2405261bbff7SFan Zhang 				printf("Cannot create pool on socket %d\n",
24062c59bd32SSlawomir Mrozowicz 					socket_id);
24072c59bd32SSlawomir Mrozowicz 				return -ENOMEM;
24082c59bd32SSlawomir Mrozowicz 			}
24092c59bd32SSlawomir Mrozowicz 
2410261bbff7SFan Zhang 			printf("Allocated pool \"%s\" on socket %d\n",
2411261bbff7SFan Zhang 				mp_name, socket_id);
2412261bbff7SFan Zhang 		}
2413261bbff7SFan Zhang 
2414261bbff7SFan Zhang 		if (session_pool_socket[socket_id].sess_mp == NULL) {
2415261bbff7SFan Zhang 			char mp_name[RTE_MEMPOOL_NAMESIZE];
2416261bbff7SFan Zhang 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2417261bbff7SFan Zhang 				"sess_mp_%u", socket_id);
2418261bbff7SFan Zhang 
2419261bbff7SFan Zhang 			session_pool_socket[socket_id].sess_mp =
2420261bbff7SFan Zhang 					rte_cryptodev_sym_session_pool_create(
2421261bbff7SFan Zhang 							mp_name,
2422261bbff7SFan Zhang 							sessions_needed,
2423261bbff7SFan Zhang 							0, 0, 0, socket_id);
2424261bbff7SFan Zhang 
2425261bbff7SFan Zhang 			if (session_pool_socket[socket_id].sess_mp == NULL) {
2426261bbff7SFan Zhang 				printf("Cannot create pool on socket %d\n",
2427261bbff7SFan Zhang 					socket_id);
2428261bbff7SFan Zhang 				return -ENOMEM;
2429261bbff7SFan Zhang 			}
2430261bbff7SFan Zhang 
2431261bbff7SFan Zhang 			printf("Allocated pool \"%s\" on socket %d\n",
2432261bbff7SFan Zhang 				mp_name, socket_id);
24332c59bd32SSlawomir Mrozowicz 		}
24342c59bd32SSlawomir Mrozowicz 
24352661f4fbSPablo de Lara 		/* Set AEAD parameters */
24362661f4fbSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
24372661f4fbSPablo de Lara 			cap = check_device_support_aead_algo(options, &dev_info,
24382661f4fbSPablo de Lara 							cdev_id);
24392661f4fbSPablo de Lara 
24402661f4fbSPablo de Lara 			options->block_size = cap->sym.aead.block_size;
24412661f4fbSPablo de Lara 
2442a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2443a6fde4f1SPablo de Lara 			if (options->aead_iv_param == 0) {
2444a6fde4f1SPablo de Lara 				if (options->aead_iv_random_size != -1)
2445a6fde4f1SPablo de Lara 					options->aead_iv.length =
2446a6fde4f1SPablo de Lara 						options->aead_iv_random_size;
2447a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2448a6fde4f1SPablo de Lara 				else
2449a6fde4f1SPablo de Lara 					options->aead_iv.length =
2450a6fde4f1SPablo de Lara 						cap->sym.aead.iv_size.min;
2451a6fde4f1SPablo de Lara 			}
24522661f4fbSPablo de Lara 
24530b920a5fSPablo de Lara 			/* Set key if not provided from command line */
24540b920a5fSPablo de Lara 			if (options->aead_key_param == 0) {
24550b920a5fSPablo de Lara 				if (options->aead_key_random_size != -1)
24562661f4fbSPablo de Lara 					options->aead_xform.aead.key.length =
2457459369fcSPablo de Lara 						options->aead_key_random_size;
24582661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
24590b920a5fSPablo de Lara 				else
24602661f4fbSPablo de Lara 					options->aead_xform.aead.key.length =
24612661f4fbSPablo de Lara 						cap->sym.aead.key_size.min;
24622661f4fbSPablo de Lara 
2463186b14d6SFan Zhang 				generate_random_key(options->aead_key,
24642661f4fbSPablo de Lara 					options->aead_xform.aead.key.length);
24650b920a5fSPablo de Lara 			}
24662661f4fbSPablo de Lara 
24670b920a5fSPablo de Lara 			/* Set AAD if not provided from command line */
24680b920a5fSPablo de Lara 			if (options->aad_param == 0) {
24690b920a5fSPablo de Lara 				if (options->aad_random_size != -1)
24700b920a5fSPablo de Lara 					options->aad.length =
24710b920a5fSPablo de Lara 						options->aad_random_size;
24722661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
24730b920a5fSPablo de Lara 				else
24740b920a5fSPablo de Lara 					options->aad.length =
24750b920a5fSPablo de Lara 						cap->sym.auth.aad_size.min;
24760b920a5fSPablo de Lara 			}
24772661f4fbSPablo de Lara 
247846a0547fSPablo de Lara 			options->aead_xform.aead.aad_length =
24792661f4fbSPablo de Lara 						options->aad.length;
24802661f4fbSPablo de Lara 
24810b920a5fSPablo de Lara 			/* Set digest size if not provided from command line */
24820b920a5fSPablo de Lara 			if (options->digest_size != -1)
24832661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
24842661f4fbSPablo de Lara 							options->digest_size;
24852661f4fbSPablo de Lara 				/* No size provided, use minimum size. */
24860b920a5fSPablo de Lara 			else
24872661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
24882661f4fbSPablo de Lara 						cap->sym.aead.digest_size.min;
24892661f4fbSPablo de Lara 		}
24902661f4fbSPablo de Lara 
249127cf2d1bSPablo de Lara 		/* Set cipher parameters */
249227cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
249327cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
249427cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
24954baea68dSPablo de Lara 			cap = check_device_support_cipher_algo(options, &dev_info,
24964baea68dSPablo de Lara 							cdev_id);
249727cf2d1bSPablo de Lara 			options->block_size = cap->sym.cipher.block_size;
24980fbd75a9SPablo de Lara 
2499a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2500a6fde4f1SPablo de Lara 			if (options->cipher_iv_param == 0) {
2501a6fde4f1SPablo de Lara 				if (options->cipher_iv_random_size != -1)
2502a6fde4f1SPablo de Lara 					options->cipher_iv.length =
2503a6fde4f1SPablo de Lara 						options->cipher_iv_random_size;
2504a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2505a6fde4f1SPablo de Lara 				else
2506a6fde4f1SPablo de Lara 					options->cipher_iv.length =
2507a6fde4f1SPablo de Lara 						cap->sym.cipher.iv_size.min;
2508a6fde4f1SPablo de Lara 			}
2509a061e50aSPablo de Lara 
25100b920a5fSPablo de Lara 			/* Set key if not provided from command line */
25110b920a5fSPablo de Lara 			if (options->ckey_param == 0) {
25120b920a5fSPablo de Lara 				if (options->ckey_random_size != -1)
2513a061e50aSPablo de Lara 					options->cipher_xform.cipher.key.length =
2514a061e50aSPablo de Lara 						options->ckey_random_size;
2515a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
25160b920a5fSPablo de Lara 				else
251727cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length =
251827cf2d1bSPablo de Lara 						cap->sym.cipher.key_size.min;
2519a061e50aSPablo de Lara 
2520186b14d6SFan Zhang 				generate_random_key(options->cipher_key,
252127cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length);
25220b920a5fSPablo de Lara 			}
252327cf2d1bSPablo de Lara 		}
252427cf2d1bSPablo de Lara 
252527cf2d1bSPablo de Lara 		/* Set auth parameters */
252627cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
252727cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
252827cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
25294baea68dSPablo de Lara 			cap = check_device_support_auth_algo(options, &dev_info,
25304baea68dSPablo de Lara 							cdev_id);
2531a6fde4f1SPablo de Lara 
2532a6fde4f1SPablo de Lara 			/* Set IV if not provided from command line */
2533a6fde4f1SPablo de Lara 			if (options->auth_iv_param == 0) {
2534a6fde4f1SPablo de Lara 				if (options->auth_iv_random_size != -1)
2535a6fde4f1SPablo de Lara 					options->auth_iv.length =
2536a6fde4f1SPablo de Lara 						options->auth_iv_random_size;
2537a6fde4f1SPablo de Lara 				/* No size provided, use minimum size. */
2538a6fde4f1SPablo de Lara 				else
2539a6fde4f1SPablo de Lara 					options->auth_iv.length =
2540a6fde4f1SPablo de Lara 						cap->sym.auth.iv_size.min;
2541a6fde4f1SPablo de Lara 			}
2542a6fde4f1SPablo de Lara 
25430b920a5fSPablo de Lara 			/* Set key if not provided from command line */
25440b920a5fSPablo de Lara 			if (options->akey_param == 0) {
25450b920a5fSPablo de Lara 				if (options->akey_random_size != -1)
2546a061e50aSPablo de Lara 					options->auth_xform.auth.key.length =
2547a061e50aSPablo de Lara 						options->akey_random_size;
2548a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
25490b920a5fSPablo de Lara 				else
255027cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length =
255127cf2d1bSPablo de Lara 						cap->sym.auth.key_size.min;
255227cf2d1bSPablo de Lara 
2553186b14d6SFan Zhang 				generate_random_key(options->auth_key,
255427cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length);
2555a061e50aSPablo de Lara 			}
25560b920a5fSPablo de Lara 
25570b920a5fSPablo de Lara 			/* Set digest size if not provided from command line */
25580b920a5fSPablo de Lara 			if (options->digest_size != -1)
2559a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2560a061e50aSPablo de Lara 							options->digest_size;
2561a061e50aSPablo de Lara 				/* No size provided, use minimum size. */
25620b920a5fSPablo de Lara 			else
2563a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2564a061e50aSPablo de Lara 						cap->sym.auth.digest_size.min;
256527cf2d1bSPablo de Lara 		}
2566387259bdSDeclan Doherty 
2567f7db6f82SPablo de Lara 		retval = rte_cryptodev_configure(cdev_id, &conf);
2568387259bdSDeclan Doherty 		if (retval < 0) {
2569387259bdSDeclan Doherty 			printf("Failed to configure cryptodev %u", cdev_id);
2570387259bdSDeclan Doherty 			return -1;
2571387259bdSDeclan Doherty 		}
2572387259bdSDeclan Doherty 
2573387259bdSDeclan Doherty 		qp_conf.nb_descriptors = 2048;
2574261bbff7SFan Zhang 		qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
2575261bbff7SFan Zhang 		qp_conf.mp_session_private =
2576261bbff7SFan Zhang 				session_pool_socket[socket_id].priv_mp;
2577387259bdSDeclan Doherty 
2578387259bdSDeclan Doherty 		retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2579725d2a7fSFan Zhang 				socket_id);
2580387259bdSDeclan Doherty 		if (retval < 0) {
2581387259bdSDeclan Doherty 			printf("Failed to setup queue pair %u on cryptodev %u",
2582387259bdSDeclan Doherty 					0, cdev_id);
2583387259bdSDeclan Doherty 			return -1;
2584387259bdSDeclan Doherty 		}
2585387259bdSDeclan Doherty 
2586800386e6SHemant Agrawal 		retval = rte_cryptodev_start(cdev_id);
2587800386e6SHemant Agrawal 		if (retval < 0) {
2588800386e6SHemant Agrawal 			printf("Failed to start device %u: error %d\n",
2589800386e6SHemant Agrawal 					cdev_id, retval);
2590800386e6SHemant Agrawal 			return -1;
2591800386e6SHemant Agrawal 		}
2592387259bdSDeclan Doherty 	}
2593387259bdSDeclan Doherty 
2594387259bdSDeclan Doherty 	return enabled_cdev_count;
2595387259bdSDeclan Doherty }
2596387259bdSDeclan Doherty 
2597387259bdSDeclan Doherty static int
2598387259bdSDeclan Doherty initialize_ports(struct l2fwd_crypto_options *options)
2599387259bdSDeclan Doherty {
26008728ccf3SThomas Monjalon 	uint16_t last_portid = 0, portid;
2601387259bdSDeclan Doherty 	unsigned enabled_portcount = 0;
2602d9a42a69SThomas Monjalon 	unsigned nb_ports = rte_eth_dev_count_avail();
2603387259bdSDeclan Doherty 
2604387259bdSDeclan Doherty 	if (nb_ports == 0) {
2605387259bdSDeclan Doherty 		printf("No Ethernet ports - bye\n");
2606387259bdSDeclan Doherty 		return -1;
2607387259bdSDeclan Doherty 	}
2608387259bdSDeclan Doherty 
2609387259bdSDeclan Doherty 	/* Reset l2fwd_dst_ports */
2610387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2611387259bdSDeclan Doherty 		l2fwd_dst_ports[portid] = 0;
2612387259bdSDeclan Doherty 
26138728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
2614387259bdSDeclan Doherty 		int retval;
2615f2b713e3SShahaf Shuler 		struct rte_eth_dev_info dev_info;
2616f2b713e3SShahaf Shuler 		struct rte_eth_rxconf rxq_conf;
2617f2b713e3SShahaf Shuler 		struct rte_eth_txconf txq_conf;
2618f2b713e3SShahaf Shuler 		struct rte_eth_conf local_port_conf = port_conf;
2619387259bdSDeclan Doherty 
2620387259bdSDeclan Doherty 		/* Skip ports that are not enabled */
2621387259bdSDeclan Doherty 		if ((options->portmask & (1 << portid)) == 0)
2622387259bdSDeclan Doherty 			continue;
2623387259bdSDeclan Doherty 
2624387259bdSDeclan Doherty 		/* init port */
2625e2cdfbd0SZhiyong Yang 		printf("Initializing port %u... ", portid);
2626387259bdSDeclan Doherty 		fflush(stdout);
2627089e5ed7SIvan Ilchenko 
2628089e5ed7SIvan Ilchenko 		retval = rte_eth_dev_info_get(portid, &dev_info);
2629089e5ed7SIvan Ilchenko 		if (retval != 0) {
2630089e5ed7SIvan Ilchenko 			printf("Error during getting device (port %u) info: %s\n",
2631089e5ed7SIvan Ilchenko 					portid, strerror(-retval));
2632089e5ed7SIvan Ilchenko 			return retval;
2633089e5ed7SIvan Ilchenko 		}
2634089e5ed7SIvan Ilchenko 
2635f2b713e3SShahaf Shuler 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2636f2b713e3SShahaf Shuler 			local_port_conf.txmode.offloads |=
2637f2b713e3SShahaf Shuler 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2638f2b713e3SShahaf Shuler 		retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2639387259bdSDeclan Doherty 		if (retval < 0) {
2640387259bdSDeclan Doherty 			printf("Cannot configure device: err=%d, port=%u\n",
2641e2cdfbd0SZhiyong Yang 				  retval, portid);
2642387259bdSDeclan Doherty 			return -1;
2643387259bdSDeclan Doherty 		}
2644387259bdSDeclan Doherty 
264560efb44fSRoman Zhukov 		retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
264660efb44fSRoman Zhukov 							  &nb_txd);
264760efb44fSRoman Zhukov 		if (retval < 0) {
264860efb44fSRoman Zhukov 			printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2649e2cdfbd0SZhiyong Yang 				retval, portid);
265060efb44fSRoman Zhukov 			return -1;
265160efb44fSRoman Zhukov 		}
265260efb44fSRoman Zhukov 
2653387259bdSDeclan Doherty 		/* init one RX queue */
2654387259bdSDeclan Doherty 		fflush(stdout);
2655f2b713e3SShahaf Shuler 		rxq_conf = dev_info.default_rxconf;
2656f2b713e3SShahaf Shuler 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
2657387259bdSDeclan Doherty 		retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2658387259bdSDeclan Doherty 					     rte_eth_dev_socket_id(portid),
2659f2b713e3SShahaf Shuler 					     &rxq_conf, l2fwd_pktmbuf_pool);
2660387259bdSDeclan Doherty 		if (retval < 0) {
2661387259bdSDeclan Doherty 			printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2662e2cdfbd0SZhiyong Yang 					retval, portid);
2663387259bdSDeclan Doherty 			return -1;
2664387259bdSDeclan Doherty 		}
2665387259bdSDeclan Doherty 
2666387259bdSDeclan Doherty 		/* init one TX queue on each port */
2667387259bdSDeclan Doherty 		fflush(stdout);
2668f2b713e3SShahaf Shuler 		txq_conf = dev_info.default_txconf;
2669f2b713e3SShahaf Shuler 		txq_conf.offloads = local_port_conf.txmode.offloads;
2670387259bdSDeclan Doherty 		retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2671387259bdSDeclan Doherty 				rte_eth_dev_socket_id(portid),
2672f2b713e3SShahaf Shuler 				&txq_conf);
2673387259bdSDeclan Doherty 		if (retval < 0) {
2674387259bdSDeclan Doherty 			printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2675e2cdfbd0SZhiyong Yang 				retval, portid);
2676387259bdSDeclan Doherty 
2677387259bdSDeclan Doherty 			return -1;
2678387259bdSDeclan Doherty 		}
2679387259bdSDeclan Doherty 
2680387259bdSDeclan Doherty 		/* Start device */
2681387259bdSDeclan Doherty 		retval = rte_eth_dev_start(portid);
2682387259bdSDeclan Doherty 		if (retval < 0) {
2683387259bdSDeclan Doherty 			printf("rte_eth_dev_start:err=%d, port=%u\n",
2684e2cdfbd0SZhiyong Yang 					retval, portid);
2685387259bdSDeclan Doherty 			return -1;
2686387259bdSDeclan Doherty 		}
2687387259bdSDeclan Doherty 
2688f430bbceSIvan Ilchenko 		retval = rte_eth_promiscuous_enable(portid);
2689f430bbceSIvan Ilchenko 		if (retval != 0) {
2690f430bbceSIvan Ilchenko 			printf("rte_eth_promiscuous_enable:err=%s, port=%u\n",
2691f430bbceSIvan Ilchenko 				rte_strerror(-retval), portid);
2692f430bbceSIvan Ilchenko 			return -1;
2693f430bbceSIvan Ilchenko 		}
2694387259bdSDeclan Doherty 
269570febdcfSIgor Romanov 		retval = rte_eth_macaddr_get(portid,
269670febdcfSIgor Romanov 					     &l2fwd_ports_eth_addr[portid]);
269770febdcfSIgor Romanov 		if (retval < 0) {
269870febdcfSIgor Romanov 			printf("rte_eth_macaddr_get :err=%d, port=%u\n",
269970febdcfSIgor Romanov 					retval, portid);
270070febdcfSIgor Romanov 			return -1;
270170febdcfSIgor Romanov 		}
2702387259bdSDeclan Doherty 
2703c2c4f87bSAman Deep Singh 		printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
2704e2cdfbd0SZhiyong Yang 			portid,
2705a7db3afcSAman Deep Singh 			RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid]));
2706387259bdSDeclan Doherty 
2707387259bdSDeclan Doherty 		/* initialize port stats */
2708387259bdSDeclan Doherty 		memset(&port_statistics, 0, sizeof(port_statistics));
2709387259bdSDeclan Doherty 
2710387259bdSDeclan Doherty 		/* Setup port forwarding table */
2711387259bdSDeclan Doherty 		if (enabled_portcount % 2) {
2712387259bdSDeclan Doherty 			l2fwd_dst_ports[portid] = last_portid;
2713387259bdSDeclan Doherty 			l2fwd_dst_ports[last_portid] = portid;
2714387259bdSDeclan Doherty 		} else {
2715387259bdSDeclan Doherty 			last_portid = portid;
2716387259bdSDeclan Doherty 		}
2717387259bdSDeclan Doherty 
2718387259bdSDeclan Doherty 		l2fwd_enabled_port_mask |= (1 << portid);
2719387259bdSDeclan Doherty 		enabled_portcount++;
2720387259bdSDeclan Doherty 	}
2721387259bdSDeclan Doherty 
2722387259bdSDeclan Doherty 	if (enabled_portcount == 1) {
2723387259bdSDeclan Doherty 		l2fwd_dst_ports[last_portid] = last_portid;
2724387259bdSDeclan Doherty 	} else if (enabled_portcount % 2) {
2725387259bdSDeclan Doherty 		printf("odd number of ports in portmask- bye\n");
2726387259bdSDeclan Doherty 		return -1;
2727387259bdSDeclan Doherty 	}
2728387259bdSDeclan Doherty 
27298728ccf3SThomas Monjalon 	check_all_ports_link_status(l2fwd_enabled_port_mask);
2730387259bdSDeclan Doherty 
2731387259bdSDeclan Doherty 	return enabled_portcount;
2732387259bdSDeclan Doherty }
2733387259bdSDeclan Doherty 
27341df9c010SPablo de Lara static void
27351df9c010SPablo de Lara reserve_key_memory(struct l2fwd_crypto_options *options)
27361df9c010SPablo de Lara {
2737186b14d6SFan Zhang 	options->cipher_xform.cipher.key.data = options->cipher_key;
27381df9c010SPablo de Lara 
2739186b14d6SFan Zhang 	options->auth_xform.auth.key.data = options->auth_key;
27401df9c010SPablo de Lara 
2741186b14d6SFan Zhang 	options->aead_xform.aead.key.data = options->aead_key;
27422661f4fbSPablo de Lara 
2743acf86169SPablo de Lara 	options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2744acf86169SPablo de Lara 	if (options->cipher_iv.data == NULL)
2745acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2746acf86169SPablo de Lara 
2747acf86169SPablo de Lara 	options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2748acf86169SPablo de Lara 	if (options->auth_iv.data == NULL)
2749acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2750617a7949SPablo de Lara 
27512661f4fbSPablo de Lara 	options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
27522661f4fbSPablo de Lara 	if (options->aead_iv.data == NULL)
27532661f4fbSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
27542661f4fbSPablo de Lara 
2755617a7949SPablo de Lara 	options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2756617a7949SPablo de Lara 	if (options->aad.data == NULL)
2757617a7949SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
275887cf4c6cSThomas Monjalon 	options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
27591df9c010SPablo de Lara }
27601df9c010SPablo de Lara 
2761387259bdSDeclan Doherty int
2762387259bdSDeclan Doherty main(int argc, char **argv)
2763387259bdSDeclan Doherty {
27648728ccf3SThomas Monjalon 	struct lcore_queue_conf *qconf = NULL;
2765387259bdSDeclan Doherty 	struct l2fwd_crypto_options options;
2766387259bdSDeclan Doherty 
2767e2cdfbd0SZhiyong Yang 	uint8_t nb_cryptodevs, cdev_id;
27688728ccf3SThomas Monjalon 	uint16_t portid;
27698728ccf3SThomas Monjalon 	unsigned lcore_id, rx_lcore_id = 0;
2770387259bdSDeclan Doherty 	int ret, enabled_cdevcount, enabled_portcount;
277127cf2d1bSPablo de Lara 	uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2772387259bdSDeclan Doherty 
2773387259bdSDeclan Doherty 	/* init EAL */
2774387259bdSDeclan Doherty 	ret = rte_eal_init(argc, argv);
2775387259bdSDeclan Doherty 	if (ret < 0)
2776387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2777387259bdSDeclan Doherty 	argc -= ret;
2778387259bdSDeclan Doherty 	argv += ret;
2779387259bdSDeclan Doherty 
27801df9c010SPablo de Lara 	/* reserve memory for Cipher/Auth key and IV */
27811df9c010SPablo de Lara 	reserve_key_memory(&options);
27821df9c010SPablo de Lara 
2783387259bdSDeclan Doherty 	/* parse application arguments (after the EAL ones) */
2784387259bdSDeclan Doherty 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
2785387259bdSDeclan Doherty 	if (ret < 0)
2786387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2787387259bdSDeclan Doherty 
2788acdfecbaSKuba Kozak 	printf("MAC updating %s\n",
2789acdfecbaSKuba Kozak 			options.mac_updating ? "enabled" : "disabled");
2790acdfecbaSKuba Kozak 
2791387259bdSDeclan Doherty 	/* create the mbuf pool */
2792c0f87eb5SDeclan Doherty 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2793ef156f4eSGagandeep Singh 			RTE_ALIGN(sizeof(struct rte_crypto_op),
2794ef156f4eSGagandeep Singh 				RTE_CACHE_LINE_SIZE),
2795c0f87eb5SDeclan Doherty 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2796387259bdSDeclan Doherty 	if (l2fwd_pktmbuf_pool == NULL)
2797387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2798387259bdSDeclan Doherty 
2799387259bdSDeclan Doherty 	/* create crypto op pool */
2800c0f87eb5SDeclan Doherty 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2801e636243eSPablo de Lara 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2802c0f87eb5SDeclan Doherty 			rte_socket_id());
2803c0f87eb5SDeclan Doherty 	if (l2fwd_crypto_op_pool == NULL)
2804387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2805387259bdSDeclan Doherty 
2806387259bdSDeclan Doherty 	/* Enable Ethernet ports */
2807387259bdSDeclan Doherty 	enabled_portcount = initialize_ports(&options);
2808387259bdSDeclan Doherty 	if (enabled_portcount < 1)
2809387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2810387259bdSDeclan Doherty 
2811387259bdSDeclan Doherty 	/* Initialize the port/queue configuration of each logical core */
28128728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(portid) {
2813387259bdSDeclan Doherty 
2814387259bdSDeclan Doherty 		/* skip ports that are not enabled */
2815387259bdSDeclan Doherty 		if ((options.portmask & (1 << portid)) == 0)
2816387259bdSDeclan Doherty 			continue;
2817387259bdSDeclan Doherty 
2818387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2819387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2820387259bdSDeclan Doherty 				rx_lcore_id++;
2821387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2822387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2823387259bdSDeclan Doherty 							"Not enough cores\n");
2824387259bdSDeclan Doherty 			}
2825387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2826387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2827387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2828387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2829387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2830387259bdSDeclan Doherty 				rx_lcore_id++;
2831387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2832387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2833387259bdSDeclan Doherty 							"Not enough cores\n");
2834387259bdSDeclan Doherty 			}
2835387259bdSDeclan Doherty 		}
2836387259bdSDeclan Doherty 
2837387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2838387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2839387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2840387259bdSDeclan Doherty 
2841387259bdSDeclan Doherty 		qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2842387259bdSDeclan Doherty 		qconf->nb_rx_ports++;
2843387259bdSDeclan Doherty 
2844e2cdfbd0SZhiyong Yang 		printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2845387259bdSDeclan Doherty 	}
2846387259bdSDeclan Doherty 
2847387259bdSDeclan Doherty 	/* Enable Crypto devices */
284827cf2d1bSPablo de Lara 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
284927cf2d1bSPablo de Lara 			enabled_cdevs);
285027cf2d1bSPablo de Lara 	if (enabled_cdevcount < 0)
285127cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
285227cf2d1bSPablo de Lara 
285327cf2d1bSPablo de Lara 	if (enabled_cdevcount < enabled_portcount)
285427cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
285527cf2d1bSPablo de Lara 				"has to be more or equal to number of ports (%d)\n",
285627cf2d1bSPablo de Lara 				enabled_cdevcount, enabled_portcount);
2857387259bdSDeclan Doherty 
2858387259bdSDeclan Doherty 	nb_cryptodevs = rte_cryptodev_count();
285927cf2d1bSPablo de Lara 
286027cf2d1bSPablo de Lara 	/* Initialize the port/cryptodev configuration of each logical core */
2861387259bdSDeclan Doherty 	for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2862387259bdSDeclan Doherty 			cdev_id < nb_cryptodevs && enabled_cdevcount;
2863387259bdSDeclan Doherty 			cdev_id++) {
286427cf2d1bSPablo de Lara 		/* Crypto op not supported by crypto device */
286527cf2d1bSPablo de Lara 		if (!enabled_cdevs[cdev_id])
2866387259bdSDeclan Doherty 			continue;
2867387259bdSDeclan Doherty 
2868387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2869387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2870387259bdSDeclan Doherty 				rx_lcore_id++;
2871387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2872387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2873387259bdSDeclan Doherty 							"Not enough cores\n");
2874387259bdSDeclan Doherty 			}
2875387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2876387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2877387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2878387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2879387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2880387259bdSDeclan Doherty 				rx_lcore_id++;
2881387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2882387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2883387259bdSDeclan Doherty 							"Not enough cores\n");
2884387259bdSDeclan Doherty 			}
2885387259bdSDeclan Doherty 		}
2886387259bdSDeclan Doherty 
2887387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2888387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2889387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2890387259bdSDeclan Doherty 
2891387259bdSDeclan Doherty 		qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2892387259bdSDeclan Doherty 		qconf->nb_crypto_devs++;
2893387259bdSDeclan Doherty 
2894387259bdSDeclan Doherty 		enabled_cdevcount--;
2895387259bdSDeclan Doherty 
2896387259bdSDeclan Doherty 		printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2897387259bdSDeclan Doherty 				(unsigned)cdev_id);
2898387259bdSDeclan Doherty 	}
2899387259bdSDeclan Doherty 
2900387259bdSDeclan Doherty 	/* launch per-lcore init on every lcore */
2901387259bdSDeclan Doherty 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2902cb056611SStephen Hemminger 			CALL_MAIN);
2903cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
2904387259bdSDeclan Doherty 		if (rte_eal_wait_lcore(lcore_id) < 0)
2905387259bdSDeclan Doherty 			return -1;
2906387259bdSDeclan Doherty 	}
2907387259bdSDeclan Doherty 
290810aa3757SChengchang Tang 	/* clean up the EAL */
290910aa3757SChengchang Tang 	rte_eal_cleanup();
291010aa3757SChengchang Tang 
2911387259bdSDeclan Doherty 	return 0;
2912387259bdSDeclan Doherty }
2913