examples/l2fwd-event: add infra for eventdev
[dpdk.git] / examples / l2fwd-event / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include "l2fwd_event.h"
6 #include "l2fwd_poll.h"
7
8 /* display usage */
9 static void
10 l2fwd_event_usage(const char *prgname)
11 {
12         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
13                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
14                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
15                "  -T PERIOD: statistics will be refreshed each PERIOD seconds "
16                "                (0 to disable, 10 default, 86400 maximum)\n"
17                "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
18                "      When enabled:\n"
19                "       - The source MAC address is replaced by the TX port MAC address\n"
20                "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
21                "  --mode: Packet transfer mode for I/O, poll or eventdev\n"
22                "          Default mode = eventdev\n"
23                "  --eventq-sched: Event queue schedule type, ordered, atomic or parallel.\n"
24                "                  Default: atomic\n"
25                "                  Valid only if --mode=eventdev\n\n",
26                prgname);
27 }
28
29 static int
30 l2fwd_event_parse_portmask(const char *portmask)
31 {
32         char *end = NULL;
33         unsigned long pm;
34
35         /* parse hexadecimal string */
36         pm = strtoul(portmask, &end, 16);
37         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
38                 return -1;
39
40         if (pm == 0)
41                 return -1;
42
43         return pm;
44 }
45
46 static unsigned int
47 l2fwd_event_parse_nqueue(const char *q_arg)
48 {
49         char *end = NULL;
50         unsigned long n;
51
52         /* parse hexadecimal string */
53         n = strtoul(q_arg, &end, 10);
54         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
55                 return 0;
56         if (n == 0)
57                 return 0;
58         if (n >= MAX_RX_QUEUE_PER_LCORE)
59                 return 0;
60
61         return n;
62 }
63
64 static int
65 l2fwd_event_parse_timer_period(const char *q_arg)
66 {
67         char *end = NULL;
68         int n;
69
70         /* parse number string */
71         n = strtol(q_arg, &end, 10);
72         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
73                 return -1;
74         if (n >= MAX_TIMER_PERIOD)
75                 return -1;
76
77         return n;
78 }
79
80 static void
81 l2fwd_event_parse_mode(const char *optarg,
82                        struct l2fwd_resources *rsrc)
83 {
84         if (!strncmp(optarg, "poll", 4))
85                 rsrc->event_mode = false;
86         else if (!strncmp(optarg, "eventdev", 8))
87                 rsrc->event_mode = true;
88 }
89
90 static void
91 l2fwd_event_parse_eventq_sched(const char *optarg,
92                                struct l2fwd_resources *rsrc)
93 {
94         if (!strncmp(optarg, "ordered", 7))
95                 rsrc->sched_type = RTE_SCHED_TYPE_ORDERED;
96         else if (!strncmp(optarg, "atomic", 6))
97                 rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
98         else if (!strncmp(optarg, "parallel", 8))
99                 rsrc->sched_type = RTE_SCHED_TYPE_PARALLEL;
100 }
101
102 static const char short_options[] =
103         "p:"  /* portmask */
104         "q:"  /* number of queues */
105         "T:"  /* timer period */
106         ;
107
108 #define CMD_LINE_OPT_MAC_UPDATING "mac-updating"
109 #define CMD_LINE_OPT_NO_MAC_UPDATING "no-mac-updating"
110 #define CMD_LINE_OPT_MODE "mode"
111 #define CMD_LINE_OPT_EVENTQ_SCHED "eventq-sched"
112
113 enum {
114         /* long options mapped to a short option */
115
116         /* first long only option value must be >= 256, so that we won't
117          * conflict with short options
118          */
119         CMD_LINE_OPT_MIN_NUM = 256,
120         CMD_LINE_OPT_MODE_NUM,
121         CMD_LINE_OPT_EVENTQ_SCHED_NUM,
122 };
123
124 /* Parse the argument given in the command line of the application */
125 static int
126 l2fwd_event_parse_args(int argc, char **argv,
127                 struct l2fwd_resources *rsrc)
128 {
129         int mac_updating = 1;
130         struct option lgopts[] = {
131                 { CMD_LINE_OPT_MAC_UPDATING, no_argument, &mac_updating, 1},
132                 { CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, &mac_updating, 0},
133                 { CMD_LINE_OPT_MODE, required_argument, NULL,
134                                                         CMD_LINE_OPT_MODE_NUM},
135                 { CMD_LINE_OPT_EVENTQ_SCHED, required_argument, NULL,
136                                                 CMD_LINE_OPT_EVENTQ_SCHED_NUM},
137                 {NULL, 0, 0, 0}
138         };
139         int opt, ret, timer_secs;
140         char *prgname = argv[0];
141         char **argvopt;
142         int option_index;
143
144         argvopt = argv;
145         while ((opt = getopt_long(argc, argvopt, short_options,
146                                   lgopts, &option_index)) != EOF) {
147
148                 switch (opt) {
149                 /* portmask */
150                 case 'p':
151                         rsrc->enabled_port_mask =
152                                         l2fwd_event_parse_portmask(optarg);
153                         if (rsrc->enabled_port_mask == 0) {
154                                 printf("invalid portmask\n");
155                                 l2fwd_event_usage(prgname);
156                                 return -1;
157                         }
158                         break;
159
160                 /* nqueue */
161                 case 'q':
162                         rsrc->rx_queue_per_lcore =
163                                         l2fwd_event_parse_nqueue(optarg);
164                         if (rsrc->rx_queue_per_lcore == 0) {
165                                 printf("invalid queue number\n");
166                                 l2fwd_event_usage(prgname);
167                                 return -1;
168                         }
169                         break;
170
171                 /* timer period */
172                 case 'T':
173                         timer_secs = l2fwd_event_parse_timer_period(optarg);
174                         if (timer_secs < 0) {
175                                 printf("invalid timer period\n");
176                                 l2fwd_event_usage(prgname);
177                                 return -1;
178                         }
179                         rsrc->timer_period = timer_secs;
180                         /* convert to number of cycles */
181                         rsrc->timer_period *= rte_get_timer_hz();
182                         break;
183
184                 case CMD_LINE_OPT_MODE_NUM:
185                         l2fwd_event_parse_mode(optarg, rsrc);
186                         break;
187
188                 case CMD_LINE_OPT_EVENTQ_SCHED_NUM:
189                         l2fwd_event_parse_eventq_sched(optarg, rsrc);
190                         break;
191
192                 /* long options */
193                 case 0:
194                         break;
195
196                 default:
197                         l2fwd_event_usage(prgname);
198                         return -1;
199                 }
200         }
201
202         rsrc->mac_updating = mac_updating;
203
204         if (optind >= 0)
205                 argv[optind-1] = prgname;
206
207         ret = optind-1;
208         optind = 1; /* reset getopt lib */
209         return ret;
210 }
211
212 static int
213 l2fwd_launch_one_lcore(void *args)
214 {
215         struct l2fwd_resources *rsrc = args;
216         struct l2fwd_poll_resources *poll_rsrc = rsrc->poll_rsrc;
217
218         poll_rsrc->poll_main_loop(rsrc);
219
220         return 0;
221 }
222
223 /* Check the link status of all ports in up to 9s, and print them finally */
224 static void
225 check_all_ports_link_status(struct l2fwd_resources *rsrc,
226                             uint32_t port_mask)
227 {
228 #define CHECK_INTERVAL 100 /* 100ms */
229 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
230         uint16_t port_id;
231         uint8_t count, all_ports_up, print_flag = 0;
232         struct rte_eth_link link;
233
234         printf("\nChecking link status...");
235         fflush(stdout);
236         for (count = 0; count <= MAX_CHECK_TIME; count++) {
237                 if (rsrc->force_quit)
238                         return;
239                 all_ports_up = 1;
240                 RTE_ETH_FOREACH_DEV(port_id) {
241                         if (rsrc->force_quit)
242                                 return;
243                         if ((port_mask & (1 << port_id)) == 0)
244                                 continue;
245                         memset(&link, 0, sizeof(link));
246                         rte_eth_link_get_nowait(port_id, &link);
247                         /* print link status if flag set */
248                         if (print_flag == 1) {
249                                 if (link.link_status)
250                                         printf(
251                                         "Port%d Link Up. Speed %u Mbps - %s\n",
252                                                 port_id, link.link_speed,
253                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
254                                         ("full-duplex") : ("half-duplex\n"));
255                                 else
256                                         printf("Port %d Link Down\n", port_id);
257                                 continue;
258                         }
259                         /* clear all_ports_up flag if any link down */
260                         if (link.link_status == ETH_LINK_DOWN) {
261                                 all_ports_up = 0;
262                                 break;
263                         }
264                 }
265                 /* after finally printing all link status, get out */
266                 if (print_flag == 1)
267                         break;
268
269                 if (all_ports_up == 0) {
270                         printf(".");
271                         fflush(stdout);
272                         rte_delay_ms(CHECK_INTERVAL);
273                 }
274
275                 /* set the print_flag if all ports up or timeout */
276                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
277                         print_flag = 1;
278                         printf("done\n");
279                 }
280         }
281 }
282
283 /* Print out statistics on packets dropped */
284 static void
285 print_stats(struct l2fwd_resources *rsrc)
286 {
287         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
288         uint32_t port_id;
289
290         total_packets_dropped = 0;
291         total_packets_tx = 0;
292         total_packets_rx = 0;
293
294         const char clr[] = {27, '[', '2', 'J', '\0' };
295         const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0' };
296
297                 /* Clear screen and move to top left */
298         printf("%s%s", clr, topLeft);
299
300         printf("\nPort statistics ====================================");
301
302         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
303                 /* skip disabled ports */
304                 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
305                         continue;
306                 printf("\nStatistics for port %u ------------------------------"
307                            "\nPackets sent: %24"PRIu64
308                            "\nPackets received: %20"PRIu64
309                            "\nPackets dropped: %21"PRIu64,
310                            port_id,
311                            rsrc->port_stats[port_id].tx,
312                            rsrc->port_stats[port_id].rx,
313                            rsrc->port_stats[port_id].dropped);
314
315                 total_packets_dropped +=
316                                         rsrc->port_stats[port_id].dropped;
317                 total_packets_tx += rsrc->port_stats[port_id].tx;
318                 total_packets_rx += rsrc->port_stats[port_id].rx;
319         }
320         printf("\nAggregate statistics ==============================="
321                    "\nTotal packets sent: %18"PRIu64
322                    "\nTotal packets received: %14"PRIu64
323                    "\nTotal packets dropped: %15"PRIu64,
324                    total_packets_tx,
325                    total_packets_rx,
326                    total_packets_dropped);
327         printf("\n====================================================\n");
328 }
329
330 static void
331 l2fwd_event_print_stats(struct l2fwd_resources *rsrc)
332 {
333         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
334         const uint64_t timer_period = rsrc->timer_period;
335
336         while (!rsrc->force_quit) {
337                 /* if timer is enabled */
338                 if (timer_period > 0) {
339                         cur_tsc = rte_rdtsc();
340                         diff_tsc = cur_tsc - prev_tsc;
341
342                         /* advance the timer */
343                         timer_tsc += diff_tsc;
344
345                         /* if timer has reached its timeout */
346                         if (unlikely(timer_tsc >= timer_period)) {
347                                 print_stats(rsrc);
348                                 /* reset the timer */
349                                 timer_tsc = 0;
350                         }
351                         prev_tsc = cur_tsc;
352                 }
353         }
354 }
355
356
357 static void
358 signal_handler(int signum)
359 {
360         struct l2fwd_resources *rsrc = l2fwd_get_rsrc();
361         if (signum == SIGINT || signum == SIGTERM) {
362                 printf("\n\nSignal %d received, preparing to exit...\n",
363                                 signum);
364                 rsrc->force_quit = true;
365         }
366 }
367
368 int
369 main(int argc, char **argv)
370 {
371         struct l2fwd_resources *rsrc;
372         uint16_t nb_ports_available = 0;
373         uint32_t nb_ports_in_mask = 0;
374         uint16_t port_id, last_port;
375         uint32_t nb_mbufs;
376         uint16_t nb_ports;
377         int ret;
378
379         /* init EAL */
380         ret = rte_eal_init(argc, argv);
381         if (ret < 0)
382                 rte_panic("Invalid EAL arguments\n");
383         argc -= ret;
384         argv += ret;
385
386         rsrc = l2fwd_get_rsrc();
387
388         signal(SIGINT, signal_handler);
389         signal(SIGTERM, signal_handler);
390
391         /* parse application arguments (after the EAL ones) */
392         ret = l2fwd_event_parse_args(argc, argv, rsrc);
393         if (ret < 0)
394                 rte_panic("Invalid L2FWD arguments\n");
395
396         printf("MAC updating %s\n", rsrc->mac_updating ? "enabled" :
397                         "disabled");
398
399         nb_ports = rte_eth_dev_count_avail();
400         if (nb_ports == 0)
401                 rte_panic("No Ethernet ports - bye\n");
402
403         /* check port mask to possible port mask */
404         if (rsrc->enabled_port_mask & ~((1 << nb_ports) - 1))
405                 rte_panic("Invalid portmask; possible (0x%x)\n",
406                         (1 << nb_ports) - 1);
407
408         /* reset l2fwd_dst_ports */
409         for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
410                 rsrc->dst_ports[port_id] = 0;
411         last_port = 0;
412
413         /*
414          * Each logical core is assigned a dedicated TX queue on each port.
415          */
416         RTE_ETH_FOREACH_DEV(port_id) {
417                 /* skip ports that are not enabled */
418                 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
419                         continue;
420
421                 if (nb_ports_in_mask % 2) {
422                         rsrc->dst_ports[port_id] = last_port;
423                         rsrc->dst_ports[last_port] = port_id;
424                 } else {
425                         last_port = port_id;
426                 }
427
428                 nb_ports_in_mask++;
429         }
430         if (nb_ports_in_mask % 2) {
431                 printf("Notice: odd number of ports in portmask.\n");
432                 rsrc->dst_ports[last_port] = last_port;
433         }
434
435         nb_mbufs = RTE_MAX(nb_ports * (RTE_TEST_RX_DESC_DEFAULT +
436                                        RTE_TEST_TX_DESC_DEFAULT +
437                                        MAX_PKT_BURST + rte_lcore_count() *
438                                        MEMPOOL_CACHE_SIZE), 8192U);
439
440         /* create the mbuf pool */
441         rsrc->pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
442                         nb_mbufs, MEMPOOL_CACHE_SIZE, 0,
443                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
444         if (rsrc->pktmbuf_pool == NULL)
445                 rte_panic("Cannot init mbuf pool\n");
446
447         nb_ports_available = l2fwd_event_init_ports(rsrc);
448         if (!nb_ports_available)
449                 rte_panic("All available ports are disabled. Please set portmask.\n");
450
451         /* Configure eventdev parameters if required */
452         if (rsrc->event_mode)
453                 l2fwd_event_resource_setup(rsrc);
454         else
455                 l2fwd_poll_resource_setup(rsrc);
456
457         /* initialize port stats */
458         memset(&rsrc->port_stats, 0,
459                                         sizeof(struct l2fwd_port_statistics));
460
461         /* All settings are done. Now enable eth devices */
462         RTE_ETH_FOREACH_DEV(port_id) {
463                 /* skip ports that are not enabled */
464                 if ((rsrc->enabled_port_mask &
465                                         (1 << port_id)) == 0)
466                         continue;
467
468                 ret = rte_eth_dev_start(port_id);
469                 if (ret < 0)
470                         rte_panic("rte_eth_dev_start:err=%d, port=%u\n", ret,
471                                   port_id);
472         }
473
474         check_all_ports_link_status(rsrc, rsrc->enabled_port_mask);
475
476         /* launch per-lcore init on every lcore */
477         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, rsrc,
478                                  SKIP_MASTER);
479         l2fwd_event_print_stats(rsrc);
480         rte_eal_mp_wait_lcore();
481
482         RTE_ETH_FOREACH_DEV(port_id) {
483                 if ((rsrc->enabled_port_mask &
484                                                 (1 << port_id)) == 0)
485                         continue;
486                 printf("Closing port %d...", port_id);
487                 rte_eth_dev_stop(port_id);
488                 rte_eth_dev_close(port_id);
489                 printf(" Done\n");
490         }
491         printf("Bye...\n");
492
493         return 0;
494 }