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;
217 struct l2fwd_event_resources *evt_rsrc = rsrc->evt_rsrc;
219 if (rsrc->event_mode)
220 evt_rsrc->ops.l2fwd_event_loop(rsrc);
222 poll_rsrc->poll_main_loop(rsrc);
227 /* Check the link status of all ports in up to 9s, and print them finally */
229 check_all_ports_link_status(struct l2fwd_resources *rsrc,
232 #define CHECK_INTERVAL 100 /* 100ms */
233 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
235 uint8_t count, all_ports_up, print_flag = 0;
236 struct rte_eth_link link;
238 printf("\nChecking link status...");
240 for (count = 0; count <= MAX_CHECK_TIME; count++) {
241 if (rsrc->force_quit)
244 RTE_ETH_FOREACH_DEV(port_id) {
245 if (rsrc->force_quit)
247 if ((port_mask & (1 << port_id)) == 0)
249 memset(&link, 0, sizeof(link));
250 rte_eth_link_get_nowait(port_id, &link);
251 /* print link status if flag set */
252 if (print_flag == 1) {
253 if (link.link_status)
255 "Port%d Link Up. Speed %u Mbps - %s\n",
256 port_id, link.link_speed,
257 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
258 ("full-duplex") : ("half-duplex\n"));
260 printf("Port %d Link Down\n", port_id);
263 /* clear all_ports_up flag if any link down */
264 if (link.link_status == ETH_LINK_DOWN) {
269 /* after finally printing all link status, get out */
273 if (all_ports_up == 0) {
276 rte_delay_ms(CHECK_INTERVAL);
279 /* set the print_flag if all ports up or timeout */
280 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
287 /* Print out statistics on packets dropped */
289 print_stats(struct l2fwd_resources *rsrc)
291 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
294 total_packets_dropped = 0;
295 total_packets_tx = 0;
296 total_packets_rx = 0;
298 const char clr[] = {27, '[', '2', 'J', '\0' };
299 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0' };
301 /* Clear screen and move to top left */
302 printf("%s%s", clr, topLeft);
304 printf("\nPort statistics ====================================");
306 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
307 /* skip disabled ports */
308 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
310 printf("\nStatistics for port %u ------------------------------"
311 "\nPackets sent: %29"PRIu64
312 "\nPackets received: %25"PRIu64
313 "\nPackets dropped: %26"PRIu64,
315 rsrc->port_stats[port_id].tx,
316 rsrc->port_stats[port_id].rx,
317 rsrc->port_stats[port_id].dropped);
319 total_packets_dropped +=
320 rsrc->port_stats[port_id].dropped;
321 total_packets_tx += rsrc->port_stats[port_id].tx;
322 total_packets_rx += rsrc->port_stats[port_id].rx;
325 if (rsrc->event_mode) {
326 struct l2fwd_event_resources *evt_rsrc = rsrc->evt_rsrc;
327 struct rte_event_eth_rx_adapter_stats rx_adptr_stats;
328 struct rte_event_eth_tx_adapter_stats tx_adptr_stats;
331 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++) {
332 ret = rte_event_eth_rx_adapter_stats_get(
333 evt_rsrc->rx_adptr.rx_adptr[i],
337 printf("\nRx adapter[%d] statistics===================="
338 "\nReceive queue poll count: %17"PRIu64
339 "\nReceived packet count: %20"PRIu64
340 "\nEventdev enqueue count: %19"PRIu64
341 "\nEventdev enqueue retry count: %13"PRIu64
342 "\nReceived packet dropped count: %12"PRIu64
343 "\nRx enqueue start timestamp: %15"PRIu64
344 "\nRx enqueue block cycles: %18"PRIu64
345 "\nRx enqueue unblock timestamp: %13"PRIu64,
346 evt_rsrc->rx_adptr.rx_adptr[i],
347 rx_adptr_stats.rx_poll_count,
348 rx_adptr_stats.rx_packets,
349 rx_adptr_stats.rx_enq_count,
350 rx_adptr_stats.rx_enq_retry,
351 rx_adptr_stats.rx_dropped,
352 rx_adptr_stats.rx_enq_start_ts,
353 rx_adptr_stats.rx_enq_block_cycles,
354 rx_adptr_stats.rx_enq_end_ts);
356 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++) {
357 ret = rte_event_eth_tx_adapter_stats_get(
358 evt_rsrc->tx_adptr.tx_adptr[i],
362 printf("\nTx adapter[%d] statistics===================="
363 "\nNumber of transmit retries: %15"PRIu64
364 "\nNumber of packets transmitted: %12"PRIu64
365 "\nNumber of packets dropped: %16"PRIu64,
366 evt_rsrc->tx_adptr.tx_adptr[i],
367 tx_adptr_stats.tx_retry,
368 tx_adptr_stats.tx_packets,
369 tx_adptr_stats.tx_dropped);
372 printf("\nAggregate lcore statistics ========================="
373 "\nTotal packets sent: %23"PRIu64
374 "\nTotal packets received: %19"PRIu64
375 "\nTotal packets dropped: %20"PRIu64,
378 total_packets_dropped);
379 printf("\n====================================================\n");
383 l2fwd_event_print_stats(struct l2fwd_resources *rsrc)
385 uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
386 const uint64_t timer_period = rsrc->timer_period;
388 while (!rsrc->force_quit) {
389 /* if timer is enabled */
390 if (timer_period > 0) {
391 cur_tsc = rte_rdtsc();
392 diff_tsc = cur_tsc - prev_tsc;
394 /* advance the timer */
395 timer_tsc += diff_tsc;
397 /* if timer has reached its timeout */
398 if (unlikely(timer_tsc >= timer_period)) {
400 /* reset the timer */
410 signal_handler(int signum)
412 struct l2fwd_resources *rsrc = l2fwd_get_rsrc();
413 if (signum == SIGINT || signum == SIGTERM) {
414 printf("\n\nSignal %d received, preparing to exit...\n",
416 rsrc->force_quit = true;
421 main(int argc, char **argv)
423 struct l2fwd_resources *rsrc;
424 uint16_t nb_ports_available = 0;
425 uint32_t nb_ports_in_mask = 0;
426 uint16_t port_id, last_port;
432 ret = rte_eal_init(argc, argv);
434 rte_panic("Invalid EAL arguments\n");
438 rsrc = l2fwd_get_rsrc();
440 signal(SIGINT, signal_handler);
441 signal(SIGTERM, signal_handler);
443 /* parse application arguments (after the EAL ones) */
444 ret = l2fwd_event_parse_args(argc, argv, rsrc);
446 rte_panic("Invalid L2FWD arguments\n");
448 printf("MAC updating %s\n", rsrc->mac_updating ? "enabled" :
451 nb_ports = rte_eth_dev_count_avail();
453 rte_panic("No Ethernet ports - bye\n");
455 /* check port mask to possible port mask */
456 if (rsrc->enabled_port_mask & ~((1 << nb_ports) - 1))
457 rte_panic("Invalid portmask; possible (0x%x)\n",
458 (1 << nb_ports) - 1);
460 /* reset l2fwd_dst_ports */
461 for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++)
462 rsrc->dst_ports[port_id] = 0;
466 * Each logical core is assigned a dedicated TX queue on each port.
468 RTE_ETH_FOREACH_DEV(port_id) {
469 /* skip ports that are not enabled */
470 if ((rsrc->enabled_port_mask & (1 << port_id)) == 0)
473 if (nb_ports_in_mask % 2) {
474 rsrc->dst_ports[port_id] = last_port;
475 rsrc->dst_ports[last_port] = port_id;
482 if (nb_ports_in_mask % 2) {
483 printf("Notice: odd number of ports in portmask.\n");
484 rsrc->dst_ports[last_port] = last_port;
487 nb_mbufs = RTE_MAX(nb_ports * (RTE_TEST_RX_DESC_DEFAULT +
488 RTE_TEST_TX_DESC_DEFAULT +
489 MAX_PKT_BURST + rte_lcore_count() *
490 MEMPOOL_CACHE_SIZE), 8192U);
492 /* create the mbuf pool */
493 rsrc->pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",
494 nb_mbufs, MEMPOOL_CACHE_SIZE, 0,
495 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
496 if (rsrc->pktmbuf_pool == NULL)
497 rte_panic("Cannot init mbuf pool\n");
499 nb_ports_available = l2fwd_event_init_ports(rsrc);
500 if (!nb_ports_available)
501 rte_panic("All available ports are disabled. Please set portmask.\n");
503 /* Configure eventdev parameters if required */
504 if (rsrc->event_mode)
505 l2fwd_event_resource_setup(rsrc);
507 l2fwd_poll_resource_setup(rsrc);
509 /* initialize port stats */
510 memset(&rsrc->port_stats, 0,
511 sizeof(struct l2fwd_port_statistics));
513 /* All settings are done. Now enable eth devices */
514 RTE_ETH_FOREACH_DEV(port_id) {
515 /* skip ports that are not enabled */
516 if ((rsrc->enabled_port_mask &
517 (1 << port_id)) == 0)
520 ret = rte_eth_dev_start(port_id);
522 rte_panic("rte_eth_dev_start:err=%d, port=%u\n", ret,
526 if (rsrc->event_mode)
527 l2fwd_event_service_setup(rsrc);
529 check_all_ports_link_status(rsrc, rsrc->enabled_port_mask);
531 /* launch per-lcore init on every lcore */
532 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, rsrc,
534 l2fwd_event_print_stats(rsrc);
535 if (rsrc->event_mode) {
536 struct l2fwd_event_resources *evt_rsrc =
538 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++)
539 rte_event_eth_rx_adapter_stop(
540 evt_rsrc->rx_adptr.rx_adptr[i]);
541 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++)
542 rte_event_eth_tx_adapter_stop(
543 evt_rsrc->tx_adptr.tx_adptr[i]);
545 RTE_ETH_FOREACH_DEV(port_id) {
546 if ((rsrc->enabled_port_mask &
547 (1 << port_id)) == 0)
549 rte_eth_dev_stop(port_id);
552 rte_eal_mp_wait_lcore();
553 RTE_ETH_FOREACH_DEV(port_id) {
554 if ((rsrc->enabled_port_mask &
555 (1 << port_id)) == 0)
557 rte_eth_dev_close(port_id);
560 rte_event_dev_stop(evt_rsrc->event_d_id);
561 rte_event_dev_close(evt_rsrc->event_d_id);
564 rte_eal_mp_wait_lcore();
566 RTE_ETH_FOREACH_DEV(port_id) {
567 if ((rsrc->enabled_port_mask &
568 (1 << port_id)) == 0)
570 printf("Closing port %d...", port_id);
571 rte_eth_dev_stop(port_id);
572 rte_eth_dev_close(port_id);