4 * Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
54 #include <rte_cryptodev.h>
55 #include <rte_cycles.h>
56 #include <rte_debug.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_interrupts.h>
62 #include <rte_launch.h>
63 #include <rte_lcore.h>
65 #include <rte_malloc.h>
67 #include <rte_memcpy.h>
68 #include <rte_memory.h>
69 #include <rte_mempool.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
73 #include <rte_hexdump.h>
81 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
85 #define MAX_STR_LEN 32
86 #define MAX_KEY_SIZE 128
87 #define MAX_IV_SIZE 16
88 #define MAX_AAD_SIZE 65535
89 #define MAX_PKT_BURST 32
90 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91 #define MAX_SESSIONS 32
92 #define SESSION_POOL_CACHE_SIZE 0
94 #define MAXIMUM_IV_LENGTH 16
95 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
96 sizeof(struct rte_crypto_sym_op))
99 * Configurable number of RX/TX ring descriptors
101 #define RTE_TEST_RX_DESC_DEFAULT 128
102 #define RTE_TEST_TX_DESC_DEFAULT 512
104 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
105 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
107 /* ethernet addresses of ports */
108 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
110 /* mask of enabled ports */
111 static uint64_t l2fwd_enabled_port_mask;
112 static uint64_t l2fwd_enabled_crypto_mask;
114 /* list of enabled ports */
115 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
120 struct rte_mbuf *buffer[MAX_PKT_BURST];
125 struct rte_crypto_op *buffer[MAX_PKT_BURST];
128 #define MAX_RX_QUEUE_PER_LCORE 16
129 #define MAX_TX_QUEUE_PER_PORT 16
131 enum l2fwd_crypto_xform_chain {
132 L2FWD_CRYPTO_CIPHER_HASH,
133 L2FWD_CRYPTO_HASH_CIPHER,
134 L2FWD_CRYPTO_CIPHER_ONLY,
135 L2FWD_CRYPTO_HASH_ONLY,
142 rte_iova_t phys_addr;
150 /** l2fwd crypto application command line options */
151 struct l2fwd_crypto_options {
153 unsigned nb_ports_per_lcore;
154 unsigned refresh_period;
155 unsigned single_lcore:1;
158 unsigned sessionless:1;
160 enum l2fwd_crypto_xform_chain xform_chain;
162 struct rte_crypto_sym_xform cipher_xform;
164 int ckey_random_size;
166 struct l2fwd_iv cipher_iv;
167 unsigned int cipher_iv_param;
168 int cipher_iv_random_size;
170 struct rte_crypto_sym_xform auth_xform;
172 int akey_random_size;
174 struct l2fwd_iv auth_iv;
175 unsigned int auth_iv_param;
176 int auth_iv_random_size;
178 struct rte_crypto_sym_xform aead_xform;
179 unsigned int aead_key_param;
180 int aead_key_random_size;
182 struct l2fwd_iv aead_iv;
183 unsigned int aead_iv_param;
184 int aead_iv_random_size;
186 struct l2fwd_key aad;
193 char string_type[MAX_STR_LEN];
195 uint64_t cryptodev_mask;
197 unsigned int mac_updating;
200 /** l2fwd crypto lcore params */
201 struct l2fwd_crypto_params {
205 unsigned digest_length;
208 struct l2fwd_iv cipher_iv;
209 struct l2fwd_iv auth_iv;
210 struct l2fwd_iv aead_iv;
211 struct l2fwd_key aad;
212 struct rte_cryptodev_sym_session *session;
219 enum rte_crypto_cipher_algorithm cipher_algo;
220 enum rte_crypto_auth_algorithm auth_algo;
221 enum rte_crypto_aead_algorithm aead_algo;
224 /** lcore configuration */
225 struct lcore_queue_conf {
226 unsigned nb_rx_ports;
227 uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
229 unsigned nb_crypto_devs;
230 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
232 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
233 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
234 } __rte_cache_aligned;
236 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
238 static const struct rte_eth_conf port_conf = {
240 .mq_mode = ETH_MQ_RX_NONE,
241 .max_rx_pkt_len = ETHER_MAX_LEN,
243 .header_split = 0, /**< Header Split disabled */
244 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
245 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
246 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
247 .hw_strip_crc = 1, /**< CRC stripped by hardware */
250 .mq_mode = ETH_MQ_TX_NONE,
254 struct rte_mempool *l2fwd_pktmbuf_pool;
255 struct rte_mempool *l2fwd_crypto_op_pool;
256 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
258 /* Per-port statistics struct */
259 struct l2fwd_port_statistics {
263 uint64_t crypto_enqueued;
264 uint64_t crypto_dequeued;
267 } __rte_cache_aligned;
269 struct l2fwd_crypto_statistics {
274 } __rte_cache_aligned;
276 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
277 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
279 /* A tsc-based timer responsible for triggering statistics printout */
280 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
281 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
283 /* default period is 10 seconds */
284 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
286 /* Print out statistics on packets dropped */
290 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
291 uint64_t total_packets_enqueued, total_packets_dequeued,
292 total_packets_errors;
296 total_packets_dropped = 0;
297 total_packets_tx = 0;
298 total_packets_rx = 0;
299 total_packets_enqueued = 0;
300 total_packets_dequeued = 0;
301 total_packets_errors = 0;
303 const char clr[] = { 27, '[', '2', 'J', '\0' };
304 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
306 /* Clear screen and move to top left */
307 printf("%s%s", clr, topLeft);
309 printf("\nPort statistics ====================================");
311 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
312 /* skip disabled ports */
313 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
315 printf("\nStatistics for port %u ------------------------------"
316 "\nPackets sent: %32"PRIu64
317 "\nPackets received: %28"PRIu64
318 "\nPackets dropped: %29"PRIu64,
320 port_statistics[portid].tx,
321 port_statistics[portid].rx,
322 port_statistics[portid].dropped);
324 total_packets_dropped += port_statistics[portid].dropped;
325 total_packets_tx += port_statistics[portid].tx;
326 total_packets_rx += port_statistics[portid].rx;
328 printf("\nCrypto statistics ==================================");
330 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
331 /* skip disabled ports */
332 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
334 printf("\nStatistics for cryptodev %"PRIu64
335 " -------------------------"
336 "\nPackets enqueued: %28"PRIu64
337 "\nPackets dequeued: %28"PRIu64
338 "\nPackets errors: %30"PRIu64,
340 crypto_statistics[cdevid].enqueued,
341 crypto_statistics[cdevid].dequeued,
342 crypto_statistics[cdevid].errors);
344 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
345 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
346 total_packets_errors += crypto_statistics[cdevid].errors;
348 printf("\nAggregate statistics ==============================="
349 "\nTotal packets received: %22"PRIu64
350 "\nTotal packets enqueued: %22"PRIu64
351 "\nTotal packets dequeued: %22"PRIu64
352 "\nTotal packets sent: %26"PRIu64
353 "\nTotal packets dropped: %23"PRIu64
354 "\nTotal packets crypto errors: %17"PRIu64,
356 total_packets_enqueued,
357 total_packets_dequeued,
359 total_packets_dropped,
360 total_packets_errors);
361 printf("\n====================================================\n");
365 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
366 struct l2fwd_crypto_params *cparams)
368 struct rte_crypto_op **op_buffer;
371 op_buffer = (struct rte_crypto_op **)
372 qconf->op_buf[cparams->dev_id].buffer;
374 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
375 cparams->qp_id, op_buffer, (uint16_t) n);
377 crypto_statistics[cparams->dev_id].enqueued += ret;
378 if (unlikely(ret < n)) {
379 crypto_statistics[cparams->dev_id].errors += (n - ret);
381 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
382 rte_crypto_op_free(op_buffer[ret]);
390 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
391 struct l2fwd_crypto_params *cparams)
393 unsigned lcore_id, len;
394 struct lcore_queue_conf *qconf;
396 lcore_id = rte_lcore_id();
398 qconf = &lcore_queue_conf[lcore_id];
399 len = qconf->op_buf[cparams->dev_id].len;
400 qconf->op_buf[cparams->dev_id].buffer[len] = op;
403 /* enough ops to be sent */
404 if (len == MAX_PKT_BURST) {
405 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
409 qconf->op_buf[cparams->dev_id].len = len;
414 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
415 struct rte_crypto_op *op,
416 struct l2fwd_crypto_params *cparams)
418 struct ether_hdr *eth_hdr;
419 struct ipv4_hdr *ip_hdr;
421 uint32_t ipdata_offset, data_len;
422 uint32_t pad_len = 0;
425 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
427 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
430 ipdata_offset = sizeof(struct ether_hdr);
432 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
435 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
436 * IPV4_IHL_MULTIPLIER;
439 /* Zero pad data to be crypto'd so it is block aligned */
440 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
442 if (cparams->do_hash && cparams->hash_verify)
443 data_len -= cparams->digest_length;
445 if (cparams->do_cipher) {
447 * Following algorithms are block cipher algorithms,
448 * and might need padding
450 switch (cparams->cipher_algo) {
451 case RTE_CRYPTO_CIPHER_AES_CBC:
452 case RTE_CRYPTO_CIPHER_AES_ECB:
453 case RTE_CRYPTO_CIPHER_DES_CBC:
454 case RTE_CRYPTO_CIPHER_3DES_CBC:
455 case RTE_CRYPTO_CIPHER_3DES_ECB:
456 if (data_len % cparams->block_size)
457 pad_len = cparams->block_size -
458 (data_len % cparams->block_size);
465 padding = rte_pktmbuf_append(m, pad_len);
466 if (unlikely(!padding))
470 memset(padding, 0, pad_len);
474 /* Set crypto operation data parameters */
475 rte_crypto_op_attach_sym_session(op, cparams->session);
477 if (cparams->do_hash) {
478 if (cparams->auth_iv.length) {
479 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
482 cparams->cipher_iv.length);
484 * Copy IV at the end of the crypto operation,
485 * after the cipher IV, if added
487 rte_memcpy(iv_ptr, cparams->auth_iv.data,
488 cparams->auth_iv.length);
490 if (!cparams->hash_verify) {
491 /* Append space for digest to end of packet */
492 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
493 cparams->digest_length);
495 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
496 uint8_t *) + ipdata_offset + data_len;
499 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m,
500 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
502 /* For wireless algorithms, offset/length must be in bits */
503 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
504 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
505 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
506 op->sym->auth.data.offset = ipdata_offset << 3;
507 op->sym->auth.data.length = data_len << 3;
509 op->sym->auth.data.offset = ipdata_offset;
510 op->sym->auth.data.length = data_len;
514 if (cparams->do_cipher) {
515 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
517 /* Copy IV at the end of the crypto operation */
518 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
519 cparams->cipher_iv.length);
521 /* For wireless algorithms, offset/length must be in bits */
522 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
523 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
524 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
525 op->sym->cipher.data.offset = ipdata_offset << 3;
526 op->sym->cipher.data.length = data_len << 3;
528 op->sym->cipher.data.offset = ipdata_offset;
529 op->sym->cipher.data.length = data_len;
533 if (cparams->do_aead) {
534 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
536 /* Copy IV at the end of the crypto operation */
538 * If doing AES-CCM, nonce is copied one byte
539 * after the start of IV field
541 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
542 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
543 cparams->aead_iv.length);
545 rte_memcpy(iv_ptr, cparams->aead_iv.data,
546 cparams->aead_iv.length);
548 op->sym->aead.data.offset = ipdata_offset;
549 op->sym->aead.data.length = data_len;
551 if (!cparams->hash_verify) {
552 /* Append space for digest to end of packet */
553 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
554 cparams->digest_length);
556 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
557 uint8_t *) + ipdata_offset + data_len;
560 op->sym->aead.digest.phys_addr = rte_pktmbuf_iova_offset(m,
561 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
563 if (cparams->aad.length) {
564 op->sym->aead.aad.data = cparams->aad.data;
565 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
571 return l2fwd_crypto_enqueue(op, cparams);
575 /* Send the burst of packets on an output interface */
577 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
580 struct rte_mbuf **pkt_buffer;
583 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
585 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
586 port_statistics[port].tx += ret;
587 if (unlikely(ret < n)) {
588 port_statistics[port].dropped += (n - ret);
590 rte_pktmbuf_free(pkt_buffer[ret]);
597 /* Enqueue packets for TX and prepare them to be sent */
599 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
601 unsigned lcore_id, len;
602 struct lcore_queue_conf *qconf;
604 lcore_id = rte_lcore_id();
606 qconf = &lcore_queue_conf[lcore_id];
607 len = qconf->pkt_buf[port].len;
608 qconf->pkt_buf[port].buffer[len] = m;
611 /* enough pkts to be sent */
612 if (unlikely(len == MAX_PKT_BURST)) {
613 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
617 qconf->pkt_buf[port].len = len;
622 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
624 struct ether_hdr *eth;
627 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
629 /* 02:00:00:00:00:xx */
630 tmp = ð->d_addr.addr_bytes[0];
631 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
634 ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
638 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
639 struct l2fwd_crypto_options *options)
643 dst_port = l2fwd_dst_ports[portid];
645 if (options->mac_updating)
646 l2fwd_mac_updating(m, dst_port);
648 l2fwd_send_packet(m, dst_port);
651 /** Generate random key */
653 generate_random_key(uint8_t *key, unsigned length)
658 fd = open("/dev/urandom", O_RDONLY);
660 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
662 ret = read(fd, key, length);
665 if (ret != (signed)length)
666 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
669 static struct rte_cryptodev_sym_session *
670 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
672 struct rte_crypto_sym_xform *first_xform;
673 struct rte_cryptodev_sym_session *session;
674 int retval = rte_cryptodev_socket_id(cdev_id);
679 uint8_t socket_id = (uint8_t) retval;
680 struct rte_mempool *sess_mp = session_pool_socket[socket_id];
682 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
683 first_xform = &options->aead_xform;
684 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
685 first_xform = &options->cipher_xform;
686 first_xform->next = &options->auth_xform;
687 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
688 first_xform = &options->auth_xform;
689 first_xform->next = &options->cipher_xform;
690 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
691 first_xform = &options->cipher_xform;
693 first_xform = &options->auth_xform;
696 session = rte_cryptodev_sym_session_create(sess_mp);
701 if (rte_cryptodev_sym_session_init(cdev_id, session,
702 first_xform, sess_mp) < 0)
709 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
711 /* main processing loop */
713 l2fwd_main_loop(struct l2fwd_crypto_options *options)
715 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
716 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
718 unsigned lcore_id = rte_lcore_id();
719 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
720 unsigned int i, j, nb_rx, len;
722 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
723 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
724 US_PER_S * BURST_TX_DRAIN_US;
725 struct l2fwd_crypto_params *cparams;
726 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
727 struct rte_cryptodev_sym_session *session;
729 if (qconf->nb_rx_ports == 0) {
730 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
734 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
736 for (i = 0; i < qconf->nb_rx_ports; i++) {
738 portid = qconf->rx_port_list[i];
739 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
743 for (i = 0; i < qconf->nb_crypto_devs; i++) {
744 port_cparams[i].do_cipher = 0;
745 port_cparams[i].do_hash = 0;
746 port_cparams[i].do_aead = 0;
748 switch (options->xform_chain) {
749 case L2FWD_CRYPTO_AEAD:
750 port_cparams[i].do_aead = 1;
752 case L2FWD_CRYPTO_CIPHER_HASH:
753 case L2FWD_CRYPTO_HASH_CIPHER:
754 port_cparams[i].do_cipher = 1;
755 port_cparams[i].do_hash = 1;
757 case L2FWD_CRYPTO_HASH_ONLY:
758 port_cparams[i].do_hash = 1;
760 case L2FWD_CRYPTO_CIPHER_ONLY:
761 port_cparams[i].do_cipher = 1;
765 port_cparams[i].dev_id = qconf->cryptodev_list[i];
766 port_cparams[i].qp_id = 0;
768 port_cparams[i].block_size = options->block_size;
770 if (port_cparams[i].do_hash) {
771 port_cparams[i].auth_iv.data = options->auth_iv.data;
772 port_cparams[i].auth_iv.length = options->auth_iv.length;
773 if (!options->auth_iv_param)
774 generate_random_key(port_cparams[i].auth_iv.data,
775 port_cparams[i].auth_iv.length);
776 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
777 port_cparams[i].hash_verify = 1;
779 port_cparams[i].hash_verify = 0;
781 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
782 port_cparams[i].digest_length =
783 options->auth_xform.auth.digest_length;
784 /* Set IV parameters */
785 if (options->auth_iv.length) {
786 options->auth_xform.auth.iv.offset =
787 IV_OFFSET + options->cipher_iv.length;
788 options->auth_xform.auth.iv.length =
789 options->auth_iv.length;
793 if (port_cparams[i].do_aead) {
794 port_cparams[i].aead_iv.data = options->aead_iv.data;
795 port_cparams[i].aead_iv.length = options->aead_iv.length;
796 if (!options->aead_iv_param)
797 generate_random_key(port_cparams[i].aead_iv.data,
798 port_cparams[i].aead_iv.length);
799 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
800 port_cparams[i].digest_length =
801 options->aead_xform.aead.digest_length;
802 if (options->aead_xform.aead.aad_length) {
803 port_cparams[i].aad.data = options->aad.data;
804 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
805 port_cparams[i].aad.length = options->aad.length;
806 if (!options->aad_param)
807 generate_random_key(port_cparams[i].aad.data,
808 port_cparams[i].aad.length);
810 * If doing AES-CCM, first 18 bytes has to be reserved,
811 * and actual AAD should start from byte 18
813 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
814 memmove(port_cparams[i].aad.data + 18,
815 port_cparams[i].aad.data,
816 port_cparams[i].aad.length);
819 port_cparams[i].aad.length = 0;
821 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
822 port_cparams[i].hash_verify = 1;
824 port_cparams[i].hash_verify = 0;
826 /* Set IV parameters */
827 options->aead_xform.aead.iv.offset = IV_OFFSET;
828 options->aead_xform.aead.iv.length = options->aead_iv.length;
831 if (port_cparams[i].do_cipher) {
832 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
833 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
834 if (!options->cipher_iv_param)
835 generate_random_key(port_cparams[i].cipher_iv.data,
836 port_cparams[i].cipher_iv.length);
838 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
839 /* Set IV parameters */
840 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
841 options->cipher_xform.cipher.iv.length =
842 options->cipher_iv.length;
845 session = initialize_crypto_session(options,
846 port_cparams[i].dev_id);
848 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
850 port_cparams[i].session = session;
852 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
853 port_cparams[i].dev_id);
856 l2fwd_crypto_options_print(options);
859 * Initialize previous tsc timestamp before the loop,
860 * to avoid showing the port statistics immediately,
861 * so user can see the crypto information.
863 prev_tsc = rte_rdtsc();
866 cur_tsc = rte_rdtsc();
869 * Crypto device/TX burst queue drain
871 diff_tsc = cur_tsc - prev_tsc;
872 if (unlikely(diff_tsc > drain_tsc)) {
873 /* Enqueue all crypto ops remaining in buffers */
874 for (i = 0; i < qconf->nb_crypto_devs; i++) {
875 cparams = &port_cparams[i];
876 len = qconf->op_buf[cparams->dev_id].len;
877 l2fwd_crypto_send_burst(qconf, len, cparams);
878 qconf->op_buf[cparams->dev_id].len = 0;
880 /* Transmit all packets remaining in buffers */
881 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
882 if (qconf->pkt_buf[portid].len == 0)
884 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
885 qconf->pkt_buf[portid].len,
887 qconf->pkt_buf[portid].len = 0;
890 /* if timer is enabled */
891 if (timer_period > 0) {
893 /* advance the timer */
894 timer_tsc += diff_tsc;
896 /* if timer has reached its timeout */
897 if (unlikely(timer_tsc >=
898 (uint64_t)timer_period)) {
900 /* do this only on master core */
901 if (lcore_id == rte_get_master_lcore()
902 && options->refresh_period) {
913 * Read packet from RX queues
915 for (i = 0; i < qconf->nb_rx_ports; i++) {
916 portid = qconf->rx_port_list[i];
918 cparams = &port_cparams[i];
920 nb_rx = rte_eth_rx_burst(portid, 0,
921 pkts_burst, MAX_PKT_BURST);
923 port_statistics[portid].rx += nb_rx;
927 * If we can't allocate a crypto_ops, then drop
928 * the rest of the burst and dequeue and
929 * process the packets to free offload structs
931 if (rte_crypto_op_bulk_alloc(
932 l2fwd_crypto_op_pool,
933 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
936 for (j = 0; j < nb_rx; j++)
937 rte_pktmbuf_free(pkts_burst[j]);
942 /* Enqueue packets from Crypto device*/
943 for (j = 0; j < nb_rx; j++) {
946 l2fwd_simple_crypto_enqueue(m,
947 ops_burst[j], cparams);
951 /* Dequeue packets from Crypto device */
953 nb_rx = rte_cryptodev_dequeue_burst(
954 cparams->dev_id, cparams->qp_id,
955 ops_burst, MAX_PKT_BURST);
957 crypto_statistics[cparams->dev_id].dequeued +=
960 /* Forward crypto'd packets */
961 for (j = 0; j < nb_rx; j++) {
962 m = ops_burst[j]->sym->m_src;
964 rte_crypto_op_free(ops_burst[j]);
965 l2fwd_simple_forward(m, portid,
968 } while (nb_rx == MAX_PKT_BURST);
974 l2fwd_launch_one_lcore(void *arg)
976 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
980 /* Display command line arguments usage */
982 l2fwd_crypto_usage(const char *prgname)
984 printf("%s [EAL options] --\n"
985 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
986 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
987 " -s manage all ports from single lcore\n"
988 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
989 " (0 to disable, 10 default, 86400 maximum)\n"
991 " --cdev_type HW / SW / ANY\n"
992 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
993 " HASH_ONLY / AEAD\n"
995 " --cipher_algo ALGO\n"
996 " --cipher_op ENCRYPT / DECRYPT\n"
997 " --cipher_key KEY (bytes separated with \":\")\n"
998 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
999 " --cipher_iv IV (bytes separated with \":\")\n"
1000 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1002 " --auth_algo ALGO\n"
1003 " --auth_op GENERATE / VERIFY\n"
1004 " --auth_key KEY (bytes separated with \":\")\n"
1005 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1006 " --auth_iv IV (bytes separated with \":\")\n"
1007 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
1009 " --aead_algo ALGO\n"
1010 " --aead_op ENCRYPT / DECRYPT\n"
1011 " --aead_key KEY (bytes separated with \":\")\n"
1012 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
1013 " --aead_iv IV (bytes separated with \":\")\n"
1014 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1015 " --aad AAD (bytes separated with \":\")\n"
1016 " --aad_random_size SIZE: size of AAD when generated randomly\n"
1018 " --digest_size SIZE: size of digest to be generated/verified\n"
1021 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1023 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1025 " - The source MAC address is replaced by the TX port MAC address\n"
1026 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1030 /** Parse crypto device type command line argument */
1032 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1034 if (strcmp("HW", optarg) == 0) {
1035 *type = CDEV_TYPE_HW;
1037 } else if (strcmp("SW", optarg) == 0) {
1038 *type = CDEV_TYPE_SW;
1040 } else if (strcmp("ANY", optarg) == 0) {
1041 *type = CDEV_TYPE_ANY;
1048 /** Parse crypto chain xform command line argument */
1050 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1052 if (strcmp("CIPHER_HASH", optarg) == 0) {
1053 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1055 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1056 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1058 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1059 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1061 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1062 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1064 } else if (strcmp("AEAD", optarg) == 0) {
1065 options->xform_chain = L2FWD_CRYPTO_AEAD;
1072 /** Parse crypto cipher algo option command line argument */
1074 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1077 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1078 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1079 "not supported!\n");
1086 /** Parse crypto cipher operation command line argument */
1088 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1090 if (strcmp("ENCRYPT", optarg) == 0) {
1091 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1093 } else if (strcmp("DECRYPT", optarg) == 0) {
1094 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1098 printf("Cipher operation not supported!\n");
1102 /** Parse bytes from command line argument */
1104 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1106 unsigned byte_count;
1110 for (byte_count = 0, token = strtok(input_arg, ":");
1111 (byte_count < max_size) && (token != NULL);
1112 token = strtok(NULL, ":")) {
1114 int number = (int)strtol(token, NULL, 16);
1116 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1119 data[byte_count++] = (uint8_t)number;
1125 /** Parse size param*/
1127 parse_size(int *size, const char *q_arg)
1132 /* parse hexadecimal string */
1133 n = strtoul(q_arg, &end, 10);
1134 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1138 printf("invalid size\n");
1146 /** Parse crypto cipher operation command line argument */
1148 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1150 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1151 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1152 "not supported!\n");
1160 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1162 if (strcmp("VERIFY", optarg) == 0) {
1163 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1165 } else if (strcmp("GENERATE", optarg) == 0) {
1166 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1170 printf("Authentication operation specified not supported!\n");
1175 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1177 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1178 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1179 "not supported!\n");
1187 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1189 if (strcmp("ENCRYPT", optarg) == 0) {
1190 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1192 } else if (strcmp("DECRYPT", optarg) == 0) {
1193 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1197 printf("AEAD operation specified not supported!\n");
1201 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1207 /* parse hexadecimal string */
1208 pm = strtoul(q_arg, &end, 16);
1209 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1212 options->cryptodev_mask = pm;
1213 if (options->cryptodev_mask == 0) {
1214 printf("invalid cryptodev_mask specified\n");
1221 /** Parse long options */
1223 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1224 struct option *lgopts, int option_index)
1228 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1229 retval = parse_cryptodev_type(&options->type, optarg);
1231 snprintf(options->string_type, MAX_STR_LEN,
1236 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1237 return parse_crypto_opt_chain(options, optarg);
1239 /* Cipher options */
1240 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1241 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1244 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1245 return parse_cipher_op(&options->cipher_xform.cipher.op,
1248 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1249 options->ckey_param = 1;
1250 options->cipher_xform.cipher.key.length =
1251 parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1253 if (options->cipher_xform.cipher.key.length > 0)
1259 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1260 return parse_size(&options->ckey_random_size, optarg);
1262 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1263 options->cipher_iv_param = 1;
1264 options->cipher_iv.length =
1265 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1266 if (options->cipher_iv.length > 0)
1272 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1273 return parse_size(&options->cipher_iv_random_size, optarg);
1275 /* Authentication options */
1276 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1277 return parse_auth_algo(&options->auth_xform.auth.algo,
1281 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1282 return parse_auth_op(&options->auth_xform.auth.op,
1285 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1286 options->akey_param = 1;
1287 options->auth_xform.auth.key.length =
1288 parse_bytes(options->auth_xform.auth.key.data, optarg,
1290 if (options->auth_xform.auth.key.length > 0)
1296 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1297 return parse_size(&options->akey_random_size, optarg);
1300 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1301 options->auth_iv_param = 1;
1302 options->auth_iv.length =
1303 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1304 if (options->auth_iv.length > 0)
1310 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1311 return parse_size(&options->auth_iv_random_size, optarg);
1314 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1315 return parse_aead_algo(&options->aead_xform.aead.algo,
1319 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1320 return parse_aead_op(&options->aead_xform.aead.op,
1323 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1324 options->aead_key_param = 1;
1325 options->aead_xform.aead.key.length =
1326 parse_bytes(options->aead_xform.aead.key.data, optarg,
1328 if (options->aead_xform.aead.key.length > 0)
1334 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1335 return parse_size(&options->aead_key_random_size, optarg);
1338 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1339 options->aead_iv_param = 1;
1340 options->aead_iv.length =
1341 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1342 if (options->aead_iv.length > 0)
1348 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1349 return parse_size(&options->aead_iv_random_size, optarg);
1351 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1352 options->aad_param = 1;
1353 options->aad.length =
1354 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1355 if (options->aad.length > 0)
1361 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1362 return parse_size(&options->aad_random_size, optarg);
1365 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1366 return parse_size(&options->digest_size, optarg);
1369 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1370 options->sessionless = 1;
1374 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1375 return parse_cryptodev_mask(options, optarg);
1377 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1378 options->mac_updating = 1;
1382 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1383 options->mac_updating = 0;
1390 /** Parse port mask */
1392 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1398 /* parse hexadecimal string */
1399 pm = strtoul(q_arg, &end, 16);
1400 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1403 options->portmask = pm;
1404 if (options->portmask == 0) {
1405 printf("invalid portmask specified\n");
1412 /** Parse number of queues */
1414 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1420 /* parse hexadecimal string */
1421 n = strtoul(q_arg, &end, 10);
1422 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1424 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1427 options->nb_ports_per_lcore = n;
1428 if (options->nb_ports_per_lcore == 0) {
1429 printf("invalid number of ports selected\n");
1436 /** Parse timer period */
1438 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1444 /* parse number string */
1445 n = (unsigned)strtol(q_arg, &end, 10);
1446 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1449 if (n >= MAX_TIMER_PERIOD) {
1450 printf("Warning refresh period specified %lu is greater than "
1451 "max value %lu! using max value",
1452 n, MAX_TIMER_PERIOD);
1453 n = MAX_TIMER_PERIOD;
1456 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1461 /** Generate default options for application */
1463 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1465 options->portmask = 0xffffffff;
1466 options->nb_ports_per_lcore = 1;
1467 options->refresh_period = 10000;
1468 options->single_lcore = 0;
1469 options->sessionless = 0;
1471 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1474 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1475 options->cipher_xform.next = NULL;
1476 options->ckey_param = 0;
1477 options->ckey_random_size = -1;
1478 options->cipher_xform.cipher.key.length = 0;
1479 options->cipher_iv_param = 0;
1480 options->cipher_iv_random_size = -1;
1481 options->cipher_iv.length = 0;
1483 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1484 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1486 /* Authentication Data */
1487 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1488 options->auth_xform.next = NULL;
1489 options->akey_param = 0;
1490 options->akey_random_size = -1;
1491 options->auth_xform.auth.key.length = 0;
1492 options->auth_iv_param = 0;
1493 options->auth_iv_random_size = -1;
1494 options->auth_iv.length = 0;
1496 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1497 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1500 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1501 options->aead_xform.next = NULL;
1502 options->aead_key_param = 0;
1503 options->aead_key_random_size = -1;
1504 options->aead_xform.aead.key.length = 0;
1505 options->aead_iv_param = 0;
1506 options->aead_iv_random_size = -1;
1507 options->aead_iv.length = 0;
1509 options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1510 options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1512 options->aad_param = 0;
1513 options->aad_random_size = -1;
1514 options->aad.length = 0;
1516 options->digest_size = -1;
1518 options->type = CDEV_TYPE_ANY;
1519 options->cryptodev_mask = UINT64_MAX;
1521 options->mac_updating = 1;
1525 display_cipher_info(struct l2fwd_crypto_options *options)
1527 printf("\n---- Cipher information ---\n");
1528 printf("Algorithm: %s\n",
1529 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1530 rte_hexdump(stdout, "Cipher key:",
1531 options->cipher_xform.cipher.key.data,
1532 options->cipher_xform.cipher.key.length);
1533 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1537 display_auth_info(struct l2fwd_crypto_options *options)
1539 printf("\n---- Authentication information ---\n");
1540 printf("Algorithm: %s\n",
1541 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1542 rte_hexdump(stdout, "Auth key:",
1543 options->auth_xform.auth.key.data,
1544 options->auth_xform.auth.key.length);
1545 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1549 display_aead_info(struct l2fwd_crypto_options *options)
1551 printf("\n---- AEAD information ---\n");
1552 printf("Algorithm: %s\n",
1553 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1554 rte_hexdump(stdout, "AEAD key:",
1555 options->aead_xform.aead.key.data,
1556 options->aead_xform.aead.key.length);
1557 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1558 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1562 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1564 char string_cipher_op[MAX_STR_LEN];
1565 char string_auth_op[MAX_STR_LEN];
1566 char string_aead_op[MAX_STR_LEN];
1568 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1569 strcpy(string_cipher_op, "Encrypt");
1571 strcpy(string_cipher_op, "Decrypt");
1573 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1574 strcpy(string_auth_op, "Auth generate");
1576 strcpy(string_auth_op, "Auth verify");
1578 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1579 strcpy(string_aead_op, "Authenticated encryption");
1581 strcpy(string_aead_op, "Authenticated decryption");
1584 printf("Options:-\nn");
1585 printf("portmask: %x\n", options->portmask);
1586 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1587 printf("refresh period : %u\n", options->refresh_period);
1588 printf("single lcore mode: %s\n",
1589 options->single_lcore ? "enabled" : "disabled");
1590 printf("stats_printing: %s\n",
1591 options->refresh_period == 0 ? "disabled" : "enabled");
1593 printf("sessionless crypto: %s\n",
1594 options->sessionless ? "enabled" : "disabled");
1596 if (options->ckey_param && (options->ckey_random_size != -1))
1597 printf("Cipher key already parsed, ignoring size of random key\n");
1599 if (options->akey_param && (options->akey_random_size != -1))
1600 printf("Auth key already parsed, ignoring size of random key\n");
1602 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1603 printf("Cipher IV already parsed, ignoring size of random IV\n");
1605 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1606 printf("Auth IV already parsed, ignoring size of random IV\n");
1608 if (options->aad_param && (options->aad_random_size != -1))
1609 printf("AAD already parsed, ignoring size of random AAD\n");
1611 printf("\nCrypto chain: ");
1612 switch (options->xform_chain) {
1613 case L2FWD_CRYPTO_AEAD:
1614 printf("Input --> %s --> Output\n", string_aead_op);
1615 display_aead_info(options);
1617 case L2FWD_CRYPTO_CIPHER_HASH:
1618 printf("Input --> %s --> %s --> Output\n",
1619 string_cipher_op, string_auth_op);
1620 display_cipher_info(options);
1621 display_auth_info(options);
1623 case L2FWD_CRYPTO_HASH_CIPHER:
1624 printf("Input --> %s --> %s --> Output\n",
1625 string_auth_op, string_cipher_op);
1626 display_cipher_info(options);
1627 display_auth_info(options);
1629 case L2FWD_CRYPTO_HASH_ONLY:
1630 printf("Input --> %s --> Output\n", string_auth_op);
1631 display_auth_info(options);
1633 case L2FWD_CRYPTO_CIPHER_ONLY:
1634 printf("Input --> %s --> Output\n", string_cipher_op);
1635 display_cipher_info(options);
1640 /* Parse the argument given in the command line of the application */
1642 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1643 int argc, char **argv)
1645 int opt, retval, option_index;
1646 char **argvopt = argv, *prgname = argv[0];
1648 static struct option lgopts[] = {
1649 { "sessionless", no_argument, 0, 0 },
1651 { "cdev_type", required_argument, 0, 0 },
1652 { "chain", required_argument, 0, 0 },
1654 { "cipher_algo", required_argument, 0, 0 },
1655 { "cipher_op", required_argument, 0, 0 },
1656 { "cipher_key", required_argument, 0, 0 },
1657 { "cipher_key_random_size", required_argument, 0, 0 },
1658 { "cipher_iv", required_argument, 0, 0 },
1659 { "cipher_iv_random_size", required_argument, 0, 0 },
1661 { "auth_algo", required_argument, 0, 0 },
1662 { "auth_op", required_argument, 0, 0 },
1663 { "auth_key", required_argument, 0, 0 },
1664 { "auth_key_random_size", required_argument, 0, 0 },
1665 { "auth_iv", required_argument, 0, 0 },
1666 { "auth_iv_random_size", required_argument, 0, 0 },
1668 { "aead_algo", required_argument, 0, 0 },
1669 { "aead_op", required_argument, 0, 0 },
1670 { "aead_key", required_argument, 0, 0 },
1671 { "aead_key_random_size", required_argument, 0, 0 },
1672 { "aead_iv", required_argument, 0, 0 },
1673 { "aead_iv_random_size", required_argument, 0, 0 },
1675 { "aad", required_argument, 0, 0 },
1676 { "aad_random_size", required_argument, 0, 0 },
1678 { "digest_size", required_argument, 0, 0 },
1680 { "sessionless", no_argument, 0, 0 },
1681 { "cryptodev_mask", required_argument, 0, 0},
1683 { "mac-updating", no_argument, 0, 0},
1684 { "no-mac-updating", no_argument, 0, 0},
1689 l2fwd_crypto_default_options(options);
1691 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1692 &option_index)) != EOF) {
1696 retval = l2fwd_crypto_parse_args_long_options(options,
1697 lgopts, option_index);
1699 l2fwd_crypto_usage(prgname);
1706 retval = l2fwd_crypto_parse_portmask(options, optarg);
1708 l2fwd_crypto_usage(prgname);
1715 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1717 l2fwd_crypto_usage(prgname);
1724 options->single_lcore = 1;
1730 retval = l2fwd_crypto_parse_timer_period(options,
1733 l2fwd_crypto_usage(prgname);
1739 l2fwd_crypto_usage(prgname);
1746 argv[optind-1] = prgname;
1749 optind = 1; /* reset getopt lib */
1754 /* Check the link status of all ports in up to 9s, and print them finally */
1756 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1758 #define CHECK_INTERVAL 100 /* 100ms */
1759 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1761 uint8_t count, all_ports_up, print_flag = 0;
1762 struct rte_eth_link link;
1764 printf("\nChecking link status");
1766 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1768 for (portid = 0; portid < port_num; portid++) {
1769 if ((port_mask & (1 << portid)) == 0)
1771 memset(&link, 0, sizeof(link));
1772 rte_eth_link_get_nowait(portid, &link);
1773 /* print link status if flag set */
1774 if (print_flag == 1) {
1775 if (link.link_status)
1777 "Port%d Link Up. Speed %u Mbps - %s\n",
1778 portid, link.link_speed,
1779 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1780 ("full-duplex") : ("half-duplex\n"));
1782 printf("Port %d Link Down\n", portid);
1785 /* clear all_ports_up flag if any link down */
1786 if (link.link_status == ETH_LINK_DOWN) {
1791 /* after finally printing all link status, get out */
1792 if (print_flag == 1)
1795 if (all_ports_up == 0) {
1798 rte_delay_ms(CHECK_INTERVAL);
1801 /* set the print_flag if all ports up or timeout */
1802 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1809 /* Check if device has to be HW/SW or any */
1811 check_type(const struct l2fwd_crypto_options *options,
1812 const struct rte_cryptodev_info *dev_info)
1814 if (options->type == CDEV_TYPE_HW &&
1815 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1817 if (options->type == CDEV_TYPE_SW &&
1818 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1820 if (options->type == CDEV_TYPE_ANY)
1826 static const struct rte_cryptodev_capabilities *
1827 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1828 const struct rte_cryptodev_info *dev_info,
1832 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1833 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1834 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1835 options->cipher_xform.cipher.algo;
1837 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1838 cap_cipher_algo = cap->sym.cipher.algo;
1839 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1840 if (cap_cipher_algo == opt_cipher_algo) {
1841 if (check_type(options, dev_info) == 0)
1845 cap = &dev_info->capabilities[++i];
1848 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1849 printf("Algorithm %s not supported by cryptodev %u"
1850 " or device not of preferred type (%s)\n",
1851 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1853 options->string_type);
1860 static const struct rte_cryptodev_capabilities *
1861 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1862 const struct rte_cryptodev_info *dev_info,
1866 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1867 enum rte_crypto_auth_algorithm cap_auth_algo;
1868 enum rte_crypto_auth_algorithm opt_auth_algo =
1869 options->auth_xform.auth.algo;
1871 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1872 cap_auth_algo = cap->sym.auth.algo;
1873 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1874 if (cap_auth_algo == opt_auth_algo) {
1875 if (check_type(options, dev_info) == 0)
1879 cap = &dev_info->capabilities[++i];
1882 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1883 printf("Algorithm %s not supported by cryptodev %u"
1884 " or device not of preferred type (%s)\n",
1885 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1887 options->string_type);
1894 static const struct rte_cryptodev_capabilities *
1895 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1896 const struct rte_cryptodev_info *dev_info,
1900 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1901 enum rte_crypto_aead_algorithm cap_aead_algo;
1902 enum rte_crypto_aead_algorithm opt_aead_algo =
1903 options->aead_xform.aead.algo;
1905 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1906 cap_aead_algo = cap->sym.aead.algo;
1907 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1908 if (cap_aead_algo == opt_aead_algo) {
1909 if (check_type(options, dev_info) == 0)
1913 cap = &dev_info->capabilities[++i];
1916 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1917 printf("Algorithm %s not supported by cryptodev %u"
1918 " or device not of preferred type (%s)\n",
1919 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1921 options->string_type);
1928 /* Check if the device is enabled by cryptodev_mask */
1930 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1933 if (options->cryptodev_mask & (1 << cdev_id))
1940 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1946 if (increment == 0) {
1953 /* Range of values */
1954 for (supp_size = min; supp_size <= max; supp_size += increment) {
1955 if (length == supp_size)
1963 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1964 unsigned int iv_param, int iv_random_size,
1965 uint16_t *iv_length)
1968 * Check if length of provided IV is supported
1969 * by the algorithm chosen.
1972 if (check_supported_size(*iv_length,
1975 iv_range_size->increment)
1977 printf("Unsupported IV length\n");
1981 * Check if length of IV to be randomly generated
1982 * is supported by the algorithm chosen.
1984 } else if (iv_random_size != -1) {
1985 if (check_supported_size(iv_random_size,
1988 iv_range_size->increment)
1990 printf("Unsupported IV length\n");
1993 *iv_length = iv_random_size;
1994 /* No size provided, use minimum size. */
1996 *iv_length = iv_range_size->min;
2002 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2003 uint8_t *enabled_cdevs)
2005 unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
2006 const struct rte_cryptodev_capabilities *cap;
2007 unsigned int sess_sz, max_sess_sz = 0;
2010 cdev_count = rte_cryptodev_count();
2011 if (cdev_count == 0) {
2012 printf("No crypto devices available\n");
2016 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2017 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
2018 if (sess_sz > max_sess_sz)
2019 max_sess_sz = sess_sz;
2022 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2024 struct rte_cryptodev_qp_conf qp_conf;
2025 struct rte_cryptodev_info dev_info;
2026 retval = rte_cryptodev_socket_id(cdev_id);
2029 printf("Invalid crypto device id used\n");
2033 uint8_t socket_id = (uint8_t) retval;
2035 struct rte_cryptodev_config conf = {
2036 .nb_queue_pairs = 1,
2037 .socket_id = socket_id,
2040 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
2043 rte_cryptodev_info_get(cdev_id, &dev_info);
2045 if (session_pool_socket[socket_id] == NULL) {
2046 char mp_name[RTE_MEMPOOL_NAMESIZE];
2047 struct rte_mempool *sess_mp;
2049 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2050 "sess_mp_%u", socket_id);
2053 * Create enough objects for session headers and
2054 * device private data
2056 sess_mp = rte_mempool_create(mp_name,
2059 SESSION_POOL_CACHE_SIZE,
2060 0, NULL, NULL, NULL,
2064 if (sess_mp == NULL) {
2065 printf("Cannot create session pool on socket %d\n",
2070 printf("Allocated session pool on socket %d\n", socket_id);
2071 session_pool_socket[socket_id] = sess_mp;
2074 /* Set AEAD parameters */
2075 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2076 /* Check if device supports AEAD algo */
2077 cap = check_device_support_aead_algo(options, &dev_info,
2082 options->block_size = cap->sym.aead.block_size;
2084 check_iv_param(&cap->sym.aead.iv_size,
2085 options->aead_iv_param,
2086 options->aead_iv_random_size,
2087 &options->aead_iv.length);
2090 * Check if length of provided AEAD key is supported
2091 * by the algorithm chosen.
2093 if (options->aead_key_param) {
2094 if (check_supported_size(
2095 options->aead_xform.aead.key.length,
2096 cap->sym.aead.key_size.min,
2097 cap->sym.aead.key_size.max,
2098 cap->sym.aead.key_size.increment)
2100 printf("Unsupported aead key length\n");
2104 * Check if length of the aead key to be randomly generated
2105 * is supported by the algorithm chosen.
2107 } else if (options->aead_key_random_size != -1) {
2108 if (check_supported_size(options->aead_key_random_size,
2109 cap->sym.aead.key_size.min,
2110 cap->sym.aead.key_size.max,
2111 cap->sym.aead.key_size.increment)
2113 printf("Unsupported aead key length\n");
2116 options->aead_xform.aead.key.length =
2117 options->aead_key_random_size;
2118 /* No size provided, use minimum size. */
2120 options->aead_xform.aead.key.length =
2121 cap->sym.aead.key_size.min;
2123 if (!options->aead_key_param)
2124 generate_random_key(
2125 options->aead_xform.aead.key.data,
2126 options->aead_xform.aead.key.length);
2129 * Check if length of provided AAD is supported
2130 * by the algorithm chosen.
2132 if (options->aad_param) {
2133 if (check_supported_size(options->aad.length,
2134 cap->sym.aead.aad_size.min,
2135 cap->sym.aead.aad_size.max,
2136 cap->sym.aead.aad_size.increment)
2138 printf("Unsupported AAD length\n");
2142 * Check if length of AAD to be randomly generated
2143 * is supported by the algorithm chosen.
2145 } else if (options->aad_random_size != -1) {
2146 if (check_supported_size(options->aad_random_size,
2147 cap->sym.aead.aad_size.min,
2148 cap->sym.aead.aad_size.max,
2149 cap->sym.aead.aad_size.increment)
2151 printf("Unsupported AAD length\n");
2154 options->aad.length = options->aad_random_size;
2155 /* No size provided, use minimum size. */
2157 options->aad.length = cap->sym.auth.aad_size.min;
2159 options->aead_xform.aead.aad_length =
2160 options->aad.length;
2162 /* Check if digest size is supported by the algorithm. */
2163 if (options->digest_size != -1) {
2164 if (check_supported_size(options->digest_size,
2165 cap->sym.aead.digest_size.min,
2166 cap->sym.aead.digest_size.max,
2167 cap->sym.aead.digest_size.increment)
2169 printf("Unsupported digest length\n");
2172 options->aead_xform.aead.digest_length =
2173 options->digest_size;
2174 /* No size provided, use minimum size. */
2176 options->aead_xform.aead.digest_length =
2177 cap->sym.aead.digest_size.min;
2180 /* Set cipher parameters */
2181 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2182 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2183 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2184 /* Check if device supports cipher algo */
2185 cap = check_device_support_cipher_algo(options, &dev_info,
2190 options->block_size = cap->sym.cipher.block_size;
2192 check_iv_param(&cap->sym.cipher.iv_size,
2193 options->cipher_iv_param,
2194 options->cipher_iv_random_size,
2195 &options->cipher_iv.length);
2198 * Check if length of provided cipher key is supported
2199 * by the algorithm chosen.
2201 if (options->ckey_param) {
2202 if (check_supported_size(
2203 options->cipher_xform.cipher.key.length,
2204 cap->sym.cipher.key_size.min,
2205 cap->sym.cipher.key_size.max,
2206 cap->sym.cipher.key_size.increment)
2208 printf("Unsupported cipher key length\n");
2212 * Check if length of the cipher key to be randomly generated
2213 * is supported by the algorithm chosen.
2215 } else if (options->ckey_random_size != -1) {
2216 if (check_supported_size(options->ckey_random_size,
2217 cap->sym.cipher.key_size.min,
2218 cap->sym.cipher.key_size.max,
2219 cap->sym.cipher.key_size.increment)
2221 printf("Unsupported cipher key length\n");
2224 options->cipher_xform.cipher.key.length =
2225 options->ckey_random_size;
2226 /* No size provided, use minimum size. */
2228 options->cipher_xform.cipher.key.length =
2229 cap->sym.cipher.key_size.min;
2231 if (!options->ckey_param)
2232 generate_random_key(
2233 options->cipher_xform.cipher.key.data,
2234 options->cipher_xform.cipher.key.length);
2238 /* Set auth parameters */
2239 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2240 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2241 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2242 /* Check if device supports auth algo */
2243 cap = check_device_support_auth_algo(options, &dev_info,
2248 check_iv_param(&cap->sym.auth.iv_size,
2249 options->auth_iv_param,
2250 options->auth_iv_random_size,
2251 &options->auth_iv.length);
2253 * Check if length of provided auth key is supported
2254 * by the algorithm chosen.
2256 if (options->akey_param) {
2257 if (check_supported_size(
2258 options->auth_xform.auth.key.length,
2259 cap->sym.auth.key_size.min,
2260 cap->sym.auth.key_size.max,
2261 cap->sym.auth.key_size.increment)
2263 printf("Unsupported auth key length\n");
2267 * Check if length of the auth key to be randomly generated
2268 * is supported by the algorithm chosen.
2270 } else if (options->akey_random_size != -1) {
2271 if (check_supported_size(options->akey_random_size,
2272 cap->sym.auth.key_size.min,
2273 cap->sym.auth.key_size.max,
2274 cap->sym.auth.key_size.increment)
2276 printf("Unsupported auth key length\n");
2279 options->auth_xform.auth.key.length =
2280 options->akey_random_size;
2281 /* No size provided, use minimum size. */
2283 options->auth_xform.auth.key.length =
2284 cap->sym.auth.key_size.min;
2286 if (!options->akey_param)
2287 generate_random_key(
2288 options->auth_xform.auth.key.data,
2289 options->auth_xform.auth.key.length);
2291 /* Check if digest size is supported by the algorithm. */
2292 if (options->digest_size != -1) {
2293 if (check_supported_size(options->digest_size,
2294 cap->sym.auth.digest_size.min,
2295 cap->sym.auth.digest_size.max,
2296 cap->sym.auth.digest_size.increment)
2298 printf("Unsupported digest length\n");
2301 options->auth_xform.auth.digest_length =
2302 options->digest_size;
2303 /* No size provided, use minimum size. */
2305 options->auth_xform.auth.digest_length =
2306 cap->sym.auth.digest_size.min;
2309 retval = rte_cryptodev_configure(cdev_id, &conf);
2311 printf("Failed to configure cryptodev %u", cdev_id);
2315 qp_conf.nb_descriptors = 2048;
2317 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2318 socket_id, session_pool_socket[socket_id]);
2320 printf("Failed to setup queue pair %u on cryptodev %u",
2325 retval = rte_cryptodev_start(cdev_id);
2327 printf("Failed to start device %u: error %d\n",
2332 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2334 enabled_cdevs[cdev_id] = 1;
2335 enabled_cdev_count++;
2338 return enabled_cdev_count;
2342 initialize_ports(struct l2fwd_crypto_options *options)
2344 uint16_t last_portid, portid;
2345 unsigned enabled_portcount = 0;
2346 unsigned nb_ports = rte_eth_dev_count();
2348 if (nb_ports == 0) {
2349 printf("No Ethernet ports - bye\n");
2353 /* Reset l2fwd_dst_ports */
2354 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2355 l2fwd_dst_ports[portid] = 0;
2357 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2360 /* Skip ports that are not enabled */
2361 if ((options->portmask & (1 << portid)) == 0)
2365 printf("Initializing port %u... ", portid);
2367 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2369 printf("Cannot configure device: err=%d, port=%u\n",
2374 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2377 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2382 /* init one RX queue */
2384 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2385 rte_eth_dev_socket_id(portid),
2386 NULL, l2fwd_pktmbuf_pool);
2388 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2393 /* init one TX queue on each port */
2395 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2396 rte_eth_dev_socket_id(portid),
2399 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2406 retval = rte_eth_dev_start(portid);
2408 printf("rte_eth_dev_start:err=%d, port=%u\n",
2413 rte_eth_promiscuous_enable(portid);
2415 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2417 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2419 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2420 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2421 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2422 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2423 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2424 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2426 /* initialize port stats */
2427 memset(&port_statistics, 0, sizeof(port_statistics));
2429 /* Setup port forwarding table */
2430 if (enabled_portcount % 2) {
2431 l2fwd_dst_ports[portid] = last_portid;
2432 l2fwd_dst_ports[last_portid] = portid;
2434 last_portid = portid;
2437 l2fwd_enabled_port_mask |= (1 << portid);
2438 enabled_portcount++;
2441 if (enabled_portcount == 1) {
2442 l2fwd_dst_ports[last_portid] = last_portid;
2443 } else if (enabled_portcount % 2) {
2444 printf("odd number of ports in portmask- bye\n");
2448 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2450 return enabled_portcount;
2454 reserve_key_memory(struct l2fwd_crypto_options *options)
2456 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2458 if (options->cipher_xform.cipher.key.data == NULL)
2459 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2461 options->auth_xform.auth.key.data = rte_malloc("auth key",
2463 if (options->auth_xform.auth.key.data == NULL)
2464 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2466 options->aead_xform.aead.key.data = rte_malloc("aead key",
2468 if (options->aead_xform.aead.key.data == NULL)
2469 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2471 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2472 if (options->cipher_iv.data == NULL)
2473 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2475 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2476 if (options->auth_iv.data == NULL)
2477 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2479 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2480 if (options->aead_iv.data == NULL)
2481 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2483 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2484 if (options->aad.data == NULL)
2485 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2486 options->aad.phys_addr = rte_malloc_virt2iova(options->aad.data);
2490 main(int argc, char **argv)
2492 struct lcore_queue_conf *qconf;
2493 struct l2fwd_crypto_options options;
2495 uint8_t nb_cryptodevs, cdev_id;
2496 uint16_t nb_ports, portid;
2497 unsigned lcore_id, rx_lcore_id;
2498 int ret, enabled_cdevcount, enabled_portcount;
2499 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2502 ret = rte_eal_init(argc, argv);
2504 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2508 /* reserve memory for Cipher/Auth key and IV */
2509 reserve_key_memory(&options);
2511 /* parse application arguments (after the EAL ones) */
2512 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2514 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2516 printf("MAC updating %s\n",
2517 options.mac_updating ? "enabled" : "disabled");
2519 /* create the mbuf pool */
2520 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2521 sizeof(struct rte_crypto_op),
2522 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2523 if (l2fwd_pktmbuf_pool == NULL)
2524 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2526 /* create crypto op pool */
2527 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2528 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2530 if (l2fwd_crypto_op_pool == NULL)
2531 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2533 /* Enable Ethernet ports */
2534 enabled_portcount = initialize_ports(&options);
2535 if (enabled_portcount < 1)
2536 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2538 nb_ports = rte_eth_dev_count();
2539 /* Initialize the port/queue configuration of each logical core */
2540 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2541 portid < nb_ports; portid++) {
2543 /* skip ports that are not enabled */
2544 if ((options.portmask & (1 << portid)) == 0)
2547 if (options.single_lcore && qconf == NULL) {
2548 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2550 if (rx_lcore_id >= RTE_MAX_LCORE)
2551 rte_exit(EXIT_FAILURE,
2552 "Not enough cores\n");
2554 } else if (!options.single_lcore) {
2555 /* get the lcore_id for this port */
2556 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2557 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2558 options.nb_ports_per_lcore) {
2560 if (rx_lcore_id >= RTE_MAX_LCORE)
2561 rte_exit(EXIT_FAILURE,
2562 "Not enough cores\n");
2566 /* Assigned a new logical core in the loop above. */
2567 if (qconf != &lcore_queue_conf[rx_lcore_id])
2568 qconf = &lcore_queue_conf[rx_lcore_id];
2570 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2571 qconf->nb_rx_ports++;
2573 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2576 /* Enable Crypto devices */
2577 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2579 if (enabled_cdevcount < 0)
2580 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2582 if (enabled_cdevcount < enabled_portcount)
2583 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2584 "has to be more or equal to number of ports (%d)\n",
2585 enabled_cdevcount, enabled_portcount);
2587 nb_cryptodevs = rte_cryptodev_count();
2589 /* Initialize the port/cryptodev configuration of each logical core */
2590 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2591 cdev_id < nb_cryptodevs && enabled_cdevcount;
2593 /* Crypto op not supported by crypto device */
2594 if (!enabled_cdevs[cdev_id])
2597 if (options.single_lcore && qconf == NULL) {
2598 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2600 if (rx_lcore_id >= RTE_MAX_LCORE)
2601 rte_exit(EXIT_FAILURE,
2602 "Not enough cores\n");
2604 } else if (!options.single_lcore) {
2605 /* get the lcore_id for this port */
2606 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2607 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2608 options.nb_ports_per_lcore) {
2610 if (rx_lcore_id >= RTE_MAX_LCORE)
2611 rte_exit(EXIT_FAILURE,
2612 "Not enough cores\n");
2616 /* Assigned a new logical core in the loop above. */
2617 if (qconf != &lcore_queue_conf[rx_lcore_id])
2618 qconf = &lcore_queue_conf[rx_lcore_id];
2620 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2621 qconf->nb_crypto_devs++;
2623 enabled_cdevcount--;
2625 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2629 /* launch per-lcore init on every lcore */
2630 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2632 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2633 if (rte_eal_wait_lcore(lcore_id) < 0)