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_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
118 phys_addr_t phys_addr;
121 /** l2fwd crypto application command line options */
122 struct l2fwd_crypto_options {
124 unsigned nb_ports_per_lcore;
125 unsigned refresh_period;
126 unsigned single_lcore:1;
128 enum rte_cryptodev_type cdev_type;
129 unsigned sessionless:1;
131 enum l2fwd_crypto_xform_chain xform_chain;
133 struct rte_crypto_sym_xform cipher_xform;
134 uint8_t ckey_data[32];
136 struct l2fwd_key iv_key;
137 uint8_t ivkey_data[16];
139 struct rte_crypto_sym_xform auth_xform;
140 uint8_t akey_data[128];
143 /** l2fwd crypto lcore params */
144 struct l2fwd_crypto_params {
148 unsigned digest_length;
150 struct l2fwd_key iv_key;
151 struct rte_cryptodev_sym_session *session;
154 /** lcore configuration */
155 struct lcore_queue_conf {
156 unsigned nb_rx_ports;
157 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
159 unsigned nb_crypto_devs;
160 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
162 struct pkt_buffer crypto_pkt_buf[RTE_MAX_ETHPORTS];
163 struct pkt_buffer tx_pkt_buf[RTE_MAX_ETHPORTS];
164 } __rte_cache_aligned;
166 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
168 static const struct rte_eth_conf port_conf = {
171 .header_split = 0, /**< Header Split disabled */
172 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
173 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
174 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
175 .hw_strip_crc = 0, /**< CRC stripped by hardware */
178 .mq_mode = ETH_MQ_TX_NONE,
182 struct rte_mempool *l2fwd_pktmbuf_pool;
183 struct rte_mempool *l2fwd_mbuf_ol_pool;
185 /* Per-port statistics struct */
186 struct l2fwd_port_statistics {
190 uint64_t crypto_enqueued;
191 uint64_t crypto_dequeued;
194 } __rte_cache_aligned;
196 struct l2fwd_crypto_statistics {
201 } __rte_cache_aligned;
203 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
204 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
206 /* A tsc-based timer responsible for triggering statistics printout */
207 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
208 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
210 /* default period is 10 seconds */
211 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
213 /* Print out statistics on packets dropped */
217 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
218 uint64_t total_packets_enqueued, total_packets_dequeued,
219 total_packets_errors;
223 total_packets_dropped = 0;
224 total_packets_tx = 0;
225 total_packets_rx = 0;
226 total_packets_enqueued = 0;
227 total_packets_dequeued = 0;
228 total_packets_errors = 0;
230 const char clr[] = { 27, '[', '2', 'J', '\0' };
231 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
233 /* Clear screen and move to top left */
234 printf("%s%s", clr, topLeft);
236 printf("\nPort statistics ====================================");
238 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
239 /* skip disabled ports */
240 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
242 printf("\nStatistics for port %u ------------------------------"
243 "\nPackets sent: %32"PRIu64
244 "\nPackets received: %28"PRIu64
245 "\nPackets dropped: %29"PRIu64,
247 port_statistics[portid].tx,
248 port_statistics[portid].rx,
249 port_statistics[portid].dropped);
251 total_packets_dropped += port_statistics[portid].dropped;
252 total_packets_tx += port_statistics[portid].tx;
253 total_packets_rx += port_statistics[portid].rx;
255 printf("\nCrypto statistics ==================================");
257 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
258 /* skip disabled ports */
259 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
261 printf("\nStatistics for cryptodev %"PRIu64
262 " -------------------------"
263 "\nPackets enqueued: %28"PRIu64
264 "\nPackets dequeued: %28"PRIu64
265 "\nPackets errors: %30"PRIu64,
267 crypto_statistics[cdevid].enqueued,
268 crypto_statistics[cdevid].dequeued,
269 crypto_statistics[cdevid].errors);
271 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
272 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
273 total_packets_errors += crypto_statistics[cdevid].errors;
275 printf("\nAggregate statistics ==============================="
276 "\nTotal packets received: %22"PRIu64
277 "\nTotal packets enqueued: %22"PRIu64
278 "\nTotal packets dequeued: %22"PRIu64
279 "\nTotal packets sent: %26"PRIu64
280 "\nTotal packets dropped: %23"PRIu64
281 "\nTotal packets crypto errors: %17"PRIu64,
283 total_packets_enqueued,
284 total_packets_dequeued,
286 total_packets_dropped,
287 total_packets_errors);
288 printf("\n====================================================\n");
294 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
295 struct l2fwd_crypto_params *cparams)
297 struct rte_mbuf **pkt_buffer;
300 pkt_buffer = (struct rte_mbuf **)
301 qconf->crypto_pkt_buf[cparams->dev_id].buffer;
303 ret = rte_cryptodev_enqueue_burst(cparams->dev_id, cparams->qp_id,
304 pkt_buffer, (uint16_t) n);
305 crypto_statistics[cparams->dev_id].enqueued += ret;
306 if (unlikely(ret < n)) {
307 crypto_statistics[cparams->dev_id].errors += (n - ret);
309 rte_pktmbuf_offload_free(pkt_buffer[ret]->offload_ops);
310 rte_pktmbuf_free(pkt_buffer[ret]);
318 l2fwd_crypto_enqueue(struct rte_mbuf *m, struct l2fwd_crypto_params *cparams)
320 unsigned lcore_id, len;
321 struct lcore_queue_conf *qconf;
323 lcore_id = rte_lcore_id();
325 qconf = &lcore_queue_conf[lcore_id];
326 len = qconf->crypto_pkt_buf[cparams->dev_id].len;
327 qconf->crypto_pkt_buf[cparams->dev_id].buffer[len] = m;
330 /* enough pkts to be sent */
331 if (len == MAX_PKT_BURST) {
332 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
336 qconf->crypto_pkt_buf[cparams->dev_id].len = len;
341 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
342 struct rte_mbuf_offload *ol,
343 struct l2fwd_crypto_params *cparams)
345 struct ether_hdr *eth_hdr;
346 struct ipv4_hdr *ip_hdr;
348 unsigned ipdata_offset, pad_len, data_len;
351 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
353 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
356 ipdata_offset = sizeof(struct ether_hdr);
358 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
361 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
362 * IPV4_IHL_MULTIPLIER;
365 /* Zero pad data to be crypto'd so it is block aligned */
366 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
367 pad_len = data_len % cparams->block_size ? cparams->block_size -
368 (data_len % cparams->block_size) : 0;
371 padding = rte_pktmbuf_append(m, pad_len);
372 if (unlikely(!padding))
376 memset(padding, 0, pad_len);
379 /* Set crypto operation data parameters */
380 rte_crypto_sym_op_attach_session(&ol->op.crypto, cparams->session);
382 /* Append space for digest to end of packet */
383 ol->op.crypto.digest.data = (uint8_t *)rte_pktmbuf_append(m,
384 cparams->digest_length);
385 ol->op.crypto.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
386 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
387 ol->op.crypto.digest.length = cparams->digest_length;
389 ol->op.crypto.iv.data = cparams->iv_key.data;
390 ol->op.crypto.iv.phys_addr = cparams->iv_key.phys_addr;
391 ol->op.crypto.iv.length = cparams->iv_key.length;
393 ol->op.crypto.data.to_cipher.offset = ipdata_offset;
394 ol->op.crypto.data.to_cipher.length = data_len;
396 ol->op.crypto.data.to_hash.offset = ipdata_offset;
397 ol->op.crypto.data.to_hash.length = data_len;
399 rte_pktmbuf_offload_attach(m, ol);
401 return l2fwd_crypto_enqueue(m, cparams);
405 /* Send the burst of packets on an output interface */
407 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
409 struct rte_mbuf **pkt_buffer;
411 unsigned queueid = 0;
413 pkt_buffer = (struct rte_mbuf **)qconf->tx_pkt_buf[port].buffer;
415 ret = rte_eth_tx_burst(port, (uint16_t) queueid, pkt_buffer,
417 port_statistics[port].tx += ret;
418 if (unlikely(ret < n)) {
419 port_statistics[port].dropped += (n - ret);
421 rte_pktmbuf_free(pkt_buffer[ret]);
428 /* Enqueue packets for TX and prepare them to be sent */
430 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
432 unsigned lcore_id, len;
433 struct lcore_queue_conf *qconf;
435 lcore_id = rte_lcore_id();
437 qconf = &lcore_queue_conf[lcore_id];
438 len = qconf->tx_pkt_buf[port].len;
439 qconf->tx_pkt_buf[port].buffer[len] = m;
442 /* enough pkts to be sent */
443 if (unlikely(len == MAX_PKT_BURST)) {
444 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
448 qconf->tx_pkt_buf[port].len = len;
453 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
455 struct ether_hdr *eth;
459 dst_port = l2fwd_dst_ports[portid];
460 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
462 /* 02:00:00:00:00:xx */
463 tmp = ð->d_addr.addr_bytes[0];
464 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
467 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
469 l2fwd_send_packet(m, (uint8_t) dst_port);
472 /** Generate random key */
474 generate_random_key(uint8_t *key, unsigned length)
478 for (i = 0; i < length; i++)
479 key[i] = rand() % 0xff;
482 static struct rte_cryptodev_sym_session *
483 initialize_crypto_session(struct l2fwd_crypto_options *options,
486 struct rte_crypto_sym_xform *first_xform;
488 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
489 first_xform = &options->cipher_xform;
490 first_xform->next = &options->auth_xform;
492 first_xform = &options->auth_xform;
493 first_xform->next = &options->cipher_xform;
496 /* Setup Cipher Parameters */
497 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
501 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
503 /* main processing loop */
505 l2fwd_main_loop(struct l2fwd_crypto_options *options)
507 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
508 unsigned lcore_id = rte_lcore_id();
509 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
510 unsigned i, j, portid, nb_rx;
511 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
512 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
513 US_PER_S * BURST_TX_DRAIN_US;
514 struct l2fwd_crypto_params *cparams;
515 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
517 if (qconf->nb_rx_ports == 0) {
518 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
522 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
524 l2fwd_crypto_options_print(options);
526 for (i = 0; i < qconf->nb_rx_ports; i++) {
528 portid = qconf->rx_port_list[i];
529 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
533 for (i = 0; i < qconf->nb_crypto_devs; i++) {
534 port_cparams[i].dev_id = qconf->cryptodev_list[i];
535 port_cparams[i].qp_id = 0;
537 port_cparams[i].block_size = 64;
538 port_cparams[i].digest_length = 20;
540 port_cparams[i].iv_key.data =
541 (uint8_t *)rte_malloc(NULL, 16, 8);
542 port_cparams[i].iv_key.length = 16;
543 port_cparams[i].iv_key.phys_addr = rte_malloc_virt2phy(
544 (void *)port_cparams[i].iv_key.data);
545 generate_random_key(port_cparams[i].iv_key.data,
546 sizeof(cparams[i].iv_key.length));
548 port_cparams[i].session = initialize_crypto_session(options,
549 port_cparams[i].dev_id);
551 if (port_cparams[i].session == NULL)
553 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
554 port_cparams[i].dev_id);
559 cur_tsc = rte_rdtsc();
562 * TX burst queue drain
564 diff_tsc = cur_tsc - prev_tsc;
565 if (unlikely(diff_tsc > drain_tsc)) {
567 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
568 if (qconf->tx_pkt_buf[portid].len == 0)
570 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
571 qconf->tx_pkt_buf[portid].len,
573 qconf->tx_pkt_buf[portid].len = 0;
576 /* if timer is enabled */
577 if (timer_period > 0) {
579 /* advance the timer */
580 timer_tsc += diff_tsc;
582 /* if timer has reached its timeout */
583 if (unlikely(timer_tsc >=
584 (uint64_t)timer_period)) {
586 /* do this only on master core */
587 if (lcore_id == rte_get_master_lcore()
588 && options->refresh_period) {
599 * Read packet from RX queues
601 for (i = 0; i < qconf->nb_rx_ports; i++) {
602 struct rte_mbuf_offload *ol;
604 portid = qconf->rx_port_list[i];
606 cparams = &port_cparams[i];
608 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
609 pkts_burst, MAX_PKT_BURST);
611 port_statistics[portid].rx += nb_rx;
613 /* Enqueue packets from Crypto device*/
614 for (j = 0; j < nb_rx; j++) {
616 ol = rte_pktmbuf_offload_alloc(
618 RTE_PKTMBUF_OL_CRYPTO_SYM);
620 * If we can't allocate a offload, then drop
621 * the rest of the burst and dequeue and
622 * process the packets to free offload structs
624 if (unlikely(ol == NULL)) {
625 for (; j < nb_rx; j++) {
626 rte_pktmbuf_free(pkts_burst[j]);
627 port_statistics[portid].dropped++;
632 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
633 rte_prefetch0((void *)ol);
635 l2fwd_simple_crypto_enqueue(m, ol, cparams);
638 /* Dequeue packets from Crypto device */
639 nb_rx = rte_cryptodev_dequeue_burst(
640 cparams->dev_id, cparams->qp_id,
641 pkts_burst, MAX_PKT_BURST);
642 crypto_statistics[cparams->dev_id].dequeued += nb_rx;
644 /* Forward crypto'd packets */
645 for (j = 0; j < nb_rx; j++) {
647 rte_pktmbuf_offload_free(m->offload_ops);
648 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
649 l2fwd_simple_forward(m, portid);
656 l2fwd_launch_one_lcore(void *arg)
658 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
662 /* Display command line arguments usage */
664 l2fwd_crypto_usage(const char *prgname)
666 printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
667 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
668 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
669 " -s manage all ports from single lcore"
670 " -t PERIOD: statistics will be refreshed each PERIOD seconds"
671 " (0 to disable, 10 default, 86400 maximum)\n"
673 " --cdev AESNI_MB / QAT\n"
674 " --chain HASH_CIPHER / CIPHER_HASH\n"
676 " --cipher_algo ALGO\n"
677 " --cipher_op ENCRYPT / DECRYPT\n"
678 " --cipher_key KEY\n"
681 " --auth_algo ALGO\n"
682 " --auth_op GENERATE / VERIFY\n"
689 /** Parse crypto device type command line argument */
691 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
693 if (strcmp("AESNI_MB", optarg) == 0) {
694 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
696 } else if (strcmp("QAT", optarg) == 0) {
697 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
704 /** Parse crypto chain xform command line argument */
706 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
708 if (strcmp("CIPHER_HASH", optarg) == 0) {
709 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
711 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
712 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
719 /** Parse crypto cipher algo option command line argument */
721 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
723 if (strcmp("AES_CBC", optarg) == 0) {
724 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
726 } else if (strcmp("AES_GCM", optarg) == 0) {
727 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
731 printf("Cipher algorithm not supported!\n");
735 /** Parse crypto cipher operation command line argument */
737 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
739 if (strcmp("ENCRYPT", optarg) == 0) {
740 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
742 } else if (strcmp("DECRYPT", optarg) == 0) {
743 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
747 printf("Cipher operation not supported!\n");
751 /** Parse crypto key command line argument */
753 parse_key(struct l2fwd_key *key __rte_unused,
754 unsigned length __rte_unused, char *arg __rte_unused)
756 printf("Currently an unsupported argument!\n");
760 /** Parse crypto cipher operation command line argument */
762 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
764 if (strcmp("SHA1", optarg) == 0) {
765 *algo = RTE_CRYPTO_AUTH_SHA1;
767 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
768 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
770 } else if (strcmp("SHA224", optarg) == 0) {
771 *algo = RTE_CRYPTO_AUTH_SHA224;
773 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
774 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
776 } else if (strcmp("SHA256", optarg) == 0) {
777 *algo = RTE_CRYPTO_AUTH_SHA256;
779 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
780 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
782 } else if (strcmp("SHA512", optarg) == 0) {
783 *algo = RTE_CRYPTO_AUTH_SHA256;
785 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
786 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
790 printf("Authentication algorithm specified not supported!\n");
795 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
797 if (strcmp("VERIFY", optarg) == 0) {
798 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
800 } else if (strcmp("GENERATE", optarg) == 0) {
801 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
805 printf("Authentication operation specified not supported!\n");
809 /** Parse long options */
811 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
812 struct option *lgopts, int option_index)
814 if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
815 return parse_cryptodev_type(&options->cdev_type, optarg);
817 else if (strcmp(lgopts[option_index].name, "chain") == 0)
818 return parse_crypto_opt_chain(options, optarg);
821 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
822 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
825 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
826 return parse_cipher_op(&options->cipher_xform.cipher.op,
829 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
830 struct l2fwd_key key = { 0 };
833 retval = parse_key(&key, sizeof(options->ckey_data), optarg);
835 options->cipher_xform.cipher.key.data = key.data;
836 options->cipher_xform.cipher.key.length = key.length;
840 } else if (strcmp(lgopts[option_index].name, "iv") == 0)
841 return parse_key(&options->iv_key, sizeof(options->ivkey_data),
844 /* Authentication options */
845 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
846 return parse_auth_algo(&options->auth_xform.auth.algo,
849 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
850 return parse_auth_op(&options->auth_xform.auth.op,
853 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
854 struct l2fwd_key key = { 0 };
857 retval = parse_key(&key, sizeof(options->akey_data), optarg);
859 options->auth_xform.auth.key.data = key.data;
860 options->auth_xform.auth.key.length = key.length;
864 } else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
865 options->sessionless = 1;
872 /** Parse port mask */
874 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
880 /* parse hexadecimal string */
881 pm = strtoul(q_arg, &end, 16);
882 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
885 options->portmask = pm;
886 if (options->portmask == 0) {
887 printf("invalid portmask specified\n");
894 /** Parse number of queues */
896 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
902 /* parse hexadecimal string */
903 n = strtoul(q_arg, &end, 10);
904 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
906 else if (n >= MAX_RX_QUEUE_PER_LCORE)
909 options->nb_ports_per_lcore = n;
910 if (options->nb_ports_per_lcore == 0) {
911 printf("invalid number of ports selected\n");
918 /** Parse timer period */
920 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
926 /* parse number string */
927 n = strtol(q_arg, &end, 10);
928 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
931 if (n >= MAX_TIMER_PERIOD) {
932 printf("Warning refresh period specified %ld is greater than "
933 "max value %d! using max value",
934 n, MAX_TIMER_PERIOD);
935 n = MAX_TIMER_PERIOD;
938 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
943 /** Generate default options for application */
945 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
949 options->portmask = 0xffffffff;
950 options->nb_ports_per_lcore = 1;
951 options->refresh_period = 10000;
952 options->single_lcore = 0;
954 options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
955 options->sessionless = 0;
956 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
959 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
960 options->cipher_xform.next = NULL;
962 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
963 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
965 generate_random_key(options->ckey_data, sizeof(options->ckey_data));
967 options->cipher_xform.cipher.key.data = options->ckey_data;
968 options->cipher_xform.cipher.key.length = 16;
971 /* Authentication Data */
972 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
973 options->auth_xform.next = NULL;
975 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
976 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
978 options->auth_xform.auth.add_auth_data_length = 0;
979 options->auth_xform.auth.digest_length = 20;
981 generate_random_key(options->akey_data, sizeof(options->akey_data));
983 options->auth_xform.auth.key.data = options->akey_data;
984 options->auth_xform.auth.key.length = 20;
988 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
990 printf("Options:-\nn");
991 printf("portmask: %x\n", options->portmask);
992 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
993 printf("refresh period : %u\n", options->refresh_period);
994 printf("single lcore mode: %s\n",
995 options->single_lcore ? "enabled" : "disabled");
996 printf("stats_printing: %s\n",
997 options->refresh_period == 0 ? "disabled" : "enabled");
999 switch (options->cdev_type) {
1000 case RTE_CRYPTODEV_AESNI_MB_PMD:
1001 printf("cryptodev type: AES-NI MB PMD\n"); break;
1002 case RTE_CRYPTODEV_QAT_SYM_PMD:
1003 printf("cryptodev type: QAT PMD\n"); break;
1008 printf("sessionless crypto: %s\n",
1009 options->sessionless ? "enabled" : "disabled");
1011 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1014 options->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1015 options->cipher_xform.next = NULL;
1017 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1018 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1020 generate_random_key(options->ckey_data, sizeof(options->ckey_data));
1022 options->cipher_xform.cipher.key.data = options->ckey_data;
1023 options->cipher_xform.cipher.key.phys_addr = 0;
1024 options->cipher_xform.cipher.key.length = 16;
1027 /* Authentication Data */
1028 options->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1029 options->auth_xform.next = NULL;
1031 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1032 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1034 options->auth_xform.auth.add_auth_data_length = 0;
1035 options->auth_xform.auth.digest_length = 20;
1037 generate_random_key(options->akey_data, sizeof(options->akey_data));
1039 options->auth_xform.auth.key.data = options->akey_data;
1040 options->auth_xform.auth.key.phys_addr = 0;
1041 options->auth_xform.auth.key.length = 20;
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 },
1069 { "sessionless", no_argument, 0, 0 },
1073 l2fwd_crypto_default_options(options);
1075 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1076 &option_index)) != EOF) {
1080 retval = l2fwd_crypto_parse_args_long_options(options,
1081 lgopts, option_index);
1083 l2fwd_crypto_usage(prgname);
1090 retval = l2fwd_crypto_parse_portmask(options, optarg);
1092 l2fwd_crypto_usage(prgname);
1099 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1101 l2fwd_crypto_usage(prgname);
1108 options->single_lcore = 1;
1114 retval = l2fwd_crypto_parse_timer_period(options,
1117 l2fwd_crypto_usage(prgname);
1123 l2fwd_crypto_usage(prgname);
1130 argv[optind-1] = prgname;
1133 optind = 0; /* reset getopt lib */
1138 /* Check the link status of all ports in up to 9s, and print them finally */
1140 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1142 #define CHECK_INTERVAL 100 /* 100ms */
1143 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1144 uint8_t portid, count, all_ports_up, print_flag = 0;
1145 struct rte_eth_link link;
1147 printf("\nChecking link status");
1149 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1151 for (portid = 0; portid < port_num; portid++) {
1152 if ((port_mask & (1 << portid)) == 0)
1154 memset(&link, 0, sizeof(link));
1155 rte_eth_link_get_nowait(portid, &link);
1156 /* print link status if flag set */
1157 if (print_flag == 1) {
1158 if (link.link_status)
1159 printf("Port %d Link Up - speed %u "
1160 "Mbps - %s\n", (uint8_t)portid,
1161 (unsigned)link.link_speed,
1162 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1163 ("full-duplex") : ("half-duplex\n"));
1165 printf("Port %d Link Down\n",
1169 /* clear all_ports_up flag if any link down */
1170 if (link.link_status == 0) {
1175 /* after finally printing all link status, get out */
1176 if (print_flag == 1)
1179 if (all_ports_up == 0) {
1182 rte_delay_ms(CHECK_INTERVAL);
1185 /* set the print_flag if all ports up or timeout */
1186 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1194 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1196 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1199 if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1200 if (rte_cryptodev_count() < nb_ports)
1202 } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1203 for (i = 0; i < nb_ports; i++) {
1204 int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1211 cdev_count = rte_cryptodev_count();
1213 cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1215 struct rte_cryptodev_qp_conf qp_conf;
1216 struct rte_cryptodev_info dev_info;
1218 struct rte_cryptodev_config conf = {
1219 .nb_queue_pairs = 1,
1220 .socket_id = SOCKET_ID_ANY,
1227 rte_cryptodev_info_get(cdev_id, &dev_info);
1229 if (dev_info.dev_type != options->cdev_type)
1233 retval = rte_cryptodev_configure(cdev_id, &conf);
1235 printf("Failed to configure cryptodev %u", cdev_id);
1239 qp_conf.nb_descriptors = 2048;
1241 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1244 printf("Failed to setup queue pair %u on cryptodev %u",
1249 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1251 enabled_cdev_count++;
1254 return enabled_cdev_count;
1258 initialize_ports(struct l2fwd_crypto_options *options)
1260 uint8_t last_portid, portid;
1261 unsigned enabled_portcount = 0;
1262 unsigned nb_ports = rte_eth_dev_count();
1264 if (nb_ports == 0) {
1265 printf("No Ethernet ports - bye\n");
1269 if (nb_ports > RTE_MAX_ETHPORTS)
1270 nb_ports = RTE_MAX_ETHPORTS;
1272 /* Reset l2fwd_dst_ports */
1273 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1274 l2fwd_dst_ports[portid] = 0;
1276 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1279 /* Skip ports that are not enabled */
1280 if ((options->portmask & (1 << portid)) == 0)
1284 printf("Initializing port %u... ", (unsigned) portid);
1286 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1288 printf("Cannot configure device: err=%d, port=%u\n",
1289 retval, (unsigned) portid);
1293 /* init one RX queue */
1295 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1296 rte_eth_dev_socket_id(portid),
1297 NULL, l2fwd_pktmbuf_pool);
1299 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1300 retval, (unsigned) portid);
1304 /* init one TX queue on each port */
1306 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1307 rte_eth_dev_socket_id(portid),
1310 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1311 retval, (unsigned) portid);
1317 retval = rte_eth_dev_start(portid);
1319 printf("rte_eth_dev_start:err=%d, port=%u\n",
1320 retval, (unsigned) portid);
1324 rte_eth_promiscuous_enable(portid);
1326 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1328 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1330 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1331 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1332 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1333 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1334 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1335 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1337 /* initialize port stats */
1338 memset(&port_statistics, 0, sizeof(port_statistics));
1340 /* Setup port forwarding table */
1341 if (enabled_portcount % 2) {
1342 l2fwd_dst_ports[portid] = last_portid;
1343 l2fwd_dst_ports[last_portid] = portid;
1345 last_portid = portid;
1348 l2fwd_enabled_port_mask |= (1 << portid);
1349 enabled_portcount++;
1352 if (enabled_portcount == 1) {
1353 l2fwd_dst_ports[last_portid] = last_portid;
1354 } else if (enabled_portcount % 2) {
1355 printf("odd number of ports in portmask- bye\n");
1359 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1361 return enabled_portcount;
1365 main(int argc, char **argv)
1367 struct lcore_queue_conf *qconf;
1368 struct l2fwd_crypto_options options;
1370 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1371 unsigned lcore_id, rx_lcore_id;
1372 int ret, enabled_cdevcount, enabled_portcount;
1375 ret = rte_eal_init(argc, argv);
1377 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1381 /* parse application arguments (after the EAL ones) */
1382 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1384 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1386 /* create the mbuf pool */
1387 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 128,
1388 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1389 if (l2fwd_pktmbuf_pool == NULL)
1390 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1392 /* create crypto op pool */
1393 l2fwd_mbuf_ol_pool = rte_pktmbuf_offload_pool_create(
1394 "mbuf_offload_pool", NB_MBUF, 128, 0, rte_socket_id());
1395 if (l2fwd_mbuf_ol_pool == NULL)
1396 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1398 /* Enable Ethernet ports */
1399 enabled_portcount = initialize_ports(&options);
1400 if (enabled_portcount < 1)
1401 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1403 nb_ports = rte_eth_dev_count();
1404 /* Initialize the port/queue configuration of each logical core */
1405 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1406 portid < nb_ports; portid++) {
1408 /* skip ports that are not enabled */
1409 if ((options.portmask & (1 << portid)) == 0)
1412 if (options.single_lcore && qconf == NULL) {
1413 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1415 if (rx_lcore_id >= RTE_MAX_LCORE)
1416 rte_exit(EXIT_FAILURE,
1417 "Not enough cores\n");
1419 } else if (!options.single_lcore) {
1420 /* get the lcore_id for this port */
1421 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1422 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1423 options.nb_ports_per_lcore) {
1425 if (rx_lcore_id >= RTE_MAX_LCORE)
1426 rte_exit(EXIT_FAILURE,
1427 "Not enough cores\n");
1431 /* Assigned a new logical core in the loop above. */
1432 if (qconf != &lcore_queue_conf[rx_lcore_id])
1433 qconf = &lcore_queue_conf[rx_lcore_id];
1435 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1436 qconf->nb_rx_ports++;
1438 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1442 /* Enable Crypto devices */
1443 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1444 if (enabled_cdevcount < 1)
1445 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1447 nb_cryptodevs = rte_cryptodev_count();
1448 /* Initialize the port/queue configuration of each logical core */
1449 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1450 cdev_id < nb_cryptodevs && enabled_cdevcount;
1452 struct rte_cryptodev_info info;
1454 rte_cryptodev_info_get(cdev_id, &info);
1456 /* skip devices of the wrong type */
1457 if (options.cdev_type != info.dev_type)
1460 if (options.single_lcore && qconf == NULL) {
1461 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1463 if (rx_lcore_id >= RTE_MAX_LCORE)
1464 rte_exit(EXIT_FAILURE,
1465 "Not enough cores\n");
1467 } else if (!options.single_lcore) {
1468 /* get the lcore_id for this port */
1469 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1470 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1471 options.nb_ports_per_lcore) {
1473 if (rx_lcore_id >= RTE_MAX_LCORE)
1474 rte_exit(EXIT_FAILURE,
1475 "Not enough cores\n");
1479 /* Assigned a new logical core in the loop above. */
1480 if (qconf != &lcore_queue_conf[rx_lcore_id])
1481 qconf = &lcore_queue_conf[rx_lcore_id];
1483 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1484 qconf->nb_crypto_devs++;
1486 enabled_cdevcount--;
1488 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1494 /* launch per-lcore init on every lcore */
1495 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1497 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1498 if (rte_eal_wait_lcore(lcore_id) < 0)