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>
75 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
79 #define MAX_PKT_BURST 32
80 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
83 * Configurable number of RX/TX ring descriptors
85 #define RTE_TEST_RX_DESC_DEFAULT 128
86 #define RTE_TEST_TX_DESC_DEFAULT 512
87 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
88 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
90 /* ethernet addresses of ports */
91 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
93 /* mask of enabled ports */
94 static uint64_t l2fwd_enabled_port_mask;
95 static uint64_t l2fwd_enabled_crypto_mask;
97 /* list of enabled ports */
98 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
103 struct rte_mbuf *buffer[MAX_PKT_BURST];
108 struct rte_crypto_op *buffer[MAX_PKT_BURST];
111 #define MAX_RX_QUEUE_PER_LCORE 16
112 #define MAX_TX_QUEUE_PER_PORT 16
114 enum l2fwd_crypto_xform_chain {
115 L2FWD_CRYPTO_CIPHER_HASH,
116 L2FWD_CRYPTO_HASH_CIPHER
122 phys_addr_t phys_addr;
125 /** l2fwd crypto application command line options */
126 struct l2fwd_crypto_options {
128 unsigned nb_ports_per_lcore;
129 unsigned refresh_period;
130 unsigned single_lcore:1;
132 enum rte_cryptodev_type cdev_type;
133 unsigned sessionless:1;
135 enum l2fwd_crypto_xform_chain xform_chain;
137 struct rte_crypto_sym_xform cipher_xform;
138 uint8_t ckey_data[32];
140 struct l2fwd_key iv_key;
141 uint8_t ivkey_data[16];
143 struct rte_crypto_sym_xform auth_xform;
144 uint8_t akey_data[128];
147 /** l2fwd crypto lcore params */
148 struct l2fwd_crypto_params {
152 unsigned digest_length;
154 struct l2fwd_key iv_key;
155 struct rte_cryptodev_sym_session *session;
158 /** lcore configuration */
159 struct lcore_queue_conf {
160 unsigned nb_rx_ports;
161 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
163 unsigned nb_crypto_devs;
164 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
166 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
167 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
168 } __rte_cache_aligned;
170 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
172 static const struct rte_eth_conf port_conf = {
175 .header_split = 0, /**< Header Split disabled */
176 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
177 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
178 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
179 .hw_strip_crc = 0, /**< CRC stripped by hardware */
182 .mq_mode = ETH_MQ_TX_NONE,
186 struct rte_mempool *l2fwd_pktmbuf_pool;
187 struct rte_mempool *l2fwd_crypto_op_pool;
189 /* Per-port statistics struct */
190 struct l2fwd_port_statistics {
194 uint64_t crypto_enqueued;
195 uint64_t crypto_dequeued;
198 } __rte_cache_aligned;
200 struct l2fwd_crypto_statistics {
205 } __rte_cache_aligned;
207 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
208 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
210 /* A tsc-based timer responsible for triggering statistics printout */
211 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
212 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
214 /* default period is 10 seconds */
215 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
217 /* Print out statistics on packets dropped */
221 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
222 uint64_t total_packets_enqueued, total_packets_dequeued,
223 total_packets_errors;
227 total_packets_dropped = 0;
228 total_packets_tx = 0;
229 total_packets_rx = 0;
230 total_packets_enqueued = 0;
231 total_packets_dequeued = 0;
232 total_packets_errors = 0;
234 const char clr[] = { 27, '[', '2', 'J', '\0' };
235 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
237 /* Clear screen and move to top left */
238 printf("%s%s", clr, topLeft);
240 printf("\nPort statistics ====================================");
242 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
243 /* skip disabled ports */
244 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
246 printf("\nStatistics for port %u ------------------------------"
247 "\nPackets sent: %32"PRIu64
248 "\nPackets received: %28"PRIu64
249 "\nPackets dropped: %29"PRIu64,
251 port_statistics[portid].tx,
252 port_statistics[portid].rx,
253 port_statistics[portid].dropped);
255 total_packets_dropped += port_statistics[portid].dropped;
256 total_packets_tx += port_statistics[portid].tx;
257 total_packets_rx += port_statistics[portid].rx;
259 printf("\nCrypto statistics ==================================");
261 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
262 /* skip disabled ports */
263 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
265 printf("\nStatistics for cryptodev %"PRIu64
266 " -------------------------"
267 "\nPackets enqueued: %28"PRIu64
268 "\nPackets dequeued: %28"PRIu64
269 "\nPackets errors: %30"PRIu64,
271 crypto_statistics[cdevid].enqueued,
272 crypto_statistics[cdevid].dequeued,
273 crypto_statistics[cdevid].errors);
275 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
276 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
277 total_packets_errors += crypto_statistics[cdevid].errors;
279 printf("\nAggregate statistics ==============================="
280 "\nTotal packets received: %22"PRIu64
281 "\nTotal packets enqueued: %22"PRIu64
282 "\nTotal packets dequeued: %22"PRIu64
283 "\nTotal packets sent: %26"PRIu64
284 "\nTotal packets dropped: %23"PRIu64
285 "\nTotal packets crypto errors: %17"PRIu64,
287 total_packets_enqueued,
288 total_packets_dequeued,
290 total_packets_dropped,
291 total_packets_errors);
292 printf("\n====================================================\n");
298 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
299 struct l2fwd_crypto_params *cparams)
301 struct rte_crypto_op **op_buffer;
304 op_buffer = (struct rte_crypto_op **)
305 qconf->op_buf[cparams->dev_id].buffer;
307 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
308 cparams->qp_id, op_buffer, (uint16_t) n);
310 crypto_statistics[cparams->dev_id].enqueued += ret;
311 if (unlikely(ret < n)) {
312 crypto_statistics[cparams->dev_id].errors += (n - ret);
314 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
315 rte_crypto_op_free(op_buffer[ret]);
323 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
324 struct l2fwd_crypto_params *cparams)
326 unsigned lcore_id, len;
327 struct lcore_queue_conf *qconf;
329 lcore_id = rte_lcore_id();
331 qconf = &lcore_queue_conf[lcore_id];
332 len = qconf->op_buf[cparams->dev_id].len;
333 qconf->op_buf[cparams->dev_id].buffer[len] = op;
336 /* enough ops to be sent */
337 if (len == MAX_PKT_BURST) {
338 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
342 qconf->op_buf[cparams->dev_id].len = len;
347 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
348 struct rte_crypto_op *op,
349 struct l2fwd_crypto_params *cparams)
351 struct ether_hdr *eth_hdr;
352 struct ipv4_hdr *ip_hdr;
354 unsigned ipdata_offset, pad_len, data_len;
357 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
359 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
362 ipdata_offset = sizeof(struct ether_hdr);
364 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
367 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
368 * IPV4_IHL_MULTIPLIER;
371 /* Zero pad data to be crypto'd so it is block aligned */
372 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
373 pad_len = data_len % cparams->block_size ? cparams->block_size -
374 (data_len % cparams->block_size) : 0;
377 padding = rte_pktmbuf_append(m, pad_len);
378 if (unlikely(!padding))
382 memset(padding, 0, pad_len);
385 /* Set crypto operation data parameters */
386 rte_crypto_op_attach_sym_session(op, cparams->session);
388 /* Append space for digest to end of packet */
389 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
390 cparams->digest_length);
391 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
392 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
393 op->sym->auth.digest.length = cparams->digest_length;
395 op->sym->auth.data.offset = ipdata_offset;
396 op->sym->auth.data.length = data_len;
399 op->sym->cipher.iv.data = cparams->iv_key.data;
400 op->sym->cipher.iv.phys_addr = cparams->iv_key.phys_addr;
401 op->sym->cipher.iv.length = cparams->iv_key.length;
403 op->sym->cipher.data.offset = ipdata_offset;
404 op->sym->cipher.data.length = data_len;
408 return l2fwd_crypto_enqueue(op, cparams);
412 /* Send the burst of packets on an output interface */
414 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
417 struct rte_mbuf **pkt_buffer;
420 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
422 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
423 port_statistics[port].tx += ret;
424 if (unlikely(ret < n)) {
425 port_statistics[port].dropped += (n - ret);
427 rte_pktmbuf_free(pkt_buffer[ret]);
434 /* Enqueue packets for TX and prepare them to be sent */
436 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
438 unsigned lcore_id, len;
439 struct lcore_queue_conf *qconf;
441 lcore_id = rte_lcore_id();
443 qconf = &lcore_queue_conf[lcore_id];
444 len = qconf->pkt_buf[port].len;
445 qconf->pkt_buf[port].buffer[len] = m;
448 /* enough pkts to be sent */
449 if (unlikely(len == MAX_PKT_BURST)) {
450 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
454 qconf->pkt_buf[port].len = len;
459 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
461 struct ether_hdr *eth;
465 dst_port = l2fwd_dst_ports[portid];
466 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
468 /* 02:00:00:00:00:xx */
469 tmp = ð->d_addr.addr_bytes[0];
470 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
473 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
475 l2fwd_send_packet(m, (uint8_t) dst_port);
478 /** Generate random key */
480 generate_random_key(uint8_t *key, unsigned length)
484 for (i = 0; i < length; i++)
485 key[i] = rand() % 0xff;
488 static struct rte_cryptodev_sym_session *
489 initialize_crypto_session(struct l2fwd_crypto_options *options,
492 struct rte_crypto_sym_xform *first_xform;
494 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
495 first_xform = &options->cipher_xform;
496 first_xform->next = &options->auth_xform;
498 first_xform = &options->auth_xform;
499 first_xform->next = &options->cipher_xform;
502 /* Setup Cipher Parameters */
503 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
507 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
509 /* main processing loop */
511 l2fwd_main_loop(struct l2fwd_crypto_options *options)
513 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
514 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
516 unsigned lcore_id = rte_lcore_id();
517 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
518 unsigned i, j, portid, nb_rx;
519 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
520 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
521 US_PER_S * BURST_TX_DRAIN_US;
522 struct l2fwd_crypto_params *cparams;
523 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
525 if (qconf->nb_rx_ports == 0) {
526 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
530 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
532 l2fwd_crypto_options_print(options);
534 for (i = 0; i < qconf->nb_rx_ports; i++) {
536 portid = qconf->rx_port_list[i];
537 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
541 for (i = 0; i < qconf->nb_crypto_devs; i++) {
542 port_cparams[i].dev_id = qconf->cryptodev_list[i];
543 port_cparams[i].qp_id = 0;
545 port_cparams[i].block_size = 64;
546 port_cparams[i].digest_length = 20;
548 port_cparams[i].iv_key.data =
549 (uint8_t *)rte_malloc(NULL, 16, 8);
550 port_cparams[i].iv_key.length = 16;
551 port_cparams[i].iv_key.phys_addr = rte_malloc_virt2phy(
552 (void *)port_cparams[i].iv_key.data);
553 generate_random_key(port_cparams[i].iv_key.data,
554 sizeof(cparams[i].iv_key.length));
556 port_cparams[i].session = initialize_crypto_session(options,
557 port_cparams[i].dev_id);
559 if (port_cparams[i].session == NULL)
561 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
562 port_cparams[i].dev_id);
567 cur_tsc = rte_rdtsc();
570 * TX burst queue drain
572 diff_tsc = cur_tsc - prev_tsc;
573 if (unlikely(diff_tsc > drain_tsc)) {
575 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
576 if (qconf->pkt_buf[portid].len == 0)
578 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
579 qconf->pkt_buf[portid].len,
581 qconf->pkt_buf[portid].len = 0;
584 /* if timer is enabled */
585 if (timer_period > 0) {
587 /* advance the timer */
588 timer_tsc += diff_tsc;
590 /* if timer has reached its timeout */
591 if (unlikely(timer_tsc >=
592 (uint64_t)timer_period)) {
594 /* do this only on master core */
595 if (lcore_id == rte_get_master_lcore()
596 && options->refresh_period) {
607 * Read packet from RX queues
609 for (i = 0; i < qconf->nb_rx_ports; i++) {
610 portid = qconf->rx_port_list[i];
612 cparams = &port_cparams[i];
614 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
615 pkts_burst, MAX_PKT_BURST);
617 port_statistics[portid].rx += nb_rx;
621 * If we can't allocate a crypto_ops, then drop
622 * the rest of the burst and dequeue and
623 * process the packets to free offload structs
625 if (rte_crypto_op_bulk_alloc(
626 l2fwd_crypto_op_pool,
627 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
630 for (j = 0; j < nb_rx; j++)
631 rte_pktmbuf_free(pkts_burst[i]);
636 /* Enqueue packets from Crypto device*/
637 for (j = 0; j < nb_rx; j++) {
640 l2fwd_simple_crypto_enqueue(m,
641 ops_burst[j], cparams);
645 /* Dequeue packets from Crypto device */
647 nb_rx = rte_cryptodev_dequeue_burst(
648 cparams->dev_id, cparams->qp_id,
649 ops_burst, MAX_PKT_BURST);
651 crypto_statistics[cparams->dev_id].dequeued +=
654 /* Forward crypto'd packets */
655 for (j = 0; j < nb_rx; j++) {
656 m = ops_burst[j]->sym->m_src;
658 rte_crypto_op_free(ops_burst[j]);
659 l2fwd_simple_forward(m, portid);
661 } while (nb_rx == MAX_PKT_BURST);
667 l2fwd_launch_one_lcore(void *arg)
669 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
673 /* Display command line arguments usage */
675 l2fwd_crypto_usage(const char *prgname)
677 printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
678 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
679 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
680 " -s manage all ports from single lcore"
681 " -t PERIOD: statistics will be refreshed each PERIOD seconds"
682 " (0 to disable, 10 default, 86400 maximum)\n"
684 " --cdev AESNI_MB / QAT\n"
685 " --chain HASH_CIPHER / CIPHER_HASH\n"
687 " --cipher_algo ALGO\n"
688 " --cipher_op ENCRYPT / DECRYPT\n"
689 " --cipher_key KEY\n"
692 " --auth_algo ALGO\n"
693 " --auth_op GENERATE / VERIFY\n"
700 /** Parse crypto device type command line argument */
702 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
704 if (strcmp("AESNI_MB", optarg) == 0) {
705 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
707 } else if (strcmp("QAT", optarg) == 0) {
708 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
715 /** Parse crypto chain xform command line argument */
717 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
719 if (strcmp("CIPHER_HASH", optarg) == 0) {
720 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
722 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
723 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
730 /** Parse crypto cipher algo option command line argument */
732 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
734 if (strcmp("AES_CBC", optarg) == 0) {
735 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
737 } else if (strcmp("AES_GCM", optarg) == 0) {
738 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
742 printf("Cipher algorithm not supported!\n");
746 /** Parse crypto cipher operation command line argument */
748 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
750 if (strcmp("ENCRYPT", optarg) == 0) {
751 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
753 } else if (strcmp("DECRYPT", optarg) == 0) {
754 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
758 printf("Cipher operation not supported!\n");
762 /** Parse crypto key command line argument */
764 parse_key(struct l2fwd_key *key __rte_unused,
765 unsigned length __rte_unused, char *arg __rte_unused)
767 printf("Currently an unsupported argument!\n");
771 /** Parse crypto cipher operation command line argument */
773 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
775 if (strcmp("SHA1", optarg) == 0) {
776 *algo = RTE_CRYPTO_AUTH_SHA1;
778 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
779 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
781 } else if (strcmp("SHA224", optarg) == 0) {
782 *algo = RTE_CRYPTO_AUTH_SHA224;
784 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
785 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
787 } else if (strcmp("SHA256", optarg) == 0) {
788 *algo = RTE_CRYPTO_AUTH_SHA256;
790 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
791 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
793 } else if (strcmp("SHA512", optarg) == 0) {
794 *algo = RTE_CRYPTO_AUTH_SHA256;
796 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
797 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
801 printf("Authentication algorithm specified not supported!\n");
806 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
808 if (strcmp("VERIFY", optarg) == 0) {
809 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
811 } else if (strcmp("GENERATE", optarg) == 0) {
812 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
816 printf("Authentication operation specified not supported!\n");
820 /** Parse long options */
822 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
823 struct option *lgopts, int option_index)
825 if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
826 return parse_cryptodev_type(&options->cdev_type, optarg);
828 else if (strcmp(lgopts[option_index].name, "chain") == 0)
829 return parse_crypto_opt_chain(options, optarg);
832 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
833 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
836 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
837 return parse_cipher_op(&options->cipher_xform.cipher.op,
840 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
841 struct l2fwd_key key = { 0 };
844 retval = parse_key(&key, sizeof(options->ckey_data), optarg);
846 options->cipher_xform.cipher.key.data = key.data;
847 options->cipher_xform.cipher.key.length = key.length;
851 } else if (strcmp(lgopts[option_index].name, "iv") == 0)
852 return parse_key(&options->iv_key, sizeof(options->ivkey_data),
855 /* Authentication options */
856 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
857 return parse_auth_algo(&options->auth_xform.auth.algo,
860 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
861 return parse_auth_op(&options->auth_xform.auth.op,
864 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
865 struct l2fwd_key key = { 0 };
868 retval = parse_key(&key, sizeof(options->akey_data), optarg);
870 options->auth_xform.auth.key.data = key.data;
871 options->auth_xform.auth.key.length = key.length;
875 } else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
876 options->sessionless = 1;
883 /** Parse port mask */
885 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
891 /* parse hexadecimal string */
892 pm = strtoul(q_arg, &end, 16);
893 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
896 options->portmask = pm;
897 if (options->portmask == 0) {
898 printf("invalid portmask specified\n");
905 /** Parse number of queues */
907 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
913 /* parse hexadecimal string */
914 n = strtoul(q_arg, &end, 10);
915 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
917 else if (n >= MAX_RX_QUEUE_PER_LCORE)
920 options->nb_ports_per_lcore = n;
921 if (options->nb_ports_per_lcore == 0) {
922 printf("invalid number of ports selected\n");
929 /** Parse timer period */
931 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
937 /* parse number string */
938 n = strtol(q_arg, &end, 10);
939 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
942 if (n >= MAX_TIMER_PERIOD) {
943 printf("Warning refresh period specified %ld is greater than "
944 "max value %d! using max value",
945 n, MAX_TIMER_PERIOD);
946 n = MAX_TIMER_PERIOD;
949 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
954 /** Generate default options for application */
956 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
960 options->portmask = 0xffffffff;
961 options->nb_ports_per_lcore = 1;
962 options->refresh_period = 10000;
963 options->single_lcore = 0;
965 options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
966 options->sessionless = 0;
967 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
970 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
971 options->cipher_xform.next = NULL;
973 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
974 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
976 generate_random_key(options->ckey_data, sizeof(options->ckey_data));
978 options->cipher_xform.cipher.key.data = options->ckey_data;
979 options->cipher_xform.cipher.key.length = 16;
982 /* Authentication Data */
983 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
984 options->auth_xform.next = NULL;
986 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
987 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
989 options->auth_xform.auth.add_auth_data_length = 0;
990 options->auth_xform.auth.digest_length = 20;
992 generate_random_key(options->akey_data, sizeof(options->akey_data));
994 options->auth_xform.auth.key.data = options->akey_data;
995 options->auth_xform.auth.key.length = 20;
999 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1001 printf("Options:-\nn");
1002 printf("portmask: %x\n", options->portmask);
1003 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1004 printf("refresh period : %u\n", options->refresh_period);
1005 printf("single lcore mode: %s\n",
1006 options->single_lcore ? "enabled" : "disabled");
1007 printf("stats_printing: %s\n",
1008 options->refresh_period == 0 ? "disabled" : "enabled");
1010 switch (options->cdev_type) {
1011 case RTE_CRYPTODEV_AESNI_MB_PMD:
1012 printf("cryptodev type: AES-NI MB PMD\n"); break;
1013 case RTE_CRYPTODEV_QAT_SYM_PMD:
1014 printf("cryptodev type: QAT PMD\n"); break;
1019 printf("sessionless crypto: %s\n",
1020 options->sessionless ? "enabled" : "disabled");
1022 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1025 options->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1026 options->cipher_xform.next = NULL;
1028 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1029 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1031 generate_random_key(options->ckey_data, sizeof(options->ckey_data));
1033 options->cipher_xform.cipher.key.data = options->ckey_data;
1034 options->cipher_xform.cipher.key.phys_addr = 0;
1035 options->cipher_xform.cipher.key.length = 16;
1038 /* Authentication Data */
1039 options->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1040 options->auth_xform.next = NULL;
1042 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1043 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1045 options->auth_xform.auth.add_auth_data_length = 0;
1046 options->auth_xform.auth.digest_length = 20;
1048 generate_random_key(options->akey_data, sizeof(options->akey_data));
1050 options->auth_xform.auth.key.data = options->akey_data;
1051 options->auth_xform.auth.key.phys_addr = 0;
1052 options->auth_xform.auth.key.length = 20;
1056 /* Parse the argument given in the command line of the application */
1058 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1059 int argc, char **argv)
1061 int opt, retval, option_index;
1062 char **argvopt = argv, *prgname = argv[0];
1064 static struct option lgopts[] = {
1065 { "sessionless", no_argument, 0, 0 },
1067 { "cdev_type", required_argument, 0, 0 },
1068 { "chain", required_argument, 0, 0 },
1070 { "cipher_algo", required_argument, 0, 0 },
1071 { "cipher_op", required_argument, 0, 0 },
1072 { "cipher_key", required_argument, 0, 0 },
1074 { "auth_algo", required_argument, 0, 0 },
1075 { "auth_op", required_argument, 0, 0 },
1076 { "auth_key", required_argument, 0, 0 },
1078 { "iv", required_argument, 0, 0 },
1080 { "sessionless", no_argument, 0, 0 },
1084 l2fwd_crypto_default_options(options);
1086 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1087 &option_index)) != EOF) {
1091 retval = l2fwd_crypto_parse_args_long_options(options,
1092 lgopts, option_index);
1094 l2fwd_crypto_usage(prgname);
1101 retval = l2fwd_crypto_parse_portmask(options, optarg);
1103 l2fwd_crypto_usage(prgname);
1110 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1112 l2fwd_crypto_usage(prgname);
1119 options->single_lcore = 1;
1125 retval = l2fwd_crypto_parse_timer_period(options,
1128 l2fwd_crypto_usage(prgname);
1134 l2fwd_crypto_usage(prgname);
1141 argv[optind-1] = prgname;
1144 optind = 0; /* reset getopt lib */
1149 /* Check the link status of all ports in up to 9s, and print them finally */
1151 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1153 #define CHECK_INTERVAL 100 /* 100ms */
1154 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1155 uint8_t portid, count, all_ports_up, print_flag = 0;
1156 struct rte_eth_link link;
1158 printf("\nChecking link status");
1160 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1162 for (portid = 0; portid < port_num; portid++) {
1163 if ((port_mask & (1 << portid)) == 0)
1165 memset(&link, 0, sizeof(link));
1166 rte_eth_link_get_nowait(portid, &link);
1167 /* print link status if flag set */
1168 if (print_flag == 1) {
1169 if (link.link_status)
1170 printf("Port %d Link Up - speed %u "
1171 "Mbps - %s\n", (uint8_t)portid,
1172 (unsigned)link.link_speed,
1173 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1174 ("full-duplex") : ("half-duplex\n"));
1176 printf("Port %d Link Down\n",
1180 /* clear all_ports_up flag if any link down */
1181 if (link.link_status == 0) {
1186 /* after finally printing all link status, get out */
1187 if (print_flag == 1)
1190 if (all_ports_up == 0) {
1193 rte_delay_ms(CHECK_INTERVAL);
1196 /* set the print_flag if all ports up or timeout */
1197 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1205 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1207 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1210 if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1211 if (rte_cryptodev_count() < nb_ports)
1213 } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1214 for (i = 0; i < nb_ports; i++) {
1215 int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1222 cdev_count = rte_cryptodev_count();
1224 cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1226 struct rte_cryptodev_qp_conf qp_conf;
1227 struct rte_cryptodev_info dev_info;
1229 struct rte_cryptodev_config conf = {
1230 .nb_queue_pairs = 1,
1231 .socket_id = SOCKET_ID_ANY,
1238 rte_cryptodev_info_get(cdev_id, &dev_info);
1240 if (dev_info.dev_type != options->cdev_type)
1244 retval = rte_cryptodev_configure(cdev_id, &conf);
1246 printf("Failed to configure cryptodev %u", cdev_id);
1250 qp_conf.nb_descriptors = 2048;
1252 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1255 printf("Failed to setup queue pair %u on cryptodev %u",
1260 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1262 enabled_cdev_count++;
1265 return enabled_cdev_count;
1269 initialize_ports(struct l2fwd_crypto_options *options)
1271 uint8_t last_portid, portid;
1272 unsigned enabled_portcount = 0;
1273 unsigned nb_ports = rte_eth_dev_count();
1275 if (nb_ports == 0) {
1276 printf("No Ethernet ports - bye\n");
1280 if (nb_ports > RTE_MAX_ETHPORTS)
1281 nb_ports = RTE_MAX_ETHPORTS;
1283 /* Reset l2fwd_dst_ports */
1284 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1285 l2fwd_dst_ports[portid] = 0;
1287 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1290 /* Skip ports that are not enabled */
1291 if ((options->portmask & (1 << portid)) == 0)
1295 printf("Initializing port %u... ", (unsigned) portid);
1297 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1299 printf("Cannot configure device: err=%d, port=%u\n",
1300 retval, (unsigned) portid);
1304 /* init one RX queue */
1306 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1307 rte_eth_dev_socket_id(portid),
1308 NULL, l2fwd_pktmbuf_pool);
1310 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1311 retval, (unsigned) portid);
1315 /* init one TX queue on each port */
1317 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1318 rte_eth_dev_socket_id(portid),
1321 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1322 retval, (unsigned) portid);
1328 retval = rte_eth_dev_start(portid);
1330 printf("rte_eth_dev_start:err=%d, port=%u\n",
1331 retval, (unsigned) portid);
1335 rte_eth_promiscuous_enable(portid);
1337 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1339 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1341 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1342 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1343 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1344 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1345 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1346 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1348 /* initialize port stats */
1349 memset(&port_statistics, 0, sizeof(port_statistics));
1351 /* Setup port forwarding table */
1352 if (enabled_portcount % 2) {
1353 l2fwd_dst_ports[portid] = last_portid;
1354 l2fwd_dst_ports[last_portid] = portid;
1356 last_portid = portid;
1359 l2fwd_enabled_port_mask |= (1 << portid);
1360 enabled_portcount++;
1363 if (enabled_portcount == 1) {
1364 l2fwd_dst_ports[last_portid] = last_portid;
1365 } else if (enabled_portcount % 2) {
1366 printf("odd number of ports in portmask- bye\n");
1370 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1372 return enabled_portcount;
1376 main(int argc, char **argv)
1378 struct lcore_queue_conf *qconf;
1379 struct l2fwd_crypto_options options;
1381 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1382 unsigned lcore_id, rx_lcore_id;
1383 int ret, enabled_cdevcount, enabled_portcount;
1386 ret = rte_eal_init(argc, argv);
1388 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1392 /* parse application arguments (after the EAL ones) */
1393 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1395 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1397 /* create the mbuf pool */
1398 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1399 sizeof(struct rte_crypto_op),
1400 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1401 if (l2fwd_pktmbuf_pool == NULL)
1402 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1404 /* create crypto op pool */
1405 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1406 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1408 if (l2fwd_crypto_op_pool == NULL)
1409 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1411 /* Enable Ethernet ports */
1412 enabled_portcount = initialize_ports(&options);
1413 if (enabled_portcount < 1)
1414 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1416 nb_ports = rte_eth_dev_count();
1417 /* Initialize the port/queue configuration of each logical core */
1418 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1419 portid < nb_ports; portid++) {
1421 /* skip ports that are not enabled */
1422 if ((options.portmask & (1 << portid)) == 0)
1425 if (options.single_lcore && qconf == NULL) {
1426 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1428 if (rx_lcore_id >= RTE_MAX_LCORE)
1429 rte_exit(EXIT_FAILURE,
1430 "Not enough cores\n");
1432 } else if (!options.single_lcore) {
1433 /* get the lcore_id for this port */
1434 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1435 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1436 options.nb_ports_per_lcore) {
1438 if (rx_lcore_id >= RTE_MAX_LCORE)
1439 rte_exit(EXIT_FAILURE,
1440 "Not enough cores\n");
1444 /* Assigned a new logical core in the loop above. */
1445 if (qconf != &lcore_queue_conf[rx_lcore_id])
1446 qconf = &lcore_queue_conf[rx_lcore_id];
1448 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1449 qconf->nb_rx_ports++;
1451 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1455 /* Enable Crypto devices */
1456 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1457 if (enabled_cdevcount < 1)
1458 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1460 nb_cryptodevs = rte_cryptodev_count();
1461 /* Initialize the port/queue configuration of each logical core */
1462 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1463 cdev_id < nb_cryptodevs && enabled_cdevcount;
1465 struct rte_cryptodev_info info;
1467 rte_cryptodev_info_get(cdev_id, &info);
1469 /* skip devices of the wrong type */
1470 if (options.cdev_type != info.dev_type)
1473 if (options.single_lcore && qconf == NULL) {
1474 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1476 if (rx_lcore_id >= RTE_MAX_LCORE)
1477 rte_exit(EXIT_FAILURE,
1478 "Not enough cores\n");
1480 } else if (!options.single_lcore) {
1481 /* get the lcore_id for this port */
1482 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1483 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1484 options.nb_ports_per_lcore) {
1486 if (rx_lcore_id >= RTE_MAX_LCORE)
1487 rte_exit(EXIT_FAILURE,
1488 "Not enough cores\n");
1492 /* Assigned a new logical core in the loop above. */
1493 if (qconf != &lcore_queue_conf[rx_lcore_id])
1494 qconf = &lcore_queue_conf[rx_lcore_id];
1496 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1497 qconf->nb_crypto_devs++;
1499 enabled_cdevcount--;
1501 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1507 /* launch per-lcore init on every lcore */
1508 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1510 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1511 if (rte_eal_wait_lcore(lcore_id) < 0)