xref: /dpdk/examples/l2fwd-crypto/main.c (revision 2c59bd32b70d333f2ae89ee6b34c232a8cb6608c)
1387259bdSDeclan Doherty /*-
2387259bdSDeclan Doherty  *   BSD LICENSE
3387259bdSDeclan Doherty  *
4*2c59bd32SSlawomir Mrozowicz  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5387259bdSDeclan Doherty  *   All rights reserved.
6387259bdSDeclan Doherty  *
7387259bdSDeclan Doherty  *   Redistribution and use in source and binary forms, with or without
8387259bdSDeclan Doherty  *   modification, are permitted provided that the following conditions
9387259bdSDeclan Doherty  *   are met:
10387259bdSDeclan Doherty  *
11387259bdSDeclan Doherty  *     * Redistributions of source code must retain the above copyright
12387259bdSDeclan Doherty  *       notice, this list of conditions and the following disclaimer.
13387259bdSDeclan Doherty  *     * Redistributions in binary form must reproduce the above copyright
14387259bdSDeclan Doherty  *       notice, this list of conditions and the following disclaimer in
15387259bdSDeclan Doherty  *       the documentation and/or other materials provided with the
16387259bdSDeclan Doherty  *       distribution.
17387259bdSDeclan Doherty  *     * Neither the name of Intel Corporation nor the names of its
18387259bdSDeclan Doherty  *       contributors may be used to endorse or promote products derived
19387259bdSDeclan Doherty  *       from this software without specific prior written permission.
20387259bdSDeclan Doherty  *
21387259bdSDeclan Doherty  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22387259bdSDeclan Doherty  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23387259bdSDeclan Doherty  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24387259bdSDeclan Doherty  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25387259bdSDeclan Doherty  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26387259bdSDeclan Doherty  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27387259bdSDeclan Doherty  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28387259bdSDeclan Doherty  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29387259bdSDeclan Doherty  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30387259bdSDeclan Doherty  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31387259bdSDeclan Doherty  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32387259bdSDeclan Doherty  */
33387259bdSDeclan Doherty 
34387259bdSDeclan Doherty #include <time.h>
35387259bdSDeclan Doherty #include <stdio.h>
36387259bdSDeclan Doherty #include <stdlib.h>
37387259bdSDeclan Doherty #include <string.h>
38387259bdSDeclan Doherty #include <stdint.h>
39387259bdSDeclan Doherty #include <inttypes.h>
40387259bdSDeclan Doherty #include <sys/types.h>
41387259bdSDeclan Doherty #include <sys/queue.h>
42387259bdSDeclan Doherty #include <netinet/in.h>
43387259bdSDeclan Doherty #include <setjmp.h>
44387259bdSDeclan Doherty #include <stdarg.h>
45387259bdSDeclan Doherty #include <ctype.h>
46387259bdSDeclan Doherty #include <errno.h>
47387259bdSDeclan Doherty #include <getopt.h>
486dad6e69SPiotr Azarewicz #include <fcntl.h>
496dad6e69SPiotr Azarewicz #include <unistd.h>
50387259bdSDeclan Doherty 
51387259bdSDeclan Doherty #include <rte_atomic.h>
52387259bdSDeclan Doherty #include <rte_branch_prediction.h>
53387259bdSDeclan Doherty #include <rte_common.h>
54387259bdSDeclan Doherty #include <rte_cryptodev.h>
55387259bdSDeclan Doherty #include <rte_cycles.h>
56387259bdSDeclan Doherty #include <rte_debug.h>
57387259bdSDeclan Doherty #include <rte_eal.h>
58387259bdSDeclan Doherty #include <rte_ether.h>
59387259bdSDeclan Doherty #include <rte_ethdev.h>
60387259bdSDeclan Doherty #include <rte_interrupts.h>
61387259bdSDeclan Doherty #include <rte_ip.h>
62387259bdSDeclan Doherty #include <rte_launch.h>
63387259bdSDeclan Doherty #include <rte_lcore.h>
64387259bdSDeclan Doherty #include <rte_log.h>
65387259bdSDeclan Doherty #include <rte_malloc.h>
66387259bdSDeclan Doherty #include <rte_mbuf.h>
67387259bdSDeclan Doherty #include <rte_memcpy.h>
68387259bdSDeclan Doherty #include <rte_memory.h>
69387259bdSDeclan Doherty #include <rte_mempool.h>
70387259bdSDeclan Doherty #include <rte_memzone.h>
71387259bdSDeclan Doherty #include <rte_pci.h>
72387259bdSDeclan Doherty #include <rte_per_lcore.h>
73387259bdSDeclan Doherty #include <rte_prefetch.h>
74387259bdSDeclan Doherty #include <rte_random.h>
7541e97c2eSPablo de Lara #include <rte_hexdump.h>
76387259bdSDeclan Doherty 
7727cf2d1bSPablo de Lara enum cdev_type {
7827cf2d1bSPablo de Lara 	CDEV_TYPE_ANY,
7927cf2d1bSPablo de Lara 	CDEV_TYPE_HW,
8027cf2d1bSPablo de Lara 	CDEV_TYPE_SW
8127cf2d1bSPablo de Lara };
8227cf2d1bSPablo de Lara 
83387259bdSDeclan Doherty #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
84387259bdSDeclan Doherty 
85387259bdSDeclan Doherty #define NB_MBUF   8192
86387259bdSDeclan Doherty 
8727cf2d1bSPablo de Lara #define MAX_STR_LEN 32
881df9c010SPablo de Lara #define MAX_KEY_SIZE 128
89387259bdSDeclan Doherty #define MAX_PKT_BURST 32
90387259bdSDeclan Doherty #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91*2c59bd32SSlawomir Mrozowicz #define MAX_SESSIONS 32
92*2c59bd32SSlawomir Mrozowicz #define SESSION_POOL_CACHE_SIZE 0
93387259bdSDeclan Doherty 
94e636243eSPablo de Lara #define MAXIMUM_IV_LENGTH	16
95e636243eSPablo de Lara #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
96e636243eSPablo de Lara 				sizeof(struct rte_crypto_sym_op))
97e636243eSPablo de Lara 
98387259bdSDeclan Doherty /*
99387259bdSDeclan Doherty  * Configurable number of RX/TX ring descriptors
100387259bdSDeclan Doherty  */
101387259bdSDeclan Doherty #define RTE_TEST_RX_DESC_DEFAULT 128
102387259bdSDeclan Doherty #define RTE_TEST_TX_DESC_DEFAULT 512
1033c96262cSPablo de Lara 
104387259bdSDeclan Doherty static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
105387259bdSDeclan Doherty static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
106387259bdSDeclan Doherty 
107387259bdSDeclan Doherty /* ethernet addresses of ports */
108387259bdSDeclan Doherty static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
109387259bdSDeclan Doherty 
110387259bdSDeclan Doherty /* mask of enabled ports */
111387259bdSDeclan Doherty static uint64_t l2fwd_enabled_port_mask;
112387259bdSDeclan Doherty static uint64_t l2fwd_enabled_crypto_mask;
113387259bdSDeclan Doherty 
114387259bdSDeclan Doherty /* list of enabled ports */
115387259bdSDeclan Doherty static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
116387259bdSDeclan Doherty 
117387259bdSDeclan Doherty 
118387259bdSDeclan Doherty struct pkt_buffer {
119387259bdSDeclan Doherty 	unsigned len;
120387259bdSDeclan Doherty 	struct rte_mbuf *buffer[MAX_PKT_BURST];
121387259bdSDeclan Doherty };
122387259bdSDeclan Doherty 
123c0f87eb5SDeclan Doherty struct op_buffer {
124c0f87eb5SDeclan Doherty 	unsigned len;
125c0f87eb5SDeclan Doherty 	struct rte_crypto_op *buffer[MAX_PKT_BURST];
126c0f87eb5SDeclan Doherty };
127c0f87eb5SDeclan Doherty 
128387259bdSDeclan Doherty #define MAX_RX_QUEUE_PER_LCORE 16
129387259bdSDeclan Doherty #define MAX_TX_QUEUE_PER_PORT 16
130387259bdSDeclan Doherty 
131387259bdSDeclan Doherty enum l2fwd_crypto_xform_chain {
132387259bdSDeclan Doherty 	L2FWD_CRYPTO_CIPHER_HASH,
1331a75e9f3SPablo de Lara 	L2FWD_CRYPTO_HASH_CIPHER,
1341a75e9f3SPablo de Lara 	L2FWD_CRYPTO_CIPHER_ONLY,
1352661f4fbSPablo de Lara 	L2FWD_CRYPTO_HASH_ONLY,
1362661f4fbSPablo de Lara 	L2FWD_CRYPTO_AEAD
137387259bdSDeclan Doherty };
138387259bdSDeclan Doherty 
139a7f4562bSFiona Trahe struct l2fwd_key {
140a7f4562bSFiona Trahe 	uint8_t *data;
141a7f4562bSFiona Trahe 	uint32_t length;
142a7f4562bSFiona Trahe 	phys_addr_t phys_addr;
143a7f4562bSFiona Trahe };
144a7f4562bSFiona Trahe 
1450fbd75a9SPablo de Lara struct l2fwd_iv {
1460fbd75a9SPablo de Lara 	uint8_t *data;
1470fbd75a9SPablo de Lara 	uint16_t length;
1480fbd75a9SPablo de Lara };
1490fbd75a9SPablo de Lara 
150387259bdSDeclan Doherty /** l2fwd crypto application command line options */
151387259bdSDeclan Doherty struct l2fwd_crypto_options {
152387259bdSDeclan Doherty 	unsigned portmask;
153387259bdSDeclan Doherty 	unsigned nb_ports_per_lcore;
154387259bdSDeclan Doherty 	unsigned refresh_period;
155387259bdSDeclan Doherty 	unsigned single_lcore:1;
156387259bdSDeclan Doherty 
15727cf2d1bSPablo de Lara 	enum cdev_type type;
158387259bdSDeclan Doherty 	unsigned sessionless:1;
159387259bdSDeclan Doherty 
160387259bdSDeclan Doherty 	enum l2fwd_crypto_xform_chain xform_chain;
161387259bdSDeclan Doherty 
1621bd407faSFiona Trahe 	struct rte_crypto_sym_xform cipher_xform;
1631df9c010SPablo de Lara 	unsigned ckey_param;
164a061e50aSPablo de Lara 	int ckey_random_size;
165387259bdSDeclan Doherty 
166acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
167acf86169SPablo de Lara 	unsigned int cipher_iv_param;
168acf86169SPablo de Lara 	int cipher_iv_random_size;
169387259bdSDeclan Doherty 
1701bd407faSFiona Trahe 	struct rte_crypto_sym_xform auth_xform;
1711df9c010SPablo de Lara 	uint8_t akey_param;
172a061e50aSPablo de Lara 	int akey_random_size;
173617a7949SPablo de Lara 
174acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
175acf86169SPablo de Lara 	unsigned int auth_iv_param;
176acf86169SPablo de Lara 	int auth_iv_random_size;
177acf86169SPablo de Lara 
1782661f4fbSPablo de Lara 	struct rte_crypto_sym_xform aead_xform;
1792661f4fbSPablo de Lara 	unsigned int aead_key_param;
1802661f4fbSPablo de Lara 	int aead_key_random_size;
1812661f4fbSPablo de Lara 
1822661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
1832661f4fbSPablo de Lara 	unsigned int aead_iv_param;
1842661f4fbSPablo de Lara 	int aead_iv_random_size;
1852661f4fbSPablo de Lara 
186617a7949SPablo de Lara 	struct l2fwd_key aad;
187617a7949SPablo de Lara 	unsigned aad_param;
188a061e50aSPablo de Lara 	int aad_random_size;
189a061e50aSPablo de Lara 
190a061e50aSPablo de Lara 	int digest_size;
19127cf2d1bSPablo de Lara 
19227cf2d1bSPablo de Lara 	uint16_t block_size;
19327cf2d1bSPablo de Lara 	char string_type[MAX_STR_LEN];
194d2797f51SFan Zhang 
195d2797f51SFan Zhang 	uint64_t cryptodev_mask;
196387259bdSDeclan Doherty };
197387259bdSDeclan Doherty 
198387259bdSDeclan Doherty /** l2fwd crypto lcore params */
199387259bdSDeclan Doherty struct l2fwd_crypto_params {
200387259bdSDeclan Doherty 	uint8_t dev_id;
201387259bdSDeclan Doherty 	uint8_t qp_id;
202387259bdSDeclan Doherty 
203387259bdSDeclan Doherty 	unsigned digest_length;
204387259bdSDeclan Doherty 	unsigned block_size;
20527cf2d1bSPablo de Lara 
206acf86169SPablo de Lara 	struct l2fwd_iv cipher_iv;
207acf86169SPablo de Lara 	struct l2fwd_iv auth_iv;
2082661f4fbSPablo de Lara 	struct l2fwd_iv aead_iv;
209617a7949SPablo de Lara 	struct l2fwd_key aad;
2101bd407faSFiona Trahe 	struct rte_cryptodev_sym_session *session;
2111a75e9f3SPablo de Lara 
2121a75e9f3SPablo de Lara 	uint8_t do_cipher;
2131a75e9f3SPablo de Lara 	uint8_t do_hash;
2142661f4fbSPablo de Lara 	uint8_t do_aead;
21527cf2d1bSPablo de Lara 	uint8_t hash_verify;
216d29ea843SPablo de Lara 
217d29ea843SPablo de Lara 	enum rte_crypto_cipher_algorithm cipher_algo;
218d29ea843SPablo de Lara 	enum rte_crypto_auth_algorithm auth_algo;
2192661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm aead_algo;
220387259bdSDeclan Doherty };
221387259bdSDeclan Doherty 
222387259bdSDeclan Doherty /** lcore configuration */
223387259bdSDeclan Doherty struct lcore_queue_conf {
224387259bdSDeclan Doherty 	unsigned nb_rx_ports;
225387259bdSDeclan Doherty 	unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
226387259bdSDeclan Doherty 
227387259bdSDeclan Doherty 	unsigned nb_crypto_devs;
228387259bdSDeclan Doherty 	unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
229387259bdSDeclan Doherty 
230ad476dd3SPablo de Lara 	struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
231c0f87eb5SDeclan Doherty 	struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
232387259bdSDeclan Doherty } __rte_cache_aligned;
233387259bdSDeclan Doherty 
234387259bdSDeclan Doherty struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
235387259bdSDeclan Doherty 
236387259bdSDeclan Doherty static const struct rte_eth_conf port_conf = {
237387259bdSDeclan Doherty 	.rxmode = {
2381df9c010SPablo de Lara 		.mq_mode = ETH_MQ_RX_NONE,
2391df9c010SPablo de Lara 		.max_rx_pkt_len = ETHER_MAX_LEN,
240387259bdSDeclan Doherty 		.split_hdr_size = 0,
241387259bdSDeclan Doherty 		.header_split   = 0, /**< Header Split disabled */
242387259bdSDeclan Doherty 		.hw_ip_checksum = 0, /**< IP checksum offload disabled */
243387259bdSDeclan Doherty 		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
244387259bdSDeclan Doherty 		.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
24560da774eSJeff Guo 		.hw_strip_crc   = 1, /**< CRC stripped by hardware */
246387259bdSDeclan Doherty 	},
247387259bdSDeclan Doherty 	.txmode = {
248387259bdSDeclan Doherty 		.mq_mode = ETH_MQ_TX_NONE,
249387259bdSDeclan Doherty 	},
250387259bdSDeclan Doherty };
251387259bdSDeclan Doherty 
252387259bdSDeclan Doherty struct rte_mempool *l2fwd_pktmbuf_pool;
253c0f87eb5SDeclan Doherty struct rte_mempool *l2fwd_crypto_op_pool;
254*2c59bd32SSlawomir Mrozowicz struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
255387259bdSDeclan Doherty 
256387259bdSDeclan Doherty /* Per-port statistics struct */
257387259bdSDeclan Doherty struct l2fwd_port_statistics {
258387259bdSDeclan Doherty 	uint64_t tx;
259387259bdSDeclan Doherty 	uint64_t rx;
260387259bdSDeclan Doherty 
261387259bdSDeclan Doherty 	uint64_t crypto_enqueued;
262387259bdSDeclan Doherty 	uint64_t crypto_dequeued;
263387259bdSDeclan Doherty 
264387259bdSDeclan Doherty 	uint64_t dropped;
265387259bdSDeclan Doherty } __rte_cache_aligned;
266387259bdSDeclan Doherty 
267387259bdSDeclan Doherty struct l2fwd_crypto_statistics {
268387259bdSDeclan Doherty 	uint64_t enqueued;
269387259bdSDeclan Doherty 	uint64_t dequeued;
270387259bdSDeclan Doherty 
271387259bdSDeclan Doherty 	uint64_t errors;
272387259bdSDeclan Doherty } __rte_cache_aligned;
273387259bdSDeclan Doherty 
274387259bdSDeclan Doherty struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
275572f0779SSlawomir Mrozowicz struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
276387259bdSDeclan Doherty 
277387259bdSDeclan Doherty /* A tsc-based timer responsible for triggering statistics printout */
278387259bdSDeclan Doherty #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
2793c96262cSPablo de Lara #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
280387259bdSDeclan Doherty 
281387259bdSDeclan Doherty /* default period is 10 seconds */
282387259bdSDeclan Doherty static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
283387259bdSDeclan Doherty 
284387259bdSDeclan Doherty /* Print out statistics on packets dropped */
285387259bdSDeclan Doherty static void
286387259bdSDeclan Doherty print_stats(void)
287387259bdSDeclan Doherty {
28828523d9aSPablo de Lara 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
28928523d9aSPablo de Lara 	uint64_t total_packets_enqueued, total_packets_dequeued,
29028523d9aSPablo de Lara 		total_packets_errors;
291387259bdSDeclan Doherty 	unsigned portid;
292387259bdSDeclan Doherty 	uint64_t cdevid;
293387259bdSDeclan Doherty 
29428523d9aSPablo de Lara 	total_packets_dropped = 0;
29528523d9aSPablo de Lara 	total_packets_tx = 0;
29628523d9aSPablo de Lara 	total_packets_rx = 0;
29728523d9aSPablo de Lara 	total_packets_enqueued = 0;
29828523d9aSPablo de Lara 	total_packets_dequeued = 0;
29928523d9aSPablo de Lara 	total_packets_errors = 0;
300387259bdSDeclan Doherty 
301387259bdSDeclan Doherty 	const char clr[] = { 27, '[', '2', 'J', '\0' };
302387259bdSDeclan Doherty 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
303387259bdSDeclan Doherty 
304387259bdSDeclan Doherty 		/* Clear screen and move to top left */
305387259bdSDeclan Doherty 	printf("%s%s", clr, topLeft);
306387259bdSDeclan Doherty 
307387259bdSDeclan Doherty 	printf("\nPort statistics ====================================");
308387259bdSDeclan Doherty 
309387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
310387259bdSDeclan Doherty 		/* skip disabled ports */
311387259bdSDeclan Doherty 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
312387259bdSDeclan Doherty 			continue;
313387259bdSDeclan Doherty 		printf("\nStatistics for port %u ------------------------------"
314387259bdSDeclan Doherty 			   "\nPackets sent: %32"PRIu64
315387259bdSDeclan Doherty 			   "\nPackets received: %28"PRIu64
316387259bdSDeclan Doherty 			   "\nPackets dropped: %29"PRIu64,
317387259bdSDeclan Doherty 			   portid,
318387259bdSDeclan Doherty 			   port_statistics[portid].tx,
319387259bdSDeclan Doherty 			   port_statistics[portid].rx,
320387259bdSDeclan Doherty 			   port_statistics[portid].dropped);
321387259bdSDeclan Doherty 
322387259bdSDeclan Doherty 		total_packets_dropped += port_statistics[portid].dropped;
323387259bdSDeclan Doherty 		total_packets_tx += port_statistics[portid].tx;
324387259bdSDeclan Doherty 		total_packets_rx += port_statistics[portid].rx;
325387259bdSDeclan Doherty 	}
326387259bdSDeclan Doherty 	printf("\nCrypto statistics ==================================");
327387259bdSDeclan Doherty 
328387259bdSDeclan Doherty 	for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
329387259bdSDeclan Doherty 		/* skip disabled ports */
330ad476dd3SPablo de Lara 		if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
331387259bdSDeclan Doherty 			continue;
332387259bdSDeclan Doherty 		printf("\nStatistics for cryptodev %"PRIu64
333387259bdSDeclan Doherty 				" -------------------------"
334387259bdSDeclan Doherty 			   "\nPackets enqueued: %28"PRIu64
335387259bdSDeclan Doherty 			   "\nPackets dequeued: %28"PRIu64
336387259bdSDeclan Doherty 			   "\nPackets errors: %30"PRIu64,
337387259bdSDeclan Doherty 			   cdevid,
338387259bdSDeclan Doherty 			   crypto_statistics[cdevid].enqueued,
339387259bdSDeclan Doherty 			   crypto_statistics[cdevid].dequeued,
340387259bdSDeclan Doherty 			   crypto_statistics[cdevid].errors);
341387259bdSDeclan Doherty 
342387259bdSDeclan Doherty 		total_packets_enqueued += crypto_statistics[cdevid].enqueued;
343387259bdSDeclan Doherty 		total_packets_dequeued += crypto_statistics[cdevid].dequeued;
344387259bdSDeclan Doherty 		total_packets_errors += crypto_statistics[cdevid].errors;
345387259bdSDeclan Doherty 	}
346387259bdSDeclan Doherty 	printf("\nAggregate statistics ==============================="
347387259bdSDeclan Doherty 		   "\nTotal packets received: %22"PRIu64
348387259bdSDeclan Doherty 		   "\nTotal packets enqueued: %22"PRIu64
349387259bdSDeclan Doherty 		   "\nTotal packets dequeued: %22"PRIu64
350387259bdSDeclan Doherty 		   "\nTotal packets sent: %26"PRIu64
351387259bdSDeclan Doherty 		   "\nTotal packets dropped: %23"PRIu64
352387259bdSDeclan Doherty 		   "\nTotal packets crypto errors: %17"PRIu64,
353387259bdSDeclan Doherty 		   total_packets_rx,
354387259bdSDeclan Doherty 		   total_packets_enqueued,
355387259bdSDeclan Doherty 		   total_packets_dequeued,
356387259bdSDeclan Doherty 		   total_packets_tx,
357387259bdSDeclan Doherty 		   total_packets_dropped,
358387259bdSDeclan Doherty 		   total_packets_errors);
359387259bdSDeclan Doherty 	printf("\n====================================================\n");
360387259bdSDeclan Doherty }
361387259bdSDeclan Doherty 
362387259bdSDeclan Doherty static int
363387259bdSDeclan Doherty l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
364387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
365387259bdSDeclan Doherty {
366c0f87eb5SDeclan Doherty 	struct rte_crypto_op **op_buffer;
367387259bdSDeclan Doherty 	unsigned ret;
368387259bdSDeclan Doherty 
369c0f87eb5SDeclan Doherty 	op_buffer = (struct rte_crypto_op **)
370c0f87eb5SDeclan Doherty 			qconf->op_buf[cparams->dev_id].buffer;
371387259bdSDeclan Doherty 
372c0f87eb5SDeclan Doherty 	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
373c0f87eb5SDeclan Doherty 			cparams->qp_id,	op_buffer, (uint16_t) n);
374c0f87eb5SDeclan Doherty 
375387259bdSDeclan Doherty 	crypto_statistics[cparams->dev_id].enqueued += ret;
376387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
377387259bdSDeclan Doherty 		crypto_statistics[cparams->dev_id].errors += (n - ret);
378387259bdSDeclan Doherty 		do {
379c0f87eb5SDeclan Doherty 			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
380c0f87eb5SDeclan Doherty 			rte_crypto_op_free(op_buffer[ret]);
381387259bdSDeclan Doherty 		} while (++ret < n);
382387259bdSDeclan Doherty 	}
383387259bdSDeclan Doherty 
384387259bdSDeclan Doherty 	return 0;
385387259bdSDeclan Doherty }
386387259bdSDeclan Doherty 
387387259bdSDeclan Doherty static int
388c0f87eb5SDeclan Doherty l2fwd_crypto_enqueue(struct rte_crypto_op *op,
389c0f87eb5SDeclan Doherty 		struct l2fwd_crypto_params *cparams)
390387259bdSDeclan Doherty {
391387259bdSDeclan Doherty 	unsigned lcore_id, len;
392387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
393387259bdSDeclan Doherty 
394387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
395387259bdSDeclan Doherty 
396387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
397c0f87eb5SDeclan Doherty 	len = qconf->op_buf[cparams->dev_id].len;
398c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].buffer[len] = op;
399387259bdSDeclan Doherty 	len++;
400387259bdSDeclan Doherty 
401c0f87eb5SDeclan Doherty 	/* enough ops to be sent */
402387259bdSDeclan Doherty 	if (len == MAX_PKT_BURST) {
403387259bdSDeclan Doherty 		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
404387259bdSDeclan Doherty 		len = 0;
405387259bdSDeclan Doherty 	}
406387259bdSDeclan Doherty 
407c0f87eb5SDeclan Doherty 	qconf->op_buf[cparams->dev_id].len = len;
408387259bdSDeclan Doherty 	return 0;
409387259bdSDeclan Doherty }
410387259bdSDeclan Doherty 
411387259bdSDeclan Doherty static int
412387259bdSDeclan Doherty l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
413c0f87eb5SDeclan Doherty 		struct rte_crypto_op *op,
414387259bdSDeclan Doherty 		struct l2fwd_crypto_params *cparams)
415387259bdSDeclan Doherty {
416387259bdSDeclan Doherty 	struct ether_hdr *eth_hdr;
417387259bdSDeclan Doherty 	struct ipv4_hdr *ip_hdr;
418387259bdSDeclan Doherty 
4195839fd20SPablo de Lara 	uint32_t ipdata_offset, data_len;
4205839fd20SPablo de Lara 	uint32_t pad_len = 0;
421387259bdSDeclan Doherty 	char *padding;
422387259bdSDeclan Doherty 
423387259bdSDeclan Doherty 	eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
424387259bdSDeclan Doherty 
425387259bdSDeclan Doherty 	if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
426387259bdSDeclan Doherty 		return -1;
427387259bdSDeclan Doherty 
428387259bdSDeclan Doherty 	ipdata_offset = sizeof(struct ether_hdr);
429387259bdSDeclan Doherty 
430387259bdSDeclan Doherty 	ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
431387259bdSDeclan Doherty 			ipdata_offset);
432387259bdSDeclan Doherty 
433387259bdSDeclan Doherty 	ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
434387259bdSDeclan Doherty 			* IPV4_IHL_MULTIPLIER;
435387259bdSDeclan Doherty 
436387259bdSDeclan Doherty 
437387259bdSDeclan Doherty 	/* Zero pad data to be crypto'd so it is block aligned */
438387259bdSDeclan Doherty 	data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
439893fbab0SPiotr Azarewicz 
440893fbab0SPiotr Azarewicz 	if (cparams->do_hash && cparams->hash_verify)
441893fbab0SPiotr Azarewicz 		data_len -= cparams->digest_length;
442893fbab0SPiotr Azarewicz 
4435839fd20SPablo de Lara 	if (cparams->do_cipher) {
4445839fd20SPablo de Lara 		/*
4455839fd20SPablo de Lara 		 * Following algorithms are block cipher algorithms,
4465839fd20SPablo de Lara 		 * and might need padding
4475839fd20SPablo de Lara 		 */
4485839fd20SPablo de Lara 		switch (cparams->cipher_algo) {
4495839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_CBC:
4505839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_AES_ECB:
4515839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_DES_CBC:
4525839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_CBC:
4535839fd20SPablo de Lara 		case RTE_CRYPTO_CIPHER_3DES_ECB:
4545839fd20SPablo de Lara 			if (data_len % cparams->block_size)
4555839fd20SPablo de Lara 				pad_len = cparams->block_size -
4565839fd20SPablo de Lara 					(data_len % cparams->block_size);
4575839fd20SPablo de Lara 			break;
4585839fd20SPablo de Lara 		default:
4595839fd20SPablo de Lara 			pad_len = 0;
4605839fd20SPablo de Lara 		}
461387259bdSDeclan Doherty 
462387259bdSDeclan Doherty 		if (pad_len) {
463387259bdSDeclan Doherty 			padding = rte_pktmbuf_append(m, pad_len);
464387259bdSDeclan Doherty 			if (unlikely(!padding))
465387259bdSDeclan Doherty 				return -1;
466387259bdSDeclan Doherty 
467387259bdSDeclan Doherty 			data_len += pad_len;
468387259bdSDeclan Doherty 			memset(padding, 0, pad_len);
469387259bdSDeclan Doherty 		}
4705839fd20SPablo de Lara 	}
471387259bdSDeclan Doherty 
472387259bdSDeclan Doherty 	/* Set crypto operation data parameters */
473c0f87eb5SDeclan Doherty 	rte_crypto_op_attach_sym_session(op, cparams->session);
474387259bdSDeclan Doherty 
4751a75e9f3SPablo de Lara 	if (cparams->do_hash) {
476acf86169SPablo de Lara 		if (cparams->auth_iv.length) {
477acf86169SPablo de Lara 			uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
478acf86169SPablo de Lara 						uint8_t *,
479acf86169SPablo de Lara 						IV_OFFSET +
480acf86169SPablo de Lara 						cparams->cipher_iv.length);
481acf86169SPablo de Lara 			/*
482acf86169SPablo de Lara 			 * Copy IV at the end of the crypto operation,
483acf86169SPablo de Lara 			 * after the cipher IV, if added
484acf86169SPablo de Lara 			 */
485acf86169SPablo de Lara 			rte_memcpy(iv_ptr, cparams->auth_iv.data,
486acf86169SPablo de Lara 					cparams->auth_iv.length);
487acf86169SPablo de Lara 		}
48827cf2d1bSPablo de Lara 		if (!cparams->hash_verify) {
489387259bdSDeclan Doherty 			/* Append space for digest to end of packet */
490c0f87eb5SDeclan Doherty 			op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
491387259bdSDeclan Doherty 				cparams->digest_length);
49227cf2d1bSPablo de Lara 		} else {
493893fbab0SPiotr Azarewicz 			op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
494893fbab0SPiotr Azarewicz 				uint8_t *) + ipdata_offset + data_len;
49527cf2d1bSPablo de Lara 		}
49627cf2d1bSPablo de Lara 
497c0f87eb5SDeclan Doherty 		op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
498387259bdSDeclan Doherty 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
499387259bdSDeclan Doherty 
5001f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
5012773c86dSPablo de Lara 		if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
5021f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
5031f393d82SPablo de Lara 				cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
504d29ea843SPablo de Lara 			op->sym->auth.data.offset = ipdata_offset << 3;
505d29ea843SPablo de Lara 			op->sym->auth.data.length = data_len << 3;
506d29ea843SPablo de Lara 		} else {
507c0f87eb5SDeclan Doherty 			op->sym->auth.data.offset = ipdata_offset;
508c0f87eb5SDeclan Doherty 			op->sym->auth.data.length = data_len;
509d29ea843SPablo de Lara 		}
5101a75e9f3SPablo de Lara 	}
5111a75e9f3SPablo de Lara 
5121a75e9f3SPablo de Lara 	if (cparams->do_cipher) {
513e636243eSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
514e636243eSPablo de Lara 							IV_OFFSET);
515e636243eSPablo de Lara 		/* Copy IV at the end of the crypto operation */
516acf86169SPablo de Lara 		rte_memcpy(iv_ptr, cparams->cipher_iv.data,
517acf86169SPablo de Lara 				cparams->cipher_iv.length);
518e636243eSPablo de Lara 
5191f393d82SPablo de Lara 		/* For wireless algorithms, offset/length must be in bits */
5202773c86dSPablo de Lara 		if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
5211f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
5221f393d82SPablo de Lara 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
523d29ea843SPablo de Lara 			op->sym->cipher.data.offset = ipdata_offset << 3;
524d29ea843SPablo de Lara 			op->sym->cipher.data.length = data_len << 3;
525d29ea843SPablo de Lara 		} else {
526c0f87eb5SDeclan Doherty 			op->sym->cipher.data.offset = ipdata_offset;
527c0f87eb5SDeclan Doherty 			op->sym->cipher.data.length = data_len;
5281a75e9f3SPablo de Lara 		}
529d29ea843SPablo de Lara 	}
530387259bdSDeclan Doherty 
5312661f4fbSPablo de Lara 	if (cparams->do_aead) {
5322661f4fbSPablo de Lara 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
5332661f4fbSPablo de Lara 							IV_OFFSET);
5342661f4fbSPablo de Lara 		/* Copy IV at the end of the crypto operation */
5352661f4fbSPablo de Lara 		rte_memcpy(iv_ptr, cparams->aead_iv.data, cparams->aead_iv.length);
5362661f4fbSPablo de Lara 
5372661f4fbSPablo de Lara 		op->sym->aead.data.offset = ipdata_offset;
5382661f4fbSPablo de Lara 		op->sym->aead.data.length = data_len;
5392661f4fbSPablo de Lara 
5402661f4fbSPablo de Lara 		if (!cparams->hash_verify) {
5412661f4fbSPablo de Lara 			/* Append space for digest to end of packet */
5422661f4fbSPablo de Lara 			op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
5432661f4fbSPablo de Lara 				cparams->digest_length);
5442661f4fbSPablo de Lara 		} else {
5452661f4fbSPablo de Lara 			op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
5462661f4fbSPablo de Lara 				uint8_t *) + ipdata_offset + data_len;
5472661f4fbSPablo de Lara 		}
5482661f4fbSPablo de Lara 
5492661f4fbSPablo de Lara 		op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
5502661f4fbSPablo de Lara 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
5512661f4fbSPablo de Lara 
5522661f4fbSPablo de Lara 		if (cparams->aad.length) {
5532661f4fbSPablo de Lara 			op->sym->aead.aad.data = cparams->aad.data;
5542661f4fbSPablo de Lara 			op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
5552661f4fbSPablo de Lara 		}
5562661f4fbSPablo de Lara 	}
5572661f4fbSPablo de Lara 
558c0f87eb5SDeclan Doherty 	op->sym->m_src = m;
559c0f87eb5SDeclan Doherty 
560c0f87eb5SDeclan Doherty 	return l2fwd_crypto_enqueue(op, cparams);
561387259bdSDeclan Doherty }
562387259bdSDeclan Doherty 
563387259bdSDeclan Doherty 
564387259bdSDeclan Doherty /* Send the burst of packets on an output interface */
565387259bdSDeclan Doherty static int
566c0f87eb5SDeclan Doherty l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
567c0f87eb5SDeclan Doherty 		uint8_t port)
568387259bdSDeclan Doherty {
569387259bdSDeclan Doherty 	struct rte_mbuf **pkt_buffer;
570387259bdSDeclan Doherty 	unsigned ret;
571387259bdSDeclan Doherty 
572c0f87eb5SDeclan Doherty 	pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
573387259bdSDeclan Doherty 
574c0f87eb5SDeclan Doherty 	ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
575387259bdSDeclan Doherty 	port_statistics[port].tx += ret;
576387259bdSDeclan Doherty 	if (unlikely(ret < n)) {
577387259bdSDeclan Doherty 		port_statistics[port].dropped += (n - ret);
578387259bdSDeclan Doherty 		do {
579387259bdSDeclan Doherty 			rte_pktmbuf_free(pkt_buffer[ret]);
580387259bdSDeclan Doherty 		} while (++ret < n);
581387259bdSDeclan Doherty 	}
582387259bdSDeclan Doherty 
583387259bdSDeclan Doherty 	return 0;
584387259bdSDeclan Doherty }
585387259bdSDeclan Doherty 
586387259bdSDeclan Doherty /* Enqueue packets for TX and prepare them to be sent */
587387259bdSDeclan Doherty static int
588387259bdSDeclan Doherty l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
589387259bdSDeclan Doherty {
590387259bdSDeclan Doherty 	unsigned lcore_id, len;
591387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
592387259bdSDeclan Doherty 
593387259bdSDeclan Doherty 	lcore_id = rte_lcore_id();
594387259bdSDeclan Doherty 
595387259bdSDeclan Doherty 	qconf = &lcore_queue_conf[lcore_id];
596c0f87eb5SDeclan Doherty 	len = qconf->pkt_buf[port].len;
597c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].buffer[len] = m;
598387259bdSDeclan Doherty 	len++;
599387259bdSDeclan Doherty 
600387259bdSDeclan Doherty 	/* enough pkts to be sent */
601387259bdSDeclan Doherty 	if (unlikely(len == MAX_PKT_BURST)) {
602387259bdSDeclan Doherty 		l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
603387259bdSDeclan Doherty 		len = 0;
604387259bdSDeclan Doherty 	}
605387259bdSDeclan Doherty 
606c0f87eb5SDeclan Doherty 	qconf->pkt_buf[port].len = len;
607387259bdSDeclan Doherty 	return 0;
608387259bdSDeclan Doherty }
609387259bdSDeclan Doherty 
610387259bdSDeclan Doherty static void
611387259bdSDeclan Doherty l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
612387259bdSDeclan Doherty {
613387259bdSDeclan Doherty 	struct ether_hdr *eth;
614387259bdSDeclan Doherty 	void *tmp;
615387259bdSDeclan Doherty 	unsigned dst_port;
616387259bdSDeclan Doherty 
617387259bdSDeclan Doherty 	dst_port = l2fwd_dst_ports[portid];
618387259bdSDeclan Doherty 	eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
619387259bdSDeclan Doherty 
620387259bdSDeclan Doherty 	/* 02:00:00:00:00:xx */
621387259bdSDeclan Doherty 	tmp = &eth->d_addr.addr_bytes[0];
622387259bdSDeclan Doherty 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
623387259bdSDeclan Doherty 
624387259bdSDeclan Doherty 	/* src addr */
625387259bdSDeclan Doherty 	ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
626387259bdSDeclan Doherty 
627387259bdSDeclan Doherty 	l2fwd_send_packet(m, (uint8_t) dst_port);
628387259bdSDeclan Doherty }
629387259bdSDeclan Doherty 
630387259bdSDeclan Doherty /** Generate random key */
631387259bdSDeclan Doherty static void
632387259bdSDeclan Doherty generate_random_key(uint8_t *key, unsigned length)
633387259bdSDeclan Doherty {
6346dad6e69SPiotr Azarewicz 	int fd;
6356dad6e69SPiotr Azarewicz 	int ret;
636387259bdSDeclan Doherty 
6376dad6e69SPiotr Azarewicz 	fd = open("/dev/urandom", O_RDONLY);
6386dad6e69SPiotr Azarewicz 	if (fd < 0)
6396dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
6406dad6e69SPiotr Azarewicz 
6416dad6e69SPiotr Azarewicz 	ret = read(fd, key, length);
6426dad6e69SPiotr Azarewicz 	close(fd);
6436dad6e69SPiotr Azarewicz 
6446dad6e69SPiotr Azarewicz 	if (ret != (signed)length)
6456dad6e69SPiotr Azarewicz 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
646387259bdSDeclan Doherty }
647387259bdSDeclan Doherty 
6481bd407faSFiona Trahe static struct rte_cryptodev_sym_session *
649*2c59bd32SSlawomir Mrozowicz initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
650387259bdSDeclan Doherty {
6511bd407faSFiona Trahe 	struct rte_crypto_sym_xform *first_xform;
652387259bdSDeclan Doherty 
6532661f4fbSPablo de Lara 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
6542661f4fbSPablo de Lara 		first_xform = &options->aead_xform;
6552661f4fbSPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
656387259bdSDeclan Doherty 		first_xform = &options->cipher_xform;
657387259bdSDeclan Doherty 		first_xform->next = &options->auth_xform;
6581a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
659387259bdSDeclan Doherty 		first_xform = &options->auth_xform;
660387259bdSDeclan Doherty 		first_xform->next = &options->cipher_xform;
6611a75e9f3SPablo de Lara 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
6621a75e9f3SPablo de Lara 		first_xform = &options->cipher_xform;
6631a75e9f3SPablo de Lara 	} else {
6641a75e9f3SPablo de Lara 		first_xform = &options->auth_xform;
665387259bdSDeclan Doherty 	}
666387259bdSDeclan Doherty 
6671bd407faSFiona Trahe 	return rte_cryptodev_sym_session_create(cdev_id, first_xform);
668387259bdSDeclan Doherty }
669387259bdSDeclan Doherty 
670387259bdSDeclan Doherty static void
671387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
672387259bdSDeclan Doherty 
673387259bdSDeclan Doherty /* main processing loop */
674387259bdSDeclan Doherty static void
675387259bdSDeclan Doherty l2fwd_main_loop(struct l2fwd_crypto_options *options)
676387259bdSDeclan Doherty {
677387259bdSDeclan Doherty 	struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
678c0f87eb5SDeclan Doherty 	struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
679c0f87eb5SDeclan Doherty 
680387259bdSDeclan Doherty 	unsigned lcore_id = rte_lcore_id();
681387259bdSDeclan Doherty 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
682268ca735SPablo de Lara 	unsigned i, j, portid, nb_rx, len;
683387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
684387259bdSDeclan Doherty 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
685387259bdSDeclan Doherty 			US_PER_S * BURST_TX_DRAIN_US;
686387259bdSDeclan Doherty 	struct l2fwd_crypto_params *cparams;
687387259bdSDeclan Doherty 	struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
688*2c59bd32SSlawomir Mrozowicz 	struct rte_cryptodev_sym_session *session;
689387259bdSDeclan Doherty 
690387259bdSDeclan Doherty 	if (qconf->nb_rx_ports == 0) {
691387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
692387259bdSDeclan Doherty 		return;
693387259bdSDeclan Doherty 	}
694387259bdSDeclan Doherty 
695387259bdSDeclan Doherty 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
696387259bdSDeclan Doherty 
697387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_rx_ports; i++) {
698387259bdSDeclan Doherty 
699387259bdSDeclan Doherty 		portid = qconf->rx_port_list[i];
700387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
701387259bdSDeclan Doherty 			portid);
702387259bdSDeclan Doherty 	}
703387259bdSDeclan Doherty 
704387259bdSDeclan Doherty 	for (i = 0; i < qconf->nb_crypto_devs; i++) {
7051a75e9f3SPablo de Lara 		port_cparams[i].do_cipher = 0;
7061a75e9f3SPablo de Lara 		port_cparams[i].do_hash = 0;
7072661f4fbSPablo de Lara 		port_cparams[i].do_aead = 0;
7081a75e9f3SPablo de Lara 
7091a75e9f3SPablo de Lara 		switch (options->xform_chain) {
7102661f4fbSPablo de Lara 		case L2FWD_CRYPTO_AEAD:
7112661f4fbSPablo de Lara 			port_cparams[i].do_aead = 1;
7122661f4fbSPablo de Lara 			break;
7131a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_HASH:
7141a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_CIPHER:
7151a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7161a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7171a75e9f3SPablo de Lara 			break;
7181a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_HASH_ONLY:
7191a75e9f3SPablo de Lara 			port_cparams[i].do_hash = 1;
7201a75e9f3SPablo de Lara 			break;
7211a75e9f3SPablo de Lara 		case L2FWD_CRYPTO_CIPHER_ONLY:
7221a75e9f3SPablo de Lara 			port_cparams[i].do_cipher = 1;
7231a75e9f3SPablo de Lara 			break;
7241a75e9f3SPablo de Lara 		}
7251a75e9f3SPablo de Lara 
726387259bdSDeclan Doherty 		port_cparams[i].dev_id = qconf->cryptodev_list[i];
727387259bdSDeclan Doherty 		port_cparams[i].qp_id = 0;
728387259bdSDeclan Doherty 
72927cf2d1bSPablo de Lara 		port_cparams[i].block_size = options->block_size;
730387259bdSDeclan Doherty 
7311a75e9f3SPablo de Lara 		if (port_cparams[i].do_hash) {
732acf86169SPablo de Lara 			port_cparams[i].auth_iv.data = options->auth_iv.data;
733acf86169SPablo de Lara 			port_cparams[i].auth_iv.length = options->auth_iv.length;
734acf86169SPablo de Lara 			if (!options->auth_iv_param)
735acf86169SPablo de Lara 				generate_random_key(port_cparams[i].auth_iv.data,
736acf86169SPablo de Lara 						port_cparams[i].auth_iv.length);
7372661f4fbSPablo de Lara 			if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
7382661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 1;
7392661f4fbSPablo de Lara 			else
7402661f4fbSPablo de Lara 				port_cparams[i].hash_verify = 0;
7412661f4fbSPablo de Lara 
7422661f4fbSPablo de Lara 			port_cparams[i].auth_algo = options->auth_xform.auth.algo;
743acf86169SPablo de Lara 			/* Set IV parameters */
744acf86169SPablo de Lara 			if (options->auth_iv.length) {
745acf86169SPablo de Lara 				options->auth_xform.auth.iv.offset =
746acf86169SPablo de Lara 					IV_OFFSET + options->cipher_iv.length;
747acf86169SPablo de Lara 				options->auth_xform.auth.iv.length =
748acf86169SPablo de Lara 					options->auth_iv.length;
749acf86169SPablo de Lara 			}
7502661f4fbSPablo de Lara 		}
7512661f4fbSPablo de Lara 
7522661f4fbSPablo de Lara 		if (port_cparams[i].do_aead) {
7532661f4fbSPablo de Lara 			port_cparams[i].aead_algo = options->aead_xform.aead.algo;
75427cf2d1bSPablo de Lara 			port_cparams[i].digest_length =
7552661f4fbSPablo de Lara 					options->aead_xform.aead.digest_length;
7562661f4fbSPablo de Lara 			if (options->aead_xform.aead.add_auth_data_length) {
75727cf2d1bSPablo de Lara 				port_cparams[i].aad.data = options->aad.data;
758617a7949SPablo de Lara 				port_cparams[i].aad.phys_addr = options->aad.phys_addr;
7592661f4fbSPablo de Lara 				port_cparams[i].aad.length = options->aad.length;
760617a7949SPablo de Lara 				if (!options->aad_param)
76127cf2d1bSPablo de Lara 					generate_random_key(port_cparams[i].aad.data,
762a158899aSPablo de Lara 						port_cparams[i].aad.length);
76327cf2d1bSPablo de Lara 
76418f421f6SPablo de Lara 			} else
76518f421f6SPablo de Lara 				port_cparams[i].aad.length = 0;
76627cf2d1bSPablo de Lara 
7672661f4fbSPablo de Lara 			if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
76827cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 1;
76927cf2d1bSPablo de Lara 			else
77027cf2d1bSPablo de Lara 				port_cparams[i].hash_verify = 0;
771d29ea843SPablo de Lara 
7722661f4fbSPablo de Lara 			/* Set IV parameters */
7732661f4fbSPablo de Lara 			options->aead_xform.aead.iv.offset = IV_OFFSET;
7742661f4fbSPablo de Lara 			options->aead_xform.aead.iv.length = options->aead_iv.length;
7751a75e9f3SPablo de Lara 		}
7761a75e9f3SPablo de Lara 
7771a75e9f3SPablo de Lara 		if (port_cparams[i].do_cipher) {
778acf86169SPablo de Lara 			port_cparams[i].cipher_iv.data = options->cipher_iv.data;
779acf86169SPablo de Lara 			port_cparams[i].cipher_iv.length = options->cipher_iv.length;
780acf86169SPablo de Lara 			if (!options->cipher_iv_param)
781acf86169SPablo de Lara 				generate_random_key(port_cparams[i].cipher_iv.data,
782acf86169SPablo de Lara 						port_cparams[i].cipher_iv.length);
783617a7949SPablo de Lara 
784d29ea843SPablo de Lara 			port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
7850fbd75a9SPablo de Lara 			/* Set IV parameters */
7860fbd75a9SPablo de Lara 			options->cipher_xform.cipher.iv.offset = IV_OFFSET;
787acf86169SPablo de Lara 			options->cipher_xform.cipher.iv.length =
788acf86169SPablo de Lara 						options->cipher_iv.length;
78927cf2d1bSPablo de Lara 		}
790617a7949SPablo de Lara 
791*2c59bd32SSlawomir Mrozowicz 		session = initialize_crypto_session(options,
792387259bdSDeclan Doherty 				port_cparams[i].dev_id);
793*2c59bd32SSlawomir Mrozowicz 		if (session == NULL)
794*2c59bd32SSlawomir Mrozowicz 			rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
795387259bdSDeclan Doherty 
796*2c59bd32SSlawomir Mrozowicz 		port_cparams[i].session = session;
797*2c59bd32SSlawomir Mrozowicz 
798387259bdSDeclan Doherty 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
799387259bdSDeclan Doherty 				port_cparams[i].dev_id);
800387259bdSDeclan Doherty 	}
801387259bdSDeclan Doherty 
80241e97c2eSPablo de Lara 	l2fwd_crypto_options_print(options);
80341e97c2eSPablo de Lara 
80441e97c2eSPablo de Lara 	/*
80541e97c2eSPablo de Lara 	 * Initialize previous tsc timestamp before the loop,
80641e97c2eSPablo de Lara 	 * to avoid showing the port statistics immediately,
80741e97c2eSPablo de Lara 	 * so user can see the crypto information.
80841e97c2eSPablo de Lara 	 */
80941e97c2eSPablo de Lara 	prev_tsc = rte_rdtsc();
810387259bdSDeclan Doherty 	while (1) {
811387259bdSDeclan Doherty 
812387259bdSDeclan Doherty 		cur_tsc = rte_rdtsc();
813387259bdSDeclan Doherty 
814387259bdSDeclan Doherty 		/*
815268ca735SPablo de Lara 		 * Crypto device/TX burst queue drain
816387259bdSDeclan Doherty 		 */
817387259bdSDeclan Doherty 		diff_tsc = cur_tsc - prev_tsc;
818387259bdSDeclan Doherty 		if (unlikely(diff_tsc > drain_tsc)) {
819268ca735SPablo de Lara 			/* Enqueue all crypto ops remaining in buffers */
820268ca735SPablo de Lara 			for (i = 0; i < qconf->nb_crypto_devs; i++) {
821268ca735SPablo de Lara 				cparams = &port_cparams[i];
822268ca735SPablo de Lara 				len = qconf->op_buf[cparams->dev_id].len;
823268ca735SPablo de Lara 				l2fwd_crypto_send_burst(qconf, len, cparams);
824268ca735SPablo de Lara 				qconf->op_buf[cparams->dev_id].len = 0;
825268ca735SPablo de Lara 			}
826268ca735SPablo de Lara 			/* Transmit all packets remaining in buffers */
827387259bdSDeclan Doherty 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
828c0f87eb5SDeclan Doherty 				if (qconf->pkt_buf[portid].len == 0)
829387259bdSDeclan Doherty 					continue;
830387259bdSDeclan Doherty 				l2fwd_send_burst(&lcore_queue_conf[lcore_id],
831c0f87eb5SDeclan Doherty 						 qconf->pkt_buf[portid].len,
832387259bdSDeclan Doherty 						 (uint8_t) portid);
833c0f87eb5SDeclan Doherty 				qconf->pkt_buf[portid].len = 0;
834387259bdSDeclan Doherty 			}
835387259bdSDeclan Doherty 
836387259bdSDeclan Doherty 			/* if timer is enabled */
837387259bdSDeclan Doherty 			if (timer_period > 0) {
838387259bdSDeclan Doherty 
839387259bdSDeclan Doherty 				/* advance the timer */
840387259bdSDeclan Doherty 				timer_tsc += diff_tsc;
841387259bdSDeclan Doherty 
842387259bdSDeclan Doherty 				/* if timer has reached its timeout */
843387259bdSDeclan Doherty 				if (unlikely(timer_tsc >=
844387259bdSDeclan Doherty 						(uint64_t)timer_period)) {
845387259bdSDeclan Doherty 
846387259bdSDeclan Doherty 					/* do this only on master core */
847ad509b4aSDeclan Doherty 					if (lcore_id == rte_get_master_lcore()
848ad509b4aSDeclan Doherty 						&& options->refresh_period) {
849387259bdSDeclan Doherty 						print_stats();
850387259bdSDeclan Doherty 						timer_tsc = 0;
851387259bdSDeclan Doherty 					}
852387259bdSDeclan Doherty 				}
853387259bdSDeclan Doherty 			}
854387259bdSDeclan Doherty 
855387259bdSDeclan Doherty 			prev_tsc = cur_tsc;
856387259bdSDeclan Doherty 		}
857387259bdSDeclan Doherty 
858387259bdSDeclan Doherty 		/*
859387259bdSDeclan Doherty 		 * Read packet from RX queues
860387259bdSDeclan Doherty 		 */
861387259bdSDeclan Doherty 		for (i = 0; i < qconf->nb_rx_ports; i++) {
862387259bdSDeclan Doherty 			portid = qconf->rx_port_list[i];
863387259bdSDeclan Doherty 
864387259bdSDeclan Doherty 			cparams = &port_cparams[i];
865387259bdSDeclan Doherty 
866387259bdSDeclan Doherty 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
867387259bdSDeclan Doherty 						 pkts_burst, MAX_PKT_BURST);
868387259bdSDeclan Doherty 
869387259bdSDeclan Doherty 			port_statistics[portid].rx += nb_rx;
870387259bdSDeclan Doherty 
871c0f87eb5SDeclan Doherty 			if (nb_rx) {
872387259bdSDeclan Doherty 				/*
873c0f87eb5SDeclan Doherty 				 * If we can't allocate a crypto_ops, then drop
874387259bdSDeclan Doherty 				 * the rest of the burst and dequeue and
875387259bdSDeclan Doherty 				 * process the packets to free offload structs
876387259bdSDeclan Doherty 				 */
877c0f87eb5SDeclan Doherty 				if (rte_crypto_op_bulk_alloc(
878c0f87eb5SDeclan Doherty 						l2fwd_crypto_op_pool,
879c0f87eb5SDeclan Doherty 						RTE_CRYPTO_OP_TYPE_SYMMETRIC,
880c0f87eb5SDeclan Doherty 						ops_burst, nb_rx) !=
881c0f87eb5SDeclan Doherty 								nb_rx) {
882c0f87eb5SDeclan Doherty 					for (j = 0; j < nb_rx; j++)
883d7acf6baSPablo de Lara 						rte_pktmbuf_free(pkts_burst[j]);
884c0f87eb5SDeclan Doherty 
885c0f87eb5SDeclan Doherty 					nb_rx = 0;
886387259bdSDeclan Doherty 				}
887387259bdSDeclan Doherty 
888c0f87eb5SDeclan Doherty 				/* Enqueue packets from Crypto device*/
889c0f87eb5SDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
890c0f87eb5SDeclan Doherty 					m = pkts_burst[j];
891387259bdSDeclan Doherty 
892c0f87eb5SDeclan Doherty 					l2fwd_simple_crypto_enqueue(m,
893c0f87eb5SDeclan Doherty 							ops_burst[j], cparams);
894c0f87eb5SDeclan Doherty 				}
895387259bdSDeclan Doherty 			}
896387259bdSDeclan Doherty 
897387259bdSDeclan Doherty 			/* Dequeue packets from Crypto device */
898c0f87eb5SDeclan Doherty 			do {
899387259bdSDeclan Doherty 				nb_rx = rte_cryptodev_dequeue_burst(
900387259bdSDeclan Doherty 						cparams->dev_id, cparams->qp_id,
901c0f87eb5SDeclan Doherty 						ops_burst, MAX_PKT_BURST);
902c0f87eb5SDeclan Doherty 
903c0f87eb5SDeclan Doherty 				crypto_statistics[cparams->dev_id].dequeued +=
904c0f87eb5SDeclan Doherty 						nb_rx;
905387259bdSDeclan Doherty 
906387259bdSDeclan Doherty 				/* Forward crypto'd packets */
907387259bdSDeclan Doherty 				for (j = 0; j < nb_rx; j++) {
908c0f87eb5SDeclan Doherty 					m = ops_burst[j]->sym->m_src;
909c0f87eb5SDeclan Doherty 
910c0f87eb5SDeclan Doherty 					rte_crypto_op_free(ops_burst[j]);
911387259bdSDeclan Doherty 					l2fwd_simple_forward(m, portid);
912387259bdSDeclan Doherty 				}
913c0f87eb5SDeclan Doherty 			} while (nb_rx == MAX_PKT_BURST);
914387259bdSDeclan Doherty 		}
915387259bdSDeclan Doherty 	}
916387259bdSDeclan Doherty }
917387259bdSDeclan Doherty 
918387259bdSDeclan Doherty static int
919387259bdSDeclan Doherty l2fwd_launch_one_lcore(void *arg)
920387259bdSDeclan Doherty {
921387259bdSDeclan Doherty 	l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
922387259bdSDeclan Doherty 	return 0;
923387259bdSDeclan Doherty }
924387259bdSDeclan Doherty 
925387259bdSDeclan Doherty /* Display command line arguments usage */
926387259bdSDeclan Doherty static void
927387259bdSDeclan Doherty l2fwd_crypto_usage(const char *prgname)
928387259bdSDeclan Doherty {
929912b3a0aSPablo de Lara 	printf("%s [EAL options] --\n"
930387259bdSDeclan Doherty 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
931387259bdSDeclan Doherty 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
93269a558e3SPablo de Lara 		"  -s manage all ports from single lcore\n"
933a3380989SPablo de Lara 		"  -T PERIOD: statistics will be refreshed each PERIOD seconds"
934387259bdSDeclan Doherty 		" (0 to disable, 10 default, 86400 maximum)\n"
935387259bdSDeclan Doherty 
936912b3a0aSPablo de Lara 		"  --cdev_type HW / SW / ANY\n"
937a3c2e34bSPablo de Lara 		"  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
9382661f4fbSPablo de Lara 		" HASH_ONLY / AEAD\n"
939387259bdSDeclan Doherty 
940387259bdSDeclan Doherty 		"  --cipher_algo ALGO\n"
941387259bdSDeclan Doherty 		"  --cipher_op ENCRYPT / DECRYPT\n"
942fcdbb3d5SPablo de Lara 		"  --cipher_key KEY (bytes separated with \":\")\n"
943a061e50aSPablo de Lara 		"  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
944acf86169SPablo de Lara 		"  --cipher_iv IV (bytes separated with \":\")\n"
945acf86169SPablo de Lara 		"  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
946387259bdSDeclan Doherty 
9473b98cbaaSPablo de Lara 		"  --auth_algo ALGO\n"
948387259bdSDeclan Doherty 		"  --auth_op GENERATE / VERIFY\n"
949fcdbb3d5SPablo de Lara 		"  --auth_key KEY (bytes separated with \":\")\n"
950a061e50aSPablo de Lara 		"  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
951acf86169SPablo de Lara 		"  --auth_iv IV (bytes separated with \":\")\n"
952acf86169SPablo de Lara 		"  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
9532661f4fbSPablo de Lara 
9542661f4fbSPablo de Lara 		"  --aead_algo ALGO\n"
9552661f4fbSPablo de Lara 		"  --aead_op ENCRYPT / DECRYPT\n"
9562661f4fbSPablo de Lara 		"  --aead_key KEY (bytes separated with \":\")\n"
9572661f4fbSPablo de Lara 		"  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
9582661f4fbSPablo de Lara 		"  --aead_iv IV (bytes separated with \":\")\n"
9592661f4fbSPablo de Lara 		"  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
960fcdbb3d5SPablo de Lara 		"  --aad AAD (bytes separated with \":\")\n"
961a061e50aSPablo de Lara 		"  --aad_random_size SIZE: size of AAD when generated randomly\n"
9622661f4fbSPablo de Lara 
963a061e50aSPablo de Lara 		"  --digest_size SIZE: size of digest to be generated/verified\n"
964387259bdSDeclan Doherty 
965d2797f51SFan Zhang 		"  --sessionless\n"
966d2797f51SFan Zhang 		"  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n",
967387259bdSDeclan Doherty 	       prgname);
968387259bdSDeclan Doherty }
969387259bdSDeclan Doherty 
970387259bdSDeclan Doherty /** Parse crypto device type command line argument */
971387259bdSDeclan Doherty static int
97227cf2d1bSPablo de Lara parse_cryptodev_type(enum cdev_type *type, char *optarg)
973387259bdSDeclan Doherty {
97427cf2d1bSPablo de Lara 	if (strcmp("HW", optarg) == 0) {
97527cf2d1bSPablo de Lara 		*type = CDEV_TYPE_HW;
976387259bdSDeclan Doherty 		return 0;
97727cf2d1bSPablo de Lara 	} else if (strcmp("SW", optarg) == 0) {
97827cf2d1bSPablo de Lara 		*type = CDEV_TYPE_SW;
97927cf2d1bSPablo de Lara 		return 0;
98027cf2d1bSPablo de Lara 	} else if (strcmp("ANY", optarg) == 0) {
98127cf2d1bSPablo de Lara 		*type = CDEV_TYPE_ANY;
982387259bdSDeclan Doherty 		return 0;
983387259bdSDeclan Doherty 	}
984387259bdSDeclan Doherty 
985387259bdSDeclan Doherty 	return -1;
986387259bdSDeclan Doherty }
987387259bdSDeclan Doherty 
988387259bdSDeclan Doherty /** Parse crypto chain xform command line argument */
989387259bdSDeclan Doherty static int
990387259bdSDeclan Doherty parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
991387259bdSDeclan Doherty {
992387259bdSDeclan Doherty 	if (strcmp("CIPHER_HASH", optarg) == 0) {
993387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
994387259bdSDeclan Doherty 		return 0;
995387259bdSDeclan Doherty 	} else if (strcmp("HASH_CIPHER", optarg) == 0) {
996387259bdSDeclan Doherty 		options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
997387259bdSDeclan Doherty 		return 0;
9981a75e9f3SPablo de Lara 	} else if (strcmp("CIPHER_ONLY", optarg) == 0) {
9991a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
10001a75e9f3SPablo de Lara 		return 0;
10011a75e9f3SPablo de Lara 	} else if (strcmp("HASH_ONLY", optarg) == 0) {
10021a75e9f3SPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
10031a75e9f3SPablo de Lara 		return 0;
10042661f4fbSPablo de Lara 	} else if (strcmp("AEAD", optarg) == 0) {
10052661f4fbSPablo de Lara 		options->xform_chain = L2FWD_CRYPTO_AEAD;
10062661f4fbSPablo de Lara 		return 0;
1007387259bdSDeclan Doherty 	}
1008387259bdSDeclan Doherty 
1009387259bdSDeclan Doherty 	return -1;
1010387259bdSDeclan Doherty }
1011387259bdSDeclan Doherty 
1012387259bdSDeclan Doherty /** Parse crypto cipher algo option command line argument */
1013387259bdSDeclan Doherty static int
1014387259bdSDeclan Doherty parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1015387259bdSDeclan Doherty {
101600c58901SPablo de Lara 
10174790f99dSPablo de Lara 	if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
10184790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Cipher algorithm specified "
10194790f99dSPablo de Lara 				"not supported!\n");
1020387259bdSDeclan Doherty 		return -1;
1021387259bdSDeclan Doherty 	}
1022387259bdSDeclan Doherty 
10234790f99dSPablo de Lara 	return 0;
10244790f99dSPablo de Lara }
10254790f99dSPablo de Lara 
1026387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1027387259bdSDeclan Doherty static int
1028387259bdSDeclan Doherty parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1029387259bdSDeclan Doherty {
1030387259bdSDeclan Doherty 	if (strcmp("ENCRYPT", optarg) == 0) {
1031387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1032387259bdSDeclan Doherty 		return 0;
1033387259bdSDeclan Doherty 	} else if (strcmp("DECRYPT", optarg) == 0) {
1034387259bdSDeclan Doherty 		*op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1035387259bdSDeclan Doherty 		return 0;
1036387259bdSDeclan Doherty 	}
1037387259bdSDeclan Doherty 
1038387259bdSDeclan Doherty 	printf("Cipher operation not supported!\n");
1039387259bdSDeclan Doherty 	return -1;
1040387259bdSDeclan Doherty }
1041387259bdSDeclan Doherty 
1042387259bdSDeclan Doherty /** Parse crypto key command line argument */
1043387259bdSDeclan Doherty static int
10441df9c010SPablo de Lara parse_key(uint8_t *data, char *input_arg)
1045387259bdSDeclan Doherty {
10461df9c010SPablo de Lara 	unsigned byte_count;
10471df9c010SPablo de Lara 	char *token;
10481df9c010SPablo de Lara 
10491df9c010SPablo de Lara 	for (byte_count = 0, token = strtok(input_arg, ":");
10501df9c010SPablo de Lara 			(byte_count < MAX_KEY_SIZE) && (token != NULL);
10511df9c010SPablo de Lara 			token = strtok(NULL, ":")) {
10521df9c010SPablo de Lara 
10531df9c010SPablo de Lara 		int number = (int)strtol(token, NULL, 16);
10541df9c010SPablo de Lara 
10551df9c010SPablo de Lara 		if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1056387259bdSDeclan Doherty 			return -1;
10571df9c010SPablo de Lara 
10581df9c010SPablo de Lara 		data[byte_count++] = (uint8_t)number;
10591df9c010SPablo de Lara 	}
10601df9c010SPablo de Lara 
1061a061e50aSPablo de Lara 	return byte_count;
1062a061e50aSPablo de Lara }
1063a061e50aSPablo de Lara 
1064a061e50aSPablo de Lara /** Parse size param*/
1065a061e50aSPablo de Lara static int
1066a061e50aSPablo de Lara parse_size(int *size, const char *q_arg)
1067a061e50aSPablo de Lara {
1068a061e50aSPablo de Lara 	char *end = NULL;
1069a061e50aSPablo de Lara 	unsigned long n;
1070a061e50aSPablo de Lara 
1071a061e50aSPablo de Lara 	/* parse hexadecimal string */
1072a061e50aSPablo de Lara 	n = strtoul(q_arg, &end, 10);
1073a061e50aSPablo de Lara 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1074a061e50aSPablo de Lara 		n = 0;
1075a061e50aSPablo de Lara 
1076a061e50aSPablo de Lara 	if (n == 0) {
1077a061e50aSPablo de Lara 		printf("invalid size\n");
1078a061e50aSPablo de Lara 		return -1;
1079a061e50aSPablo de Lara 	}
1080a061e50aSPablo de Lara 
1081a061e50aSPablo de Lara 	*size = n;
10821df9c010SPablo de Lara 	return 0;
1083387259bdSDeclan Doherty }
1084387259bdSDeclan Doherty 
1085387259bdSDeclan Doherty /** Parse crypto cipher operation command line argument */
1086387259bdSDeclan Doherty static int
1087387259bdSDeclan Doherty parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1088387259bdSDeclan Doherty {
10894790f99dSPablo de Lara 	if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
10904790f99dSPablo de Lara 		RTE_LOG(ERR, USER1, "Authentication algorithm specified "
10914790f99dSPablo de Lara 				"not supported!\n");
1092387259bdSDeclan Doherty 		return -1;
1093387259bdSDeclan Doherty 	}
1094387259bdSDeclan Doherty 
10954790f99dSPablo de Lara 	return 0;
10964790f99dSPablo de Lara }
10974790f99dSPablo de Lara 
1098387259bdSDeclan Doherty static int
1099387259bdSDeclan Doherty parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1100387259bdSDeclan Doherty {
1101387259bdSDeclan Doherty 	if (strcmp("VERIFY", optarg) == 0) {
1102387259bdSDeclan Doherty 		*op = RTE_CRYPTO_AUTH_OP_VERIFY;
1103387259bdSDeclan Doherty 		return 0;
1104387259bdSDeclan Doherty 	} else if (strcmp("GENERATE", optarg) == 0) {
110572169087SPablo de Lara 		*op = RTE_CRYPTO_AUTH_OP_GENERATE;
1106387259bdSDeclan Doherty 		return 0;
1107387259bdSDeclan Doherty 	}
1108387259bdSDeclan Doherty 
1109387259bdSDeclan Doherty 	printf("Authentication operation specified not supported!\n");
1110387259bdSDeclan Doherty 	return -1;
1111387259bdSDeclan Doherty }
1112387259bdSDeclan Doherty 
1113d2797f51SFan Zhang static int
11142661f4fbSPablo de Lara parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
11152661f4fbSPablo de Lara {
11162661f4fbSPablo de Lara 	if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
11172661f4fbSPablo de Lara 		RTE_LOG(ERR, USER1, "AEAD algorithm specified "
11182661f4fbSPablo de Lara 				"not supported!\n");
11192661f4fbSPablo de Lara 		return -1;
11202661f4fbSPablo de Lara 	}
11212661f4fbSPablo de Lara 
11222661f4fbSPablo de Lara 	return 0;
11232661f4fbSPablo de Lara }
11242661f4fbSPablo de Lara 
11252661f4fbSPablo de Lara static int
11262661f4fbSPablo de Lara parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
11272661f4fbSPablo de Lara {
11282661f4fbSPablo de Lara 	if (strcmp("ENCRYPT", optarg) == 0) {
11292661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
11302661f4fbSPablo de Lara 		return 0;
11312661f4fbSPablo de Lara 	} else if (strcmp("DECRYPT", optarg) == 0) {
11322661f4fbSPablo de Lara 		*op = RTE_CRYPTO_AEAD_OP_DECRYPT;
11332661f4fbSPablo de Lara 		return 0;
11342661f4fbSPablo de Lara 	}
11352661f4fbSPablo de Lara 
11362661f4fbSPablo de Lara 	printf("AEAD operation specified not supported!\n");
11372661f4fbSPablo de Lara 	return -1;
11382661f4fbSPablo de Lara }
11392661f4fbSPablo de Lara static int
1140d2797f51SFan Zhang parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1141d2797f51SFan Zhang 		const char *q_arg)
1142d2797f51SFan Zhang {
1143d2797f51SFan Zhang 	char *end = NULL;
1144d2797f51SFan Zhang 	uint64_t pm;
1145d2797f51SFan Zhang 
1146d2797f51SFan Zhang 	/* parse hexadecimal string */
1147d2797f51SFan Zhang 	pm = strtoul(q_arg, &end, 16);
1148d2797f51SFan Zhang 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1149d2797f51SFan Zhang 		pm = 0;
1150d2797f51SFan Zhang 
1151d2797f51SFan Zhang 	options->cryptodev_mask = pm;
1152d2797f51SFan Zhang 	if (options->cryptodev_mask == 0) {
1153d2797f51SFan Zhang 		printf("invalid cryptodev_mask specified\n");
1154d2797f51SFan Zhang 		return -1;
1155d2797f51SFan Zhang 	}
1156d2797f51SFan Zhang 
1157d2797f51SFan Zhang 	return 0;
1158d2797f51SFan Zhang }
1159d2797f51SFan Zhang 
1160387259bdSDeclan Doherty /** Parse long options */
1161387259bdSDeclan Doherty static int
1162387259bdSDeclan Doherty l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1163387259bdSDeclan Doherty 		struct option *lgopts, int option_index)
1164387259bdSDeclan Doherty {
116527cf2d1bSPablo de Lara 	int retval;
116627cf2d1bSPablo de Lara 
116749f79e86SPablo de Lara 	if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
116849f79e86SPablo de Lara 		retval = parse_cryptodev_type(&options->type, optarg);
116949f79e86SPablo de Lara 		if (retval == 0)
117099218e76SPablo de Lara 			snprintf(options->string_type, MAX_STR_LEN,
117199218e76SPablo de Lara 				"%s", optarg);
117249f79e86SPablo de Lara 		return retval;
117349f79e86SPablo de Lara 	}
1174387259bdSDeclan Doherty 
1175387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "chain") == 0)
1176387259bdSDeclan Doherty 		return parse_crypto_opt_chain(options, optarg);
1177387259bdSDeclan Doherty 
1178387259bdSDeclan Doherty 	/* Cipher options */
117900c58901SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
118000c58901SPablo de Lara 		return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1181387259bdSDeclan Doherty 				optarg);
1182387259bdSDeclan Doherty 
1183387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1184387259bdSDeclan Doherty 		return parse_cipher_op(&options->cipher_xform.cipher.op,
1185387259bdSDeclan Doherty 				optarg);
1186387259bdSDeclan Doherty 
1187a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
11881df9c010SPablo de Lara 		options->ckey_param = 1;
1189a061e50aSPablo de Lara 		options->cipher_xform.cipher.key.length =
1190a061e50aSPablo de Lara 			parse_key(options->cipher_xform.cipher.key.data, optarg);
1191a061e50aSPablo de Lara 		if (options->cipher_xform.cipher.key.length > 0)
1192a061e50aSPablo de Lara 			return 0;
1193a061e50aSPablo de Lara 		else
1194a061e50aSPablo de Lara 			return -1;
11951df9c010SPablo de Lara 	}
1196387259bdSDeclan Doherty 
1197a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1198a061e50aSPablo de Lara 		return parse_size(&options->ckey_random_size, optarg);
1199a061e50aSPablo de Lara 
1200acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1201acf86169SPablo de Lara 		options->cipher_iv_param = 1;
1202acf86169SPablo de Lara 		options->cipher_iv.length =
1203acf86169SPablo de Lara 			parse_key(options->cipher_iv.data, optarg);
1204acf86169SPablo de Lara 		if (options->cipher_iv.length > 0)
1205a061e50aSPablo de Lara 			return 0;
1206a061e50aSPablo de Lara 		else
1207a061e50aSPablo de Lara 			return -1;
12081df9c010SPablo de Lara 	}
1209387259bdSDeclan Doherty 
1210acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1211acf86169SPablo de Lara 		return parse_size(&options->cipher_iv_random_size, optarg);
1212a061e50aSPablo de Lara 
1213387259bdSDeclan Doherty 	/* Authentication options */
121427cf2d1bSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
121500c58901SPablo de Lara 		return parse_auth_algo(&options->auth_xform.auth.algo,
1216387259bdSDeclan Doherty 				optarg);
121727cf2d1bSPablo de Lara 	}
1218387259bdSDeclan Doherty 
1219387259bdSDeclan Doherty 	else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
122072169087SPablo de Lara 		return parse_auth_op(&options->auth_xform.auth.op,
1221387259bdSDeclan Doherty 				optarg);
1222387259bdSDeclan Doherty 
1223a7f4562bSFiona Trahe 	else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
12241df9c010SPablo de Lara 		options->akey_param = 1;
1225a061e50aSPablo de Lara 		options->auth_xform.auth.key.length =
1226a061e50aSPablo de Lara 			parse_key(options->auth_xform.auth.key.data, optarg);
1227a061e50aSPablo de Lara 		if (options->auth_xform.auth.key.length > 0)
1228a061e50aSPablo de Lara 			return 0;
1229a061e50aSPablo de Lara 		else
1230a061e50aSPablo de Lara 			return -1;
1231a061e50aSPablo de Lara 	}
1232a061e50aSPablo de Lara 
1233a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1234a061e50aSPablo de Lara 		return parse_size(&options->akey_random_size, optarg);
12351df9c010SPablo de Lara 	}
1236387259bdSDeclan Doherty 
1237acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1238acf86169SPablo de Lara 		options->auth_iv_param = 1;
1239acf86169SPablo de Lara 		options->auth_iv.length =
1240acf86169SPablo de Lara 			parse_key(options->auth_iv.data, optarg);
1241acf86169SPablo de Lara 		if (options->auth_iv.length > 0)
1242acf86169SPablo de Lara 			return 0;
1243acf86169SPablo de Lara 		else
1244acf86169SPablo de Lara 			return -1;
1245acf86169SPablo de Lara 	}
1246acf86169SPablo de Lara 
1247acf86169SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1248acf86169SPablo de Lara 		return parse_size(&options->auth_iv_random_size, optarg);
1249acf86169SPablo de Lara 
12502661f4fbSPablo de Lara 	/* AEAD options */
12512661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
12522661f4fbSPablo de Lara 		return parse_aead_algo(&options->aead_xform.aead.algo,
12532661f4fbSPablo de Lara 				optarg);
12542661f4fbSPablo de Lara 	}
12552661f4fbSPablo de Lara 
12562661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
12572661f4fbSPablo de Lara 		return parse_aead_op(&options->aead_xform.aead.op,
12582661f4fbSPablo de Lara 				optarg);
12592661f4fbSPablo de Lara 
12602661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
12612661f4fbSPablo de Lara 		options->aead_key_param = 1;
12622661f4fbSPablo de Lara 		options->aead_xform.aead.key.length =
12632661f4fbSPablo de Lara 			parse_key(options->aead_xform.aead.key.data, optarg);
12642661f4fbSPablo de Lara 		if (options->aead_xform.aead.key.length > 0)
12652661f4fbSPablo de Lara 			return 0;
12662661f4fbSPablo de Lara 		else
12672661f4fbSPablo de Lara 			return -1;
12682661f4fbSPablo de Lara 	}
12692661f4fbSPablo de Lara 
12702661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
12712661f4fbSPablo de Lara 		return parse_size(&options->aead_key_random_size, optarg);
12722661f4fbSPablo de Lara 
12732661f4fbSPablo de Lara 
12742661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
12752661f4fbSPablo de Lara 		options->aead_iv_param = 1;
12762661f4fbSPablo de Lara 		options->aead_iv.length =
12772661f4fbSPablo de Lara 			parse_key(options->aead_iv.data, optarg);
12782661f4fbSPablo de Lara 		if (options->aead_iv.length > 0)
12792661f4fbSPablo de Lara 			return 0;
12802661f4fbSPablo de Lara 		else
12812661f4fbSPablo de Lara 			return -1;
12822661f4fbSPablo de Lara 	}
12832661f4fbSPablo de Lara 
12842661f4fbSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
12852661f4fbSPablo de Lara 		return parse_size(&options->aead_iv_random_size, optarg);
12862661f4fbSPablo de Lara 
1287617a7949SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1288617a7949SPablo de Lara 		options->aad_param = 1;
1289a061e50aSPablo de Lara 		options->aad.length =
1290a061e50aSPablo de Lara 			parse_key(options->aad.data, optarg);
1291a061e50aSPablo de Lara 		if (options->aad.length > 0)
1292a061e50aSPablo de Lara 			return 0;
1293a061e50aSPablo de Lara 		else
1294a061e50aSPablo de Lara 			return -1;
1295a061e50aSPablo de Lara 	}
1296a061e50aSPablo de Lara 
1297a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1298a061e50aSPablo de Lara 		return parse_size(&options->aad_random_size, optarg);
1299a061e50aSPablo de Lara 	}
1300a061e50aSPablo de Lara 
1301a061e50aSPablo de Lara 	else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1302a061e50aSPablo de Lara 		return parse_size(&options->digest_size, optarg);
1303617a7949SPablo de Lara 	}
1304617a7949SPablo de Lara 
13051df9c010SPablo de Lara 	else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1306387259bdSDeclan Doherty 		options->sessionless = 1;
1307387259bdSDeclan Doherty 		return 0;
1308387259bdSDeclan Doherty 	}
1309387259bdSDeclan Doherty 
1310d2797f51SFan Zhang 	else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1311d2797f51SFan Zhang 		return parse_cryptodev_mask(options, optarg);
1312d2797f51SFan Zhang 
1313387259bdSDeclan Doherty 	return -1;
1314387259bdSDeclan Doherty }
1315387259bdSDeclan Doherty 
1316387259bdSDeclan Doherty /** Parse port mask */
1317387259bdSDeclan Doherty static int
1318387259bdSDeclan Doherty l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1319387259bdSDeclan Doherty 		const char *q_arg)
1320387259bdSDeclan Doherty {
1321387259bdSDeclan Doherty 	char *end = NULL;
1322387259bdSDeclan Doherty 	unsigned long pm;
1323387259bdSDeclan Doherty 
1324387259bdSDeclan Doherty 	/* parse hexadecimal string */
1325387259bdSDeclan Doherty 	pm = strtoul(q_arg, &end, 16);
1326387259bdSDeclan Doherty 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1327387259bdSDeclan Doherty 		pm = 0;
1328387259bdSDeclan Doherty 
1329387259bdSDeclan Doherty 	options->portmask = pm;
1330387259bdSDeclan Doherty 	if (options->portmask == 0) {
1331387259bdSDeclan Doherty 		printf("invalid portmask specified\n");
1332387259bdSDeclan Doherty 		return -1;
1333387259bdSDeclan Doherty 	}
1334387259bdSDeclan Doherty 
1335387259bdSDeclan Doherty 	return pm;
1336387259bdSDeclan Doherty }
1337387259bdSDeclan Doherty 
1338387259bdSDeclan Doherty /** Parse number of queues */
1339387259bdSDeclan Doherty static int
1340387259bdSDeclan Doherty l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1341387259bdSDeclan Doherty 		const char *q_arg)
1342387259bdSDeclan Doherty {
1343387259bdSDeclan Doherty 	char *end = NULL;
1344387259bdSDeclan Doherty 	unsigned long n;
1345387259bdSDeclan Doherty 
1346387259bdSDeclan Doherty 	/* parse hexadecimal string */
1347387259bdSDeclan Doherty 	n = strtoul(q_arg, &end, 10);
1348387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1349387259bdSDeclan Doherty 		n = 0;
1350387259bdSDeclan Doherty 	else if (n >= MAX_RX_QUEUE_PER_LCORE)
1351387259bdSDeclan Doherty 		n = 0;
1352387259bdSDeclan Doherty 
1353387259bdSDeclan Doherty 	options->nb_ports_per_lcore = n;
1354387259bdSDeclan Doherty 	if (options->nb_ports_per_lcore == 0) {
1355387259bdSDeclan Doherty 		printf("invalid number of ports selected\n");
1356387259bdSDeclan Doherty 		return -1;
1357387259bdSDeclan Doherty 	}
1358387259bdSDeclan Doherty 
1359387259bdSDeclan Doherty 	return 0;
1360387259bdSDeclan Doherty }
1361387259bdSDeclan Doherty 
1362387259bdSDeclan Doherty /** Parse timer period */
1363387259bdSDeclan Doherty static int
1364387259bdSDeclan Doherty l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1365387259bdSDeclan Doherty 		const char *q_arg)
1366387259bdSDeclan Doherty {
1367387259bdSDeclan Doherty 	char *end = NULL;
13683c96262cSPablo de Lara 	unsigned long n;
1369387259bdSDeclan Doherty 
1370387259bdSDeclan Doherty 	/* parse number string */
13713c96262cSPablo de Lara 	n = (unsigned)strtol(q_arg, &end, 10);
1372387259bdSDeclan Doherty 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1373387259bdSDeclan Doherty 		n = 0;
1374387259bdSDeclan Doherty 
1375ad509b4aSDeclan Doherty 	if (n >= MAX_TIMER_PERIOD) {
13763c96262cSPablo de Lara 		printf("Warning refresh period specified %lu is greater than "
13773c96262cSPablo de Lara 				"max value %lu! using max value",
1378ad509b4aSDeclan Doherty 				n, MAX_TIMER_PERIOD);
1379ad509b4aSDeclan Doherty 		n = MAX_TIMER_PERIOD;
1380ad509b4aSDeclan Doherty 	}
1381387259bdSDeclan Doherty 
1382387259bdSDeclan Doherty 	options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1383387259bdSDeclan Doherty 
1384387259bdSDeclan Doherty 	return 0;
1385387259bdSDeclan Doherty }
1386387259bdSDeclan Doherty 
1387387259bdSDeclan Doherty /** Generate default options for application */
1388387259bdSDeclan Doherty static void
1389387259bdSDeclan Doherty l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1390387259bdSDeclan Doherty {
1391387259bdSDeclan Doherty 	options->portmask = 0xffffffff;
1392387259bdSDeclan Doherty 	options->nb_ports_per_lcore = 1;
1393387259bdSDeclan Doherty 	options->refresh_period = 10000;
1394387259bdSDeclan Doherty 	options->single_lcore = 0;
13953c96262cSPablo de Lara 	options->sessionless = 0;
1396387259bdSDeclan Doherty 
1397387259bdSDeclan Doherty 	options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1398387259bdSDeclan Doherty 
1399387259bdSDeclan Doherty 	/* Cipher Data */
14001bd407faSFiona Trahe 	options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1401387259bdSDeclan Doherty 	options->cipher_xform.next = NULL;
14021df9c010SPablo de Lara 	options->ckey_param = 0;
1403a061e50aSPablo de Lara 	options->ckey_random_size = -1;
1404a061e50aSPablo de Lara 	options->cipher_xform.cipher.key.length = 0;
1405acf86169SPablo de Lara 	options->cipher_iv_param = 0;
1406acf86169SPablo de Lara 	options->cipher_iv_random_size = -1;
1407acf86169SPablo de Lara 	options->cipher_iv.length = 0;
1408387259bdSDeclan Doherty 
1409387259bdSDeclan Doherty 	options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1410387259bdSDeclan Doherty 	options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1411387259bdSDeclan Doherty 
1412387259bdSDeclan Doherty 	/* Authentication Data */
14131bd407faSFiona Trahe 	options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1414387259bdSDeclan Doherty 	options->auth_xform.next = NULL;
14151df9c010SPablo de Lara 	options->akey_param = 0;
1416a061e50aSPablo de Lara 	options->akey_random_size = -1;
1417a061e50aSPablo de Lara 	options->auth_xform.auth.key.length = 0;
1418acf86169SPablo de Lara 	options->auth_iv_param = 0;
1419acf86169SPablo de Lara 	options->auth_iv_random_size = -1;
1420acf86169SPablo de Lara 	options->auth_iv.length = 0;
1421387259bdSDeclan Doherty 
1422387259bdSDeclan Doherty 	options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
142327cf2d1bSPablo de Lara 	options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1424387259bdSDeclan Doherty 
14252661f4fbSPablo de Lara 	/* AEAD Data */
14262661f4fbSPablo de Lara 	options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
14272661f4fbSPablo de Lara 	options->aead_xform.next = NULL;
14282661f4fbSPablo de Lara 	options->aead_key_param = 0;
14292661f4fbSPablo de Lara 	options->aead_key_random_size = -1;
14302661f4fbSPablo de Lara 	options->aead_xform.aead.key.length = 0;
14312661f4fbSPablo de Lara 	options->aead_iv_param = 0;
14322661f4fbSPablo de Lara 	options->aead_iv_random_size = -1;
14332661f4fbSPablo de Lara 	options->aead_iv.length = 0;
14342661f4fbSPablo de Lara 
1435b79e4c00SPablo de Lara 	options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1436b79e4c00SPablo de Lara 	options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1437b79e4c00SPablo de Lara 
14382661f4fbSPablo de Lara 	options->aad_param = 0;
14392661f4fbSPablo de Lara 	options->aad_random_size = -1;
14402661f4fbSPablo de Lara 	options->aad.length = 0;
14412661f4fbSPablo de Lara 
14422661f4fbSPablo de Lara 	options->digest_size = -1;
14432661f4fbSPablo de Lara 
144427cf2d1bSPablo de Lara 	options->type = CDEV_TYPE_ANY;
1445d2797f51SFan Zhang 	options->cryptodev_mask = UINT64_MAX;
1446387259bdSDeclan Doherty }
1447387259bdSDeclan Doherty 
1448387259bdSDeclan Doherty static void
144941e97c2eSPablo de Lara display_cipher_info(struct l2fwd_crypto_options *options)
145041e97c2eSPablo de Lara {
145141e97c2eSPablo de Lara 	printf("\n---- Cipher information ---\n");
145241e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
14534790f99dSPablo de Lara 		rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
145441e97c2eSPablo de Lara 	rte_hexdump(stdout, "Cipher key:",
145541e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.data,
145641e97c2eSPablo de Lara 			options->cipher_xform.cipher.key.length);
1457acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
145841e97c2eSPablo de Lara }
145941e97c2eSPablo de Lara 
146041e97c2eSPablo de Lara static void
146141e97c2eSPablo de Lara display_auth_info(struct l2fwd_crypto_options *options)
146241e97c2eSPablo de Lara {
146341e97c2eSPablo de Lara 	printf("\n---- Authentication information ---\n");
146441e97c2eSPablo de Lara 	printf("Algorithm: %s\n",
14654eb45d2dSPablo de Lara 		rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
146641e97c2eSPablo de Lara 	rte_hexdump(stdout, "Auth key:",
146741e97c2eSPablo de Lara 			options->auth_xform.auth.key.data,
146841e97c2eSPablo de Lara 			options->auth_xform.auth.key.length);
1469acf86169SPablo de Lara 	rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
14702661f4fbSPablo de Lara }
14712661f4fbSPablo de Lara 
14722661f4fbSPablo de Lara static void
14732661f4fbSPablo de Lara display_aead_info(struct l2fwd_crypto_options *options)
14742661f4fbSPablo de Lara {
14752661f4fbSPablo de Lara 	printf("\n---- AEAD information ---\n");
14762661f4fbSPablo de Lara 	printf("Algorithm: %s\n",
14772661f4fbSPablo de Lara 		rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
14782661f4fbSPablo de Lara 	rte_hexdump(stdout, "AEAD key:",
14792661f4fbSPablo de Lara 			options->aead_xform.aead.key.data,
14802661f4fbSPablo de Lara 			options->aead_xform.aead.key.length);
14812661f4fbSPablo de Lara 	rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
148241e97c2eSPablo de Lara 	rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
148341e97c2eSPablo de Lara }
148441e97c2eSPablo de Lara 
148541e97c2eSPablo de Lara static void
1486387259bdSDeclan Doherty l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1487387259bdSDeclan Doherty {
148841e97c2eSPablo de Lara 	char string_cipher_op[MAX_STR_LEN];
148941e97c2eSPablo de Lara 	char string_auth_op[MAX_STR_LEN];
14902661f4fbSPablo de Lara 	char string_aead_op[MAX_STR_LEN];
149141e97c2eSPablo de Lara 
149241e97c2eSPablo de Lara 	if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
149341e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Encrypt");
149441e97c2eSPablo de Lara 	else
149541e97c2eSPablo de Lara 		strcpy(string_cipher_op, "Decrypt");
149641e97c2eSPablo de Lara 
149741e97c2eSPablo de Lara 	if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
149841e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth generate");
149941e97c2eSPablo de Lara 	else
150041e97c2eSPablo de Lara 		strcpy(string_auth_op, "Auth verify");
150141e97c2eSPablo de Lara 
15022661f4fbSPablo de Lara 	if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
15032661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated encryption");
15042661f4fbSPablo de Lara 	else
15052661f4fbSPablo de Lara 		strcpy(string_aead_op, "Authenticated decryption");
15062661f4fbSPablo de Lara 
15072661f4fbSPablo de Lara 
1508387259bdSDeclan Doherty 	printf("Options:-\nn");
1509387259bdSDeclan Doherty 	printf("portmask: %x\n", options->portmask);
1510387259bdSDeclan Doherty 	printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1511387259bdSDeclan Doherty 	printf("refresh period : %u\n", options->refresh_period);
1512387259bdSDeclan Doherty 	printf("single lcore mode: %s\n",
1513387259bdSDeclan Doherty 			options->single_lcore ? "enabled" : "disabled");
1514387259bdSDeclan Doherty 	printf("stats_printing: %s\n",
1515ad509b4aSDeclan Doherty 			options->refresh_period == 0 ? "disabled" : "enabled");
1516387259bdSDeclan Doherty 
1517387259bdSDeclan Doherty 	printf("sessionless crypto: %s\n",
1518387259bdSDeclan Doherty 			options->sessionless ? "enabled" : "disabled");
151941e97c2eSPablo de Lara 
152041e97c2eSPablo de Lara 	if (options->ckey_param && (options->ckey_random_size != -1))
152141e97c2eSPablo de Lara 		printf("Cipher key already parsed, ignoring size of random key\n");
152241e97c2eSPablo de Lara 
152341e97c2eSPablo de Lara 	if (options->akey_param && (options->akey_random_size != -1))
152441e97c2eSPablo de Lara 		printf("Auth key already parsed, ignoring size of random key\n");
152541e97c2eSPablo de Lara 
1526acf86169SPablo de Lara 	if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1527acf86169SPablo de Lara 		printf("Cipher IV already parsed, ignoring size of random IV\n");
1528acf86169SPablo de Lara 
1529acf86169SPablo de Lara 	if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1530acf86169SPablo de Lara 		printf("Auth IV already parsed, ignoring size of random IV\n");
153141e97c2eSPablo de Lara 
153241e97c2eSPablo de Lara 	if (options->aad_param && (options->aad_random_size != -1))
153341e97c2eSPablo de Lara 		printf("AAD already parsed, ignoring size of random AAD\n");
153441e97c2eSPablo de Lara 
153541e97c2eSPablo de Lara 	printf("\nCrypto chain: ");
153641e97c2eSPablo de Lara 	switch (options->xform_chain) {
15372661f4fbSPablo de Lara 	case L2FWD_CRYPTO_AEAD:
15382661f4fbSPablo de Lara 		printf("Input --> %s --> Output\n", string_aead_op);
15392661f4fbSPablo de Lara 		display_aead_info(options);
15402661f4fbSPablo de Lara 		break;
154141e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_HASH:
154241e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
154341e97c2eSPablo de Lara 			string_cipher_op, string_auth_op);
154441e97c2eSPablo de Lara 		display_cipher_info(options);
154541e97c2eSPablo de Lara 		display_auth_info(options);
154641e97c2eSPablo de Lara 		break;
154741e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_CIPHER:
154841e97c2eSPablo de Lara 		printf("Input --> %s --> %s --> Output\n",
154941e97c2eSPablo de Lara 			string_auth_op, string_cipher_op);
155041e97c2eSPablo de Lara 		display_cipher_info(options);
155141e97c2eSPablo de Lara 		display_auth_info(options);
155241e97c2eSPablo de Lara 		break;
155341e97c2eSPablo de Lara 	case L2FWD_CRYPTO_HASH_ONLY:
155441e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_auth_op);
155541e97c2eSPablo de Lara 		display_auth_info(options);
155641e97c2eSPablo de Lara 		break;
155741e97c2eSPablo de Lara 	case L2FWD_CRYPTO_CIPHER_ONLY:
155841e97c2eSPablo de Lara 		printf("Input --> %s --> Output\n", string_cipher_op);
155941e97c2eSPablo de Lara 		display_cipher_info(options);
156041e97c2eSPablo de Lara 		break;
156141e97c2eSPablo de Lara 	}
1562387259bdSDeclan Doherty }
1563387259bdSDeclan Doherty 
1564387259bdSDeclan Doherty /* Parse the argument given in the command line of the application */
1565387259bdSDeclan Doherty static int
1566387259bdSDeclan Doherty l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1567387259bdSDeclan Doherty 		int argc, char **argv)
1568387259bdSDeclan Doherty {
1569387259bdSDeclan Doherty 	int opt, retval, option_index;
1570387259bdSDeclan Doherty 	char **argvopt = argv, *prgname = argv[0];
1571387259bdSDeclan Doherty 
1572387259bdSDeclan Doherty 	static struct option lgopts[] = {
1573387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1574387259bdSDeclan Doherty 
1575387259bdSDeclan Doherty 			{ "cdev_type", required_argument, 0, 0 },
1576387259bdSDeclan Doherty 			{ "chain", required_argument, 0, 0 },
1577387259bdSDeclan Doherty 
1578387259bdSDeclan Doherty 			{ "cipher_algo", required_argument, 0, 0 },
1579387259bdSDeclan Doherty 			{ "cipher_op", required_argument, 0, 0 },
1580387259bdSDeclan Doherty 			{ "cipher_key", required_argument, 0, 0 },
1581a061e50aSPablo de Lara 			{ "cipher_key_random_size", required_argument, 0, 0 },
1582acf86169SPablo de Lara 			{ "cipher_iv", required_argument, 0, 0 },
1583acf86169SPablo de Lara 			{ "cipher_iv_random_size", required_argument, 0, 0 },
1584387259bdSDeclan Doherty 
1585387259bdSDeclan Doherty 			{ "auth_algo", required_argument, 0, 0 },
1586387259bdSDeclan Doherty 			{ "auth_op", required_argument, 0, 0 },
1587387259bdSDeclan Doherty 			{ "auth_key", required_argument, 0, 0 },
1588a061e50aSPablo de Lara 			{ "auth_key_random_size", required_argument, 0, 0 },
1589acf86169SPablo de Lara 			{ "auth_iv", required_argument, 0, 0 },
1590acf86169SPablo de Lara 			{ "auth_iv_random_size", required_argument, 0, 0 },
1591387259bdSDeclan Doherty 
15922661f4fbSPablo de Lara 			{ "aead_algo", required_argument, 0, 0 },
15932661f4fbSPablo de Lara 			{ "aead_op", required_argument, 0, 0 },
15942661f4fbSPablo de Lara 			{ "aead_key", required_argument, 0, 0 },
15952661f4fbSPablo de Lara 			{ "aead_key_random_size", required_argument, 0, 0 },
15962661f4fbSPablo de Lara 			{ "aead_iv", required_argument, 0, 0 },
15972661f4fbSPablo de Lara 			{ "aead_iv_random_size", required_argument, 0, 0 },
15982661f4fbSPablo de Lara 
1599617a7949SPablo de Lara 			{ "aad", required_argument, 0, 0 },
1600a061e50aSPablo de Lara 			{ "aad_random_size", required_argument, 0, 0 },
16012661f4fbSPablo de Lara 
1602a061e50aSPablo de Lara 			{ "digest_size", required_argument, 0, 0 },
1603387259bdSDeclan Doherty 
1604387259bdSDeclan Doherty 			{ "sessionless", no_argument, 0, 0 },
1605d2797f51SFan Zhang 			{ "cryptodev_mask", required_argument, 0, 0},
16063c96262cSPablo de Lara 
1607387259bdSDeclan Doherty 			{ NULL, 0, 0, 0 }
1608387259bdSDeclan Doherty 	};
1609387259bdSDeclan Doherty 
1610387259bdSDeclan Doherty 	l2fwd_crypto_default_options(options);
1611387259bdSDeclan Doherty 
1612f1ae15baSPablo de Lara 	while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1613387259bdSDeclan Doherty 			&option_index)) != EOF) {
1614387259bdSDeclan Doherty 		switch (opt) {
1615387259bdSDeclan Doherty 		/* long options */
1616387259bdSDeclan Doherty 		case 0:
1617387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_args_long_options(options,
1618387259bdSDeclan Doherty 					lgopts, option_index);
1619387259bdSDeclan Doherty 			if (retval < 0) {
1620387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1621387259bdSDeclan Doherty 				return -1;
1622387259bdSDeclan Doherty 			}
1623387259bdSDeclan Doherty 			break;
1624387259bdSDeclan Doherty 
1625387259bdSDeclan Doherty 		/* portmask */
1626387259bdSDeclan Doherty 		case 'p':
1627387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_portmask(options, optarg);
1628387259bdSDeclan Doherty 			if (retval < 0) {
1629387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1630387259bdSDeclan Doherty 				return -1;
1631387259bdSDeclan Doherty 			}
1632387259bdSDeclan Doherty 			break;
1633387259bdSDeclan Doherty 
1634387259bdSDeclan Doherty 		/* nqueue */
1635387259bdSDeclan Doherty 		case 'q':
1636387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_nqueue(options, optarg);
1637387259bdSDeclan Doherty 			if (retval < 0) {
1638387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1639387259bdSDeclan Doherty 				return -1;
1640387259bdSDeclan Doherty 			}
1641387259bdSDeclan Doherty 			break;
1642387259bdSDeclan Doherty 
1643387259bdSDeclan Doherty 		/* single  */
1644387259bdSDeclan Doherty 		case 's':
1645387259bdSDeclan Doherty 			options->single_lcore = 1;
1646387259bdSDeclan Doherty 
1647387259bdSDeclan Doherty 			break;
1648387259bdSDeclan Doherty 
1649387259bdSDeclan Doherty 		/* timer period */
1650a3380989SPablo de Lara 		case 'T':
1651387259bdSDeclan Doherty 			retval = l2fwd_crypto_parse_timer_period(options,
1652387259bdSDeclan Doherty 					optarg);
1653387259bdSDeclan Doherty 			if (retval < 0) {
1654387259bdSDeclan Doherty 				l2fwd_crypto_usage(prgname);
1655387259bdSDeclan Doherty 				return -1;
1656387259bdSDeclan Doherty 			}
1657387259bdSDeclan Doherty 			break;
1658387259bdSDeclan Doherty 
1659387259bdSDeclan Doherty 		default:
1660387259bdSDeclan Doherty 			l2fwd_crypto_usage(prgname);
1661387259bdSDeclan Doherty 			return -1;
1662387259bdSDeclan Doherty 		}
1663387259bdSDeclan Doherty 	}
1664387259bdSDeclan Doherty 
1665387259bdSDeclan Doherty 
1666387259bdSDeclan Doherty 	if (optind >= 0)
1667387259bdSDeclan Doherty 		argv[optind-1] = prgname;
1668387259bdSDeclan Doherty 
1669387259bdSDeclan Doherty 	retval = optind-1;
16709d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
1671387259bdSDeclan Doherty 
1672387259bdSDeclan Doherty 	return retval;
1673387259bdSDeclan Doherty }
1674387259bdSDeclan Doherty 
1675387259bdSDeclan Doherty /* Check the link status of all ports in up to 9s, and print them finally */
1676387259bdSDeclan Doherty static void
1677387259bdSDeclan Doherty check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1678387259bdSDeclan Doherty {
1679387259bdSDeclan Doherty #define CHECK_INTERVAL 100 /* 100ms */
1680387259bdSDeclan Doherty #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1681387259bdSDeclan Doherty 	uint8_t portid, count, all_ports_up, print_flag = 0;
1682387259bdSDeclan Doherty 	struct rte_eth_link link;
1683387259bdSDeclan Doherty 
1684387259bdSDeclan Doherty 	printf("\nChecking link status");
1685387259bdSDeclan Doherty 	fflush(stdout);
1686387259bdSDeclan Doherty 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1687387259bdSDeclan Doherty 		all_ports_up = 1;
1688387259bdSDeclan Doherty 		for (portid = 0; portid < port_num; portid++) {
1689387259bdSDeclan Doherty 			if ((port_mask & (1 << portid)) == 0)
1690387259bdSDeclan Doherty 				continue;
1691387259bdSDeclan Doherty 			memset(&link, 0, sizeof(link));
1692387259bdSDeclan Doherty 			rte_eth_link_get_nowait(portid, &link);
1693387259bdSDeclan Doherty 			/* print link status if flag set */
1694387259bdSDeclan Doherty 			if (print_flag == 1) {
1695387259bdSDeclan Doherty 				if (link.link_status)
1696387259bdSDeclan Doherty 					printf("Port %d Link Up - speed %u "
1697387259bdSDeclan Doherty 						"Mbps - %s\n", (uint8_t)portid,
1698387259bdSDeclan Doherty 						(unsigned)link.link_speed,
1699387259bdSDeclan Doherty 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1700387259bdSDeclan Doherty 					("full-duplex") : ("half-duplex\n"));
1701387259bdSDeclan Doherty 				else
1702387259bdSDeclan Doherty 					printf("Port %d Link Down\n",
1703387259bdSDeclan Doherty 						(uint8_t)portid);
1704387259bdSDeclan Doherty 				continue;
1705387259bdSDeclan Doherty 			}
1706387259bdSDeclan Doherty 			/* clear all_ports_up flag if any link down */
170709419f23SThomas Monjalon 			if (link.link_status == ETH_LINK_DOWN) {
1708387259bdSDeclan Doherty 				all_ports_up = 0;
1709387259bdSDeclan Doherty 				break;
1710387259bdSDeclan Doherty 			}
1711387259bdSDeclan Doherty 		}
1712387259bdSDeclan Doherty 		/* after finally printing all link status, get out */
1713387259bdSDeclan Doherty 		if (print_flag == 1)
1714387259bdSDeclan Doherty 			break;
1715387259bdSDeclan Doherty 
1716387259bdSDeclan Doherty 		if (all_ports_up == 0) {
1717387259bdSDeclan Doherty 			printf(".");
1718387259bdSDeclan Doherty 			fflush(stdout);
1719387259bdSDeclan Doherty 			rte_delay_ms(CHECK_INTERVAL);
1720387259bdSDeclan Doherty 		}
1721387259bdSDeclan Doherty 
1722387259bdSDeclan Doherty 		/* set the print_flag if all ports up or timeout */
1723387259bdSDeclan Doherty 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1724387259bdSDeclan Doherty 			print_flag = 1;
1725387259bdSDeclan Doherty 			printf("done\n");
1726387259bdSDeclan Doherty 		}
1727387259bdSDeclan Doherty 	}
1728387259bdSDeclan Doherty }
1729387259bdSDeclan Doherty 
173027cf2d1bSPablo de Lara /* Check if device has to be HW/SW or any */
1731387259bdSDeclan Doherty static int
17324baea68dSPablo de Lara check_type(const struct l2fwd_crypto_options *options,
17334baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info)
173427cf2d1bSPablo de Lara {
173527cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_HW &&
173627cf2d1bSPablo de Lara 			(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
173727cf2d1bSPablo de Lara 		return 0;
173827cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_SW &&
173927cf2d1bSPablo de Lara 			!(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
174027cf2d1bSPablo de Lara 		return 0;
174127cf2d1bSPablo de Lara 	if (options->type == CDEV_TYPE_ANY)
174227cf2d1bSPablo de Lara 		return 0;
174327cf2d1bSPablo de Lara 
174427cf2d1bSPablo de Lara 	return -1;
174527cf2d1bSPablo de Lara }
174627cf2d1bSPablo de Lara 
17474baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
17484baea68dSPablo de Lara check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
17494baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
17504baea68dSPablo de Lara 		uint8_t cdev_id)
17514baea68dSPablo de Lara {
17524baea68dSPablo de Lara 	unsigned int i = 0;
17534baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
17544baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm cap_cipher_algo;
17554baea68dSPablo de Lara 	enum rte_crypto_cipher_algorithm opt_cipher_algo =
17564baea68dSPablo de Lara 					options->cipher_xform.cipher.algo;
17574baea68dSPablo de Lara 
17584baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
17594baea68dSPablo de Lara 		cap_cipher_algo = cap->sym.cipher.algo;
17604baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
17614baea68dSPablo de Lara 			if (cap_cipher_algo == opt_cipher_algo) {
17624baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
17634baea68dSPablo de Lara 					break;
17644baea68dSPablo de Lara 			}
17654baea68dSPablo de Lara 		}
17664baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
17674baea68dSPablo de Lara 	}
17684baea68dSPablo de Lara 
17694baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
17704baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
17714baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
17724baea68dSPablo de Lara 			rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
17734baea68dSPablo de Lara 			cdev_id,
17744baea68dSPablo de Lara 			options->string_type);
17754baea68dSPablo de Lara 		return NULL;
17764baea68dSPablo de Lara 	}
17774baea68dSPablo de Lara 
17784baea68dSPablo de Lara 	return cap;
17794baea68dSPablo de Lara }
17804baea68dSPablo de Lara 
17814baea68dSPablo de Lara static const struct rte_cryptodev_capabilities *
17824baea68dSPablo de Lara check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
17834baea68dSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
17844baea68dSPablo de Lara 		uint8_t cdev_id)
17854baea68dSPablo de Lara {
17864baea68dSPablo de Lara 	unsigned int i = 0;
17874baea68dSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
17884baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm cap_auth_algo;
17894baea68dSPablo de Lara 	enum rte_crypto_auth_algorithm opt_auth_algo =
17904baea68dSPablo de Lara 					options->auth_xform.auth.algo;
17914baea68dSPablo de Lara 
17924baea68dSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
17934baea68dSPablo de Lara 		cap_auth_algo = cap->sym.auth.algo;
17944baea68dSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
17954baea68dSPablo de Lara 			if (cap_auth_algo == opt_auth_algo) {
17964baea68dSPablo de Lara 				if (check_type(options, dev_info) == 0)
17974baea68dSPablo de Lara 					break;
17984baea68dSPablo de Lara 			}
17994baea68dSPablo de Lara 		}
18004baea68dSPablo de Lara 		cap = &dev_info->capabilities[++i];
18014baea68dSPablo de Lara 	}
18024baea68dSPablo de Lara 
18034baea68dSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18044baea68dSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18054baea68dSPablo de Lara 			" or device not of preferred type (%s)\n",
18064baea68dSPablo de Lara 			rte_crypto_auth_algorithm_strings[opt_auth_algo],
18074baea68dSPablo de Lara 			cdev_id,
18084baea68dSPablo de Lara 			options->string_type);
18094baea68dSPablo de Lara 		return NULL;
18104baea68dSPablo de Lara 	}
18114baea68dSPablo de Lara 
18124baea68dSPablo de Lara 	return cap;
18134baea68dSPablo de Lara }
18144baea68dSPablo de Lara 
18152661f4fbSPablo de Lara static const struct rte_cryptodev_capabilities *
18162661f4fbSPablo de Lara check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
18172661f4fbSPablo de Lara 		const struct rte_cryptodev_info *dev_info,
18182661f4fbSPablo de Lara 		uint8_t cdev_id)
18192661f4fbSPablo de Lara {
18202661f4fbSPablo de Lara 	unsigned int i = 0;
18212661f4fbSPablo de Lara 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
18222661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm cap_aead_algo;
18232661f4fbSPablo de Lara 	enum rte_crypto_aead_algorithm opt_aead_algo =
18242661f4fbSPablo de Lara 					options->aead_xform.aead.algo;
18252661f4fbSPablo de Lara 
18262661f4fbSPablo de Lara 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18272661f4fbSPablo de Lara 		cap_aead_algo = cap->sym.aead.algo;
18282661f4fbSPablo de Lara 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
18292661f4fbSPablo de Lara 			if (cap_aead_algo == opt_aead_algo) {
18302661f4fbSPablo de Lara 				if (check_type(options, dev_info) == 0)
18312661f4fbSPablo de Lara 					break;
18322661f4fbSPablo de Lara 			}
18332661f4fbSPablo de Lara 		}
18342661f4fbSPablo de Lara 		cap = &dev_info->capabilities[++i];
18352661f4fbSPablo de Lara 	}
18362661f4fbSPablo de Lara 
18372661f4fbSPablo de Lara 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
18382661f4fbSPablo de Lara 		printf("Algorithm %s not supported by cryptodev %u"
18392661f4fbSPablo de Lara 			" or device not of preferred type (%s)\n",
18402661f4fbSPablo de Lara 			rte_crypto_aead_algorithm_strings[opt_aead_algo],
18412661f4fbSPablo de Lara 			cdev_id,
18422661f4fbSPablo de Lara 			options->string_type);
18432661f4fbSPablo de Lara 		return NULL;
18442661f4fbSPablo de Lara 	}
18452661f4fbSPablo de Lara 
18462661f4fbSPablo de Lara 	return cap;
18472661f4fbSPablo de Lara }
18482661f4fbSPablo de Lara 
1849d2797f51SFan Zhang /* Check if the device is enabled by cryptodev_mask */
1850d2797f51SFan Zhang static int
1851d2797f51SFan Zhang check_cryptodev_mask(struct l2fwd_crypto_options *options,
1852d2797f51SFan Zhang 		uint8_t cdev_id)
1853d2797f51SFan Zhang {
1854d2797f51SFan Zhang 	if (options->cryptodev_mask & (1 << cdev_id))
1855d2797f51SFan Zhang 		return 0;
1856d2797f51SFan Zhang 
1857d2797f51SFan Zhang 	return -1;
1858d2797f51SFan Zhang }
1859d2797f51SFan Zhang 
1860a061e50aSPablo de Lara static inline int
1861a061e50aSPablo de Lara check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1862a061e50aSPablo de Lara 		uint16_t increment)
1863a061e50aSPablo de Lara {
1864a061e50aSPablo de Lara 	uint16_t supp_size;
1865a061e50aSPablo de Lara 
186637ebd9e1SPablo de Lara 	/* Single value */
186737ebd9e1SPablo de Lara 	if (increment == 0) {
186837ebd9e1SPablo de Lara 		if (length == min)
186937ebd9e1SPablo de Lara 			return 0;
187037ebd9e1SPablo de Lara 		else
187137ebd9e1SPablo de Lara 			return -1;
187237ebd9e1SPablo de Lara 	}
187337ebd9e1SPablo de Lara 
187437ebd9e1SPablo de Lara 	/* Range of values */
1875a061e50aSPablo de Lara 	for (supp_size = min; supp_size <= max; supp_size += increment) {
1876a061e50aSPablo de Lara 		if (length == supp_size)
1877a061e50aSPablo de Lara 			return 0;
1878a061e50aSPablo de Lara 	}
1879a061e50aSPablo de Lara 
1880a061e50aSPablo de Lara 	return -1;
1881a061e50aSPablo de Lara }
18820fbd75a9SPablo de Lara 
18830fbd75a9SPablo de Lara static int
18840fbd75a9SPablo de Lara check_iv_param(const struct rte_crypto_param_range *iv_range_size,
18850fbd75a9SPablo de Lara 		unsigned int iv_param, int iv_random_size,
18860fbd75a9SPablo de Lara 		uint16_t *iv_length)
18870fbd75a9SPablo de Lara {
18880fbd75a9SPablo de Lara 	/*
18890fbd75a9SPablo de Lara 	 * Check if length of provided IV is supported
18900fbd75a9SPablo de Lara 	 * by the algorithm chosen.
18910fbd75a9SPablo de Lara 	 */
18920fbd75a9SPablo de Lara 	if (iv_param) {
18930fbd75a9SPablo de Lara 		if (check_supported_size(*iv_length,
18940fbd75a9SPablo de Lara 				iv_range_size->min,
18950fbd75a9SPablo de Lara 				iv_range_size->max,
18960fbd75a9SPablo de Lara 				iv_range_size->increment)
18970fbd75a9SPablo de Lara 					!= 0) {
18980fbd75a9SPablo de Lara 			printf("Unsupported IV length\n");
18990fbd75a9SPablo de Lara 			return -1;
19000fbd75a9SPablo de Lara 		}
19010fbd75a9SPablo de Lara 	/*
19020fbd75a9SPablo de Lara 	 * Check if length of IV to be randomly generated
19030fbd75a9SPablo de Lara 	 * is supported by the algorithm chosen.
19040fbd75a9SPablo de Lara 	 */
19050fbd75a9SPablo de Lara 	} else if (iv_random_size != -1) {
19060fbd75a9SPablo de Lara 		if (check_supported_size(iv_random_size,
19070fbd75a9SPablo de Lara 				iv_range_size->min,
19080fbd75a9SPablo de Lara 				iv_range_size->max,
19090fbd75a9SPablo de Lara 				iv_range_size->increment)
19100fbd75a9SPablo de Lara 					!= 0) {
19110fbd75a9SPablo de Lara 			printf("Unsupported IV length\n");
19120fbd75a9SPablo de Lara 			return -1;
19130fbd75a9SPablo de Lara 		}
19140fbd75a9SPablo de Lara 		*iv_length = iv_random_size;
19150fbd75a9SPablo de Lara 	/* No size provided, use minimum size. */
19160fbd75a9SPablo de Lara 	} else
19170fbd75a9SPablo de Lara 		*iv_length = iv_range_size->min;
19180fbd75a9SPablo de Lara 
19190fbd75a9SPablo de Lara 	return 0;
19200fbd75a9SPablo de Lara }
19210fbd75a9SPablo de Lara 
192227cf2d1bSPablo de Lara static int
192327cf2d1bSPablo de Lara initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
192427cf2d1bSPablo de Lara 		uint8_t *enabled_cdevs)
1925387259bdSDeclan Doherty {
19264baea68dSPablo de Lara 	unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
192727cf2d1bSPablo de Lara 	const struct rte_cryptodev_capabilities *cap;
1928*2c59bd32SSlawomir Mrozowicz 	unsigned int sess_sz, max_sess_sz = 0;
1929387259bdSDeclan Doherty 	int retval;
1930387259bdSDeclan Doherty 
193127cf2d1bSPablo de Lara 	cdev_count = rte_cryptodev_count();
193227cf2d1bSPablo de Lara 	if (cdev_count == 0) {
193327cf2d1bSPablo de Lara 		printf("No crypto devices available\n");
1934387259bdSDeclan Doherty 		return -1;
1935387259bdSDeclan Doherty 	}
1936387259bdSDeclan Doherty 
1937*2c59bd32SSlawomir Mrozowicz 	for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
1938*2c59bd32SSlawomir Mrozowicz 		sess_sz = sizeof(struct rte_cryptodev_sym_session) +
1939*2c59bd32SSlawomir Mrozowicz 			rte_cryptodev_get_private_session_size(cdev_id);
1940*2c59bd32SSlawomir Mrozowicz 		if (sess_sz > max_sess_sz)
1941*2c59bd32SSlawomir Mrozowicz 			max_sess_sz = sess_sz;
1942*2c59bd32SSlawomir Mrozowicz 	}
1943*2c59bd32SSlawomir Mrozowicz 
194427cf2d1bSPablo de Lara 	for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1945387259bdSDeclan Doherty 			cdev_id++) {
1946387259bdSDeclan Doherty 		struct rte_cryptodev_qp_conf qp_conf;
1947387259bdSDeclan Doherty 		struct rte_cryptodev_info dev_info;
1948*2c59bd32SSlawomir Mrozowicz 		uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
1949387259bdSDeclan Doherty 
1950387259bdSDeclan Doherty 		struct rte_cryptodev_config conf = {
1951387259bdSDeclan Doherty 			.nb_queue_pairs = 1,
1952*2c59bd32SSlawomir Mrozowicz 			.socket_id = socket_id,
1953387259bdSDeclan Doherty 		};
1954387259bdSDeclan Doherty 
1955d2797f51SFan Zhang 		if (check_cryptodev_mask(options, (uint8_t)cdev_id))
1956d2797f51SFan Zhang 			continue;
1957d2797f51SFan Zhang 
1958387259bdSDeclan Doherty 		rte_cryptodev_info_get(cdev_id, &dev_info);
1959387259bdSDeclan Doherty 
1960*2c59bd32SSlawomir Mrozowicz 		if (session_pool_socket[socket_id] == NULL) {
1961*2c59bd32SSlawomir Mrozowicz 			char mp_name[RTE_MEMPOOL_NAMESIZE];
1962*2c59bd32SSlawomir Mrozowicz 			struct rte_mempool *sess_mp;
1963*2c59bd32SSlawomir Mrozowicz 
1964*2c59bd32SSlawomir Mrozowicz 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1965*2c59bd32SSlawomir Mrozowicz 				"sess_mp_%u", socket_id);
1966*2c59bd32SSlawomir Mrozowicz 
1967*2c59bd32SSlawomir Mrozowicz 			/*
1968*2c59bd32SSlawomir Mrozowicz 			 * Create enough objects for session headers and
1969*2c59bd32SSlawomir Mrozowicz 			 * device private data
1970*2c59bd32SSlawomir Mrozowicz 			 */
1971*2c59bd32SSlawomir Mrozowicz 			sess_mp = rte_mempool_create(mp_name,
1972*2c59bd32SSlawomir Mrozowicz 						MAX_SESSIONS * 2,
1973*2c59bd32SSlawomir Mrozowicz 						max_sess_sz,
1974*2c59bd32SSlawomir Mrozowicz 						SESSION_POOL_CACHE_SIZE,
1975*2c59bd32SSlawomir Mrozowicz 						0, NULL, NULL, NULL,
1976*2c59bd32SSlawomir Mrozowicz 						NULL, socket_id,
1977*2c59bd32SSlawomir Mrozowicz 						0);
1978*2c59bd32SSlawomir Mrozowicz 
1979*2c59bd32SSlawomir Mrozowicz 			if (sess_mp == NULL) {
1980*2c59bd32SSlawomir Mrozowicz 				printf("Cannot create session pool on socket %d\n",
1981*2c59bd32SSlawomir Mrozowicz 					socket_id);
1982*2c59bd32SSlawomir Mrozowicz 				return -ENOMEM;
1983*2c59bd32SSlawomir Mrozowicz 			}
1984*2c59bd32SSlawomir Mrozowicz 
1985*2c59bd32SSlawomir Mrozowicz 			printf("Allocated session pool on socket %d\n", socket_id);
1986*2c59bd32SSlawomir Mrozowicz 			session_pool_socket[socket_id] = sess_mp;
1987*2c59bd32SSlawomir Mrozowicz 		}
1988*2c59bd32SSlawomir Mrozowicz 
19892661f4fbSPablo de Lara 		/* Set AEAD parameters */
19902661f4fbSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
19912661f4fbSPablo de Lara 			/* Check if device supports AEAD algo */
19922661f4fbSPablo de Lara 			cap = check_device_support_aead_algo(options, &dev_info,
19932661f4fbSPablo de Lara 							cdev_id);
19942661f4fbSPablo de Lara 			if (cap == NULL)
19952661f4fbSPablo de Lara 				continue;
19962661f4fbSPablo de Lara 
19972661f4fbSPablo de Lara 			options->block_size = cap->sym.aead.block_size;
19982661f4fbSPablo de Lara 
19992661f4fbSPablo de Lara 			check_iv_param(&cap->sym.aead.iv_size,
20002661f4fbSPablo de Lara 					options->aead_iv_param,
20012661f4fbSPablo de Lara 					options->aead_iv_random_size,
20022661f4fbSPablo de Lara 					&options->aead_iv.length);
20032661f4fbSPablo de Lara 
20042661f4fbSPablo de Lara 			/*
20052661f4fbSPablo de Lara 			 * Check if length of provided AEAD key is supported
20062661f4fbSPablo de Lara 			 * by the algorithm chosen.
20072661f4fbSPablo de Lara 			 */
20082661f4fbSPablo de Lara 			if (options->aead_key_param) {
20092661f4fbSPablo de Lara 				if (check_supported_size(
20102661f4fbSPablo de Lara 						options->aead_xform.aead.key.length,
20112661f4fbSPablo de Lara 						cap->sym.aead.key_size.min,
20122661f4fbSPablo de Lara 						cap->sym.aead.key_size.max,
20132661f4fbSPablo de Lara 						cap->sym.aead.key_size.increment)
20142661f4fbSPablo de Lara 							!= 0) {
20152661f4fbSPablo de Lara 					printf("Unsupported aead key length\n");
20162661f4fbSPablo de Lara 					return -1;
20172661f4fbSPablo de Lara 				}
20182661f4fbSPablo de Lara 			/*
20192661f4fbSPablo de Lara 			 * Check if length of the aead key to be randomly generated
20202661f4fbSPablo de Lara 			 * is supported by the algorithm chosen.
20212661f4fbSPablo de Lara 			 */
20222661f4fbSPablo de Lara 			} else if (options->aead_key_random_size != -1) {
20232661f4fbSPablo de Lara 				if (check_supported_size(options->ckey_random_size,
20242661f4fbSPablo de Lara 						cap->sym.aead.key_size.min,
20252661f4fbSPablo de Lara 						cap->sym.aead.key_size.max,
20262661f4fbSPablo de Lara 						cap->sym.aead.key_size.increment)
20272661f4fbSPablo de Lara 							!= 0) {
20282661f4fbSPablo de Lara 					printf("Unsupported aead key length\n");
20292661f4fbSPablo de Lara 					return -1;
20302661f4fbSPablo de Lara 				}
20312661f4fbSPablo de Lara 				options->aead_xform.aead.key.length =
20322661f4fbSPablo de Lara 							options->ckey_random_size;
20332661f4fbSPablo de Lara 			/* No size provided, use minimum size. */
20342661f4fbSPablo de Lara 			} else
20352661f4fbSPablo de Lara 				options->aead_xform.aead.key.length =
20362661f4fbSPablo de Lara 						cap->sym.aead.key_size.min;
20372661f4fbSPablo de Lara 
20382661f4fbSPablo de Lara 			if (!options->aead_key_param)
20392661f4fbSPablo de Lara 				generate_random_key(
20402661f4fbSPablo de Lara 					options->aead_xform.aead.key.data,
20412661f4fbSPablo de Lara 					options->aead_xform.aead.key.length);
20422661f4fbSPablo de Lara 
20432661f4fbSPablo de Lara 			/*
20442661f4fbSPablo de Lara 			 * Check if length of provided AAD is supported
20452661f4fbSPablo de Lara 			 * by the algorithm chosen.
20462661f4fbSPablo de Lara 			 */
20472661f4fbSPablo de Lara 			if (options->aad_param) {
20482661f4fbSPablo de Lara 				if (check_supported_size(options->aad.length,
20492661f4fbSPablo de Lara 						cap->sym.aead.aad_size.min,
20502661f4fbSPablo de Lara 						cap->sym.aead.aad_size.max,
20512661f4fbSPablo de Lara 						cap->sym.aead.aad_size.increment)
20522661f4fbSPablo de Lara 							!= 0) {
20532661f4fbSPablo de Lara 					printf("Unsupported AAD length\n");
20542661f4fbSPablo de Lara 					return -1;
20552661f4fbSPablo de Lara 				}
20562661f4fbSPablo de Lara 			/*
20572661f4fbSPablo de Lara 			 * Check if length of AAD to be randomly generated
20582661f4fbSPablo de Lara 			 * is supported by the algorithm chosen.
20592661f4fbSPablo de Lara 			 */
20602661f4fbSPablo de Lara 			} else if (options->aad_random_size != -1) {
20612661f4fbSPablo de Lara 				if (check_supported_size(options->aad_random_size,
20622661f4fbSPablo de Lara 						cap->sym.aead.aad_size.min,
20632661f4fbSPablo de Lara 						cap->sym.aead.aad_size.max,
20642661f4fbSPablo de Lara 						cap->sym.aead.aad_size.increment)
20652661f4fbSPablo de Lara 							!= 0) {
20662661f4fbSPablo de Lara 					printf("Unsupported AAD length\n");
20672661f4fbSPablo de Lara 					return -1;
20682661f4fbSPablo de Lara 				}
20692661f4fbSPablo de Lara 				options->aad.length = options->aad_random_size;
20702661f4fbSPablo de Lara 			/* No size provided, use minimum size. */
20712661f4fbSPablo de Lara 			} else
20722661f4fbSPablo de Lara 				options->aad.length = cap->sym.auth.aad_size.min;
20732661f4fbSPablo de Lara 
20742661f4fbSPablo de Lara 			options->aead_xform.aead.add_auth_data_length =
20752661f4fbSPablo de Lara 						options->aad.length;
20762661f4fbSPablo de Lara 
20772661f4fbSPablo de Lara 			/* Check if digest size is supported by the algorithm. */
20782661f4fbSPablo de Lara 			if (options->digest_size != -1) {
20792661f4fbSPablo de Lara 				if (check_supported_size(options->digest_size,
20802661f4fbSPablo de Lara 						cap->sym.aead.digest_size.min,
20812661f4fbSPablo de Lara 						cap->sym.aead.digest_size.max,
20822661f4fbSPablo de Lara 						cap->sym.aead.digest_size.increment)
20832661f4fbSPablo de Lara 							!= 0) {
20842661f4fbSPablo de Lara 					printf("Unsupported digest length\n");
20852661f4fbSPablo de Lara 					return -1;
20862661f4fbSPablo de Lara 				}
20872661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
20882661f4fbSPablo de Lara 							options->digest_size;
20892661f4fbSPablo de Lara 			/* No size provided, use minimum size. */
20902661f4fbSPablo de Lara 			} else
20912661f4fbSPablo de Lara 				options->aead_xform.aead.digest_length =
20922661f4fbSPablo de Lara 						cap->sym.aead.digest_size.min;
20932661f4fbSPablo de Lara 		}
20942661f4fbSPablo de Lara 
209527cf2d1bSPablo de Lara 		/* Set cipher parameters */
209627cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
209727cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
209827cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
209927cf2d1bSPablo de Lara 			/* Check if device supports cipher algo */
21004baea68dSPablo de Lara 			cap = check_device_support_cipher_algo(options, &dev_info,
21014baea68dSPablo de Lara 							cdev_id);
21024baea68dSPablo de Lara 			if (cap == NULL)
210327cf2d1bSPablo de Lara 				continue;
210427cf2d1bSPablo de Lara 
210527cf2d1bSPablo de Lara 			options->block_size = cap->sym.cipher.block_size;
21060fbd75a9SPablo de Lara 
2107acf86169SPablo de Lara 			check_iv_param(&cap->sym.cipher.iv_size,
2108acf86169SPablo de Lara 					options->cipher_iv_param,
2109acf86169SPablo de Lara 					options->cipher_iv_random_size,
2110acf86169SPablo de Lara 					&options->cipher_iv.length);
2111a061e50aSPablo de Lara 
2112a061e50aSPablo de Lara 			/*
2113a061e50aSPablo de Lara 			 * Check if length of provided cipher key is supported
2114a061e50aSPablo de Lara 			 * by the algorithm chosen.
2115a061e50aSPablo de Lara 			 */
2116a061e50aSPablo de Lara 			if (options->ckey_param) {
2117a061e50aSPablo de Lara 				if (check_supported_size(
2118a061e50aSPablo de Lara 						options->cipher_xform.cipher.key.length,
2119a061e50aSPablo de Lara 						cap->sym.cipher.key_size.min,
2120a061e50aSPablo de Lara 						cap->sym.cipher.key_size.max,
2121a061e50aSPablo de Lara 						cap->sym.cipher.key_size.increment)
2122a061e50aSPablo de Lara 							!= 0) {
2123a061e50aSPablo de Lara 					printf("Unsupported cipher key length\n");
2124a061e50aSPablo de Lara 					return -1;
2125a061e50aSPablo de Lara 				}
2126a061e50aSPablo de Lara 			/*
2127a061e50aSPablo de Lara 			 * Check if length of the cipher key to be randomly generated
2128a061e50aSPablo de Lara 			 * is supported by the algorithm chosen.
2129a061e50aSPablo de Lara 			 */
2130a061e50aSPablo de Lara 			} else if (options->ckey_random_size != -1) {
2131a061e50aSPablo de Lara 				if (check_supported_size(options->ckey_random_size,
2132a061e50aSPablo de Lara 						cap->sym.cipher.key_size.min,
2133a061e50aSPablo de Lara 						cap->sym.cipher.key_size.max,
2134a061e50aSPablo de Lara 						cap->sym.cipher.key_size.increment)
2135a061e50aSPablo de Lara 							!= 0) {
2136a061e50aSPablo de Lara 					printf("Unsupported cipher key length\n");
2137a061e50aSPablo de Lara 					return -1;
2138a061e50aSPablo de Lara 				}
2139a061e50aSPablo de Lara 				options->cipher_xform.cipher.key.length =
2140a061e50aSPablo de Lara 							options->ckey_random_size;
2141a061e50aSPablo de Lara 			/* No size provided, use minimum size. */
2142a061e50aSPablo de Lara 			} else
214327cf2d1bSPablo de Lara 				options->cipher_xform.cipher.key.length =
214427cf2d1bSPablo de Lara 						cap->sym.cipher.key_size.min;
2145a061e50aSPablo de Lara 
214627cf2d1bSPablo de Lara 			if (!options->ckey_param)
214727cf2d1bSPablo de Lara 				generate_random_key(
214827cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.data,
214927cf2d1bSPablo de Lara 					options->cipher_xform.cipher.key.length);
215027cf2d1bSPablo de Lara 
215127cf2d1bSPablo de Lara 		}
215227cf2d1bSPablo de Lara 
215327cf2d1bSPablo de Lara 		/* Set auth parameters */
215427cf2d1bSPablo de Lara 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
215527cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
215627cf2d1bSPablo de Lara 				options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
215727cf2d1bSPablo de Lara 			/* Check if device supports auth algo */
21584baea68dSPablo de Lara 			cap = check_device_support_auth_algo(options, &dev_info,
21594baea68dSPablo de Lara 							cdev_id);
21604baea68dSPablo de Lara 			if (cap == NULL)
216127cf2d1bSPablo de Lara 				continue;
216227cf2d1bSPablo de Lara 
2163acf86169SPablo de Lara 			check_iv_param(&cap->sym.auth.iv_size,
2164acf86169SPablo de Lara 					options->auth_iv_param,
2165acf86169SPablo de Lara 					options->auth_iv_random_size,
2166acf86169SPablo de Lara 					&options->auth_iv.length);
2167a061e50aSPablo de Lara 			/*
2168a061e50aSPablo de Lara 			 * Check if length of provided auth key is supported
2169a061e50aSPablo de Lara 			 * by the algorithm chosen.
2170a061e50aSPablo de Lara 			 */
2171a061e50aSPablo de Lara 			if (options->akey_param) {
2172a061e50aSPablo de Lara 				if (check_supported_size(
2173a061e50aSPablo de Lara 						options->auth_xform.auth.key.length,
2174a061e50aSPablo de Lara 						cap->sym.auth.key_size.min,
2175a061e50aSPablo de Lara 						cap->sym.auth.key_size.max,
2176a061e50aSPablo de Lara 						cap->sym.auth.key_size.increment)
2177a061e50aSPablo de Lara 							!= 0) {
2178a061e50aSPablo de Lara 					printf("Unsupported auth key length\n");
2179a061e50aSPablo de Lara 					return -1;
2180a061e50aSPablo de Lara 				}
2181a061e50aSPablo de Lara 			/*
2182a061e50aSPablo de Lara 			 * Check if length of the auth key to be randomly generated
2183a061e50aSPablo de Lara 			 * is supported by the algorithm chosen.
2184a061e50aSPablo de Lara 			 */
2185a061e50aSPablo de Lara 			} else if (options->akey_random_size != -1) {
2186a061e50aSPablo de Lara 				if (check_supported_size(options->akey_random_size,
2187a061e50aSPablo de Lara 						cap->sym.auth.key_size.min,
2188a061e50aSPablo de Lara 						cap->sym.auth.key_size.max,
2189a061e50aSPablo de Lara 						cap->sym.auth.key_size.increment)
2190a061e50aSPablo de Lara 							!= 0) {
2191a061e50aSPablo de Lara 					printf("Unsupported auth key length\n");
2192a061e50aSPablo de Lara 					return -1;
2193a061e50aSPablo de Lara 				}
2194a061e50aSPablo de Lara 				options->auth_xform.auth.key.length =
2195a061e50aSPablo de Lara 							options->akey_random_size;
2196a061e50aSPablo de Lara 			/* No size provided, use minimum size. */
2197a061e50aSPablo de Lara 			} else
219827cf2d1bSPablo de Lara 				options->auth_xform.auth.key.length =
219927cf2d1bSPablo de Lara 						cap->sym.auth.key_size.min;
220027cf2d1bSPablo de Lara 
220127cf2d1bSPablo de Lara 			if (!options->akey_param)
220227cf2d1bSPablo de Lara 				generate_random_key(
220327cf2d1bSPablo de Lara 					options->auth_xform.auth.key.data,
220427cf2d1bSPablo de Lara 					options->auth_xform.auth.key.length);
2205a061e50aSPablo de Lara 
2206a061e50aSPablo de Lara 			/* Check if digest size is supported by the algorithm. */
2207a061e50aSPablo de Lara 			if (options->digest_size != -1) {
2208a061e50aSPablo de Lara 				if (check_supported_size(options->digest_size,
2209a061e50aSPablo de Lara 						cap->sym.auth.digest_size.min,
2210a061e50aSPablo de Lara 						cap->sym.auth.digest_size.max,
2211a061e50aSPablo de Lara 						cap->sym.auth.digest_size.increment)
2212a061e50aSPablo de Lara 							!= 0) {
2213a061e50aSPablo de Lara 					printf("Unsupported digest length\n");
2214a061e50aSPablo de Lara 					return -1;
2215a061e50aSPablo de Lara 				}
2216a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2217a061e50aSPablo de Lara 							options->digest_size;
2218a061e50aSPablo de Lara 			/* No size provided, use minimum size. */
2219a061e50aSPablo de Lara 			} else
2220a061e50aSPablo de Lara 				options->auth_xform.auth.digest_length =
2221a061e50aSPablo de Lara 						cap->sym.auth.digest_size.min;
222227cf2d1bSPablo de Lara 		}
2223387259bdSDeclan Doherty 
2224*2c59bd32SSlawomir Mrozowicz 		retval = rte_cryptodev_configure(cdev_id, &conf,
2225*2c59bd32SSlawomir Mrozowicz 				session_pool_socket[socket_id]);
2226387259bdSDeclan Doherty 		if (retval < 0) {
2227387259bdSDeclan Doherty 			printf("Failed to configure cryptodev %u", cdev_id);
2228387259bdSDeclan Doherty 			return -1;
2229387259bdSDeclan Doherty 		}
2230387259bdSDeclan Doherty 
2231387259bdSDeclan Doherty 		qp_conf.nb_descriptors = 2048;
2232387259bdSDeclan Doherty 
2233387259bdSDeclan Doherty 		retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2234*2c59bd32SSlawomir Mrozowicz 				socket_id);
2235387259bdSDeclan Doherty 		if (retval < 0) {
2236387259bdSDeclan Doherty 			printf("Failed to setup queue pair %u on cryptodev %u",
2237387259bdSDeclan Doherty 					0, cdev_id);
2238387259bdSDeclan Doherty 			return -1;
2239387259bdSDeclan Doherty 		}
2240387259bdSDeclan Doherty 
2241800386e6SHemant Agrawal 		retval = rte_cryptodev_start(cdev_id);
2242800386e6SHemant Agrawal 		if (retval < 0) {
2243800386e6SHemant Agrawal 			printf("Failed to start device %u: error %d\n",
2244800386e6SHemant Agrawal 					cdev_id, retval);
2245800386e6SHemant Agrawal 			return -1;
2246800386e6SHemant Agrawal 		}
2247800386e6SHemant Agrawal 
2248ad476dd3SPablo de Lara 		l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2249387259bdSDeclan Doherty 
225027cf2d1bSPablo de Lara 		enabled_cdevs[cdev_id] = 1;
2251387259bdSDeclan Doherty 		enabled_cdev_count++;
2252387259bdSDeclan Doherty 	}
2253387259bdSDeclan Doherty 
2254387259bdSDeclan Doherty 	return enabled_cdev_count;
2255387259bdSDeclan Doherty }
2256387259bdSDeclan Doherty 
2257387259bdSDeclan Doherty static int
2258387259bdSDeclan Doherty initialize_ports(struct l2fwd_crypto_options *options)
2259387259bdSDeclan Doherty {
2260387259bdSDeclan Doherty 	uint8_t last_portid, portid;
2261387259bdSDeclan Doherty 	unsigned enabled_portcount = 0;
2262387259bdSDeclan Doherty 	unsigned nb_ports = rte_eth_dev_count();
2263387259bdSDeclan Doherty 
2264387259bdSDeclan Doherty 	if (nb_ports == 0) {
2265387259bdSDeclan Doherty 		printf("No Ethernet ports - bye\n");
2266387259bdSDeclan Doherty 		return -1;
2267387259bdSDeclan Doherty 	}
2268387259bdSDeclan Doherty 
2269387259bdSDeclan Doherty 	/* Reset l2fwd_dst_ports */
2270387259bdSDeclan Doherty 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2271387259bdSDeclan Doherty 		l2fwd_dst_ports[portid] = 0;
2272387259bdSDeclan Doherty 
2273387259bdSDeclan Doherty 	for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2274387259bdSDeclan Doherty 		int retval;
2275387259bdSDeclan Doherty 
2276387259bdSDeclan Doherty 		/* Skip ports that are not enabled */
2277387259bdSDeclan Doherty 		if ((options->portmask & (1 << portid)) == 0)
2278387259bdSDeclan Doherty 			continue;
2279387259bdSDeclan Doherty 
2280387259bdSDeclan Doherty 		/* init port */
2281387259bdSDeclan Doherty 		printf("Initializing port %u... ", (unsigned) portid);
2282387259bdSDeclan Doherty 		fflush(stdout);
2283387259bdSDeclan Doherty 		retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2284387259bdSDeclan Doherty 		if (retval < 0) {
2285387259bdSDeclan Doherty 			printf("Cannot configure device: err=%d, port=%u\n",
2286387259bdSDeclan Doherty 				  retval, (unsigned) portid);
2287387259bdSDeclan Doherty 			return -1;
2288387259bdSDeclan Doherty 		}
2289387259bdSDeclan Doherty 
2290387259bdSDeclan Doherty 		/* init one RX queue */
2291387259bdSDeclan Doherty 		fflush(stdout);
2292387259bdSDeclan Doherty 		retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2293387259bdSDeclan Doherty 					     rte_eth_dev_socket_id(portid),
2294387259bdSDeclan Doherty 					     NULL, l2fwd_pktmbuf_pool);
2295387259bdSDeclan Doherty 		if (retval < 0) {
2296387259bdSDeclan Doherty 			printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2297387259bdSDeclan Doherty 					retval, (unsigned) portid);
2298387259bdSDeclan Doherty 			return -1;
2299387259bdSDeclan Doherty 		}
2300387259bdSDeclan Doherty 
2301387259bdSDeclan Doherty 		/* init one TX queue on each port */
2302387259bdSDeclan Doherty 		fflush(stdout);
2303387259bdSDeclan Doherty 		retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2304387259bdSDeclan Doherty 				rte_eth_dev_socket_id(portid),
2305387259bdSDeclan Doherty 				NULL);
2306387259bdSDeclan Doherty 		if (retval < 0) {
2307387259bdSDeclan Doherty 			printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2308387259bdSDeclan Doherty 				retval, (unsigned) portid);
2309387259bdSDeclan Doherty 
2310387259bdSDeclan Doherty 			return -1;
2311387259bdSDeclan Doherty 		}
2312387259bdSDeclan Doherty 
2313387259bdSDeclan Doherty 		/* Start device */
2314387259bdSDeclan Doherty 		retval = rte_eth_dev_start(portid);
2315387259bdSDeclan Doherty 		if (retval < 0) {
2316387259bdSDeclan Doherty 			printf("rte_eth_dev_start:err=%d, port=%u\n",
2317387259bdSDeclan Doherty 					retval, (unsigned) portid);
2318387259bdSDeclan Doherty 			return -1;
2319387259bdSDeclan Doherty 		}
2320387259bdSDeclan Doherty 
2321387259bdSDeclan Doherty 		rte_eth_promiscuous_enable(portid);
2322387259bdSDeclan Doherty 
2323387259bdSDeclan Doherty 		rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2324387259bdSDeclan Doherty 
2325387259bdSDeclan Doherty 		printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2326387259bdSDeclan Doherty 				(unsigned) portid,
2327387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[0],
2328387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[1],
2329387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[2],
2330387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[3],
2331387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[4],
2332387259bdSDeclan Doherty 				l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2333387259bdSDeclan Doherty 
2334387259bdSDeclan Doherty 		/* initialize port stats */
2335387259bdSDeclan Doherty 		memset(&port_statistics, 0, sizeof(port_statistics));
2336387259bdSDeclan Doherty 
2337387259bdSDeclan Doherty 		/* Setup port forwarding table */
2338387259bdSDeclan Doherty 		if (enabled_portcount % 2) {
2339387259bdSDeclan Doherty 			l2fwd_dst_ports[portid] = last_portid;
2340387259bdSDeclan Doherty 			l2fwd_dst_ports[last_portid] = portid;
2341387259bdSDeclan Doherty 		} else {
2342387259bdSDeclan Doherty 			last_portid = portid;
2343387259bdSDeclan Doherty 		}
2344387259bdSDeclan Doherty 
2345387259bdSDeclan Doherty 		l2fwd_enabled_port_mask |= (1 << portid);
2346387259bdSDeclan Doherty 		enabled_portcount++;
2347387259bdSDeclan Doherty 	}
2348387259bdSDeclan Doherty 
2349387259bdSDeclan Doherty 	if (enabled_portcount == 1) {
2350387259bdSDeclan Doherty 		l2fwd_dst_ports[last_portid] = last_portid;
2351387259bdSDeclan Doherty 	} else if (enabled_portcount % 2) {
2352387259bdSDeclan Doherty 		printf("odd number of ports in portmask- bye\n");
2353387259bdSDeclan Doherty 		return -1;
2354387259bdSDeclan Doherty 	}
2355387259bdSDeclan Doherty 
2356387259bdSDeclan Doherty 	check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2357387259bdSDeclan Doherty 
2358387259bdSDeclan Doherty 	return enabled_portcount;
2359387259bdSDeclan Doherty }
2360387259bdSDeclan Doherty 
23611df9c010SPablo de Lara static void
23621df9c010SPablo de Lara reserve_key_memory(struct l2fwd_crypto_options *options)
23631df9c010SPablo de Lara {
23641df9c010SPablo de Lara 	options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
23651df9c010SPablo de Lara 						MAX_KEY_SIZE, 0);
23661df9c010SPablo de Lara 	if (options->cipher_xform.cipher.key.data == NULL)
23671df9c010SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
23681df9c010SPablo de Lara 
23691df9c010SPablo de Lara 	options->auth_xform.auth.key.data = rte_malloc("auth key",
23701df9c010SPablo de Lara 						MAX_KEY_SIZE, 0);
23711df9c010SPablo de Lara 	if (options->auth_xform.auth.key.data == NULL)
23721df9c010SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
23731df9c010SPablo de Lara 
23742661f4fbSPablo de Lara 	options->aead_xform.aead.key.data = rte_malloc("aead key",
23752661f4fbSPablo de Lara 						MAX_KEY_SIZE, 0);
23762661f4fbSPablo de Lara 	if (options->aead_xform.aead.key.data == NULL)
23772661f4fbSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
23782661f4fbSPablo de Lara 
2379acf86169SPablo de Lara 	options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2380acf86169SPablo de Lara 	if (options->cipher_iv.data == NULL)
2381acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2382acf86169SPablo de Lara 
2383acf86169SPablo de Lara 	options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2384acf86169SPablo de Lara 	if (options->auth_iv.data == NULL)
2385acf86169SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2386617a7949SPablo de Lara 
23872661f4fbSPablo de Lara 	options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
23882661f4fbSPablo de Lara 	if (options->aead_iv.data == NULL)
23892661f4fbSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
23902661f4fbSPablo de Lara 
2391617a7949SPablo de Lara 	options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2392617a7949SPablo de Lara 	if (options->aad.data == NULL)
2393617a7949SPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2394617a7949SPablo de Lara 	options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
23951df9c010SPablo de Lara }
23961df9c010SPablo de Lara 
2397387259bdSDeclan Doherty int
2398387259bdSDeclan Doherty main(int argc, char **argv)
2399387259bdSDeclan Doherty {
2400387259bdSDeclan Doherty 	struct lcore_queue_conf *qconf;
2401387259bdSDeclan Doherty 	struct l2fwd_crypto_options options;
2402387259bdSDeclan Doherty 
2403387259bdSDeclan Doherty 	uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
2404387259bdSDeclan Doherty 	unsigned lcore_id, rx_lcore_id;
2405387259bdSDeclan Doherty 	int ret, enabled_cdevcount, enabled_portcount;
240627cf2d1bSPablo de Lara 	uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2407387259bdSDeclan Doherty 
2408387259bdSDeclan Doherty 	/* init EAL */
2409387259bdSDeclan Doherty 	ret = rte_eal_init(argc, argv);
2410387259bdSDeclan Doherty 	if (ret < 0)
2411387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2412387259bdSDeclan Doherty 	argc -= ret;
2413387259bdSDeclan Doherty 	argv += ret;
2414387259bdSDeclan Doherty 
24151df9c010SPablo de Lara 	/* reserve memory for Cipher/Auth key and IV */
24161df9c010SPablo de Lara 	reserve_key_memory(&options);
24171df9c010SPablo de Lara 
2418387259bdSDeclan Doherty 	/* parse application arguments (after the EAL ones) */
2419387259bdSDeclan Doherty 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
2420387259bdSDeclan Doherty 	if (ret < 0)
2421387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2422387259bdSDeclan Doherty 
2423387259bdSDeclan Doherty 	/* create the mbuf pool */
2424c0f87eb5SDeclan Doherty 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2425c0f87eb5SDeclan Doherty 			sizeof(struct rte_crypto_op),
2426c0f87eb5SDeclan Doherty 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2427387259bdSDeclan Doherty 	if (l2fwd_pktmbuf_pool == NULL)
2428387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2429387259bdSDeclan Doherty 
2430387259bdSDeclan Doherty 	/* create crypto op pool */
2431c0f87eb5SDeclan Doherty 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2432e636243eSPablo de Lara 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2433c0f87eb5SDeclan Doherty 			rte_socket_id());
2434c0f87eb5SDeclan Doherty 	if (l2fwd_crypto_op_pool == NULL)
2435387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2436387259bdSDeclan Doherty 
2437387259bdSDeclan Doherty 	/* Enable Ethernet ports */
2438387259bdSDeclan Doherty 	enabled_portcount = initialize_ports(&options);
2439387259bdSDeclan Doherty 	if (enabled_portcount < 1)
2440387259bdSDeclan Doherty 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2441387259bdSDeclan Doherty 
2442387259bdSDeclan Doherty 	nb_ports = rte_eth_dev_count();
2443387259bdSDeclan Doherty 	/* Initialize the port/queue configuration of each logical core */
2444387259bdSDeclan Doherty 	for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2445387259bdSDeclan Doherty 			portid < nb_ports; portid++) {
2446387259bdSDeclan Doherty 
2447387259bdSDeclan Doherty 		/* skip ports that are not enabled */
2448387259bdSDeclan Doherty 		if ((options.portmask & (1 << portid)) == 0)
2449387259bdSDeclan Doherty 			continue;
2450387259bdSDeclan Doherty 
2451387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2452387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2453387259bdSDeclan Doherty 				rx_lcore_id++;
2454387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2455387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2456387259bdSDeclan Doherty 							"Not enough cores\n");
2457387259bdSDeclan Doherty 			}
2458387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2459387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2460387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2461387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2462387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2463387259bdSDeclan Doherty 				rx_lcore_id++;
2464387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2465387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2466387259bdSDeclan Doherty 							"Not enough cores\n");
2467387259bdSDeclan Doherty 			}
2468387259bdSDeclan Doherty 		}
2469387259bdSDeclan Doherty 
2470387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2471387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2472387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2473387259bdSDeclan Doherty 
2474387259bdSDeclan Doherty 		qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2475387259bdSDeclan Doherty 		qconf->nb_rx_ports++;
2476387259bdSDeclan Doherty 
2477387259bdSDeclan Doherty 		printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
2478387259bdSDeclan Doherty 	}
2479387259bdSDeclan Doherty 
2480387259bdSDeclan Doherty 	/* Enable Crypto devices */
248127cf2d1bSPablo de Lara 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
248227cf2d1bSPablo de Lara 			enabled_cdevs);
248327cf2d1bSPablo de Lara 	if (enabled_cdevcount < 0)
248427cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
248527cf2d1bSPablo de Lara 
248627cf2d1bSPablo de Lara 	if (enabled_cdevcount < enabled_portcount)
248727cf2d1bSPablo de Lara 		rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
248827cf2d1bSPablo de Lara 				"has to be more or equal to number of ports (%d)\n",
248927cf2d1bSPablo de Lara 				enabled_cdevcount, enabled_portcount);
2490387259bdSDeclan Doherty 
2491387259bdSDeclan Doherty 	nb_cryptodevs = rte_cryptodev_count();
249227cf2d1bSPablo de Lara 
249327cf2d1bSPablo de Lara 	/* Initialize the port/cryptodev configuration of each logical core */
2494387259bdSDeclan Doherty 	for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2495387259bdSDeclan Doherty 			cdev_id < nb_cryptodevs && enabled_cdevcount;
2496387259bdSDeclan Doherty 			cdev_id++) {
249727cf2d1bSPablo de Lara 		/* Crypto op not supported by crypto device */
249827cf2d1bSPablo de Lara 		if (!enabled_cdevs[cdev_id])
2499387259bdSDeclan Doherty 			continue;
2500387259bdSDeclan Doherty 
2501387259bdSDeclan Doherty 		if (options.single_lcore && qconf == NULL) {
2502387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2503387259bdSDeclan Doherty 				rx_lcore_id++;
2504387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2505387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2506387259bdSDeclan Doherty 							"Not enough cores\n");
2507387259bdSDeclan Doherty 			}
2508387259bdSDeclan Doherty 		} else if (!options.single_lcore) {
2509387259bdSDeclan Doherty 			/* get the lcore_id for this port */
2510387259bdSDeclan Doherty 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2511387259bdSDeclan Doherty 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2512387259bdSDeclan Doherty 			       options.nb_ports_per_lcore) {
2513387259bdSDeclan Doherty 				rx_lcore_id++;
2514387259bdSDeclan Doherty 				if (rx_lcore_id >= RTE_MAX_LCORE)
2515387259bdSDeclan Doherty 					rte_exit(EXIT_FAILURE,
2516387259bdSDeclan Doherty 							"Not enough cores\n");
2517387259bdSDeclan Doherty 			}
2518387259bdSDeclan Doherty 		}
2519387259bdSDeclan Doherty 
2520387259bdSDeclan Doherty 		/* Assigned a new logical core in the loop above. */
2521387259bdSDeclan Doherty 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2522387259bdSDeclan Doherty 			qconf = &lcore_queue_conf[rx_lcore_id];
2523387259bdSDeclan Doherty 
2524387259bdSDeclan Doherty 		qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2525387259bdSDeclan Doherty 		qconf->nb_crypto_devs++;
2526387259bdSDeclan Doherty 
2527387259bdSDeclan Doherty 		enabled_cdevcount--;
2528387259bdSDeclan Doherty 
2529387259bdSDeclan Doherty 		printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2530387259bdSDeclan Doherty 				(unsigned)cdev_id);
2531387259bdSDeclan Doherty 	}
2532387259bdSDeclan Doherty 
2533387259bdSDeclan Doherty 	/* launch per-lcore init on every lcore */
2534387259bdSDeclan Doherty 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2535387259bdSDeclan Doherty 			CALL_MASTER);
2536387259bdSDeclan Doherty 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2537387259bdSDeclan Doherty 		if (rte_eal_wait_lcore(lcore_id) < 0)
2538387259bdSDeclan Doherty 			return -1;
2539387259bdSDeclan Doherty 	}
2540387259bdSDeclan Doherty 
2541387259bdSDeclan Doherty 	return 0;
2542387259bdSDeclan Doherty }
2543