1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
10 #include <sys/types.h>
11 #include <sys/queue.h>
18 #include <rte_common.h>
20 #include <rte_malloc.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
24 #include <rte_launch.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_mempool.h>
38 #define RTE_LOGTYPE_LSI RTE_LOGTYPE_USER1
42 #define MAX_PKT_BURST 32
43 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
46 * Configurable number of RX/TX ring descriptors
48 #define RTE_TEST_RX_DESC_DEFAULT 1024
49 #define RTE_TEST_TX_DESC_DEFAULT 1024
50 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
51 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
53 /* ethernet addresses of ports */
54 static struct rte_ether_addr lsi_ports_eth_addr[RTE_MAX_ETHPORTS];
56 /* mask of enabled ports */
57 static uint32_t lsi_enabled_port_mask = 0;
59 static unsigned int lsi_rx_queue_per_lcore = 1;
61 /* destination port for L2 forwarding */
62 static unsigned lsi_dst_ports[RTE_MAX_ETHPORTS] = {0};
64 #define MAX_PKT_BURST 32
66 #define MAX_RX_QUEUE_PER_LCORE 16
67 #define MAX_TX_QUEUE_PER_PORT 16
68 /* List of queues must be polled for a give lcore. 8< */
69 struct lcore_queue_conf {
71 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
73 } __rte_cache_aligned;
74 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
75 /* >8 End of list of queues to be polled. */
77 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
79 /* Global configuration stored in a static structure. 8< */
80 static struct rte_eth_conf port_conf = {
85 .mq_mode = RTE_ETH_MQ_TX_NONE,
88 .lsc = 1, /**< lsc interrupt feature enabled */
91 /* >8 End of global configuration stored in a static structure. */
93 struct rte_mempool * lsi_pktmbuf_pool = NULL;
95 /* Per-port statistics struct */
96 struct lsi_port_statistics {
100 } __rte_cache_aligned;
101 struct lsi_port_statistics port_statistics[RTE_MAX_ETHPORTS];
103 /* A tsc-based timer responsible for triggering statistics printout */
104 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
105 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
106 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
108 /* Print out statistics on packets dropped */
112 struct rte_eth_link link;
113 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
116 total_packets_dropped = 0;
117 total_packets_tx = 0;
118 total_packets_rx = 0;
120 const char clr[] = { 27, '[', '2', 'J', '\0' };
121 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
124 /* Clear screen and move to top left */
125 printf("%s%s", clr, topLeft);
127 printf("\nPort statistics ====================================");
129 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
130 /* skip ports that are not enabled */
131 if ((lsi_enabled_port_mask & (1 << portid)) == 0)
134 memset(&link, 0, sizeof(link));
135 link_get_err = rte_eth_link_get_nowait(portid, &link);
136 printf("\nStatistics for port %u ------------------------------"
137 "\nLink status: %25s"
139 "\nLink duplex: %25s"
140 "\nPackets sent: %24"PRIu64
141 "\nPackets received: %20"PRIu64
142 "\nPackets dropped: %21"PRIu64,
144 link_get_err < 0 ? "Link get failed" :
145 (link.link_status ? "Link up" : "Link down"),
146 link_get_err < 0 ? "0" :
147 rte_eth_link_speed_to_str(link.link_speed),
148 link_get_err < 0 ? "Link get failed" :
149 (link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX ?
150 "full-duplex" : "half-duplex"),
151 port_statistics[portid].tx,
152 port_statistics[portid].rx,
153 port_statistics[portid].dropped);
155 total_packets_dropped += port_statistics[portid].dropped;
156 total_packets_tx += port_statistics[portid].tx;
157 total_packets_rx += port_statistics[portid].rx;
159 printf("\nAggregate statistics ==============================="
160 "\nTotal packets sent: %18"PRIu64
161 "\nTotal packets received: %14"PRIu64
162 "\nTotal packets dropped: %15"PRIu64,
165 total_packets_dropped);
166 printf("\n====================================================\n");
171 /* Replacing the source and destination MAC addresses. 8< */
173 lsi_simple_forward(struct rte_mbuf *m, unsigned portid)
175 struct rte_ether_hdr *eth;
177 unsigned dst_port = lsi_dst_ports[portid];
179 struct rte_eth_dev_tx_buffer *buffer;
181 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
183 /* 02:00:00:00:00:xx */
184 tmp = ð->dst_addr.addr_bytes[0];
185 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
188 rte_ether_addr_copy(&lsi_ports_eth_addr[dst_port], ð->src_addr);
190 buffer = tx_buffer[dst_port];
191 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
193 port_statistics[dst_port].tx += sent;
195 /* >8 End of replacing the source and destination MAC addresses. */
197 /* main processing loop */
201 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
205 uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
206 unsigned i, j, portid, nb_rx;
207 struct lcore_queue_conf *qconf;
208 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
210 struct rte_eth_dev_tx_buffer *buffer;
215 lcore_id = rte_lcore_id();
216 qconf = &lcore_queue_conf[lcore_id];
218 if (qconf->n_rx_port == 0) {
219 RTE_LOG(INFO, LSI, "lcore %u has nothing to do\n", lcore_id);
223 RTE_LOG(INFO, LSI, "entering main loop on lcore %u\n", lcore_id);
225 for (i = 0; i < qconf->n_rx_port; i++) {
227 portid = qconf->rx_port_list[i];
228 RTE_LOG(INFO, LSI, " -- lcoreid=%u portid=%u\n", lcore_id,
234 /* Draining TX queue in its main loop. 8< */
235 cur_tsc = rte_rdtsc();
238 * TX burst queue drain
240 diff_tsc = cur_tsc - prev_tsc;
241 if (unlikely(diff_tsc > drain_tsc)) {
243 for (i = 0; i < qconf->n_rx_port; i++) {
245 portid = lsi_dst_ports[qconf->rx_port_list[i]];
246 buffer = tx_buffer[portid];
248 sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
250 port_statistics[portid].tx += sent;
254 /* if timer is enabled */
255 if (timer_period > 0) {
257 /* advance the timer */
258 timer_tsc += diff_tsc;
260 /* if timer has reached its timeout */
261 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
263 /* do this only on main core */
264 if (lcore_id == rte_get_main_lcore()) {
266 /* reset the timer */
274 /* >8 End of draining TX queue in its main loop. */
276 /* Read packet from RX queues. 8< */
277 for (i = 0; i < qconf->n_rx_port; i++) {
279 portid = qconf->rx_port_list[i];
280 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
281 pkts_burst, MAX_PKT_BURST);
283 port_statistics[portid].rx += nb_rx;
285 for (j = 0; j < nb_rx; j++) {
287 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
288 lsi_simple_forward(m, portid);
291 /* >8 End of reading packet from RX queues. */
296 lsi_launch_one_lcore(__rte_unused void *dummy)
304 lsi_usage(const char *prgname)
306 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
307 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
308 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
309 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
314 lsi_parse_portmask(const char *portmask)
319 /* parse hexadecimal string */
320 pm = strtoul(portmask, &end, 16);
321 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
328 lsi_parse_nqueue(const char *q_arg)
333 /* parse hexadecimal string */
334 n = strtoul(q_arg, &end, 10);
335 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
339 if (n >= MAX_RX_QUEUE_PER_LCORE)
346 lsi_parse_timer_period(const char *q_arg)
351 /* parse number string */
352 n = strtol(q_arg, &end, 10);
353 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
355 if (n >= MAX_TIMER_PERIOD)
361 /* Parse the argument given in the command line of the application */
363 lsi_parse_args(int argc, char **argv)
368 char *prgname = argv[0];
369 static struct option lgopts[] = {
375 while ((opt = getopt_long(argc, argvopt, "p:q:T:",
376 lgopts, &option_index)) != EOF) {
381 lsi_enabled_port_mask = lsi_parse_portmask(optarg);
382 if (lsi_enabled_port_mask == 0) {
383 printf("invalid portmask\n");
391 lsi_rx_queue_per_lcore = lsi_parse_nqueue(optarg);
392 if (lsi_rx_queue_per_lcore == 0) {
393 printf("invalid queue number\n");
401 timer_period = lsi_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
402 if (timer_period < 0) {
403 printf("invalid timer period\n");
421 argv[optind-1] = prgname;
424 optind = 1; /* reset getopt lib */
429 * It will be called as the callback for specified port after a LSI interrupt
430 * has been fully handled. This callback needs to be implemented carefully as
431 * it will be called in the interrupt host thread which is different from the
432 * application main thread.
439 * Pointer to(address of) the parameters.
445 /* lsi_event_callback 8< */
447 lsi_event_callback(uint16_t port_id, enum rte_eth_event_type type, void *param,
450 struct rte_eth_link link;
452 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
455 RTE_SET_USED(ret_param);
457 printf("\n\nIn registered callback...\n");
458 printf("Event type: %s\n", type == RTE_ETH_EVENT_INTR_LSC ? "LSC interrupt" : "unknown event");
459 ret = rte_eth_link_get_nowait(port_id, &link);
461 printf("Failed link get on port %d: %s\n",
462 port_id, rte_strerror(-ret));
465 rte_eth_link_to_str(link_status_text, sizeof(link_status_text), &link);
466 printf("Port %d %s\n\n", port_id, link_status_text);
470 /* >8 End of registering one or more callbacks. */
472 /* Check the link status of all ports in up to 9s, and print them finally */
474 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
476 #define CHECK_INTERVAL 100 /* 100ms */
477 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
478 uint8_t count, all_ports_up, print_flag = 0;
480 struct rte_eth_link link;
482 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
484 printf("\nChecking link status");
486 for (count = 0; count <= MAX_CHECK_TIME; count++) {
488 for (portid = 0; portid < port_num; portid++) {
489 if ((port_mask & (1 << portid)) == 0)
491 memset(&link, 0, sizeof(link));
492 ret = rte_eth_link_get_nowait(portid, &link);
496 printf("Port %u link get failed: %s\n",
497 portid, rte_strerror(-ret));
500 /* print link status if flag set */
501 if (print_flag == 1) {
502 rte_eth_link_to_str(link_status_text,
503 sizeof(link_status_text), &link);
504 printf("Port %d %s", portid,
508 /* clear all_ports_up flag if any link down */
509 if (link.link_status == RTE_ETH_LINK_DOWN) {
514 /* after finally printing all link status, get out */
518 if (all_ports_up == 0) {
521 rte_delay_ms(CHECK_INTERVAL);
524 /* set the print_flag if all ports up or timeout */
525 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
533 main(int argc, char **argv)
535 struct lcore_queue_conf *qconf;
538 uint16_t portid, portid_last = 0;
539 unsigned lcore_id, rx_lcore_id;
540 unsigned nb_ports_in_mask = 0;
543 ret = rte_eal_init(argc, argv);
545 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
549 /* parse application arguments (after the EAL ones) */
550 ret = lsi_parse_args(argc, argv);
552 rte_exit(EXIT_FAILURE, "Invalid arguments");
554 /* create the mbuf pool */
556 rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32, 0,
557 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
558 if (lsi_pktmbuf_pool == NULL)
559 rte_panic("Cannot init mbuf pool\n");
561 nb_ports = rte_eth_dev_count_avail();
563 rte_panic("No Ethernet port - bye\n");
565 /* Each logical core is assigned a dedicated TX queue on each port. 8< */
566 for (portid = 0; portid < nb_ports; portid++) {
567 /* skip ports that are not enabled */
568 if ((lsi_enabled_port_mask & (1 << portid)) == 0)
571 /* save the destination port id */
572 if (nb_ports_in_mask % 2) {
573 lsi_dst_ports[portid] = portid_last;
574 lsi_dst_ports[portid_last] = portid;
577 portid_last = portid;
581 /* >8 End of assigning logical core. */
582 if (nb_ports_in_mask < 2 || nb_ports_in_mask % 2)
583 rte_exit(EXIT_FAILURE, "Current enabled port number is %u, "
584 "but it should be even and at least 2\n",
588 qconf = &lcore_queue_conf[rx_lcore_id];
590 /* Initialize the port/queue configuration of each logical core */
591 for (portid = 0; portid < nb_ports; portid++) {
592 /* skip ports that are not enabled */
593 if ((lsi_enabled_port_mask & (1 << portid)) == 0)
596 /* get the lcore_id for this port */
597 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
598 lcore_queue_conf[rx_lcore_id].n_rx_port ==
599 lsi_rx_queue_per_lcore) {
602 if (rx_lcore_id >= RTE_MAX_LCORE)
603 rte_exit(EXIT_FAILURE, "Not enough cores\n");
605 if (qconf != &lcore_queue_conf[rx_lcore_id])
606 /* Assigned a new logical core in the loop above. */
607 qconf = &lcore_queue_conf[rx_lcore_id];
609 qconf->rx_port_list[qconf->n_rx_port] = portid;
611 printf("Lcore %u: RX port %u\n",rx_lcore_id, (unsigned) portid);
614 /* Initialise each port */
615 for (portid = 0; portid < nb_ports; portid++) {
616 struct rte_eth_rxconf rxq_conf;
617 struct rte_eth_txconf txq_conf;
618 struct rte_eth_conf local_port_conf = port_conf;
619 struct rte_eth_dev_info dev_info;
621 /* skip ports that are not enabled */
622 if ((lsi_enabled_port_mask & (1 << portid)) == 0) {
623 printf("Skipping disabled port %u\n", (unsigned) portid);
627 printf("Initializing port %u... ", (unsigned) portid);
630 ret = rte_eth_dev_info_get(portid, &dev_info);
632 rte_exit(EXIT_FAILURE,
633 "Error during getting device (port %u) info: %s\n",
634 portid, strerror(-ret));
636 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
637 local_port_conf.txmode.offloads |=
638 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
639 /* Configure RX and TX queues. 8< */
640 ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
642 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
643 ret, (unsigned) portid);
644 /* >8 End of configure RX and TX queues. */
646 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
649 rte_exit(EXIT_FAILURE,
650 "rte_eth_dev_adjust_nb_rx_tx_desc: err=%d, port=%u\n",
651 ret, (unsigned) portid);
653 /* register lsi interrupt callback, need to be after
654 * rte_eth_dev_configure(). if (intr_conf.lsc == 0), no
655 * lsc interrupt will be present, and below callback to
656 * be registered will never be called.
659 /* RTE callback register. 8< */
660 rte_eth_dev_callback_register(portid,
661 RTE_ETH_EVENT_INTR_LSC, lsi_event_callback, NULL);
662 /* >8 End of registering lsi interrupt callback. */
664 ret = rte_eth_macaddr_get(portid,
665 &lsi_ports_eth_addr[portid]);
667 rte_exit(EXIT_FAILURE,
668 "rte_eth_macaddr_get: err=%d, port=%u\n",
669 ret, (unsigned int)portid);
671 /* init one RX queue */
673 rxq_conf = dev_info.default_rxconf;
674 rxq_conf.offloads = local_port_conf.rxmode.offloads;
675 /* RX queue initialization. 8< */
676 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
677 rte_eth_dev_socket_id(portid),
681 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, port=%u\n",
682 ret, (unsigned) portid);
683 /* >8 End of RX queue initialization. */
685 /* init one TX queue logical core on each port. 8< */
687 txq_conf = dev_info.default_txconf;
688 txq_conf.offloads = local_port_conf.txmode.offloads;
689 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
690 rte_eth_dev_socket_id(portid),
693 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d,port=%u\n",
694 ret, (unsigned) portid);
695 /* >8 End of init one TX queue. */
697 /* Initialize TX buffers */
698 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
699 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
700 rte_eth_dev_socket_id(portid));
701 if (tx_buffer[portid] == NULL)
702 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
705 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
707 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
708 rte_eth_tx_buffer_count_callback,
709 &port_statistics[portid].dropped);
711 rte_exit(EXIT_FAILURE, "Cannot set error callback for "
712 "tx buffer on port %u\n", (unsigned) portid);
715 ret = rte_eth_dev_start(portid);
717 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%u\n",
718 ret, (unsigned) portid);
721 ret = rte_eth_promiscuous_enable(portid);
723 rte_exit(EXIT_FAILURE,
724 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
725 rte_strerror(-ret), portid);
727 printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
729 RTE_ETHER_ADDR_BYTES(&lsi_ports_eth_addr[portid]));
731 /* initialize port stats */
732 memset(&port_statistics, 0, sizeof(port_statistics));
735 check_all_ports_link_status(nb_ports, lsi_enabled_port_mask);
737 /* launch per-lcore init on every lcore */
738 rte_eal_mp_remote_launch(lsi_launch_one_lcore, NULL, CALL_MAIN);
739 RTE_LCORE_FOREACH_WORKER(lcore_id) {
740 if (rte_eal_wait_lcore(lcore_id) < 0)
744 /* clean up the EAL */