1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2016 Intel Corporation
11 #include <sys/types.h>
12 #include <sys/queue.h>
13 #include <netinet/in.h>
22 #include <rte_atomic.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>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_interrupts.h>
33 #include <rte_launch.h>
34 #include <rte_lcore.h>
36 #include <rte_malloc.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_LIBRTE_PMD_CRYPTO_SCHEDULER
46 #include <rte_cryptodev_scheduler.h>
55 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
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
67 #define MAXIMUM_IV_LENGTH 16
68 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
69 sizeof(struct rte_crypto_sym_op))
72 * Configurable number of RX/TX ring descriptors
74 #define RTE_TEST_RX_DESC_DEFAULT 1024
75 #define RTE_TEST_TX_DESC_DEFAULT 1024
77 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
78 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
80 /* ethernet addresses of ports */
81 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
83 /* mask of enabled ports */
84 static uint64_t l2fwd_enabled_port_mask;
85 static uint64_t l2fwd_enabled_crypto_mask;
87 /* list of enabled ports */
88 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
93 struct rte_mbuf *buffer[MAX_PKT_BURST];
98 struct rte_crypto_op *buffer[MAX_PKT_BURST];
101 #define MAX_RX_QUEUE_PER_LCORE 16
102 #define MAX_TX_QUEUE_PER_PORT 16
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,
115 rte_iova_t phys_addr;
123 /** l2fwd crypto application command line options */
124 struct l2fwd_crypto_options {
126 unsigned nb_ports_per_lcore;
127 unsigned refresh_period;
128 unsigned single_lcore:1;
131 unsigned sessionless:1;
133 enum l2fwd_crypto_xform_chain xform_chain;
135 struct rte_crypto_sym_xform cipher_xform;
137 int ckey_random_size;
139 struct l2fwd_iv cipher_iv;
140 unsigned int cipher_iv_param;
141 int cipher_iv_random_size;
143 struct rte_crypto_sym_xform auth_xform;
145 int akey_random_size;
147 struct l2fwd_iv auth_iv;
148 unsigned int auth_iv_param;
149 int auth_iv_random_size;
151 struct rte_crypto_sym_xform aead_xform;
152 unsigned int aead_key_param;
153 int aead_key_random_size;
155 struct l2fwd_iv aead_iv;
156 unsigned int aead_iv_param;
157 int aead_iv_random_size;
159 struct l2fwd_key aad;
166 char string_type[MAX_STR_LEN];
168 uint64_t cryptodev_mask;
170 unsigned int mac_updating;
173 /** l2fwd crypto lcore params */
174 struct l2fwd_crypto_params {
178 unsigned digest_length;
181 struct l2fwd_iv cipher_iv;
182 struct l2fwd_iv auth_iv;
183 struct l2fwd_iv aead_iv;
184 struct l2fwd_key aad;
185 struct rte_cryptodev_sym_session *session;
192 enum rte_crypto_cipher_algorithm cipher_algo;
193 enum rte_crypto_auth_algorithm auth_algo;
194 enum rte_crypto_aead_algorithm aead_algo;
197 /** lcore configuration */
198 struct lcore_queue_conf {
199 unsigned nb_rx_ports;
200 uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
202 unsigned nb_crypto_devs;
203 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
205 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
206 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
207 } __rte_cache_aligned;
209 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
211 static struct rte_eth_conf port_conf = {
213 .mq_mode = ETH_MQ_RX_NONE,
214 .max_rx_pkt_len = ETHER_MAX_LEN,
218 .mq_mode = ETH_MQ_TX_NONE,
222 struct rte_mempool *l2fwd_pktmbuf_pool;
223 struct rte_mempool *l2fwd_crypto_op_pool;
224 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
226 /* Per-port statistics struct */
227 struct l2fwd_port_statistics {
231 uint64_t crypto_enqueued;
232 uint64_t crypto_dequeued;
235 } __rte_cache_aligned;
237 struct l2fwd_crypto_statistics {
242 } __rte_cache_aligned;
244 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
245 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
247 /* A tsc-based timer responsible for triggering statistics printout */
248 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
249 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
251 /* default period is 10 seconds */
252 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
254 /* Print out statistics on packets dropped */
258 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
259 uint64_t total_packets_enqueued, total_packets_dequeued,
260 total_packets_errors;
264 total_packets_dropped = 0;
265 total_packets_tx = 0;
266 total_packets_rx = 0;
267 total_packets_enqueued = 0;
268 total_packets_dequeued = 0;
269 total_packets_errors = 0;
271 const char clr[] = { 27, '[', '2', 'J', '\0' };
272 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
274 /* Clear screen and move to top left */
275 printf("%s%s", clr, topLeft);
277 printf("\nPort statistics ====================================");
279 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
280 /* skip disabled ports */
281 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
283 printf("\nStatistics for port %u ------------------------------"
284 "\nPackets sent: %32"PRIu64
285 "\nPackets received: %28"PRIu64
286 "\nPackets dropped: %29"PRIu64,
288 port_statistics[portid].tx,
289 port_statistics[portid].rx,
290 port_statistics[portid].dropped);
292 total_packets_dropped += port_statistics[portid].dropped;
293 total_packets_tx += port_statistics[portid].tx;
294 total_packets_rx += port_statistics[portid].rx;
296 printf("\nCrypto statistics ==================================");
298 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
299 /* skip disabled ports */
300 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
302 printf("\nStatistics for cryptodev %"PRIu64
303 " -------------------------"
304 "\nPackets enqueued: %28"PRIu64
305 "\nPackets dequeued: %28"PRIu64
306 "\nPackets errors: %30"PRIu64,
308 crypto_statistics[cdevid].enqueued,
309 crypto_statistics[cdevid].dequeued,
310 crypto_statistics[cdevid].errors);
312 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
313 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
314 total_packets_errors += crypto_statistics[cdevid].errors;
316 printf("\nAggregate statistics ==============================="
317 "\nTotal packets received: %22"PRIu64
318 "\nTotal packets enqueued: %22"PRIu64
319 "\nTotal packets dequeued: %22"PRIu64
320 "\nTotal packets sent: %26"PRIu64
321 "\nTotal packets dropped: %23"PRIu64
322 "\nTotal packets crypto errors: %17"PRIu64,
324 total_packets_enqueued,
325 total_packets_dequeued,
327 total_packets_dropped,
328 total_packets_errors);
329 printf("\n====================================================\n");
333 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
334 struct l2fwd_crypto_params *cparams)
336 struct rte_crypto_op **op_buffer;
339 op_buffer = (struct rte_crypto_op **)
340 qconf->op_buf[cparams->dev_id].buffer;
342 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
343 cparams->qp_id, op_buffer, (uint16_t) n);
345 crypto_statistics[cparams->dev_id].enqueued += ret;
346 if (unlikely(ret < n)) {
347 crypto_statistics[cparams->dev_id].errors += (n - ret);
349 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
350 rte_crypto_op_free(op_buffer[ret]);
358 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
359 struct l2fwd_crypto_params *cparams)
361 unsigned lcore_id, len;
362 struct lcore_queue_conf *qconf;
364 lcore_id = rte_lcore_id();
366 qconf = &lcore_queue_conf[lcore_id];
367 len = qconf->op_buf[cparams->dev_id].len;
368 qconf->op_buf[cparams->dev_id].buffer[len] = op;
371 /* enough ops to be sent */
372 if (len == MAX_PKT_BURST) {
373 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
377 qconf->op_buf[cparams->dev_id].len = len;
382 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
383 struct rte_crypto_op *op,
384 struct l2fwd_crypto_params *cparams)
386 struct ether_hdr *eth_hdr;
387 struct ipv4_hdr *ip_hdr;
389 uint32_t ipdata_offset, data_len;
390 uint32_t pad_len = 0;
393 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
395 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
398 ipdata_offset = sizeof(struct ether_hdr);
400 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
403 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
404 * IPV4_IHL_MULTIPLIER;
407 /* Zero pad data to be crypto'd so it is block aligned */
408 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
410 if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify)
411 data_len -= cparams->digest_length;
413 if (cparams->do_cipher) {
415 * Following algorithms are block cipher algorithms,
416 * and might need padding
418 switch (cparams->cipher_algo) {
419 case RTE_CRYPTO_CIPHER_AES_CBC:
420 case RTE_CRYPTO_CIPHER_AES_ECB:
421 case RTE_CRYPTO_CIPHER_DES_CBC:
422 case RTE_CRYPTO_CIPHER_3DES_CBC:
423 case RTE_CRYPTO_CIPHER_3DES_ECB:
424 if (data_len % cparams->block_size)
425 pad_len = cparams->block_size -
426 (data_len % cparams->block_size);
433 padding = rte_pktmbuf_append(m, pad_len);
434 if (unlikely(!padding))
438 memset(padding, 0, pad_len);
442 /* Set crypto operation data parameters */
443 rte_crypto_op_attach_sym_session(op, cparams->session);
445 if (cparams->do_hash) {
446 if (cparams->auth_iv.length) {
447 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
450 cparams->cipher_iv.length);
452 * Copy IV at the end of the crypto operation,
453 * after the cipher IV, if added
455 rte_memcpy(iv_ptr, cparams->auth_iv.data,
456 cparams->auth_iv.length);
458 if (!cparams->hash_verify) {
459 /* Append space for digest to end of packet */
460 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
461 cparams->digest_length);
463 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
464 uint8_t *) + ipdata_offset + data_len;
467 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
468 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
470 /* For wireless algorithms, offset/length must be in bits */
471 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
472 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
473 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
474 op->sym->auth.data.offset = ipdata_offset << 3;
475 op->sym->auth.data.length = data_len << 3;
477 op->sym->auth.data.offset = ipdata_offset;
478 op->sym->auth.data.length = data_len;
482 if (cparams->do_cipher) {
483 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
485 /* Copy IV at the end of the crypto operation */
486 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
487 cparams->cipher_iv.length);
489 /* For wireless algorithms, offset/length must be in bits */
490 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
491 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
492 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
493 op->sym->cipher.data.offset = ipdata_offset << 3;
494 op->sym->cipher.data.length = data_len << 3;
496 op->sym->cipher.data.offset = ipdata_offset;
497 op->sym->cipher.data.length = data_len;
501 if (cparams->do_aead) {
502 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
504 /* Copy IV at the end of the crypto operation */
506 * If doing AES-CCM, nonce is copied one byte
507 * after the start of IV field
509 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
510 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
511 cparams->aead_iv.length);
513 rte_memcpy(iv_ptr, cparams->aead_iv.data,
514 cparams->aead_iv.length);
516 op->sym->aead.data.offset = ipdata_offset;
517 op->sym->aead.data.length = data_len;
519 if (!cparams->hash_verify) {
520 /* Append space for digest to end of packet */
521 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
522 cparams->digest_length);
524 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
525 uint8_t *) + ipdata_offset + data_len;
528 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
529 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
531 if (cparams->aad.length) {
532 op->sym->aead.aad.data = cparams->aad.data;
533 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
539 return l2fwd_crypto_enqueue(op, cparams);
543 /* Send the burst of packets on an output interface */
545 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
548 struct rte_mbuf **pkt_buffer;
551 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
553 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
554 port_statistics[port].tx += ret;
555 if (unlikely(ret < n)) {
556 port_statistics[port].dropped += (n - ret);
558 rte_pktmbuf_free(pkt_buffer[ret]);
565 /* Enqueue packets for TX and prepare them to be sent */
567 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
569 unsigned lcore_id, len;
570 struct lcore_queue_conf *qconf;
572 lcore_id = rte_lcore_id();
574 qconf = &lcore_queue_conf[lcore_id];
575 len = qconf->pkt_buf[port].len;
576 qconf->pkt_buf[port].buffer[len] = m;
579 /* enough pkts to be sent */
580 if (unlikely(len == MAX_PKT_BURST)) {
581 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
585 qconf->pkt_buf[port].len = len;
590 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
592 struct ether_hdr *eth;
595 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
597 /* 02:00:00:00:00:xx */
598 tmp = ð->d_addr.addr_bytes[0];
599 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
602 ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
606 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
607 struct l2fwd_crypto_options *options)
611 dst_port = l2fwd_dst_ports[portid];
613 if (options->mac_updating)
614 l2fwd_mac_updating(m, dst_port);
616 l2fwd_send_packet(m, dst_port);
619 /** Generate random key */
621 generate_random_key(uint8_t *key, unsigned length)
626 fd = open("/dev/urandom", O_RDONLY);
628 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
630 ret = read(fd, key, length);
633 if (ret != (signed)length)
634 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
637 static struct rte_cryptodev_sym_session *
638 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
640 struct rte_crypto_sym_xform *first_xform;
641 struct rte_cryptodev_sym_session *session;
642 int retval = rte_cryptodev_socket_id(cdev_id);
647 uint8_t socket_id = (uint8_t) retval;
648 struct rte_mempool *sess_mp = session_pool_socket[socket_id];
650 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
651 first_xform = &options->aead_xform;
652 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
653 first_xform = &options->cipher_xform;
654 first_xform->next = &options->auth_xform;
655 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
656 first_xform = &options->auth_xform;
657 first_xform->next = &options->cipher_xform;
658 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
659 first_xform = &options->cipher_xform;
661 first_xform = &options->auth_xform;
664 session = rte_cryptodev_sym_session_create(sess_mp);
669 if (rte_cryptodev_sym_session_init(cdev_id, session,
670 first_xform, sess_mp) < 0)
677 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
679 /* main processing loop */
681 l2fwd_main_loop(struct l2fwd_crypto_options *options)
683 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
684 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
686 unsigned lcore_id = rte_lcore_id();
687 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
688 unsigned int i, j, nb_rx, len;
690 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
691 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
692 US_PER_S * BURST_TX_DRAIN_US;
693 struct l2fwd_crypto_params *cparams;
694 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
695 struct rte_cryptodev_sym_session *session;
697 if (qconf->nb_rx_ports == 0) {
698 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
702 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
704 for (i = 0; i < qconf->nb_rx_ports; i++) {
706 portid = qconf->rx_port_list[i];
707 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
711 for (i = 0; i < qconf->nb_crypto_devs; i++) {
712 port_cparams[i].do_cipher = 0;
713 port_cparams[i].do_hash = 0;
714 port_cparams[i].do_aead = 0;
716 switch (options->xform_chain) {
717 case L2FWD_CRYPTO_AEAD:
718 port_cparams[i].do_aead = 1;
720 case L2FWD_CRYPTO_CIPHER_HASH:
721 case L2FWD_CRYPTO_HASH_CIPHER:
722 port_cparams[i].do_cipher = 1;
723 port_cparams[i].do_hash = 1;
725 case L2FWD_CRYPTO_HASH_ONLY:
726 port_cparams[i].do_hash = 1;
728 case L2FWD_CRYPTO_CIPHER_ONLY:
729 port_cparams[i].do_cipher = 1;
733 port_cparams[i].dev_id = qconf->cryptodev_list[i];
734 port_cparams[i].qp_id = 0;
736 port_cparams[i].block_size = options->block_size;
738 if (port_cparams[i].do_hash) {
739 port_cparams[i].auth_iv.data = options->auth_iv.data;
740 port_cparams[i].auth_iv.length = options->auth_iv.length;
741 if (!options->auth_iv_param)
742 generate_random_key(port_cparams[i].auth_iv.data,
743 port_cparams[i].auth_iv.length);
744 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
745 port_cparams[i].hash_verify = 1;
747 port_cparams[i].hash_verify = 0;
749 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
750 port_cparams[i].digest_length =
751 options->auth_xform.auth.digest_length;
752 /* Set IV parameters */
753 if (options->auth_iv.length) {
754 options->auth_xform.auth.iv.offset =
755 IV_OFFSET + options->cipher_iv.length;
756 options->auth_xform.auth.iv.length =
757 options->auth_iv.length;
761 if (port_cparams[i].do_aead) {
762 port_cparams[i].aead_iv.data = options->aead_iv.data;
763 port_cparams[i].aead_iv.length = options->aead_iv.length;
764 if (!options->aead_iv_param)
765 generate_random_key(port_cparams[i].aead_iv.data,
766 port_cparams[i].aead_iv.length);
767 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
768 port_cparams[i].digest_length =
769 options->aead_xform.aead.digest_length;
770 if (options->aead_xform.aead.aad_length) {
771 port_cparams[i].aad.data = options->aad.data;
772 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
773 port_cparams[i].aad.length = options->aad.length;
774 if (!options->aad_param)
775 generate_random_key(port_cparams[i].aad.data,
776 port_cparams[i].aad.length);
778 * If doing AES-CCM, first 18 bytes has to be reserved,
779 * and actual AAD should start from byte 18
781 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
782 memmove(port_cparams[i].aad.data + 18,
783 port_cparams[i].aad.data,
784 port_cparams[i].aad.length);
787 port_cparams[i].aad.length = 0;
789 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
790 port_cparams[i].hash_verify = 1;
792 port_cparams[i].hash_verify = 0;
794 /* Set IV parameters */
795 options->aead_xform.aead.iv.offset = IV_OFFSET;
796 options->aead_xform.aead.iv.length = options->aead_iv.length;
799 if (port_cparams[i].do_cipher) {
800 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
801 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
802 if (!options->cipher_iv_param)
803 generate_random_key(port_cparams[i].cipher_iv.data,
804 port_cparams[i].cipher_iv.length);
806 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
807 /* Set IV parameters */
808 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
809 options->cipher_xform.cipher.iv.length =
810 options->cipher_iv.length;
813 session = initialize_crypto_session(options,
814 port_cparams[i].dev_id);
816 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
818 port_cparams[i].session = session;
820 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
821 port_cparams[i].dev_id);
824 l2fwd_crypto_options_print(options);
827 * Initialize previous tsc timestamp before the loop,
828 * to avoid showing the port statistics immediately,
829 * so user can see the crypto information.
831 prev_tsc = rte_rdtsc();
834 cur_tsc = rte_rdtsc();
837 * Crypto device/TX burst queue drain
839 diff_tsc = cur_tsc - prev_tsc;
840 if (unlikely(diff_tsc > drain_tsc)) {
841 /* Enqueue all crypto ops remaining in buffers */
842 for (i = 0; i < qconf->nb_crypto_devs; i++) {
843 cparams = &port_cparams[i];
844 len = qconf->op_buf[cparams->dev_id].len;
845 l2fwd_crypto_send_burst(qconf, len, cparams);
846 qconf->op_buf[cparams->dev_id].len = 0;
848 /* Transmit all packets remaining in buffers */
849 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
850 if (qconf->pkt_buf[portid].len == 0)
852 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
853 qconf->pkt_buf[portid].len,
855 qconf->pkt_buf[portid].len = 0;
858 /* if timer is enabled */
859 if (timer_period > 0) {
861 /* advance the timer */
862 timer_tsc += diff_tsc;
864 /* if timer has reached its timeout */
865 if (unlikely(timer_tsc >=
866 (uint64_t)timer_period)) {
868 /* do this only on master core */
869 if (lcore_id == rte_get_master_lcore()
870 && options->refresh_period) {
881 * Read packet from RX queues
883 for (i = 0; i < qconf->nb_rx_ports; i++) {
884 portid = qconf->rx_port_list[i];
886 cparams = &port_cparams[i];
888 nb_rx = rte_eth_rx_burst(portid, 0,
889 pkts_burst, MAX_PKT_BURST);
891 port_statistics[portid].rx += nb_rx;
895 * If we can't allocate a crypto_ops, then drop
896 * the rest of the burst and dequeue and
897 * process the packets to free offload structs
899 if (rte_crypto_op_bulk_alloc(
900 l2fwd_crypto_op_pool,
901 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
904 for (j = 0; j < nb_rx; j++)
905 rte_pktmbuf_free(pkts_burst[j]);
910 /* Enqueue packets from Crypto device*/
911 for (j = 0; j < nb_rx; j++) {
914 l2fwd_simple_crypto_enqueue(m,
915 ops_burst[j], cparams);
919 /* Dequeue packets from Crypto device */
921 nb_rx = rte_cryptodev_dequeue_burst(
922 cparams->dev_id, cparams->qp_id,
923 ops_burst, MAX_PKT_BURST);
925 crypto_statistics[cparams->dev_id].dequeued +=
928 /* Forward crypto'd packets */
929 for (j = 0; j < nb_rx; j++) {
930 m = ops_burst[j]->sym->m_src;
932 rte_crypto_op_free(ops_burst[j]);
933 l2fwd_simple_forward(m, portid,
936 } while (nb_rx == MAX_PKT_BURST);
942 l2fwd_launch_one_lcore(void *arg)
944 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
948 /* Display command line arguments usage */
950 l2fwd_crypto_usage(const char *prgname)
952 printf("%s [EAL options] --\n"
953 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
954 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
955 " -s manage all ports from single lcore\n"
956 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
957 " (0 to disable, 10 default, 86400 maximum)\n"
959 " --cdev_type HW / SW / ANY\n"
960 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
961 " HASH_ONLY / AEAD\n"
963 " --cipher_algo ALGO\n"
964 " --cipher_op ENCRYPT / DECRYPT\n"
965 " --cipher_key KEY (bytes separated with \":\")\n"
966 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
967 " --cipher_iv IV (bytes separated with \":\")\n"
968 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
970 " --auth_algo ALGO\n"
971 " --auth_op GENERATE / VERIFY\n"
972 " --auth_key KEY (bytes separated with \":\")\n"
973 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
974 " --auth_iv IV (bytes separated with \":\")\n"
975 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
977 " --aead_algo ALGO\n"
978 " --aead_op ENCRYPT / DECRYPT\n"
979 " --aead_key KEY (bytes separated with \":\")\n"
980 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
981 " --aead_iv IV (bytes separated with \":\")\n"
982 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
983 " --aad AAD (bytes separated with \":\")\n"
984 " --aad_random_size SIZE: size of AAD when generated randomly\n"
986 " --digest_size SIZE: size of digest to be generated/verified\n"
989 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
991 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
993 " - The source MAC address is replaced by the TX port MAC address\n"
994 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
998 /** Parse crypto device type command line argument */
1000 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1002 if (strcmp("HW", optarg) == 0) {
1003 *type = CDEV_TYPE_HW;
1005 } else if (strcmp("SW", optarg) == 0) {
1006 *type = CDEV_TYPE_SW;
1008 } else if (strcmp("ANY", optarg) == 0) {
1009 *type = CDEV_TYPE_ANY;
1016 /** Parse crypto chain xform command line argument */
1018 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1020 if (strcmp("CIPHER_HASH", optarg) == 0) {
1021 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1023 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1024 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1026 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1027 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1029 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1030 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1032 } else if (strcmp("AEAD", optarg) == 0) {
1033 options->xform_chain = L2FWD_CRYPTO_AEAD;
1040 /** Parse crypto cipher algo option command line argument */
1042 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1045 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1046 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1047 "not supported!\n");
1054 /** Parse crypto cipher operation command line argument */
1056 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1058 if (strcmp("ENCRYPT", optarg) == 0) {
1059 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1061 } else if (strcmp("DECRYPT", optarg) == 0) {
1062 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1066 printf("Cipher operation not supported!\n");
1070 /** Parse bytes from command line argument */
1072 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1074 unsigned byte_count;
1078 for (byte_count = 0, token = strtok(input_arg, ":");
1079 (byte_count < max_size) && (token != NULL);
1080 token = strtok(NULL, ":")) {
1082 int number = (int)strtol(token, NULL, 16);
1084 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1087 data[byte_count++] = (uint8_t)number;
1093 /** Parse size param*/
1095 parse_size(int *size, const char *q_arg)
1100 /* parse hexadecimal string */
1101 n = strtoul(q_arg, &end, 10);
1102 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1106 printf("invalid size\n");
1114 /** Parse crypto cipher operation command line argument */
1116 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1118 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1119 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1120 "not supported!\n");
1128 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1130 if (strcmp("VERIFY", optarg) == 0) {
1131 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1133 } else if (strcmp("GENERATE", optarg) == 0) {
1134 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1138 printf("Authentication operation specified not supported!\n");
1143 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1145 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1146 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1147 "not supported!\n");
1155 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1157 if (strcmp("ENCRYPT", optarg) == 0) {
1158 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1160 } else if (strcmp("DECRYPT", optarg) == 0) {
1161 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1165 printf("AEAD operation specified not supported!\n");
1169 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1175 /* parse hexadecimal string */
1176 pm = strtoul(q_arg, &end, 16);
1177 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1180 options->cryptodev_mask = pm;
1181 if (options->cryptodev_mask == 0) {
1182 printf("invalid cryptodev_mask specified\n");
1189 /** Parse long options */
1191 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1192 struct option *lgopts, int option_index)
1196 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1197 retval = parse_cryptodev_type(&options->type, optarg);
1199 snprintf(options->string_type, MAX_STR_LEN,
1204 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1205 return parse_crypto_opt_chain(options, optarg);
1207 /* Cipher options */
1208 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1209 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1212 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1213 return parse_cipher_op(&options->cipher_xform.cipher.op,
1216 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1217 options->ckey_param = 1;
1218 options->cipher_xform.cipher.key.length =
1219 parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1221 if (options->cipher_xform.cipher.key.length > 0)
1227 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1228 return parse_size(&options->ckey_random_size, optarg);
1230 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1231 options->cipher_iv_param = 1;
1232 options->cipher_iv.length =
1233 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1234 if (options->cipher_iv.length > 0)
1240 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1241 return parse_size(&options->cipher_iv_random_size, optarg);
1243 /* Authentication options */
1244 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1245 return parse_auth_algo(&options->auth_xform.auth.algo,
1249 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1250 return parse_auth_op(&options->auth_xform.auth.op,
1253 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1254 options->akey_param = 1;
1255 options->auth_xform.auth.key.length =
1256 parse_bytes(options->auth_xform.auth.key.data, optarg,
1258 if (options->auth_xform.auth.key.length > 0)
1264 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1265 return parse_size(&options->akey_random_size, optarg);
1268 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1269 options->auth_iv_param = 1;
1270 options->auth_iv.length =
1271 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1272 if (options->auth_iv.length > 0)
1278 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1279 return parse_size(&options->auth_iv_random_size, optarg);
1282 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1283 return parse_aead_algo(&options->aead_xform.aead.algo,
1287 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1288 return parse_aead_op(&options->aead_xform.aead.op,
1291 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1292 options->aead_key_param = 1;
1293 options->aead_xform.aead.key.length =
1294 parse_bytes(options->aead_xform.aead.key.data, optarg,
1296 if (options->aead_xform.aead.key.length > 0)
1302 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1303 return parse_size(&options->aead_key_random_size, optarg);
1306 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1307 options->aead_iv_param = 1;
1308 options->aead_iv.length =
1309 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1310 if (options->aead_iv.length > 0)
1316 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1317 return parse_size(&options->aead_iv_random_size, optarg);
1319 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1320 options->aad_param = 1;
1321 options->aad.length =
1322 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1323 if (options->aad.length > 0)
1329 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1330 return parse_size(&options->aad_random_size, optarg);
1333 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1334 return parse_size(&options->digest_size, optarg);
1337 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1338 options->sessionless = 1;
1342 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1343 return parse_cryptodev_mask(options, optarg);
1345 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1346 options->mac_updating = 1;
1350 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1351 options->mac_updating = 0;
1358 /** Parse port mask */
1360 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1366 /* parse hexadecimal string */
1367 pm = strtoul(q_arg, &end, 16);
1368 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1371 options->portmask = pm;
1372 if (options->portmask == 0) {
1373 printf("invalid portmask specified\n");
1380 /** Parse number of queues */
1382 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1388 /* parse hexadecimal string */
1389 n = strtoul(q_arg, &end, 10);
1390 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1392 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1395 options->nb_ports_per_lcore = n;
1396 if (options->nb_ports_per_lcore == 0) {
1397 printf("invalid number of ports selected\n");
1404 /** Parse timer period */
1406 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1412 /* parse number string */
1413 n = (unsigned)strtol(q_arg, &end, 10);
1414 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1417 if (n >= MAX_TIMER_PERIOD) {
1418 printf("Warning refresh period specified %lu is greater than "
1419 "max value %lu! using max value",
1420 n, MAX_TIMER_PERIOD);
1421 n = MAX_TIMER_PERIOD;
1424 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1429 /** Generate default options for application */
1431 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1433 options->portmask = 0xffffffff;
1434 options->nb_ports_per_lcore = 1;
1435 options->refresh_period = 10000;
1436 options->single_lcore = 0;
1437 options->sessionless = 0;
1439 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1442 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1443 options->cipher_xform.next = NULL;
1444 options->ckey_param = 0;
1445 options->ckey_random_size = -1;
1446 options->cipher_xform.cipher.key.length = 0;
1447 options->cipher_iv_param = 0;
1448 options->cipher_iv_random_size = -1;
1449 options->cipher_iv.length = 0;
1451 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1452 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1454 /* Authentication Data */
1455 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1456 options->auth_xform.next = NULL;
1457 options->akey_param = 0;
1458 options->akey_random_size = -1;
1459 options->auth_xform.auth.key.length = 0;
1460 options->auth_iv_param = 0;
1461 options->auth_iv_random_size = -1;
1462 options->auth_iv.length = 0;
1464 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1465 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1468 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1469 options->aead_xform.next = NULL;
1470 options->aead_key_param = 0;
1471 options->aead_key_random_size = -1;
1472 options->aead_xform.aead.key.length = 0;
1473 options->aead_iv_param = 0;
1474 options->aead_iv_random_size = -1;
1475 options->aead_iv.length = 0;
1477 options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1478 options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1480 options->aad_param = 0;
1481 options->aad_random_size = -1;
1482 options->aad.length = 0;
1484 options->digest_size = -1;
1486 options->type = CDEV_TYPE_ANY;
1487 options->cryptodev_mask = UINT64_MAX;
1489 options->mac_updating = 1;
1493 display_cipher_info(struct l2fwd_crypto_options *options)
1495 printf("\n---- Cipher information ---\n");
1496 printf("Algorithm: %s\n",
1497 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1498 rte_hexdump(stdout, "Cipher key:",
1499 options->cipher_xform.cipher.key.data,
1500 options->cipher_xform.cipher.key.length);
1501 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1505 display_auth_info(struct l2fwd_crypto_options *options)
1507 printf("\n---- Authentication information ---\n");
1508 printf("Algorithm: %s\n",
1509 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1510 rte_hexdump(stdout, "Auth key:",
1511 options->auth_xform.auth.key.data,
1512 options->auth_xform.auth.key.length);
1513 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1517 display_aead_info(struct l2fwd_crypto_options *options)
1519 printf("\n---- AEAD information ---\n");
1520 printf("Algorithm: %s\n",
1521 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1522 rte_hexdump(stdout, "AEAD key:",
1523 options->aead_xform.aead.key.data,
1524 options->aead_xform.aead.key.length);
1525 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1526 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1530 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1532 char string_cipher_op[MAX_STR_LEN];
1533 char string_auth_op[MAX_STR_LEN];
1534 char string_aead_op[MAX_STR_LEN];
1536 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1537 strcpy(string_cipher_op, "Encrypt");
1539 strcpy(string_cipher_op, "Decrypt");
1541 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1542 strcpy(string_auth_op, "Auth generate");
1544 strcpy(string_auth_op, "Auth verify");
1546 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1547 strcpy(string_aead_op, "Authenticated encryption");
1549 strcpy(string_aead_op, "Authenticated decryption");
1552 printf("Options:-\nn");
1553 printf("portmask: %x\n", options->portmask);
1554 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1555 printf("refresh period : %u\n", options->refresh_period);
1556 printf("single lcore mode: %s\n",
1557 options->single_lcore ? "enabled" : "disabled");
1558 printf("stats_printing: %s\n",
1559 options->refresh_period == 0 ? "disabled" : "enabled");
1561 printf("sessionless crypto: %s\n",
1562 options->sessionless ? "enabled" : "disabled");
1564 if (options->ckey_param && (options->ckey_random_size != -1))
1565 printf("Cipher key already parsed, ignoring size of random key\n");
1567 if (options->akey_param && (options->akey_random_size != -1))
1568 printf("Auth key already parsed, ignoring size of random key\n");
1570 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1571 printf("Cipher IV already parsed, ignoring size of random IV\n");
1573 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1574 printf("Auth IV already parsed, ignoring size of random IV\n");
1576 if (options->aad_param && (options->aad_random_size != -1))
1577 printf("AAD already parsed, ignoring size of random AAD\n");
1579 printf("\nCrypto chain: ");
1580 switch (options->xform_chain) {
1581 case L2FWD_CRYPTO_AEAD:
1582 printf("Input --> %s --> Output\n", string_aead_op);
1583 display_aead_info(options);
1585 case L2FWD_CRYPTO_CIPHER_HASH:
1586 printf("Input --> %s --> %s --> Output\n",
1587 string_cipher_op, string_auth_op);
1588 display_cipher_info(options);
1589 display_auth_info(options);
1591 case L2FWD_CRYPTO_HASH_CIPHER:
1592 printf("Input --> %s --> %s --> Output\n",
1593 string_auth_op, string_cipher_op);
1594 display_cipher_info(options);
1595 display_auth_info(options);
1597 case L2FWD_CRYPTO_HASH_ONLY:
1598 printf("Input --> %s --> Output\n", string_auth_op);
1599 display_auth_info(options);
1601 case L2FWD_CRYPTO_CIPHER_ONLY:
1602 printf("Input --> %s --> Output\n", string_cipher_op);
1603 display_cipher_info(options);
1608 /* Parse the argument given in the command line of the application */
1610 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1611 int argc, char **argv)
1613 int opt, retval, option_index;
1614 char **argvopt = argv, *prgname = argv[0];
1616 static struct option lgopts[] = {
1617 { "sessionless", no_argument, 0, 0 },
1619 { "cdev_type", required_argument, 0, 0 },
1620 { "chain", required_argument, 0, 0 },
1622 { "cipher_algo", required_argument, 0, 0 },
1623 { "cipher_op", required_argument, 0, 0 },
1624 { "cipher_key", required_argument, 0, 0 },
1625 { "cipher_key_random_size", required_argument, 0, 0 },
1626 { "cipher_iv", required_argument, 0, 0 },
1627 { "cipher_iv_random_size", required_argument, 0, 0 },
1629 { "auth_algo", required_argument, 0, 0 },
1630 { "auth_op", required_argument, 0, 0 },
1631 { "auth_key", required_argument, 0, 0 },
1632 { "auth_key_random_size", required_argument, 0, 0 },
1633 { "auth_iv", required_argument, 0, 0 },
1634 { "auth_iv_random_size", required_argument, 0, 0 },
1636 { "aead_algo", required_argument, 0, 0 },
1637 { "aead_op", required_argument, 0, 0 },
1638 { "aead_key", required_argument, 0, 0 },
1639 { "aead_key_random_size", required_argument, 0, 0 },
1640 { "aead_iv", required_argument, 0, 0 },
1641 { "aead_iv_random_size", required_argument, 0, 0 },
1643 { "aad", required_argument, 0, 0 },
1644 { "aad_random_size", required_argument, 0, 0 },
1646 { "digest_size", required_argument, 0, 0 },
1648 { "sessionless", no_argument, 0, 0 },
1649 { "cryptodev_mask", required_argument, 0, 0},
1651 { "mac-updating", no_argument, 0, 0},
1652 { "no-mac-updating", no_argument, 0, 0},
1657 l2fwd_crypto_default_options(options);
1659 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1660 &option_index)) != EOF) {
1664 retval = l2fwd_crypto_parse_args_long_options(options,
1665 lgopts, option_index);
1667 l2fwd_crypto_usage(prgname);
1674 retval = l2fwd_crypto_parse_portmask(options, optarg);
1676 l2fwd_crypto_usage(prgname);
1683 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1685 l2fwd_crypto_usage(prgname);
1692 options->single_lcore = 1;
1698 retval = l2fwd_crypto_parse_timer_period(options,
1701 l2fwd_crypto_usage(prgname);
1707 l2fwd_crypto_usage(prgname);
1714 argv[optind-1] = prgname;
1717 optind = 1; /* reset getopt lib */
1722 /* Check the link status of all ports in up to 9s, and print them finally */
1724 check_all_ports_link_status(uint32_t port_mask)
1726 #define CHECK_INTERVAL 100 /* 100ms */
1727 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1729 uint8_t count, all_ports_up, print_flag = 0;
1730 struct rte_eth_link link;
1732 printf("\nChecking link status");
1734 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1736 RTE_ETH_FOREACH_DEV(portid) {
1737 if ((port_mask & (1 << portid)) == 0)
1739 memset(&link, 0, sizeof(link));
1740 rte_eth_link_get_nowait(portid, &link);
1741 /* print link status if flag set */
1742 if (print_flag == 1) {
1743 if (link.link_status)
1745 "Port%d Link Up. Speed %u Mbps - %s\n",
1746 portid, link.link_speed,
1747 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1748 ("full-duplex") : ("half-duplex\n"));
1750 printf("Port %d Link Down\n", portid);
1753 /* clear all_ports_up flag if any link down */
1754 if (link.link_status == ETH_LINK_DOWN) {
1759 /* after finally printing all link status, get out */
1760 if (print_flag == 1)
1763 if (all_ports_up == 0) {
1766 rte_delay_ms(CHECK_INTERVAL);
1769 /* set the print_flag if all ports up or timeout */
1770 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1777 /* Check if device has to be HW/SW or any */
1779 check_type(const struct l2fwd_crypto_options *options,
1780 const struct rte_cryptodev_info *dev_info)
1782 if (options->type == CDEV_TYPE_HW &&
1783 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1785 if (options->type == CDEV_TYPE_SW &&
1786 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1788 if (options->type == CDEV_TYPE_ANY)
1794 static const struct rte_cryptodev_capabilities *
1795 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1796 const struct rte_cryptodev_info *dev_info,
1800 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1801 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1802 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1803 options->cipher_xform.cipher.algo;
1805 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1806 cap_cipher_algo = cap->sym.cipher.algo;
1807 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1808 if (cap_cipher_algo == opt_cipher_algo) {
1809 if (check_type(options, dev_info) == 0)
1813 cap = &dev_info->capabilities[++i];
1816 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1817 printf("Algorithm %s not supported by cryptodev %u"
1818 " or device not of preferred type (%s)\n",
1819 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1821 options->string_type);
1828 static const struct rte_cryptodev_capabilities *
1829 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1830 const struct rte_cryptodev_info *dev_info,
1834 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1835 enum rte_crypto_auth_algorithm cap_auth_algo;
1836 enum rte_crypto_auth_algorithm opt_auth_algo =
1837 options->auth_xform.auth.algo;
1839 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1840 cap_auth_algo = cap->sym.auth.algo;
1841 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1842 if (cap_auth_algo == opt_auth_algo) {
1843 if (check_type(options, dev_info) == 0)
1847 cap = &dev_info->capabilities[++i];
1850 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1851 printf("Algorithm %s not supported by cryptodev %u"
1852 " or device not of preferred type (%s)\n",
1853 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1855 options->string_type);
1862 static const struct rte_cryptodev_capabilities *
1863 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1864 const struct rte_cryptodev_info *dev_info,
1868 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1869 enum rte_crypto_aead_algorithm cap_aead_algo;
1870 enum rte_crypto_aead_algorithm opt_aead_algo =
1871 options->aead_xform.aead.algo;
1873 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1874 cap_aead_algo = cap->sym.aead.algo;
1875 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1876 if (cap_aead_algo == opt_aead_algo) {
1877 if (check_type(options, dev_info) == 0)
1881 cap = &dev_info->capabilities[++i];
1884 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1885 printf("Algorithm %s not supported by cryptodev %u"
1886 " or device not of preferred type (%s)\n",
1887 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1889 options->string_type);
1896 /* Check if the device is enabled by cryptodev_mask */
1898 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1901 if (options->cryptodev_mask & (1 << cdev_id))
1908 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1914 if (increment == 0) {
1921 /* Range of values */
1922 for (supp_size = min; supp_size <= max; supp_size += increment) {
1923 if (length == supp_size)
1931 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1932 unsigned int iv_param, int iv_random_size,
1936 * Check if length of provided IV is supported
1937 * by the algorithm chosen.
1940 if (check_supported_size(iv_length,
1943 iv_range_size->increment)
1947 * Check if length of IV to be randomly generated
1948 * is supported by the algorithm chosen.
1950 } else if (iv_random_size != -1) {
1951 if (check_supported_size(iv_random_size,
1954 iv_range_size->increment)
1963 check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id)
1965 struct rte_cryptodev_info dev_info;
1966 const struct rte_cryptodev_capabilities *cap;
1968 rte_cryptodev_info_get(cdev_id, &dev_info);
1970 /* Set AEAD parameters */
1971 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
1972 /* Check if device supports AEAD algo */
1973 cap = check_device_support_aead_algo(options, &dev_info,
1978 if (check_iv_param(&cap->sym.aead.iv_size,
1979 options->aead_iv_param,
1980 options->aead_iv_random_size,
1981 options->aead_iv.length) != 0) {
1982 RTE_LOG(DEBUG, USER1,
1983 "Device %u does not support IV length\n",
1989 * Check if length of provided AEAD key is supported
1990 * by the algorithm chosen.
1992 if (options->aead_key_param) {
1993 if (check_supported_size(
1994 options->aead_xform.aead.key.length,
1995 cap->sym.aead.key_size.min,
1996 cap->sym.aead.key_size.max,
1997 cap->sym.aead.key_size.increment)
1999 RTE_LOG(DEBUG, USER1,
2000 "Device %u does not support "
2001 "AEAD key length\n",
2006 * Check if length of the aead key to be randomly generated
2007 * is supported by the algorithm chosen.
2009 } else if (options->aead_key_random_size != -1) {
2010 if (check_supported_size(options->aead_key_random_size,
2011 cap->sym.aead.key_size.min,
2012 cap->sym.aead.key_size.max,
2013 cap->sym.aead.key_size.increment)
2015 RTE_LOG(DEBUG, USER1,
2016 "Device %u does not support "
2017 "AEAD key length\n",
2025 * Check if length of provided AAD is supported
2026 * by the algorithm chosen.
2028 if (options->aad_param) {
2029 if (check_supported_size(options->aad.length,
2030 cap->sym.aead.aad_size.min,
2031 cap->sym.aead.aad_size.max,
2032 cap->sym.aead.aad_size.increment)
2034 RTE_LOG(DEBUG, USER1,
2035 "Device %u does not support "
2041 * Check if length of AAD to be randomly generated
2042 * is supported by the algorithm chosen.
2044 } else if (options->aad_random_size != -1) {
2045 if (check_supported_size(options->aad_random_size,
2046 cap->sym.aead.aad_size.min,
2047 cap->sym.aead.aad_size.max,
2048 cap->sym.aead.aad_size.increment)
2050 RTE_LOG(DEBUG, USER1,
2051 "Device %u does not support "
2058 /* Check if digest size is supported by the algorithm. */
2059 if (options->digest_size != -1) {
2060 if (check_supported_size(options->digest_size,
2061 cap->sym.aead.digest_size.min,
2062 cap->sym.aead.digest_size.max,
2063 cap->sym.aead.digest_size.increment)
2065 RTE_LOG(DEBUG, USER1,
2066 "Device %u does not support "
2074 /* Set cipher parameters */
2075 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2076 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2077 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2078 /* Check if device supports cipher algo */
2079 cap = check_device_support_cipher_algo(options, &dev_info,
2084 if (check_iv_param(&cap->sym.cipher.iv_size,
2085 options->cipher_iv_param,
2086 options->cipher_iv_random_size,
2087 options->cipher_iv.length) != 0) {
2088 RTE_LOG(DEBUG, USER1,
2089 "Device %u does not support IV length\n",
2095 * Check if length of provided cipher key is supported
2096 * by the algorithm chosen.
2098 if (options->ckey_param) {
2099 if (check_supported_size(
2100 options->cipher_xform.cipher.key.length,
2101 cap->sym.cipher.key_size.min,
2102 cap->sym.cipher.key_size.max,
2103 cap->sym.cipher.key_size.increment)
2105 RTE_LOG(DEBUG, USER1,
2106 "Device %u does not support cipher "
2112 * Check if length of the cipher key to be randomly generated
2113 * is supported by the algorithm chosen.
2115 } else if (options->ckey_random_size != -1) {
2116 if (check_supported_size(options->ckey_random_size,
2117 cap->sym.cipher.key_size.min,
2118 cap->sym.cipher.key_size.max,
2119 cap->sym.cipher.key_size.increment)
2121 RTE_LOG(DEBUG, USER1,
2122 "Device %u does not support cipher "
2130 /* Set auth parameters */
2131 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2132 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2133 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2134 /* Check if device supports auth algo */
2135 cap = check_device_support_auth_algo(options, &dev_info,
2140 if (check_iv_param(&cap->sym.auth.iv_size,
2141 options->auth_iv_param,
2142 options->auth_iv_random_size,
2143 options->auth_iv.length) != 0) {
2144 RTE_LOG(DEBUG, USER1,
2145 "Device %u does not support IV length\n",
2150 * Check if length of provided auth key is supported
2151 * by the algorithm chosen.
2153 if (options->akey_param) {
2154 if (check_supported_size(
2155 options->auth_xform.auth.key.length,
2156 cap->sym.auth.key_size.min,
2157 cap->sym.auth.key_size.max,
2158 cap->sym.auth.key_size.increment)
2160 RTE_LOG(DEBUG, USER1,
2161 "Device %u does not support auth "
2167 * Check if length of the auth key to be randomly generated
2168 * is supported by the algorithm chosen.
2170 } else if (options->akey_random_size != -1) {
2171 if (check_supported_size(options->akey_random_size,
2172 cap->sym.auth.key_size.min,
2173 cap->sym.auth.key_size.max,
2174 cap->sym.auth.key_size.increment)
2176 RTE_LOG(DEBUG, USER1,
2177 "Device %u does not support auth "
2184 /* Check if digest size is supported by the algorithm. */
2185 if (options->digest_size != -1) {
2186 if (check_supported_size(options->digest_size,
2187 cap->sym.auth.digest_size.min,
2188 cap->sym.auth.digest_size.max,
2189 cap->sym.auth.digest_size.increment)
2191 RTE_LOG(DEBUG, USER1,
2192 "Device %u does not support "
2204 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2205 uint8_t *enabled_cdevs)
2207 uint8_t cdev_id, cdev_count, enabled_cdev_count = 0;
2208 const struct rte_cryptodev_capabilities *cap;
2209 unsigned int sess_sz, max_sess_sz = 0;
2210 uint32_t sessions_needed = 0;
2213 cdev_count = rte_cryptodev_count();
2214 if (cdev_count == 0) {
2215 printf("No crypto devices available\n");
2219 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2221 if (check_cryptodev_mask(options, cdev_id) < 0)
2224 if (check_capabilities(options, cdev_id) < 0)
2227 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2228 if (sess_sz > max_sess_sz)
2229 max_sess_sz = sess_sz;
2231 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2233 enabled_cdevs[cdev_id] = 1;
2234 enabled_cdev_count++;
2237 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2238 struct rte_cryptodev_qp_conf qp_conf;
2239 struct rte_cryptodev_info dev_info;
2241 if (enabled_cdevs[cdev_id] == 0)
2244 retval = rte_cryptodev_socket_id(cdev_id);
2247 printf("Invalid crypto device id used\n");
2251 uint8_t socket_id = (uint8_t) retval;
2253 struct rte_cryptodev_config conf = {
2254 .nb_queue_pairs = 1,
2255 .socket_id = socket_id,
2258 rte_cryptodev_info_get(cdev_id, &dev_info);
2261 * Two sessions objects are required for each session
2262 * (one for the header, one for the private data)
2264 if (!strcmp(dev_info.driver_name, "crypto_scheduler")) {
2265 #ifdef RTE_LIBRTE_PMD_CRYPTO_SCHEDULER
2266 uint32_t nb_slaves =
2267 rte_cryptodev_scheduler_slaves_get(cdev_id,
2270 sessions_needed = 2 * enabled_cdev_count * nb_slaves;
2273 sessions_needed = 2 * enabled_cdev_count;
2275 if (session_pool_socket[socket_id] == NULL) {
2276 char mp_name[RTE_MEMPOOL_NAMESIZE];
2277 struct rte_mempool *sess_mp;
2279 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2280 "sess_mp_%u", socket_id);
2283 * Create enough objects for session headers and
2284 * device private data
2286 sess_mp = rte_mempool_create(mp_name,
2289 SESSION_POOL_CACHE_SIZE,
2290 0, NULL, NULL, NULL,
2294 if (sess_mp == NULL) {
2295 printf("Cannot create session pool on socket %d\n",
2300 printf("Allocated session pool on socket %d\n", socket_id);
2301 session_pool_socket[socket_id] = sess_mp;
2304 /* Set AEAD parameters */
2305 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2306 cap = check_device_support_aead_algo(options, &dev_info,
2309 options->block_size = cap->sym.aead.block_size;
2311 /* Set IV if not provided from command line */
2312 if (options->aead_iv_param == 0) {
2313 if (options->aead_iv_random_size != -1)
2314 options->aead_iv.length =
2315 options->aead_iv_random_size;
2316 /* No size provided, use minimum size. */
2318 options->aead_iv.length =
2319 cap->sym.aead.iv_size.min;
2322 /* Set key if not provided from command line */
2323 if (options->aead_key_param == 0) {
2324 if (options->aead_key_random_size != -1)
2325 options->aead_xform.aead.key.length =
2326 options->aead_key_random_size;
2327 /* No size provided, use minimum size. */
2329 options->aead_xform.aead.key.length =
2330 cap->sym.aead.key_size.min;
2332 generate_random_key(
2333 options->aead_xform.aead.key.data,
2334 options->aead_xform.aead.key.length);
2337 /* Set AAD if not provided from command line */
2338 if (options->aad_param == 0) {
2339 if (options->aad_random_size != -1)
2340 options->aad.length =
2341 options->aad_random_size;
2342 /* No size provided, use minimum size. */
2344 options->aad.length =
2345 cap->sym.auth.aad_size.min;
2348 options->aead_xform.aead.aad_length =
2349 options->aad.length;
2351 /* Set digest size if not provided from command line */
2352 if (options->digest_size != -1)
2353 options->aead_xform.aead.digest_length =
2354 options->digest_size;
2355 /* No size provided, use minimum size. */
2357 options->aead_xform.aead.digest_length =
2358 cap->sym.aead.digest_size.min;
2361 /* Set cipher parameters */
2362 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2363 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2364 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2365 cap = check_device_support_cipher_algo(options, &dev_info,
2367 options->block_size = cap->sym.cipher.block_size;
2369 /* Set IV if not provided from command line */
2370 if (options->cipher_iv_param == 0) {
2371 if (options->cipher_iv_random_size != -1)
2372 options->cipher_iv.length =
2373 options->cipher_iv_random_size;
2374 /* No size provided, use minimum size. */
2376 options->cipher_iv.length =
2377 cap->sym.cipher.iv_size.min;
2380 /* Set key if not provided from command line */
2381 if (options->ckey_param == 0) {
2382 if (options->ckey_random_size != -1)
2383 options->cipher_xform.cipher.key.length =
2384 options->ckey_random_size;
2385 /* No size provided, use minimum size. */
2387 options->cipher_xform.cipher.key.length =
2388 cap->sym.cipher.key_size.min;
2390 generate_random_key(
2391 options->cipher_xform.cipher.key.data,
2392 options->cipher_xform.cipher.key.length);
2396 /* Set auth parameters */
2397 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2398 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2399 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2400 cap = check_device_support_auth_algo(options, &dev_info,
2403 /* Set IV if not provided from command line */
2404 if (options->auth_iv_param == 0) {
2405 if (options->auth_iv_random_size != -1)
2406 options->auth_iv.length =
2407 options->auth_iv_random_size;
2408 /* No size provided, use minimum size. */
2410 options->auth_iv.length =
2411 cap->sym.auth.iv_size.min;
2414 /* Set key if not provided from command line */
2415 if (options->akey_param == 0) {
2416 if (options->akey_random_size != -1)
2417 options->auth_xform.auth.key.length =
2418 options->akey_random_size;
2419 /* No size provided, use minimum size. */
2421 options->auth_xform.auth.key.length =
2422 cap->sym.auth.key_size.min;
2424 generate_random_key(
2425 options->auth_xform.auth.key.data,
2426 options->auth_xform.auth.key.length);
2429 /* Set digest size if not provided from command line */
2430 if (options->digest_size != -1)
2431 options->auth_xform.auth.digest_length =
2432 options->digest_size;
2433 /* No size provided, use minimum size. */
2435 options->auth_xform.auth.digest_length =
2436 cap->sym.auth.digest_size.min;
2439 retval = rte_cryptodev_configure(cdev_id, &conf);
2441 printf("Failed to configure cryptodev %u", cdev_id);
2445 qp_conf.nb_descriptors = 2048;
2447 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2448 socket_id, session_pool_socket[socket_id]);
2450 printf("Failed to setup queue pair %u on cryptodev %u",
2455 retval = rte_cryptodev_start(cdev_id);
2457 printf("Failed to start device %u: error %d\n",
2463 return enabled_cdev_count;
2467 initialize_ports(struct l2fwd_crypto_options *options)
2469 uint16_t last_portid = 0, portid;
2470 unsigned enabled_portcount = 0;
2471 unsigned nb_ports = rte_eth_dev_count_avail();
2473 if (nb_ports == 0) {
2474 printf("No Ethernet ports - bye\n");
2478 /* Reset l2fwd_dst_ports */
2479 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2480 l2fwd_dst_ports[portid] = 0;
2482 RTE_ETH_FOREACH_DEV(portid) {
2484 struct rte_eth_dev_info dev_info;
2485 struct rte_eth_rxconf rxq_conf;
2486 struct rte_eth_txconf txq_conf;
2487 struct rte_eth_conf local_port_conf = port_conf;
2489 /* Skip ports that are not enabled */
2490 if ((options->portmask & (1 << portid)) == 0)
2494 printf("Initializing port %u... ", portid);
2496 rte_eth_dev_info_get(portid, &dev_info);
2497 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2498 local_port_conf.txmode.offloads |=
2499 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2500 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2502 printf("Cannot configure device: err=%d, port=%u\n",
2507 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2510 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2515 /* init one RX queue */
2517 rxq_conf = dev_info.default_rxconf;
2518 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2519 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2520 rte_eth_dev_socket_id(portid),
2521 &rxq_conf, l2fwd_pktmbuf_pool);
2523 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2528 /* init one TX queue on each port */
2530 txq_conf = dev_info.default_txconf;
2531 txq_conf.offloads = local_port_conf.txmode.offloads;
2532 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2533 rte_eth_dev_socket_id(portid),
2536 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2543 retval = rte_eth_dev_start(portid);
2545 printf("rte_eth_dev_start:err=%d, port=%u\n",
2550 rte_eth_promiscuous_enable(portid);
2552 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2554 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2556 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2557 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2558 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2559 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2560 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2561 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2563 /* initialize port stats */
2564 memset(&port_statistics, 0, sizeof(port_statistics));
2566 /* Setup port forwarding table */
2567 if (enabled_portcount % 2) {
2568 l2fwd_dst_ports[portid] = last_portid;
2569 l2fwd_dst_ports[last_portid] = portid;
2571 last_portid = portid;
2574 l2fwd_enabled_port_mask |= (1 << portid);
2575 enabled_portcount++;
2578 if (enabled_portcount == 1) {
2579 l2fwd_dst_ports[last_portid] = last_portid;
2580 } else if (enabled_portcount % 2) {
2581 printf("odd number of ports in portmask- bye\n");
2585 check_all_ports_link_status(l2fwd_enabled_port_mask);
2587 return enabled_portcount;
2591 reserve_key_memory(struct l2fwd_crypto_options *options)
2593 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2595 if (options->cipher_xform.cipher.key.data == NULL)
2596 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2598 options->auth_xform.auth.key.data = rte_malloc("auth key",
2600 if (options->auth_xform.auth.key.data == NULL)
2601 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2603 options->aead_xform.aead.key.data = rte_malloc("aead key",
2605 if (options->aead_xform.aead.key.data == NULL)
2606 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2608 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2609 if (options->cipher_iv.data == NULL)
2610 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2612 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2613 if (options->auth_iv.data == NULL)
2614 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2616 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2617 if (options->aead_iv.data == NULL)
2618 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2620 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2621 if (options->aad.data == NULL)
2622 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2623 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2627 main(int argc, char **argv)
2629 struct lcore_queue_conf *qconf = NULL;
2630 struct l2fwd_crypto_options options;
2632 uint8_t nb_cryptodevs, cdev_id;
2634 unsigned lcore_id, rx_lcore_id = 0;
2635 int ret, enabled_cdevcount, enabled_portcount;
2636 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2639 ret = rte_eal_init(argc, argv);
2641 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2645 /* reserve memory for Cipher/Auth key and IV */
2646 reserve_key_memory(&options);
2648 /* parse application arguments (after the EAL ones) */
2649 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2651 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2653 printf("MAC updating %s\n",
2654 options.mac_updating ? "enabled" : "disabled");
2656 /* create the mbuf pool */
2657 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2658 sizeof(struct rte_crypto_op),
2659 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2660 if (l2fwd_pktmbuf_pool == NULL)
2661 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2663 /* create crypto op pool */
2664 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2665 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2667 if (l2fwd_crypto_op_pool == NULL)
2668 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2670 /* Enable Ethernet ports */
2671 enabled_portcount = initialize_ports(&options);
2672 if (enabled_portcount < 1)
2673 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2675 /* Initialize the port/queue configuration of each logical core */
2676 RTE_ETH_FOREACH_DEV(portid) {
2678 /* skip ports that are not enabled */
2679 if ((options.portmask & (1 << portid)) == 0)
2682 if (options.single_lcore && qconf == NULL) {
2683 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2685 if (rx_lcore_id >= RTE_MAX_LCORE)
2686 rte_exit(EXIT_FAILURE,
2687 "Not enough cores\n");
2689 } else if (!options.single_lcore) {
2690 /* get the lcore_id for this port */
2691 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2692 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2693 options.nb_ports_per_lcore) {
2695 if (rx_lcore_id >= RTE_MAX_LCORE)
2696 rte_exit(EXIT_FAILURE,
2697 "Not enough cores\n");
2701 /* Assigned a new logical core in the loop above. */
2702 if (qconf != &lcore_queue_conf[rx_lcore_id])
2703 qconf = &lcore_queue_conf[rx_lcore_id];
2705 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2706 qconf->nb_rx_ports++;
2708 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2711 /* Enable Crypto devices */
2712 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2714 if (enabled_cdevcount < 0)
2715 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2717 if (enabled_cdevcount < enabled_portcount)
2718 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2719 "has to be more or equal to number of ports (%d)\n",
2720 enabled_cdevcount, enabled_portcount);
2722 nb_cryptodevs = rte_cryptodev_count();
2724 /* Initialize the port/cryptodev configuration of each logical core */
2725 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2726 cdev_id < nb_cryptodevs && enabled_cdevcount;
2728 /* Crypto op not supported by crypto device */
2729 if (!enabled_cdevs[cdev_id])
2732 if (options.single_lcore && qconf == NULL) {
2733 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2735 if (rx_lcore_id >= RTE_MAX_LCORE)
2736 rte_exit(EXIT_FAILURE,
2737 "Not enough cores\n");
2739 } else if (!options.single_lcore) {
2740 /* get the lcore_id for this port */
2741 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2742 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2743 options.nb_ports_per_lcore) {
2745 if (rx_lcore_id >= RTE_MAX_LCORE)
2746 rte_exit(EXIT_FAILURE,
2747 "Not enough cores\n");
2751 /* Assigned a new logical core in the loop above. */
2752 if (qconf != &lcore_queue_conf[rx_lcore_id])
2753 qconf = &lcore_queue_conf[rx_lcore_id];
2755 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2756 qconf->nb_crypto_devs++;
2758 enabled_cdevcount--;
2760 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2764 /* launch per-lcore init on every lcore */
2765 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2767 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2768 if (rte_eal_wait_lcore(lcore_id) < 0)