New upstream version 17.11-rc3
[deb_dpdk.git] / examples / packet_ordering / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <signal.h>
35 #include <getopt.h>
36
37 #include <rte_eal.h>
38 #include <rte_common.h>
39 #include <rte_errno.h>
40 #include <rte_ethdev.h>
41 #include <rte_lcore.h>
42 #include <rte_malloc.h>
43 #include <rte_mbuf.h>
44 #include <rte_mempool.h>
45 #include <rte_ring.h>
46 #include <rte_reorder.h>
47
48 #define RX_DESC_PER_QUEUE 128
49 #define TX_DESC_PER_QUEUE 512
50
51 #define MAX_PKTS_BURST 32
52 #define REORDER_BUFFER_SIZE 8192
53 #define MBUF_PER_POOL 65535
54 #define MBUF_POOL_CACHE_SIZE 250
55
56 #define RING_SIZE 16384
57
58 /* Macros for printing using RTE_LOG */
59 #define RTE_LOGTYPE_REORDERAPP          RTE_LOGTYPE_USER1
60
61 unsigned int portmask;
62 unsigned int disable_reorder;
63 volatile uint8_t quit_signal;
64
65 static struct rte_mempool *mbuf_pool;
66
67 static struct rte_eth_conf port_conf_default;
68
69 struct worker_thread_args {
70         struct rte_ring *ring_in;
71         struct rte_ring *ring_out;
72 };
73
74 struct send_thread_args {
75         struct rte_ring *ring_in;
76         struct rte_reorder_buffer *buffer;
77 };
78
79 volatile struct app_stats {
80         struct {
81                 uint64_t rx_pkts;
82                 uint64_t enqueue_pkts;
83                 uint64_t enqueue_failed_pkts;
84         } rx __rte_cache_aligned;
85
86         struct {
87                 uint64_t dequeue_pkts;
88                 uint64_t enqueue_pkts;
89                 uint64_t enqueue_failed_pkts;
90         } wkr __rte_cache_aligned;
91
92         struct {
93                 uint64_t dequeue_pkts;
94                 /* Too early pkts transmitted directly w/o reordering */
95                 uint64_t early_pkts_txtd_woro;
96                 /* Too early pkts failed from direct transmit */
97                 uint64_t early_pkts_tx_failed_woro;
98                 uint64_t ro_tx_pkts;
99                 uint64_t ro_tx_failed_pkts;
100         } tx __rte_cache_aligned;
101 } app_stats;
102
103 /**
104  * Get the last enabled lcore ID
105  *
106  * @return
107  *   The last enabled lcore ID.
108  */
109 static unsigned int
110 get_last_lcore_id(void)
111 {
112         int i;
113
114         for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
115                 if (rte_lcore_is_enabled(i))
116                         return i;
117         return 0;
118 }
119
120 /**
121  * Get the previous enabled lcore ID
122  * @param id
123  *  The current lcore ID
124  * @return
125  *   The previous enabled lcore ID or the current lcore
126  *   ID if it is the first available core.
127  */
128 static unsigned int
129 get_previous_lcore_id(unsigned int id)
130 {
131         int i;
132
133         for (i = id - 1; i >= 0; i--)
134                 if (rte_lcore_is_enabled(i))
135                         return i;
136         return id;
137 }
138
139 static inline void
140 pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
141 {
142         unsigned int i;
143
144         for (i = 0; i < n; i++)
145                 rte_pktmbuf_free(mbuf_table[i]);
146 }
147
148 /* display usage */
149 static void
150 print_usage(const char *prgname)
151 {
152         printf("%s [EAL options] -- -p PORTMASK\n"
153                         "  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
154                         prgname);
155 }
156
157 static int
158 parse_portmask(const char *portmask)
159 {
160         unsigned long pm;
161         char *end = NULL;
162
163         /* parse hexadecimal string */
164         pm = strtoul(portmask, &end, 16);
165         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
166                 return -1;
167
168         if (pm == 0)
169                 return -1;
170
171         return pm;
172 }
173
174 /* Parse the argument given in the command line of the application */
175 static int
176 parse_args(int argc, char **argv)
177 {
178         int opt;
179         int option_index;
180         char **argvopt;
181         char *prgname = argv[0];
182         static struct option lgopts[] = {
183                 {"disable-reorder", 0, 0, 0},
184                 {NULL, 0, 0, 0}
185         };
186
187         argvopt = argv;
188
189         while ((opt = getopt_long(argc, argvopt, "p:",
190                                         lgopts, &option_index)) != EOF) {
191                 switch (opt) {
192                 /* portmask */
193                 case 'p':
194                         portmask = parse_portmask(optarg);
195                         if (portmask == 0) {
196                                 printf("invalid portmask\n");
197                                 print_usage(prgname);
198                                 return -1;
199                         }
200                         break;
201                 /* long options */
202                 case 0:
203                         if (!strcmp(lgopts[option_index].name, "disable-reorder")) {
204                                 printf("reorder disabled\n");
205                                 disable_reorder = 1;
206                         }
207                         break;
208                 default:
209                         print_usage(prgname);
210                         return -1;
211                 }
212         }
213         if (optind <= 1) {
214                 print_usage(prgname);
215                 return -1;
216         }
217
218         argv[optind-1] = prgname;
219         optind = 1; /* reset getopt lib */
220         return 0;
221 }
222
223 /*
224  * Tx buffer error callback
225  */
226 static void
227 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
228                 void *userdata __rte_unused) {
229
230         /* free the mbufs which failed from transmit */
231         app_stats.tx.ro_tx_failed_pkts += count;
232         RTE_LOG_DP(DEBUG, REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
233         pktmbuf_free_bulk(unsent, count);
234
235 }
236
237 static inline int
238 free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
239         const uint8_t nb_ports = rte_eth_dev_count();
240         unsigned port_id;
241
242         /* initialize buffers for all ports */
243         for (port_id = 0; port_id < nb_ports; port_id++) {
244                 /* skip ports that are not enabled */
245                 if ((portmask & (1 << port_id)) == 0)
246                         continue;
247
248                 rte_free(tx_buffer[port_id]);
249         }
250         return 0;
251 }
252
253 static inline int
254 configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
255 {
256         const uint8_t nb_ports = rte_eth_dev_count();
257         unsigned port_id;
258         int ret;
259
260         /* initialize buffers for all ports */
261         for (port_id = 0; port_id < nb_ports; port_id++) {
262                 /* skip ports that are not enabled */
263                 if ((portmask & (1 << port_id)) == 0)
264                         continue;
265
266                 /* Initialize TX buffers */
267                 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
268                                 RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
269                                 rte_eth_dev_socket_id(port_id));
270                 if (tx_buffer[port_id] == NULL)
271                         rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
272                                  port_id);
273
274                 rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
275
276                 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
277                                 flush_tx_error_callback, NULL);
278                 if (ret < 0)
279                         rte_exit(EXIT_FAILURE,
280                         "Cannot set error callback for tx buffer on port %u\n",
281                                  port_id);
282         }
283         return 0;
284 }
285
286 static inline int
287 configure_eth_port(uint16_t port_id)
288 {
289         struct ether_addr addr;
290         const uint16_t rxRings = 1, txRings = 1;
291         const uint8_t nb_ports = rte_eth_dev_count();
292         int ret;
293         uint16_t q;
294         uint16_t nb_rxd = RX_DESC_PER_QUEUE;
295         uint16_t nb_txd = TX_DESC_PER_QUEUE;
296
297         if (port_id > nb_ports)
298                 return -1;
299
300         ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
301         if (ret != 0)
302                 return ret;
303
304         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
305         if (ret != 0)
306                 return ret;
307
308         for (q = 0; q < rxRings; q++) {
309                 ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
310                                 rte_eth_dev_socket_id(port_id), NULL,
311                                 mbuf_pool);
312                 if (ret < 0)
313                         return ret;
314         }
315
316         for (q = 0; q < txRings; q++) {
317                 ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
318                                 rte_eth_dev_socket_id(port_id), NULL);
319                 if (ret < 0)
320                         return ret;
321         }
322
323         ret = rte_eth_dev_start(port_id);
324         if (ret < 0)
325                 return ret;
326
327         rte_eth_macaddr_get(port_id, &addr);
328         printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
329                         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
330                         port_id,
331                         addr.addr_bytes[0], addr.addr_bytes[1],
332                         addr.addr_bytes[2], addr.addr_bytes[3],
333                         addr.addr_bytes[4], addr.addr_bytes[5]);
334
335         rte_eth_promiscuous_enable(port_id);
336
337         return 0;
338 }
339
340 static void
341 print_stats(void)
342 {
343         const uint8_t nb_ports = rte_eth_dev_count();
344         unsigned i;
345         struct rte_eth_stats eth_stats;
346
347         printf("\nRX thread stats:\n");
348         printf(" - Pkts rxd:                            %"PRIu64"\n",
349                                                 app_stats.rx.rx_pkts);
350         printf(" - Pkts enqd to workers ring:           %"PRIu64"\n",
351                                                 app_stats.rx.enqueue_pkts);
352
353         printf("\nWorker thread stats:\n");
354         printf(" - Pkts deqd from workers ring:         %"PRIu64"\n",
355                                                 app_stats.wkr.dequeue_pkts);
356         printf(" - Pkts enqd to tx ring:                %"PRIu64"\n",
357                                                 app_stats.wkr.enqueue_pkts);
358         printf(" - Pkts enq to tx failed:               %"PRIu64"\n",
359                                                 app_stats.wkr.enqueue_failed_pkts);
360
361         printf("\nTX stats:\n");
362         printf(" - Pkts deqd from tx ring:              %"PRIu64"\n",
363                                                 app_stats.tx.dequeue_pkts);
364         printf(" - Ro Pkts transmitted:                 %"PRIu64"\n",
365                                                 app_stats.tx.ro_tx_pkts);
366         printf(" - Ro Pkts tx failed:                   %"PRIu64"\n",
367                                                 app_stats.tx.ro_tx_failed_pkts);
368         printf(" - Pkts transmitted w/o reorder:        %"PRIu64"\n",
369                                                 app_stats.tx.early_pkts_txtd_woro);
370         printf(" - Pkts tx failed w/o reorder:          %"PRIu64"\n",
371                                                 app_stats.tx.early_pkts_tx_failed_woro);
372
373         for (i = 0; i < nb_ports; i++) {
374                 rte_eth_stats_get(i, &eth_stats);
375                 printf("\nPort %u stats:\n", i);
376                 printf(" - Pkts in:   %"PRIu64"\n", eth_stats.ipackets);
377                 printf(" - Pkts out:  %"PRIu64"\n", eth_stats.opackets);
378                 printf(" - In Errs:   %"PRIu64"\n", eth_stats.ierrors);
379                 printf(" - Out Errs:  %"PRIu64"\n", eth_stats.oerrors);
380                 printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
381         }
382 }
383
384 static void
385 int_handler(int sig_num)
386 {
387         printf("Exiting on signal %d\n", sig_num);
388         quit_signal = 1;
389 }
390
391 /**
392  * This thread receives mbufs from the port and affects them an internal
393  * sequence number to keep track of their order of arrival through an
394  * mbuf structure.
395  * The mbufs are then passed to the worker threads via the rx_to_workers
396  * ring.
397  */
398 static int
399 rx_thread(struct rte_ring *ring_out)
400 {
401         const uint8_t nb_ports = rte_eth_dev_count();
402         uint32_t seqn = 0;
403         uint16_t i, ret = 0;
404         uint16_t nb_rx_pkts;
405         uint16_t port_id;
406         struct rte_mbuf *pkts[MAX_PKTS_BURST];
407
408         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
409                                                         rte_lcore_id());
410
411         while (!quit_signal) {
412
413                 for (port_id = 0; port_id < nb_ports; port_id++) {
414                         if ((portmask & (1 << port_id)) != 0) {
415
416                                 /* receive packets */
417                                 nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
418                                                                 pkts, MAX_PKTS_BURST);
419                                 if (nb_rx_pkts == 0) {
420                                         RTE_LOG_DP(DEBUG, REORDERAPP,
421                                         "%s():Received zero packets\n", __func__);
422                                         continue;
423                                 }
424                                 app_stats.rx.rx_pkts += nb_rx_pkts;
425
426                                 /* mark sequence number */
427                                 for (i = 0; i < nb_rx_pkts; )
428                                         pkts[i++]->seqn = seqn++;
429
430                                 /* enqueue to rx_to_workers ring */
431                                 ret = rte_ring_enqueue_burst(ring_out,
432                                                 (void *)pkts, nb_rx_pkts, NULL);
433                                 app_stats.rx.enqueue_pkts += ret;
434                                 if (unlikely(ret < nb_rx_pkts)) {
435                                         app_stats.rx.enqueue_failed_pkts +=
436                                                                         (nb_rx_pkts-ret);
437                                         pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
438                                 }
439                         }
440                 }
441         }
442         return 0;
443 }
444
445 /**
446  * This thread takes bursts of packets from the rx_to_workers ring and
447  * Changes the input port value to output port value. And feds it to
448  * workers_to_tx
449  */
450 static int
451 worker_thread(void *args_ptr)
452 {
453         const uint8_t nb_ports = rte_eth_dev_count();
454         uint16_t i, ret = 0;
455         uint16_t burst_size = 0;
456         struct worker_thread_args *args;
457         struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
458         struct rte_ring *ring_in, *ring_out;
459         const unsigned xor_val = (nb_ports > 1);
460
461         args = (struct worker_thread_args *) args_ptr;
462         ring_in  = args->ring_in;
463         ring_out = args->ring_out;
464
465         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
466                                                         rte_lcore_id());
467
468         while (!quit_signal) {
469
470                 /* dequeue the mbufs from rx_to_workers ring */
471                 burst_size = rte_ring_dequeue_burst(ring_in,
472                                 (void *)burst_buffer, MAX_PKTS_BURST, NULL);
473                 if (unlikely(burst_size == 0))
474                         continue;
475
476                 __sync_fetch_and_add(&app_stats.wkr.dequeue_pkts, burst_size);
477
478                 /* just do some operation on mbuf */
479                 for (i = 0; i < burst_size;)
480                         burst_buffer[i++]->port ^= xor_val;
481
482                 /* enqueue the modified mbufs to workers_to_tx ring */
483                 ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
484                                 burst_size, NULL);
485                 __sync_fetch_and_add(&app_stats.wkr.enqueue_pkts, ret);
486                 if (unlikely(ret < burst_size)) {
487                         /* Return the mbufs to their respective pool, dropping packets */
488                         __sync_fetch_and_add(&app_stats.wkr.enqueue_failed_pkts,
489                                         (int)burst_size - ret);
490                         pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
491                 }
492         }
493         return 0;
494 }
495
496 /**
497  * Dequeue mbufs from the workers_to_tx ring and reorder them before
498  * transmitting.
499  */
500 static int
501 send_thread(struct send_thread_args *args)
502 {
503         int ret;
504         unsigned int i, dret;
505         uint16_t nb_dq_mbufs;
506         uint8_t outp;
507         unsigned sent;
508         struct rte_mbuf *mbufs[MAX_PKTS_BURST];
509         struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
510         static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
511
512         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
513
514         configure_tx_buffers(tx_buffer);
515
516         while (!quit_signal) {
517
518                 /* deque the mbufs from workers_to_tx ring */
519                 nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
520                                 (void *)mbufs, MAX_PKTS_BURST, NULL);
521
522                 if (unlikely(nb_dq_mbufs == 0))
523                         continue;
524
525                 app_stats.tx.dequeue_pkts += nb_dq_mbufs;
526
527                 for (i = 0; i < nb_dq_mbufs; i++) {
528                         /* send dequeued mbufs for reordering */
529                         ret = rte_reorder_insert(args->buffer, mbufs[i]);
530
531                         if (ret == -1 && rte_errno == ERANGE) {
532                                 /* Too early pkts should be transmitted out directly */
533                                 RTE_LOG_DP(DEBUG, REORDERAPP,
534                                                 "%s():Cannot reorder early packet "
535                                                 "direct enqueuing to TX\n", __func__);
536                                 outp = mbufs[i]->port;
537                                 if ((portmask & (1 << outp)) == 0) {
538                                         rte_pktmbuf_free(mbufs[i]);
539                                         continue;
540                                 }
541                                 if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
542                                         rte_pktmbuf_free(mbufs[i]);
543                                         app_stats.tx.early_pkts_tx_failed_woro++;
544                                 } else
545                                         app_stats.tx.early_pkts_txtd_woro++;
546                         } else if (ret == -1 && rte_errno == ENOSPC) {
547                                 /**
548                                  * Early pkts just outside of window should be dropped
549                                  */
550                                 rte_pktmbuf_free(mbufs[i]);
551                         }
552                 }
553
554                 /*
555                  * drain MAX_PKTS_BURST of reordered
556                  * mbufs for transmit
557                  */
558                 dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
559                 for (i = 0; i < dret; i++) {
560
561                         struct rte_eth_dev_tx_buffer *outbuf;
562                         uint8_t outp1;
563
564                         outp1 = rombufs[i]->port;
565                         /* skip ports that are not enabled */
566                         if ((portmask & (1 << outp1)) == 0) {
567                                 rte_pktmbuf_free(rombufs[i]);
568                                 continue;
569                         }
570
571                         outbuf = tx_buffer[outp1];
572                         sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
573                         if (sent)
574                                 app_stats.tx.ro_tx_pkts += sent;
575                 }
576         }
577
578         free_tx_buffers(tx_buffer);
579
580         return 0;
581 }
582
583 /**
584  * Dequeue mbufs from the workers_to_tx ring and transmit them
585  */
586 static int
587 tx_thread(struct rte_ring *ring_in)
588 {
589         uint32_t i, dqnum;
590         uint8_t outp;
591         unsigned sent;
592         struct rte_mbuf *mbufs[MAX_PKTS_BURST];
593         struct rte_eth_dev_tx_buffer *outbuf;
594         static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
595
596         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
597                                                         rte_lcore_id());
598
599         configure_tx_buffers(tx_buffer);
600
601         while (!quit_signal) {
602
603                 /* deque the mbufs from workers_to_tx ring */
604                 dqnum = rte_ring_dequeue_burst(ring_in,
605                                 (void *)mbufs, MAX_PKTS_BURST, NULL);
606
607                 if (unlikely(dqnum == 0))
608                         continue;
609
610                 app_stats.tx.dequeue_pkts += dqnum;
611
612                 for (i = 0; i < dqnum; i++) {
613                         outp = mbufs[i]->port;
614                         /* skip ports that are not enabled */
615                         if ((portmask & (1 << outp)) == 0) {
616                                 rte_pktmbuf_free(mbufs[i]);
617                                 continue;
618                         }
619
620                         outbuf = tx_buffer[outp];
621                         sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
622                         if (sent)
623                                 app_stats.tx.ro_tx_pkts += sent;
624                 }
625         }
626
627         return 0;
628 }
629
630 int
631 main(int argc, char **argv)
632 {
633         int ret;
634         unsigned nb_ports;
635         unsigned int lcore_id, last_lcore_id, master_lcore_id;
636         uint16_t port_id;
637         uint16_t nb_ports_available;
638         struct worker_thread_args worker_args = {NULL, NULL};
639         struct send_thread_args send_args = {NULL, NULL};
640         struct rte_ring *rx_to_workers;
641         struct rte_ring *workers_to_tx;
642
643         /* catch ctrl-c so we can print on exit */
644         signal(SIGINT, int_handler);
645
646         /* Initialize EAL */
647         ret = rte_eal_init(argc, argv);
648         if (ret < 0)
649                 return -1;
650
651         argc -= ret;
652         argv += ret;
653
654         /* Parse the application specific arguments */
655         ret = parse_args(argc, argv);
656         if (ret < 0)
657                 return -1;
658
659         /* Check if we have enought cores */
660         if (rte_lcore_count() < 3)
661                 rte_exit(EXIT_FAILURE, "Error, This application needs at "
662                                 "least 3 logical cores to run:\n"
663                                 "1 lcore for packet RX\n"
664                                 "1 lcore for packet TX\n"
665                                 "and at least 1 lcore for worker threads\n");
666
667         nb_ports = rte_eth_dev_count();
668         if (nb_ports == 0)
669                 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
670         if (nb_ports != 1 && (nb_ports & 1))
671                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
672                                 "when using a single port\n");
673
674         mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
675                         MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
676                         rte_socket_id());
677         if (mbuf_pool == NULL)
678                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
679
680         nb_ports_available = nb_ports;
681
682         /* initialize all ports */
683         for (port_id = 0; port_id < nb_ports; port_id++) {
684                 /* skip ports that are not enabled */
685                 if ((portmask & (1 << port_id)) == 0) {
686                         printf("\nSkipping disabled port %d\n", port_id);
687                         nb_ports_available--;
688                         continue;
689                 }
690                 /* init port */
691                 printf("Initializing port %u... done\n", port_id);
692
693                 if (configure_eth_port(port_id) != 0)
694                         rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
695                                         port_id);
696         }
697
698         if (!nb_ports_available) {
699                 rte_exit(EXIT_FAILURE,
700                         "All available ports are disabled. Please set portmask.\n");
701         }
702
703         /* Create rings for inter core communication */
704         rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
705                         RING_F_SP_ENQ);
706         if (rx_to_workers == NULL)
707                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
708
709         workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
710                         RING_F_SC_DEQ);
711         if (workers_to_tx == NULL)
712                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
713
714         if (!disable_reorder) {
715                 send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
716                                 REORDER_BUFFER_SIZE);
717                 if (send_args.buffer == NULL)
718                         rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
719         }
720
721         last_lcore_id   = get_last_lcore_id();
722         master_lcore_id = rte_get_master_lcore();
723
724         worker_args.ring_in  = rx_to_workers;
725         worker_args.ring_out = workers_to_tx;
726
727         /* Start worker_thread() on all the available slave cores but the last 1 */
728         for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
729                 if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id)
730                         rte_eal_remote_launch(worker_thread, (void *)&worker_args,
731                                         lcore_id);
732
733         if (disable_reorder) {
734                 /* Start tx_thread() on the last slave core */
735                 rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
736                                 last_lcore_id);
737         } else {
738                 send_args.ring_in = workers_to_tx;
739                 /* Start send_thread() on the last slave core */
740                 rte_eal_remote_launch((lcore_function_t *)send_thread,
741                                 (void *)&send_args, last_lcore_id);
742         }
743
744         /* Start rx_thread() on the master core */
745         rx_thread(rx_to_workers);
746
747         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
748                 if (rte_eal_wait_lcore(lcore_id) < 0)
749                         return -1;
750         }
751
752         print_stats();
753         return 0;
754 }