mbuf_offload: remove library
[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
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_common.h>
52 #include <rte_cryptodev.h>
53 #include <rte_cycles.h>
54 #include <rte_debug.h>
55 #include <rte_eal.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_interrupts.h>
59 #include <rte_ip.h>
60 #include <rte_launch.h>
61 #include <rte_lcore.h>
62 #include <rte_log.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_memcpy.h>
66 #include <rte_memory.h>
67 #include <rte_mempool.h>
68 #include <rte_memzone.h>
69 #include <rte_pci.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
73 #include <rte_ring.h>
74
75 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
76
77 #define NB_MBUF   8192
78
79 #define MAX_PKT_BURST 32
80 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
81
82 /*
83  * Configurable number of RX/TX ring descriptors
84  */
85 #define RTE_TEST_RX_DESC_DEFAULT 128
86 #define RTE_TEST_TX_DESC_DEFAULT 512
87 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
88 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
89
90 /* ethernet addresses of ports */
91 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
92
93 /* mask of enabled ports */
94 static uint64_t l2fwd_enabled_port_mask;
95 static uint64_t l2fwd_enabled_crypto_mask;
96
97 /* list of enabled ports */
98 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
99
100
101 struct pkt_buffer {
102         unsigned len;
103         struct rte_mbuf *buffer[MAX_PKT_BURST];
104 };
105
106 struct op_buffer {
107         unsigned len;
108         struct rte_crypto_op *buffer[MAX_PKT_BURST];
109 };
110
111 #define MAX_RX_QUEUE_PER_LCORE 16
112 #define MAX_TX_QUEUE_PER_PORT 16
113
114 enum l2fwd_crypto_xform_chain {
115         L2FWD_CRYPTO_CIPHER_HASH,
116         L2FWD_CRYPTO_HASH_CIPHER
117 };
118
119 struct l2fwd_key {
120         uint8_t *data;
121         uint32_t length;
122         phys_addr_t phys_addr;
123 };
124
125 /** l2fwd crypto application command line options */
126 struct l2fwd_crypto_options {
127         unsigned portmask;
128         unsigned nb_ports_per_lcore;
129         unsigned refresh_period;
130         unsigned single_lcore:1;
131
132         enum rte_cryptodev_type cdev_type;
133         unsigned sessionless:1;
134
135         enum l2fwd_crypto_xform_chain xform_chain;
136
137         struct rte_crypto_sym_xform cipher_xform;
138         uint8_t ckey_data[32];
139
140         struct l2fwd_key iv_key;
141         uint8_t ivkey_data[16];
142
143         struct rte_crypto_sym_xform auth_xform;
144         uint8_t akey_data[128];
145 };
146
147 /** l2fwd crypto lcore params */
148 struct l2fwd_crypto_params {
149         uint8_t dev_id;
150         uint8_t qp_id;
151
152         unsigned digest_length;
153         unsigned block_size;
154         struct l2fwd_key iv_key;
155         struct rte_cryptodev_sym_session *session;
156 };
157
158 /** lcore configuration */
159 struct lcore_queue_conf {
160         unsigned nb_rx_ports;
161         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
162
163         unsigned nb_crypto_devs;
164         unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
165
166         struct op_buffer op_buf[RTE_MAX_ETHPORTS];
167         struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
168 } __rte_cache_aligned;
169
170 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
171
172 static const struct rte_eth_conf port_conf = {
173         .rxmode = {
174                 .split_hdr_size = 0,
175                 .header_split   = 0, /**< Header Split disabled */
176                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
177                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
178                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
179                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
180         },
181         .txmode = {
182                 .mq_mode = ETH_MQ_TX_NONE,
183         },
184 };
185
186 struct rte_mempool *l2fwd_pktmbuf_pool;
187 struct rte_mempool *l2fwd_crypto_op_pool;
188
189 /* Per-port statistics struct */
190 struct l2fwd_port_statistics {
191         uint64_t tx;
192         uint64_t rx;
193
194         uint64_t crypto_enqueued;
195         uint64_t crypto_dequeued;
196
197         uint64_t dropped;
198 } __rte_cache_aligned;
199
200 struct l2fwd_crypto_statistics {
201         uint64_t enqueued;
202         uint64_t dequeued;
203
204         uint64_t errors;
205 } __rte_cache_aligned;
206
207 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
208 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
209
210 /* A tsc-based timer responsible for triggering statistics printout */
211 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
212 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
213
214 /* default period is 10 seconds */
215 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
216
217 /* Print out statistics on packets dropped */
218 static void
219 print_stats(void)
220 {
221         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
222         uint64_t total_packets_enqueued, total_packets_dequeued,
223                 total_packets_errors;
224         unsigned portid;
225         uint64_t cdevid;
226
227         total_packets_dropped = 0;
228         total_packets_tx = 0;
229         total_packets_rx = 0;
230         total_packets_enqueued = 0;
231         total_packets_dequeued = 0;
232         total_packets_errors = 0;
233
234         const char clr[] = { 27, '[', '2', 'J', '\0' };
235         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
236
237                 /* Clear screen and move to top left */
238         printf("%s%s", clr, topLeft);
239
240         printf("\nPort statistics ====================================");
241
242         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
243                 /* skip disabled ports */
244                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
245                         continue;
246                 printf("\nStatistics for port %u ------------------------------"
247                            "\nPackets sent: %32"PRIu64
248                            "\nPackets received: %28"PRIu64
249                            "\nPackets dropped: %29"PRIu64,
250                            portid,
251                            port_statistics[portid].tx,
252                            port_statistics[portid].rx,
253                            port_statistics[portid].dropped);
254
255                 total_packets_dropped += port_statistics[portid].dropped;
256                 total_packets_tx += port_statistics[portid].tx;
257                 total_packets_rx += port_statistics[portid].rx;
258         }
259         printf("\nCrypto statistics ==================================");
260
261         for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
262                 /* skip disabled ports */
263                 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
264                         continue;
265                 printf("\nStatistics for cryptodev %"PRIu64
266                                 " -------------------------"
267                            "\nPackets enqueued: %28"PRIu64
268                            "\nPackets dequeued: %28"PRIu64
269                            "\nPackets errors: %30"PRIu64,
270                            cdevid,
271                            crypto_statistics[cdevid].enqueued,
272                            crypto_statistics[cdevid].dequeued,
273                            crypto_statistics[cdevid].errors);
274
275                 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
276                 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
277                 total_packets_errors += crypto_statistics[cdevid].errors;
278         }
279         printf("\nAggregate statistics ==============================="
280                    "\nTotal packets received: %22"PRIu64
281                    "\nTotal packets enqueued: %22"PRIu64
282                    "\nTotal packets dequeued: %22"PRIu64
283                    "\nTotal packets sent: %26"PRIu64
284                    "\nTotal packets dropped: %23"PRIu64
285                    "\nTotal packets crypto errors: %17"PRIu64,
286                    total_packets_rx,
287                    total_packets_enqueued,
288                    total_packets_dequeued,
289                    total_packets_tx,
290                    total_packets_dropped,
291                    total_packets_errors);
292         printf("\n====================================================\n");
293 }
294
295
296
297 static int
298 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
299                 struct l2fwd_crypto_params *cparams)
300 {
301         struct rte_crypto_op **op_buffer;
302         unsigned ret;
303
304         op_buffer = (struct rte_crypto_op **)
305                         qconf->op_buf[cparams->dev_id].buffer;
306
307         ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
308                         cparams->qp_id, op_buffer, (uint16_t) n);
309
310         crypto_statistics[cparams->dev_id].enqueued += ret;
311         if (unlikely(ret < n)) {
312                 crypto_statistics[cparams->dev_id].errors += (n - ret);
313                 do {
314                         rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
315                         rte_crypto_op_free(op_buffer[ret]);
316                 } while (++ret < n);
317         }
318
319         return 0;
320 }
321
322 static int
323 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
324                 struct l2fwd_crypto_params *cparams)
325 {
326         unsigned lcore_id, len;
327         struct lcore_queue_conf *qconf;
328
329         lcore_id = rte_lcore_id();
330
331         qconf = &lcore_queue_conf[lcore_id];
332         len = qconf->op_buf[cparams->dev_id].len;
333         qconf->op_buf[cparams->dev_id].buffer[len] = op;
334         len++;
335
336         /* enough ops to be sent */
337         if (len == MAX_PKT_BURST) {
338                 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
339                 len = 0;
340         }
341
342         qconf->op_buf[cparams->dev_id].len = len;
343         return 0;
344 }
345
346 static int
347 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
348                 struct rte_crypto_op *op,
349                 struct l2fwd_crypto_params *cparams)
350 {
351         struct ether_hdr *eth_hdr;
352         struct ipv4_hdr *ip_hdr;
353
354         unsigned ipdata_offset, pad_len, data_len;
355         char *padding;
356
357         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
358
359         if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
360                 return -1;
361
362         ipdata_offset = sizeof(struct ether_hdr);
363
364         ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
365                         ipdata_offset);
366
367         ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
368                         * IPV4_IHL_MULTIPLIER;
369
370
371         /* Zero pad data to be crypto'd so it is block aligned */
372         data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
373         pad_len = data_len % cparams->block_size ? cparams->block_size -
374                         (data_len % cparams->block_size) : 0;
375
376         if (pad_len) {
377                 padding = rte_pktmbuf_append(m, pad_len);
378                 if (unlikely(!padding))
379                         return -1;
380
381                 data_len += pad_len;
382                 memset(padding, 0, pad_len);
383         }
384
385         /* Set crypto operation data parameters */
386         rte_crypto_op_attach_sym_session(op, cparams->session);
387
388         /* Append space for digest to end of packet */
389         op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
390                         cparams->digest_length);
391         op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
392                         rte_pktmbuf_pkt_len(m) - cparams->digest_length);
393         op->sym->auth.digest.length = cparams->digest_length;
394
395         op->sym->auth.data.offset = ipdata_offset;
396         op->sym->auth.data.length = data_len;
397
398
399         op->sym->cipher.iv.data = cparams->iv_key.data;
400         op->sym->cipher.iv.phys_addr = cparams->iv_key.phys_addr;
401         op->sym->cipher.iv.length = cparams->iv_key.length;
402
403         op->sym->cipher.data.offset = ipdata_offset;
404         op->sym->cipher.data.length = data_len;
405
406         op->sym->m_src = m;
407
408         return l2fwd_crypto_enqueue(op, cparams);
409 }
410
411
412 /* Send the burst of packets on an output interface */
413 static int
414 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
415                 uint8_t port)
416 {
417         struct rte_mbuf **pkt_buffer;
418         unsigned ret;
419
420         pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
421
422         ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
423         port_statistics[port].tx += ret;
424         if (unlikely(ret < n)) {
425                 port_statistics[port].dropped += (n - ret);
426                 do {
427                         rte_pktmbuf_free(pkt_buffer[ret]);
428                 } while (++ret < n);
429         }
430
431         return 0;
432 }
433
434 /* Enqueue packets for TX and prepare them to be sent */
435 static int
436 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
437 {
438         unsigned lcore_id, len;
439         struct lcore_queue_conf *qconf;
440
441         lcore_id = rte_lcore_id();
442
443         qconf = &lcore_queue_conf[lcore_id];
444         len = qconf->pkt_buf[port].len;
445         qconf->pkt_buf[port].buffer[len] = m;
446         len++;
447
448         /* enough pkts to be sent */
449         if (unlikely(len == MAX_PKT_BURST)) {
450                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
451                 len = 0;
452         }
453
454         qconf->pkt_buf[port].len = len;
455         return 0;
456 }
457
458 static void
459 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
460 {
461         struct ether_hdr *eth;
462         void *tmp;
463         unsigned dst_port;
464
465         dst_port = l2fwd_dst_ports[portid];
466         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
467
468         /* 02:00:00:00:00:xx */
469         tmp = &eth->d_addr.addr_bytes[0];
470         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
471
472         /* src addr */
473         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
474
475         l2fwd_send_packet(m, (uint8_t) dst_port);
476 }
477
478 /** Generate random key */
479 static void
480 generate_random_key(uint8_t *key, unsigned length)
481 {
482         unsigned i;
483
484         for (i = 0; i < length; i++)
485                 key[i] = rand() % 0xff;
486 }
487
488 static struct rte_cryptodev_sym_session *
489 initialize_crypto_session(struct l2fwd_crypto_options *options,
490                 uint8_t cdev_id)
491 {
492         struct rte_crypto_sym_xform *first_xform;
493
494         if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
495                 first_xform = &options->cipher_xform;
496                 first_xform->next = &options->auth_xform;
497         } else {
498                 first_xform = &options->auth_xform;
499                 first_xform->next = &options->cipher_xform;
500         }
501
502         /* Setup Cipher Parameters */
503         return rte_cryptodev_sym_session_create(cdev_id, first_xform);
504 }
505
506 static void
507 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
508
509 /* main processing loop */
510 static void
511 l2fwd_main_loop(struct l2fwd_crypto_options *options)
512 {
513         struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
514         struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
515
516         unsigned lcore_id = rte_lcore_id();
517         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
518         unsigned i, j, portid, nb_rx;
519         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
520         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
521                         US_PER_S * BURST_TX_DRAIN_US;
522         struct l2fwd_crypto_params *cparams;
523         struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
524
525         if (qconf->nb_rx_ports == 0) {
526                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
527                 return;
528         }
529
530         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
531
532         l2fwd_crypto_options_print(options);
533
534         for (i = 0; i < qconf->nb_rx_ports; i++) {
535
536                 portid = qconf->rx_port_list[i];
537                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
538                         portid);
539         }
540
541         for (i = 0; i < qconf->nb_crypto_devs; i++) {
542                 port_cparams[i].dev_id = qconf->cryptodev_list[i];
543                 port_cparams[i].qp_id = 0;
544
545                 port_cparams[i].block_size = 64;
546                 port_cparams[i].digest_length = 20;
547
548                 port_cparams[i].iv_key.data =
549                                 (uint8_t *)rte_malloc(NULL, 16, 8);
550                 port_cparams[i].iv_key.length = 16;
551                 port_cparams[i].iv_key.phys_addr = rte_malloc_virt2phy(
552                                 (void *)port_cparams[i].iv_key.data);
553                 generate_random_key(port_cparams[i].iv_key.data,
554                                 sizeof(cparams[i].iv_key.length));
555
556                 port_cparams[i].session = initialize_crypto_session(options,
557                                 port_cparams[i].dev_id);
558
559                 if (port_cparams[i].session == NULL)
560                         return;
561                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
562                                 port_cparams[i].dev_id);
563         }
564
565         while (1) {
566
567                 cur_tsc = rte_rdtsc();
568
569                 /*
570                  * TX burst queue drain
571                  */
572                 diff_tsc = cur_tsc - prev_tsc;
573                 if (unlikely(diff_tsc > drain_tsc)) {
574
575                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
576                                 if (qconf->pkt_buf[portid].len == 0)
577                                         continue;
578                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
579                                                  qconf->pkt_buf[portid].len,
580                                                  (uint8_t) portid);
581                                 qconf->pkt_buf[portid].len = 0;
582                         }
583
584                         /* if timer is enabled */
585                         if (timer_period > 0) {
586
587                                 /* advance the timer */
588                                 timer_tsc += diff_tsc;
589
590                                 /* if timer has reached its timeout */
591                                 if (unlikely(timer_tsc >=
592                                                 (uint64_t)timer_period)) {
593
594                                         /* do this only on master core */
595                                         if (lcore_id == rte_get_master_lcore()
596                                                 && options->refresh_period) {
597                                                 print_stats();
598                                                 timer_tsc = 0;
599                                         }
600                                 }
601                         }
602
603                         prev_tsc = cur_tsc;
604                 }
605
606                 /*
607                  * Read packet from RX queues
608                  */
609                 for (i = 0; i < qconf->nb_rx_ports; i++) {
610                         portid = qconf->rx_port_list[i];
611
612                         cparams = &port_cparams[i];
613
614                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
615                                                  pkts_burst, MAX_PKT_BURST);
616
617                         port_statistics[portid].rx += nb_rx;
618
619                         if (nb_rx) {
620                                 /*
621                                  * If we can't allocate a crypto_ops, then drop
622                                  * the rest of the burst and dequeue and
623                                  * process the packets to free offload structs
624                                  */
625                                 if (rte_crypto_op_bulk_alloc(
626                                                 l2fwd_crypto_op_pool,
627                                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
628                                                 ops_burst, nb_rx) !=
629                                                                 nb_rx) {
630                                         for (j = 0; j < nb_rx; j++)
631                                                 rte_pktmbuf_free(pkts_burst[i]);
632
633                                         nb_rx = 0;
634                                 }
635
636                                 /* Enqueue packets from Crypto device*/
637                                 for (j = 0; j < nb_rx; j++) {
638                                         m = pkts_burst[j];
639
640                                         l2fwd_simple_crypto_enqueue(m,
641                                                         ops_burst[j], cparams);
642                                 }
643                         }
644
645                         /* Dequeue packets from Crypto device */
646                         do {
647                                 nb_rx = rte_cryptodev_dequeue_burst(
648                                                 cparams->dev_id, cparams->qp_id,
649                                                 ops_burst, MAX_PKT_BURST);
650
651                                 crypto_statistics[cparams->dev_id].dequeued +=
652                                                 nb_rx;
653
654                                 /* Forward crypto'd packets */
655                                 for (j = 0; j < nb_rx; j++) {
656                                         m = ops_burst[j]->sym->m_src;
657
658                                         rte_crypto_op_free(ops_burst[j]);
659                                         l2fwd_simple_forward(m, portid);
660                                 }
661                         } while (nb_rx == MAX_PKT_BURST);
662                 }
663         }
664 }
665
666 static int
667 l2fwd_launch_one_lcore(void *arg)
668 {
669         l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
670         return 0;
671 }
672
673 /* Display command line arguments usage */
674 static void
675 l2fwd_crypto_usage(const char *prgname)
676 {
677         printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
678                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
679                 "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
680                 "  -s manage all ports from single lcore"
681                 "  -t PERIOD: statistics will be refreshed each PERIOD seconds"
682                 " (0 to disable, 10 default, 86400 maximum)\n"
683
684                 "  --cdev AESNI_MB / QAT\n"
685                 "  --chain HASH_CIPHER / CIPHER_HASH\n"
686
687                 "  --cipher_algo ALGO\n"
688                 "  --cipher_op ENCRYPT / DECRYPT\n"
689                 "  --cipher_key KEY\n"
690                 "  --iv IV\n"
691
692                 "  --auth_algo ALGO\n"
693                 "  --auth_op GENERATE / VERIFY\n"
694                 "  --auth_key KEY\n"
695
696                 "  --sessionless\n",
697                prgname);
698 }
699
700 /** Parse crypto device type command line argument */
701 static int
702 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
703 {
704         if (strcmp("AESNI_MB", optarg) == 0) {
705                 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
706                 return 0;
707         } else if (strcmp("QAT", optarg) == 0) {
708                 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
709                 return 0;
710         }
711
712         return -1;
713 }
714
715 /** Parse crypto chain xform command line argument */
716 static int
717 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
718 {
719         if (strcmp("CIPHER_HASH", optarg) == 0) {
720                 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
721                 return 0;
722         } else if (strcmp("HASH_CIPHER", optarg) == 0) {
723                 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
724                 return 0;
725         }
726
727         return -1;
728 }
729
730 /** Parse crypto cipher algo option command line argument */
731 static int
732 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
733 {
734         if (strcmp("AES_CBC", optarg) == 0) {
735                 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
736                 return 0;
737         } else if (strcmp("AES_GCM", optarg) == 0) {
738                 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
739                 return 0;
740         }
741
742         printf("Cipher algorithm  not supported!\n");
743         return -1;
744 }
745
746 /** Parse crypto cipher operation command line argument */
747 static int
748 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
749 {
750         if (strcmp("ENCRYPT", optarg) == 0) {
751                 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
752                 return 0;
753         } else if (strcmp("DECRYPT", optarg) == 0) {
754                 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
755                 return 0;
756         }
757
758         printf("Cipher operation not supported!\n");
759         return -1;
760 }
761
762 /** Parse crypto key command line argument */
763 static int
764 parse_key(struct l2fwd_key *key __rte_unused,
765                 unsigned length __rte_unused, char *arg __rte_unused)
766 {
767         printf("Currently an unsupported argument!\n");
768         return -1;
769 }
770
771 /** Parse crypto cipher operation command line argument */
772 static int
773 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
774 {
775         if (strcmp("SHA1", optarg) == 0) {
776                 *algo = RTE_CRYPTO_AUTH_SHA1;
777                 return 0;
778         } else if (strcmp("SHA1_HMAC", optarg) == 0) {
779                 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
780                 return 0;
781         } else if (strcmp("SHA224", optarg) == 0) {
782                 *algo = RTE_CRYPTO_AUTH_SHA224;
783                 return 0;
784         } else if (strcmp("SHA224_HMAC", optarg) == 0) {
785                 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
786                 return 0;
787         } else if (strcmp("SHA256", optarg) == 0) {
788                 *algo = RTE_CRYPTO_AUTH_SHA256;
789                 return 0;
790         } else if (strcmp("SHA256_HMAC", optarg) == 0) {
791                 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
792                 return 0;
793         } else if (strcmp("SHA512", optarg) == 0) {
794                 *algo = RTE_CRYPTO_AUTH_SHA256;
795                 return 0;
796         } else if (strcmp("SHA512_HMAC", optarg) == 0) {
797                 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
798                 return 0;
799         }
800
801         printf("Authentication algorithm specified not supported!\n");
802         return -1;
803 }
804
805 static int
806 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
807 {
808         if (strcmp("VERIFY", optarg) == 0) {
809                 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
810                 return 0;
811         } else if (strcmp("GENERATE", optarg) == 0) {
812                 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
813                 return 0;
814         }
815
816         printf("Authentication operation specified not supported!\n");
817         return -1;
818 }
819
820 /** Parse long options */
821 static int
822 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
823                 struct option *lgopts, int option_index)
824 {
825         if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
826                 return parse_cryptodev_type(&options->cdev_type, optarg);
827
828         else if (strcmp(lgopts[option_index].name, "chain") == 0)
829                 return parse_crypto_opt_chain(options, optarg);
830
831         /* Cipher options */
832         else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
833                 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
834                                 optarg);
835
836         else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
837                 return parse_cipher_op(&options->cipher_xform.cipher.op,
838                                 optarg);
839
840         else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
841                 struct l2fwd_key key = { 0 };
842                 int retval = 0;
843
844                 retval = parse_key(&key, sizeof(options->ckey_data), optarg);
845
846                 options->cipher_xform.cipher.key.data = key.data;
847                 options->cipher_xform.cipher.key.length = key.length;
848
849                 return retval;
850
851         } else if (strcmp(lgopts[option_index].name, "iv") == 0)
852                 return parse_key(&options->iv_key, sizeof(options->ivkey_data),
853                                 optarg);
854
855         /* Authentication options */
856         else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
857                 return parse_auth_algo(&options->auth_xform.auth.algo,
858                                 optarg);
859
860         else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
861                 return parse_auth_op(&options->auth_xform.auth.op,
862                                 optarg);
863
864         else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
865                 struct l2fwd_key key = { 0 };
866                 int retval = 0;
867
868                 retval = parse_key(&key, sizeof(options->akey_data), optarg);
869
870                 options->auth_xform.auth.key.data = key.data;
871                 options->auth_xform.auth.key.length = key.length;
872
873                 return retval;
874
875         } else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
876                 options->sessionless = 1;
877                 return 0;
878         }
879
880         return -1;
881 }
882
883 /** Parse port mask */
884 static int
885 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
886                 const char *q_arg)
887 {
888         char *end = NULL;
889         unsigned long pm;
890
891         /* parse hexadecimal string */
892         pm = strtoul(q_arg, &end, 16);
893         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
894                 pm = 0;
895
896         options->portmask = pm;
897         if (options->portmask == 0) {
898                 printf("invalid portmask specified\n");
899                 return -1;
900         }
901
902         return pm;
903 }
904
905 /** Parse number of queues */
906 static int
907 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
908                 const char *q_arg)
909 {
910         char *end = NULL;
911         unsigned long n;
912
913         /* parse hexadecimal string */
914         n = strtoul(q_arg, &end, 10);
915         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
916                 n = 0;
917         else if (n >= MAX_RX_QUEUE_PER_LCORE)
918                 n = 0;
919
920         options->nb_ports_per_lcore = n;
921         if (options->nb_ports_per_lcore == 0) {
922                 printf("invalid number of ports selected\n");
923                 return -1;
924         }
925
926         return 0;
927 }
928
929 /** Parse timer period */
930 static int
931 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
932                 const char *q_arg)
933 {
934         char *end = NULL;
935         long int n;
936
937         /* parse number string */
938         n = strtol(q_arg, &end, 10);
939         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
940                 n = 0;
941
942         if (n >= MAX_TIMER_PERIOD) {
943                 printf("Warning refresh period specified %ld is greater than "
944                                 "max value %d! using max value",
945                                 n, MAX_TIMER_PERIOD);
946                 n = MAX_TIMER_PERIOD;
947         }
948
949         options->refresh_period = n * 1000 * TIMER_MILLISECOND;
950
951         return 0;
952 }
953
954 /** Generate default options for application */
955 static void
956 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
957 {
958         srand(time(NULL));
959
960         options->portmask = 0xffffffff;
961         options->nb_ports_per_lcore = 1;
962         options->refresh_period = 10000;
963         options->single_lcore = 0;
964
965         options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
966         options->sessionless = 0;
967         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
968
969         /* Cipher Data */
970         options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
971         options->cipher_xform.next = NULL;
972
973         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
974         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
975
976         generate_random_key(options->ckey_data, sizeof(options->ckey_data));
977
978         options->cipher_xform.cipher.key.data = options->ckey_data;
979         options->cipher_xform.cipher.key.length = 16;
980
981
982         /* Authentication Data */
983         options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
984         options->auth_xform.next = NULL;
985
986         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
987         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
988
989         options->auth_xform.auth.add_auth_data_length = 0;
990         options->auth_xform.auth.digest_length = 20;
991
992         generate_random_key(options->akey_data, sizeof(options->akey_data));
993
994         options->auth_xform.auth.key.data = options->akey_data;
995         options->auth_xform.auth.key.length = 20;
996 }
997
998 static void
999 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1000 {
1001         printf("Options:-\nn");
1002         printf("portmask: %x\n", options->portmask);
1003         printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1004         printf("refresh period : %u\n", options->refresh_period);
1005         printf("single lcore mode: %s\n",
1006                         options->single_lcore ? "enabled" : "disabled");
1007         printf("stats_printing: %s\n",
1008                         options->refresh_period == 0 ? "disabled" : "enabled");
1009
1010         switch (options->cdev_type) {
1011         case RTE_CRYPTODEV_AESNI_MB_PMD:
1012                 printf("cryptodev type: AES-NI MB PMD\n"); break;
1013         case RTE_CRYPTODEV_QAT_SYM_PMD:
1014                 printf("cryptodev type: QAT PMD\n"); break;
1015         default:
1016                 break;
1017         }
1018
1019         printf("sessionless crypto: %s\n",
1020                         options->sessionless ? "enabled" : "disabled");
1021 #if 0
1022         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1023
1024         /* Cipher Data */
1025         options->cipher_xform.type = RTE_CRYPTO_XFORM_CIPHER;
1026         options->cipher_xform.next = NULL;
1027
1028         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1029         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1030
1031         generate_random_key(options->ckey_data, sizeof(options->ckey_data));
1032
1033         options->cipher_xform.cipher.key.data = options->ckey_data;
1034         options->cipher_xform.cipher.key.phys_addr = 0;
1035         options->cipher_xform.cipher.key.length = 16;
1036
1037
1038         /* Authentication Data */
1039         options->auth_xform.type = RTE_CRYPTO_XFORM_AUTH;
1040         options->auth_xform.next = NULL;
1041
1042         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1043         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1044
1045         options->auth_xform.auth.add_auth_data_length = 0;
1046         options->auth_xform.auth.digest_length = 20;
1047
1048         generate_random_key(options->akey_data, sizeof(options->akey_data));
1049
1050         options->auth_xform.auth.key.data = options->akey_data;
1051         options->auth_xform.auth.key.phys_addr = 0;
1052         options->auth_xform.auth.key.length = 20;
1053 #endif
1054 }
1055
1056 /* Parse the argument given in the command line of the application */
1057 static int
1058 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1059                 int argc, char **argv)
1060 {
1061         int opt, retval, option_index;
1062         char **argvopt = argv, *prgname = argv[0];
1063
1064         static struct option lgopts[] = {
1065                         { "sessionless", no_argument, 0, 0 },
1066
1067                         { "cdev_type", required_argument, 0, 0 },
1068                         { "chain", required_argument, 0, 0 },
1069
1070                         { "cipher_algo", required_argument, 0, 0 },
1071                         { "cipher_op", required_argument, 0, 0 },
1072                         { "cipher_key", required_argument, 0, 0 },
1073
1074                         { "auth_algo", required_argument, 0, 0 },
1075                         { "auth_op", required_argument, 0, 0 },
1076                         { "auth_key", required_argument, 0, 0 },
1077
1078                         { "iv", required_argument, 0, 0 },
1079
1080                         { "sessionless", no_argument, 0, 0 },
1081                         { NULL, 0, 0, 0 }
1082         };
1083
1084         l2fwd_crypto_default_options(options);
1085
1086         while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1087                         &option_index)) != EOF) {
1088                 switch (opt) {
1089                 /* long options */
1090                 case 0:
1091                         retval = l2fwd_crypto_parse_args_long_options(options,
1092                                         lgopts, option_index);
1093                         if (retval < 0) {
1094                                 l2fwd_crypto_usage(prgname);
1095                                 return -1;
1096                         }
1097                         break;
1098
1099                 /* portmask */
1100                 case 'p':
1101                         retval = l2fwd_crypto_parse_portmask(options, optarg);
1102                         if (retval < 0) {
1103                                 l2fwd_crypto_usage(prgname);
1104                                 return -1;
1105                         }
1106                         break;
1107
1108                 /* nqueue */
1109                 case 'q':
1110                         retval = l2fwd_crypto_parse_nqueue(options, optarg);
1111                         if (retval < 0) {
1112                                 l2fwd_crypto_usage(prgname);
1113                                 return -1;
1114                         }
1115                         break;
1116
1117                 /* single  */
1118                 case 's':
1119                         options->single_lcore = 1;
1120
1121                         break;
1122
1123                 /* timer period */
1124                 case 't':
1125                         retval = l2fwd_crypto_parse_timer_period(options,
1126                                         optarg);
1127                         if (retval < 0) {
1128                                 l2fwd_crypto_usage(prgname);
1129                                 return -1;
1130                         }
1131                         break;
1132
1133                 default:
1134                         l2fwd_crypto_usage(prgname);
1135                         return -1;
1136                 }
1137         }
1138
1139
1140         if (optind >= 0)
1141                 argv[optind-1] = prgname;
1142
1143         retval = optind-1;
1144         optind = 0; /* reset getopt lib */
1145
1146         return retval;
1147 }
1148
1149 /* Check the link status of all ports in up to 9s, and print them finally */
1150 static void
1151 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1152 {
1153 #define CHECK_INTERVAL 100 /* 100ms */
1154 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1155         uint8_t portid, count, all_ports_up, print_flag = 0;
1156         struct rte_eth_link link;
1157
1158         printf("\nChecking link status");
1159         fflush(stdout);
1160         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1161                 all_ports_up = 1;
1162                 for (portid = 0; portid < port_num; portid++) {
1163                         if ((port_mask & (1 << portid)) == 0)
1164                                 continue;
1165                         memset(&link, 0, sizeof(link));
1166                         rte_eth_link_get_nowait(portid, &link);
1167                         /* print link status if flag set */
1168                         if (print_flag == 1) {
1169                                 if (link.link_status)
1170                                         printf("Port %d Link Up - speed %u "
1171                                                 "Mbps - %s\n", (uint8_t)portid,
1172                                                 (unsigned)link.link_speed,
1173                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1174                                         ("full-duplex") : ("half-duplex\n"));
1175                                 else
1176                                         printf("Port %d Link Down\n",
1177                                                 (uint8_t)portid);
1178                                 continue;
1179                         }
1180                         /* clear all_ports_up flag if any link down */
1181                         if (link.link_status == 0) {
1182                                 all_ports_up = 0;
1183                                 break;
1184                         }
1185                 }
1186                 /* after finally printing all link status, get out */
1187                 if (print_flag == 1)
1188                         break;
1189
1190                 if (all_ports_up == 0) {
1191                         printf(".");
1192                         fflush(stdout);
1193                         rte_delay_ms(CHECK_INTERVAL);
1194                 }
1195
1196                 /* set the print_flag if all ports up or timeout */
1197                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1198                         print_flag = 1;
1199                         printf("done\n");
1200                 }
1201         }
1202 }
1203
1204 static int
1205 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1206 {
1207         unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1208         int retval;
1209
1210         if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1211                 if (rte_cryptodev_count() < nb_ports)
1212                         return -1;
1213         } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1214                 for (i = 0; i < nb_ports; i++) {
1215                         int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1216                                         NULL);
1217                         if (retval < 0)
1218                                 return -1;
1219                 }
1220         }
1221
1222         cdev_count = rte_cryptodev_count();
1223         for (cdev_id = 0;
1224                         cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1225                         cdev_id++) {
1226                 struct rte_cryptodev_qp_conf qp_conf;
1227                 struct rte_cryptodev_info dev_info;
1228
1229                 struct rte_cryptodev_config conf = {
1230                         .nb_queue_pairs = 1,
1231                         .socket_id = SOCKET_ID_ANY,
1232                         .session_mp = {
1233                                 .nb_objs = 2048,
1234                                 .cache_size = 64
1235                         }
1236                 };
1237
1238                 rte_cryptodev_info_get(cdev_id, &dev_info);
1239
1240                 if (dev_info.dev_type != options->cdev_type)
1241                         continue;
1242
1243
1244                 retval = rte_cryptodev_configure(cdev_id, &conf);
1245                 if (retval < 0) {
1246                         printf("Failed to configure cryptodev %u", cdev_id);
1247                         return -1;
1248                 }
1249
1250                 qp_conf.nb_descriptors = 2048;
1251
1252                 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1253                                 SOCKET_ID_ANY);
1254                 if (retval < 0) {
1255                         printf("Failed to setup queue pair %u on cryptodev %u",
1256                                         0, cdev_id);
1257                         return -1;
1258                 }
1259
1260                 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1261
1262                 enabled_cdev_count++;
1263         }
1264
1265         return enabled_cdev_count;
1266 }
1267
1268 static int
1269 initialize_ports(struct l2fwd_crypto_options *options)
1270 {
1271         uint8_t last_portid, portid;
1272         unsigned enabled_portcount = 0;
1273         unsigned nb_ports = rte_eth_dev_count();
1274
1275         if (nb_ports == 0) {
1276                 printf("No Ethernet ports - bye\n");
1277                 return -1;
1278         }
1279
1280         if (nb_ports > RTE_MAX_ETHPORTS)
1281                 nb_ports = RTE_MAX_ETHPORTS;
1282
1283         /* Reset l2fwd_dst_ports */
1284         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1285                 l2fwd_dst_ports[portid] = 0;
1286
1287         for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1288                 int retval;
1289
1290                 /* Skip ports that are not enabled */
1291                 if ((options->portmask & (1 << portid)) == 0)
1292                         continue;
1293
1294                 /* init port */
1295                 printf("Initializing port %u... ", (unsigned) portid);
1296                 fflush(stdout);
1297                 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1298                 if (retval < 0) {
1299                         printf("Cannot configure device: err=%d, port=%u\n",
1300                                   retval, (unsigned) portid);
1301                         return -1;
1302                 }
1303
1304                 /* init one RX queue */
1305                 fflush(stdout);
1306                 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1307                                              rte_eth_dev_socket_id(portid),
1308                                              NULL, l2fwd_pktmbuf_pool);
1309                 if (retval < 0) {
1310                         printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1311                                         retval, (unsigned) portid);
1312                         return -1;
1313                 }
1314
1315                 /* init one TX queue on each port */
1316                 fflush(stdout);
1317                 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1318                                 rte_eth_dev_socket_id(portid),
1319                                 NULL);
1320                 if (retval < 0) {
1321                         printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1322                                 retval, (unsigned) portid);
1323
1324                         return -1;
1325                 }
1326
1327                 /* Start device */
1328                 retval = rte_eth_dev_start(portid);
1329                 if (retval < 0) {
1330                         printf("rte_eth_dev_start:err=%d, port=%u\n",
1331                                         retval, (unsigned) portid);
1332                         return -1;
1333                 }
1334
1335                 rte_eth_promiscuous_enable(portid);
1336
1337                 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1338
1339                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1340                                 (unsigned) portid,
1341                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1342                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1343                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1344                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1345                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1346                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1347
1348                 /* initialize port stats */
1349                 memset(&port_statistics, 0, sizeof(port_statistics));
1350
1351                 /* Setup port forwarding table */
1352                 if (enabled_portcount % 2) {
1353                         l2fwd_dst_ports[portid] = last_portid;
1354                         l2fwd_dst_ports[last_portid] = portid;
1355                 } else {
1356                         last_portid = portid;
1357                 }
1358
1359                 l2fwd_enabled_port_mask |= (1 << portid);
1360                 enabled_portcount++;
1361         }
1362
1363         if (enabled_portcount == 1) {
1364                 l2fwd_dst_ports[last_portid] = last_portid;
1365         } else if (enabled_portcount % 2) {
1366                 printf("odd number of ports in portmask- bye\n");
1367                 return -1;
1368         }
1369
1370         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1371
1372         return enabled_portcount;
1373 }
1374
1375 int
1376 main(int argc, char **argv)
1377 {
1378         struct lcore_queue_conf *qconf;
1379         struct l2fwd_crypto_options options;
1380
1381         uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1382         unsigned lcore_id, rx_lcore_id;
1383         int ret, enabled_cdevcount, enabled_portcount;
1384
1385         /* init EAL */
1386         ret = rte_eal_init(argc, argv);
1387         if (ret < 0)
1388                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1389         argc -= ret;
1390         argv += ret;
1391
1392         /* parse application arguments (after the EAL ones) */
1393         ret = l2fwd_crypto_parse_args(&options, argc, argv);
1394         if (ret < 0)
1395                 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1396
1397         /* create the mbuf pool */
1398         l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1399                         sizeof(struct rte_crypto_op),
1400                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1401         if (l2fwd_pktmbuf_pool == NULL)
1402                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1403
1404         /* create crypto op pool */
1405         l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1406                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1407                         rte_socket_id());
1408         if (l2fwd_crypto_op_pool == NULL)
1409                 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1410
1411         /* Enable Ethernet ports */
1412         enabled_portcount = initialize_ports(&options);
1413         if (enabled_portcount < 1)
1414                 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1415
1416         nb_ports = rte_eth_dev_count();
1417         /* Initialize the port/queue configuration of each logical core */
1418         for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1419                         portid < nb_ports; portid++) {
1420
1421                 /* skip ports that are not enabled */
1422                 if ((options.portmask & (1 << portid)) == 0)
1423                         continue;
1424
1425                 if (options.single_lcore && qconf == NULL) {
1426                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1427                                 rx_lcore_id++;
1428                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1429                                         rte_exit(EXIT_FAILURE,
1430                                                         "Not enough cores\n");
1431                         }
1432                 } else if (!options.single_lcore) {
1433                         /* get the lcore_id for this port */
1434                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1435                                lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1436                                options.nb_ports_per_lcore) {
1437                                 rx_lcore_id++;
1438                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1439                                         rte_exit(EXIT_FAILURE,
1440                                                         "Not enough cores\n");
1441                         }
1442                 }
1443
1444                 /* Assigned a new logical core in the loop above. */
1445                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1446                         qconf = &lcore_queue_conf[rx_lcore_id];
1447
1448                 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1449                 qconf->nb_rx_ports++;
1450
1451                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1452         }
1453
1454
1455         /* Enable Crypto devices */
1456         enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1457         if (enabled_cdevcount < 1)
1458                 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1459
1460         nb_cryptodevs = rte_cryptodev_count();
1461         /* Initialize the port/queue configuration of each logical core */
1462         for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1463                         cdev_id < nb_cryptodevs && enabled_cdevcount;
1464                         cdev_id++) {
1465                 struct rte_cryptodev_info info;
1466
1467                 rte_cryptodev_info_get(cdev_id, &info);
1468
1469                 /* skip devices of the wrong type */
1470                 if (options.cdev_type != info.dev_type)
1471                         continue;
1472
1473                 if (options.single_lcore && qconf == NULL) {
1474                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1475                                 rx_lcore_id++;
1476                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1477                                         rte_exit(EXIT_FAILURE,
1478                                                         "Not enough cores\n");
1479                         }
1480                 } else if (!options.single_lcore) {
1481                         /* get the lcore_id for this port */
1482                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1483                                lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1484                                options.nb_ports_per_lcore) {
1485                                 rx_lcore_id++;
1486                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1487                                         rte_exit(EXIT_FAILURE,
1488                                                         "Not enough cores\n");
1489                         }
1490                 }
1491
1492                 /* Assigned a new logical core in the loop above. */
1493                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1494                         qconf = &lcore_queue_conf[rx_lcore_id];
1495
1496                 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1497                 qconf->nb_crypto_devs++;
1498
1499                 enabled_cdevcount--;
1500
1501                 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1502                                 (unsigned)cdev_id);
1503         }
1504
1505
1506
1507         /* launch per-lcore init on every lcore */
1508         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1509                         CALL_MASTER);
1510         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1511                 if (rte_eal_wait_lcore(lcore_id) < 0)
1512                         return -1;
1513         }
1514
1515         return 0;
1516 }