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