xref: /dpdk/examples/l2fwd-crypto/main.c (revision 3c96262cf8ea9c282983d973991e57b6c4a39f2c)
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 
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_common.h>
52 #include <rte_cryptodev.h>
53 #include <rte_cycles.h>
54 #include <rte_debug.h>
55 #include <rte_eal.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_interrupts.h>
59 #include <rte_ip.h>
60 #include <rte_launch.h>
61 #include <rte_lcore.h>
62 #include <rte_log.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_memcpy.h>
66 #include <rte_memory.h>
67 #include <rte_mempool.h>
68 #include <rte_memzone.h>
69 #include <rte_pci.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
73 #include <rte_ring.h>
74 
75 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
76 
77 #define NB_MBUF   8192
78 
79 #define MAX_PKT_BURST 32
80 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
81 
82 /*
83  * Configurable number of RX/TX ring descriptors
84  */
85 #define RTE_TEST_RX_DESC_DEFAULT 128
86 #define RTE_TEST_TX_DESC_DEFAULT 512
87 
88 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
89 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
90 
91 /* ethernet addresses of ports */
92 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
93 
94 /* mask of enabled ports */
95 static uint64_t l2fwd_enabled_port_mask;
96 static uint64_t l2fwd_enabled_crypto_mask;
97 
98 /* list of enabled ports */
99 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
100 
101 
102 struct pkt_buffer {
103 	unsigned len;
104 	struct rte_mbuf *buffer[MAX_PKT_BURST];
105 };
106 
107 struct op_buffer {
108 	unsigned len;
109 	struct rte_crypto_op *buffer[MAX_PKT_BURST];
110 };
111 
112 #define MAX_RX_QUEUE_PER_LCORE 16
113 #define MAX_TX_QUEUE_PER_PORT 16
114 
115 enum l2fwd_crypto_xform_chain {
116 	L2FWD_CRYPTO_CIPHER_HASH,
117 	L2FWD_CRYPTO_HASH_CIPHER
118 };
119 
120 struct l2fwd_key {
121 	uint8_t *data;
122 	uint32_t length;
123 	phys_addr_t phys_addr;
124 };
125 
126 /** l2fwd crypto application command line options */
127 struct l2fwd_crypto_options {
128 	unsigned portmask;
129 	unsigned nb_ports_per_lcore;
130 	unsigned refresh_period;
131 	unsigned single_lcore:1;
132 
133 	enum rte_cryptodev_type cdev_type;
134 	unsigned sessionless:1;
135 
136 	enum l2fwd_crypto_xform_chain xform_chain;
137 
138 	struct rte_crypto_sym_xform cipher_xform;
139 	uint8_t ckey_data[32];
140 
141 	struct l2fwd_key iv_key;
142 	uint8_t ivkey_data[16];
143 
144 	struct rte_crypto_sym_xform auth_xform;
145 	uint8_t akey_data[128];
146 };
147 
148 /** l2fwd crypto lcore params */
149 struct l2fwd_crypto_params {
150 	uint8_t dev_id;
151 	uint8_t qp_id;
152 
153 	unsigned digest_length;
154 	unsigned block_size;
155 	struct l2fwd_key iv_key;
156 	struct rte_cryptodev_sym_session *session;
157 };
158 
159 /** lcore configuration */
160 struct lcore_queue_conf {
161 	unsigned nb_rx_ports;
162 	unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
163 
164 	unsigned nb_crypto_devs;
165 	unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
166 
167 	struct op_buffer op_buf[RTE_MAX_ETHPORTS];
168 	struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
169 } __rte_cache_aligned;
170 
171 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
172 
173 static const struct rte_eth_conf port_conf = {
174 	.rxmode = {
175 		.split_hdr_size = 0,
176 		.header_split   = 0, /**< Header Split disabled */
177 		.hw_ip_checksum = 0, /**< IP checksum offload disabled */
178 		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
179 		.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
180 		.hw_strip_crc   = 0, /**< CRC stripped by hardware */
181 	},
182 	.txmode = {
183 		.mq_mode = ETH_MQ_TX_NONE,
184 	},
185 };
186 
187 struct rte_mempool *l2fwd_pktmbuf_pool;
188 struct rte_mempool *l2fwd_crypto_op_pool;
189 
190 /* Per-port statistics struct */
191 struct l2fwd_port_statistics {
192 	uint64_t tx;
193 	uint64_t rx;
194 
195 	uint64_t crypto_enqueued;
196 	uint64_t crypto_dequeued;
197 
198 	uint64_t dropped;
199 } __rte_cache_aligned;
200 
201 struct l2fwd_crypto_statistics {
202 	uint64_t enqueued;
203 	uint64_t dequeued;
204 
205 	uint64_t errors;
206 } __rte_cache_aligned;
207 
208 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
209 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
210 
211 /* A tsc-based timer responsible for triggering statistics printout */
212 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
213 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
214 
215 /* default period is 10 seconds */
216 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
217 
218 /* Print out statistics on packets dropped */
219 static void
220 print_stats(void)
221 {
222 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
223 	uint64_t total_packets_enqueued, total_packets_dequeued,
224 		total_packets_errors;
225 	unsigned portid;
226 	uint64_t cdevid;
227 
228 	total_packets_dropped = 0;
229 	total_packets_tx = 0;
230 	total_packets_rx = 0;
231 	total_packets_enqueued = 0;
232 	total_packets_dequeued = 0;
233 	total_packets_errors = 0;
234 
235 	const char clr[] = { 27, '[', '2', 'J', '\0' };
236 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
237 
238 		/* Clear screen and move to top left */
239 	printf("%s%s", clr, topLeft);
240 
241 	printf("\nPort statistics ====================================");
242 
243 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
244 		/* skip disabled ports */
245 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
246 			continue;
247 		printf("\nStatistics for port %u ------------------------------"
248 			   "\nPackets sent: %32"PRIu64
249 			   "\nPackets received: %28"PRIu64
250 			   "\nPackets dropped: %29"PRIu64,
251 			   portid,
252 			   port_statistics[portid].tx,
253 			   port_statistics[portid].rx,
254 			   port_statistics[portid].dropped);
255 
256 		total_packets_dropped += port_statistics[portid].dropped;
257 		total_packets_tx += port_statistics[portid].tx;
258 		total_packets_rx += port_statistics[portid].rx;
259 	}
260 	printf("\nCrypto statistics ==================================");
261 
262 	for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
263 		/* skip disabled ports */
264 		if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
265 			continue;
266 		printf("\nStatistics for cryptodev %"PRIu64
267 				" -------------------------"
268 			   "\nPackets enqueued: %28"PRIu64
269 			   "\nPackets dequeued: %28"PRIu64
270 			   "\nPackets errors: %30"PRIu64,
271 			   cdevid,
272 			   crypto_statistics[cdevid].enqueued,
273 			   crypto_statistics[cdevid].dequeued,
274 			   crypto_statistics[cdevid].errors);
275 
276 		total_packets_enqueued += crypto_statistics[cdevid].enqueued;
277 		total_packets_dequeued += crypto_statistics[cdevid].dequeued;
278 		total_packets_errors += crypto_statistics[cdevid].errors;
279 	}
280 	printf("\nAggregate statistics ==============================="
281 		   "\nTotal packets received: %22"PRIu64
282 		   "\nTotal packets enqueued: %22"PRIu64
283 		   "\nTotal packets dequeued: %22"PRIu64
284 		   "\nTotal packets sent: %26"PRIu64
285 		   "\nTotal packets dropped: %23"PRIu64
286 		   "\nTotal packets crypto errors: %17"PRIu64,
287 		   total_packets_rx,
288 		   total_packets_enqueued,
289 		   total_packets_dequeued,
290 		   total_packets_tx,
291 		   total_packets_dropped,
292 		   total_packets_errors);
293 	printf("\n====================================================\n");
294 }
295 
296 
297 
298 static int
299 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
300 		struct l2fwd_crypto_params *cparams)
301 {
302 	struct rte_crypto_op **op_buffer;
303 	unsigned ret;
304 
305 	op_buffer = (struct rte_crypto_op **)
306 			qconf->op_buf[cparams->dev_id].buffer;
307 
308 	ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
309 			cparams->qp_id,	op_buffer, (uint16_t) n);
310 
311 	crypto_statistics[cparams->dev_id].enqueued += ret;
312 	if (unlikely(ret < n)) {
313 		crypto_statistics[cparams->dev_id].errors += (n - ret);
314 		do {
315 			rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
316 			rte_crypto_op_free(op_buffer[ret]);
317 		} while (++ret < n);
318 	}
319 
320 	return 0;
321 }
322 
323 static int
324 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
325 		struct l2fwd_crypto_params *cparams)
326 {
327 	unsigned lcore_id, len;
328 	struct lcore_queue_conf *qconf;
329 
330 	lcore_id = rte_lcore_id();
331 
332 	qconf = &lcore_queue_conf[lcore_id];
333 	len = qconf->op_buf[cparams->dev_id].len;
334 	qconf->op_buf[cparams->dev_id].buffer[len] = op;
335 	len++;
336 
337 	/* enough ops to be sent */
338 	if (len == MAX_PKT_BURST) {
339 		l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
340 		len = 0;
341 	}
342 
343 	qconf->op_buf[cparams->dev_id].len = len;
344 	return 0;
345 }
346 
347 static int
348 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
349 		struct rte_crypto_op *op,
350 		struct l2fwd_crypto_params *cparams)
351 {
352 	struct ether_hdr *eth_hdr;
353 	struct ipv4_hdr *ip_hdr;
354 
355 	unsigned ipdata_offset, pad_len, data_len;
356 	char *padding;
357 
358 	eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
359 
360 	if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
361 		return -1;
362 
363 	ipdata_offset = sizeof(struct ether_hdr);
364 
365 	ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
366 			ipdata_offset);
367 
368 	ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
369 			* IPV4_IHL_MULTIPLIER;
370 
371 
372 	/* Zero pad data to be crypto'd so it is block aligned */
373 	data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
374 	pad_len = data_len % cparams->block_size ? cparams->block_size -
375 			(data_len % cparams->block_size) : 0;
376 
377 	if (pad_len) {
378 		padding = rte_pktmbuf_append(m, pad_len);
379 		if (unlikely(!padding))
380 			return -1;
381 
382 		data_len += pad_len;
383 		memset(padding, 0, pad_len);
384 	}
385 
386 	/* Set crypto operation data parameters */
387 	rte_crypto_op_attach_sym_session(op, cparams->session);
388 
389 	/* Append space for digest to end of packet */
390 	op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
391 			cparams->digest_length);
392 	op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
393 			rte_pktmbuf_pkt_len(m) - cparams->digest_length);
394 	op->sym->auth.digest.length = cparams->digest_length;
395 
396 	op->sym->auth.data.offset = ipdata_offset;
397 	op->sym->auth.data.length = data_len;
398 
399 
400 	op->sym->cipher.iv.data = cparams->iv_key.data;
401 	op->sym->cipher.iv.phys_addr = cparams->iv_key.phys_addr;
402 	op->sym->cipher.iv.length = cparams->iv_key.length;
403 
404 	op->sym->cipher.data.offset = ipdata_offset;
405 	op->sym->cipher.data.length = data_len;
406 
407 	op->sym->m_src = m;
408 
409 	return l2fwd_crypto_enqueue(op, cparams);
410 }
411 
412 
413 /* Send the burst of packets on an output interface */
414 static int
415 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
416 		uint8_t port)
417 {
418 	struct rte_mbuf **pkt_buffer;
419 	unsigned ret;
420 
421 	pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
422 
423 	ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
424 	port_statistics[port].tx += ret;
425 	if (unlikely(ret < n)) {
426 		port_statistics[port].dropped += (n - ret);
427 		do {
428 			rte_pktmbuf_free(pkt_buffer[ret]);
429 		} while (++ret < n);
430 	}
431 
432 	return 0;
433 }
434 
435 /* Enqueue packets for TX and prepare them to be sent */
436 static int
437 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
438 {
439 	unsigned lcore_id, len;
440 	struct lcore_queue_conf *qconf;
441 
442 	lcore_id = rte_lcore_id();
443 
444 	qconf = &lcore_queue_conf[lcore_id];
445 	len = qconf->pkt_buf[port].len;
446 	qconf->pkt_buf[port].buffer[len] = m;
447 	len++;
448 
449 	/* enough pkts to be sent */
450 	if (unlikely(len == MAX_PKT_BURST)) {
451 		l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
452 		len = 0;
453 	}
454 
455 	qconf->pkt_buf[port].len = len;
456 	return 0;
457 }
458 
459 static void
460 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
461 {
462 	struct ether_hdr *eth;
463 	void *tmp;
464 	unsigned dst_port;
465 
466 	dst_port = l2fwd_dst_ports[portid];
467 	eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
468 
469 	/* 02:00:00:00:00:xx */
470 	tmp = &eth->d_addr.addr_bytes[0];
471 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
472 
473 	/* src addr */
474 	ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
475 
476 	l2fwd_send_packet(m, (uint8_t) dst_port);
477 }
478 
479 /** Generate random key */
480 static void
481 generate_random_key(uint8_t *key, unsigned length)
482 {
483 	unsigned i;
484 
485 	for (i = 0; i < length; i++)
486 		key[i] = rand() % 0xff;
487 }
488 
489 static struct rte_cryptodev_sym_session *
490 initialize_crypto_session(struct l2fwd_crypto_options *options,
491 		uint8_t cdev_id)
492 {
493 	struct rte_crypto_sym_xform *first_xform;
494 
495 	if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
496 		first_xform = &options->cipher_xform;
497 		first_xform->next = &options->auth_xform;
498 	} else {
499 		first_xform = &options->auth_xform;
500 		first_xform->next = &options->cipher_xform;
501 	}
502 
503 	/* Setup Cipher Parameters */
504 	return rte_cryptodev_sym_session_create(cdev_id, first_xform);
505 }
506 
507 static void
508 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
509 
510 /* main processing loop */
511 static void
512 l2fwd_main_loop(struct l2fwd_crypto_options *options)
513 {
514 	struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
515 	struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
516 
517 	unsigned lcore_id = rte_lcore_id();
518 	uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
519 	unsigned i, j, portid, nb_rx;
520 	struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
521 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
522 			US_PER_S * BURST_TX_DRAIN_US;
523 	struct l2fwd_crypto_params *cparams;
524 	struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
525 
526 	if (qconf->nb_rx_ports == 0) {
527 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
528 		return;
529 	}
530 
531 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
532 
533 	l2fwd_crypto_options_print(options);
534 
535 	for (i = 0; i < qconf->nb_rx_ports; i++) {
536 
537 		portid = qconf->rx_port_list[i];
538 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
539 			portid);
540 	}
541 
542 	for (i = 0; i < qconf->nb_crypto_devs; i++) {
543 		port_cparams[i].dev_id = qconf->cryptodev_list[i];
544 		port_cparams[i].qp_id = 0;
545 
546 		port_cparams[i].block_size = 64;
547 		port_cparams[i].digest_length = 20;
548 
549 		port_cparams[i].iv_key.data =
550 				(uint8_t *)rte_malloc(NULL, 16, 8);
551 		port_cparams[i].iv_key.length = 16;
552 		port_cparams[i].iv_key.phys_addr = rte_malloc_virt2phy(
553 				(void *)port_cparams[i].iv_key.data);
554 		generate_random_key(port_cparams[i].iv_key.data,
555 				sizeof(cparams[i].iv_key.length));
556 
557 		port_cparams[i].session = initialize_crypto_session(options,
558 				port_cparams[i].dev_id);
559 
560 		if (port_cparams[i].session == NULL)
561 			return;
562 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
563 				port_cparams[i].dev_id);
564 	}
565 
566 	while (1) {
567 
568 		cur_tsc = rte_rdtsc();
569 
570 		/*
571 		 * TX burst queue drain
572 		 */
573 		diff_tsc = cur_tsc - prev_tsc;
574 		if (unlikely(diff_tsc > drain_tsc)) {
575 
576 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
577 				if (qconf->pkt_buf[portid].len == 0)
578 					continue;
579 				l2fwd_send_burst(&lcore_queue_conf[lcore_id],
580 						 qconf->pkt_buf[portid].len,
581 						 (uint8_t) portid);
582 				qconf->pkt_buf[portid].len = 0;
583 			}
584 
585 			/* if timer is enabled */
586 			if (timer_period > 0) {
587 
588 				/* advance the timer */
589 				timer_tsc += diff_tsc;
590 
591 				/* if timer has reached its timeout */
592 				if (unlikely(timer_tsc >=
593 						(uint64_t)timer_period)) {
594 
595 					/* do this only on master core */
596 					if (lcore_id == rte_get_master_lcore()
597 						&& options->refresh_period) {
598 						print_stats();
599 						timer_tsc = 0;
600 					}
601 				}
602 			}
603 
604 			prev_tsc = cur_tsc;
605 		}
606 
607 		/*
608 		 * Read packet from RX queues
609 		 */
610 		for (i = 0; i < qconf->nb_rx_ports; i++) {
611 			portid = qconf->rx_port_list[i];
612 
613 			cparams = &port_cparams[i];
614 
615 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
616 						 pkts_burst, MAX_PKT_BURST);
617 
618 			port_statistics[portid].rx += nb_rx;
619 
620 			if (nb_rx) {
621 				/*
622 				 * If we can't allocate a crypto_ops, then drop
623 				 * the rest of the burst and dequeue and
624 				 * process the packets to free offload structs
625 				 */
626 				if (rte_crypto_op_bulk_alloc(
627 						l2fwd_crypto_op_pool,
628 						RTE_CRYPTO_OP_TYPE_SYMMETRIC,
629 						ops_burst, nb_rx) !=
630 								nb_rx) {
631 					for (j = 0; j < nb_rx; j++)
632 						rte_pktmbuf_free(pkts_burst[i]);
633 
634 					nb_rx = 0;
635 				}
636 
637 				/* Enqueue packets from Crypto device*/
638 				for (j = 0; j < nb_rx; j++) {
639 					m = pkts_burst[j];
640 
641 					l2fwd_simple_crypto_enqueue(m,
642 							ops_burst[j], cparams);
643 				}
644 			}
645 
646 			/* Dequeue packets from Crypto device */
647 			do {
648 				nb_rx = rte_cryptodev_dequeue_burst(
649 						cparams->dev_id, cparams->qp_id,
650 						ops_burst, MAX_PKT_BURST);
651 
652 				crypto_statistics[cparams->dev_id].dequeued +=
653 						nb_rx;
654 
655 				/* Forward crypto'd packets */
656 				for (j = 0; j < nb_rx; j++) {
657 					m = ops_burst[j]->sym->m_src;
658 
659 					rte_crypto_op_free(ops_burst[j]);
660 					l2fwd_simple_forward(m, portid);
661 				}
662 			} while (nb_rx == MAX_PKT_BURST);
663 		}
664 	}
665 }
666 
667 static int
668 l2fwd_launch_one_lcore(void *arg)
669 {
670 	l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
671 	return 0;
672 }
673 
674 /* Display command line arguments usage */
675 static void
676 l2fwd_crypto_usage(const char *prgname)
677 {
678 	printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
679 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
680 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
681 		"  -s manage all ports from single lcore"
682 		"  -t PERIOD: statistics will be refreshed each PERIOD seconds"
683 		" (0 to disable, 10 default, 86400 maximum)\n"
684 
685 		"  --cdev AESNI_MB / QAT\n"
686 		"  --chain HASH_CIPHER / CIPHER_HASH\n"
687 
688 		"  --cipher_algo ALGO\n"
689 		"  --cipher_op ENCRYPT / DECRYPT\n"
690 		"  --cipher_key KEY\n"
691 		"  --iv IV\n"
692 
693 		"  --auth_algo ALGO\n"
694 		"  --auth_op GENERATE / VERIFY\n"
695 		"  --auth_key KEY\n"
696 
697 		"  --sessionless\n",
698 	       prgname);
699 }
700 
701 /** Parse crypto device type command line argument */
702 static int
703 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
704 {
705 	if (strcmp("AESNI_MB", optarg) == 0) {
706 		*type = RTE_CRYPTODEV_AESNI_MB_PMD;
707 		return 0;
708 	} else if (strcmp("QAT", optarg) == 0) {
709 		*type = RTE_CRYPTODEV_QAT_SYM_PMD;
710 		return 0;
711 	}
712 
713 	return -1;
714 }
715 
716 /** Parse crypto chain xform command line argument */
717 static int
718 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
719 {
720 	if (strcmp("CIPHER_HASH", optarg) == 0) {
721 		options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
722 		return 0;
723 	} else if (strcmp("HASH_CIPHER", optarg) == 0) {
724 		options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
725 		return 0;
726 	}
727 
728 	return -1;
729 }
730 
731 /** Parse crypto cipher algo option command line argument */
732 static int
733 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
734 {
735 	if (strcmp("AES_CBC", optarg) == 0) {
736 		*algo = RTE_CRYPTO_CIPHER_AES_CBC;
737 		return 0;
738 	} else if (strcmp("AES_GCM", optarg) == 0) {
739 		*algo = RTE_CRYPTO_CIPHER_AES_GCM;
740 		return 0;
741 	}
742 
743 	printf("Cipher algorithm  not supported!\n");
744 	return -1;
745 }
746 
747 /** Parse crypto cipher operation command line argument */
748 static int
749 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
750 {
751 	if (strcmp("ENCRYPT", optarg) == 0) {
752 		*op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
753 		return 0;
754 	} else if (strcmp("DECRYPT", optarg) == 0) {
755 		*op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
756 		return 0;
757 	}
758 
759 	printf("Cipher operation not supported!\n");
760 	return -1;
761 }
762 
763 /** Parse crypto key command line argument */
764 static int
765 parse_key(struct l2fwd_key *key __rte_unused,
766 		unsigned length __rte_unused, char *arg __rte_unused)
767 {
768 	printf("Currently an unsupported argument!\n");
769 	return -1;
770 }
771 
772 /** Parse crypto cipher operation command line argument */
773 static int
774 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
775 {
776 	if (strcmp("SHA1", optarg) == 0) {
777 		*algo = RTE_CRYPTO_AUTH_SHA1;
778 		return 0;
779 	} else if (strcmp("SHA1_HMAC", optarg) == 0) {
780 		*algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
781 		return 0;
782 	} else if (strcmp("SHA224", optarg) == 0) {
783 		*algo = RTE_CRYPTO_AUTH_SHA224;
784 		return 0;
785 	} else if (strcmp("SHA224_HMAC", optarg) == 0) {
786 		*algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
787 		return 0;
788 	} else if (strcmp("SHA256", optarg) == 0) {
789 		*algo = RTE_CRYPTO_AUTH_SHA256;
790 		return 0;
791 	} else if (strcmp("SHA256_HMAC", optarg) == 0) {
792 		*algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
793 		return 0;
794 	} else if (strcmp("SHA512", optarg) == 0) {
795 		*algo = RTE_CRYPTO_AUTH_SHA256;
796 		return 0;
797 	} else if (strcmp("SHA512_HMAC", optarg) == 0) {
798 		*algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
799 		return 0;
800 	}
801 
802 	printf("Authentication algorithm specified not supported!\n");
803 	return -1;
804 }
805 
806 static int
807 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
808 {
809 	if (strcmp("VERIFY", optarg) == 0) {
810 		*op = RTE_CRYPTO_AUTH_OP_VERIFY;
811 		return 0;
812 	} else if (strcmp("GENERATE", optarg) == 0) {
813 		*op = RTE_CRYPTO_AUTH_OP_GENERATE;
814 		return 0;
815 	}
816 
817 	printf("Authentication operation specified not supported!\n");
818 	return -1;
819 }
820 
821 /** Parse long options */
822 static int
823 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
824 		struct option *lgopts, int option_index)
825 {
826 	if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
827 		return parse_cryptodev_type(&options->cdev_type, optarg);
828 
829 	else if (strcmp(lgopts[option_index].name, "chain") == 0)
830 		return parse_crypto_opt_chain(options, optarg);
831 
832 	/* Cipher options */
833 	else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
834 		return parse_cipher_algo(&options->cipher_xform.cipher.algo,
835 				optarg);
836 
837 	else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
838 		return parse_cipher_op(&options->cipher_xform.cipher.op,
839 				optarg);
840 
841 	else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
842 		struct l2fwd_key key = { 0 };
843 		int retval = 0;
844 
845 		retval = parse_key(&key, sizeof(options->ckey_data), optarg);
846 
847 		options->cipher_xform.cipher.key.data = key.data;
848 		options->cipher_xform.cipher.key.length = key.length;
849 
850 		return retval;
851 	} else if (strcmp(lgopts[option_index].name, "iv") == 0)
852 		return parse_key(&options->iv_key, sizeof(options->ivkey_data),
853 				optarg);
854 
855 	/* Authentication options */
856 	else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
857 		return parse_auth_algo(&options->auth_xform.auth.algo,
858 				optarg);
859 
860 	else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
861 		return parse_auth_op(&options->auth_xform.auth.op,
862 				optarg);
863 
864 	else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
865 		struct l2fwd_key key = { 0 };
866 		int retval = 0;
867 
868 		retval = parse_key(&key, sizeof(options->akey_data), optarg);
869 
870 		options->auth_xform.auth.key.data = key.data;
871 		options->auth_xform.auth.key.length = key.length;
872 
873 		return retval;
874 	} else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
875 		options->sessionless = 1;
876 		return 0;
877 	}
878 
879 	return -1;
880 }
881 
882 /** Parse port mask */
883 static int
884 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
885 		const char *q_arg)
886 {
887 	char *end = NULL;
888 	unsigned long pm;
889 
890 	/* parse hexadecimal string */
891 	pm = strtoul(q_arg, &end, 16);
892 	if ((pm == '\0') || (end == NULL) || (*end != '\0'))
893 		pm = 0;
894 
895 	options->portmask = pm;
896 	if (options->portmask == 0) {
897 		printf("invalid portmask specified\n");
898 		return -1;
899 	}
900 
901 	return pm;
902 }
903 
904 /** Parse number of queues */
905 static int
906 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
907 		const char *q_arg)
908 {
909 	char *end = NULL;
910 	unsigned long n;
911 
912 	/* parse hexadecimal string */
913 	n = strtoul(q_arg, &end, 10);
914 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
915 		n = 0;
916 	else if (n >= MAX_RX_QUEUE_PER_LCORE)
917 		n = 0;
918 
919 	options->nb_ports_per_lcore = n;
920 	if (options->nb_ports_per_lcore == 0) {
921 		printf("invalid number of ports selected\n");
922 		return -1;
923 	}
924 
925 	return 0;
926 }
927 
928 /** Parse timer period */
929 static int
930 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
931 		const char *q_arg)
932 {
933 	char *end = NULL;
934 	unsigned long n;
935 
936 	/* parse number string */
937 	n = (unsigned)strtol(q_arg, &end, 10);
938 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
939 		n = 0;
940 
941 	if (n >= MAX_TIMER_PERIOD) {
942 		printf("Warning refresh period specified %lu is greater than "
943 				"max value %lu! using max value",
944 				n, MAX_TIMER_PERIOD);
945 		n = MAX_TIMER_PERIOD;
946 	}
947 
948 	options->refresh_period = n * 1000 * TIMER_MILLISECOND;
949 
950 	return 0;
951 }
952 
953 /** Generate default options for application */
954 static void
955 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
956 {
957 	srand(time(NULL));
958 
959 	options->portmask = 0xffffffff;
960 	options->nb_ports_per_lcore = 1;
961 	options->refresh_period = 10000;
962 	options->single_lcore = 0;
963 	options->sessionless = 0;
964 
965 	options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
966 	options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
967 
968 	/* Cipher Data */
969 	options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
970 	options->cipher_xform.next = NULL;
971 
972 	options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
973 	options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
974 
975 	generate_random_key(options->ckey_data, sizeof(options->ckey_data));
976 
977 	options->cipher_xform.cipher.key.data = options->ckey_data;
978 	options->cipher_xform.cipher.key.length = 16;
979 
980 
981 	/* Authentication Data */
982 	options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
983 	options->auth_xform.next = NULL;
984 
985 	options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
986 	options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
987 
988 	options->auth_xform.auth.add_auth_data_length = 0;
989 	options->auth_xform.auth.digest_length = 20;
990 
991 	generate_random_key(options->akey_data, sizeof(options->akey_data));
992 
993 	options->auth_xform.auth.key.data = options->akey_data;
994 	options->auth_xform.auth.key.length = 20;
995 }
996 
997 static void
998 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
999 {
1000 	printf("Options:-\nn");
1001 	printf("portmask: %x\n", options->portmask);
1002 	printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1003 	printf("refresh period : %u\n", options->refresh_period);
1004 	printf("single lcore mode: %s\n",
1005 			options->single_lcore ? "enabled" : "disabled");
1006 	printf("stats_printing: %s\n",
1007 			options->refresh_period == 0 ? "disabled" : "enabled");
1008 
1009 	switch (options->cdev_type) {
1010 	case RTE_CRYPTODEV_AESNI_MB_PMD:
1011 		printf("cryptodev type: AES-NI MB PMD\n"); break;
1012 	case RTE_CRYPTODEV_QAT_SYM_PMD:
1013 		printf("cryptodev type: QAT PMD\n"); break;
1014 	default:
1015 		break;
1016 	}
1017 
1018 	printf("sessionless crypto: %s\n",
1019 			options->sessionless ? "enabled" : "disabled");
1020 }
1021 
1022 /* Parse the argument given in the command line of the application */
1023 static int
1024 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1025 		int argc, char **argv)
1026 {
1027 	int opt, retval, option_index;
1028 	char **argvopt = argv, *prgname = argv[0];
1029 
1030 	static struct option lgopts[] = {
1031 			{ "sessionless", no_argument, 0, 0 },
1032 
1033 			{ "cdev_type", required_argument, 0, 0 },
1034 			{ "chain", required_argument, 0, 0 },
1035 
1036 			{ "cipher_algo", required_argument, 0, 0 },
1037 			{ "cipher_op", required_argument, 0, 0 },
1038 			{ "cipher_key", required_argument, 0, 0 },
1039 
1040 			{ "auth_algo", required_argument, 0, 0 },
1041 			{ "auth_op", required_argument, 0, 0 },
1042 			{ "auth_key", required_argument, 0, 0 },
1043 
1044 			{ "iv", required_argument, 0, 0 },
1045 
1046 			{ "sessionless", no_argument, 0, 0 },
1047 
1048 			{ NULL, 0, 0, 0 }
1049 	};
1050 
1051 	l2fwd_crypto_default_options(options);
1052 
1053 	while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1054 			&option_index)) != EOF) {
1055 		switch (opt) {
1056 		/* long options */
1057 		case 0:
1058 			retval = l2fwd_crypto_parse_args_long_options(options,
1059 					lgopts, option_index);
1060 			if (retval < 0) {
1061 				l2fwd_crypto_usage(prgname);
1062 				return -1;
1063 			}
1064 			break;
1065 
1066 		/* portmask */
1067 		case 'p':
1068 			retval = l2fwd_crypto_parse_portmask(options, optarg);
1069 			if (retval < 0) {
1070 				l2fwd_crypto_usage(prgname);
1071 				return -1;
1072 			}
1073 			break;
1074 
1075 		/* nqueue */
1076 		case 'q':
1077 			retval = l2fwd_crypto_parse_nqueue(options, optarg);
1078 			if (retval < 0) {
1079 				l2fwd_crypto_usage(prgname);
1080 				return -1;
1081 			}
1082 			break;
1083 
1084 		/* single  */
1085 		case 's':
1086 			options->single_lcore = 1;
1087 
1088 			break;
1089 
1090 		/* timer period */
1091 		case 't':
1092 			retval = l2fwd_crypto_parse_timer_period(options,
1093 					optarg);
1094 			if (retval < 0) {
1095 				l2fwd_crypto_usage(prgname);
1096 				return -1;
1097 			}
1098 			break;
1099 
1100 		default:
1101 			l2fwd_crypto_usage(prgname);
1102 			return -1;
1103 		}
1104 	}
1105 
1106 
1107 	if (optind >= 0)
1108 		argv[optind-1] = prgname;
1109 
1110 	retval = optind-1;
1111 	optind = 0; /* reset getopt lib */
1112 
1113 	return retval;
1114 }
1115 
1116 /* Check the link status of all ports in up to 9s, and print them finally */
1117 static void
1118 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1119 {
1120 #define CHECK_INTERVAL 100 /* 100ms */
1121 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1122 	uint8_t portid, count, all_ports_up, print_flag = 0;
1123 	struct rte_eth_link link;
1124 
1125 	printf("\nChecking link status");
1126 	fflush(stdout);
1127 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
1128 		all_ports_up = 1;
1129 		for (portid = 0; portid < port_num; portid++) {
1130 			if ((port_mask & (1 << portid)) == 0)
1131 				continue;
1132 			memset(&link, 0, sizeof(link));
1133 			rte_eth_link_get_nowait(portid, &link);
1134 			/* print link status if flag set */
1135 			if (print_flag == 1) {
1136 				if (link.link_status)
1137 					printf("Port %d Link Up - speed %u "
1138 						"Mbps - %s\n", (uint8_t)portid,
1139 						(unsigned)link.link_speed,
1140 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1141 					("full-duplex") : ("half-duplex\n"));
1142 				else
1143 					printf("Port %d Link Down\n",
1144 						(uint8_t)portid);
1145 				continue;
1146 			}
1147 			/* clear all_ports_up flag if any link down */
1148 			if (link.link_status == 0) {
1149 				all_ports_up = 0;
1150 				break;
1151 			}
1152 		}
1153 		/* after finally printing all link status, get out */
1154 		if (print_flag == 1)
1155 			break;
1156 
1157 		if (all_ports_up == 0) {
1158 			printf(".");
1159 			fflush(stdout);
1160 			rte_delay_ms(CHECK_INTERVAL);
1161 		}
1162 
1163 		/* set the print_flag if all ports up or timeout */
1164 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1165 			print_flag = 1;
1166 			printf("done\n");
1167 		}
1168 	}
1169 }
1170 
1171 static int
1172 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1173 {
1174 	unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1175 	int retval;
1176 
1177 	if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1178 		if (rte_cryptodev_count() < nb_ports)
1179 			return -1;
1180 	} else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1181 		for (i = 0; i < nb_ports; i++) {
1182 			int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1183 					NULL);
1184 			if (retval < 0)
1185 				return -1;
1186 		}
1187 	}
1188 
1189 	cdev_count = rte_cryptodev_count();
1190 	for (cdev_id = 0;
1191 			cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1192 			cdev_id++) {
1193 		struct rte_cryptodev_qp_conf qp_conf;
1194 		struct rte_cryptodev_info dev_info;
1195 
1196 		struct rte_cryptodev_config conf = {
1197 			.nb_queue_pairs = 1,
1198 			.socket_id = SOCKET_ID_ANY,
1199 			.session_mp = {
1200 				.nb_objs = 2048,
1201 				.cache_size = 64
1202 			}
1203 		};
1204 
1205 		rte_cryptodev_info_get(cdev_id, &dev_info);
1206 
1207 		if (dev_info.dev_type != options->cdev_type)
1208 			continue;
1209 
1210 
1211 		retval = rte_cryptodev_configure(cdev_id, &conf);
1212 		if (retval < 0) {
1213 			printf("Failed to configure cryptodev %u", cdev_id);
1214 			return -1;
1215 		}
1216 
1217 		qp_conf.nb_descriptors = 2048;
1218 
1219 		retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1220 				SOCKET_ID_ANY);
1221 		if (retval < 0) {
1222 			printf("Failed to setup queue pair %u on cryptodev %u",
1223 					0, cdev_id);
1224 			return -1;
1225 		}
1226 
1227 		l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1228 
1229 		enabled_cdev_count++;
1230 	}
1231 
1232 	return enabled_cdev_count;
1233 }
1234 
1235 static int
1236 initialize_ports(struct l2fwd_crypto_options *options)
1237 {
1238 	uint8_t last_portid, portid;
1239 	unsigned enabled_portcount = 0;
1240 	unsigned nb_ports = rte_eth_dev_count();
1241 
1242 	if (nb_ports == 0) {
1243 		printf("No Ethernet ports - bye\n");
1244 		return -1;
1245 	}
1246 
1247 	if (nb_ports > RTE_MAX_ETHPORTS)
1248 		nb_ports = RTE_MAX_ETHPORTS;
1249 
1250 	/* Reset l2fwd_dst_ports */
1251 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1252 		l2fwd_dst_ports[portid] = 0;
1253 
1254 	for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1255 		int retval;
1256 
1257 		/* Skip ports that are not enabled */
1258 		if ((options->portmask & (1 << portid)) == 0)
1259 			continue;
1260 
1261 		/* init port */
1262 		printf("Initializing port %u... ", (unsigned) portid);
1263 		fflush(stdout);
1264 		retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1265 		if (retval < 0) {
1266 			printf("Cannot configure device: err=%d, port=%u\n",
1267 				  retval, (unsigned) portid);
1268 			return -1;
1269 		}
1270 
1271 		/* init one RX queue */
1272 		fflush(stdout);
1273 		retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1274 					     rte_eth_dev_socket_id(portid),
1275 					     NULL, l2fwd_pktmbuf_pool);
1276 		if (retval < 0) {
1277 			printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1278 					retval, (unsigned) portid);
1279 			return -1;
1280 		}
1281 
1282 		/* init one TX queue on each port */
1283 		fflush(stdout);
1284 		retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1285 				rte_eth_dev_socket_id(portid),
1286 				NULL);
1287 		if (retval < 0) {
1288 			printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1289 				retval, (unsigned) portid);
1290 
1291 			return -1;
1292 		}
1293 
1294 		/* Start device */
1295 		retval = rte_eth_dev_start(portid);
1296 		if (retval < 0) {
1297 			printf("rte_eth_dev_start:err=%d, port=%u\n",
1298 					retval, (unsigned) portid);
1299 			return -1;
1300 		}
1301 
1302 		rte_eth_promiscuous_enable(portid);
1303 
1304 		rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1305 
1306 		printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1307 				(unsigned) portid,
1308 				l2fwd_ports_eth_addr[portid].addr_bytes[0],
1309 				l2fwd_ports_eth_addr[portid].addr_bytes[1],
1310 				l2fwd_ports_eth_addr[portid].addr_bytes[2],
1311 				l2fwd_ports_eth_addr[portid].addr_bytes[3],
1312 				l2fwd_ports_eth_addr[portid].addr_bytes[4],
1313 				l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1314 
1315 		/* initialize port stats */
1316 		memset(&port_statistics, 0, sizeof(port_statistics));
1317 
1318 		/* Setup port forwarding table */
1319 		if (enabled_portcount % 2) {
1320 			l2fwd_dst_ports[portid] = last_portid;
1321 			l2fwd_dst_ports[last_portid] = portid;
1322 		} else {
1323 			last_portid = portid;
1324 		}
1325 
1326 		l2fwd_enabled_port_mask |= (1 << portid);
1327 		enabled_portcount++;
1328 	}
1329 
1330 	if (enabled_portcount == 1) {
1331 		l2fwd_dst_ports[last_portid] = last_portid;
1332 	} else if (enabled_portcount % 2) {
1333 		printf("odd number of ports in portmask- bye\n");
1334 		return -1;
1335 	}
1336 
1337 	check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1338 
1339 	return enabled_portcount;
1340 }
1341 
1342 int
1343 main(int argc, char **argv)
1344 {
1345 	struct lcore_queue_conf *qconf;
1346 	struct l2fwd_crypto_options options;
1347 
1348 	uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1349 	unsigned lcore_id, rx_lcore_id;
1350 	int ret, enabled_cdevcount, enabled_portcount;
1351 
1352 	/* init EAL */
1353 	ret = rte_eal_init(argc, argv);
1354 	if (ret < 0)
1355 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1356 	argc -= ret;
1357 	argv += ret;
1358 
1359 	/* parse application arguments (after the EAL ones) */
1360 	ret = l2fwd_crypto_parse_args(&options, argc, argv);
1361 	if (ret < 0)
1362 		rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1363 
1364 	/* create the mbuf pool */
1365 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1366 			sizeof(struct rte_crypto_op),
1367 			RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1368 	if (l2fwd_pktmbuf_pool == NULL)
1369 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1370 
1371 	/* create crypto op pool */
1372 	l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1373 			RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1374 			rte_socket_id());
1375 	if (l2fwd_crypto_op_pool == NULL)
1376 		rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1377 
1378 	/* Enable Ethernet ports */
1379 	enabled_portcount = initialize_ports(&options);
1380 	if (enabled_portcount < 1)
1381 		rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1382 
1383 	nb_ports = rte_eth_dev_count();
1384 	/* Initialize the port/queue configuration of each logical core */
1385 	for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1386 			portid < nb_ports; portid++) {
1387 
1388 		/* skip ports that are not enabled */
1389 		if ((options.portmask & (1 << portid)) == 0)
1390 			continue;
1391 
1392 		if (options.single_lcore && qconf == NULL) {
1393 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1394 				rx_lcore_id++;
1395 				if (rx_lcore_id >= RTE_MAX_LCORE)
1396 					rte_exit(EXIT_FAILURE,
1397 							"Not enough cores\n");
1398 			}
1399 		} else if (!options.single_lcore) {
1400 			/* get the lcore_id for this port */
1401 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1402 			       lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1403 			       options.nb_ports_per_lcore) {
1404 				rx_lcore_id++;
1405 				if (rx_lcore_id >= RTE_MAX_LCORE)
1406 					rte_exit(EXIT_FAILURE,
1407 							"Not enough cores\n");
1408 			}
1409 		}
1410 
1411 		/* Assigned a new logical core in the loop above. */
1412 		if (qconf != &lcore_queue_conf[rx_lcore_id])
1413 			qconf = &lcore_queue_conf[rx_lcore_id];
1414 
1415 		qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1416 		qconf->nb_rx_ports++;
1417 
1418 		printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1419 	}
1420 
1421 
1422 	/* Enable Crypto devices */
1423 	enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1424 	if (enabled_cdevcount < 1)
1425 		rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1426 
1427 	nb_cryptodevs = rte_cryptodev_count();
1428 	/* Initialize the port/queue configuration of each logical core */
1429 	for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1430 			cdev_id < nb_cryptodevs && enabled_cdevcount;
1431 			cdev_id++) {
1432 		struct rte_cryptodev_info info;
1433 
1434 		rte_cryptodev_info_get(cdev_id, &info);
1435 
1436 		/* skip devices of the wrong type */
1437 		if (options.cdev_type != info.dev_type)
1438 			continue;
1439 
1440 		if (options.single_lcore && qconf == NULL) {
1441 			while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1442 				rx_lcore_id++;
1443 				if (rx_lcore_id >= RTE_MAX_LCORE)
1444 					rte_exit(EXIT_FAILURE,
1445 							"Not enough cores\n");
1446 			}
1447 		} else if (!options.single_lcore) {
1448 			/* get the lcore_id for this port */
1449 			while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1450 			       lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1451 			       options.nb_ports_per_lcore) {
1452 				rx_lcore_id++;
1453 				if (rx_lcore_id >= RTE_MAX_LCORE)
1454 					rte_exit(EXIT_FAILURE,
1455 							"Not enough cores\n");
1456 			}
1457 		}
1458 
1459 		/* Assigned a new logical core in the loop above. */
1460 		if (qconf != &lcore_queue_conf[rx_lcore_id])
1461 			qconf = &lcore_queue_conf[rx_lcore_id];
1462 
1463 		qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1464 		qconf->nb_crypto_devs++;
1465 
1466 		enabled_cdevcount--;
1467 
1468 		printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1469 				(unsigned)cdev_id);
1470 	}
1471 
1472 
1473 
1474 	/* launch per-lcore init on every lcore */
1475 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1476 			CALL_MASTER);
1477 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1478 		if (rte_eal_wait_lcore(lcore_id) < 0)
1479 			return -1;
1480 	}
1481 
1482 	return 0;
1483 }
1484