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