xref: /dpdk/examples/l2fwd-crypto/main.c (revision 3e0ceb9f17fff027fc6c8f18de35e11719ffa61e)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <setjmp.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <getopt.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
54 #include <rte_cryptodev.h>
55 #include <rte_cycles.h>
56 #include <rte_debug.h>
57 #include <rte_eal.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_interrupts.h>
61 #include <rte_ip.h>
62 #include <rte_launch.h>
63 #include <rte_lcore.h>
64 #include <rte_log.h>
65 #include <rte_malloc.h>
66 #include <rte_mbuf.h>
67 #include <rte_memcpy.h>
68 #include <rte_memory.h>
69 #include <rte_mempool.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
73 #include <rte_hexdump.h>
74 
75 enum cdev_type {
76 	CDEV_TYPE_ANY,
77 	CDEV_TYPE_HW,
78 	CDEV_TYPE_SW
79 };
80 
81 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
82 
83 #define NB_MBUF   8192
84 
85 #define MAX_STR_LEN 32
86 #define MAX_KEY_SIZE 128
87 #define MAX_IV_SIZE 16
88 #define MAX_AAD_SIZE 65535
89 #define MAX_PKT_BURST 32
90 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91 #define MAX_SESSIONS 32
92 #define SESSION_POOL_CACHE_SIZE 0
93 
94 #define MAXIMUM_IV_LENGTH	16
95 #define IV_OFFSET		(sizeof(struct rte_crypto_op) + \
96 				sizeof(struct rte_crypto_sym_op))
97 
98 /*
99  * Configurable number of RX/TX ring descriptors
100  */
101 #define RTE_TEST_RX_DESC_DEFAULT 128
102 #define RTE_TEST_TX_DESC_DEFAULT 512
103 
104 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
105 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
106 
107 /* ethernet addresses of ports */
108 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
109 
110 /* mask of enabled ports */
111 static uint64_t l2fwd_enabled_port_mask;
112 static uint64_t l2fwd_enabled_crypto_mask;
113 
114 /* list of enabled ports */
115 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
116 
117 
118 struct pkt_buffer {
119 	unsigned len;
120 	struct rte_mbuf *buffer[MAX_PKT_BURST];
121 };
122 
123 struct op_buffer {
124 	unsigned len;
125 	struct rte_crypto_op *buffer[MAX_PKT_BURST];
126 };
127 
128 #define MAX_RX_QUEUE_PER_LCORE 16
129 #define MAX_TX_QUEUE_PER_PORT 16
130 
131 enum l2fwd_crypto_xform_chain {
132 	L2FWD_CRYPTO_CIPHER_HASH,
133 	L2FWD_CRYPTO_HASH_CIPHER,
134 	L2FWD_CRYPTO_CIPHER_ONLY,
135 	L2FWD_CRYPTO_HASH_ONLY,
136 	L2FWD_CRYPTO_AEAD
137 };
138 
139 struct l2fwd_key {
140 	uint8_t *data;
141 	uint32_t length;
142 	rte_iova_t phys_addr;
143 };
144 
145 struct l2fwd_iv {
146 	uint8_t *data;
147 	uint16_t length;
148 };
149 
150 /** l2fwd crypto application command line options */
151 struct l2fwd_crypto_options {
152 	unsigned portmask;
153 	unsigned nb_ports_per_lcore;
154 	unsigned refresh_period;
155 	unsigned single_lcore:1;
156 
157 	enum cdev_type type;
158 	unsigned sessionless:1;
159 
160 	enum l2fwd_crypto_xform_chain xform_chain;
161 
162 	struct rte_crypto_sym_xform cipher_xform;
163 	unsigned ckey_param;
164 	int ckey_random_size;
165 
166 	struct l2fwd_iv cipher_iv;
167 	unsigned int cipher_iv_param;
168 	int cipher_iv_random_size;
169 
170 	struct rte_crypto_sym_xform auth_xform;
171 	uint8_t akey_param;
172 	int akey_random_size;
173 
174 	struct l2fwd_iv auth_iv;
175 	unsigned int auth_iv_param;
176 	int auth_iv_random_size;
177 
178 	struct rte_crypto_sym_xform aead_xform;
179 	unsigned int aead_key_param;
180 	int aead_key_random_size;
181 
182 	struct l2fwd_iv aead_iv;
183 	unsigned int aead_iv_param;
184 	int aead_iv_random_size;
185 
186 	struct l2fwd_key aad;
187 	unsigned aad_param;
188 	int aad_random_size;
189 
190 	int digest_size;
191 
192 	uint16_t block_size;
193 	char string_type[MAX_STR_LEN];
194 
195 	uint64_t cryptodev_mask;
196 
197 	unsigned int mac_updating;
198 };
199 
200 /** l2fwd crypto lcore params */
201 struct l2fwd_crypto_params {
202 	uint8_t dev_id;
203 	uint8_t qp_id;
204 
205 	unsigned digest_length;
206 	unsigned block_size;
207 
208 	struct l2fwd_iv cipher_iv;
209 	struct l2fwd_iv auth_iv;
210 	struct l2fwd_iv aead_iv;
211 	struct l2fwd_key aad;
212 	struct rte_cryptodev_sym_session *session;
213 
214 	uint8_t do_cipher;
215 	uint8_t do_hash;
216 	uint8_t do_aead;
217 	uint8_t hash_verify;
218 
219 	enum rte_crypto_cipher_algorithm cipher_algo;
220 	enum rte_crypto_auth_algorithm auth_algo;
221 	enum rte_crypto_aead_algorithm aead_algo;
222 };
223 
224 /** lcore configuration */
225 struct lcore_queue_conf {
226 	unsigned nb_rx_ports;
227 	uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
228 
229 	unsigned nb_crypto_devs;
230 	unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
231 
232 	struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
233 	struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
234 } __rte_cache_aligned;
235 
236 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
237 
238 static const struct rte_eth_conf port_conf = {
239 	.rxmode = {
240 		.mq_mode = ETH_MQ_RX_NONE,
241 		.max_rx_pkt_len = ETHER_MAX_LEN,
242 		.split_hdr_size = 0,
243 		.header_split   = 0, /**< Header Split disabled */
244 		.hw_ip_checksum = 0, /**< IP checksum offload disabled */
245 		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
246 		.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
247 		.hw_strip_crc   = 1, /**< CRC stripped by hardware */
248 	},
249 	.txmode = {
250 		.mq_mode = ETH_MQ_TX_NONE,
251 	},
252 };
253 
254 struct rte_mempool *l2fwd_pktmbuf_pool;
255 struct rte_mempool *l2fwd_crypto_op_pool;
256 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
257 
258 /* Per-port statistics struct */
259 struct l2fwd_port_statistics {
260 	uint64_t tx;
261 	uint64_t rx;
262 
263 	uint64_t crypto_enqueued;
264 	uint64_t crypto_dequeued;
265 
266 	uint64_t dropped;
267 } __rte_cache_aligned;
268 
269 struct l2fwd_crypto_statistics {
270 	uint64_t enqueued;
271 	uint64_t dequeued;
272 
273 	uint64_t errors;
274 } __rte_cache_aligned;
275 
276 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
277 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
278 
279 /* A tsc-based timer responsible for triggering statistics printout */
280 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
281 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
282 
283 /* default period is 10 seconds */
284 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
285 
286 /* Print out statistics on packets dropped */
287 static void
288 print_stats(void)
289 {
290 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
291 	uint64_t total_packets_enqueued, total_packets_dequeued,
292 		total_packets_errors;
293 	uint16_t portid;
294 	uint64_t cdevid;
295 
296 	total_packets_dropped = 0;
297 	total_packets_tx = 0;
298 	total_packets_rx = 0;
299 	total_packets_enqueued = 0;
300 	total_packets_dequeued = 0;
301 	total_packets_errors = 0;
302 
303 	const char clr[] = { 27, '[', '2', 'J', '\0' };
304 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
305 
306 		/* Clear screen and move to top left */
307 	printf("%s%s", clr, topLeft);
308 
309 	printf("\nPort statistics ====================================");
310 
311 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
312 		/* skip disabled ports */
313 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
314 			continue;
315 		printf("\nStatistics for port %u ------------------------------"
316 			   "\nPackets sent: %32"PRIu64
317 			   "\nPackets received: %28"PRIu64
318 			   "\nPackets dropped: %29"PRIu64,
319 			   portid,
320 			   port_statistics[portid].tx,
321 			   port_statistics[portid].rx,
322 			   port_statistics[portid].dropped);
323 
324 		total_packets_dropped += port_statistics[portid].dropped;
325 		total_packets_tx += port_statistics[portid].tx;
326 		total_packets_rx += port_statistics[portid].rx;
327 	}
328 	printf("\nCrypto statistics ==================================");
329 
330 	for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
331 		/* skip disabled ports */
332 		if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
333 			continue;
334 		printf("\nStatistics for cryptodev %"PRIu64
335 				" -------------------------"
336 			   "\nPackets enqueued: %28"PRIu64
337 			   "\nPackets dequeued: %28"PRIu64
338 			   "\nPackets errors: %30"PRIu64,
339 			   cdevid,
340 			   crypto_statistics[cdevid].enqueued,
341 			   crypto_statistics[cdevid].dequeued,
342 			   crypto_statistics[cdevid].errors);
343 
344 		total_packets_enqueued += crypto_statistics[cdevid].enqueued;
345 		total_packets_dequeued += crypto_statistics[cdevid].dequeued;
346 		total_packets_errors += crypto_statistics[cdevid].errors;
347 	}
348 	printf("\nAggregate statistics ==============================="
349 		   "\nTotal packets received: %22"PRIu64
350 		   "\nTotal packets enqueued: %22"PRIu64
351 		   "\nTotal packets dequeued: %22"PRIu64
352 		   "\nTotal packets sent: %26"PRIu64
353 		   "\nTotal packets dropped: %23"PRIu64
354 		   "\nTotal packets crypto errors: %17"PRIu64,
355 		   total_packets_rx,
356 		   total_packets_enqueued,
357 		   total_packets_dequeued,
358 		   total_packets_tx,
359 		   total_packets_dropped,
360 		   total_packets_errors);
361 	printf("\n====================================================\n");
362 }
363 
364 static int
365 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
366 		struct l2fwd_crypto_params *cparams)
367 {
368 	struct rte_crypto_op **op_buffer;
369 	unsigned ret;
370 
371 	op_buffer = (struct rte_crypto_op **)
372 			qconf->op_buf[cparams->dev_id].buffer;
373 
374 	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
375 			cparams->qp_id,	op_buffer, (uint16_t) n);
376 
377 	crypto_statistics[cparams->dev_id].enqueued += ret;
378 	if (unlikely(ret < n)) {
379 		crypto_statistics[cparams->dev_id].errors += (n - ret);
380 		do {
381 			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
382 			rte_crypto_op_free(op_buffer[ret]);
383 		} while (++ret < n);
384 	}
385 
386 	return 0;
387 }
388 
389 static int
390 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
391 		struct l2fwd_crypto_params *cparams)
392 {
393 	unsigned lcore_id, len;
394 	struct lcore_queue_conf *qconf;
395 
396 	lcore_id = rte_lcore_id();
397 
398 	qconf = &lcore_queue_conf[lcore_id];
399 	len = qconf->op_buf[cparams->dev_id].len;
400 	qconf->op_buf[cparams->dev_id].buffer[len] = op;
401 	len++;
402 
403 	/* enough ops to be sent */
404 	if (len == MAX_PKT_BURST) {
405 		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
406 		len = 0;
407 	}
408 
409 	qconf->op_buf[cparams->dev_id].len = len;
410 	return 0;
411 }
412 
413 static int
414 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
415 		struct rte_crypto_op *op,
416 		struct l2fwd_crypto_params *cparams)
417 {
418 	struct ether_hdr *eth_hdr;
419 	struct ipv4_hdr *ip_hdr;
420 
421 	uint32_t ipdata_offset, data_len;
422 	uint32_t pad_len = 0;
423 	char *padding;
424 
425 	eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
426 
427 	if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
428 		return -1;
429 
430 	ipdata_offset = sizeof(struct ether_hdr);
431 
432 	ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
433 			ipdata_offset);
434 
435 	ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
436 			* IPV4_IHL_MULTIPLIER;
437 
438 
439 	/* Zero pad data to be crypto'd so it is block aligned */
440 	data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
441 
442 	if (cparams->do_hash && cparams->hash_verify)
443 		data_len -= cparams->digest_length;
444 
445 	if (cparams->do_cipher) {
446 		/*
447 		 * Following algorithms are block cipher algorithms,
448 		 * and might need padding
449 		 */
450 		switch (cparams->cipher_algo) {
451 		case RTE_CRYPTO_CIPHER_AES_CBC:
452 		case RTE_CRYPTO_CIPHER_AES_ECB:
453 		case RTE_CRYPTO_CIPHER_DES_CBC:
454 		case RTE_CRYPTO_CIPHER_3DES_CBC:
455 		case RTE_CRYPTO_CIPHER_3DES_ECB:
456 			if (data_len % cparams->block_size)
457 				pad_len = cparams->block_size -
458 					(data_len % cparams->block_size);
459 			break;
460 		default:
461 			pad_len = 0;
462 		}
463 
464 		if (pad_len) {
465 			padding = rte_pktmbuf_append(m, pad_len);
466 			if (unlikely(!padding))
467 				return -1;
468 
469 			data_len += pad_len;
470 			memset(padding, 0, pad_len);
471 		}
472 	}
473 
474 	/* Set crypto operation data parameters */
475 	rte_crypto_op_attach_sym_session(op, cparams->session);
476 
477 	if (cparams->do_hash) {
478 		if (cparams->auth_iv.length) {
479 			uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
480 						uint8_t *,
481 						IV_OFFSET +
482 						cparams->cipher_iv.length);
483 			/*
484 			 * Copy IV at the end of the crypto operation,
485 			 * after the cipher IV, if added
486 			 */
487 			rte_memcpy(iv_ptr, cparams->auth_iv.data,
488 					cparams->auth_iv.length);
489 		}
490 		if (!cparams->hash_verify) {
491 			/* Append space for digest to end of packet */
492 			op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
493 				cparams->digest_length);
494 		} else {
495 			op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
496 				uint8_t *) + ipdata_offset + data_len;
497 		}
498 
499 		op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
500 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
501 
502 		/* For wireless algorithms, offset/length must be in bits */
503 		if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
504 				cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
505 				cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
506 			op->sym->auth.data.offset = ipdata_offset << 3;
507 			op->sym->auth.data.length = data_len << 3;
508 		} else {
509 			op->sym->auth.data.offset = ipdata_offset;
510 			op->sym->auth.data.length = data_len;
511 		}
512 	}
513 
514 	if (cparams->do_cipher) {
515 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
516 							IV_OFFSET);
517 		/* Copy IV at the end of the crypto operation */
518 		rte_memcpy(iv_ptr, cparams->cipher_iv.data,
519 				cparams->cipher_iv.length);
520 
521 		/* For wireless algorithms, offset/length must be in bits */
522 		if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
523 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
524 				cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
525 			op->sym->cipher.data.offset = ipdata_offset << 3;
526 			op->sym->cipher.data.length = data_len << 3;
527 		} else {
528 			op->sym->cipher.data.offset = ipdata_offset;
529 			op->sym->cipher.data.length = data_len;
530 		}
531 	}
532 
533 	if (cparams->do_aead) {
534 		uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
535 							IV_OFFSET);
536 		/* Copy IV at the end of the crypto operation */
537 		/*
538 		 * If doing AES-CCM, nonce is copied one byte
539 		 * after the start of IV field
540 		 */
541 		if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
542 			rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
543 					cparams->aead_iv.length);
544 		else
545 			rte_memcpy(iv_ptr, cparams->aead_iv.data,
546 					cparams->aead_iv.length);
547 
548 		op->sym->aead.data.offset = ipdata_offset;
549 		op->sym->aead.data.length = data_len;
550 
551 		if (!cparams->hash_verify) {
552 			/* Append space for digest to end of packet */
553 			op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
554 				cparams->digest_length);
555 		} else {
556 			op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
557 				uint8_t *) + ipdata_offset + data_len;
558 		}
559 
560 		op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
561 				rte_pktmbuf_pkt_len(m) - cparams->digest_length);
562 
563 		if (cparams->aad.length) {
564 			op->sym->aead.aad.data = cparams->aad.data;
565 			op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
566 		}
567 	}
568 
569 	op->sym->m_src = m;
570 
571 	return l2fwd_crypto_enqueue(op, cparams);
572 }
573 
574 
575 /* Send the burst of packets on an output interface */
576 static int
577 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
578 		uint16_t port)
579 {
580 	struct rte_mbuf **pkt_buffer;
581 	unsigned ret;
582 
583 	pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
584 
585 	ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
586 	port_statistics[port].tx += ret;
587 	if (unlikely(ret < n)) {
588 		port_statistics[port].dropped += (n - ret);
589 		do {
590 			rte_pktmbuf_free(pkt_buffer[ret]);
591 		} while (++ret < n);
592 	}
593 
594 	return 0;
595 }
596 
597 /* Enqueue packets for TX and prepare them to be sent */
598 static int
599 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
600 {
601 	unsigned lcore_id, len;
602 	struct lcore_queue_conf *qconf;
603 
604 	lcore_id = rte_lcore_id();
605 
606 	qconf = &lcore_queue_conf[lcore_id];
607 	len = qconf->pkt_buf[port].len;
608 	qconf->pkt_buf[port].buffer[len] = m;
609 	len++;
610 
611 	/* enough pkts to be sent */
612 	if (unlikely(len == MAX_PKT_BURST)) {
613 		l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
614 		len = 0;
615 	}
616 
617 	qconf->pkt_buf[port].len = len;
618 	return 0;
619 }
620 
621 static void
622 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
623 {
624 	struct ether_hdr *eth;
625 	void *tmp;
626 
627 	eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
628 
629 	/* 02:00:00:00:00:xx */
630 	tmp = &eth->d_addr.addr_bytes[0];
631 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
632 
633 	/* src addr */
634 	ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
635 }
636 
637 static void
638 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
639 		struct l2fwd_crypto_options *options)
640 {
641 	uint16_t dst_port;
642 
643 	dst_port = l2fwd_dst_ports[portid];
644 
645 	if (options->mac_updating)
646 		l2fwd_mac_updating(m, dst_port);
647 
648 	l2fwd_send_packet(m, dst_port);
649 }
650 
651 /** Generate random key */
652 static void
653 generate_random_key(uint8_t *key, unsigned length)
654 {
655 	int fd;
656 	int ret;
657 
658 	fd = open("/dev/urandom", O_RDONLY);
659 	if (fd < 0)
660 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
661 
662 	ret = read(fd, key, length);
663 	close(fd);
664 
665 	if (ret != (signed)length)
666 		rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
667 }
668 
669 static struct rte_cryptodev_sym_session *
670 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
671 {
672 	struct rte_crypto_sym_xform *first_xform;
673 	struct rte_cryptodev_sym_session *session;
674 	int retval = rte_cryptodev_socket_id(cdev_id);
675 
676 	if (retval < 0)
677 		return NULL;
678 
679 	uint8_t socket_id = (uint8_t) retval;
680 	struct rte_mempool *sess_mp = session_pool_socket[socket_id];
681 
682 	if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
683 		first_xform = &options->aead_xform;
684 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
685 		first_xform = &options->cipher_xform;
686 		first_xform->next = &options->auth_xform;
687 	} else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
688 		first_xform = &options->auth_xform;
689 		first_xform->next = &options->cipher_xform;
690 	} else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
691 		first_xform = &options->cipher_xform;
692 	} else {
693 		first_xform = &options->auth_xform;
694 	}
695 
696 	session = rte_cryptodev_sym_session_create(sess_mp);
697 
698 	if (session == NULL)
699 		return NULL;
700 
701 	if (rte_cryptodev_sym_session_init(cdev_id, session,
702 				first_xform, sess_mp) < 0)
703 		return NULL;
704 
705 	return session;
706 }
707 
708 static void
709 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
710 
711 /* main processing loop */
712 static void
713 l2fwd_main_loop(struct l2fwd_crypto_options *options)
714 {
715 	struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
716 	struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
717 
718 	unsigned lcore_id = rte_lcore_id();
719 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
720 	unsigned int i, j, nb_rx, len;
721 	uint16_t portid;
722 	struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
723 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
724 			US_PER_S * BURST_TX_DRAIN_US;
725 	struct l2fwd_crypto_params *cparams;
726 	struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
727 	struct rte_cryptodev_sym_session *session;
728 
729 	if (qconf->nb_rx_ports == 0) {
730 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
731 		return;
732 	}
733 
734 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
735 
736 	for (i = 0; i < qconf->nb_rx_ports; i++) {
737 
738 		portid = qconf->rx_port_list[i];
739 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
740 			portid);
741 	}
742 
743 	for (i = 0; i < qconf->nb_crypto_devs; i++) {
744 		port_cparams[i].do_cipher = 0;
745 		port_cparams[i].do_hash = 0;
746 		port_cparams[i].do_aead = 0;
747 
748 		switch (options->xform_chain) {
749 		case L2FWD_CRYPTO_AEAD:
750 			port_cparams[i].do_aead = 1;
751 			break;
752 		case L2FWD_CRYPTO_CIPHER_HASH:
753 		case L2FWD_CRYPTO_HASH_CIPHER:
754 			port_cparams[i].do_cipher = 1;
755 			port_cparams[i].do_hash = 1;
756 			break;
757 		case L2FWD_CRYPTO_HASH_ONLY:
758 			port_cparams[i].do_hash = 1;
759 			break;
760 		case L2FWD_CRYPTO_CIPHER_ONLY:
761 			port_cparams[i].do_cipher = 1;
762 			break;
763 		}
764 
765 		port_cparams[i].dev_id = qconf->cryptodev_list[i];
766 		port_cparams[i].qp_id = 0;
767 
768 		port_cparams[i].block_size = options->block_size;
769 
770 		if (port_cparams[i].do_hash) {
771 			port_cparams[i].auth_iv.data = options->auth_iv.data;
772 			port_cparams[i].auth_iv.length = options->auth_iv.length;
773 			if (!options->auth_iv_param)
774 				generate_random_key(port_cparams[i].auth_iv.data,
775 						port_cparams[i].auth_iv.length);
776 			if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
777 				port_cparams[i].hash_verify = 1;
778 			else
779 				port_cparams[i].hash_verify = 0;
780 
781 			port_cparams[i].auth_algo = options->auth_xform.auth.algo;
782 			port_cparams[i].digest_length =
783 					options->auth_xform.auth.digest_length;
784 			/* Set IV parameters */
785 			if (options->auth_iv.length) {
786 				options->auth_xform.auth.iv.offset =
787 					IV_OFFSET + options->cipher_iv.length;
788 				options->auth_xform.auth.iv.length =
789 					options->auth_iv.length;
790 			}
791 		}
792 
793 		if (port_cparams[i].do_aead) {
794 			port_cparams[i].aead_iv.data = options->aead_iv.data;
795 			port_cparams[i].aead_iv.length = options->aead_iv.length;
796 			if (!options->aead_iv_param)
797 				generate_random_key(port_cparams[i].aead_iv.data,
798 						port_cparams[i].aead_iv.length);
799 			port_cparams[i].aead_algo = options->aead_xform.aead.algo;
800 			port_cparams[i].digest_length =
801 					options->aead_xform.aead.digest_length;
802 			if (options->aead_xform.aead.aad_length) {
803 				port_cparams[i].aad.data = options->aad.data;
804 				port_cparams[i].aad.phys_addr = options->aad.phys_addr;
805 				port_cparams[i].aad.length = options->aad.length;
806 				if (!options->aad_param)
807 					generate_random_key(port_cparams[i].aad.data,
808 						port_cparams[i].aad.length);
809 				/*
810 				 * If doing AES-CCM, first 18 bytes has to be reserved,
811 				 * and actual AAD should start from byte 18
812 				 */
813 				if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
814 					memmove(port_cparams[i].aad.data + 18,
815 							port_cparams[i].aad.data,
816 							port_cparams[i].aad.length);
817 
818 			} else
819 				port_cparams[i].aad.length = 0;
820 
821 			if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
822 				port_cparams[i].hash_verify = 1;
823 			else
824 				port_cparams[i].hash_verify = 0;
825 
826 			/* Set IV parameters */
827 			options->aead_xform.aead.iv.offset = IV_OFFSET;
828 			options->aead_xform.aead.iv.length = options->aead_iv.length;
829 		}
830 
831 		if (port_cparams[i].do_cipher) {
832 			port_cparams[i].cipher_iv.data = options->cipher_iv.data;
833 			port_cparams[i].cipher_iv.length = options->cipher_iv.length;
834 			if (!options->cipher_iv_param)
835 				generate_random_key(port_cparams[i].cipher_iv.data,
836 						port_cparams[i].cipher_iv.length);
837 
838 			port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
839 			/* Set IV parameters */
840 			options->cipher_xform.cipher.iv.offset = IV_OFFSET;
841 			options->cipher_xform.cipher.iv.length =
842 						options->cipher_iv.length;
843 		}
844 
845 		session = initialize_crypto_session(options,
846 				port_cparams[i].dev_id);
847 		if (session == NULL)
848 			rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
849 
850 		port_cparams[i].session = session;
851 
852 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
853 				port_cparams[i].dev_id);
854 	}
855 
856 	l2fwd_crypto_options_print(options);
857 
858 	/*
859 	 * Initialize previous tsc timestamp before the loop,
860 	 * to avoid showing the port statistics immediately,
861 	 * so user can see the crypto information.
862 	 */
863 	prev_tsc = rte_rdtsc();
864 	while (1) {
865 
866 		cur_tsc = rte_rdtsc();
867 
868 		/*
869 		 * Crypto device/TX burst queue drain
870 		 */
871 		diff_tsc = cur_tsc - prev_tsc;
872 		if (unlikely(diff_tsc > drain_tsc)) {
873 			/* Enqueue all crypto ops remaining in buffers */
874 			for (i = 0; i < qconf->nb_crypto_devs; i++) {
875 				cparams = &port_cparams[i];
876 				len = qconf->op_buf[cparams->dev_id].len;
877 				l2fwd_crypto_send_burst(qconf, len, cparams);
878 				qconf->op_buf[cparams->dev_id].len = 0;
879 			}
880 			/* Transmit all packets remaining in buffers */
881 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
882 				if (qconf->pkt_buf[portid].len == 0)
883 					continue;
884 				l2fwd_send_burst(&lcore_queue_conf[lcore_id],
885 						 qconf->pkt_buf[portid].len,
886 						 portid);
887 				qconf->pkt_buf[portid].len = 0;
888 			}
889 
890 			/* if timer is enabled */
891 			if (timer_period > 0) {
892 
893 				/* advance the timer */
894 				timer_tsc += diff_tsc;
895 
896 				/* if timer has reached its timeout */
897 				if (unlikely(timer_tsc >=
898 						(uint64_t)timer_period)) {
899 
900 					/* do this only on master core */
901 					if (lcore_id == rte_get_master_lcore()
902 						&& options->refresh_period) {
903 						print_stats();
904 						timer_tsc = 0;
905 					}
906 				}
907 			}
908 
909 			prev_tsc = cur_tsc;
910 		}
911 
912 		/*
913 		 * Read packet from RX queues
914 		 */
915 		for (i = 0; i < qconf->nb_rx_ports; i++) {
916 			portid = qconf->rx_port_list[i];
917 
918 			cparams = &port_cparams[i];
919 
920 			nb_rx = rte_eth_rx_burst(portid, 0,
921 						 pkts_burst, MAX_PKT_BURST);
922 
923 			port_statistics[portid].rx += nb_rx;
924 
925 			if (nb_rx) {
926 				/*
927 				 * If we can't allocate a crypto_ops, then drop
928 				 * the rest of the burst and dequeue and
929 				 * process the packets to free offload structs
930 				 */
931 				if (rte_crypto_op_bulk_alloc(
932 						l2fwd_crypto_op_pool,
933 						RTE_CRYPTO_OP_TYPE_SYMMETRIC,
934 						ops_burst, nb_rx) !=
935 								nb_rx) {
936 					for (j = 0; j < nb_rx; j++)
937 						rte_pktmbuf_free(pkts_burst[j]);
938 
939 					nb_rx = 0;
940 				}
941 
942 				/* Enqueue packets from Crypto device*/
943 				for (j = 0; j < nb_rx; j++) {
944 					m = pkts_burst[j];
945 
946 					l2fwd_simple_crypto_enqueue(m,
947 							ops_burst[j], cparams);
948 				}
949 			}
950 
951 			/* Dequeue packets from Crypto device */
952 			do {
953 				nb_rx = rte_cryptodev_dequeue_burst(
954 						cparams->dev_id, cparams->qp_id,
955 						ops_burst, MAX_PKT_BURST);
956 
957 				crypto_statistics[cparams->dev_id].dequeued +=
958 						nb_rx;
959 
960 				/* Forward crypto'd packets */
961 				for (j = 0; j < nb_rx; j++) {
962 					m = ops_burst[j]->sym->m_src;
963 
964 					rte_crypto_op_free(ops_burst[j]);
965 					l2fwd_simple_forward(m, portid,
966 							options);
967 				}
968 			} while (nb_rx == MAX_PKT_BURST);
969 		}
970 	}
971 }
972 
973 static int
974 l2fwd_launch_one_lcore(void *arg)
975 {
976 	l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
977 	return 0;
978 }
979 
980 /* Display command line arguments usage */
981 static void
982 l2fwd_crypto_usage(const char *prgname)
983 {
984 	printf("%s [EAL options] --\n"
985 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
986 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
987 		"  -s manage all ports from single lcore\n"
988 		"  -T PERIOD: statistics will be refreshed each PERIOD seconds"
989 		" (0 to disable, 10 default, 86400 maximum)\n"
990 
991 		"  --cdev_type HW / SW / ANY\n"
992 		"  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
993 		" HASH_ONLY / AEAD\n"
994 
995 		"  --cipher_algo ALGO\n"
996 		"  --cipher_op ENCRYPT / DECRYPT\n"
997 		"  --cipher_key KEY (bytes separated with \":\")\n"
998 		"  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
999 		"  --cipher_iv IV (bytes separated with \":\")\n"
1000 		"  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1001 
1002 		"  --auth_algo ALGO\n"
1003 		"  --auth_op GENERATE / VERIFY\n"
1004 		"  --auth_key KEY (bytes separated with \":\")\n"
1005 		"  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1006 		"  --auth_iv IV (bytes separated with \":\")\n"
1007 		"  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
1008 
1009 		"  --aead_algo ALGO\n"
1010 		"  --aead_op ENCRYPT / DECRYPT\n"
1011 		"  --aead_key KEY (bytes separated with \":\")\n"
1012 		"  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
1013 		"  --aead_iv IV (bytes separated with \":\")\n"
1014 		"  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1015 		"  --aad AAD (bytes separated with \":\")\n"
1016 		"  --aad_random_size SIZE: size of AAD when generated randomly\n"
1017 
1018 		"  --digest_size SIZE: size of digest to be generated/verified\n"
1019 
1020 		"  --sessionless\n"
1021 		"  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1022 
1023 		"  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1024 		"      When enabled:\n"
1025 		"       - The source MAC address is replaced by the TX port MAC address\n"
1026 		"       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1027 	       prgname);
1028 }
1029 
1030 /** Parse crypto device type command line argument */
1031 static int
1032 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1033 {
1034 	if (strcmp("HW", optarg) == 0) {
1035 		*type = CDEV_TYPE_HW;
1036 		return 0;
1037 	} else if (strcmp("SW", optarg) == 0) {
1038 		*type = CDEV_TYPE_SW;
1039 		return 0;
1040 	} else if (strcmp("ANY", optarg) == 0) {
1041 		*type = CDEV_TYPE_ANY;
1042 		return 0;
1043 	}
1044 
1045 	return -1;
1046 }
1047 
1048 /** Parse crypto chain xform command line argument */
1049 static int
1050 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1051 {
1052 	if (strcmp("CIPHER_HASH", optarg) == 0) {
1053 		options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1054 		return 0;
1055 	} else if (strcmp("HASH_CIPHER", optarg) == 0) {
1056 		options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1057 		return 0;
1058 	} else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1059 		options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1060 		return 0;
1061 	} else if (strcmp("HASH_ONLY", optarg) == 0) {
1062 		options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1063 		return 0;
1064 	} else if (strcmp("AEAD", optarg) == 0) {
1065 		options->xform_chain = L2FWD_CRYPTO_AEAD;
1066 		return 0;
1067 	}
1068 
1069 	return -1;
1070 }
1071 
1072 /** Parse crypto cipher algo option command line argument */
1073 static int
1074 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1075 {
1076 
1077 	if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1078 		RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1079 				"not supported!\n");
1080 		return -1;
1081 	}
1082 
1083 	return 0;
1084 }
1085 
1086 /** Parse crypto cipher operation command line argument */
1087 static int
1088 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1089 {
1090 	if (strcmp("ENCRYPT", optarg) == 0) {
1091 		*op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1092 		return 0;
1093 	} else if (strcmp("DECRYPT", optarg) == 0) {
1094 		*op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1095 		return 0;
1096 	}
1097 
1098 	printf("Cipher operation not supported!\n");
1099 	return -1;
1100 }
1101 
1102 /** Parse bytes from command line argument */
1103 static int
1104 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1105 {
1106 	unsigned byte_count;
1107 	char *token;
1108 
1109 	errno = 0;
1110 	for (byte_count = 0, token = strtok(input_arg, ":");
1111 			(byte_count < max_size) && (token != NULL);
1112 			token = strtok(NULL, ":")) {
1113 
1114 		int number = (int)strtol(token, NULL, 16);
1115 
1116 		if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1117 			return -1;
1118 
1119 		data[byte_count++] = (uint8_t)number;
1120 	}
1121 
1122 	return byte_count;
1123 }
1124 
1125 /** Parse size param*/
1126 static int
1127 parse_size(int *size, const char *q_arg)
1128 {
1129 	char *end = NULL;
1130 	unsigned long n;
1131 
1132 	/* parse hexadecimal string */
1133 	n = strtoul(q_arg, &end, 10);
1134 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1135 		n = 0;
1136 
1137 	if (n == 0) {
1138 		printf("invalid size\n");
1139 		return -1;
1140 	}
1141 
1142 	*size = n;
1143 	return 0;
1144 }
1145 
1146 /** Parse crypto cipher operation command line argument */
1147 static int
1148 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1149 {
1150 	if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1151 		RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1152 				"not supported!\n");
1153 		return -1;
1154 	}
1155 
1156 	return 0;
1157 }
1158 
1159 static int
1160 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1161 {
1162 	if (strcmp("VERIFY", optarg) == 0) {
1163 		*op = RTE_CRYPTO_AUTH_OP_VERIFY;
1164 		return 0;
1165 	} else if (strcmp("GENERATE", optarg) == 0) {
1166 		*op = RTE_CRYPTO_AUTH_OP_GENERATE;
1167 		return 0;
1168 	}
1169 
1170 	printf("Authentication operation specified not supported!\n");
1171 	return -1;
1172 }
1173 
1174 static int
1175 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1176 {
1177 	if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1178 		RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1179 				"not supported!\n");
1180 		return -1;
1181 	}
1182 
1183 	return 0;
1184 }
1185 
1186 static int
1187 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1188 {
1189 	if (strcmp("ENCRYPT", optarg) == 0) {
1190 		*op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1191 		return 0;
1192 	} else if (strcmp("DECRYPT", optarg) == 0) {
1193 		*op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1194 		return 0;
1195 	}
1196 
1197 	printf("AEAD operation specified not supported!\n");
1198 	return -1;
1199 }
1200 static int
1201 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1202 		const char *q_arg)
1203 {
1204 	char *end = NULL;
1205 	uint64_t pm;
1206 
1207 	/* parse hexadecimal string */
1208 	pm = strtoul(q_arg, &end, 16);
1209 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1210 		pm = 0;
1211 
1212 	options->cryptodev_mask = pm;
1213 	if (options->cryptodev_mask == 0) {
1214 		printf("invalid cryptodev_mask specified\n");
1215 		return -1;
1216 	}
1217 
1218 	return 0;
1219 }
1220 
1221 /** Parse long options */
1222 static int
1223 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1224 		struct option *lgopts, int option_index)
1225 {
1226 	int retval;
1227 
1228 	if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1229 		retval = parse_cryptodev_type(&options->type, optarg);
1230 		if (retval == 0)
1231 			snprintf(options->string_type, MAX_STR_LEN,
1232 				"%s", optarg);
1233 		return retval;
1234 	}
1235 
1236 	else if (strcmp(lgopts[option_index].name, "chain") == 0)
1237 		return parse_crypto_opt_chain(options, optarg);
1238 
1239 	/* Cipher options */
1240 	else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1241 		return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1242 				optarg);
1243 
1244 	else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1245 		return parse_cipher_op(&options->cipher_xform.cipher.op,
1246 				optarg);
1247 
1248 	else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1249 		options->ckey_param = 1;
1250 		options->cipher_xform.cipher.key.length =
1251 			parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1252 					MAX_KEY_SIZE);
1253 		if (options->cipher_xform.cipher.key.length > 0)
1254 			return 0;
1255 		else
1256 			return -1;
1257 	}
1258 
1259 	else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1260 		return parse_size(&options->ckey_random_size, optarg);
1261 
1262 	else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1263 		options->cipher_iv_param = 1;
1264 		options->cipher_iv.length =
1265 			parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1266 		if (options->cipher_iv.length > 0)
1267 			return 0;
1268 		else
1269 			return -1;
1270 	}
1271 
1272 	else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1273 		return parse_size(&options->cipher_iv_random_size, optarg);
1274 
1275 	/* Authentication options */
1276 	else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1277 		return parse_auth_algo(&options->auth_xform.auth.algo,
1278 				optarg);
1279 	}
1280 
1281 	else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1282 		return parse_auth_op(&options->auth_xform.auth.op,
1283 				optarg);
1284 
1285 	else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1286 		options->akey_param = 1;
1287 		options->auth_xform.auth.key.length =
1288 			parse_bytes(options->auth_xform.auth.key.data, optarg,
1289 					MAX_KEY_SIZE);
1290 		if (options->auth_xform.auth.key.length > 0)
1291 			return 0;
1292 		else
1293 			return -1;
1294 	}
1295 
1296 	else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1297 		return parse_size(&options->akey_random_size, optarg);
1298 	}
1299 
1300 	else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1301 		options->auth_iv_param = 1;
1302 		options->auth_iv.length =
1303 			parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1304 		if (options->auth_iv.length > 0)
1305 			return 0;
1306 		else
1307 			return -1;
1308 	}
1309 
1310 	else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1311 		return parse_size(&options->auth_iv_random_size, optarg);
1312 
1313 	/* AEAD options */
1314 	else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1315 		return parse_aead_algo(&options->aead_xform.aead.algo,
1316 				optarg);
1317 	}
1318 
1319 	else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1320 		return parse_aead_op(&options->aead_xform.aead.op,
1321 				optarg);
1322 
1323 	else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1324 		options->aead_key_param = 1;
1325 		options->aead_xform.aead.key.length =
1326 			parse_bytes(options->aead_xform.aead.key.data, optarg,
1327 					MAX_KEY_SIZE);
1328 		if (options->aead_xform.aead.key.length > 0)
1329 			return 0;
1330 		else
1331 			return -1;
1332 	}
1333 
1334 	else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1335 		return parse_size(&options->aead_key_random_size, optarg);
1336 
1337 
1338 	else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1339 		options->aead_iv_param = 1;
1340 		options->aead_iv.length =
1341 			parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1342 		if (options->aead_iv.length > 0)
1343 			return 0;
1344 		else
1345 			return -1;
1346 	}
1347 
1348 	else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1349 		return parse_size(&options->aead_iv_random_size, optarg);
1350 
1351 	else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1352 		options->aad_param = 1;
1353 		options->aad.length =
1354 			parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1355 		if (options->aad.length > 0)
1356 			return 0;
1357 		else
1358 			return -1;
1359 	}
1360 
1361 	else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1362 		return parse_size(&options->aad_random_size, optarg);
1363 	}
1364 
1365 	else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1366 		return parse_size(&options->digest_size, optarg);
1367 	}
1368 
1369 	else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1370 		options->sessionless = 1;
1371 		return 0;
1372 	}
1373 
1374 	else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1375 		return parse_cryptodev_mask(options, optarg);
1376 
1377 	else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1378 		options->mac_updating = 1;
1379 		return 0;
1380 	}
1381 
1382 	else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1383 		options->mac_updating = 0;
1384 		return 0;
1385 	}
1386 
1387 	return -1;
1388 }
1389 
1390 /** Parse port mask */
1391 static int
1392 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1393 		const char *q_arg)
1394 {
1395 	char *end = NULL;
1396 	unsigned long pm;
1397 
1398 	/* parse hexadecimal string */
1399 	pm = strtoul(q_arg, &end, 16);
1400 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1401 		pm = 0;
1402 
1403 	options->portmask = pm;
1404 	if (options->portmask == 0) {
1405 		printf("invalid portmask specified\n");
1406 		return -1;
1407 	}
1408 
1409 	return pm;
1410 }
1411 
1412 /** Parse number of queues */
1413 static int
1414 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1415 		const char *q_arg)
1416 {
1417 	char *end = NULL;
1418 	unsigned long n;
1419 
1420 	/* parse hexadecimal string */
1421 	n = strtoul(q_arg, &end, 10);
1422 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1423 		n = 0;
1424 	else if (n >= MAX_RX_QUEUE_PER_LCORE)
1425 		n = 0;
1426 
1427 	options->nb_ports_per_lcore = n;
1428 	if (options->nb_ports_per_lcore == 0) {
1429 		printf("invalid number of ports selected\n");
1430 		return -1;
1431 	}
1432 
1433 	return 0;
1434 }
1435 
1436 /** Parse timer period */
1437 static int
1438 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1439 		const char *q_arg)
1440 {
1441 	char *end = NULL;
1442 	unsigned long n;
1443 
1444 	/* parse number string */
1445 	n = (unsigned)strtol(q_arg, &end, 10);
1446 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1447 		n = 0;
1448 
1449 	if (n >= MAX_TIMER_PERIOD) {
1450 		printf("Warning refresh period specified %lu is greater than "
1451 				"max value %lu! using max value",
1452 				n, MAX_TIMER_PERIOD);
1453 		n = MAX_TIMER_PERIOD;
1454 	}
1455 
1456 	options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1457 
1458 	return 0;
1459 }
1460 
1461 /** Generate default options for application */
1462 static void
1463 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1464 {
1465 	options->portmask = 0xffffffff;
1466 	options->nb_ports_per_lcore = 1;
1467 	options->refresh_period = 10000;
1468 	options->single_lcore = 0;
1469 	options->sessionless = 0;
1470 
1471 	options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1472 
1473 	/* Cipher Data */
1474 	options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1475 	options->cipher_xform.next = NULL;
1476 	options->ckey_param = 0;
1477 	options->ckey_random_size = -1;
1478 	options->cipher_xform.cipher.key.length = 0;
1479 	options->cipher_iv_param = 0;
1480 	options->cipher_iv_random_size = -1;
1481 	options->cipher_iv.length = 0;
1482 
1483 	options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1484 	options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1485 
1486 	/* Authentication Data */
1487 	options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1488 	options->auth_xform.next = NULL;
1489 	options->akey_param = 0;
1490 	options->akey_random_size = -1;
1491 	options->auth_xform.auth.key.length = 0;
1492 	options->auth_iv_param = 0;
1493 	options->auth_iv_random_size = -1;
1494 	options->auth_iv.length = 0;
1495 
1496 	options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1497 	options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1498 
1499 	/* AEAD Data */
1500 	options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1501 	options->aead_xform.next = NULL;
1502 	options->aead_key_param = 0;
1503 	options->aead_key_random_size = -1;
1504 	options->aead_xform.aead.key.length = 0;
1505 	options->aead_iv_param = 0;
1506 	options->aead_iv_random_size = -1;
1507 	options->aead_iv.length = 0;
1508 
1509 	options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1510 	options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1511 
1512 	options->aad_param = 0;
1513 	options->aad_random_size = -1;
1514 	options->aad.length = 0;
1515 
1516 	options->digest_size = -1;
1517 
1518 	options->type = CDEV_TYPE_ANY;
1519 	options->cryptodev_mask = UINT64_MAX;
1520 
1521 	options->mac_updating = 1;
1522 }
1523 
1524 static void
1525 display_cipher_info(struct l2fwd_crypto_options *options)
1526 {
1527 	printf("\n---- Cipher information ---\n");
1528 	printf("Algorithm: %s\n",
1529 		rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1530 	rte_hexdump(stdout, "Cipher key:",
1531 			options->cipher_xform.cipher.key.data,
1532 			options->cipher_xform.cipher.key.length);
1533 	rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1534 }
1535 
1536 static void
1537 display_auth_info(struct l2fwd_crypto_options *options)
1538 {
1539 	printf("\n---- Authentication information ---\n");
1540 	printf("Algorithm: %s\n",
1541 		rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1542 	rte_hexdump(stdout, "Auth key:",
1543 			options->auth_xform.auth.key.data,
1544 			options->auth_xform.auth.key.length);
1545 	rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1546 }
1547 
1548 static void
1549 display_aead_info(struct l2fwd_crypto_options *options)
1550 {
1551 	printf("\n---- AEAD information ---\n");
1552 	printf("Algorithm: %s\n",
1553 		rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1554 	rte_hexdump(stdout, "AEAD key:",
1555 			options->aead_xform.aead.key.data,
1556 			options->aead_xform.aead.key.length);
1557 	rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1558 	rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1559 }
1560 
1561 static void
1562 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1563 {
1564 	char string_cipher_op[MAX_STR_LEN];
1565 	char string_auth_op[MAX_STR_LEN];
1566 	char string_aead_op[MAX_STR_LEN];
1567 
1568 	if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1569 		strcpy(string_cipher_op, "Encrypt");
1570 	else
1571 		strcpy(string_cipher_op, "Decrypt");
1572 
1573 	if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1574 		strcpy(string_auth_op, "Auth generate");
1575 	else
1576 		strcpy(string_auth_op, "Auth verify");
1577 
1578 	if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1579 		strcpy(string_aead_op, "Authenticated encryption");
1580 	else
1581 		strcpy(string_aead_op, "Authenticated decryption");
1582 
1583 
1584 	printf("Options:-\nn");
1585 	printf("portmask: %x\n", options->portmask);
1586 	printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1587 	printf("refresh period : %u\n", options->refresh_period);
1588 	printf("single lcore mode: %s\n",
1589 			options->single_lcore ? "enabled" : "disabled");
1590 	printf("stats_printing: %s\n",
1591 			options->refresh_period == 0 ? "disabled" : "enabled");
1592 
1593 	printf("sessionless crypto: %s\n",
1594 			options->sessionless ? "enabled" : "disabled");
1595 
1596 	if (options->ckey_param && (options->ckey_random_size != -1))
1597 		printf("Cipher key already parsed, ignoring size of random key\n");
1598 
1599 	if (options->akey_param && (options->akey_random_size != -1))
1600 		printf("Auth key already parsed, ignoring size of random key\n");
1601 
1602 	if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1603 		printf("Cipher IV already parsed, ignoring size of random IV\n");
1604 
1605 	if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1606 		printf("Auth IV already parsed, ignoring size of random IV\n");
1607 
1608 	if (options->aad_param && (options->aad_random_size != -1))
1609 		printf("AAD already parsed, ignoring size of random AAD\n");
1610 
1611 	printf("\nCrypto chain: ");
1612 	switch (options->xform_chain) {
1613 	case L2FWD_CRYPTO_AEAD:
1614 		printf("Input --> %s --> Output\n", string_aead_op);
1615 		display_aead_info(options);
1616 		break;
1617 	case L2FWD_CRYPTO_CIPHER_HASH:
1618 		printf("Input --> %s --> %s --> Output\n",
1619 			string_cipher_op, string_auth_op);
1620 		display_cipher_info(options);
1621 		display_auth_info(options);
1622 		break;
1623 	case L2FWD_CRYPTO_HASH_CIPHER:
1624 		printf("Input --> %s --> %s --> Output\n",
1625 			string_auth_op, string_cipher_op);
1626 		display_cipher_info(options);
1627 		display_auth_info(options);
1628 		break;
1629 	case L2FWD_CRYPTO_HASH_ONLY:
1630 		printf("Input --> %s --> Output\n", string_auth_op);
1631 		display_auth_info(options);
1632 		break;
1633 	case L2FWD_CRYPTO_CIPHER_ONLY:
1634 		printf("Input --> %s --> Output\n", string_cipher_op);
1635 		display_cipher_info(options);
1636 		break;
1637 	}
1638 }
1639 
1640 /* Parse the argument given in the command line of the application */
1641 static int
1642 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1643 		int argc, char **argv)
1644 {
1645 	int opt, retval, option_index;
1646 	char **argvopt = argv, *prgname = argv[0];
1647 
1648 	static struct option lgopts[] = {
1649 			{ "sessionless", no_argument, 0, 0 },
1650 
1651 			{ "cdev_type", required_argument, 0, 0 },
1652 			{ "chain", required_argument, 0, 0 },
1653 
1654 			{ "cipher_algo", required_argument, 0, 0 },
1655 			{ "cipher_op", required_argument, 0, 0 },
1656 			{ "cipher_key", required_argument, 0, 0 },
1657 			{ "cipher_key_random_size", required_argument, 0, 0 },
1658 			{ "cipher_iv", required_argument, 0, 0 },
1659 			{ "cipher_iv_random_size", required_argument, 0, 0 },
1660 
1661 			{ "auth_algo", required_argument, 0, 0 },
1662 			{ "auth_op", required_argument, 0, 0 },
1663 			{ "auth_key", required_argument, 0, 0 },
1664 			{ "auth_key_random_size", required_argument, 0, 0 },
1665 			{ "auth_iv", required_argument, 0, 0 },
1666 			{ "auth_iv_random_size", required_argument, 0, 0 },
1667 
1668 			{ "aead_algo", required_argument, 0, 0 },
1669 			{ "aead_op", required_argument, 0, 0 },
1670 			{ "aead_key", required_argument, 0, 0 },
1671 			{ "aead_key_random_size", required_argument, 0, 0 },
1672 			{ "aead_iv", required_argument, 0, 0 },
1673 			{ "aead_iv_random_size", required_argument, 0, 0 },
1674 
1675 			{ "aad", required_argument, 0, 0 },
1676 			{ "aad_random_size", required_argument, 0, 0 },
1677 
1678 			{ "digest_size", required_argument, 0, 0 },
1679 
1680 			{ "sessionless", no_argument, 0, 0 },
1681 			{ "cryptodev_mask", required_argument, 0, 0},
1682 
1683 			{ "mac-updating", no_argument, 0, 0},
1684 			{ "no-mac-updating", no_argument, 0, 0},
1685 
1686 			{ NULL, 0, 0, 0 }
1687 	};
1688 
1689 	l2fwd_crypto_default_options(options);
1690 
1691 	while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1692 			&option_index)) != EOF) {
1693 		switch (opt) {
1694 		/* long options */
1695 		case 0:
1696 			retval = l2fwd_crypto_parse_args_long_options(options,
1697 					lgopts, option_index);
1698 			if (retval < 0) {
1699 				l2fwd_crypto_usage(prgname);
1700 				return -1;
1701 			}
1702 			break;
1703 
1704 		/* portmask */
1705 		case 'p':
1706 			retval = l2fwd_crypto_parse_portmask(options, optarg);
1707 			if (retval < 0) {
1708 				l2fwd_crypto_usage(prgname);
1709 				return -1;
1710 			}
1711 			break;
1712 
1713 		/* nqueue */
1714 		case 'q':
1715 			retval = l2fwd_crypto_parse_nqueue(options, optarg);
1716 			if (retval < 0) {
1717 				l2fwd_crypto_usage(prgname);
1718 				return -1;
1719 			}
1720 			break;
1721 
1722 		/* single  */
1723 		case 's':
1724 			options->single_lcore = 1;
1725 
1726 			break;
1727 
1728 		/* timer period */
1729 		case 'T':
1730 			retval = l2fwd_crypto_parse_timer_period(options,
1731 					optarg);
1732 			if (retval < 0) {
1733 				l2fwd_crypto_usage(prgname);
1734 				return -1;
1735 			}
1736 			break;
1737 
1738 		default:
1739 			l2fwd_crypto_usage(prgname);
1740 			return -1;
1741 		}
1742 	}
1743 
1744 
1745 	if (optind >= 0)
1746 		argv[optind-1] = prgname;
1747 
1748 	retval = optind-1;
1749 	optind = 1; /* reset getopt lib */
1750 
1751 	return retval;
1752 }
1753 
1754 /* Check the link status of all ports in up to 9s, and print them finally */
1755 static void
1756 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1757 {
1758 #define CHECK_INTERVAL 100 /* 100ms */
1759 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1760 	uint16_t portid;
1761 	uint8_t count, all_ports_up, print_flag = 0;
1762 	struct rte_eth_link link;
1763 
1764 	printf("\nChecking link status");
1765 	fflush(stdout);
1766 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1767 		all_ports_up = 1;
1768 		for (portid = 0; portid < port_num; portid++) {
1769 			if ((port_mask & (1 << portid)) == 0)
1770 				continue;
1771 			memset(&link, 0, sizeof(link));
1772 			rte_eth_link_get_nowait(portid, &link);
1773 			/* print link status if flag set */
1774 			if (print_flag == 1) {
1775 				if (link.link_status)
1776 					printf(
1777 					"Port%d Link Up. Speed %u Mbps - %s\n",
1778 						portid, link.link_speed,
1779 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1780 					("full-duplex") : ("half-duplex\n"));
1781 				else
1782 					printf("Port %d Link Down\n", portid);
1783 				continue;
1784 			}
1785 			/* clear all_ports_up flag if any link down */
1786 			if (link.link_status == ETH_LINK_DOWN) {
1787 				all_ports_up = 0;
1788 				break;
1789 			}
1790 		}
1791 		/* after finally printing all link status, get out */
1792 		if (print_flag == 1)
1793 			break;
1794 
1795 		if (all_ports_up == 0) {
1796 			printf(".");
1797 			fflush(stdout);
1798 			rte_delay_ms(CHECK_INTERVAL);
1799 		}
1800 
1801 		/* set the print_flag if all ports up or timeout */
1802 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1803 			print_flag = 1;
1804 			printf("done\n");
1805 		}
1806 	}
1807 }
1808 
1809 /* Check if device has to be HW/SW or any */
1810 static int
1811 check_type(const struct l2fwd_crypto_options *options,
1812 		const struct rte_cryptodev_info *dev_info)
1813 {
1814 	if (options->type == CDEV_TYPE_HW &&
1815 			(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1816 		return 0;
1817 	if (options->type == CDEV_TYPE_SW &&
1818 			!(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1819 		return 0;
1820 	if (options->type == CDEV_TYPE_ANY)
1821 		return 0;
1822 
1823 	return -1;
1824 }
1825 
1826 static const struct rte_cryptodev_capabilities *
1827 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1828 		const struct rte_cryptodev_info *dev_info,
1829 		uint8_t cdev_id)
1830 {
1831 	unsigned int i = 0;
1832 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1833 	enum rte_crypto_cipher_algorithm cap_cipher_algo;
1834 	enum rte_crypto_cipher_algorithm opt_cipher_algo =
1835 					options->cipher_xform.cipher.algo;
1836 
1837 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1838 		cap_cipher_algo = cap->sym.cipher.algo;
1839 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1840 			if (cap_cipher_algo == opt_cipher_algo) {
1841 				if (check_type(options, dev_info) == 0)
1842 					break;
1843 			}
1844 		}
1845 		cap = &dev_info->capabilities[++i];
1846 	}
1847 
1848 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1849 		printf("Algorithm %s not supported by cryptodev %u"
1850 			" or device not of preferred type (%s)\n",
1851 			rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1852 			cdev_id,
1853 			options->string_type);
1854 		return NULL;
1855 	}
1856 
1857 	return cap;
1858 }
1859 
1860 static const struct rte_cryptodev_capabilities *
1861 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1862 		const struct rte_cryptodev_info *dev_info,
1863 		uint8_t cdev_id)
1864 {
1865 	unsigned int i = 0;
1866 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1867 	enum rte_crypto_auth_algorithm cap_auth_algo;
1868 	enum rte_crypto_auth_algorithm opt_auth_algo =
1869 					options->auth_xform.auth.algo;
1870 
1871 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1872 		cap_auth_algo = cap->sym.auth.algo;
1873 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1874 			if (cap_auth_algo == opt_auth_algo) {
1875 				if (check_type(options, dev_info) == 0)
1876 					break;
1877 			}
1878 		}
1879 		cap = &dev_info->capabilities[++i];
1880 	}
1881 
1882 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1883 		printf("Algorithm %s not supported by cryptodev %u"
1884 			" or device not of preferred type (%s)\n",
1885 			rte_crypto_auth_algorithm_strings[opt_auth_algo],
1886 			cdev_id,
1887 			options->string_type);
1888 		return NULL;
1889 	}
1890 
1891 	return cap;
1892 }
1893 
1894 static const struct rte_cryptodev_capabilities *
1895 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1896 		const struct rte_cryptodev_info *dev_info,
1897 		uint8_t cdev_id)
1898 {
1899 	unsigned int i = 0;
1900 	const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1901 	enum rte_crypto_aead_algorithm cap_aead_algo;
1902 	enum rte_crypto_aead_algorithm opt_aead_algo =
1903 					options->aead_xform.aead.algo;
1904 
1905 	while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1906 		cap_aead_algo = cap->sym.aead.algo;
1907 		if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1908 			if (cap_aead_algo == opt_aead_algo) {
1909 				if (check_type(options, dev_info) == 0)
1910 					break;
1911 			}
1912 		}
1913 		cap = &dev_info->capabilities[++i];
1914 	}
1915 
1916 	if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1917 		printf("Algorithm %s not supported by cryptodev %u"
1918 			" or device not of preferred type (%s)\n",
1919 			rte_crypto_aead_algorithm_strings[opt_aead_algo],
1920 			cdev_id,
1921 			options->string_type);
1922 		return NULL;
1923 	}
1924 
1925 	return cap;
1926 }
1927 
1928 /* Check if the device is enabled by cryptodev_mask */
1929 static int
1930 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1931 		uint8_t cdev_id)
1932 {
1933 	if (options->cryptodev_mask & (1 << cdev_id))
1934 		return 0;
1935 
1936 	return -1;
1937 }
1938 
1939 static inline int
1940 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1941 		uint16_t increment)
1942 {
1943 	uint16_t supp_size;
1944 
1945 	/* Single value */
1946 	if (increment == 0) {
1947 		if (length == min)
1948 			return 0;
1949 		else
1950 			return -1;
1951 	}
1952 
1953 	/* Range of values */
1954 	for (supp_size = min; supp_size <= max; supp_size += increment) {
1955 		if (length == supp_size)
1956 			return 0;
1957 	}
1958 
1959 	return -1;
1960 }
1961 
1962 static int
1963 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1964 		unsigned int iv_param, int iv_random_size,
1965 		uint16_t *iv_length)
1966 {
1967 	/*
1968 	 * Check if length of provided IV is supported
1969 	 * by the algorithm chosen.
1970 	 */
1971 	if (iv_param) {
1972 		if (check_supported_size(*iv_length,
1973 				iv_range_size->min,
1974 				iv_range_size->max,
1975 				iv_range_size->increment)
1976 					!= 0) {
1977 			printf("Unsupported IV length\n");
1978 			return -1;
1979 		}
1980 	/*
1981 	 * Check if length of IV to be randomly generated
1982 	 * is supported by the algorithm chosen.
1983 	 */
1984 	} else if (iv_random_size != -1) {
1985 		if (check_supported_size(iv_random_size,
1986 				iv_range_size->min,
1987 				iv_range_size->max,
1988 				iv_range_size->increment)
1989 					!= 0) {
1990 			printf("Unsupported IV length\n");
1991 			return -1;
1992 		}
1993 		*iv_length = iv_random_size;
1994 	/* No size provided, use minimum size. */
1995 	} else
1996 		*iv_length = iv_range_size->min;
1997 
1998 	return 0;
1999 }
2000 
2001 static int
2002 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2003 		uint8_t *enabled_cdevs)
2004 {
2005 	unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
2006 	const struct rte_cryptodev_capabilities *cap;
2007 	unsigned int sess_sz, max_sess_sz = 0;
2008 	int retval;
2009 
2010 	cdev_count = rte_cryptodev_count();
2011 	if (cdev_count == 0) {
2012 		printf("No crypto devices available\n");
2013 		return -1;
2014 	}
2015 
2016 	for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2017 		sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
2018 		if (sess_sz > max_sess_sz)
2019 			max_sess_sz = sess_sz;
2020 	}
2021 
2022 	for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2023 			cdev_id++) {
2024 		struct rte_cryptodev_qp_conf qp_conf;
2025 		struct rte_cryptodev_info dev_info;
2026 		retval = rte_cryptodev_socket_id(cdev_id);
2027 
2028 		if (retval < 0) {
2029 			printf("Invalid crypto device id used\n");
2030 			return -1;
2031 		}
2032 
2033 		uint8_t socket_id = (uint8_t) retval;
2034 
2035 		struct rte_cryptodev_config conf = {
2036 			.nb_queue_pairs = 1,
2037 			.socket_id = socket_id,
2038 		};
2039 
2040 		if (check_cryptodev_mask(options, (uint8_t)cdev_id))
2041 			continue;
2042 
2043 		rte_cryptodev_info_get(cdev_id, &dev_info);
2044 
2045 		if (session_pool_socket[socket_id] == NULL) {
2046 			char mp_name[RTE_MEMPOOL_NAMESIZE];
2047 			struct rte_mempool *sess_mp;
2048 
2049 			snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2050 				"sess_mp_%u", socket_id);
2051 
2052 			/*
2053 			 * Create enough objects for session headers and
2054 			 * device private data
2055 			 */
2056 			sess_mp = rte_mempool_create(mp_name,
2057 						MAX_SESSIONS * 2,
2058 						max_sess_sz,
2059 						SESSION_POOL_CACHE_SIZE,
2060 						0, NULL, NULL, NULL,
2061 						NULL, socket_id,
2062 						0);
2063 
2064 			if (sess_mp == NULL) {
2065 				printf("Cannot create session pool on socket %d\n",
2066 					socket_id);
2067 				return -ENOMEM;
2068 			}
2069 
2070 			printf("Allocated session pool on socket %d\n", socket_id);
2071 			session_pool_socket[socket_id] = sess_mp;
2072 		}
2073 
2074 		/* Set AEAD parameters */
2075 		if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2076 			/* Check if device supports AEAD algo */
2077 			cap = check_device_support_aead_algo(options, &dev_info,
2078 							cdev_id);
2079 			if (cap == NULL)
2080 				continue;
2081 
2082 			options->block_size = cap->sym.aead.block_size;
2083 
2084 			check_iv_param(&cap->sym.aead.iv_size,
2085 					options->aead_iv_param,
2086 					options->aead_iv_random_size,
2087 					&options->aead_iv.length);
2088 
2089 			/*
2090 			 * Check if length of provided AEAD key is supported
2091 			 * by the algorithm chosen.
2092 			 */
2093 			if (options->aead_key_param) {
2094 				if (check_supported_size(
2095 						options->aead_xform.aead.key.length,
2096 						cap->sym.aead.key_size.min,
2097 						cap->sym.aead.key_size.max,
2098 						cap->sym.aead.key_size.increment)
2099 							!= 0) {
2100 					printf("Unsupported aead key length\n");
2101 					return -1;
2102 				}
2103 			/*
2104 			 * Check if length of the aead key to be randomly generated
2105 			 * is supported by the algorithm chosen.
2106 			 */
2107 			} else if (options->aead_key_random_size != -1) {
2108 				if (check_supported_size(options->aead_key_random_size,
2109 						cap->sym.aead.key_size.min,
2110 						cap->sym.aead.key_size.max,
2111 						cap->sym.aead.key_size.increment)
2112 							!= 0) {
2113 					printf("Unsupported aead key length\n");
2114 					return -1;
2115 				}
2116 				options->aead_xform.aead.key.length =
2117 							options->aead_key_random_size;
2118 			/* No size provided, use minimum size. */
2119 			} else
2120 				options->aead_xform.aead.key.length =
2121 						cap->sym.aead.key_size.min;
2122 
2123 			if (!options->aead_key_param)
2124 				generate_random_key(
2125 					options->aead_xform.aead.key.data,
2126 					options->aead_xform.aead.key.length);
2127 
2128 			/*
2129 			 * Check if length of provided AAD is supported
2130 			 * by the algorithm chosen.
2131 			 */
2132 			if (options->aad_param) {
2133 				if (check_supported_size(options->aad.length,
2134 						cap->sym.aead.aad_size.min,
2135 						cap->sym.aead.aad_size.max,
2136 						cap->sym.aead.aad_size.increment)
2137 							!= 0) {
2138 					printf("Unsupported AAD length\n");
2139 					return -1;
2140 				}
2141 			/*
2142 			 * Check if length of AAD to be randomly generated
2143 			 * is supported by the algorithm chosen.
2144 			 */
2145 			} else if (options->aad_random_size != -1) {
2146 				if (check_supported_size(options->aad_random_size,
2147 						cap->sym.aead.aad_size.min,
2148 						cap->sym.aead.aad_size.max,
2149 						cap->sym.aead.aad_size.increment)
2150 							!= 0) {
2151 					printf("Unsupported AAD length\n");
2152 					return -1;
2153 				}
2154 				options->aad.length = options->aad_random_size;
2155 			/* No size provided, use minimum size. */
2156 			} else
2157 				options->aad.length = cap->sym.auth.aad_size.min;
2158 
2159 			options->aead_xform.aead.aad_length =
2160 						options->aad.length;
2161 
2162 			/* Check if digest size is supported by the algorithm. */
2163 			if (options->digest_size != -1) {
2164 				if (check_supported_size(options->digest_size,
2165 						cap->sym.aead.digest_size.min,
2166 						cap->sym.aead.digest_size.max,
2167 						cap->sym.aead.digest_size.increment)
2168 							!= 0) {
2169 					printf("Unsupported digest length\n");
2170 					return -1;
2171 				}
2172 				options->aead_xform.aead.digest_length =
2173 							options->digest_size;
2174 			/* No size provided, use minimum size. */
2175 			} else
2176 				options->aead_xform.aead.digest_length =
2177 						cap->sym.aead.digest_size.min;
2178 		}
2179 
2180 		/* Set cipher parameters */
2181 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2182 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2183 				options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2184 			/* Check if device supports cipher algo */
2185 			cap = check_device_support_cipher_algo(options, &dev_info,
2186 							cdev_id);
2187 			if (cap == NULL)
2188 				continue;
2189 
2190 			options->block_size = cap->sym.cipher.block_size;
2191 
2192 			check_iv_param(&cap->sym.cipher.iv_size,
2193 					options->cipher_iv_param,
2194 					options->cipher_iv_random_size,
2195 					&options->cipher_iv.length);
2196 
2197 			/*
2198 			 * Check if length of provided cipher key is supported
2199 			 * by the algorithm chosen.
2200 			 */
2201 			if (options->ckey_param) {
2202 				if (check_supported_size(
2203 						options->cipher_xform.cipher.key.length,
2204 						cap->sym.cipher.key_size.min,
2205 						cap->sym.cipher.key_size.max,
2206 						cap->sym.cipher.key_size.increment)
2207 							!= 0) {
2208 					printf("Unsupported cipher key length\n");
2209 					return -1;
2210 				}
2211 			/*
2212 			 * Check if length of the cipher key to be randomly generated
2213 			 * is supported by the algorithm chosen.
2214 			 */
2215 			} else if (options->ckey_random_size != -1) {
2216 				if (check_supported_size(options->ckey_random_size,
2217 						cap->sym.cipher.key_size.min,
2218 						cap->sym.cipher.key_size.max,
2219 						cap->sym.cipher.key_size.increment)
2220 							!= 0) {
2221 					printf("Unsupported cipher key length\n");
2222 					return -1;
2223 				}
2224 				options->cipher_xform.cipher.key.length =
2225 							options->ckey_random_size;
2226 			/* No size provided, use minimum size. */
2227 			} else
2228 				options->cipher_xform.cipher.key.length =
2229 						cap->sym.cipher.key_size.min;
2230 
2231 			if (!options->ckey_param)
2232 				generate_random_key(
2233 					options->cipher_xform.cipher.key.data,
2234 					options->cipher_xform.cipher.key.length);
2235 
2236 		}
2237 
2238 		/* Set auth parameters */
2239 		if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2240 				options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2241 				options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2242 			/* Check if device supports auth algo */
2243 			cap = check_device_support_auth_algo(options, &dev_info,
2244 							cdev_id);
2245 			if (cap == NULL)
2246 				continue;
2247 
2248 			check_iv_param(&cap->sym.auth.iv_size,
2249 					options->auth_iv_param,
2250 					options->auth_iv_random_size,
2251 					&options->auth_iv.length);
2252 			/*
2253 			 * Check if length of provided auth key is supported
2254 			 * by the algorithm chosen.
2255 			 */
2256 			if (options->akey_param) {
2257 				if (check_supported_size(
2258 						options->auth_xform.auth.key.length,
2259 						cap->sym.auth.key_size.min,
2260 						cap->sym.auth.key_size.max,
2261 						cap->sym.auth.key_size.increment)
2262 							!= 0) {
2263 					printf("Unsupported auth key length\n");
2264 					return -1;
2265 				}
2266 			/*
2267 			 * Check if length of the auth key to be randomly generated
2268 			 * is supported by the algorithm chosen.
2269 			 */
2270 			} else if (options->akey_random_size != -1) {
2271 				if (check_supported_size(options->akey_random_size,
2272 						cap->sym.auth.key_size.min,
2273 						cap->sym.auth.key_size.max,
2274 						cap->sym.auth.key_size.increment)
2275 							!= 0) {
2276 					printf("Unsupported auth key length\n");
2277 					return -1;
2278 				}
2279 				options->auth_xform.auth.key.length =
2280 							options->akey_random_size;
2281 			/* No size provided, use minimum size. */
2282 			} else
2283 				options->auth_xform.auth.key.length =
2284 						cap->sym.auth.key_size.min;
2285 
2286 			if (!options->akey_param)
2287 				generate_random_key(
2288 					options->auth_xform.auth.key.data,
2289 					options->auth_xform.auth.key.length);
2290 
2291 			/* Check if digest size is supported by the algorithm. */
2292 			if (options->digest_size != -1) {
2293 				if (check_supported_size(options->digest_size,
2294 						cap->sym.auth.digest_size.min,
2295 						cap->sym.auth.digest_size.max,
2296 						cap->sym.auth.digest_size.increment)
2297 							!= 0) {
2298 					printf("Unsupported digest length\n");
2299 					return -1;
2300 				}
2301 				options->auth_xform.auth.digest_length =
2302 							options->digest_size;
2303 			/* No size provided, use minimum size. */
2304 			} else
2305 				options->auth_xform.auth.digest_length =
2306 						cap->sym.auth.digest_size.min;
2307 		}
2308 
2309 		retval = rte_cryptodev_configure(cdev_id, &conf);
2310 		if (retval < 0) {
2311 			printf("Failed to configure cryptodev %u", cdev_id);
2312 			return -1;
2313 		}
2314 
2315 		qp_conf.nb_descriptors = 2048;
2316 
2317 		retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2318 				socket_id, session_pool_socket[socket_id]);
2319 		if (retval < 0) {
2320 			printf("Failed to setup queue pair %u on cryptodev %u",
2321 					0, cdev_id);
2322 			return -1;
2323 		}
2324 
2325 		retval = rte_cryptodev_start(cdev_id);
2326 		if (retval < 0) {
2327 			printf("Failed to start device %u: error %d\n",
2328 					cdev_id, retval);
2329 			return -1;
2330 		}
2331 
2332 		l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2333 
2334 		enabled_cdevs[cdev_id] = 1;
2335 		enabled_cdev_count++;
2336 	}
2337 
2338 	return enabled_cdev_count;
2339 }
2340 
2341 static int
2342 initialize_ports(struct l2fwd_crypto_options *options)
2343 {
2344 	uint16_t last_portid, portid;
2345 	unsigned enabled_portcount = 0;
2346 	unsigned nb_ports = rte_eth_dev_count();
2347 
2348 	if (nb_ports == 0) {
2349 		printf("No Ethernet ports - bye\n");
2350 		return -1;
2351 	}
2352 
2353 	/* Reset l2fwd_dst_ports */
2354 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2355 		l2fwd_dst_ports[portid] = 0;
2356 
2357 	for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2358 		int retval;
2359 
2360 		/* Skip ports that are not enabled */
2361 		if ((options->portmask & (1 << portid)) == 0)
2362 			continue;
2363 
2364 		/* init port */
2365 		printf("Initializing port %u... ", portid);
2366 		fflush(stdout);
2367 		retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2368 		if (retval < 0) {
2369 			printf("Cannot configure device: err=%d, port=%u\n",
2370 				  retval, portid);
2371 			return -1;
2372 		}
2373 
2374 		retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2375 							  &nb_txd);
2376 		if (retval < 0) {
2377 			printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2378 				retval, portid);
2379 			return -1;
2380 		}
2381 
2382 		/* init one RX queue */
2383 		fflush(stdout);
2384 		retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2385 					     rte_eth_dev_socket_id(portid),
2386 					     NULL, l2fwd_pktmbuf_pool);
2387 		if (retval < 0) {
2388 			printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2389 					retval, portid);
2390 			return -1;
2391 		}
2392 
2393 		/* init one TX queue on each port */
2394 		fflush(stdout);
2395 		retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2396 				rte_eth_dev_socket_id(portid),
2397 				NULL);
2398 		if (retval < 0) {
2399 			printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2400 				retval, portid);
2401 
2402 			return -1;
2403 		}
2404 
2405 		/* Start device */
2406 		retval = rte_eth_dev_start(portid);
2407 		if (retval < 0) {
2408 			printf("rte_eth_dev_start:err=%d, port=%u\n",
2409 					retval, portid);
2410 			return -1;
2411 		}
2412 
2413 		rte_eth_promiscuous_enable(portid);
2414 
2415 		rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2416 
2417 		printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2418 				portid,
2419 				l2fwd_ports_eth_addr[portid].addr_bytes[0],
2420 				l2fwd_ports_eth_addr[portid].addr_bytes[1],
2421 				l2fwd_ports_eth_addr[portid].addr_bytes[2],
2422 				l2fwd_ports_eth_addr[portid].addr_bytes[3],
2423 				l2fwd_ports_eth_addr[portid].addr_bytes[4],
2424 				l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2425 
2426 		/* initialize port stats */
2427 		memset(&port_statistics, 0, sizeof(port_statistics));
2428 
2429 		/* Setup port forwarding table */
2430 		if (enabled_portcount % 2) {
2431 			l2fwd_dst_ports[portid] = last_portid;
2432 			l2fwd_dst_ports[last_portid] = portid;
2433 		} else {
2434 			last_portid = portid;
2435 		}
2436 
2437 		l2fwd_enabled_port_mask |= (1 << portid);
2438 		enabled_portcount++;
2439 	}
2440 
2441 	if (enabled_portcount == 1) {
2442 		l2fwd_dst_ports[last_portid] = last_portid;
2443 	} else if (enabled_portcount % 2) {
2444 		printf("odd number of ports in portmask- bye\n");
2445 		return -1;
2446 	}
2447 
2448 	check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2449 
2450 	return enabled_portcount;
2451 }
2452 
2453 static void
2454 reserve_key_memory(struct l2fwd_crypto_options *options)
2455 {
2456 	options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2457 						MAX_KEY_SIZE, 0);
2458 	if (options->cipher_xform.cipher.key.data == NULL)
2459 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2460 
2461 	options->auth_xform.auth.key.data = rte_malloc("auth key",
2462 						MAX_KEY_SIZE, 0);
2463 	if (options->auth_xform.auth.key.data == NULL)
2464 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2465 
2466 	options->aead_xform.aead.key.data = rte_malloc("aead key",
2467 						MAX_KEY_SIZE, 0);
2468 	if (options->aead_xform.aead.key.data == NULL)
2469 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2470 
2471 	options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2472 	if (options->cipher_iv.data == NULL)
2473 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2474 
2475 	options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2476 	if (options->auth_iv.data == NULL)
2477 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2478 
2479 	options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2480 	if (options->aead_iv.data == NULL)
2481 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2482 
2483 	options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2484 	if (options->aad.data == NULL)
2485 		rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2486 	options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2487 }
2488 
2489 int
2490 main(int argc, char **argv)
2491 {
2492 	struct lcore_queue_conf *qconf;
2493 	struct l2fwd_crypto_options options;
2494 
2495 	uint8_t nb_cryptodevs, cdev_id;
2496 	uint16_t nb_ports, portid;
2497 	unsigned lcore_id, rx_lcore_id;
2498 	int ret, enabled_cdevcount, enabled_portcount;
2499 	uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2500 
2501 	/* init EAL */
2502 	ret = rte_eal_init(argc, argv);
2503 	if (ret < 0)
2504 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2505 	argc -= ret;
2506 	argv += ret;
2507 
2508 	/* reserve memory for Cipher/Auth key and IV */
2509 	reserve_key_memory(&options);
2510 
2511 	/* parse application arguments (after the EAL ones) */
2512 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
2513 	if (ret < 0)
2514 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2515 
2516 	printf("MAC updating %s\n",
2517 			options.mac_updating ? "enabled" : "disabled");
2518 
2519 	/* create the mbuf pool */
2520 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2521 			sizeof(struct rte_crypto_op),
2522 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2523 	if (l2fwd_pktmbuf_pool == NULL)
2524 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2525 
2526 	/* create crypto op pool */
2527 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2528 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2529 			rte_socket_id());
2530 	if (l2fwd_crypto_op_pool == NULL)
2531 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2532 
2533 	/* Enable Ethernet ports */
2534 	enabled_portcount = initialize_ports(&options);
2535 	if (enabled_portcount < 1)
2536 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2537 
2538 	nb_ports = rte_eth_dev_count();
2539 	/* Initialize the port/queue configuration of each logical core */
2540 	for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2541 			portid < nb_ports; portid++) {
2542 
2543 		/* skip ports that are not enabled */
2544 		if ((options.portmask & (1 << portid)) == 0)
2545 			continue;
2546 
2547 		if (options.single_lcore && qconf == NULL) {
2548 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2549 				rx_lcore_id++;
2550 				if (rx_lcore_id >= RTE_MAX_LCORE)
2551 					rte_exit(EXIT_FAILURE,
2552 							"Not enough cores\n");
2553 			}
2554 		} else if (!options.single_lcore) {
2555 			/* get the lcore_id for this port */
2556 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2557 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2558 			       options.nb_ports_per_lcore) {
2559 				rx_lcore_id++;
2560 				if (rx_lcore_id >= RTE_MAX_LCORE)
2561 					rte_exit(EXIT_FAILURE,
2562 							"Not enough cores\n");
2563 			}
2564 		}
2565 
2566 		/* Assigned a new logical core in the loop above. */
2567 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2568 			qconf = &lcore_queue_conf[rx_lcore_id];
2569 
2570 		qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2571 		qconf->nb_rx_ports++;
2572 
2573 		printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2574 	}
2575 
2576 	/* Enable Crypto devices */
2577 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2578 			enabled_cdevs);
2579 	if (enabled_cdevcount < 0)
2580 		rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2581 
2582 	if (enabled_cdevcount < enabled_portcount)
2583 		rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2584 				"has to be more or equal to number of ports (%d)\n",
2585 				enabled_cdevcount, enabled_portcount);
2586 
2587 	nb_cryptodevs = rte_cryptodev_count();
2588 
2589 	/* Initialize the port/cryptodev configuration of each logical core */
2590 	for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2591 			cdev_id < nb_cryptodevs && enabled_cdevcount;
2592 			cdev_id++) {
2593 		/* Crypto op not supported by crypto device */
2594 		if (!enabled_cdevs[cdev_id])
2595 			continue;
2596 
2597 		if (options.single_lcore && qconf == NULL) {
2598 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2599 				rx_lcore_id++;
2600 				if (rx_lcore_id >= RTE_MAX_LCORE)
2601 					rte_exit(EXIT_FAILURE,
2602 							"Not enough cores\n");
2603 			}
2604 		} else if (!options.single_lcore) {
2605 			/* get the lcore_id for this port */
2606 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2607 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2608 			       options.nb_ports_per_lcore) {
2609 				rx_lcore_id++;
2610 				if (rx_lcore_id >= RTE_MAX_LCORE)
2611 					rte_exit(EXIT_FAILURE,
2612 							"Not enough cores\n");
2613 			}
2614 		}
2615 
2616 		/* Assigned a new logical core in the loop above. */
2617 		if (qconf != &lcore_queue_conf[rx_lcore_id])
2618 			qconf = &lcore_queue_conf[rx_lcore_id];
2619 
2620 		qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2621 		qconf->nb_crypto_devs++;
2622 
2623 		enabled_cdevcount--;
2624 
2625 		printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2626 				(unsigned)cdev_id);
2627 	}
2628 
2629 	/* launch per-lcore init on every lcore */
2630 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2631 			CALL_MASTER);
2632 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2633 		if (rte_eal_wait_lcore(lcore_id) < 0)
2634 			return -1;
2635 	}
2636 
2637 	return 0;
2638 }
2639