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
124 phys_addr_t phys_addr;
127 /** l2fwd crypto application command line options */
128 struct l2fwd_crypto_options {
130 unsigned nb_ports_per_lcore;
131 unsigned refresh_period;
132 unsigned single_lcore:1;
134 enum rte_cryptodev_type cdev_type;
135 unsigned sessionless:1;
137 enum l2fwd_crypto_xform_chain xform_chain;
139 struct rte_crypto_sym_xform cipher_xform;
145 struct rte_crypto_sym_xform auth_xform;
148 struct l2fwd_key aad;
152 /** l2fwd crypto lcore params */
153 struct l2fwd_crypto_params {
157 unsigned digest_length;
160 struct l2fwd_key aad;
161 struct rte_cryptodev_sym_session *session;
164 /** lcore configuration */
165 struct lcore_queue_conf {
166 unsigned nb_rx_ports;
167 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
169 unsigned nb_crypto_devs;
170 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
172 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
173 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
174 } __rte_cache_aligned;
176 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
178 static const struct rte_eth_conf port_conf = {
180 .mq_mode = ETH_MQ_RX_NONE,
181 .max_rx_pkt_len = ETHER_MAX_LEN,
183 .header_split = 0, /**< Header Split disabled */
184 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
185 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
186 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
187 .hw_strip_crc = 0, /**< CRC stripped by hardware */
190 .mq_mode = ETH_MQ_TX_NONE,
194 struct rte_mempool *l2fwd_pktmbuf_pool;
195 struct rte_mempool *l2fwd_crypto_op_pool;
197 /* Per-port statistics struct */
198 struct l2fwd_port_statistics {
202 uint64_t crypto_enqueued;
203 uint64_t crypto_dequeued;
206 } __rte_cache_aligned;
208 struct l2fwd_crypto_statistics {
213 } __rte_cache_aligned;
215 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
216 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
218 /* A tsc-based timer responsible for triggering statistics printout */
219 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
220 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
222 /* default period is 10 seconds */
223 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
225 /* Print out statistics on packets dropped */
229 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
230 uint64_t total_packets_enqueued, total_packets_dequeued,
231 total_packets_errors;
235 total_packets_dropped = 0;
236 total_packets_tx = 0;
237 total_packets_rx = 0;
238 total_packets_enqueued = 0;
239 total_packets_dequeued = 0;
240 total_packets_errors = 0;
242 const char clr[] = { 27, '[', '2', 'J', '\0' };
243 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
245 /* Clear screen and move to top left */
246 printf("%s%s", clr, topLeft);
248 printf("\nPort statistics ====================================");
250 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
251 /* skip disabled ports */
252 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
254 printf("\nStatistics for port %u ------------------------------"
255 "\nPackets sent: %32"PRIu64
256 "\nPackets received: %28"PRIu64
257 "\nPackets dropped: %29"PRIu64,
259 port_statistics[portid].tx,
260 port_statistics[portid].rx,
261 port_statistics[portid].dropped);
263 total_packets_dropped += port_statistics[portid].dropped;
264 total_packets_tx += port_statistics[portid].tx;
265 total_packets_rx += port_statistics[portid].rx;
267 printf("\nCrypto statistics ==================================");
269 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
270 /* skip disabled ports */
271 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
273 printf("\nStatistics for cryptodev %"PRIu64
274 " -------------------------"
275 "\nPackets enqueued: %28"PRIu64
276 "\nPackets dequeued: %28"PRIu64
277 "\nPackets errors: %30"PRIu64,
279 crypto_statistics[cdevid].enqueued,
280 crypto_statistics[cdevid].dequeued,
281 crypto_statistics[cdevid].errors);
283 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
284 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
285 total_packets_errors += crypto_statistics[cdevid].errors;
287 printf("\nAggregate statistics ==============================="
288 "\nTotal packets received: %22"PRIu64
289 "\nTotal packets enqueued: %22"PRIu64
290 "\nTotal packets dequeued: %22"PRIu64
291 "\nTotal packets sent: %26"PRIu64
292 "\nTotal packets dropped: %23"PRIu64
293 "\nTotal packets crypto errors: %17"PRIu64,
295 total_packets_enqueued,
296 total_packets_dequeued,
298 total_packets_dropped,
299 total_packets_errors);
300 printf("\n====================================================\n");
306 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
307 struct l2fwd_crypto_params *cparams)
309 struct rte_crypto_op **op_buffer;
312 op_buffer = (struct rte_crypto_op **)
313 qconf->op_buf[cparams->dev_id].buffer;
315 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
316 cparams->qp_id, op_buffer, (uint16_t) n);
318 crypto_statistics[cparams->dev_id].enqueued += ret;
319 if (unlikely(ret < n)) {
320 crypto_statistics[cparams->dev_id].errors += (n - ret);
322 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
323 rte_crypto_op_free(op_buffer[ret]);
331 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
332 struct l2fwd_crypto_params *cparams)
334 unsigned lcore_id, len;
335 struct lcore_queue_conf *qconf;
337 lcore_id = rte_lcore_id();
339 qconf = &lcore_queue_conf[lcore_id];
340 len = qconf->op_buf[cparams->dev_id].len;
341 qconf->op_buf[cparams->dev_id].buffer[len] = op;
344 /* enough ops to be sent */
345 if (len == MAX_PKT_BURST) {
346 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
350 qconf->op_buf[cparams->dev_id].len = len;
355 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
356 struct rte_crypto_op *op,
357 struct l2fwd_crypto_params *cparams)
359 struct ether_hdr *eth_hdr;
360 struct ipv4_hdr *ip_hdr;
362 unsigned ipdata_offset, pad_len, data_len;
365 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
367 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
370 ipdata_offset = sizeof(struct ether_hdr);
372 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
375 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
376 * IPV4_IHL_MULTIPLIER;
379 /* Zero pad data to be crypto'd so it is block aligned */
380 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
381 pad_len = data_len % cparams->block_size ? cparams->block_size -
382 (data_len % cparams->block_size) : 0;
385 padding = rte_pktmbuf_append(m, pad_len);
386 if (unlikely(!padding))
390 memset(padding, 0, pad_len);
393 /* Set crypto operation data parameters */
394 rte_crypto_op_attach_sym_session(op, cparams->session);
396 /* Append space for digest to end of packet */
397 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
398 cparams->digest_length);
399 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
400 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
401 op->sym->auth.digest.length = cparams->digest_length;
403 op->sym->auth.data.offset = ipdata_offset;
404 op->sym->auth.data.length = data_len;
407 op->sym->cipher.iv.data = cparams->iv.data;
408 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
409 op->sym->cipher.iv.length = cparams->iv.length;
411 if (cparams->aad.length) {
412 op->sym->auth.aad.data = cparams->aad.data;
413 op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
414 op->sym->auth.aad.length = cparams->aad.length;
417 op->sym->cipher.data.offset = ipdata_offset;
418 op->sym->cipher.data.length = data_len;
422 return l2fwd_crypto_enqueue(op, cparams);
426 /* Send the burst of packets on an output interface */
428 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
431 struct rte_mbuf **pkt_buffer;
434 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
436 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
437 port_statistics[port].tx += ret;
438 if (unlikely(ret < n)) {
439 port_statistics[port].dropped += (n - ret);
441 rte_pktmbuf_free(pkt_buffer[ret]);
448 /* Enqueue packets for TX and prepare them to be sent */
450 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
452 unsigned lcore_id, len;
453 struct lcore_queue_conf *qconf;
455 lcore_id = rte_lcore_id();
457 qconf = &lcore_queue_conf[lcore_id];
458 len = qconf->pkt_buf[port].len;
459 qconf->pkt_buf[port].buffer[len] = m;
462 /* enough pkts to be sent */
463 if (unlikely(len == MAX_PKT_BURST)) {
464 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
468 qconf->pkt_buf[port].len = len;
473 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
475 struct ether_hdr *eth;
479 dst_port = l2fwd_dst_ports[portid];
480 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
482 /* 02:00:00:00:00:xx */
483 tmp = ð->d_addr.addr_bytes[0];
484 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
487 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
489 l2fwd_send_packet(m, (uint8_t) dst_port);
492 /** Generate random key */
494 generate_random_key(uint8_t *key, unsigned length)
498 for (i = 0; i < length; i++)
499 key[i] = rand() % 0xff;
502 static struct rte_cryptodev_sym_session *
503 initialize_crypto_session(struct l2fwd_crypto_options *options,
506 struct rte_crypto_sym_xform *first_xform;
508 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
509 first_xform = &options->cipher_xform;
510 first_xform->next = &options->auth_xform;
512 first_xform = &options->auth_xform;
513 first_xform->next = &options->cipher_xform;
516 /* Setup Cipher Parameters */
517 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
521 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
523 /* main processing loop */
525 l2fwd_main_loop(struct l2fwd_crypto_options *options)
527 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
528 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
530 unsigned lcore_id = rte_lcore_id();
531 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
532 unsigned i, j, portid, nb_rx;
533 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
534 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
535 US_PER_S * BURST_TX_DRAIN_US;
536 struct l2fwd_crypto_params *cparams;
537 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
539 if (qconf->nb_rx_ports == 0) {
540 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
544 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
546 l2fwd_crypto_options_print(options);
548 for (i = 0; i < qconf->nb_rx_ports; i++) {
550 portid = qconf->rx_port_list[i];
551 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
555 for (i = 0; i < qconf->nb_crypto_devs; i++) {
556 port_cparams[i].dev_id = qconf->cryptodev_list[i];
557 port_cparams[i].qp_id = 0;
559 port_cparams[i].block_size = 64;
560 port_cparams[i].digest_length = 20;
562 port_cparams[i].iv.length = 16;
563 port_cparams[i].iv.data = options->iv.data;
564 port_cparams[i].iv.phys_addr = options->iv.phys_addr;
565 if (!options->iv_param)
566 generate_random_key(options->iv.data,
567 port_cparams[i].iv.length);
569 port_cparams[i].aad.length =
570 options->auth_xform.auth.add_auth_data_length;
571 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
572 port_cparams[i].aad.data = options->aad.data;
573 if (!options->aad_param)
574 generate_random_key(options->aad.data,
575 port_cparams[i].aad.length);
578 port_cparams[i].session = initialize_crypto_session(options,
579 port_cparams[i].dev_id);
581 if (port_cparams[i].session == NULL)
583 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
584 port_cparams[i].dev_id);
589 cur_tsc = rte_rdtsc();
592 * TX burst queue drain
594 diff_tsc = cur_tsc - prev_tsc;
595 if (unlikely(diff_tsc > drain_tsc)) {
597 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
598 if (qconf->pkt_buf[portid].len == 0)
600 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
601 qconf->pkt_buf[portid].len,
603 qconf->pkt_buf[portid].len = 0;
606 /* if timer is enabled */
607 if (timer_period > 0) {
609 /* advance the timer */
610 timer_tsc += diff_tsc;
612 /* if timer has reached its timeout */
613 if (unlikely(timer_tsc >=
614 (uint64_t)timer_period)) {
616 /* do this only on master core */
617 if (lcore_id == rte_get_master_lcore()
618 && options->refresh_period) {
629 * Read packet from RX queues
631 for (i = 0; i < qconf->nb_rx_ports; i++) {
632 portid = qconf->rx_port_list[i];
634 cparams = &port_cparams[i];
636 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
637 pkts_burst, MAX_PKT_BURST);
639 port_statistics[portid].rx += nb_rx;
643 * If we can't allocate a crypto_ops, then drop
644 * the rest of the burst and dequeue and
645 * process the packets to free offload structs
647 if (rte_crypto_op_bulk_alloc(
648 l2fwd_crypto_op_pool,
649 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
652 for (j = 0; j < nb_rx; j++)
653 rte_pktmbuf_free(pkts_burst[i]);
658 /* Enqueue packets from Crypto device*/
659 for (j = 0; j < nb_rx; j++) {
662 l2fwd_simple_crypto_enqueue(m,
663 ops_burst[j], cparams);
667 /* Dequeue packets from Crypto device */
669 nb_rx = rte_cryptodev_dequeue_burst(
670 cparams->dev_id, cparams->qp_id,
671 ops_burst, MAX_PKT_BURST);
673 crypto_statistics[cparams->dev_id].dequeued +=
676 /* Forward crypto'd packets */
677 for (j = 0; j < nb_rx; j++) {
678 m = ops_burst[j]->sym->m_src;
680 rte_crypto_op_free(ops_burst[j]);
681 l2fwd_simple_forward(m, portid);
683 } while (nb_rx == MAX_PKT_BURST);
689 l2fwd_launch_one_lcore(void *arg)
691 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
695 /* Display command line arguments usage */
697 l2fwd_crypto_usage(const char *prgname)
699 printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
700 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
701 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
702 " -s manage all ports from single lcore"
703 " -t PERIOD: statistics will be refreshed each PERIOD seconds"
704 " (0 to disable, 10 default, 86400 maximum)\n"
706 " --cdev AESNI_MB / QAT\n"
707 " --chain HASH_CIPHER / CIPHER_HASH\n"
709 " --cipher_algo ALGO\n"
710 " --cipher_op ENCRYPT / DECRYPT\n"
711 " --cipher_key KEY\n"
714 " --auth_algo ALGO\n"
715 " --auth_op GENERATE / VERIFY\n"
723 /** Parse crypto device type command line argument */
725 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
727 if (strcmp("AESNI_MB", optarg) == 0) {
728 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
730 } else if (strcmp("QAT", optarg) == 0) {
731 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
738 /** Parse crypto chain xform command line argument */
740 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
742 if (strcmp("CIPHER_HASH", optarg) == 0) {
743 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
745 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
746 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
753 /** Parse crypto cipher algo option command line argument */
755 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
757 if (strcmp("AES_CBC", optarg) == 0) {
758 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
760 } else if (strcmp("AES_GCM", optarg) == 0) {
761 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
765 printf("Cipher algorithm not supported!\n");
769 /** Parse crypto cipher operation command line argument */
771 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
773 if (strcmp("ENCRYPT", optarg) == 0) {
774 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
776 } else if (strcmp("DECRYPT", optarg) == 0) {
777 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
781 printf("Cipher operation not supported!\n");
785 /** Parse crypto key command line argument */
787 parse_key(uint8_t *data, char *input_arg)
792 for (byte_count = 0, token = strtok(input_arg, ":");
793 (byte_count < MAX_KEY_SIZE) && (token != NULL);
794 token = strtok(NULL, ":")) {
796 int number = (int)strtol(token, NULL, 16);
798 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
801 data[byte_count++] = (uint8_t)number;
807 /** Parse crypto cipher operation command line argument */
809 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
811 if (strcmp("MD5_HMAC", optarg) == 0) {
812 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
814 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
815 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
817 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
818 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
820 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
821 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
823 } else if (strcmp("SHA384_HMAC", optarg) == 0) {
824 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
826 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
827 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
831 printf("Authentication algorithm specified not supported!\n");
836 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
838 if (strcmp("VERIFY", optarg) == 0) {
839 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
841 } else if (strcmp("GENERATE", optarg) == 0) {
842 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
846 printf("Authentication operation specified not supported!\n");
850 /** Parse long options */
852 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
853 struct option *lgopts, int option_index)
855 if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
856 return parse_cryptodev_type(&options->cdev_type, optarg);
858 else if (strcmp(lgopts[option_index].name, "chain") == 0)
859 return parse_crypto_opt_chain(options, optarg);
862 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
863 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
866 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
867 return parse_cipher_op(&options->cipher_xform.cipher.op,
870 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
871 options->ckey_param = 1;
872 return parse_key(options->cipher_xform.cipher.key.data, optarg);
875 else if (strcmp(lgopts[option_index].name, "iv") == 0) {
876 options->iv_param = 1;
877 return parse_key(options->iv.data, optarg);
880 /* Authentication options */
881 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
882 return parse_auth_algo(&options->auth_xform.auth.algo,
885 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
886 return parse_auth_op(&options->auth_xform.auth.op,
889 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
890 options->akey_param = 1;
891 return parse_key(options->auth_xform.auth.key.data, optarg);
894 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
895 options->aad_param = 1;
896 return parse_key(options->aad.data, optarg);
899 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
900 options->sessionless = 1;
907 /** Parse port mask */
909 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
915 /* parse hexadecimal string */
916 pm = strtoul(q_arg, &end, 16);
917 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
920 options->portmask = pm;
921 if (options->portmask == 0) {
922 printf("invalid portmask specified\n");
929 /** Parse number of queues */
931 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
937 /* parse hexadecimal string */
938 n = strtoul(q_arg, &end, 10);
939 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
941 else if (n >= MAX_RX_QUEUE_PER_LCORE)
944 options->nb_ports_per_lcore = n;
945 if (options->nb_ports_per_lcore == 0) {
946 printf("invalid number of ports selected\n");
953 /** Parse timer period */
955 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
961 /* parse number string */
962 n = (unsigned)strtol(q_arg, &end, 10);
963 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
966 if (n >= MAX_TIMER_PERIOD) {
967 printf("Warning refresh period specified %lu is greater than "
968 "max value %lu! using max value",
969 n, MAX_TIMER_PERIOD);
970 n = MAX_TIMER_PERIOD;
973 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
978 /** Generate default options for application */
980 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
984 options->portmask = 0xffffffff;
985 options->nb_ports_per_lcore = 1;
986 options->refresh_period = 10000;
987 options->single_lcore = 0;
988 options->sessionless = 0;
990 options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
991 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
994 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
995 options->cipher_xform.next = NULL;
996 options->ckey_param = 0;
997 options->iv_param = 0;
999 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1000 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1002 options->cipher_xform.cipher.key.length = 16;
1005 /* Authentication Data */
1006 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1007 options->auth_xform.next = NULL;
1008 options->akey_param = 0;
1009 options->aad_param = 0;
1011 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1012 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1014 options->auth_xform.auth.add_auth_data_length = 0;
1015 options->auth_xform.auth.digest_length = 20;
1017 options->auth_xform.auth.key.length = 20;
1021 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1023 printf("Options:-\nn");
1024 printf("portmask: %x\n", options->portmask);
1025 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1026 printf("refresh period : %u\n", options->refresh_period);
1027 printf("single lcore mode: %s\n",
1028 options->single_lcore ? "enabled" : "disabled");
1029 printf("stats_printing: %s\n",
1030 options->refresh_period == 0 ? "disabled" : "enabled");
1032 switch (options->cdev_type) {
1033 case RTE_CRYPTODEV_AESNI_MB_PMD:
1034 printf("cryptodev type: AES-NI MB PMD\n"); break;
1035 case RTE_CRYPTODEV_QAT_SYM_PMD:
1036 printf("cryptodev type: QAT PMD\n"); break;
1041 printf("sessionless crypto: %s\n",
1042 options->sessionless ? "enabled" : "disabled");
1045 /* Parse the argument given in the command line of the application */
1047 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1048 int argc, char **argv)
1050 int opt, retval, option_index;
1051 char **argvopt = argv, *prgname = argv[0];
1053 static struct option lgopts[] = {
1054 { "sessionless", no_argument, 0, 0 },
1056 { "cdev_type", required_argument, 0, 0 },
1057 { "chain", required_argument, 0, 0 },
1059 { "cipher_algo", required_argument, 0, 0 },
1060 { "cipher_op", required_argument, 0, 0 },
1061 { "cipher_key", required_argument, 0, 0 },
1063 { "auth_algo", required_argument, 0, 0 },
1064 { "auth_op", required_argument, 0, 0 },
1065 { "auth_key", required_argument, 0, 0 },
1067 { "iv", required_argument, 0, 0 },
1068 { "aad", required_argument, 0, 0 },
1070 { "sessionless", no_argument, 0, 0 },
1075 l2fwd_crypto_default_options(options);
1077 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1078 &option_index)) != EOF) {
1082 retval = l2fwd_crypto_parse_args_long_options(options,
1083 lgopts, option_index);
1085 l2fwd_crypto_usage(prgname);
1092 retval = l2fwd_crypto_parse_portmask(options, optarg);
1094 l2fwd_crypto_usage(prgname);
1101 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1103 l2fwd_crypto_usage(prgname);
1110 options->single_lcore = 1;
1116 retval = l2fwd_crypto_parse_timer_period(options,
1119 l2fwd_crypto_usage(prgname);
1125 l2fwd_crypto_usage(prgname);
1132 argv[optind-1] = prgname;
1135 optind = 0; /* reset getopt lib */
1140 /* Check the link status of all ports in up to 9s, and print them finally */
1142 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1144 #define CHECK_INTERVAL 100 /* 100ms */
1145 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1146 uint8_t portid, count, all_ports_up, print_flag = 0;
1147 struct rte_eth_link link;
1149 printf("\nChecking link status");
1151 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1153 for (portid = 0; portid < port_num; portid++) {
1154 if ((port_mask & (1 << portid)) == 0)
1156 memset(&link, 0, sizeof(link));
1157 rte_eth_link_get_nowait(portid, &link);
1158 /* print link status if flag set */
1159 if (print_flag == 1) {
1160 if (link.link_status)
1161 printf("Port %d Link Up - speed %u "
1162 "Mbps - %s\n", (uint8_t)portid,
1163 (unsigned)link.link_speed,
1164 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1165 ("full-duplex") : ("half-duplex\n"));
1167 printf("Port %d Link Down\n",
1171 /* clear all_ports_up flag if any link down */
1172 if (link.link_status == 0) {
1177 /* after finally printing all link status, get out */
1178 if (print_flag == 1)
1181 if (all_ports_up == 0) {
1184 rte_delay_ms(CHECK_INTERVAL);
1187 /* set the print_flag if all ports up or timeout */
1188 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1196 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1198 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1201 if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1202 if (rte_cryptodev_count() < nb_ports)
1204 } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1205 for (i = 0; i < nb_ports; i++) {
1206 int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1213 cdev_count = rte_cryptodev_count();
1215 cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1217 struct rte_cryptodev_qp_conf qp_conf;
1218 struct rte_cryptodev_info dev_info;
1220 struct rte_cryptodev_config conf = {
1221 .nb_queue_pairs = 1,
1222 .socket_id = SOCKET_ID_ANY,
1229 rte_cryptodev_info_get(cdev_id, &dev_info);
1231 if (dev_info.dev_type != options->cdev_type)
1235 retval = rte_cryptodev_configure(cdev_id, &conf);
1237 printf("Failed to configure cryptodev %u", cdev_id);
1241 qp_conf.nb_descriptors = 2048;
1243 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1246 printf("Failed to setup queue pair %u on cryptodev %u",
1251 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1253 enabled_cdev_count++;
1256 return enabled_cdev_count;
1260 initialize_ports(struct l2fwd_crypto_options *options)
1262 uint8_t last_portid, portid;
1263 unsigned enabled_portcount = 0;
1264 unsigned nb_ports = rte_eth_dev_count();
1266 if (nb_ports == 0) {
1267 printf("No Ethernet ports - bye\n");
1271 if (nb_ports > RTE_MAX_ETHPORTS)
1272 nb_ports = RTE_MAX_ETHPORTS;
1274 /* Reset l2fwd_dst_ports */
1275 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1276 l2fwd_dst_ports[portid] = 0;
1278 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1281 /* Skip ports that are not enabled */
1282 if ((options->portmask & (1 << portid)) == 0)
1286 printf("Initializing port %u... ", (unsigned) portid);
1288 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1290 printf("Cannot configure device: err=%d, port=%u\n",
1291 retval, (unsigned) portid);
1295 /* init one RX queue */
1297 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1298 rte_eth_dev_socket_id(portid),
1299 NULL, l2fwd_pktmbuf_pool);
1301 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1302 retval, (unsigned) portid);
1306 /* init one TX queue on each port */
1308 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1309 rte_eth_dev_socket_id(portid),
1312 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1313 retval, (unsigned) portid);
1319 retval = rte_eth_dev_start(portid);
1321 printf("rte_eth_dev_start:err=%d, port=%u\n",
1322 retval, (unsigned) portid);
1326 rte_eth_promiscuous_enable(portid);
1328 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1330 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1332 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1333 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1334 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1335 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1336 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1337 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1339 /* initialize port stats */
1340 memset(&port_statistics, 0, sizeof(port_statistics));
1342 /* Setup port forwarding table */
1343 if (enabled_portcount % 2) {
1344 l2fwd_dst_ports[portid] = last_portid;
1345 l2fwd_dst_ports[last_portid] = portid;
1347 last_portid = portid;
1350 l2fwd_enabled_port_mask |= (1 << portid);
1351 enabled_portcount++;
1354 if (enabled_portcount == 1) {
1355 l2fwd_dst_ports[last_portid] = last_portid;
1356 } else if (enabled_portcount % 2) {
1357 printf("odd number of ports in portmask- bye\n");
1361 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1363 return enabled_portcount;
1367 reserve_key_memory(struct l2fwd_crypto_options *options)
1369 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1371 if (options->cipher_xform.cipher.key.data == NULL)
1372 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1375 options->auth_xform.auth.key.data = rte_malloc("auth key",
1377 if (options->auth_xform.auth.key.data == NULL)
1378 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1380 options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1381 if (options->iv.data == NULL)
1382 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1383 options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1385 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1386 if (options->aad.data == NULL)
1387 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1388 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1392 main(int argc, char **argv)
1394 struct lcore_queue_conf *qconf;
1395 struct l2fwd_crypto_options options;
1397 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1398 unsigned lcore_id, rx_lcore_id;
1399 int ret, enabled_cdevcount, enabled_portcount;
1402 ret = rte_eal_init(argc, argv);
1404 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1408 /* reserve memory for Cipher/Auth key and IV */
1409 reserve_key_memory(&options);
1411 /* parse application arguments (after the EAL ones) */
1412 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1414 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1416 /* create the mbuf pool */
1417 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1418 sizeof(struct rte_crypto_op),
1419 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1420 if (l2fwd_pktmbuf_pool == NULL)
1421 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1423 /* create crypto op pool */
1424 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1425 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1427 if (l2fwd_crypto_op_pool == NULL)
1428 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1430 /* Enable Ethernet ports */
1431 enabled_portcount = initialize_ports(&options);
1432 if (enabled_portcount < 1)
1433 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1435 nb_ports = rte_eth_dev_count();
1436 /* Initialize the port/queue configuration of each logical core */
1437 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1438 portid < nb_ports; portid++) {
1440 /* skip ports that are not enabled */
1441 if ((options.portmask & (1 << portid)) == 0)
1444 if (options.single_lcore && qconf == NULL) {
1445 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1447 if (rx_lcore_id >= RTE_MAX_LCORE)
1448 rte_exit(EXIT_FAILURE,
1449 "Not enough cores\n");
1451 } else if (!options.single_lcore) {
1452 /* get the lcore_id for this port */
1453 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1454 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1455 options.nb_ports_per_lcore) {
1457 if (rx_lcore_id >= RTE_MAX_LCORE)
1458 rte_exit(EXIT_FAILURE,
1459 "Not enough cores\n");
1463 /* Assigned a new logical core in the loop above. */
1464 if (qconf != &lcore_queue_conf[rx_lcore_id])
1465 qconf = &lcore_queue_conf[rx_lcore_id];
1467 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1468 qconf->nb_rx_ports++;
1470 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1474 /* Enable Crypto devices */
1475 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1476 if (enabled_cdevcount < 1)
1477 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1479 nb_cryptodevs = rte_cryptodev_count();
1480 /* Initialize the port/queue configuration of each logical core */
1481 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1482 cdev_id < nb_cryptodevs && enabled_cdevcount;
1484 struct rte_cryptodev_info info;
1486 rte_cryptodev_info_get(cdev_id, &info);
1488 /* skip devices of the wrong type */
1489 if (options.cdev_type != info.dev_type)
1492 if (options.single_lcore && qconf == NULL) {
1493 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1495 if (rx_lcore_id >= RTE_MAX_LCORE)
1496 rte_exit(EXIT_FAILURE,
1497 "Not enough cores\n");
1499 } else if (!options.single_lcore) {
1500 /* get the lcore_id for this port */
1501 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1502 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1503 options.nb_ports_per_lcore) {
1505 if (rx_lcore_id >= RTE_MAX_LCORE)
1506 rte_exit(EXIT_FAILURE,
1507 "Not enough cores\n");
1511 /* Assigned a new logical core in the loop above. */
1512 if (qconf != &lcore_queue_conf[rx_lcore_id])
1513 qconf = &lcore_queue_conf[rx_lcore_id];
1515 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1516 qconf->nb_crypto_devs++;
1518 enabled_cdevcount--;
1520 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1524 if (!options.akey_param)
1525 generate_random_key(options.auth_xform.auth.key.data,
1526 options.auth_xform.auth.key.length);
1528 if (!options.ckey_param)
1529 generate_random_key(options.cipher_xform.cipher.key.data,
1530 options.cipher_xform.cipher.key.length);
1533 /* launch per-lcore init on every lcore */
1534 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1536 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1537 if (rte_eal_wait_lcore(lcore_id) < 0)