examples: use SPDX tag for Intel copyright files
[dpdk.git] / examples / distributor / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <inttypes.h>
7 #include <unistd.h>
8 #include <signal.h>
9 #include <getopt.h>
10
11 #include <rte_eal.h>
12 #include <rte_ethdev.h>
13 #include <rte_cycles.h>
14 #include <rte_malloc.h>
15 #include <rte_debug.h>
16 #include <rte_prefetch.h>
17 #include <rte_distributor.h>
18 #include <rte_pause.h>
19
20 #define RX_RING_SIZE 512
21 #define TX_RING_SIZE 512
22 #define NUM_MBUFS ((64*1024)-1)
23 #define MBUF_CACHE_SIZE 128
24 #define BURST_SIZE 64
25 #define SCHED_RX_RING_SZ 8192
26 #define SCHED_TX_RING_SZ 65536
27 #define BURST_SIZE_TX 32
28
29 #define RTE_LOGTYPE_DISTRAPP RTE_LOGTYPE_USER1
30
31 #define ANSI_COLOR_RED     "\x1b[31m"
32 #define ANSI_COLOR_RESET   "\x1b[0m"
33
34 /* mask of enabled ports */
35 static uint32_t enabled_port_mask;
36 volatile uint8_t quit_signal;
37 volatile uint8_t quit_signal_rx;
38 volatile uint8_t quit_signal_dist;
39 volatile uint8_t quit_signal_work;
40
41 static volatile struct app_stats {
42         struct {
43                 uint64_t rx_pkts;
44                 uint64_t returned_pkts;
45                 uint64_t enqueued_pkts;
46                 uint64_t enqdrop_pkts;
47         } rx __rte_cache_aligned;
48         int pad1 __rte_cache_aligned;
49
50         struct {
51                 uint64_t in_pkts;
52                 uint64_t ret_pkts;
53                 uint64_t sent_pkts;
54                 uint64_t enqdrop_pkts;
55         } dist __rte_cache_aligned;
56         int pad2 __rte_cache_aligned;
57
58         struct {
59                 uint64_t dequeue_pkts;
60                 uint64_t tx_pkts;
61                 uint64_t enqdrop_pkts;
62         } tx __rte_cache_aligned;
63         int pad3 __rte_cache_aligned;
64
65         uint64_t worker_pkts[64] __rte_cache_aligned;
66
67         int pad4 __rte_cache_aligned;
68
69         uint64_t worker_bursts[64][8] __rte_cache_aligned;
70
71         int pad5 __rte_cache_aligned;
72
73         uint64_t port_rx_pkts[64] __rte_cache_aligned;
74         uint64_t port_tx_pkts[64] __rte_cache_aligned;
75 } app_stats;
76
77 struct app_stats prev_app_stats;
78
79 static const struct rte_eth_conf port_conf_default = {
80         .rxmode = {
81                 .mq_mode = ETH_MQ_RX_RSS,
82                 .max_rx_pkt_len = ETHER_MAX_LEN,
83         },
84         .txmode = {
85                 .mq_mode = ETH_MQ_TX_NONE,
86         },
87         .rx_adv_conf = {
88                 .rss_conf = {
89                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
90                                 ETH_RSS_TCP | ETH_RSS_SCTP,
91                 }
92         },
93 };
94
95 struct output_buffer {
96         unsigned count;
97         struct rte_mbuf *mbufs[BURST_SIZE];
98 };
99
100 static void print_stats(void);
101
102 /*
103  * Initialises a given port using global settings and with the rx buffers
104  * coming from the mbuf_pool passed as parameter
105  */
106 static inline int
107 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
108 {
109         struct rte_eth_conf port_conf = port_conf_default;
110         const uint16_t rxRings = 1, txRings = rte_lcore_count() - 1;
111         int retval;
112         uint16_t q;
113         uint16_t nb_rxd = RX_RING_SIZE;
114         uint16_t nb_txd = TX_RING_SIZE;
115
116         if (port >= rte_eth_dev_count())
117                 return -1;
118
119         retval = rte_eth_dev_configure(port, rxRings, txRings, &port_conf);
120         if (retval != 0)
121                 return retval;
122
123         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
124         if (retval != 0)
125                 return retval;
126
127         for (q = 0; q < rxRings; q++) {
128                 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
129                                                 rte_eth_dev_socket_id(port),
130                                                 NULL, mbuf_pool);
131                 if (retval < 0)
132                         return retval;
133         }
134
135         for (q = 0; q < txRings; q++) {
136                 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
137                                                 rte_eth_dev_socket_id(port),
138                                                 NULL);
139                 if (retval < 0)
140                         return retval;
141         }
142
143         retval = rte_eth_dev_start(port);
144         if (retval < 0)
145                 return retval;
146
147         struct rte_eth_link link;
148         rte_eth_link_get_nowait(port, &link);
149         while (!link.link_status) {
150                 printf("Waiting for Link up on port %"PRIu16"\n", port);
151                 sleep(1);
152                 rte_eth_link_get_nowait(port, &link);
153         }
154
155         if (!link.link_status) {
156                 printf("Link down on port %"PRIu16"\n", port);
157                 return 0;
158         }
159
160         struct ether_addr addr;
161         rte_eth_macaddr_get(port, &addr);
162         printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
163                         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
164                         port,
165                         addr.addr_bytes[0], addr.addr_bytes[1],
166                         addr.addr_bytes[2], addr.addr_bytes[3],
167                         addr.addr_bytes[4], addr.addr_bytes[5]);
168
169         rte_eth_promiscuous_enable(port);
170
171         return 0;
172 }
173
174 struct lcore_params {
175         unsigned worker_id;
176         struct rte_distributor *d;
177         struct rte_ring *rx_dist_ring;
178         struct rte_ring *dist_tx_ring;
179         struct rte_mempool *mem_pool;
180 };
181
182 static int
183 lcore_rx(struct lcore_params *p)
184 {
185         const uint16_t nb_ports = rte_eth_dev_count();
186         const int socket_id = rte_socket_id();
187         uint16_t port;
188         struct rte_mbuf *bufs[BURST_SIZE*2];
189
190         for (port = 0; port < nb_ports; port++) {
191                 /* skip ports that are not enabled */
192                 if ((enabled_port_mask & (1 << port)) == 0)
193                         continue;
194
195                 if (rte_eth_dev_socket_id(port) > 0 &&
196                                 rte_eth_dev_socket_id(port) != socket_id)
197                         printf("WARNING, port %u is on remote NUMA node to "
198                                         "RX thread.\n\tPerformance will not "
199                                         "be optimal.\n", port);
200         }
201
202         printf("\nCore %u doing packet RX.\n", rte_lcore_id());
203         port = 0;
204         while (!quit_signal_rx) {
205
206                 /* skip ports that are not enabled */
207                 if ((enabled_port_mask & (1 << port)) == 0) {
208                         if (++port == nb_ports)
209                                 port = 0;
210                         continue;
211                 }
212                 const uint16_t nb_rx = rte_eth_rx_burst(port, 0, bufs,
213                                 BURST_SIZE);
214                 if (unlikely(nb_rx == 0)) {
215                         if (++port == nb_ports)
216                                 port = 0;
217                         continue;
218                 }
219                 app_stats.rx.rx_pkts += nb_rx;
220
221 /*
222  * You can run the distributor on the rx core with this code. Returned
223  * packets are then send straight to the tx core.
224  */
225 #if 0
226         rte_distributor_process(d, bufs, nb_rx);
227         const uint16_t nb_ret = rte_distributor_returned_pktsd,
228                         bufs, BURST_SIZE*2);
229
230                 app_stats.rx.returned_pkts += nb_ret;
231                 if (unlikely(nb_ret == 0)) {
232                         if (++port == nb_ports)
233                                 port = 0;
234                         continue;
235                 }
236
237                 struct rte_ring *tx_ring = p->dist_tx_ring;
238                 uint16_t sent = rte_ring_enqueue_burst(tx_ring,
239                                 (void *)bufs, nb_ret, NULL);
240 #else
241                 uint16_t nb_ret = nb_rx;
242                 /*
243                  * Swap the following two lines if you want the rx traffic
244                  * to go directly to tx, no distribution.
245                  */
246                 struct rte_ring *out_ring = p->rx_dist_ring;
247                 /* struct rte_ring *out_ring = p->dist_tx_ring; */
248
249                 uint16_t sent = rte_ring_enqueue_burst(out_ring,
250                                 (void *)bufs, nb_ret, NULL);
251 #endif
252
253                 app_stats.rx.enqueued_pkts += sent;
254                 if (unlikely(sent < nb_ret)) {
255                         app_stats.rx.enqdrop_pkts +=  nb_ret - sent;
256                         RTE_LOG_DP(DEBUG, DISTRAPP,
257                                 "%s:Packet loss due to full ring\n", __func__);
258                         while (sent < nb_ret)
259                                 rte_pktmbuf_free(bufs[sent++]);
260                 }
261                 if (++port == nb_ports)
262                         port = 0;
263         }
264         /* set worker & tx threads quit flag */
265         printf("\nCore %u exiting rx task.\n", rte_lcore_id());
266         quit_signal = 1;
267         return 0;
268 }
269
270 static inline void
271 flush_one_port(struct output_buffer *outbuf, uint8_t outp)
272 {
273         unsigned int nb_tx = rte_eth_tx_burst(outp, 0,
274                         outbuf->mbufs, outbuf->count);
275         app_stats.tx.tx_pkts += outbuf->count;
276
277         if (unlikely(nb_tx < outbuf->count)) {
278                 app_stats.tx.enqdrop_pkts +=  outbuf->count - nb_tx;
279                 do {
280                         rte_pktmbuf_free(outbuf->mbufs[nb_tx]);
281                 } while (++nb_tx < outbuf->count);
282         }
283         outbuf->count = 0;
284 }
285
286 static inline void
287 flush_all_ports(struct output_buffer *tx_buffers, uint16_t nb_ports)
288 {
289         uint16_t outp;
290
291         for (outp = 0; outp < nb_ports; outp++) {
292                 /* skip ports that are not enabled */
293                 if ((enabled_port_mask & (1 << outp)) == 0)
294                         continue;
295
296                 if (tx_buffers[outp].count == 0)
297                         continue;
298
299                 flush_one_port(&tx_buffers[outp], outp);
300         }
301 }
302
303
304
305 static int
306 lcore_distributor(struct lcore_params *p)
307 {
308         struct rte_ring *in_r = p->rx_dist_ring;
309         struct rte_ring *out_r = p->dist_tx_ring;
310         struct rte_mbuf *bufs[BURST_SIZE * 4];
311         struct rte_distributor *d = p->d;
312
313         printf("\nCore %u acting as distributor core.\n", rte_lcore_id());
314         while (!quit_signal_dist) {
315                 const uint16_t nb_rx = rte_ring_dequeue_burst(in_r,
316                                 (void *)bufs, BURST_SIZE*1, NULL);
317                 if (nb_rx) {
318                         app_stats.dist.in_pkts += nb_rx;
319
320                         /* Distribute the packets */
321                         rte_distributor_process(d, bufs, nb_rx);
322                         /* Handle Returns */
323                         const uint16_t nb_ret =
324                                 rte_distributor_returned_pkts(d,
325                                         bufs, BURST_SIZE*2);
326
327                         if (unlikely(nb_ret == 0))
328                                 continue;
329                         app_stats.dist.ret_pkts += nb_ret;
330
331                         uint16_t sent = rte_ring_enqueue_burst(out_r,
332                                         (void *)bufs, nb_ret, NULL);
333                         app_stats.dist.sent_pkts += sent;
334                         if (unlikely(sent < nb_ret)) {
335                                 app_stats.dist.enqdrop_pkts += nb_ret - sent;
336                                 RTE_LOG(DEBUG, DISTRAPP,
337                                         "%s:Packet loss due to full out ring\n",
338                                         __func__);
339                                 while (sent < nb_ret)
340                                         rte_pktmbuf_free(bufs[sent++]);
341                         }
342                 }
343         }
344         printf("\nCore %u exiting distributor task.\n", rte_lcore_id());
345         quit_signal_work = 1;
346
347         rte_distributor_flush(d);
348         /* Unblock any returns so workers can exit */
349         rte_distributor_clear_returns(d);
350         quit_signal_rx = 1;
351         return 0;
352 }
353
354
355 static int
356 lcore_tx(struct rte_ring *in_r)
357 {
358         static struct output_buffer tx_buffers[RTE_MAX_ETHPORTS];
359         const uint16_t nb_ports = rte_eth_dev_count();
360         const int socket_id = rte_socket_id();
361         uint16_t port;
362
363         for (port = 0; port < nb_ports; port++) {
364                 /* skip ports that are not enabled */
365                 if ((enabled_port_mask & (1 << port)) == 0)
366                         continue;
367
368                 if (rte_eth_dev_socket_id(port) > 0 &&
369                                 rte_eth_dev_socket_id(port) != socket_id)
370                         printf("WARNING, port %u is on remote NUMA node to "
371                                         "TX thread.\n\tPerformance will not "
372                                         "be optimal.\n", port);
373         }
374
375         printf("\nCore %u doing packet TX.\n", rte_lcore_id());
376         while (!quit_signal) {
377
378                 for (port = 0; port < nb_ports; port++) {
379                         /* skip ports that are not enabled */
380                         if ((enabled_port_mask & (1 << port)) == 0)
381                                 continue;
382
383                         struct rte_mbuf *bufs[BURST_SIZE_TX];
384                         const uint16_t nb_rx = rte_ring_dequeue_burst(in_r,
385                                         (void *)bufs, BURST_SIZE_TX, NULL);
386                         app_stats.tx.dequeue_pkts += nb_rx;
387
388                         /* if we get no traffic, flush anything we have */
389                         if (unlikely(nb_rx == 0)) {
390                                 flush_all_ports(tx_buffers, nb_ports);
391                                 continue;
392                         }
393
394                         /* for traffic we receive, queue it up for transmit */
395                         uint16_t i;
396                         rte_prefetch_non_temporal((void *)bufs[0]);
397                         rte_prefetch_non_temporal((void *)bufs[1]);
398                         rte_prefetch_non_temporal((void *)bufs[2]);
399                         for (i = 0; i < nb_rx; i++) {
400                                 struct output_buffer *outbuf;
401                                 uint8_t outp;
402                                 rte_prefetch_non_temporal((void *)bufs[i + 3]);
403                                 /*
404                                  * workers should update in_port to hold the
405                                  * output port value
406                                  */
407                                 outp = bufs[i]->port;
408                                 /* skip ports that are not enabled */
409                                 if ((enabled_port_mask & (1 << outp)) == 0)
410                                         continue;
411
412                                 outbuf = &tx_buffers[outp];
413                                 outbuf->mbufs[outbuf->count++] = bufs[i];
414                                 if (outbuf->count == BURST_SIZE_TX)
415                                         flush_one_port(outbuf, outp);
416                         }
417                 }
418         }
419         printf("\nCore %u exiting tx task.\n", rte_lcore_id());
420         return 0;
421 }
422
423 static void
424 int_handler(int sig_num)
425 {
426         printf("Exiting on signal %d\n", sig_num);
427         /* set quit flag for rx thread to exit */
428         quit_signal_dist = 1;
429 }
430
431 static void
432 print_stats(void)
433 {
434         struct rte_eth_stats eth_stats;
435         unsigned int i, j;
436         const unsigned int num_workers = rte_lcore_count() - 4;
437
438         for (i = 0; i < rte_eth_dev_count(); i++) {
439                 rte_eth_stats_get(i, &eth_stats);
440                 app_stats.port_rx_pkts[i] = eth_stats.ipackets;
441                 app_stats.port_tx_pkts[i] = eth_stats.opackets;
442         }
443
444         printf("\n\nRX Thread:\n");
445         for (i = 0; i < rte_eth_dev_count(); i++) {
446                 printf("Port %u Pktsin : %5.2f\n", i,
447                                 (app_stats.port_rx_pkts[i] -
448                                 prev_app_stats.port_rx_pkts[i])/1000000.0);
449                 prev_app_stats.port_rx_pkts[i] = app_stats.port_rx_pkts[i];
450         }
451         printf(" - Received:    %5.2f\n",
452                         (app_stats.rx.rx_pkts -
453                         prev_app_stats.rx.rx_pkts)/1000000.0);
454         printf(" - Returned:    %5.2f\n",
455                         (app_stats.rx.returned_pkts -
456                         prev_app_stats.rx.returned_pkts)/1000000.0);
457         printf(" - Enqueued:    %5.2f\n",
458                         (app_stats.rx.enqueued_pkts -
459                         prev_app_stats.rx.enqueued_pkts)/1000000.0);
460         printf(" - Dropped:     %s%5.2f%s\n", ANSI_COLOR_RED,
461                         (app_stats.rx.enqdrop_pkts -
462                         prev_app_stats.rx.enqdrop_pkts)/1000000.0,
463                         ANSI_COLOR_RESET);
464
465         printf("Distributor thread:\n");
466         printf(" - In:          %5.2f\n",
467                         (app_stats.dist.in_pkts -
468                         prev_app_stats.dist.in_pkts)/1000000.0);
469         printf(" - Returned:    %5.2f\n",
470                         (app_stats.dist.ret_pkts -
471                         prev_app_stats.dist.ret_pkts)/1000000.0);
472         printf(" - Sent:        %5.2f\n",
473                         (app_stats.dist.sent_pkts -
474                         prev_app_stats.dist.sent_pkts)/1000000.0);
475         printf(" - Dropped      %s%5.2f%s\n", ANSI_COLOR_RED,
476                         (app_stats.dist.enqdrop_pkts -
477                         prev_app_stats.dist.enqdrop_pkts)/1000000.0,
478                         ANSI_COLOR_RESET);
479
480         printf("TX thread:\n");
481         printf(" - Dequeued:    %5.2f\n",
482                         (app_stats.tx.dequeue_pkts -
483                         prev_app_stats.tx.dequeue_pkts)/1000000.0);
484         for (i = 0; i < rte_eth_dev_count(); i++) {
485                 printf("Port %u Pktsout: %5.2f\n",
486                                 i, (app_stats.port_tx_pkts[i] -
487                                 prev_app_stats.port_tx_pkts[i])/1000000.0);
488                 prev_app_stats.port_tx_pkts[i] = app_stats.port_tx_pkts[i];
489         }
490         printf(" - Transmitted: %5.2f\n",
491                         (app_stats.tx.tx_pkts -
492                         prev_app_stats.tx.tx_pkts)/1000000.0);
493         printf(" - Dropped:     %s%5.2f%s\n", ANSI_COLOR_RED,
494                         (app_stats.tx.enqdrop_pkts -
495                         prev_app_stats.tx.enqdrop_pkts)/1000000.0,
496                         ANSI_COLOR_RESET);
497
498         prev_app_stats.rx.rx_pkts = app_stats.rx.rx_pkts;
499         prev_app_stats.rx.returned_pkts = app_stats.rx.returned_pkts;
500         prev_app_stats.rx.enqueued_pkts = app_stats.rx.enqueued_pkts;
501         prev_app_stats.rx.enqdrop_pkts = app_stats.rx.enqdrop_pkts;
502         prev_app_stats.dist.in_pkts = app_stats.dist.in_pkts;
503         prev_app_stats.dist.ret_pkts = app_stats.dist.ret_pkts;
504         prev_app_stats.dist.sent_pkts = app_stats.dist.sent_pkts;
505         prev_app_stats.dist.enqdrop_pkts = app_stats.dist.enqdrop_pkts;
506         prev_app_stats.tx.dequeue_pkts = app_stats.tx.dequeue_pkts;
507         prev_app_stats.tx.tx_pkts = app_stats.tx.tx_pkts;
508         prev_app_stats.tx.enqdrop_pkts = app_stats.tx.enqdrop_pkts;
509
510         for (i = 0; i < num_workers; i++) {
511                 printf("Worker %02u Pkts: %5.2f. Bursts(1-8): ", i,
512                                 (app_stats.worker_pkts[i] -
513                                 prev_app_stats.worker_pkts[i])/1000000.0);
514                 for (j = 0; j < 8; j++) {
515                         printf("%"PRIu64" ", app_stats.worker_bursts[i][j]);
516                         app_stats.worker_bursts[i][j] = 0;
517                 }
518                 printf("\n");
519                 prev_app_stats.worker_pkts[i] = app_stats.worker_pkts[i];
520         }
521 }
522
523 static int
524 lcore_worker(struct lcore_params *p)
525 {
526         struct rte_distributor *d = p->d;
527         const unsigned id = p->worker_id;
528         unsigned int num = 0;
529         unsigned int i;
530
531         /*
532          * for single port, xor_val will be zero so we won't modify the output
533          * port, otherwise we send traffic from 0 to 1, 2 to 3, and vice versa
534          */
535         const unsigned xor_val = (rte_eth_dev_count() > 1);
536         struct rte_mbuf *buf[8] __rte_cache_aligned;
537
538         for (i = 0; i < 8; i++)
539                 buf[i] = NULL;
540
541         app_stats.worker_pkts[p->worker_id] = 1;
542
543         printf("\nCore %u acting as worker core.\n", rte_lcore_id());
544         while (!quit_signal_work) {
545                 num = rte_distributor_get_pkt(d, id, buf, buf, num);
546                 /* Do a little bit of work for each packet */
547                 for (i = 0; i < num; i++) {
548                         uint64_t t = rte_rdtsc()+100;
549
550                         while (rte_rdtsc() < t)
551                                 rte_pause();
552                         buf[i]->port ^= xor_val;
553                 }
554
555                 app_stats.worker_pkts[p->worker_id] += num;
556                 if (num > 0)
557                         app_stats.worker_bursts[p->worker_id][num-1]++;
558         }
559         return 0;
560 }
561
562 /* display usage */
563 static void
564 print_usage(const char *prgname)
565 {
566         printf("%s [EAL options] -- -p PORTMASK\n"
567                         "  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
568                         prgname);
569 }
570
571 static int
572 parse_portmask(const char *portmask)
573 {
574         char *end = NULL;
575         unsigned long pm;
576
577         /* parse hexadecimal string */
578         pm = strtoul(portmask, &end, 16);
579         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
580                 return -1;
581
582         if (pm == 0)
583                 return -1;
584
585         return pm;
586 }
587
588 /* Parse the argument given in the command line of the application */
589 static int
590 parse_args(int argc, char **argv)
591 {
592         int opt;
593         char **argvopt;
594         int option_index;
595         char *prgname = argv[0];
596         static struct option lgopts[] = {
597                 {NULL, 0, 0, 0}
598         };
599
600         argvopt = argv;
601
602         while ((opt = getopt_long(argc, argvopt, "p:",
603                         lgopts, &option_index)) != EOF) {
604
605                 switch (opt) {
606                 /* portmask */
607                 case 'p':
608                         enabled_port_mask = parse_portmask(optarg);
609                         if (enabled_port_mask == 0) {
610                                 printf("invalid portmask\n");
611                                 print_usage(prgname);
612                                 return -1;
613                         }
614                         break;
615
616                 default:
617                         print_usage(prgname);
618                         return -1;
619                 }
620         }
621
622         if (optind <= 1) {
623                 print_usage(prgname);
624                 return -1;
625         }
626
627         argv[optind-1] = prgname;
628
629         optind = 1; /* reset getopt lib */
630         return 0;
631 }
632
633 /* Main function, does initialization and calls the per-lcore functions */
634 int
635 main(int argc, char *argv[])
636 {
637         struct rte_mempool *mbuf_pool;
638         struct rte_distributor *d;
639         struct rte_ring *dist_tx_ring;
640         struct rte_ring *rx_dist_ring;
641         unsigned lcore_id, worker_id = 0;
642         unsigned nb_ports;
643         uint16_t portid;
644         uint16_t nb_ports_available;
645         uint64_t t, freq;
646
647         /* catch ctrl-c so we can print on exit */
648         signal(SIGINT, int_handler);
649
650         /* init EAL */
651         int ret = rte_eal_init(argc, argv);
652         if (ret < 0)
653                 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
654         argc -= ret;
655         argv += ret;
656
657         /* parse application arguments (after the EAL ones) */
658         ret = parse_args(argc, argv);
659         if (ret < 0)
660                 rte_exit(EXIT_FAILURE, "Invalid distributor parameters\n");
661
662         if (rte_lcore_count() < 5)
663                 rte_exit(EXIT_FAILURE, "Error, This application needs at "
664                                 "least 5 logical cores to run:\n"
665                                 "1 lcore for stats (can be core 0)\n"
666                                 "1 lcore for packet RX\n"
667                                 "1 lcore for distribution\n"
668                                 "1 lcore for packet TX\n"
669                                 "and at least 1 lcore for worker threads\n");
670
671         nb_ports = rte_eth_dev_count();
672         if (nb_ports == 0)
673                 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
674         if (nb_ports != 1 && (nb_ports & 1))
675                 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
676                                 "when using a single port\n");
677
678         mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
679                 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
680                 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
681         if (mbuf_pool == NULL)
682                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
683         nb_ports_available = nb_ports;
684
685         /* initialize all ports */
686         for (portid = 0; portid < nb_ports; portid++) {
687                 /* skip ports that are not enabled */
688                 if ((enabled_port_mask & (1 << portid)) == 0) {
689                         printf("\nSkipping disabled port %d\n", portid);
690                         nb_ports_available--;
691                         continue;
692                 }
693                 /* init port */
694                 printf("Initializing port %u... done\n", portid);
695
696                 if (port_init(portid, mbuf_pool) != 0)
697                         rte_exit(EXIT_FAILURE, "Cannot initialize port %u\n",
698                                         portid);
699         }
700
701         if (!nb_ports_available) {
702                 rte_exit(EXIT_FAILURE,
703                                 "All available ports are disabled. Please set portmask.\n");
704         }
705
706         d = rte_distributor_create("PKT_DIST", rte_socket_id(),
707                         rte_lcore_count() - 4,
708                         RTE_DIST_ALG_BURST);
709         if (d == NULL)
710                 rte_exit(EXIT_FAILURE, "Cannot create distributor\n");
711
712         /*
713          * scheduler ring is read by the transmitter core, and written to
714          * by scheduler core
715          */
716         dist_tx_ring = rte_ring_create("Output_ring", SCHED_TX_RING_SZ,
717                         rte_socket_id(), RING_F_SC_DEQ | RING_F_SP_ENQ);
718         if (dist_tx_ring == NULL)
719                 rte_exit(EXIT_FAILURE, "Cannot create output ring\n");
720
721         rx_dist_ring = rte_ring_create("Input_ring", SCHED_RX_RING_SZ,
722                         rte_socket_id(), RING_F_SC_DEQ | RING_F_SP_ENQ);
723         if (rx_dist_ring == NULL)
724                 rte_exit(EXIT_FAILURE, "Cannot create output ring\n");
725
726         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
727                 if (worker_id == rte_lcore_count() - 3) {
728                         printf("Starting distributor on lcore_id %d\n",
729                                         lcore_id);
730                         /* distributor core */
731                         struct lcore_params *p =
732                                         rte_malloc(NULL, sizeof(*p), 0);
733                         if (!p)
734                                 rte_panic("malloc failure\n");
735                         *p = (struct lcore_params){worker_id, d,
736                                 rx_dist_ring, dist_tx_ring, mbuf_pool};
737                         rte_eal_remote_launch(
738                                 (lcore_function_t *)lcore_distributor,
739                                 p, lcore_id);
740                 } else if (worker_id == rte_lcore_count() - 4) {
741                         printf("Starting tx  on worker_id %d, lcore_id %d\n",
742                                         worker_id, lcore_id);
743                         /* tx core */
744                         rte_eal_remote_launch((lcore_function_t *)lcore_tx,
745                                         dist_tx_ring, lcore_id);
746                 } else if (worker_id == rte_lcore_count() - 2) {
747                         printf("Starting rx on worker_id %d, lcore_id %d\n",
748                                         worker_id, lcore_id);
749                         /* rx core */
750                         struct lcore_params *p =
751                                         rte_malloc(NULL, sizeof(*p), 0);
752                         if (!p)
753                                 rte_panic("malloc failure\n");
754                         *p = (struct lcore_params){worker_id, d, rx_dist_ring,
755                                         dist_tx_ring, mbuf_pool};
756                         rte_eal_remote_launch((lcore_function_t *)lcore_rx,
757                                         p, lcore_id);
758                 } else {
759                         printf("Starting worker on worker_id %d, lcore_id %d\n",
760                                         worker_id, lcore_id);
761                         struct lcore_params *p =
762                                         rte_malloc(NULL, sizeof(*p), 0);
763                         if (!p)
764                                 rte_panic("malloc failure\n");
765                         *p = (struct lcore_params){worker_id, d, rx_dist_ring,
766                                         dist_tx_ring, mbuf_pool};
767
768                         rte_eal_remote_launch((lcore_function_t *)lcore_worker,
769                                         p, lcore_id);
770                 }
771                 worker_id++;
772         }
773
774         freq = rte_get_timer_hz();
775         t = rte_rdtsc() + freq;
776         while (!quit_signal_dist) {
777                 if (t < rte_rdtsc()) {
778                         print_stats();
779                         t = rte_rdtsc() + freq;
780                 }
781                 usleep(1000);
782         }
783
784         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
785                 if (rte_eal_wait_lcore(lcore_id) < 0)
786                         return -1;
787         }
788
789         print_stats();
790         return 0;
791 }