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_string_fns.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_common.h>
26 #include <rte_cryptodev.h>
27 #include <rte_cycles.h>
28 #include <rte_debug.h>
30 #include <rte_ether.h>
31 #include <rte_ethdev.h>
32 #include <rte_interrupts.h>
34 #include <rte_launch.h>
35 #include <rte_lcore.h>
37 #include <rte_malloc.h>
39 #include <rte_memcpy.h>
40 #include <rte_memory.h>
41 #include <rte_mempool.h>
42 #include <rte_per_lcore.h>
43 #include <rte_prefetch.h>
44 #include <rte_random.h>
45 #include <rte_hexdump.h>
46 #ifdef RTE_CRYPTO_SCHEDULER
47 #include <rte_cryptodev_scheduler.h>
56 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
60 #define MAX_STR_LEN 32
61 #define MAX_KEY_SIZE 128
62 #define MAX_IV_SIZE 16
63 #define MAX_AAD_SIZE 65535
64 #define MAX_PKT_BURST 32
65 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
66 #define SESSION_POOL_CACHE_SIZE 0
68 #define MAXIMUM_IV_LENGTH 16
69 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
70 sizeof(struct rte_crypto_sym_op))
73 * Configurable number of RX/TX ring descriptors
75 #define RTE_TEST_RX_DESC_DEFAULT 1024
76 #define RTE_TEST_TX_DESC_DEFAULT 1024
78 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
79 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
81 /* ethernet addresses of ports */
82 static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
84 /* mask of enabled ports */
85 static uint64_t l2fwd_enabled_port_mask;
86 static uint64_t l2fwd_enabled_crypto_mask;
88 /* list of enabled ports */
89 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
94 struct rte_mbuf *buffer[MAX_PKT_BURST];
99 struct rte_crypto_op *buffer[MAX_PKT_BURST];
102 #define MAX_RX_QUEUE_PER_LCORE 16
103 #define MAX_TX_QUEUE_PER_PORT 16
105 enum l2fwd_crypto_xform_chain {
106 L2FWD_CRYPTO_CIPHER_HASH,
107 L2FWD_CRYPTO_HASH_CIPHER,
108 L2FWD_CRYPTO_CIPHER_ONLY,
109 L2FWD_CRYPTO_HASH_ONLY,
116 rte_iova_t phys_addr;
124 /** l2fwd crypto application command line options */
125 struct l2fwd_crypto_options {
127 unsigned nb_ports_per_lcore;
128 unsigned refresh_period;
129 unsigned single_lcore:1;
132 unsigned sessionless:1;
134 enum l2fwd_crypto_xform_chain xform_chain;
136 struct rte_crypto_sym_xform cipher_xform;
138 int ckey_random_size;
139 uint8_t cipher_key[MAX_KEY_SIZE];
141 struct l2fwd_iv cipher_iv;
142 unsigned int cipher_iv_param;
143 int cipher_iv_random_size;
145 struct rte_crypto_sym_xform auth_xform;
147 int akey_random_size;
148 uint8_t auth_key[MAX_KEY_SIZE];
150 struct l2fwd_iv auth_iv;
151 unsigned int auth_iv_param;
152 int auth_iv_random_size;
154 struct rte_crypto_sym_xform aead_xform;
155 unsigned int aead_key_param;
156 int aead_key_random_size;
157 uint8_t aead_key[MAX_KEY_SIZE];
159 struct l2fwd_iv aead_iv;
160 unsigned int aead_iv_param;
161 int aead_iv_random_size;
163 struct l2fwd_key aad;
170 char string_type[MAX_STR_LEN];
172 uint64_t cryptodev_mask;
174 unsigned int mac_updating;
177 /** l2fwd crypto lcore params */
178 struct l2fwd_crypto_params {
182 unsigned digest_length;
185 uint16_t cipher_dataunit_len;
187 struct l2fwd_iv cipher_iv;
188 struct l2fwd_iv auth_iv;
189 struct l2fwd_iv aead_iv;
190 struct l2fwd_key aad;
191 struct rte_cryptodev_sym_session *session;
198 enum rte_crypto_cipher_algorithm cipher_algo;
199 enum rte_crypto_auth_algorithm auth_algo;
200 enum rte_crypto_aead_algorithm aead_algo;
203 /** lcore configuration */
204 struct lcore_queue_conf {
205 unsigned nb_rx_ports;
206 uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
208 unsigned nb_crypto_devs;
209 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
211 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
212 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
213 } __rte_cache_aligned;
215 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
217 static struct rte_eth_conf port_conf = {
219 .mq_mode = ETH_MQ_RX_NONE,
220 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
224 .mq_mode = ETH_MQ_TX_NONE,
228 struct rte_mempool *l2fwd_pktmbuf_pool;
229 struct rte_mempool *l2fwd_crypto_op_pool;
231 struct rte_mempool *sess_mp;
232 struct rte_mempool *priv_mp;
233 } session_pool_socket[RTE_MAX_NUMA_NODES];
235 /* Per-port statistics struct */
236 struct l2fwd_port_statistics {
240 uint64_t crypto_enqueued;
241 uint64_t crypto_dequeued;
244 } __rte_cache_aligned;
246 struct l2fwd_crypto_statistics {
251 } __rte_cache_aligned;
253 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
254 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
256 /* A tsc-based timer responsible for triggering statistics printout */
257 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
258 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
260 /* default period is 10 seconds */
261 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
263 /* Print out statistics on packets dropped */
267 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
268 uint64_t total_packets_enqueued, total_packets_dequeued,
269 total_packets_errors;
273 total_packets_dropped = 0;
274 total_packets_tx = 0;
275 total_packets_rx = 0;
276 total_packets_enqueued = 0;
277 total_packets_dequeued = 0;
278 total_packets_errors = 0;
280 const char clr[] = { 27, '[', '2', 'J', '\0' };
281 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
283 /* Clear screen and move to top left */
284 printf("%s%s", clr, topLeft);
286 printf("\nPort statistics ====================================");
288 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
289 /* skip disabled ports */
290 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
292 printf("\nStatistics for port %u ------------------------------"
293 "\nPackets sent: %32"PRIu64
294 "\nPackets received: %28"PRIu64
295 "\nPackets dropped: %29"PRIu64,
297 port_statistics[portid].tx,
298 port_statistics[portid].rx,
299 port_statistics[portid].dropped);
301 total_packets_dropped += port_statistics[portid].dropped;
302 total_packets_tx += port_statistics[portid].tx;
303 total_packets_rx += port_statistics[portid].rx;
305 printf("\nCrypto statistics ==================================");
307 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
308 /* skip disabled ports */
309 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
311 printf("\nStatistics for cryptodev %"PRIu64
312 " -------------------------"
313 "\nPackets enqueued: %28"PRIu64
314 "\nPackets dequeued: %28"PRIu64
315 "\nPackets errors: %30"PRIu64,
317 crypto_statistics[cdevid].enqueued,
318 crypto_statistics[cdevid].dequeued,
319 crypto_statistics[cdevid].errors);
321 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
322 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
323 total_packets_errors += crypto_statistics[cdevid].errors;
325 printf("\nAggregate statistics ==============================="
326 "\nTotal packets received: %22"PRIu64
327 "\nTotal packets enqueued: %22"PRIu64
328 "\nTotal packets dequeued: %22"PRIu64
329 "\nTotal packets sent: %26"PRIu64
330 "\nTotal packets dropped: %23"PRIu64
331 "\nTotal packets crypto errors: %17"PRIu64,
333 total_packets_enqueued,
334 total_packets_dequeued,
336 total_packets_dropped,
337 total_packets_errors);
338 printf("\n====================================================\n");
344 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
345 struct l2fwd_crypto_params *cparams)
347 struct rte_crypto_op **op_buffer;
350 op_buffer = (struct rte_crypto_op **)
351 qconf->op_buf[cparams->dev_id].buffer;
353 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
354 cparams->qp_id, op_buffer, (uint16_t) n);
356 crypto_statistics[cparams->dev_id].enqueued += ret;
357 if (unlikely(ret < n)) {
358 crypto_statistics[cparams->dev_id].errors += (n - ret);
360 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
361 rte_crypto_op_free(op_buffer[ret]);
369 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
370 struct l2fwd_crypto_params *cparams)
372 unsigned lcore_id, len;
373 struct lcore_queue_conf *qconf;
375 lcore_id = rte_lcore_id();
377 qconf = &lcore_queue_conf[lcore_id];
378 len = qconf->op_buf[cparams->dev_id].len;
379 qconf->op_buf[cparams->dev_id].buffer[len] = op;
382 /* enough ops to be sent */
383 if (len == MAX_PKT_BURST) {
384 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
388 qconf->op_buf[cparams->dev_id].len = len;
393 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
394 struct rte_crypto_op *op,
395 struct l2fwd_crypto_params *cparams)
397 struct rte_ether_hdr *eth_hdr;
398 struct rte_ipv4_hdr *ip_hdr;
400 uint32_t ipdata_offset, data_len;
401 uint32_t pad_len = 0;
404 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
406 if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
409 ipdata_offset = sizeof(struct rte_ether_hdr);
411 ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
414 ipdata_offset += (ip_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK)
415 * RTE_IPV4_IHL_MULTIPLIER;
418 /* Zero pad data to be crypto'd so it is block aligned */
419 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
421 if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify)
422 data_len -= cparams->digest_length;
424 if (cparams->do_cipher) {
426 * Following algorithms are block cipher algorithms,
427 * and might need padding
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);
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);
450 padding = rte_pktmbuf_append(m, pad_len);
451 if (unlikely(!padding))
455 memset(padding, 0, pad_len);
459 /* Set crypto operation data parameters */
460 rte_crypto_op_attach_sym_session(op, cparams->session);
462 if (cparams->do_hash) {
463 if (cparams->auth_iv.length) {
464 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
467 cparams->cipher_iv.length);
469 * Copy IV at the end of the crypto operation,
470 * after the cipher IV, if added
472 rte_memcpy(iv_ptr, cparams->auth_iv.data,
473 cparams->auth_iv.length);
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);
480 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
481 uint8_t *) + ipdata_offset + data_len;
484 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
485 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
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;
494 op->sym->auth.data.offset = ipdata_offset;
495 op->sym->auth.data.length = data_len;
499 if (cparams->do_cipher) {
500 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
502 /* Copy IV at the end of the crypto operation */
503 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
504 cparams->cipher_iv.length);
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;
513 op->sym->cipher.data.offset = ipdata_offset;
514 op->sym->cipher.data.length = data_len;
518 if (cparams->do_aead) {
519 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
521 /* Copy IV at the end of the crypto operation */
523 * If doing AES-CCM, nonce is copied one byte
524 * after the start of IV field
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);
530 rte_memcpy(iv_ptr, cparams->aead_iv.data,
531 cparams->aead_iv.length);
533 op->sym->aead.data.offset = ipdata_offset;
534 op->sym->aead.data.length = data_len;
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);
541 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
542 uint8_t *) + ipdata_offset + data_len;
545 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
546 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
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;
556 return l2fwd_crypto_enqueue(op, cparams);
560 /* Send the burst of packets on an output interface */
562 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
565 struct rte_mbuf **pkt_buffer;
568 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
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);
575 rte_pktmbuf_free(pkt_buffer[ret]);
582 /* Enqueue packets for TX and prepare them to be sent */
584 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
586 unsigned lcore_id, len;
587 struct lcore_queue_conf *qconf;
589 lcore_id = rte_lcore_id();
591 qconf = &lcore_queue_conf[lcore_id];
592 len = qconf->pkt_buf[port].len;
593 qconf->pkt_buf[port].buffer[len] = m;
596 /* enough pkts to be sent */
597 if (unlikely(len == MAX_PKT_BURST)) {
598 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
602 qconf->pkt_buf[port].len = len;
607 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
609 struct rte_ether_hdr *eth;
612 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
614 /* 02:00:00:00:00:xx */
615 tmp = ð->d_addr.addr_bytes[0];
616 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
619 rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
623 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
624 struct l2fwd_crypto_options *options)
628 struct rte_ipv4_hdr *ip_hdr;
629 uint32_t ipdata_offset = sizeof(struct rte_ether_hdr);
631 ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
633 dst_port = l2fwd_dst_ports[portid];
635 if (options->mac_updating)
636 l2fwd_mac_updating(m, dst_port);
638 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
639 rte_pktmbuf_trim(m, options->auth_xform.auth.digest_length);
641 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
642 pad_len = m->pkt_len - rte_be_to_cpu_16(ip_hdr->total_length) -
644 rte_pktmbuf_trim(m, pad_len);
647 l2fwd_send_packet(m, dst_port);
650 /** Generate random key */
652 generate_random_key(uint8_t *key, unsigned length)
657 fd = open("/dev/urandom", O_RDONLY);
659 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
661 ret = read(fd, key, length);
664 if (ret != (signed)length)
665 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
668 static struct rte_cryptodev_sym_session *
669 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
671 struct rte_crypto_sym_xform *first_xform;
672 struct rte_cryptodev_sym_session *session;
673 int retval = rte_cryptodev_socket_id(cdev_id);
678 uint8_t socket_id = (uint8_t) retval;
680 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
681 first_xform = &options->aead_xform;
682 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
683 first_xform = &options->cipher_xform;
684 first_xform->next = &options->auth_xform;
685 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
686 first_xform = &options->auth_xform;
687 first_xform->next = &options->cipher_xform;
688 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
689 first_xform = &options->cipher_xform;
691 first_xform = &options->auth_xform;
694 session = rte_cryptodev_sym_session_create(
695 session_pool_socket[socket_id].sess_mp);
699 if (rte_cryptodev_sym_session_init(cdev_id, session,
701 session_pool_socket[socket_id].priv_mp) < 0)
708 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
710 /* main processing loop */
712 l2fwd_main_loop(struct l2fwd_crypto_options *options)
714 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
715 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
717 unsigned lcore_id = rte_lcore_id();
718 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
719 unsigned int i, j, nb_rx, len;
721 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
722 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
723 US_PER_S * BURST_TX_DRAIN_US;
724 struct l2fwd_crypto_params *cparams;
725 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
726 struct rte_cryptodev_sym_session *session;
728 if (qconf->nb_rx_ports == 0) {
729 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
733 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
735 for (i = 0; i < qconf->nb_rx_ports; i++) {
737 portid = qconf->rx_port_list[i];
738 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
742 for (i = 0; i < qconf->nb_crypto_devs; i++) {
743 port_cparams[i].do_cipher = 0;
744 port_cparams[i].do_hash = 0;
745 port_cparams[i].do_aead = 0;
747 switch (options->xform_chain) {
748 case L2FWD_CRYPTO_AEAD:
749 port_cparams[i].do_aead = 1;
751 case L2FWD_CRYPTO_CIPHER_HASH:
752 case L2FWD_CRYPTO_HASH_CIPHER:
753 port_cparams[i].do_cipher = 1;
754 port_cparams[i].do_hash = 1;
756 case L2FWD_CRYPTO_HASH_ONLY:
757 port_cparams[i].do_hash = 1;
759 case L2FWD_CRYPTO_CIPHER_ONLY:
760 port_cparams[i].do_cipher = 1;
764 port_cparams[i].dev_id = qconf->cryptodev_list[i];
765 port_cparams[i].qp_id = 0;
767 port_cparams[i].block_size = options->block_size;
769 if (port_cparams[i].do_hash) {
770 port_cparams[i].auth_iv.data = options->auth_iv.data;
771 port_cparams[i].auth_iv.length = options->auth_iv.length;
772 if (!options->auth_iv_param)
773 generate_random_key(port_cparams[i].auth_iv.data,
774 port_cparams[i].auth_iv.length);
775 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
776 port_cparams[i].hash_verify = 1;
778 port_cparams[i].hash_verify = 0;
780 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
781 port_cparams[i].digest_length =
782 options->auth_xform.auth.digest_length;
783 /* Set IV parameters */
784 if (options->auth_iv.length) {
785 options->auth_xform.auth.iv.offset =
786 IV_OFFSET + options->cipher_iv.length;
787 options->auth_xform.auth.iv.length =
788 options->auth_iv.length;
792 if (port_cparams[i].do_aead) {
793 port_cparams[i].aead_iv.data = options->aead_iv.data;
794 port_cparams[i].aead_iv.length = options->aead_iv.length;
795 if (!options->aead_iv_param)
796 generate_random_key(port_cparams[i].aead_iv.data,
797 port_cparams[i].aead_iv.length);
798 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
799 port_cparams[i].digest_length =
800 options->aead_xform.aead.digest_length;
801 if (options->aead_xform.aead.aad_length) {
802 port_cparams[i].aad.data = options->aad.data;
803 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
804 port_cparams[i].aad.length = options->aad.length;
805 if (!options->aad_param)
806 generate_random_key(port_cparams[i].aad.data,
807 port_cparams[i].aad.length);
809 * If doing AES-CCM, first 18 bytes has to be reserved,
810 * and actual AAD should start from byte 18
812 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
813 memmove(port_cparams[i].aad.data + 18,
814 port_cparams[i].aad.data,
815 port_cparams[i].aad.length);
818 port_cparams[i].aad.length = 0;
820 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
821 port_cparams[i].hash_verify = 1;
823 port_cparams[i].hash_verify = 0;
825 /* Set IV parameters */
826 options->aead_xform.aead.iv.offset = IV_OFFSET;
827 options->aead_xform.aead.iv.length = options->aead_iv.length;
830 if (port_cparams[i].do_cipher) {
831 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
832 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
833 if (!options->cipher_iv_param)
834 generate_random_key(port_cparams[i].cipher_iv.data,
835 port_cparams[i].cipher_iv.length);
837 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
838 port_cparams[i].cipher_dataunit_len =
839 options->cipher_xform.cipher.dataunit_len;
840 /* Set IV parameters */
841 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
842 options->cipher_xform.cipher.iv.length =
843 options->cipher_iv.length;
846 session = initialize_crypto_session(options,
847 port_cparams[i].dev_id);
849 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
851 port_cparams[i].session = session;
853 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
854 port_cparams[i].dev_id);
857 l2fwd_crypto_options_print(options);
860 * Initialize previous tsc timestamp before the loop,
861 * to avoid showing the port statistics immediately,
862 * so user can see the crypto information.
864 prev_tsc = rte_rdtsc();
867 cur_tsc = rte_rdtsc();
870 * Crypto device/TX burst queue drain
872 diff_tsc = cur_tsc - prev_tsc;
873 if (unlikely(diff_tsc > drain_tsc)) {
874 /* Enqueue all crypto ops remaining in buffers */
875 for (i = 0; i < qconf->nb_crypto_devs; i++) {
876 cparams = &port_cparams[i];
877 len = qconf->op_buf[cparams->dev_id].len;
878 l2fwd_crypto_send_burst(qconf, len, cparams);
879 qconf->op_buf[cparams->dev_id].len = 0;
881 /* Transmit all packets remaining in buffers */
882 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
883 if (qconf->pkt_buf[portid].len == 0)
885 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
886 qconf->pkt_buf[portid].len,
888 qconf->pkt_buf[portid].len = 0;
891 /* if timer is enabled */
892 if (timer_period > 0) {
894 /* advance the timer */
895 timer_tsc += diff_tsc;
897 /* if timer has reached its timeout */
898 if (unlikely(timer_tsc >=
899 (uint64_t)timer_period)) {
901 /* do this only on main core */
902 if (lcore_id == rte_get_main_lcore()
903 && options->refresh_period) {
914 * Read packet from RX queues
916 for (i = 0; i < qconf->nb_rx_ports; i++) {
917 portid = qconf->rx_port_list[i];
919 cparams = &port_cparams[i];
921 nb_rx = rte_eth_rx_burst(portid, 0,
922 pkts_burst, MAX_PKT_BURST);
924 port_statistics[portid].rx += nb_rx;
928 * If we can't allocate a crypto_ops, then drop
929 * the rest of the burst and dequeue and
930 * process the packets to free offload structs
932 if (rte_crypto_op_bulk_alloc(
933 l2fwd_crypto_op_pool,
934 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
937 for (j = 0; j < nb_rx; j++)
938 rte_pktmbuf_free(pkts_burst[j]);
943 /* Enqueue packets from Crypto device*/
944 for (j = 0; j < nb_rx; j++) {
947 l2fwd_simple_crypto_enqueue(m,
948 ops_burst[j], cparams);
952 /* Dequeue packets from Crypto device */
954 nb_rx = rte_cryptodev_dequeue_burst(
955 cparams->dev_id, cparams->qp_id,
956 ops_burst, MAX_PKT_BURST);
958 crypto_statistics[cparams->dev_id].dequeued +=
961 /* Forward crypto'd packets */
962 for (j = 0; j < nb_rx; j++) {
963 m = ops_burst[j]->sym->m_src;
965 rte_crypto_op_free(ops_burst[j]);
966 l2fwd_simple_forward(m, portid,
969 } while (nb_rx == MAX_PKT_BURST);
975 l2fwd_launch_one_lcore(void *arg)
977 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
981 /* Display command line arguments usage */
983 l2fwd_crypto_usage(const char *prgname)
985 printf("%s [EAL options] --\n"
986 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
987 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
988 " -s manage all ports from single lcore\n"
989 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
990 " (0 to disable, 10 default, 86400 maximum)\n"
992 " --cdev_type HW / SW / ANY\n"
993 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
994 " HASH_ONLY / AEAD\n"
996 " --cipher_algo ALGO\n"
997 " --cipher_op ENCRYPT / DECRYPT\n"
998 " --cipher_key KEY (bytes separated with \":\")\n"
999 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
1000 " --cipher_iv IV (bytes separated with \":\")\n"
1001 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1002 " --cipher_dataunit_len SIZE: length of the algorithm data-unit\n"
1004 " --auth_algo ALGO\n"
1005 " --auth_op GENERATE / VERIFY\n"
1006 " --auth_key KEY (bytes separated with \":\")\n"
1007 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1008 " --auth_iv IV (bytes separated with \":\")\n"
1009 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
1011 " --aead_algo ALGO\n"
1012 " --aead_op ENCRYPT / DECRYPT\n"
1013 " --aead_key KEY (bytes separated with \":\")\n"
1014 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
1015 " --aead_iv IV (bytes separated with \":\")\n"
1016 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1017 " --aad AAD (bytes separated with \":\")\n"
1018 " --aad_random_size SIZE: size of AAD when generated randomly\n"
1020 " --digest_size SIZE: size of digest to be generated/verified\n"
1023 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1025 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1027 " - The source MAC address is replaced by the TX port MAC address\n"
1028 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1032 /** Parse crypto device type command line argument */
1034 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1036 if (strcmp("HW", optarg) == 0) {
1037 *type = CDEV_TYPE_HW;
1039 } else if (strcmp("SW", optarg) == 0) {
1040 *type = CDEV_TYPE_SW;
1042 } else if (strcmp("ANY", optarg) == 0) {
1043 *type = CDEV_TYPE_ANY;
1050 /** Parse crypto chain xform command line argument */
1052 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1054 if (strcmp("CIPHER_HASH", optarg) == 0) {
1055 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1057 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1058 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1060 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1061 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1063 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1064 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1066 } else if (strcmp("AEAD", optarg) == 0) {
1067 options->xform_chain = L2FWD_CRYPTO_AEAD;
1074 /** Parse crypto cipher algo option command line argument */
1076 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1079 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1080 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1081 "not supported!\n");
1088 /** Parse crypto cipher operation command line argument */
1090 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1092 if (strcmp("ENCRYPT", optarg) == 0) {
1093 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1095 } else if (strcmp("DECRYPT", optarg) == 0) {
1096 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1100 printf("Cipher operation not supported!\n");
1104 /** Parse bytes from command line argument */
1106 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1108 unsigned byte_count;
1112 for (byte_count = 0, token = strtok(input_arg, ":");
1113 (byte_count < max_size) && (token != NULL);
1114 token = strtok(NULL, ":")) {
1116 int number = (int)strtol(token, NULL, 16);
1118 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1121 data[byte_count++] = (uint8_t)number;
1127 /** Parse size param*/
1129 parse_size(int *size, const char *q_arg)
1134 /* parse hexadecimal string */
1135 n = strtoul(q_arg, &end, 10);
1136 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1140 printf("invalid size\n");
1148 /** Parse crypto cipher operation command line argument */
1150 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1152 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1153 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1154 "not supported!\n");
1162 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1164 if (strcmp("VERIFY", optarg) == 0) {
1165 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1167 } else if (strcmp("GENERATE", optarg) == 0) {
1168 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1172 printf("Authentication operation specified not supported!\n");
1177 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1179 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1180 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1181 "not supported!\n");
1189 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1191 if (strcmp("ENCRYPT", optarg) == 0) {
1192 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1194 } else if (strcmp("DECRYPT", optarg) == 0) {
1195 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1199 printf("AEAD operation specified not supported!\n");
1203 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1209 /* parse hexadecimal string */
1210 pm = strtoul(q_arg, &end, 16);
1211 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1214 options->cryptodev_mask = pm;
1215 if (options->cryptodev_mask == 0) {
1216 printf("invalid cryptodev_mask specified\n");
1223 /** Parse long options */
1225 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1226 struct option *lgopts, int option_index)
1231 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1232 retval = parse_cryptodev_type(&options->type, optarg);
1234 strlcpy(options->string_type, optarg, MAX_STR_LEN);
1238 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1239 return parse_crypto_opt_chain(options, optarg);
1241 /* Cipher options */
1242 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1243 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1246 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1247 return parse_cipher_op(&options->cipher_xform.cipher.op,
1250 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1251 options->ckey_param = 1;
1252 options->cipher_xform.cipher.key.length =
1253 parse_bytes(options->cipher_key, optarg, MAX_KEY_SIZE);
1254 if (options->cipher_xform.cipher.key.length > 0)
1260 else if (strcmp(lgopts[option_index].name, "cipher_dataunit_len") == 0) {
1261 retval = parse_size(&val, optarg);
1262 if (retval == 0 && val >= 0 && val <= UINT16_MAX) {
1263 options->cipher_xform.cipher.dataunit_len =
1270 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1271 return parse_size(&options->ckey_random_size, optarg);
1273 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1274 options->cipher_iv_param = 1;
1275 options->cipher_iv.length =
1276 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1277 if (options->cipher_iv.length > 0)
1283 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1284 return parse_size(&options->cipher_iv_random_size, optarg);
1286 /* Authentication options */
1287 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1288 return parse_auth_algo(&options->auth_xform.auth.algo,
1292 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1293 return parse_auth_op(&options->auth_xform.auth.op,
1296 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1297 options->akey_param = 1;
1298 options->auth_xform.auth.key.length =
1299 parse_bytes(options->auth_key, optarg, MAX_KEY_SIZE);
1300 if (options->auth_xform.auth.key.length > 0)
1306 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1307 return parse_size(&options->akey_random_size, optarg);
1310 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1311 options->auth_iv_param = 1;
1312 options->auth_iv.length =
1313 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1314 if (options->auth_iv.length > 0)
1320 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1321 return parse_size(&options->auth_iv_random_size, optarg);
1324 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1325 return parse_aead_algo(&options->aead_xform.aead.algo,
1329 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1330 return parse_aead_op(&options->aead_xform.aead.op,
1333 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1334 options->aead_key_param = 1;
1335 options->aead_xform.aead.key.length =
1336 parse_bytes(options->aead_key, optarg, MAX_KEY_SIZE);
1337 if (options->aead_xform.aead.key.length > 0)
1343 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1344 return parse_size(&options->aead_key_random_size, optarg);
1347 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1348 options->aead_iv_param = 1;
1349 options->aead_iv.length =
1350 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1351 if (options->aead_iv.length > 0)
1357 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1358 return parse_size(&options->aead_iv_random_size, optarg);
1360 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1361 options->aad_param = 1;
1362 options->aad.length =
1363 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1364 if (options->aad.length > 0)
1370 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1371 return parse_size(&options->aad_random_size, optarg);
1374 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1375 return parse_size(&options->digest_size, optarg);
1378 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1379 options->sessionless = 1;
1383 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1384 return parse_cryptodev_mask(options, optarg);
1386 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1387 options->mac_updating = 1;
1391 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1392 options->mac_updating = 0;
1399 /** Parse port mask */
1401 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1407 /* parse hexadecimal string */
1408 pm = strtoul(q_arg, &end, 16);
1409 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1412 options->portmask = pm;
1413 if (options->portmask == 0) {
1414 printf("invalid portmask specified\n");
1421 /** Parse number of queues */
1423 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1429 /* parse hexadecimal string */
1430 n = strtoul(q_arg, &end, 10);
1431 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1433 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1436 options->nb_ports_per_lcore = n;
1437 if (options->nb_ports_per_lcore == 0) {
1438 printf("invalid number of ports selected\n");
1445 /** Parse timer period */
1447 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1453 /* parse number string */
1454 n = (unsigned)strtol(q_arg, &end, 10);
1455 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1458 if (n >= MAX_TIMER_PERIOD) {
1459 printf("Warning refresh period specified %lu is greater than "
1460 "max value %lu! using max value",
1461 n, MAX_TIMER_PERIOD);
1462 n = MAX_TIMER_PERIOD;
1465 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1470 /** Generate default options for application */
1472 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1474 options->portmask = 0xffffffff;
1475 options->nb_ports_per_lcore = 1;
1476 options->refresh_period = 10000;
1477 options->single_lcore = 0;
1478 options->sessionless = 0;
1480 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1483 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1484 options->cipher_xform.next = NULL;
1485 options->ckey_param = 0;
1486 options->ckey_random_size = -1;
1487 options->cipher_xform.cipher.key.length = 0;
1488 options->cipher_iv_param = 0;
1489 options->cipher_iv_random_size = -1;
1490 options->cipher_iv.length = 0;
1492 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1493 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1494 options->cipher_xform.cipher.dataunit_len = 0;
1496 /* Authentication Data */
1497 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1498 options->auth_xform.next = NULL;
1499 options->akey_param = 0;
1500 options->akey_random_size = -1;
1501 options->auth_xform.auth.key.length = 0;
1502 options->auth_iv_param = 0;
1503 options->auth_iv_random_size = -1;
1504 options->auth_iv.length = 0;
1506 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1507 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1510 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1511 options->aead_xform.next = NULL;
1512 options->aead_key_param = 0;
1513 options->aead_key_random_size = -1;
1514 options->aead_xform.aead.key.length = 0;
1515 options->aead_iv_param = 0;
1516 options->aead_iv_random_size = -1;
1517 options->aead_iv.length = 0;
1519 options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1520 options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1522 options->aad_param = 0;
1523 options->aad_random_size = -1;
1524 options->aad.length = 0;
1526 options->digest_size = -1;
1528 options->type = CDEV_TYPE_ANY;
1529 options->cryptodev_mask = UINT64_MAX;
1531 options->mac_updating = 1;
1535 display_cipher_info(struct l2fwd_crypto_options *options)
1537 printf("\n---- Cipher information ---\n");
1538 printf("Algorithm: %s\n",
1539 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1540 rte_hexdump(stdout, "Cipher key:",
1541 options->cipher_xform.cipher.key.data,
1542 options->cipher_xform.cipher.key.length);
1543 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1547 display_auth_info(struct l2fwd_crypto_options *options)
1549 printf("\n---- Authentication information ---\n");
1550 printf("Algorithm: %s\n",
1551 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1552 rte_hexdump(stdout, "Auth key:",
1553 options->auth_xform.auth.key.data,
1554 options->auth_xform.auth.key.length);
1555 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1559 display_aead_info(struct l2fwd_crypto_options *options)
1561 printf("\n---- AEAD information ---\n");
1562 printf("Algorithm: %s\n",
1563 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1564 rte_hexdump(stdout, "AEAD key:",
1565 options->aead_xform.aead.key.data,
1566 options->aead_xform.aead.key.length);
1567 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1568 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1572 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1574 char string_cipher_op[MAX_STR_LEN];
1575 char string_auth_op[MAX_STR_LEN];
1576 char string_aead_op[MAX_STR_LEN];
1578 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1579 strcpy(string_cipher_op, "Encrypt");
1581 strcpy(string_cipher_op, "Decrypt");
1583 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1584 strcpy(string_auth_op, "Auth generate");
1586 strcpy(string_auth_op, "Auth verify");
1588 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1589 strcpy(string_aead_op, "Authenticated encryption");
1591 strcpy(string_aead_op, "Authenticated decryption");
1594 printf("Options:-\nn");
1595 printf("portmask: %x\n", options->portmask);
1596 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1597 printf("refresh period : %u\n", options->refresh_period);
1598 printf("single lcore mode: %s\n",
1599 options->single_lcore ? "enabled" : "disabled");
1600 printf("stats_printing: %s\n",
1601 options->refresh_period == 0 ? "disabled" : "enabled");
1603 printf("sessionless crypto: %s\n",
1604 options->sessionless ? "enabled" : "disabled");
1606 if (options->ckey_param && (options->ckey_random_size != -1))
1607 printf("Cipher key already parsed, ignoring size of random key\n");
1609 if (options->akey_param && (options->akey_random_size != -1))
1610 printf("Auth key already parsed, ignoring size of random key\n");
1612 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1613 printf("Cipher IV already parsed, ignoring size of random IV\n");
1615 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1616 printf("Auth IV already parsed, ignoring size of random IV\n");
1618 if (options->aad_param && (options->aad_random_size != -1))
1619 printf("AAD already parsed, ignoring size of random AAD\n");
1621 printf("\nCrypto chain: ");
1622 switch (options->xform_chain) {
1623 case L2FWD_CRYPTO_AEAD:
1624 printf("Input --> %s --> Output\n", string_aead_op);
1625 display_aead_info(options);
1627 case L2FWD_CRYPTO_CIPHER_HASH:
1628 printf("Input --> %s --> %s --> Output\n",
1629 string_cipher_op, string_auth_op);
1630 display_cipher_info(options);
1631 display_auth_info(options);
1633 case L2FWD_CRYPTO_HASH_CIPHER:
1634 printf("Input --> %s --> %s --> Output\n",
1635 string_auth_op, string_cipher_op);
1636 display_cipher_info(options);
1637 display_auth_info(options);
1639 case L2FWD_CRYPTO_HASH_ONLY:
1640 printf("Input --> %s --> Output\n", string_auth_op);
1641 display_auth_info(options);
1643 case L2FWD_CRYPTO_CIPHER_ONLY:
1644 printf("Input --> %s --> Output\n", string_cipher_op);
1645 display_cipher_info(options);
1650 /* Parse the argument given in the command line of the application */
1652 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1653 int argc, char **argv)
1655 int opt, retval, option_index;
1656 char **argvopt = argv, *prgname = argv[0];
1658 static struct option lgopts[] = {
1659 { "sessionless", no_argument, 0, 0 },
1661 { "cdev_type", required_argument, 0, 0 },
1662 { "chain", required_argument, 0, 0 },
1664 { "cipher_algo", required_argument, 0, 0 },
1665 { "cipher_op", required_argument, 0, 0 },
1666 { "cipher_key", required_argument, 0, 0 },
1667 { "cipher_key_random_size", required_argument, 0, 0 },
1668 { "cipher_iv", required_argument, 0, 0 },
1669 { "cipher_iv_random_size", required_argument, 0, 0 },
1670 { "cipher_dataunit_len", required_argument, 0, 0},
1672 { "auth_algo", required_argument, 0, 0 },
1673 { "auth_op", required_argument, 0, 0 },
1674 { "auth_key", required_argument, 0, 0 },
1675 { "auth_key_random_size", required_argument, 0, 0 },
1676 { "auth_iv", required_argument, 0, 0 },
1677 { "auth_iv_random_size", required_argument, 0, 0 },
1679 { "aead_algo", required_argument, 0, 0 },
1680 { "aead_op", required_argument, 0, 0 },
1681 { "aead_key", required_argument, 0, 0 },
1682 { "aead_key_random_size", required_argument, 0, 0 },
1683 { "aead_iv", required_argument, 0, 0 },
1684 { "aead_iv_random_size", required_argument, 0, 0 },
1686 { "aad", required_argument, 0, 0 },
1687 { "aad_random_size", required_argument, 0, 0 },
1689 { "digest_size", required_argument, 0, 0 },
1691 { "sessionless", no_argument, 0, 0 },
1692 { "cryptodev_mask", required_argument, 0, 0},
1694 { "mac-updating", no_argument, 0, 0},
1695 { "no-mac-updating", no_argument, 0, 0},
1700 l2fwd_crypto_default_options(options);
1702 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1703 &option_index)) != EOF) {
1707 retval = l2fwd_crypto_parse_args_long_options(options,
1708 lgopts, option_index);
1710 l2fwd_crypto_usage(prgname);
1717 retval = l2fwd_crypto_parse_portmask(options, optarg);
1719 l2fwd_crypto_usage(prgname);
1726 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1728 l2fwd_crypto_usage(prgname);
1735 options->single_lcore = 1;
1741 retval = l2fwd_crypto_parse_timer_period(options,
1744 l2fwd_crypto_usage(prgname);
1750 l2fwd_crypto_usage(prgname);
1757 argv[optind-1] = prgname;
1760 optind = 1; /* reset getopt lib */
1765 /* Check the link status of all ports in up to 9s, and print them finally */
1767 check_all_ports_link_status(uint32_t port_mask)
1769 #define CHECK_INTERVAL 100 /* 100ms */
1770 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1772 uint8_t count, all_ports_up, print_flag = 0;
1773 struct rte_eth_link link;
1775 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1777 printf("\nChecking link status");
1779 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1781 RTE_ETH_FOREACH_DEV(portid) {
1782 if ((port_mask & (1 << portid)) == 0)
1784 memset(&link, 0, sizeof(link));
1785 ret = rte_eth_link_get_nowait(portid, &link);
1788 if (print_flag == 1)
1789 printf("Port %u link get failed: %s\n",
1790 portid, rte_strerror(-ret));
1793 /* print link status if flag set */
1794 if (print_flag == 1) {
1795 rte_eth_link_to_str(link_status_text,
1796 sizeof(link_status_text), &link);
1797 printf("Port %d %s\n", portid,
1801 /* clear all_ports_up flag if any link down */
1802 if (link.link_status == ETH_LINK_DOWN) {
1807 /* after finally printing all link status, get out */
1808 if (print_flag == 1)
1811 if (all_ports_up == 0) {
1814 rte_delay_ms(CHECK_INTERVAL);
1817 /* set the print_flag if all ports up or timeout */
1818 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1825 /* Check if device has to be HW/SW or any */
1827 check_type(const struct l2fwd_crypto_options *options,
1828 const struct rte_cryptodev_info *dev_info)
1830 if (options->type == CDEV_TYPE_HW &&
1831 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1833 if (options->type == CDEV_TYPE_SW &&
1834 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1836 if (options->type == CDEV_TYPE_ANY)
1842 static const struct rte_cryptodev_capabilities *
1843 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1844 const struct rte_cryptodev_info *dev_info,
1848 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1849 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1850 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1851 options->cipher_xform.cipher.algo;
1853 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1854 cap_cipher_algo = cap->sym.cipher.algo;
1855 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1856 if (cap_cipher_algo == opt_cipher_algo) {
1857 if (check_type(options, dev_info) == 0)
1861 cap = &dev_info->capabilities[++i];
1864 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1865 printf("Algorithm %s not supported by cryptodev %u"
1866 " or device not of preferred type (%s)\n",
1867 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1869 options->string_type);
1876 static const struct rte_cryptodev_capabilities *
1877 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1878 const struct rte_cryptodev_info *dev_info,
1882 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1883 enum rte_crypto_auth_algorithm cap_auth_algo;
1884 enum rte_crypto_auth_algorithm opt_auth_algo =
1885 options->auth_xform.auth.algo;
1887 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1888 cap_auth_algo = cap->sym.auth.algo;
1889 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1890 if (cap_auth_algo == opt_auth_algo) {
1891 if (check_type(options, dev_info) == 0)
1895 cap = &dev_info->capabilities[++i];
1898 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1899 printf("Algorithm %s not supported by cryptodev %u"
1900 " or device not of preferred type (%s)\n",
1901 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1903 options->string_type);
1910 static const struct rte_cryptodev_capabilities *
1911 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1912 const struct rte_cryptodev_info *dev_info,
1916 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1917 enum rte_crypto_aead_algorithm cap_aead_algo;
1918 enum rte_crypto_aead_algorithm opt_aead_algo =
1919 options->aead_xform.aead.algo;
1921 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1922 cap_aead_algo = cap->sym.aead.algo;
1923 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1924 if (cap_aead_algo == opt_aead_algo) {
1925 if (check_type(options, dev_info) == 0)
1929 cap = &dev_info->capabilities[++i];
1932 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1933 printf("Algorithm %s not supported by cryptodev %u"
1934 " or device not of preferred type (%s)\n",
1935 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1937 options->string_type);
1944 /* Check if the device is enabled by cryptodev_mask */
1946 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1949 if (options->cryptodev_mask & (1 << cdev_id))
1956 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1962 if (increment == 0) {
1969 /* Range of values */
1970 for (supp_size = min; supp_size <= max; supp_size += increment) {
1971 if (length == supp_size)
1979 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1980 unsigned int iv_param, int iv_random_size,
1984 * Check if length of provided IV is supported
1985 * by the algorithm chosen.
1988 if (check_supported_size(iv_length,
1991 iv_range_size->increment)
1995 * Check if length of IV to be randomly generated
1996 * is supported by the algorithm chosen.
1998 } else if (iv_random_size != -1) {
1999 if (check_supported_size(iv_random_size,
2002 iv_range_size->increment)
2011 check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id)
2013 struct rte_cryptodev_info dev_info;
2014 const struct rte_cryptodev_capabilities *cap;
2016 rte_cryptodev_info_get(cdev_id, &dev_info);
2018 /* Set AEAD parameters */
2019 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2020 /* Check if device supports AEAD algo */
2021 cap = check_device_support_aead_algo(options, &dev_info,
2026 if (check_iv_param(&cap->sym.aead.iv_size,
2027 options->aead_iv_param,
2028 options->aead_iv_random_size,
2029 options->aead_iv.length) != 0) {
2030 RTE_LOG(DEBUG, USER1,
2031 "Device %u does not support IV length\n",
2037 * Check if length of provided AEAD key is supported
2038 * by the algorithm chosen.
2040 if (options->aead_key_param) {
2041 if (check_supported_size(
2042 options->aead_xform.aead.key.length,
2043 cap->sym.aead.key_size.min,
2044 cap->sym.aead.key_size.max,
2045 cap->sym.aead.key_size.increment)
2047 RTE_LOG(DEBUG, USER1,
2048 "Device %u does not support "
2049 "AEAD key length\n",
2054 * Check if length of the aead key to be randomly generated
2055 * is supported by the algorithm chosen.
2057 } else if (options->aead_key_random_size != -1) {
2058 if (check_supported_size(options->aead_key_random_size,
2059 cap->sym.aead.key_size.min,
2060 cap->sym.aead.key_size.max,
2061 cap->sym.aead.key_size.increment)
2063 RTE_LOG(DEBUG, USER1,
2064 "Device %u does not support "
2065 "AEAD key length\n",
2073 * Check if length of provided AAD is supported
2074 * by the algorithm chosen.
2076 if (options->aad_param) {
2077 if (check_supported_size(options->aad.length,
2078 cap->sym.aead.aad_size.min,
2079 cap->sym.aead.aad_size.max,
2080 cap->sym.aead.aad_size.increment)
2082 RTE_LOG(DEBUG, USER1,
2083 "Device %u does not support "
2089 * Check if length of AAD to be randomly generated
2090 * is supported by the algorithm chosen.
2092 } else if (options->aad_random_size != -1) {
2093 if (check_supported_size(options->aad_random_size,
2094 cap->sym.aead.aad_size.min,
2095 cap->sym.aead.aad_size.max,
2096 cap->sym.aead.aad_size.increment)
2098 RTE_LOG(DEBUG, USER1,
2099 "Device %u does not support "
2106 /* Check if digest size is supported by the algorithm. */
2107 if (options->digest_size != -1) {
2108 if (check_supported_size(options->digest_size,
2109 cap->sym.aead.digest_size.min,
2110 cap->sym.aead.digest_size.max,
2111 cap->sym.aead.digest_size.increment)
2113 RTE_LOG(DEBUG, USER1,
2114 "Device %u does not support "
2122 /* Set cipher parameters */
2123 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2124 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2125 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2126 /* Check if device supports cipher algo */
2127 cap = check_device_support_cipher_algo(options, &dev_info,
2132 if (check_iv_param(&cap->sym.cipher.iv_size,
2133 options->cipher_iv_param,
2134 options->cipher_iv_random_size,
2135 options->cipher_iv.length) != 0) {
2136 RTE_LOG(DEBUG, USER1,
2137 "Device %u does not support IV length\n",
2143 * Check if length of provided cipher key is supported
2144 * by the algorithm chosen.
2146 if (options->ckey_param) {
2147 if (check_supported_size(
2148 options->cipher_xform.cipher.key.length,
2149 cap->sym.cipher.key_size.min,
2150 cap->sym.cipher.key_size.max,
2151 cap->sym.cipher.key_size.increment)
2153 if (dev_info.feature_flags &
2154 RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY) {
2155 RTE_LOG(DEBUG, USER1,
2156 "Key length does not match the device "
2157 "%u capability. Key may be wrapped\n",
2160 RTE_LOG(DEBUG, USER1,
2161 "Key length does not match the device "
2169 * Check if length of the cipher key to be randomly generated
2170 * is supported by the algorithm chosen.
2172 } else if (options->ckey_random_size != -1) {
2173 if (check_supported_size(options->ckey_random_size,
2174 cap->sym.cipher.key_size.min,
2175 cap->sym.cipher.key_size.max,
2176 cap->sym.cipher.key_size.increment)
2178 RTE_LOG(DEBUG, USER1,
2179 "Device %u does not support cipher "
2186 if (options->cipher_xform.cipher.dataunit_len > 0) {
2187 if (!(dev_info.feature_flags &
2188 RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS)) {
2189 RTE_LOG(DEBUG, USER1,
2190 "Device %u does not support "
2191 "cipher multiple data units\n",
2195 if (cap->sym.cipher.dataunit_set != 0) {
2198 switch (options->cipher_xform.cipher.dataunit_len) {
2200 if (!(cap->sym.cipher.dataunit_set &
2201 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES))
2205 if (!(cap->sym.cipher.dataunit_set &
2206 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_4096_BYTES))
2213 RTE_LOG(DEBUG, USER1,
2214 "Device %u does not support "
2215 "data-unit length %u\n",
2217 options->cipher_xform.cipher.dataunit_len);
2224 /* Set auth parameters */
2225 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2226 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2227 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2228 /* Check if device supports auth algo */
2229 cap = check_device_support_auth_algo(options, &dev_info,
2234 if (check_iv_param(&cap->sym.auth.iv_size,
2235 options->auth_iv_param,
2236 options->auth_iv_random_size,
2237 options->auth_iv.length) != 0) {
2238 RTE_LOG(DEBUG, USER1,
2239 "Device %u does not support IV length\n",
2244 * Check if length of provided auth key is supported
2245 * by the algorithm chosen.
2247 if (options->akey_param) {
2248 if (check_supported_size(
2249 options->auth_xform.auth.key.length,
2250 cap->sym.auth.key_size.min,
2251 cap->sym.auth.key_size.max,
2252 cap->sym.auth.key_size.increment)
2254 RTE_LOG(DEBUG, USER1,
2255 "Device %u does not support auth "
2261 * Check if length of the auth key to be randomly generated
2262 * is supported by the algorithm chosen.
2264 } else if (options->akey_random_size != -1) {
2265 if (check_supported_size(options->akey_random_size,
2266 cap->sym.auth.key_size.min,
2267 cap->sym.auth.key_size.max,
2268 cap->sym.auth.key_size.increment)
2270 RTE_LOG(DEBUG, USER1,
2271 "Device %u does not support auth "
2278 /* Check if digest size is supported by the algorithm. */
2279 if (options->digest_size != -1) {
2280 if (check_supported_size(options->digest_size,
2281 cap->sym.auth.digest_size.min,
2282 cap->sym.auth.digest_size.max,
2283 cap->sym.auth.digest_size.increment)
2285 RTE_LOG(DEBUG, USER1,
2286 "Device %u does not support "
2298 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2299 uint8_t *enabled_cdevs)
2301 uint8_t cdev_id, cdev_count, enabled_cdev_count = 0;
2302 const struct rte_cryptodev_capabilities *cap;
2303 unsigned int sess_sz, max_sess_sz = 0;
2304 uint32_t sessions_needed = 0;
2307 cdev_count = rte_cryptodev_count();
2308 if (cdev_count == 0) {
2309 printf("No crypto devices available\n");
2313 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2315 if (check_cryptodev_mask(options, cdev_id) < 0)
2318 if (check_capabilities(options, cdev_id) < 0)
2321 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2322 if (sess_sz > max_sess_sz)
2323 max_sess_sz = sess_sz;
2325 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2327 enabled_cdevs[cdev_id] = 1;
2328 enabled_cdev_count++;
2331 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2332 struct rte_cryptodev_qp_conf qp_conf;
2333 struct rte_cryptodev_info dev_info;
2335 if (enabled_cdevs[cdev_id] == 0)
2338 if (check_cryptodev_mask(options, cdev_id) < 0)
2341 if (check_capabilities(options, cdev_id) < 0)
2344 retval = rte_cryptodev_socket_id(cdev_id);
2347 printf("Invalid crypto device id used\n");
2351 uint8_t socket_id = (uint8_t) retval;
2353 struct rte_cryptodev_config conf = {
2354 .nb_queue_pairs = 1,
2355 .socket_id = socket_id,
2356 .ff_disable = RTE_CRYPTODEV_FF_SECURITY,
2359 rte_cryptodev_info_get(cdev_id, &dev_info);
2362 * Two sessions objects are required for each session
2363 * (one for the header, one for the private data)
2365 if (!strcmp(dev_info.driver_name, "crypto_scheduler")) {
2366 #ifdef RTE_CRYPTO_SCHEDULER
2367 uint32_t nb_workers =
2368 rte_cryptodev_scheduler_workers_get(cdev_id,
2371 sessions_needed = enabled_cdev_count * nb_workers;
2374 sessions_needed = enabled_cdev_count;
2376 if (session_pool_socket[socket_id].priv_mp == NULL) {
2377 char mp_name[RTE_MEMPOOL_NAMESIZE];
2379 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2380 "priv_sess_mp_%u", socket_id);
2382 session_pool_socket[socket_id].priv_mp =
2383 rte_mempool_create(mp_name,
2386 0, 0, NULL, NULL, NULL,
2390 if (session_pool_socket[socket_id].priv_mp == NULL) {
2391 printf("Cannot create pool on socket %d\n",
2396 printf("Allocated pool \"%s\" on socket %d\n",
2397 mp_name, socket_id);
2400 if (session_pool_socket[socket_id].sess_mp == NULL) {
2401 char mp_name[RTE_MEMPOOL_NAMESIZE];
2402 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2403 "sess_mp_%u", socket_id);
2405 session_pool_socket[socket_id].sess_mp =
2406 rte_cryptodev_sym_session_pool_create(
2409 0, 0, 0, socket_id);
2411 if (session_pool_socket[socket_id].sess_mp == NULL) {
2412 printf("Cannot create pool on socket %d\n",
2417 printf("Allocated pool \"%s\" on socket %d\n",
2418 mp_name, socket_id);
2421 /* Set AEAD parameters */
2422 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2423 cap = check_device_support_aead_algo(options, &dev_info,
2426 options->block_size = cap->sym.aead.block_size;
2428 /* Set IV if not provided from command line */
2429 if (options->aead_iv_param == 0) {
2430 if (options->aead_iv_random_size != -1)
2431 options->aead_iv.length =
2432 options->aead_iv_random_size;
2433 /* No size provided, use minimum size. */
2435 options->aead_iv.length =
2436 cap->sym.aead.iv_size.min;
2439 /* Set key if not provided from command line */
2440 if (options->aead_key_param == 0) {
2441 if (options->aead_key_random_size != -1)
2442 options->aead_xform.aead.key.length =
2443 options->aead_key_random_size;
2444 /* No size provided, use minimum size. */
2446 options->aead_xform.aead.key.length =
2447 cap->sym.aead.key_size.min;
2449 generate_random_key(options->aead_key,
2450 options->aead_xform.aead.key.length);
2453 /* Set AAD if not provided from command line */
2454 if (options->aad_param == 0) {
2455 if (options->aad_random_size != -1)
2456 options->aad.length =
2457 options->aad_random_size;
2458 /* No size provided, use minimum size. */
2460 options->aad.length =
2461 cap->sym.auth.aad_size.min;
2464 options->aead_xform.aead.aad_length =
2465 options->aad.length;
2467 /* Set digest size if not provided from command line */
2468 if (options->digest_size != -1)
2469 options->aead_xform.aead.digest_length =
2470 options->digest_size;
2471 /* No size provided, use minimum size. */
2473 options->aead_xform.aead.digest_length =
2474 cap->sym.aead.digest_size.min;
2477 /* Set cipher parameters */
2478 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2479 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2480 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2481 cap = check_device_support_cipher_algo(options, &dev_info,
2483 options->block_size = cap->sym.cipher.block_size;
2485 /* Set IV if not provided from command line */
2486 if (options->cipher_iv_param == 0) {
2487 if (options->cipher_iv_random_size != -1)
2488 options->cipher_iv.length =
2489 options->cipher_iv_random_size;
2490 /* No size provided, use minimum size. */
2492 options->cipher_iv.length =
2493 cap->sym.cipher.iv_size.min;
2496 /* Set key if not provided from command line */
2497 if (options->ckey_param == 0) {
2498 if (options->ckey_random_size != -1)
2499 options->cipher_xform.cipher.key.length =
2500 options->ckey_random_size;
2501 /* No size provided, use minimum size. */
2503 options->cipher_xform.cipher.key.length =
2504 cap->sym.cipher.key_size.min;
2506 generate_random_key(options->cipher_key,
2507 options->cipher_xform.cipher.key.length);
2511 /* Set auth parameters */
2512 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2513 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2514 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2515 cap = check_device_support_auth_algo(options, &dev_info,
2518 /* Set IV if not provided from command line */
2519 if (options->auth_iv_param == 0) {
2520 if (options->auth_iv_random_size != -1)
2521 options->auth_iv.length =
2522 options->auth_iv_random_size;
2523 /* No size provided, use minimum size. */
2525 options->auth_iv.length =
2526 cap->sym.auth.iv_size.min;
2529 /* Set key if not provided from command line */
2530 if (options->akey_param == 0) {
2531 if (options->akey_random_size != -1)
2532 options->auth_xform.auth.key.length =
2533 options->akey_random_size;
2534 /* No size provided, use minimum size. */
2536 options->auth_xform.auth.key.length =
2537 cap->sym.auth.key_size.min;
2539 generate_random_key(options->auth_key,
2540 options->auth_xform.auth.key.length);
2543 /* Set digest size if not provided from command line */
2544 if (options->digest_size != -1)
2545 options->auth_xform.auth.digest_length =
2546 options->digest_size;
2547 /* No size provided, use minimum size. */
2549 options->auth_xform.auth.digest_length =
2550 cap->sym.auth.digest_size.min;
2553 retval = rte_cryptodev_configure(cdev_id, &conf);
2555 printf("Failed to configure cryptodev %u", cdev_id);
2559 qp_conf.nb_descriptors = 2048;
2560 qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
2561 qp_conf.mp_session_private =
2562 session_pool_socket[socket_id].priv_mp;
2564 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2567 printf("Failed to setup queue pair %u on cryptodev %u",
2572 retval = rte_cryptodev_start(cdev_id);
2574 printf("Failed to start device %u: error %d\n",
2580 return enabled_cdev_count;
2584 initialize_ports(struct l2fwd_crypto_options *options)
2586 uint16_t last_portid = 0, portid;
2587 unsigned enabled_portcount = 0;
2588 unsigned nb_ports = rte_eth_dev_count_avail();
2590 if (nb_ports == 0) {
2591 printf("No Ethernet ports - bye\n");
2595 /* Reset l2fwd_dst_ports */
2596 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2597 l2fwd_dst_ports[portid] = 0;
2599 RTE_ETH_FOREACH_DEV(portid) {
2601 struct rte_eth_dev_info dev_info;
2602 struct rte_eth_rxconf rxq_conf;
2603 struct rte_eth_txconf txq_conf;
2604 struct rte_eth_conf local_port_conf = port_conf;
2606 /* Skip ports that are not enabled */
2607 if ((options->portmask & (1 << portid)) == 0)
2611 printf("Initializing port %u... ", portid);
2614 retval = rte_eth_dev_info_get(portid, &dev_info);
2616 printf("Error during getting device (port %u) info: %s\n",
2617 portid, strerror(-retval));
2621 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2622 local_port_conf.txmode.offloads |=
2623 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2624 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2626 printf("Cannot configure device: err=%d, port=%u\n",
2631 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2634 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2639 /* init one RX queue */
2641 rxq_conf = dev_info.default_rxconf;
2642 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2643 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2644 rte_eth_dev_socket_id(portid),
2645 &rxq_conf, l2fwd_pktmbuf_pool);
2647 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2652 /* init one TX queue on each port */
2654 txq_conf = dev_info.default_txconf;
2655 txq_conf.offloads = local_port_conf.txmode.offloads;
2656 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2657 rte_eth_dev_socket_id(portid),
2660 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2667 retval = rte_eth_dev_start(portid);
2669 printf("rte_eth_dev_start:err=%d, port=%u\n",
2674 retval = rte_eth_promiscuous_enable(portid);
2676 printf("rte_eth_promiscuous_enable:err=%s, port=%u\n",
2677 rte_strerror(-retval), portid);
2681 retval = rte_eth_macaddr_get(portid,
2682 &l2fwd_ports_eth_addr[portid]);
2684 printf("rte_eth_macaddr_get :err=%d, port=%u\n",
2689 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2691 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2692 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2693 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2694 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2695 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2696 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2698 /* initialize port stats */
2699 memset(&port_statistics, 0, sizeof(port_statistics));
2701 /* Setup port forwarding table */
2702 if (enabled_portcount % 2) {
2703 l2fwd_dst_ports[portid] = last_portid;
2704 l2fwd_dst_ports[last_portid] = portid;
2706 last_portid = portid;
2709 l2fwd_enabled_port_mask |= (1 << portid);
2710 enabled_portcount++;
2713 if (enabled_portcount == 1) {
2714 l2fwd_dst_ports[last_portid] = last_portid;
2715 } else if (enabled_portcount % 2) {
2716 printf("odd number of ports in portmask- bye\n");
2720 check_all_ports_link_status(l2fwd_enabled_port_mask);
2722 return enabled_portcount;
2726 reserve_key_memory(struct l2fwd_crypto_options *options)
2728 options->cipher_xform.cipher.key.data = options->cipher_key;
2730 options->auth_xform.auth.key.data = options->auth_key;
2732 options->aead_xform.aead.key.data = options->aead_key;
2734 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2735 if (options->cipher_iv.data == NULL)
2736 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2738 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2739 if (options->auth_iv.data == NULL)
2740 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2742 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2743 if (options->aead_iv.data == NULL)
2744 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2746 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2747 if (options->aad.data == NULL)
2748 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2749 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2753 main(int argc, char **argv)
2755 struct lcore_queue_conf *qconf = NULL;
2756 struct l2fwd_crypto_options options;
2758 uint8_t nb_cryptodevs, cdev_id;
2760 unsigned lcore_id, rx_lcore_id = 0;
2761 int ret, enabled_cdevcount, enabled_portcount;
2762 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2765 ret = rte_eal_init(argc, argv);
2767 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2771 /* reserve memory for Cipher/Auth key and IV */
2772 reserve_key_memory(&options);
2774 /* parse application arguments (after the EAL ones) */
2775 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2777 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2779 printf("MAC updating %s\n",
2780 options.mac_updating ? "enabled" : "disabled");
2782 /* create the mbuf pool */
2783 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2784 RTE_ALIGN(sizeof(struct rte_crypto_op),
2785 RTE_CACHE_LINE_SIZE),
2786 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2787 if (l2fwd_pktmbuf_pool == NULL)
2788 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2790 /* create crypto op pool */
2791 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2792 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2794 if (l2fwd_crypto_op_pool == NULL)
2795 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2797 /* Enable Ethernet ports */
2798 enabled_portcount = initialize_ports(&options);
2799 if (enabled_portcount < 1)
2800 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2802 /* Initialize the port/queue configuration of each logical core */
2803 RTE_ETH_FOREACH_DEV(portid) {
2805 /* skip ports that are not enabled */
2806 if ((options.portmask & (1 << portid)) == 0)
2809 if (options.single_lcore && qconf == NULL) {
2810 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2812 if (rx_lcore_id >= RTE_MAX_LCORE)
2813 rte_exit(EXIT_FAILURE,
2814 "Not enough cores\n");
2816 } else if (!options.single_lcore) {
2817 /* get the lcore_id for this port */
2818 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2819 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2820 options.nb_ports_per_lcore) {
2822 if (rx_lcore_id >= RTE_MAX_LCORE)
2823 rte_exit(EXIT_FAILURE,
2824 "Not enough cores\n");
2828 /* Assigned a new logical core in the loop above. */
2829 if (qconf != &lcore_queue_conf[rx_lcore_id])
2830 qconf = &lcore_queue_conf[rx_lcore_id];
2832 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2833 qconf->nb_rx_ports++;
2835 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2838 /* Enable Crypto devices */
2839 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2841 if (enabled_cdevcount < 0)
2842 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2844 if (enabled_cdevcount < enabled_portcount)
2845 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2846 "has to be more or equal to number of ports (%d)\n",
2847 enabled_cdevcount, enabled_portcount);
2849 nb_cryptodevs = rte_cryptodev_count();
2851 /* Initialize the port/cryptodev configuration of each logical core */
2852 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2853 cdev_id < nb_cryptodevs && enabled_cdevcount;
2855 /* Crypto op not supported by crypto device */
2856 if (!enabled_cdevs[cdev_id])
2859 if (options.single_lcore && qconf == NULL) {
2860 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2862 if (rx_lcore_id >= RTE_MAX_LCORE)
2863 rte_exit(EXIT_FAILURE,
2864 "Not enough cores\n");
2866 } else if (!options.single_lcore) {
2867 /* get the lcore_id for this port */
2868 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2869 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2870 options.nb_ports_per_lcore) {
2872 if (rx_lcore_id >= RTE_MAX_LCORE)
2873 rte_exit(EXIT_FAILURE,
2874 "Not enough cores\n");
2878 /* Assigned a new logical core in the loop above. */
2879 if (qconf != &lcore_queue_conf[rx_lcore_id])
2880 qconf = &lcore_queue_conf[rx_lcore_id];
2882 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2883 qconf->nb_crypto_devs++;
2885 enabled_cdevcount--;
2887 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2891 /* launch per-lcore init on every lcore */
2892 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2894 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2895 if (rte_eal_wait_lcore(lcore_id) < 0)
2899 /* clean up the EAL */