1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
5 #include "l2fwd_event.h"
6 #include "l2fwd_poll.h"
10 l2fwd_event_usage(const char *prgname)
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"
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"
25 " Valid only if --mode=eventdev\n\n",
30 l2fwd_event_parse_portmask(const char *portmask)
35 /* parse hexadecimal string */
36 pm = strtoul(portmask, &end, 16);
37 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
47 l2fwd_event_parse_nqueue(const char *q_arg)
52 /* parse hexadecimal string */
53 n = strtoul(q_arg, &end, 10);
54 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
58 if (n >= MAX_RX_QUEUE_PER_LCORE)
65 l2fwd_event_parse_timer_period(const char *q_arg)
70 /* parse number string */
71 n = strtol(q_arg, &end, 10);
72 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
74 if (n >= MAX_TIMER_PERIOD)
81 l2fwd_event_parse_mode(const char *optarg,
82 struct l2fwd_resources *rsrc)
84 if (!strncmp(optarg, "poll", 4))
85 rsrc->event_mode = false;
86 else if (!strncmp(optarg, "eventdev", 8))
87 rsrc->event_mode = true;
91 l2fwd_event_parse_eventq_sched(const char *optarg,
92 struct l2fwd_resources *rsrc)
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;
102 static const char short_options[] =
104 "q:" /* number of queues */
105 "T:" /* timer period */
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"
114 /* long options mapped to a short option */
116 /* first long only option value must be >= 256, so that we won't
117 * conflict with short options
119 CMD_LINE_OPT_MIN_NUM = 256,
120 CMD_LINE_OPT_MODE_NUM,
121 CMD_LINE_OPT_EVENTQ_SCHED_NUM,
124 /* Parse the argument given in the command line of the application */
126 l2fwd_event_parse_args(int argc, char **argv,
127 struct l2fwd_resources *rsrc)
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},
139 int opt, ret, timer_secs;
140 char *prgname = argv[0];
145 while ((opt = getopt_long(argc, argvopt, short_options,
146 lgopts, &option_index)) != EOF) {
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);
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);
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);
179 rsrc->timer_period = timer_secs;
180 /* convert to number of cycles */
181 rsrc->timer_period *= rte_get_timer_hz();
184 case CMD_LINE_OPT_MODE_NUM:
185 l2fwd_event_parse_mode(optarg, rsrc);
188 case CMD_LINE_OPT_EVENTQ_SCHED_NUM:
189 l2fwd_event_parse_eventq_sched(optarg, rsrc);
197 l2fwd_event_usage(prgname);
202 rsrc->mac_updating = mac_updating;
205 argv[optind-1] = prgname;
208 optind = 1; /* reset getopt lib */
213 l2fwd_launch_one_lcore(void *args)
215 struct l2fwd_resources *rsrc = args;
216 struct l2fwd_poll_resources *poll_rsrc = rsrc->poll_rsrc;
218 poll_rsrc->poll_main_loop(rsrc);
223 /* Check the link status of all ports in up to 9s, and print them finally */
225 check_all_ports_link_status(struct l2fwd_resources *rsrc,
228 #define CHECK_INTERVAL 100 /* 100ms */
229 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
231 uint8_t count, all_ports_up, print_flag = 0;
232 struct rte_eth_link link;
234 printf("\nChecking link status...");
236 for (count = 0; count <= MAX_CHECK_TIME; count++) {
237 if (rsrc->force_quit)
240 RTE_ETH_FOREACH_DEV(port_id) {
241 if (rsrc->force_quit)
243 if ((port_mask & (1 << port_id)) == 0)
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)
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"));
256 printf("Port %d Link Down\n", port_id);
259 /* clear all_ports_up flag if any link down */
260 if (link.link_status == ETH_LINK_DOWN) {
265 /* after finally printing all link status, get out */
269 if (all_ports_up == 0) {
272 rte_delay_ms(CHECK_INTERVAL);
275 /* set the print_flag if all ports up or timeout */
276 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
283 /* Print out statistics on packets dropped */
285 print_stats(struct l2fwd_resources *rsrc)
287 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
290 total_packets_dropped = 0;
291 total_packets_tx = 0;
292 total_packets_rx = 0;
294 const char clr[] = {27, '[', '2', 'J', '\0' };
295 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0' };
297 /* Clear screen and move to top left */
298 printf("%s%s", clr, topLeft);
300 printf("\nPort statistics ====================================");
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)
306 printf("\nStatistics for port %u ------------------------------"
307 "\nPackets sent: %24"PRIu64
308 "\nPackets received: %20"PRIu64
309 "\nPackets dropped: %21"PRIu64,
311 rsrc->port_stats[port_id].tx,
312 rsrc->port_stats[port_id].rx,
313 rsrc->port_stats[port_id].dropped);
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;
320 printf("\nAggregate statistics ==============================="
321 "\nTotal packets sent: %18"PRIu64
322 "\nTotal packets received: %14"PRIu64
323 "\nTotal packets dropped: %15"PRIu64,
326 total_packets_dropped);
327 printf("\n====================================================\n");
331 l2fwd_event_print_stats(struct l2fwd_resources *rsrc)
333 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
334 const uint64_t timer_period = rsrc->timer_period;
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;
342 /* advance the timer */
343 timer_tsc += diff_tsc;
345 /* if timer has reached its timeout */
346 if (unlikely(timer_tsc >= timer_period)) {
348 /* reset the timer */
358 signal_handler(int signum)
360 struct l2fwd_resources *rsrc = l2fwd_get_rsrc();
361 if (signum == SIGINT || signum == SIGTERM) {
362 printf("\n\nSignal %d received, preparing to exit...\n",
364 rsrc->force_quit = true;
369 main(int argc, char **argv)
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;
380 ret = rte_eal_init(argc, argv);
382 rte_panic("Invalid EAL arguments\n");
386 rsrc = l2fwd_get_rsrc();
388 signal(SIGINT, signal_handler);
389 signal(SIGTERM, signal_handler);
391 /* parse application arguments (after the EAL ones) */
392 ret = l2fwd_event_parse_args(argc, argv, rsrc);
394 rte_panic("Invalid L2FWD arguments\n");
396 printf("MAC updating %s\n", rsrc->mac_updating ? "enabled" :
399 nb_ports = rte_eth_dev_count_avail();
401 rte_panic("No Ethernet ports - bye\n");
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);
408 /* reset l2fwd_dst_ports */
409 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
410 rsrc->dst_ports[port_id] = 0;
414 * Each logical core is assigned a dedicated TX queue on each port.
416 RTE_ETH_FOREACH_DEV(port_id) {
417 /* skip ports that are not enabled */
418 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
421 if (nb_ports_in_mask % 2) {
422 rsrc->dst_ports[port_id] = last_port;
423 rsrc->dst_ports[last_port] = port_id;
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;
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);
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");
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");
451 /* Configure eventdev parameters if required */
452 if (rsrc->event_mode)
453 l2fwd_event_resource_setup(rsrc);
455 l2fwd_poll_resource_setup(rsrc);
457 /* initialize port stats */
458 memset(&rsrc->port_stats, 0,
459 sizeof(struct l2fwd_port_statistics));
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)
468 ret = rte_eth_dev_start(port_id);
470 rte_panic("rte_eth_dev_start:err=%d, port=%u\n", ret,
474 if (rsrc->event_mode)
475 l2fwd_event_service_setup(rsrc);
477 check_all_ports_link_status(rsrc, rsrc->enabled_port_mask);
479 /* launch per-lcore init on every lcore */
480 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, rsrc,
482 l2fwd_event_print_stats(rsrc);
483 rte_eal_mp_wait_lcore();
485 RTE_ETH_FOREACH_DEV(port_id) {
486 if ((rsrc->enabled_port_mask &
487 (1 << port_id)) == 0)
489 printf("Closing port %d...", port_id);
490 rte_eth_dev_stop(port_id);
491 rte_eth_dev_close(port_id);