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