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;
149 /** l2fwd crypto lcore params */
150 struct l2fwd_crypto_params {
154 unsigned digest_length;
157 struct rte_cryptodev_sym_session *session;
160 /** lcore configuration */
161 struct lcore_queue_conf {
162 unsigned nb_rx_ports;
163 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
165 unsigned nb_crypto_devs;
166 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
168 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
169 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
170 } __rte_cache_aligned;
172 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
174 static const struct rte_eth_conf port_conf = {
176 .mq_mode = ETH_MQ_RX_NONE,
177 .max_rx_pkt_len = ETHER_MAX_LEN,
179 .header_split = 0, /**< Header Split disabled */
180 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
181 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
182 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
183 .hw_strip_crc = 0, /**< CRC stripped by hardware */
186 .mq_mode = ETH_MQ_TX_NONE,
190 struct rte_mempool *l2fwd_pktmbuf_pool;
191 struct rte_mempool *l2fwd_crypto_op_pool;
193 /* Per-port statistics struct */
194 struct l2fwd_port_statistics {
198 uint64_t crypto_enqueued;
199 uint64_t crypto_dequeued;
202 } __rte_cache_aligned;
204 struct l2fwd_crypto_statistics {
209 } __rte_cache_aligned;
211 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
212 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
214 /* A tsc-based timer responsible for triggering statistics printout */
215 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
216 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
218 /* default period is 10 seconds */
219 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
221 /* Print out statistics on packets dropped */
225 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
226 uint64_t total_packets_enqueued, total_packets_dequeued,
227 total_packets_errors;
231 total_packets_dropped = 0;
232 total_packets_tx = 0;
233 total_packets_rx = 0;
234 total_packets_enqueued = 0;
235 total_packets_dequeued = 0;
236 total_packets_errors = 0;
238 const char clr[] = { 27, '[', '2', 'J', '\0' };
239 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
241 /* Clear screen and move to top left */
242 printf("%s%s", clr, topLeft);
244 printf("\nPort statistics ====================================");
246 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
247 /* skip disabled ports */
248 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
250 printf("\nStatistics for port %u ------------------------------"
251 "\nPackets sent: %32"PRIu64
252 "\nPackets received: %28"PRIu64
253 "\nPackets dropped: %29"PRIu64,
255 port_statistics[portid].tx,
256 port_statistics[portid].rx,
257 port_statistics[portid].dropped);
259 total_packets_dropped += port_statistics[portid].dropped;
260 total_packets_tx += port_statistics[portid].tx;
261 total_packets_rx += port_statistics[portid].rx;
263 printf("\nCrypto statistics ==================================");
265 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
266 /* skip disabled ports */
267 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
269 printf("\nStatistics for cryptodev %"PRIu64
270 " -------------------------"
271 "\nPackets enqueued: %28"PRIu64
272 "\nPackets dequeued: %28"PRIu64
273 "\nPackets errors: %30"PRIu64,
275 crypto_statistics[cdevid].enqueued,
276 crypto_statistics[cdevid].dequeued,
277 crypto_statistics[cdevid].errors);
279 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
280 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
281 total_packets_errors += crypto_statistics[cdevid].errors;
283 printf("\nAggregate statistics ==============================="
284 "\nTotal packets received: %22"PRIu64
285 "\nTotal packets enqueued: %22"PRIu64
286 "\nTotal packets dequeued: %22"PRIu64
287 "\nTotal packets sent: %26"PRIu64
288 "\nTotal packets dropped: %23"PRIu64
289 "\nTotal packets crypto errors: %17"PRIu64,
291 total_packets_enqueued,
292 total_packets_dequeued,
294 total_packets_dropped,
295 total_packets_errors);
296 printf("\n====================================================\n");
302 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
303 struct l2fwd_crypto_params *cparams)
305 struct rte_crypto_op **op_buffer;
308 op_buffer = (struct rte_crypto_op **)
309 qconf->op_buf[cparams->dev_id].buffer;
311 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
312 cparams->qp_id, op_buffer, (uint16_t) n);
314 crypto_statistics[cparams->dev_id].enqueued += ret;
315 if (unlikely(ret < n)) {
316 crypto_statistics[cparams->dev_id].errors += (n - ret);
318 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
319 rte_crypto_op_free(op_buffer[ret]);
327 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
328 struct l2fwd_crypto_params *cparams)
330 unsigned lcore_id, len;
331 struct lcore_queue_conf *qconf;
333 lcore_id = rte_lcore_id();
335 qconf = &lcore_queue_conf[lcore_id];
336 len = qconf->op_buf[cparams->dev_id].len;
337 qconf->op_buf[cparams->dev_id].buffer[len] = op;
340 /* enough ops to be sent */
341 if (len == MAX_PKT_BURST) {
342 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
346 qconf->op_buf[cparams->dev_id].len = len;
351 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
352 struct rte_crypto_op *op,
353 struct l2fwd_crypto_params *cparams)
355 struct ether_hdr *eth_hdr;
356 struct ipv4_hdr *ip_hdr;
358 unsigned ipdata_offset, pad_len, data_len;
361 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
363 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
366 ipdata_offset = sizeof(struct ether_hdr);
368 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
371 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
372 * IPV4_IHL_MULTIPLIER;
375 /* Zero pad data to be crypto'd so it is block aligned */
376 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
377 pad_len = data_len % cparams->block_size ? cparams->block_size -
378 (data_len % cparams->block_size) : 0;
381 padding = rte_pktmbuf_append(m, pad_len);
382 if (unlikely(!padding))
386 memset(padding, 0, pad_len);
389 /* Set crypto operation data parameters */
390 rte_crypto_op_attach_sym_session(op, cparams->session);
392 /* Append space for digest to end of packet */
393 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
394 cparams->digest_length);
395 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
396 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
397 op->sym->auth.digest.length = cparams->digest_length;
399 op->sym->auth.data.offset = ipdata_offset;
400 op->sym->auth.data.length = data_len;
403 op->sym->cipher.iv.data = cparams->iv.data;
404 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
405 op->sym->cipher.iv.length = cparams->iv.length;
407 op->sym->cipher.data.offset = ipdata_offset;
408 op->sym->cipher.data.length = data_len;
412 return l2fwd_crypto_enqueue(op, cparams);
416 /* Send the burst of packets on an output interface */
418 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
421 struct rte_mbuf **pkt_buffer;
424 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
426 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
427 port_statistics[port].tx += ret;
428 if (unlikely(ret < n)) {
429 port_statistics[port].dropped += (n - ret);
431 rte_pktmbuf_free(pkt_buffer[ret]);
438 /* Enqueue packets for TX and prepare them to be sent */
440 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
442 unsigned lcore_id, len;
443 struct lcore_queue_conf *qconf;
445 lcore_id = rte_lcore_id();
447 qconf = &lcore_queue_conf[lcore_id];
448 len = qconf->pkt_buf[port].len;
449 qconf->pkt_buf[port].buffer[len] = m;
452 /* enough pkts to be sent */
453 if (unlikely(len == MAX_PKT_BURST)) {
454 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
458 qconf->pkt_buf[port].len = len;
463 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
465 struct ether_hdr *eth;
469 dst_port = l2fwd_dst_ports[portid];
470 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
472 /* 02:00:00:00:00:xx */
473 tmp = ð->d_addr.addr_bytes[0];
474 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
477 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
479 l2fwd_send_packet(m, (uint8_t) dst_port);
482 /** Generate random key */
484 generate_random_key(uint8_t *key, unsigned length)
488 for (i = 0; i < length; i++)
489 key[i] = rand() % 0xff;
492 static struct rte_cryptodev_sym_session *
493 initialize_crypto_session(struct l2fwd_crypto_options *options,
496 struct rte_crypto_sym_xform *first_xform;
498 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
499 first_xform = &options->cipher_xform;
500 first_xform->next = &options->auth_xform;
502 first_xform = &options->auth_xform;
503 first_xform->next = &options->cipher_xform;
506 /* Setup Cipher Parameters */
507 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
511 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
513 /* main processing loop */
515 l2fwd_main_loop(struct l2fwd_crypto_options *options)
517 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
518 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
520 unsigned lcore_id = rte_lcore_id();
521 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
522 unsigned i, j, portid, nb_rx;
523 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
524 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
525 US_PER_S * BURST_TX_DRAIN_US;
526 struct l2fwd_crypto_params *cparams;
527 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
529 if (qconf->nb_rx_ports == 0) {
530 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
534 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
536 l2fwd_crypto_options_print(options);
538 for (i = 0; i < qconf->nb_rx_ports; i++) {
540 portid = qconf->rx_port_list[i];
541 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
545 for (i = 0; i < qconf->nb_crypto_devs; i++) {
546 port_cparams[i].dev_id = qconf->cryptodev_list[i];
547 port_cparams[i].qp_id = 0;
549 port_cparams[i].block_size = 64;
550 port_cparams[i].digest_length = 20;
552 port_cparams[i].iv.length = 16;
553 port_cparams[i].iv.data = options->iv.data;
554 port_cparams[i].iv.phys_addr = options->iv.phys_addr;
555 if (!options->iv_param)
556 generate_random_key(options->iv.data,
557 port_cparams[i].iv.length);
559 port_cparams[i].session = initialize_crypto_session(options,
560 port_cparams[i].dev_id);
562 if (port_cparams[i].session == NULL)
564 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
565 port_cparams[i].dev_id);
570 cur_tsc = rte_rdtsc();
573 * TX burst queue drain
575 diff_tsc = cur_tsc - prev_tsc;
576 if (unlikely(diff_tsc > drain_tsc)) {
578 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
579 if (qconf->pkt_buf[portid].len == 0)
581 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
582 qconf->pkt_buf[portid].len,
584 qconf->pkt_buf[portid].len = 0;
587 /* if timer is enabled */
588 if (timer_period > 0) {
590 /* advance the timer */
591 timer_tsc += diff_tsc;
593 /* if timer has reached its timeout */
594 if (unlikely(timer_tsc >=
595 (uint64_t)timer_period)) {
597 /* do this only on master core */
598 if (lcore_id == rte_get_master_lcore()
599 && options->refresh_period) {
610 * Read packet from RX queues
612 for (i = 0; i < qconf->nb_rx_ports; i++) {
613 portid = qconf->rx_port_list[i];
615 cparams = &port_cparams[i];
617 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
618 pkts_burst, MAX_PKT_BURST);
620 port_statistics[portid].rx += nb_rx;
624 * If we can't allocate a crypto_ops, then drop
625 * the rest of the burst and dequeue and
626 * process the packets to free offload structs
628 if (rte_crypto_op_bulk_alloc(
629 l2fwd_crypto_op_pool,
630 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
633 for (j = 0; j < nb_rx; j++)
634 rte_pktmbuf_free(pkts_burst[i]);
639 /* Enqueue packets from Crypto device*/
640 for (j = 0; j < nb_rx; j++) {
643 l2fwd_simple_crypto_enqueue(m,
644 ops_burst[j], cparams);
648 /* Dequeue packets from Crypto device */
650 nb_rx = rte_cryptodev_dequeue_burst(
651 cparams->dev_id, cparams->qp_id,
652 ops_burst, MAX_PKT_BURST);
654 crypto_statistics[cparams->dev_id].dequeued +=
657 /* Forward crypto'd packets */
658 for (j = 0; j < nb_rx; j++) {
659 m = ops_burst[j]->sym->m_src;
661 rte_crypto_op_free(ops_burst[j]);
662 l2fwd_simple_forward(m, portid);
664 } while (nb_rx == MAX_PKT_BURST);
670 l2fwd_launch_one_lcore(void *arg)
672 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
676 /* Display command line arguments usage */
678 l2fwd_crypto_usage(const char *prgname)
680 printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
681 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
682 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
683 " -s manage all ports from single lcore"
684 " -t PERIOD: statistics will be refreshed each PERIOD seconds"
685 " (0 to disable, 10 default, 86400 maximum)\n"
687 " --cdev AESNI_MB / QAT\n"
688 " --chain HASH_CIPHER / CIPHER_HASH\n"
690 " --cipher_algo ALGO\n"
691 " --cipher_op ENCRYPT / DECRYPT\n"
692 " --cipher_key KEY\n"
695 " --auth_algo ALGO\n"
696 " --auth_op GENERATE / VERIFY\n"
703 /** Parse crypto device type command line argument */
705 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
707 if (strcmp("AESNI_MB", optarg) == 0) {
708 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
710 } else if (strcmp("QAT", optarg) == 0) {
711 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
718 /** Parse crypto chain xform command line argument */
720 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
722 if (strcmp("CIPHER_HASH", optarg) == 0) {
723 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
725 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
726 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
733 /** Parse crypto cipher algo option command line argument */
735 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
737 if (strcmp("AES_CBC", optarg) == 0) {
738 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
740 } else if (strcmp("AES_GCM", optarg) == 0) {
741 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
745 printf("Cipher algorithm not supported!\n");
749 /** Parse crypto cipher operation command line argument */
751 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
753 if (strcmp("ENCRYPT", optarg) == 0) {
754 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
756 } else if (strcmp("DECRYPT", optarg) == 0) {
757 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
761 printf("Cipher operation not supported!\n");
765 /** Parse crypto key command line argument */
767 parse_key(uint8_t *data, char *input_arg)
772 for (byte_count = 0, token = strtok(input_arg, ":");
773 (byte_count < MAX_KEY_SIZE) && (token != NULL);
774 token = strtok(NULL, ":")) {
776 int number = (int)strtol(token, NULL, 16);
778 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
781 data[byte_count++] = (uint8_t)number;
787 /** Parse crypto cipher operation command line argument */
789 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
791 if (strcmp("MD5_HMAC", optarg) == 0) {
792 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
794 } else if (strcmp("SHA1_HMAC", optarg) == 0) {
795 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
797 } else if (strcmp("SHA224_HMAC", optarg) == 0) {
798 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
800 } else if (strcmp("SHA256_HMAC", optarg) == 0) {
801 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
803 } else if (strcmp("SHA384_HMAC", optarg) == 0) {
804 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
806 } else if (strcmp("SHA512_HMAC", optarg) == 0) {
807 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
811 printf("Authentication algorithm specified not supported!\n");
816 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
818 if (strcmp("VERIFY", optarg) == 0) {
819 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
821 } else if (strcmp("GENERATE", optarg) == 0) {
822 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
826 printf("Authentication operation specified not supported!\n");
830 /** Parse long options */
832 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
833 struct option *lgopts, int option_index)
835 if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
836 return parse_cryptodev_type(&options->cdev_type, optarg);
838 else if (strcmp(lgopts[option_index].name, "chain") == 0)
839 return parse_crypto_opt_chain(options, optarg);
842 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
843 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
846 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
847 return parse_cipher_op(&options->cipher_xform.cipher.op,
850 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
851 options->ckey_param = 1;
852 return parse_key(options->cipher_xform.cipher.key.data, optarg);
855 else if (strcmp(lgopts[option_index].name, "iv") == 0) {
856 options->iv_param = 1;
857 return parse_key(options->iv.data, optarg);
860 /* Authentication options */
861 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
862 return parse_auth_algo(&options->auth_xform.auth.algo,
865 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
866 return parse_auth_op(&options->auth_xform.auth.op,
869 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
870 options->akey_param = 1;
871 return parse_key(options->auth_xform.auth.key.data, optarg);
874 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
875 options->sessionless = 1;
882 /** Parse port mask */
884 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
890 /* parse hexadecimal string */
891 pm = strtoul(q_arg, &end, 16);
892 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
895 options->portmask = pm;
896 if (options->portmask == 0) {
897 printf("invalid portmask specified\n");
904 /** Parse number of queues */
906 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
912 /* parse hexadecimal string */
913 n = strtoul(q_arg, &end, 10);
914 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
916 else if (n >= MAX_RX_QUEUE_PER_LCORE)
919 options->nb_ports_per_lcore = n;
920 if (options->nb_ports_per_lcore == 0) {
921 printf("invalid number of ports selected\n");
928 /** Parse timer period */
930 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
936 /* parse number string */
937 n = (unsigned)strtol(q_arg, &end, 10);
938 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
941 if (n >= MAX_TIMER_PERIOD) {
942 printf("Warning refresh period specified %lu is greater than "
943 "max value %lu! using max value",
944 n, MAX_TIMER_PERIOD);
945 n = MAX_TIMER_PERIOD;
948 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
953 /** Generate default options for application */
955 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
959 options->portmask = 0xffffffff;
960 options->nb_ports_per_lcore = 1;
961 options->refresh_period = 10000;
962 options->single_lcore = 0;
963 options->sessionless = 0;
965 options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
966 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
969 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
970 options->cipher_xform.next = NULL;
971 options->ckey_param = 0;
972 options->iv_param = 0;
974 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
975 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
977 options->cipher_xform.cipher.key.length = 16;
980 /* Authentication Data */
981 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
982 options->auth_xform.next = NULL;
983 options->akey_param = 0;
985 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
986 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
988 options->auth_xform.auth.add_auth_data_length = 0;
989 options->auth_xform.auth.digest_length = 20;
991 options->auth_xform.auth.key.length = 20;
995 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
997 printf("Options:-\nn");
998 printf("portmask: %x\n", options->portmask);
999 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1000 printf("refresh period : %u\n", options->refresh_period);
1001 printf("single lcore mode: %s\n",
1002 options->single_lcore ? "enabled" : "disabled");
1003 printf("stats_printing: %s\n",
1004 options->refresh_period == 0 ? "disabled" : "enabled");
1006 switch (options->cdev_type) {
1007 case RTE_CRYPTODEV_AESNI_MB_PMD:
1008 printf("cryptodev type: AES-NI MB PMD\n"); break;
1009 case RTE_CRYPTODEV_QAT_SYM_PMD:
1010 printf("cryptodev type: QAT PMD\n"); break;
1015 printf("sessionless crypto: %s\n",
1016 options->sessionless ? "enabled" : "disabled");
1019 /* Parse the argument given in the command line of the application */
1021 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1022 int argc, char **argv)
1024 int opt, retval, option_index;
1025 char **argvopt = argv, *prgname = argv[0];
1027 static struct option lgopts[] = {
1028 { "sessionless", no_argument, 0, 0 },
1030 { "cdev_type", required_argument, 0, 0 },
1031 { "chain", required_argument, 0, 0 },
1033 { "cipher_algo", required_argument, 0, 0 },
1034 { "cipher_op", required_argument, 0, 0 },
1035 { "cipher_key", required_argument, 0, 0 },
1037 { "auth_algo", required_argument, 0, 0 },
1038 { "auth_op", required_argument, 0, 0 },
1039 { "auth_key", required_argument, 0, 0 },
1041 { "iv", required_argument, 0, 0 },
1043 { "sessionless", no_argument, 0, 0 },
1048 l2fwd_crypto_default_options(options);
1050 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1051 &option_index)) != EOF) {
1055 retval = l2fwd_crypto_parse_args_long_options(options,
1056 lgopts, option_index);
1058 l2fwd_crypto_usage(prgname);
1065 retval = l2fwd_crypto_parse_portmask(options, optarg);
1067 l2fwd_crypto_usage(prgname);
1074 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1076 l2fwd_crypto_usage(prgname);
1083 options->single_lcore = 1;
1089 retval = l2fwd_crypto_parse_timer_period(options,
1092 l2fwd_crypto_usage(prgname);
1098 l2fwd_crypto_usage(prgname);
1105 argv[optind-1] = prgname;
1108 optind = 0; /* reset getopt lib */
1113 /* Check the link status of all ports in up to 9s, and print them finally */
1115 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1117 #define CHECK_INTERVAL 100 /* 100ms */
1118 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1119 uint8_t portid, count, all_ports_up, print_flag = 0;
1120 struct rte_eth_link link;
1122 printf("\nChecking link status");
1124 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1126 for (portid = 0; portid < port_num; portid++) {
1127 if ((port_mask & (1 << portid)) == 0)
1129 memset(&link, 0, sizeof(link));
1130 rte_eth_link_get_nowait(portid, &link);
1131 /* print link status if flag set */
1132 if (print_flag == 1) {
1133 if (link.link_status)
1134 printf("Port %d Link Up - speed %u "
1135 "Mbps - %s\n", (uint8_t)portid,
1136 (unsigned)link.link_speed,
1137 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1138 ("full-duplex") : ("half-duplex\n"));
1140 printf("Port %d Link Down\n",
1144 /* clear all_ports_up flag if any link down */
1145 if (link.link_status == 0) {
1150 /* after finally printing all link status, get out */
1151 if (print_flag == 1)
1154 if (all_ports_up == 0) {
1157 rte_delay_ms(CHECK_INTERVAL);
1160 /* set the print_flag if all ports up or timeout */
1161 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1169 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1171 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1174 if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1175 if (rte_cryptodev_count() < nb_ports)
1177 } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1178 for (i = 0; i < nb_ports; i++) {
1179 int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1186 cdev_count = rte_cryptodev_count();
1188 cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1190 struct rte_cryptodev_qp_conf qp_conf;
1191 struct rte_cryptodev_info dev_info;
1193 struct rte_cryptodev_config conf = {
1194 .nb_queue_pairs = 1,
1195 .socket_id = SOCKET_ID_ANY,
1202 rte_cryptodev_info_get(cdev_id, &dev_info);
1204 if (dev_info.dev_type != options->cdev_type)
1208 retval = rte_cryptodev_configure(cdev_id, &conf);
1210 printf("Failed to configure cryptodev %u", cdev_id);
1214 qp_conf.nb_descriptors = 2048;
1216 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1219 printf("Failed to setup queue pair %u on cryptodev %u",
1224 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1226 enabled_cdev_count++;
1229 return enabled_cdev_count;
1233 initialize_ports(struct l2fwd_crypto_options *options)
1235 uint8_t last_portid, portid;
1236 unsigned enabled_portcount = 0;
1237 unsigned nb_ports = rte_eth_dev_count();
1239 if (nb_ports == 0) {
1240 printf("No Ethernet ports - bye\n");
1244 if (nb_ports > RTE_MAX_ETHPORTS)
1245 nb_ports = RTE_MAX_ETHPORTS;
1247 /* Reset l2fwd_dst_ports */
1248 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1249 l2fwd_dst_ports[portid] = 0;
1251 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1254 /* Skip ports that are not enabled */
1255 if ((options->portmask & (1 << portid)) == 0)
1259 printf("Initializing port %u... ", (unsigned) portid);
1261 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1263 printf("Cannot configure device: err=%d, port=%u\n",
1264 retval, (unsigned) portid);
1268 /* init one RX queue */
1270 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1271 rte_eth_dev_socket_id(portid),
1272 NULL, l2fwd_pktmbuf_pool);
1274 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1275 retval, (unsigned) portid);
1279 /* init one TX queue on each port */
1281 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1282 rte_eth_dev_socket_id(portid),
1285 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1286 retval, (unsigned) portid);
1292 retval = rte_eth_dev_start(portid);
1294 printf("rte_eth_dev_start:err=%d, port=%u\n",
1295 retval, (unsigned) portid);
1299 rte_eth_promiscuous_enable(portid);
1301 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1303 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1305 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1306 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1307 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1308 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1309 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1310 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1312 /* initialize port stats */
1313 memset(&port_statistics, 0, sizeof(port_statistics));
1315 /* Setup port forwarding table */
1316 if (enabled_portcount % 2) {
1317 l2fwd_dst_ports[portid] = last_portid;
1318 l2fwd_dst_ports[last_portid] = portid;
1320 last_portid = portid;
1323 l2fwd_enabled_port_mask |= (1 << portid);
1324 enabled_portcount++;
1327 if (enabled_portcount == 1) {
1328 l2fwd_dst_ports[last_portid] = last_portid;
1329 } else if (enabled_portcount % 2) {
1330 printf("odd number of ports in portmask- bye\n");
1334 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1336 return enabled_portcount;
1340 reserve_key_memory(struct l2fwd_crypto_options *options)
1342 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1344 if (options->cipher_xform.cipher.key.data == NULL)
1345 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1348 options->auth_xform.auth.key.data = rte_malloc("auth key",
1350 if (options->auth_xform.auth.key.data == NULL)
1351 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1353 options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1354 if (options->iv.data == NULL)
1355 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1356 options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1360 main(int argc, char **argv)
1362 struct lcore_queue_conf *qconf;
1363 struct l2fwd_crypto_options options;
1365 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1366 unsigned lcore_id, rx_lcore_id;
1367 int ret, enabled_cdevcount, enabled_portcount;
1370 ret = rte_eal_init(argc, argv);
1372 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1376 /* reserve memory for Cipher/Auth key and IV */
1377 reserve_key_memory(&options);
1379 /* parse application arguments (after the EAL ones) */
1380 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1382 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1384 /* create the mbuf pool */
1385 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1386 sizeof(struct rte_crypto_op),
1387 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1388 if (l2fwd_pktmbuf_pool == NULL)
1389 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1391 /* create crypto op pool */
1392 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1393 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1395 if (l2fwd_crypto_op_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,
1492 if (!options.akey_param)
1493 generate_random_key(options.auth_xform.auth.key.data,
1494 options.auth_xform.auth.key.length);
1496 if (!options.ckey_param)
1497 generate_random_key(options.cipher_xform.cipher.key.data,
1498 options.cipher_xform.cipher.key.length);
1501 /* launch per-lcore init on every lcore */
1502 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1504 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1505 if (rte_eal_wait_lcore(lcore_id) < 0)