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>
76 #include <rte_hexdump.h>
84 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
88 #define MAX_STR_LEN 32
89 #define MAX_KEY_SIZE 128
90 #define MAX_PKT_BURST 32
91 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
94 * Configurable number of RX/TX ring descriptors
96 #define RTE_TEST_RX_DESC_DEFAULT 128
97 #define RTE_TEST_TX_DESC_DEFAULT 512
99 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
100 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
102 /* ethernet addresses of ports */
103 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
105 /* mask of enabled ports */
106 static uint64_t l2fwd_enabled_port_mask;
107 static uint64_t l2fwd_enabled_crypto_mask;
109 /* list of enabled ports */
110 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
115 struct rte_mbuf *buffer[MAX_PKT_BURST];
120 struct rte_crypto_op *buffer[MAX_PKT_BURST];
123 #define MAX_RX_QUEUE_PER_LCORE 16
124 #define MAX_TX_QUEUE_PER_PORT 16
126 enum l2fwd_crypto_xform_chain {
127 L2FWD_CRYPTO_CIPHER_HASH,
128 L2FWD_CRYPTO_HASH_CIPHER,
129 L2FWD_CRYPTO_CIPHER_ONLY,
130 L2FWD_CRYPTO_HASH_ONLY
136 phys_addr_t phys_addr;
139 char supported_auth_algo[RTE_CRYPTO_AUTH_LIST_END][MAX_STR_LEN];
140 char supported_cipher_algo[RTE_CRYPTO_CIPHER_LIST_END][MAX_STR_LEN];
142 /** l2fwd crypto application command line options */
143 struct l2fwd_crypto_options {
145 unsigned nb_ports_per_lcore;
146 unsigned refresh_period;
147 unsigned single_lcore:1;
150 unsigned sessionless:1;
152 enum l2fwd_crypto_xform_chain xform_chain;
154 struct rte_crypto_sym_xform cipher_xform;
156 int ckey_random_size;
162 struct rte_crypto_sym_xform auth_xform;
164 int akey_random_size;
166 struct l2fwd_key aad;
173 char string_type[MAX_STR_LEN];
176 /** l2fwd crypto lcore params */
177 struct l2fwd_crypto_params {
181 unsigned digest_length;
185 struct l2fwd_key aad;
186 struct rte_cryptodev_sym_session *session;
192 enum rte_crypto_cipher_algorithm cipher_algo;
193 enum rte_crypto_auth_algorithm auth_algo;
196 /** lcore configuration */
197 struct lcore_queue_conf {
198 unsigned nb_rx_ports;
199 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
201 unsigned nb_crypto_devs;
202 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
204 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
205 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
206 } __rte_cache_aligned;
208 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
210 static const struct rte_eth_conf port_conf = {
212 .mq_mode = ETH_MQ_RX_NONE,
213 .max_rx_pkt_len = ETHER_MAX_LEN,
215 .header_split = 0, /**< Header Split disabled */
216 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
217 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
218 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
219 .hw_strip_crc = 0, /**< CRC stripped by hardware */
222 .mq_mode = ETH_MQ_TX_NONE,
226 struct rte_mempool *l2fwd_pktmbuf_pool;
227 struct rte_mempool *l2fwd_crypto_op_pool;
229 /* Per-port statistics struct */
230 struct l2fwd_port_statistics {
234 uint64_t crypto_enqueued;
235 uint64_t crypto_dequeued;
238 } __rte_cache_aligned;
240 struct l2fwd_crypto_statistics {
245 } __rte_cache_aligned;
247 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
248 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
250 /* A tsc-based timer responsible for triggering statistics printout */
251 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
252 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
254 /* default period is 10 seconds */
255 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
257 /* Print out statistics on packets dropped */
261 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
262 uint64_t total_packets_enqueued, total_packets_dequeued,
263 total_packets_errors;
267 total_packets_dropped = 0;
268 total_packets_tx = 0;
269 total_packets_rx = 0;
270 total_packets_enqueued = 0;
271 total_packets_dequeued = 0;
272 total_packets_errors = 0;
274 const char clr[] = { 27, '[', '2', 'J', '\0' };
275 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
277 /* Clear screen and move to top left */
278 printf("%s%s", clr, topLeft);
280 printf("\nPort statistics ====================================");
282 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
283 /* skip disabled ports */
284 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
286 printf("\nStatistics for port %u ------------------------------"
287 "\nPackets sent: %32"PRIu64
288 "\nPackets received: %28"PRIu64
289 "\nPackets dropped: %29"PRIu64,
291 port_statistics[portid].tx,
292 port_statistics[portid].rx,
293 port_statistics[portid].dropped);
295 total_packets_dropped += port_statistics[portid].dropped;
296 total_packets_tx += port_statistics[portid].tx;
297 total_packets_rx += port_statistics[portid].rx;
299 printf("\nCrypto statistics ==================================");
301 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
302 /* skip disabled ports */
303 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
305 printf("\nStatistics for cryptodev %"PRIu64
306 " -------------------------"
307 "\nPackets enqueued: %28"PRIu64
308 "\nPackets dequeued: %28"PRIu64
309 "\nPackets errors: %30"PRIu64,
311 crypto_statistics[cdevid].enqueued,
312 crypto_statistics[cdevid].dequeued,
313 crypto_statistics[cdevid].errors);
315 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
316 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
317 total_packets_errors += crypto_statistics[cdevid].errors;
319 printf("\nAggregate statistics ==============================="
320 "\nTotal packets received: %22"PRIu64
321 "\nTotal packets enqueued: %22"PRIu64
322 "\nTotal packets dequeued: %22"PRIu64
323 "\nTotal packets sent: %26"PRIu64
324 "\nTotal packets dropped: %23"PRIu64
325 "\nTotal packets crypto errors: %17"PRIu64,
327 total_packets_enqueued,
328 total_packets_dequeued,
330 total_packets_dropped,
331 total_packets_errors);
332 printf("\n====================================================\n");
336 fill_supported_algorithm_tables(void)
340 for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++)
341 strcpy(supported_auth_algo[i], "NOT_SUPPORTED");
343 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_AES_GCM], "AES_GCM");
344 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_MD5_HMAC], "MD5_HMAC");
345 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_NULL], "NULL");
346 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_AES_XCBC_MAC],
348 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA1_HMAC], "SHA1_HMAC");
349 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA224_HMAC], "SHA224_HMAC");
350 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA256_HMAC], "SHA256_HMAC");
351 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA384_HMAC], "SHA384_HMAC");
352 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA512_HMAC], "SHA512_HMAC");
353 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SNOW3G_UIA2], "SNOW3G_UIA2");
354 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_KASUMI_F9], "KASUMI_F9");
356 for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++)
357 strcpy(supported_cipher_algo[i], "NOT_SUPPORTED");
359 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_CBC], "AES_CBC");
360 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_CTR], "AES_CTR");
361 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_GCM], "AES_GCM");
362 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_NULL], "NULL");
363 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_SNOW3G_UEA2], "SNOW3G_UEA2");
364 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_KASUMI_F8], "KASUMI_F8");
369 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
370 struct l2fwd_crypto_params *cparams)
372 struct rte_crypto_op **op_buffer;
375 op_buffer = (struct rte_crypto_op **)
376 qconf->op_buf[cparams->dev_id].buffer;
378 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
379 cparams->qp_id, op_buffer, (uint16_t) n);
381 crypto_statistics[cparams->dev_id].enqueued += ret;
382 if (unlikely(ret < n)) {
383 crypto_statistics[cparams->dev_id].errors += (n - ret);
385 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
386 rte_crypto_op_free(op_buffer[ret]);
394 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
395 struct l2fwd_crypto_params *cparams)
397 unsigned lcore_id, len;
398 struct lcore_queue_conf *qconf;
400 lcore_id = rte_lcore_id();
402 qconf = &lcore_queue_conf[lcore_id];
403 len = qconf->op_buf[cparams->dev_id].len;
404 qconf->op_buf[cparams->dev_id].buffer[len] = op;
407 /* enough ops to be sent */
408 if (len == MAX_PKT_BURST) {
409 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
413 qconf->op_buf[cparams->dev_id].len = len;
418 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
419 struct rte_crypto_op *op,
420 struct l2fwd_crypto_params *cparams)
422 struct ether_hdr *eth_hdr;
423 struct ipv4_hdr *ip_hdr;
425 unsigned ipdata_offset, pad_len, data_len;
428 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
430 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
433 ipdata_offset = sizeof(struct ether_hdr);
435 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
438 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
439 * IPV4_IHL_MULTIPLIER;
442 /* Zero pad data to be crypto'd so it is block aligned */
443 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
444 pad_len = data_len % cparams->block_size ? cparams->block_size -
445 (data_len % cparams->block_size) : 0;
448 padding = rte_pktmbuf_append(m, pad_len);
449 if (unlikely(!padding))
453 memset(padding, 0, pad_len);
456 /* Set crypto operation data parameters */
457 rte_crypto_op_attach_sym_session(op, cparams->session);
459 if (cparams->do_hash) {
460 if (!cparams->hash_verify) {
461 /* Append space for digest to end of packet */
462 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
463 cparams->digest_length);
465 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
466 cparams->digest_length);
469 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
470 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
471 op->sym->auth.digest.length = cparams->digest_length;
473 /* For SNOW3G/KASUMI algorithms, offset/length must be in bits */
474 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
475 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9) {
476 op->sym->auth.data.offset = ipdata_offset << 3;
477 op->sym->auth.data.length = data_len << 3;
479 op->sym->auth.data.offset = ipdata_offset;
480 op->sym->auth.data.length = data_len;
483 if (cparams->aad.length) {
484 op->sym->auth.aad.data = cparams->aad.data;
485 op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
486 op->sym->auth.aad.length = cparams->aad.length;
490 if (cparams->do_cipher) {
491 op->sym->cipher.iv.data = cparams->iv.data;
492 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
493 op->sym->cipher.iv.length = cparams->iv.length;
495 /* For SNOW3G algorithms, offset/length must be in bits */
496 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
497 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8) {
498 op->sym->cipher.data.offset = ipdata_offset << 3;
499 if (cparams->do_hash && cparams->hash_verify)
500 /* Do not cipher the hash tag */
501 op->sym->cipher.data.length = (data_len -
502 cparams->digest_length) << 3;
504 op->sym->cipher.data.length = data_len << 3;
507 op->sym->cipher.data.offset = ipdata_offset;
508 if (cparams->do_hash && cparams->hash_verify)
509 /* Do not cipher the hash tag */
510 op->sym->cipher.data.length = data_len -
511 cparams->digest_length;
513 op->sym->cipher.data.length = data_len;
519 return l2fwd_crypto_enqueue(op, cparams);
523 /* Send the burst of packets on an output interface */
525 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
528 struct rte_mbuf **pkt_buffer;
531 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
533 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
534 port_statistics[port].tx += ret;
535 if (unlikely(ret < n)) {
536 port_statistics[port].dropped += (n - ret);
538 rte_pktmbuf_free(pkt_buffer[ret]);
545 /* Enqueue packets for TX and prepare them to be sent */
547 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
549 unsigned lcore_id, len;
550 struct lcore_queue_conf *qconf;
552 lcore_id = rte_lcore_id();
554 qconf = &lcore_queue_conf[lcore_id];
555 len = qconf->pkt_buf[port].len;
556 qconf->pkt_buf[port].buffer[len] = m;
559 /* enough pkts to be sent */
560 if (unlikely(len == MAX_PKT_BURST)) {
561 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
565 qconf->pkt_buf[port].len = len;
570 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
572 struct ether_hdr *eth;
576 dst_port = l2fwd_dst_ports[portid];
577 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
579 /* 02:00:00:00:00:xx */
580 tmp = ð->d_addr.addr_bytes[0];
581 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
584 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
586 l2fwd_send_packet(m, (uint8_t) dst_port);
589 /** Generate random key */
591 generate_random_key(uint8_t *key, unsigned length)
596 fd = open("/dev/urandom", O_RDONLY);
598 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
600 ret = read(fd, key, length);
603 if (ret != (signed)length)
604 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
607 static struct rte_cryptodev_sym_session *
608 initialize_crypto_session(struct l2fwd_crypto_options *options,
611 struct rte_crypto_sym_xform *first_xform;
613 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
614 first_xform = &options->cipher_xform;
615 first_xform->next = &options->auth_xform;
616 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
617 first_xform = &options->auth_xform;
618 first_xform->next = &options->cipher_xform;
619 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
620 first_xform = &options->cipher_xform;
622 first_xform = &options->auth_xform;
625 /* Setup Cipher Parameters */
626 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
630 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
632 /* main processing loop */
634 l2fwd_main_loop(struct l2fwd_crypto_options *options)
636 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
637 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
639 unsigned lcore_id = rte_lcore_id();
640 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
641 unsigned i, j, portid, nb_rx, len;
642 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
643 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
644 US_PER_S * BURST_TX_DRAIN_US;
645 struct l2fwd_crypto_params *cparams;
646 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
648 if (qconf->nb_rx_ports == 0) {
649 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
653 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
655 for (i = 0; i < qconf->nb_rx_ports; i++) {
657 portid = qconf->rx_port_list[i];
658 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
662 for (i = 0; i < qconf->nb_crypto_devs; i++) {
663 port_cparams[i].do_cipher = 0;
664 port_cparams[i].do_hash = 0;
666 switch (options->xform_chain) {
667 case L2FWD_CRYPTO_CIPHER_HASH:
668 case L2FWD_CRYPTO_HASH_CIPHER:
669 port_cparams[i].do_cipher = 1;
670 port_cparams[i].do_hash = 1;
672 case L2FWD_CRYPTO_HASH_ONLY:
673 port_cparams[i].do_hash = 1;
675 case L2FWD_CRYPTO_CIPHER_ONLY:
676 port_cparams[i].do_cipher = 1;
680 port_cparams[i].dev_id = qconf->cryptodev_list[i];
681 port_cparams[i].qp_id = 0;
683 port_cparams[i].block_size = options->block_size;
685 if (port_cparams[i].do_hash) {
686 port_cparams[i].digest_length =
687 options->auth_xform.auth.digest_length;
688 if (options->auth_xform.auth.add_auth_data_length) {
689 port_cparams[i].aad.data = options->aad.data;
690 port_cparams[i].aad.length =
691 options->auth_xform.auth.add_auth_data_length;
692 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
693 if (!options->aad_param)
694 generate_random_key(port_cparams[i].aad.data,
695 port_cparams[i].aad.length);
699 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
700 port_cparams[i].hash_verify = 1;
702 port_cparams[i].hash_verify = 0;
704 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
707 if (port_cparams[i].do_cipher) {
708 port_cparams[i].iv.data = options->iv.data;
709 port_cparams[i].iv.length = options->iv.length;
710 port_cparams[i].iv.phys_addr = options->iv.phys_addr;
711 if (!options->iv_param)
712 generate_random_key(port_cparams[i].iv.data,
713 port_cparams[i].iv.length);
715 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
718 port_cparams[i].session = initialize_crypto_session(options,
719 port_cparams[i].dev_id);
721 if (port_cparams[i].session == NULL)
723 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
724 port_cparams[i].dev_id);
727 l2fwd_crypto_options_print(options);
730 * Initialize previous tsc timestamp before the loop,
731 * to avoid showing the port statistics immediately,
732 * so user can see the crypto information.
734 prev_tsc = rte_rdtsc();
737 cur_tsc = rte_rdtsc();
740 * Crypto device/TX burst queue drain
742 diff_tsc = cur_tsc - prev_tsc;
743 if (unlikely(diff_tsc > drain_tsc)) {
744 /* Enqueue all crypto ops remaining in buffers */
745 for (i = 0; i < qconf->nb_crypto_devs; i++) {
746 cparams = &port_cparams[i];
747 len = qconf->op_buf[cparams->dev_id].len;
748 l2fwd_crypto_send_burst(qconf, len, cparams);
749 qconf->op_buf[cparams->dev_id].len = 0;
751 /* Transmit all packets remaining in buffers */
752 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
753 if (qconf->pkt_buf[portid].len == 0)
755 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
756 qconf->pkt_buf[portid].len,
758 qconf->pkt_buf[portid].len = 0;
761 /* if timer is enabled */
762 if (timer_period > 0) {
764 /* advance the timer */
765 timer_tsc += diff_tsc;
767 /* if timer has reached its timeout */
768 if (unlikely(timer_tsc >=
769 (uint64_t)timer_period)) {
771 /* do this only on master core */
772 if (lcore_id == rte_get_master_lcore()
773 && options->refresh_period) {
784 * Read packet from RX queues
786 for (i = 0; i < qconf->nb_rx_ports; i++) {
787 portid = qconf->rx_port_list[i];
789 cparams = &port_cparams[i];
791 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
792 pkts_burst, MAX_PKT_BURST);
794 port_statistics[portid].rx += nb_rx;
798 * If we can't allocate a crypto_ops, then drop
799 * the rest of the burst and dequeue and
800 * process the packets to free offload structs
802 if (rte_crypto_op_bulk_alloc(
803 l2fwd_crypto_op_pool,
804 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
807 for (j = 0; j < nb_rx; j++)
808 rte_pktmbuf_free(pkts_burst[i]);
813 /* Enqueue packets from Crypto device*/
814 for (j = 0; j < nb_rx; j++) {
817 l2fwd_simple_crypto_enqueue(m,
818 ops_burst[j], cparams);
822 /* Dequeue packets from Crypto device */
824 nb_rx = rte_cryptodev_dequeue_burst(
825 cparams->dev_id, cparams->qp_id,
826 ops_burst, MAX_PKT_BURST);
828 crypto_statistics[cparams->dev_id].dequeued +=
831 /* Forward crypto'd packets */
832 for (j = 0; j < nb_rx; j++) {
833 m = ops_burst[j]->sym->m_src;
835 rte_crypto_op_free(ops_burst[j]);
836 l2fwd_simple_forward(m, portid);
838 } while (nb_rx == MAX_PKT_BURST);
844 l2fwd_launch_one_lcore(void *arg)
846 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
850 /* Display command line arguments usage */
852 l2fwd_crypto_usage(const char *prgname)
854 printf("%s [EAL options] --\n"
855 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
856 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
857 " -s manage all ports from single lcore\n"
858 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
859 " (0 to disable, 10 default, 86400 maximum)\n"
861 " --cdev_type HW / SW / ANY\n"
862 " --chain HASH_CIPHER / CIPHER_HASH\n"
864 " --cipher_algo ALGO\n"
865 " --cipher_op ENCRYPT / DECRYPT\n"
866 " --cipher_key KEY (bytes separated with \":\")\n"
867 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
868 " --iv IV (bytes separated with \":\")\n"
869 " --iv_random_size SIZE: size of IV when generated randomly\n"
871 " --auth_algo ALGO\n"
872 " --auth_op GENERATE / VERIFY\n"
873 " --auth_key KEY (bytes separated with \":\")\n"
874 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
875 " --aad AAD (bytes separated with \":\")\n"
876 " --aad_random_size SIZE: size of AAD when generated randomly\n"
877 " --digest_size SIZE: size of digest to be generated/verified\n"
883 /** Parse crypto device type command line argument */
885 parse_cryptodev_type(enum cdev_type *type, char *optarg)
887 if (strcmp("HW", optarg) == 0) {
888 *type = CDEV_TYPE_HW;
890 } else if (strcmp("SW", optarg) == 0) {
891 *type = CDEV_TYPE_SW;
893 } else if (strcmp("ANY", optarg) == 0) {
894 *type = CDEV_TYPE_ANY;
901 /** Parse crypto chain xform command line argument */
903 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
905 if (strcmp("CIPHER_HASH", optarg) == 0) {
906 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
908 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
909 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
911 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
912 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
914 } else if (strcmp("HASH_ONLY", optarg) == 0) {
915 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
922 /** Parse crypto cipher algo option command line argument */
924 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
928 for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++) {
929 if (!strcmp(supported_cipher_algo[i], optarg)) {
930 *algo = (enum rte_crypto_cipher_algorithm)i;
935 printf("Cipher algorithm not supported!\n");
939 /** Parse crypto cipher operation command line argument */
941 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
943 if (strcmp("ENCRYPT", optarg) == 0) {
944 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
946 } else if (strcmp("DECRYPT", optarg) == 0) {
947 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
951 printf("Cipher operation not supported!\n");
955 /** Parse crypto key command line argument */
957 parse_key(uint8_t *data, char *input_arg)
962 for (byte_count = 0, token = strtok(input_arg, ":");
963 (byte_count < MAX_KEY_SIZE) && (token != NULL);
964 token = strtok(NULL, ":")) {
966 int number = (int)strtol(token, NULL, 16);
968 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
971 data[byte_count++] = (uint8_t)number;
977 /** Parse size param*/
979 parse_size(int *size, const char *q_arg)
984 /* parse hexadecimal string */
985 n = strtoul(q_arg, &end, 10);
986 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
990 printf("invalid size\n");
998 /** Parse crypto cipher operation command line argument */
1000 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1004 for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++) {
1005 if (!strcmp(supported_auth_algo[i], optarg)) {
1006 *algo = (enum rte_crypto_auth_algorithm)i;
1011 printf("Authentication algorithm specified not supported!\n");
1016 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1018 if (strcmp("VERIFY", optarg) == 0) {
1019 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1021 } else if (strcmp("GENERATE", optarg) == 0) {
1022 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1026 printf("Authentication operation specified not supported!\n");
1030 /** Parse long options */
1032 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1033 struct option *lgopts, int option_index)
1037 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1038 retval = parse_cryptodev_type(&options->type, optarg);
1040 snprintf(options->string_type, MAX_STR_LEN,
1045 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1046 return parse_crypto_opt_chain(options, optarg);
1048 /* Cipher options */
1049 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1050 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1053 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1054 return parse_cipher_op(&options->cipher_xform.cipher.op,
1057 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1058 options->ckey_param = 1;
1059 options->cipher_xform.cipher.key.length =
1060 parse_key(options->cipher_xform.cipher.key.data, optarg);
1061 if (options->cipher_xform.cipher.key.length > 0)
1067 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1068 return parse_size(&options->ckey_random_size, optarg);
1070 else if (strcmp(lgopts[option_index].name, "iv") == 0) {
1071 options->iv_param = 1;
1072 options->iv.length =
1073 parse_key(options->iv.data, optarg);
1074 if (options->iv.length > 0)
1080 else if (strcmp(lgopts[option_index].name, "iv_random_size") == 0)
1081 return parse_size(&options->iv_random_size, optarg);
1083 /* Authentication options */
1084 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1085 return parse_auth_algo(&options->auth_xform.auth.algo,
1089 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1090 return parse_auth_op(&options->auth_xform.auth.op,
1093 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1094 options->akey_param = 1;
1095 options->auth_xform.auth.key.length =
1096 parse_key(options->auth_xform.auth.key.data, optarg);
1097 if (options->auth_xform.auth.key.length > 0)
1103 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1104 return parse_size(&options->akey_random_size, optarg);
1107 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1108 options->aad_param = 1;
1109 options->aad.length =
1110 parse_key(options->aad.data, optarg);
1111 if (options->aad.length > 0)
1117 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1118 return parse_size(&options->aad_random_size, optarg);
1121 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1122 return parse_size(&options->digest_size, optarg);
1125 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1126 options->sessionless = 1;
1133 /** Parse port mask */
1135 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1141 /* parse hexadecimal string */
1142 pm = strtoul(q_arg, &end, 16);
1143 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1146 options->portmask = pm;
1147 if (options->portmask == 0) {
1148 printf("invalid portmask specified\n");
1155 /** Parse number of queues */
1157 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1163 /* parse hexadecimal string */
1164 n = strtoul(q_arg, &end, 10);
1165 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1167 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1170 options->nb_ports_per_lcore = n;
1171 if (options->nb_ports_per_lcore == 0) {
1172 printf("invalid number of ports selected\n");
1179 /** Parse timer period */
1181 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1187 /* parse number string */
1188 n = (unsigned)strtol(q_arg, &end, 10);
1189 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1192 if (n >= MAX_TIMER_PERIOD) {
1193 printf("Warning refresh period specified %lu is greater than "
1194 "max value %lu! using max value",
1195 n, MAX_TIMER_PERIOD);
1196 n = MAX_TIMER_PERIOD;
1199 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1204 /** Generate default options for application */
1206 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1208 options->portmask = 0xffffffff;
1209 options->nb_ports_per_lcore = 1;
1210 options->refresh_period = 10000;
1211 options->single_lcore = 0;
1212 options->sessionless = 0;
1214 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1217 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1218 options->cipher_xform.next = NULL;
1219 options->ckey_param = 0;
1220 options->ckey_random_size = -1;
1221 options->cipher_xform.cipher.key.length = 0;
1222 options->iv_param = 0;
1223 options->iv_random_size = -1;
1224 options->iv.length = 0;
1226 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1227 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1229 /* Authentication Data */
1230 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1231 options->auth_xform.next = NULL;
1232 options->akey_param = 0;
1233 options->akey_random_size = -1;
1234 options->auth_xform.auth.key.length = 0;
1235 options->aad_param = 0;
1236 options->aad_random_size = -1;
1237 options->aad.length = 0;
1238 options->digest_size = -1;
1240 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1241 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1243 options->type = CDEV_TYPE_ANY;
1247 display_cipher_info(struct l2fwd_crypto_options *options)
1249 printf("\n---- Cipher information ---\n");
1250 printf("Algorithm: %s\n",
1251 supported_cipher_algo[options->cipher_xform.cipher.algo]);
1252 rte_hexdump(stdout, "Cipher key:",
1253 options->cipher_xform.cipher.key.data,
1254 options->cipher_xform.cipher.key.length);
1255 rte_hexdump(stdout, "IV:", options->iv.data, options->iv.length);
1259 display_auth_info(struct l2fwd_crypto_options *options)
1261 printf("\n---- Authentication information ---\n");
1262 printf("Algorithm: %s\n",
1263 supported_auth_algo[options->auth_xform.auth.algo]);
1264 rte_hexdump(stdout, "Auth key:",
1265 options->auth_xform.auth.key.data,
1266 options->auth_xform.auth.key.length);
1267 rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1271 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1273 char string_cipher_op[MAX_STR_LEN];
1274 char string_auth_op[MAX_STR_LEN];
1276 if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1277 strcpy(string_cipher_op, "Encrypt");
1279 strcpy(string_cipher_op, "Decrypt");
1281 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1282 strcpy(string_auth_op, "Auth generate");
1284 strcpy(string_auth_op, "Auth verify");
1286 printf("Options:-\nn");
1287 printf("portmask: %x\n", options->portmask);
1288 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1289 printf("refresh period : %u\n", options->refresh_period);
1290 printf("single lcore mode: %s\n",
1291 options->single_lcore ? "enabled" : "disabled");
1292 printf("stats_printing: %s\n",
1293 options->refresh_period == 0 ? "disabled" : "enabled");
1295 printf("sessionless crypto: %s\n",
1296 options->sessionless ? "enabled" : "disabled");
1298 if (options->ckey_param && (options->ckey_random_size != -1))
1299 printf("Cipher key already parsed, ignoring size of random key\n");
1301 if (options->akey_param && (options->akey_random_size != -1))
1302 printf("Auth key already parsed, ignoring size of random key\n");
1304 if (options->iv_param && (options->iv_random_size != -1))
1305 printf("IV already parsed, ignoring size of random IV\n");
1307 if (options->aad_param && (options->aad_random_size != -1))
1308 printf("AAD already parsed, ignoring size of random AAD\n");
1310 printf("\nCrypto chain: ");
1311 switch (options->xform_chain) {
1312 case L2FWD_CRYPTO_CIPHER_HASH:
1313 printf("Input --> %s --> %s --> Output\n",
1314 string_cipher_op, string_auth_op);
1315 display_cipher_info(options);
1316 display_auth_info(options);
1318 case L2FWD_CRYPTO_HASH_CIPHER:
1319 printf("Input --> %s --> %s --> Output\n",
1320 string_auth_op, string_cipher_op);
1321 display_cipher_info(options);
1322 display_auth_info(options);
1324 case L2FWD_CRYPTO_HASH_ONLY:
1325 printf("Input --> %s --> Output\n", string_auth_op);
1326 display_auth_info(options);
1328 case L2FWD_CRYPTO_CIPHER_ONLY:
1329 printf("Input --> %s --> Output\n", string_cipher_op);
1330 display_cipher_info(options);
1335 /* Parse the argument given in the command line of the application */
1337 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1338 int argc, char **argv)
1340 int opt, retval, option_index;
1341 char **argvopt = argv, *prgname = argv[0];
1343 static struct option lgopts[] = {
1344 { "sessionless", no_argument, 0, 0 },
1346 { "cdev_type", required_argument, 0, 0 },
1347 { "chain", required_argument, 0, 0 },
1349 { "cipher_algo", required_argument, 0, 0 },
1350 { "cipher_op", required_argument, 0, 0 },
1351 { "cipher_key", required_argument, 0, 0 },
1352 { "cipher_key_random_size", required_argument, 0, 0 },
1354 { "auth_algo", required_argument, 0, 0 },
1355 { "auth_op", required_argument, 0, 0 },
1356 { "auth_key", required_argument, 0, 0 },
1357 { "auth_key_random_size", required_argument, 0, 0 },
1359 { "iv", required_argument, 0, 0 },
1360 { "iv_random_size", required_argument, 0, 0 },
1361 { "aad", required_argument, 0, 0 },
1362 { "aad_random_size", required_argument, 0, 0 },
1363 { "digest_size", required_argument, 0, 0 },
1365 { "sessionless", no_argument, 0, 0 },
1370 l2fwd_crypto_default_options(options);
1372 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1373 &option_index)) != EOF) {
1377 retval = l2fwd_crypto_parse_args_long_options(options,
1378 lgopts, option_index);
1380 l2fwd_crypto_usage(prgname);
1387 retval = l2fwd_crypto_parse_portmask(options, optarg);
1389 l2fwd_crypto_usage(prgname);
1396 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1398 l2fwd_crypto_usage(prgname);
1405 options->single_lcore = 1;
1411 retval = l2fwd_crypto_parse_timer_period(options,
1414 l2fwd_crypto_usage(prgname);
1420 l2fwd_crypto_usage(prgname);
1427 argv[optind-1] = prgname;
1430 optind = 0; /* reset getopt lib */
1435 /* Check the link status of all ports in up to 9s, and print them finally */
1437 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1439 #define CHECK_INTERVAL 100 /* 100ms */
1440 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1441 uint8_t portid, count, all_ports_up, print_flag = 0;
1442 struct rte_eth_link link;
1444 printf("\nChecking link status");
1446 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1448 for (portid = 0; portid < port_num; portid++) {
1449 if ((port_mask & (1 << portid)) == 0)
1451 memset(&link, 0, sizeof(link));
1452 rte_eth_link_get_nowait(portid, &link);
1453 /* print link status if flag set */
1454 if (print_flag == 1) {
1455 if (link.link_status)
1456 printf("Port %d Link Up - speed %u "
1457 "Mbps - %s\n", (uint8_t)portid,
1458 (unsigned)link.link_speed,
1459 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1460 ("full-duplex") : ("half-duplex\n"));
1462 printf("Port %d Link Down\n",
1466 /* clear all_ports_up flag if any link down */
1467 if (link.link_status == ETH_LINK_DOWN) {
1472 /* after finally printing all link status, get out */
1473 if (print_flag == 1)
1476 if (all_ports_up == 0) {
1479 rte_delay_ms(CHECK_INTERVAL);
1482 /* set the print_flag if all ports up or timeout */
1483 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1490 /* Check if device has to be HW/SW or any */
1492 check_type(struct l2fwd_crypto_options *options, struct rte_cryptodev_info *dev_info)
1494 if (options->type == CDEV_TYPE_HW &&
1495 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1497 if (options->type == CDEV_TYPE_SW &&
1498 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1500 if (options->type == CDEV_TYPE_ANY)
1507 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1513 if (increment == 0) {
1520 /* Range of values */
1521 for (supp_size = min; supp_size <= max; supp_size += increment) {
1522 if (length == supp_size)
1529 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1530 uint8_t *enabled_cdevs)
1532 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1533 const struct rte_cryptodev_capabilities *cap;
1534 enum rte_crypto_auth_algorithm cap_auth_algo;
1535 enum rte_crypto_auth_algorithm opt_auth_algo;
1536 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1537 enum rte_crypto_cipher_algorithm opt_cipher_algo;
1540 cdev_count = rte_cryptodev_count();
1541 if (cdev_count == 0) {
1542 printf("No crypto devices available\n");
1546 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1548 struct rte_cryptodev_qp_conf qp_conf;
1549 struct rte_cryptodev_info dev_info;
1551 struct rte_cryptodev_config conf = {
1552 .nb_queue_pairs = 1,
1553 .socket_id = SOCKET_ID_ANY,
1560 rte_cryptodev_info_get(cdev_id, &dev_info);
1562 /* Set cipher parameters */
1563 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1564 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1565 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
1566 /* Check if device supports cipher algo */
1568 opt_cipher_algo = options->cipher_xform.cipher.algo;
1569 cap = &dev_info.capabilities[i];
1570 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1571 cap_cipher_algo = cap->sym.cipher.algo;
1572 if (cap->sym.xform_type ==
1573 RTE_CRYPTO_SYM_XFORM_CIPHER) {
1574 if (cap_cipher_algo == opt_cipher_algo) {
1575 if (check_type(options, &dev_info) == 0)
1579 cap = &dev_info.capabilities[++i];
1582 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1583 printf("Algorithm %s not supported by cryptodev %u"
1584 " or device not of preferred type (%s)\n",
1585 supported_cipher_algo[opt_cipher_algo],
1587 options->string_type);
1591 options->block_size = cap->sym.cipher.block_size;
1593 * Check if length of provided IV is supported
1594 * by the algorithm chosen.
1596 if (options->iv_param) {
1597 if (check_supported_size(options->iv.length,
1598 cap->sym.cipher.iv_size.min,
1599 cap->sym.cipher.iv_size.max,
1600 cap->sym.cipher.iv_size.increment)
1602 printf("Unsupported IV length\n");
1606 * Check if length of IV to be randomly generated
1607 * is supported by the algorithm chosen.
1609 } else if (options->iv_random_size != -1) {
1610 if (check_supported_size(options->iv_random_size,
1611 cap->sym.cipher.iv_size.min,
1612 cap->sym.cipher.iv_size.max,
1613 cap->sym.cipher.iv_size.increment)
1615 printf("Unsupported IV length\n");
1618 options->iv.length = options->iv_random_size;
1619 /* No size provided, use minimum size. */
1621 options->iv.length = cap->sym.cipher.iv_size.min;
1624 * Check if length of provided cipher key is supported
1625 * by the algorithm chosen.
1627 if (options->ckey_param) {
1628 if (check_supported_size(
1629 options->cipher_xform.cipher.key.length,
1630 cap->sym.cipher.key_size.min,
1631 cap->sym.cipher.key_size.max,
1632 cap->sym.cipher.key_size.increment)
1634 printf("Unsupported cipher key length\n");
1638 * Check if length of the cipher key to be randomly generated
1639 * is supported by the algorithm chosen.
1641 } else if (options->ckey_random_size != -1) {
1642 if (check_supported_size(options->ckey_random_size,
1643 cap->sym.cipher.key_size.min,
1644 cap->sym.cipher.key_size.max,
1645 cap->sym.cipher.key_size.increment)
1647 printf("Unsupported cipher key length\n");
1650 options->cipher_xform.cipher.key.length =
1651 options->ckey_random_size;
1652 /* No size provided, use minimum size. */
1654 options->cipher_xform.cipher.key.length =
1655 cap->sym.cipher.key_size.min;
1657 if (!options->ckey_param)
1658 generate_random_key(
1659 options->cipher_xform.cipher.key.data,
1660 options->cipher_xform.cipher.key.length);
1664 /* Set auth parameters */
1665 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1666 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1667 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
1668 /* Check if device supports auth algo */
1670 opt_auth_algo = options->auth_xform.auth.algo;
1671 cap = &dev_info.capabilities[i];
1672 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1673 cap_auth_algo = cap->sym.auth.algo;
1674 if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
1675 (cap_auth_algo == opt_auth_algo) &&
1676 (check_type(options, &dev_info) == 0)) {
1679 cap = &dev_info.capabilities[++i];
1682 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1683 printf("Algorithm %s not supported by cryptodev %u"
1684 " or device not of preferred type (%s)\n",
1685 supported_auth_algo[opt_auth_algo],
1687 options->string_type);
1691 options->block_size = cap->sym.auth.block_size;
1693 * Check if length of provided AAD is supported
1694 * by the algorithm chosen.
1696 if (options->aad_param) {
1697 if (check_supported_size(options->aad.length,
1698 cap->sym.auth.aad_size.min,
1699 cap->sym.auth.aad_size.max,
1700 cap->sym.auth.aad_size.increment)
1702 printf("Unsupported AAD length\n");
1706 * Check if length of AAD to be randomly generated
1707 * is supported by the algorithm chosen.
1709 } else if (options->aad_random_size != -1) {
1710 if (check_supported_size(options->aad_random_size,
1711 cap->sym.auth.aad_size.min,
1712 cap->sym.auth.aad_size.max,
1713 cap->sym.auth.aad_size.increment)
1715 printf("Unsupported AAD length\n");
1718 options->aad.length = options->aad_random_size;
1719 /* No size provided, use minimum size. */
1721 options->aad.length = cap->sym.auth.aad_size.min;
1723 options->auth_xform.auth.add_auth_data_length =
1724 options->aad.length;
1727 * Check if length of provided auth key is supported
1728 * by the algorithm chosen.
1730 if (options->akey_param) {
1731 if (check_supported_size(
1732 options->auth_xform.auth.key.length,
1733 cap->sym.auth.key_size.min,
1734 cap->sym.auth.key_size.max,
1735 cap->sym.auth.key_size.increment)
1737 printf("Unsupported auth key length\n");
1741 * Check if length of the auth key to be randomly generated
1742 * is supported by the algorithm chosen.
1744 } else if (options->akey_random_size != -1) {
1745 if (check_supported_size(options->akey_random_size,
1746 cap->sym.auth.key_size.min,
1747 cap->sym.auth.key_size.max,
1748 cap->sym.auth.key_size.increment)
1750 printf("Unsupported auth key length\n");
1753 options->auth_xform.auth.key.length =
1754 options->akey_random_size;
1755 /* No size provided, use minimum size. */
1757 options->auth_xform.auth.key.length =
1758 cap->sym.auth.key_size.min;
1760 if (!options->akey_param)
1761 generate_random_key(
1762 options->auth_xform.auth.key.data,
1763 options->auth_xform.auth.key.length);
1765 /* Check if digest size is supported by the algorithm. */
1766 if (options->digest_size != -1) {
1767 if (check_supported_size(options->digest_size,
1768 cap->sym.auth.digest_size.min,
1769 cap->sym.auth.digest_size.max,
1770 cap->sym.auth.digest_size.increment)
1772 printf("Unsupported digest length\n");
1775 options->auth_xform.auth.digest_length =
1776 options->digest_size;
1777 /* No size provided, use minimum size. */
1779 options->auth_xform.auth.digest_length =
1780 cap->sym.auth.digest_size.min;
1783 retval = rte_cryptodev_configure(cdev_id, &conf);
1785 printf("Failed to configure cryptodev %u", cdev_id);
1789 qp_conf.nb_descriptors = 2048;
1791 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1794 printf("Failed to setup queue pair %u on cryptodev %u",
1799 retval = rte_cryptodev_start(cdev_id);
1801 printf("Failed to start device %u: error %d\n",
1806 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1808 enabled_cdevs[cdev_id] = 1;
1809 enabled_cdev_count++;
1812 return enabled_cdev_count;
1816 initialize_ports(struct l2fwd_crypto_options *options)
1818 uint8_t last_portid, portid;
1819 unsigned enabled_portcount = 0;
1820 unsigned nb_ports = rte_eth_dev_count();
1822 if (nb_ports == 0) {
1823 printf("No Ethernet ports - bye\n");
1827 /* Reset l2fwd_dst_ports */
1828 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1829 l2fwd_dst_ports[portid] = 0;
1831 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1834 /* Skip ports that are not enabled */
1835 if ((options->portmask & (1 << portid)) == 0)
1839 printf("Initializing port %u... ", (unsigned) portid);
1841 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1843 printf("Cannot configure device: err=%d, port=%u\n",
1844 retval, (unsigned) portid);
1848 /* init one RX queue */
1850 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1851 rte_eth_dev_socket_id(portid),
1852 NULL, l2fwd_pktmbuf_pool);
1854 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1855 retval, (unsigned) portid);
1859 /* init one TX queue on each port */
1861 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1862 rte_eth_dev_socket_id(portid),
1865 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1866 retval, (unsigned) portid);
1872 retval = rte_eth_dev_start(portid);
1874 printf("rte_eth_dev_start:err=%d, port=%u\n",
1875 retval, (unsigned) portid);
1879 rte_eth_promiscuous_enable(portid);
1881 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1883 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1885 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1886 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1887 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1888 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1889 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1890 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1892 /* initialize port stats */
1893 memset(&port_statistics, 0, sizeof(port_statistics));
1895 /* Setup port forwarding table */
1896 if (enabled_portcount % 2) {
1897 l2fwd_dst_ports[portid] = last_portid;
1898 l2fwd_dst_ports[last_portid] = portid;
1900 last_portid = portid;
1903 l2fwd_enabled_port_mask |= (1 << portid);
1904 enabled_portcount++;
1907 if (enabled_portcount == 1) {
1908 l2fwd_dst_ports[last_portid] = last_portid;
1909 } else if (enabled_portcount % 2) {
1910 printf("odd number of ports in portmask- bye\n");
1914 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1916 return enabled_portcount;
1920 reserve_key_memory(struct l2fwd_crypto_options *options)
1922 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1924 if (options->cipher_xform.cipher.key.data == NULL)
1925 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1928 options->auth_xform.auth.key.data = rte_malloc("auth key",
1930 if (options->auth_xform.auth.key.data == NULL)
1931 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1933 options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1934 if (options->iv.data == NULL)
1935 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1936 options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1938 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1939 if (options->aad.data == NULL)
1940 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1941 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1945 main(int argc, char **argv)
1947 struct lcore_queue_conf *qconf;
1948 struct l2fwd_crypto_options options;
1950 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1951 unsigned lcore_id, rx_lcore_id;
1952 int ret, enabled_cdevcount, enabled_portcount;
1953 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
1956 ret = rte_eal_init(argc, argv);
1958 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1962 /* reserve memory for Cipher/Auth key and IV */
1963 reserve_key_memory(&options);
1965 /* fill out the supported algorithm tables */
1966 fill_supported_algorithm_tables();
1968 /* parse application arguments (after the EAL ones) */
1969 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1971 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1973 /* create the mbuf pool */
1974 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1975 sizeof(struct rte_crypto_op),
1976 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1977 if (l2fwd_pktmbuf_pool == NULL)
1978 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1980 /* create crypto op pool */
1981 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1982 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1984 if (l2fwd_crypto_op_pool == NULL)
1985 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1987 /* Enable Ethernet ports */
1988 enabled_portcount = initialize_ports(&options);
1989 if (enabled_portcount < 1)
1990 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1992 nb_ports = rte_eth_dev_count();
1993 /* Initialize the port/queue configuration of each logical core */
1994 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1995 portid < nb_ports; portid++) {
1997 /* skip ports that are not enabled */
1998 if ((options.portmask & (1 << portid)) == 0)
2001 if (options.single_lcore && qconf == NULL) {
2002 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2004 if (rx_lcore_id >= RTE_MAX_LCORE)
2005 rte_exit(EXIT_FAILURE,
2006 "Not enough cores\n");
2008 } else if (!options.single_lcore) {
2009 /* get the lcore_id for this port */
2010 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2011 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2012 options.nb_ports_per_lcore) {
2014 if (rx_lcore_id >= RTE_MAX_LCORE)
2015 rte_exit(EXIT_FAILURE,
2016 "Not enough cores\n");
2020 /* Assigned a new logical core in the loop above. */
2021 if (qconf != &lcore_queue_conf[rx_lcore_id])
2022 qconf = &lcore_queue_conf[rx_lcore_id];
2024 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2025 qconf->nb_rx_ports++;
2027 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
2030 /* Enable Crypto devices */
2031 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2033 if (enabled_cdevcount < 0)
2034 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2036 if (enabled_cdevcount < enabled_portcount)
2037 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2038 "has to be more or equal to number of ports (%d)\n",
2039 enabled_cdevcount, enabled_portcount);
2041 nb_cryptodevs = rte_cryptodev_count();
2043 /* Initialize the port/cryptodev configuration of each logical core */
2044 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2045 cdev_id < nb_cryptodevs && enabled_cdevcount;
2047 /* Crypto op not supported by crypto device */
2048 if (!enabled_cdevs[cdev_id])
2051 if (options.single_lcore && qconf == NULL) {
2052 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2054 if (rx_lcore_id >= RTE_MAX_LCORE)
2055 rte_exit(EXIT_FAILURE,
2056 "Not enough cores\n");
2058 } else if (!options.single_lcore) {
2059 /* get the lcore_id for this port */
2060 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2061 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2062 options.nb_ports_per_lcore) {
2064 if (rx_lcore_id >= RTE_MAX_LCORE)
2065 rte_exit(EXIT_FAILURE,
2066 "Not enough cores\n");
2070 /* Assigned a new logical core in the loop above. */
2071 if (qconf != &lcore_queue_conf[rx_lcore_id])
2072 qconf = &lcore_queue_conf[rx_lcore_id];
2074 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2075 qconf->nb_crypto_devs++;
2077 enabled_cdevcount--;
2079 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2083 /* launch per-lcore init on every lcore */
2084 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2086 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2087 if (rte_eal_wait_lcore(lcore_id) < 0)