4 * Copyright(c) 2010-2014 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_mbuf_offload.h>
66 #include <rte_memcpy.h>
67 #include <rte_memory.h>
68 #include <rte_mempool.h>
69 #include <rte_memzone.h>
71 #include <rte_per_lcore.h>
72 #include <rte_prefetch.h>
73 #include <rte_random.h>
76 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
80 #define MAX_PKT_BURST 32
81 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
84 * Configurable number of RX/TX ring descriptors
86 #define RTE_TEST_RX_DESC_DEFAULT 128
87 #define RTE_TEST_TX_DESC_DEFAULT 512
88 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
89 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
91 /* ethernet addresses of ports */
92 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
94 /* mask of enabled ports */
95 static uint64_t l2fwd_enabled_port_mask;
96 static uint64_t l2fwd_enabled_crypto_mask;
98 /* list of enabled ports */
99 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
104 struct rte_mbuf *buffer[MAX_PKT_BURST];
107 #define MAX_RX_QUEUE_PER_LCORE 16
108 #define MAX_TX_QUEUE_PER_PORT 16
110 enum l2fwd_crypto_xform_chain {
111 L2FWD_CRYPTO_CIPHER_HASH,
112 L2FWD_CRYPTO_HASH_CIPHER
115 /** l2fwd crypto application command line options */
116 struct l2fwd_crypto_options {
118 unsigned nb_ports_per_lcore;
119 unsigned refresh_period;
120 unsigned single_lcore:1;
121 unsigned no_stats_printing:1;
123 enum rte_cryptodev_type cdev_type;
124 unsigned sessionless:1;
126 enum l2fwd_crypto_xform_chain xform_chain;
128 struct rte_crypto_xform cipher_xform;
129 uint8_t ckey_data[32];
131 struct rte_crypto_key iv_key;
132 uint8_t ivkey_data[16];
134 struct rte_crypto_xform auth_xform;
135 uint8_t akey_data[128];
138 /** l2fwd crypto lcore params */
139 struct l2fwd_crypto_params {
143 unsigned digest_length;
146 struct rte_crypto_key iv_key;
147 struct rte_cryptodev_session *session;
150 /** lcore configuration */
151 struct lcore_queue_conf {
152 unsigned nb_rx_ports;
153 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
155 unsigned nb_crypto_devs;
156 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
158 struct pkt_buffer crypto_pkt_buf[RTE_MAX_ETHPORTS];
159 struct pkt_buffer tx_pkt_buf[RTE_MAX_ETHPORTS];
160 } __rte_cache_aligned;
162 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
164 static const struct rte_eth_conf port_conf = {
167 .header_split = 0, /**< Header Split disabled */
168 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
169 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
170 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
171 .hw_strip_crc = 0, /**< CRC stripped by hardware */
174 .mq_mode = ETH_MQ_TX_NONE,
178 struct rte_mempool *l2fwd_pktmbuf_pool;
179 struct rte_mempool *l2fwd_mbuf_ol_pool;
181 /* Per-port statistics struct */
182 struct l2fwd_port_statistics {
186 uint64_t crypto_enqueued;
187 uint64_t crypto_dequeued;
190 } __rte_cache_aligned;
192 struct l2fwd_crypto_statistics {
197 } __rte_cache_aligned;
199 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
200 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
202 /* A tsc-based timer responsible for triggering statistics printout */
203 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
204 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
206 /* default period is 10 seconds */
207 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
209 uint64_t total_packets_dropped = 0, total_packets_tx = 0, total_packets_rx = 0,
210 total_packets_enqueued = 0, total_packets_dequeued = 0,
211 total_packets_errors = 0;
213 /* Print out statistics on packets dropped */
221 const char clr[] = { 27, '[', '2', 'J', '\0' };
222 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
224 /* Clear screen and move to top left */
225 printf("%s%s", clr, topLeft);
227 printf("\nPort statistics ====================================");
229 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
230 /* skip disabled ports */
231 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
233 printf("\nStatistics for port %u ------------------------------"
234 "\nPackets sent: %32"PRIu64
235 "\nPackets received: %28"PRIu64
236 "\nPackets dropped: %29"PRIu64,
238 port_statistics[portid].tx,
239 port_statistics[portid].rx,
240 port_statistics[portid].dropped);
242 total_packets_dropped += port_statistics[portid].dropped;
243 total_packets_tx += port_statistics[portid].tx;
244 total_packets_rx += port_statistics[portid].rx;
246 printf("\nCrypto statistics ==================================");
248 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
249 /* skip disabled ports */
250 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
252 printf("\nStatistics for cryptodev %"PRIu64
253 " -------------------------"
254 "\nPackets enqueued: %28"PRIu64
255 "\nPackets dequeued: %28"PRIu64
256 "\nPackets errors: %30"PRIu64,
258 crypto_statistics[cdevid].enqueued,
259 crypto_statistics[cdevid].dequeued,
260 crypto_statistics[cdevid].errors);
262 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
263 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
264 total_packets_errors += crypto_statistics[cdevid].errors;
266 printf("\nAggregate statistics ==============================="
267 "\nTotal packets received: %22"PRIu64
268 "\nTotal packets enqueued: %22"PRIu64
269 "\nTotal packets dequeued: %22"PRIu64
270 "\nTotal packets sent: %26"PRIu64
271 "\nTotal packets dropped: %23"PRIu64
272 "\nTotal packets crypto errors: %17"PRIu64,
274 total_packets_enqueued,
275 total_packets_dequeued,
277 total_packets_dropped,
278 total_packets_errors);
279 printf("\n====================================================\n");
285 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
286 struct l2fwd_crypto_params *cparams)
288 struct rte_mbuf **pkt_buffer;
291 pkt_buffer = (struct rte_mbuf **)
292 qconf->crypto_pkt_buf[cparams->dev_id].buffer;
294 ret = rte_cryptodev_enqueue_burst(cparams->dev_id, cparams->qp_id,
295 pkt_buffer, (uint16_t) n);
296 crypto_statistics[cparams->dev_id].enqueued += ret;
297 if (unlikely(ret < n)) {
298 crypto_statistics[cparams->dev_id].errors += (n - ret);
300 rte_pktmbuf_offload_free(pkt_buffer[ret]->offload_ops);
301 rte_pktmbuf_free(pkt_buffer[ret]);
309 l2fwd_crypto_enqueue(struct rte_mbuf *m, struct l2fwd_crypto_params *cparams)
311 unsigned lcore_id, len;
312 struct lcore_queue_conf *qconf;
314 lcore_id = rte_lcore_id();
316 qconf = &lcore_queue_conf[lcore_id];
317 len = qconf->crypto_pkt_buf[cparams->dev_id].len;
318 qconf->crypto_pkt_buf[cparams->dev_id].buffer[len] = m;
321 /* enough pkts to be sent */
322 if (len == MAX_PKT_BURST) {
323 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
327 qconf->crypto_pkt_buf[cparams->dev_id].len = len;
332 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
333 struct rte_mbuf_offload *ol,
334 struct l2fwd_crypto_params *cparams)
336 struct ether_hdr *eth_hdr;
337 struct ipv4_hdr *ip_hdr;
339 unsigned ipdata_offset, pad_len, data_len;
342 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
344 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
347 ipdata_offset = sizeof(struct ether_hdr);
349 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
352 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
353 * IPV4_IHL_MULTIPLIER;
356 /* Zero pad data to be crypto'd so it is block aligned */
357 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
358 pad_len = data_len % cparams->block_size ? cparams->block_size -
359 (data_len % cparams->block_size) : 0;
362 padding = rte_pktmbuf_append(m, pad_len);
363 if (unlikely(!padding))
367 memset(padding, 0, pad_len);
370 /* Set crypto operation data parameters */
371 rte_crypto_op_attach_session(&ol->op.crypto, cparams->session);
373 /* Append space for digest to end of packet */
374 ol->op.crypto.digest.data = (uint8_t *)rte_pktmbuf_append(m,
375 cparams->digest_length);
376 ol->op.crypto.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
377 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
378 ol->op.crypto.digest.length = cparams->digest_length;
380 ol->op.crypto.iv.data = cparams->iv_key.data;
381 ol->op.crypto.iv.phys_addr = cparams->iv_key.phys_addr;
382 ol->op.crypto.iv.length = cparams->iv_key.length;
384 ol->op.crypto.data.to_cipher.offset = ipdata_offset;
385 ol->op.crypto.data.to_cipher.length = data_len;
387 ol->op.crypto.data.to_hash.offset = ipdata_offset;
388 ol->op.crypto.data.to_hash.length = data_len;
390 rte_pktmbuf_offload_attach(m, ol);
392 return l2fwd_crypto_enqueue(m, cparams);
396 /* Send the burst of packets on an output interface */
398 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
400 struct rte_mbuf **pkt_buffer;
402 unsigned queueid = 0;
404 pkt_buffer = (struct rte_mbuf **)qconf->tx_pkt_buf[port].buffer;
406 ret = rte_eth_tx_burst(port, (uint16_t) queueid, pkt_buffer,
408 port_statistics[port].tx += ret;
409 if (unlikely(ret < n)) {
410 port_statistics[port].dropped += (n - ret);
412 rte_pktmbuf_free(pkt_buffer[ret]);
419 /* Enqueue packets for TX and prepare them to be sent */
421 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
423 unsigned lcore_id, len;
424 struct lcore_queue_conf *qconf;
426 lcore_id = rte_lcore_id();
428 qconf = &lcore_queue_conf[lcore_id];
429 len = qconf->tx_pkt_buf[port].len;
430 qconf->tx_pkt_buf[port].buffer[len] = m;
433 /* enough pkts to be sent */
434 if (unlikely(len == MAX_PKT_BURST)) {
435 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
439 qconf->tx_pkt_buf[port].len = len;
444 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
446 struct ether_hdr *eth;
450 dst_port = l2fwd_dst_ports[portid];
451 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
453 /* 02:00:00:00:00:xx */
454 tmp = ð->d_addr.addr_bytes[0];
455 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
458 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
460 l2fwd_send_packet(m, (uint8_t) dst_port);
463 /** Generate random key */
465 generate_random_key(uint8_t *key, unsigned length)
469 for (i = 0; i < length; i++)
470 key[i] = rand() % 0xff;
473 static struct rte_cryptodev_session *
474 initialize_crypto_session(struct l2fwd_crypto_options *options,
477 struct rte_crypto_xform *first_xform;
479 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
480 first_xform = &options->cipher_xform;
481 first_xform->next = &options->auth_xform;
483 first_xform = &options->auth_xform;
484 first_xform->next = &options->cipher_xform;
487 /* Setup Cipher Parameters */
488 return rte_cryptodev_session_create(cdev_id, first_xform);
492 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
494 /* main processing loop */
496 l2fwd_main_loop(struct l2fwd_crypto_options *options)
498 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
499 unsigned lcore_id = rte_lcore_id();
500 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
501 unsigned i, j, portid, nb_rx;
502 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
503 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
504 US_PER_S * BURST_TX_DRAIN_US;
505 struct l2fwd_crypto_params *cparams;
506 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
508 if (qconf->nb_rx_ports == 0) {
509 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
513 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
515 l2fwd_crypto_options_print(options);
517 for (i = 0; i < qconf->nb_rx_ports; i++) {
519 portid = qconf->rx_port_list[i];
520 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
524 for (i = 0; i < qconf->nb_crypto_devs; i++) {
525 port_cparams[i].dev_id = qconf->cryptodev_list[i];
526 port_cparams[i].qp_id = 0;
528 port_cparams[i].block_size = 64;
529 port_cparams[i].digest_length = 20;
531 port_cparams[i].iv_key.data =
532 (uint8_t *)rte_malloc(NULL, 16, 8);
533 port_cparams[i].iv_key.length = 16;
534 port_cparams[i].iv_key.phys_addr = rte_malloc_virt2phy(
535 (void *)port_cparams[i].iv_key.data);
536 generate_random_key(port_cparams[i].iv_key.data,
537 sizeof(cparams[i].iv_key.length));
539 port_cparams[i].session = initialize_crypto_session(options,
540 port_cparams[i].dev_id);
542 if (port_cparams[i].session == NULL)
544 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
545 port_cparams[i].dev_id);
550 cur_tsc = rte_rdtsc();
553 * TX burst queue drain
555 diff_tsc = cur_tsc - prev_tsc;
556 if (unlikely(diff_tsc > drain_tsc)) {
558 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
559 if (qconf->tx_pkt_buf[portid].len == 0)
561 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
562 qconf->tx_pkt_buf[portid].len,
564 qconf->tx_pkt_buf[portid].len = 0;
567 /* if timer is enabled */
568 if (timer_period > 0) {
570 /* advance the timer */
571 timer_tsc += diff_tsc;
573 /* if timer has reached its timeout */
574 if (unlikely(timer_tsc >=
575 (uint64_t)timer_period)) {
577 /* do this only on master core */
578 if (lcore_id == rte_get_master_lcore() &&
579 !options->no_stats_printing) {
581 /* reset the timer */
591 * Read packet from RX queues
593 for (i = 0; i < qconf->nb_rx_ports; i++) {
594 struct rte_mbuf_offload *ol;
596 portid = qconf->rx_port_list[i];
598 cparams = &port_cparams[i];
600 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
601 pkts_burst, MAX_PKT_BURST);
603 port_statistics[portid].rx += nb_rx;
605 /* Enqueue packets from Crypto device*/
606 for (j = 0; j < nb_rx; j++) {
608 ol = rte_pktmbuf_offload_alloc(
610 RTE_PKTMBUF_OL_CRYPTO);
612 * If we can't allocate a offload, then drop
613 * the rest of the burst and dequeue and
614 * process the packets to free offload structs
616 if (unlikely(ol == NULL)) {
617 for (; j < nb_rx; j++) {
618 rte_pktmbuf_free(pkts_burst[j]);
619 port_statistics[portid].dropped++;
624 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
625 rte_prefetch0((void *)ol);
627 l2fwd_simple_crypto_enqueue(m, ol, cparams);
630 /* Dequeue packets from Crypto device */
631 nb_rx = rte_cryptodev_dequeue_burst(
632 cparams->dev_id, cparams->qp_id,
633 pkts_burst, MAX_PKT_BURST);
634 crypto_statistics[cparams->dev_id].dequeued += nb_rx;
636 /* Forward crypto'd packets */
637 for (j = 0; j < nb_rx; j++) {
639 rte_pktmbuf_offload_free(m->offload_ops);
640 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
641 l2fwd_simple_forward(m, portid);
648 l2fwd_launch_one_lcore(void *arg)
650 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
654 /* Display command line arguments usage */
656 l2fwd_crypto_usage(const char *prgname)
658 printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
659 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
660 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
661 " -s manage all ports from single lcore"
662 " -t PERIOD: statistics will be refreshed each PERIOD seconds"
663 " (0 to disable, 10 default, 86400 maximum)\n"
665 " --cdev AESNI_MB / QAT\n"
666 " --chain HASH_CIPHER / CIPHER_HASH\n"
668 " --cipher_algo ALGO\n"
669 " --cipher_op ENCRYPT / DECRYPT\n"
670 " --cipher_key KEY\n"
673 " --auth_op GENERATE / VERIFY\n"
680 /** Parse crypto device type command line argument */
682 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
684 if (strcmp("AESNI_MB", optarg) == 0) {
685 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
687 } else if (strcmp("QAT", optarg) == 0) {
688 *type = RTE_CRYPTODEV_QAT_PMD;
695 /** Parse crypto chain xform command line argument */
697 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
699 if (strcmp("CIPHER_HASH", optarg) == 0) {
700 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
702 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
703 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
710 /** Parse crypto cipher algo option command line argument */
712 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
714 if (strcmp("AES_CBC", optarg) == 0) {
715 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
717 } else if (strcmp("AES_GCM", optarg) == 0) {
718 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
722 printf("Cipher algorithm not supported!\n");
726 /** Parse crypto cipher operation command line argument */
728 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
730 if (strcmp("ENCRYPT", optarg) == 0) {
731 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
733 } else if (strcmp("DECRYPT", optarg) == 0) {
734 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
738 printf("Cipher operation not supported!\n");
742 /** Parse crypto key command line argument */
744 parse_key(struct rte_crypto_key *key __rte_unused,
745 unsigned length __rte_unused, char *arg __rte_unused)
747 printf("Currently an unsupported argument!\n");
751 /** Parse crypto cipher operation command line argument */
753 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
755 if (strcmp("SHA1", optarg) == 0) {
756 *algo = RTE_CRYPTO_AUTH_SHA1;
758 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
759 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
761 } else if (strcmp("SHA224", optarg) == 0) {
762 *algo = RTE_CRYPTO_AUTH_SHA224;
764 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
765 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
767 } else if (strcmp("SHA256", optarg) == 0) {
768 *algo = RTE_CRYPTO_AUTH_SHA256;
770 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
771 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
773 } else if (strcmp("SHA512", optarg) == 0) {
774 *algo = RTE_CRYPTO_AUTH_SHA256;
776 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
777 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
781 printf("Authentication algorithm specified not supported!\n");
786 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
788 if (strcmp("VERIFY", optarg) == 0) {
789 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
791 } else if (strcmp("GENERATE", optarg) == 0) {
792 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
796 printf("Authentication operation specified not supported!\n");
800 /** Parse long options */
802 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
803 struct option *lgopts, int option_index)
805 if (strcmp(lgopts[option_index].name, "no_stats") == 0) {
806 options->no_stats_printing = 1;
810 if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
811 return parse_cryptodev_type(&options->cdev_type, optarg);
813 else if (strcmp(lgopts[option_index].name, "chain") == 0)
814 return parse_crypto_opt_chain(options, optarg);
817 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
818 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
821 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
822 return parse_cipher_op(&options->cipher_xform.cipher.op,
825 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0)
826 return parse_key(&options->cipher_xform.cipher.key,
827 sizeof(options->ckey_data), optarg);
829 else if (strcmp(lgopts[option_index].name, "iv") == 0)
830 return parse_key(&options->iv_key, sizeof(options->ivkey_data),
833 /* Authentication options */
834 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
835 return parse_auth_algo(&options->cipher_xform.auth.algo,
838 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
839 return parse_auth_op(&options->cipher_xform.auth.op,
842 else if (strcmp(lgopts[option_index].name, "auth_key") == 0)
843 return parse_key(&options->auth_xform.auth.key,
844 sizeof(options->akey_data), optarg);
846 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
847 options->sessionless = 1;
854 /** Parse port mask */
856 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
862 /* parse hexadecimal string */
863 pm = strtoul(q_arg, &end, 16);
864 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
867 options->portmask = pm;
868 if (options->portmask == 0) {
869 printf("invalid portmask specified\n");
876 /** Parse number of queues */
878 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
884 /* parse hexadecimal string */
885 n = strtoul(q_arg, &end, 10);
886 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
888 else if (n >= MAX_RX_QUEUE_PER_LCORE)
891 options->nb_ports_per_lcore = n;
892 if (options->nb_ports_per_lcore == 0) {
893 printf("invalid number of ports selected\n");
900 /** Parse timer period */
902 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
908 /* parse number string */
909 n = strtol(q_arg, &end, 10);
910 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
913 if (n >= MAX_TIMER_PERIOD)
916 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
917 if (options->refresh_period == 0) {
918 printf("invalid refresh period specified\n");
925 /** Generate default options for application */
927 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
931 options->portmask = 0xffffffff;
932 options->nb_ports_per_lcore = 1;
933 options->refresh_period = 10000;
934 options->single_lcore = 0;
935 options->no_stats_printing = 0;
937 options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
938 options->sessionless = 0;
939 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
942 options->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
943 options->cipher_xform.next = NULL;
945 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
946 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
948 generate_random_key(options->ckey_data, sizeof(options->ckey_data));
950 options->cipher_xform.cipher.key.data = options->ckey_data;
951 options->cipher_xform.cipher.key.phys_addr = 0;
952 options->cipher_xform.cipher.key.length = 16;
955 /* Authentication Data */
956 options->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
957 options->auth_xform.next = NULL;
959 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
960 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
962 options->auth_xform.auth.add_auth_data_length = 0;
963 options->auth_xform.auth.digest_length = 20;
965 generate_random_key(options->akey_data, sizeof(options->akey_data));
967 options->auth_xform.auth.key.data = options->akey_data;
968 options->auth_xform.auth.key.phys_addr = 0;
969 options->auth_xform.auth.key.length = 20;
973 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
975 printf("Options:-\nn");
976 printf("portmask: %x\n", options->portmask);
977 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
978 printf("refresh period : %u\n", options->refresh_period);
979 printf("single lcore mode: %s\n",
980 options->single_lcore ? "enabled" : "disabled");
981 printf("stats_printing: %s\n",
982 options->no_stats_printing ? "disabled" : "enabled");
984 switch (options->cdev_type) {
985 case RTE_CRYPTODEV_AESNI_MB_PMD:
986 printf("crytpodev type: AES-NI MB PMD\n"); break;
987 case RTE_CRYPTODEV_QAT_PMD:
988 printf("crytpodev type: QAT PMD\n"); break;
993 printf("sessionless crypto: %s\n",
994 options->sessionless ? "enabled" : "disabled");
996 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
999 options->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1000 options->cipher_xform.next = NULL;
1002 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1003 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1005 generate_random_key(options->ckey_data, sizeof(options->ckey_data));
1007 options->cipher_xform.cipher.key.data = options->ckey_data;
1008 options->cipher_xform.cipher.key.phys_addr = 0;
1009 options->cipher_xform.cipher.key.length = 16;
1012 /* Authentication Data */
1013 options->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1014 options->auth_xform.next = NULL;
1016 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1017 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1019 options->auth_xform.auth.add_auth_data_length = 0;
1020 options->auth_xform.auth.digest_length = 20;
1022 generate_random_key(options->akey_data, sizeof(options->akey_data));
1024 options->auth_xform.auth.key.data = options->akey_data;
1025 options->auth_xform.auth.key.phys_addr = 0;
1026 options->auth_xform.auth.key.length = 20;
1030 /* Parse the argument given in the command line of the application */
1032 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1033 int argc, char **argv)
1035 int opt, retval, option_index;
1036 char **argvopt = argv, *prgname = argv[0];
1038 static struct option lgopts[] = {
1039 { "no_stats", no_argument, 0, 0 },
1040 { "sessionless", no_argument, 0, 0 },
1042 { "cdev_type", required_argument, 0, 0 },
1043 { "chain", required_argument, 0, 0 },
1045 { "cipher_algo", required_argument, 0, 0 },
1046 { "cipher_op", required_argument, 0, 0 },
1047 { "cipher_key", required_argument, 0, 0 },
1049 { "auth_algo", required_argument, 0, 0 },
1050 { "auth_op", required_argument, 0, 0 },
1051 { "auth_key", required_argument, 0, 0 },
1053 { "iv", required_argument, 0, 0 },
1055 { "sessionless", no_argument, 0, 0 },
1059 l2fwd_crypto_default_options(options);
1061 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1062 &option_index)) != EOF) {
1066 retval = l2fwd_crypto_parse_args_long_options(options,
1067 lgopts, option_index);
1069 l2fwd_crypto_usage(prgname);
1076 retval = l2fwd_crypto_parse_portmask(options, optarg);
1078 l2fwd_crypto_usage(prgname);
1085 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1087 l2fwd_crypto_usage(prgname);
1094 options->single_lcore = 1;
1100 retval = l2fwd_crypto_parse_timer_period(options,
1103 l2fwd_crypto_usage(prgname);
1109 l2fwd_crypto_usage(prgname);
1116 argv[optind-1] = prgname;
1119 optind = 0; /* reset getopt lib */
1124 /* Check the link status of all ports in up to 9s, and print them finally */
1126 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1128 #define CHECK_INTERVAL 100 /* 100ms */
1129 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1130 uint8_t portid, count, all_ports_up, print_flag = 0;
1131 struct rte_eth_link link;
1133 printf("\nChecking link status");
1135 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1137 for (portid = 0; portid < port_num; portid++) {
1138 if ((port_mask & (1 << portid)) == 0)
1140 memset(&link, 0, sizeof(link));
1141 rte_eth_link_get_nowait(portid, &link);
1142 /* print link status if flag set */
1143 if (print_flag == 1) {
1144 if (link.link_status)
1145 printf("Port %d Link Up - speed %u "
1146 "Mbps - %s\n", (uint8_t)portid,
1147 (unsigned)link.link_speed,
1148 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1149 ("full-duplex") : ("half-duplex\n"));
1151 printf("Port %d Link Down\n",
1155 /* clear all_ports_up flag if any link down */
1156 if (link.link_status == 0) {
1161 /* after finally printing all link status, get out */
1162 if (print_flag == 1)
1165 if (all_ports_up == 0) {
1168 rte_delay_ms(CHECK_INTERVAL);
1171 /* set the print_flag if all ports up or timeout */
1172 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1180 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1182 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1185 if (options->cdev_type == RTE_CRYPTODEV_QAT_PMD) {
1186 if (rte_cryptodev_count() < nb_ports)
1188 } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1189 for (i = 0; i < nb_ports; i++) {
1190 int id = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1197 cdev_count = rte_cryptodev_count();
1199 cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1201 struct rte_cryptodev_qp_conf qp_conf;
1202 struct rte_cryptodev_info dev_info;
1204 struct rte_cryptodev_config conf = {
1205 .nb_queue_pairs = 1,
1206 .socket_id = SOCKET_ID_ANY,
1213 rte_cryptodev_info_get(cdev_id, &dev_info);
1215 if (dev_info.dev_type != options->cdev_type)
1219 retval = rte_cryptodev_configure(cdev_id, &conf);
1221 printf("Failed to configure cryptodev %u", cdev_id);
1225 qp_conf.nb_descriptors = 2048;
1227 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1230 printf("Failed to setup queue pair %u on cryptodev %u",
1235 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1237 enabled_cdev_count++;
1240 return enabled_cdev_count;
1244 initialize_ports(struct l2fwd_crypto_options *options)
1246 uint8_t last_portid, portid;
1247 unsigned enabled_portcount = 0;
1248 unsigned nb_ports = rte_eth_dev_count();
1250 if (nb_ports == 0) {
1251 printf("No Ethernet ports - bye\n");
1255 if (nb_ports > RTE_MAX_ETHPORTS)
1256 nb_ports = RTE_MAX_ETHPORTS;
1258 /* Reset l2fwd_dst_ports */
1259 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1260 l2fwd_dst_ports[portid] = 0;
1262 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1265 /* Skip ports that are not enabled */
1266 if ((options->portmask & (1 << portid)) == 0)
1270 printf("Initializing port %u... ", (unsigned) portid);
1272 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1274 printf("Cannot configure device: err=%d, port=%u\n",
1275 retval, (unsigned) portid);
1279 /* init one RX queue */
1281 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1282 rte_eth_dev_socket_id(portid),
1283 NULL, l2fwd_pktmbuf_pool);
1285 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1286 retval, (unsigned) portid);
1290 /* init one TX queue on each port */
1292 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1293 rte_eth_dev_socket_id(portid),
1296 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1297 retval, (unsigned) portid);
1303 retval = rte_eth_dev_start(portid);
1305 printf("rte_eth_dev_start:err=%d, port=%u\n",
1306 retval, (unsigned) portid);
1310 rte_eth_promiscuous_enable(portid);
1312 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1314 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1316 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1317 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1318 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1319 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1320 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1321 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1323 /* initialize port stats */
1324 memset(&port_statistics, 0, sizeof(port_statistics));
1326 /* Setup port forwarding table */
1327 if (enabled_portcount % 2) {
1328 l2fwd_dst_ports[portid] = last_portid;
1329 l2fwd_dst_ports[last_portid] = portid;
1331 last_portid = portid;
1334 l2fwd_enabled_port_mask |= (1 << portid);
1335 enabled_portcount++;
1338 if (enabled_portcount == 1) {
1339 l2fwd_dst_ports[last_portid] = last_portid;
1340 } else if (enabled_portcount % 2) {
1341 printf("odd number of ports in portmask- bye\n");
1345 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1347 return enabled_portcount;
1351 main(int argc, char **argv)
1353 struct lcore_queue_conf *qconf;
1354 struct l2fwd_crypto_options options;
1356 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1357 unsigned lcore_id, rx_lcore_id;
1358 int ret, enabled_cdevcount, enabled_portcount;
1361 ret = rte_eal_init(argc, argv);
1363 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1367 /* parse application arguments (after the EAL ones) */
1368 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1370 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1372 /* create the mbuf pool */
1373 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 128,
1374 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1375 if (l2fwd_pktmbuf_pool == NULL)
1376 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1378 /* create crypto op pool */
1379 l2fwd_mbuf_ol_pool = rte_pktmbuf_offload_pool_create(
1380 "mbuf_offload_pool", NB_MBUF, 128, 0, rte_socket_id());
1381 if (l2fwd_mbuf_ol_pool == NULL)
1382 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1384 /* Enable Ethernet ports */
1385 enabled_portcount = initialize_ports(&options);
1386 if (enabled_portcount < 1)
1387 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1389 nb_ports = rte_eth_dev_count();
1390 /* Initialize the port/queue configuration of each logical core */
1391 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1392 portid < nb_ports; portid++) {
1394 /* skip ports that are not enabled */
1395 if ((options.portmask & (1 << portid)) == 0)
1398 if (options.single_lcore && qconf == NULL) {
1399 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1401 if (rx_lcore_id >= RTE_MAX_LCORE)
1402 rte_exit(EXIT_FAILURE,
1403 "Not enough cores\n");
1405 } else if (!options.single_lcore) {
1406 /* get the lcore_id for this port */
1407 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1408 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1409 options.nb_ports_per_lcore) {
1411 if (rx_lcore_id >= RTE_MAX_LCORE)
1412 rte_exit(EXIT_FAILURE,
1413 "Not enough cores\n");
1417 /* Assigned a new logical core in the loop above. */
1418 if (qconf != &lcore_queue_conf[rx_lcore_id])
1419 qconf = &lcore_queue_conf[rx_lcore_id];
1421 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1422 qconf->nb_rx_ports++;
1424 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1428 /* Enable Crypto devices */
1429 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1430 if (enabled_cdevcount < 1)
1431 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1433 nb_cryptodevs = rte_cryptodev_count();
1434 /* Initialize the port/queue configuration of each logical core */
1435 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1436 cdev_id < nb_cryptodevs && enabled_cdevcount;
1438 struct rte_cryptodev_info info;
1440 rte_cryptodev_info_get(cdev_id, &info);
1442 /* skip devices of the wrong type */
1443 if (options.cdev_type != info.dev_type)
1446 if (options.single_lcore && qconf == NULL) {
1447 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1449 if (rx_lcore_id >= RTE_MAX_LCORE)
1450 rte_exit(EXIT_FAILURE,
1451 "Not enough cores\n");
1453 } else if (!options.single_lcore) {
1454 /* get the lcore_id for this port */
1455 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1456 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1457 options.nb_ports_per_lcore) {
1459 if (rx_lcore_id >= RTE_MAX_LCORE)
1460 rte_exit(EXIT_FAILURE,
1461 "Not enough cores\n");
1465 /* Assigned a new logical core in the loop above. */
1466 if (qconf != &lcore_queue_conf[rx_lcore_id])
1467 qconf = &lcore_queue_conf[rx_lcore_id];
1469 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1470 qconf->nb_crypto_devs++;
1472 enabled_cdevcount--;
1474 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1480 /* launch per-lcore init on every lcore */
1481 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1483 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1484 if (rte_eal_wait_lcore(lcore_id) < 0)