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>
81 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
85 #define MAX_STR_LEN 32
86 #define MAX_KEY_SIZE 128
87 #define MAX_PKT_BURST 32
88 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91 * Configurable number of RX/TX ring descriptors
93 #define RTE_TEST_RX_DESC_DEFAULT 128
94 #define RTE_TEST_TX_DESC_DEFAULT 512
96 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
97 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
99 /* ethernet addresses of ports */
100 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
102 /* mask of enabled ports */
103 static uint64_t l2fwd_enabled_port_mask;
104 static uint64_t l2fwd_enabled_crypto_mask;
106 /* list of enabled ports */
107 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
112 struct rte_mbuf *buffer[MAX_PKT_BURST];
117 struct rte_crypto_op *buffer[MAX_PKT_BURST];
120 #define MAX_RX_QUEUE_PER_LCORE 16
121 #define MAX_TX_QUEUE_PER_PORT 16
123 enum l2fwd_crypto_xform_chain {
124 L2FWD_CRYPTO_CIPHER_HASH,
125 L2FWD_CRYPTO_HASH_CIPHER,
126 L2FWD_CRYPTO_CIPHER_ONLY,
127 L2FWD_CRYPTO_HASH_ONLY
133 phys_addr_t phys_addr;
136 char supported_auth_algo[RTE_CRYPTO_AUTH_LIST_END][MAX_STR_LEN];
137 char supported_cipher_algo[RTE_CRYPTO_CIPHER_LIST_END][MAX_STR_LEN];
139 /** l2fwd crypto application command line options */
140 struct l2fwd_crypto_options {
142 unsigned nb_ports_per_lcore;
143 unsigned refresh_period;
144 unsigned single_lcore:1;
147 unsigned sessionless:1;
149 enum l2fwd_crypto_xform_chain xform_chain;
151 struct rte_crypto_sym_xform cipher_xform;
153 int ckey_random_size;
159 struct rte_crypto_sym_xform auth_xform;
161 int akey_random_size;
163 struct l2fwd_key aad;
170 char string_type[MAX_STR_LEN];
173 /** l2fwd crypto lcore params */
174 struct l2fwd_crypto_params {
178 unsigned digest_length;
182 struct l2fwd_key aad;
183 struct rte_cryptodev_sym_session *session;
189 enum rte_crypto_cipher_algorithm cipher_algo;
190 enum rte_crypto_auth_algorithm auth_algo;
193 /** lcore configuration */
194 struct lcore_queue_conf {
195 unsigned nb_rx_ports;
196 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
198 unsigned nb_crypto_devs;
199 unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
201 struct op_buffer op_buf[RTE_MAX_ETHPORTS];
202 struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
203 } __rte_cache_aligned;
205 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
207 static const struct rte_eth_conf port_conf = {
209 .mq_mode = ETH_MQ_RX_NONE,
210 .max_rx_pkt_len = ETHER_MAX_LEN,
212 .header_split = 0, /**< Header Split disabled */
213 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
214 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
215 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
216 .hw_strip_crc = 0, /**< CRC stripped by hardware */
219 .mq_mode = ETH_MQ_TX_NONE,
223 struct rte_mempool *l2fwd_pktmbuf_pool;
224 struct rte_mempool *l2fwd_crypto_op_pool;
226 /* Per-port statistics struct */
227 struct l2fwd_port_statistics {
231 uint64_t crypto_enqueued;
232 uint64_t crypto_dequeued;
235 } __rte_cache_aligned;
237 struct l2fwd_crypto_statistics {
242 } __rte_cache_aligned;
244 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
245 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
247 /* A tsc-based timer responsible for triggering statistics printout */
248 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
249 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
251 /* default period is 10 seconds */
252 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
254 /* Print out statistics on packets dropped */
258 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
259 uint64_t total_packets_enqueued, total_packets_dequeued,
260 total_packets_errors;
264 total_packets_dropped = 0;
265 total_packets_tx = 0;
266 total_packets_rx = 0;
267 total_packets_enqueued = 0;
268 total_packets_dequeued = 0;
269 total_packets_errors = 0;
271 const char clr[] = { 27, '[', '2', 'J', '\0' };
272 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
274 /* Clear screen and move to top left */
275 printf("%s%s", clr, topLeft);
277 printf("\nPort statistics ====================================");
279 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
280 /* skip disabled ports */
281 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
283 printf("\nStatistics for port %u ------------------------------"
284 "\nPackets sent: %32"PRIu64
285 "\nPackets received: %28"PRIu64
286 "\nPackets dropped: %29"PRIu64,
288 port_statistics[portid].tx,
289 port_statistics[portid].rx,
290 port_statistics[portid].dropped);
292 total_packets_dropped += port_statistics[portid].dropped;
293 total_packets_tx += port_statistics[portid].tx;
294 total_packets_rx += port_statistics[portid].rx;
296 printf("\nCrypto statistics ==================================");
298 for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
299 /* skip disabled ports */
300 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
302 printf("\nStatistics for cryptodev %"PRIu64
303 " -------------------------"
304 "\nPackets enqueued: %28"PRIu64
305 "\nPackets dequeued: %28"PRIu64
306 "\nPackets errors: %30"PRIu64,
308 crypto_statistics[cdevid].enqueued,
309 crypto_statistics[cdevid].dequeued,
310 crypto_statistics[cdevid].errors);
312 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
313 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
314 total_packets_errors += crypto_statistics[cdevid].errors;
316 printf("\nAggregate statistics ==============================="
317 "\nTotal packets received: %22"PRIu64
318 "\nTotal packets enqueued: %22"PRIu64
319 "\nTotal packets dequeued: %22"PRIu64
320 "\nTotal packets sent: %26"PRIu64
321 "\nTotal packets dropped: %23"PRIu64
322 "\nTotal packets crypto errors: %17"PRIu64,
324 total_packets_enqueued,
325 total_packets_dequeued,
327 total_packets_dropped,
328 total_packets_errors);
329 printf("\n====================================================\n");
333 fill_supported_algorithm_tables(void)
337 for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++)
338 strcpy(supported_auth_algo[i], "NOT_SUPPORTED");
340 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_AES_GCM], "AES_GCM");
341 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_MD5_HMAC], "MD5_HMAC");
342 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_NULL], "NULL");
343 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA1_HMAC], "SHA1_HMAC");
344 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA224_HMAC], "SHA224_HMAC");
345 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA256_HMAC], "SHA256_HMAC");
346 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA384_HMAC], "SHA384_HMAC");
347 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA512_HMAC], "SHA512_HMAC");
348 strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SNOW3G_UIA2], "SNOW3G_UIA2");
350 for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++)
351 strcpy(supported_cipher_algo[i], "NOT_SUPPORTED");
353 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_CBC], "AES_CBC");
354 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_GCM], "AES_GCM");
355 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_NULL], "NULL");
356 strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_SNOW3G_UEA2], "SNOW3G_UEA2");
361 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
362 struct l2fwd_crypto_params *cparams)
364 struct rte_crypto_op **op_buffer;
367 op_buffer = (struct rte_crypto_op **)
368 qconf->op_buf[cparams->dev_id].buffer;
370 ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
371 cparams->qp_id, op_buffer, (uint16_t) n);
373 crypto_statistics[cparams->dev_id].enqueued += ret;
374 if (unlikely(ret < n)) {
375 crypto_statistics[cparams->dev_id].errors += (n - ret);
377 rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
378 rte_crypto_op_free(op_buffer[ret]);
386 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
387 struct l2fwd_crypto_params *cparams)
389 unsigned lcore_id, len;
390 struct lcore_queue_conf *qconf;
392 lcore_id = rte_lcore_id();
394 qconf = &lcore_queue_conf[lcore_id];
395 len = qconf->op_buf[cparams->dev_id].len;
396 qconf->op_buf[cparams->dev_id].buffer[len] = op;
399 /* enough ops to be sent */
400 if (len == MAX_PKT_BURST) {
401 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
405 qconf->op_buf[cparams->dev_id].len = len;
410 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
411 struct rte_crypto_op *op,
412 struct l2fwd_crypto_params *cparams)
414 struct ether_hdr *eth_hdr;
415 struct ipv4_hdr *ip_hdr;
417 unsigned ipdata_offset, pad_len, data_len;
420 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
422 if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
425 ipdata_offset = sizeof(struct ether_hdr);
427 ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
430 ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
431 * IPV4_IHL_MULTIPLIER;
434 /* Zero pad data to be crypto'd so it is block aligned */
435 data_len = rte_pktmbuf_data_len(m) - ipdata_offset;
436 pad_len = data_len % cparams->block_size ? cparams->block_size -
437 (data_len % cparams->block_size) : 0;
440 padding = rte_pktmbuf_append(m, pad_len);
441 if (unlikely(!padding))
445 memset(padding, 0, pad_len);
448 /* Set crypto operation data parameters */
449 rte_crypto_op_attach_sym_session(op, cparams->session);
451 if (cparams->do_hash) {
452 if (!cparams->hash_verify) {
453 /* Append space for digest to end of packet */
454 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
455 cparams->digest_length);
457 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
458 cparams->digest_length);
461 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
462 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
463 op->sym->auth.digest.length = cparams->digest_length;
465 /* For SNOW3G algorithms, offset/length must be in bits */
466 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
467 op->sym->auth.data.offset = ipdata_offset << 3;
468 op->sym->auth.data.length = data_len << 3;
470 op->sym->auth.data.offset = ipdata_offset;
471 op->sym->auth.data.length = data_len;
474 if (cparams->aad.length) {
475 op->sym->auth.aad.data = cparams->aad.data;
476 op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
477 op->sym->auth.aad.length = cparams->aad.length;
481 if (cparams->do_cipher) {
482 op->sym->cipher.iv.data = cparams->iv.data;
483 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
484 op->sym->cipher.iv.length = cparams->iv.length;
486 /* For SNOW3G algorithms, offset/length must be in bits */
487 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2) {
488 op->sym->cipher.data.offset = ipdata_offset << 3;
489 if (cparams->do_hash && cparams->hash_verify)
490 /* Do not cipher the hash tag */
491 op->sym->cipher.data.length = (data_len -
492 cparams->digest_length) << 3;
494 op->sym->cipher.data.length = data_len << 3;
497 op->sym->cipher.data.offset = ipdata_offset;
498 if (cparams->do_hash && cparams->hash_verify)
499 /* Do not cipher the hash tag */
500 op->sym->cipher.data.length = data_len -
501 cparams->digest_length;
503 op->sym->cipher.data.length = data_len;
509 return l2fwd_crypto_enqueue(op, cparams);
513 /* Send the burst of packets on an output interface */
515 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
518 struct rte_mbuf **pkt_buffer;
521 pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
523 ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
524 port_statistics[port].tx += ret;
525 if (unlikely(ret < n)) {
526 port_statistics[port].dropped += (n - ret);
528 rte_pktmbuf_free(pkt_buffer[ret]);
535 /* Enqueue packets for TX and prepare them to be sent */
537 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
539 unsigned lcore_id, len;
540 struct lcore_queue_conf *qconf;
542 lcore_id = rte_lcore_id();
544 qconf = &lcore_queue_conf[lcore_id];
545 len = qconf->pkt_buf[port].len;
546 qconf->pkt_buf[port].buffer[len] = m;
549 /* enough pkts to be sent */
550 if (unlikely(len == MAX_PKT_BURST)) {
551 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
555 qconf->pkt_buf[port].len = len;
560 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
562 struct ether_hdr *eth;
566 dst_port = l2fwd_dst_ports[portid];
567 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
569 /* 02:00:00:00:00:xx */
570 tmp = ð->d_addr.addr_bytes[0];
571 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
574 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
576 l2fwd_send_packet(m, (uint8_t) dst_port);
579 /** Generate random key */
581 generate_random_key(uint8_t *key, unsigned length)
585 for (i = 0; i < length; i++)
586 key[i] = rand() % 0xff;
589 static struct rte_cryptodev_sym_session *
590 initialize_crypto_session(struct l2fwd_crypto_options *options,
593 struct rte_crypto_sym_xform *first_xform;
595 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
596 first_xform = &options->cipher_xform;
597 first_xform->next = &options->auth_xform;
598 } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
599 first_xform = &options->auth_xform;
600 first_xform->next = &options->cipher_xform;
601 } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
602 first_xform = &options->cipher_xform;
604 first_xform = &options->auth_xform;
607 /* Setup Cipher Parameters */
608 return rte_cryptodev_sym_session_create(cdev_id, first_xform);
612 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
614 /* main processing loop */
616 l2fwd_main_loop(struct l2fwd_crypto_options *options)
618 struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
619 struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
621 unsigned lcore_id = rte_lcore_id();
622 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
623 unsigned i, j, portid, nb_rx;
624 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
625 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
626 US_PER_S * BURST_TX_DRAIN_US;
627 struct l2fwd_crypto_params *cparams;
628 struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
630 if (qconf->nb_rx_ports == 0) {
631 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
635 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
637 l2fwd_crypto_options_print(options);
639 for (i = 0; i < qconf->nb_rx_ports; i++) {
641 portid = qconf->rx_port_list[i];
642 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
646 for (i = 0; i < qconf->nb_crypto_devs; i++) {
647 port_cparams[i].do_cipher = 0;
648 port_cparams[i].do_hash = 0;
650 switch (options->xform_chain) {
651 case L2FWD_CRYPTO_CIPHER_HASH:
652 case L2FWD_CRYPTO_HASH_CIPHER:
653 port_cparams[i].do_cipher = 1;
654 port_cparams[i].do_hash = 1;
656 case L2FWD_CRYPTO_HASH_ONLY:
657 port_cparams[i].do_hash = 1;
659 case L2FWD_CRYPTO_CIPHER_ONLY:
660 port_cparams[i].do_cipher = 1;
664 port_cparams[i].dev_id = qconf->cryptodev_list[i];
665 port_cparams[i].qp_id = 0;
667 port_cparams[i].block_size = options->block_size;
669 if (port_cparams[i].do_hash) {
670 port_cparams[i].digest_length =
671 options->auth_xform.auth.digest_length;
672 if (options->auth_xform.auth.add_auth_data_length) {
673 port_cparams[i].aad.data = options->aad.data;
674 port_cparams[i].aad.length =
675 options->auth_xform.auth.add_auth_data_length;
676 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
677 if (!options->aad_param)
678 generate_random_key(port_cparams[i].aad.data,
679 port_cparams[i].aad.length);
683 if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
684 port_cparams[i].hash_verify = 1;
686 port_cparams[i].hash_verify = 0;
688 port_cparams[i].auth_algo = options->auth_xform.auth.algo;
691 if (port_cparams[i].do_cipher) {
692 port_cparams[i].iv.data = options->iv.data;
693 port_cparams[i].iv.length = options->iv.length;
694 port_cparams[i].iv.phys_addr = options->iv.phys_addr;
695 if (!options->iv_param)
696 generate_random_key(port_cparams[i].iv.data,
697 port_cparams[i].iv.length);
699 port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
702 port_cparams[i].session = initialize_crypto_session(options,
703 port_cparams[i].dev_id);
705 if (port_cparams[i].session == NULL)
707 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
708 port_cparams[i].dev_id);
713 cur_tsc = rte_rdtsc();
716 * TX burst queue drain
718 diff_tsc = cur_tsc - prev_tsc;
719 if (unlikely(diff_tsc > drain_tsc)) {
720 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
721 if (qconf->pkt_buf[portid].len == 0)
723 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
724 qconf->pkt_buf[portid].len,
726 qconf->pkt_buf[portid].len = 0;
729 /* if timer is enabled */
730 if (timer_period > 0) {
732 /* advance the timer */
733 timer_tsc += diff_tsc;
735 /* if timer has reached its timeout */
736 if (unlikely(timer_tsc >=
737 (uint64_t)timer_period)) {
739 /* do this only on master core */
740 if (lcore_id == rte_get_master_lcore()
741 && options->refresh_period) {
752 * Read packet from RX queues
754 for (i = 0; i < qconf->nb_rx_ports; i++) {
755 portid = qconf->rx_port_list[i];
757 cparams = &port_cparams[i];
759 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
760 pkts_burst, MAX_PKT_BURST);
762 port_statistics[portid].rx += nb_rx;
766 * If we can't allocate a crypto_ops, then drop
767 * the rest of the burst and dequeue and
768 * process the packets to free offload structs
770 if (rte_crypto_op_bulk_alloc(
771 l2fwd_crypto_op_pool,
772 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
775 for (j = 0; j < nb_rx; j++)
776 rte_pktmbuf_free(pkts_burst[i]);
781 /* Enqueue packets from Crypto device*/
782 for (j = 0; j < nb_rx; j++) {
785 l2fwd_simple_crypto_enqueue(m,
786 ops_burst[j], cparams);
790 /* Dequeue packets from Crypto device */
792 nb_rx = rte_cryptodev_dequeue_burst(
793 cparams->dev_id, cparams->qp_id,
794 ops_burst, MAX_PKT_BURST);
796 crypto_statistics[cparams->dev_id].dequeued +=
799 /* Forward crypto'd packets */
800 for (j = 0; j < nb_rx; j++) {
801 m = ops_burst[j]->sym->m_src;
803 rte_crypto_op_free(ops_burst[j]);
804 l2fwd_simple_forward(m, portid);
806 } while (nb_rx == MAX_PKT_BURST);
812 l2fwd_launch_one_lcore(void *arg)
814 l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
818 /* Display command line arguments usage */
820 l2fwd_crypto_usage(const char *prgname)
822 printf("%s [EAL options] --\n"
823 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
824 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
825 " -s manage all ports from single lcore\n"
826 " -T PERIOD: statistics will be refreshed each PERIOD seconds"
827 " (0 to disable, 10 default, 86400 maximum)\n"
829 " --cdev_type HW / SW / ANY\n"
830 " --chain HASH_CIPHER / CIPHER_HASH\n"
832 " --cipher_algo ALGO\n"
833 " --cipher_op ENCRYPT / DECRYPT\n"
834 " --cipher_key KEY (bytes separated with \":\")\n"
835 " --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
836 " --iv IV (bytes separated with \":\")\n"
837 " --iv_random_size SIZE: size of IV when generated randomly\n"
839 " --auth_algo ALGO\n"
840 " --auth_op GENERATE / VERIFY\n"
841 " --auth_key KEY (bytes separated with \":\")\n"
842 " --auth_key_random_size SIZE: size of auth key when generated randomly\n"
843 " --aad AAD (bytes separated with \":\")\n"
844 " --aad_random_size SIZE: size of AAD when generated randomly\n"
845 " --digest_size SIZE: size of digest to be generated/verified\n"
851 /** Parse crypto device type command line argument */
853 parse_cryptodev_type(enum cdev_type *type, char *optarg)
855 if (strcmp("HW", optarg) == 0) {
856 *type = CDEV_TYPE_HW;
858 } else if (strcmp("SW", optarg) == 0) {
859 *type = CDEV_TYPE_SW;
861 } else if (strcmp("ANY", optarg) == 0) {
862 *type = CDEV_TYPE_ANY;
869 /** Parse crypto chain xform command line argument */
871 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
873 if (strcmp("CIPHER_HASH", optarg) == 0) {
874 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
876 } else if (strcmp("HASH_CIPHER", optarg) == 0) {
877 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
879 } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
880 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
882 } else if (strcmp("HASH_ONLY", optarg) == 0) {
883 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
890 /** Parse crypto cipher algo option command line argument */
892 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
896 for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++) {
897 if (!strcmp(supported_cipher_algo[i], optarg)) {
903 printf("Cipher algorithm not supported!\n");
907 /** Parse crypto cipher operation command line argument */
909 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
911 if (strcmp("ENCRYPT", optarg) == 0) {
912 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
914 } else if (strcmp("DECRYPT", optarg) == 0) {
915 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
919 printf("Cipher operation not supported!\n");
923 /** Parse crypto key command line argument */
925 parse_key(uint8_t *data, char *input_arg)
930 for (byte_count = 0, token = strtok(input_arg, ":");
931 (byte_count < MAX_KEY_SIZE) && (token != NULL);
932 token = strtok(NULL, ":")) {
934 int number = (int)strtol(token, NULL, 16);
936 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
939 data[byte_count++] = (uint8_t)number;
945 /** Parse size param*/
947 parse_size(int *size, const char *q_arg)
952 /* parse hexadecimal string */
953 n = strtoul(q_arg, &end, 10);
954 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
958 printf("invalid size\n");
966 /** Parse crypto cipher operation command line argument */
968 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
972 for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++) {
973 if (!strcmp(supported_auth_algo[i], optarg)) {
979 printf("Authentication algorithm specified not supported!\n");
984 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
986 if (strcmp("VERIFY", optarg) == 0) {
987 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
989 } else if (strcmp("GENERATE", optarg) == 0) {
990 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
994 printf("Authentication operation specified not supported!\n");
998 /** Parse long options */
1000 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1001 struct option *lgopts, int option_index)
1005 if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1006 retval = parse_cryptodev_type(&options->type, optarg);
1008 strcpy(options->string_type, optarg);
1012 else if (strcmp(lgopts[option_index].name, "chain") == 0)
1013 return parse_crypto_opt_chain(options, optarg);
1015 /* Cipher options */
1016 else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1017 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1020 else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1021 return parse_cipher_op(&options->cipher_xform.cipher.op,
1024 else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1025 options->ckey_param = 1;
1026 options->cipher_xform.cipher.key.length =
1027 parse_key(options->cipher_xform.cipher.key.data, optarg);
1028 if (options->cipher_xform.cipher.key.length > 0)
1034 else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1035 return parse_size(&options->ckey_random_size, optarg);
1037 else if (strcmp(lgopts[option_index].name, "iv") == 0) {
1038 options->iv_param = 1;
1039 options->iv.length =
1040 parse_key(options->iv.data, optarg);
1041 if (options->iv.length > 0)
1047 else if (strcmp(lgopts[option_index].name, "iv_random_size") == 0)
1048 return parse_size(&options->iv_random_size, optarg);
1050 /* Authentication options */
1051 else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1052 return parse_auth_algo(&options->auth_xform.auth.algo,
1056 else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1057 return parse_auth_op(&options->auth_xform.auth.op,
1060 else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1061 options->akey_param = 1;
1062 options->auth_xform.auth.key.length =
1063 parse_key(options->auth_xform.auth.key.data, optarg);
1064 if (options->auth_xform.auth.key.length > 0)
1070 else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1071 return parse_size(&options->akey_random_size, optarg);
1074 else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1075 options->aad_param = 1;
1076 options->aad.length =
1077 parse_key(options->aad.data, optarg);
1078 if (options->aad.length > 0)
1084 else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1085 return parse_size(&options->aad_random_size, optarg);
1088 else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1089 return parse_size(&options->digest_size, optarg);
1092 else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1093 options->sessionless = 1;
1100 /** Parse port mask */
1102 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1108 /* parse hexadecimal string */
1109 pm = strtoul(q_arg, &end, 16);
1110 if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1113 options->portmask = pm;
1114 if (options->portmask == 0) {
1115 printf("invalid portmask specified\n");
1122 /** Parse number of queues */
1124 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1130 /* parse hexadecimal string */
1131 n = strtoul(q_arg, &end, 10);
1132 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1134 else if (n >= MAX_RX_QUEUE_PER_LCORE)
1137 options->nb_ports_per_lcore = n;
1138 if (options->nb_ports_per_lcore == 0) {
1139 printf("invalid number of ports selected\n");
1146 /** Parse timer period */
1148 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1154 /* parse number string */
1155 n = (unsigned)strtol(q_arg, &end, 10);
1156 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1159 if (n >= MAX_TIMER_PERIOD) {
1160 printf("Warning refresh period specified %lu is greater than "
1161 "max value %lu! using max value",
1162 n, MAX_TIMER_PERIOD);
1163 n = MAX_TIMER_PERIOD;
1166 options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1171 /** Generate default options for application */
1173 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1177 options->portmask = 0xffffffff;
1178 options->nb_ports_per_lcore = 1;
1179 options->refresh_period = 10000;
1180 options->single_lcore = 0;
1181 options->sessionless = 0;
1183 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1186 options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1187 options->cipher_xform.next = NULL;
1188 options->ckey_param = 0;
1189 options->ckey_random_size = -1;
1190 options->cipher_xform.cipher.key.length = 0;
1191 options->iv_param = 0;
1192 options->iv_random_size = -1;
1193 options->iv.length = 0;
1195 options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1196 options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1198 /* Authentication Data */
1199 options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1200 options->auth_xform.next = NULL;
1201 options->akey_param = 0;
1202 options->akey_random_size = -1;
1203 options->auth_xform.auth.key.length = 0;
1204 options->aad_param = 0;
1205 options->aad_random_size = -1;
1206 options->aad.length = 0;
1207 options->digest_size = -1;
1209 options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1210 options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1212 options->type = CDEV_TYPE_ANY;
1216 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1218 printf("Options:-\nn");
1219 printf("portmask: %x\n", options->portmask);
1220 printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1221 printf("refresh period : %u\n", options->refresh_period);
1222 printf("single lcore mode: %s\n",
1223 options->single_lcore ? "enabled" : "disabled");
1224 printf("stats_printing: %s\n",
1225 options->refresh_period == 0 ? "disabled" : "enabled");
1227 printf("sessionless crypto: %s\n",
1228 options->sessionless ? "enabled" : "disabled");
1231 /* Parse the argument given in the command line of the application */
1233 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1234 int argc, char **argv)
1236 int opt, retval, option_index;
1237 char **argvopt = argv, *prgname = argv[0];
1239 static struct option lgopts[] = {
1240 { "sessionless", no_argument, 0, 0 },
1242 { "cdev_type", required_argument, 0, 0 },
1243 { "chain", required_argument, 0, 0 },
1245 { "cipher_algo", required_argument, 0, 0 },
1246 { "cipher_op", required_argument, 0, 0 },
1247 { "cipher_key", required_argument, 0, 0 },
1248 { "cipher_key_random_size", required_argument, 0, 0 },
1250 { "auth_algo", required_argument, 0, 0 },
1251 { "auth_op", required_argument, 0, 0 },
1252 { "auth_key", required_argument, 0, 0 },
1253 { "auth_key_random_size", required_argument, 0, 0 },
1255 { "iv", required_argument, 0, 0 },
1256 { "iv_random_size", required_argument, 0, 0 },
1257 { "aad", required_argument, 0, 0 },
1258 { "aad_random_size", required_argument, 0, 0 },
1259 { "digest_size", required_argument, 0, 0 },
1261 { "sessionless", no_argument, 0, 0 },
1266 l2fwd_crypto_default_options(options);
1268 while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1269 &option_index)) != EOF) {
1273 retval = l2fwd_crypto_parse_args_long_options(options,
1274 lgopts, option_index);
1276 l2fwd_crypto_usage(prgname);
1283 retval = l2fwd_crypto_parse_portmask(options, optarg);
1285 l2fwd_crypto_usage(prgname);
1292 retval = l2fwd_crypto_parse_nqueue(options, optarg);
1294 l2fwd_crypto_usage(prgname);
1301 options->single_lcore = 1;
1307 retval = l2fwd_crypto_parse_timer_period(options,
1310 l2fwd_crypto_usage(prgname);
1316 l2fwd_crypto_usage(prgname);
1323 argv[optind-1] = prgname;
1326 optind = 0; /* reset getopt lib */
1331 /* Check the link status of all ports in up to 9s, and print them finally */
1333 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1335 #define CHECK_INTERVAL 100 /* 100ms */
1336 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1337 uint8_t portid, count, all_ports_up, print_flag = 0;
1338 struct rte_eth_link link;
1340 printf("\nChecking link status");
1342 for (count = 0; count <= MAX_CHECK_TIME; count++) {
1344 for (portid = 0; portid < port_num; portid++) {
1345 if ((port_mask & (1 << portid)) == 0)
1347 memset(&link, 0, sizeof(link));
1348 rte_eth_link_get_nowait(portid, &link);
1349 /* print link status if flag set */
1350 if (print_flag == 1) {
1351 if (link.link_status)
1352 printf("Port %d Link Up - speed %u "
1353 "Mbps - %s\n", (uint8_t)portid,
1354 (unsigned)link.link_speed,
1355 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1356 ("full-duplex") : ("half-duplex\n"));
1358 printf("Port %d Link Down\n",
1362 /* clear all_ports_up flag if any link down */
1363 if (link.link_status == 0) {
1368 /* after finally printing all link status, get out */
1369 if (print_flag == 1)
1372 if (all_ports_up == 0) {
1375 rte_delay_ms(CHECK_INTERVAL);
1378 /* set the print_flag if all ports up or timeout */
1379 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1386 /* Check if device has to be HW/SW or any */
1388 check_type(struct l2fwd_crypto_options *options, struct rte_cryptodev_info *dev_info)
1390 if (options->type == CDEV_TYPE_HW &&
1391 (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1393 if (options->type == CDEV_TYPE_SW &&
1394 !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1396 if (options->type == CDEV_TYPE_ANY)
1403 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1408 for (supp_size = min; supp_size <= max; supp_size += increment) {
1409 if (length == supp_size)
1416 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1417 uint8_t *enabled_cdevs)
1419 unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1420 const struct rte_cryptodev_capabilities *cap;
1421 enum rte_crypto_auth_algorithm cap_auth_algo;
1422 enum rte_crypto_auth_algorithm opt_auth_algo;
1423 enum rte_crypto_cipher_algorithm cap_cipher_algo;
1424 enum rte_crypto_cipher_algorithm opt_cipher_algo;
1427 cdev_count = rte_cryptodev_count();
1428 if (cdev_count == 0) {
1429 printf("No crypto devices available\n");
1433 for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1435 struct rte_cryptodev_qp_conf qp_conf;
1436 struct rte_cryptodev_info dev_info;
1438 struct rte_cryptodev_config conf = {
1439 .nb_queue_pairs = 1,
1440 .socket_id = SOCKET_ID_ANY,
1447 rte_cryptodev_info_get(cdev_id, &dev_info);
1449 /* Set cipher parameters */
1450 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1451 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1452 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
1453 /* Check if device supports cipher algo */
1455 opt_cipher_algo = options->cipher_xform.cipher.algo;
1456 cap = &dev_info.capabilities[i];
1457 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1458 cap_cipher_algo = cap->sym.cipher.algo;
1459 if (cap->sym.xform_type ==
1460 RTE_CRYPTO_SYM_XFORM_CIPHER) {
1461 if (cap_cipher_algo == opt_cipher_algo) {
1462 if (check_type(options, &dev_info) == 0)
1466 cap = &dev_info.capabilities[++i];
1469 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1470 printf("Algorithm %s not supported by cryptodev %u"
1471 " or device not of preferred type (%s)\n",
1472 supported_cipher_algo[opt_cipher_algo],
1474 options->string_type);
1478 options->block_size = cap->sym.cipher.block_size;
1480 * Check if length of provided IV is supported
1481 * by the algorithm chosen.
1483 if (options->iv_param) {
1484 if (check_supported_size(options->iv.length,
1485 cap->sym.cipher.iv_size.min,
1486 cap->sym.cipher.iv_size.max,
1487 cap->sym.cipher.iv_size.increment)
1489 printf("Unsupported IV length\n");
1493 * Check if length of IV to be randomly generated
1494 * is supported by the algorithm chosen.
1496 } else if (options->iv_random_size != -1) {
1497 if (check_supported_size(options->iv_random_size,
1498 cap->sym.cipher.iv_size.min,
1499 cap->sym.cipher.iv_size.max,
1500 cap->sym.cipher.iv_size.increment)
1502 printf("Unsupported IV length\n");
1505 options->iv.length = options->iv_random_size;
1506 /* No size provided, use minimum size. */
1508 options->iv.length = cap->sym.cipher.iv_size.min;
1511 * Check if length of provided cipher key is supported
1512 * by the algorithm chosen.
1514 if (options->ckey_param) {
1515 if (check_supported_size(
1516 options->cipher_xform.cipher.key.length,
1517 cap->sym.cipher.key_size.min,
1518 cap->sym.cipher.key_size.max,
1519 cap->sym.cipher.key_size.increment)
1521 printf("Unsupported cipher key length\n");
1525 * Check if length of the cipher key to be randomly generated
1526 * is supported by the algorithm chosen.
1528 } else if (options->ckey_random_size != -1) {
1529 if (check_supported_size(options->ckey_random_size,
1530 cap->sym.cipher.key_size.min,
1531 cap->sym.cipher.key_size.max,
1532 cap->sym.cipher.key_size.increment)
1534 printf("Unsupported cipher key length\n");
1537 options->cipher_xform.cipher.key.length =
1538 options->ckey_random_size;
1539 /* No size provided, use minimum size. */
1541 options->cipher_xform.cipher.key.length =
1542 cap->sym.cipher.key_size.min;
1544 if (!options->ckey_param)
1545 generate_random_key(
1546 options->cipher_xform.cipher.key.data,
1547 options->cipher_xform.cipher.key.length);
1551 /* Set auth parameters */
1552 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1553 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1554 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
1555 /* Check if device supports auth algo */
1557 opt_auth_algo = options->auth_xform.auth.algo;
1558 cap = &dev_info.capabilities[i];
1559 while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1560 cap_auth_algo = cap->sym.auth.algo;
1561 if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
1562 (cap_auth_algo == opt_auth_algo) &&
1563 (check_type(options, &dev_info) == 0)) {
1566 cap = &dev_info.capabilities[++i];
1569 if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1570 printf("Algorithm %s not supported by cryptodev %u"
1571 " or device not of preferred type (%s)\n",
1572 supported_auth_algo[opt_auth_algo],
1574 options->string_type);
1578 options->block_size = cap->sym.auth.block_size;
1580 * Check if length of provided AAD is supported
1581 * by the algorithm chosen.
1583 if (options->aad_param) {
1584 if (check_supported_size(options->aad.length,
1585 cap->sym.auth.aad_size.min,
1586 cap->sym.auth.aad_size.max,
1587 cap->sym.auth.aad_size.increment)
1589 printf("Unsupported AAD length\n");
1593 * Check if length of AAD to be randomly generated
1594 * is supported by the algorithm chosen.
1596 } else if (options->aad_random_size != -1) {
1597 if (check_supported_size(options->aad_random_size,
1598 cap->sym.auth.aad_size.min,
1599 cap->sym.auth.aad_size.max,
1600 cap->sym.auth.aad_size.increment)
1602 printf("Unsupported AAD length\n");
1605 options->aad.length = options->aad_random_size;
1606 /* No size provided, use minimum size. */
1608 options->aad.length = cap->sym.auth.aad_size.min;
1610 options->auth_xform.auth.add_auth_data_length =
1611 options->aad.length;
1614 * Check if length of provided auth key is supported
1615 * by the algorithm chosen.
1617 if (options->akey_param) {
1618 if (check_supported_size(
1619 options->auth_xform.auth.key.length,
1620 cap->sym.auth.key_size.min,
1621 cap->sym.auth.key_size.max,
1622 cap->sym.auth.key_size.increment)
1624 printf("Unsupported auth key length\n");
1628 * Check if length of the auth key to be randomly generated
1629 * is supported by the algorithm chosen.
1631 } else if (options->akey_random_size != -1) {
1632 if (check_supported_size(options->akey_random_size,
1633 cap->sym.auth.key_size.min,
1634 cap->sym.auth.key_size.max,
1635 cap->sym.auth.key_size.increment)
1637 printf("Unsupported auth key length\n");
1640 options->auth_xform.auth.key.length =
1641 options->akey_random_size;
1642 /* No size provided, use minimum size. */
1644 options->auth_xform.auth.key.length =
1645 cap->sym.auth.key_size.min;
1647 if (!options->akey_param)
1648 generate_random_key(
1649 options->auth_xform.auth.key.data,
1650 options->auth_xform.auth.key.length);
1652 /* Check if digest size is supported by the algorithm. */
1653 if (options->digest_size != -1) {
1654 if (check_supported_size(options->digest_size,
1655 cap->sym.auth.digest_size.min,
1656 cap->sym.auth.digest_size.max,
1657 cap->sym.auth.digest_size.increment)
1659 printf("Unsupported digest length\n");
1662 options->auth_xform.auth.digest_length =
1663 options->digest_size;
1664 /* No size provided, use minimum size. */
1666 options->auth_xform.auth.digest_length =
1667 cap->sym.auth.digest_size.min;
1670 retval = rte_cryptodev_configure(cdev_id, &conf);
1672 printf("Failed to configure cryptodev %u", cdev_id);
1676 qp_conf.nb_descriptors = 2048;
1678 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1681 printf("Failed to setup queue pair %u on cryptodev %u",
1686 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1688 enabled_cdevs[cdev_id] = 1;
1689 enabled_cdev_count++;
1692 return enabled_cdev_count;
1696 initialize_ports(struct l2fwd_crypto_options *options)
1698 uint8_t last_portid, portid;
1699 unsigned enabled_portcount = 0;
1700 unsigned nb_ports = rte_eth_dev_count();
1702 if (nb_ports == 0) {
1703 printf("No Ethernet ports - bye\n");
1707 if (nb_ports > RTE_MAX_ETHPORTS)
1708 nb_ports = RTE_MAX_ETHPORTS;
1710 /* Reset l2fwd_dst_ports */
1711 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1712 l2fwd_dst_ports[portid] = 0;
1714 for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1717 /* Skip ports that are not enabled */
1718 if ((options->portmask & (1 << portid)) == 0)
1722 printf("Initializing port %u... ", (unsigned) portid);
1724 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1726 printf("Cannot configure device: err=%d, port=%u\n",
1727 retval, (unsigned) portid);
1731 /* init one RX queue */
1733 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1734 rte_eth_dev_socket_id(portid),
1735 NULL, l2fwd_pktmbuf_pool);
1737 printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1738 retval, (unsigned) portid);
1742 /* init one TX queue on each port */
1744 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1745 rte_eth_dev_socket_id(portid),
1748 printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1749 retval, (unsigned) portid);
1755 retval = rte_eth_dev_start(portid);
1757 printf("rte_eth_dev_start:err=%d, port=%u\n",
1758 retval, (unsigned) portid);
1762 rte_eth_promiscuous_enable(portid);
1764 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1766 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1768 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1769 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1770 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1771 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1772 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1773 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1775 /* initialize port stats */
1776 memset(&port_statistics, 0, sizeof(port_statistics));
1778 /* Setup port forwarding table */
1779 if (enabled_portcount % 2) {
1780 l2fwd_dst_ports[portid] = last_portid;
1781 l2fwd_dst_ports[last_portid] = portid;
1783 last_portid = portid;
1786 l2fwd_enabled_port_mask |= (1 << portid);
1787 enabled_portcount++;
1790 if (enabled_portcount == 1) {
1791 l2fwd_dst_ports[last_portid] = last_portid;
1792 } else if (enabled_portcount % 2) {
1793 printf("odd number of ports in portmask- bye\n");
1797 check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1799 return enabled_portcount;
1803 reserve_key_memory(struct l2fwd_crypto_options *options)
1805 options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1807 if (options->cipher_xform.cipher.key.data == NULL)
1808 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1811 options->auth_xform.auth.key.data = rte_malloc("auth key",
1813 if (options->auth_xform.auth.key.data == NULL)
1814 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1816 options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1817 if (options->iv.data == NULL)
1818 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1819 options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1821 options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1822 if (options->aad.data == NULL)
1823 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1824 options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1828 main(int argc, char **argv)
1830 struct lcore_queue_conf *qconf;
1831 struct l2fwd_crypto_options options;
1833 uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1834 unsigned lcore_id, rx_lcore_id;
1835 int ret, enabled_cdevcount, enabled_portcount;
1836 uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
1839 ret = rte_eal_init(argc, argv);
1841 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1845 /* reserve memory for Cipher/Auth key and IV */
1846 reserve_key_memory(&options);
1848 /* fill out the supported algorithm tables */
1849 fill_supported_algorithm_tables();
1851 /* parse application arguments (after the EAL ones) */
1852 ret = l2fwd_crypto_parse_args(&options, argc, argv);
1854 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1856 /* create the mbuf pool */
1857 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1858 sizeof(struct rte_crypto_op),
1859 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1860 if (l2fwd_pktmbuf_pool == NULL)
1861 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1863 /* create crypto op pool */
1864 l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1865 RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1867 if (l2fwd_crypto_op_pool == NULL)
1868 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1870 /* Enable Ethernet ports */
1871 enabled_portcount = initialize_ports(&options);
1872 if (enabled_portcount < 1)
1873 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1875 nb_ports = rte_eth_dev_count();
1876 /* Initialize the port/queue configuration of each logical core */
1877 for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1878 portid < nb_ports; portid++) {
1880 /* skip ports that are not enabled */
1881 if ((options.portmask & (1 << portid)) == 0)
1884 if (options.single_lcore && qconf == NULL) {
1885 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1887 if (rx_lcore_id >= RTE_MAX_LCORE)
1888 rte_exit(EXIT_FAILURE,
1889 "Not enough cores\n");
1891 } else if (!options.single_lcore) {
1892 /* get the lcore_id for this port */
1893 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1894 lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1895 options.nb_ports_per_lcore) {
1897 if (rx_lcore_id >= RTE_MAX_LCORE)
1898 rte_exit(EXIT_FAILURE,
1899 "Not enough cores\n");
1903 /* Assigned a new logical core in the loop above. */
1904 if (qconf != &lcore_queue_conf[rx_lcore_id])
1905 qconf = &lcore_queue_conf[rx_lcore_id];
1907 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1908 qconf->nb_rx_ports++;
1910 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1913 /* Enable Crypto devices */
1914 enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
1916 if (enabled_cdevcount < 0)
1917 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
1919 if (enabled_cdevcount < enabled_portcount)
1920 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
1921 "has to be more or equal to number of ports (%d)\n",
1922 enabled_cdevcount, enabled_portcount);
1924 nb_cryptodevs = rte_cryptodev_count();
1926 /* Initialize the port/cryptodev configuration of each logical core */
1927 for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1928 cdev_id < nb_cryptodevs && enabled_cdevcount;
1930 /* Crypto op not supported by crypto device */
1931 if (!enabled_cdevs[cdev_id])
1934 if (options.single_lcore && qconf == NULL) {
1935 while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1937 if (rx_lcore_id >= RTE_MAX_LCORE)
1938 rte_exit(EXIT_FAILURE,
1939 "Not enough cores\n");
1941 } else if (!options.single_lcore) {
1942 /* get the lcore_id for this port */
1943 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1944 lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1945 options.nb_ports_per_lcore) {
1947 if (rx_lcore_id >= RTE_MAX_LCORE)
1948 rte_exit(EXIT_FAILURE,
1949 "Not enough cores\n");
1953 /* Assigned a new logical core in the loop above. */
1954 if (qconf != &lcore_queue_conf[rx_lcore_id])
1955 qconf = &lcore_queue_conf[rx_lcore_id];
1957 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1958 qconf->nb_crypto_devs++;
1960 enabled_cdevcount--;
1962 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1966 /* launch per-lcore init on every lcore */
1967 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1969 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1970 if (rte_eal_wait_lcore(lcore_id) < 0)