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.
42 #include <sys/queue.h>
44 #include <netinet/ip.h>
46 #include <rte_common.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
50 #include <rte_byteorder.h>
51 #include <rte_launch.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_branch_prediction.h>
55 #include <rte_atomic.h>
58 #include <rte_debug.h>
59 #include <rte_mempool.h>
60 #include <rte_memcpy.h>
62 #include <rte_ether.h>
63 #include <rte_interrupts.h>
65 #include <rte_ethdev.h>
66 #include <rte_byteorder.h>
67 #include <rte_malloc.h>
68 #include <rte_string_fns.h>
77 * When doing reads from the NIC or the node queues,
80 #define PACKET_READ_SIZE 32
83 * Local buffers to put packets in, used to send packets in bursts to the
87 struct rte_mbuf *buffer[PACKET_READ_SIZE];
96 /* One buffer per node rx queue - dynamically allocate array */
97 static struct node_rx_buf *cl_rx_buf;
100 get_printable_mac_addr(uint8_t port)
102 static const char err_address[] = "00:00:00:00:00:00";
103 static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
104 struct ether_addr mac;
106 if (unlikely(port >= RTE_MAX_ETHPORTS))
108 if (unlikely(addresses[port][0] == '\0')) {
109 rte_eth_macaddr_get(port, &mac);
110 snprintf(addresses[port], sizeof(addresses[port]),
111 "%02x:%02x:%02x:%02x:%02x:%02x\n",
112 mac.addr_bytes[0], mac.addr_bytes[1],
113 mac.addr_bytes[2], mac.addr_bytes[3],
114 mac.addr_bytes[4], mac.addr_bytes[5]);
116 return addresses[port];
120 * This function displays the recorded statistics for each port
121 * and for each node. It uses ANSI terminal codes to clear
122 * screen when called. It is called from a single non-master
123 * thread in the server process, when the process is run with more
124 * than one lcore enabled.
127 do_stats_display(void)
130 const char clr[] = {27, '[', '2', 'J', '\0'};
131 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
132 uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
133 uint64_t node_tx[MAX_NODES], node_tx_drop[MAX_NODES];
135 /* to get TX stats, we need to do some summing calculations */
136 memset(port_tx, 0, sizeof(port_tx));
137 memset(port_tx_drop, 0, sizeof(port_tx_drop));
138 memset(node_tx, 0, sizeof(node_tx));
139 memset(node_tx_drop, 0, sizeof(node_tx_drop));
141 for (i = 0; i < num_nodes; i++) {
142 const struct tx_stats *tx = &info->tx_stats[i];
144 for (j = 0; j < info->num_ports; j++) {
145 const uint64_t tx_val = tx->tx[info->id[j]];
146 const uint64_t drop_val = tx->tx_drop[info->id[j]];
148 port_tx[j] += tx_val;
149 port_tx_drop[j] += drop_val;
150 node_tx[i] += tx_val;
151 node_tx_drop[i] += drop_val;
155 /* Clear screen and move to top left */
156 printf("%s%s", clr, topLeft);
160 for (i = 0; i < info->num_ports; i++)
161 printf("Port %u: '%s'\t", (unsigned int)info->id[i],
162 get_printable_mac_addr(info->id[i]));
164 for (i = 0; i < info->num_ports; i++) {
165 printf("Port %u - rx: %9"PRIu64"\t"
167 (unsigned int)info->id[i], info->rx_stats.rx[i],
171 printf("\nSERVER\n");
173 printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
174 flow_dist_stats.distributed, flow_dist_stats.drop);
178 for (i = 0; i < num_nodes; i++) {
179 const unsigned long long rx = nodes[i].stats.rx;
180 const unsigned long long rx_drop = nodes[i].stats.rx_drop;
181 const struct filter_stats *filter = &info->filter_stats[i];
183 printf("Node %2u - rx: %9llu, rx_drop: %9llu\n"
184 " tx: %9"PRIu64", tx_drop: %9"PRIu64"\n"
185 " filter_passed: %9"PRIu64", "
186 "filter_drop: %9"PRIu64"\n",
187 i, rx, rx_drop, node_tx[i], node_tx_drop[i],
188 filter->passed, filter->drop);
195 * The function called from each non-master lcore used by the process.
196 * The test_and_set function is used to randomly pick a single lcore on which
197 * the code to display the statistics will run. Otherwise, the code just
201 sleep_lcore(__attribute__((unused)) void *dummy)
203 /* Used to pick a display thread - static, so zero-initialised */
204 static rte_atomic32_t display_stats;
206 /* Only one core should display stats */
207 if (rte_atomic32_test_and_set(&display_stats)) {
208 const unsigned int sleeptime = 1;
210 printf("Core %u displaying statistics\n", rte_lcore_id());
212 /* Longer initial pause so above printf is seen */
213 sleep(sleeptime * 3);
215 /* Loop forever: sleep always returns 0 or <= param */
216 while (sleep(sleeptime) <= sleeptime)
223 * Function to set all the node statistic values to zero.
224 * Called at program startup.
231 for (i = 0; i < num_nodes; i++)
232 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
236 * send a burst of traffic to a node, assuming there are packets
237 * available to be sent to this node
240 flush_rx_queue(uint16_t node)
245 if (cl_rx_buf[node].count == 0)
249 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer,
250 cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){
251 for (j = 0; j < cl_rx_buf[node].count; j++)
252 rte_pktmbuf_free(cl_rx_buf[node].buffer[j]);
253 cl->stats.rx_drop += cl_rx_buf[node].count;
255 cl->stats.rx += cl_rx_buf[node].count;
257 cl_rx_buf[node].count = 0;
261 * marks a packet down to be sent to a particular node process
264 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf)
266 cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
270 * This function takes a group of packets and routes them
271 * individually to the node process. Very simply round-robins the packets
272 * without checking any of the packet contents.
275 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[],
276 uint16_t rx_count, unsigned int socket_id)
280 efd_value_t data[RTE_EFD_BURST_MAX];
281 const void *key_ptrs[RTE_EFD_BURST_MAX];
283 struct ipv4_hdr *ipv4_hdr;
284 uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX];
286 for (i = 0; i < rx_count; i++) {
287 /* Handle IPv4 header.*/
288 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i], struct ipv4_hdr *,
289 sizeof(struct ether_hdr));
290 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
291 key_ptrs[i] = (void *)&ipv4_dst_ip[i];
294 rte_efd_lookup_bulk(efd_table, socket_id, rx_count,
295 (const void **) key_ptrs, data);
296 for (i = 0; i < rx_count; i++) {
297 node = (uint8_t) ((uintptr_t)data[i]);
299 if (node >= num_nodes) {
301 * Node is out of range, which means that
302 * flow has not been inserted
304 flow_dist_stats.drop++;
305 rte_pktmbuf_free(pkts[i]);
307 flow_dist_stats.distributed++;
308 enqueue_rx_packet(node, pkts[i]);
312 for (i = 0; i < num_nodes; i++)
317 * Function called by the master lcore of the DPDK process.
320 do_packet_forwarding(void)
322 unsigned int port_num = 0; /* indexes the port[] array */
323 unsigned int socket_id = rte_socket_id();
326 struct rte_mbuf *buf[PACKET_READ_SIZE];
330 rx_count = rte_eth_rx_burst(info->id[port_num], 0,
331 buf, PACKET_READ_SIZE);
332 info->rx_stats.rx[port_num] += rx_count;
334 /* Now process the NIC packets read */
335 if (likely(rx_count > 0))
336 process_packets(port_num, buf, rx_count, socket_id);
338 /* move to next port */
339 if (++port_num == info->num_ports)
345 main(int argc, char *argv[])
347 /* initialise the system */
348 if (init(argc, argv) < 0)
350 RTE_LOG(INFO, APP, "Finished Process Init.\n");
352 cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
354 /* clear statistics */
357 /* put all other cores to sleep bar master */
358 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
360 do_packet_forwarding();