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