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_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 uint32_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 phys_addr_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;
198 /** l2fwd crypto lcore params */
199 struct l2fwd_crypto_params {
203 unsigned digest_length;
206 struct l2fwd_iv cipher_iv;
207 struct l2fwd_iv auth_iv;
208 struct l2fwd_iv aead_iv;
209 struct l2fwd_key aad;
210 struct rte_cryptodev_sym_session *session;
217 enum rte_crypto_cipher_algorithm cipher_algo;
218 enum rte_crypto_auth_algorithm auth_algo;
219 enum rte_crypto_aead_algorithm aead_algo;
222 /** lcore configuration */
223 struct lcore_queue_conf {
224 unsigned nb_rx_ports;
225 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
227 unsigned nb_crypto_devs;
228 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
230 struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
231 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
232 } __rte_cache_aligned;
234 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
236 static const struct rte_eth_conf port_conf = {
238 .mq_mode = ETH_MQ_RX_NONE,
239 .max_rx_pkt_len = ETHER_MAX_LEN,
241 .header_split = 0, /**< Header Split disabled */
242 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
243 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
244 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
245 .hw_strip_crc = 1, /**< CRC stripped by hardware */
248 .mq_mode = ETH_MQ_TX_NONE,
252 struct rte_mempool *l2fwd_pktmbuf_pool;
253 struct rte_mempool *l2fwd_crypto_op_pool;
254 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
256 /* Per-port statistics struct */
257 struct l2fwd_port_statistics {
261 uint64_t crypto_enqueued;
262 uint64_t crypto_dequeued;
265 } __rte_cache_aligned;
267 struct l2fwd_crypto_statistics {
272 } __rte_cache_aligned;
274 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
275 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
277 /* A tsc-based timer responsible for triggering statistics printout */
278 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
279 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
281 /* default period is 10 seconds */
282 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
284 /* Print out statistics on packets dropped */
288 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
289 uint64_t total_packets_enqueued, total_packets_dequeued,
290 total_packets_errors;
294 total_packets_dropped = 0;
295 total_packets_tx = 0;
296 total_packets_rx = 0;
297 total_packets_enqueued = 0;
298 total_packets_dequeued = 0;
299 total_packets_errors = 0;
301 const char clr[] = { 27, '[', '2', 'J', '\0' };
302 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
304 /* Clear screen and move to top left */
305 printf("%s%s", clr, topLeft);
307 printf("\nPort statistics ====================================");
309 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
310 /* skip disabled ports */
311 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
313 printf("\nStatistics for port %u ------------------------------"
314 "\nPackets sent: %32"PRIu64
315 "\nPackets received: %28"PRIu64
316 "\nPackets dropped: %29"PRIu64,
318 port_statistics[portid].tx,
319 port_statistics[portid].rx,
320 port_statistics[portid].dropped);
322 total_packets_dropped += port_statistics[portid].dropped;
323 total_packets_tx += port_statistics[portid].tx;
324 total_packets_rx += port_statistics[portid].rx;
326 printf("\nCrypto statistics ==================================");
328 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
329 /* skip disabled ports */
330 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
332 printf("\nStatistics for cryptodev %"PRIu64
333 " -------------------------"
334 "\nPackets enqueued: %28"PRIu64
335 "\nPackets dequeued: %28"PRIu64
336 "\nPackets errors: %30"PRIu64,
338 crypto_statistics[cdevid].enqueued,
339 crypto_statistics[cdevid].dequeued,
340 crypto_statistics[cdevid].errors);
342 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
343 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
344 total_packets_errors += crypto_statistics[cdevid].errors;
346 printf("\nAggregate statistics ==============================="
347 "\nTotal packets received: %22"PRIu64
348 "\nTotal packets enqueued: %22"PRIu64
349 "\nTotal packets dequeued: %22"PRIu64
350 "\nTotal packets sent: %26"PRIu64
351 "\nTotal packets dropped: %23"PRIu64
352 "\nTotal packets crypto errors: %17"PRIu64,
354 total_packets_enqueued,
355 total_packets_dequeued,
357 total_packets_dropped,
358 total_packets_errors);
359 printf("\n====================================================\n");
363 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
364 struct l2fwd_crypto_params *cparams)
366 struct rte_crypto_op **op_buffer;
369 op_buffer = (struct rte_crypto_op **)
370 qconf->op_buf[cparams->dev_id].buffer;
372 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
373 cparams->qp_id, op_buffer, (uint16_t) n);
375 crypto_statistics[cparams->dev_id].enqueued += ret;
376 if (unlikely(ret < n)) {
377 crypto_statistics[cparams->dev_id].errors += (n - ret);
379 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
380 rte_crypto_op_free(op_buffer[ret]);
388 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
389 struct l2fwd_crypto_params *cparams)
391 unsigned lcore_id, len;
392 struct lcore_queue_conf *qconf;
394 lcore_id = rte_lcore_id();
396 qconf = &lcore_queue_conf[lcore_id];
397 len = qconf->op_buf[cparams->dev_id].len;
398 qconf->op_buf[cparams->dev_id].buffer[len] = op;
401 /* enough ops to be sent */
402 if (len == MAX_PKT_BURST) {
403 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
407 qconf->op_buf[cparams->dev_id].len = len;
412 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
413 struct rte_crypto_op *op,
414 struct l2fwd_crypto_params *cparams)
416 struct ether_hdr *eth_hdr;
417 struct ipv4_hdr *ip_hdr;
419 uint32_t ipdata_offset, data_len;
420 uint32_t pad_len = 0;
423 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
425 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
428 ipdata_offset = sizeof(struct ether_hdr);
430 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
433 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
434 * IPV4_IHL_MULTIPLIER;
437 /* Zero pad data to be crypto'd so it is block aligned */
438 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
440 if (cparams->do_hash && cparams->hash_verify)
441 data_len -= cparams->digest_length;
443 if (cparams->do_cipher) {
445 * Following algorithms are block cipher algorithms,
446 * and might need padding
448 switch (cparams->cipher_algo) {
449 case RTE_CRYPTO_CIPHER_AES_CBC:
450 case RTE_CRYPTO_CIPHER_AES_ECB:
451 case RTE_CRYPTO_CIPHER_DES_CBC:
452 case RTE_CRYPTO_CIPHER_3DES_CBC:
453 case RTE_CRYPTO_CIPHER_3DES_ECB:
454 if (data_len % cparams->block_size)
455 pad_len = cparams->block_size -
456 (data_len % cparams->block_size);
463 padding = rte_pktmbuf_append(m, pad_len);
464 if (unlikely(!padding))
468 memset(padding, 0, pad_len);
472 /* Set crypto operation data parameters */
473 rte_crypto_op_attach_sym_session(op, cparams->session);
475 if (cparams->do_hash) {
476 if (cparams->auth_iv.length) {
477 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
480 cparams->cipher_iv.length);
482 * Copy IV at the end of the crypto operation,
483 * after the cipher IV, if added
485 rte_memcpy(iv_ptr, cparams->auth_iv.data,
486 cparams->auth_iv.length);
488 if (!cparams->hash_verify) {
489 /* Append space for digest to end of packet */
490 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
491 cparams->digest_length);
493 op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
494 uint8_t *) + ipdata_offset + data_len;
497 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
498 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
500 /* For wireless algorithms, offset/length must be in bits */
501 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
502 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
503 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
504 op->sym->auth.data.offset = ipdata_offset << 3;
505 op->sym->auth.data.length = data_len << 3;
507 op->sym->auth.data.offset = ipdata_offset;
508 op->sym->auth.data.length = data_len;
512 if (cparams->do_cipher) {
513 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
515 /* Copy IV at the end of the crypto operation */
516 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
517 cparams->cipher_iv.length);
519 /* For wireless algorithms, offset/length must be in bits */
520 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
521 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
522 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
523 op->sym->cipher.data.offset = ipdata_offset << 3;
524 op->sym->cipher.data.length = data_len << 3;
526 op->sym->cipher.data.offset = ipdata_offset;
527 op->sym->cipher.data.length = data_len;
531 if (cparams->do_aead) {
532 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
534 /* Copy IV at the end of the crypto operation */
535 rte_memcpy(iv_ptr, cparams->aead_iv.data, cparams->aead_iv.length);
537 op->sym->aead.data.offset = ipdata_offset;
538 op->sym->aead.data.length = data_len;
540 if (!cparams->hash_verify) {
541 /* Append space for digest to end of packet */
542 op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
543 cparams->digest_length);
545 op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
546 uint8_t *) + ipdata_offset + data_len;
549 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
550 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
552 if (cparams->aad.length) {
553 op->sym->aead.aad.data = cparams->aad.data;
554 op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
560 return l2fwd_crypto_enqueue(op, cparams);
564 /* Send the burst of packets on an output interface */
566 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
569 struct rte_mbuf **pkt_buffer;
572 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
574 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
575 port_statistics[port].tx += ret;
576 if (unlikely(ret < n)) {
577 port_statistics[port].dropped += (n - ret);
579 rte_pktmbuf_free(pkt_buffer[ret]);
586 /* Enqueue packets for TX and prepare them to be sent */
588 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
590 unsigned lcore_id, len;
591 struct lcore_queue_conf *qconf;
593 lcore_id = rte_lcore_id();
595 qconf = &lcore_queue_conf[lcore_id];
596 len = qconf->pkt_buf[port].len;
597 qconf->pkt_buf[port].buffer[len] = m;
600 /* enough pkts to be sent */
601 if (unlikely(len == MAX_PKT_BURST)) {
602 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
606 qconf->pkt_buf[port].len = len;
611 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
613 struct ether_hdr *eth;
617 dst_port = l2fwd_dst_ports[portid];
618 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
620 /* 02:00:00:00:00:xx */
621 tmp = ð->d_addr.addr_bytes[0];
622 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
625 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
627 l2fwd_send_packet(m, (uint8_t) dst_port);
630 /** Generate random key */
632 generate_random_key(uint8_t *key, unsigned length)
637 fd = open("/dev/urandom", O_RDONLY);
639 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
641 ret = read(fd, key, length);
644 if (ret != (signed)length)
645 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
648 static struct rte_cryptodev_sym_session *
649 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
651 struct rte_crypto_sym_xform *first_xform;
652 struct rte_cryptodev_sym_session *session;
653 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
654 struct rte_mempool *sess_mp = session_pool_socket[socket_id];
656 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
657 first_xform = &options->aead_xform;
658 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
659 first_xform = &options->cipher_xform;
660 first_xform->next = &options->auth_xform;
661 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
662 first_xform = &options->auth_xform;
663 first_xform->next = &options->cipher_xform;
664 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
665 first_xform = &options->cipher_xform;
667 first_xform = &options->auth_xform;
670 session = rte_cryptodev_sym_session_create(sess_mp);
675 if (rte_cryptodev_sym_session_init(cdev_id, session,
676 first_xform, sess_mp) < 0)
683 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
685 /* main processing loop */
687 l2fwd_main_loop(struct l2fwd_crypto_options *options)
689 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
690 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
692 unsigned lcore_id = rte_lcore_id();
693 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
694 unsigned i, j, portid, nb_rx, len;
695 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
696 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
697 US_PER_S * BURST_TX_DRAIN_US;
698 struct l2fwd_crypto_params *cparams;
699 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
700 struct rte_cryptodev_sym_session *session;
702 if (qconf->nb_rx_ports == 0) {
703 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
707 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
709 for (i = 0; i < qconf->nb_rx_ports; i++) {
711 portid = qconf->rx_port_list[i];
712 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
716 for (i = 0; i < qconf->nb_crypto_devs; i++) {
717 port_cparams[i].do_cipher = 0;
718 port_cparams[i].do_hash = 0;
719 port_cparams[i].do_aead = 0;
721 switch (options->xform_chain) {
722 case L2FWD_CRYPTO_AEAD:
723 port_cparams[i].do_aead = 1;
725 case L2FWD_CRYPTO_CIPHER_HASH:
726 case L2FWD_CRYPTO_HASH_CIPHER:
727 port_cparams[i].do_cipher = 1;
728 port_cparams[i].do_hash = 1;
730 case L2FWD_CRYPTO_HASH_ONLY:
731 port_cparams[i].do_hash = 1;
733 case L2FWD_CRYPTO_CIPHER_ONLY:
734 port_cparams[i].do_cipher = 1;
738 port_cparams[i].dev_id = qconf->cryptodev_list[i];
739 port_cparams[i].qp_id = 0;
741 port_cparams[i].block_size = options->block_size;
743 if (port_cparams[i].do_hash) {
744 port_cparams[i].auth_iv.data = options->auth_iv.data;
745 port_cparams[i].auth_iv.length = options->auth_iv.length;
746 if (!options->auth_iv_param)
747 generate_random_key(port_cparams[i].auth_iv.data,
748 port_cparams[i].auth_iv.length);
749 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
750 port_cparams[i].hash_verify = 1;
752 port_cparams[i].hash_verify = 0;
754 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
755 port_cparams[i].digest_length =
756 options->auth_xform.auth.digest_length;
757 /* Set IV parameters */
758 if (options->auth_iv.length) {
759 options->auth_xform.auth.iv.offset =
760 IV_OFFSET + options->cipher_iv.length;
761 options->auth_xform.auth.iv.length =
762 options->auth_iv.length;
766 if (port_cparams[i].do_aead) {
767 port_cparams[i].aead_iv.data = options->aead_iv.data;
768 port_cparams[i].aead_iv.length = options->aead_iv.length;
769 if (!options->aead_iv_param)
770 generate_random_key(port_cparams[i].aead_iv.data,
771 port_cparams[i].aead_iv.length);
772 port_cparams[i].aead_algo = options->aead_xform.aead.algo;
773 port_cparams[i].digest_length =
774 options->aead_xform.aead.digest_length;
775 if (options->aead_xform.aead.aad_length) {
776 port_cparams[i].aad.data = options->aad.data;
777 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
778 port_cparams[i].aad.length = options->aad.length;
779 if (!options->aad_param)
780 generate_random_key(port_cparams[i].aad.data,
781 port_cparams[i].aad.length);
784 port_cparams[i].aad.length = 0;
786 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
787 port_cparams[i].hash_verify = 1;
789 port_cparams[i].hash_verify = 0;
791 /* Set IV parameters */
792 options->aead_xform.aead.iv.offset = IV_OFFSET;
793 options->aead_xform.aead.iv.length = options->aead_iv.length;
796 if (port_cparams[i].do_cipher) {
797 port_cparams[i].cipher_iv.data = options->cipher_iv.data;
798 port_cparams[i].cipher_iv.length = options->cipher_iv.length;
799 if (!options->cipher_iv_param)
800 generate_random_key(port_cparams[i].cipher_iv.data,
801 port_cparams[i].cipher_iv.length);
803 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
804 /* Set IV parameters */
805 options->cipher_xform.cipher.iv.offset = IV_OFFSET;
806 options->cipher_xform.cipher.iv.length =
807 options->cipher_iv.length;
810 session = initialize_crypto_session(options,
811 port_cparams[i].dev_id);
813 rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
815 port_cparams[i].session = session;
817 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
818 port_cparams[i].dev_id);
821 l2fwd_crypto_options_print(options);
824 * Initialize previous tsc timestamp before the loop,
825 * to avoid showing the port statistics immediately,
826 * so user can see the crypto information.
828 prev_tsc = rte_rdtsc();
831 cur_tsc = rte_rdtsc();
834 * Crypto device/TX burst queue drain
836 diff_tsc = cur_tsc - prev_tsc;
837 if (unlikely(diff_tsc > drain_tsc)) {
838 /* Enqueue all crypto ops remaining in buffers */
839 for (i = 0; i < qconf->nb_crypto_devs; i++) {
840 cparams = &port_cparams[i];
841 len = qconf->op_buf[cparams->dev_id].len;
842 l2fwd_crypto_send_burst(qconf, len, cparams);
843 qconf->op_buf[cparams->dev_id].len = 0;
845 /* Transmit all packets remaining in buffers */
846 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
847 if (qconf->pkt_buf[portid].len == 0)
849 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
850 qconf->pkt_buf[portid].len,
852 qconf->pkt_buf[portid].len = 0;
855 /* if timer is enabled */
856 if (timer_period > 0) {
858 /* advance the timer */
859 timer_tsc += diff_tsc;
861 /* if timer has reached its timeout */
862 if (unlikely(timer_tsc >=
863 (uint64_t)timer_period)) {
865 /* do this only on master core */
866 if (lcore_id == rte_get_master_lcore()
867 && options->refresh_period) {
878 * Read packet from RX queues
880 for (i = 0; i < qconf->nb_rx_ports; i++) {
881 portid = qconf->rx_port_list[i];
883 cparams = &port_cparams[i];
885 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
886 pkts_burst, MAX_PKT_BURST);
888 port_statistics[portid].rx += nb_rx;
892 * If we can't allocate a crypto_ops, then drop
893 * the rest of the burst and dequeue and
894 * process the packets to free offload structs
896 if (rte_crypto_op_bulk_alloc(
897 l2fwd_crypto_op_pool,
898 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
901 for (j = 0; j < nb_rx; j++)
902 rte_pktmbuf_free(pkts_burst[j]);
907 /* Enqueue packets from Crypto device*/
908 for (j = 0; j < nb_rx; j++) {
911 l2fwd_simple_crypto_enqueue(m,
912 ops_burst[j], cparams);
916 /* Dequeue packets from Crypto device */
918 nb_rx = rte_cryptodev_dequeue_burst(
919 cparams->dev_id, cparams->qp_id,
920 ops_burst, MAX_PKT_BURST);
922 crypto_statistics[cparams->dev_id].dequeued +=
925 /* Forward crypto'd packets */
926 for (j = 0; j < nb_rx; j++) {
927 m = ops_burst[j]->sym->m_src;
929 rte_crypto_op_free(ops_burst[j]);
930 l2fwd_simple_forward(m, portid);
932 } while (nb_rx == MAX_PKT_BURST);
938 l2fwd_launch_one_lcore(void *arg)
940 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
944 /* Display command line arguments usage */
946 l2fwd_crypto_usage(const char *prgname)
948 printf("%s [EAL options] --\n"
949 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
950 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
951 " -s manage all ports from single lcore\n"
952 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
953 " (0 to disable, 10 default, 86400 maximum)\n"
955 " --cdev_type HW / SW / ANY\n"
956 " --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
957 " HASH_ONLY / AEAD\n"
959 " --cipher_algo ALGO\n"
960 " --cipher_op ENCRYPT / DECRYPT\n"
961 " --cipher_key KEY (bytes separated with \":\")\n"
962 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
963 " --cipher_iv IV (bytes separated with \":\")\n"
964 " --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
966 " --auth_algo ALGO\n"
967 " --auth_op GENERATE / VERIFY\n"
968 " --auth_key KEY (bytes separated with \":\")\n"
969 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
970 " --auth_iv IV (bytes separated with \":\")\n"
971 " --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
973 " --aead_algo ALGO\n"
974 " --aead_op ENCRYPT / DECRYPT\n"
975 " --aead_key KEY (bytes separated with \":\")\n"
976 " --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
977 " --aead_iv IV (bytes separated with \":\")\n"
978 " --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
979 " --aad AAD (bytes separated with \":\")\n"
980 " --aad_random_size SIZE: size of AAD when generated randomly\n"
982 " --digest_size SIZE: size of digest to be generated/verified\n"
985 " --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n",
989 /** Parse crypto device type command line argument */
991 parse_cryptodev_type(enum cdev_type *type, char *optarg)
993 if (strcmp("HW", optarg) == 0) {
994 *type = CDEV_TYPE_HW;
996 } else if (strcmp("SW", optarg) == 0) {
997 *type = CDEV_TYPE_SW;
999 } else if (strcmp("ANY", optarg) == 0) {
1000 *type = CDEV_TYPE_ANY;
1007 /** Parse crypto chain xform command line argument */
1009 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1011 if (strcmp("CIPHER_HASH", optarg) == 0) {
1012 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1014 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1015 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1017 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1018 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1020 } else if (strcmp("HASH_ONLY", optarg) == 0) {
1021 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1023 } else if (strcmp("AEAD", optarg) == 0) {
1024 options->xform_chain = L2FWD_CRYPTO_AEAD;
1031 /** Parse crypto cipher algo option command line argument */
1033 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1036 if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1037 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1038 "not supported!\n");
1045 /** Parse crypto cipher operation command line argument */
1047 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1049 if (strcmp("ENCRYPT", optarg) == 0) {
1050 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1052 } else if (strcmp("DECRYPT", optarg) == 0) {
1053 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1057 printf("Cipher operation not supported!\n");
1061 /** Parse crypto key command line argument */
1063 parse_key(uint8_t *data, char *input_arg)
1065 unsigned byte_count;
1068 for (byte_count = 0, token = strtok(input_arg, ":");
1069 (byte_count < MAX_KEY_SIZE) && (token != NULL);
1070 token = strtok(NULL, ":")) {
1072 int number = (int)strtol(token, NULL, 16);
1074 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1077 data[byte_count++] = (uint8_t)number;
1083 /** Parse size param*/
1085 parse_size(int *size, const char *q_arg)
1090 /* parse hexadecimal string */
1091 n = strtoul(q_arg, &end, 10);
1092 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1096 printf("invalid size\n");
1104 /** Parse crypto cipher operation command line argument */
1106 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1108 if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1109 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1110 "not supported!\n");
1118 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1120 if (strcmp("VERIFY", optarg) == 0) {
1121 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1123 } else if (strcmp("GENERATE", optarg) == 0) {
1124 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1128 printf("Authentication operation specified not supported!\n");
1133 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1135 if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1136 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1137 "not supported!\n");
1145 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1147 if (strcmp("ENCRYPT", optarg) == 0) {
1148 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1150 } else if (strcmp("DECRYPT", optarg) == 0) {
1151 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1155 printf("AEAD operation specified not supported!\n");
1159 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1165 /* parse hexadecimal string */
1166 pm = strtoul(q_arg, &end, 16);
1167 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1170 options->cryptodev_mask = pm;
1171 if (options->cryptodev_mask == 0) {
1172 printf("invalid cryptodev_mask specified\n");
1179 /** Parse long options */
1181 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1182 struct option *lgopts, int option_index)
1186 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1187 retval = parse_cryptodev_type(&options->type, optarg);
1189 snprintf(options->string_type, MAX_STR_LEN,
1194 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1195 return parse_crypto_opt_chain(options, optarg);
1197 /* Cipher options */
1198 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1199 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1202 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1203 return parse_cipher_op(&options->cipher_xform.cipher.op,
1206 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1207 options->ckey_param = 1;
1208 options->cipher_xform.cipher.key.length =
1209 parse_key(options->cipher_xform.cipher.key.data, optarg);
1210 if (options->cipher_xform.cipher.key.length > 0)
1216 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1217 return parse_size(&options->ckey_random_size, optarg);
1219 else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1220 options->cipher_iv_param = 1;
1221 options->cipher_iv.length =
1222 parse_key(options->cipher_iv.data, optarg);
1223 if (options->cipher_iv.length > 0)
1229 else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1230 return parse_size(&options->cipher_iv_random_size, optarg);
1232 /* Authentication options */
1233 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1234 return parse_auth_algo(&options->auth_xform.auth.algo,
1238 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1239 return parse_auth_op(&options->auth_xform.auth.op,
1242 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1243 options->akey_param = 1;
1244 options->auth_xform.auth.key.length =
1245 parse_key(options->auth_xform.auth.key.data, optarg);
1246 if (options->auth_xform.auth.key.length > 0)
1252 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1253 return parse_size(&options->akey_random_size, optarg);
1256 else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1257 options->auth_iv_param = 1;
1258 options->auth_iv.length =
1259 parse_key(options->auth_iv.data, optarg);
1260 if (options->auth_iv.length > 0)
1266 else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1267 return parse_size(&options->auth_iv_random_size, optarg);
1270 else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1271 return parse_aead_algo(&options->aead_xform.aead.algo,
1275 else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1276 return parse_aead_op(&options->aead_xform.aead.op,
1279 else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1280 options->aead_key_param = 1;
1281 options->aead_xform.aead.key.length =
1282 parse_key(options->aead_xform.aead.key.data, optarg);
1283 if (options->aead_xform.aead.key.length > 0)
1289 else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1290 return parse_size(&options->aead_key_random_size, optarg);
1293 else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1294 options->aead_iv_param = 1;
1295 options->aead_iv.length =
1296 parse_key(options->aead_iv.data, optarg);
1297 if (options->aead_iv.length > 0)
1303 else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1304 return parse_size(&options->aead_iv_random_size, optarg);
1306 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1307 options->aad_param = 1;
1308 options->aad.length =
1309 parse_key(options->aad.data, optarg);
1310 if (options->aad.length > 0)
1316 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1317 return parse_size(&options->aad_random_size, optarg);
1320 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1321 return parse_size(&options->digest_size, optarg);
1324 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1325 options->sessionless = 1;
1329 else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1330 return parse_cryptodev_mask(options, optarg);
1335 /** Parse port mask */
1337 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1343 /* parse hexadecimal string */
1344 pm = strtoul(q_arg, &end, 16);
1345 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1348 options->portmask = pm;
1349 if (options->portmask == 0) {
1350 printf("invalid portmask specified\n");
1357 /** Parse number of queues */
1359 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1365 /* parse hexadecimal string */
1366 n = strtoul(q_arg, &end, 10);
1367 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1369 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1372 options->nb_ports_per_lcore = n;
1373 if (options->nb_ports_per_lcore == 0) {
1374 printf("invalid number of ports selected\n");
1381 /** Parse timer period */
1383 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1389 /* parse number string */
1390 n = (unsigned)strtol(q_arg, &end, 10);
1391 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1394 if (n >= MAX_TIMER_PERIOD) {
1395 printf("Warning refresh period specified %lu is greater than "
1396 "max value %lu! using max value",
1397 n, MAX_TIMER_PERIOD);
1398 n = MAX_TIMER_PERIOD;
1401 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1406 /** Generate default options for application */
1408 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1410 options->portmask = 0xffffffff;
1411 options->nb_ports_per_lcore = 1;
1412 options->refresh_period = 10000;
1413 options->single_lcore = 0;
1414 options->sessionless = 0;
1416 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1419 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1420 options->cipher_xform.next = NULL;
1421 options->ckey_param = 0;
1422 options->ckey_random_size = -1;
1423 options->cipher_xform.cipher.key.length = 0;
1424 options->cipher_iv_param = 0;
1425 options->cipher_iv_random_size = -1;
1426 options->cipher_iv.length = 0;
1428 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1429 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1431 /* Authentication Data */
1432 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1433 options->auth_xform.next = NULL;
1434 options->akey_param = 0;
1435 options->akey_random_size = -1;
1436 options->auth_xform.auth.key.length = 0;
1437 options->auth_iv_param = 0;
1438 options->auth_iv_random_size = -1;
1439 options->auth_iv.length = 0;
1441 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1442 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1445 options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1446 options->aead_xform.next = NULL;
1447 options->aead_key_param = 0;
1448 options->aead_key_random_size = -1;
1449 options->aead_xform.aead.key.length = 0;
1450 options->aead_iv_param = 0;
1451 options->aead_iv_random_size = -1;
1452 options->aead_iv.length = 0;
1454 options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1455 options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1457 options->aad_param = 0;
1458 options->aad_random_size = -1;
1459 options->aad.length = 0;
1461 options->digest_size = -1;
1463 options->type = CDEV_TYPE_ANY;
1464 options->cryptodev_mask = UINT64_MAX;
1468 display_cipher_info(struct l2fwd_crypto_options *options)
1470 printf("\n---- Cipher information ---\n");
1471 printf("Algorithm: %s\n",
1472 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1473 rte_hexdump(stdout, "Cipher key:",
1474 options->cipher_xform.cipher.key.data,
1475 options->cipher_xform.cipher.key.length);
1476 rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1480 display_auth_info(struct l2fwd_crypto_options *options)
1482 printf("\n---- Authentication information ---\n");
1483 printf("Algorithm: %s\n",
1484 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1485 rte_hexdump(stdout, "Auth key:",
1486 options->auth_xform.auth.key.data,
1487 options->auth_xform.auth.key.length);
1488 rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1492 display_aead_info(struct l2fwd_crypto_options *options)
1494 printf("\n---- AEAD information ---\n");
1495 printf("Algorithm: %s\n",
1496 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1497 rte_hexdump(stdout, "AEAD key:",
1498 options->aead_xform.aead.key.data,
1499 options->aead_xform.aead.key.length);
1500 rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1501 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1505 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1507 char string_cipher_op[MAX_STR_LEN];
1508 char string_auth_op[MAX_STR_LEN];
1509 char string_aead_op[MAX_STR_LEN];
1511 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1512 strcpy(string_cipher_op, "Encrypt");
1514 strcpy(string_cipher_op, "Decrypt");
1516 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1517 strcpy(string_auth_op, "Auth generate");
1519 strcpy(string_auth_op, "Auth verify");
1521 if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1522 strcpy(string_aead_op, "Authenticated encryption");
1524 strcpy(string_aead_op, "Authenticated decryption");
1527 printf("Options:-\nn");
1528 printf("portmask: %x\n", options->portmask);
1529 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1530 printf("refresh period : %u\n", options->refresh_period);
1531 printf("single lcore mode: %s\n",
1532 options->single_lcore ? "enabled" : "disabled");
1533 printf("stats_printing: %s\n",
1534 options->refresh_period == 0 ? "disabled" : "enabled");
1536 printf("sessionless crypto: %s\n",
1537 options->sessionless ? "enabled" : "disabled");
1539 if (options->ckey_param && (options->ckey_random_size != -1))
1540 printf("Cipher key already parsed, ignoring size of random key\n");
1542 if (options->akey_param && (options->akey_random_size != -1))
1543 printf("Auth key already parsed, ignoring size of random key\n");
1545 if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1546 printf("Cipher IV already parsed, ignoring size of random IV\n");
1548 if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1549 printf("Auth IV already parsed, ignoring size of random IV\n");
1551 if (options->aad_param && (options->aad_random_size != -1))
1552 printf("AAD already parsed, ignoring size of random AAD\n");
1554 printf("\nCrypto chain: ");
1555 switch (options->xform_chain) {
1556 case L2FWD_CRYPTO_AEAD:
1557 printf("Input --> %s --> Output\n", string_aead_op);
1558 display_aead_info(options);
1560 case L2FWD_CRYPTO_CIPHER_HASH:
1561 printf("Input --> %s --> %s --> Output\n",
1562 string_cipher_op, string_auth_op);
1563 display_cipher_info(options);
1564 display_auth_info(options);
1566 case L2FWD_CRYPTO_HASH_CIPHER:
1567 printf("Input --> %s --> %s --> Output\n",
1568 string_auth_op, string_cipher_op);
1569 display_cipher_info(options);
1570 display_auth_info(options);
1572 case L2FWD_CRYPTO_HASH_ONLY:
1573 printf("Input --> %s --> Output\n", string_auth_op);
1574 display_auth_info(options);
1576 case L2FWD_CRYPTO_CIPHER_ONLY:
1577 printf("Input --> %s --> Output\n", string_cipher_op);
1578 display_cipher_info(options);
1583 /* Parse the argument given in the command line of the application */
1585 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1586 int argc, char **argv)
1588 int opt, retval, option_index;
1589 char **argvopt = argv, *prgname = argv[0];
1591 static struct option lgopts[] = {
1592 { "sessionless", no_argument, 0, 0 },
1594 { "cdev_type", required_argument, 0, 0 },
1595 { "chain", required_argument, 0, 0 },
1597 { "cipher_algo", required_argument, 0, 0 },
1598 { "cipher_op", required_argument, 0, 0 },
1599 { "cipher_key", required_argument, 0, 0 },
1600 { "cipher_key_random_size", required_argument, 0, 0 },
1601 { "cipher_iv", required_argument, 0, 0 },
1602 { "cipher_iv_random_size", required_argument, 0, 0 },
1604 { "auth_algo", required_argument, 0, 0 },
1605 { "auth_op", required_argument, 0, 0 },
1606 { "auth_key", required_argument, 0, 0 },
1607 { "auth_key_random_size", required_argument, 0, 0 },
1608 { "auth_iv", required_argument, 0, 0 },
1609 { "auth_iv_random_size", required_argument, 0, 0 },
1611 { "aead_algo", required_argument, 0, 0 },
1612 { "aead_op", required_argument, 0, 0 },
1613 { "aead_key", required_argument, 0, 0 },
1614 { "aead_key_random_size", required_argument, 0, 0 },
1615 { "aead_iv", required_argument, 0, 0 },
1616 { "aead_iv_random_size", required_argument, 0, 0 },
1618 { "aad", required_argument, 0, 0 },
1619 { "aad_random_size", required_argument, 0, 0 },
1621 { "digest_size", required_argument, 0, 0 },
1623 { "sessionless", no_argument, 0, 0 },
1624 { "cryptodev_mask", required_argument, 0, 0},
1629 l2fwd_crypto_default_options(options);
1631 while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1632 &option_index)) != EOF) {
1636 retval = l2fwd_crypto_parse_args_long_options(options,
1637 lgopts, option_index);
1639 l2fwd_crypto_usage(prgname);
1646 retval = l2fwd_crypto_parse_portmask(options, optarg);
1648 l2fwd_crypto_usage(prgname);
1655 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1657 l2fwd_crypto_usage(prgname);
1664 options->single_lcore = 1;
1670 retval = l2fwd_crypto_parse_timer_period(options,
1673 l2fwd_crypto_usage(prgname);
1679 l2fwd_crypto_usage(prgname);
1686 argv[optind-1] = prgname;
1689 optind = 1; /* reset getopt lib */
1694 /* Check the link status of all ports in up to 9s, and print them finally */
1696 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1698 #define CHECK_INTERVAL 100 /* 100ms */
1699 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1700 uint8_t portid, count, all_ports_up, print_flag = 0;
1701 struct rte_eth_link link;
1703 printf("\nChecking link status");
1705 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1707 for (portid = 0; portid < port_num; portid++) {
1708 if ((port_mask & (1 << portid)) == 0)
1710 memset(&link, 0, sizeof(link));
1711 rte_eth_link_get_nowait(portid, &link);
1712 /* print link status if flag set */
1713 if (print_flag == 1) {
1714 if (link.link_status)
1715 printf("Port %d Link Up - speed %u "
1716 "Mbps - %s\n", (uint8_t)portid,
1717 (unsigned)link.link_speed,
1718 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1719 ("full-duplex") : ("half-duplex\n"));
1721 printf("Port %d Link Down\n",
1725 /* clear all_ports_up flag if any link down */
1726 if (link.link_status == ETH_LINK_DOWN) {
1731 /* after finally printing all link status, get out */
1732 if (print_flag == 1)
1735 if (all_ports_up == 0) {
1738 rte_delay_ms(CHECK_INTERVAL);
1741 /* set the print_flag if all ports up or timeout */
1742 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1749 /* Check if device has to be HW/SW or any */
1751 check_type(const struct l2fwd_crypto_options *options,
1752 const struct rte_cryptodev_info *dev_info)
1754 if (options->type == CDEV_TYPE_HW &&
1755 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1757 if (options->type == CDEV_TYPE_SW &&
1758 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1760 if (options->type == CDEV_TYPE_ANY)
1766 static const struct rte_cryptodev_capabilities *
1767 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1768 const struct rte_cryptodev_info *dev_info,
1772 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1773 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1774 enum rte_crypto_cipher_algorithm opt_cipher_algo =
1775 options->cipher_xform.cipher.algo;
1777 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1778 cap_cipher_algo = cap->sym.cipher.algo;
1779 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1780 if (cap_cipher_algo == opt_cipher_algo) {
1781 if (check_type(options, dev_info) == 0)
1785 cap = &dev_info->capabilities[++i];
1788 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1789 printf("Algorithm %s not supported by cryptodev %u"
1790 " or device not of preferred type (%s)\n",
1791 rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1793 options->string_type);
1800 static const struct rte_cryptodev_capabilities *
1801 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1802 const struct rte_cryptodev_info *dev_info,
1806 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1807 enum rte_crypto_auth_algorithm cap_auth_algo;
1808 enum rte_crypto_auth_algorithm opt_auth_algo =
1809 options->auth_xform.auth.algo;
1811 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1812 cap_auth_algo = cap->sym.auth.algo;
1813 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1814 if (cap_auth_algo == opt_auth_algo) {
1815 if (check_type(options, dev_info) == 0)
1819 cap = &dev_info->capabilities[++i];
1822 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1823 printf("Algorithm %s not supported by cryptodev %u"
1824 " or device not of preferred type (%s)\n",
1825 rte_crypto_auth_algorithm_strings[opt_auth_algo],
1827 options->string_type);
1834 static const struct rte_cryptodev_capabilities *
1835 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1836 const struct rte_cryptodev_info *dev_info,
1840 const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1841 enum rte_crypto_aead_algorithm cap_aead_algo;
1842 enum rte_crypto_aead_algorithm opt_aead_algo =
1843 options->aead_xform.aead.algo;
1845 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1846 cap_aead_algo = cap->sym.aead.algo;
1847 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1848 if (cap_aead_algo == opt_aead_algo) {
1849 if (check_type(options, dev_info) == 0)
1853 cap = &dev_info->capabilities[++i];
1856 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1857 printf("Algorithm %s not supported by cryptodev %u"
1858 " or device not of preferred type (%s)\n",
1859 rte_crypto_aead_algorithm_strings[opt_aead_algo],
1861 options->string_type);
1868 /* Check if the device is enabled by cryptodev_mask */
1870 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1873 if (options->cryptodev_mask & (1 << cdev_id))
1880 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1886 if (increment == 0) {
1893 /* Range of values */
1894 for (supp_size = min; supp_size <= max; supp_size += increment) {
1895 if (length == supp_size)
1903 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1904 unsigned int iv_param, int iv_random_size,
1905 uint16_t *iv_length)
1908 * Check if length of provided IV is supported
1909 * by the algorithm chosen.
1912 if (check_supported_size(*iv_length,
1915 iv_range_size->increment)
1917 printf("Unsupported IV length\n");
1921 * Check if length of IV to be randomly generated
1922 * is supported by the algorithm chosen.
1924 } else if (iv_random_size != -1) {
1925 if (check_supported_size(iv_random_size,
1928 iv_range_size->increment)
1930 printf("Unsupported IV length\n");
1933 *iv_length = iv_random_size;
1934 /* No size provided, use minimum size. */
1936 *iv_length = iv_range_size->min;
1942 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1943 uint8_t *enabled_cdevs)
1945 unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
1946 const struct rte_cryptodev_capabilities *cap;
1947 unsigned int sess_sz, max_sess_sz = 0;
1950 cdev_count = rte_cryptodev_count();
1951 if (cdev_count == 0) {
1952 printf("No crypto devices available\n");
1956 for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
1957 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1958 if (sess_sz > max_sess_sz)
1959 max_sess_sz = sess_sz;
1962 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1964 struct rte_cryptodev_qp_conf qp_conf;
1965 struct rte_cryptodev_info dev_info;
1966 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
1968 struct rte_cryptodev_config conf = {
1969 .nb_queue_pairs = 1,
1970 .socket_id = socket_id,
1973 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
1976 rte_cryptodev_info_get(cdev_id, &dev_info);
1978 if (session_pool_socket[socket_id] == NULL) {
1979 char mp_name[RTE_MEMPOOL_NAMESIZE];
1980 struct rte_mempool *sess_mp;
1982 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1983 "sess_mp_%u", socket_id);
1986 * Create enough objects for session headers and
1987 * device private data
1989 sess_mp = rte_mempool_create(mp_name,
1992 SESSION_POOL_CACHE_SIZE,
1993 0, NULL, NULL, NULL,
1997 if (sess_mp == NULL) {
1998 printf("Cannot create session pool on socket %d\n",
2003 printf("Allocated session pool on socket %d\n", socket_id);
2004 session_pool_socket[socket_id] = sess_mp;
2007 /* Set AEAD parameters */
2008 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2009 /* Check if device supports AEAD algo */
2010 cap = check_device_support_aead_algo(options, &dev_info,
2015 options->block_size = cap->sym.aead.block_size;
2017 check_iv_param(&cap->sym.aead.iv_size,
2018 options->aead_iv_param,
2019 options->aead_iv_random_size,
2020 &options->aead_iv.length);
2023 * Check if length of provided AEAD key is supported
2024 * by the algorithm chosen.
2026 if (options->aead_key_param) {
2027 if (check_supported_size(
2028 options->aead_xform.aead.key.length,
2029 cap->sym.aead.key_size.min,
2030 cap->sym.aead.key_size.max,
2031 cap->sym.aead.key_size.increment)
2033 printf("Unsupported aead key length\n");
2037 * Check if length of the aead key to be randomly generated
2038 * is supported by the algorithm chosen.
2040 } else if (options->aead_key_random_size != -1) {
2041 if (check_supported_size(options->ckey_random_size,
2042 cap->sym.aead.key_size.min,
2043 cap->sym.aead.key_size.max,
2044 cap->sym.aead.key_size.increment)
2046 printf("Unsupported aead key length\n");
2049 options->aead_xform.aead.key.length =
2050 options->ckey_random_size;
2051 /* No size provided, use minimum size. */
2053 options->aead_xform.aead.key.length =
2054 cap->sym.aead.key_size.min;
2056 if (!options->aead_key_param)
2057 generate_random_key(
2058 options->aead_xform.aead.key.data,
2059 options->aead_xform.aead.key.length);
2062 * Check if length of provided AAD is supported
2063 * by the algorithm chosen.
2065 if (options->aad_param) {
2066 if (check_supported_size(options->aad.length,
2067 cap->sym.aead.aad_size.min,
2068 cap->sym.aead.aad_size.max,
2069 cap->sym.aead.aad_size.increment)
2071 printf("Unsupported AAD length\n");
2075 * Check if length of AAD to be randomly generated
2076 * is supported by the algorithm chosen.
2078 } else if (options->aad_random_size != -1) {
2079 if (check_supported_size(options->aad_random_size,
2080 cap->sym.aead.aad_size.min,
2081 cap->sym.aead.aad_size.max,
2082 cap->sym.aead.aad_size.increment)
2084 printf("Unsupported AAD length\n");
2087 options->aad.length = options->aad_random_size;
2088 /* No size provided, use minimum size. */
2090 options->aad.length = cap->sym.auth.aad_size.min;
2092 options->aead_xform.aead.aad_length =
2093 options->aad.length;
2095 /* Check if digest size is supported by the algorithm. */
2096 if (options->digest_size != -1) {
2097 if (check_supported_size(options->digest_size,
2098 cap->sym.aead.digest_size.min,
2099 cap->sym.aead.digest_size.max,
2100 cap->sym.aead.digest_size.increment)
2102 printf("Unsupported digest length\n");
2105 options->aead_xform.aead.digest_length =
2106 options->digest_size;
2107 /* No size provided, use minimum size. */
2109 options->aead_xform.aead.digest_length =
2110 cap->sym.aead.digest_size.min;
2113 /* Set cipher parameters */
2114 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2115 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2116 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2117 /* Check if device supports cipher algo */
2118 cap = check_device_support_cipher_algo(options, &dev_info,
2123 options->block_size = cap->sym.cipher.block_size;
2125 check_iv_param(&cap->sym.cipher.iv_size,
2126 options->cipher_iv_param,
2127 options->cipher_iv_random_size,
2128 &options->cipher_iv.length);
2131 * Check if length of provided cipher key is supported
2132 * by the algorithm chosen.
2134 if (options->ckey_param) {
2135 if (check_supported_size(
2136 options->cipher_xform.cipher.key.length,
2137 cap->sym.cipher.key_size.min,
2138 cap->sym.cipher.key_size.max,
2139 cap->sym.cipher.key_size.increment)
2141 printf("Unsupported cipher key length\n");
2145 * Check if length of the cipher key to be randomly generated
2146 * is supported by the algorithm chosen.
2148 } else if (options->ckey_random_size != -1) {
2149 if (check_supported_size(options->ckey_random_size,
2150 cap->sym.cipher.key_size.min,
2151 cap->sym.cipher.key_size.max,
2152 cap->sym.cipher.key_size.increment)
2154 printf("Unsupported cipher key length\n");
2157 options->cipher_xform.cipher.key.length =
2158 options->ckey_random_size;
2159 /* No size provided, use minimum size. */
2161 options->cipher_xform.cipher.key.length =
2162 cap->sym.cipher.key_size.min;
2164 if (!options->ckey_param)
2165 generate_random_key(
2166 options->cipher_xform.cipher.key.data,
2167 options->cipher_xform.cipher.key.length);
2171 /* Set auth parameters */
2172 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2173 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2174 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2175 /* Check if device supports auth algo */
2176 cap = check_device_support_auth_algo(options, &dev_info,
2181 check_iv_param(&cap->sym.auth.iv_size,
2182 options->auth_iv_param,
2183 options->auth_iv_random_size,
2184 &options->auth_iv.length);
2186 * Check if length of provided auth key is supported
2187 * by the algorithm chosen.
2189 if (options->akey_param) {
2190 if (check_supported_size(
2191 options->auth_xform.auth.key.length,
2192 cap->sym.auth.key_size.min,
2193 cap->sym.auth.key_size.max,
2194 cap->sym.auth.key_size.increment)
2196 printf("Unsupported auth key length\n");
2200 * Check if length of the auth key to be randomly generated
2201 * is supported by the algorithm chosen.
2203 } else if (options->akey_random_size != -1) {
2204 if (check_supported_size(options->akey_random_size,
2205 cap->sym.auth.key_size.min,
2206 cap->sym.auth.key_size.max,
2207 cap->sym.auth.key_size.increment)
2209 printf("Unsupported auth key length\n");
2212 options->auth_xform.auth.key.length =
2213 options->akey_random_size;
2214 /* No size provided, use minimum size. */
2216 options->auth_xform.auth.key.length =
2217 cap->sym.auth.key_size.min;
2219 if (!options->akey_param)
2220 generate_random_key(
2221 options->auth_xform.auth.key.data,
2222 options->auth_xform.auth.key.length);
2224 /* Check if digest size is supported by the algorithm. */
2225 if (options->digest_size != -1) {
2226 if (check_supported_size(options->digest_size,
2227 cap->sym.auth.digest_size.min,
2228 cap->sym.auth.digest_size.max,
2229 cap->sym.auth.digest_size.increment)
2231 printf("Unsupported digest length\n");
2234 options->auth_xform.auth.digest_length =
2235 options->digest_size;
2236 /* No size provided, use minimum size. */
2238 options->auth_xform.auth.digest_length =
2239 cap->sym.auth.digest_size.min;
2242 retval = rte_cryptodev_configure(cdev_id, &conf);
2244 printf("Failed to configure cryptodev %u", cdev_id);
2248 qp_conf.nb_descriptors = 2048;
2250 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2251 socket_id, session_pool_socket[socket_id]);
2253 printf("Failed to setup queue pair %u on cryptodev %u",
2258 retval = rte_cryptodev_start(cdev_id);
2260 printf("Failed to start device %u: error %d\n",
2265 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2267 enabled_cdevs[cdev_id] = 1;
2268 enabled_cdev_count++;
2271 return enabled_cdev_count;
2275 initialize_ports(struct l2fwd_crypto_options *options)
2277 uint8_t last_portid, portid;
2278 unsigned enabled_portcount = 0;
2279 unsigned nb_ports = rte_eth_dev_count();
2281 if (nb_ports == 0) {
2282 printf("No Ethernet ports - bye\n");
2286 /* Reset l2fwd_dst_ports */
2287 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2288 l2fwd_dst_ports[portid] = 0;
2290 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2293 /* Skip ports that are not enabled */
2294 if ((options->portmask & (1 << portid)) == 0)
2298 printf("Initializing port %u... ", (unsigned) portid);
2300 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2302 printf("Cannot configure device: err=%d, port=%u\n",
2303 retval, (unsigned) portid);
2307 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2310 printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2311 retval, (unsigned) portid);
2315 /* init one RX queue */
2317 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2318 rte_eth_dev_socket_id(portid),
2319 NULL, l2fwd_pktmbuf_pool);
2321 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2322 retval, (unsigned) portid);
2326 /* init one TX queue on each port */
2328 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2329 rte_eth_dev_socket_id(portid),
2332 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2333 retval, (unsigned) portid);
2339 retval = rte_eth_dev_start(portid);
2341 printf("rte_eth_dev_start:err=%d, port=%u\n",
2342 retval, (unsigned) portid);
2346 rte_eth_promiscuous_enable(portid);
2348 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2350 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2352 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2353 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2354 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2355 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2356 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2357 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2359 /* initialize port stats */
2360 memset(&port_statistics, 0, sizeof(port_statistics));
2362 /* Setup port forwarding table */
2363 if (enabled_portcount % 2) {
2364 l2fwd_dst_ports[portid] = last_portid;
2365 l2fwd_dst_ports[last_portid] = portid;
2367 last_portid = portid;
2370 l2fwd_enabled_port_mask |= (1 << portid);
2371 enabled_portcount++;
2374 if (enabled_portcount == 1) {
2375 l2fwd_dst_ports[last_portid] = last_portid;
2376 } else if (enabled_portcount % 2) {
2377 printf("odd number of ports in portmask- bye\n");
2381 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2383 return enabled_portcount;
2387 reserve_key_memory(struct l2fwd_crypto_options *options)
2389 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2391 if (options->cipher_xform.cipher.key.data == NULL)
2392 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2394 options->auth_xform.auth.key.data = rte_malloc("auth key",
2396 if (options->auth_xform.auth.key.data == NULL)
2397 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2399 options->aead_xform.aead.key.data = rte_malloc("aead key",
2401 if (options->aead_xform.aead.key.data == NULL)
2402 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2404 options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2405 if (options->cipher_iv.data == NULL)
2406 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2408 options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2409 if (options->auth_iv.data == NULL)
2410 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2412 options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2413 if (options->aead_iv.data == NULL)
2414 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2416 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2417 if (options->aad.data == NULL)
2418 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2419 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
2423 main(int argc, char **argv)
2425 struct lcore_queue_conf *qconf;
2426 struct l2fwd_crypto_options options;
2428 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
2429 unsigned lcore_id, rx_lcore_id;
2430 int ret, enabled_cdevcount, enabled_portcount;
2431 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2434 ret = rte_eal_init(argc, argv);
2436 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2440 /* reserve memory for Cipher/Auth key and IV */
2441 reserve_key_memory(&options);
2443 /* parse application arguments (after the EAL ones) */
2444 ret = l2fwd_crypto_parse_args(&options, argc, argv);
2446 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2448 /* create the mbuf pool */
2449 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2450 sizeof(struct rte_crypto_op),
2451 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2452 if (l2fwd_pktmbuf_pool == NULL)
2453 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2455 /* create crypto op pool */
2456 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2457 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2459 if (l2fwd_crypto_op_pool == NULL)
2460 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2462 /* Enable Ethernet ports */
2463 enabled_portcount = initialize_ports(&options);
2464 if (enabled_portcount < 1)
2465 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2467 nb_ports = rte_eth_dev_count();
2468 /* Initialize the port/queue configuration of each logical core */
2469 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2470 portid < nb_ports; portid++) {
2472 /* skip ports that are not enabled */
2473 if ((options.portmask & (1 << portid)) == 0)
2476 if (options.single_lcore && qconf == NULL) {
2477 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2479 if (rx_lcore_id >= RTE_MAX_LCORE)
2480 rte_exit(EXIT_FAILURE,
2481 "Not enough cores\n");
2483 } else if (!options.single_lcore) {
2484 /* get the lcore_id for this port */
2485 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2486 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2487 options.nb_ports_per_lcore) {
2489 if (rx_lcore_id >= RTE_MAX_LCORE)
2490 rte_exit(EXIT_FAILURE,
2491 "Not enough cores\n");
2495 /* Assigned a new logical core in the loop above. */
2496 if (qconf != &lcore_queue_conf[rx_lcore_id])
2497 qconf = &lcore_queue_conf[rx_lcore_id];
2499 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2500 qconf->nb_rx_ports++;
2502 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
2505 /* Enable Crypto devices */
2506 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2508 if (enabled_cdevcount < 0)
2509 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2511 if (enabled_cdevcount < enabled_portcount)
2512 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2513 "has to be more or equal to number of ports (%d)\n",
2514 enabled_cdevcount, enabled_portcount);
2516 nb_cryptodevs = rte_cryptodev_count();
2518 /* Initialize the port/cryptodev configuration of each logical core */
2519 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2520 cdev_id < nb_cryptodevs && enabled_cdevcount;
2522 /* Crypto op not supported by crypto device */
2523 if (!enabled_cdevs[cdev_id])
2526 if (options.single_lcore && qconf == NULL) {
2527 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2529 if (rx_lcore_id >= RTE_MAX_LCORE)
2530 rte_exit(EXIT_FAILURE,
2531 "Not enough cores\n");
2533 } else if (!options.single_lcore) {
2534 /* get the lcore_id for this port */
2535 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2536 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2537 options.nb_ports_per_lcore) {
2539 if (rx_lcore_id >= RTE_MAX_LCORE)
2540 rte_exit(EXIT_FAILURE,
2541 "Not enough cores\n");
2545 /* Assigned a new logical core in the loop above. */
2546 if (qconf != &lcore_queue_conf[rx_lcore_id])
2547 qconf = &lcore_queue_conf[rx_lcore_id];
2549 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2550 qconf->nb_crypto_devs++;
2552 enabled_cdevcount--;
2554 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2558 /* launch per-lcore init on every lcore */
2559 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2561 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2562 if (rte_eal_wait_lcore(lcore_id) < 0)