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_KEY_SIZE 128
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
89 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
90 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
92 /* ethernet addresses of ports */
93 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
95 /* mask of enabled ports */
96 static uint64_t l2fwd_enabled_port_mask;
97 static uint64_t l2fwd_enabled_crypto_mask;
99 /* list of enabled ports */
100 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
105 struct rte_mbuf *buffer[MAX_PKT_BURST];
110 struct rte_crypto_op *buffer[MAX_PKT_BURST];
113 #define MAX_RX_QUEUE_PER_LCORE 16
114 #define MAX_TX_QUEUE_PER_PORT 16
116 enum l2fwd_crypto_xform_chain {
117 L2FWD_CRYPTO_CIPHER_HASH,
118 L2FWD_CRYPTO_HASH_CIPHER,
119 L2FWD_CRYPTO_CIPHER_ONLY,
120 L2FWD_CRYPTO_HASH_ONLY
126 phys_addr_t phys_addr;
129 /** l2fwd crypto application command line options */
130 struct l2fwd_crypto_options {
132 unsigned nb_ports_per_lcore;
133 unsigned refresh_period;
134 unsigned single_lcore:1;
136 enum rte_cryptodev_type cdev_type;
137 unsigned sessionless:1;
139 enum l2fwd_crypto_xform_chain xform_chain;
141 struct rte_crypto_sym_xform cipher_xform;
147 struct rte_crypto_sym_xform auth_xform;
150 struct l2fwd_key aad;
154 /** l2fwd crypto lcore params */
155 struct l2fwd_crypto_params {
159 unsigned digest_length;
162 struct l2fwd_key aad;
163 struct rte_cryptodev_sym_session *session;
169 /** lcore configuration */
170 struct lcore_queue_conf {
171 unsigned nb_rx_ports;
172 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
174 unsigned nb_crypto_devs;
175 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
177 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
178 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
179 } __rte_cache_aligned;
181 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
183 static const struct rte_eth_conf port_conf = {
185 .mq_mode = ETH_MQ_RX_NONE,
186 .max_rx_pkt_len = ETHER_MAX_LEN,
188 .header_split = 0, /**< Header Split disabled */
189 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
190 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
191 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
192 .hw_strip_crc = 0, /**< CRC stripped by hardware */
195 .mq_mode = ETH_MQ_TX_NONE,
199 struct rte_mempool *l2fwd_pktmbuf_pool;
200 struct rte_mempool *l2fwd_crypto_op_pool;
202 /* Per-port statistics struct */
203 struct l2fwd_port_statistics {
207 uint64_t crypto_enqueued;
208 uint64_t crypto_dequeued;
211 } __rte_cache_aligned;
213 struct l2fwd_crypto_statistics {
218 } __rte_cache_aligned;
220 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
221 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
223 /* A tsc-based timer responsible for triggering statistics printout */
224 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
225 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
227 /* default period is 10 seconds */
228 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
230 /* Print out statistics on packets dropped */
234 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
235 uint64_t total_packets_enqueued, total_packets_dequeued,
236 total_packets_errors;
240 total_packets_dropped = 0;
241 total_packets_tx = 0;
242 total_packets_rx = 0;
243 total_packets_enqueued = 0;
244 total_packets_dequeued = 0;
245 total_packets_errors = 0;
247 const char clr[] = { 27, '[', '2', 'J', '\0' };
248 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
250 /* Clear screen and move to top left */
251 printf("%s%s", clr, topLeft);
253 printf("\nPort statistics ====================================");
255 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
256 /* skip disabled ports */
257 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
259 printf("\nStatistics for port %u ------------------------------"
260 "\nPackets sent: %32"PRIu64
261 "\nPackets received: %28"PRIu64
262 "\nPackets dropped: %29"PRIu64,
264 port_statistics[portid].tx,
265 port_statistics[portid].rx,
266 port_statistics[portid].dropped);
268 total_packets_dropped += port_statistics[portid].dropped;
269 total_packets_tx += port_statistics[portid].tx;
270 total_packets_rx += port_statistics[portid].rx;
272 printf("\nCrypto statistics ==================================");
274 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
275 /* skip disabled ports */
276 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
278 printf("\nStatistics for cryptodev %"PRIu64
279 " -------------------------"
280 "\nPackets enqueued: %28"PRIu64
281 "\nPackets dequeued: %28"PRIu64
282 "\nPackets errors: %30"PRIu64,
284 crypto_statistics[cdevid].enqueued,
285 crypto_statistics[cdevid].dequeued,
286 crypto_statistics[cdevid].errors);
288 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
289 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
290 total_packets_errors += crypto_statistics[cdevid].errors;
292 printf("\nAggregate statistics ==============================="
293 "\nTotal packets received: %22"PRIu64
294 "\nTotal packets enqueued: %22"PRIu64
295 "\nTotal packets dequeued: %22"PRIu64
296 "\nTotal packets sent: %26"PRIu64
297 "\nTotal packets dropped: %23"PRIu64
298 "\nTotal packets crypto errors: %17"PRIu64,
300 total_packets_enqueued,
301 total_packets_dequeued,
303 total_packets_dropped,
304 total_packets_errors);
305 printf("\n====================================================\n");
311 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
312 struct l2fwd_crypto_params *cparams)
314 struct rte_crypto_op **op_buffer;
317 op_buffer = (struct rte_crypto_op **)
318 qconf->op_buf[cparams->dev_id].buffer;
320 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
321 cparams->qp_id, op_buffer, (uint16_t) n);
323 crypto_statistics[cparams->dev_id].enqueued += ret;
324 if (unlikely(ret < n)) {
325 crypto_statistics[cparams->dev_id].errors += (n - ret);
327 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
328 rte_crypto_op_free(op_buffer[ret]);
336 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
337 struct l2fwd_crypto_params *cparams)
339 unsigned lcore_id, len;
340 struct lcore_queue_conf *qconf;
342 lcore_id = rte_lcore_id();
344 qconf = &lcore_queue_conf[lcore_id];
345 len = qconf->op_buf[cparams->dev_id].len;
346 qconf->op_buf[cparams->dev_id].buffer[len] = op;
349 /* enough ops to be sent */
350 if (len == MAX_PKT_BURST) {
351 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
355 qconf->op_buf[cparams->dev_id].len = len;
360 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
361 struct rte_crypto_op *op,
362 struct l2fwd_crypto_params *cparams)
364 struct ether_hdr *eth_hdr;
365 struct ipv4_hdr *ip_hdr;
367 unsigned ipdata_offset, pad_len, data_len;
370 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
372 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
375 ipdata_offset = sizeof(struct ether_hdr);
377 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
380 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
381 * IPV4_IHL_MULTIPLIER;
384 /* Zero pad data to be crypto'd so it is block aligned */
385 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
386 pad_len = data_len % cparams->block_size ? cparams->block_size -
387 (data_len % cparams->block_size) : 0;
390 padding = rte_pktmbuf_append(m, pad_len);
391 if (unlikely(!padding))
395 memset(padding, 0, pad_len);
398 /* Set crypto operation data parameters */
399 rte_crypto_op_attach_sym_session(op, cparams->session);
401 if (cparams->do_hash) {
402 /* Append space for digest to end of packet */
403 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
404 cparams->digest_length);
405 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
406 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
407 op->sym->auth.digest.length = cparams->digest_length;
409 op->sym->auth.data.offset = ipdata_offset;
410 op->sym->auth.data.length = data_len;
412 if (cparams->aad.length) {
413 op->sym->auth.aad.data = cparams->aad.data;
414 op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
415 op->sym->auth.aad.length = cparams->aad.length;
419 if (cparams->do_cipher) {
420 op->sym->cipher.iv.data = cparams->iv.data;
421 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
422 op->sym->cipher.iv.length = cparams->iv.length;
424 op->sym->cipher.data.offset = ipdata_offset;
425 op->sym->cipher.data.length = data_len;
430 return l2fwd_crypto_enqueue(op, cparams);
434 /* Send the burst of packets on an output interface */
436 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
439 struct rte_mbuf **pkt_buffer;
442 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
444 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
445 port_statistics[port].tx += ret;
446 if (unlikely(ret < n)) {
447 port_statistics[port].dropped += (n - ret);
449 rte_pktmbuf_free(pkt_buffer[ret]);
456 /* Enqueue packets for TX and prepare them to be sent */
458 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
460 unsigned lcore_id, len;
461 struct lcore_queue_conf *qconf;
463 lcore_id = rte_lcore_id();
465 qconf = &lcore_queue_conf[lcore_id];
466 len = qconf->pkt_buf[port].len;
467 qconf->pkt_buf[port].buffer[len] = m;
470 /* enough pkts to be sent */
471 if (unlikely(len == MAX_PKT_BURST)) {
472 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
476 qconf->pkt_buf[port].len = len;
481 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
483 struct ether_hdr *eth;
487 dst_port = l2fwd_dst_ports[portid];
488 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
490 /* 02:00:00:00:00:xx */
491 tmp = ð->d_addr.addr_bytes[0];
492 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
495 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
497 l2fwd_send_packet(m, (uint8_t) dst_port);
500 /** Generate random key */
502 generate_random_key(uint8_t *key, unsigned length)
506 for (i = 0; i < length; i++)
507 key[i] = rand() % 0xff;
510 static struct rte_cryptodev_sym_session *
511 initialize_crypto_session(struct l2fwd_crypto_options *options,
514 struct rte_crypto_sym_xform *first_xform;
516 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
517 first_xform = &options->cipher_xform;
518 first_xform->next = &options->auth_xform;
519 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
520 first_xform = &options->auth_xform;
521 first_xform->next = &options->cipher_xform;
522 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
523 first_xform = &options->cipher_xform;
525 first_xform = &options->auth_xform;
528 /* Setup Cipher Parameters */
529 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
533 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
535 /* main processing loop */
537 l2fwd_main_loop(struct l2fwd_crypto_options *options)
539 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
540 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
542 unsigned lcore_id = rte_lcore_id();
543 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
544 unsigned i, j, portid, nb_rx;
545 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
546 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
547 US_PER_S * BURST_TX_DRAIN_US;
548 struct l2fwd_crypto_params *cparams;
549 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
551 if (qconf->nb_rx_ports == 0) {
552 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
556 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
558 l2fwd_crypto_options_print(options);
560 for (i = 0; i < qconf->nb_rx_ports; i++) {
562 portid = qconf->rx_port_list[i];
563 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
567 for (i = 0; i < qconf->nb_crypto_devs; i++) {
568 port_cparams[i].do_cipher = 0;
569 port_cparams[i].do_hash = 0;
571 switch (options->xform_chain) {
572 case L2FWD_CRYPTO_CIPHER_HASH:
573 case L2FWD_CRYPTO_HASH_CIPHER:
574 port_cparams[i].do_cipher = 1;
575 port_cparams[i].do_hash = 1;
577 case L2FWD_CRYPTO_HASH_ONLY:
578 port_cparams[i].do_hash = 1;
580 case L2FWD_CRYPTO_CIPHER_ONLY:
581 port_cparams[i].do_cipher = 1;
585 port_cparams[i].dev_id = qconf->cryptodev_list[i];
586 port_cparams[i].qp_id = 0;
588 port_cparams[i].block_size = 64;
590 if (port_cparams[i].do_hash) {
591 port_cparams[i].digest_length = 20;
593 port_cparams[i].aad.length =
594 options->auth_xform.auth.add_auth_data_length;
595 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
596 port_cparams[i].aad.data = options->aad.data;
597 if (!options->aad_param)
598 generate_random_key(options->aad.data,
599 port_cparams[i].aad.length);
602 if (port_cparams[i].do_cipher) {
603 port_cparams[i].iv.length = 16;
604 port_cparams[i].iv.data = options->iv.data;
605 port_cparams[i].iv.phys_addr = options->iv.phys_addr;
606 if (!options->iv_param)
607 generate_random_key(options->iv.data,
608 port_cparams[i].iv.length);
612 port_cparams[i].session = initialize_crypto_session(options,
613 port_cparams[i].dev_id);
615 if (port_cparams[i].session == NULL)
617 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
618 port_cparams[i].dev_id);
623 cur_tsc = rte_rdtsc();
626 * TX burst queue drain
628 diff_tsc = cur_tsc - prev_tsc;
629 if (unlikely(diff_tsc > drain_tsc)) {
631 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
632 if (qconf->pkt_buf[portid].len == 0)
634 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
635 qconf->pkt_buf[portid].len,
637 qconf->pkt_buf[portid].len = 0;
640 /* if timer is enabled */
641 if (timer_period > 0) {
643 /* advance the timer */
644 timer_tsc += diff_tsc;
646 /* if timer has reached its timeout */
647 if (unlikely(timer_tsc >=
648 (uint64_t)timer_period)) {
650 /* do this only on master core */
651 if (lcore_id == rte_get_master_lcore()
652 && options->refresh_period) {
663 * Read packet from RX queues
665 for (i = 0; i < qconf->nb_rx_ports; i++) {
666 portid = qconf->rx_port_list[i];
668 cparams = &port_cparams[i];
670 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
671 pkts_burst, MAX_PKT_BURST);
673 port_statistics[portid].rx += nb_rx;
677 * If we can't allocate a crypto_ops, then drop
678 * the rest of the burst and dequeue and
679 * process the packets to free offload structs
681 if (rte_crypto_op_bulk_alloc(
682 l2fwd_crypto_op_pool,
683 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
686 for (j = 0; j < nb_rx; j++)
687 rte_pktmbuf_free(pkts_burst[i]);
692 /* Enqueue packets from Crypto device*/
693 for (j = 0; j < nb_rx; j++) {
696 l2fwd_simple_crypto_enqueue(m,
697 ops_burst[j], cparams);
701 /* Dequeue packets from Crypto device */
703 nb_rx = rte_cryptodev_dequeue_burst(
704 cparams->dev_id, cparams->qp_id,
705 ops_burst, MAX_PKT_BURST);
707 crypto_statistics[cparams->dev_id].dequeued +=
710 /* Forward crypto'd packets */
711 for (j = 0; j < nb_rx; j++) {
712 m = ops_burst[j]->sym->m_src;
714 rte_crypto_op_free(ops_burst[j]);
715 l2fwd_simple_forward(m, portid);
717 } while (nb_rx == MAX_PKT_BURST);
723 l2fwd_launch_one_lcore(void *arg)
725 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
729 /* Display command line arguments usage */
731 l2fwd_crypto_usage(const char *prgname)
733 printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
734 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
735 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
736 " -s manage all ports from single lcore"
737 " -t PERIOD: statistics will be refreshed each PERIOD seconds"
738 " (0 to disable, 10 default, 86400 maximum)\n"
740 " --cdev AESNI_MB / QAT\n"
741 " --chain HASH_CIPHER / CIPHER_HASH\n"
743 " --cipher_algo ALGO\n"
744 " --cipher_op ENCRYPT / DECRYPT\n"
745 " --cipher_key KEY\n"
748 " --auth_algo ALGO\n"
749 " --auth_op GENERATE / VERIFY\n"
757 /** Parse crypto device type command line argument */
759 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
761 if (strcmp("AESNI_MB", optarg) == 0) {
762 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
764 } else if (strcmp("QAT", optarg) == 0) {
765 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
772 /** Parse crypto chain xform command line argument */
774 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
776 if (strcmp("CIPHER_HASH", optarg) == 0) {
777 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
779 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
780 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
782 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
783 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
785 } else if (strcmp("HASH_ONLY", optarg) == 0) {
786 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
793 /** Parse crypto cipher algo option command line argument */
795 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
797 if (strcmp("AES_CBC", optarg) == 0) {
798 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
800 } else if (strcmp("AES_GCM", optarg) == 0) {
801 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
805 printf("Cipher algorithm not supported!\n");
809 /** Parse crypto cipher operation command line argument */
811 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
813 if (strcmp("ENCRYPT", optarg) == 0) {
814 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
816 } else if (strcmp("DECRYPT", optarg) == 0) {
817 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
821 printf("Cipher operation not supported!\n");
825 /** Parse crypto key command line argument */
827 parse_key(uint8_t *data, char *input_arg)
832 for (byte_count = 0, token = strtok(input_arg, ":");
833 (byte_count < MAX_KEY_SIZE) && (token != NULL);
834 token = strtok(NULL, ":")) {
836 int number = (int)strtol(token, NULL, 16);
838 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
841 data[byte_count++] = (uint8_t)number;
847 /** Parse crypto cipher operation command line argument */
849 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
851 if (strcmp("MD5_HMAC", optarg) == 0) {
852 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
854 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
855 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
857 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
858 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
860 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
861 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
863 } else if (strcmp("SHA384_HMAC", optarg) == 0) {
864 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
866 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
867 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
871 printf("Authentication algorithm specified not supported!\n");
876 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
878 if (strcmp("VERIFY", optarg) == 0) {
879 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
881 } else if (strcmp("GENERATE", optarg) == 0) {
882 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
886 printf("Authentication operation specified not supported!\n");
890 /** Parse long options */
892 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
893 struct option *lgopts, int option_index)
895 if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
896 return parse_cryptodev_type(&options->cdev_type, optarg);
898 else if (strcmp(lgopts[option_index].name, "chain") == 0)
899 return parse_crypto_opt_chain(options, optarg);
902 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
903 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
906 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
907 return parse_cipher_op(&options->cipher_xform.cipher.op,
910 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
911 options->ckey_param = 1;
912 return parse_key(options->cipher_xform.cipher.key.data, optarg);
915 else if (strcmp(lgopts[option_index].name, "iv") == 0) {
916 options->iv_param = 1;
917 return parse_key(options->iv.data, optarg);
920 /* Authentication options */
921 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
922 return parse_auth_algo(&options->auth_xform.auth.algo,
925 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
926 return parse_auth_op(&options->auth_xform.auth.op,
929 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
930 options->akey_param = 1;
931 return parse_key(options->auth_xform.auth.key.data, optarg);
934 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
935 options->aad_param = 1;
936 return parse_key(options->aad.data, optarg);
939 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
940 options->sessionless = 1;
947 /** Parse port mask */
949 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
955 /* parse hexadecimal string */
956 pm = strtoul(q_arg, &end, 16);
957 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
960 options->portmask = pm;
961 if (options->portmask == 0) {
962 printf("invalid portmask specified\n");
969 /** Parse number of queues */
971 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
977 /* parse hexadecimal string */
978 n = strtoul(q_arg, &end, 10);
979 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
981 else if (n >= MAX_RX_QUEUE_PER_LCORE)
984 options->nb_ports_per_lcore = n;
985 if (options->nb_ports_per_lcore == 0) {
986 printf("invalid number of ports selected\n");
993 /** Parse timer period */
995 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1001 /* parse number string */
1002 n = (unsigned)strtol(q_arg, &end, 10);
1003 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1006 if (n >= MAX_TIMER_PERIOD) {
1007 printf("Warning refresh period specified %lu is greater than "
1008 "max value %lu! using max value",
1009 n, MAX_TIMER_PERIOD);
1010 n = MAX_TIMER_PERIOD;
1013 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1018 /** Generate default options for application */
1020 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1024 options->portmask = 0xffffffff;
1025 options->nb_ports_per_lcore = 1;
1026 options->refresh_period = 10000;
1027 options->single_lcore = 0;
1028 options->sessionless = 0;
1030 options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
1031 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1034 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1035 options->cipher_xform.next = NULL;
1036 options->ckey_param = 0;
1037 options->iv_param = 0;
1039 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1040 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1042 options->cipher_xform.cipher.key.length = 16;
1045 /* Authentication Data */
1046 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1047 options->auth_xform.next = NULL;
1048 options->akey_param = 0;
1049 options->aad_param = 0;
1051 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1052 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1054 options->auth_xform.auth.add_auth_data_length = 0;
1055 options->auth_xform.auth.digest_length = 20;
1057 options->auth_xform.auth.key.length = 20;
1061 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1063 printf("Options:-\nn");
1064 printf("portmask: %x\n", options->portmask);
1065 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1066 printf("refresh period : %u\n", options->refresh_period);
1067 printf("single lcore mode: %s\n",
1068 options->single_lcore ? "enabled" : "disabled");
1069 printf("stats_printing: %s\n",
1070 options->refresh_period == 0 ? "disabled" : "enabled");
1072 switch (options->cdev_type) {
1073 case RTE_CRYPTODEV_AESNI_MB_PMD:
1074 printf("cryptodev type: AES-NI MB PMD\n"); break;
1075 case RTE_CRYPTODEV_QAT_SYM_PMD:
1076 printf("cryptodev type: QAT PMD\n"); break;
1081 printf("sessionless crypto: %s\n",
1082 options->sessionless ? "enabled" : "disabled");
1085 /* Parse the argument given in the command line of the application */
1087 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1088 int argc, char **argv)
1090 int opt, retval, option_index;
1091 char **argvopt = argv, *prgname = argv[0];
1093 static struct option lgopts[] = {
1094 { "sessionless", no_argument, 0, 0 },
1096 { "cdev_type", required_argument, 0, 0 },
1097 { "chain", required_argument, 0, 0 },
1099 { "cipher_algo", required_argument, 0, 0 },
1100 { "cipher_op", required_argument, 0, 0 },
1101 { "cipher_key", required_argument, 0, 0 },
1103 { "auth_algo", required_argument, 0, 0 },
1104 { "auth_op", required_argument, 0, 0 },
1105 { "auth_key", required_argument, 0, 0 },
1107 { "iv", required_argument, 0, 0 },
1108 { "aad", required_argument, 0, 0 },
1110 { "sessionless", no_argument, 0, 0 },
1115 l2fwd_crypto_default_options(options);
1117 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1118 &option_index)) != EOF) {
1122 retval = l2fwd_crypto_parse_args_long_options(options,
1123 lgopts, option_index);
1125 l2fwd_crypto_usage(prgname);
1132 retval = l2fwd_crypto_parse_portmask(options, optarg);
1134 l2fwd_crypto_usage(prgname);
1141 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1143 l2fwd_crypto_usage(prgname);
1150 options->single_lcore = 1;
1156 retval = l2fwd_crypto_parse_timer_period(options,
1159 l2fwd_crypto_usage(prgname);
1165 l2fwd_crypto_usage(prgname);
1172 argv[optind-1] = prgname;
1175 optind = 0; /* reset getopt lib */
1180 /* Check the link status of all ports in up to 9s, and print them finally */
1182 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1184 #define CHECK_INTERVAL 100 /* 100ms */
1185 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1186 uint8_t portid, count, all_ports_up, print_flag = 0;
1187 struct rte_eth_link link;
1189 printf("\nChecking link status");
1191 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1193 for (portid = 0; portid < port_num; portid++) {
1194 if ((port_mask & (1 << portid)) == 0)
1196 memset(&link, 0, sizeof(link));
1197 rte_eth_link_get_nowait(portid, &link);
1198 /* print link status if flag set */
1199 if (print_flag == 1) {
1200 if (link.link_status)
1201 printf("Port %d Link Up - speed %u "
1202 "Mbps - %s\n", (uint8_t)portid,
1203 (unsigned)link.link_speed,
1204 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1205 ("full-duplex") : ("half-duplex\n"));
1207 printf("Port %d Link Down\n",
1211 /* clear all_ports_up flag if any link down */
1212 if (link.link_status == 0) {
1217 /* after finally printing all link status, get out */
1218 if (print_flag == 1)
1221 if (all_ports_up == 0) {
1224 rte_delay_ms(CHECK_INTERVAL);
1227 /* set the print_flag if all ports up or timeout */
1228 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1236 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1238 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1241 if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1242 if (rte_cryptodev_count() < nb_ports)
1244 } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1245 for (i = 0; i < nb_ports; i++) {
1246 int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1253 cdev_count = rte_cryptodev_count();
1255 cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1257 struct rte_cryptodev_qp_conf qp_conf;
1258 struct rte_cryptodev_info dev_info;
1260 struct rte_cryptodev_config conf = {
1261 .nb_queue_pairs = 1,
1262 .socket_id = SOCKET_ID_ANY,
1269 rte_cryptodev_info_get(cdev_id, &dev_info);
1271 if (dev_info.dev_type != options->cdev_type)
1275 retval = rte_cryptodev_configure(cdev_id, &conf);
1277 printf("Failed to configure cryptodev %u", cdev_id);
1281 qp_conf.nb_descriptors = 2048;
1283 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1286 printf("Failed to setup queue pair %u on cryptodev %u",
1291 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1293 enabled_cdev_count++;
1296 return enabled_cdev_count;
1300 initialize_ports(struct l2fwd_crypto_options *options)
1302 uint8_t last_portid, portid;
1303 unsigned enabled_portcount = 0;
1304 unsigned nb_ports = rte_eth_dev_count();
1306 if (nb_ports == 0) {
1307 printf("No Ethernet ports - bye\n");
1311 if (nb_ports > RTE_MAX_ETHPORTS)
1312 nb_ports = RTE_MAX_ETHPORTS;
1314 /* Reset l2fwd_dst_ports */
1315 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1316 l2fwd_dst_ports[portid] = 0;
1318 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1321 /* Skip ports that are not enabled */
1322 if ((options->portmask & (1 << portid)) == 0)
1326 printf("Initializing port %u... ", (unsigned) portid);
1328 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1330 printf("Cannot configure device: err=%d, port=%u\n",
1331 retval, (unsigned) portid);
1335 /* init one RX queue */
1337 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1338 rte_eth_dev_socket_id(portid),
1339 NULL, l2fwd_pktmbuf_pool);
1341 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1342 retval, (unsigned) portid);
1346 /* init one TX queue on each port */
1348 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1349 rte_eth_dev_socket_id(portid),
1352 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1353 retval, (unsigned) portid);
1359 retval = rte_eth_dev_start(portid);
1361 printf("rte_eth_dev_start:err=%d, port=%u\n",
1362 retval, (unsigned) portid);
1366 rte_eth_promiscuous_enable(portid);
1368 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1370 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1372 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1373 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1374 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1375 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1376 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1377 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1379 /* initialize port stats */
1380 memset(&port_statistics, 0, sizeof(port_statistics));
1382 /* Setup port forwarding table */
1383 if (enabled_portcount % 2) {
1384 l2fwd_dst_ports[portid] = last_portid;
1385 l2fwd_dst_ports[last_portid] = portid;
1387 last_portid = portid;
1390 l2fwd_enabled_port_mask |= (1 << portid);
1391 enabled_portcount++;
1394 if (enabled_portcount == 1) {
1395 l2fwd_dst_ports[last_portid] = last_portid;
1396 } else if (enabled_portcount % 2) {
1397 printf("odd number of ports in portmask- bye\n");
1401 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1403 return enabled_portcount;
1407 reserve_key_memory(struct l2fwd_crypto_options *options)
1409 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1411 if (options->cipher_xform.cipher.key.data == NULL)
1412 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1415 options->auth_xform.auth.key.data = rte_malloc("auth key",
1417 if (options->auth_xform.auth.key.data == NULL)
1418 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1420 options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1421 if (options->iv.data == NULL)
1422 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1423 options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1425 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1426 if (options->aad.data == NULL)
1427 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1428 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1432 main(int argc, char **argv)
1434 struct lcore_queue_conf *qconf;
1435 struct l2fwd_crypto_options options;
1437 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1438 unsigned lcore_id, rx_lcore_id;
1439 int ret, enabled_cdevcount, enabled_portcount;
1442 ret = rte_eal_init(argc, argv);
1444 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1448 /* reserve memory for Cipher/Auth key and IV */
1449 reserve_key_memory(&options);
1451 /* parse application arguments (after the EAL ones) */
1452 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1454 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1456 /* create the mbuf pool */
1457 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1458 sizeof(struct rte_crypto_op),
1459 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1460 if (l2fwd_pktmbuf_pool == NULL)
1461 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1463 /* create crypto op pool */
1464 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1465 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1467 if (l2fwd_crypto_op_pool == NULL)
1468 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1470 /* Enable Ethernet ports */
1471 enabled_portcount = initialize_ports(&options);
1472 if (enabled_portcount < 1)
1473 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1475 nb_ports = rte_eth_dev_count();
1476 /* Initialize the port/queue configuration of each logical core */
1477 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1478 portid < nb_ports; portid++) {
1480 /* skip ports that are not enabled */
1481 if ((options.portmask & (1 << portid)) == 0)
1484 if (options.single_lcore && qconf == NULL) {
1485 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1487 if (rx_lcore_id >= RTE_MAX_LCORE)
1488 rte_exit(EXIT_FAILURE,
1489 "Not enough cores\n");
1491 } else if (!options.single_lcore) {
1492 /* get the lcore_id for this port */
1493 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1494 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1495 options.nb_ports_per_lcore) {
1497 if (rx_lcore_id >= RTE_MAX_LCORE)
1498 rte_exit(EXIT_FAILURE,
1499 "Not enough cores\n");
1503 /* Assigned a new logical core in the loop above. */
1504 if (qconf != &lcore_queue_conf[rx_lcore_id])
1505 qconf = &lcore_queue_conf[rx_lcore_id];
1507 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1508 qconf->nb_rx_ports++;
1510 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1514 /* Enable Crypto devices */
1515 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1516 if (enabled_cdevcount < 1)
1517 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1519 nb_cryptodevs = rte_cryptodev_count();
1520 /* Initialize the port/queue configuration of each logical core */
1521 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1522 cdev_id < nb_cryptodevs && enabled_cdevcount;
1524 struct rte_cryptodev_info info;
1526 rte_cryptodev_info_get(cdev_id, &info);
1528 /* skip devices of the wrong type */
1529 if (options.cdev_type != info.dev_type)
1532 if (options.single_lcore && qconf == NULL) {
1533 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1535 if (rx_lcore_id >= RTE_MAX_LCORE)
1536 rte_exit(EXIT_FAILURE,
1537 "Not enough cores\n");
1539 } else if (!options.single_lcore) {
1540 /* get the lcore_id for this port */
1541 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1542 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1543 options.nb_ports_per_lcore) {
1545 if (rx_lcore_id >= RTE_MAX_LCORE)
1546 rte_exit(EXIT_FAILURE,
1547 "Not enough cores\n");
1551 /* Assigned a new logical core in the loop above. */
1552 if (qconf != &lcore_queue_conf[rx_lcore_id])
1553 qconf = &lcore_queue_conf[rx_lcore_id];
1555 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1556 qconf->nb_crypto_devs++;
1558 enabled_cdevcount--;
1560 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1564 if (!options.akey_param)
1565 generate_random_key(options.auth_xform.auth.key.data,
1566 options.auth_xform.auth.key.length);
1568 if (!options.ckey_param)
1569 generate_random_key(options.cipher_xform.cipher.key.data,
1570 options.cipher_xform.cipher.key.length);
1573 /* launch per-lcore init on every lcore */
1574 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1576 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1577 if (rte_eal_wait_lcore(lcore_id) < 0)