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>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_common.h>
52 #include <rte_cryptodev.h>
53 #include <rte_cycles.h>
54 #include <rte_debug.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_interrupts.h>
60 #include <rte_launch.h>
61 #include <rte_lcore.h>
63 #include <rte_malloc.h>
65 #include <rte_memcpy.h>
66 #include <rte_memory.h>
67 #include <rte_mempool.h>
68 #include <rte_memzone.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
81 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
85 #define MAX_STR_LEN 32
86 #define MAX_KEY_SIZE 128
87 #define MAX_PKT_BURST 32
88 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91 * Configurable number of RX/TX ring descriptors
93 #define RTE_TEST_RX_DESC_DEFAULT 128
94 #define RTE_TEST_TX_DESC_DEFAULT 512
96 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
97 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
99 /* ethernet addresses of ports */
100 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
102 /* mask of enabled ports */
103 static uint64_t l2fwd_enabled_port_mask;
104 static uint64_t l2fwd_enabled_crypto_mask;
106 /* list of enabled ports */
107 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
112 struct rte_mbuf *buffer[MAX_PKT_BURST];
117 struct rte_crypto_op *buffer[MAX_PKT_BURST];
120 #define MAX_RX_QUEUE_PER_LCORE 16
121 #define MAX_TX_QUEUE_PER_PORT 16
123 enum l2fwd_crypto_xform_chain {
124 L2FWD_CRYPTO_CIPHER_HASH,
125 L2FWD_CRYPTO_HASH_CIPHER,
126 L2FWD_CRYPTO_CIPHER_ONLY,
127 L2FWD_CRYPTO_HASH_ONLY
133 phys_addr_t phys_addr;
136 /** l2fwd crypto application command line options */
137 struct l2fwd_crypto_options {
139 unsigned nb_ports_per_lcore;
140 unsigned refresh_period;
141 unsigned single_lcore:1;
144 unsigned sessionless:1;
146 enum l2fwd_crypto_xform_chain xform_chain;
148 struct rte_crypto_sym_xform cipher_xform;
150 int ckey_random_size;
156 struct rte_crypto_sym_xform auth_xform;
158 int akey_random_size;
160 struct l2fwd_key aad;
167 char string_auth_algo[MAX_STR_LEN];
168 char string_cipher_algo[MAX_STR_LEN];
169 char string_type[MAX_STR_LEN];
172 /** l2fwd crypto lcore params */
173 struct l2fwd_crypto_params {
177 unsigned digest_length;
181 struct l2fwd_key aad;
182 struct rte_cryptodev_sym_session *session;
188 enum rte_crypto_cipher_algorithm cipher_algo;
189 enum rte_crypto_auth_algorithm auth_algo;
192 /** lcore configuration */
193 struct lcore_queue_conf {
194 unsigned nb_rx_ports;
195 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
197 unsigned nb_crypto_devs;
198 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
200 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
201 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
202 } __rte_cache_aligned;
204 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
206 static const struct rte_eth_conf port_conf = {
208 .mq_mode = ETH_MQ_RX_NONE,
209 .max_rx_pkt_len = ETHER_MAX_LEN,
211 .header_split = 0, /**< Header Split disabled */
212 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
213 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
214 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
215 .hw_strip_crc = 0, /**< CRC stripped by hardware */
218 .mq_mode = ETH_MQ_TX_NONE,
222 struct rte_mempool *l2fwd_pktmbuf_pool;
223 struct rte_mempool *l2fwd_crypto_op_pool;
225 /* Per-port statistics struct */
226 struct l2fwd_port_statistics {
230 uint64_t crypto_enqueued;
231 uint64_t crypto_dequeued;
234 } __rte_cache_aligned;
236 struct l2fwd_crypto_statistics {
241 } __rte_cache_aligned;
243 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
244 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
246 /* A tsc-based timer responsible for triggering statistics printout */
247 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
248 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
250 /* default period is 10 seconds */
251 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
253 /* Print out statistics on packets dropped */
257 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
258 uint64_t total_packets_enqueued, total_packets_dequeued,
259 total_packets_errors;
263 total_packets_dropped = 0;
264 total_packets_tx = 0;
265 total_packets_rx = 0;
266 total_packets_enqueued = 0;
267 total_packets_dequeued = 0;
268 total_packets_errors = 0;
270 const char clr[] = { 27, '[', '2', 'J', '\0' };
271 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
273 /* Clear screen and move to top left */
274 printf("%s%s", clr, topLeft);
276 printf("\nPort statistics ====================================");
278 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
279 /* skip disabled ports */
280 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
282 printf("\nStatistics for port %u ------------------------------"
283 "\nPackets sent: %32"PRIu64
284 "\nPackets received: %28"PRIu64
285 "\nPackets dropped: %29"PRIu64,
287 port_statistics[portid].tx,
288 port_statistics[portid].rx,
289 port_statistics[portid].dropped);
291 total_packets_dropped += port_statistics[portid].dropped;
292 total_packets_tx += port_statistics[portid].tx;
293 total_packets_rx += port_statistics[portid].rx;
295 printf("\nCrypto statistics ==================================");
297 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
298 /* skip disabled ports */
299 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
301 printf("\nStatistics for cryptodev %"PRIu64
302 " -------------------------"
303 "\nPackets enqueued: %28"PRIu64
304 "\nPackets dequeued: %28"PRIu64
305 "\nPackets errors: %30"PRIu64,
307 crypto_statistics[cdevid].enqueued,
308 crypto_statistics[cdevid].dequeued,
309 crypto_statistics[cdevid].errors);
311 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
312 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
313 total_packets_errors += crypto_statistics[cdevid].errors;
315 printf("\nAggregate statistics ==============================="
316 "\nTotal packets received: %22"PRIu64
317 "\nTotal packets enqueued: %22"PRIu64
318 "\nTotal packets dequeued: %22"PRIu64
319 "\nTotal packets sent: %26"PRIu64
320 "\nTotal packets dropped: %23"PRIu64
321 "\nTotal packets crypto errors: %17"PRIu64,
323 total_packets_enqueued,
324 total_packets_dequeued,
326 total_packets_dropped,
327 total_packets_errors);
328 printf("\n====================================================\n");
334 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
335 struct l2fwd_crypto_params *cparams)
337 struct rte_crypto_op **op_buffer;
340 op_buffer = (struct rte_crypto_op **)
341 qconf->op_buf[cparams->dev_id].buffer;
343 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
344 cparams->qp_id, op_buffer, (uint16_t) n);
346 crypto_statistics[cparams->dev_id].enqueued += ret;
347 if (unlikely(ret < n)) {
348 crypto_statistics[cparams->dev_id].errors += (n - ret);
350 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
351 rte_crypto_op_free(op_buffer[ret]);
359 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
360 struct l2fwd_crypto_params *cparams)
362 unsigned lcore_id, len;
363 struct lcore_queue_conf *qconf;
365 lcore_id = rte_lcore_id();
367 qconf = &lcore_queue_conf[lcore_id];
368 len = qconf->op_buf[cparams->dev_id].len;
369 qconf->op_buf[cparams->dev_id].buffer[len] = op;
372 /* enough ops to be sent */
373 if (len == MAX_PKT_BURST) {
374 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
378 qconf->op_buf[cparams->dev_id].len = len;
383 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
384 struct rte_crypto_op *op,
385 struct l2fwd_crypto_params *cparams)
387 struct ether_hdr *eth_hdr;
388 struct ipv4_hdr *ip_hdr;
390 unsigned ipdata_offset, pad_len, data_len;
393 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
395 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
398 ipdata_offset = sizeof(struct ether_hdr);
400 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
403 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
404 * IPV4_IHL_MULTIPLIER;
407 /* Zero pad data to be crypto'd so it is block aligned */
408 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
409 pad_len = data_len % cparams->block_size ? cparams->block_size -
410 (data_len % cparams->block_size) : 0;
413 padding = rte_pktmbuf_append(m, pad_len);
414 if (unlikely(!padding))
418 memset(padding, 0, pad_len);
421 /* Set crypto operation data parameters */
422 rte_crypto_op_attach_sym_session(op, cparams->session);
424 if (cparams->do_hash) {
425 if (!cparams->hash_verify) {
426 /* Append space for digest to end of packet */
427 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
428 cparams->digest_length);
430 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
431 cparams->digest_length);
434 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
435 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
436 op->sym->auth.digest.length = cparams->digest_length;
438 /* For SNOW3G algorithms, offset/length must be in bits */
439 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
440 op->sym->auth.data.offset = ipdata_offset << 3;
441 op->sym->auth.data.length = data_len << 3;
443 op->sym->auth.data.offset = ipdata_offset;
444 op->sym->auth.data.length = data_len;
447 if (cparams->aad.length) {
448 op->sym->auth.aad.data = cparams->aad.data;
449 op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
450 op->sym->auth.aad.length = cparams->aad.length;
454 if (cparams->do_cipher) {
455 op->sym->cipher.iv.data = cparams->iv.data;
456 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
457 op->sym->cipher.iv.length = cparams->iv.length;
459 /* For SNOW3G algorithms, offset/length must be in bits */
460 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2) {
461 op->sym->cipher.data.offset = ipdata_offset << 3;
462 if (cparams->do_hash && cparams->hash_verify)
463 /* Do not cipher the hash tag */
464 op->sym->cipher.data.length = (data_len -
465 cparams->digest_length) << 3;
467 op->sym->cipher.data.length = data_len << 3;
470 op->sym->cipher.data.offset = ipdata_offset;
471 if (cparams->do_hash && cparams->hash_verify)
472 /* Do not cipher the hash tag */
473 op->sym->cipher.data.length = data_len -
474 cparams->digest_length;
476 op->sym->cipher.data.length = data_len;
482 return l2fwd_crypto_enqueue(op, cparams);
486 /* Send the burst of packets on an output interface */
488 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
491 struct rte_mbuf **pkt_buffer;
494 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
496 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
497 port_statistics[port].tx += ret;
498 if (unlikely(ret < n)) {
499 port_statistics[port].dropped += (n - ret);
501 rte_pktmbuf_free(pkt_buffer[ret]);
508 /* Enqueue packets for TX and prepare them to be sent */
510 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
512 unsigned lcore_id, len;
513 struct lcore_queue_conf *qconf;
515 lcore_id = rte_lcore_id();
517 qconf = &lcore_queue_conf[lcore_id];
518 len = qconf->pkt_buf[port].len;
519 qconf->pkt_buf[port].buffer[len] = m;
522 /* enough pkts to be sent */
523 if (unlikely(len == MAX_PKT_BURST)) {
524 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
528 qconf->pkt_buf[port].len = len;
533 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
535 struct ether_hdr *eth;
539 dst_port = l2fwd_dst_ports[portid];
540 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
542 /* 02:00:00:00:00:xx */
543 tmp = ð->d_addr.addr_bytes[0];
544 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
547 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
549 l2fwd_send_packet(m, (uint8_t) dst_port);
552 /** Generate random key */
554 generate_random_key(uint8_t *key, unsigned length)
558 for (i = 0; i < length; i++)
559 key[i] = rand() % 0xff;
562 static struct rte_cryptodev_sym_session *
563 initialize_crypto_session(struct l2fwd_crypto_options *options,
566 struct rte_crypto_sym_xform *first_xform;
568 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
569 first_xform = &options->cipher_xform;
570 first_xform->next = &options->auth_xform;
571 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
572 first_xform = &options->auth_xform;
573 first_xform->next = &options->cipher_xform;
574 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
575 first_xform = &options->cipher_xform;
577 first_xform = &options->auth_xform;
580 /* Setup Cipher Parameters */
581 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
585 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
587 /* main processing loop */
589 l2fwd_main_loop(struct l2fwd_crypto_options *options)
591 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
592 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
594 unsigned lcore_id = rte_lcore_id();
595 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
596 unsigned i, j, portid, nb_rx;
597 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
598 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
599 US_PER_S * BURST_TX_DRAIN_US;
600 struct l2fwd_crypto_params *cparams;
601 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
603 if (qconf->nb_rx_ports == 0) {
604 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
608 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
610 l2fwd_crypto_options_print(options);
612 for (i = 0; i < qconf->nb_rx_ports; i++) {
614 portid = qconf->rx_port_list[i];
615 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
619 for (i = 0; i < qconf->nb_crypto_devs; i++) {
620 port_cparams[i].do_cipher = 0;
621 port_cparams[i].do_hash = 0;
623 switch (options->xform_chain) {
624 case L2FWD_CRYPTO_CIPHER_HASH:
625 case L2FWD_CRYPTO_HASH_CIPHER:
626 port_cparams[i].do_cipher = 1;
627 port_cparams[i].do_hash = 1;
629 case L2FWD_CRYPTO_HASH_ONLY:
630 port_cparams[i].do_hash = 1;
632 case L2FWD_CRYPTO_CIPHER_ONLY:
633 port_cparams[i].do_cipher = 1;
637 port_cparams[i].dev_id = qconf->cryptodev_list[i];
638 port_cparams[i].qp_id = 0;
640 port_cparams[i].block_size = options->block_size;
642 if (port_cparams[i].do_hash) {
643 port_cparams[i].digest_length =
644 options->auth_xform.auth.digest_length;
645 if (options->auth_xform.auth.add_auth_data_length) {
646 port_cparams[i].aad.data = options->aad.data;
647 port_cparams[i].aad.length =
648 options->auth_xform.auth.add_auth_data_length;
649 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
650 if (!options->aad_param)
651 generate_random_key(port_cparams[i].aad.data,
652 port_cparams[i].aad.length);
656 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
657 port_cparams[i].hash_verify = 1;
659 port_cparams[i].hash_verify = 0;
661 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
664 if (port_cparams[i].do_cipher) {
665 port_cparams[i].iv.data = options->iv.data;
666 port_cparams[i].iv.length = options->iv.length;
667 port_cparams[i].iv.phys_addr = options->iv.phys_addr;
668 if (!options->iv_param)
669 generate_random_key(port_cparams[i].iv.data,
670 port_cparams[i].iv.length);
672 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
675 port_cparams[i].session = initialize_crypto_session(options,
676 port_cparams[i].dev_id);
678 if (port_cparams[i].session == NULL)
680 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
681 port_cparams[i].dev_id);
686 cur_tsc = rte_rdtsc();
689 * TX burst queue drain
691 diff_tsc = cur_tsc - prev_tsc;
692 if (unlikely(diff_tsc > drain_tsc)) {
693 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
694 if (qconf->pkt_buf[portid].len == 0)
696 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
697 qconf->pkt_buf[portid].len,
699 qconf->pkt_buf[portid].len = 0;
702 /* if timer is enabled */
703 if (timer_period > 0) {
705 /* advance the timer */
706 timer_tsc += diff_tsc;
708 /* if timer has reached its timeout */
709 if (unlikely(timer_tsc >=
710 (uint64_t)timer_period)) {
712 /* do this only on master core */
713 if (lcore_id == rte_get_master_lcore()
714 && options->refresh_period) {
725 * Read packet from RX queues
727 for (i = 0; i < qconf->nb_rx_ports; i++) {
728 portid = qconf->rx_port_list[i];
730 cparams = &port_cparams[i];
732 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
733 pkts_burst, MAX_PKT_BURST);
735 port_statistics[portid].rx += nb_rx;
739 * If we can't allocate a crypto_ops, then drop
740 * the rest of the burst and dequeue and
741 * process the packets to free offload structs
743 if (rte_crypto_op_bulk_alloc(
744 l2fwd_crypto_op_pool,
745 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
748 for (j = 0; j < nb_rx; j++)
749 rte_pktmbuf_free(pkts_burst[i]);
754 /* Enqueue packets from Crypto device*/
755 for (j = 0; j < nb_rx; j++) {
758 l2fwd_simple_crypto_enqueue(m,
759 ops_burst[j], cparams);
763 /* Dequeue packets from Crypto device */
765 nb_rx = rte_cryptodev_dequeue_burst(
766 cparams->dev_id, cparams->qp_id,
767 ops_burst, MAX_PKT_BURST);
769 crypto_statistics[cparams->dev_id].dequeued +=
772 /* Forward crypto'd packets */
773 for (j = 0; j < nb_rx; j++) {
774 m = ops_burst[j]->sym->m_src;
776 rte_crypto_op_free(ops_burst[j]);
777 l2fwd_simple_forward(m, portid);
779 } while (nb_rx == MAX_PKT_BURST);
785 l2fwd_launch_one_lcore(void *arg)
787 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
791 /* Display command line arguments usage */
793 l2fwd_crypto_usage(const char *prgname)
795 printf("%s [EAL options] --\n"
796 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
797 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
798 " -s manage all ports from single lcore\n"
799 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
800 " (0 to disable, 10 default, 86400 maximum)\n"
802 " --cdev_type HW / SW / ANY\n"
803 " --chain HASH_CIPHER / CIPHER_HASH\n"
805 " --cipher_algo ALGO\n"
806 " --cipher_op ENCRYPT / DECRYPT\n"
807 " --cipher_key KEY\n"
808 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
810 " --iv_random_size SIZE: size of IV when generated randomly\n"
812 " --auth_algo ALGO\n"
813 " --auth_op GENERATE / VERIFY\n"
815 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
817 " --aad_random_size SIZE: size of AAD when generated randomly\n"
818 " --digest_size SIZE: size of digest to be generated/verified\n"
824 /** Parse crypto device type command line argument */
826 parse_cryptodev_type(enum cdev_type *type, char *optarg)
828 if (strcmp("HW", optarg) == 0) {
829 *type = CDEV_TYPE_HW;
831 } else if (strcmp("SW", optarg) == 0) {
832 *type = CDEV_TYPE_SW;
834 } else if (strcmp("ANY", optarg) == 0) {
835 *type = CDEV_TYPE_ANY;
842 /** Parse crypto chain xform command line argument */
844 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
846 if (strcmp("CIPHER_HASH", optarg) == 0) {
847 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
849 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
850 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
852 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
853 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
855 } else if (strcmp("HASH_ONLY", optarg) == 0) {
856 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
863 /** Parse crypto cipher algo option command line argument */
865 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
867 if (strcmp("AES_CBC", optarg) == 0) {
868 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
870 } else if (strcmp("AES_GCM", optarg) == 0) {
871 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
873 } else if (strcmp("NULL", optarg) == 0) {
874 *algo = RTE_CRYPTO_CIPHER_NULL;
876 } else if (strcmp("SNOW3G_UEA2", optarg) == 0) {
877 *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
881 printf("Cipher algorithm not supported!\n");
885 /** Parse crypto cipher operation command line argument */
887 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
889 if (strcmp("ENCRYPT", optarg) == 0) {
890 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
892 } else if (strcmp("DECRYPT", optarg) == 0) {
893 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
897 printf("Cipher operation not supported!\n");
901 /** Parse crypto key command line argument */
903 parse_key(uint8_t *data, char *input_arg)
908 for (byte_count = 0, token = strtok(input_arg, ":");
909 (byte_count < MAX_KEY_SIZE) && (token != NULL);
910 token = strtok(NULL, ":")) {
912 int number = (int)strtol(token, NULL, 16);
914 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
917 data[byte_count++] = (uint8_t)number;
923 /** Parse size param*/
925 parse_size(int *size, const char *q_arg)
930 /* parse hexadecimal string */
931 n = strtoul(q_arg, &end, 10);
932 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
936 printf("invalid size\n");
944 /** Parse crypto cipher operation command line argument */
946 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
948 if (strcmp("AES_GCM", optarg) == 0) {
949 *algo = RTE_CRYPTO_AUTH_AES_GCM;
951 } else if (strcmp("MD5_HMAC", optarg) == 0) {
952 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
954 } else if (strcmp("NULL", optarg) == 0) {
955 *algo = RTE_CRYPTO_AUTH_NULL;
957 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
958 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
960 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
961 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
963 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
964 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
966 } else if (strcmp("SHA384_HMAC", optarg) == 0) {
967 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
969 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
970 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
972 } else if (strcmp("SNOW3G_UIA2", optarg) == 0) {
973 *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
977 printf("Authentication algorithm specified not supported!\n");
982 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
984 if (strcmp("VERIFY", optarg) == 0) {
985 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
987 } else if (strcmp("GENERATE", optarg) == 0) {
988 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
992 printf("Authentication operation specified not supported!\n");
996 /** Parse long options */
998 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
999 struct option *lgopts, int option_index)
1003 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1004 retval = parse_cryptodev_type(&options->type, optarg);
1006 strcpy(options->string_type, optarg);
1010 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1011 return parse_crypto_opt_chain(options, optarg);
1013 /* Cipher options */
1014 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0) {
1015 retval = parse_cipher_algo(&options->cipher_xform.cipher.algo,
1018 strcpy(options->string_cipher_algo, optarg);
1022 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1023 return parse_cipher_op(&options->cipher_xform.cipher.op,
1026 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1027 options->ckey_param = 1;
1028 options->cipher_xform.cipher.key.length =
1029 parse_key(options->cipher_xform.cipher.key.data, optarg);
1030 if (options->cipher_xform.cipher.key.length > 0)
1036 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1037 return parse_size(&options->ckey_random_size, optarg);
1039 else if (strcmp(lgopts[option_index].name, "iv") == 0) {
1040 options->iv_param = 1;
1041 options->iv.length =
1042 parse_key(options->iv.data, optarg);
1043 if (options->iv.length > 0)
1049 else if (strcmp(lgopts[option_index].name, "iv_random_size") == 0)
1050 return parse_size(&options->iv_random_size, optarg);
1052 /* Authentication options */
1053 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1054 retval = parse_auth_algo(&options->auth_xform.auth.algo,
1057 strcpy(options->string_auth_algo, optarg);
1061 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1062 return parse_auth_op(&options->auth_xform.auth.op,
1065 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1066 options->akey_param = 1;
1067 options->auth_xform.auth.key.length =
1068 parse_key(options->auth_xform.auth.key.data, optarg);
1069 if (options->auth_xform.auth.key.length > 0)
1075 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1076 return parse_size(&options->akey_random_size, optarg);
1079 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1080 options->aad_param = 1;
1081 options->aad.length =
1082 parse_key(options->aad.data, optarg);
1083 if (options->aad.length > 0)
1089 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1090 return parse_size(&options->aad_random_size, optarg);
1093 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1094 return parse_size(&options->digest_size, optarg);
1097 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1098 options->sessionless = 1;
1105 /** Parse port mask */
1107 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1113 /* parse hexadecimal string */
1114 pm = strtoul(q_arg, &end, 16);
1115 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1118 options->portmask = pm;
1119 if (options->portmask == 0) {
1120 printf("invalid portmask specified\n");
1127 /** Parse number of queues */
1129 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1135 /* parse hexadecimal string */
1136 n = strtoul(q_arg, &end, 10);
1137 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1139 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1142 options->nb_ports_per_lcore = n;
1143 if (options->nb_ports_per_lcore == 0) {
1144 printf("invalid number of ports selected\n");
1151 /** Parse timer period */
1153 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1159 /* parse number string */
1160 n = (unsigned)strtol(q_arg, &end, 10);
1161 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1164 if (n >= MAX_TIMER_PERIOD) {
1165 printf("Warning refresh period specified %lu is greater than "
1166 "max value %lu! using max value",
1167 n, MAX_TIMER_PERIOD);
1168 n = MAX_TIMER_PERIOD;
1171 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1176 /** Generate default options for application */
1178 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1182 options->portmask = 0xffffffff;
1183 options->nb_ports_per_lcore = 1;
1184 options->refresh_period = 10000;
1185 options->single_lcore = 0;
1186 options->sessionless = 0;
1188 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1191 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1192 options->cipher_xform.next = NULL;
1193 options->ckey_param = 0;
1194 options->ckey_random_size = -1;
1195 options->cipher_xform.cipher.key.length = 0;
1196 options->iv_param = 0;
1197 options->iv_random_size = -1;
1198 options->iv.length = 0;
1200 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1201 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1203 /* Authentication Data */
1204 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1205 options->auth_xform.next = NULL;
1206 options->akey_param = 0;
1207 options->akey_random_size = -1;
1208 options->auth_xform.auth.key.length = 0;
1209 options->aad_param = 0;
1210 options->aad_random_size = -1;
1211 options->aad.length = 0;
1212 options->digest_size = -1;
1214 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1215 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1217 options->type = CDEV_TYPE_ANY;
1221 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1223 printf("Options:-\nn");
1224 printf("portmask: %x\n", options->portmask);
1225 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1226 printf("refresh period : %u\n", options->refresh_period);
1227 printf("single lcore mode: %s\n",
1228 options->single_lcore ? "enabled" : "disabled");
1229 printf("stats_printing: %s\n",
1230 options->refresh_period == 0 ? "disabled" : "enabled");
1232 printf("sessionless crypto: %s\n",
1233 options->sessionless ? "enabled" : "disabled");
1236 /* Parse the argument given in the command line of the application */
1238 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1239 int argc, char **argv)
1241 int opt, retval, option_index;
1242 char **argvopt = argv, *prgname = argv[0];
1244 static struct option lgopts[] = {
1245 { "sessionless", no_argument, 0, 0 },
1247 { "cdev_type", required_argument, 0, 0 },
1248 { "chain", required_argument, 0, 0 },
1250 { "cipher_algo", required_argument, 0, 0 },
1251 { "cipher_op", required_argument, 0, 0 },
1252 { "cipher_key", required_argument, 0, 0 },
1253 { "cipher_key_random_size", required_argument, 0, 0 },
1255 { "auth_algo", required_argument, 0, 0 },
1256 { "auth_op", required_argument, 0, 0 },
1257 { "auth_key", required_argument, 0, 0 },
1258 { "auth_key_random_size", required_argument, 0, 0 },
1260 { "iv", required_argument, 0, 0 },
1261 { "iv_random_size", required_argument, 0, 0 },
1262 { "aad", required_argument, 0, 0 },
1263 { "aad_random_size", required_argument, 0, 0 },
1264 { "digest_size", required_argument, 0, 0 },
1266 { "sessionless", no_argument, 0, 0 },
1271 l2fwd_crypto_default_options(options);
1273 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1274 &option_index)) != EOF) {
1278 retval = l2fwd_crypto_parse_args_long_options(options,
1279 lgopts, option_index);
1281 l2fwd_crypto_usage(prgname);
1288 retval = l2fwd_crypto_parse_portmask(options, optarg);
1290 l2fwd_crypto_usage(prgname);
1297 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1299 l2fwd_crypto_usage(prgname);
1306 options->single_lcore = 1;
1312 retval = l2fwd_crypto_parse_timer_period(options,
1315 l2fwd_crypto_usage(prgname);
1321 l2fwd_crypto_usage(prgname);
1328 argv[optind-1] = prgname;
1331 optind = 0; /* reset getopt lib */
1336 /* Check the link status of all ports in up to 9s, and print them finally */
1338 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1340 #define CHECK_INTERVAL 100 /* 100ms */
1341 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1342 uint8_t portid, count, all_ports_up, print_flag = 0;
1343 struct rte_eth_link link;
1345 printf("\nChecking link status");
1347 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1349 for (portid = 0; portid < port_num; portid++) {
1350 if ((port_mask & (1 << portid)) == 0)
1352 memset(&link, 0, sizeof(link));
1353 rte_eth_link_get_nowait(portid, &link);
1354 /* print link status if flag set */
1355 if (print_flag == 1) {
1356 if (link.link_status)
1357 printf("Port %d Link Up - speed %u "
1358 "Mbps - %s\n", (uint8_t)portid,
1359 (unsigned)link.link_speed,
1360 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1361 ("full-duplex") : ("half-duplex\n"));
1363 printf("Port %d Link Down\n",
1367 /* clear all_ports_up flag if any link down */
1368 if (link.link_status == 0) {
1373 /* after finally printing all link status, get out */
1374 if (print_flag == 1)
1377 if (all_ports_up == 0) {
1380 rte_delay_ms(CHECK_INTERVAL);
1383 /* set the print_flag if all ports up or timeout */
1384 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1391 /* Check if device has to be HW/SW or any */
1393 check_type(struct l2fwd_crypto_options *options, struct rte_cryptodev_info *dev_info)
1395 if (options->type == CDEV_TYPE_HW &&
1396 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1398 if (options->type == CDEV_TYPE_SW &&
1399 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1401 if (options->type == CDEV_TYPE_ANY)
1408 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1413 for (supp_size = min; supp_size <= max; supp_size += increment) {
1414 if (length == supp_size)
1421 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1422 uint8_t *enabled_cdevs)
1424 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1425 const struct rte_cryptodev_capabilities *cap;
1426 enum rte_crypto_auth_algorithm cap_auth_algo;
1427 enum rte_crypto_auth_algorithm opt_auth_algo;
1428 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1429 enum rte_crypto_cipher_algorithm opt_cipher_algo;
1432 cdev_count = rte_cryptodev_count();
1433 if (cdev_count == 0) {
1434 printf("No crypto devices available\n");
1438 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1440 struct rte_cryptodev_qp_conf qp_conf;
1441 struct rte_cryptodev_info dev_info;
1443 struct rte_cryptodev_config conf = {
1444 .nb_queue_pairs = 1,
1445 .socket_id = SOCKET_ID_ANY,
1452 rte_cryptodev_info_get(cdev_id, &dev_info);
1454 /* Set cipher parameters */
1455 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1456 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1457 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
1458 /* Check if device supports cipher algo */
1460 opt_cipher_algo = options->cipher_xform.cipher.algo;
1461 cap = &dev_info.capabilities[i];
1462 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1463 cap_cipher_algo = cap->sym.cipher.algo;
1464 if (cap->sym.xform_type ==
1465 RTE_CRYPTO_SYM_XFORM_CIPHER) {
1466 if (cap_cipher_algo == opt_cipher_algo) {
1467 if (check_type(options, &dev_info) == 0)
1471 cap = &dev_info.capabilities[++i];
1474 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1475 printf("Algorithm %s not supported by cryptodev %u"
1476 " or device not of preferred type (%s)\n",
1477 options->string_cipher_algo, cdev_id,
1478 options->string_type);
1482 options->block_size = cap->sym.cipher.block_size;
1484 * Check if length of provided IV is supported
1485 * by the algorithm chosen.
1487 if (options->iv_param) {
1488 if (check_supported_size(options->iv.length,
1489 cap->sym.cipher.iv_size.min,
1490 cap->sym.cipher.iv_size.max,
1491 cap->sym.cipher.iv_size.increment)
1493 printf("Unsupported IV length\n");
1497 * Check if length of IV to be randomly generated
1498 * is supported by the algorithm chosen.
1500 } else if (options->iv_random_size != -1) {
1501 if (check_supported_size(options->iv_random_size,
1502 cap->sym.cipher.iv_size.min,
1503 cap->sym.cipher.iv_size.max,
1504 cap->sym.cipher.iv_size.increment)
1506 printf("Unsupported IV length\n");
1509 options->iv.length = options->iv_random_size;
1510 /* No size provided, use minimum size. */
1512 options->iv.length = cap->sym.cipher.iv_size.min;
1515 * Check if length of provided cipher key is supported
1516 * by the algorithm chosen.
1518 if (options->ckey_param) {
1519 if (check_supported_size(
1520 options->cipher_xform.cipher.key.length,
1521 cap->sym.cipher.key_size.min,
1522 cap->sym.cipher.key_size.max,
1523 cap->sym.cipher.key_size.increment)
1525 printf("Unsupported cipher key length\n");
1529 * Check if length of the cipher key to be randomly generated
1530 * is supported by the algorithm chosen.
1532 } else if (options->ckey_random_size != -1) {
1533 if (check_supported_size(options->ckey_random_size,
1534 cap->sym.cipher.key_size.min,
1535 cap->sym.cipher.key_size.max,
1536 cap->sym.cipher.key_size.increment)
1538 printf("Unsupported cipher key length\n");
1541 options->cipher_xform.cipher.key.length =
1542 options->ckey_random_size;
1543 /* No size provided, use minimum size. */
1545 options->cipher_xform.cipher.key.length =
1546 cap->sym.cipher.key_size.min;
1548 if (!options->ckey_param)
1549 generate_random_key(
1550 options->cipher_xform.cipher.key.data,
1551 options->cipher_xform.cipher.key.length);
1555 /* Set auth parameters */
1556 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1557 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1558 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
1559 /* Check if device supports auth algo */
1561 opt_auth_algo = options->auth_xform.auth.algo;
1562 cap = &dev_info.capabilities[i];
1563 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1564 cap_auth_algo = cap->sym.auth.algo;
1565 if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
1566 (cap_auth_algo == opt_auth_algo) &&
1567 (check_type(options, &dev_info) == 0)) {
1570 cap = &dev_info.capabilities[++i];
1573 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1574 printf("Algorithm %s not supported by cryptodev %u"
1575 " or device not of preferred type (%s)\n",
1576 options->string_auth_algo, cdev_id,
1577 options->string_type);
1581 options->block_size = cap->sym.auth.block_size;
1583 * Check if length of provided AAD is supported
1584 * by the algorithm chosen.
1586 if (options->aad_param) {
1587 if (check_supported_size(options->aad.length,
1588 cap->sym.auth.aad_size.min,
1589 cap->sym.auth.aad_size.max,
1590 cap->sym.auth.aad_size.increment)
1592 printf("Unsupported AAD length\n");
1596 * Check if length of AAD to be randomly generated
1597 * is supported by the algorithm chosen.
1599 } else if (options->aad_random_size != -1) {
1600 if (check_supported_size(options->aad_random_size,
1601 cap->sym.auth.aad_size.min,
1602 cap->sym.auth.aad_size.max,
1603 cap->sym.auth.aad_size.increment)
1605 printf("Unsupported AAD length\n");
1608 options->aad.length = options->aad_random_size;
1609 /* No size provided, use minimum size. */
1611 options->aad.length = cap->sym.auth.aad_size.min;
1613 options->auth_xform.auth.add_auth_data_length =
1614 options->aad.length;
1617 * Check if length of provided auth key is supported
1618 * by the algorithm chosen.
1620 if (options->akey_param) {
1621 if (check_supported_size(
1622 options->auth_xform.auth.key.length,
1623 cap->sym.auth.key_size.min,
1624 cap->sym.auth.key_size.max,
1625 cap->sym.auth.key_size.increment)
1627 printf("Unsupported auth key length\n");
1631 * Check if length of the auth key to be randomly generated
1632 * is supported by the algorithm chosen.
1634 } else if (options->akey_random_size != -1) {
1635 if (check_supported_size(options->akey_random_size,
1636 cap->sym.auth.key_size.min,
1637 cap->sym.auth.key_size.max,
1638 cap->sym.auth.key_size.increment)
1640 printf("Unsupported auth key length\n");
1643 options->auth_xform.auth.key.length =
1644 options->akey_random_size;
1645 /* No size provided, use minimum size. */
1647 options->auth_xform.auth.key.length =
1648 cap->sym.auth.key_size.min;
1650 if (!options->akey_param)
1651 generate_random_key(
1652 options->auth_xform.auth.key.data,
1653 options->auth_xform.auth.key.length);
1655 /* Check if digest size is supported by the algorithm. */
1656 if (options->digest_size != -1) {
1657 if (check_supported_size(options->digest_size,
1658 cap->sym.auth.digest_size.min,
1659 cap->sym.auth.digest_size.max,
1660 cap->sym.auth.digest_size.increment)
1662 printf("Unsupported digest length\n");
1665 options->auth_xform.auth.digest_length =
1666 options->digest_size;
1667 /* No size provided, use minimum size. */
1669 options->auth_xform.auth.digest_length =
1670 cap->sym.auth.digest_size.min;
1673 retval = rte_cryptodev_configure(cdev_id, &conf);
1675 printf("Failed to configure cryptodev %u", cdev_id);
1679 qp_conf.nb_descriptors = 2048;
1681 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1684 printf("Failed to setup queue pair %u on cryptodev %u",
1689 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1691 enabled_cdevs[cdev_id] = 1;
1692 enabled_cdev_count++;
1695 return enabled_cdev_count;
1699 initialize_ports(struct l2fwd_crypto_options *options)
1701 uint8_t last_portid, portid;
1702 unsigned enabled_portcount = 0;
1703 unsigned nb_ports = rte_eth_dev_count();
1705 if (nb_ports == 0) {
1706 printf("No Ethernet ports - bye\n");
1710 if (nb_ports > RTE_MAX_ETHPORTS)
1711 nb_ports = RTE_MAX_ETHPORTS;
1713 /* Reset l2fwd_dst_ports */
1714 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1715 l2fwd_dst_ports[portid] = 0;
1717 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1720 /* Skip ports that are not enabled */
1721 if ((options->portmask & (1 << portid)) == 0)
1725 printf("Initializing port %u... ", (unsigned) portid);
1727 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1729 printf("Cannot configure device: err=%d, port=%u\n",
1730 retval, (unsigned) portid);
1734 /* init one RX queue */
1736 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1737 rte_eth_dev_socket_id(portid),
1738 NULL, l2fwd_pktmbuf_pool);
1740 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1741 retval, (unsigned) portid);
1745 /* init one TX queue on each port */
1747 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1748 rte_eth_dev_socket_id(portid),
1751 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1752 retval, (unsigned) portid);
1758 retval = rte_eth_dev_start(portid);
1760 printf("rte_eth_dev_start:err=%d, port=%u\n",
1761 retval, (unsigned) portid);
1765 rte_eth_promiscuous_enable(portid);
1767 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1769 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1771 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1772 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1773 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1774 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1775 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1776 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1778 /* initialize port stats */
1779 memset(&port_statistics, 0, sizeof(port_statistics));
1781 /* Setup port forwarding table */
1782 if (enabled_portcount % 2) {
1783 l2fwd_dst_ports[portid] = last_portid;
1784 l2fwd_dst_ports[last_portid] = portid;
1786 last_portid = portid;
1789 l2fwd_enabled_port_mask |= (1 << portid);
1790 enabled_portcount++;
1793 if (enabled_portcount == 1) {
1794 l2fwd_dst_ports[last_portid] = last_portid;
1795 } else if (enabled_portcount % 2) {
1796 printf("odd number of ports in portmask- bye\n");
1800 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1802 return enabled_portcount;
1806 reserve_key_memory(struct l2fwd_crypto_options *options)
1808 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1810 if (options->cipher_xform.cipher.key.data == NULL)
1811 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1814 options->auth_xform.auth.key.data = rte_malloc("auth key",
1816 if (options->auth_xform.auth.key.data == NULL)
1817 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1819 options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1820 if (options->iv.data == NULL)
1821 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1822 options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1824 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1825 if (options->aad.data == NULL)
1826 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1827 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1831 main(int argc, char **argv)
1833 struct lcore_queue_conf *qconf;
1834 struct l2fwd_crypto_options options;
1836 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1837 unsigned lcore_id, rx_lcore_id;
1838 int ret, enabled_cdevcount, enabled_portcount;
1839 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
1842 ret = rte_eal_init(argc, argv);
1844 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1848 /* reserve memory for Cipher/Auth key and IV */
1849 reserve_key_memory(&options);
1851 /* parse application arguments (after the EAL ones) */
1852 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1854 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1856 /* create the mbuf pool */
1857 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1858 sizeof(struct rte_crypto_op),
1859 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1860 if (l2fwd_pktmbuf_pool == NULL)
1861 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1863 /* create crypto op pool */
1864 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1865 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1867 if (l2fwd_crypto_op_pool == NULL)
1868 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1870 /* Enable Ethernet ports */
1871 enabled_portcount = initialize_ports(&options);
1872 if (enabled_portcount < 1)
1873 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1875 nb_ports = rte_eth_dev_count();
1876 /* Initialize the port/queue configuration of each logical core */
1877 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1878 portid < nb_ports; portid++) {
1880 /* skip ports that are not enabled */
1881 if ((options.portmask & (1 << portid)) == 0)
1884 if (options.single_lcore && qconf == NULL) {
1885 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1887 if (rx_lcore_id >= RTE_MAX_LCORE)
1888 rte_exit(EXIT_FAILURE,
1889 "Not enough cores\n");
1891 } else if (!options.single_lcore) {
1892 /* get the lcore_id for this port */
1893 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1894 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1895 options.nb_ports_per_lcore) {
1897 if (rx_lcore_id >= RTE_MAX_LCORE)
1898 rte_exit(EXIT_FAILURE,
1899 "Not enough cores\n");
1903 /* Assigned a new logical core in the loop above. */
1904 if (qconf != &lcore_queue_conf[rx_lcore_id])
1905 qconf = &lcore_queue_conf[rx_lcore_id];
1907 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1908 qconf->nb_rx_ports++;
1910 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1913 /* Enable Crypto devices */
1914 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
1916 if (enabled_cdevcount < 0)
1917 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
1919 if (enabled_cdevcount < enabled_portcount)
1920 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
1921 "has to be more or equal to number of ports (%d)\n",
1922 enabled_cdevcount, enabled_portcount);
1924 nb_cryptodevs = rte_cryptodev_count();
1926 /* Initialize the port/cryptodev configuration of each logical core */
1927 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1928 cdev_id < nb_cryptodevs && enabled_cdevcount;
1930 /* Crypto op not supported by crypto device */
1931 if (!enabled_cdevs[cdev_id])
1934 if (options.single_lcore && qconf == NULL) {
1935 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1937 if (rx_lcore_id >= RTE_MAX_LCORE)
1938 rte_exit(EXIT_FAILURE,
1939 "Not enough cores\n");
1941 } else if (!options.single_lcore) {
1942 /* get the lcore_id for this port */
1943 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1944 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1945 options.nb_ports_per_lcore) {
1947 if (rx_lcore_id >= RTE_MAX_LCORE)
1948 rte_exit(EXIT_FAILURE,
1949 "Not enough cores\n");
1953 /* Assigned a new logical core in the loop above. */
1954 if (qconf != &lcore_queue_conf[rx_lcore_id])
1955 qconf = &lcore_queue_conf[rx_lcore_id];
1957 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1958 qconf->nb_crypto_devs++;
1960 enabled_cdevcount--;
1962 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1966 /* launch per-lcore init on every lcore */
1967 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1969 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1970 if (rte_eal_wait_lcore(lcore_id) < 0)