ef4530483a73267e60c7ee19f46af84a4b7ac517
[dpdk.git] / examples / l2fwd-crypto / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <setjmp.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <getopt.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
54 #include <rte_cryptodev.h>
55 #include <rte_cycles.h>
56 #include <rte_debug.h>
57 #include <rte_eal.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_interrupts.h>
61 #include <rte_ip.h>
62 #include <rte_launch.h>
63 #include <rte_lcore.h>
64 #include <rte_log.h>
65 #include <rte_malloc.h>
66 #include <rte_mbuf.h>
67 #include <rte_memcpy.h>
68 #include <rte_memory.h>
69 #include <rte_mempool.h>
70 #include <rte_memzone.h>
71 #include <rte_pci.h>
72 #include <rte_per_lcore.h>
73 #include <rte_prefetch.h>
74 #include <rte_random.h>
75 #include <rte_hexdump.h>
76
77 enum cdev_type {
78         CDEV_TYPE_ANY,
79         CDEV_TYPE_HW,
80         CDEV_TYPE_SW
81 };
82
83 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
84
85 #define NB_MBUF   8192
86
87 #define MAX_STR_LEN 32
88 #define MAX_KEY_SIZE 128
89 #define MAX_PKT_BURST 32
90 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91
92 #define MAXIMUM_IV_LENGTH       16
93 #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
94                                 sizeof(struct rte_crypto_sym_op))
95
96 /*
97  * Configurable number of RX/TX ring descriptors
98  */
99 #define RTE_TEST_RX_DESC_DEFAULT 128
100 #define RTE_TEST_TX_DESC_DEFAULT 512
101
102 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
103 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
104
105 /* ethernet addresses of ports */
106 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
107
108 /* mask of enabled ports */
109 static uint64_t l2fwd_enabled_port_mask;
110 static uint64_t l2fwd_enabled_crypto_mask;
111
112 /* list of enabled ports */
113 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
114
115
116 struct pkt_buffer {
117         unsigned len;
118         struct rte_mbuf *buffer[MAX_PKT_BURST];
119 };
120
121 struct op_buffer {
122         unsigned len;
123         struct rte_crypto_op *buffer[MAX_PKT_BURST];
124 };
125
126 #define MAX_RX_QUEUE_PER_LCORE 16
127 #define MAX_TX_QUEUE_PER_PORT 16
128
129 enum l2fwd_crypto_xform_chain {
130         L2FWD_CRYPTO_CIPHER_HASH,
131         L2FWD_CRYPTO_HASH_CIPHER,
132         L2FWD_CRYPTO_CIPHER_ONLY,
133         L2FWD_CRYPTO_HASH_ONLY
134 };
135
136 struct l2fwd_key {
137         uint8_t *data;
138         uint32_t length;
139         phys_addr_t phys_addr;
140 };
141
142 struct l2fwd_iv {
143         uint8_t *data;
144         uint16_t length;
145 };
146
147 /** l2fwd crypto application command line options */
148 struct l2fwd_crypto_options {
149         unsigned portmask;
150         unsigned nb_ports_per_lcore;
151         unsigned refresh_period;
152         unsigned single_lcore:1;
153
154         enum cdev_type type;
155         unsigned sessionless:1;
156
157         enum l2fwd_crypto_xform_chain xform_chain;
158
159         struct rte_crypto_sym_xform cipher_xform;
160         unsigned ckey_param;
161         int ckey_random_size;
162
163         struct l2fwd_iv cipher_iv;
164         unsigned int cipher_iv_param;
165         int cipher_iv_random_size;
166
167         struct rte_crypto_sym_xform auth_xform;
168         uint8_t akey_param;
169         int akey_random_size;
170
171         struct l2fwd_iv auth_iv;
172         unsigned int auth_iv_param;
173         int auth_iv_random_size;
174
175         struct l2fwd_key aad;
176         unsigned aad_param;
177         int aad_random_size;
178
179         int digest_size;
180
181         uint16_t block_size;
182         char string_type[MAX_STR_LEN];
183
184         uint64_t cryptodev_mask;
185 };
186
187 /** l2fwd crypto lcore params */
188 struct l2fwd_crypto_params {
189         uint8_t dev_id;
190         uint8_t qp_id;
191
192         unsigned digest_length;
193         unsigned block_size;
194
195         struct l2fwd_iv cipher_iv;
196         struct l2fwd_iv auth_iv;
197         struct l2fwd_key aad;
198         struct rte_cryptodev_sym_session *session;
199
200         uint8_t do_cipher;
201         uint8_t do_hash;
202         uint8_t hash_verify;
203
204         enum rte_crypto_cipher_algorithm cipher_algo;
205         enum rte_crypto_auth_algorithm auth_algo;
206 };
207
208 /** lcore configuration */
209 struct lcore_queue_conf {
210         unsigned nb_rx_ports;
211         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
212
213         unsigned nb_crypto_devs;
214         unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
215
216         struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
217         struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
218 } __rte_cache_aligned;
219
220 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
221
222 static const struct rte_eth_conf port_conf = {
223         .rxmode = {
224                 .mq_mode = ETH_MQ_RX_NONE,
225                 .max_rx_pkt_len = ETHER_MAX_LEN,
226                 .split_hdr_size = 0,
227                 .header_split   = 0, /**< Header Split disabled */
228                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
229                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
230                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
231                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
232         },
233         .txmode = {
234                 .mq_mode = ETH_MQ_TX_NONE,
235         },
236 };
237
238 struct rte_mempool *l2fwd_pktmbuf_pool;
239 struct rte_mempool *l2fwd_crypto_op_pool;
240
241 /* Per-port statistics struct */
242 struct l2fwd_port_statistics {
243         uint64_t tx;
244         uint64_t rx;
245
246         uint64_t crypto_enqueued;
247         uint64_t crypto_dequeued;
248
249         uint64_t dropped;
250 } __rte_cache_aligned;
251
252 struct l2fwd_crypto_statistics {
253         uint64_t enqueued;
254         uint64_t dequeued;
255
256         uint64_t errors;
257 } __rte_cache_aligned;
258
259 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
260 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
261
262 /* A tsc-based timer responsible for triggering statistics printout */
263 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
264 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
265
266 /* default period is 10 seconds */
267 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
268
269 /* Print out statistics on packets dropped */
270 static void
271 print_stats(void)
272 {
273         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
274         uint64_t total_packets_enqueued, total_packets_dequeued,
275                 total_packets_errors;
276         unsigned portid;
277         uint64_t cdevid;
278
279         total_packets_dropped = 0;
280         total_packets_tx = 0;
281         total_packets_rx = 0;
282         total_packets_enqueued = 0;
283         total_packets_dequeued = 0;
284         total_packets_errors = 0;
285
286         const char clr[] = { 27, '[', '2', 'J', '\0' };
287         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
288
289                 /* Clear screen and move to top left */
290         printf("%s%s", clr, topLeft);
291
292         printf("\nPort statistics ====================================");
293
294         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
295                 /* skip disabled ports */
296                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
297                         continue;
298                 printf("\nStatistics for port %u ------------------------------"
299                            "\nPackets sent: %32"PRIu64
300                            "\nPackets received: %28"PRIu64
301                            "\nPackets dropped: %29"PRIu64,
302                            portid,
303                            port_statistics[portid].tx,
304                            port_statistics[portid].rx,
305                            port_statistics[portid].dropped);
306
307                 total_packets_dropped += port_statistics[portid].dropped;
308                 total_packets_tx += port_statistics[portid].tx;
309                 total_packets_rx += port_statistics[portid].rx;
310         }
311         printf("\nCrypto statistics ==================================");
312
313         for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
314                 /* skip disabled ports */
315                 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
316                         continue;
317                 printf("\nStatistics for cryptodev %"PRIu64
318                                 " -------------------------"
319                            "\nPackets enqueued: %28"PRIu64
320                            "\nPackets dequeued: %28"PRIu64
321                            "\nPackets errors: %30"PRIu64,
322                            cdevid,
323                            crypto_statistics[cdevid].enqueued,
324                            crypto_statistics[cdevid].dequeued,
325                            crypto_statistics[cdevid].errors);
326
327                 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
328                 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
329                 total_packets_errors += crypto_statistics[cdevid].errors;
330         }
331         printf("\nAggregate statistics ==============================="
332                    "\nTotal packets received: %22"PRIu64
333                    "\nTotal packets enqueued: %22"PRIu64
334                    "\nTotal packets dequeued: %22"PRIu64
335                    "\nTotal packets sent: %26"PRIu64
336                    "\nTotal packets dropped: %23"PRIu64
337                    "\nTotal packets crypto errors: %17"PRIu64,
338                    total_packets_rx,
339                    total_packets_enqueued,
340                    total_packets_dequeued,
341                    total_packets_tx,
342                    total_packets_dropped,
343                    total_packets_errors);
344         printf("\n====================================================\n");
345 }
346
347 static int
348 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
349                 struct l2fwd_crypto_params *cparams)
350 {
351         struct rte_crypto_op **op_buffer;
352         unsigned ret;
353
354         op_buffer = (struct rte_crypto_op **)
355                         qconf->op_buf[cparams->dev_id].buffer;
356
357         ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
358                         cparams->qp_id, op_buffer, (uint16_t) n);
359
360         crypto_statistics[cparams->dev_id].enqueued += ret;
361         if (unlikely(ret < n)) {
362                 crypto_statistics[cparams->dev_id].errors += (n - ret);
363                 do {
364                         rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
365                         rte_crypto_op_free(op_buffer[ret]);
366                 } while (++ret < n);
367         }
368
369         return 0;
370 }
371
372 static int
373 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
374                 struct l2fwd_crypto_params *cparams)
375 {
376         unsigned lcore_id, len;
377         struct lcore_queue_conf *qconf;
378
379         lcore_id = rte_lcore_id();
380
381         qconf = &lcore_queue_conf[lcore_id];
382         len = qconf->op_buf[cparams->dev_id].len;
383         qconf->op_buf[cparams->dev_id].buffer[len] = op;
384         len++;
385
386         /* enough ops to be sent */
387         if (len == MAX_PKT_BURST) {
388                 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
389                 len = 0;
390         }
391
392         qconf->op_buf[cparams->dev_id].len = len;
393         return 0;
394 }
395
396 static int
397 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
398                 struct rte_crypto_op *op,
399                 struct l2fwd_crypto_params *cparams)
400 {
401         struct ether_hdr *eth_hdr;
402         struct ipv4_hdr *ip_hdr;
403
404         uint32_t ipdata_offset, data_len;
405         uint32_t pad_len = 0;
406         char *padding;
407
408         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
409
410         if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
411                 return -1;
412
413         ipdata_offset = sizeof(struct ether_hdr);
414
415         ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
416                         ipdata_offset);
417
418         ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
419                         * IPV4_IHL_MULTIPLIER;
420
421
422         /* Zero pad data to be crypto'd so it is block aligned */
423         data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
424
425         if (cparams->do_hash && cparams->hash_verify)
426                 data_len -= cparams->digest_length;
427
428         if (cparams->do_cipher) {
429                 /*
430                  * Following algorithms are block cipher algorithms,
431                  * and might need padding
432                  */
433                 switch (cparams->cipher_algo) {
434                 case RTE_CRYPTO_CIPHER_AES_CBC:
435                 case RTE_CRYPTO_CIPHER_AES_ECB:
436                 case RTE_CRYPTO_CIPHER_DES_CBC:
437                 case RTE_CRYPTO_CIPHER_3DES_CBC:
438                 case RTE_CRYPTO_CIPHER_3DES_ECB:
439                         if (data_len % cparams->block_size)
440                                 pad_len = cparams->block_size -
441                                         (data_len % cparams->block_size);
442                         break;
443                 default:
444                         pad_len = 0;
445                 }
446
447                 if (pad_len) {
448                         padding = rte_pktmbuf_append(m, pad_len);
449                         if (unlikely(!padding))
450                                 return -1;
451
452                         data_len += pad_len;
453                         memset(padding, 0, pad_len);
454                 }
455         }
456
457         /* Set crypto operation data parameters */
458         rte_crypto_op_attach_sym_session(op, cparams->session);
459
460         if (cparams->do_hash) {
461                 if (cparams->auth_iv.length) {
462                         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
463                                                 uint8_t *,
464                                                 IV_OFFSET +
465                                                 cparams->cipher_iv.length);
466                         /*
467                          * Copy IV at the end of the crypto operation,
468                          * after the cipher IV, if added
469                          */
470                         rte_memcpy(iv_ptr, cparams->auth_iv.data,
471                                         cparams->auth_iv.length);
472                 }
473                 if (!cparams->hash_verify) {
474                         /* Append space for digest to end of packet */
475                         op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
476                                 cparams->digest_length);
477                 } else {
478                         op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
479                                 uint8_t *) + ipdata_offset + data_len;
480                 }
481
482                 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
483                                 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
484
485                 /* For wireless algorithms, offset/length must be in bits */
486                 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
487                                 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
488                                 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
489                         op->sym->auth.data.offset = ipdata_offset << 3;
490                         op->sym->auth.data.length = data_len << 3;
491                 } else {
492                         op->sym->auth.data.offset = ipdata_offset;
493                         op->sym->auth.data.length = data_len;
494                 }
495
496                 if (cparams->aad.length) {
497                         op->sym->auth.aad.data = cparams->aad.data;
498                         op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
499                 } else {
500                         op->sym->auth.aad.data = NULL;
501                         op->sym->auth.aad.phys_addr = 0;
502                 }
503         }
504
505         if (cparams->do_cipher) {
506                 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
507                                                         IV_OFFSET);
508                 /* Copy IV at the end of the crypto operation */
509                 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
510                                 cparams->cipher_iv.length);
511
512                 /* For wireless algorithms, offset/length must be in bits */
513                 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
514                                 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
515                                 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
516                         op->sym->cipher.data.offset = ipdata_offset << 3;
517                         op->sym->cipher.data.length = data_len << 3;
518                 } else {
519                         op->sym->cipher.data.offset = ipdata_offset;
520                         op->sym->cipher.data.length = data_len;
521                 }
522         }
523
524         op->sym->m_src = m;
525
526         return l2fwd_crypto_enqueue(op, cparams);
527 }
528
529
530 /* Send the burst of packets on an output interface */
531 static int
532 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
533                 uint8_t port)
534 {
535         struct rte_mbuf **pkt_buffer;
536         unsigned ret;
537
538         pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
539
540         ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
541         port_statistics[port].tx += ret;
542         if (unlikely(ret < n)) {
543                 port_statistics[port].dropped += (n - ret);
544                 do {
545                         rte_pktmbuf_free(pkt_buffer[ret]);
546                 } while (++ret < n);
547         }
548
549         return 0;
550 }
551
552 /* Enqueue packets for TX and prepare them to be sent */
553 static int
554 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
555 {
556         unsigned lcore_id, len;
557         struct lcore_queue_conf *qconf;
558
559         lcore_id = rte_lcore_id();
560
561         qconf = &lcore_queue_conf[lcore_id];
562         len = qconf->pkt_buf[port].len;
563         qconf->pkt_buf[port].buffer[len] = m;
564         len++;
565
566         /* enough pkts to be sent */
567         if (unlikely(len == MAX_PKT_BURST)) {
568                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
569                 len = 0;
570         }
571
572         qconf->pkt_buf[port].len = len;
573         return 0;
574 }
575
576 static void
577 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
578 {
579         struct ether_hdr *eth;
580         void *tmp;
581         unsigned dst_port;
582
583         dst_port = l2fwd_dst_ports[portid];
584         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
585
586         /* 02:00:00:00:00:xx */
587         tmp = &eth->d_addr.addr_bytes[0];
588         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
589
590         /* src addr */
591         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
592
593         l2fwd_send_packet(m, (uint8_t) dst_port);
594 }
595
596 /** Generate random key */
597 static void
598 generate_random_key(uint8_t *key, unsigned length)
599 {
600         int fd;
601         int ret;
602
603         fd = open("/dev/urandom", O_RDONLY);
604         if (fd < 0)
605                 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
606
607         ret = read(fd, key, length);
608         close(fd);
609
610         if (ret != (signed)length)
611                 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
612 }
613
614 static struct rte_cryptodev_sym_session *
615 initialize_crypto_session(struct l2fwd_crypto_options *options,
616                 uint8_t cdev_id)
617 {
618         struct rte_crypto_sym_xform *first_xform;
619
620         if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
621                 first_xform = &options->cipher_xform;
622                 first_xform->next = &options->auth_xform;
623         } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
624                 first_xform = &options->auth_xform;
625                 first_xform->next = &options->cipher_xform;
626         } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
627                 first_xform = &options->cipher_xform;
628         } else {
629                 first_xform = &options->auth_xform;
630         }
631
632         /* Setup Cipher Parameters */
633         return rte_cryptodev_sym_session_create(cdev_id, first_xform);
634 }
635
636 static void
637 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
638
639 /* main processing loop */
640 static void
641 l2fwd_main_loop(struct l2fwd_crypto_options *options)
642 {
643         struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
644         struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
645
646         unsigned lcore_id = rte_lcore_id();
647         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
648         unsigned i, j, portid, nb_rx, len;
649         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
650         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
651                         US_PER_S * BURST_TX_DRAIN_US;
652         struct l2fwd_crypto_params *cparams;
653         struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
654
655         if (qconf->nb_rx_ports == 0) {
656                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
657                 return;
658         }
659
660         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
661
662         for (i = 0; i < qconf->nb_rx_ports; i++) {
663
664                 portid = qconf->rx_port_list[i];
665                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
666                         portid);
667         }
668
669         for (i = 0; i < qconf->nb_crypto_devs; i++) {
670                 port_cparams[i].do_cipher = 0;
671                 port_cparams[i].do_hash = 0;
672
673                 switch (options->xform_chain) {
674                 case L2FWD_CRYPTO_CIPHER_HASH:
675                 case L2FWD_CRYPTO_HASH_CIPHER:
676                         port_cparams[i].do_cipher = 1;
677                         port_cparams[i].do_hash = 1;
678                         break;
679                 case L2FWD_CRYPTO_HASH_ONLY:
680                         port_cparams[i].do_hash = 1;
681                         break;
682                 case L2FWD_CRYPTO_CIPHER_ONLY:
683                         port_cparams[i].do_cipher = 1;
684                         break;
685                 }
686
687                 port_cparams[i].dev_id = qconf->cryptodev_list[i];
688                 port_cparams[i].qp_id = 0;
689
690                 port_cparams[i].block_size = options->block_size;
691
692                 if (port_cparams[i].do_hash) {
693                         port_cparams[i].auth_iv.data = options->auth_iv.data;
694                         port_cparams[i].auth_iv.length = options->auth_iv.length;
695                         if (!options->auth_iv_param)
696                                 generate_random_key(port_cparams[i].auth_iv.data,
697                                                 port_cparams[i].auth_iv.length);
698                         /* Set IV parameters */
699                         if (options->auth_iv.length) {
700                                 options->auth_xform.auth.iv.offset =
701                                         IV_OFFSET + options->cipher_iv.length;
702                                 options->auth_xform.auth.iv.length =
703                                         options->auth_iv.length;
704                         }
705                         port_cparams[i].digest_length =
706                                         options->auth_xform.auth.digest_length;
707                         if (options->auth_xform.auth.add_auth_data_length) {
708                                 port_cparams[i].aad.data = options->aad.data;
709                                 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
710                                 if (!options->aad_param)
711                                         generate_random_key(port_cparams[i].aad.data,
712                                                 port_cparams[i].aad.length);
713
714                         } else
715                                 port_cparams[i].aad.length = 0;
716
717                         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
718                                 port_cparams[i].hash_verify = 1;
719                         else
720                                 port_cparams[i].hash_verify = 0;
721
722                         port_cparams[i].auth_algo = options->auth_xform.auth.algo;
723                 }
724
725                 if (port_cparams[i].do_cipher) {
726                         port_cparams[i].cipher_iv.data = options->cipher_iv.data;
727                         port_cparams[i].cipher_iv.length = options->cipher_iv.length;
728                         if (!options->cipher_iv_param)
729                                 generate_random_key(port_cparams[i].cipher_iv.data,
730                                                 port_cparams[i].cipher_iv.length);
731
732                         port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
733                         /* Set IV parameters */
734                         options->cipher_xform.cipher.iv.offset = IV_OFFSET;
735                         options->cipher_xform.cipher.iv.length =
736                                                 options->cipher_iv.length;
737                 }
738
739                 port_cparams[i].session = initialize_crypto_session(options,
740                                 port_cparams[i].dev_id);
741
742                 if (port_cparams[i].session == NULL)
743                         return;
744                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
745                                 port_cparams[i].dev_id);
746         }
747
748         l2fwd_crypto_options_print(options);
749
750         /*
751          * Initialize previous tsc timestamp before the loop,
752          * to avoid showing the port statistics immediately,
753          * so user can see the crypto information.
754          */
755         prev_tsc = rte_rdtsc();
756         while (1) {
757
758                 cur_tsc = rte_rdtsc();
759
760                 /*
761                  * Crypto device/TX burst queue drain
762                  */
763                 diff_tsc = cur_tsc - prev_tsc;
764                 if (unlikely(diff_tsc > drain_tsc)) {
765                         /* Enqueue all crypto ops remaining in buffers */
766                         for (i = 0; i < qconf->nb_crypto_devs; i++) {
767                                 cparams = &port_cparams[i];
768                                 len = qconf->op_buf[cparams->dev_id].len;
769                                 l2fwd_crypto_send_burst(qconf, len, cparams);
770                                 qconf->op_buf[cparams->dev_id].len = 0;
771                         }
772                         /* Transmit all packets remaining in buffers */
773                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
774                                 if (qconf->pkt_buf[portid].len == 0)
775                                         continue;
776                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
777                                                  qconf->pkt_buf[portid].len,
778                                                  (uint8_t) portid);
779                                 qconf->pkt_buf[portid].len = 0;
780                         }
781
782                         /* if timer is enabled */
783                         if (timer_period > 0) {
784
785                                 /* advance the timer */
786                                 timer_tsc += diff_tsc;
787
788                                 /* if timer has reached its timeout */
789                                 if (unlikely(timer_tsc >=
790                                                 (uint64_t)timer_period)) {
791
792                                         /* do this only on master core */
793                                         if (lcore_id == rte_get_master_lcore()
794                                                 && options->refresh_period) {
795                                                 print_stats();
796                                                 timer_tsc = 0;
797                                         }
798                                 }
799                         }
800
801                         prev_tsc = cur_tsc;
802                 }
803
804                 /*
805                  * Read packet from RX queues
806                  */
807                 for (i = 0; i < qconf->nb_rx_ports; i++) {
808                         portid = qconf->rx_port_list[i];
809
810                         cparams = &port_cparams[i];
811
812                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
813                                                  pkts_burst, MAX_PKT_BURST);
814
815                         port_statistics[portid].rx += nb_rx;
816
817                         if (nb_rx) {
818                                 /*
819                                  * If we can't allocate a crypto_ops, then drop
820                                  * the rest of the burst and dequeue and
821                                  * process the packets to free offload structs
822                                  */
823                                 if (rte_crypto_op_bulk_alloc(
824                                                 l2fwd_crypto_op_pool,
825                                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
826                                                 ops_burst, nb_rx) !=
827                                                                 nb_rx) {
828                                         for (j = 0; j < nb_rx; j++)
829                                                 rte_pktmbuf_free(pkts_burst[j]);
830
831                                         nb_rx = 0;
832                                 }
833
834                                 /* Enqueue packets from Crypto device*/
835                                 for (j = 0; j < nb_rx; j++) {
836                                         m = pkts_burst[j];
837
838                                         l2fwd_simple_crypto_enqueue(m,
839                                                         ops_burst[j], cparams);
840                                 }
841                         }
842
843                         /* Dequeue packets from Crypto device */
844                         do {
845                                 nb_rx = rte_cryptodev_dequeue_burst(
846                                                 cparams->dev_id, cparams->qp_id,
847                                                 ops_burst, MAX_PKT_BURST);
848
849                                 crypto_statistics[cparams->dev_id].dequeued +=
850                                                 nb_rx;
851
852                                 /* Forward crypto'd packets */
853                                 for (j = 0; j < nb_rx; j++) {
854                                         m = ops_burst[j]->sym->m_src;
855
856                                         rte_crypto_op_free(ops_burst[j]);
857                                         l2fwd_simple_forward(m, portid);
858                                 }
859                         } while (nb_rx == MAX_PKT_BURST);
860                 }
861         }
862 }
863
864 static int
865 l2fwd_launch_one_lcore(void *arg)
866 {
867         l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
868         return 0;
869 }
870
871 /* Display command line arguments usage */
872 static void
873 l2fwd_crypto_usage(const char *prgname)
874 {
875         printf("%s [EAL options] --\n"
876                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
877                 "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
878                 "  -s manage all ports from single lcore\n"
879                 "  -T PERIOD: statistics will be refreshed each PERIOD seconds"
880                 " (0 to disable, 10 default, 86400 maximum)\n"
881
882                 "  --cdev_type HW / SW / ANY\n"
883                 "  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
884                 " HASH_ONLY\n"
885
886                 "  --cipher_algo ALGO\n"
887                 "  --cipher_op ENCRYPT / DECRYPT\n"
888                 "  --cipher_key KEY (bytes separated with \":\")\n"
889                 "  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
890                 "  --cipher_iv IV (bytes separated with \":\")\n"
891                 "  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
892
893                 "  --auth_algo ALGO\n"
894                 "  --auth_op GENERATE / VERIFY\n"
895                 "  --auth_key KEY (bytes separated with \":\")\n"
896                 "  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
897                 "  --auth_iv IV (bytes separated with \":\")\n"
898                 "  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
899                 "  --aad AAD (bytes separated with \":\")\n"
900                 "  --aad_random_size SIZE: size of AAD when generated randomly\n"
901                 "  --digest_size SIZE: size of digest to be generated/verified\n"
902
903                 "  --sessionless\n"
904                 "  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n",
905                prgname);
906 }
907
908 /** Parse crypto device type command line argument */
909 static int
910 parse_cryptodev_type(enum cdev_type *type, char *optarg)
911 {
912         if (strcmp("HW", optarg) == 0) {
913                 *type = CDEV_TYPE_HW;
914                 return 0;
915         } else if (strcmp("SW", optarg) == 0) {
916                 *type = CDEV_TYPE_SW;
917                 return 0;
918         } else if (strcmp("ANY", optarg) == 0) {
919                 *type = CDEV_TYPE_ANY;
920                 return 0;
921         }
922
923         return -1;
924 }
925
926 /** Parse crypto chain xform command line argument */
927 static int
928 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
929 {
930         if (strcmp("CIPHER_HASH", optarg) == 0) {
931                 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
932                 return 0;
933         } else if (strcmp("HASH_CIPHER", optarg) == 0) {
934                 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
935                 return 0;
936         } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
937                 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
938                 return 0;
939         } else if (strcmp("HASH_ONLY", optarg) == 0) {
940                 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
941                 return 0;
942         }
943
944         return -1;
945 }
946
947 /** Parse crypto cipher algo option command line argument */
948 static int
949 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
950 {
951
952         if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
953                 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
954                                 "not supported!\n");
955                 return -1;
956         }
957
958         return 0;
959 }
960
961 /** Parse crypto cipher operation command line argument */
962 static int
963 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
964 {
965         if (strcmp("ENCRYPT", optarg) == 0) {
966                 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
967                 return 0;
968         } else if (strcmp("DECRYPT", optarg) == 0) {
969                 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
970                 return 0;
971         }
972
973         printf("Cipher operation not supported!\n");
974         return -1;
975 }
976
977 /** Parse crypto key command line argument */
978 static int
979 parse_key(uint8_t *data, char *input_arg)
980 {
981         unsigned byte_count;
982         char *token;
983
984         for (byte_count = 0, token = strtok(input_arg, ":");
985                         (byte_count < MAX_KEY_SIZE) && (token != NULL);
986                         token = strtok(NULL, ":")) {
987
988                 int number = (int)strtol(token, NULL, 16);
989
990                 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
991                         return -1;
992
993                 data[byte_count++] = (uint8_t)number;
994         }
995
996         return byte_count;
997 }
998
999 /** Parse size param*/
1000 static int
1001 parse_size(int *size, const char *q_arg)
1002 {
1003         char *end = NULL;
1004         unsigned long n;
1005
1006         /* parse hexadecimal string */
1007         n = strtoul(q_arg, &end, 10);
1008         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1009                 n = 0;
1010
1011         if (n == 0) {
1012                 printf("invalid size\n");
1013                 return -1;
1014         }
1015
1016         *size = n;
1017         return 0;
1018 }
1019
1020 /** Parse crypto cipher operation command line argument */
1021 static int
1022 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1023 {
1024         if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1025                 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1026                                 "not supported!\n");
1027                 return -1;
1028         }
1029
1030         return 0;
1031 }
1032
1033 static int
1034 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1035 {
1036         if (strcmp("VERIFY", optarg) == 0) {
1037                 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1038                 return 0;
1039         } else if (strcmp("GENERATE", optarg) == 0) {
1040                 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1041                 return 0;
1042         }
1043
1044         printf("Authentication operation specified not supported!\n");
1045         return -1;
1046 }
1047
1048 static int
1049 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1050                 const char *q_arg)
1051 {
1052         char *end = NULL;
1053         uint64_t pm;
1054
1055         /* parse hexadecimal string */
1056         pm = strtoul(q_arg, &end, 16);
1057         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1058                 pm = 0;
1059
1060         options->cryptodev_mask = pm;
1061         if (options->cryptodev_mask == 0) {
1062                 printf("invalid cryptodev_mask specified\n");
1063                 return -1;
1064         }
1065
1066         return 0;
1067 }
1068
1069 /** Parse long options */
1070 static int
1071 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1072                 struct option *lgopts, int option_index)
1073 {
1074         int retval;
1075
1076         if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1077                 retval = parse_cryptodev_type(&options->type, optarg);
1078                 if (retval == 0)
1079                         snprintf(options->string_type, MAX_STR_LEN,
1080                                 "%s", optarg);
1081                 return retval;
1082         }
1083
1084         else if (strcmp(lgopts[option_index].name, "chain") == 0)
1085                 return parse_crypto_opt_chain(options, optarg);
1086
1087         /* Cipher options */
1088         else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1089                 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1090                                 optarg);
1091
1092         else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1093                 return parse_cipher_op(&options->cipher_xform.cipher.op,
1094                                 optarg);
1095
1096         else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1097                 options->ckey_param = 1;
1098                 options->cipher_xform.cipher.key.length =
1099                         parse_key(options->cipher_xform.cipher.key.data, optarg);
1100                 if (options->cipher_xform.cipher.key.length > 0)
1101                         return 0;
1102                 else
1103                         return -1;
1104         }
1105
1106         else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1107                 return parse_size(&options->ckey_random_size, optarg);
1108
1109         else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1110                 options->cipher_iv_param = 1;
1111                 options->cipher_iv.length =
1112                         parse_key(options->cipher_iv.data, optarg);
1113                 if (options->cipher_iv.length > 0)
1114                         return 0;
1115                 else
1116                         return -1;
1117         }
1118
1119         else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1120                 return parse_size(&options->cipher_iv_random_size, optarg);
1121
1122         /* Authentication options */
1123         else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1124                 return parse_auth_algo(&options->auth_xform.auth.algo,
1125                                 optarg);
1126         }
1127
1128         else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1129                 return parse_auth_op(&options->auth_xform.auth.op,
1130                                 optarg);
1131
1132         else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1133                 options->akey_param = 1;
1134                 options->auth_xform.auth.key.length =
1135                         parse_key(options->auth_xform.auth.key.data, optarg);
1136                 if (options->auth_xform.auth.key.length > 0)
1137                         return 0;
1138                 else
1139                         return -1;
1140         }
1141
1142         else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1143                 return parse_size(&options->akey_random_size, optarg);
1144         }
1145
1146
1147         else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1148                 options->auth_iv_param = 1;
1149                 options->auth_iv.length =
1150                         parse_key(options->auth_iv.data, optarg);
1151                 if (options->auth_iv.length > 0)
1152                         return 0;
1153                 else
1154                         return -1;
1155         }
1156
1157         else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1158                 return parse_size(&options->auth_iv_random_size, optarg);
1159
1160         else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1161                 options->aad_param = 1;
1162                 options->aad.length =
1163                         parse_key(options->aad.data, optarg);
1164                 if (options->aad.length > 0)
1165                         return 0;
1166                 else
1167                         return -1;
1168         }
1169
1170         else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1171                 return parse_size(&options->aad_random_size, optarg);
1172         }
1173
1174         else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1175                 return parse_size(&options->digest_size, optarg);
1176         }
1177
1178         else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1179                 options->sessionless = 1;
1180                 return 0;
1181         }
1182
1183         else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1184                 return parse_cryptodev_mask(options, optarg);
1185
1186         return -1;
1187 }
1188
1189 /** Parse port mask */
1190 static int
1191 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1192                 const char *q_arg)
1193 {
1194         char *end = NULL;
1195         unsigned long pm;
1196
1197         /* parse hexadecimal string */
1198         pm = strtoul(q_arg, &end, 16);
1199         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1200                 pm = 0;
1201
1202         options->portmask = pm;
1203         if (options->portmask == 0) {
1204                 printf("invalid portmask specified\n");
1205                 return -1;
1206         }
1207
1208         return pm;
1209 }
1210
1211 /** Parse number of queues */
1212 static int
1213 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1214                 const char *q_arg)
1215 {
1216         char *end = NULL;
1217         unsigned long n;
1218
1219         /* parse hexadecimal string */
1220         n = strtoul(q_arg, &end, 10);
1221         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1222                 n = 0;
1223         else if (n >= MAX_RX_QUEUE_PER_LCORE)
1224                 n = 0;
1225
1226         options->nb_ports_per_lcore = n;
1227         if (options->nb_ports_per_lcore == 0) {
1228                 printf("invalid number of ports selected\n");
1229                 return -1;
1230         }
1231
1232         return 0;
1233 }
1234
1235 /** Parse timer period */
1236 static int
1237 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1238                 const char *q_arg)
1239 {
1240         char *end = NULL;
1241         unsigned long n;
1242
1243         /* parse number string */
1244         n = (unsigned)strtol(q_arg, &end, 10);
1245         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1246                 n = 0;
1247
1248         if (n >= MAX_TIMER_PERIOD) {
1249                 printf("Warning refresh period specified %lu is greater than "
1250                                 "max value %lu! using max value",
1251                                 n, MAX_TIMER_PERIOD);
1252                 n = MAX_TIMER_PERIOD;
1253         }
1254
1255         options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1256
1257         return 0;
1258 }
1259
1260 /** Generate default options for application */
1261 static void
1262 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1263 {
1264         options->portmask = 0xffffffff;
1265         options->nb_ports_per_lcore = 1;
1266         options->refresh_period = 10000;
1267         options->single_lcore = 0;
1268         options->sessionless = 0;
1269
1270         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1271
1272         /* Cipher Data */
1273         options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1274         options->cipher_xform.next = NULL;
1275         options->ckey_param = 0;
1276         options->ckey_random_size = -1;
1277         options->cipher_xform.cipher.key.length = 0;
1278         options->cipher_iv_param = 0;
1279         options->cipher_iv_random_size = -1;
1280         options->cipher_iv.length = 0;
1281
1282         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1283         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1284
1285         /* Authentication Data */
1286         options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1287         options->auth_xform.next = NULL;
1288         options->akey_param = 0;
1289         options->akey_random_size = -1;
1290         options->auth_xform.auth.key.length = 0;
1291         options->auth_iv_param = 0;
1292         options->auth_iv_random_size = -1;
1293         options->auth_iv.length = 0;
1294         options->aad_param = 0;
1295         options->aad_random_size = -1;
1296         options->aad.length = 0;
1297         options->digest_size = -1;
1298
1299         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1300         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1301
1302         options->type = CDEV_TYPE_ANY;
1303         options->cryptodev_mask = UINT64_MAX;
1304 }
1305
1306 static void
1307 display_cipher_info(struct l2fwd_crypto_options *options)
1308 {
1309         printf("\n---- Cipher information ---\n");
1310         printf("Algorithm: %s\n",
1311                 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1312         rte_hexdump(stdout, "Cipher key:",
1313                         options->cipher_xform.cipher.key.data,
1314                         options->cipher_xform.cipher.key.length);
1315         rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1316 }
1317
1318 static void
1319 display_auth_info(struct l2fwd_crypto_options *options)
1320 {
1321         printf("\n---- Authentication information ---\n");
1322         printf("Algorithm: %s\n",
1323                 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1324         rte_hexdump(stdout, "Auth key:",
1325                         options->auth_xform.auth.key.data,
1326                         options->auth_xform.auth.key.length);
1327         rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1328         rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1329 }
1330
1331 static void
1332 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1333 {
1334         char string_cipher_op[MAX_STR_LEN];
1335         char string_auth_op[MAX_STR_LEN];
1336
1337         if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1338                 strcpy(string_cipher_op, "Encrypt");
1339         else
1340                 strcpy(string_cipher_op, "Decrypt");
1341
1342         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1343                 strcpy(string_auth_op, "Auth generate");
1344         else
1345                 strcpy(string_auth_op, "Auth verify");
1346
1347         printf("Options:-\nn");
1348         printf("portmask: %x\n", options->portmask);
1349         printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1350         printf("refresh period : %u\n", options->refresh_period);
1351         printf("single lcore mode: %s\n",
1352                         options->single_lcore ? "enabled" : "disabled");
1353         printf("stats_printing: %s\n",
1354                         options->refresh_period == 0 ? "disabled" : "enabled");
1355
1356         printf("sessionless crypto: %s\n",
1357                         options->sessionless ? "enabled" : "disabled");
1358
1359         if (options->ckey_param && (options->ckey_random_size != -1))
1360                 printf("Cipher key already parsed, ignoring size of random key\n");
1361
1362         if (options->akey_param && (options->akey_random_size != -1))
1363                 printf("Auth key already parsed, ignoring size of random key\n");
1364
1365         if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1366                 printf("Cipher IV already parsed, ignoring size of random IV\n");
1367
1368         if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1369                 printf("Auth IV already parsed, ignoring size of random IV\n");
1370
1371         if (options->aad_param && (options->aad_random_size != -1))
1372                 printf("AAD already parsed, ignoring size of random AAD\n");
1373
1374         printf("\nCrypto chain: ");
1375         switch (options->xform_chain) {
1376         case L2FWD_CRYPTO_CIPHER_HASH:
1377                 printf("Input --> %s --> %s --> Output\n",
1378                         string_cipher_op, string_auth_op);
1379                 display_cipher_info(options);
1380                 display_auth_info(options);
1381                 break;
1382         case L2FWD_CRYPTO_HASH_CIPHER:
1383                 printf("Input --> %s --> %s --> Output\n",
1384                         string_auth_op, string_cipher_op);
1385                 display_cipher_info(options);
1386                 display_auth_info(options);
1387                 break;
1388         case L2FWD_CRYPTO_HASH_ONLY:
1389                 printf("Input --> %s --> Output\n", string_auth_op);
1390                 display_auth_info(options);
1391                 break;
1392         case L2FWD_CRYPTO_CIPHER_ONLY:
1393                 printf("Input --> %s --> Output\n", string_cipher_op);
1394                 display_cipher_info(options);
1395                 break;
1396         }
1397 }
1398
1399 /* Parse the argument given in the command line of the application */
1400 static int
1401 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1402                 int argc, char **argv)
1403 {
1404         int opt, retval, option_index;
1405         char **argvopt = argv, *prgname = argv[0];
1406
1407         static struct option lgopts[] = {
1408                         { "sessionless", no_argument, 0, 0 },
1409
1410                         { "cdev_type", required_argument, 0, 0 },
1411                         { "chain", required_argument, 0, 0 },
1412
1413                         { "cipher_algo", required_argument, 0, 0 },
1414                         { "cipher_op", required_argument, 0, 0 },
1415                         { "cipher_key", required_argument, 0, 0 },
1416                         { "cipher_key_random_size", required_argument, 0, 0 },
1417                         { "cipher_iv", required_argument, 0, 0 },
1418                         { "cipher_iv_random_size", required_argument, 0, 0 },
1419
1420                         { "auth_algo", required_argument, 0, 0 },
1421                         { "auth_op", required_argument, 0, 0 },
1422                         { "auth_key", required_argument, 0, 0 },
1423                         { "auth_key_random_size", required_argument, 0, 0 },
1424                         { "auth_iv", required_argument, 0, 0 },
1425                         { "auth_iv_random_size", required_argument, 0, 0 },
1426
1427                         { "aad", required_argument, 0, 0 },
1428                         { "aad_random_size", required_argument, 0, 0 },
1429                         { "digest_size", required_argument, 0, 0 },
1430
1431                         { "sessionless", no_argument, 0, 0 },
1432                         { "cryptodev_mask", required_argument, 0, 0},
1433
1434                         { NULL, 0, 0, 0 }
1435         };
1436
1437         l2fwd_crypto_default_options(options);
1438
1439         while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1440                         &option_index)) != EOF) {
1441                 switch (opt) {
1442                 /* long options */
1443                 case 0:
1444                         retval = l2fwd_crypto_parse_args_long_options(options,
1445                                         lgopts, option_index);
1446                         if (retval < 0) {
1447                                 l2fwd_crypto_usage(prgname);
1448                                 return -1;
1449                         }
1450                         break;
1451
1452                 /* portmask */
1453                 case 'p':
1454                         retval = l2fwd_crypto_parse_portmask(options, optarg);
1455                         if (retval < 0) {
1456                                 l2fwd_crypto_usage(prgname);
1457                                 return -1;
1458                         }
1459                         break;
1460
1461                 /* nqueue */
1462                 case 'q':
1463                         retval = l2fwd_crypto_parse_nqueue(options, optarg);
1464                         if (retval < 0) {
1465                                 l2fwd_crypto_usage(prgname);
1466                                 return -1;
1467                         }
1468                         break;
1469
1470                 /* single  */
1471                 case 's':
1472                         options->single_lcore = 1;
1473
1474                         break;
1475
1476                 /* timer period */
1477                 case 'T':
1478                         retval = l2fwd_crypto_parse_timer_period(options,
1479                                         optarg);
1480                         if (retval < 0) {
1481                                 l2fwd_crypto_usage(prgname);
1482                                 return -1;
1483                         }
1484                         break;
1485
1486                 default:
1487                         l2fwd_crypto_usage(prgname);
1488                         return -1;
1489                 }
1490         }
1491
1492
1493         if (optind >= 0)
1494                 argv[optind-1] = prgname;
1495
1496         retval = optind-1;
1497         optind = 1; /* reset getopt lib */
1498
1499         return retval;
1500 }
1501
1502 /* Check the link status of all ports in up to 9s, and print them finally */
1503 static void
1504 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1505 {
1506 #define CHECK_INTERVAL 100 /* 100ms */
1507 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1508         uint8_t portid, count, all_ports_up, print_flag = 0;
1509         struct rte_eth_link link;
1510
1511         printf("\nChecking link status");
1512         fflush(stdout);
1513         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1514                 all_ports_up = 1;
1515                 for (portid = 0; portid < port_num; portid++) {
1516                         if ((port_mask & (1 << portid)) == 0)
1517                                 continue;
1518                         memset(&link, 0, sizeof(link));
1519                         rte_eth_link_get_nowait(portid, &link);
1520                         /* print link status if flag set */
1521                         if (print_flag == 1) {
1522                                 if (link.link_status)
1523                                         printf("Port %d Link Up - speed %u "
1524                                                 "Mbps - %s\n", (uint8_t)portid,
1525                                                 (unsigned)link.link_speed,
1526                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1527                                         ("full-duplex") : ("half-duplex\n"));
1528                                 else
1529                                         printf("Port %d Link Down\n",
1530                                                 (uint8_t)portid);
1531                                 continue;
1532                         }
1533                         /* clear all_ports_up flag if any link down */
1534                         if (link.link_status == ETH_LINK_DOWN) {
1535                                 all_ports_up = 0;
1536                                 break;
1537                         }
1538                 }
1539                 /* after finally printing all link status, get out */
1540                 if (print_flag == 1)
1541                         break;
1542
1543                 if (all_ports_up == 0) {
1544                         printf(".");
1545                         fflush(stdout);
1546                         rte_delay_ms(CHECK_INTERVAL);
1547                 }
1548
1549                 /* set the print_flag if all ports up or timeout */
1550                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1551                         print_flag = 1;
1552                         printf("done\n");
1553                 }
1554         }
1555 }
1556
1557 /* Check if device has to be HW/SW or any */
1558 static int
1559 check_type(const struct l2fwd_crypto_options *options,
1560                 const struct rte_cryptodev_info *dev_info)
1561 {
1562         if (options->type == CDEV_TYPE_HW &&
1563                         (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1564                 return 0;
1565         if (options->type == CDEV_TYPE_SW &&
1566                         !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1567                 return 0;
1568         if (options->type == CDEV_TYPE_ANY)
1569                 return 0;
1570
1571         return -1;
1572 }
1573
1574 static const struct rte_cryptodev_capabilities *
1575 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1576                 const struct rte_cryptodev_info *dev_info,
1577                 uint8_t cdev_id)
1578 {
1579         unsigned int i = 0;
1580         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1581         enum rte_crypto_cipher_algorithm cap_cipher_algo;
1582         enum rte_crypto_cipher_algorithm opt_cipher_algo =
1583                                         options->cipher_xform.cipher.algo;
1584
1585         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1586                 cap_cipher_algo = cap->sym.cipher.algo;
1587                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1588                         if (cap_cipher_algo == opt_cipher_algo) {
1589                                 if (check_type(options, dev_info) == 0)
1590                                         break;
1591                         }
1592                 }
1593                 cap = &dev_info->capabilities[++i];
1594         }
1595
1596         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1597                 printf("Algorithm %s not supported by cryptodev %u"
1598                         " or device not of preferred type (%s)\n",
1599                         rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1600                         cdev_id,
1601                         options->string_type);
1602                 return NULL;
1603         }
1604
1605         return cap;
1606 }
1607
1608 static const struct rte_cryptodev_capabilities *
1609 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1610                 const struct rte_cryptodev_info *dev_info,
1611                 uint8_t cdev_id)
1612 {
1613         unsigned int i = 0;
1614         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1615         enum rte_crypto_auth_algorithm cap_auth_algo;
1616         enum rte_crypto_auth_algorithm opt_auth_algo =
1617                                         options->auth_xform.auth.algo;
1618
1619         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1620                 cap_auth_algo = cap->sym.auth.algo;
1621                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1622                         if (cap_auth_algo == opt_auth_algo) {
1623                                 if (check_type(options, dev_info) == 0)
1624                                         break;
1625                         }
1626                 }
1627                 cap = &dev_info->capabilities[++i];
1628         }
1629
1630         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1631                 printf("Algorithm %s not supported by cryptodev %u"
1632                         " or device not of preferred type (%s)\n",
1633                         rte_crypto_auth_algorithm_strings[opt_auth_algo],
1634                         cdev_id,
1635                         options->string_type);
1636                 return NULL;
1637         }
1638
1639         return cap;
1640 }
1641
1642 /* Check if the device is enabled by cryptodev_mask */
1643 static int
1644 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1645                 uint8_t cdev_id)
1646 {
1647         if (options->cryptodev_mask & (1 << cdev_id))
1648                 return 0;
1649
1650         return -1;
1651 }
1652
1653 static inline int
1654 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1655                 uint16_t increment)
1656 {
1657         uint16_t supp_size;
1658
1659         /* Single value */
1660         if (increment == 0) {
1661                 if (length == min)
1662                         return 0;
1663                 else
1664                         return -1;
1665         }
1666
1667         /* Range of values */
1668         for (supp_size = min; supp_size <= max; supp_size += increment) {
1669                 if (length == supp_size)
1670                         return 0;
1671         }
1672
1673         return -1;
1674 }
1675
1676 static int
1677 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1678                 unsigned int iv_param, int iv_random_size,
1679                 uint16_t *iv_length)
1680 {
1681         /*
1682          * Check if length of provided IV is supported
1683          * by the algorithm chosen.
1684          */
1685         if (iv_param) {
1686                 if (check_supported_size(*iv_length,
1687                                 iv_range_size->min,
1688                                 iv_range_size->max,
1689                                 iv_range_size->increment)
1690                                         != 0) {
1691                         printf("Unsupported IV length\n");
1692                         return -1;
1693                 }
1694         /*
1695          * Check if length of IV to be randomly generated
1696          * is supported by the algorithm chosen.
1697          */
1698         } else if (iv_random_size != -1) {
1699                 if (check_supported_size(iv_random_size,
1700                                 iv_range_size->min,
1701                                 iv_range_size->max,
1702                                 iv_range_size->increment)
1703                                         != 0) {
1704                         printf("Unsupported IV length\n");
1705                         return -1;
1706                 }
1707                 *iv_length = iv_random_size;
1708         /* No size provided, use minimum size. */
1709         } else
1710                 *iv_length = iv_range_size->min;
1711
1712         return 0;
1713 }
1714
1715 static int
1716 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1717                 uint8_t *enabled_cdevs)
1718 {
1719         unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
1720         const struct rte_cryptodev_capabilities *cap;
1721         int retval;
1722
1723         cdev_count = rte_cryptodev_count();
1724         if (cdev_count == 0) {
1725                 printf("No crypto devices available\n");
1726                 return -1;
1727         }
1728
1729         for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1730                         cdev_id++) {
1731                 struct rte_cryptodev_qp_conf qp_conf;
1732                 struct rte_cryptodev_info dev_info;
1733
1734                 struct rte_cryptodev_config conf = {
1735                         .nb_queue_pairs = 1,
1736                         .socket_id = SOCKET_ID_ANY,
1737                         .session_mp = {
1738                                 .nb_objs = 2048,
1739                                 .cache_size = 64
1740                         }
1741                 };
1742
1743                 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
1744                         continue;
1745
1746                 rte_cryptodev_info_get(cdev_id, &dev_info);
1747
1748                 /* Set cipher parameters */
1749                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1750                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1751                                 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
1752                         /* Check if device supports cipher algo */
1753                         cap = check_device_support_cipher_algo(options, &dev_info,
1754                                                         cdev_id);
1755                         if (cap == NULL)
1756                                 continue;
1757
1758                         options->block_size = cap->sym.cipher.block_size;
1759
1760                         check_iv_param(&cap->sym.cipher.iv_size,
1761                                         options->cipher_iv_param,
1762                                         options->cipher_iv_random_size,
1763                                         &options->cipher_iv.length);
1764
1765                         /*
1766                          * Check if length of provided cipher key is supported
1767                          * by the algorithm chosen.
1768                          */
1769                         if (options->ckey_param) {
1770                                 if (check_supported_size(
1771                                                 options->cipher_xform.cipher.key.length,
1772                                                 cap->sym.cipher.key_size.min,
1773                                                 cap->sym.cipher.key_size.max,
1774                                                 cap->sym.cipher.key_size.increment)
1775                                                         != 0) {
1776                                         printf("Unsupported cipher key length\n");
1777                                         return -1;
1778                                 }
1779                         /*
1780                          * Check if length of the cipher key to be randomly generated
1781                          * is supported by the algorithm chosen.
1782                          */
1783                         } else if (options->ckey_random_size != -1) {
1784                                 if (check_supported_size(options->ckey_random_size,
1785                                                 cap->sym.cipher.key_size.min,
1786                                                 cap->sym.cipher.key_size.max,
1787                                                 cap->sym.cipher.key_size.increment)
1788                                                         != 0) {
1789                                         printf("Unsupported cipher key length\n");
1790                                         return -1;
1791                                 }
1792                                 options->cipher_xform.cipher.key.length =
1793                                                         options->ckey_random_size;
1794                         /* No size provided, use minimum size. */
1795                         } else
1796                                 options->cipher_xform.cipher.key.length =
1797                                                 cap->sym.cipher.key_size.min;
1798
1799                         if (!options->ckey_param)
1800                                 generate_random_key(
1801                                         options->cipher_xform.cipher.key.data,
1802                                         options->cipher_xform.cipher.key.length);
1803
1804                 }
1805
1806                 /* Set auth parameters */
1807                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1808                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1809                                 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
1810                         /* Check if device supports auth algo */
1811                         cap = check_device_support_auth_algo(options, &dev_info,
1812                                                         cdev_id);
1813                         if (cap == NULL)
1814                                 continue;
1815
1816                         check_iv_param(&cap->sym.auth.iv_size,
1817                                         options->auth_iv_param,
1818                                         options->auth_iv_random_size,
1819                                         &options->auth_iv.length);
1820                         /*
1821                          * Check if length of provided AAD is supported
1822                          * by the algorithm chosen.
1823                          */
1824                         if (options->aad_param) {
1825                                 if (check_supported_size(options->aad.length,
1826                                                 cap->sym.auth.aad_size.min,
1827                                                 cap->sym.auth.aad_size.max,
1828                                                 cap->sym.auth.aad_size.increment)
1829                                                         != 0) {
1830                                         printf("Unsupported AAD length\n");
1831                                         return -1;
1832                                 }
1833                         /*
1834                          * Check if length of AAD to be randomly generated
1835                          * is supported by the algorithm chosen.
1836                          */
1837                         } else if (options->aad_random_size != -1) {
1838                                 if (check_supported_size(options->aad_random_size,
1839                                                 cap->sym.auth.aad_size.min,
1840                                                 cap->sym.auth.aad_size.max,
1841                                                 cap->sym.auth.aad_size.increment)
1842                                                         != 0) {
1843                                         printf("Unsupported AAD length\n");
1844                                         return -1;
1845                                 }
1846                                 options->aad.length = options->aad_random_size;
1847                         /* No size provided, use minimum size. */
1848                         } else
1849                                 options->aad.length = cap->sym.auth.aad_size.min;
1850
1851                         options->auth_xform.auth.add_auth_data_length =
1852                                                 options->aad.length;
1853
1854                         /*
1855                          * Check if length of provided auth key is supported
1856                          * by the algorithm chosen.
1857                          */
1858                         if (options->akey_param) {
1859                                 if (check_supported_size(
1860                                                 options->auth_xform.auth.key.length,
1861                                                 cap->sym.auth.key_size.min,
1862                                                 cap->sym.auth.key_size.max,
1863                                                 cap->sym.auth.key_size.increment)
1864                                                         != 0) {
1865                                         printf("Unsupported auth key length\n");
1866                                         return -1;
1867                                 }
1868                         /*
1869                          * Check if length of the auth key to be randomly generated
1870                          * is supported by the algorithm chosen.
1871                          */
1872                         } else if (options->akey_random_size != -1) {
1873                                 if (check_supported_size(options->akey_random_size,
1874                                                 cap->sym.auth.key_size.min,
1875                                                 cap->sym.auth.key_size.max,
1876                                                 cap->sym.auth.key_size.increment)
1877                                                         != 0) {
1878                                         printf("Unsupported auth key length\n");
1879                                         return -1;
1880                                 }
1881                                 options->auth_xform.auth.key.length =
1882                                                         options->akey_random_size;
1883                         /* No size provided, use minimum size. */
1884                         } else
1885                                 options->auth_xform.auth.key.length =
1886                                                 cap->sym.auth.key_size.min;
1887
1888                         if (!options->akey_param)
1889                                 generate_random_key(
1890                                         options->auth_xform.auth.key.data,
1891                                         options->auth_xform.auth.key.length);
1892
1893                         /* Check if digest size is supported by the algorithm. */
1894                         if (options->digest_size != -1) {
1895                                 if (check_supported_size(options->digest_size,
1896                                                 cap->sym.auth.digest_size.min,
1897                                                 cap->sym.auth.digest_size.max,
1898                                                 cap->sym.auth.digest_size.increment)
1899                                                         != 0) {
1900                                         printf("Unsupported digest length\n");
1901                                         return -1;
1902                                 }
1903                                 options->auth_xform.auth.digest_length =
1904                                                         options->digest_size;
1905                         /* No size provided, use minimum size. */
1906                         } else
1907                                 options->auth_xform.auth.digest_length =
1908                                                 cap->sym.auth.digest_size.min;
1909                 }
1910
1911                 retval = rte_cryptodev_configure(cdev_id, &conf);
1912                 if (retval < 0) {
1913                         printf("Failed to configure cryptodev %u", cdev_id);
1914                         return -1;
1915                 }
1916
1917                 qp_conf.nb_descriptors = 2048;
1918
1919                 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1920                                 SOCKET_ID_ANY);
1921                 if (retval < 0) {
1922                         printf("Failed to setup queue pair %u on cryptodev %u",
1923                                         0, cdev_id);
1924                         return -1;
1925                 }
1926
1927                 retval = rte_cryptodev_start(cdev_id);
1928                 if (retval < 0) {
1929                         printf("Failed to start device %u: error %d\n",
1930                                         cdev_id, retval);
1931                         return -1;
1932                 }
1933
1934                 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
1935
1936                 enabled_cdevs[cdev_id] = 1;
1937                 enabled_cdev_count++;
1938         }
1939
1940         return enabled_cdev_count;
1941 }
1942
1943 static int
1944 initialize_ports(struct l2fwd_crypto_options *options)
1945 {
1946         uint8_t last_portid, portid;
1947         unsigned enabled_portcount = 0;
1948         unsigned nb_ports = rte_eth_dev_count();
1949
1950         if (nb_ports == 0) {
1951                 printf("No Ethernet ports - bye\n");
1952                 return -1;
1953         }
1954
1955         /* Reset l2fwd_dst_ports */
1956         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1957                 l2fwd_dst_ports[portid] = 0;
1958
1959         for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1960                 int retval;
1961
1962                 /* Skip ports that are not enabled */
1963                 if ((options->portmask & (1 << portid)) == 0)
1964                         continue;
1965
1966                 /* init port */
1967                 printf("Initializing port %u... ", (unsigned) portid);
1968                 fflush(stdout);
1969                 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1970                 if (retval < 0) {
1971                         printf("Cannot configure device: err=%d, port=%u\n",
1972                                   retval, (unsigned) portid);
1973                         return -1;
1974                 }
1975
1976                 /* init one RX queue */
1977                 fflush(stdout);
1978                 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1979                                              rte_eth_dev_socket_id(portid),
1980                                              NULL, l2fwd_pktmbuf_pool);
1981                 if (retval < 0) {
1982                         printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1983                                         retval, (unsigned) portid);
1984                         return -1;
1985                 }
1986
1987                 /* init one TX queue on each port */
1988                 fflush(stdout);
1989                 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1990                                 rte_eth_dev_socket_id(portid),
1991                                 NULL);
1992                 if (retval < 0) {
1993                         printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1994                                 retval, (unsigned) portid);
1995
1996                         return -1;
1997                 }
1998
1999                 /* Start device */
2000                 retval = rte_eth_dev_start(portid);
2001                 if (retval < 0) {
2002                         printf("rte_eth_dev_start:err=%d, port=%u\n",
2003                                         retval, (unsigned) portid);
2004                         return -1;
2005                 }
2006
2007                 rte_eth_promiscuous_enable(portid);
2008
2009                 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2010
2011                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2012                                 (unsigned) portid,
2013                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2014                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2015                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2016                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2017                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2018                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2019
2020                 /* initialize port stats */
2021                 memset(&port_statistics, 0, sizeof(port_statistics));
2022
2023                 /* Setup port forwarding table */
2024                 if (enabled_portcount % 2) {
2025                         l2fwd_dst_ports[portid] = last_portid;
2026                         l2fwd_dst_ports[last_portid] = portid;
2027                 } else {
2028                         last_portid = portid;
2029                 }
2030
2031                 l2fwd_enabled_port_mask |= (1 << portid);
2032                 enabled_portcount++;
2033         }
2034
2035         if (enabled_portcount == 1) {
2036                 l2fwd_dst_ports[last_portid] = last_portid;
2037         } else if (enabled_portcount % 2) {
2038                 printf("odd number of ports in portmask- bye\n");
2039                 return -1;
2040         }
2041
2042         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2043
2044         return enabled_portcount;
2045 }
2046
2047 static void
2048 reserve_key_memory(struct l2fwd_crypto_options *options)
2049 {
2050         options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2051                                                 MAX_KEY_SIZE, 0);
2052         if (options->cipher_xform.cipher.key.data == NULL)
2053                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2054
2055
2056         options->auth_xform.auth.key.data = rte_malloc("auth key",
2057                                                 MAX_KEY_SIZE, 0);
2058         if (options->auth_xform.auth.key.data == NULL)
2059                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2060
2061         options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2062         if (options->cipher_iv.data == NULL)
2063                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2064
2065         options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2066         if (options->auth_iv.data == NULL)
2067                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2068
2069         options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2070         if (options->aad.data == NULL)
2071                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2072         options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
2073 }
2074
2075 int
2076 main(int argc, char **argv)
2077 {
2078         struct lcore_queue_conf *qconf;
2079         struct l2fwd_crypto_options options;
2080
2081         uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
2082         unsigned lcore_id, rx_lcore_id;
2083         int ret, enabled_cdevcount, enabled_portcount;
2084         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2085
2086         /* init EAL */
2087         ret = rte_eal_init(argc, argv);
2088         if (ret < 0)
2089                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2090         argc -= ret;
2091         argv += ret;
2092
2093         /* reserve memory for Cipher/Auth key and IV */
2094         reserve_key_memory(&options);
2095
2096         /* parse application arguments (after the EAL ones) */
2097         ret = l2fwd_crypto_parse_args(&options, argc, argv);
2098         if (ret < 0)
2099                 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2100
2101         /* create the mbuf pool */
2102         l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2103                         sizeof(struct rte_crypto_op),
2104                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2105         if (l2fwd_pktmbuf_pool == NULL)
2106                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2107
2108         /* create crypto op pool */
2109         l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2110                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2111                         rte_socket_id());
2112         if (l2fwd_crypto_op_pool == NULL)
2113                 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2114
2115         /* Enable Ethernet ports */
2116         enabled_portcount = initialize_ports(&options);
2117         if (enabled_portcount < 1)
2118                 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2119
2120         nb_ports = rte_eth_dev_count();
2121         /* Initialize the port/queue configuration of each logical core */
2122         for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2123                         portid < nb_ports; portid++) {
2124
2125                 /* skip ports that are not enabled */
2126                 if ((options.portmask & (1 << portid)) == 0)
2127                         continue;
2128
2129                 if (options.single_lcore && qconf == NULL) {
2130                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2131                                 rx_lcore_id++;
2132                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2133                                         rte_exit(EXIT_FAILURE,
2134                                                         "Not enough cores\n");
2135                         }
2136                 } else if (!options.single_lcore) {
2137                         /* get the lcore_id for this port */
2138                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2139                                lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2140                                options.nb_ports_per_lcore) {
2141                                 rx_lcore_id++;
2142                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2143                                         rte_exit(EXIT_FAILURE,
2144                                                         "Not enough cores\n");
2145                         }
2146                 }
2147
2148                 /* Assigned a new logical core in the loop above. */
2149                 if (qconf != &lcore_queue_conf[rx_lcore_id])
2150                         qconf = &lcore_queue_conf[rx_lcore_id];
2151
2152                 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2153                 qconf->nb_rx_ports++;
2154
2155                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
2156         }
2157
2158         /* Enable Crypto devices */
2159         enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2160                         enabled_cdevs);
2161         if (enabled_cdevcount < 0)
2162                 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2163
2164         if (enabled_cdevcount < enabled_portcount)
2165                 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2166                                 "has to be more or equal to number of ports (%d)\n",
2167                                 enabled_cdevcount, enabled_portcount);
2168
2169         nb_cryptodevs = rte_cryptodev_count();
2170
2171         /* Initialize the port/cryptodev configuration of each logical core */
2172         for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2173                         cdev_id < nb_cryptodevs && enabled_cdevcount;
2174                         cdev_id++) {
2175                 /* Crypto op not supported by crypto device */
2176                 if (!enabled_cdevs[cdev_id])
2177                         continue;
2178
2179                 if (options.single_lcore && qconf == NULL) {
2180                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2181                                 rx_lcore_id++;
2182                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2183                                         rte_exit(EXIT_FAILURE,
2184                                                         "Not enough cores\n");
2185                         }
2186                 } else if (!options.single_lcore) {
2187                         /* get the lcore_id for this port */
2188                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2189                                lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2190                                options.nb_ports_per_lcore) {
2191                                 rx_lcore_id++;
2192                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2193                                         rte_exit(EXIT_FAILURE,
2194                                                         "Not enough cores\n");
2195                         }
2196                 }
2197
2198                 /* Assigned a new logical core in the loop above. */
2199                 if (qconf != &lcore_queue_conf[rx_lcore_id])
2200                         qconf = &lcore_queue_conf[rx_lcore_id];
2201
2202                 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2203                 qconf->nb_crypto_devs++;
2204
2205                 enabled_cdevcount--;
2206
2207                 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2208                                 (unsigned)cdev_id);
2209         }
2210
2211         /* launch per-lcore init on every lcore */
2212         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2213                         CALL_MASTER);
2214         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2215                 if (rte_eal_wait_lcore(lcore_id) < 0)
2216                         return -1;
2217         }
2218
2219         return 0;
2220 }