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