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