132f582a9539c390a69b3cdbddb222872be63271
[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         ret = rte_eth_dev_info_get(port_id, &dev_info);
287         if (ret != 0) {
288                 printf("Error during getting device (port %u) info: %s\n",
289                                 port_id, strerror(-ret));
290                 return ret;
291         }
292
293         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
294                 port_conf.txmode.offloads |=
295                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
296         ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
297         if (ret != 0)
298                 return ret;
299
300         ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
301         if (ret != 0)
302                 return ret;
303
304         for (q = 0; q < rxRings; q++) {
305                 ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
306                                 rte_eth_dev_socket_id(port_id), NULL,
307                                 mbuf_pool);
308                 if (ret < 0)
309                         return ret;
310         }
311
312         txconf = dev_info.default_txconf;
313         txconf.offloads = port_conf.txmode.offloads;
314         for (q = 0; q < txRings; q++) {
315                 ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
316                                 rte_eth_dev_socket_id(port_id), &txconf);
317                 if (ret < 0)
318                         return ret;
319         }
320
321         ret = rte_eth_dev_start(port_id);
322         if (ret < 0)
323                 return ret;
324
325         rte_eth_macaddr_get(port_id, &addr);
326         printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
327                         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
328                         port_id,
329                         addr.addr_bytes[0], addr.addr_bytes[1],
330                         addr.addr_bytes[2], addr.addr_bytes[3],
331                         addr.addr_bytes[4], addr.addr_bytes[5]);
332
333         ret = rte_eth_promiscuous_enable(port_id);
334         if (ret != 0)
335                 return ret;
336
337         return 0;
338 }
339
340 static void
341 print_stats(void)
342 {
343         uint16_t i;
344         struct rte_eth_stats eth_stats;
345         unsigned int lcore_id, last_lcore_id, master_lcore_id, end_w_lcore_id;
346
347         last_lcore_id   = get_last_lcore_id();
348         master_lcore_id = rte_get_master_lcore();
349         end_w_lcore_id  = get_previous_lcore_id(last_lcore_id);
350
351         printf("\nRX thread stats:\n");
352         printf(" - Pkts rxd:                            %"PRIu64"\n",
353                                                 app_stats.rx.rx_pkts);
354         printf(" - Pkts enqd to workers ring:           %"PRIu64"\n",
355                                                 app_stats.rx.enqueue_pkts);
356
357         for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
358                 if (insight_worker
359                         && rte_lcore_is_enabled(lcore_id)
360                         && lcore_id != master_lcore_id) {
361                         printf("\nWorker thread stats on core [%u]:\n",
362                                         lcore_id);
363                         printf(" - Pkts deqd from workers ring:         %"PRIu64"\n",
364                                         wkr_stats[lcore_id].deq_pkts);
365                         printf(" - Pkts enqd to tx ring:                %"PRIu64"\n",
366                                         wkr_stats[lcore_id].enq_pkts);
367                         printf(" - Pkts enq to tx failed:               %"PRIu64"\n",
368                                         wkr_stats[lcore_id].enq_failed_pkts);
369                 }
370
371                 app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
372                 app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
373                 app_stats.wkr.enqueue_failed_pkts +=
374                         wkr_stats[lcore_id].enq_failed_pkts;
375         }
376
377         printf("\nWorker thread stats:\n");
378         printf(" - Pkts deqd from workers ring:         %"PRIu64"\n",
379                                                 app_stats.wkr.dequeue_pkts);
380         printf(" - Pkts enqd to tx ring:                %"PRIu64"\n",
381                                                 app_stats.wkr.enqueue_pkts);
382         printf(" - Pkts enq to tx failed:               %"PRIu64"\n",
383                                                 app_stats.wkr.enqueue_failed_pkts);
384
385         printf("\nTX stats:\n");
386         printf(" - Pkts deqd from tx ring:              %"PRIu64"\n",
387                                                 app_stats.tx.dequeue_pkts);
388         printf(" - Ro Pkts transmitted:                 %"PRIu64"\n",
389                                                 app_stats.tx.ro_tx_pkts);
390         printf(" - Ro Pkts tx failed:                   %"PRIu64"\n",
391                                                 app_stats.tx.ro_tx_failed_pkts);
392         printf(" - Pkts transmitted w/o reorder:        %"PRIu64"\n",
393                                                 app_stats.tx.early_pkts_txtd_woro);
394         printf(" - Pkts tx failed w/o reorder:          %"PRIu64"\n",
395                                                 app_stats.tx.early_pkts_tx_failed_woro);
396
397         RTE_ETH_FOREACH_DEV(i) {
398                 rte_eth_stats_get(i, &eth_stats);
399                 printf("\nPort %u stats:\n", i);
400                 printf(" - Pkts in:   %"PRIu64"\n", eth_stats.ipackets);
401                 printf(" - Pkts out:  %"PRIu64"\n", eth_stats.opackets);
402                 printf(" - In Errs:   %"PRIu64"\n", eth_stats.ierrors);
403                 printf(" - Out Errs:  %"PRIu64"\n", eth_stats.oerrors);
404                 printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
405         }
406 }
407
408 static void
409 int_handler(int sig_num)
410 {
411         printf("Exiting on signal %d\n", sig_num);
412         quit_signal = 1;
413 }
414
415 /**
416  * This thread receives mbufs from the port and affects them an internal
417  * sequence number to keep track of their order of arrival through an
418  * mbuf structure.
419  * The mbufs are then passed to the worker threads via the rx_to_workers
420  * ring.
421  */
422 static int
423 rx_thread(struct rte_ring *ring_out)
424 {
425         uint32_t seqn = 0;
426         uint16_t i, ret = 0;
427         uint16_t nb_rx_pkts;
428         uint16_t port_id;
429         struct rte_mbuf *pkts[MAX_PKTS_BURST];
430
431         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
432                                                         rte_lcore_id());
433
434         while (!quit_signal) {
435
436                 RTE_ETH_FOREACH_DEV(port_id) {
437                         if ((portmask & (1 << port_id)) != 0) {
438
439                                 /* receive packets */
440                                 nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
441                                                                 pkts, MAX_PKTS_BURST);
442                                 if (nb_rx_pkts == 0) {
443                                         RTE_LOG_DP(DEBUG, REORDERAPP,
444                                         "%s():Received zero packets\n", __func__);
445                                         continue;
446                                 }
447                                 app_stats.rx.rx_pkts += nb_rx_pkts;
448
449                                 /* mark sequence number */
450                                 for (i = 0; i < nb_rx_pkts; )
451                                         pkts[i++]->seqn = seqn++;
452
453                                 /* enqueue to rx_to_workers ring */
454                                 ret = rte_ring_enqueue_burst(ring_out,
455                                                 (void *)pkts, nb_rx_pkts, NULL);
456                                 app_stats.rx.enqueue_pkts += ret;
457                                 if (unlikely(ret < nb_rx_pkts)) {
458                                         app_stats.rx.enqueue_failed_pkts +=
459                                                                         (nb_rx_pkts-ret);
460                                         pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
461                                 }
462                         }
463                 }
464         }
465         return 0;
466 }
467
468 /**
469  * This thread takes bursts of packets from the rx_to_workers ring and
470  * Changes the input port value to output port value. And feds it to
471  * workers_to_tx
472  */
473 static int
474 worker_thread(void *args_ptr)
475 {
476         const uint16_t nb_ports = rte_eth_dev_count_avail();
477         uint16_t i, ret = 0;
478         uint16_t burst_size = 0;
479         struct worker_thread_args *args;
480         struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
481         struct rte_ring *ring_in, *ring_out;
482         const unsigned xor_val = (nb_ports > 1);
483         unsigned int core_id = rte_lcore_id();
484
485         args = (struct worker_thread_args *) args_ptr;
486         ring_in  = args->ring_in;
487         ring_out = args->ring_out;
488
489         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
490                                                         core_id);
491
492         while (!quit_signal) {
493
494                 /* dequeue the mbufs from rx_to_workers ring */
495                 burst_size = rte_ring_dequeue_burst(ring_in,
496                                 (void *)burst_buffer, MAX_PKTS_BURST, NULL);
497                 if (unlikely(burst_size == 0))
498                         continue;
499
500                 wkr_stats[core_id].deq_pkts += burst_size;
501
502                 /* just do some operation on mbuf */
503                 for (i = 0; i < burst_size;)
504                         burst_buffer[i++]->port ^= xor_val;
505
506                 /* enqueue the modified mbufs to workers_to_tx ring */
507                 ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
508                                 burst_size, NULL);
509                 wkr_stats[core_id].enq_pkts += ret;
510                 if (unlikely(ret < burst_size)) {
511                         /* Return the mbufs to their respective pool, dropping packets */
512                         wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
513                         pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
514                 }
515         }
516         return 0;
517 }
518
519 /**
520  * Dequeue mbufs from the workers_to_tx ring and reorder them before
521  * transmitting.
522  */
523 static int
524 send_thread(struct send_thread_args *args)
525 {
526         int ret;
527         unsigned int i, dret;
528         uint16_t nb_dq_mbufs;
529         uint8_t outp;
530         unsigned sent;
531         struct rte_mbuf *mbufs[MAX_PKTS_BURST];
532         struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
533         static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
534
535         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
536
537         configure_tx_buffers(tx_buffer);
538
539         while (!quit_signal) {
540
541                 /* deque the mbufs from workers_to_tx ring */
542                 nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
543                                 (void *)mbufs, MAX_PKTS_BURST, NULL);
544
545                 if (unlikely(nb_dq_mbufs == 0))
546                         continue;
547
548                 app_stats.tx.dequeue_pkts += nb_dq_mbufs;
549
550                 for (i = 0; i < nb_dq_mbufs; i++) {
551                         /* send dequeued mbufs for reordering */
552                         ret = rte_reorder_insert(args->buffer, mbufs[i]);
553
554                         if (ret == -1 && rte_errno == ERANGE) {
555                                 /* Too early pkts should be transmitted out directly */
556                                 RTE_LOG_DP(DEBUG, REORDERAPP,
557                                                 "%s():Cannot reorder early packet "
558                                                 "direct enqueuing to TX\n", __func__);
559                                 outp = mbufs[i]->port;
560                                 if ((portmask & (1 << outp)) == 0) {
561                                         rte_pktmbuf_free(mbufs[i]);
562                                         continue;
563                                 }
564                                 if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
565                                         rte_pktmbuf_free(mbufs[i]);
566                                         app_stats.tx.early_pkts_tx_failed_woro++;
567                                 } else
568                                         app_stats.tx.early_pkts_txtd_woro++;
569                         } else if (ret == -1 && rte_errno == ENOSPC) {
570                                 /**
571                                  * Early pkts just outside of window should be dropped
572                                  */
573                                 rte_pktmbuf_free(mbufs[i]);
574                         }
575                 }
576
577                 /*
578                  * drain MAX_PKTS_BURST of reordered
579                  * mbufs for transmit
580                  */
581                 dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
582                 for (i = 0; i < dret; i++) {
583
584                         struct rte_eth_dev_tx_buffer *outbuf;
585                         uint8_t outp1;
586
587                         outp1 = rombufs[i]->port;
588                         /* skip ports that are not enabled */
589                         if ((portmask & (1 << outp1)) == 0) {
590                                 rte_pktmbuf_free(rombufs[i]);
591                                 continue;
592                         }
593
594                         outbuf = tx_buffer[outp1];
595                         sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
596                         if (sent)
597                                 app_stats.tx.ro_tx_pkts += sent;
598                 }
599         }
600
601         free_tx_buffers(tx_buffer);
602
603         return 0;
604 }
605
606 /**
607  * Dequeue mbufs from the workers_to_tx ring and transmit them
608  */
609 static int
610 tx_thread(struct rte_ring *ring_in)
611 {
612         uint32_t i, dqnum;
613         uint8_t outp;
614         unsigned sent;
615         struct rte_mbuf *mbufs[MAX_PKTS_BURST];
616         struct rte_eth_dev_tx_buffer *outbuf;
617         static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
618
619         RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
620                                                         rte_lcore_id());
621
622         configure_tx_buffers(tx_buffer);
623
624         while (!quit_signal) {
625
626                 /* deque the mbufs from workers_to_tx ring */
627                 dqnum = rte_ring_dequeue_burst(ring_in,
628                                 (void *)mbufs, MAX_PKTS_BURST, NULL);
629
630                 if (unlikely(dqnum == 0))
631                         continue;
632
633                 app_stats.tx.dequeue_pkts += dqnum;
634
635                 for (i = 0; i < dqnum; i++) {
636                         outp = mbufs[i]->port;
637                         /* skip ports that are not enabled */
638                         if ((portmask & (1 << outp)) == 0) {
639                                 rte_pktmbuf_free(mbufs[i]);
640                                 continue;
641                         }
642
643                         outbuf = tx_buffer[outp];
644                         sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
645                         if (sent)
646                                 app_stats.tx.ro_tx_pkts += sent;
647                 }
648         }
649
650         return 0;
651 }
652
653 int
654 main(int argc, char **argv)
655 {
656         int ret;
657         unsigned nb_ports;
658         unsigned int lcore_id, last_lcore_id, master_lcore_id;
659         uint16_t port_id;
660         uint16_t nb_ports_available;
661         struct worker_thread_args worker_args = {NULL, NULL};
662         struct send_thread_args send_args = {NULL, NULL};
663         struct rte_ring *rx_to_workers;
664         struct rte_ring *workers_to_tx;
665
666         /* catch ctrl-c so we can print on exit */
667         signal(SIGINT, int_handler);
668
669         /* Initialize EAL */
670         ret = rte_eal_init(argc, argv);
671         if (ret < 0)
672                 return -1;
673
674         argc -= ret;
675         argv += ret;
676
677         /* Parse the application specific arguments */
678         ret = parse_args(argc, argv);
679         if (ret < 0)
680                 return -1;
681
682         /* Check if we have enought cores */
683         if (rte_lcore_count() < 3)
684                 rte_exit(EXIT_FAILURE, "Error, This application needs at "
685                                 "least 3 logical cores to run:\n"
686                                 "1 lcore for packet RX\n"
687                                 "1 lcore for packet TX\n"
688                                 "and at least 1 lcore for worker threads\n");
689
690         nb_ports = rte_eth_dev_count_avail();
691         if (nb_ports == 0)
692                 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
693         if (nb_ports != 1 && (nb_ports & 1))
694                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
695                                 "when using a single port\n");
696
697         mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
698                         MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
699                         rte_socket_id());
700         if (mbuf_pool == NULL)
701                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
702
703         nb_ports_available = nb_ports;
704
705         /* initialize all ports */
706         RTE_ETH_FOREACH_DEV(port_id) {
707                 /* skip ports that are not enabled */
708                 if ((portmask & (1 << port_id)) == 0) {
709                         printf("\nSkipping disabled port %d\n", port_id);
710                         nb_ports_available--;
711                         continue;
712                 }
713                 /* init port */
714                 printf("Initializing port %u... done\n", port_id);
715
716                 if (configure_eth_port(port_id) != 0)
717                         rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
718                                         port_id);
719         }
720
721         if (!nb_ports_available) {
722                 rte_exit(EXIT_FAILURE,
723                         "All available ports are disabled. Please set portmask.\n");
724         }
725
726         /* Create rings for inter core communication */
727         rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
728                         RING_F_SP_ENQ);
729         if (rx_to_workers == NULL)
730                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
731
732         workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
733                         RING_F_SC_DEQ);
734         if (workers_to_tx == NULL)
735                 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
736
737         if (!disable_reorder) {
738                 send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
739                                 REORDER_BUFFER_SIZE);
740                 if (send_args.buffer == NULL)
741                         rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
742         }
743
744         last_lcore_id   = get_last_lcore_id();
745         master_lcore_id = rte_get_master_lcore();
746
747         worker_args.ring_in  = rx_to_workers;
748         worker_args.ring_out = workers_to_tx;
749
750         /* Start worker_thread() on all the available slave cores but the last 1 */
751         for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
752                 if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id)
753                         rte_eal_remote_launch(worker_thread, (void *)&worker_args,
754                                         lcore_id);
755
756         if (disable_reorder) {
757                 /* Start tx_thread() on the last slave core */
758                 rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
759                                 last_lcore_id);
760         } else {
761                 send_args.ring_in = workers_to_tx;
762                 /* Start send_thread() on the last slave core */
763                 rte_eal_remote_launch((lcore_function_t *)send_thread,
764                                 (void *)&send_args, last_lcore_id);
765         }
766
767         /* Start rx_thread() on the master core */
768         rx_thread(rx_to_workers);
769
770         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
771                 if (rte_eal_wait_lcore(lcore_id) < 0)
772                         return -1;
773         }
774
775         print_stats();
776         return 0;
777 }