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