4 * Copyright(c) 2010-2013 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.
43 #include <sys/queue.h>
45 #include <netinet/ip.h>
47 #include <rte_common.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
52 #include <rte_byteorder.h>
53 #include <rte_launch.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56 #include <rte_branch_prediction.h>
57 #include <rte_atomic.h>
60 #include <rte_debug.h>
61 #include <rte_mempool.h>
62 #include <rte_memcpy.h>
64 #include <rte_ether.h>
65 #include <rte_interrupts.h>
67 #include <rte_ethdev.h>
68 #include <rte_byteorder.h>
69 #include <rte_malloc.h>
70 #include <rte_fbk_hash.h>
71 #include <rte_string_fns.h>
79 * When doing reads from the NIC or the client queues,
82 #define PACKET_READ_SIZE 32
85 * Local buffers to put packets in, used to send packets in bursts to the
88 struct client_rx_buf {
89 struct rte_mbuf *buffer[PACKET_READ_SIZE];
93 /* One buffer per client rx queue - dynamically allocate array */
94 static struct client_rx_buf *cl_rx_buf;
97 get_printable_mac_addr(uint8_t port)
99 static const char err_address[] = "00:00:00:00:00:00";
100 static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
102 if (unlikely(port >= RTE_MAX_ETHPORTS))
104 if (unlikely(addresses[port][0]=='\0')){
105 struct ether_addr mac;
106 rte_eth_macaddr_get(port, &mac);
107 rte_snprintf(addresses[port], sizeof(addresses[port]),
108 "%02x:%02x:%02x:%02x:%02x:%02x\n",
109 mac.addr_bytes[0], mac.addr_bytes[1], mac.addr_bytes[2],
110 mac.addr_bytes[3], mac.addr_bytes[4], mac.addr_bytes[5]);
112 return addresses[port];
116 * This function displays the recorded statistics for each port
117 * and for each client. It uses ANSI terminal codes to clear
118 * screen when called. It is called from a single non-master
119 * thread in the server process, when the process is run with more
120 * than one lcore enabled.
123 do_stats_display(void)
126 const char clr[] = { 27, '[', '2', 'J', '\0' };
127 const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
128 uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
129 uint64_t client_tx[MAX_CLIENTS], client_tx_drop[MAX_CLIENTS];
131 /* to get TX stats, we need to do some summing calculations */
132 memset(port_tx, 0, sizeof(port_tx));
133 memset(port_tx_drop, 0, sizeof(port_tx_drop));
134 memset(client_tx, 0, sizeof(client_tx));
135 memset(client_tx_drop, 0, sizeof(client_tx_drop));
137 for (i = 0; i < num_clients; i++){
138 const volatile struct tx_stats *tx = &ports->tx_stats[i];
139 for (j = 0; j < ports->num_ports; j++){
140 /* assign to local variables here, save re-reading volatile vars */
141 const uint64_t tx_val = tx->tx[ports->id[j]];
142 const uint64_t drop_val = tx->tx_drop[ports->id[j]];
143 port_tx[j] += tx_val;
144 port_tx_drop[j] += drop_val;
145 client_tx[i] += tx_val;
146 client_tx_drop[i] += drop_val;
150 /* Clear screen and move to top left */
151 printf("%s%s", clr, topLeft);
155 for (i = 0; i < ports->num_ports; i++)
156 printf("Port %u: '%s'\t", (unsigned)ports->id[i],
157 get_printable_mac_addr(ports->id[i]));
159 for (i = 0; i < ports->num_ports; i++){
160 printf("Port %u - rx: %9"PRIu64"\t"
162 (unsigned)ports->id[i], ports->rx_stats.rx[i],
166 printf("\nCLIENTS\n");
168 for (i = 0; i < num_clients; i++){
169 const unsigned long long rx = clients[i].stats.rx;
170 const unsigned long long rx_drop = clients[i].stats.rx_drop;
171 printf("Client %2u - rx: %9llu, rx_drop: %9llu\n"
172 " tx: %9"PRIu64", tx_drop: %9"PRIu64"\n",
173 i, rx, rx_drop, client_tx[i], client_tx_drop[i]);
180 * The function called from each non-master lcore used by the process.
181 * The test_and_set function is used to randomly pick a single lcore on which
182 * the code to display the statistics will run. Otherwise, the code just
186 sleep_lcore(__attribute__((unused)) void *dummy)
188 /* Used to pick a display thread - static, so zero-initialised */
189 static rte_atomic32_t display_stats;
191 /* Only one core should display stats */
192 if (rte_atomic32_test_and_set(&display_stats)) {
193 const unsigned sleeptime = 1;
194 printf("Core %u displaying statistics\n", rte_lcore_id());
196 /* Longer initial pause so above printf is seen */
197 sleep(sleeptime * 3);
199 /* Loop forever: sleep always returns 0 or <= param */
200 while (sleep(sleeptime) <= sleeptime)
207 * Function to set all the client statistic values to zero.
208 * Called at program startup.
215 for (i = 0; i < num_clients; i++)
216 clients[i].stats.rx = clients[i].stats.rx_drop = 0;
220 * send a burst of traffic to a client, assuming there are packets
221 * available to be sent to this client
224 flush_rx_queue(uint16_t client)
229 if (cl_rx_buf[client].count == 0)
232 cl = &clients[client];
233 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[client].buffer,
234 cl_rx_buf[client].count) != 0){
235 for (j = 0; j < cl_rx_buf[client].count; j++)
236 rte_pktmbuf_free(cl_rx_buf[client].buffer[j]);
237 cl->stats.rx_drop += cl_rx_buf[client].count;
240 cl->stats.rx += cl_rx_buf[client].count;
242 cl_rx_buf[client].count = 0;
246 * marks a packet down to be sent to a particular client process
249 enqueue_rx_packet(uint8_t client, struct rte_mbuf *buf)
251 cl_rx_buf[client].buffer[cl_rx_buf[client].count++] = buf;
255 * This function takes a group of packets and routes them
256 * individually to the client process. Very simply round-robins the packets
257 * without checking any of the packet contents.
260 process_packets(uint32_t port_num __rte_unused,
261 struct rte_mbuf *pkts[], uint16_t rx_count)
266 for (i = 0; i < rx_count; i++) {
267 enqueue_rx_packet(client, pkts[i]);
269 if (++client == num_clients)
273 for (i = 0; i < num_clients; i++)
278 * Function called by the master lcore of the DPDK process.
281 do_packet_forwarding(void)
283 unsigned port_num = 0; /* indexes the port[] array */
286 struct rte_mbuf *buf[PACKET_READ_SIZE];
290 rx_count = rte_eth_rx_burst(ports->id[port_num], 0, \
291 buf, PACKET_READ_SIZE);
292 ports->rx_stats.rx[port_num] += rx_count;
294 /* Now process the NIC packets read */
295 if (likely(rx_count > 0))
296 process_packets(port_num, buf, rx_count);
298 /* move to next port */
299 if (++port_num == ports->num_ports)
305 MAIN(int argc, char *argv[])
307 /* initialise the system */
308 if (init(argc, argv) < 0 )
310 RTE_LOG(INFO, APP, "Finished Process Init.\n");
312 cl_rx_buf = calloc(num_clients, sizeof(cl_rx_buf[0]));
314 /* clear statistics */
317 /* put all other cores to sleep bar master */
318 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
320 do_packet_forwarding();