mbuf: remove the rte_pktmbuf structure
[dpdk.git] / examples / dpdk_qat / 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 <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_ring.h>
67 #include <rte_mempool.h>
68 #include <rte_mbuf.h>
69 #include <rte_ip.h>
70 #include <rte_string_fns.h>
71
72 #include "main.h"
73 #include "crypto.h"
74
75 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
76 #define NB_MBUF   (32 * 1024)
77
78 /*
79  * RX and TX Prefetch, Host, and Write-back threshold values should be
80  * carefully set for optimal performance. Consult the network
81  * controller's datasheet and supporting DPDK documentation for guidance
82  * on how these parameters should be set.
83  */
84 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
85 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
86 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
87
88 /*
89  * These default values are optimized for use with the Intel(R) 82599 10 GbE
90  * Controller and the DPDK ixgbe PMD. Consider using other values for other
91  * network controllers and/or network drivers.
92  */
93 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
94 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
95 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
96
97 #define MAX_PKT_BURST 32
98 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
99
100 #define TX_QUEUE_FLUSH_MASK 0xFFFFFFFF
101 #define TSC_COUNT_LIMIT 1000
102
103 #define ACTION_ENCRYPT 1
104 #define ACTION_DECRYPT 2
105
106 /*
107  * Configurable number of RX/TX ring descriptors
108  */
109 #define RTE_TEST_RX_DESC_DEFAULT 128
110 #define RTE_TEST_TX_DESC_DEFAULT 512
111 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
112 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
113
114 /* ethernet addresses of ports */
115 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
116
117 /* mask of enabled ports */
118 static unsigned enabled_port_mask = 0;
119 static int promiscuous_on = 1; /**< Ports set in promiscuous mode on by default. */
120
121 /* list of enabled ports */
122 static uint32_t dst_ports[RTE_MAX_ETHPORTS];
123
124 struct mbuf_table {
125         uint16_t len;
126         struct rte_mbuf *m_table[MAX_PKT_BURST];
127 };
128
129 struct lcore_rx_queue {
130         uint8_t port_id;
131         uint8_t queue_id;
132 };
133
134 #define MAX_RX_QUEUE_PER_LCORE 16
135
136 #define MAX_LCORE_PARAMS 1024
137 struct lcore_params {
138         uint8_t port_id;
139         uint8_t queue_id;
140         uint8_t lcore_id;
141 };
142
143 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
144 static struct lcore_params lcore_params_array_default[] = {
145         {0, 0, 2},
146         {0, 1, 2},
147         {0, 2, 2},
148         {1, 0, 2},
149         {1, 1, 2},
150         {1, 2, 2},
151         {2, 0, 2},
152         {3, 0, 3},
153         {3, 1, 3},
154 };
155
156 static struct lcore_params * lcore_params = lcore_params_array_default;
157 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
158                                 sizeof(lcore_params_array_default[0]);
159
160 static struct rte_eth_conf port_conf = {
161         .rxmode = {
162                 .mq_mode        = ETH_MQ_RX_RSS,
163                 .split_hdr_size = 0,
164                 .header_split   = 0, /**< Header Split disabled */
165                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
166                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
167                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
168                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
169         },
170         .rx_adv_conf = {
171                 .rss_conf = {
172                         .rss_key = NULL,
173                         .rss_hf = ETH_RSS_IP,
174                 },
175         },
176         .txmode = {
177                 .mq_mode = ETH_MQ_TX_NONE,
178         },
179 };
180
181 static const struct rte_eth_rxconf rx_conf = {
182         .rx_thresh = {
183                 .pthresh = RX_PTHRESH,
184                 .hthresh = RX_HTHRESH,
185                 .wthresh = RX_WTHRESH,
186         },
187 };
188
189 static const struct rte_eth_txconf tx_conf = {
190         .tx_thresh = {
191                 .pthresh = TX_PTHRESH,
192                 .hthresh = TX_HTHRESH,
193                 .wthresh = TX_WTHRESH,
194         },
195         .tx_free_thresh = 0, /* Use PMD default values */
196         .tx_rs_thresh = 0, /* Use PMD default values */
197 };
198
199 static struct rte_mempool * pktmbuf_pool[RTE_MAX_NUMA_NODES];
200
201 struct lcore_conf {
202         uint64_t tsc;
203         uint64_t tsc_count;
204         uint32_t tx_mask;
205         uint16_t n_rx_queue;
206         uint16_t rx_queue_list_pos;
207         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
208         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
209         struct mbuf_table rx_mbuf;
210         uint32_t rx_mbuf_pos;
211         uint32_t rx_curr_queue;
212         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
213 } __rte_cache_aligned;
214
215 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
216
217 static inline struct rte_mbuf *
218 nic_rx_get_packet(struct lcore_conf *qconf)
219 {
220         struct rte_mbuf *pkt;
221
222         if (unlikely(qconf->n_rx_queue == 0))
223                 return NULL;
224
225         /* Look for the next queue with packets; return if none */
226         if (unlikely(qconf->rx_mbuf_pos == qconf->rx_mbuf.len)) {
227                 uint32_t i;
228
229                 qconf->rx_mbuf_pos = 0;
230                 for (i = 0; i < qconf->n_rx_queue; i++) {
231                         qconf->rx_mbuf.len = rte_eth_rx_burst(
232                                 qconf->rx_queue_list[qconf->rx_curr_queue].port_id,
233                                 qconf->rx_queue_list[qconf->rx_curr_queue].queue_id,
234                                 qconf->rx_mbuf.m_table, MAX_PKT_BURST);
235
236                         qconf->rx_curr_queue++;
237                         if (unlikely(qconf->rx_curr_queue == qconf->n_rx_queue))
238                                 qconf->rx_curr_queue = 0;
239                         if (likely(qconf->rx_mbuf.len > 0))
240                                 break;
241                 }
242                 if (unlikely(i == qconf->n_rx_queue))
243                         return NULL;
244         }
245
246         /* Get the next packet from the current queue; if last packet, go to next queue */
247         pkt = qconf->rx_mbuf.m_table[qconf->rx_mbuf_pos];
248         qconf->rx_mbuf_pos++;
249
250         return pkt;
251 }
252
253 static inline void
254 nic_tx_flush_queues(struct lcore_conf *qconf)
255 {
256         uint8_t portid;
257
258         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
259                 struct rte_mbuf **m_table = NULL;
260                 uint16_t queueid, len;
261                 uint32_t n, i;
262
263                 if (likely((qconf->tx_mask & (1 << portid)) == 0))
264                         continue;
265
266                 len = qconf->tx_mbufs[portid].len;
267                 if (likely(len == 0))
268                         continue;
269
270                 queueid = qconf->tx_queue_id[portid];
271                 m_table = qconf->tx_mbufs[portid].m_table;
272
273                 n = rte_eth_tx_burst(portid, queueid, m_table, len);
274                 for (i = n; i < len; i++){
275                         rte_pktmbuf_free(m_table[i]);
276                 }
277
278                 qconf->tx_mbufs[portid].len = 0;
279         }
280
281         qconf->tx_mask = TX_QUEUE_FLUSH_MASK;
282 }
283
284 static inline void
285 nic_tx_send_packet(struct rte_mbuf *pkt, uint8_t port)
286 {
287         struct lcore_conf *qconf;
288         uint32_t lcoreid;
289         uint16_t len;
290
291         if (unlikely(pkt == NULL)) {
292                 return;
293         }
294
295         lcoreid = rte_lcore_id();
296         qconf = &lcore_conf[lcoreid];
297
298         len = qconf->tx_mbufs[port].len;
299         qconf->tx_mbufs[port].m_table[len] = pkt;
300         len++;
301
302         /* enough pkts to be sent */
303         if (unlikely(len == MAX_PKT_BURST)) {
304                 uint32_t n, i;
305                 uint16_t queueid;
306
307                 queueid = qconf->tx_queue_id[port];
308                 n = rte_eth_tx_burst(port, queueid, qconf->tx_mbufs[port].m_table, MAX_PKT_BURST);
309                 for (i = n; i < MAX_PKT_BURST; i++){
310                         rte_pktmbuf_free(qconf->tx_mbufs[port].m_table[i]);
311                 }
312
313                 qconf->tx_mask &= ~(1 << port);
314                 len = 0;
315         }
316
317         qconf->tx_mbufs[port].len = len;
318 }
319
320 /* main processing loop */
321 static __attribute__((noreturn)) int
322 main_loop(__attribute__((unused)) void *dummy)
323 {
324         uint32_t lcoreid;
325         struct lcore_conf *qconf;
326         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
327
328         lcoreid = rte_lcore_id();
329         qconf = &lcore_conf[lcoreid];
330
331         printf("Thread %u starting...\n", lcoreid);
332
333         for (;;) {
334                 struct rte_mbuf *pkt;
335                 uint32_t pkt_from_nic_rx = 0;
336                 uint8_t port;
337
338                 /* Flush TX queues */
339                 qconf->tsc_count++;
340                 if (unlikely(qconf->tsc_count == TSC_COUNT_LIMIT)) {
341                         uint64_t tsc, diff_tsc;
342
343                         tsc = rte_rdtsc();
344
345                         diff_tsc = tsc - qconf->tsc;
346                         if (unlikely(diff_tsc > drain_tsc)) {
347                                 nic_tx_flush_queues(qconf);
348                                 crypto_flush_tx_queue(lcoreid);
349                                 qconf->tsc = tsc;
350                         }
351
352                         qconf->tsc_count = 0;
353                 }
354
355                 /*
356                  * Check the Intel QuickAssist queues first
357                  *
358                  ***/
359                 pkt = (struct rte_mbuf *) crypto_get_next_response();
360                 if (pkt == NULL) {
361                         pkt = nic_rx_get_packet(qconf);
362                         pkt_from_nic_rx = 1;
363                 }
364                 if (pkt == NULL)
365                         continue;
366                 /* Send packet to either QAT encrypt, QAT decrypt or NIC TX */
367                 if (pkt_from_nic_rx) {
368                         struct ipv4_hdr *ip  = (struct ipv4_hdr *) (rte_pktmbuf_mtod(pkt, unsigned char *) +
369                                         sizeof(struct ether_hdr));
370                         if (ip->src_addr & rte_cpu_to_be_32(ACTION_ENCRYPT)) {
371                                 if (CRYPTO_RESULT_FAIL == crypto_encrypt(pkt,
372                                         (enum cipher_alg)((ip->src_addr >> 16) & 0xFF),
373                                         (enum hash_alg)((ip->src_addr >> 8) & 0xFF)))
374                                         rte_pktmbuf_free(pkt);
375                                 continue;
376                         }
377
378                         if (ip->src_addr & rte_cpu_to_be_32(ACTION_DECRYPT)) {
379                                 if(CRYPTO_RESULT_FAIL == crypto_decrypt(pkt,
380                                         (enum cipher_alg)((ip->src_addr >> 16) & 0xFF),
381                                         (enum hash_alg)((ip->src_addr >> 8) & 0xFF)))
382                                         rte_pktmbuf_free(pkt);
383                                 continue;
384                         }
385                 }
386
387                 port = dst_ports[pkt->in_port];
388
389                 /* Transmit the packet */
390                 nic_tx_send_packet(pkt, (uint8_t)port);
391         }
392 }
393
394 static inline unsigned
395 get_port_max_rx_queues(uint8_t port_id)
396 {
397         struct rte_eth_dev_info dev_info;
398
399         rte_eth_dev_info_get(port_id, &dev_info);
400         return dev_info.max_rx_queues;
401 }
402
403 static inline unsigned
404 get_port_max_tx_queues(uint8_t port_id)
405 {
406         struct rte_eth_dev_info dev_info;
407
408         rte_eth_dev_info_get(port_id, &dev_info);
409         return dev_info.max_tx_queues;
410 }
411
412 static int
413 check_lcore_params(void)
414 {
415         uint16_t i;
416
417         for (i = 0; i < nb_lcore_params; ++i) {
418                 if (lcore_params[i].queue_id >= get_port_max_rx_queues(lcore_params[i].port_id)) {
419                         printf("invalid queue number: %hhu\n", lcore_params[i].queue_id);
420                         return -1;
421                 }
422                 if (!rte_lcore_is_enabled(lcore_params[i].lcore_id)) {
423                         printf("error: lcore %hhu is not enabled in lcore mask\n",
424                                 lcore_params[i].lcore_id);
425                         return -1;
426                 }
427         }
428         return 0;
429 }
430
431 static int
432 check_port_config(const unsigned nb_ports)
433 {
434         unsigned portid;
435         uint16_t i;
436
437         for (i = 0; i < nb_lcore_params; ++i) {
438                 portid = lcore_params[i].port_id;
439                 if ((enabled_port_mask & (1 << portid)) == 0) {
440                         printf("port %u is not enabled in port mask\n", portid);
441                         return -1;
442                 }
443                 if (portid >= nb_ports) {
444                         printf("port %u is not present on the board\n", portid);
445                         return -1;
446                 }
447         }
448         return 0;
449 }
450
451 static uint8_t
452 get_port_n_rx_queues(const uint8_t port)
453 {
454         int queue = -1;
455         uint16_t i;
456
457         for (i = 0; i < nb_lcore_params; ++i) {
458                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
459                         queue = lcore_params[i].queue_id;
460         }
461         return (uint8_t)(++queue);
462 }
463
464 static int
465 init_lcore_rx_queues(void)
466 {
467         uint16_t i, nb_rx_queue;
468         uint8_t lcore;
469
470         for (i = 0; i < nb_lcore_params; ++i) {
471                 lcore = lcore_params[i].lcore_id;
472                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
473                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
474                         printf("error: too many queues (%u) for lcore: %u\n",
475                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
476                         return -1;
477                 }
478                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
479                         lcore_params[i].port_id;
480                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
481                         lcore_params[i].queue_id;
482                 lcore_conf[lcore].n_rx_queue++;
483         }
484         return 0;
485 }
486
487 /* display usage */
488 static void
489 print_usage(const char *prgname)
490 {
491         printf ("%s [EAL options] -- -p PORTMASK [--no-promisc]"
492                 "  [--config '(port,queue,lcore)[,(port,queue,lcore)]'\n"
493                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
494                 "  --no-promisc: disable promiscuous mode (default is ON)\n"
495                 "  --config '(port,queue,lcore)': rx queues configuration\n",
496                 prgname);
497 }
498
499 static unsigned
500 parse_portmask(const char *portmask)
501 {
502         char *end = NULL;
503         unsigned pm;
504
505         /* parse hexadecimal string */
506         pm = strtoul(portmask, &end, 16);
507         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
508                 return 0;
509
510         return pm;
511 }
512
513 static int
514 parse_config(const char *q_arg)
515 {
516         char s[256];
517         const char *p, *p_end = q_arg;
518         char *end;
519         enum fieldnames {
520                 FLD_PORT = 0,
521                 FLD_QUEUE,
522                 FLD_LCORE,
523                 _NUM_FLD
524         };
525         unsigned long int_fld[_NUM_FLD];
526         char *str_fld[_NUM_FLD];
527         int i;
528         unsigned size;
529
530         nb_lcore_params = 0;
531
532         while ((p = strchr(p_end,'(')) != NULL) {
533                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
534                         printf("exceeded max number of lcore params: %hu\n",
535                                 nb_lcore_params);
536                         return -1;
537                 }
538                 ++p;
539                 if((p_end = strchr(p,')')) == NULL)
540                         return -1;
541
542                 size = p_end - p;
543                 if(size >= sizeof(s))
544                         return -1;
545
546                 snprintf(s, sizeof(s), "%.*s", size, p);
547                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
548                         return -1;
549                 for (i = 0; i < _NUM_FLD; i++) {
550                         errno = 0;
551                         int_fld[i] = strtoul(str_fld[i], &end, 0);
552                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
553                                 return -1;
554                 }
555                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
556                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
557                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
558                 ++nb_lcore_params;
559         }
560         lcore_params = lcore_params_array;
561         return 0;
562 }
563
564 /* Parse the argument given in the command line of the application */
565 static int
566 parse_args(int argc, char **argv)
567 {
568         int opt, ret;
569         char **argvopt;
570         int option_index;
571         char *prgname = argv[0];
572         static struct option lgopts[] = {
573                 {"config", 1, 0, 0},
574                 {"no-promisc", 0, 0, 0},
575                 {NULL, 0, 0, 0}
576         };
577
578         argvopt = argv;
579
580         while ((opt = getopt_long(argc, argvopt, "p:",
581                                 lgopts, &option_index)) != EOF) {
582
583                 switch (opt) {
584                 /* portmask */
585                 case 'p':
586                         enabled_port_mask = parse_portmask(optarg);
587                         if (enabled_port_mask == 0) {
588                                 printf("invalid portmask\n");
589                                 print_usage(prgname);
590                                 return -1;
591                         }
592                         break;
593
594                 /* long options */
595                 case 0:
596                         if (strcmp(lgopts[option_index].name, "config") == 0) {
597                                 ret = parse_config(optarg);
598                                 if (ret) {
599                                         printf("invalid config\n");
600                                         print_usage(prgname);
601                                         return -1;
602                                 }
603                         }
604                         if (strcmp(lgopts[option_index].name, "no-promisc") == 0) {
605                                 printf("Promiscuous mode disabled\n");
606                                 promiscuous_on = 0;
607                         }
608                         break;
609                 default:
610                         print_usage(prgname);
611                         return -1;
612                 }
613         }
614
615         if (enabled_port_mask == 0) {
616                 printf("portmask not specified\n");
617                 print_usage(prgname);
618                 return -1;
619         }
620
621         if (optind >= 0)
622                 argv[optind-1] = prgname;
623
624         ret = optind-1;
625         optind = 0; /* reset getopt lib */
626         return ret;
627 }
628
629 static void
630 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
631 {
632         printf ("%s%02X:%02X:%02X:%02X:%02X:%02X", name,
633                 eth_addr->addr_bytes[0],
634                 eth_addr->addr_bytes[1],
635                 eth_addr->addr_bytes[2],
636                 eth_addr->addr_bytes[3],
637                 eth_addr->addr_bytes[4],
638                 eth_addr->addr_bytes[5]);
639 }
640
641 static int
642 init_mem(void)
643 {
644         const unsigned flags = 0;
645         int socketid;
646         unsigned lcoreid;
647         char s[64];
648
649         RTE_LCORE_FOREACH(lcoreid) {
650                 socketid = rte_lcore_to_socket_id(lcoreid);
651                 if (socketid >= RTE_MAX_NUMA_NODES) {
652                         printf("Socket %d of lcore %u is out of range %d\n",
653                                 socketid, lcoreid, RTE_MAX_NUMA_NODES);
654                         return -1;
655                 }
656                 if (pktmbuf_pool[socketid] == NULL) {
657                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
658                         pktmbuf_pool[socketid] =
659                                 rte_mempool_create(s, NB_MBUF, MBUF_SIZE, 32,
660                                         sizeof(struct rte_pktmbuf_pool_private),
661                                         rte_pktmbuf_pool_init, NULL,
662                                         rte_pktmbuf_init, NULL,
663                                         socketid, flags);
664                         if (pktmbuf_pool[socketid] == NULL) {
665                                 printf("Cannot init mbuf pool on socket %d\n", socketid);
666                                 return -1;
667                         }
668                         printf("Allocated mbuf pool on socket %d\n", socketid);
669                 }
670         }
671         return 0;
672 }
673
674 int
675 MAIN(int argc, char **argv)
676 {
677         struct lcore_conf *qconf;
678         struct rte_eth_link link;
679         int ret;
680         unsigned nb_ports;
681         uint16_t queueid;
682         unsigned lcoreid;
683         uint32_t nb_tx_queue;
684         uint8_t portid, nb_rx_queue, queue, socketid, last_port;
685         unsigned nb_ports_in_mask = 0;
686
687         /* init EAL */
688         ret = rte_eal_init(argc, argv);
689         if (ret < 0)
690                 return -1;
691         argc -= ret;
692         argv += ret;
693
694         /* parse application arguments (after the EAL ones) */
695         ret = parse_args(argc, argv);
696         if (ret < 0)
697                 return -1;
698
699         if (rte_eal_pci_probe() < 0)
700                 rte_panic("Cannot probe PCI\n");
701
702         if (check_lcore_params() < 0)
703                 rte_panic("check_lcore_params failed\n");
704
705         ret = init_lcore_rx_queues();
706         if (ret < 0)
707                 return -1;
708
709         ret = init_mem();
710         if (ret < 0)
711                 return -1;
712
713         nb_ports = rte_eth_dev_count();
714         if (nb_ports > RTE_MAX_ETHPORTS)
715                 nb_ports = RTE_MAX_ETHPORTS;
716
717         if (check_port_config(nb_ports) < 0)
718                 rte_panic("check_port_config failed\n");
719
720         /* reset dst_ports */
721         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
722                 dst_ports[portid] = 0;
723         last_port = 0;
724
725         /*
726          * Each logical core is assigned a dedicated TX queue on each port.
727          */
728         for (portid = 0; portid < nb_ports; portid++) {
729                 /* skip ports that are not enabled */
730                 if ((enabled_port_mask & (1 << portid)) == 0)
731                         continue;
732
733                 if (nb_ports_in_mask % 2) {
734                         dst_ports[portid] = last_port;
735                         dst_ports[last_port] = portid;
736                 }
737                 else
738                         last_port = portid;
739
740                 nb_ports_in_mask++;
741         }
742         if (nb_ports_in_mask % 2) {
743                 printf("Notice: odd number of ports in portmask.\n");
744                 dst_ports[last_port] = last_port;
745         }
746
747         /* initialize all ports */
748         for (portid = 0; portid < nb_ports; portid++) {
749                 /* skip ports that are not enabled */
750                 if ((enabled_port_mask & (1 << portid)) == 0) {
751                         printf("\nSkipping disabled port %d\n", portid);
752                         continue;
753                 }
754
755                 /* init port */
756                 printf("Initializing port %d ... ", portid );
757                 fflush(stdout);
758
759                 nb_rx_queue = get_port_n_rx_queues(portid);
760                 if (nb_rx_queue > get_port_max_rx_queues(portid))
761                         rte_panic("Number of rx queues %d exceeds max number of rx queues %u"
762                                 " for port %d\n", nb_rx_queue, get_port_max_rx_queues(portid),
763                                 portid);
764                 nb_tx_queue = rte_lcore_count();
765                 if (nb_tx_queue > get_port_max_tx_queues(portid))
766                         rte_panic("Number of lcores %u exceeds max number of tx queues %u"
767                                 " for port %d\n", nb_tx_queue, get_port_max_tx_queues(portid),
768                                 portid);
769                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
770                         nb_rx_queue, (unsigned)nb_tx_queue );
771                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
772                                         (uint16_t)nb_tx_queue, &port_conf);
773                 if (ret < 0)
774                         rte_panic("Cannot configure device: err=%d, port=%d\n",
775                                 ret, portid);
776
777                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
778                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
779                 printf(", ");
780
781                 /* init one TX queue per couple (lcore,port) */
782                 queueid = 0;
783                 RTE_LCORE_FOREACH(lcoreid) {
784                         socketid = (uint8_t)rte_lcore_to_socket_id(lcoreid);
785                         printf("txq=%u,%d,%d ", lcoreid, queueid, socketid);
786                         fflush(stdout);
787                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
788                                                      socketid, &tx_conf);
789                         if (ret < 0)
790                                 rte_panic("rte_eth_tx_queue_setup: err=%d, "
791                                         "port=%d\n", ret, portid);
792
793                         qconf = &lcore_conf[lcoreid];
794                         qconf->tx_queue_id[portid] = queueid;
795                         queueid++;
796                 }
797                 printf("\n");
798         }
799
800         RTE_LCORE_FOREACH(lcoreid) {
801                 qconf = &lcore_conf[lcoreid];
802                 printf("\nInitializing rx queues on lcore %u ... ", lcoreid );
803                 fflush(stdout);
804                 /* init RX queues */
805                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
806                         portid = qconf->rx_queue_list[queue].port_id;
807                         queueid = qconf->rx_queue_list[queue].queue_id;
808                         socketid = (uint8_t)rte_lcore_to_socket_id(lcoreid);
809                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
810                         fflush(stdout);
811
812                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
813                                         socketid, &rx_conf, pktmbuf_pool[socketid]);
814                         if (ret < 0)
815                                 rte_panic("rte_eth_rx_queue_setup: err=%d,"
816                                                 "port=%d\n", ret, portid);
817                 }
818         }
819
820         printf("\n");
821
822         /* start ports */
823         for (portid = 0; portid < nb_ports; portid++) {
824                 if ((enabled_port_mask & (1 << portid)) == 0)
825                         continue;
826                 /* Start device */
827                 ret = rte_eth_dev_start(portid);
828                 if (ret < 0)
829                         rte_panic("rte_eth_dev_start: err=%d, port=%d\n",
830                                 ret, portid);
831
832                 printf("done: Port %d ", portid);
833
834                 /* get link status */
835                 rte_eth_link_get(portid, &link);
836                 if (link.link_status)
837                         printf(" Link Up - speed %u Mbps - %s\n",
838                                (unsigned) link.link_speed,
839                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
840                                ("full-duplex") : ("half-duplex\n"));
841                 else
842                         printf(" Link Down\n");
843                 /*
844                  * If enabled, put device in promiscuous mode.
845                  * This allows IO forwarding mode to forward packets
846                  * to itself through 2 cross-connected  ports of the
847                  * target machine.
848                  */
849                 if (promiscuous_on)
850                         rte_eth_promiscuous_enable(portid);
851         }
852         printf("Crypto: Initializing Crypto...\n");
853         if (crypto_init() != 0)
854                 return -1;
855
856         RTE_LCORE_FOREACH(lcoreid) {
857                 if (per_core_crypto_init(lcoreid) != 0) {
858                 printf("Crypto: Cannot init lcore crypto on lcore %u\n", (unsigned)lcoreid);
859                         return -1;
860                 }
861         }
862         printf("Crypto: Initialization complete\n");
863         /* launch per-lcore init on every lcore */
864         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
865         RTE_LCORE_FOREACH_SLAVE(lcoreid) {
866                 if (rte_eal_wait_lcore(lcoreid) < 0)
867                         return -1;
868         }
869
870         return 0;
871 }