1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2016 Intel Corporation
11 #include <sys/types.h>
12 #include <sys/queue.h>
13 #include <netinet/in.h>
22 #include <rte_atomic.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_common.h>
25 #include <rte_cryptodev.h>
26 #include <rte_cycles.h>
27 #include <rte_debug.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_interrupts.h>
33 #include <rte_launch.h>
34 #include <rte_lcore.h>
36 #include <rte_malloc.h>
38 #include <rte_memcpy.h>
39 #include <rte_memory.h>
40 #include <rte_mempool.h>
41 #include <rte_per_lcore.h>
42 #include <rte_prefetch.h>
43 #include <rte_random.h>
44 #include <rte_hexdump.h>
52 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
56 #define MAX_STR_LEN 32
57 #define MAX_KEY_SIZE 128
58 #define MAX_IV_SIZE 16
59 #define MAX_AAD_SIZE 65535
60 #define MAX_PKT_BURST 32
61 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
62 #define MAX_SESSIONS 32
63 #define SESSION_POOL_CACHE_SIZE 0
65 #define MAXIMUM_IV_LENGTH 16
66 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
67 sizeof(struct rte_crypto_sym_op))
70 * Configurable number of RX/TX ring descriptors
72 #define RTE_TEST_RX_DESC_DEFAULT 1024
73 #define RTE_TEST_TX_DESC_DEFAULT 1024
75 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
76 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
78 /* ethernet addresses of ports */
79 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
81 /* mask of enabled ports */
82 static uint64_t l2fwd_enabled_port_mask;
83 static uint64_t l2fwd_enabled_crypto_mask;
85 /* list of enabled ports */
86 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
91 struct rte_mbuf *buffer[MAX_PKT_BURST];
96 struct rte_crypto_op *buffer[MAX_PKT_BURST];
99 #define MAX_RX_QUEUE_PER_LCORE 16
100 #define MAX_TX_QUEUE_PER_PORT 16
102 enum l2fwd_crypto_xform_chain {
103 L2FWD_CRYPTO_CIPHER_HASH,
104 L2FWD_CRYPTO_HASH_CIPHER,
105 L2FWD_CRYPTO_CIPHER_ONLY,
106 L2FWD_CRYPTO_HASH_ONLY,
113 rte_iova_t phys_addr;
121 /** l2fwd crypto application command line options */
122 struct l2fwd_crypto_options {
124 unsigned nb_ports_per_lcore;
125 unsigned refresh_period;
126 unsigned single_lcore:1;
129 unsigned sessionless:1;
131 enum l2fwd_crypto_xform_chain xform_chain;
133 struct rte_crypto_sym_xform cipher_xform;
135 int ckey_random_size;
137 struct l2fwd_iv cipher_iv;
138 unsigned int cipher_iv_param;
139 int cipher_iv_random_size;
141 struct rte_crypto_sym_xform auth_xform;
143 int akey_random_size;
145 struct l2fwd_iv auth_iv;
146 unsigned int auth_iv_param;
147 int auth_iv_random_size;
149 struct rte_crypto_sym_xform aead_xform;
150 unsigned int aead_key_param;
151 int aead_key_random_size;
153 struct l2fwd_iv aead_iv;
154 unsigned int aead_iv_param;
155 int aead_iv_random_size;
157 struct l2fwd_key aad;
164 char string_type[MAX_STR_LEN];
166 uint64_t cryptodev_mask;
168 unsigned int mac_updating;
171 /** l2fwd crypto lcore params */
172 struct l2fwd_crypto_params {
176 unsigned digest_length;
179 struct l2fwd_iv cipher_iv;
180 struct l2fwd_iv auth_iv;
181 struct l2fwd_iv aead_iv;
182 struct l2fwd_key aad;
183 struct rte_cryptodev_sym_session *session;
190 enum rte_crypto_cipher_algorithm cipher_algo;
191 enum rte_crypto_auth_algorithm auth_algo;
192 enum rte_crypto_aead_algorithm aead_algo;
195 /** lcore configuration */
196 struct lcore_queue_conf {
197 unsigned nb_rx_ports;
198 uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
200 unsigned nb_crypto_devs;
201 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
203 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
204 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
205 } __rte_cache_aligned;
207 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
209 static struct rte_eth_conf port_conf = {
211 .mq_mode = ETH_MQ_RX_NONE,
212 .max_rx_pkt_len = ETHER_MAX_LEN,
214 .ignore_offload_bitfield = 1,
215 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
218 .mq_mode = ETH_MQ_TX_NONE,
222 struct rte_mempool *l2fwd_pktmbuf_pool;
223 struct rte_mempool *l2fwd_crypto_op_pool;
224 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
226 /* Per-port statistics struct */
227 struct l2fwd_port_statistics {
231 uint64_t crypto_enqueued;
232 uint64_t crypto_dequeued;
235 } __rte_cache_aligned;
237 struct l2fwd_crypto_statistics {
242 } __rte_cache_aligned;
244 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
245 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
247 /* A tsc-based timer responsible for triggering statistics printout */
248 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
249 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
251 /* default period is 10 seconds */
252 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
254 /* Print out statistics on packets dropped */
258 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
259 uint64_t total_packets_enqueued, total_packets_dequeued,
260 total_packets_errors;
264 total_packets_dropped = 0;
265 total_packets_tx = 0;
266 total_packets_rx = 0;
267 total_packets_enqueued = 0;
268 total_packets_dequeued = 0;
269 total_packets_errors = 0;
271 const char clr[] = { 27, '[', '2', 'J', '\0' };
272 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
274 /* Clear screen and move to top left */
275 printf("%s%s", clr, topLeft);
277 printf("\nPort statistics ====================================");
279 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
280 /* skip disabled ports */
281 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
283 printf("\nStatistics for port %u ------------------------------"
284 "\nPackets sent: %32"PRIu64
285 "\nPackets received: %28"PRIu64
286 "\nPackets dropped: %29"PRIu64,
288 port_statistics[portid].tx,
289 port_statistics[portid].rx,
290 port_statistics[portid].dropped);
292 total_packets_dropped += port_statistics[portid].dropped;
293 total_packets_tx += port_statistics[portid].tx;
294 total_packets_rx += port_statistics[portid].rx;
296 printf("\nCrypto statistics ==================================");
298 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
299 /* skip disabled ports */
300 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
302 printf("\nStatistics for cryptodev %"PRIu64
303 " -------------------------"
304 "\nPackets enqueued: %28"PRIu64
305 "\nPackets dequeued: %28"PRIu64
306 "\nPackets errors: %30"PRIu64,
308 crypto_statistics[cdevid].enqueued,
309 crypto_statistics[cdevid].dequeued,
310 crypto_statistics[cdevid].errors);
312 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
313 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
314 total_packets_errors += crypto_statistics[cdevid].errors;
316 printf("\nAggregate statistics ==============================="
317 "\nTotal packets received: %22"PRIu64
318 "\nTotal packets enqueued: %22"PRIu64
319 "\nTotal packets dequeued: %22"PRIu64
320 "\nTotal packets sent: %26"PRIu64
321 "\nTotal packets dropped: %23"PRIu64
322 "\nTotal packets crypto errors: %17"PRIu64,
324 total_packets_enqueued,
325 total_packets_dequeued,
327 total_packets_dropped,
328 total_packets_errors);
329 printf("\n====================================================\n");
333 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
334 struct l2fwd_crypto_params *cparams)
336 struct rte_crypto_op **op_buffer;
339 op_buffer = (struct rte_crypto_op **)
340 qconf->op_buf[cparams->dev_id].buffer;
342 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
343 cparams->qp_id, op_buffer, (uint16_t) n);
345 crypto_statistics[cparams->dev_id].enqueued += ret;
346 if (unlikely(ret < n)) {
347 crypto_statistics[cparams->dev_id].errors += (n - ret);
349 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
350 rte_crypto_op_free(op_buffer[ret]);
358 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
359 struct l2fwd_crypto_params *cparams)
361 unsigned lcore_id, len;
362 struct lcore_queue_conf *qconf;
364 lcore_id = rte_lcore_id();
366 qconf = &lcore_queue_conf[lcore_id];
367 len = qconf->op_buf[cparams->dev_id].len;
368 qconf->op_buf[cparams->dev_id].buffer[len] = op;
371 /* enough ops to be sent */
372 if (len == MAX_PKT_BURST) {
373 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
377 qconf->op_buf[cparams->dev_id].len = len;
382 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
383 struct rte_crypto_op *op,
384 struct l2fwd_crypto_params *cparams)
386 struct ether_hdr *eth_hdr;
387 struct ipv4_hdr *ip_hdr;
389 uint32_t ipdata_offset, data_len;
390 uint32_t pad_len = 0;
393 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
395 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
398 ipdata_offset = sizeof(struct ether_hdr);
400 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
403 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
404 * IPV4_IHL_MULTIPLIER;
407 /* Zero pad data to be crypto'd so it is block aligned */
408 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
410 if (cparams->do_hash && cparams->hash_verify)
411 data_len -= cparams->digest_length;
413 if (cparams->do_cipher) {
415 * Following algorithms are block cipher algorithms,
416 * and might need padding
418 switch (cparams->cipher_algo) {
419 case RTE_CRYPTO_CIPHER_AES_CBC:
420 case RTE_CRYPTO_CIPHER_AES_ECB:
421 case RTE_CRYPTO_CIPHER_DES_CBC:
422 case RTE_CRYPTO_CIPHER_3DES_CBC:
423 case RTE_CRYPTO_CIPHER_3DES_ECB:
424 if (data_len % cparams->block_size)
425 pad_len = cparams->block_size -
426 (data_len % cparams->block_size);
433 padding = rte_pktmbuf_append(m, pad_len);
434 if (unlikely(!padding))
438 memset(padding, 0, pad_len);
442 /* Set crypto operation data parameters */
443 rte_crypto_op_attach_sym_session(op, cparams->session);
445 if (cparams->do_hash) {
446 if (cparams->auth_iv.length) {
447 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
450 cparams->cipher_iv.length);
452 * Copy IV at the end of the crypto operation,
453 * after the cipher IV, if added
455 rte_memcpy(iv_ptr, cparams->auth_iv.data,
456 cparams->auth_iv.length);
458 if (!cparams->hash_verify) {
459 /* Append space for digest to end of packet */
460 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
461 cparams->digest_length);
463 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
464 uint8_t *) + ipdata_offset + data_len;
467 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
468 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
470 /* For wireless algorithms, offset/length must be in bits */
471 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
472 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
473 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
474 op->sym->auth.data.offset = ipdata_offset << 3;
475 op->sym->auth.data.length = data_len << 3;
477 op->sym->auth.data.offset = ipdata_offset;
478 op->sym->auth.data.length = data_len;
482 if (cparams->do_cipher) {
483 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
485 /* Copy IV at the end of the crypto operation */
486 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
487 cparams->cipher_iv.length);
489 /* For wireless algorithms, offset/length must be in bits */
490 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
491 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
492 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
493 op->sym->cipher.data.offset = ipdata_offset << 3;
494 op->sym->cipher.data.length = data_len << 3;
496 op->sym->cipher.data.offset = ipdata_offset;
497 op->sym->cipher.data.length = data_len;
501 if (cparams->do_aead) {
502 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
504 /* Copy IV at the end of the crypto operation */
506 * If doing AES-CCM, nonce is copied one byte
507 * after the start of IV field
509 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
510 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
511 cparams->aead_iv.length);
513 rte_memcpy(iv_ptr, cparams->aead_iv.data,
514 cparams->aead_iv.length);
516 op->sym->aead.data.offset = ipdata_offset;
517 op->sym->aead.data.length = data_len;
519 if (!cparams->hash_verify) {
520 /* Append space for digest to end of packet */
521 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
522 cparams->digest_length);
524 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
525 uint8_t *) + ipdata_offset + data_len;
528 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
529 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
531 if (cparams->aad.length) {
532 op->sym->aead.aad.data = cparams->aad.data;
533 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
539 return l2fwd_crypto_enqueue(op, cparams);
543 /* Send the burst of packets on an output interface */
545 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
548 struct rte_mbuf **pkt_buffer;
551 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
553 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
554 port_statistics[port].tx += ret;
555 if (unlikely(ret < n)) {
556 port_statistics[port].dropped += (n - ret);
558 rte_pktmbuf_free(pkt_buffer[ret]);
565 /* Enqueue packets for TX and prepare them to be sent */
567 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
569 unsigned lcore_id, len;
570 struct lcore_queue_conf *qconf;
572 lcore_id = rte_lcore_id();
574 qconf = &lcore_queue_conf[lcore_id];
575 len = qconf->pkt_buf[port].len;
576 qconf->pkt_buf[port].buffer[len] = m;
579 /* enough pkts to be sent */
580 if (unlikely(len == MAX_PKT_BURST)) {
581 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
585 qconf->pkt_buf[port].len = len;
590 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
592 struct ether_hdr *eth;
595 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
597 /* 02:00:00:00:00:xx */
598 tmp = ð->d_addr.addr_bytes[0];
599 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
602 ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
606 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
607 struct l2fwd_crypto_options *options)
611 dst_port = l2fwd_dst_ports[portid];
613 if (options->mac_updating)
614 l2fwd_mac_updating(m, dst_port);
616 l2fwd_send_packet(m, dst_port);
619 /** Generate random key */
621 generate_random_key(uint8_t *key, unsigned length)
626 fd = open("/dev/urandom", O_RDONLY);
628 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
630 ret = read(fd, key, length);
633 if (ret != (signed)length)
634 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
637 static struct rte_cryptodev_sym_session *
638 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
640 struct rte_crypto_sym_xform *first_xform;
641 struct rte_cryptodev_sym_session *session;
642 int retval = rte_cryptodev_socket_id(cdev_id);
647 uint8_t socket_id = (uint8_t) retval;
648 struct rte_mempool *sess_mp = session_pool_socket[socket_id];
650 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
651 first_xform = &options->aead_xform;
652 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
653 first_xform = &options->cipher_xform;
654 first_xform->next = &options->auth_xform;
655 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
656 first_xform = &options->auth_xform;
657 first_xform->next = &options->cipher_xform;
658 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
659 first_xform = &options->cipher_xform;
661 first_xform = &options->auth_xform;
664 session = rte_cryptodev_sym_session_create(sess_mp);
669 if (rte_cryptodev_sym_session_init(cdev_id, session,
670 first_xform, sess_mp) < 0)
677 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
679 /* main processing loop */
681 l2fwd_main_loop(struct l2fwd_crypto_options *options)
683 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
684 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
686 unsigned lcore_id = rte_lcore_id();
687 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
688 unsigned int i, j, nb_rx, len;
690 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
691 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
692 US_PER_S * BURST_TX_DRAIN_US;
693 struct l2fwd_crypto_params *cparams;
694 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
695 struct rte_cryptodev_sym_session *session;
697 if (qconf->nb_rx_ports == 0) {
698 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
702 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
704 for (i = 0; i < qconf->nb_rx_ports; i++) {
706 portid = qconf->rx_port_list[i];
707 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
711 for (i = 0; i < qconf->nb_crypto_devs; i++) {
712 port_cparams[i].do_cipher = 0;
713 port_cparams[i].do_hash = 0;
714 port_cparams[i].do_aead = 0;
716 switch (options->xform_chain) {
717 case L2FWD_CRYPTO_AEAD:
718 port_cparams[i].do_aead = 1;
720 case L2FWD_CRYPTO_CIPHER_HASH:
721 case L2FWD_CRYPTO_HASH_CIPHER:
722 port_cparams[i].do_cipher = 1;
723 port_cparams[i].do_hash = 1;
725 case L2FWD_CRYPTO_HASH_ONLY:
726 port_cparams[i].do_hash = 1;
728 case L2FWD_CRYPTO_CIPHER_ONLY:
729 port_cparams[i].do_cipher = 1;
733 port_cparams[i].dev_id = qconf->cryptodev_list[i];
734 port_cparams[i].qp_id = 0;
736 port_cparams[i].block_size = options->block_size;
738 if (port_cparams[i].do_hash) {
739 port_cparams[i].auth_iv.data = options->auth_iv.data;
740 port_cparams[i].auth_iv.length = options->auth_iv.length;
741 if (!options->auth_iv_param)
742 generate_random_key(port_cparams[i].auth_iv.data,
743 port_cparams[i].auth_iv.length);
744 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
745 port_cparams[i].hash_verify = 1;
747 port_cparams[i].hash_verify = 0;
749 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
750 port_cparams[i].digest_length =
751 options->auth_xform.auth.digest_length;
752 /* Set IV parameters */
753 if (options->auth_iv.length) {
754 options->auth_xform.auth.iv.offset =
755 IV_OFFSET + options->cipher_iv.length;
756 options->auth_xform.auth.iv.length =
757 options->auth_iv.length;
761 if (port_cparams[i].do_aead) {
762 port_cparams[i].aead_iv.data = options->aead_iv.data;
763 port_cparams[i].aead_iv.length = options->aead_iv.length;
764 if (!options->aead_iv_param)
765 generate_random_key(port_cparams[i].aead_iv.data,
766 port_cparams[i].aead_iv.length);
767 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
768 port_cparams[i].digest_length =
769 options->aead_xform.aead.digest_length;
770 if (options->aead_xform.aead.aad_length) {
771 port_cparams[i].aad.data = options->aad.data;
772 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
773 port_cparams[i].aad.length = options->aad.length;
774 if (!options->aad_param)
775 generate_random_key(port_cparams[i].aad.data,
776 port_cparams[i].aad.length);
778 * If doing AES-CCM, first 18 bytes has to be reserved,
779 * and actual AAD should start from byte 18
781 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
782 memmove(port_cparams[i].aad.data + 18,
783 port_cparams[i].aad.data,
784 port_cparams[i].aad.length);
787 port_cparams[i].aad.length = 0;
789 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
790 port_cparams[i].hash_verify = 1;
792 port_cparams[i].hash_verify = 0;
794 /* Set IV parameters */
795 options->aead_xform.aead.iv.offset = IV_OFFSET;
796 options->aead_xform.aead.iv.length = options->aead_iv.length;
799 if (port_cparams[i].do_cipher) {
800 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
801 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
802 if (!options->cipher_iv_param)
803 generate_random_key(port_cparams[i].cipher_iv.data,
804 port_cparams[i].cipher_iv.length);
806 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
807 /* Set IV parameters */
808 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
809 options->cipher_xform.cipher.iv.length =
810 options->cipher_iv.length;
813 session = initialize_crypto_session(options,
814 port_cparams[i].dev_id);
816 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
818 port_cparams[i].session = session;
820 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
821 port_cparams[i].dev_id);
824 l2fwd_crypto_options_print(options);
827 * Initialize previous tsc timestamp before the loop,
828 * to avoid showing the port statistics immediately,
829 * so user can see the crypto information.
831 prev_tsc = rte_rdtsc();
834 cur_tsc = rte_rdtsc();
837 * Crypto device/TX burst queue drain
839 diff_tsc = cur_tsc - prev_tsc;
840 if (unlikely(diff_tsc > drain_tsc)) {
841 /* Enqueue all crypto ops remaining in buffers */
842 for (i = 0; i < qconf->nb_crypto_devs; i++) {
843 cparams = &port_cparams[i];
844 len = qconf->op_buf[cparams->dev_id].len;
845 l2fwd_crypto_send_burst(qconf, len, cparams);
846 qconf->op_buf[cparams->dev_id].len = 0;
848 /* Transmit all packets remaining in buffers */
849 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
850 if (qconf->pkt_buf[portid].len == 0)
852 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
853 qconf->pkt_buf[portid].len,
855 qconf->pkt_buf[portid].len = 0;
858 /* if timer is enabled */
859 if (timer_period > 0) {
861 /* advance the timer */
862 timer_tsc += diff_tsc;
864 /* if timer has reached its timeout */
865 if (unlikely(timer_tsc >=
866 (uint64_t)timer_period)) {
868 /* do this only on master core */
869 if (lcore_id == rte_get_master_lcore()
870 && options->refresh_period) {
881 * Read packet from RX queues
883 for (i = 0; i < qconf->nb_rx_ports; i++) {
884 portid = qconf->rx_port_list[i];
886 cparams = &port_cparams[i];
888 nb_rx = rte_eth_rx_burst(portid, 0,
889 pkts_burst, MAX_PKT_BURST);
891 port_statistics[portid].rx += nb_rx;
895 * If we can't allocate a crypto_ops, then drop
896 * the rest of the burst and dequeue and
897 * process the packets to free offload structs
899 if (rte_crypto_op_bulk_alloc(
900 l2fwd_crypto_op_pool,
901 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
904 for (j = 0; j < nb_rx; j++)
905 rte_pktmbuf_free(pkts_burst[j]);
910 /* Enqueue packets from Crypto device*/
911 for (j = 0; j < nb_rx; j++) {
914 l2fwd_simple_crypto_enqueue(m,
915 ops_burst[j], cparams);
919 /* Dequeue packets from Crypto device */
921 nb_rx = rte_cryptodev_dequeue_burst(
922 cparams->dev_id, cparams->qp_id,
923 ops_burst, MAX_PKT_BURST);
925 crypto_statistics[cparams->dev_id].dequeued +=
928 /* Forward crypto'd packets */
929 for (j = 0; j < nb_rx; j++) {
930 m = ops_burst[j]->sym->m_src;
932 rte_crypto_op_free(ops_burst[j]);
933 l2fwd_simple_forward(m, portid,
936 } while (nb_rx == MAX_PKT_BURST);
942 l2fwd_launch_one_lcore(void *arg)
944 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
948 /* Display command line arguments usage */
950 l2fwd_crypto_usage(const char *prgname)
952 printf("%s [EAL options] --\n"
953 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
954 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
955 " -s manage all ports from single lcore\n"
956 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
957 " (0 to disable, 10 default, 86400 maximum)\n"
959 " --cdev_type HW / SW / ANY\n"
960 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
961 " HASH_ONLY / AEAD\n"
963 " --cipher_algo ALGO\n"
964 " --cipher_op ENCRYPT / DECRYPT\n"
965 " --cipher_key KEY (bytes separated with \":\")\n"
966 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
967 " --cipher_iv IV (bytes separated with \":\")\n"
968 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
970 " --auth_algo ALGO\n"
971 " --auth_op GENERATE / VERIFY\n"
972 " --auth_key KEY (bytes separated with \":\")\n"
973 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
974 " --auth_iv IV (bytes separated with \":\")\n"
975 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
977 " --aead_algo ALGO\n"
978 " --aead_op ENCRYPT / DECRYPT\n"
979 " --aead_key KEY (bytes separated with \":\")\n"
980 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
981 " --aead_iv IV (bytes separated with \":\")\n"
982 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
983 " --aad AAD (bytes separated with \":\")\n"
984 " --aad_random_size SIZE: size of AAD when generated randomly\n"
986 " --digest_size SIZE: size of digest to be generated/verified\n"
989 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
991 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
993 " - The source MAC address is replaced by the TX port MAC address\n"
994 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
998 /** Parse crypto device type command line argument */
1000 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1002 if (strcmp("HW", optarg) == 0) {
1003 *type = CDEV_TYPE_HW;
1005 } else if (strcmp("SW", optarg) == 0) {
1006 *type = CDEV_TYPE_SW;
1008 } else if (strcmp("ANY", optarg) == 0) {
1009 *type = CDEV_TYPE_ANY;
1016 /** Parse crypto chain xform command line argument */
1018 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1020 if (strcmp("CIPHER_HASH", optarg) == 0) {
1021 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1023 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1024 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1026 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1027 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1029 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1030 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1032 } else if (strcmp("AEAD", optarg) == 0) {
1033 options->xform_chain = L2FWD_CRYPTO_AEAD;
1040 /** Parse crypto cipher algo option command line argument */
1042 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1045 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1046 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1047 "not supported!\n");
1054 /** Parse crypto cipher operation command line argument */
1056 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1058 if (strcmp("ENCRYPT", optarg) == 0) {
1059 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1061 } else if (strcmp("DECRYPT", optarg) == 0) {
1062 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1066 printf("Cipher operation not supported!\n");
1070 /** Parse bytes from command line argument */
1072 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1074 unsigned byte_count;
1078 for (byte_count = 0, token = strtok(input_arg, ":");
1079 (byte_count < max_size) && (token != NULL);
1080 token = strtok(NULL, ":")) {
1082 int number = (int)strtol(token, NULL, 16);
1084 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1087 data[byte_count++] = (uint8_t)number;
1093 /** Parse size param*/
1095 parse_size(int *size, const char *q_arg)
1100 /* parse hexadecimal string */
1101 n = strtoul(q_arg, &end, 10);
1102 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1106 printf("invalid size\n");
1114 /** Parse crypto cipher operation command line argument */
1116 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1118 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1119 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1120 "not supported!\n");
1128 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1130 if (strcmp("VERIFY", optarg) == 0) {
1131 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1133 } else if (strcmp("GENERATE", optarg) == 0) {
1134 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1138 printf("Authentication operation specified not supported!\n");
1143 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1145 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1146 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1147 "not supported!\n");
1155 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1157 if (strcmp("ENCRYPT", optarg) == 0) {
1158 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1160 } else if (strcmp("DECRYPT", optarg) == 0) {
1161 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1165 printf("AEAD operation specified not supported!\n");
1169 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1175 /* parse hexadecimal string */
1176 pm = strtoul(q_arg, &end, 16);
1177 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1180 options->cryptodev_mask = pm;
1181 if (options->cryptodev_mask == 0) {
1182 printf("invalid cryptodev_mask specified\n");
1189 /** Parse long options */
1191 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1192 struct option *lgopts, int option_index)
1196 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1197 retval = parse_cryptodev_type(&options->type, optarg);
1199 snprintf(options->string_type, MAX_STR_LEN,
1204 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1205 return parse_crypto_opt_chain(options, optarg);
1207 /* Cipher options */
1208 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1209 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1212 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1213 return parse_cipher_op(&options->cipher_xform.cipher.op,
1216 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1217 options->ckey_param = 1;
1218 options->cipher_xform.cipher.key.length =
1219 parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1221 if (options->cipher_xform.cipher.key.length > 0)
1227 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1228 return parse_size(&options->ckey_random_size, optarg);
1230 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1231 options->cipher_iv_param = 1;
1232 options->cipher_iv.length =
1233 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1234 if (options->cipher_iv.length > 0)
1240 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1241 return parse_size(&options->cipher_iv_random_size, optarg);
1243 /* Authentication options */
1244 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1245 return parse_auth_algo(&options->auth_xform.auth.algo,
1249 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1250 return parse_auth_op(&options->auth_xform.auth.op,
1253 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1254 options->akey_param = 1;
1255 options->auth_xform.auth.key.length =
1256 parse_bytes(options->auth_xform.auth.key.data, optarg,
1258 if (options->auth_xform.auth.key.length > 0)
1264 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1265 return parse_size(&options->akey_random_size, optarg);
1268 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1269 options->auth_iv_param = 1;
1270 options->auth_iv.length =
1271 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1272 if (options->auth_iv.length > 0)
1278 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1279 return parse_size(&options->auth_iv_random_size, optarg);
1282 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1283 return parse_aead_algo(&options->aead_xform.aead.algo,
1287 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1288 return parse_aead_op(&options->aead_xform.aead.op,
1291 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1292 options->aead_key_param = 1;
1293 options->aead_xform.aead.key.length =
1294 parse_bytes(options->aead_xform.aead.key.data, optarg,
1296 if (options->aead_xform.aead.key.length > 0)
1302 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1303 return parse_size(&options->aead_key_random_size, optarg);
1306 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1307 options->aead_iv_param = 1;
1308 options->aead_iv.length =
1309 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1310 if (options->aead_iv.length > 0)
1316 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1317 return parse_size(&options->aead_iv_random_size, optarg);
1319 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1320 options->aad_param = 1;
1321 options->aad.length =
1322 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1323 if (options->aad.length > 0)
1329 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1330 return parse_size(&options->aad_random_size, optarg);
1333 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1334 return parse_size(&options->digest_size, optarg);
1337 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1338 options->sessionless = 1;
1342 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1343 return parse_cryptodev_mask(options, optarg);
1345 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1346 options->mac_updating = 1;
1350 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1351 options->mac_updating = 0;
1358 /** Parse port mask */
1360 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1366 /* parse hexadecimal string */
1367 pm = strtoul(q_arg, &end, 16);
1368 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1371 options->portmask = pm;
1372 if (options->portmask == 0) {
1373 printf("invalid portmask specified\n");
1380 /** Parse number of queues */
1382 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1388 /* parse hexadecimal string */
1389 n = strtoul(q_arg, &end, 10);
1390 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1392 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1395 options->nb_ports_per_lcore = n;
1396 if (options->nb_ports_per_lcore == 0) {
1397 printf("invalid number of ports selected\n");
1404 /** Parse timer period */
1406 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1412 /* parse number string */
1413 n = (unsigned)strtol(q_arg, &end, 10);
1414 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1417 if (n >= MAX_TIMER_PERIOD) {
1418 printf("Warning refresh period specified %lu is greater than "
1419 "max value %lu! using max value",
1420 n, MAX_TIMER_PERIOD);
1421 n = MAX_TIMER_PERIOD;
1424 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1429 /** Generate default options for application */
1431 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1433 options->portmask = 0xffffffff;
1434 options->nb_ports_per_lcore = 1;
1435 options->refresh_period = 10000;
1436 options->single_lcore = 0;
1437 options->sessionless = 0;
1439 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1442 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1443 options->cipher_xform.next = NULL;
1444 options->ckey_param = 0;
1445 options->ckey_random_size = -1;
1446 options->cipher_xform.cipher.key.length = 0;
1447 options->cipher_iv_param = 0;
1448 options->cipher_iv_random_size = -1;
1449 options->cipher_iv.length = 0;
1451 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1452 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1454 /* Authentication Data */
1455 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1456 options->auth_xform.next = NULL;
1457 options->akey_param = 0;
1458 options->akey_random_size = -1;
1459 options->auth_xform.auth.key.length = 0;
1460 options->auth_iv_param = 0;
1461 options->auth_iv_random_size = -1;
1462 options->auth_iv.length = 0;
1464 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1465 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1468 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1469 options->aead_xform.next = NULL;
1470 options->aead_key_param = 0;
1471 options->aead_key_random_size = -1;
1472 options->aead_xform.aead.key.length = 0;
1473 options->aead_iv_param = 0;
1474 options->aead_iv_random_size = -1;
1475 options->aead_iv.length = 0;
1477 options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1478 options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1480 options->aad_param = 0;
1481 options->aad_random_size = -1;
1482 options->aad.length = 0;
1484 options->digest_size = -1;
1486 options->type = CDEV_TYPE_ANY;
1487 options->cryptodev_mask = UINT64_MAX;
1489 options->mac_updating = 1;
1493 display_cipher_info(struct l2fwd_crypto_options *options)
1495 printf("\n---- Cipher information ---\n");
1496 printf("Algorithm: %s\n",
1497 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1498 rte_hexdump(stdout, "Cipher key:",
1499 options->cipher_xform.cipher.key.data,
1500 options->cipher_xform.cipher.key.length);
1501 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1505 display_auth_info(struct l2fwd_crypto_options *options)
1507 printf("\n---- Authentication information ---\n");
1508 printf("Algorithm: %s\n",
1509 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1510 rte_hexdump(stdout, "Auth key:",
1511 options->auth_xform.auth.key.data,
1512 options->auth_xform.auth.key.length);
1513 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1517 display_aead_info(struct l2fwd_crypto_options *options)
1519 printf("\n---- AEAD information ---\n");
1520 printf("Algorithm: %s\n",
1521 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1522 rte_hexdump(stdout, "AEAD key:",
1523 options->aead_xform.aead.key.data,
1524 options->aead_xform.aead.key.length);
1525 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1526 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1530 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1532 char string_cipher_op[MAX_STR_LEN];
1533 char string_auth_op[MAX_STR_LEN];
1534 char string_aead_op[MAX_STR_LEN];
1536 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1537 strcpy(string_cipher_op, "Encrypt");
1539 strcpy(string_cipher_op, "Decrypt");
1541 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1542 strcpy(string_auth_op, "Auth generate");
1544 strcpy(string_auth_op, "Auth verify");
1546 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1547 strcpy(string_aead_op, "Authenticated encryption");
1549 strcpy(string_aead_op, "Authenticated decryption");
1552 printf("Options:-\nn");
1553 printf("portmask: %x\n", options->portmask);
1554 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1555 printf("refresh period : %u\n", options->refresh_period);
1556 printf("single lcore mode: %s\n",
1557 options->single_lcore ? "enabled" : "disabled");
1558 printf("stats_printing: %s\n",
1559 options->refresh_period == 0 ? "disabled" : "enabled");
1561 printf("sessionless crypto: %s\n",
1562 options->sessionless ? "enabled" : "disabled");
1564 if (options->ckey_param && (options->ckey_random_size != -1))
1565 printf("Cipher key already parsed, ignoring size of random key\n");
1567 if (options->akey_param && (options->akey_random_size != -1))
1568 printf("Auth key already parsed, ignoring size of random key\n");
1570 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1571 printf("Cipher IV already parsed, ignoring size of random IV\n");
1573 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1574 printf("Auth IV already parsed, ignoring size of random IV\n");
1576 if (options->aad_param && (options->aad_random_size != -1))
1577 printf("AAD already parsed, ignoring size of random AAD\n");
1579 printf("\nCrypto chain: ");
1580 switch (options->xform_chain) {
1581 case L2FWD_CRYPTO_AEAD:
1582 printf("Input --> %s --> Output\n", string_aead_op);
1583 display_aead_info(options);
1585 case L2FWD_CRYPTO_CIPHER_HASH:
1586 printf("Input --> %s --> %s --> Output\n",
1587 string_cipher_op, string_auth_op);
1588 display_cipher_info(options);
1589 display_auth_info(options);
1591 case L2FWD_CRYPTO_HASH_CIPHER:
1592 printf("Input --> %s --> %s --> Output\n",
1593 string_auth_op, string_cipher_op);
1594 display_cipher_info(options);
1595 display_auth_info(options);
1597 case L2FWD_CRYPTO_HASH_ONLY:
1598 printf("Input --> %s --> Output\n", string_auth_op);
1599 display_auth_info(options);
1601 case L2FWD_CRYPTO_CIPHER_ONLY:
1602 printf("Input --> %s --> Output\n", string_cipher_op);
1603 display_cipher_info(options);
1608 /* Parse the argument given in the command line of the application */
1610 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1611 int argc, char **argv)
1613 int opt, retval, option_index;
1614 char **argvopt = argv, *prgname = argv[0];
1616 static struct option lgopts[] = {
1617 { "sessionless", no_argument, 0, 0 },
1619 { "cdev_type", required_argument, 0, 0 },
1620 { "chain", required_argument, 0, 0 },
1622 { "cipher_algo", required_argument, 0, 0 },
1623 { "cipher_op", required_argument, 0, 0 },
1624 { "cipher_key", required_argument, 0, 0 },
1625 { "cipher_key_random_size", required_argument, 0, 0 },
1626 { "cipher_iv", required_argument, 0, 0 },
1627 { "cipher_iv_random_size", required_argument, 0, 0 },
1629 { "auth_algo", required_argument, 0, 0 },
1630 { "auth_op", required_argument, 0, 0 },
1631 { "auth_key", required_argument, 0, 0 },
1632 { "auth_key_random_size", required_argument, 0, 0 },
1633 { "auth_iv", required_argument, 0, 0 },
1634 { "auth_iv_random_size", required_argument, 0, 0 },
1636 { "aead_algo", required_argument, 0, 0 },
1637 { "aead_op", required_argument, 0, 0 },
1638 { "aead_key", required_argument, 0, 0 },
1639 { "aead_key_random_size", required_argument, 0, 0 },
1640 { "aead_iv", required_argument, 0, 0 },
1641 { "aead_iv_random_size", required_argument, 0, 0 },
1643 { "aad", required_argument, 0, 0 },
1644 { "aad_random_size", required_argument, 0, 0 },
1646 { "digest_size", required_argument, 0, 0 },
1648 { "sessionless", no_argument, 0, 0 },
1649 { "cryptodev_mask", required_argument, 0, 0},
1651 { "mac-updating", no_argument, 0, 0},
1652 { "no-mac-updating", no_argument, 0, 0},
1657 l2fwd_crypto_default_options(options);
1659 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1660 &option_index)) != EOF) {
1664 retval = l2fwd_crypto_parse_args_long_options(options,
1665 lgopts, option_index);
1667 l2fwd_crypto_usage(prgname);
1674 retval = l2fwd_crypto_parse_portmask(options, optarg);
1676 l2fwd_crypto_usage(prgname);
1683 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1685 l2fwd_crypto_usage(prgname);
1692 options->single_lcore = 1;
1698 retval = l2fwd_crypto_parse_timer_period(options,
1701 l2fwd_crypto_usage(prgname);
1707 l2fwd_crypto_usage(prgname);
1714 argv[optind-1] = prgname;
1717 optind = 1; /* reset getopt lib */
1722 /* Check the link status of all ports in up to 9s, and print them finally */
1724 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1726 #define CHECK_INTERVAL 100 /* 100ms */
1727 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1729 uint8_t count, all_ports_up, print_flag = 0;
1730 struct rte_eth_link link;
1732 printf("\nChecking link status");
1734 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1736 for (portid = 0; portid < port_num; portid++) {
1737 if ((port_mask & (1 << portid)) == 0)
1739 memset(&link, 0, sizeof(link));
1740 rte_eth_link_get_nowait(portid, &link);
1741 /* print link status if flag set */
1742 if (print_flag == 1) {
1743 if (link.link_status)
1745 "Port%d Link Up. Speed %u Mbps - %s\n",
1746 portid, link.link_speed,
1747 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1748 ("full-duplex") : ("half-duplex\n"));
1750 printf("Port %d Link Down\n", portid);
1753 /* clear all_ports_up flag if any link down */
1754 if (link.link_status == ETH_LINK_DOWN) {
1759 /* after finally printing all link status, get out */
1760 if (print_flag == 1)
1763 if (all_ports_up == 0) {
1766 rte_delay_ms(CHECK_INTERVAL);
1769 /* set the print_flag if all ports up or timeout */
1770 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1777 /* Check if device has to be HW/SW or any */
1779 check_type(const struct l2fwd_crypto_options *options,
1780 const struct rte_cryptodev_info *dev_info)
1782 if (options->type == CDEV_TYPE_HW &&
1783 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1785 if (options->type == CDEV_TYPE_SW &&
1786 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1788 if (options->type == CDEV_TYPE_ANY)
1794 static const struct rte_cryptodev_capabilities *
1795 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1796 const struct rte_cryptodev_info *dev_info,
1800 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1801 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1802 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1803 options->cipher_xform.cipher.algo;
1805 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1806 cap_cipher_algo = cap->sym.cipher.algo;
1807 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1808 if (cap_cipher_algo == opt_cipher_algo) {
1809 if (check_type(options, dev_info) == 0)
1813 cap = &dev_info->capabilities[++i];
1816 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1817 printf("Algorithm %s not supported by cryptodev %u"
1818 " or device not of preferred type (%s)\n",
1819 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1821 options->string_type);
1828 static const struct rte_cryptodev_capabilities *
1829 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1830 const struct rte_cryptodev_info *dev_info,
1834 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1835 enum rte_crypto_auth_algorithm cap_auth_algo;
1836 enum rte_crypto_auth_algorithm opt_auth_algo =
1837 options->auth_xform.auth.algo;
1839 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1840 cap_auth_algo = cap->sym.auth.algo;
1841 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1842 if (cap_auth_algo == opt_auth_algo) {
1843 if (check_type(options, dev_info) == 0)
1847 cap = &dev_info->capabilities[++i];
1850 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1851 printf("Algorithm %s not supported by cryptodev %u"
1852 " or device not of preferred type (%s)\n",
1853 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1855 options->string_type);
1862 static const struct rte_cryptodev_capabilities *
1863 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1864 const struct rte_cryptodev_info *dev_info,
1868 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1869 enum rte_crypto_aead_algorithm cap_aead_algo;
1870 enum rte_crypto_aead_algorithm opt_aead_algo =
1871 options->aead_xform.aead.algo;
1873 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1874 cap_aead_algo = cap->sym.aead.algo;
1875 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1876 if (cap_aead_algo == opt_aead_algo) {
1877 if (check_type(options, dev_info) == 0)
1881 cap = &dev_info->capabilities[++i];
1884 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1885 printf("Algorithm %s not supported by cryptodev %u"
1886 " or device not of preferred type (%s)\n",
1887 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1889 options->string_type);
1896 /* Check if the device is enabled by cryptodev_mask */
1898 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1901 if (options->cryptodev_mask & (1 << cdev_id))
1908 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1914 if (increment == 0) {
1921 /* Range of values */
1922 for (supp_size = min; supp_size <= max; supp_size += increment) {
1923 if (length == supp_size)
1931 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1932 unsigned int iv_param, int iv_random_size,
1933 uint16_t *iv_length)
1936 * Check if length of provided IV is supported
1937 * by the algorithm chosen.
1940 if (check_supported_size(*iv_length,
1943 iv_range_size->increment)
1945 printf("Unsupported IV length\n");
1949 * Check if length of IV to be randomly generated
1950 * is supported by the algorithm chosen.
1952 } else if (iv_random_size != -1) {
1953 if (check_supported_size(iv_random_size,
1956 iv_range_size->increment)
1958 printf("Unsupported IV length\n");
1961 *iv_length = iv_random_size;
1962 /* No size provided, use minimum size. */
1964 *iv_length = iv_range_size->min;
1970 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1971 uint8_t *enabled_cdevs)
1973 unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
1974 const struct rte_cryptodev_capabilities *cap;
1975 unsigned int sess_sz, max_sess_sz = 0;
1978 cdev_count = rte_cryptodev_count();
1979 if (cdev_count == 0) {
1980 printf("No crypto devices available\n");
1984 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
1985 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1986 if (sess_sz > max_sess_sz)
1987 max_sess_sz = sess_sz;
1990 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1992 struct rte_cryptodev_qp_conf qp_conf;
1993 struct rte_cryptodev_info dev_info;
1994 retval = rte_cryptodev_socket_id(cdev_id);
1997 printf("Invalid crypto device id used\n");
2001 uint8_t socket_id = (uint8_t) retval;
2003 struct rte_cryptodev_config conf = {
2004 .nb_queue_pairs = 1,
2005 .socket_id = socket_id,
2008 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
2011 rte_cryptodev_info_get(cdev_id, &dev_info);
2013 if (session_pool_socket[socket_id] == NULL) {
2014 char mp_name[RTE_MEMPOOL_NAMESIZE];
2015 struct rte_mempool *sess_mp;
2017 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2018 "sess_mp_%u", socket_id);
2021 * Create enough objects for session headers and
2022 * device private data
2024 sess_mp = rte_mempool_create(mp_name,
2027 SESSION_POOL_CACHE_SIZE,
2028 0, NULL, NULL, NULL,
2032 if (sess_mp == NULL) {
2033 printf("Cannot create session pool on socket %d\n",
2038 printf("Allocated session pool on socket %d\n", socket_id);
2039 session_pool_socket[socket_id] = sess_mp;
2042 /* Set AEAD parameters */
2043 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2044 /* Check if device supports AEAD algo */
2045 cap = check_device_support_aead_algo(options, &dev_info,
2050 options->block_size = cap->sym.aead.block_size;
2052 check_iv_param(&cap->sym.aead.iv_size,
2053 options->aead_iv_param,
2054 options->aead_iv_random_size,
2055 &options->aead_iv.length);
2058 * Check if length of provided AEAD key is supported
2059 * by the algorithm chosen.
2061 if (options->aead_key_param) {
2062 if (check_supported_size(
2063 options->aead_xform.aead.key.length,
2064 cap->sym.aead.key_size.min,
2065 cap->sym.aead.key_size.max,
2066 cap->sym.aead.key_size.increment)
2068 printf("Unsupported aead key length\n");
2072 * Check if length of the aead key to be randomly generated
2073 * is supported by the algorithm chosen.
2075 } else if (options->aead_key_random_size != -1) {
2076 if (check_supported_size(options->aead_key_random_size,
2077 cap->sym.aead.key_size.min,
2078 cap->sym.aead.key_size.max,
2079 cap->sym.aead.key_size.increment)
2081 printf("Unsupported aead key length\n");
2084 options->aead_xform.aead.key.length =
2085 options->aead_key_random_size;
2086 /* No size provided, use minimum size. */
2088 options->aead_xform.aead.key.length =
2089 cap->sym.aead.key_size.min;
2091 if (!options->aead_key_param)
2092 generate_random_key(
2093 options->aead_xform.aead.key.data,
2094 options->aead_xform.aead.key.length);
2097 * Check if length of provided AAD is supported
2098 * by the algorithm chosen.
2100 if (options->aad_param) {
2101 if (check_supported_size(options->aad.length,
2102 cap->sym.aead.aad_size.min,
2103 cap->sym.aead.aad_size.max,
2104 cap->sym.aead.aad_size.increment)
2106 printf("Unsupported AAD length\n");
2110 * Check if length of AAD to be randomly generated
2111 * is supported by the algorithm chosen.
2113 } else if (options->aad_random_size != -1) {
2114 if (check_supported_size(options->aad_random_size,
2115 cap->sym.aead.aad_size.min,
2116 cap->sym.aead.aad_size.max,
2117 cap->sym.aead.aad_size.increment)
2119 printf("Unsupported AAD length\n");
2122 options->aad.length = options->aad_random_size;
2123 /* No size provided, use minimum size. */
2125 options->aad.length = cap->sym.auth.aad_size.min;
2127 options->aead_xform.aead.aad_length =
2128 options->aad.length;
2130 /* Check if digest size is supported by the algorithm. */
2131 if (options->digest_size != -1) {
2132 if (check_supported_size(options->digest_size,
2133 cap->sym.aead.digest_size.min,
2134 cap->sym.aead.digest_size.max,
2135 cap->sym.aead.digest_size.increment)
2137 printf("Unsupported digest length\n");
2140 options->aead_xform.aead.digest_length =
2141 options->digest_size;
2142 /* No size provided, use minimum size. */
2144 options->aead_xform.aead.digest_length =
2145 cap->sym.aead.digest_size.min;
2148 /* Set cipher parameters */
2149 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2150 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2151 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2152 /* Check if device supports cipher algo */
2153 cap = check_device_support_cipher_algo(options, &dev_info,
2158 options->block_size = cap->sym.cipher.block_size;
2160 check_iv_param(&cap->sym.cipher.iv_size,
2161 options->cipher_iv_param,
2162 options->cipher_iv_random_size,
2163 &options->cipher_iv.length);
2166 * Check if length of provided cipher key is supported
2167 * by the algorithm chosen.
2169 if (options->ckey_param) {
2170 if (check_supported_size(
2171 options->cipher_xform.cipher.key.length,
2172 cap->sym.cipher.key_size.min,
2173 cap->sym.cipher.key_size.max,
2174 cap->sym.cipher.key_size.increment)
2176 printf("Unsupported cipher key length\n");
2180 * Check if length of the cipher key to be randomly generated
2181 * is supported by the algorithm chosen.
2183 } else if (options->ckey_random_size != -1) {
2184 if (check_supported_size(options->ckey_random_size,
2185 cap->sym.cipher.key_size.min,
2186 cap->sym.cipher.key_size.max,
2187 cap->sym.cipher.key_size.increment)
2189 printf("Unsupported cipher key length\n");
2192 options->cipher_xform.cipher.key.length =
2193 options->ckey_random_size;
2194 /* No size provided, use minimum size. */
2196 options->cipher_xform.cipher.key.length =
2197 cap->sym.cipher.key_size.min;
2199 if (!options->ckey_param)
2200 generate_random_key(
2201 options->cipher_xform.cipher.key.data,
2202 options->cipher_xform.cipher.key.length);
2206 /* Set auth parameters */
2207 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2208 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2209 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2210 /* Check if device supports auth algo */
2211 cap = check_device_support_auth_algo(options, &dev_info,
2216 check_iv_param(&cap->sym.auth.iv_size,
2217 options->auth_iv_param,
2218 options->auth_iv_random_size,
2219 &options->auth_iv.length);
2221 * Check if length of provided auth key is supported
2222 * by the algorithm chosen.
2224 if (options->akey_param) {
2225 if (check_supported_size(
2226 options->auth_xform.auth.key.length,
2227 cap->sym.auth.key_size.min,
2228 cap->sym.auth.key_size.max,
2229 cap->sym.auth.key_size.increment)
2231 printf("Unsupported auth key length\n");
2235 * Check if length of the auth key to be randomly generated
2236 * is supported by the algorithm chosen.
2238 } else if (options->akey_random_size != -1) {
2239 if (check_supported_size(options->akey_random_size,
2240 cap->sym.auth.key_size.min,
2241 cap->sym.auth.key_size.max,
2242 cap->sym.auth.key_size.increment)
2244 printf("Unsupported auth key length\n");
2247 options->auth_xform.auth.key.length =
2248 options->akey_random_size;
2249 /* No size provided, use minimum size. */
2251 options->auth_xform.auth.key.length =
2252 cap->sym.auth.key_size.min;
2254 if (!options->akey_param)
2255 generate_random_key(
2256 options->auth_xform.auth.key.data,
2257 options->auth_xform.auth.key.length);
2259 /* Check if digest size is supported by the algorithm. */
2260 if (options->digest_size != -1) {
2261 if (check_supported_size(options->digest_size,
2262 cap->sym.auth.digest_size.min,
2263 cap->sym.auth.digest_size.max,
2264 cap->sym.auth.digest_size.increment)
2266 printf("Unsupported digest length\n");
2269 options->auth_xform.auth.digest_length =
2270 options->digest_size;
2271 /* No size provided, use minimum size. */
2273 options->auth_xform.auth.digest_length =
2274 cap->sym.auth.digest_size.min;
2277 retval = rte_cryptodev_configure(cdev_id, &conf);
2279 printf("Failed to configure cryptodev %u", cdev_id);
2283 qp_conf.nb_descriptors = 2048;
2285 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2286 socket_id, session_pool_socket[socket_id]);
2288 printf("Failed to setup queue pair %u on cryptodev %u",
2293 retval = rte_cryptodev_start(cdev_id);
2295 printf("Failed to start device %u: error %d\n",
2300 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2302 enabled_cdevs[cdev_id] = 1;
2303 enabled_cdev_count++;
2306 return enabled_cdev_count;
2310 initialize_ports(struct l2fwd_crypto_options *options)
2312 uint16_t last_portid, portid;
2313 unsigned enabled_portcount = 0;
2314 unsigned nb_ports = rte_eth_dev_count();
2316 if (nb_ports == 0) {
2317 printf("No Ethernet ports - bye\n");
2321 /* Reset l2fwd_dst_ports */
2322 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2323 l2fwd_dst_ports[portid] = 0;
2325 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2327 struct rte_eth_dev_info dev_info;
2328 struct rte_eth_rxconf rxq_conf;
2329 struct rte_eth_txconf txq_conf;
2330 struct rte_eth_conf local_port_conf = port_conf;
2332 /* Skip ports that are not enabled */
2333 if ((options->portmask & (1 << portid)) == 0)
2337 printf("Initializing port %u... ", portid);
2339 rte_eth_dev_info_get(portid, &dev_info);
2340 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2341 local_port_conf.txmode.offloads |=
2342 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2343 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2345 printf("Cannot configure device: err=%d, port=%u\n",
2350 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2353 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2358 /* init one RX queue */
2360 rxq_conf = dev_info.default_rxconf;
2361 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2362 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2363 rte_eth_dev_socket_id(portid),
2364 &rxq_conf, l2fwd_pktmbuf_pool);
2366 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2371 /* init one TX queue on each port */
2373 txq_conf = dev_info.default_txconf;
2374 txq_conf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
2375 txq_conf.offloads = local_port_conf.txmode.offloads;
2376 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2377 rte_eth_dev_socket_id(portid),
2380 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2387 retval = rte_eth_dev_start(portid);
2389 printf("rte_eth_dev_start:err=%d, port=%u\n",
2394 rte_eth_promiscuous_enable(portid);
2396 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2398 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2400 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2401 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2402 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2403 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2404 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2405 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2407 /* initialize port stats */
2408 memset(&port_statistics, 0, sizeof(port_statistics));
2410 /* Setup port forwarding table */
2411 if (enabled_portcount % 2) {
2412 l2fwd_dst_ports[portid] = last_portid;
2413 l2fwd_dst_ports[last_portid] = portid;
2415 last_portid = portid;
2418 l2fwd_enabled_port_mask |= (1 << portid);
2419 enabled_portcount++;
2422 if (enabled_portcount == 1) {
2423 l2fwd_dst_ports[last_portid] = last_portid;
2424 } else if (enabled_portcount % 2) {
2425 printf("odd number of ports in portmask- bye\n");
2429 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2431 return enabled_portcount;
2435 reserve_key_memory(struct l2fwd_crypto_options *options)
2437 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2439 if (options->cipher_xform.cipher.key.data == NULL)
2440 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2442 options->auth_xform.auth.key.data = rte_malloc("auth key",
2444 if (options->auth_xform.auth.key.data == NULL)
2445 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2447 options->aead_xform.aead.key.data = rte_malloc("aead key",
2449 if (options->aead_xform.aead.key.data == NULL)
2450 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2452 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2453 if (options->cipher_iv.data == NULL)
2454 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2456 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2457 if (options->auth_iv.data == NULL)
2458 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2460 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2461 if (options->aead_iv.data == NULL)
2462 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2464 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2465 if (options->aad.data == NULL)
2466 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2467 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2471 main(int argc, char **argv)
2473 struct lcore_queue_conf *qconf;
2474 struct l2fwd_crypto_options options;
2476 uint8_t nb_cryptodevs, cdev_id;
2477 uint16_t nb_ports, portid;
2478 unsigned lcore_id, rx_lcore_id;
2479 int ret, enabled_cdevcount, enabled_portcount;
2480 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2483 ret = rte_eal_init(argc, argv);
2485 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2489 /* reserve memory for Cipher/Auth key and IV */
2490 reserve_key_memory(&options);
2492 /* parse application arguments (after the EAL ones) */
2493 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2495 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2497 printf("MAC updating %s\n",
2498 options.mac_updating ? "enabled" : "disabled");
2500 /* create the mbuf pool */
2501 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2502 sizeof(struct rte_crypto_op),
2503 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2504 if (l2fwd_pktmbuf_pool == NULL)
2505 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2507 /* create crypto op pool */
2508 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2509 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2511 if (l2fwd_crypto_op_pool == NULL)
2512 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2514 /* Enable Ethernet ports */
2515 enabled_portcount = initialize_ports(&options);
2516 if (enabled_portcount < 1)
2517 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2519 nb_ports = rte_eth_dev_count();
2520 /* Initialize the port/queue configuration of each logical core */
2521 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2522 portid < nb_ports; portid++) {
2524 /* skip ports that are not enabled */
2525 if ((options.portmask & (1 << portid)) == 0)
2528 if (options.single_lcore && qconf == NULL) {
2529 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2531 if (rx_lcore_id >= RTE_MAX_LCORE)
2532 rte_exit(EXIT_FAILURE,
2533 "Not enough cores\n");
2535 } else if (!options.single_lcore) {
2536 /* get the lcore_id for this port */
2537 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2538 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2539 options.nb_ports_per_lcore) {
2541 if (rx_lcore_id >= RTE_MAX_LCORE)
2542 rte_exit(EXIT_FAILURE,
2543 "Not enough cores\n");
2547 /* Assigned a new logical core in the loop above. */
2548 if (qconf != &lcore_queue_conf[rx_lcore_id])
2549 qconf = &lcore_queue_conf[rx_lcore_id];
2551 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2552 qconf->nb_rx_ports++;
2554 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2557 /* Enable Crypto devices */
2558 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2560 if (enabled_cdevcount < 0)
2561 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2563 if (enabled_cdevcount < enabled_portcount)
2564 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2565 "has to be more or equal to number of ports (%d)\n",
2566 enabled_cdevcount, enabled_portcount);
2568 nb_cryptodevs = rte_cryptodev_count();
2570 /* Initialize the port/cryptodev configuration of each logical core */
2571 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2572 cdev_id < nb_cryptodevs && enabled_cdevcount;
2574 /* Crypto op not supported by crypto device */
2575 if (!enabled_cdevs[cdev_id])
2578 if (options.single_lcore && qconf == NULL) {
2579 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2581 if (rx_lcore_id >= RTE_MAX_LCORE)
2582 rte_exit(EXIT_FAILURE,
2583 "Not enough cores\n");
2585 } else if (!options.single_lcore) {
2586 /* get the lcore_id for this port */
2587 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2588 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2589 options.nb_ports_per_lcore) {
2591 if (rx_lcore_id >= RTE_MAX_LCORE)
2592 rte_exit(EXIT_FAILURE,
2593 "Not enough cores\n");
2597 /* Assigned a new logical core in the loop above. */
2598 if (qconf != &lcore_queue_conf[rx_lcore_id])
2599 qconf = &lcore_queue_conf[rx_lcore_id];
2601 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2602 qconf->nb_crypto_devs++;
2604 enabled_cdevcount--;
2606 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2610 /* launch per-lcore init on every lcore */
2611 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2613 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2614 if (rte_eal_wait_lcore(lcore_id) < 0)