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");
343 /* l2fwd_crypto_send_burst 8< */
345 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
346 struct l2fwd_crypto_params *cparams)
348 struct rte_crypto_op **op_buffer;
351 op_buffer = (struct rte_crypto_op **)
352 qconf->op_buf[cparams->dev_id].buffer;
354 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
355 cparams->qp_id, op_buffer, (uint16_t) n);
357 crypto_statistics[cparams->dev_id].enqueued += ret;
358 if (unlikely(ret < n)) {
359 crypto_statistics[cparams->dev_id].errors += (n - ret);
361 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
362 rte_crypto_op_free(op_buffer[ret]);
368 /* >8 End of l2fwd_crypto_send_burst. */
370 /* Crypto enqueue. 8< */
372 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
373 struct l2fwd_crypto_params *cparams)
375 unsigned lcore_id, len;
376 struct lcore_queue_conf *qconf;
378 lcore_id = rte_lcore_id();
380 qconf = &lcore_queue_conf[lcore_id];
381 len = qconf->op_buf[cparams->dev_id].len;
382 qconf->op_buf[cparams->dev_id].buffer[len] = op;
385 /* enough ops to be sent */
386 if (len == MAX_PKT_BURST) {
387 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
391 qconf->op_buf[cparams->dev_id].len = len;
394 /* >8 End of crypto enqueue. */
397 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
398 struct rte_crypto_op *op,
399 struct l2fwd_crypto_params *cparams)
401 struct rte_ether_hdr *eth_hdr;
402 struct rte_ipv4_hdr *ip_hdr;
404 uint32_t ipdata_offset, data_len;
405 uint32_t pad_len = 0;
408 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
410 if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
413 ipdata_offset = sizeof(struct rte_ether_hdr);
415 ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
418 ipdata_offset += (ip_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK)
419 * RTE_IPV4_IHL_MULTIPLIER;
422 /* Zero pad data to be crypto'd so it is block aligned */
423 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
425 if ((cparams->do_hash || cparams->do_aead) && cparams->hash_verify)
426 data_len -= cparams->digest_length;
428 if (cparams->do_cipher) {
430 * Following algorithms are block cipher algorithms,
431 * and might need padding
433 switch (cparams->cipher_algo) {
434 case RTE_CRYPTO_CIPHER_AES_CBC:
435 case RTE_CRYPTO_CIPHER_AES_ECB:
436 case RTE_CRYPTO_CIPHER_DES_CBC:
437 case RTE_CRYPTO_CIPHER_3DES_CBC:
438 case RTE_CRYPTO_CIPHER_3DES_ECB:
439 if (data_len % cparams->block_size)
440 pad_len = cparams->block_size -
441 (data_len % cparams->block_size);
443 case RTE_CRYPTO_CIPHER_AES_XTS:
444 if (cparams->cipher_dataunit_len != 0 &&
445 (data_len % cparams->cipher_dataunit_len))
446 pad_len = cparams->cipher_dataunit_len -
447 (data_len % cparams->cipher_dataunit_len);
454 padding = rte_pktmbuf_append(m, pad_len);
455 if (unlikely(!padding))
459 memset(padding, 0, pad_len);
463 /* Set crypto operation data parameters */
464 rte_crypto_op_attach_sym_session(op, cparams->session);
466 if (cparams->do_hash) {
467 if (cparams->auth_iv.length) {
468 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
471 cparams->cipher_iv.length);
473 * Copy IV at the end of the crypto operation,
474 * after the cipher IV, if added
476 rte_memcpy(iv_ptr, cparams->auth_iv.data,
477 cparams->auth_iv.length);
479 if (!cparams->hash_verify) {
480 /* Append space for digest to end of packet */
481 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
482 cparams->digest_length);
484 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
485 uint8_t *) + ipdata_offset + data_len;
488 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
489 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
491 /* For wireless algorithms, offset/length must be in bits */
492 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
493 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
494 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
495 op->sym->auth.data.offset = ipdata_offset << 3;
496 op->sym->auth.data.length = data_len << 3;
498 op->sym->auth.data.offset = ipdata_offset;
499 op->sym->auth.data.length = data_len;
503 if (cparams->do_cipher) {
504 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
506 /* Copy IV at the end of the crypto operation */
507 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
508 cparams->cipher_iv.length);
510 /* For wireless algorithms, offset/length must be in bits */
511 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
512 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
513 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
514 op->sym->cipher.data.offset = ipdata_offset << 3;
515 op->sym->cipher.data.length = data_len << 3;
517 op->sym->cipher.data.offset = ipdata_offset;
518 op->sym->cipher.data.length = data_len;
522 if (cparams->do_aead) {
523 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
525 /* Copy IV at the end of the crypto operation */
527 * If doing AES-CCM, nonce is copied one byte
528 * after the start of IV field
530 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
531 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
532 cparams->aead_iv.length);
534 rte_memcpy(iv_ptr, cparams->aead_iv.data,
535 cparams->aead_iv.length);
537 op->sym->aead.data.offset = ipdata_offset;
538 op->sym->aead.data.length = data_len;
540 if (!cparams->hash_verify) {
541 /* Append space for digest to end of packet */
542 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
543 cparams->digest_length);
545 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
546 uint8_t *) + ipdata_offset + data_len;
549 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
550 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
552 if (cparams->aad.length) {
553 op->sym->aead.aad.data = cparams->aad.data;
554 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
560 return l2fwd_crypto_enqueue(op, cparams);
564 /* Send the burst of packets on an output interface */
566 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
569 struct rte_mbuf **pkt_buffer;
572 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
574 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
575 port_statistics[port].tx += ret;
576 if (unlikely(ret < n)) {
577 port_statistics[port].dropped += (n - ret);
579 rte_pktmbuf_free(pkt_buffer[ret]);
586 /* Enqueue packets for TX and prepare them to be sent. 8< */
588 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
590 unsigned lcore_id, len;
591 struct lcore_queue_conf *qconf;
593 lcore_id = rte_lcore_id();
595 qconf = &lcore_queue_conf[lcore_id];
596 len = qconf->pkt_buf[port].len;
597 qconf->pkt_buf[port].buffer[len] = m;
600 /* enough pkts to be sent */
601 if (unlikely(len == MAX_PKT_BURST)) {
602 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
606 qconf->pkt_buf[port].len = len;
609 /* >8 End of Enqueuing packets for TX. */
612 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
614 struct rte_ether_hdr *eth;
617 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
619 /* 02:00:00:00:00:xx */
620 tmp = ð->d_addr.addr_bytes[0];
621 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
624 rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
628 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
629 struct l2fwd_crypto_options *options)
633 struct rte_ipv4_hdr *ip_hdr;
634 uint32_t ipdata_offset = sizeof(struct rte_ether_hdr);
636 ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
638 dst_port = l2fwd_dst_ports[portid];
640 if (options->mac_updating)
641 l2fwd_mac_updating(m, dst_port);
643 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
644 rte_pktmbuf_trim(m, options->auth_xform.auth.digest_length);
646 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT) {
647 pad_len = m->pkt_len - rte_be_to_cpu_16(ip_hdr->total_length) -
649 rte_pktmbuf_trim(m, pad_len);
652 l2fwd_send_packet(m, dst_port);
655 /** Generate random key */
657 generate_random_key(uint8_t *key, unsigned length)
662 fd = open("/dev/urandom", O_RDONLY);
664 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
666 ret = read(fd, key, length);
669 if (ret != (signed)length)
670 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
673 /* Session is created and is later attached to the crypto operation. 8< */
674 static struct rte_cryptodev_sym_session *
675 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
677 struct rte_crypto_sym_xform *first_xform;
678 struct rte_cryptodev_sym_session *session;
679 int retval = rte_cryptodev_socket_id(cdev_id);
684 uint8_t socket_id = (uint8_t) retval;
686 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
687 first_xform = &options->aead_xform;
688 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
689 first_xform = &options->cipher_xform;
690 first_xform->next = &options->auth_xform;
691 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
692 first_xform = &options->auth_xform;
693 first_xform->next = &options->cipher_xform;
694 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
695 first_xform = &options->cipher_xform;
697 first_xform = &options->auth_xform;
700 session = rte_cryptodev_sym_session_create(
701 session_pool_socket[socket_id].sess_mp);
705 if (rte_cryptodev_sym_session_init(cdev_id, session,
707 session_pool_socket[socket_id].priv_mp) < 0)
712 /* >8 End of creation of session. */
715 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
717 /* main processing loop */
719 l2fwd_main_loop(struct l2fwd_crypto_options *options)
721 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
722 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
724 unsigned lcore_id = rte_lcore_id();
725 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
726 unsigned int i, j, nb_rx, len;
728 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
729 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
730 US_PER_S * BURST_TX_DRAIN_US;
731 struct l2fwd_crypto_params *cparams;
732 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
733 struct rte_cryptodev_sym_session *session;
735 if (qconf->nb_rx_ports == 0) {
736 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
740 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
742 for (i = 0; i < qconf->nb_rx_ports; i++) {
744 portid = qconf->rx_port_list[i];
745 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
749 for (i = 0; i < qconf->nb_crypto_devs; i++) {
750 port_cparams[i].do_cipher = 0;
751 port_cparams[i].do_hash = 0;
752 port_cparams[i].do_aead = 0;
754 switch (options->xform_chain) {
755 case L2FWD_CRYPTO_AEAD:
756 port_cparams[i].do_aead = 1;
758 case L2FWD_CRYPTO_CIPHER_HASH:
759 case L2FWD_CRYPTO_HASH_CIPHER:
760 port_cparams[i].do_cipher = 1;
761 port_cparams[i].do_hash = 1;
763 case L2FWD_CRYPTO_HASH_ONLY:
764 port_cparams[i].do_hash = 1;
766 case L2FWD_CRYPTO_CIPHER_ONLY:
767 port_cparams[i].do_cipher = 1;
771 port_cparams[i].dev_id = qconf->cryptodev_list[i];
772 port_cparams[i].qp_id = 0;
774 port_cparams[i].block_size = options->block_size;
776 if (port_cparams[i].do_hash) {
777 port_cparams[i].auth_iv.data = options->auth_iv.data;
778 port_cparams[i].auth_iv.length = options->auth_iv.length;
779 if (!options->auth_iv_param)
780 generate_random_key(port_cparams[i].auth_iv.data,
781 port_cparams[i].auth_iv.length);
782 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
783 port_cparams[i].hash_verify = 1;
785 port_cparams[i].hash_verify = 0;
787 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
788 port_cparams[i].digest_length =
789 options->auth_xform.auth.digest_length;
790 /* Set IV parameters */
791 if (options->auth_iv.length) {
792 options->auth_xform.auth.iv.offset =
793 IV_OFFSET + options->cipher_iv.length;
794 options->auth_xform.auth.iv.length =
795 options->auth_iv.length;
799 if (port_cparams[i].do_aead) {
800 port_cparams[i].aead_iv.data = options->aead_iv.data;
801 port_cparams[i].aead_iv.length = options->aead_iv.length;
802 if (!options->aead_iv_param)
803 generate_random_key(port_cparams[i].aead_iv.data,
804 port_cparams[i].aead_iv.length);
805 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
806 port_cparams[i].digest_length =
807 options->aead_xform.aead.digest_length;
808 if (options->aead_xform.aead.aad_length) {
809 port_cparams[i].aad.data = options->aad.data;
810 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
811 port_cparams[i].aad.length = options->aad.length;
812 if (!options->aad_param)
813 generate_random_key(port_cparams[i].aad.data,
814 port_cparams[i].aad.length);
816 * If doing AES-CCM, first 18 bytes has to be reserved,
817 * and actual AAD should start from byte 18
819 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
820 memmove(port_cparams[i].aad.data + 18,
821 port_cparams[i].aad.data,
822 port_cparams[i].aad.length);
825 port_cparams[i].aad.length = 0;
827 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
828 port_cparams[i].hash_verify = 1;
830 port_cparams[i].hash_verify = 0;
832 /* Set IV parameters */
833 options->aead_xform.aead.iv.offset = IV_OFFSET;
834 options->aead_xform.aead.iv.length = options->aead_iv.length;
837 if (port_cparams[i].do_cipher) {
838 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
839 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
840 if (!options->cipher_iv_param)
841 generate_random_key(port_cparams[i].cipher_iv.data,
842 port_cparams[i].cipher_iv.length);
844 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
845 port_cparams[i].cipher_dataunit_len =
846 options->cipher_xform.cipher.dataunit_len;
847 /* Set IV parameters */
848 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
849 options->cipher_xform.cipher.iv.length =
850 options->cipher_iv.length;
853 session = initialize_crypto_session(options,
854 port_cparams[i].dev_id);
856 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
858 port_cparams[i].session = session;
860 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
861 port_cparams[i].dev_id);
864 l2fwd_crypto_options_print(options);
867 * Initialize previous tsc timestamp before the loop,
868 * to avoid showing the port statistics immediately,
869 * so user can see the crypto information.
871 prev_tsc = rte_rdtsc();
874 cur_tsc = rte_rdtsc();
877 * Crypto device/TX burst queue drain
879 diff_tsc = cur_tsc - prev_tsc;
880 if (unlikely(diff_tsc > drain_tsc)) {
881 /* Enqueue all crypto ops remaining in buffers */
882 for (i = 0; i < qconf->nb_crypto_devs; i++) {
883 cparams = &port_cparams[i];
884 len = qconf->op_buf[cparams->dev_id].len;
885 l2fwd_crypto_send_burst(qconf, len, cparams);
886 qconf->op_buf[cparams->dev_id].len = 0;
888 /* Transmit all packets remaining in buffers */
889 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
890 if (qconf->pkt_buf[portid].len == 0)
892 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
893 qconf->pkt_buf[portid].len,
895 qconf->pkt_buf[portid].len = 0;
898 /* if timer is enabled */
899 if (timer_period > 0) {
901 /* advance the timer */
902 timer_tsc += diff_tsc;
904 /* if timer has reached its timeout */
905 if (unlikely(timer_tsc >=
906 (uint64_t)timer_period)) {
908 /* do this only on main core */
909 if (lcore_id == rte_get_main_lcore()
910 && options->refresh_period) {
921 * Read packet from RX queues
923 for (i = 0; i < qconf->nb_rx_ports; i++) {
924 portid = qconf->rx_port_list[i];
926 cparams = &port_cparams[i];
928 nb_rx = rte_eth_rx_burst(portid, 0,
929 pkts_burst, MAX_PKT_BURST);
931 port_statistics[portid].rx += nb_rx;
933 /* Allocate and fillcrypto operations. 8< */
936 * If we can't allocate a crypto_ops, then drop
937 * the rest of the burst and dequeue and
938 * process the packets to free offload structs
940 if (rte_crypto_op_bulk_alloc(
941 l2fwd_crypto_op_pool,
942 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
945 for (j = 0; j < nb_rx; j++)
946 rte_pktmbuf_free(pkts_burst[j]);
950 /* >8 End of crypto operation allocated and filled. */
952 /* Enqueue packets from Crypto device*/
953 for (j = 0; j < nb_rx; j++) {
956 l2fwd_simple_crypto_enqueue(m,
957 ops_burst[j], cparams);
961 /* Dequeue packets from Crypto device. 8< */
963 nb_rx = rte_cryptodev_dequeue_burst(
964 cparams->dev_id, cparams->qp_id,
965 ops_burst, MAX_PKT_BURST);
967 crypto_statistics[cparams->dev_id].dequeued +=
970 /* Forward crypto'd packets */
971 for (j = 0; j < nb_rx; j++) {
972 m = ops_burst[j]->sym->m_src;
974 rte_crypto_op_free(ops_burst[j]);
975 l2fwd_simple_forward(m, portid,
978 } while (nb_rx == MAX_PKT_BURST);
979 /* >8 End of dequeue packets from crypto device. */
985 l2fwd_launch_one_lcore(void *arg)
987 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
991 /* Display command line arguments usage */
993 l2fwd_crypto_usage(const char *prgname)
995 printf("%s [EAL options] --\n"
996 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
997 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
998 " -s manage all ports from single lcore\n"
999 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
1000 " (0 to disable, 10 default, 86400 maximum)\n"
1002 " --cdev_type HW / SW / ANY\n"
1003 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
1004 " HASH_ONLY / AEAD\n"
1006 " --cipher_algo ALGO\n"
1007 " --cipher_op ENCRYPT / DECRYPT\n"
1008 " --cipher_key KEY (bytes separated with \":\")\n"
1009 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
1010 " --cipher_iv IV (bytes separated with \":\")\n"
1011 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1012 " --cipher_dataunit_len SIZE: length of the algorithm data-unit\n"
1014 " --auth_algo ALGO\n"
1015 " --auth_op GENERATE / VERIFY\n"
1016 " --auth_key KEY (bytes separated with \":\")\n"
1017 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1018 " --auth_iv IV (bytes separated with \":\")\n"
1019 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
1021 " --aead_algo ALGO\n"
1022 " --aead_op ENCRYPT / DECRYPT\n"
1023 " --aead_key KEY (bytes separated with \":\")\n"
1024 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
1025 " --aead_iv IV (bytes separated with \":\")\n"
1026 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1027 " --aad AAD (bytes separated with \":\")\n"
1028 " --aad_random_size SIZE: size of AAD when generated randomly\n"
1030 " --digest_size SIZE: size of digest to be generated/verified\n"
1033 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1035 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1037 " - The source MAC address is replaced by the TX port MAC address\n"
1038 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1042 /** Parse crypto device type command line argument */
1044 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1046 if (strcmp("HW", optarg) == 0) {
1047 *type = CDEV_TYPE_HW;
1049 } else if (strcmp("SW", optarg) == 0) {
1050 *type = CDEV_TYPE_SW;
1052 } else if (strcmp("ANY", optarg) == 0) {
1053 *type = CDEV_TYPE_ANY;
1060 /** Parse crypto chain xform command line argument */
1062 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1064 if (strcmp("CIPHER_HASH", optarg) == 0) {
1065 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1067 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1068 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1070 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1071 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1073 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1074 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1076 } else if (strcmp("AEAD", optarg) == 0) {
1077 options->xform_chain = L2FWD_CRYPTO_AEAD;
1084 /** Parse crypto cipher algo option command line argument */
1086 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1089 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1090 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1091 "not supported!\n");
1098 /** Parse crypto cipher operation command line argument */
1100 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1102 if (strcmp("ENCRYPT", optarg) == 0) {
1103 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1105 } else if (strcmp("DECRYPT", optarg) == 0) {
1106 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1110 printf("Cipher operation not supported!\n");
1114 /** Parse bytes from command line argument */
1116 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1118 unsigned byte_count;
1122 for (byte_count = 0, token = strtok(input_arg, ":");
1123 (byte_count < max_size) && (token != NULL);
1124 token = strtok(NULL, ":")) {
1126 int number = (int)strtol(token, NULL, 16);
1128 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1131 data[byte_count++] = (uint8_t)number;
1137 /** Parse size param*/
1139 parse_size(int *size, const char *q_arg)
1144 /* parse hexadecimal string */
1145 n = strtoul(q_arg, &end, 10);
1146 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1150 printf("invalid size\n");
1158 /** Parse crypto cipher operation command line argument */
1160 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1162 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1163 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1164 "not supported!\n");
1172 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1174 if (strcmp("VERIFY", optarg) == 0) {
1175 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1177 } else if (strcmp("GENERATE", optarg) == 0) {
1178 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1182 printf("Authentication operation specified not supported!\n");
1187 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1189 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1190 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1191 "not supported!\n");
1199 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1201 if (strcmp("ENCRYPT", optarg) == 0) {
1202 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1204 } else if (strcmp("DECRYPT", optarg) == 0) {
1205 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1209 printf("AEAD operation specified not supported!\n");
1213 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1219 /* parse hexadecimal string */
1220 pm = strtoul(q_arg, &end, 16);
1221 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1224 options->cryptodev_mask = pm;
1225 if (options->cryptodev_mask == 0) {
1226 printf("invalid cryptodev_mask specified\n");
1233 /** Parse long options */
1235 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1236 struct option *lgopts, int option_index)
1241 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1242 retval = parse_cryptodev_type(&options->type, optarg);
1244 strlcpy(options->string_type, optarg, MAX_STR_LEN);
1248 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1249 return parse_crypto_opt_chain(options, optarg);
1251 /* Cipher options */
1252 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1253 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1256 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1257 return parse_cipher_op(&options->cipher_xform.cipher.op,
1260 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1261 options->ckey_param = 1;
1262 options->cipher_xform.cipher.key.length =
1263 parse_bytes(options->cipher_key, optarg, MAX_KEY_SIZE);
1264 if (options->cipher_xform.cipher.key.length > 0)
1270 else if (strcmp(lgopts[option_index].name, "cipher_dataunit_len") == 0) {
1271 retval = parse_size(&val, optarg);
1272 if (retval == 0 && val >= 0 && val <= UINT16_MAX) {
1273 options->cipher_xform.cipher.dataunit_len =
1280 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1281 return parse_size(&options->ckey_random_size, optarg);
1283 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1284 options->cipher_iv_param = 1;
1285 options->cipher_iv.length =
1286 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1287 if (options->cipher_iv.length > 0)
1293 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1294 return parse_size(&options->cipher_iv_random_size, optarg);
1296 /* Authentication options */
1297 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1298 return parse_auth_algo(&options->auth_xform.auth.algo,
1302 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1303 return parse_auth_op(&options->auth_xform.auth.op,
1306 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1307 options->akey_param = 1;
1308 options->auth_xform.auth.key.length =
1309 parse_bytes(options->auth_key, optarg, MAX_KEY_SIZE);
1310 if (options->auth_xform.auth.key.length > 0)
1316 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1317 return parse_size(&options->akey_random_size, optarg);
1320 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1321 options->auth_iv_param = 1;
1322 options->auth_iv.length =
1323 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1324 if (options->auth_iv.length > 0)
1330 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1331 return parse_size(&options->auth_iv_random_size, optarg);
1334 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1335 return parse_aead_algo(&options->aead_xform.aead.algo,
1339 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1340 return parse_aead_op(&options->aead_xform.aead.op,
1343 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1344 options->aead_key_param = 1;
1345 options->aead_xform.aead.key.length =
1346 parse_bytes(options->aead_key, optarg, MAX_KEY_SIZE);
1347 if (options->aead_xform.aead.key.length > 0)
1353 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1354 return parse_size(&options->aead_key_random_size, optarg);
1357 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1358 options->aead_iv_param = 1;
1359 options->aead_iv.length =
1360 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1361 if (options->aead_iv.length > 0)
1367 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1368 return parse_size(&options->aead_iv_random_size, optarg);
1370 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1371 options->aad_param = 1;
1372 options->aad.length =
1373 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1374 if (options->aad.length > 0)
1380 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1381 return parse_size(&options->aad_random_size, optarg);
1384 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1385 return parse_size(&options->digest_size, optarg);
1388 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1389 options->sessionless = 1;
1393 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1394 return parse_cryptodev_mask(options, optarg);
1396 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1397 options->mac_updating = 1;
1401 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1402 options->mac_updating = 0;
1409 /** Parse port mask */
1411 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1417 /* parse hexadecimal string */
1418 pm = strtoul(q_arg, &end, 16);
1419 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1422 options->portmask = pm;
1423 if (options->portmask == 0) {
1424 printf("invalid portmask specified\n");
1431 /** Parse number of queues */
1433 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1439 /* parse hexadecimal string */
1440 n = strtoul(q_arg, &end, 10);
1441 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1443 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1446 options->nb_ports_per_lcore = n;
1447 if (options->nb_ports_per_lcore == 0) {
1448 printf("invalid number of ports selected\n");
1455 /** Parse timer period */
1457 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1463 /* parse number string */
1464 n = (unsigned)strtol(q_arg, &end, 10);
1465 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1468 if (n >= MAX_TIMER_PERIOD) {
1469 printf("Warning refresh period specified %lu is greater than "
1470 "max value %lu! using max value",
1471 n, MAX_TIMER_PERIOD);
1472 n = MAX_TIMER_PERIOD;
1475 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1480 /** Generate default options for application */
1482 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1484 options->portmask = 0xffffffff;
1485 options->nb_ports_per_lcore = 1;
1486 options->refresh_period = 10000;
1487 options->single_lcore = 0;
1488 options->sessionless = 0;
1490 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1493 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1494 options->cipher_xform.next = NULL;
1495 options->ckey_param = 0;
1496 options->ckey_random_size = -1;
1497 options->cipher_xform.cipher.key.length = 0;
1498 options->cipher_iv_param = 0;
1499 options->cipher_iv_random_size = -1;
1500 options->cipher_iv.length = 0;
1502 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1503 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1504 options->cipher_xform.cipher.dataunit_len = 0;
1506 /* Authentication Data */
1507 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1508 options->auth_xform.next = NULL;
1509 options->akey_param = 0;
1510 options->akey_random_size = -1;
1511 options->auth_xform.auth.key.length = 0;
1512 options->auth_iv_param = 0;
1513 options->auth_iv_random_size = -1;
1514 options->auth_iv.length = 0;
1516 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1517 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1520 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1521 options->aead_xform.next = NULL;
1522 options->aead_key_param = 0;
1523 options->aead_key_random_size = -1;
1524 options->aead_xform.aead.key.length = 0;
1525 options->aead_iv_param = 0;
1526 options->aead_iv_random_size = -1;
1527 options->aead_iv.length = 0;
1529 options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1530 options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1532 options->aad_param = 0;
1533 options->aad_random_size = -1;
1534 options->aad.length = 0;
1536 options->digest_size = -1;
1538 options->type = CDEV_TYPE_ANY;
1539 options->cryptodev_mask = UINT64_MAX;
1541 options->mac_updating = 1;
1545 display_cipher_info(struct l2fwd_crypto_options *options)
1547 printf("\n---- Cipher information ---\n");
1548 printf("Algorithm: %s\n",
1549 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1550 rte_hexdump(stdout, "Cipher key:",
1551 options->cipher_xform.cipher.key.data,
1552 options->cipher_xform.cipher.key.length);
1553 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1557 display_auth_info(struct l2fwd_crypto_options *options)
1559 printf("\n---- Authentication information ---\n");
1560 printf("Algorithm: %s\n",
1561 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1562 rte_hexdump(stdout, "Auth key:",
1563 options->auth_xform.auth.key.data,
1564 options->auth_xform.auth.key.length);
1565 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1569 display_aead_info(struct l2fwd_crypto_options *options)
1571 printf("\n---- AEAD information ---\n");
1572 printf("Algorithm: %s\n",
1573 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1574 rte_hexdump(stdout, "AEAD key:",
1575 options->aead_xform.aead.key.data,
1576 options->aead_xform.aead.key.length);
1577 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1578 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1582 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1584 char string_cipher_op[MAX_STR_LEN];
1585 char string_auth_op[MAX_STR_LEN];
1586 char string_aead_op[MAX_STR_LEN];
1588 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1589 strcpy(string_cipher_op, "Encrypt");
1591 strcpy(string_cipher_op, "Decrypt");
1593 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1594 strcpy(string_auth_op, "Auth generate");
1596 strcpy(string_auth_op, "Auth verify");
1598 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1599 strcpy(string_aead_op, "Authenticated encryption");
1601 strcpy(string_aead_op, "Authenticated decryption");
1604 printf("Options:-\nn");
1605 printf("portmask: %x\n", options->portmask);
1606 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1607 printf("refresh period : %u\n", options->refresh_period);
1608 printf("single lcore mode: %s\n",
1609 options->single_lcore ? "enabled" : "disabled");
1610 printf("stats_printing: %s\n",
1611 options->refresh_period == 0 ? "disabled" : "enabled");
1613 printf("sessionless crypto: %s\n",
1614 options->sessionless ? "enabled" : "disabled");
1616 if (options->ckey_param && (options->ckey_random_size != -1))
1617 printf("Cipher key already parsed, ignoring size of random key\n");
1619 if (options->akey_param && (options->akey_random_size != -1))
1620 printf("Auth key already parsed, ignoring size of random key\n");
1622 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1623 printf("Cipher IV already parsed, ignoring size of random IV\n");
1625 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1626 printf("Auth IV already parsed, ignoring size of random IV\n");
1628 if (options->aad_param && (options->aad_random_size != -1))
1629 printf("AAD already parsed, ignoring size of random AAD\n");
1631 printf("\nCrypto chain: ");
1632 switch (options->xform_chain) {
1633 case L2FWD_CRYPTO_AEAD:
1634 printf("Input --> %s --> Output\n", string_aead_op);
1635 display_aead_info(options);
1637 case L2FWD_CRYPTO_CIPHER_HASH:
1638 printf("Input --> %s --> %s --> Output\n",
1639 string_cipher_op, string_auth_op);
1640 display_cipher_info(options);
1641 display_auth_info(options);
1643 case L2FWD_CRYPTO_HASH_CIPHER:
1644 printf("Input --> %s --> %s --> Output\n",
1645 string_auth_op, string_cipher_op);
1646 display_cipher_info(options);
1647 display_auth_info(options);
1649 case L2FWD_CRYPTO_HASH_ONLY:
1650 printf("Input --> %s --> Output\n", string_auth_op);
1651 display_auth_info(options);
1653 case L2FWD_CRYPTO_CIPHER_ONLY:
1654 printf("Input --> %s --> Output\n", string_cipher_op);
1655 display_cipher_info(options);
1660 /* Parse the argument given in the command line of the application */
1662 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1663 int argc, char **argv)
1665 int opt, retval, option_index;
1666 char **argvopt = argv, *prgname = argv[0];
1668 static struct option lgopts[] = {
1669 { "sessionless", no_argument, 0, 0 },
1671 { "cdev_type", required_argument, 0, 0 },
1672 { "chain", required_argument, 0, 0 },
1674 { "cipher_algo", required_argument, 0, 0 },
1675 { "cipher_op", required_argument, 0, 0 },
1676 { "cipher_key", required_argument, 0, 0 },
1677 { "cipher_key_random_size", required_argument, 0, 0 },
1678 { "cipher_iv", required_argument, 0, 0 },
1679 { "cipher_iv_random_size", required_argument, 0, 0 },
1680 { "cipher_dataunit_len", required_argument, 0, 0},
1682 { "auth_algo", required_argument, 0, 0 },
1683 { "auth_op", required_argument, 0, 0 },
1684 { "auth_key", required_argument, 0, 0 },
1685 { "auth_key_random_size", required_argument, 0, 0 },
1686 { "auth_iv", required_argument, 0, 0 },
1687 { "auth_iv_random_size", required_argument, 0, 0 },
1689 { "aead_algo", required_argument, 0, 0 },
1690 { "aead_op", required_argument, 0, 0 },
1691 { "aead_key", required_argument, 0, 0 },
1692 { "aead_key_random_size", required_argument, 0, 0 },
1693 { "aead_iv", required_argument, 0, 0 },
1694 { "aead_iv_random_size", required_argument, 0, 0 },
1696 { "aad", required_argument, 0, 0 },
1697 { "aad_random_size", required_argument, 0, 0 },
1699 { "digest_size", required_argument, 0, 0 },
1701 { "sessionless", no_argument, 0, 0 },
1702 { "cryptodev_mask", required_argument, 0, 0},
1704 { "mac-updating", no_argument, 0, 0},
1705 { "no-mac-updating", no_argument, 0, 0},
1710 l2fwd_crypto_default_options(options);
1712 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1713 &option_index)) != EOF) {
1717 retval = l2fwd_crypto_parse_args_long_options(options,
1718 lgopts, option_index);
1720 l2fwd_crypto_usage(prgname);
1727 retval = l2fwd_crypto_parse_portmask(options, optarg);
1729 l2fwd_crypto_usage(prgname);
1736 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1738 l2fwd_crypto_usage(prgname);
1745 options->single_lcore = 1;
1751 retval = l2fwd_crypto_parse_timer_period(options,
1754 l2fwd_crypto_usage(prgname);
1760 l2fwd_crypto_usage(prgname);
1767 argv[optind-1] = prgname;
1770 optind = 1; /* reset getopt lib */
1775 /* Check the link status of all ports in up to 9s, and print them finally */
1777 check_all_ports_link_status(uint32_t port_mask)
1779 #define CHECK_INTERVAL 100 /* 100ms */
1780 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1782 uint8_t count, all_ports_up, print_flag = 0;
1783 struct rte_eth_link link;
1785 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1787 printf("\nChecking link status");
1789 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1791 RTE_ETH_FOREACH_DEV(portid) {
1792 if ((port_mask & (1 << portid)) == 0)
1794 memset(&link, 0, sizeof(link));
1795 ret = rte_eth_link_get_nowait(portid, &link);
1798 if (print_flag == 1)
1799 printf("Port %u link get failed: %s\n",
1800 portid, rte_strerror(-ret));
1803 /* print link status if flag set */
1804 if (print_flag == 1) {
1805 rte_eth_link_to_str(link_status_text,
1806 sizeof(link_status_text), &link);
1807 printf("Port %d %s\n", portid,
1811 /* clear all_ports_up flag if any link down */
1812 if (link.link_status == ETH_LINK_DOWN) {
1817 /* after finally printing all link status, get out */
1818 if (print_flag == 1)
1821 if (all_ports_up == 0) {
1824 rte_delay_ms(CHECK_INTERVAL);
1827 /* set the print_flag if all ports up or timeout */
1828 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1835 /* Check if device has to be HW/SW or any */
1837 check_type(const struct l2fwd_crypto_options *options,
1838 const struct rte_cryptodev_info *dev_info)
1840 if (options->type == CDEV_TYPE_HW &&
1841 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1843 if (options->type == CDEV_TYPE_SW &&
1844 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1846 if (options->type == CDEV_TYPE_ANY)
1852 static const struct rte_cryptodev_capabilities *
1853 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1854 const struct rte_cryptodev_info *dev_info,
1858 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1859 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1860 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1861 options->cipher_xform.cipher.algo;
1863 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1864 cap_cipher_algo = cap->sym.cipher.algo;
1865 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1866 if (cap_cipher_algo == opt_cipher_algo) {
1867 if (check_type(options, dev_info) == 0)
1871 cap = &dev_info->capabilities[++i];
1874 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1875 printf("Algorithm %s not supported by cryptodev %u"
1876 " or device not of preferred type (%s)\n",
1877 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1879 options->string_type);
1886 static const struct rte_cryptodev_capabilities *
1887 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1888 const struct rte_cryptodev_info *dev_info,
1892 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1893 enum rte_crypto_auth_algorithm cap_auth_algo;
1894 enum rte_crypto_auth_algorithm opt_auth_algo =
1895 options->auth_xform.auth.algo;
1897 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1898 cap_auth_algo = cap->sym.auth.algo;
1899 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1900 if (cap_auth_algo == opt_auth_algo) {
1901 if (check_type(options, dev_info) == 0)
1905 cap = &dev_info->capabilities[++i];
1908 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1909 printf("Algorithm %s not supported by cryptodev %u"
1910 " or device not of preferred type (%s)\n",
1911 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1913 options->string_type);
1920 static const struct rte_cryptodev_capabilities *
1921 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1922 const struct rte_cryptodev_info *dev_info,
1926 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1927 enum rte_crypto_aead_algorithm cap_aead_algo;
1928 enum rte_crypto_aead_algorithm opt_aead_algo =
1929 options->aead_xform.aead.algo;
1931 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1932 cap_aead_algo = cap->sym.aead.algo;
1933 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1934 if (cap_aead_algo == opt_aead_algo) {
1935 if (check_type(options, dev_info) == 0)
1939 cap = &dev_info->capabilities[++i];
1942 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1943 printf("Algorithm %s not supported by cryptodev %u"
1944 " or device not of preferred type (%s)\n",
1945 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1947 options->string_type);
1954 /* Check if the device is enabled by cryptodev_mask */
1956 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1959 if (options->cryptodev_mask & (1 << cdev_id))
1966 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1972 if (increment == 0) {
1979 /* Range of values */
1980 for (supp_size = min; supp_size <= max; supp_size += increment) {
1981 if (length == supp_size)
1989 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1990 unsigned int iv_param, int iv_random_size,
1994 * Check if length of provided IV is supported
1995 * by the algorithm chosen.
1998 if (check_supported_size(iv_length,
2001 iv_range_size->increment)
2005 * Check if length of IV to be randomly generated
2006 * is supported by the algorithm chosen.
2008 } else if (iv_random_size != -1) {
2009 if (check_supported_size(iv_random_size,
2012 iv_range_size->increment)
2021 check_capabilities(struct l2fwd_crypto_options *options, uint8_t cdev_id)
2023 struct rte_cryptodev_info dev_info;
2024 const struct rte_cryptodev_capabilities *cap;
2026 rte_cryptodev_info_get(cdev_id, &dev_info);
2028 /* Set AEAD parameters */
2029 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2030 /* Check if device supports AEAD algo */
2031 cap = check_device_support_aead_algo(options, &dev_info,
2036 if (check_iv_param(&cap->sym.aead.iv_size,
2037 options->aead_iv_param,
2038 options->aead_iv_random_size,
2039 options->aead_iv.length) != 0) {
2040 RTE_LOG(DEBUG, USER1,
2041 "Device %u does not support IV length\n",
2047 * Check if length of provided AEAD key is supported
2048 * by the algorithm chosen.
2050 if (options->aead_key_param) {
2051 if (check_supported_size(
2052 options->aead_xform.aead.key.length,
2053 cap->sym.aead.key_size.min,
2054 cap->sym.aead.key_size.max,
2055 cap->sym.aead.key_size.increment)
2057 RTE_LOG(DEBUG, USER1,
2058 "Device %u does not support "
2059 "AEAD key length\n",
2064 * Check if length of the aead key to be randomly generated
2065 * is supported by the algorithm chosen.
2067 } else if (options->aead_key_random_size != -1) {
2068 if (check_supported_size(options->aead_key_random_size,
2069 cap->sym.aead.key_size.min,
2070 cap->sym.aead.key_size.max,
2071 cap->sym.aead.key_size.increment)
2073 RTE_LOG(DEBUG, USER1,
2074 "Device %u does not support "
2075 "AEAD key length\n",
2083 * Check if length of provided AAD is supported
2084 * by the algorithm chosen.
2086 if (options->aad_param) {
2087 if (check_supported_size(options->aad.length,
2088 cap->sym.aead.aad_size.min,
2089 cap->sym.aead.aad_size.max,
2090 cap->sym.aead.aad_size.increment)
2092 RTE_LOG(DEBUG, USER1,
2093 "Device %u does not support "
2099 * Check if length of AAD to be randomly generated
2100 * is supported by the algorithm chosen.
2102 } else if (options->aad_random_size != -1) {
2103 if (check_supported_size(options->aad_random_size,
2104 cap->sym.aead.aad_size.min,
2105 cap->sym.aead.aad_size.max,
2106 cap->sym.aead.aad_size.increment)
2108 RTE_LOG(DEBUG, USER1,
2109 "Device %u does not support "
2116 /* Check if digest size is supported by the algorithm. */
2117 if (options->digest_size != -1) {
2118 if (check_supported_size(options->digest_size,
2119 cap->sym.aead.digest_size.min,
2120 cap->sym.aead.digest_size.max,
2121 cap->sym.aead.digest_size.increment)
2123 RTE_LOG(DEBUG, USER1,
2124 "Device %u does not support "
2132 /* Set cipher parameters */
2133 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2134 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2135 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2137 /* Check if device supports cipher algo. 8< */
2138 cap = check_device_support_cipher_algo(options, &dev_info,
2143 if (check_iv_param(&cap->sym.cipher.iv_size,
2144 options->cipher_iv_param,
2145 options->cipher_iv_random_size,
2146 options->cipher_iv.length) != 0) {
2147 RTE_LOG(DEBUG, USER1,
2148 "Device %u does not support IV length\n",
2152 /* >8 End of check if device supports cipher algo. */
2154 /* Check if capable cipher is supported. 8< */
2157 * Check if length of provided cipher key is supported
2158 * by the algorithm chosen.
2160 if (options->ckey_param) {
2161 if (check_supported_size(
2162 options->cipher_xform.cipher.key.length,
2163 cap->sym.cipher.key_size.min,
2164 cap->sym.cipher.key_size.max,
2165 cap->sym.cipher.key_size.increment)
2167 if (dev_info.feature_flags &
2168 RTE_CRYPTODEV_FF_CIPHER_WRAPPED_KEY) {
2169 RTE_LOG(DEBUG, USER1,
2170 "Key length does not match the device "
2171 "%u capability. Key may be wrapped\n",
2174 RTE_LOG(DEBUG, USER1,
2175 "Key length does not match the device "
2183 * Check if length of the cipher key to be randomly generated
2184 * is supported by the algorithm chosen.
2186 } else if (options->ckey_random_size != -1) {
2187 if (check_supported_size(options->ckey_random_size,
2188 cap->sym.cipher.key_size.min,
2189 cap->sym.cipher.key_size.max,
2190 cap->sym.cipher.key_size.increment)
2192 RTE_LOG(DEBUG, USER1,
2193 "Device %u does not support cipher "
2200 if (options->cipher_xform.cipher.dataunit_len > 0) {
2201 if (!(dev_info.feature_flags &
2202 RTE_CRYPTODEV_FF_CIPHER_MULTIPLE_DATA_UNITS)) {
2203 RTE_LOG(DEBUG, USER1,
2204 "Device %u does not support "
2205 "cipher multiple data units\n",
2209 if (cap->sym.cipher.dataunit_set != 0) {
2212 switch (options->cipher_xform.cipher.dataunit_len) {
2214 if (!(cap->sym.cipher.dataunit_set &
2215 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_512_BYTES))
2219 if (!(cap->sym.cipher.dataunit_set &
2220 RTE_CRYPTO_CIPHER_DATA_UNIT_LEN_4096_BYTES))
2227 RTE_LOG(DEBUG, USER1,
2228 "Device %u does not support "
2229 "data-unit length %u\n",
2231 options->cipher_xform.cipher.dataunit_len);
2236 /* >8 End of checking if cipher is supported. */
2239 /* Set auth parameters */
2240 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2241 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2242 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2243 /* Check if device supports auth algo */
2244 cap = check_device_support_auth_algo(options, &dev_info,
2249 if (check_iv_param(&cap->sym.auth.iv_size,
2250 options->auth_iv_param,
2251 options->auth_iv_random_size,
2252 options->auth_iv.length) != 0) {
2253 RTE_LOG(DEBUG, USER1,
2254 "Device %u does not support IV length\n",
2259 * Check if length of provided auth key is supported
2260 * by the algorithm chosen.
2262 if (options->akey_param) {
2263 if (check_supported_size(
2264 options->auth_xform.auth.key.length,
2265 cap->sym.auth.key_size.min,
2266 cap->sym.auth.key_size.max,
2267 cap->sym.auth.key_size.increment)
2269 RTE_LOG(DEBUG, USER1,
2270 "Device %u does not support auth "
2276 * Check if length of the auth key to be randomly generated
2277 * is supported by the algorithm chosen.
2279 } else if (options->akey_random_size != -1) {
2280 if (check_supported_size(options->akey_random_size,
2281 cap->sym.auth.key_size.min,
2282 cap->sym.auth.key_size.max,
2283 cap->sym.auth.key_size.increment)
2285 RTE_LOG(DEBUG, USER1,
2286 "Device %u does not support auth "
2293 /* Check if digest size is supported by the algorithm. */
2294 if (options->digest_size != -1) {
2295 if (check_supported_size(options->digest_size,
2296 cap->sym.auth.digest_size.min,
2297 cap->sym.auth.digest_size.max,
2298 cap->sym.auth.digest_size.increment)
2300 RTE_LOG(DEBUG, USER1,
2301 "Device %u does not support "
2313 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2314 uint8_t *enabled_cdevs)
2316 uint8_t cdev_id, cdev_count, enabled_cdev_count = 0;
2317 const struct rte_cryptodev_capabilities *cap;
2318 unsigned int sess_sz, max_sess_sz = 0;
2319 uint32_t sessions_needed = 0;
2322 cdev_count = rte_cryptodev_count();
2323 if (cdev_count == 0) {
2324 printf("No crypto devices available\n");
2328 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2330 if (check_cryptodev_mask(options, cdev_id) < 0)
2333 if (check_capabilities(options, cdev_id) < 0)
2336 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2337 if (sess_sz > max_sess_sz)
2338 max_sess_sz = sess_sz;
2340 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2342 enabled_cdevs[cdev_id] = 1;
2343 enabled_cdev_count++;
2346 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2347 struct rte_cryptodev_qp_conf qp_conf;
2348 struct rte_cryptodev_info dev_info;
2350 if (enabled_cdevs[cdev_id] == 0)
2353 if (check_cryptodev_mask(options, cdev_id) < 0)
2356 if (check_capabilities(options, cdev_id) < 0)
2359 retval = rte_cryptodev_socket_id(cdev_id);
2362 printf("Invalid crypto device id used\n");
2366 uint8_t socket_id = (uint8_t) retval;
2368 struct rte_cryptodev_config conf = {
2369 .nb_queue_pairs = 1,
2370 .socket_id = socket_id,
2371 .ff_disable = RTE_CRYPTODEV_FF_SECURITY,
2374 rte_cryptodev_info_get(cdev_id, &dev_info);
2377 * Two sessions objects are required for each session
2378 * (one for the header, one for the private data)
2380 if (!strcmp(dev_info.driver_name, "crypto_scheduler")) {
2381 #ifdef RTE_CRYPTO_SCHEDULER
2382 uint32_t nb_workers =
2383 rte_cryptodev_scheduler_workers_get(cdev_id,
2386 sessions_needed = enabled_cdev_count * nb_workers;
2389 sessions_needed = enabled_cdev_count;
2391 if (session_pool_socket[socket_id].priv_mp == NULL) {
2392 char mp_name[RTE_MEMPOOL_NAMESIZE];
2394 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2395 "priv_sess_mp_%u", socket_id);
2397 session_pool_socket[socket_id].priv_mp =
2398 rte_mempool_create(mp_name,
2401 0, 0, NULL, NULL, NULL,
2405 if (session_pool_socket[socket_id].priv_mp == NULL) {
2406 printf("Cannot create pool on socket %d\n",
2411 printf("Allocated pool \"%s\" on socket %d\n",
2412 mp_name, socket_id);
2415 if (session_pool_socket[socket_id].sess_mp == NULL) {
2416 char mp_name[RTE_MEMPOOL_NAMESIZE];
2417 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2418 "sess_mp_%u", socket_id);
2420 session_pool_socket[socket_id].sess_mp =
2421 rte_cryptodev_sym_session_pool_create(
2424 0, 0, 0, socket_id);
2426 if (session_pool_socket[socket_id].sess_mp == NULL) {
2427 printf("Cannot create pool on socket %d\n",
2432 printf("Allocated pool \"%s\" on socket %d\n",
2433 mp_name, socket_id);
2436 /* Set AEAD parameters */
2437 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2438 cap = check_device_support_aead_algo(options, &dev_info,
2441 options->block_size = cap->sym.aead.block_size;
2443 /* Set IV if not provided from command line */
2444 if (options->aead_iv_param == 0) {
2445 if (options->aead_iv_random_size != -1)
2446 options->aead_iv.length =
2447 options->aead_iv_random_size;
2448 /* No size provided, use minimum size. */
2450 options->aead_iv.length =
2451 cap->sym.aead.iv_size.min;
2454 /* Set key if not provided from command line */
2455 if (options->aead_key_param == 0) {
2456 if (options->aead_key_random_size != -1)
2457 options->aead_xform.aead.key.length =
2458 options->aead_key_random_size;
2459 /* No size provided, use minimum size. */
2461 options->aead_xform.aead.key.length =
2462 cap->sym.aead.key_size.min;
2464 generate_random_key(options->aead_key,
2465 options->aead_xform.aead.key.length);
2468 /* Set AAD if not provided from command line */
2469 if (options->aad_param == 0) {
2470 if (options->aad_random_size != -1)
2471 options->aad.length =
2472 options->aad_random_size;
2473 /* No size provided, use minimum size. */
2475 options->aad.length =
2476 cap->sym.auth.aad_size.min;
2479 options->aead_xform.aead.aad_length =
2480 options->aad.length;
2482 /* Set digest size if not provided from command line */
2483 if (options->digest_size != -1)
2484 options->aead_xform.aead.digest_length =
2485 options->digest_size;
2486 /* No size provided, use minimum size. */
2488 options->aead_xform.aead.digest_length =
2489 cap->sym.aead.digest_size.min;
2492 /* Set cipher parameters */
2493 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2494 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2495 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2496 cap = check_device_support_cipher_algo(options, &dev_info,
2498 options->block_size = cap->sym.cipher.block_size;
2500 /* Set IV if not provided from command line */
2501 if (options->cipher_iv_param == 0) {
2502 if (options->cipher_iv_random_size != -1)
2503 options->cipher_iv.length =
2504 options->cipher_iv_random_size;
2505 /* No size provided, use minimum size. */
2507 options->cipher_iv.length =
2508 cap->sym.cipher.iv_size.min;
2511 /* Set key if not provided from command line */
2512 if (options->ckey_param == 0) {
2513 if (options->ckey_random_size != -1)
2514 options->cipher_xform.cipher.key.length =
2515 options->ckey_random_size;
2516 /* No size provided, use minimum size. */
2518 options->cipher_xform.cipher.key.length =
2519 cap->sym.cipher.key_size.min;
2521 generate_random_key(options->cipher_key,
2522 options->cipher_xform.cipher.key.length);
2526 /* Set auth parameters */
2527 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2528 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2529 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2530 cap = check_device_support_auth_algo(options, &dev_info,
2533 /* Set IV if not provided from command line */
2534 if (options->auth_iv_param == 0) {
2535 if (options->auth_iv_random_size != -1)
2536 options->auth_iv.length =
2537 options->auth_iv_random_size;
2538 /* No size provided, use minimum size. */
2540 options->auth_iv.length =
2541 cap->sym.auth.iv_size.min;
2544 /* Set key if not provided from command line */
2545 if (options->akey_param == 0) {
2546 if (options->akey_random_size != -1)
2547 options->auth_xform.auth.key.length =
2548 options->akey_random_size;
2549 /* No size provided, use minimum size. */
2551 options->auth_xform.auth.key.length =
2552 cap->sym.auth.key_size.min;
2554 generate_random_key(options->auth_key,
2555 options->auth_xform.auth.key.length);
2558 /* Set digest size if not provided from command line */
2559 if (options->digest_size != -1)
2560 options->auth_xform.auth.digest_length =
2561 options->digest_size;
2562 /* No size provided, use minimum size. */
2564 options->auth_xform.auth.digest_length =
2565 cap->sym.auth.digest_size.min;
2568 retval = rte_cryptodev_configure(cdev_id, &conf);
2570 printf("Failed to configure cryptodev %u", cdev_id);
2574 qp_conf.nb_descriptors = 2048;
2575 qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
2576 qp_conf.mp_session_private =
2577 session_pool_socket[socket_id].priv_mp;
2579 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2582 printf("Failed to setup queue pair %u on cryptodev %u",
2587 retval = rte_cryptodev_start(cdev_id);
2589 printf("Failed to start device %u: error %d\n",
2595 return enabled_cdev_count;
2599 initialize_ports(struct l2fwd_crypto_options *options)
2601 uint16_t last_portid = 0, portid;
2602 unsigned enabled_portcount = 0;
2603 unsigned nb_ports = rte_eth_dev_count_avail();
2605 if (nb_ports == 0) {
2606 printf("No Ethernet ports - bye\n");
2610 /* Reset l2fwd_dst_ports */
2611 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2612 l2fwd_dst_ports[portid] = 0;
2614 RTE_ETH_FOREACH_DEV(portid) {
2616 struct rte_eth_dev_info dev_info;
2617 struct rte_eth_rxconf rxq_conf;
2618 struct rte_eth_txconf txq_conf;
2619 struct rte_eth_conf local_port_conf = port_conf;
2621 /* Skip ports that are not enabled */
2622 if ((options->portmask & (1 << portid)) == 0)
2626 printf("Initializing port %u... ", portid);
2629 retval = rte_eth_dev_info_get(portid, &dev_info);
2631 printf("Error during getting device (port %u) info: %s\n",
2632 portid, strerror(-retval));
2636 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2637 local_port_conf.txmode.offloads |=
2638 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2639 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2641 printf("Cannot configure device: err=%d, port=%u\n",
2646 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2649 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2654 /* init one RX queue */
2656 rxq_conf = dev_info.default_rxconf;
2657 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2658 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2659 rte_eth_dev_socket_id(portid),
2660 &rxq_conf, l2fwd_pktmbuf_pool);
2662 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2667 /* init one TX queue on each port */
2669 txq_conf = dev_info.default_txconf;
2670 txq_conf.offloads = local_port_conf.txmode.offloads;
2671 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2672 rte_eth_dev_socket_id(portid),
2675 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2682 retval = rte_eth_dev_start(portid);
2684 printf("rte_eth_dev_start:err=%d, port=%u\n",
2689 retval = rte_eth_promiscuous_enable(portid);
2691 printf("rte_eth_promiscuous_enable:err=%s, port=%u\n",
2692 rte_strerror(-retval), portid);
2696 retval = rte_eth_macaddr_get(portid,
2697 &l2fwd_ports_eth_addr[portid]);
2699 printf("rte_eth_macaddr_get :err=%d, port=%u\n",
2704 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2706 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2707 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2708 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2709 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2710 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2711 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2713 /* initialize port stats */
2714 memset(&port_statistics, 0, sizeof(port_statistics));
2716 /* Setup port forwarding table */
2717 if (enabled_portcount % 2) {
2718 l2fwd_dst_ports[portid] = last_portid;
2719 l2fwd_dst_ports[last_portid] = portid;
2721 last_portid = portid;
2724 l2fwd_enabled_port_mask |= (1 << portid);
2725 enabled_portcount++;
2728 if (enabled_portcount == 1) {
2729 l2fwd_dst_ports[last_portid] = last_portid;
2730 } else if (enabled_portcount % 2) {
2731 printf("odd number of ports in portmask- bye\n");
2735 check_all_ports_link_status(l2fwd_enabled_port_mask);
2737 return enabled_portcount;
2741 reserve_key_memory(struct l2fwd_crypto_options *options)
2743 options->cipher_xform.cipher.key.data = options->cipher_key;
2745 options->auth_xform.auth.key.data = options->auth_key;
2747 options->aead_xform.aead.key.data = options->aead_key;
2749 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2750 if (options->cipher_iv.data == NULL)
2751 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2753 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2754 if (options->auth_iv.data == NULL)
2755 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2757 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2758 if (options->aead_iv.data == NULL)
2759 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2761 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2762 if (options->aad.data == NULL)
2763 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2764 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2768 main(int argc, char **argv)
2770 struct lcore_queue_conf *qconf = NULL;
2771 struct l2fwd_crypto_options options;
2773 uint8_t nb_cryptodevs, cdev_id;
2775 unsigned lcore_id, rx_lcore_id = 0;
2776 int ret, enabled_cdevcount, enabled_portcount;
2777 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2780 ret = rte_eal_init(argc, argv);
2782 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2786 /* reserve memory for Cipher/Auth key and IV */
2787 reserve_key_memory(&options);
2789 /* parse application arguments (after the EAL ones) */
2790 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2792 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2794 printf("MAC updating %s\n",
2795 options.mac_updating ? "enabled" : "disabled");
2797 /* create the mbuf pool */
2798 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2799 RTE_ALIGN(sizeof(struct rte_crypto_op),
2800 RTE_CACHE_LINE_SIZE),
2801 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2802 if (l2fwd_pktmbuf_pool == NULL)
2803 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2805 /* create crypto op pool */
2806 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2807 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2809 if (l2fwd_crypto_op_pool == NULL)
2810 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2812 /* Enable Ethernet ports */
2813 enabled_portcount = initialize_ports(&options);
2814 if (enabled_portcount < 1)
2815 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2817 /* Initialize the port/queue configuration of each logical core */
2818 RTE_ETH_FOREACH_DEV(portid) {
2820 /* skip ports that are not enabled */
2821 if ((options.portmask & (1 << portid)) == 0)
2824 if (options.single_lcore && qconf == NULL) {
2825 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2827 if (rx_lcore_id >= RTE_MAX_LCORE)
2828 rte_exit(EXIT_FAILURE,
2829 "Not enough cores\n");
2831 } else if (!options.single_lcore) {
2832 /* get the lcore_id for this port */
2833 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2834 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2835 options.nb_ports_per_lcore) {
2837 if (rx_lcore_id >= RTE_MAX_LCORE)
2838 rte_exit(EXIT_FAILURE,
2839 "Not enough cores\n");
2843 /* Assigned a new logical core in the loop above. */
2844 if (qconf != &lcore_queue_conf[rx_lcore_id])
2845 qconf = &lcore_queue_conf[rx_lcore_id];
2847 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2848 qconf->nb_rx_ports++;
2850 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2853 /* Enable Crypto devices */
2854 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2856 if (enabled_cdevcount < 0)
2857 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2859 if (enabled_cdevcount < enabled_portcount)
2860 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2861 "has to be more or equal to number of ports (%d)\n",
2862 enabled_cdevcount, enabled_portcount);
2864 nb_cryptodevs = rte_cryptodev_count();
2866 /* Initialize the port/cryptodev configuration of each logical core */
2867 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2868 cdev_id < nb_cryptodevs && enabled_cdevcount;
2870 /* Crypto op not supported by crypto device */
2871 if (!enabled_cdevs[cdev_id])
2874 if (options.single_lcore && qconf == NULL) {
2875 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2877 if (rx_lcore_id >= RTE_MAX_LCORE)
2878 rte_exit(EXIT_FAILURE,
2879 "Not enough cores\n");
2881 } else if (!options.single_lcore) {
2882 /* get the lcore_id for this port */
2883 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2884 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2885 options.nb_ports_per_lcore) {
2887 if (rx_lcore_id >= RTE_MAX_LCORE)
2888 rte_exit(EXIT_FAILURE,
2889 "Not enough cores\n");
2893 /* Assigned a new logical core in the loop above. */
2894 if (qconf != &lcore_queue_conf[rx_lcore_id])
2895 qconf = &lcore_queue_conf[rx_lcore_id];
2897 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2898 qconf->nb_crypto_devs++;
2900 enabled_cdevcount--;
2902 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2906 /* launch per-lcore init on every lcore */
2907 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2909 RTE_LCORE_FOREACH_WORKER(lcore_id) {
2910 if (rte_eal_wait_lcore(lcore_id) < 0)
2914 /* clean up the EAL */