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