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