4 * Copyright(c) 2010-2015 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.
37 #include <rte_ethdev.h>
38 #include <rte_cycles.h>
39 #include <rte_lcore.h>
42 #define RX_RING_SIZE 128
43 #define TX_RING_SIZE 512
45 #define NUM_MBUFS 8191
46 #define MBUF_CACHE_SIZE 250
49 static const struct rte_eth_conf port_conf_default = {
50 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN, },
53 static unsigned nb_ports;
56 uint64_t total_cycles;
62 add_timestamps(uint8_t port __rte_unused, uint16_t qidx __rte_unused,
63 struct rte_mbuf **pkts, uint16_t nb_pkts,
64 uint16_t max_pkts __rte_unused, void *_ __rte_unused)
67 uint64_t now = rte_rdtsc();
69 for (i = 0; i < nb_pkts; i++)
70 pkts[i]->udata64 = now;
75 calc_latency(uint8_t port __rte_unused, uint16_t qidx __rte_unused,
76 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
79 uint64_t now = rte_rdtsc();
82 for (i = 0; i < nb_pkts; i++)
83 cycles += now - pkts[i]->udata64;
84 latency_numbers.total_cycles += cycles;
85 latency_numbers.total_pkts += nb_pkts;
87 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
88 printf("Latency = %"PRIu64" cycles\n",
89 latency_numbers.total_cycles / latency_numbers.total_pkts);
90 latency_numbers.total_cycles = latency_numbers.total_pkts = 0;
96 * Initialises a given port using global settings and with the rx buffers
97 * coming from the mbuf_pool passed as parameter
100 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
102 struct rte_eth_conf port_conf = port_conf_default;
103 const uint16_t rx_rings = 1, tx_rings = 1;
107 if (port >= rte_eth_dev_count())
110 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
114 for (q = 0; q < rx_rings; q++) {
115 retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
116 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
121 for (q = 0; q < tx_rings; q++) {
122 retval = rte_eth_tx_queue_setup(port, q, TX_RING_SIZE,
123 rte_eth_dev_socket_id(port), NULL);
128 retval = rte_eth_dev_start(port);
132 struct ether_addr addr;
134 rte_eth_macaddr_get(port, &addr);
135 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
136 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
138 addr.addr_bytes[0], addr.addr_bytes[1],
139 addr.addr_bytes[2], addr.addr_bytes[3],
140 addr.addr_bytes[4], addr.addr_bytes[5]);
142 rte_eth_promiscuous_enable(port);
143 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
144 rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
150 * Main thread that does the work, reading from INPUT_PORT
151 * and writing to OUTPUT_PORT
153 static __attribute__((noreturn)) void
158 for (port = 0; port < nb_ports; port++)
159 if (rte_eth_dev_socket_id(port) > 0 &&
160 rte_eth_dev_socket_id(port) !=
161 (int)rte_socket_id())
162 printf("WARNING, port %u is on remote NUMA node to "
163 "polling thread.\n\tPerformance will "
164 "not be optimal.\n", port);
166 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
169 for (port = 0; port < nb_ports; port++) {
170 struct rte_mbuf *bufs[BURST_SIZE];
171 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
173 if (unlikely(nb_rx == 0))
175 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
177 if (unlikely(nb_tx < nb_rx)) {
180 for (buf = nb_tx; buf < nb_rx; buf++)
181 rte_pktmbuf_free(bufs[buf]);
187 /* Main function, does initialisation and calls the per-lcore functions */
189 main(int argc, char *argv[])
191 struct rte_mempool *mbuf_pool;
195 int ret = rte_eal_init(argc, argv);
198 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
202 nb_ports = rte_eth_dev_count();
203 if (nb_ports < 2 || (nb_ports & 1))
204 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
206 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
207 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
208 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
209 if (mbuf_pool == NULL)
210 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
212 /* initialize all ports */
213 for (portid = 0; portid < nb_ports; portid++)
214 if (port_init(portid, mbuf_pool) != 0)
215 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
218 if (rte_lcore_count() > 1)
219 printf("\nWARNING: Too much enabled lcores - "
220 "App uses only 1 lcore\n");
222 /* call lcore_main on master core only */