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_memzone.h>
72 #include <rte_per_lcore.h>
73 #include <rte_prefetch.h>
74 #include <rte_random.h>
75 #include <rte_hexdump.h>
83 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
87 #define MAX_STR_LEN 32
88 #define MAX_KEY_SIZE 128
89 #define MAX_IV_SIZE 16
90 #define MAX_AAD_SIZE 65535
91 #define MAX_PKT_BURST 32
92 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
93 #define MAX_SESSIONS 32
94 #define SESSION_POOL_CACHE_SIZE 0
96 #define MAXIMUM_IV_LENGTH 16
97 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
98 sizeof(struct rte_crypto_sym_op))
101 * Configurable number of RX/TX ring descriptors
103 #define RTE_TEST_RX_DESC_DEFAULT 128
104 #define RTE_TEST_TX_DESC_DEFAULT 512
106 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
107 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
109 /* ethernet addresses of ports */
110 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
112 /* mask of enabled ports */
113 static uint64_t l2fwd_enabled_port_mask;
114 static uint64_t l2fwd_enabled_crypto_mask;
116 /* list of enabled ports */
117 static uint16_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
122 struct rte_mbuf *buffer[MAX_PKT_BURST];
127 struct rte_crypto_op *buffer[MAX_PKT_BURST];
130 #define MAX_RX_QUEUE_PER_LCORE 16
131 #define MAX_TX_QUEUE_PER_PORT 16
133 enum l2fwd_crypto_xform_chain {
134 L2FWD_CRYPTO_CIPHER_HASH,
135 L2FWD_CRYPTO_HASH_CIPHER,
136 L2FWD_CRYPTO_CIPHER_ONLY,
137 L2FWD_CRYPTO_HASH_ONLY,
144 phys_addr_t phys_addr;
152 /** l2fwd crypto application command line options */
153 struct l2fwd_crypto_options {
155 unsigned nb_ports_per_lcore;
156 unsigned refresh_period;
157 unsigned single_lcore:1;
160 unsigned sessionless:1;
162 enum l2fwd_crypto_xform_chain xform_chain;
164 struct rte_crypto_sym_xform cipher_xform;
166 int ckey_random_size;
168 struct l2fwd_iv cipher_iv;
169 unsigned int cipher_iv_param;
170 int cipher_iv_random_size;
172 struct rte_crypto_sym_xform auth_xform;
174 int akey_random_size;
176 struct l2fwd_iv auth_iv;
177 unsigned int auth_iv_param;
178 int auth_iv_random_size;
180 struct rte_crypto_sym_xform aead_xform;
181 unsigned int aead_key_param;
182 int aead_key_random_size;
184 struct l2fwd_iv aead_iv;
185 unsigned int aead_iv_param;
186 int aead_iv_random_size;
188 struct l2fwd_key aad;
195 char string_type[MAX_STR_LEN];
197 uint64_t cryptodev_mask;
199 unsigned int mac_updating;
202 /** l2fwd crypto lcore params */
203 struct l2fwd_crypto_params {
207 unsigned digest_length;
210 struct l2fwd_iv cipher_iv;
211 struct l2fwd_iv auth_iv;
212 struct l2fwd_iv aead_iv;
213 struct l2fwd_key aad;
214 struct rte_cryptodev_sym_session *session;
221 enum rte_crypto_cipher_algorithm cipher_algo;
222 enum rte_crypto_auth_algorithm auth_algo;
223 enum rte_crypto_aead_algorithm aead_algo;
226 /** lcore configuration */
227 struct lcore_queue_conf {
228 unsigned nb_rx_ports;
229 uint16_t rx_port_list[MAX_RX_QUEUE_PER_LCORE];
231 unsigned nb_crypto_devs;
232 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
234 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
235 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
236 } __rte_cache_aligned;
238 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
240 static const struct rte_eth_conf port_conf = {
242 .mq_mode = ETH_MQ_RX_NONE,
243 .max_rx_pkt_len = ETHER_MAX_LEN,
245 .header_split = 0, /**< Header Split disabled */
246 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
247 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
248 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
249 .hw_strip_crc = 1, /**< CRC stripped by hardware */
252 .mq_mode = ETH_MQ_TX_NONE,
256 struct rte_mempool *l2fwd_pktmbuf_pool;
257 struct rte_mempool *l2fwd_crypto_op_pool;
258 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
260 /* Per-port statistics struct */
261 struct l2fwd_port_statistics {
265 uint64_t crypto_enqueued;
266 uint64_t crypto_dequeued;
269 } __rte_cache_aligned;
271 struct l2fwd_crypto_statistics {
276 } __rte_cache_aligned;
278 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
279 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
281 /* A tsc-based timer responsible for triggering statistics printout */
282 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
283 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
285 /* default period is 10 seconds */
286 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
288 /* Print out statistics on packets dropped */
292 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
293 uint64_t total_packets_enqueued, total_packets_dequeued,
294 total_packets_errors;
298 total_packets_dropped = 0;
299 total_packets_tx = 0;
300 total_packets_rx = 0;
301 total_packets_enqueued = 0;
302 total_packets_dequeued = 0;
303 total_packets_errors = 0;
305 const char clr[] = { 27, '[', '2', 'J', '\0' };
306 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
308 /* Clear screen and move to top left */
309 printf("%s%s", clr, topLeft);
311 printf("\nPort statistics ====================================");
313 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
314 /* skip disabled ports */
315 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
317 printf("\nStatistics for port %u ------------------------------"
318 "\nPackets sent: %32"PRIu64
319 "\nPackets received: %28"PRIu64
320 "\nPackets dropped: %29"PRIu64,
322 port_statistics[portid].tx,
323 port_statistics[portid].rx,
324 port_statistics[portid].dropped);
326 total_packets_dropped += port_statistics[portid].dropped;
327 total_packets_tx += port_statistics[portid].tx;
328 total_packets_rx += port_statistics[portid].rx;
330 printf("\nCrypto statistics ==================================");
332 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
333 /* skip disabled ports */
334 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
336 printf("\nStatistics for cryptodev %"PRIu64
337 " -------------------------"
338 "\nPackets enqueued: %28"PRIu64
339 "\nPackets dequeued: %28"PRIu64
340 "\nPackets errors: %30"PRIu64,
342 crypto_statistics[cdevid].enqueued,
343 crypto_statistics[cdevid].dequeued,
344 crypto_statistics[cdevid].errors);
346 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
347 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
348 total_packets_errors += crypto_statistics[cdevid].errors;
350 printf("\nAggregate statistics ==============================="
351 "\nTotal packets received: %22"PRIu64
352 "\nTotal packets enqueued: %22"PRIu64
353 "\nTotal packets dequeued: %22"PRIu64
354 "\nTotal packets sent: %26"PRIu64
355 "\nTotal packets dropped: %23"PRIu64
356 "\nTotal packets crypto errors: %17"PRIu64,
358 total_packets_enqueued,
359 total_packets_dequeued,
361 total_packets_dropped,
362 total_packets_errors);
363 printf("\n====================================================\n");
367 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
368 struct l2fwd_crypto_params *cparams)
370 struct rte_crypto_op **op_buffer;
373 op_buffer = (struct rte_crypto_op **)
374 qconf->op_buf[cparams->dev_id].buffer;
376 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
377 cparams->qp_id, op_buffer, (uint16_t) n);
379 crypto_statistics[cparams->dev_id].enqueued += ret;
380 if (unlikely(ret < n)) {
381 crypto_statistics[cparams->dev_id].errors += (n - ret);
383 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
384 rte_crypto_op_free(op_buffer[ret]);
392 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
393 struct l2fwd_crypto_params *cparams)
395 unsigned lcore_id, len;
396 struct lcore_queue_conf *qconf;
398 lcore_id = rte_lcore_id();
400 qconf = &lcore_queue_conf[lcore_id];
401 len = qconf->op_buf[cparams->dev_id].len;
402 qconf->op_buf[cparams->dev_id].buffer[len] = op;
405 /* enough ops to be sent */
406 if (len == MAX_PKT_BURST) {
407 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
411 qconf->op_buf[cparams->dev_id].len = len;
416 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
417 struct rte_crypto_op *op,
418 struct l2fwd_crypto_params *cparams)
420 struct ether_hdr *eth_hdr;
421 struct ipv4_hdr *ip_hdr;
423 uint32_t ipdata_offset, data_len;
424 uint32_t pad_len = 0;
427 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
429 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
432 ipdata_offset = sizeof(struct ether_hdr);
434 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
437 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
438 * IPV4_IHL_MULTIPLIER;
441 /* Zero pad data to be crypto'd so it is block aligned */
442 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
444 if (cparams->do_hash && cparams->hash_verify)
445 data_len -= cparams->digest_length;
447 if (cparams->do_cipher) {
449 * Following algorithms are block cipher algorithms,
450 * and might need padding
452 switch (cparams->cipher_algo) {
453 case RTE_CRYPTO_CIPHER_AES_CBC:
454 case RTE_CRYPTO_CIPHER_AES_ECB:
455 case RTE_CRYPTO_CIPHER_DES_CBC:
456 case RTE_CRYPTO_CIPHER_3DES_CBC:
457 case RTE_CRYPTO_CIPHER_3DES_ECB:
458 if (data_len % cparams->block_size)
459 pad_len = cparams->block_size -
460 (data_len % cparams->block_size);
467 padding = rte_pktmbuf_append(m, pad_len);
468 if (unlikely(!padding))
472 memset(padding, 0, pad_len);
476 /* Set crypto operation data parameters */
477 rte_crypto_op_attach_sym_session(op, cparams->session);
479 if (cparams->do_hash) {
480 if (cparams->auth_iv.length) {
481 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
484 cparams->cipher_iv.length);
486 * Copy IV at the end of the crypto operation,
487 * after the cipher IV, if added
489 rte_memcpy(iv_ptr, cparams->auth_iv.data,
490 cparams->auth_iv.length);
492 if (!cparams->hash_verify) {
493 /* Append space for digest to end of packet */
494 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
495 cparams->digest_length);
497 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
498 uint8_t *) + ipdata_offset + data_len;
501 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
502 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
504 /* For wireless algorithms, offset/length must be in bits */
505 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
506 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
507 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
508 op->sym->auth.data.offset = ipdata_offset << 3;
509 op->sym->auth.data.length = data_len << 3;
511 op->sym->auth.data.offset = ipdata_offset;
512 op->sym->auth.data.length = data_len;
516 if (cparams->do_cipher) {
517 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
519 /* Copy IV at the end of the crypto operation */
520 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
521 cparams->cipher_iv.length);
523 /* For wireless algorithms, offset/length must be in bits */
524 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
525 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
526 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
527 op->sym->cipher.data.offset = ipdata_offset << 3;
528 op->sym->cipher.data.length = data_len << 3;
530 op->sym->cipher.data.offset = ipdata_offset;
531 op->sym->cipher.data.length = data_len;
535 if (cparams->do_aead) {
536 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
538 /* Copy IV at the end of the crypto operation */
540 * If doing AES-CCM, nonce is copied one byte
541 * after the start of IV field
543 if (cparams->aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
544 rte_memcpy(iv_ptr + 1, cparams->aead_iv.data,
545 cparams->aead_iv.length);
547 rte_memcpy(iv_ptr, cparams->aead_iv.data,
548 cparams->aead_iv.length);
550 op->sym->aead.data.offset = ipdata_offset;
551 op->sym->aead.data.length = data_len;
553 if (!cparams->hash_verify) {
554 /* Append space for digest to end of packet */
555 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
556 cparams->digest_length);
558 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
559 uint8_t *) + ipdata_offset + data_len;
562 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
563 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
565 if (cparams->aad.length) {
566 op->sym->aead.aad.data = cparams->aad.data;
567 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
573 return l2fwd_crypto_enqueue(op, cparams);
577 /* Send the burst of packets on an output interface */
579 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
582 struct rte_mbuf **pkt_buffer;
585 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
587 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
588 port_statistics[port].tx += ret;
589 if (unlikely(ret < n)) {
590 port_statistics[port].dropped += (n - ret);
592 rte_pktmbuf_free(pkt_buffer[ret]);
599 /* Enqueue packets for TX and prepare them to be sent */
601 l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
603 unsigned lcore_id, len;
604 struct lcore_queue_conf *qconf;
606 lcore_id = rte_lcore_id();
608 qconf = &lcore_queue_conf[lcore_id];
609 len = qconf->pkt_buf[port].len;
610 qconf->pkt_buf[port].buffer[len] = m;
613 /* enough pkts to be sent */
614 if (unlikely(len == MAX_PKT_BURST)) {
615 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
619 qconf->pkt_buf[port].len = len;
624 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
626 struct ether_hdr *eth;
629 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
631 /* 02:00:00:00:00:xx */
632 tmp = ð->d_addr.addr_bytes[0];
633 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
636 ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], ð->s_addr);
640 l2fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
641 struct l2fwd_crypto_options *options)
645 dst_port = l2fwd_dst_ports[portid];
647 if (options->mac_updating)
648 l2fwd_mac_updating(m, dst_port);
650 l2fwd_send_packet(m, dst_port);
653 /** Generate random key */
655 generate_random_key(uint8_t *key, unsigned length)
660 fd = open("/dev/urandom", O_RDONLY);
662 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
664 ret = read(fd, key, length);
667 if (ret != (signed)length)
668 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
671 static struct rte_cryptodev_sym_session *
672 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
674 struct rte_crypto_sym_xform *first_xform;
675 struct rte_cryptodev_sym_session *session;
676 int retval = rte_cryptodev_socket_id(cdev_id);
681 uint8_t socket_id = (uint8_t) retval;
682 struct rte_mempool *sess_mp = session_pool_socket[socket_id];
684 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
685 first_xform = &options->aead_xform;
686 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
687 first_xform = &options->cipher_xform;
688 first_xform->next = &options->auth_xform;
689 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
690 first_xform = &options->auth_xform;
691 first_xform->next = &options->cipher_xform;
692 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
693 first_xform = &options->cipher_xform;
695 first_xform = &options->auth_xform;
698 session = rte_cryptodev_sym_session_create(sess_mp);
703 if (rte_cryptodev_sym_session_init(cdev_id, session,
704 first_xform, sess_mp) < 0)
711 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
713 /* main processing loop */
715 l2fwd_main_loop(struct l2fwd_crypto_options *options)
717 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
718 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
720 unsigned lcore_id = rte_lcore_id();
721 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
722 unsigned int i, j, nb_rx, len;
724 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
725 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
726 US_PER_S * BURST_TX_DRAIN_US;
727 struct l2fwd_crypto_params *cparams;
728 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
729 struct rte_cryptodev_sym_session *session;
731 if (qconf->nb_rx_ports == 0) {
732 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
736 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
738 for (i = 0; i < qconf->nb_rx_ports; i++) {
740 portid = qconf->rx_port_list[i];
741 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
745 for (i = 0; i < qconf->nb_crypto_devs; i++) {
746 port_cparams[i].do_cipher = 0;
747 port_cparams[i].do_hash = 0;
748 port_cparams[i].do_aead = 0;
750 switch (options->xform_chain) {
751 case L2FWD_CRYPTO_AEAD:
752 port_cparams[i].do_aead = 1;
754 case L2FWD_CRYPTO_CIPHER_HASH:
755 case L2FWD_CRYPTO_HASH_CIPHER:
756 port_cparams[i].do_cipher = 1;
757 port_cparams[i].do_hash = 1;
759 case L2FWD_CRYPTO_HASH_ONLY:
760 port_cparams[i].do_hash = 1;
762 case L2FWD_CRYPTO_CIPHER_ONLY:
763 port_cparams[i].do_cipher = 1;
767 port_cparams[i].dev_id = qconf->cryptodev_list[i];
768 port_cparams[i].qp_id = 0;
770 port_cparams[i].block_size = options->block_size;
772 if (port_cparams[i].do_hash) {
773 port_cparams[i].auth_iv.data = options->auth_iv.data;
774 port_cparams[i].auth_iv.length = options->auth_iv.length;
775 if (!options->auth_iv_param)
776 generate_random_key(port_cparams[i].auth_iv.data,
777 port_cparams[i].auth_iv.length);
778 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
779 port_cparams[i].hash_verify = 1;
781 port_cparams[i].hash_verify = 0;
783 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
784 port_cparams[i].digest_length =
785 options->auth_xform.auth.digest_length;
786 /* Set IV parameters */
787 if (options->auth_iv.length) {
788 options->auth_xform.auth.iv.offset =
789 IV_OFFSET + options->cipher_iv.length;
790 options->auth_xform.auth.iv.length =
791 options->auth_iv.length;
795 if (port_cparams[i].do_aead) {
796 port_cparams[i].aead_iv.data = options->aead_iv.data;
797 port_cparams[i].aead_iv.length = options->aead_iv.length;
798 if (!options->aead_iv_param)
799 generate_random_key(port_cparams[i].aead_iv.data,
800 port_cparams[i].aead_iv.length);
801 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
802 port_cparams[i].digest_length =
803 options->aead_xform.aead.digest_length;
804 if (options->aead_xform.aead.aad_length) {
805 port_cparams[i].aad.data = options->aad.data;
806 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
807 port_cparams[i].aad.length = options->aad.length;
808 if (!options->aad_param)
809 generate_random_key(port_cparams[i].aad.data,
810 port_cparams[i].aad.length);
812 * If doing AES-CCM, first 18 bytes has to be reserved,
813 * and actual AAD should start from byte 18
815 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
816 memmove(port_cparams[i].aad.data + 18,
817 port_cparams[i].aad.data,
818 port_cparams[i].aad.length);
821 port_cparams[i].aad.length = 0;
823 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
824 port_cparams[i].hash_verify = 1;
826 port_cparams[i].hash_verify = 0;
828 /* Set IV parameters */
829 options->aead_xform.aead.iv.offset = IV_OFFSET;
830 options->aead_xform.aead.iv.length = options->aead_iv.length;
833 if (port_cparams[i].do_cipher) {
834 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
835 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
836 if (!options->cipher_iv_param)
837 generate_random_key(port_cparams[i].cipher_iv.data,
838 port_cparams[i].cipher_iv.length);
840 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
841 /* Set IV parameters */
842 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
843 options->cipher_xform.cipher.iv.length =
844 options->cipher_iv.length;
847 session = initialize_crypto_session(options,
848 port_cparams[i].dev_id);
850 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
852 port_cparams[i].session = session;
854 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
855 port_cparams[i].dev_id);
858 l2fwd_crypto_options_print(options);
861 * Initialize previous tsc timestamp before the loop,
862 * to avoid showing the port statistics immediately,
863 * so user can see the crypto information.
865 prev_tsc = rte_rdtsc();
868 cur_tsc = rte_rdtsc();
871 * Crypto device/TX burst queue drain
873 diff_tsc = cur_tsc - prev_tsc;
874 if (unlikely(diff_tsc > drain_tsc)) {
875 /* Enqueue all crypto ops remaining in buffers */
876 for (i = 0; i < qconf->nb_crypto_devs; i++) {
877 cparams = &port_cparams[i];
878 len = qconf->op_buf[cparams->dev_id].len;
879 l2fwd_crypto_send_burst(qconf, len, cparams);
880 qconf->op_buf[cparams->dev_id].len = 0;
882 /* Transmit all packets remaining in buffers */
883 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
884 if (qconf->pkt_buf[portid].len == 0)
886 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
887 qconf->pkt_buf[portid].len,
889 qconf->pkt_buf[portid].len = 0;
892 /* if timer is enabled */
893 if (timer_period > 0) {
895 /* advance the timer */
896 timer_tsc += diff_tsc;
898 /* if timer has reached its timeout */
899 if (unlikely(timer_tsc >=
900 (uint64_t)timer_period)) {
902 /* do this only on master core */
903 if (lcore_id == rte_get_master_lcore()
904 && options->refresh_period) {
915 * Read packet from RX queues
917 for (i = 0; i < qconf->nb_rx_ports; i++) {
918 portid = qconf->rx_port_list[i];
920 cparams = &port_cparams[i];
922 nb_rx = rte_eth_rx_burst(portid, 0,
923 pkts_burst, MAX_PKT_BURST);
925 port_statistics[portid].rx += nb_rx;
929 * If we can't allocate a crypto_ops, then drop
930 * the rest of the burst and dequeue and
931 * process the packets to free offload structs
933 if (rte_crypto_op_bulk_alloc(
934 l2fwd_crypto_op_pool,
935 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
938 for (j = 0; j < nb_rx; j++)
939 rte_pktmbuf_free(pkts_burst[j]);
944 /* Enqueue packets from Crypto device*/
945 for (j = 0; j < nb_rx; j++) {
948 l2fwd_simple_crypto_enqueue(m,
949 ops_burst[j], cparams);
953 /* Dequeue packets from Crypto device */
955 nb_rx = rte_cryptodev_dequeue_burst(
956 cparams->dev_id, cparams->qp_id,
957 ops_burst, MAX_PKT_BURST);
959 crypto_statistics[cparams->dev_id].dequeued +=
962 /* Forward crypto'd packets */
963 for (j = 0; j < nb_rx; j++) {
964 m = ops_burst[j]->sym->m_src;
966 rte_crypto_op_free(ops_burst[j]);
967 l2fwd_simple_forward(m, portid,
970 } while (nb_rx == MAX_PKT_BURST);
976 l2fwd_launch_one_lcore(void *arg)
978 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
982 /* Display command line arguments usage */
984 l2fwd_crypto_usage(const char *prgname)
986 printf("%s [EAL options] --\n"
987 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
988 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
989 " -s manage all ports from single lcore\n"
990 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
991 " (0 to disable, 10 default, 86400 maximum)\n"
993 " --cdev_type HW / SW / ANY\n"
994 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
995 " HASH_ONLY / AEAD\n"
997 " --cipher_algo ALGO\n"
998 " --cipher_op ENCRYPT / DECRYPT\n"
999 " --cipher_key KEY (bytes separated with \":\")\n"
1000 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
1001 " --cipher_iv IV (bytes separated with \":\")\n"
1002 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1004 " --auth_algo ALGO\n"
1005 " --auth_op GENERATE / VERIFY\n"
1006 " --auth_key KEY (bytes separated with \":\")\n"
1007 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1008 " --auth_iv IV (bytes separated with \":\")\n"
1009 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
1011 " --aead_algo ALGO\n"
1012 " --aead_op ENCRYPT / DECRYPT\n"
1013 " --aead_key KEY (bytes separated with \":\")\n"
1014 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
1015 " --aead_iv IV (bytes separated with \":\")\n"
1016 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1017 " --aad AAD (bytes separated with \":\")\n"
1018 " --aad_random_size SIZE: size of AAD when generated randomly\n"
1020 " --digest_size SIZE: size of digest to be generated/verified\n"
1023 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1025 " --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1027 " - The source MAC address is replaced by the TX port MAC address\n"
1028 " - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1032 /** Parse crypto device type command line argument */
1034 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1036 if (strcmp("HW", optarg) == 0) {
1037 *type = CDEV_TYPE_HW;
1039 } else if (strcmp("SW", optarg) == 0) {
1040 *type = CDEV_TYPE_SW;
1042 } else if (strcmp("ANY", optarg) == 0) {
1043 *type = CDEV_TYPE_ANY;
1050 /** Parse crypto chain xform command line argument */
1052 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1054 if (strcmp("CIPHER_HASH", optarg) == 0) {
1055 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1057 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1058 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1060 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1061 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1063 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1064 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1066 } else if (strcmp("AEAD", optarg) == 0) {
1067 options->xform_chain = L2FWD_CRYPTO_AEAD;
1074 /** Parse crypto cipher algo option command line argument */
1076 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1079 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1080 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1081 "not supported!\n");
1088 /** Parse crypto cipher operation command line argument */
1090 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1092 if (strcmp("ENCRYPT", optarg) == 0) {
1093 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1095 } else if (strcmp("DECRYPT", optarg) == 0) {
1096 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1100 printf("Cipher operation not supported!\n");
1104 /** Parse bytes from command line argument */
1106 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1108 unsigned byte_count;
1112 for (byte_count = 0, token = strtok(input_arg, ":");
1113 (byte_count < max_size) && (token != NULL);
1114 token = strtok(NULL, ":")) {
1116 int number = (int)strtol(token, NULL, 16);
1118 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1121 data[byte_count++] = (uint8_t)number;
1127 /** Parse size param*/
1129 parse_size(int *size, const char *q_arg)
1134 /* parse hexadecimal string */
1135 n = strtoul(q_arg, &end, 10);
1136 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1140 printf("invalid size\n");
1148 /** Parse crypto cipher operation command line argument */
1150 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1152 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1153 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1154 "not supported!\n");
1162 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1164 if (strcmp("VERIFY", optarg) == 0) {
1165 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1167 } else if (strcmp("GENERATE", optarg) == 0) {
1168 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1172 printf("Authentication operation specified not supported!\n");
1177 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1179 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1180 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1181 "not supported!\n");
1189 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1191 if (strcmp("ENCRYPT", optarg) == 0) {
1192 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1194 } else if (strcmp("DECRYPT", optarg) == 0) {
1195 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1199 printf("AEAD operation specified not supported!\n");
1203 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1209 /* parse hexadecimal string */
1210 pm = strtoul(q_arg, &end, 16);
1211 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1214 options->cryptodev_mask = pm;
1215 if (options->cryptodev_mask == 0) {
1216 printf("invalid cryptodev_mask specified\n");
1223 /** Parse long options */
1225 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1226 struct option *lgopts, int option_index)
1230 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1231 retval = parse_cryptodev_type(&options->type, optarg);
1233 snprintf(options->string_type, MAX_STR_LEN,
1238 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1239 return parse_crypto_opt_chain(options, optarg);
1241 /* Cipher options */
1242 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1243 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1246 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1247 return parse_cipher_op(&options->cipher_xform.cipher.op,
1250 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1251 options->ckey_param = 1;
1252 options->cipher_xform.cipher.key.length =
1253 parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1255 if (options->cipher_xform.cipher.key.length > 0)
1261 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1262 return parse_size(&options->ckey_random_size, optarg);
1264 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1265 options->cipher_iv_param = 1;
1266 options->cipher_iv.length =
1267 parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1268 if (options->cipher_iv.length > 0)
1274 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1275 return parse_size(&options->cipher_iv_random_size, optarg);
1277 /* Authentication options */
1278 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1279 return parse_auth_algo(&options->auth_xform.auth.algo,
1283 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1284 return parse_auth_op(&options->auth_xform.auth.op,
1287 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1288 options->akey_param = 1;
1289 options->auth_xform.auth.key.length =
1290 parse_bytes(options->auth_xform.auth.key.data, optarg,
1292 if (options->auth_xform.auth.key.length > 0)
1298 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1299 return parse_size(&options->akey_random_size, optarg);
1302 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1303 options->auth_iv_param = 1;
1304 options->auth_iv.length =
1305 parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1306 if (options->auth_iv.length > 0)
1312 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1313 return parse_size(&options->auth_iv_random_size, optarg);
1316 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1317 return parse_aead_algo(&options->aead_xform.aead.algo,
1321 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1322 return parse_aead_op(&options->aead_xform.aead.op,
1325 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1326 options->aead_key_param = 1;
1327 options->aead_xform.aead.key.length =
1328 parse_bytes(options->aead_xform.aead.key.data, optarg,
1330 if (options->aead_xform.aead.key.length > 0)
1336 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1337 return parse_size(&options->aead_key_random_size, optarg);
1340 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1341 options->aead_iv_param = 1;
1342 options->aead_iv.length =
1343 parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1344 if (options->aead_iv.length > 0)
1350 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1351 return parse_size(&options->aead_iv_random_size, optarg);
1353 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1354 options->aad_param = 1;
1355 options->aad.length =
1356 parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1357 if (options->aad.length > 0)
1363 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1364 return parse_size(&options->aad_random_size, optarg);
1367 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1368 return parse_size(&options->digest_size, optarg);
1371 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1372 options->sessionless = 1;
1376 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1377 return parse_cryptodev_mask(options, optarg);
1379 else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1380 options->mac_updating = 1;
1384 else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1385 options->mac_updating = 0;
1392 /** Parse port mask */
1394 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1400 /* parse hexadecimal string */
1401 pm = strtoul(q_arg, &end, 16);
1402 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1405 options->portmask = pm;
1406 if (options->portmask == 0) {
1407 printf("invalid portmask specified\n");
1414 /** Parse number of queues */
1416 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1422 /* parse hexadecimal string */
1423 n = strtoul(q_arg, &end, 10);
1424 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1426 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1429 options->nb_ports_per_lcore = n;
1430 if (options->nb_ports_per_lcore == 0) {
1431 printf("invalid number of ports selected\n");
1438 /** Parse timer period */
1440 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1446 /* parse number string */
1447 n = (unsigned)strtol(q_arg, &end, 10);
1448 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1451 if (n >= MAX_TIMER_PERIOD) {
1452 printf("Warning refresh period specified %lu is greater than "
1453 "max value %lu! using max value",
1454 n, MAX_TIMER_PERIOD);
1455 n = MAX_TIMER_PERIOD;
1458 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1463 /** Generate default options for application */
1465 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1467 options->portmask = 0xffffffff;
1468 options->nb_ports_per_lcore = 1;
1469 options->refresh_period = 10000;
1470 options->single_lcore = 0;
1471 options->sessionless = 0;
1473 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1476 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1477 options->cipher_xform.next = NULL;
1478 options->ckey_param = 0;
1479 options->ckey_random_size = -1;
1480 options->cipher_xform.cipher.key.length = 0;
1481 options->cipher_iv_param = 0;
1482 options->cipher_iv_random_size = -1;
1483 options->cipher_iv.length = 0;
1485 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1486 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1488 /* Authentication Data */
1489 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1490 options->auth_xform.next = NULL;
1491 options->akey_param = 0;
1492 options->akey_random_size = -1;
1493 options->auth_xform.auth.key.length = 0;
1494 options->auth_iv_param = 0;
1495 options->auth_iv_random_size = -1;
1496 options->auth_iv.length = 0;
1498 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1499 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1502 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1503 options->aead_xform.next = NULL;
1504 options->aead_key_param = 0;
1505 options->aead_key_random_size = -1;
1506 options->aead_xform.aead.key.length = 0;
1507 options->aead_iv_param = 0;
1508 options->aead_iv_random_size = -1;
1509 options->aead_iv.length = 0;
1511 options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1512 options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1514 options->aad_param = 0;
1515 options->aad_random_size = -1;
1516 options->aad.length = 0;
1518 options->digest_size = -1;
1520 options->type = CDEV_TYPE_ANY;
1521 options->cryptodev_mask = UINT64_MAX;
1523 options->mac_updating = 1;
1527 display_cipher_info(struct l2fwd_crypto_options *options)
1529 printf("\n---- Cipher information ---\n");
1530 printf("Algorithm: %s\n",
1531 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1532 rte_hexdump(stdout, "Cipher key:",
1533 options->cipher_xform.cipher.key.data,
1534 options->cipher_xform.cipher.key.length);
1535 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1539 display_auth_info(struct l2fwd_crypto_options *options)
1541 printf("\n---- Authentication information ---\n");
1542 printf("Algorithm: %s\n",
1543 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1544 rte_hexdump(stdout, "Auth key:",
1545 options->auth_xform.auth.key.data,
1546 options->auth_xform.auth.key.length);
1547 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1551 display_aead_info(struct l2fwd_crypto_options *options)
1553 printf("\n---- AEAD information ---\n");
1554 printf("Algorithm: %s\n",
1555 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1556 rte_hexdump(stdout, "AEAD key:",
1557 options->aead_xform.aead.key.data,
1558 options->aead_xform.aead.key.length);
1559 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1560 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1564 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1566 char string_cipher_op[MAX_STR_LEN];
1567 char string_auth_op[MAX_STR_LEN];
1568 char string_aead_op[MAX_STR_LEN];
1570 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1571 strcpy(string_cipher_op, "Encrypt");
1573 strcpy(string_cipher_op, "Decrypt");
1575 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1576 strcpy(string_auth_op, "Auth generate");
1578 strcpy(string_auth_op, "Auth verify");
1580 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1581 strcpy(string_aead_op, "Authenticated encryption");
1583 strcpy(string_aead_op, "Authenticated decryption");
1586 printf("Options:-\nn");
1587 printf("portmask: %x\n", options->portmask);
1588 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1589 printf("refresh period : %u\n", options->refresh_period);
1590 printf("single lcore mode: %s\n",
1591 options->single_lcore ? "enabled" : "disabled");
1592 printf("stats_printing: %s\n",
1593 options->refresh_period == 0 ? "disabled" : "enabled");
1595 printf("sessionless crypto: %s\n",
1596 options->sessionless ? "enabled" : "disabled");
1598 if (options->ckey_param && (options->ckey_random_size != -1))
1599 printf("Cipher key already parsed, ignoring size of random key\n");
1601 if (options->akey_param && (options->akey_random_size != -1))
1602 printf("Auth key already parsed, ignoring size of random key\n");
1604 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1605 printf("Cipher IV already parsed, ignoring size of random IV\n");
1607 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1608 printf("Auth IV already parsed, ignoring size of random IV\n");
1610 if (options->aad_param && (options->aad_random_size != -1))
1611 printf("AAD already parsed, ignoring size of random AAD\n");
1613 printf("\nCrypto chain: ");
1614 switch (options->xform_chain) {
1615 case L2FWD_CRYPTO_AEAD:
1616 printf("Input --> %s --> Output\n", string_aead_op);
1617 display_aead_info(options);
1619 case L2FWD_CRYPTO_CIPHER_HASH:
1620 printf("Input --> %s --> %s --> Output\n",
1621 string_cipher_op, string_auth_op);
1622 display_cipher_info(options);
1623 display_auth_info(options);
1625 case L2FWD_CRYPTO_HASH_CIPHER:
1626 printf("Input --> %s --> %s --> Output\n",
1627 string_auth_op, string_cipher_op);
1628 display_cipher_info(options);
1629 display_auth_info(options);
1631 case L2FWD_CRYPTO_HASH_ONLY:
1632 printf("Input --> %s --> Output\n", string_auth_op);
1633 display_auth_info(options);
1635 case L2FWD_CRYPTO_CIPHER_ONLY:
1636 printf("Input --> %s --> Output\n", string_cipher_op);
1637 display_cipher_info(options);
1642 /* Parse the argument given in the command line of the application */
1644 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1645 int argc, char **argv)
1647 int opt, retval, option_index;
1648 char **argvopt = argv, *prgname = argv[0];
1650 static struct option lgopts[] = {
1651 { "sessionless", no_argument, 0, 0 },
1653 { "cdev_type", required_argument, 0, 0 },
1654 { "chain", required_argument, 0, 0 },
1656 { "cipher_algo", required_argument, 0, 0 },
1657 { "cipher_op", required_argument, 0, 0 },
1658 { "cipher_key", required_argument, 0, 0 },
1659 { "cipher_key_random_size", required_argument, 0, 0 },
1660 { "cipher_iv", required_argument, 0, 0 },
1661 { "cipher_iv_random_size", required_argument, 0, 0 },
1663 { "auth_algo", required_argument, 0, 0 },
1664 { "auth_op", required_argument, 0, 0 },
1665 { "auth_key", required_argument, 0, 0 },
1666 { "auth_key_random_size", required_argument, 0, 0 },
1667 { "auth_iv", required_argument, 0, 0 },
1668 { "auth_iv_random_size", required_argument, 0, 0 },
1670 { "aead_algo", required_argument, 0, 0 },
1671 { "aead_op", required_argument, 0, 0 },
1672 { "aead_key", required_argument, 0, 0 },
1673 { "aead_key_random_size", required_argument, 0, 0 },
1674 { "aead_iv", required_argument, 0, 0 },
1675 { "aead_iv_random_size", required_argument, 0, 0 },
1677 { "aad", required_argument, 0, 0 },
1678 { "aad_random_size", required_argument, 0, 0 },
1680 { "digest_size", required_argument, 0, 0 },
1682 { "sessionless", no_argument, 0, 0 },
1683 { "cryptodev_mask", required_argument, 0, 0},
1685 { "mac-updating", no_argument, 0, 0},
1686 { "no-mac-updating", no_argument, 0, 0},
1691 l2fwd_crypto_default_options(options);
1693 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1694 &option_index)) != EOF) {
1698 retval = l2fwd_crypto_parse_args_long_options(options,
1699 lgopts, option_index);
1701 l2fwd_crypto_usage(prgname);
1708 retval = l2fwd_crypto_parse_portmask(options, optarg);
1710 l2fwd_crypto_usage(prgname);
1717 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1719 l2fwd_crypto_usage(prgname);
1726 options->single_lcore = 1;
1732 retval = l2fwd_crypto_parse_timer_period(options,
1735 l2fwd_crypto_usage(prgname);
1741 l2fwd_crypto_usage(prgname);
1748 argv[optind-1] = prgname;
1751 optind = 1; /* reset getopt lib */
1756 /* Check the link status of all ports in up to 9s, and print them finally */
1758 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1760 #define CHECK_INTERVAL 100 /* 100ms */
1761 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1763 uint8_t count, all_ports_up, print_flag = 0;
1764 struct rte_eth_link link;
1766 printf("\nChecking link status");
1768 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1770 for (portid = 0; portid < port_num; portid++) {
1771 if ((port_mask & (1 << portid)) == 0)
1773 memset(&link, 0, sizeof(link));
1774 rte_eth_link_get_nowait(portid, &link);
1775 /* print link status if flag set */
1776 if (print_flag == 1) {
1777 if (link.link_status)
1779 "Port%d Link Up. Speed %u Mbps - %s\n",
1780 portid, link.link_speed,
1781 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1782 ("full-duplex") : ("half-duplex\n"));
1784 printf("Port %d Link Down\n", portid);
1787 /* clear all_ports_up flag if any link down */
1788 if (link.link_status == ETH_LINK_DOWN) {
1793 /* after finally printing all link status, get out */
1794 if (print_flag == 1)
1797 if (all_ports_up == 0) {
1800 rte_delay_ms(CHECK_INTERVAL);
1803 /* set the print_flag if all ports up or timeout */
1804 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1811 /* Check if device has to be HW/SW or any */
1813 check_type(const struct l2fwd_crypto_options *options,
1814 const struct rte_cryptodev_info *dev_info)
1816 if (options->type == CDEV_TYPE_HW &&
1817 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1819 if (options->type == CDEV_TYPE_SW &&
1820 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1822 if (options->type == CDEV_TYPE_ANY)
1828 static const struct rte_cryptodev_capabilities *
1829 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1830 const struct rte_cryptodev_info *dev_info,
1834 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1835 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1836 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1837 options->cipher_xform.cipher.algo;
1839 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1840 cap_cipher_algo = cap->sym.cipher.algo;
1841 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1842 if (cap_cipher_algo == opt_cipher_algo) {
1843 if (check_type(options, dev_info) == 0)
1847 cap = &dev_info->capabilities[++i];
1850 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1851 printf("Algorithm %s not supported by cryptodev %u"
1852 " or device not of preferred type (%s)\n",
1853 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1855 options->string_type);
1862 static const struct rte_cryptodev_capabilities *
1863 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1864 const struct rte_cryptodev_info *dev_info,
1868 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1869 enum rte_crypto_auth_algorithm cap_auth_algo;
1870 enum rte_crypto_auth_algorithm opt_auth_algo =
1871 options->auth_xform.auth.algo;
1873 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1874 cap_auth_algo = cap->sym.auth.algo;
1875 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1876 if (cap_auth_algo == opt_auth_algo) {
1877 if (check_type(options, dev_info) == 0)
1881 cap = &dev_info->capabilities[++i];
1884 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1885 printf("Algorithm %s not supported by cryptodev %u"
1886 " or device not of preferred type (%s)\n",
1887 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1889 options->string_type);
1896 static const struct rte_cryptodev_capabilities *
1897 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1898 const struct rte_cryptodev_info *dev_info,
1902 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1903 enum rte_crypto_aead_algorithm cap_aead_algo;
1904 enum rte_crypto_aead_algorithm opt_aead_algo =
1905 options->aead_xform.aead.algo;
1907 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1908 cap_aead_algo = cap->sym.aead.algo;
1909 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1910 if (cap_aead_algo == opt_aead_algo) {
1911 if (check_type(options, dev_info) == 0)
1915 cap = &dev_info->capabilities[++i];
1918 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1919 printf("Algorithm %s not supported by cryptodev %u"
1920 " or device not of preferred type (%s)\n",
1921 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1923 options->string_type);
1930 /* Check if the device is enabled by cryptodev_mask */
1932 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1935 if (options->cryptodev_mask & (1 << cdev_id))
1942 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1948 if (increment == 0) {
1955 /* Range of values */
1956 for (supp_size = min; supp_size <= max; supp_size += increment) {
1957 if (length == supp_size)
1965 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1966 unsigned int iv_param, int iv_random_size,
1967 uint16_t *iv_length)
1970 * Check if length of provided IV is supported
1971 * by the algorithm chosen.
1974 if (check_supported_size(*iv_length,
1977 iv_range_size->increment)
1979 printf("Unsupported IV length\n");
1983 * Check if length of IV to be randomly generated
1984 * is supported by the algorithm chosen.
1986 } else if (iv_random_size != -1) {
1987 if (check_supported_size(iv_random_size,
1990 iv_range_size->increment)
1992 printf("Unsupported IV length\n");
1995 *iv_length = iv_random_size;
1996 /* No size provided, use minimum size. */
1998 *iv_length = iv_range_size->min;
2004 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2005 uint8_t *enabled_cdevs)
2007 unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
2008 const struct rte_cryptodev_capabilities *cap;
2009 unsigned int sess_sz, max_sess_sz = 0;
2012 cdev_count = rte_cryptodev_count();
2013 if (cdev_count == 0) {
2014 printf("No crypto devices available\n");
2018 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2019 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
2020 if (sess_sz > max_sess_sz)
2021 max_sess_sz = sess_sz;
2024 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2026 struct rte_cryptodev_qp_conf qp_conf;
2027 struct rte_cryptodev_info dev_info;
2028 retval = rte_cryptodev_socket_id(cdev_id);
2031 printf("Invalid crypto device id used\n");
2035 uint8_t socket_id = (uint8_t) retval;
2037 struct rte_cryptodev_config conf = {
2038 .nb_queue_pairs = 1,
2039 .socket_id = socket_id,
2042 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
2045 rte_cryptodev_info_get(cdev_id, &dev_info);
2047 if (session_pool_socket[socket_id] == NULL) {
2048 char mp_name[RTE_MEMPOOL_NAMESIZE];
2049 struct rte_mempool *sess_mp;
2051 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2052 "sess_mp_%u", socket_id);
2055 * Create enough objects for session headers and
2056 * device private data
2058 sess_mp = rte_mempool_create(mp_name,
2061 SESSION_POOL_CACHE_SIZE,
2062 0, NULL, NULL, NULL,
2066 if (sess_mp == NULL) {
2067 printf("Cannot create session pool on socket %d\n",
2072 printf("Allocated session pool on socket %d\n", socket_id);
2073 session_pool_socket[socket_id] = sess_mp;
2076 /* Set AEAD parameters */
2077 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2078 /* Check if device supports AEAD algo */
2079 cap = check_device_support_aead_algo(options, &dev_info,
2084 options->block_size = cap->sym.aead.block_size;
2086 check_iv_param(&cap->sym.aead.iv_size,
2087 options->aead_iv_param,
2088 options->aead_iv_random_size,
2089 &options->aead_iv.length);
2092 * Check if length of provided AEAD key is supported
2093 * by the algorithm chosen.
2095 if (options->aead_key_param) {
2096 if (check_supported_size(
2097 options->aead_xform.aead.key.length,
2098 cap->sym.aead.key_size.min,
2099 cap->sym.aead.key_size.max,
2100 cap->sym.aead.key_size.increment)
2102 printf("Unsupported aead key length\n");
2106 * Check if length of the aead key to be randomly generated
2107 * is supported by the algorithm chosen.
2109 } else if (options->aead_key_random_size != -1) {
2110 if (check_supported_size(options->aead_key_random_size,
2111 cap->sym.aead.key_size.min,
2112 cap->sym.aead.key_size.max,
2113 cap->sym.aead.key_size.increment)
2115 printf("Unsupported aead key length\n");
2118 options->aead_xform.aead.key.length =
2119 options->aead_key_random_size;
2120 /* No size provided, use minimum size. */
2122 options->aead_xform.aead.key.length =
2123 cap->sym.aead.key_size.min;
2125 if (!options->aead_key_param)
2126 generate_random_key(
2127 options->aead_xform.aead.key.data,
2128 options->aead_xform.aead.key.length);
2131 * Check if length of provided AAD is supported
2132 * by the algorithm chosen.
2134 if (options->aad_param) {
2135 if (check_supported_size(options->aad.length,
2136 cap->sym.aead.aad_size.min,
2137 cap->sym.aead.aad_size.max,
2138 cap->sym.aead.aad_size.increment)
2140 printf("Unsupported AAD length\n");
2144 * Check if length of AAD to be randomly generated
2145 * is supported by the algorithm chosen.
2147 } else if (options->aad_random_size != -1) {
2148 if (check_supported_size(options->aad_random_size,
2149 cap->sym.aead.aad_size.min,
2150 cap->sym.aead.aad_size.max,
2151 cap->sym.aead.aad_size.increment)
2153 printf("Unsupported AAD length\n");
2156 options->aad.length = options->aad_random_size;
2157 /* No size provided, use minimum size. */
2159 options->aad.length = cap->sym.auth.aad_size.min;
2161 options->aead_xform.aead.aad_length =
2162 options->aad.length;
2164 /* Check if digest size is supported by the algorithm. */
2165 if (options->digest_size != -1) {
2166 if (check_supported_size(options->digest_size,
2167 cap->sym.aead.digest_size.min,
2168 cap->sym.aead.digest_size.max,
2169 cap->sym.aead.digest_size.increment)
2171 printf("Unsupported digest length\n");
2174 options->aead_xform.aead.digest_length =
2175 options->digest_size;
2176 /* No size provided, use minimum size. */
2178 options->aead_xform.aead.digest_length =
2179 cap->sym.aead.digest_size.min;
2182 /* Set cipher parameters */
2183 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2184 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2185 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2186 /* Check if device supports cipher algo */
2187 cap = check_device_support_cipher_algo(options, &dev_info,
2192 options->block_size = cap->sym.cipher.block_size;
2194 check_iv_param(&cap->sym.cipher.iv_size,
2195 options->cipher_iv_param,
2196 options->cipher_iv_random_size,
2197 &options->cipher_iv.length);
2200 * Check if length of provided cipher key is supported
2201 * by the algorithm chosen.
2203 if (options->ckey_param) {
2204 if (check_supported_size(
2205 options->cipher_xform.cipher.key.length,
2206 cap->sym.cipher.key_size.min,
2207 cap->sym.cipher.key_size.max,
2208 cap->sym.cipher.key_size.increment)
2210 printf("Unsupported cipher key length\n");
2214 * Check if length of the cipher key to be randomly generated
2215 * is supported by the algorithm chosen.
2217 } else if (options->ckey_random_size != -1) {
2218 if (check_supported_size(options->ckey_random_size,
2219 cap->sym.cipher.key_size.min,
2220 cap->sym.cipher.key_size.max,
2221 cap->sym.cipher.key_size.increment)
2223 printf("Unsupported cipher key length\n");
2226 options->cipher_xform.cipher.key.length =
2227 options->ckey_random_size;
2228 /* No size provided, use minimum size. */
2230 options->cipher_xform.cipher.key.length =
2231 cap->sym.cipher.key_size.min;
2233 if (!options->ckey_param)
2234 generate_random_key(
2235 options->cipher_xform.cipher.key.data,
2236 options->cipher_xform.cipher.key.length);
2240 /* Set auth parameters */
2241 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2242 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2243 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2244 /* Check if device supports auth algo */
2245 cap = check_device_support_auth_algo(options, &dev_info,
2250 check_iv_param(&cap->sym.auth.iv_size,
2251 options->auth_iv_param,
2252 options->auth_iv_random_size,
2253 &options->auth_iv.length);
2255 * Check if length of provided auth key is supported
2256 * by the algorithm chosen.
2258 if (options->akey_param) {
2259 if (check_supported_size(
2260 options->auth_xform.auth.key.length,
2261 cap->sym.auth.key_size.min,
2262 cap->sym.auth.key_size.max,
2263 cap->sym.auth.key_size.increment)
2265 printf("Unsupported auth key length\n");
2269 * Check if length of the auth key to be randomly generated
2270 * is supported by the algorithm chosen.
2272 } else if (options->akey_random_size != -1) {
2273 if (check_supported_size(options->akey_random_size,
2274 cap->sym.auth.key_size.min,
2275 cap->sym.auth.key_size.max,
2276 cap->sym.auth.key_size.increment)
2278 printf("Unsupported auth key length\n");
2281 options->auth_xform.auth.key.length =
2282 options->akey_random_size;
2283 /* No size provided, use minimum size. */
2285 options->auth_xform.auth.key.length =
2286 cap->sym.auth.key_size.min;
2288 if (!options->akey_param)
2289 generate_random_key(
2290 options->auth_xform.auth.key.data,
2291 options->auth_xform.auth.key.length);
2293 /* Check if digest size is supported by the algorithm. */
2294 if (options->digest_size != -1) {
2295 if (check_supported_size(options->digest_size,
2296 cap->sym.auth.digest_size.min,
2297 cap->sym.auth.digest_size.max,
2298 cap->sym.auth.digest_size.increment)
2300 printf("Unsupported digest length\n");
2303 options->auth_xform.auth.digest_length =
2304 options->digest_size;
2305 /* No size provided, use minimum size. */
2307 options->auth_xform.auth.digest_length =
2308 cap->sym.auth.digest_size.min;
2311 retval = rte_cryptodev_configure(cdev_id, &conf);
2313 printf("Failed to configure cryptodev %u", cdev_id);
2317 qp_conf.nb_descriptors = 2048;
2319 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2320 socket_id, session_pool_socket[socket_id]);
2322 printf("Failed to setup queue pair %u on cryptodev %u",
2327 retval = rte_cryptodev_start(cdev_id);
2329 printf("Failed to start device %u: error %d\n",
2334 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2336 enabled_cdevs[cdev_id] = 1;
2337 enabled_cdev_count++;
2340 return enabled_cdev_count;
2344 initialize_ports(struct l2fwd_crypto_options *options)
2346 uint16_t last_portid, portid;
2347 unsigned enabled_portcount = 0;
2348 unsigned nb_ports = rte_eth_dev_count();
2350 if (nb_ports == 0) {
2351 printf("No Ethernet ports - bye\n");
2355 /* Reset l2fwd_dst_ports */
2356 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2357 l2fwd_dst_ports[portid] = 0;
2359 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2362 /* Skip ports that are not enabled */
2363 if ((options->portmask & (1 << portid)) == 0)
2367 printf("Initializing port %u... ", portid);
2369 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2371 printf("Cannot configure device: err=%d, port=%u\n",
2376 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2379 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2384 /* init one RX queue */
2386 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2387 rte_eth_dev_socket_id(portid),
2388 NULL, l2fwd_pktmbuf_pool);
2390 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2395 /* init one TX queue on each port */
2397 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2398 rte_eth_dev_socket_id(portid),
2401 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2408 retval = rte_eth_dev_start(portid);
2410 printf("rte_eth_dev_start:err=%d, port=%u\n",
2415 rte_eth_promiscuous_enable(portid);
2417 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2419 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2421 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2422 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2423 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2424 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2425 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2426 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2428 /* initialize port stats */
2429 memset(&port_statistics, 0, sizeof(port_statistics));
2431 /* Setup port forwarding table */
2432 if (enabled_portcount % 2) {
2433 l2fwd_dst_ports[portid] = last_portid;
2434 l2fwd_dst_ports[last_portid] = portid;
2436 last_portid = portid;
2439 l2fwd_enabled_port_mask |= (1 << portid);
2440 enabled_portcount++;
2443 if (enabled_portcount == 1) {
2444 l2fwd_dst_ports[last_portid] = last_portid;
2445 } else if (enabled_portcount % 2) {
2446 printf("odd number of ports in portmask- bye\n");
2450 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2452 return enabled_portcount;
2456 reserve_key_memory(struct l2fwd_crypto_options *options)
2458 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2460 if (options->cipher_xform.cipher.key.data == NULL)
2461 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2463 options->auth_xform.auth.key.data = rte_malloc("auth key",
2465 if (options->auth_xform.auth.key.data == NULL)
2466 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2468 options->aead_xform.aead.key.data = rte_malloc("aead key",
2470 if (options->aead_xform.aead.key.data == NULL)
2471 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2473 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2474 if (options->cipher_iv.data == NULL)
2475 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2477 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2478 if (options->auth_iv.data == NULL)
2479 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2481 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2482 if (options->aead_iv.data == NULL)
2483 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2485 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2486 if (options->aad.data == NULL)
2487 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2488 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
2492 main(int argc, char **argv)
2494 struct lcore_queue_conf *qconf;
2495 struct l2fwd_crypto_options options;
2497 uint8_t nb_cryptodevs, cdev_id;
2498 uint16_t nb_ports, portid;
2499 unsigned lcore_id, rx_lcore_id;
2500 int ret, enabled_cdevcount, enabled_portcount;
2501 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2504 ret = rte_eal_init(argc, argv);
2506 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2510 /* reserve memory for Cipher/Auth key and IV */
2511 reserve_key_memory(&options);
2513 /* parse application arguments (after the EAL ones) */
2514 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2516 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2518 printf("MAC updating %s\n",
2519 options.mac_updating ? "enabled" : "disabled");
2521 /* create the mbuf pool */
2522 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2523 sizeof(struct rte_crypto_op),
2524 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2525 if (l2fwd_pktmbuf_pool == NULL)
2526 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2528 /* create crypto op pool */
2529 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2530 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2532 if (l2fwd_crypto_op_pool == NULL)
2533 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2535 /* Enable Ethernet ports */
2536 enabled_portcount = initialize_ports(&options);
2537 if (enabled_portcount < 1)
2538 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2540 nb_ports = rte_eth_dev_count();
2541 /* Initialize the port/queue configuration of each logical core */
2542 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2543 portid < nb_ports; portid++) {
2545 /* skip ports that are not enabled */
2546 if ((options.portmask & (1 << portid)) == 0)
2549 if (options.single_lcore && qconf == NULL) {
2550 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2552 if (rx_lcore_id >= RTE_MAX_LCORE)
2553 rte_exit(EXIT_FAILURE,
2554 "Not enough cores\n");
2556 } else if (!options.single_lcore) {
2557 /* get the lcore_id for this port */
2558 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2559 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2560 options.nb_ports_per_lcore) {
2562 if (rx_lcore_id >= RTE_MAX_LCORE)
2563 rte_exit(EXIT_FAILURE,
2564 "Not enough cores\n");
2568 /* Assigned a new logical core in the loop above. */
2569 if (qconf != &lcore_queue_conf[rx_lcore_id])
2570 qconf = &lcore_queue_conf[rx_lcore_id];
2572 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2573 qconf->nb_rx_ports++;
2575 printf("Lcore %u: RX port %u\n", rx_lcore_id, portid);
2578 /* Enable Crypto devices */
2579 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2581 if (enabled_cdevcount < 0)
2582 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2584 if (enabled_cdevcount < enabled_portcount)
2585 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2586 "has to be more or equal to number of ports (%d)\n",
2587 enabled_cdevcount, enabled_portcount);
2589 nb_cryptodevs = rte_cryptodev_count();
2591 /* Initialize the port/cryptodev configuration of each logical core */
2592 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2593 cdev_id < nb_cryptodevs && enabled_cdevcount;
2595 /* Crypto op not supported by crypto device */
2596 if (!enabled_cdevs[cdev_id])
2599 if (options.single_lcore && qconf == NULL) {
2600 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2602 if (rx_lcore_id >= RTE_MAX_LCORE)
2603 rte_exit(EXIT_FAILURE,
2604 "Not enough cores\n");
2606 } else if (!options.single_lcore) {
2607 /* get the lcore_id for this port */
2608 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2609 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2610 options.nb_ports_per_lcore) {
2612 if (rx_lcore_id >= RTE_MAX_LCORE)
2613 rte_exit(EXIT_FAILURE,
2614 "Not enough cores\n");
2618 /* Assigned a new logical core in the loop above. */
2619 if (qconf != &lcore_queue_conf[rx_lcore_id])
2620 qconf = &lcore_queue_conf[rx_lcore_id];
2622 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2623 qconf->nb_crypto_devs++;
2625 enabled_cdevcount--;
2627 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2631 /* launch per-lcore init on every lcore */
2632 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2634 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2635 if (rte_eal_wait_lcore(lcore_id) < 0)