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 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
217 .mq_mode = ETH_MQ_TX_NONE,
221 struct rte_mempool *l2fwd_pktmbuf_pool;
222 struct rte_mempool *l2fwd_crypto_op_pool;
223 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
225 /* Per-port statistics struct */
226 struct l2fwd_port_statistics {
230 uint64_t crypto_enqueued;
231 uint64_t crypto_dequeued;
234 } __rte_cache_aligned;
236 struct l2fwd_crypto_statistics {
241 } __rte_cache_aligned;
243 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
244 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
246 /* A tsc-based timer responsible for triggering statistics printout */
247 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
248 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
250 /* default period is 10 seconds */
251 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
253 /* Print out statistics on packets dropped */
257 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
258 uint64_t total_packets_enqueued, total_packets_dequeued,
259 total_packets_errors;
263 total_packets_dropped = 0;
264 total_packets_tx = 0;
265 total_packets_rx = 0;
266 total_packets_enqueued = 0;
267 total_packets_dequeued = 0;
268 total_packets_errors = 0;
270 const char clr[] = { 27, '[', '2', 'J', '\0' };
271 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
273 /* Clear screen and move to top left */
274 printf("%s%s", clr, topLeft);
276 printf("\nPort statistics ====================================");
278 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
279 /* skip disabled ports */
280 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
282 printf("\nStatistics for port %u ------------------------------"
283 "\nPackets sent: %32"PRIu64
284 "\nPackets received: %28"PRIu64
285 "\nPackets dropped: %29"PRIu64,
287 port_statistics[portid].tx,
288 port_statistics[portid].rx,
289 port_statistics[portid].dropped);
291 total_packets_dropped += port_statistics[portid].dropped;
292 total_packets_tx += port_statistics[portid].tx;
293 total_packets_rx += port_statistics[portid].rx;
295 printf("\nCrypto statistics ==================================");
297 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
298 /* skip disabled ports */
299 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
301 printf("\nStatistics for cryptodev %"PRIu64
302 " -------------------------"
303 "\nPackets enqueued: %28"PRIu64
304 "\nPackets dequeued: %28"PRIu64
305 "\nPackets errors: %30"PRIu64,
307 crypto_statistics[cdevid].enqueued,
308 crypto_statistics[cdevid].dequeued,
309 crypto_statistics[cdevid].errors);
311 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
312 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
313 total_packets_errors += crypto_statistics[cdevid].errors;
315 printf("\nAggregate statistics ==============================="
316 "\nTotal packets received: %22"PRIu64
317 "\nTotal packets enqueued: %22"PRIu64
318 "\nTotal packets dequeued: %22"PRIu64
319 "\nTotal packets sent: %26"PRIu64
320 "\nTotal packets dropped: %23"PRIu64
321 "\nTotal packets crypto errors: %17"PRIu64,
323 total_packets_enqueued,
324 total_packets_dequeued,
326 total_packets_dropped,
327 total_packets_errors);
328 printf("\n====================================================\n");
332 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
333 struct l2fwd_crypto_params *cparams)
335 struct rte_crypto_op **op_buffer;
338 op_buffer = (struct rte_crypto_op **)
339 qconf->op_buf[cparams->dev_id].buffer;
341 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
342 cparams->qp_id, op_buffer, (uint16_t) n);
344 crypto_statistics[cparams->dev_id].enqueued += ret;
345 if (unlikely(ret < n)) {
346 crypto_statistics[cparams->dev_id].errors += (n - ret);
348 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
349 rte_crypto_op_free(op_buffer[ret]);
357 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
358 struct l2fwd_crypto_params *cparams)
360 unsigned lcore_id, len;
361 struct lcore_queue_conf *qconf;
363 lcore_id = rte_lcore_id();
365 qconf = &lcore_queue_conf[lcore_id];
366 len = qconf->op_buf[cparams->dev_id].len;
367 qconf->op_buf[cparams->dev_id].buffer[len] = op;
370 /* enough ops to be sent */
371 if (len == MAX_PKT_BURST) {
372 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
376 qconf->op_buf[cparams->dev_id].len = len;
381 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
382 struct rte_crypto_op *op,
383 struct l2fwd_crypto_params *cparams)
385 struct ether_hdr *eth_hdr;
386 struct ipv4_hdr *ip_hdr;
388 uint32_t ipdata_offset, data_len;
389 uint32_t pad_len = 0;
392 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
394 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
397 ipdata_offset = sizeof(struct ether_hdr);
399 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
402 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
403 * IPV4_IHL_MULTIPLIER;
406 /* Zero pad data to be crypto'd so it is block aligned */
407 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
409 if (cparams->do_hash && cparams->hash_verify)
410 data_len -= cparams->digest_length;
412 if (cparams->do_cipher) {
414 * Following algorithms are block cipher algorithms,
415 * and might need padding
417 switch (cparams->cipher_algo) {
418 case RTE_CRYPTO_CIPHER_AES_CBC:
419 case RTE_CRYPTO_CIPHER_AES_ECB:
420 case RTE_CRYPTO_CIPHER_DES_CBC:
421 case RTE_CRYPTO_CIPHER_3DES_CBC:
422 case RTE_CRYPTO_CIPHER_3DES_ECB:
423 if (data_len % cparams->block_size)
424 pad_len = cparams->block_size -
425 (data_len % cparams->block_size);
432 padding = rte_pktmbuf_append(m, pad_len);
433 if (unlikely(!padding))
437 memset(padding, 0, pad_len);
441 /* Set crypto operation data parameters */
442 rte_crypto_op_attach_sym_session(op, cparams->session);
444 if (cparams->do_hash) {
445 if (cparams->auth_iv.length) {
446 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
449 cparams->cipher_iv.length);
451 * Copy IV at the end of the crypto operation,
452 * after the cipher IV, if added
454 rte_memcpy(iv_ptr, cparams->auth_iv.data,
455 cparams->auth_iv.length);
457 if (!cparams->hash_verify) {
458 /* Append space for digest to end of packet */
459 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
460 cparams->digest_length);
462 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
463 uint8_t *) + ipdata_offset + data_len;
466 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
467 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
469 /* For wireless algorithms, offset/length must be in bits */
470 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
471 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
472 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
473 op->sym->auth.data.offset = ipdata_offset << 3;
474 op->sym->auth.data.length = data_len << 3;
476 op->sym->auth.data.offset = ipdata_offset;
477 op->sym->auth.data.length = data_len;
481 if (cparams->do_cipher) {
482 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
484 /* Copy IV at the end of the crypto operation */
485 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
486 cparams->cipher_iv.length);
488 /* For wireless algorithms, offset/length must be in bits */
489 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
490 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
491 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
492 op->sym->cipher.data.offset = ipdata_offset << 3;
493 op->sym->cipher.data.length = data_len << 3;
495 op->sym->cipher.data.offset = ipdata_offset;
496 op->sym->cipher.data.length = data_len;
500 if (cparams->do_aead) {
501 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
503 /* Copy IV at the end of the crypto operation */
505 * If doing AES-CCM, nonce is copied one byte
506 * after the start of IV field
508 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
509 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
510 cparams->aead_iv.length);
512 rte_memcpy(iv_ptr, cparams->aead_iv.data,
513 cparams->aead_iv.length);
515 op->sym->aead.data.offset = ipdata_offset;
516 op->sym->aead.data.length = data_len;
518 if (!cparams->hash_verify) {
519 /* Append space for digest to end of packet */
520 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
521 cparams->digest_length);
523 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
524 uint8_t *) + ipdata_offset + data_len;
527 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
528 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
530 if (cparams->aad.length) {
531 op->sym->aead.aad.data = cparams->aad.data;
532 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
538 return l2fwd_crypto_enqueue(op, cparams);
542 /* Send the burst of packets on an output interface */
544 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
547 struct rte_mbuf **pkt_buffer;
550 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
552 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
553 port_statistics[port].tx += ret;
554 if (unlikely(ret < n)) {
555 port_statistics[port].dropped += (n - ret);
557 rte_pktmbuf_free(pkt_buffer[ret]);
564 /* Enqueue packets for TX and prepare them to be sent */
566 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
568 unsigned lcore_id, len;
569 struct lcore_queue_conf *qconf;
571 lcore_id = rte_lcore_id();
573 qconf = &lcore_queue_conf[lcore_id];
574 len = qconf->pkt_buf[port].len;
575 qconf->pkt_buf[port].buffer[len] = m;
578 /* enough pkts to be sent */
579 if (unlikely(len == MAX_PKT_BURST)) {
580 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
584 qconf->pkt_buf[port].len = len;
589 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
591 struct ether_hdr *eth;
594 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
596 /* 02:00:00:00:00:xx */
597 tmp = ð->d_addr.addr_bytes[0];
598 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
601 ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
605 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
606 struct l2fwd_crypto_options *options)
610 dst_port = l2fwd_dst_ports[portid];
612 if (options->mac_updating)
613 l2fwd_mac_updating(m, dst_port);
615 l2fwd_send_packet(m, dst_port);
618 /** Generate random key */
620 generate_random_key(uint8_t *key, unsigned length)
625 fd = open("/dev/urandom", O_RDONLY);
627 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
629 ret = read(fd, key, length);
632 if (ret != (signed)length)
633 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
636 static struct rte_cryptodev_sym_session *
637 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
639 struct rte_crypto_sym_xform *first_xform;
640 struct rte_cryptodev_sym_session *session;
641 int retval = rte_cryptodev_socket_id(cdev_id);
646 uint8_t socket_id = (uint8_t) retval;
647 struct rte_mempool *sess_mp = session_pool_socket[socket_id];
649 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
650 first_xform = &options->aead_xform;
651 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
652 first_xform = &options->cipher_xform;
653 first_xform->next = &options->auth_xform;
654 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
655 first_xform = &options->auth_xform;
656 first_xform->next = &options->cipher_xform;
657 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
658 first_xform = &options->cipher_xform;
660 first_xform = &options->auth_xform;
663 session = rte_cryptodev_sym_session_create(sess_mp);
668 if (rte_cryptodev_sym_session_init(cdev_id, session,
669 first_xform, sess_mp) < 0)
676 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
678 /* main processing loop */
680 l2fwd_main_loop(struct l2fwd_crypto_options *options)
682 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
683 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
685 unsigned lcore_id = rte_lcore_id();
686 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
687 unsigned int i, j, nb_rx, len;
689 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
690 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
691 US_PER_S * BURST_TX_DRAIN_US;
692 struct l2fwd_crypto_params *cparams;
693 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
694 struct rte_cryptodev_sym_session *session;
696 if (qconf->nb_rx_ports == 0) {
697 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
701 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
703 for (i = 0; i < qconf->nb_rx_ports; i++) {
705 portid = qconf->rx_port_list[i];
706 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
710 for (i = 0; i < qconf->nb_crypto_devs; i++) {
711 port_cparams[i].do_cipher = 0;
712 port_cparams[i].do_hash = 0;
713 port_cparams[i].do_aead = 0;
715 switch (options->xform_chain) {
716 case L2FWD_CRYPTO_AEAD:
717 port_cparams[i].do_aead = 1;
719 case L2FWD_CRYPTO_CIPHER_HASH:
720 case L2FWD_CRYPTO_HASH_CIPHER:
721 port_cparams[i].do_cipher = 1;
722 port_cparams[i].do_hash = 1;
724 case L2FWD_CRYPTO_HASH_ONLY:
725 port_cparams[i].do_hash = 1;
727 case L2FWD_CRYPTO_CIPHER_ONLY:
728 port_cparams[i].do_cipher = 1;
732 port_cparams[i].dev_id = qconf->cryptodev_list[i];
733 port_cparams[i].qp_id = 0;
735 port_cparams[i].block_size = options->block_size;
737 if (port_cparams[i].do_hash) {
738 port_cparams[i].auth_iv.data = options->auth_iv.data;
739 port_cparams[i].auth_iv.length = options->auth_iv.length;
740 if (!options->auth_iv_param)
741 generate_random_key(port_cparams[i].auth_iv.data,
742 port_cparams[i].auth_iv.length);
743 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
744 port_cparams[i].hash_verify = 1;
746 port_cparams[i].hash_verify = 0;
748 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
749 port_cparams[i].digest_length =
750 options->auth_xform.auth.digest_length;
751 /* Set IV parameters */
752 if (options->auth_iv.length) {
753 options->auth_xform.auth.iv.offset =
754 IV_OFFSET + options->cipher_iv.length;
755 options->auth_xform.auth.iv.length =
756 options->auth_iv.length;
760 if (port_cparams[i].do_aead) {
761 port_cparams[i].aead_iv.data = options->aead_iv.data;
762 port_cparams[i].aead_iv.length = options->aead_iv.length;
763 if (!options->aead_iv_param)
764 generate_random_key(port_cparams[i].aead_iv.data,
765 port_cparams[i].aead_iv.length);
766 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
767 port_cparams[i].digest_length =
768 options->aead_xform.aead.digest_length;
769 if (options->aead_xform.aead.aad_length) {
770 port_cparams[i].aad.data = options->aad.data;
771 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
772 port_cparams[i].aad.length = options->aad.length;
773 if (!options->aad_param)
774 generate_random_key(port_cparams[i].aad.data,
775 port_cparams[i].aad.length);
777 * If doing AES-CCM, first 18 bytes has to be reserved,
778 * and actual AAD should start from byte 18
780 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
781 memmove(port_cparams[i].aad.data + 18,
782 port_cparams[i].aad.data,
783 port_cparams[i].aad.length);
786 port_cparams[i].aad.length = 0;
788 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
789 port_cparams[i].hash_verify = 1;
791 port_cparams[i].hash_verify = 0;
793 /* Set IV parameters */
794 options->aead_xform.aead.iv.offset = IV_OFFSET;
795 options->aead_xform.aead.iv.length = options->aead_iv.length;
798 if (port_cparams[i].do_cipher) {
799 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
800 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
801 if (!options->cipher_iv_param)
802 generate_random_key(port_cparams[i].cipher_iv.data,
803 port_cparams[i].cipher_iv.length);
805 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
806 /* Set IV parameters */
807 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
808 options->cipher_xform.cipher.iv.length =
809 options->cipher_iv.length;
812 session = initialize_crypto_session(options,
813 port_cparams[i].dev_id);
815 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
817 port_cparams[i].session = session;
819 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
820 port_cparams[i].dev_id);
823 l2fwd_crypto_options_print(options);
826 * Initialize previous tsc timestamp before the loop,
827 * to avoid showing the port statistics immediately,
828 * so user can see the crypto information.
830 prev_tsc = rte_rdtsc();
833 cur_tsc = rte_rdtsc();
836 * Crypto device/TX burst queue drain
838 diff_tsc = cur_tsc - prev_tsc;
839 if (unlikely(diff_tsc > drain_tsc)) {
840 /* Enqueue all crypto ops remaining in buffers */
841 for (i = 0; i < qconf->nb_crypto_devs; i++) {
842 cparams = &port_cparams[i];
843 len = qconf->op_buf[cparams->dev_id].len;
844 l2fwd_crypto_send_burst(qconf, len, cparams);
845 qconf->op_buf[cparams->dev_id].len = 0;
847 /* Transmit all packets remaining in buffers */
848 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
849 if (qconf->pkt_buf[portid].len == 0)
851 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
852 qconf->pkt_buf[portid].len,
854 qconf->pkt_buf[portid].len = 0;
857 /* if timer is enabled */
858 if (timer_period > 0) {
860 /* advance the timer */
861 timer_tsc += diff_tsc;
863 /* if timer has reached its timeout */
864 if (unlikely(timer_tsc >=
865 (uint64_t)timer_period)) {
867 /* do this only on master core */
868 if (lcore_id == rte_get_master_lcore()
869 && options->refresh_period) {
880 * Read packet from RX queues
882 for (i = 0; i < qconf->nb_rx_ports; i++) {
883 portid = qconf->rx_port_list[i];
885 cparams = &port_cparams[i];
887 nb_rx = rte_eth_rx_burst(portid, 0,
888 pkts_burst, MAX_PKT_BURST);
890 port_statistics[portid].rx += nb_rx;
894 * If we can't allocate a crypto_ops, then drop
895 * the rest of the burst and dequeue and
896 * process the packets to free offload structs
898 if (rte_crypto_op_bulk_alloc(
899 l2fwd_crypto_op_pool,
900 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
903 for (j = 0; j < nb_rx; j++)
904 rte_pktmbuf_free(pkts_burst[j]);
909 /* Enqueue packets from Crypto device*/
910 for (j = 0; j < nb_rx; j++) {
913 l2fwd_simple_crypto_enqueue(m,
914 ops_burst[j], cparams);
918 /* Dequeue packets from Crypto device */
920 nb_rx = rte_cryptodev_dequeue_burst(
921 cparams->dev_id, cparams->qp_id,
922 ops_burst, MAX_PKT_BURST);
924 crypto_statistics[cparams->dev_id].dequeued +=
927 /* Forward crypto'd packets */
928 for (j = 0; j < nb_rx; j++) {
929 m = ops_burst[j]->sym->m_src;
931 rte_crypto_op_free(ops_burst[j]);
932 l2fwd_simple_forward(m, portid,
935 } while (nb_rx == MAX_PKT_BURST);
941 l2fwd_launch_one_lcore(void *arg)
943 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
947 /* Display command line arguments usage */
949 l2fwd_crypto_usage(const char *prgname)
951 printf("%s [EAL options] --\n"
952 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
953 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
954 " -s manage all ports from single lcore\n"
955 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
956 " (0 to disable, 10 default, 86400 maximum)\n"
958 " --cdev_type HW / SW / ANY\n"
959 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
960 " HASH_ONLY / AEAD\n"
962 " --cipher_algo ALGO\n"
963 " --cipher_op ENCRYPT / DECRYPT\n"
964 " --cipher_key KEY (bytes separated with \":\")\n"
965 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
966 " --cipher_iv IV (bytes separated with \":\")\n"
967 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
969 " --auth_algo ALGO\n"
970 " --auth_op GENERATE / VERIFY\n"
971 " --auth_key KEY (bytes separated with \":\")\n"
972 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
973 " --auth_iv IV (bytes separated with \":\")\n"
974 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
976 " --aead_algo ALGO\n"
977 " --aead_op ENCRYPT / DECRYPT\n"
978 " --aead_key KEY (bytes separated with \":\")\n"
979 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
980 " --aead_iv IV (bytes separated with \":\")\n"
981 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
982 " --aad AAD (bytes separated with \":\")\n"
983 " --aad_random_size SIZE: size of AAD when generated randomly\n"
985 " --digest_size SIZE: size of digest to be generated/verified\n"
988 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
990 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
992 " - The source MAC address is replaced by the TX port MAC address\n"
993 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
997 /** Parse crypto device type command line argument */
999 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1001 if (strcmp("HW", optarg) == 0) {
1002 *type = CDEV_TYPE_HW;
1004 } else if (strcmp("SW", optarg) == 0) {
1005 *type = CDEV_TYPE_SW;
1007 } else if (strcmp("ANY", optarg) == 0) {
1008 *type = CDEV_TYPE_ANY;
1015 /** Parse crypto chain xform command line argument */
1017 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1019 if (strcmp("CIPHER_HASH", optarg) == 0) {
1020 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1022 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1023 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1025 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1026 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1028 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1029 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1031 } else if (strcmp("AEAD", optarg) == 0) {
1032 options->xform_chain = L2FWD_CRYPTO_AEAD;
1039 /** Parse crypto cipher algo option command line argument */
1041 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1044 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1045 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1046 "not supported!\n");
1053 /** Parse crypto cipher operation command line argument */
1055 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1057 if (strcmp("ENCRYPT", optarg) == 0) {
1058 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1060 } else if (strcmp("DECRYPT", optarg) == 0) {
1061 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1065 printf("Cipher operation not supported!\n");
1069 /** Parse bytes from command line argument */
1071 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1073 unsigned byte_count;
1077 for (byte_count = 0, token = strtok(input_arg, ":");
1078 (byte_count < max_size) && (token != NULL);
1079 token = strtok(NULL, ":")) {
1081 int number = (int)strtol(token, NULL, 16);
1083 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1086 data[byte_count++] = (uint8_t)number;
1092 /** Parse size param*/
1094 parse_size(int *size, const char *q_arg)
1099 /* parse hexadecimal string */
1100 n = strtoul(q_arg, &end, 10);
1101 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1105 printf("invalid size\n");
1113 /** Parse crypto cipher operation command line argument */
1115 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1117 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1118 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1119 "not supported!\n");
1127 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1129 if (strcmp("VERIFY", optarg) == 0) {
1130 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1132 } else if (strcmp("GENERATE", optarg) == 0) {
1133 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1137 printf("Authentication operation specified not supported!\n");
1142 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1144 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1145 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1146 "not supported!\n");
1154 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1156 if (strcmp("ENCRYPT", optarg) == 0) {
1157 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1159 } else if (strcmp("DECRYPT", optarg) == 0) {
1160 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1164 printf("AEAD operation specified not supported!\n");
1168 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1174 /* parse hexadecimal string */
1175 pm = strtoul(q_arg, &end, 16);
1176 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1179 options->cryptodev_mask = pm;
1180 if (options->cryptodev_mask == 0) {
1181 printf("invalid cryptodev_mask specified\n");
1188 /** Parse long options */
1190 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1191 struct option *lgopts, int option_index)
1195 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1196 retval = parse_cryptodev_type(&options->type, optarg);
1198 snprintf(options->string_type, MAX_STR_LEN,
1203 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1204 return parse_crypto_opt_chain(options, optarg);
1206 /* Cipher options */
1207 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1208 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1211 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1212 return parse_cipher_op(&options->cipher_xform.cipher.op,
1215 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1216 options->ckey_param = 1;
1217 options->cipher_xform.cipher.key.length =
1218 parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1220 if (options->cipher_xform.cipher.key.length > 0)
1226 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1227 return parse_size(&options->ckey_random_size, optarg);
1229 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1230 options->cipher_iv_param = 1;
1231 options->cipher_iv.length =
1232 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1233 if (options->cipher_iv.length > 0)
1239 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1240 return parse_size(&options->cipher_iv_random_size, optarg);
1242 /* Authentication options */
1243 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1244 return parse_auth_algo(&options->auth_xform.auth.algo,
1248 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1249 return parse_auth_op(&options->auth_xform.auth.op,
1252 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1253 options->akey_param = 1;
1254 options->auth_xform.auth.key.length =
1255 parse_bytes(options->auth_xform.auth.key.data, optarg,
1257 if (options->auth_xform.auth.key.length > 0)
1263 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1264 return parse_size(&options->akey_random_size, optarg);
1267 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1268 options->auth_iv_param = 1;
1269 options->auth_iv.length =
1270 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1271 if (options->auth_iv.length > 0)
1277 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1278 return parse_size(&options->auth_iv_random_size, optarg);
1281 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1282 return parse_aead_algo(&options->aead_xform.aead.algo,
1286 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1287 return parse_aead_op(&options->aead_xform.aead.op,
1290 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1291 options->aead_key_param = 1;
1292 options->aead_xform.aead.key.length =
1293 parse_bytes(options->aead_xform.aead.key.data, optarg,
1295 if (options->aead_xform.aead.key.length > 0)
1301 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1302 return parse_size(&options->aead_key_random_size, optarg);
1305 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1306 options->aead_iv_param = 1;
1307 options->aead_iv.length =
1308 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1309 if (options->aead_iv.length > 0)
1315 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1316 return parse_size(&options->aead_iv_random_size, optarg);
1318 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1319 options->aad_param = 1;
1320 options->aad.length =
1321 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1322 if (options->aad.length > 0)
1328 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1329 return parse_size(&options->aad_random_size, optarg);
1332 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1333 return parse_size(&options->digest_size, optarg);
1336 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1337 options->sessionless = 1;
1341 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1342 return parse_cryptodev_mask(options, optarg);
1344 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1345 options->mac_updating = 1;
1349 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1350 options->mac_updating = 0;
1357 /** Parse port mask */
1359 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1365 /* parse hexadecimal string */
1366 pm = strtoul(q_arg, &end, 16);
1367 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1370 options->portmask = pm;
1371 if (options->portmask == 0) {
1372 printf("invalid portmask specified\n");
1379 /** Parse number of queues */
1381 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1387 /* parse hexadecimal string */
1388 n = strtoul(q_arg, &end, 10);
1389 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1391 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1394 options->nb_ports_per_lcore = n;
1395 if (options->nb_ports_per_lcore == 0) {
1396 printf("invalid number of ports selected\n");
1403 /** Parse timer period */
1405 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1411 /* parse number string */
1412 n = (unsigned)strtol(q_arg, &end, 10);
1413 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1416 if (n >= MAX_TIMER_PERIOD) {
1417 printf("Warning refresh period specified %lu is greater than "
1418 "max value %lu! using max value",
1419 n, MAX_TIMER_PERIOD);
1420 n = MAX_TIMER_PERIOD;
1423 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1428 /** Generate default options for application */
1430 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1432 options->portmask = 0xffffffff;
1433 options->nb_ports_per_lcore = 1;
1434 options->refresh_period = 10000;
1435 options->single_lcore = 0;
1436 options->sessionless = 0;
1438 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1441 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1442 options->cipher_xform.next = NULL;
1443 options->ckey_param = 0;
1444 options->ckey_random_size = -1;
1445 options->cipher_xform.cipher.key.length = 0;
1446 options->cipher_iv_param = 0;
1447 options->cipher_iv_random_size = -1;
1448 options->cipher_iv.length = 0;
1450 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1451 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1453 /* Authentication Data */
1454 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1455 options->auth_xform.next = NULL;
1456 options->akey_param = 0;
1457 options->akey_random_size = -1;
1458 options->auth_xform.auth.key.length = 0;
1459 options->auth_iv_param = 0;
1460 options->auth_iv_random_size = -1;
1461 options->auth_iv.length = 0;
1463 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1464 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1467 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1468 options->aead_xform.next = NULL;
1469 options->aead_key_param = 0;
1470 options->aead_key_random_size = -1;
1471 options->aead_xform.aead.key.length = 0;
1472 options->aead_iv_param = 0;
1473 options->aead_iv_random_size = -1;
1474 options->aead_iv.length = 0;
1476 options->aead_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1477 options->aead_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1479 options->aad_param = 0;
1480 options->aad_random_size = -1;
1481 options->aad.length = 0;
1483 options->digest_size = -1;
1485 options->type = CDEV_TYPE_ANY;
1486 options->cryptodev_mask = UINT64_MAX;
1488 options->mac_updating = 1;
1492 display_cipher_info(struct l2fwd_crypto_options *options)
1494 printf("\n---- Cipher information ---\n");
1495 printf("Algorithm: %s\n",
1496 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1497 rte_hexdump(stdout, "Cipher key:",
1498 options->cipher_xform.cipher.key.data,
1499 options->cipher_xform.cipher.key.length);
1500 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1504 display_auth_info(struct l2fwd_crypto_options *options)
1506 printf("\n---- Authentication information ---\n");
1507 printf("Algorithm: %s\n",
1508 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1509 rte_hexdump(stdout, "Auth key:",
1510 options->auth_xform.auth.key.data,
1511 options->auth_xform.auth.key.length);
1512 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1516 display_aead_info(struct l2fwd_crypto_options *options)
1518 printf("\n---- AEAD information ---\n");
1519 printf("Algorithm: %s\n",
1520 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1521 rte_hexdump(stdout, "AEAD key:",
1522 options->aead_xform.aead.key.data,
1523 options->aead_xform.aead.key.length);
1524 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1525 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1529 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1531 char string_cipher_op[MAX_STR_LEN];
1532 char string_auth_op[MAX_STR_LEN];
1533 char string_aead_op[MAX_STR_LEN];
1535 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1536 strcpy(string_cipher_op, "Encrypt");
1538 strcpy(string_cipher_op, "Decrypt");
1540 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1541 strcpy(string_auth_op, "Auth generate");
1543 strcpy(string_auth_op, "Auth verify");
1545 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1546 strcpy(string_aead_op, "Authenticated encryption");
1548 strcpy(string_aead_op, "Authenticated decryption");
1551 printf("Options:-\nn");
1552 printf("portmask: %x\n", options->portmask);
1553 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1554 printf("refresh period : %u\n", options->refresh_period);
1555 printf("single lcore mode: %s\n",
1556 options->single_lcore ? "enabled" : "disabled");
1557 printf("stats_printing: %s\n",
1558 options->refresh_period == 0 ? "disabled" : "enabled");
1560 printf("sessionless crypto: %s\n",
1561 options->sessionless ? "enabled" : "disabled");
1563 if (options->ckey_param && (options->ckey_random_size != -1))
1564 printf("Cipher key already parsed, ignoring size of random key\n");
1566 if (options->akey_param && (options->akey_random_size != -1))
1567 printf("Auth key already parsed, ignoring size of random key\n");
1569 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1570 printf("Cipher IV already parsed, ignoring size of random IV\n");
1572 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1573 printf("Auth IV already parsed, ignoring size of random IV\n");
1575 if (options->aad_param && (options->aad_random_size != -1))
1576 printf("AAD already parsed, ignoring size of random AAD\n");
1578 printf("\nCrypto chain: ");
1579 switch (options->xform_chain) {
1580 case L2FWD_CRYPTO_AEAD:
1581 printf("Input --> %s --> Output\n", string_aead_op);
1582 display_aead_info(options);
1584 case L2FWD_CRYPTO_CIPHER_HASH:
1585 printf("Input --> %s --> %s --> Output\n",
1586 string_cipher_op, string_auth_op);
1587 display_cipher_info(options);
1588 display_auth_info(options);
1590 case L2FWD_CRYPTO_HASH_CIPHER:
1591 printf("Input --> %s --> %s --> Output\n",
1592 string_auth_op, string_cipher_op);
1593 display_cipher_info(options);
1594 display_auth_info(options);
1596 case L2FWD_CRYPTO_HASH_ONLY:
1597 printf("Input --> %s --> Output\n", string_auth_op);
1598 display_auth_info(options);
1600 case L2FWD_CRYPTO_CIPHER_ONLY:
1601 printf("Input --> %s --> Output\n", string_cipher_op);
1602 display_cipher_info(options);
1607 /* Parse the argument given in the command line of the application */
1609 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1610 int argc, char **argv)
1612 int opt, retval, option_index;
1613 char **argvopt = argv, *prgname = argv[0];
1615 static struct option lgopts[] = {
1616 { "sessionless", no_argument, 0, 0 },
1618 { "cdev_type", required_argument, 0, 0 },
1619 { "chain", required_argument, 0, 0 },
1621 { "cipher_algo", required_argument, 0, 0 },
1622 { "cipher_op", required_argument, 0, 0 },
1623 { "cipher_key", required_argument, 0, 0 },
1624 { "cipher_key_random_size", required_argument, 0, 0 },
1625 { "cipher_iv", required_argument, 0, 0 },
1626 { "cipher_iv_random_size", required_argument, 0, 0 },
1628 { "auth_algo", required_argument, 0, 0 },
1629 { "auth_op", required_argument, 0, 0 },
1630 { "auth_key", required_argument, 0, 0 },
1631 { "auth_key_random_size", required_argument, 0, 0 },
1632 { "auth_iv", required_argument, 0, 0 },
1633 { "auth_iv_random_size", required_argument, 0, 0 },
1635 { "aead_algo", required_argument, 0, 0 },
1636 { "aead_op", required_argument, 0, 0 },
1637 { "aead_key", required_argument, 0, 0 },
1638 { "aead_key_random_size", required_argument, 0, 0 },
1639 { "aead_iv", required_argument, 0, 0 },
1640 { "aead_iv_random_size", required_argument, 0, 0 },
1642 { "aad", required_argument, 0, 0 },
1643 { "aad_random_size", required_argument, 0, 0 },
1645 { "digest_size", required_argument, 0, 0 },
1647 { "sessionless", no_argument, 0, 0 },
1648 { "cryptodev_mask", required_argument, 0, 0},
1650 { "mac-updating", no_argument, 0, 0},
1651 { "no-mac-updating", no_argument, 0, 0},
1656 l2fwd_crypto_default_options(options);
1658 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1659 &option_index)) != EOF) {
1663 retval = l2fwd_crypto_parse_args_long_options(options,
1664 lgopts, option_index);
1666 l2fwd_crypto_usage(prgname);
1673 retval = l2fwd_crypto_parse_portmask(options, optarg);
1675 l2fwd_crypto_usage(prgname);
1682 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1684 l2fwd_crypto_usage(prgname);
1691 options->single_lcore = 1;
1697 retval = l2fwd_crypto_parse_timer_period(options,
1700 l2fwd_crypto_usage(prgname);
1706 l2fwd_crypto_usage(prgname);
1713 argv[optind-1] = prgname;
1716 optind = 1; /* reset getopt lib */
1721 /* Check the link status of all ports in up to 9s, and print them finally */
1723 check_all_ports_link_status(uint32_t port_mask)
1725 #define CHECK_INTERVAL 100 /* 100ms */
1726 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1728 uint8_t count, all_ports_up, print_flag = 0;
1729 struct rte_eth_link link;
1731 printf("\nChecking link status");
1733 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1735 RTE_ETH_FOREACH_DEV(portid) {
1736 if ((port_mask & (1 << portid)) == 0)
1738 memset(&link, 0, sizeof(link));
1739 rte_eth_link_get_nowait(portid, &link);
1740 /* print link status if flag set */
1741 if (print_flag == 1) {
1742 if (link.link_status)
1744 "Port%d Link Up. Speed %u Mbps - %s\n",
1745 portid, link.link_speed,
1746 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1747 ("full-duplex") : ("half-duplex\n"));
1749 printf("Port %d Link Down\n", portid);
1752 /* clear all_ports_up flag if any link down */
1753 if (link.link_status == ETH_LINK_DOWN) {
1758 /* after finally printing all link status, get out */
1759 if (print_flag == 1)
1762 if (all_ports_up == 0) {
1765 rte_delay_ms(CHECK_INTERVAL);
1768 /* set the print_flag if all ports up or timeout */
1769 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1776 /* Check if device has to be HW/SW or any */
1778 check_type(const struct l2fwd_crypto_options *options,
1779 const struct rte_cryptodev_info *dev_info)
1781 if (options->type == CDEV_TYPE_HW &&
1782 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1784 if (options->type == CDEV_TYPE_SW &&
1785 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1787 if (options->type == CDEV_TYPE_ANY)
1793 static const struct rte_cryptodev_capabilities *
1794 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1795 const struct rte_cryptodev_info *dev_info,
1799 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1800 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1801 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1802 options->cipher_xform.cipher.algo;
1804 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1805 cap_cipher_algo = cap->sym.cipher.algo;
1806 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1807 if (cap_cipher_algo == opt_cipher_algo) {
1808 if (check_type(options, dev_info) == 0)
1812 cap = &dev_info->capabilities[++i];
1815 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1816 printf("Algorithm %s not supported by cryptodev %u"
1817 " or device not of preferred type (%s)\n",
1818 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1820 options->string_type);
1827 static const struct rte_cryptodev_capabilities *
1828 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1829 const struct rte_cryptodev_info *dev_info,
1833 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1834 enum rte_crypto_auth_algorithm cap_auth_algo;
1835 enum rte_crypto_auth_algorithm opt_auth_algo =
1836 options->auth_xform.auth.algo;
1838 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1839 cap_auth_algo = cap->sym.auth.algo;
1840 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1841 if (cap_auth_algo == opt_auth_algo) {
1842 if (check_type(options, dev_info) == 0)
1846 cap = &dev_info->capabilities[++i];
1849 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1850 printf("Algorithm %s not supported by cryptodev %u"
1851 " or device not of preferred type (%s)\n",
1852 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1854 options->string_type);
1861 static const struct rte_cryptodev_capabilities *
1862 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1863 const struct rte_cryptodev_info *dev_info,
1867 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1868 enum rte_crypto_aead_algorithm cap_aead_algo;
1869 enum rte_crypto_aead_algorithm opt_aead_algo =
1870 options->aead_xform.aead.algo;
1872 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1873 cap_aead_algo = cap->sym.aead.algo;
1874 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1875 if (cap_aead_algo == opt_aead_algo) {
1876 if (check_type(options, dev_info) == 0)
1880 cap = &dev_info->capabilities[++i];
1883 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1884 printf("Algorithm %s not supported by cryptodev %u"
1885 " or device not of preferred type (%s)\n",
1886 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1888 options->string_type);
1895 /* Check if the device is enabled by cryptodev_mask */
1897 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1900 if (options->cryptodev_mask & (1 << cdev_id))
1907 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1913 if (increment == 0) {
1920 /* Range of values */
1921 for (supp_size = min; supp_size <= max; supp_size += increment) {
1922 if (length == supp_size)
1930 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1931 unsigned int iv_param, int iv_random_size,
1932 uint16_t *iv_length)
1935 * Check if length of provided IV is supported
1936 * by the algorithm chosen.
1939 if (check_supported_size(*iv_length,
1942 iv_range_size->increment)
1944 printf("Unsupported IV length\n");
1948 * Check if length of IV to be randomly generated
1949 * is supported by the algorithm chosen.
1951 } else if (iv_random_size != -1) {
1952 if (check_supported_size(iv_random_size,
1955 iv_range_size->increment)
1957 printf("Unsupported IV length\n");
1960 *iv_length = iv_random_size;
1961 /* No size provided, use minimum size. */
1963 *iv_length = iv_range_size->min;
1969 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1970 uint8_t *enabled_cdevs)
1972 unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
1973 const struct rte_cryptodev_capabilities *cap;
1974 unsigned int sess_sz, max_sess_sz = 0;
1977 cdev_count = rte_cryptodev_count();
1978 if (cdev_count == 0) {
1979 printf("No crypto devices available\n");
1983 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
1984 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
1985 if (sess_sz > max_sess_sz)
1986 max_sess_sz = sess_sz;
1989 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1991 struct rte_cryptodev_qp_conf qp_conf;
1992 struct rte_cryptodev_info dev_info;
1993 retval = rte_cryptodev_socket_id(cdev_id);
1996 printf("Invalid crypto device id used\n");
2000 uint8_t socket_id = (uint8_t) retval;
2002 struct rte_cryptodev_config conf = {
2003 .nb_queue_pairs = 1,
2004 .socket_id = socket_id,
2007 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
2010 rte_cryptodev_info_get(cdev_id, &dev_info);
2012 if (session_pool_socket[socket_id] == NULL) {
2013 char mp_name[RTE_MEMPOOL_NAMESIZE];
2014 struct rte_mempool *sess_mp;
2016 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2017 "sess_mp_%u", socket_id);
2020 * Create enough objects for session headers and
2021 * device private data
2023 sess_mp = rte_mempool_create(mp_name,
2026 SESSION_POOL_CACHE_SIZE,
2027 0, NULL, NULL, NULL,
2031 if (sess_mp == NULL) {
2032 printf("Cannot create session pool on socket %d\n",
2037 printf("Allocated session pool on socket %d\n", socket_id);
2038 session_pool_socket[socket_id] = sess_mp;
2041 /* Set AEAD parameters */
2042 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2043 /* Check if device supports AEAD algo */
2044 cap = check_device_support_aead_algo(options, &dev_info,
2049 options->block_size = cap->sym.aead.block_size;
2051 check_iv_param(&cap->sym.aead.iv_size,
2052 options->aead_iv_param,
2053 options->aead_iv_random_size,
2054 &options->aead_iv.length);
2057 * Check if length of provided AEAD key is supported
2058 * by the algorithm chosen.
2060 if (options->aead_key_param) {
2061 if (check_supported_size(
2062 options->aead_xform.aead.key.length,
2063 cap->sym.aead.key_size.min,
2064 cap->sym.aead.key_size.max,
2065 cap->sym.aead.key_size.increment)
2067 printf("Unsupported aead key length\n");
2071 * Check if length of the aead key to be randomly generated
2072 * is supported by the algorithm chosen.
2074 } else if (options->aead_key_random_size != -1) {
2075 if (check_supported_size(options->aead_key_random_size,
2076 cap->sym.aead.key_size.min,
2077 cap->sym.aead.key_size.max,
2078 cap->sym.aead.key_size.increment)
2080 printf("Unsupported aead key length\n");
2083 options->aead_xform.aead.key.length =
2084 options->aead_key_random_size;
2085 /* No size provided, use minimum size. */
2087 options->aead_xform.aead.key.length =
2088 cap->sym.aead.key_size.min;
2090 if (!options->aead_key_param)
2091 generate_random_key(
2092 options->aead_xform.aead.key.data,
2093 options->aead_xform.aead.key.length);
2096 * Check if length of provided AAD is supported
2097 * by the algorithm chosen.
2099 if (options->aad_param) {
2100 if (check_supported_size(options->aad.length,
2101 cap->sym.aead.aad_size.min,
2102 cap->sym.aead.aad_size.max,
2103 cap->sym.aead.aad_size.increment)
2105 printf("Unsupported AAD length\n");
2109 * Check if length of AAD to be randomly generated
2110 * is supported by the algorithm chosen.
2112 } else if (options->aad_random_size != -1) {
2113 if (check_supported_size(options->aad_random_size,
2114 cap->sym.aead.aad_size.min,
2115 cap->sym.aead.aad_size.max,
2116 cap->sym.aead.aad_size.increment)
2118 printf("Unsupported AAD length\n");
2121 options->aad.length = options->aad_random_size;
2122 /* No size provided, use minimum size. */
2124 options->aad.length = cap->sym.auth.aad_size.min;
2126 options->aead_xform.aead.aad_length =
2127 options->aad.length;
2129 /* Check if digest size is supported by the algorithm. */
2130 if (options->digest_size != -1) {
2131 if (check_supported_size(options->digest_size,
2132 cap->sym.aead.digest_size.min,
2133 cap->sym.aead.digest_size.max,
2134 cap->sym.aead.digest_size.increment)
2136 printf("Unsupported digest length\n");
2139 options->aead_xform.aead.digest_length =
2140 options->digest_size;
2141 /* No size provided, use minimum size. */
2143 options->aead_xform.aead.digest_length =
2144 cap->sym.aead.digest_size.min;
2147 /* Set cipher parameters */
2148 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2149 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2150 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2151 /* Check if device supports cipher algo */
2152 cap = check_device_support_cipher_algo(options, &dev_info,
2157 options->block_size = cap->sym.cipher.block_size;
2159 check_iv_param(&cap->sym.cipher.iv_size,
2160 options->cipher_iv_param,
2161 options->cipher_iv_random_size,
2162 &options->cipher_iv.length);
2165 * Check if length of provided cipher key is supported
2166 * by the algorithm chosen.
2168 if (options->ckey_param) {
2169 if (check_supported_size(
2170 options->cipher_xform.cipher.key.length,
2171 cap->sym.cipher.key_size.min,
2172 cap->sym.cipher.key_size.max,
2173 cap->sym.cipher.key_size.increment)
2175 printf("Unsupported cipher key length\n");
2179 * Check if length of the cipher key to be randomly generated
2180 * is supported by the algorithm chosen.
2182 } else if (options->ckey_random_size != -1) {
2183 if (check_supported_size(options->ckey_random_size,
2184 cap->sym.cipher.key_size.min,
2185 cap->sym.cipher.key_size.max,
2186 cap->sym.cipher.key_size.increment)
2188 printf("Unsupported cipher key length\n");
2191 options->cipher_xform.cipher.key.length =
2192 options->ckey_random_size;
2193 /* No size provided, use minimum size. */
2195 options->cipher_xform.cipher.key.length =
2196 cap->sym.cipher.key_size.min;
2198 if (!options->ckey_param)
2199 generate_random_key(
2200 options->cipher_xform.cipher.key.data,
2201 options->cipher_xform.cipher.key.length);
2205 /* Set auth parameters */
2206 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2207 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2208 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2209 /* Check if device supports auth algo */
2210 cap = check_device_support_auth_algo(options, &dev_info,
2215 check_iv_param(&cap->sym.auth.iv_size,
2216 options->auth_iv_param,
2217 options->auth_iv_random_size,
2218 &options->auth_iv.length);
2220 * Check if length of provided auth key is supported
2221 * by the algorithm chosen.
2223 if (options->akey_param) {
2224 if (check_supported_size(
2225 options->auth_xform.auth.key.length,
2226 cap->sym.auth.key_size.min,
2227 cap->sym.auth.key_size.max,
2228 cap->sym.auth.key_size.increment)
2230 printf("Unsupported auth key length\n");
2234 * Check if length of the auth key to be randomly generated
2235 * is supported by the algorithm chosen.
2237 } else if (options->akey_random_size != -1) {
2238 if (check_supported_size(options->akey_random_size,
2239 cap->sym.auth.key_size.min,
2240 cap->sym.auth.key_size.max,
2241 cap->sym.auth.key_size.increment)
2243 printf("Unsupported auth key length\n");
2246 options->auth_xform.auth.key.length =
2247 options->akey_random_size;
2248 /* No size provided, use minimum size. */
2250 options->auth_xform.auth.key.length =
2251 cap->sym.auth.key_size.min;
2253 if (!options->akey_param)
2254 generate_random_key(
2255 options->auth_xform.auth.key.data,
2256 options->auth_xform.auth.key.length);
2258 /* Check if digest size is supported by the algorithm. */
2259 if (options->digest_size != -1) {
2260 if (check_supported_size(options->digest_size,
2261 cap->sym.auth.digest_size.min,
2262 cap->sym.auth.digest_size.max,
2263 cap->sym.auth.digest_size.increment)
2265 printf("Unsupported digest length\n");
2268 options->auth_xform.auth.digest_length =
2269 options->digest_size;
2270 /* No size provided, use minimum size. */
2272 options->auth_xform.auth.digest_length =
2273 cap->sym.auth.digest_size.min;
2276 retval = rte_cryptodev_configure(cdev_id, &conf);
2278 printf("Failed to configure cryptodev %u", cdev_id);
2282 qp_conf.nb_descriptors = 2048;
2284 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2285 socket_id, session_pool_socket[socket_id]);
2287 printf("Failed to setup queue pair %u on cryptodev %u",
2292 retval = rte_cryptodev_start(cdev_id);
2294 printf("Failed to start device %u: error %d\n",
2299 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2301 enabled_cdevs[cdev_id] = 1;
2302 enabled_cdev_count++;
2305 return enabled_cdev_count;
2309 initialize_ports(struct l2fwd_crypto_options *options)
2311 uint16_t last_portid = 0, portid;
2312 unsigned enabled_portcount = 0;
2313 unsigned nb_ports = rte_eth_dev_count_avail();
2315 if (nb_ports == 0) {
2316 printf("No Ethernet ports - bye\n");
2320 /* Reset l2fwd_dst_ports */
2321 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2322 l2fwd_dst_ports[portid] = 0;
2324 RTE_ETH_FOREACH_DEV(portid) {
2326 struct rte_eth_dev_info dev_info;
2327 struct rte_eth_rxconf rxq_conf;
2328 struct rte_eth_txconf txq_conf;
2329 struct rte_eth_conf local_port_conf = port_conf;
2331 /* Skip ports that are not enabled */
2332 if ((options->portmask & (1 << portid)) == 0)
2336 printf("Initializing port %u... ", portid);
2338 rte_eth_dev_info_get(portid, &dev_info);
2339 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2340 local_port_conf.txmode.offloads |=
2341 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2342 retval = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
2344 printf("Cannot configure device: err=%d, port=%u\n",
2349 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2352 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2357 /* init one RX queue */
2359 rxq_conf = dev_info.default_rxconf;
2360 rxq_conf.offloads = local_port_conf.rxmode.offloads;
2361 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2362 rte_eth_dev_socket_id(portid),
2363 &rxq_conf, l2fwd_pktmbuf_pool);
2365 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2370 /* init one TX queue on each port */
2372 txq_conf = dev_info.default_txconf;
2373 txq_conf.offloads = local_port_conf.txmode.offloads;
2374 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2375 rte_eth_dev_socket_id(portid),
2378 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2385 retval = rte_eth_dev_start(portid);
2387 printf("rte_eth_dev_start:err=%d, port=%u\n",
2392 rte_eth_promiscuous_enable(portid);
2394 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2396 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2398 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2399 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2400 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2401 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2402 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2403 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2405 /* initialize port stats */
2406 memset(&port_statistics, 0, sizeof(port_statistics));
2408 /* Setup port forwarding table */
2409 if (enabled_portcount % 2) {
2410 l2fwd_dst_ports[portid] = last_portid;
2411 l2fwd_dst_ports[last_portid] = portid;
2413 last_portid = portid;
2416 l2fwd_enabled_port_mask |= (1 << portid);
2417 enabled_portcount++;
2420 if (enabled_portcount == 1) {
2421 l2fwd_dst_ports[last_portid] = last_portid;
2422 } else if (enabled_portcount % 2) {
2423 printf("odd number of ports in portmask- bye\n");
2427 check_all_ports_link_status(l2fwd_enabled_port_mask);
2429 return enabled_portcount;
2433 reserve_key_memory(struct l2fwd_crypto_options *options)
2435 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2437 if (options->cipher_xform.cipher.key.data == NULL)
2438 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2440 options->auth_xform.auth.key.data = rte_malloc("auth key",
2442 if (options->auth_xform.auth.key.data == NULL)
2443 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2445 options->aead_xform.aead.key.data = rte_malloc("aead key",
2447 if (options->aead_xform.aead.key.data == NULL)
2448 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2450 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2451 if (options->cipher_iv.data == NULL)
2452 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2454 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2455 if (options->auth_iv.data == NULL)
2456 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2458 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2459 if (options->aead_iv.data == NULL)
2460 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2462 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2463 if (options->aad.data == NULL)
2464 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2465 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2469 main(int argc, char **argv)
2471 struct lcore_queue_conf *qconf = NULL;
2472 struct l2fwd_crypto_options options;
2474 uint8_t nb_cryptodevs, cdev_id;
2476 unsigned lcore_id, rx_lcore_id = 0;
2477 int ret, enabled_cdevcount, enabled_portcount;
2478 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2481 ret = rte_eal_init(argc, argv);
2483 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2487 /* reserve memory for Cipher/Auth key and IV */
2488 reserve_key_memory(&options);
2490 /* parse application arguments (after the EAL ones) */
2491 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2493 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2495 printf("MAC updating %s\n",
2496 options.mac_updating ? "enabled" : "disabled");
2498 /* create the mbuf pool */
2499 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2500 sizeof(struct rte_crypto_op),
2501 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2502 if (l2fwd_pktmbuf_pool == NULL)
2503 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2505 /* create crypto op pool */
2506 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2507 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2509 if (l2fwd_crypto_op_pool == NULL)
2510 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2512 /* Enable Ethernet ports */
2513 enabled_portcount = initialize_ports(&options);
2514 if (enabled_portcount < 1)
2515 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2517 /* Initialize the port/queue configuration of each logical core */
2518 RTE_ETH_FOREACH_DEV(portid) {
2520 /* skip ports that are not enabled */
2521 if ((options.portmask & (1 << portid)) == 0)
2524 if (options.single_lcore && qconf == NULL) {
2525 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2527 if (rx_lcore_id >= RTE_MAX_LCORE)
2528 rte_exit(EXIT_FAILURE,
2529 "Not enough cores\n");
2531 } else if (!options.single_lcore) {
2532 /* get the lcore_id for this port */
2533 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2534 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2535 options.nb_ports_per_lcore) {
2537 if (rx_lcore_id >= RTE_MAX_LCORE)
2538 rte_exit(EXIT_FAILURE,
2539 "Not enough cores\n");
2543 /* Assigned a new logical core in the loop above. */
2544 if (qconf != &lcore_queue_conf[rx_lcore_id])
2545 qconf = &lcore_queue_conf[rx_lcore_id];
2547 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2548 qconf->nb_rx_ports++;
2550 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2553 /* Enable Crypto devices */
2554 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2556 if (enabled_cdevcount < 0)
2557 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2559 if (enabled_cdevcount < enabled_portcount)
2560 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2561 "has to be more or equal to number of ports (%d)\n",
2562 enabled_cdevcount, enabled_portcount);
2564 nb_cryptodevs = rte_cryptodev_count();
2566 /* Initialize the port/cryptodev configuration of each logical core */
2567 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2568 cdev_id < nb_cryptodevs && enabled_cdevcount;
2570 /* Crypto op not supported by crypto device */
2571 if (!enabled_cdevs[cdev_id])
2574 if (options.single_lcore && qconf == NULL) {
2575 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2577 if (rx_lcore_id >= RTE_MAX_LCORE)
2578 rte_exit(EXIT_FAILURE,
2579 "Not enough cores\n");
2581 } else if (!options.single_lcore) {
2582 /* get the lcore_id for this port */
2583 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2584 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2585 options.nb_ports_per_lcore) {
2587 if (rx_lcore_id >= RTE_MAX_LCORE)
2588 rte_exit(EXIT_FAILURE,
2589 "Not enough cores\n");
2593 /* Assigned a new logical core in the loop above. */
2594 if (qconf != &lcore_queue_conf[rx_lcore_id])
2595 qconf = &lcore_queue_conf[rx_lcore_id];
2597 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2598 qconf->nb_crypto_devs++;
2600 enabled_cdevcount--;
2602 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2606 /* launch per-lcore init on every lcore */
2607 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2609 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2610 if (rte_eal_wait_lcore(lcore_id) < 0)