examples/l2fwd-crypto: support AES-CCM
[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 uint32_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         unsigned 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         unsigned 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->auth.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                 uint8_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, uint8_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, unsigned int 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, unsigned int portid,
641                 struct l2fwd_crypto_options *options)
642 {
643         unsigned int 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, (uint8_t) 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 i, j, portid, nb_rx, len;
723         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
724         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
725                         US_PER_S * BURST_TX_DRAIN_US;
726         struct l2fwd_crypto_params *cparams;
727         struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
728         struct rte_cryptodev_sym_session *session;
729
730         if (qconf->nb_rx_ports == 0) {
731                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
732                 return;
733         }
734
735         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
736
737         for (i = 0; i < qconf->nb_rx_ports; i++) {
738
739                 portid = qconf->rx_port_list[i];
740                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
741                         portid);
742         }
743
744         for (i = 0; i < qconf->nb_crypto_devs; i++) {
745                 port_cparams[i].do_cipher = 0;
746                 port_cparams[i].do_hash = 0;
747                 port_cparams[i].do_aead = 0;
748
749                 switch (options->xform_chain) {
750                 case L2FWD_CRYPTO_AEAD:
751                         port_cparams[i].do_aead = 1;
752                         break;
753                 case L2FWD_CRYPTO_CIPHER_HASH:
754                 case L2FWD_CRYPTO_HASH_CIPHER:
755                         port_cparams[i].do_cipher = 1;
756                         port_cparams[i].do_hash = 1;
757                         break;
758                 case L2FWD_CRYPTO_HASH_ONLY:
759                         port_cparams[i].do_hash = 1;
760                         break;
761                 case L2FWD_CRYPTO_CIPHER_ONLY:
762                         port_cparams[i].do_cipher = 1;
763                         break;
764                 }
765
766                 port_cparams[i].dev_id = qconf->cryptodev_list[i];
767                 port_cparams[i].qp_id = 0;
768
769                 port_cparams[i].block_size = options->block_size;
770
771                 if (port_cparams[i].do_hash) {
772                         port_cparams[i].auth_iv.data = options->auth_iv.data;
773                         port_cparams[i].auth_iv.length = options->auth_iv.length;
774                         if (!options->auth_iv_param)
775                                 generate_random_key(port_cparams[i].auth_iv.data,
776                                                 port_cparams[i].auth_iv.length);
777                         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
778                                 port_cparams[i].hash_verify = 1;
779                         else
780                                 port_cparams[i].hash_verify = 0;
781
782                         port_cparams[i].auth_algo = options->auth_xform.auth.algo;
783                         port_cparams[i].digest_length =
784                                         options->auth_xform.auth.digest_length;
785                         /* Set IV parameters */
786                         if (options->auth_iv.length) {
787                                 options->auth_xform.auth.iv.offset =
788                                         IV_OFFSET + options->cipher_iv.length;
789                                 options->auth_xform.auth.iv.length =
790                                         options->auth_iv.length;
791                         }
792                 }
793
794                 if (port_cparams[i].do_aead) {
795                         port_cparams[i].aead_iv.data = options->aead_iv.data;
796                         port_cparams[i].aead_iv.length = options->aead_iv.length;
797                         if (!options->aead_iv_param)
798                                 generate_random_key(port_cparams[i].aead_iv.data,
799                                                 port_cparams[i].aead_iv.length);
800                         port_cparams[i].aead_algo = options->aead_xform.aead.algo;
801                         port_cparams[i].digest_length =
802                                         options->aead_xform.aead.digest_length;
803                         if (options->aead_xform.aead.aad_length) {
804                                 port_cparams[i].aad.data = options->aad.data;
805                                 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
806                                 port_cparams[i].aad.length = options->aad.length;
807                                 if (!options->aad_param)
808                                         generate_random_key(port_cparams[i].aad.data,
809                                                 port_cparams[i].aad.length);
810                                 /*
811                                  * If doing AES-CCM, first 18 bytes has to be reserved,
812                                  * and actual AAD should start from byte 18
813                                  */
814                                 if (port_cparams[i].aead_algo == RTE_CRYPTO_AEAD_AES_CCM)
815                                         memmove(port_cparams[i].aad.data + 18,
816                                                         port_cparams[i].aad.data,
817                                                         port_cparams[i].aad.length);
818
819                         } else
820                                 port_cparams[i].aad.length = 0;
821
822                         if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
823                                 port_cparams[i].hash_verify = 1;
824                         else
825                                 port_cparams[i].hash_verify = 0;
826
827                         /* Set IV parameters */
828                         options->aead_xform.aead.iv.offset = IV_OFFSET;
829                         options->aead_xform.aead.iv.length = options->aead_iv.length;
830                 }
831
832                 if (port_cparams[i].do_cipher) {
833                         port_cparams[i].cipher_iv.data = options->cipher_iv.data;
834                         port_cparams[i].cipher_iv.length = options->cipher_iv.length;
835                         if (!options->cipher_iv_param)
836                                 generate_random_key(port_cparams[i].cipher_iv.data,
837                                                 port_cparams[i].cipher_iv.length);
838
839                         port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
840                         /* Set IV parameters */
841                         options->cipher_xform.cipher.iv.offset = IV_OFFSET;
842                         options->cipher_xform.cipher.iv.length =
843                                                 options->cipher_iv.length;
844                 }
845
846                 session = initialize_crypto_session(options,
847                                 port_cparams[i].dev_id);
848                 if (session == NULL)
849                         rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
850
851                 port_cparams[i].session = session;
852
853                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
854                                 port_cparams[i].dev_id);
855         }
856
857         l2fwd_crypto_options_print(options);
858
859         /*
860          * Initialize previous tsc timestamp before the loop,
861          * to avoid showing the port statistics immediately,
862          * so user can see the crypto information.
863          */
864         prev_tsc = rte_rdtsc();
865         while (1) {
866
867                 cur_tsc = rte_rdtsc();
868
869                 /*
870                  * Crypto device/TX burst queue drain
871                  */
872                 diff_tsc = cur_tsc - prev_tsc;
873                 if (unlikely(diff_tsc > drain_tsc)) {
874                         /* Enqueue all crypto ops remaining in buffers */
875                         for (i = 0; i < qconf->nb_crypto_devs; i++) {
876                                 cparams = &port_cparams[i];
877                                 len = qconf->op_buf[cparams->dev_id].len;
878                                 l2fwd_crypto_send_burst(qconf, len, cparams);
879                                 qconf->op_buf[cparams->dev_id].len = 0;
880                         }
881                         /* Transmit all packets remaining in buffers */
882                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
883                                 if (qconf->pkt_buf[portid].len == 0)
884                                         continue;
885                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
886                                                  qconf->pkt_buf[portid].len,
887                                                  (uint8_t) portid);
888                                 qconf->pkt_buf[portid].len = 0;
889                         }
890
891                         /* if timer is enabled */
892                         if (timer_period > 0) {
893
894                                 /* advance the timer */
895                                 timer_tsc += diff_tsc;
896
897                                 /* if timer has reached its timeout */
898                                 if (unlikely(timer_tsc >=
899                                                 (uint64_t)timer_period)) {
900
901                                         /* do this only on master core */
902                                         if (lcore_id == rte_get_master_lcore()
903                                                 && options->refresh_period) {
904                                                 print_stats();
905                                                 timer_tsc = 0;
906                                         }
907                                 }
908                         }
909
910                         prev_tsc = cur_tsc;
911                 }
912
913                 /*
914                  * Read packet from RX queues
915                  */
916                 for (i = 0; i < qconf->nb_rx_ports; i++) {
917                         portid = qconf->rx_port_list[i];
918
919                         cparams = &port_cparams[i];
920
921                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
922                                                  pkts_burst, MAX_PKT_BURST);
923
924                         port_statistics[portid].rx += nb_rx;
925
926                         if (nb_rx) {
927                                 /*
928                                  * If we can't allocate a crypto_ops, then drop
929                                  * the rest of the burst and dequeue and
930                                  * process the packets to free offload structs
931                                  */
932                                 if (rte_crypto_op_bulk_alloc(
933                                                 l2fwd_crypto_op_pool,
934                                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
935                                                 ops_burst, nb_rx) !=
936                                                                 nb_rx) {
937                                         for (j = 0; j < nb_rx; j++)
938                                                 rte_pktmbuf_free(pkts_burst[j]);
939
940                                         nb_rx = 0;
941                                 }
942
943                                 /* Enqueue packets from Crypto device*/
944                                 for (j = 0; j < nb_rx; j++) {
945                                         m = pkts_burst[j];
946
947                                         l2fwd_simple_crypto_enqueue(m,
948                                                         ops_burst[j], cparams);
949                                 }
950                         }
951
952                         /* Dequeue packets from Crypto device */
953                         do {
954                                 nb_rx = rte_cryptodev_dequeue_burst(
955                                                 cparams->dev_id, cparams->qp_id,
956                                                 ops_burst, MAX_PKT_BURST);
957
958                                 crypto_statistics[cparams->dev_id].dequeued +=
959                                                 nb_rx;
960
961                                 /* Forward crypto'd packets */
962                                 for (j = 0; j < nb_rx; j++) {
963                                         m = ops_burst[j]->sym->m_src;
964
965                                         rte_crypto_op_free(ops_burst[j]);
966                                         l2fwd_simple_forward(m, portid,
967                                                         options);
968                                 }
969                         } while (nb_rx == MAX_PKT_BURST);
970                 }
971         }
972 }
973
974 static int
975 l2fwd_launch_one_lcore(void *arg)
976 {
977         l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
978         return 0;
979 }
980
981 /* Display command line arguments usage */
982 static void
983 l2fwd_crypto_usage(const char *prgname)
984 {
985         printf("%s [EAL options] --\n"
986                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
987                 "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
988                 "  -s manage all ports from single lcore\n"
989                 "  -T PERIOD: statistics will be refreshed each PERIOD seconds"
990                 " (0 to disable, 10 default, 86400 maximum)\n"
991
992                 "  --cdev_type HW / SW / ANY\n"
993                 "  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
994                 " HASH_ONLY / AEAD\n"
995
996                 "  --cipher_algo ALGO\n"
997                 "  --cipher_op ENCRYPT / DECRYPT\n"
998                 "  --cipher_key KEY (bytes separated with \":\")\n"
999                 "  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
1000                 "  --cipher_iv IV (bytes separated with \":\")\n"
1001                 "  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
1002
1003                 "  --auth_algo ALGO\n"
1004                 "  --auth_op GENERATE / VERIFY\n"
1005                 "  --auth_key KEY (bytes separated with \":\")\n"
1006                 "  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
1007                 "  --auth_iv IV (bytes separated with \":\")\n"
1008                 "  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
1009
1010                 "  --aead_algo ALGO\n"
1011                 "  --aead_op ENCRYPT / DECRYPT\n"
1012                 "  --aead_key KEY (bytes separated with \":\")\n"
1013                 "  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
1014                 "  --aead_iv IV (bytes separated with \":\")\n"
1015                 "  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
1016                 "  --aad AAD (bytes separated with \":\")\n"
1017                 "  --aad_random_size SIZE: size of AAD when generated randomly\n"
1018
1019                 "  --digest_size SIZE: size of digest to be generated/verified\n"
1020
1021                 "  --sessionless\n"
1022                 "  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n"
1023
1024                 "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
1025                 "      When enabled:\n"
1026                 "       - The source MAC address is replaced by the TX port MAC address\n"
1027                 "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
1028                prgname);
1029 }
1030
1031 /** Parse crypto device type command line argument */
1032 static int
1033 parse_cryptodev_type(enum cdev_type *type, char *optarg)
1034 {
1035         if (strcmp("HW", optarg) == 0) {
1036                 *type = CDEV_TYPE_HW;
1037                 return 0;
1038         } else if (strcmp("SW", optarg) == 0) {
1039                 *type = CDEV_TYPE_SW;
1040                 return 0;
1041         } else if (strcmp("ANY", optarg) == 0) {
1042                 *type = CDEV_TYPE_ANY;
1043                 return 0;
1044         }
1045
1046         return -1;
1047 }
1048
1049 /** Parse crypto chain xform command line argument */
1050 static int
1051 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1052 {
1053         if (strcmp("CIPHER_HASH", optarg) == 0) {
1054                 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1055                 return 0;
1056         } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1057                 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1058                 return 0;
1059         } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1060                 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1061                 return 0;
1062         } else if (strcmp("HASH_ONLY", optarg) == 0) {
1063                 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1064                 return 0;
1065         } else if (strcmp("AEAD", optarg) == 0) {
1066                 options->xform_chain = L2FWD_CRYPTO_AEAD;
1067                 return 0;
1068         }
1069
1070         return -1;
1071 }
1072
1073 /** Parse crypto cipher algo option command line argument */
1074 static int
1075 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1076 {
1077
1078         if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1079                 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1080                                 "not supported!\n");
1081                 return -1;
1082         }
1083
1084         return 0;
1085 }
1086
1087 /** Parse crypto cipher operation command line argument */
1088 static int
1089 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1090 {
1091         if (strcmp("ENCRYPT", optarg) == 0) {
1092                 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1093                 return 0;
1094         } else if (strcmp("DECRYPT", optarg) == 0) {
1095                 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1096                 return 0;
1097         }
1098
1099         printf("Cipher operation not supported!\n");
1100         return -1;
1101 }
1102
1103 /** Parse bytes from command line argument */
1104 static int
1105 parse_bytes(uint8_t *data, char *input_arg, uint16_t max_size)
1106 {
1107         unsigned byte_count;
1108         char *token;
1109
1110         errno = 0;
1111         for (byte_count = 0, token = strtok(input_arg, ":");
1112                         (byte_count < max_size) && (token != NULL);
1113                         token = strtok(NULL, ":")) {
1114
1115                 int number = (int)strtol(token, NULL, 16);
1116
1117                 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1118                         return -1;
1119
1120                 data[byte_count++] = (uint8_t)number;
1121         }
1122
1123         return byte_count;
1124 }
1125
1126 /** Parse size param*/
1127 static int
1128 parse_size(int *size, const char *q_arg)
1129 {
1130         char *end = NULL;
1131         unsigned long n;
1132
1133         /* parse hexadecimal string */
1134         n = strtoul(q_arg, &end, 10);
1135         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1136                 n = 0;
1137
1138         if (n == 0) {
1139                 printf("invalid size\n");
1140                 return -1;
1141         }
1142
1143         *size = n;
1144         return 0;
1145 }
1146
1147 /** Parse crypto cipher operation command line argument */
1148 static int
1149 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1150 {
1151         if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1152                 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1153                                 "not supported!\n");
1154                 return -1;
1155         }
1156
1157         return 0;
1158 }
1159
1160 static int
1161 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1162 {
1163         if (strcmp("VERIFY", optarg) == 0) {
1164                 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1165                 return 0;
1166         } else if (strcmp("GENERATE", optarg) == 0) {
1167                 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1168                 return 0;
1169         }
1170
1171         printf("Authentication operation specified not supported!\n");
1172         return -1;
1173 }
1174
1175 static int
1176 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1177 {
1178         if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1179                 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1180                                 "not supported!\n");
1181                 return -1;
1182         }
1183
1184         return 0;
1185 }
1186
1187 static int
1188 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1189 {
1190         if (strcmp("ENCRYPT", optarg) == 0) {
1191                 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1192                 return 0;
1193         } else if (strcmp("DECRYPT", optarg) == 0) {
1194                 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1195                 return 0;
1196         }
1197
1198         printf("AEAD operation specified not supported!\n");
1199         return -1;
1200 }
1201 static int
1202 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1203                 const char *q_arg)
1204 {
1205         char *end = NULL;
1206         uint64_t pm;
1207
1208         /* parse hexadecimal string */
1209         pm = strtoul(q_arg, &end, 16);
1210         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1211                 pm = 0;
1212
1213         options->cryptodev_mask = pm;
1214         if (options->cryptodev_mask == 0) {
1215                 printf("invalid cryptodev_mask specified\n");
1216                 return -1;
1217         }
1218
1219         return 0;
1220 }
1221
1222 /** Parse long options */
1223 static int
1224 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1225                 struct option *lgopts, int option_index)
1226 {
1227         int retval;
1228
1229         if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1230                 retval = parse_cryptodev_type(&options->type, optarg);
1231                 if (retval == 0)
1232                         snprintf(options->string_type, MAX_STR_LEN,
1233                                 "%s", optarg);
1234                 return retval;
1235         }
1236
1237         else if (strcmp(lgopts[option_index].name, "chain") == 0)
1238                 return parse_crypto_opt_chain(options, optarg);
1239
1240         /* Cipher options */
1241         else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1242                 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1243                                 optarg);
1244
1245         else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1246                 return parse_cipher_op(&options->cipher_xform.cipher.op,
1247                                 optarg);
1248
1249         else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1250                 options->ckey_param = 1;
1251                 options->cipher_xform.cipher.key.length =
1252                         parse_bytes(options->cipher_xform.cipher.key.data, optarg,
1253                                         MAX_KEY_SIZE);
1254                 if (options->cipher_xform.cipher.key.length > 0)
1255                         return 0;
1256                 else
1257                         return -1;
1258         }
1259
1260         else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1261                 return parse_size(&options->ckey_random_size, optarg);
1262
1263         else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1264                 options->cipher_iv_param = 1;
1265                 options->cipher_iv.length =
1266                         parse_bytes(options->cipher_iv.data, optarg, MAX_IV_SIZE);
1267                 if (options->cipher_iv.length > 0)
1268                         return 0;
1269                 else
1270                         return -1;
1271         }
1272
1273         else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1274                 return parse_size(&options->cipher_iv_random_size, optarg);
1275
1276         /* Authentication options */
1277         else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1278                 return parse_auth_algo(&options->auth_xform.auth.algo,
1279                                 optarg);
1280         }
1281
1282         else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1283                 return parse_auth_op(&options->auth_xform.auth.op,
1284                                 optarg);
1285
1286         else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1287                 options->akey_param = 1;
1288                 options->auth_xform.auth.key.length =
1289                         parse_bytes(options->auth_xform.auth.key.data, optarg,
1290                                         MAX_KEY_SIZE);
1291                 if (options->auth_xform.auth.key.length > 0)
1292                         return 0;
1293                 else
1294                         return -1;
1295         }
1296
1297         else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1298                 return parse_size(&options->akey_random_size, optarg);
1299         }
1300
1301         else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1302                 options->auth_iv_param = 1;
1303                 options->auth_iv.length =
1304                         parse_bytes(options->auth_iv.data, optarg, MAX_IV_SIZE);
1305                 if (options->auth_iv.length > 0)
1306                         return 0;
1307                 else
1308                         return -1;
1309         }
1310
1311         else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1312                 return parse_size(&options->auth_iv_random_size, optarg);
1313
1314         /* AEAD options */
1315         else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1316                 return parse_aead_algo(&options->aead_xform.aead.algo,
1317                                 optarg);
1318         }
1319
1320         else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1321                 return parse_aead_op(&options->aead_xform.aead.op,
1322                                 optarg);
1323
1324         else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1325                 options->aead_key_param = 1;
1326                 options->aead_xform.aead.key.length =
1327                         parse_bytes(options->aead_xform.aead.key.data, optarg,
1328                                         MAX_KEY_SIZE);
1329                 if (options->aead_xform.aead.key.length > 0)
1330                         return 0;
1331                 else
1332                         return -1;
1333         }
1334
1335         else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1336                 return parse_size(&options->aead_key_random_size, optarg);
1337
1338
1339         else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1340                 options->aead_iv_param = 1;
1341                 options->aead_iv.length =
1342                         parse_bytes(options->aead_iv.data, optarg, MAX_IV_SIZE);
1343                 if (options->aead_iv.length > 0)
1344                         return 0;
1345                 else
1346                         return -1;
1347         }
1348
1349         else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1350                 return parse_size(&options->aead_iv_random_size, optarg);
1351
1352         else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1353                 options->aad_param = 1;
1354                 options->aad.length =
1355                         parse_bytes(options->aad.data, optarg, MAX_AAD_SIZE);
1356                 if (options->aad.length > 0)
1357                         return 0;
1358                 else
1359                         return -1;
1360         }
1361
1362         else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1363                 return parse_size(&options->aad_random_size, optarg);
1364         }
1365
1366         else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1367                 return parse_size(&options->digest_size, optarg);
1368         }
1369
1370         else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1371                 options->sessionless = 1;
1372                 return 0;
1373         }
1374
1375         else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1376                 return parse_cryptodev_mask(options, optarg);
1377
1378         else if (strcmp(lgopts[option_index].name, "mac-updating") == 0) {
1379                 options->mac_updating = 1;
1380                 return 0;
1381         }
1382
1383         else if (strcmp(lgopts[option_index].name, "no-mac-updating") == 0) {
1384                 options->mac_updating = 0;
1385                 return 0;
1386         }
1387
1388         return -1;
1389 }
1390
1391 /** Parse port mask */
1392 static int
1393 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1394                 const char *q_arg)
1395 {
1396         char *end = NULL;
1397         unsigned long pm;
1398
1399         /* parse hexadecimal string */
1400         pm = strtoul(q_arg, &end, 16);
1401         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1402                 pm = 0;
1403
1404         options->portmask = pm;
1405         if (options->portmask == 0) {
1406                 printf("invalid portmask specified\n");
1407                 return -1;
1408         }
1409
1410         return pm;
1411 }
1412
1413 /** Parse number of queues */
1414 static int
1415 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1416                 const char *q_arg)
1417 {
1418         char *end = NULL;
1419         unsigned long n;
1420
1421         /* parse hexadecimal string */
1422         n = strtoul(q_arg, &end, 10);
1423         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1424                 n = 0;
1425         else if (n >= MAX_RX_QUEUE_PER_LCORE)
1426                 n = 0;
1427
1428         options->nb_ports_per_lcore = n;
1429         if (options->nb_ports_per_lcore == 0) {
1430                 printf("invalid number of ports selected\n");
1431                 return -1;
1432         }
1433
1434         return 0;
1435 }
1436
1437 /** Parse timer period */
1438 static int
1439 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1440                 const char *q_arg)
1441 {
1442         char *end = NULL;
1443         unsigned long n;
1444
1445         /* parse number string */
1446         n = (unsigned)strtol(q_arg, &end, 10);
1447         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1448                 n = 0;
1449
1450         if (n >= MAX_TIMER_PERIOD) {
1451                 printf("Warning refresh period specified %lu is greater than "
1452                                 "max value %lu! using max value",
1453                                 n, MAX_TIMER_PERIOD);
1454                 n = MAX_TIMER_PERIOD;
1455         }
1456
1457         options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1458
1459         return 0;
1460 }
1461
1462 /** Generate default options for application */
1463 static void
1464 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1465 {
1466         options->portmask = 0xffffffff;
1467         options->nb_ports_per_lcore = 1;
1468         options->refresh_period = 10000;
1469         options->single_lcore = 0;
1470         options->sessionless = 0;
1471
1472         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1473
1474         /* Cipher Data */
1475         options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1476         options->cipher_xform.next = NULL;
1477         options->ckey_param = 0;
1478         options->ckey_random_size = -1;
1479         options->cipher_xform.cipher.key.length = 0;
1480         options->cipher_iv_param = 0;
1481         options->cipher_iv_random_size = -1;
1482         options->cipher_iv.length = 0;
1483
1484         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1485         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1486
1487         /* Authentication Data */
1488         options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1489         options->auth_xform.next = NULL;
1490         options->akey_param = 0;
1491         options->akey_random_size = -1;
1492         options->auth_xform.auth.key.length = 0;
1493         options->auth_iv_param = 0;
1494         options->auth_iv_random_size = -1;
1495         options->auth_iv.length = 0;
1496
1497         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1498         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1499
1500         /* AEAD Data */
1501         options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1502         options->aead_xform.next = NULL;
1503         options->aead_key_param = 0;
1504         options->aead_key_random_size = -1;
1505         options->aead_xform.aead.key.length = 0;
1506         options->aead_iv_param = 0;
1507         options->aead_iv_random_size = -1;
1508         options->aead_iv.length = 0;
1509
1510         options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1511         options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1512
1513         options->aad_param = 0;
1514         options->aad_random_size = -1;
1515         options->aad.length = 0;
1516
1517         options->digest_size = -1;
1518
1519         options->type = CDEV_TYPE_ANY;
1520         options->cryptodev_mask = UINT64_MAX;
1521
1522         options->mac_updating = 1;
1523 }
1524
1525 static void
1526 display_cipher_info(struct l2fwd_crypto_options *options)
1527 {
1528         printf("\n---- Cipher information ---\n");
1529         printf("Algorithm: %s\n",
1530                 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1531         rte_hexdump(stdout, "Cipher key:",
1532                         options->cipher_xform.cipher.key.data,
1533                         options->cipher_xform.cipher.key.length);
1534         rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1535 }
1536
1537 static void
1538 display_auth_info(struct l2fwd_crypto_options *options)
1539 {
1540         printf("\n---- Authentication information ---\n");
1541         printf("Algorithm: %s\n",
1542                 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1543         rte_hexdump(stdout, "Auth key:",
1544                         options->auth_xform.auth.key.data,
1545                         options->auth_xform.auth.key.length);
1546         rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1547 }
1548
1549 static void
1550 display_aead_info(struct l2fwd_crypto_options *options)
1551 {
1552         printf("\n---- AEAD information ---\n");
1553         printf("Algorithm: %s\n",
1554                 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1555         rte_hexdump(stdout, "AEAD key:",
1556                         options->aead_xform.aead.key.data,
1557                         options->aead_xform.aead.key.length);
1558         rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1559         rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1560 }
1561
1562 static void
1563 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1564 {
1565         char string_cipher_op[MAX_STR_LEN];
1566         char string_auth_op[MAX_STR_LEN];
1567         char string_aead_op[MAX_STR_LEN];
1568
1569         if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1570                 strcpy(string_cipher_op, "Encrypt");
1571         else
1572                 strcpy(string_cipher_op, "Decrypt");
1573
1574         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1575                 strcpy(string_auth_op, "Auth generate");
1576         else
1577                 strcpy(string_auth_op, "Auth verify");
1578
1579         if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1580                 strcpy(string_aead_op, "Authenticated encryption");
1581         else
1582                 strcpy(string_aead_op, "Authenticated decryption");
1583
1584
1585         printf("Options:-\nn");
1586         printf("portmask: %x\n", options->portmask);
1587         printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1588         printf("refresh period : %u\n", options->refresh_period);
1589         printf("single lcore mode: %s\n",
1590                         options->single_lcore ? "enabled" : "disabled");
1591         printf("stats_printing: %s\n",
1592                         options->refresh_period == 0 ? "disabled" : "enabled");
1593
1594         printf("sessionless crypto: %s\n",
1595                         options->sessionless ? "enabled" : "disabled");
1596
1597         if (options->ckey_param && (options->ckey_random_size != -1))
1598                 printf("Cipher key already parsed, ignoring size of random key\n");
1599
1600         if (options->akey_param && (options->akey_random_size != -1))
1601                 printf("Auth key already parsed, ignoring size of random key\n");
1602
1603         if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1604                 printf("Cipher IV already parsed, ignoring size of random IV\n");
1605
1606         if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1607                 printf("Auth IV already parsed, ignoring size of random IV\n");
1608
1609         if (options->aad_param && (options->aad_random_size != -1))
1610                 printf("AAD already parsed, ignoring size of random AAD\n");
1611
1612         printf("\nCrypto chain: ");
1613         switch (options->xform_chain) {
1614         case L2FWD_CRYPTO_AEAD:
1615                 printf("Input --> %s --> Output\n", string_aead_op);
1616                 display_aead_info(options);
1617                 break;
1618         case L2FWD_CRYPTO_CIPHER_HASH:
1619                 printf("Input --> %s --> %s --> Output\n",
1620                         string_cipher_op, string_auth_op);
1621                 display_cipher_info(options);
1622                 display_auth_info(options);
1623                 break;
1624         case L2FWD_CRYPTO_HASH_CIPHER:
1625                 printf("Input --> %s --> %s --> Output\n",
1626                         string_auth_op, string_cipher_op);
1627                 display_cipher_info(options);
1628                 display_auth_info(options);
1629                 break;
1630         case L2FWD_CRYPTO_HASH_ONLY:
1631                 printf("Input --> %s --> Output\n", string_auth_op);
1632                 display_auth_info(options);
1633                 break;
1634         case L2FWD_CRYPTO_CIPHER_ONLY:
1635                 printf("Input --> %s --> Output\n", string_cipher_op);
1636                 display_cipher_info(options);
1637                 break;
1638         }
1639 }
1640
1641 /* Parse the argument given in the command line of the application */
1642 static int
1643 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1644                 int argc, char **argv)
1645 {
1646         int opt, retval, option_index;
1647         char **argvopt = argv, *prgname = argv[0];
1648
1649         static struct option lgopts[] = {
1650                         { "sessionless", no_argument, 0, 0 },
1651
1652                         { "cdev_type", required_argument, 0, 0 },
1653                         { "chain", required_argument, 0, 0 },
1654
1655                         { "cipher_algo", required_argument, 0, 0 },
1656                         { "cipher_op", required_argument, 0, 0 },
1657                         { "cipher_key", required_argument, 0, 0 },
1658                         { "cipher_key_random_size", required_argument, 0, 0 },
1659                         { "cipher_iv", required_argument, 0, 0 },
1660                         { "cipher_iv_random_size", required_argument, 0, 0 },
1661
1662                         { "auth_algo", required_argument, 0, 0 },
1663                         { "auth_op", required_argument, 0, 0 },
1664                         { "auth_key", required_argument, 0, 0 },
1665                         { "auth_key_random_size", required_argument, 0, 0 },
1666                         { "auth_iv", required_argument, 0, 0 },
1667                         { "auth_iv_random_size", required_argument, 0, 0 },
1668
1669                         { "aead_algo", required_argument, 0, 0 },
1670                         { "aead_op", required_argument, 0, 0 },
1671                         { "aead_key", required_argument, 0, 0 },
1672                         { "aead_key_random_size", required_argument, 0, 0 },
1673                         { "aead_iv", required_argument, 0, 0 },
1674                         { "aead_iv_random_size", required_argument, 0, 0 },
1675
1676                         { "aad", required_argument, 0, 0 },
1677                         { "aad_random_size", required_argument, 0, 0 },
1678
1679                         { "digest_size", required_argument, 0, 0 },
1680
1681                         { "sessionless", no_argument, 0, 0 },
1682                         { "cryptodev_mask", required_argument, 0, 0},
1683
1684                         { "mac-updating", no_argument, 0, 0},
1685                         { "no-mac-updating", no_argument, 0, 0},
1686
1687                         { NULL, 0, 0, 0 }
1688         };
1689
1690         l2fwd_crypto_default_options(options);
1691
1692         while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1693                         &option_index)) != EOF) {
1694                 switch (opt) {
1695                 /* long options */
1696                 case 0:
1697                         retval = l2fwd_crypto_parse_args_long_options(options,
1698                                         lgopts, option_index);
1699                         if (retval < 0) {
1700                                 l2fwd_crypto_usage(prgname);
1701                                 return -1;
1702                         }
1703                         break;
1704
1705                 /* portmask */
1706                 case 'p':
1707                         retval = l2fwd_crypto_parse_portmask(options, optarg);
1708                         if (retval < 0) {
1709                                 l2fwd_crypto_usage(prgname);
1710                                 return -1;
1711                         }
1712                         break;
1713
1714                 /* nqueue */
1715                 case 'q':
1716                         retval = l2fwd_crypto_parse_nqueue(options, optarg);
1717                         if (retval < 0) {
1718                                 l2fwd_crypto_usage(prgname);
1719                                 return -1;
1720                         }
1721                         break;
1722
1723                 /* single  */
1724                 case 's':
1725                         options->single_lcore = 1;
1726
1727                         break;
1728
1729                 /* timer period */
1730                 case 'T':
1731                         retval = l2fwd_crypto_parse_timer_period(options,
1732                                         optarg);
1733                         if (retval < 0) {
1734                                 l2fwd_crypto_usage(prgname);
1735                                 return -1;
1736                         }
1737                         break;
1738
1739                 default:
1740                         l2fwd_crypto_usage(prgname);
1741                         return -1;
1742                 }
1743         }
1744
1745
1746         if (optind >= 0)
1747                 argv[optind-1] = prgname;
1748
1749         retval = optind-1;
1750         optind = 1; /* reset getopt lib */
1751
1752         return retval;
1753 }
1754
1755 /* Check the link status of all ports in up to 9s, and print them finally */
1756 static void
1757 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1758 {
1759 #define CHECK_INTERVAL 100 /* 100ms */
1760 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1761         uint8_t portid, count, all_ports_up, print_flag = 0;
1762         struct rte_eth_link link;
1763
1764         printf("\nChecking link status");
1765         fflush(stdout);
1766         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1767                 all_ports_up = 1;
1768                 for (portid = 0; portid < port_num; portid++) {
1769                         if ((port_mask & (1 << portid)) == 0)
1770                                 continue;
1771                         memset(&link, 0, sizeof(link));
1772                         rte_eth_link_get_nowait(portid, &link);
1773                         /* print link status if flag set */
1774                         if (print_flag == 1) {
1775                                 if (link.link_status)
1776                                         printf("Port %d Link Up - speed %u "
1777                                                 "Mbps - %s\n", (uint8_t)portid,
1778                                                 (unsigned)link.link_speed,
1779                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1780                                         ("full-duplex") : ("half-duplex\n"));
1781                                 else
1782                                         printf("Port %d Link Down\n",
1783                                                 (uint8_t)portid);
1784                                 continue;
1785                         }
1786                         /* clear all_ports_up flag if any link down */
1787                         if (link.link_status == ETH_LINK_DOWN) {
1788                                 all_ports_up = 0;
1789                                 break;
1790                         }
1791                 }
1792                 /* after finally printing all link status, get out */
1793                 if (print_flag == 1)
1794                         break;
1795
1796                 if (all_ports_up == 0) {
1797                         printf(".");
1798                         fflush(stdout);
1799                         rte_delay_ms(CHECK_INTERVAL);
1800                 }
1801
1802                 /* set the print_flag if all ports up or timeout */
1803                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1804                         print_flag = 1;
1805                         printf("done\n");
1806                 }
1807         }
1808 }
1809
1810 /* Check if device has to be HW/SW or any */
1811 static int
1812 check_type(const struct l2fwd_crypto_options *options,
1813                 const struct rte_cryptodev_info *dev_info)
1814 {
1815         if (options->type == CDEV_TYPE_HW &&
1816                         (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1817                 return 0;
1818         if (options->type == CDEV_TYPE_SW &&
1819                         !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1820                 return 0;
1821         if (options->type == CDEV_TYPE_ANY)
1822                 return 0;
1823
1824         return -1;
1825 }
1826
1827 static const struct rte_cryptodev_capabilities *
1828 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1829                 const struct rte_cryptodev_info *dev_info,
1830                 uint8_t cdev_id)
1831 {
1832         unsigned int i = 0;
1833         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1834         enum rte_crypto_cipher_algorithm cap_cipher_algo;
1835         enum rte_crypto_cipher_algorithm opt_cipher_algo =
1836                                         options->cipher_xform.cipher.algo;
1837
1838         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1839                 cap_cipher_algo = cap->sym.cipher.algo;
1840                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1841                         if (cap_cipher_algo == opt_cipher_algo) {
1842                                 if (check_type(options, dev_info) == 0)
1843                                         break;
1844                         }
1845                 }
1846                 cap = &dev_info->capabilities[++i];
1847         }
1848
1849         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1850                 printf("Algorithm %s not supported by cryptodev %u"
1851                         " or device not of preferred type (%s)\n",
1852                         rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1853                         cdev_id,
1854                         options->string_type);
1855                 return NULL;
1856         }
1857
1858         return cap;
1859 }
1860
1861 static const struct rte_cryptodev_capabilities *
1862 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1863                 const struct rte_cryptodev_info *dev_info,
1864                 uint8_t cdev_id)
1865 {
1866         unsigned int i = 0;
1867         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1868         enum rte_crypto_auth_algorithm cap_auth_algo;
1869         enum rte_crypto_auth_algorithm opt_auth_algo =
1870                                         options->auth_xform.auth.algo;
1871
1872         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1873                 cap_auth_algo = cap->sym.auth.algo;
1874                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1875                         if (cap_auth_algo == opt_auth_algo) {
1876                                 if (check_type(options, dev_info) == 0)
1877                                         break;
1878                         }
1879                 }
1880                 cap = &dev_info->capabilities[++i];
1881         }
1882
1883         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1884                 printf("Algorithm %s not supported by cryptodev %u"
1885                         " or device not of preferred type (%s)\n",
1886                         rte_crypto_auth_algorithm_strings[opt_auth_algo],
1887                         cdev_id,
1888                         options->string_type);
1889                 return NULL;
1890         }
1891
1892         return cap;
1893 }
1894
1895 static const struct rte_cryptodev_capabilities *
1896 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1897                 const struct rte_cryptodev_info *dev_info,
1898                 uint8_t cdev_id)
1899 {
1900         unsigned int i = 0;
1901         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1902         enum rte_crypto_aead_algorithm cap_aead_algo;
1903         enum rte_crypto_aead_algorithm opt_aead_algo =
1904                                         options->aead_xform.aead.algo;
1905
1906         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1907                 cap_aead_algo = cap->sym.aead.algo;
1908                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1909                         if (cap_aead_algo == opt_aead_algo) {
1910                                 if (check_type(options, dev_info) == 0)
1911                                         break;
1912                         }
1913                 }
1914                 cap = &dev_info->capabilities[++i];
1915         }
1916
1917         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1918                 printf("Algorithm %s not supported by cryptodev %u"
1919                         " or device not of preferred type (%s)\n",
1920                         rte_crypto_aead_algorithm_strings[opt_aead_algo],
1921                         cdev_id,
1922                         options->string_type);
1923                 return NULL;
1924         }
1925
1926         return cap;
1927 }
1928
1929 /* Check if the device is enabled by cryptodev_mask */
1930 static int
1931 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1932                 uint8_t cdev_id)
1933 {
1934         if (options->cryptodev_mask & (1 << cdev_id))
1935                 return 0;
1936
1937         return -1;
1938 }
1939
1940 static inline int
1941 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1942                 uint16_t increment)
1943 {
1944         uint16_t supp_size;
1945
1946         /* Single value */
1947         if (increment == 0) {
1948                 if (length == min)
1949                         return 0;
1950                 else
1951                         return -1;
1952         }
1953
1954         /* Range of values */
1955         for (supp_size = min; supp_size <= max; supp_size += increment) {
1956                 if (length == supp_size)
1957                         return 0;
1958         }
1959
1960         return -1;
1961 }
1962
1963 static int
1964 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1965                 unsigned int iv_param, int iv_random_size,
1966                 uint16_t *iv_length)
1967 {
1968         /*
1969          * Check if length of provided IV is supported
1970          * by the algorithm chosen.
1971          */
1972         if (iv_param) {
1973                 if (check_supported_size(*iv_length,
1974                                 iv_range_size->min,
1975                                 iv_range_size->max,
1976                                 iv_range_size->increment)
1977                                         != 0) {
1978                         printf("Unsupported IV length\n");
1979                         return -1;
1980                 }
1981         /*
1982          * Check if length of IV to be randomly generated
1983          * is supported by the algorithm chosen.
1984          */
1985         } else if (iv_random_size != -1) {
1986                 if (check_supported_size(iv_random_size,
1987                                 iv_range_size->min,
1988                                 iv_range_size->max,
1989                                 iv_range_size->increment)
1990                                         != 0) {
1991                         printf("Unsupported IV length\n");
1992                         return -1;
1993                 }
1994                 *iv_length = iv_random_size;
1995         /* No size provided, use minimum size. */
1996         } else
1997                 *iv_length = iv_range_size->min;
1998
1999         return 0;
2000 }
2001
2002 static int
2003 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
2004                 uint8_t *enabled_cdevs)
2005 {
2006         unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
2007         const struct rte_cryptodev_capabilities *cap;
2008         unsigned int sess_sz, max_sess_sz = 0;
2009         int retval;
2010
2011         cdev_count = rte_cryptodev_count();
2012         if (cdev_count == 0) {
2013                 printf("No crypto devices available\n");
2014                 return -1;
2015         }
2016
2017         for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
2018                 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
2019                 if (sess_sz > max_sess_sz)
2020                         max_sess_sz = sess_sz;
2021         }
2022
2023         for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
2024                         cdev_id++) {
2025                 struct rte_cryptodev_qp_conf qp_conf;
2026                 struct rte_cryptodev_info dev_info;
2027                 retval = rte_cryptodev_socket_id(cdev_id);
2028
2029                 if (retval < 0) {
2030                         printf("Invalid crypto device id used\n");
2031                         return -1;
2032                 }
2033
2034                 uint8_t socket_id = (uint8_t) retval;
2035
2036                 struct rte_cryptodev_config conf = {
2037                         .nb_queue_pairs = 1,
2038                         .socket_id = socket_id,
2039                 };
2040
2041                 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
2042                         continue;
2043
2044                 rte_cryptodev_info_get(cdev_id, &dev_info);
2045
2046                 if (session_pool_socket[socket_id] == NULL) {
2047                         char mp_name[RTE_MEMPOOL_NAMESIZE];
2048                         struct rte_mempool *sess_mp;
2049
2050                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2051                                 "sess_mp_%u", socket_id);
2052
2053                         /*
2054                          * Create enough objects for session headers and
2055                          * device private data
2056                          */
2057                         sess_mp = rte_mempool_create(mp_name,
2058                                                 MAX_SESSIONS * 2,
2059                                                 max_sess_sz,
2060                                                 SESSION_POOL_CACHE_SIZE,
2061                                                 0, NULL, NULL, NULL,
2062                                                 NULL, socket_id,
2063                                                 0);
2064
2065                         if (sess_mp == NULL) {
2066                                 printf("Cannot create session pool on socket %d\n",
2067                                         socket_id);
2068                                 return -ENOMEM;
2069                         }
2070
2071                         printf("Allocated session pool on socket %d\n", socket_id);
2072                         session_pool_socket[socket_id] = sess_mp;
2073                 }
2074
2075                 /* Set AEAD parameters */
2076                 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2077                         /* Check if device supports AEAD algo */
2078                         cap = check_device_support_aead_algo(options, &dev_info,
2079                                                         cdev_id);
2080                         if (cap == NULL)
2081                                 continue;
2082
2083                         options->block_size = cap->sym.aead.block_size;
2084
2085                         check_iv_param(&cap->sym.aead.iv_size,
2086                                         options->aead_iv_param,
2087                                         options->aead_iv_random_size,
2088                                         &options->aead_iv.length);
2089
2090                         /*
2091                          * Check if length of provided AEAD key is supported
2092                          * by the algorithm chosen.
2093                          */
2094                         if (options->aead_key_param) {
2095                                 if (check_supported_size(
2096                                                 options->aead_xform.aead.key.length,
2097                                                 cap->sym.aead.key_size.min,
2098                                                 cap->sym.aead.key_size.max,
2099                                                 cap->sym.aead.key_size.increment)
2100                                                         != 0) {
2101                                         printf("Unsupported aead key length\n");
2102                                         return -1;
2103                                 }
2104                         /*
2105                          * Check if length of the aead key to be randomly generated
2106                          * is supported by the algorithm chosen.
2107                          */
2108                         } else if (options->aead_key_random_size != -1) {
2109                                 if (check_supported_size(options->aead_key_random_size,
2110                                                 cap->sym.aead.key_size.min,
2111                                                 cap->sym.aead.key_size.max,
2112                                                 cap->sym.aead.key_size.increment)
2113                                                         != 0) {
2114                                         printf("Unsupported aead key length\n");
2115                                         return -1;
2116                                 }
2117                                 options->aead_xform.aead.key.length =
2118                                                         options->aead_key_random_size;
2119                         /* No size provided, use minimum size. */
2120                         } else
2121                                 options->aead_xform.aead.key.length =
2122                                                 cap->sym.aead.key_size.min;
2123
2124                         if (!options->aead_key_param)
2125                                 generate_random_key(
2126                                         options->aead_xform.aead.key.data,
2127                                         options->aead_xform.aead.key.length);
2128
2129                         /*
2130                          * Check if length of provided AAD is supported
2131                          * by the algorithm chosen.
2132                          */
2133                         if (options->aad_param) {
2134                                 if (check_supported_size(options->aad.length,
2135                                                 cap->sym.aead.aad_size.min,
2136                                                 cap->sym.aead.aad_size.max,
2137                                                 cap->sym.aead.aad_size.increment)
2138                                                         != 0) {
2139                                         printf("Unsupported AAD length\n");
2140                                         return -1;
2141                                 }
2142                         /*
2143                          * Check if length of AAD to be randomly generated
2144                          * is supported by the algorithm chosen.
2145                          */
2146                         } else if (options->aad_random_size != -1) {
2147                                 if (check_supported_size(options->aad_random_size,
2148                                                 cap->sym.aead.aad_size.min,
2149                                                 cap->sym.aead.aad_size.max,
2150                                                 cap->sym.aead.aad_size.increment)
2151                                                         != 0) {
2152                                         printf("Unsupported AAD length\n");
2153                                         return -1;
2154                                 }
2155                                 options->aad.length = options->aad_random_size;
2156                         /* No size provided, use minimum size. */
2157                         } else
2158                                 options->aad.length = cap->sym.auth.aad_size.min;
2159
2160                         options->aead_xform.aead.aad_length =
2161                                                 options->aad.length;
2162
2163                         /* Check if digest size is supported by the algorithm. */
2164                         if (options->digest_size != -1) {
2165                                 if (check_supported_size(options->digest_size,
2166                                                 cap->sym.aead.digest_size.min,
2167                                                 cap->sym.aead.digest_size.max,
2168                                                 cap->sym.aead.digest_size.increment)
2169                                                         != 0) {
2170                                         printf("Unsupported digest length\n");
2171                                         return -1;
2172                                 }
2173                                 options->aead_xform.aead.digest_length =
2174                                                         options->digest_size;
2175                         /* No size provided, use minimum size. */
2176                         } else
2177                                 options->aead_xform.aead.digest_length =
2178                                                 cap->sym.aead.digest_size.min;
2179                 }
2180
2181                 /* Set cipher parameters */
2182                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2183                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2184                                 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2185                         /* Check if device supports cipher algo */
2186                         cap = check_device_support_cipher_algo(options, &dev_info,
2187                                                         cdev_id);
2188                         if (cap == NULL)
2189                                 continue;
2190
2191                         options->block_size = cap->sym.cipher.block_size;
2192
2193                         check_iv_param(&cap->sym.cipher.iv_size,
2194                                         options->cipher_iv_param,
2195                                         options->cipher_iv_random_size,
2196                                         &options->cipher_iv.length);
2197
2198                         /*
2199                          * Check if length of provided cipher key is supported
2200                          * by the algorithm chosen.
2201                          */
2202                         if (options->ckey_param) {
2203                                 if (check_supported_size(
2204                                                 options->cipher_xform.cipher.key.length,
2205                                                 cap->sym.cipher.key_size.min,
2206                                                 cap->sym.cipher.key_size.max,
2207                                                 cap->sym.cipher.key_size.increment)
2208                                                         != 0) {
2209                                         printf("Unsupported cipher key length\n");
2210                                         return -1;
2211                                 }
2212                         /*
2213                          * Check if length of the cipher key to be randomly generated
2214                          * is supported by the algorithm chosen.
2215                          */
2216                         } else if (options->ckey_random_size != -1) {
2217                                 if (check_supported_size(options->ckey_random_size,
2218                                                 cap->sym.cipher.key_size.min,
2219                                                 cap->sym.cipher.key_size.max,
2220                                                 cap->sym.cipher.key_size.increment)
2221                                                         != 0) {
2222                                         printf("Unsupported cipher key length\n");
2223                                         return -1;
2224                                 }
2225                                 options->cipher_xform.cipher.key.length =
2226                                                         options->ckey_random_size;
2227                         /* No size provided, use minimum size. */
2228                         } else
2229                                 options->cipher_xform.cipher.key.length =
2230                                                 cap->sym.cipher.key_size.min;
2231
2232                         if (!options->ckey_param)
2233                                 generate_random_key(
2234                                         options->cipher_xform.cipher.key.data,
2235                                         options->cipher_xform.cipher.key.length);
2236
2237                 }
2238
2239                 /* Set auth parameters */
2240                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2241                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2242                                 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2243                         /* Check if device supports auth algo */
2244                         cap = check_device_support_auth_algo(options, &dev_info,
2245                                                         cdev_id);
2246                         if (cap == NULL)
2247                                 continue;
2248
2249                         check_iv_param(&cap->sym.auth.iv_size,
2250                                         options->auth_iv_param,
2251                                         options->auth_iv_random_size,
2252                                         &options->auth_iv.length);
2253                         /*
2254                          * Check if length of provided auth key is supported
2255                          * by the algorithm chosen.
2256                          */
2257                         if (options->akey_param) {
2258                                 if (check_supported_size(
2259                                                 options->auth_xform.auth.key.length,
2260                                                 cap->sym.auth.key_size.min,
2261                                                 cap->sym.auth.key_size.max,
2262                                                 cap->sym.auth.key_size.increment)
2263                                                         != 0) {
2264                                         printf("Unsupported auth key length\n");
2265                                         return -1;
2266                                 }
2267                         /*
2268                          * Check if length of the auth key to be randomly generated
2269                          * is supported by the algorithm chosen.
2270                          */
2271                         } else if (options->akey_random_size != -1) {
2272                                 if (check_supported_size(options->akey_random_size,
2273                                                 cap->sym.auth.key_size.min,
2274                                                 cap->sym.auth.key_size.max,
2275                                                 cap->sym.auth.key_size.increment)
2276                                                         != 0) {
2277                                         printf("Unsupported auth key length\n");
2278                                         return -1;
2279                                 }
2280                                 options->auth_xform.auth.key.length =
2281                                                         options->akey_random_size;
2282                         /* No size provided, use minimum size. */
2283                         } else
2284                                 options->auth_xform.auth.key.length =
2285                                                 cap->sym.auth.key_size.min;
2286
2287                         if (!options->akey_param)
2288                                 generate_random_key(
2289                                         options->auth_xform.auth.key.data,
2290                                         options->auth_xform.auth.key.length);
2291
2292                         /* Check if digest size is supported by the algorithm. */
2293                         if (options->digest_size != -1) {
2294                                 if (check_supported_size(options->digest_size,
2295                                                 cap->sym.auth.digest_size.min,
2296                                                 cap->sym.auth.digest_size.max,
2297                                                 cap->sym.auth.digest_size.increment)
2298                                                         != 0) {
2299                                         printf("Unsupported digest length\n");
2300                                         return -1;
2301                                 }
2302                                 options->auth_xform.auth.digest_length =
2303                                                         options->digest_size;
2304                         /* No size provided, use minimum size. */
2305                         } else
2306                                 options->auth_xform.auth.digest_length =
2307                                                 cap->sym.auth.digest_size.min;
2308                 }
2309
2310                 retval = rte_cryptodev_configure(cdev_id, &conf);
2311                 if (retval < 0) {
2312                         printf("Failed to configure cryptodev %u", cdev_id);
2313                         return -1;
2314                 }
2315
2316                 qp_conf.nb_descriptors = 2048;
2317
2318                 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2319                                 socket_id, session_pool_socket[socket_id]);
2320                 if (retval < 0) {
2321                         printf("Failed to setup queue pair %u on cryptodev %u",
2322                                         0, cdev_id);
2323                         return -1;
2324                 }
2325
2326                 retval = rte_cryptodev_start(cdev_id);
2327                 if (retval < 0) {
2328                         printf("Failed to start device %u: error %d\n",
2329                                         cdev_id, retval);
2330                         return -1;
2331                 }
2332
2333                 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2334
2335                 enabled_cdevs[cdev_id] = 1;
2336                 enabled_cdev_count++;
2337         }
2338
2339         return enabled_cdev_count;
2340 }
2341
2342 static int
2343 initialize_ports(struct l2fwd_crypto_options *options)
2344 {
2345         uint8_t last_portid, portid;
2346         unsigned enabled_portcount = 0;
2347         unsigned nb_ports = rte_eth_dev_count();
2348
2349         if (nb_ports == 0) {
2350                 printf("No Ethernet ports - bye\n");
2351                 return -1;
2352         }
2353
2354         /* Reset l2fwd_dst_ports */
2355         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2356                 l2fwd_dst_ports[portid] = 0;
2357
2358         for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2359                 int retval;
2360
2361                 /* Skip ports that are not enabled */
2362                 if ((options->portmask & (1 << portid)) == 0)
2363                         continue;
2364
2365                 /* init port */
2366                 printf("Initializing port %u... ", (unsigned) portid);
2367                 fflush(stdout);
2368                 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2369                 if (retval < 0) {
2370                         printf("Cannot configure device: err=%d, port=%u\n",
2371                                   retval, (unsigned) portid);
2372                         return -1;
2373                 }
2374
2375                 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2376                                                           &nb_txd);
2377                 if (retval < 0) {
2378                         printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2379                                 retval, (unsigned) portid);
2380                         return -1;
2381                 }
2382
2383                 /* init one RX queue */
2384                 fflush(stdout);
2385                 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2386                                              rte_eth_dev_socket_id(portid),
2387                                              NULL, l2fwd_pktmbuf_pool);
2388                 if (retval < 0) {
2389                         printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2390                                         retval, (unsigned) portid);
2391                         return -1;
2392                 }
2393
2394                 /* init one TX queue on each port */
2395                 fflush(stdout);
2396                 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2397                                 rte_eth_dev_socket_id(portid),
2398                                 NULL);
2399                 if (retval < 0) {
2400                         printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2401                                 retval, (unsigned) portid);
2402
2403                         return -1;
2404                 }
2405
2406                 /* Start device */
2407                 retval = rte_eth_dev_start(portid);
2408                 if (retval < 0) {
2409                         printf("rte_eth_dev_start:err=%d, port=%u\n",
2410                                         retval, (unsigned) portid);
2411                         return -1;
2412                 }
2413
2414                 rte_eth_promiscuous_enable(portid);
2415
2416                 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2417
2418                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2419                                 (unsigned) portid,
2420                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2421                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2422                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2423                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2424                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2425                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2426
2427                 /* initialize port stats */
2428                 memset(&port_statistics, 0, sizeof(port_statistics));
2429
2430                 /* Setup port forwarding table */
2431                 if (enabled_portcount % 2) {
2432                         l2fwd_dst_ports[portid] = last_portid;
2433                         l2fwd_dst_ports[last_portid] = portid;
2434                 } else {
2435                         last_portid = portid;
2436                 }
2437
2438                 l2fwd_enabled_port_mask |= (1 << portid);
2439                 enabled_portcount++;
2440         }
2441
2442         if (enabled_portcount == 1) {
2443                 l2fwd_dst_ports[last_portid] = last_portid;
2444         } else if (enabled_portcount % 2) {
2445                 printf("odd number of ports in portmask- bye\n");
2446                 return -1;
2447         }
2448
2449         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2450
2451         return enabled_portcount;
2452 }
2453
2454 static void
2455 reserve_key_memory(struct l2fwd_crypto_options *options)
2456 {
2457         options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2458                                                 MAX_KEY_SIZE, 0);
2459         if (options->cipher_xform.cipher.key.data == NULL)
2460                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2461
2462         options->auth_xform.auth.key.data = rte_malloc("auth key",
2463                                                 MAX_KEY_SIZE, 0);
2464         if (options->auth_xform.auth.key.data == NULL)
2465                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2466
2467         options->aead_xform.aead.key.data = rte_malloc("aead key",
2468                                                 MAX_KEY_SIZE, 0);
2469         if (options->aead_xform.aead.key.data == NULL)
2470                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2471
2472         options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2473         if (options->cipher_iv.data == NULL)
2474                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2475
2476         options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2477         if (options->auth_iv.data == NULL)
2478                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2479
2480         options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2481         if (options->aead_iv.data == NULL)
2482                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2483
2484         options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2485         if (options->aad.data == NULL)
2486                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2487         options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
2488 }
2489
2490 int
2491 main(int argc, char **argv)
2492 {
2493         struct lcore_queue_conf *qconf;
2494         struct l2fwd_crypto_options options;
2495
2496         uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
2497         unsigned lcore_id, rx_lcore_id;
2498         int ret, enabled_cdevcount, enabled_portcount;
2499         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2500
2501         /* init EAL */
2502         ret = rte_eal_init(argc, argv);
2503         if (ret < 0)
2504                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2505         argc -= ret;
2506         argv += ret;
2507
2508         /* reserve memory for Cipher/Auth key and IV */
2509         reserve_key_memory(&options);
2510
2511         /* parse application arguments (after the EAL ones) */
2512         ret = l2fwd_crypto_parse_args(&options, argc, argv);
2513         if (ret < 0)
2514                 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2515
2516         printf("MAC updating %s\n",
2517                         options.mac_updating ? "enabled" : "disabled");
2518
2519         /* create the mbuf pool */
2520         l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2521                         sizeof(struct rte_crypto_op),
2522                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2523         if (l2fwd_pktmbuf_pool == NULL)
2524                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2525
2526         /* create crypto op pool */
2527         l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2528                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2529                         rte_socket_id());
2530         if (l2fwd_crypto_op_pool == NULL)
2531                 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2532
2533         /* Enable Ethernet ports */
2534         enabled_portcount = initialize_ports(&options);
2535         if (enabled_portcount < 1)
2536                 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2537
2538         nb_ports = rte_eth_dev_count();
2539         /* Initialize the port/queue configuration of each logical core */
2540         for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2541                         portid < nb_ports; portid++) {
2542
2543                 /* skip ports that are not enabled */
2544                 if ((options.portmask & (1 << portid)) == 0)
2545                         continue;
2546
2547                 if (options.single_lcore && qconf == NULL) {
2548                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2549                                 rx_lcore_id++;
2550                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2551                                         rte_exit(EXIT_FAILURE,
2552                                                         "Not enough cores\n");
2553                         }
2554                 } else if (!options.single_lcore) {
2555                         /* get the lcore_id for this port */
2556                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2557                                lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2558                                options.nb_ports_per_lcore) {
2559                                 rx_lcore_id++;
2560                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2561                                         rte_exit(EXIT_FAILURE,
2562                                                         "Not enough cores\n");
2563                         }
2564                 }
2565
2566                 /* Assigned a new logical core in the loop above. */
2567                 if (qconf != &lcore_queue_conf[rx_lcore_id])
2568                         qconf = &lcore_queue_conf[rx_lcore_id];
2569
2570                 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2571                 qconf->nb_rx_ports++;
2572
2573                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
2574         }
2575
2576         /* Enable Crypto devices */
2577         enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2578                         enabled_cdevs);
2579         if (enabled_cdevcount < 0)
2580                 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2581
2582         if (enabled_cdevcount < enabled_portcount)
2583                 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2584                                 "has to be more or equal to number of ports (%d)\n",
2585                                 enabled_cdevcount, enabled_portcount);
2586
2587         nb_cryptodevs = rte_cryptodev_count();
2588
2589         /* Initialize the port/cryptodev configuration of each logical core */
2590         for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2591                         cdev_id < nb_cryptodevs && enabled_cdevcount;
2592                         cdev_id++) {
2593                 /* Crypto op not supported by crypto device */
2594                 if (!enabled_cdevs[cdev_id])
2595                         continue;
2596
2597                 if (options.single_lcore && qconf == NULL) {
2598                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2599                                 rx_lcore_id++;
2600                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2601                                         rte_exit(EXIT_FAILURE,
2602                                                         "Not enough cores\n");
2603                         }
2604                 } else if (!options.single_lcore) {
2605                         /* get the lcore_id for this port */
2606                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2607                                lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2608                                options.nb_ports_per_lcore) {
2609                                 rx_lcore_id++;
2610                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2611                                         rte_exit(EXIT_FAILURE,
2612                                                         "Not enough cores\n");
2613                         }
2614                 }
2615
2616                 /* Assigned a new logical core in the loop above. */
2617                 if (qconf != &lcore_queue_conf[rx_lcore_id])
2618                         qconf = &lcore_queue_conf[rx_lcore_id];
2619
2620                 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2621                 qconf->nb_crypto_devs++;
2622
2623                 enabled_cdevcount--;
2624
2625                 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2626                                 (unsigned)cdev_id);
2627         }
2628
2629         /* launch per-lcore init on every lcore */
2630         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2631                         CALL_MASTER);
2632         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2633                 if (rte_eal_wait_lcore(lcore_id) < 0)
2634                         return -1;
2635         }
2636
2637         return 0;
2638 }