4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
49 #include <rte_common.h>
51 #include <rte_malloc.h>
52 #include <rte_memory.h>
53 #include <rte_memcpy.h>
54 #include <rte_memzone.h>
56 #include <rte_per_lcore.h>
57 #include <rte_launch.h>
58 #include <rte_atomic.h>
59 #include <rte_cycles.h>
60 #include <rte_prefetch.h>
61 #include <rte_lcore.h>
62 #include <rte_per_lcore.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_interrupts.h>
66 #include <rte_random.h>
67 #include <rte_debug.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
71 #include <rte_mempool.h>
74 #define RTE_LOGTYPE_LSI RTE_LOGTYPE_USER1
78 #define MAX_PKT_BURST 32
79 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
82 * Configurable number of RX/TX ring descriptors
84 #define RTE_TEST_RX_DESC_DEFAULT 128
85 #define RTE_TEST_TX_DESC_DEFAULT 512
86 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
87 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
89 /* ethernet addresses of ports */
90 static struct ether_addr lsi_ports_eth_addr[RTE_MAX_ETHPORTS];
92 /* mask of enabled ports */
93 static uint32_t lsi_enabled_port_mask = 0;
95 static unsigned int lsi_rx_queue_per_lcore = 1;
97 /* destination port for L2 forwarding */
98 static unsigned lsi_dst_ports[RTE_MAX_ETHPORTS] = {0};
100 #define MAX_PKT_BURST 32
102 #define MAX_RX_QUEUE_PER_LCORE 16
103 #define MAX_TX_QUEUE_PER_PORT 16
104 struct lcore_queue_conf {
106 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
107 unsigned tx_queue_id;
108 } __rte_cache_aligned;
109 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
111 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
113 static const struct rte_eth_conf port_conf = {
116 .header_split = 0, /**< Header Split disabled */
117 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
118 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
119 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
120 .hw_strip_crc = 0, /**< CRC stripped by hardware */
123 .mq_mode = ETH_MQ_TX_NONE,
126 .lsc = 1, /**< lsc interrupt feature enabled */
130 struct rte_mempool * lsi_pktmbuf_pool = NULL;
132 /* Per-port statistics struct */
133 struct lsi_port_statistics {
137 } __rte_cache_aligned;
138 struct lsi_port_statistics port_statistics[RTE_MAX_ETHPORTS];
140 /* A tsc-based timer responsible for triggering statistics printout */
141 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
142 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
143 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
145 /* Print out statistics on packets dropped */
149 struct rte_eth_link link;
150 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
153 total_packets_dropped = 0;
154 total_packets_tx = 0;
155 total_packets_rx = 0;
157 const char clr[] = { 27, '[', '2', 'J', '\0' };
158 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
160 /* Clear screen and move to top left */
161 printf("%s%s", clr, topLeft);
163 printf("\nPort statistics ====================================");
165 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
166 /* skip ports that are not enabled */
167 if ((lsi_enabled_port_mask & (1 << portid)) == 0)
170 memset(&link, 0, sizeof(link));
171 rte_eth_link_get_nowait((uint8_t)portid, &link);
172 printf("\nStatistics for port %u ------------------------------"
173 "\nLink status: %25s"
175 "\nLink duplex: %25s"
176 "\nPackets sent: %24"PRIu64
177 "\nPackets received: %20"PRIu64
178 "\nPackets dropped: %21"PRIu64,
180 (link.link_status ? "Link up" : "Link down"),
181 (unsigned)link.link_speed,
182 (link.link_duplex == ETH_LINK_FULL_DUPLEX ? \
183 "full-duplex" : "half-duplex"),
184 port_statistics[portid].tx,
185 port_statistics[portid].rx,
186 port_statistics[portid].dropped);
188 total_packets_dropped += port_statistics[portid].dropped;
189 total_packets_tx += port_statistics[portid].tx;
190 total_packets_rx += port_statistics[portid].rx;
192 printf("\nAggregate statistics ==============================="
193 "\nTotal packets sent: %18"PRIu64
194 "\nTotal packets received: %14"PRIu64
195 "\nTotal packets dropped: %15"PRIu64,
198 total_packets_dropped);
199 printf("\n====================================================\n");
203 lsi_simple_forward(struct rte_mbuf *m, unsigned portid)
205 struct ether_hdr *eth;
207 unsigned dst_port = lsi_dst_ports[portid];
209 struct rte_eth_dev_tx_buffer *buffer;
211 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
213 /* 02:00:00:00:00:xx */
214 tmp = ð->d_addr.addr_bytes[0];
215 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
218 ether_addr_copy(&lsi_ports_eth_addr[dst_port], ð->s_addr);
220 buffer = tx_buffer[dst_port];
221 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
223 port_statistics[dst_port].tx += sent;
226 /* main processing loop */
230 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
234 uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
235 unsigned i, j, portid, nb_rx;
236 struct lcore_queue_conf *qconf;
237 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
239 struct rte_eth_dev_tx_buffer *buffer;
244 lcore_id = rte_lcore_id();
245 qconf = &lcore_queue_conf[lcore_id];
247 if (qconf->n_rx_port == 0) {
248 RTE_LOG(INFO, LSI, "lcore %u has nothing to do\n", lcore_id);
252 RTE_LOG(INFO, LSI, "entering main loop on lcore %u\n", lcore_id);
254 for (i = 0; i < qconf->n_rx_port; i++) {
256 portid = qconf->rx_port_list[i];
257 RTE_LOG(INFO, LSI, " -- lcoreid=%u portid=%u\n", lcore_id,
263 cur_tsc = rte_rdtsc();
266 * TX burst queue drain
268 diff_tsc = cur_tsc - prev_tsc;
269 if (unlikely(diff_tsc > drain_tsc)) {
271 for (i = 0; i < qconf->n_rx_port; i++) {
273 portid = lsi_dst_ports[qconf->rx_port_list[i]];
274 buffer = tx_buffer[portid];
276 sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
278 port_statistics[portid].tx += sent;
282 /* if timer is enabled */
283 if (timer_period > 0) {
285 /* advance the timer */
286 timer_tsc += diff_tsc;
288 /* if timer has reached its timeout */
289 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
291 /* do this only on master core */
292 if (lcore_id == rte_get_master_lcore()) {
294 /* reset the timer */
304 * Read packet from RX queues
306 for (i = 0; i < qconf->n_rx_port; i++) {
308 portid = qconf->rx_port_list[i];
309 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
310 pkts_burst, MAX_PKT_BURST);
312 port_statistics[portid].rx += nb_rx;
314 for (j = 0; j < nb_rx; j++) {
316 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
317 lsi_simple_forward(m, portid);
324 lsi_launch_one_lcore(__attribute__((unused)) void *dummy)
332 lsi_usage(const char *prgname)
334 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
335 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
336 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
337 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
342 lsi_parse_portmask(const char *portmask)
347 /* parse hexadecimal string */
348 pm = strtoul(portmask, &end, 16);
349 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
359 lsi_parse_nqueue(const char *q_arg)
364 /* parse hexadecimal string */
365 n = strtoul(q_arg, &end, 10);
366 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
370 if (n >= MAX_RX_QUEUE_PER_LCORE)
377 lsi_parse_timer_period(const char *q_arg)
382 /* parse number string */
383 n = strtol(q_arg, &end, 10);
384 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
386 if (n >= MAX_TIMER_PERIOD)
392 /* Parse the argument given in the command line of the application */
394 lsi_parse_args(int argc, char **argv)
399 char *prgname = argv[0];
400 static struct option lgopts[] = {
406 while ((opt = getopt_long(argc, argvopt, "p:q:T:",
407 lgopts, &option_index)) != EOF) {
412 lsi_enabled_port_mask = lsi_parse_portmask(optarg);
413 if (lsi_enabled_port_mask == 0) {
414 printf("invalid portmask\n");
422 lsi_rx_queue_per_lcore = lsi_parse_nqueue(optarg);
423 if (lsi_rx_queue_per_lcore == 0) {
424 printf("invalid queue number\n");
432 timer_period = lsi_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
433 if (timer_period < 0) {
434 printf("invalid timer period\n");
452 argv[optind-1] = prgname;
455 optind = 0; /* reset getopt lib */
460 * It will be called as the callback for specified port after a LSI interrupt
461 * has been fully handled. This callback needs to be implemented carefully as
462 * it will be called in the interrupt host thread which is different from the
463 * application main thread.
470 * Pointer to(address of) the parameters.
476 lsi_event_callback(uint8_t port_id, enum rte_eth_event_type type, void *param)
478 struct rte_eth_link link;
482 printf("\n\nIn registered callback...\n");
483 printf("Event type: %s\n", type == RTE_ETH_EVENT_INTR_LSC ? "LSC interrupt" : "unknown event");
484 rte_eth_link_get_nowait(port_id, &link);
485 if (link.link_status) {
486 printf("Port %d Link Up - speed %u Mbps - %s\n\n",
487 port_id, (unsigned)link.link_speed,
488 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
489 ("full-duplex") : ("half-duplex"));
491 printf("Port %d Link Down\n\n", port_id);
494 /* Check the link status of all ports in up to 9s, and print them finally */
496 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
498 #define CHECK_INTERVAL 100 /* 100ms */
499 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
500 uint8_t portid, count, all_ports_up, print_flag = 0;
501 struct rte_eth_link link;
503 printf("\nChecking link status");
505 for (count = 0; count <= MAX_CHECK_TIME; count++) {
507 for (portid = 0; portid < port_num; portid++) {
508 if ((port_mask & (1 << portid)) == 0)
510 memset(&link, 0, sizeof(link));
511 rte_eth_link_get_nowait(portid, &link);
512 /* print link status if flag set */
513 if (print_flag == 1) {
514 if (link.link_status)
515 printf("Port %d Link Up - speed %u "
516 "Mbps - %s\n", (uint8_t)portid,
517 (unsigned)link.link_speed,
518 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
519 ("full-duplex") : ("half-duplex\n"));
521 printf("Port %d Link Down\n",
525 /* clear all_ports_up flag if any link down */
526 if (link.link_status == ETH_LINK_DOWN) {
531 /* after finally printing all link status, get out */
535 if (all_ports_up == 0) {
538 rte_delay_ms(CHECK_INTERVAL);
541 /* set the print_flag if all ports up or timeout */
542 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
550 main(int argc, char **argv)
552 struct lcore_queue_conf *qconf;
553 struct rte_eth_dev_info dev_info;
556 uint8_t portid, portid_last = 0;
557 unsigned lcore_id, rx_lcore_id;
558 unsigned nb_ports_in_mask = 0;
561 ret = rte_eal_init(argc, argv);
563 rte_exit(EXIT_FAILURE, "rte_eal_init failed");
567 /* parse application arguments (after the EAL ones) */
568 ret = lsi_parse_args(argc, argv);
570 rte_exit(EXIT_FAILURE, "Invalid arguments");
572 /* create the mbuf pool */
574 rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32, 0,
575 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
576 if (lsi_pktmbuf_pool == NULL)
577 rte_panic("Cannot init mbuf pool\n");
579 nb_ports = rte_eth_dev_count();
581 rte_panic("No Ethernet port - bye\n");
584 * Each logical core is assigned a dedicated TX queue on each port.
586 for (portid = 0; portid < nb_ports; portid++) {
587 /* skip ports that are not enabled */
588 if ((lsi_enabled_port_mask & (1 << portid)) == 0)
591 /* save the destination port id */
592 if (nb_ports_in_mask % 2) {
593 lsi_dst_ports[portid] = portid_last;
594 lsi_dst_ports[portid_last] = portid;
597 portid_last = portid;
601 rte_eth_dev_info_get(portid, &dev_info);
603 if (nb_ports_in_mask < 2 || nb_ports_in_mask % 2)
604 rte_exit(EXIT_FAILURE, "Current enabled port number is %u, "
605 "but it should be even and at least 2\n",
609 qconf = &lcore_queue_conf[rx_lcore_id];
611 /* Initialize the port/queue configuration of each logical core */
612 for (portid = 0; portid < nb_ports; portid++) {
613 /* skip ports that are not enabled */
614 if ((lsi_enabled_port_mask & (1 << portid)) == 0)
617 /* get the lcore_id for this port */
618 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
619 lcore_queue_conf[rx_lcore_id].n_rx_port ==
620 lsi_rx_queue_per_lcore) {
623 if (rx_lcore_id >= RTE_MAX_LCORE)
624 rte_exit(EXIT_FAILURE, "Not enough cores\n");
626 if (qconf != &lcore_queue_conf[rx_lcore_id])
627 /* Assigned a new logical core in the loop above. */
628 qconf = &lcore_queue_conf[rx_lcore_id];
630 qconf->rx_port_list[qconf->n_rx_port] = portid;
632 printf("Lcore %u: RX port %u\n",rx_lcore_id, (unsigned) portid);
635 /* Initialise each port */
636 for (portid = 0; portid < nb_ports; portid++) {
637 /* skip ports that are not enabled */
638 if ((lsi_enabled_port_mask & (1 << portid)) == 0) {
639 printf("Skipping disabled port %u\n", (unsigned) portid);
643 printf("Initializing port %u... ", (unsigned) portid);
645 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
647 rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
648 ret, (unsigned) portid);
650 /* register lsi interrupt callback, need to be after
651 * rte_eth_dev_configure(). if (intr_conf.lsc == 0), no
652 * lsc interrupt will be present, and below callback to
653 * be registered will never be called.
655 rte_eth_dev_callback_register(portid,
656 RTE_ETH_EVENT_INTR_LSC, lsi_event_callback, NULL);
658 rte_eth_macaddr_get(portid,
659 &lsi_ports_eth_addr[portid]);
661 /* init one RX queue */
663 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
664 rte_eth_dev_socket_id(portid),
668 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, port=%u\n",
669 ret, (unsigned) portid);
671 /* init one TX queue logical core on each port */
673 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
674 rte_eth_dev_socket_id(portid),
677 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d,port=%u\n",
678 ret, (unsigned) portid);
680 /* Initialize TX buffers */
681 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
682 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
683 rte_eth_dev_socket_id(portid));
684 if (tx_buffer[portid] == NULL)
685 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
688 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
690 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
691 rte_eth_tx_buffer_count_callback,
692 &port_statistics[portid].dropped);
694 rte_exit(EXIT_FAILURE, "Cannot set error callback for "
695 "tx buffer on port %u\n", (unsigned) portid);
698 ret = rte_eth_dev_start(portid);
700 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%u\n",
701 ret, (unsigned) portid);
704 rte_eth_promiscuous_enable(portid);
706 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
708 lsi_ports_eth_addr[portid].addr_bytes[0],
709 lsi_ports_eth_addr[portid].addr_bytes[1],
710 lsi_ports_eth_addr[portid].addr_bytes[2],
711 lsi_ports_eth_addr[portid].addr_bytes[3],
712 lsi_ports_eth_addr[portid].addr_bytes[4],
713 lsi_ports_eth_addr[portid].addr_bytes[5]);
715 /* initialize port stats */
716 memset(&port_statistics, 0, sizeof(port_statistics));
719 check_all_ports_link_status(nb_ports, lsi_enabled_port_mask);
721 /* launch per-lcore init on every lcore */
722 rte_eal_mp_remote_launch(lsi_launch_one_lcore, NULL, CALL_MASTER);
723 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
724 if (rte_eal_wait_lcore(lcore_id) < 0)