4 * Copyright(c) 2010-2012 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.
40 #include <sys/types.h>
42 #include <sys/queue.h>
43 #include <netinet/in.h>
50 #include <rte_common.h>
52 #include <rte_memory.h>
53 #include <rte_memcpy.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
57 #include <rte_per_lcore.h>
58 #include <rte_launch.h>
59 #include <rte_atomic.h>
60 #include <rte_cycles.h>
61 #include <rte_prefetch.h>
62 #include <rte_lcore.h>
63 #include <rte_per_lcore.h>
64 #include <rte_branch_prediction.h>
65 #include <rte_interrupts.h>
67 #include <rte_random.h>
68 #include <rte_debug.h>
69 #include <rte_ether.h>
70 #include <rte_ethdev.h>
72 #include <rte_mempool.h>
77 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
79 #define L2FWD_MAX_PORTS 32
81 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
85 * RX and TX Prefetch, Host, and Write-back threshold values should be
86 * carefully set for optimal performance. Consult the network
87 * controller's datasheet and supporting DPDK documentation for guidance
88 * on how these parameters should be set.
90 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
91 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
92 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
95 * These default values are optimized for use with the Intel(R) 82599 10 GbE
96 * Controller and the DPDK ixgbe PMD. Consider using other values for other
97 * network controllers and/or network drivers.
99 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
100 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
101 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
103 #define MAX_PKT_BURST 32
104 #define BURST_TX_DRAIN 200000ULL /* around 100us at 2 Ghz */
109 * Configurable number of RX/TX ring descriptors
111 #define RTE_TEST_RX_DESC_DEFAULT 128
112 #define RTE_TEST_TX_DESC_DEFAULT 512
113 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
114 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
116 /* ethernet addresses of ports */
117 static struct ether_addr l2fwd_ports_eth_addr[L2FWD_MAX_PORTS];
119 /* mask of enabled ports */
120 static uint32_t l2fwd_enabled_port_mask = 0;
122 /* list of enabled ports */
123 static uint32_t l2fwd_dst_ports[L2FWD_MAX_PORTS];
125 static unsigned int l2fwd_rx_queue_per_lcore = 1;
127 #define MAX_PKT_BURST 32
130 struct rte_mbuf *m_table[MAX_PKT_BURST];
133 #define MAX_RX_QUEUE_PER_LCORE 16
134 #define MAX_TX_QUEUE_PER_PORT 16
135 struct lcore_queue_conf {
137 unsigned rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
138 unsigned tx_queue_id;
139 struct mbuf_table tx_mbufs[L2FWD_MAX_PORTS];
141 } __rte_cache_aligned;
142 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
144 static const struct rte_eth_conf port_conf = {
147 .header_split = 0, /**< Header Split disabled */
148 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
149 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
150 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
151 .hw_strip_crc = 0, /**< CRC stripped by hardware */
157 static const struct rte_eth_rxconf rx_conf = {
159 .pthresh = RX_PTHRESH,
160 .hthresh = RX_HTHRESH,
161 .wthresh = RX_WTHRESH,
165 static const struct rte_eth_txconf tx_conf = {
167 .pthresh = TX_PTHRESH,
168 .hthresh = TX_HTHRESH,
169 .wthresh = TX_WTHRESH,
171 .tx_free_thresh = 0, /* Use PMD default values */
172 .tx_rs_thresh = 0, /* Use PMD default values */
175 struct rte_mempool * l2fwd_pktmbuf_pool = NULL;
177 /* Per-port statistics struct */
178 struct l2fwd_port_statistics {
182 } __rte_cache_aligned;
183 struct l2fwd_port_statistics port_statistics[L2FWD_MAX_PORTS];
185 /* A tsc-based timer responsible for triggering statistics printout */
186 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
187 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
188 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
190 /* Print out statistics on packets dropped */
194 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
197 total_packets_dropped = 0;
198 total_packets_tx = 0;
199 total_packets_rx = 0;
201 const char clr[] = { 27, '[', '2', 'J', '\0' };
202 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
204 /* Clear screen and move to top left */
205 printf("%s%s", clr, topLeft);
207 printf("\nPort statistics ====================================");
209 for (portid = 0; portid < L2FWD_MAX_PORTS; portid++) {
210 /* skip disabled ports */
211 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
213 printf("\nStatistics for port %u ------------------------------"
214 "\nPackets sent: %24"PRIu64
215 "\nPackets received: %20"PRIu64
216 "\nPackets dropped: %21"PRIu64,
218 port_statistics[portid].tx,
219 port_statistics[portid].rx,
220 port_statistics[portid].dropped);
222 total_packets_dropped += port_statistics[portid].dropped;
223 total_packets_tx += port_statistics[portid].tx;
224 total_packets_rx += port_statistics[portid].rx;
226 printf("\nAggregate statistics ==============================="
227 "\nTotal packets sent: %18"PRIu64
228 "\nTotal packets received: %14"PRIu64
229 "\nTotal packets dropped: %15"PRIu64,
232 total_packets_dropped);
233 printf("\n====================================================\n");
236 /* Send the packet on an output interface */
238 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
240 struct rte_mbuf **m_table;
244 queueid = (uint16_t) qconf->tx_queue_id;
245 m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
247 ret = rte_eth_tx_burst(port, (uint16_t) queueid, m_table, (uint16_t) n);
248 port_statistics[port].tx += ret;
249 if (unlikely(ret < n)) {
250 port_statistics[port].dropped += (n - ret);
252 rte_pktmbuf_free(m_table[ret]);
259 /* Send the packet on an output interface */
261 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
263 unsigned lcore_id, len;
264 struct lcore_queue_conf *qconf;
266 lcore_id = rte_lcore_id();
268 qconf = &lcore_queue_conf[lcore_id];
269 len = qconf->tx_mbufs[port].len;
270 qconf->tx_mbufs[port].m_table[len] = m;
273 /* enough pkts to be sent */
274 if (unlikely(len == MAX_PKT_BURST)) {
275 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
279 qconf->tx_mbufs[port].len = len;
284 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
286 struct ether_hdr *eth;
290 dst_port = l2fwd_dst_ports[portid];
291 eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
293 /* 00:09:c0:00:00:xx */
294 tmp = ð->d_addr.addr_bytes[0];
295 *((uint64_t *)tmp) = 0x000000c00900 + (dst_port << 24);
298 ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->s_addr);
300 l2fwd_send_packet(m, (uint8_t) dst_port);
303 /* main processing loop */
305 l2fwd_main_loop(void)
307 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
310 uint64_t prev_tsc = 0;
311 uint64_t diff_tsc, cur_tsc, timer_tsc;
312 unsigned i, j, portid, nb_rx;
313 struct lcore_queue_conf *qconf;
317 lcore_id = rte_lcore_id();
318 qconf = &lcore_queue_conf[lcore_id];
320 if (qconf->n_rx_queue == 0) {
321 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
325 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
327 for (i = 0; i < qconf->n_rx_queue; i++) {
329 portid = qconf->rx_queue_list[i];
330 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
336 cur_tsc = rte_rdtsc();
339 * TX burst queue drain
341 diff_tsc = cur_tsc - prev_tsc;
342 if (unlikely(diff_tsc > BURST_TX_DRAIN)) {
344 /* this could be optimized (use queueid instead of
345 * portid), but it is not called so often */
346 for (portid = 0; portid < L2FWD_MAX_PORTS; portid++) {
347 if (qconf->tx_mbufs[portid].len == 0)
349 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
350 qconf->tx_mbufs[portid].len,
352 qconf->tx_mbufs[portid].len = 0;
355 /* if timer is enabled */
356 if (timer_period > 0) {
358 /* advance the timer */
359 timer_tsc += diff_tsc;
361 /* if timer has reached its timeout */
362 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
364 /* do this only on master core */
365 if (lcore_id == rte_get_master_lcore()) {
367 /* reset the timer */
377 * Read packet from RX queues
379 for (i = 0; i < qconf->n_rx_queue; i++) {
381 portid = qconf->rx_queue_list[i];
382 nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
383 pkts_burst, MAX_PKT_BURST);
385 port_statistics[portid].rx += nb_rx;
387 for (j = 0; j < nb_rx; j++) {
389 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
390 l2fwd_simple_forward(m, portid);
397 l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy)
405 l2fwd_usage(const char *prgname)
407 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
408 " -p PORTMASK: hexadecimal bitmask of ports to configure\n"
409 " -q NQ: number of queue (=ports) per lcore (default is 1)\n"
410 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
415 l2fwd_parse_portmask(const char *portmask)
420 /* parse hexadecimal string */
421 pm = strtoul(portmask, &end, 16);
422 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
432 l2fwd_parse_nqueue(const char *q_arg)
437 /* parse hexadecimal string */
438 n = strtoul(q_arg, &end, 10);
439 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
443 if (n >= MAX_RX_QUEUE_PER_LCORE)
450 l2fwd_parse_timer_period(const char *q_arg)
455 /* parse number string */
456 n = strtol(q_arg, &end, 10);
457 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
459 if (n >= MAX_TIMER_PERIOD)
465 /* Parse the argument given in the command line of the application */
467 l2fwd_parse_args(int argc, char **argv)
472 char *prgname = argv[0];
473 static struct option lgopts[] = {
479 while ((opt = getopt_long(argc, argvopt, "p:q:T:",
480 lgopts, &option_index)) != EOF) {
485 l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
486 if (l2fwd_enabled_port_mask == 0) {
487 printf("invalid portmask\n");
488 l2fwd_usage(prgname);
495 l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
496 if (l2fwd_rx_queue_per_lcore == 0) {
497 printf("invalid queue number\n");
498 l2fwd_usage(prgname);
505 timer_period = l2fwd_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
506 if (timer_period < 0) {
507 printf("invalid timer period\n");
508 l2fwd_usage(prgname);
515 l2fwd_usage(prgname);
519 l2fwd_usage(prgname);
525 argv[optind-1] = prgname;
528 optind = 0; /* reset getopt lib */
533 MAIN(int argc, char **argv)
535 struct lcore_queue_conf *qconf;
536 struct rte_eth_dev_info dev_info;
537 struct rte_eth_link link;
539 unsigned int nb_ports, nb_lcores;
540 unsigned portid, last_port, queueid = 0;
541 unsigned lcore_id, rx_lcore_id;
542 unsigned n_tx_queue, max_tx_queues;
543 unsigned nb_ports_in_mask = 0;
546 ret = rte_eal_init(argc, argv);
548 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
552 /* parse application arguments (after the EAL ones) */
553 ret = l2fwd_parse_args(argc, argv);
555 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
557 /* create the mbuf pool */
559 rte_mempool_create("mbuf_pool", NB_MBUF,
561 sizeof(struct rte_pktmbuf_pool_private),
562 rte_pktmbuf_pool_init, NULL,
563 rte_pktmbuf_init, NULL,
565 if (l2fwd_pktmbuf_pool == NULL)
566 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
569 if (rte_pmd_init_all() < 0)
570 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
572 if (rte_eal_pci_probe() < 0)
573 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
575 nb_ports = rte_eth_dev_count();
577 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
579 if (nb_ports > L2FWD_MAX_PORTS)
580 nb_ports = L2FWD_MAX_PORTS;
582 nb_lcores = rte_lcore_count();
584 /* reset l2fwd_dst_ports */
585 for (portid = 0; portid < L2FWD_MAX_PORTS; portid++)
586 l2fwd_dst_ports[portid] = 0;
590 * Each logical core is assigned a dedicated TX queue on each port.
591 * Compute the maximum number of TX queues that can be used.
593 max_tx_queues = nb_lcores;
594 for (portid = 0; portid < nb_ports; portid++) {
595 /* skip ports that are not enabled */
596 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
599 if (nb_ports_in_mask % 2) {
600 l2fwd_dst_ports[portid] = last_port;
601 l2fwd_dst_ports[last_port] = portid;
608 rte_eth_dev_info_get((uint8_t) portid, &dev_info);
609 if (max_tx_queues > dev_info.max_tx_queues)
610 max_tx_queues = dev_info.max_tx_queues;
613 if (nb_ports_in_mask < 2 || nb_ports_in_mask % 2) {
614 rte_exit(EXIT_FAILURE, "invalid number of ports in portmask. "
615 "Should be an even number.\n");
622 /* Initialize the port/queue configuration of each logical core */
623 for (portid = 0; portid < nb_ports; portid++) {
624 /* skip ports that are not enabled */
625 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
628 /* get the lcore_id for this port */
629 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
630 lcore_queue_conf[rx_lcore_id].n_rx_queue ==
631 l2fwd_rx_queue_per_lcore) {
634 if (rx_lcore_id >= RTE_MAX_LCORE)
635 rte_exit(EXIT_FAILURE, "Not enough cores\n");
637 if (qconf != &lcore_queue_conf[rx_lcore_id]) {
638 if (n_tx_queue == max_tx_queues)
639 rte_exit(EXIT_FAILURE,
640 "Not enough TX queues\n");
641 /* Assigned a new logical core in the loop above. */
642 qconf = &lcore_queue_conf[rx_lcore_id];
643 qconf->tx_queue_id = n_tx_queue;
646 qconf->rx_queue_list[qconf->n_rx_queue] = portid;
648 printf("Lcore %u: RX port %u TX queue %u\n",
649 rx_lcore_id, portid, qconf->tx_queue_id);
652 /* Initialise each port */
653 for (portid = 0; portid < nb_ports; portid++) {
655 /* skip ports that are not enabled */
656 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
657 printf("Skipping disabled port %u\n", portid);
661 printf("Initializing port %u... ", portid);
663 ret = rte_eth_dev_configure((uint8_t) portid, 1,
664 (uint16_t) n_tx_queue, &port_conf);
666 rte_exit(EXIT_FAILURE, "Cannot configure device: "
670 rte_eth_macaddr_get((uint8_t) portid,
671 &l2fwd_ports_eth_addr[portid]);
673 /* init one RX queue */
675 ret = rte_eth_rx_queue_setup((uint8_t) portid, 0, nb_rxd,
679 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
683 /* init one TX queue logical core on each port */
684 for (queueid = 0; queueid < n_tx_queue; queueid++) {
686 ret = rte_eth_tx_queue_setup((uint8_t) portid,
687 (uint16_t) queueid, nb_txd,
690 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
691 "err=%d, port=%u queue=%u\n",
692 ret, portid, queueid);
696 ret = rte_eth_dev_start((uint8_t) portid);
698 rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
704 /* get link status */
705 rte_eth_link_get((uint8_t) portid, &link);
706 if (link.link_status) {
707 printf(" Link Up - speed %u Mbps - %s\n",
708 (unsigned) link.link_speed,
709 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
710 ("full-duplex") : ("half-duplex\n"));
712 printf(" Link Down\n");
715 rte_eth_promiscuous_enable((uint8_t)portid);
717 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
719 l2fwd_ports_eth_addr[portid].addr_bytes[0],
720 l2fwd_ports_eth_addr[portid].addr_bytes[1],
721 l2fwd_ports_eth_addr[portid].addr_bytes[2],
722 l2fwd_ports_eth_addr[portid].addr_bytes[3],
723 l2fwd_ports_eth_addr[portid].addr_bytes[4],
724 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
726 /* initialize port stats */
727 memset(&port_statistics, 0, sizeof(port_statistics));
730 /* launch per-lcore init on every lcore */
731 rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MASTER);
732 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
733 if (rte_eal_wait_lcore(lcore_id) < 0)