4 * Copyright(c) 2016-2017 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.
41 #include <sys/queue.h>
43 #include <netinet/ip.h>
45 #include <rte_common.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
49 #include <rte_launch.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_atomic.h>
56 #include <rte_debug.h>
57 #include <rte_mempool.h>
58 #include <rte_memcpy.h>
60 #include <rte_ether.h>
61 #include <rte_interrupts.h>
63 #include <rte_ethdev.h>
64 #include <rte_byteorder.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
75 * When doing reads from the NIC or the node queues,
78 #define PACKET_READ_SIZE 32
81 * Local buffers to put packets in, used to send packets in bursts to the
85 struct rte_mbuf *buffer[PACKET_READ_SIZE];
94 /* One buffer per node rx queue - dynamically allocate array */
95 static struct node_rx_buf *cl_rx_buf;
98 get_printable_mac_addr(uint16_t port)
100 static const char err_address[] = "00:00:00:00:00:00";
101 static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
102 struct ether_addr mac;
104 if (unlikely(port >= RTE_MAX_ETHPORTS))
106 if (unlikely(addresses[port][0] == '\0')) {
107 rte_eth_macaddr_get(port, &mac);
108 snprintf(addresses[port], sizeof(addresses[port]),
109 "%02x:%02x:%02x:%02x:%02x:%02x\n",
110 mac.addr_bytes[0], mac.addr_bytes[1],
111 mac.addr_bytes[2], mac.addr_bytes[3],
112 mac.addr_bytes[4], mac.addr_bytes[5]);
114 return addresses[port];
118 * This function displays the recorded statistics for each port
119 * and for each node. It uses ANSI terminal codes to clear
120 * screen when called. It is called from a single non-master
121 * thread in the server process, when the process is run with more
122 * than one lcore enabled.
125 do_stats_display(void)
128 const char clr[] = {27, '[', '2', 'J', '\0'};
129 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
130 uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
131 uint64_t node_tx[MAX_NODES], node_tx_drop[MAX_NODES];
133 /* to get TX stats, we need to do some summing calculations */
134 memset(port_tx, 0, sizeof(port_tx));
135 memset(port_tx_drop, 0, sizeof(port_tx_drop));
136 memset(node_tx, 0, sizeof(node_tx));
137 memset(node_tx_drop, 0, sizeof(node_tx_drop));
139 for (i = 0; i < num_nodes; i++) {
140 const struct tx_stats *tx = &info->tx_stats[i];
142 for (j = 0; j < info->num_ports; j++) {
143 const uint64_t tx_val = tx->tx[info->id[j]];
144 const uint64_t drop_val = tx->tx_drop[info->id[j]];
146 port_tx[j] += tx_val;
147 port_tx_drop[j] += drop_val;
148 node_tx[i] += tx_val;
149 node_tx_drop[i] += drop_val;
153 /* Clear screen and move to top left */
154 printf("%s%s", clr, topLeft);
158 for (i = 0; i < info->num_ports; i++)
159 printf("Port %u: '%s'\t", (unsigned int)info->id[i],
160 get_printable_mac_addr(info->id[i]));
162 for (i = 0; i < info->num_ports; i++) {
163 printf("Port %u - rx: %9"PRIu64"\t"
165 (unsigned int)info->id[i], info->rx_stats.rx[i],
169 printf("\nSERVER\n");
171 printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
172 flow_dist_stats.distributed, flow_dist_stats.drop);
176 for (i = 0; i < num_nodes; i++) {
177 const unsigned long long rx = nodes[i].stats.rx;
178 const unsigned long long rx_drop = nodes[i].stats.rx_drop;
179 const struct filter_stats *filter = &info->filter_stats[i];
181 printf("Node %2u - rx: %9llu, rx_drop: %9llu\n"
182 " tx: %9"PRIu64", tx_drop: %9"PRIu64"\n"
183 " filter_passed: %9"PRIu64", "
184 "filter_drop: %9"PRIu64"\n",
185 i, rx, rx_drop, node_tx[i], node_tx_drop[i],
186 filter->passed, filter->drop);
193 * The function called from each non-master lcore used by the process.
194 * The test_and_set function is used to randomly pick a single lcore on which
195 * the code to display the statistics will run. Otherwise, the code just
199 sleep_lcore(__attribute__((unused)) void *dummy)
201 /* Used to pick a display thread - static, so zero-initialised */
202 static rte_atomic32_t display_stats;
204 /* Only one core should display stats */
205 if (rte_atomic32_test_and_set(&display_stats)) {
206 const unsigned int sleeptime = 1;
208 printf("Core %u displaying statistics\n", rte_lcore_id());
210 /* Longer initial pause so above printf is seen */
211 sleep(sleeptime * 3);
213 /* Loop forever: sleep always returns 0 or <= param */
214 while (sleep(sleeptime) <= sleeptime)
221 * Function to set all the node statistic values to zero.
222 * Called at program startup.
229 for (i = 0; i < num_nodes; i++)
230 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
234 * send a burst of traffic to a node, assuming there are packets
235 * available to be sent to this node
238 flush_rx_queue(uint16_t node)
243 if (cl_rx_buf[node].count == 0)
247 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer,
248 cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){
249 for (j = 0; j < cl_rx_buf[node].count; j++)
250 rte_pktmbuf_free(cl_rx_buf[node].buffer[j]);
251 cl->stats.rx_drop += cl_rx_buf[node].count;
253 cl->stats.rx += cl_rx_buf[node].count;
255 cl_rx_buf[node].count = 0;
259 * marks a packet down to be sent to a particular node process
262 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf)
264 cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
268 * This function takes a group of packets and routes them
269 * individually to the node process. Very simply round-robins the packets
270 * without checking any of the packet contents.
273 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[],
274 uint16_t rx_count, unsigned int socket_id)
278 efd_value_t data[RTE_EFD_BURST_MAX];
279 const void *key_ptrs[RTE_EFD_BURST_MAX];
281 struct ipv4_hdr *ipv4_hdr;
282 uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX];
284 for (i = 0; i < rx_count; i++) {
285 /* Handle IPv4 header.*/
286 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i], struct ipv4_hdr *,
287 sizeof(struct ether_hdr));
288 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
289 key_ptrs[i] = (void *)&ipv4_dst_ip[i];
292 rte_efd_lookup_bulk(efd_table, socket_id, rx_count,
293 (const void **) key_ptrs, data);
294 for (i = 0; i < rx_count; i++) {
295 node = (uint8_t) ((uintptr_t)data[i]);
297 if (node >= num_nodes) {
299 * Node is out of range, which means that
300 * flow has not been inserted
302 flow_dist_stats.drop++;
303 rte_pktmbuf_free(pkts[i]);
305 flow_dist_stats.distributed++;
306 enqueue_rx_packet(node, pkts[i]);
310 for (i = 0; i < num_nodes; i++)
315 * Function called by the master lcore of the DPDK process.
318 do_packet_forwarding(void)
320 unsigned int port_num = 0; /* indexes the port[] array */
321 unsigned int socket_id = rte_socket_id();
324 struct rte_mbuf *buf[PACKET_READ_SIZE];
328 rx_count = rte_eth_rx_burst(info->id[port_num], 0,
329 buf, PACKET_READ_SIZE);
330 info->rx_stats.rx[port_num] += rx_count;
332 /* Now process the NIC packets read */
333 if (likely(rx_count > 0))
334 process_packets(port_num, buf, rx_count, socket_id);
336 /* move to next port */
337 if (++port_num == info->num_ports)
343 main(int argc, char *argv[])
345 /* initialise the system */
346 if (init(argc, argv) < 0)
348 RTE_LOG(INFO, APP, "Finished Process Init.\n");
350 cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
352 /* clear statistics */
355 /* put all other cores to sleep bar master */
356 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
358 do_packet_forwarding();