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