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>
48 #include <rte_launch.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_atomic.h>
55 #include <rte_debug.h>
56 #include <rte_mempool.h>
57 #include <rte_memcpy.h>
59 #include <rte_ether.h>
60 #include <rte_interrupts.h>
61 #include <rte_ethdev.h>
62 #include <rte_byteorder.h>
63 #include <rte_malloc.h>
64 #include <rte_string_fns.h>
73 * When doing reads from the NIC or the node queues,
76 #define PACKET_READ_SIZE 32
79 * Local buffers to put packets in, used to send packets in bursts to the
83 struct rte_mbuf *buffer[PACKET_READ_SIZE];
92 /* One buffer per node rx queue - dynamically allocate array */
93 static struct node_rx_buf *cl_rx_buf;
96 get_printable_mac_addr(uint16_t port)
98 static const char err_address[] = "00:00:00:00:00:00";
99 static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
100 struct ether_addr mac;
102 if (unlikely(port >= RTE_MAX_ETHPORTS))
104 if (unlikely(addresses[port][0] == '\0')) {
105 rte_eth_macaddr_get(port, &mac);
106 snprintf(addresses[port], sizeof(addresses[port]),
107 "%02x:%02x:%02x:%02x:%02x:%02x\n",
108 mac.addr_bytes[0], mac.addr_bytes[1],
109 mac.addr_bytes[2], mac.addr_bytes[3],
110 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 node. 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 node_tx[MAX_NODES], node_tx_drop[MAX_NODES];
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(node_tx, 0, sizeof(node_tx));
135 memset(node_tx_drop, 0, sizeof(node_tx_drop));
137 for (i = 0; i < num_nodes; i++) {
138 const struct tx_stats *tx = &info->tx_stats[i];
140 for (j = 0; j < info->num_ports; j++) {
141 const uint64_t tx_val = tx->tx[info->id[j]];
142 const uint64_t drop_val = tx->tx_drop[info->id[j]];
144 port_tx[j] += tx_val;
145 port_tx_drop[j] += drop_val;
146 node_tx[i] += tx_val;
147 node_tx_drop[i] += drop_val;
151 /* Clear screen and move to top left */
152 printf("%s%s", clr, topLeft);
156 for (i = 0; i < info->num_ports; i++)
157 printf("Port %u: '%s'\t", (unsigned int)info->id[i],
158 get_printable_mac_addr(info->id[i]));
160 for (i = 0; i < info->num_ports; i++) {
161 printf("Port %u - rx: %9"PRIu64"\t"
163 (unsigned int)info->id[i], info->rx_stats.rx[i],
167 printf("\nSERVER\n");
169 printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
170 flow_dist_stats.distributed, flow_dist_stats.drop);
174 for (i = 0; i < num_nodes; i++) {
175 const unsigned long long rx = nodes[i].stats.rx;
176 const unsigned long long rx_drop = nodes[i].stats.rx_drop;
177 const struct filter_stats *filter = &info->filter_stats[i];
179 printf("Node %2u - rx: %9llu, rx_drop: %9llu\n"
180 " tx: %9"PRIu64", tx_drop: %9"PRIu64"\n"
181 " filter_passed: %9"PRIu64", "
182 "filter_drop: %9"PRIu64"\n",
183 i, rx, rx_drop, node_tx[i], node_tx_drop[i],
184 filter->passed, filter->drop);
191 * The function called from each non-master lcore used by the process.
192 * The test_and_set function is used to randomly pick a single lcore on which
193 * the code to display the statistics will run. Otherwise, the code just
197 sleep_lcore(__attribute__((unused)) void *dummy)
199 /* Used to pick a display thread - static, so zero-initialised */
200 static rte_atomic32_t display_stats;
202 /* Only one core should display stats */
203 if (rte_atomic32_test_and_set(&display_stats)) {
204 const unsigned int sleeptime = 1;
206 printf("Core %u displaying statistics\n", rte_lcore_id());
208 /* Longer initial pause so above printf is seen */
209 sleep(sleeptime * 3);
211 /* Loop forever: sleep always returns 0 or <= param */
212 while (sleep(sleeptime) <= sleeptime)
219 * Function to set all the node statistic values to zero.
220 * Called at program startup.
227 for (i = 0; i < num_nodes; i++)
228 nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
232 * send a burst of traffic to a node, assuming there are packets
233 * available to be sent to this node
236 flush_rx_queue(uint16_t node)
241 if (cl_rx_buf[node].count == 0)
245 if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[node].buffer,
246 cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){
247 for (j = 0; j < cl_rx_buf[node].count; j++)
248 rte_pktmbuf_free(cl_rx_buf[node].buffer[j]);
249 cl->stats.rx_drop += cl_rx_buf[node].count;
251 cl->stats.rx += cl_rx_buf[node].count;
253 cl_rx_buf[node].count = 0;
257 * marks a packet down to be sent to a particular node process
260 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf)
262 cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
266 * This function takes a group of packets and routes them
267 * individually to the node process. Very simply round-robins the packets
268 * without checking any of the packet contents.
271 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[],
272 uint16_t rx_count, unsigned int socket_id)
276 efd_value_t data[RTE_EFD_BURST_MAX];
277 const void *key_ptrs[RTE_EFD_BURST_MAX];
279 struct ipv4_hdr *ipv4_hdr;
280 uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX];
282 for (i = 0; i < rx_count; i++) {
283 /* Handle IPv4 header.*/
284 ipv4_hdr = rte_pktmbuf_mtod_offset(pkts[i], struct ipv4_hdr *,
285 sizeof(struct ether_hdr));
286 ipv4_dst_ip[i] = ipv4_hdr->dst_addr;
287 key_ptrs[i] = (void *)&ipv4_dst_ip[i];
290 rte_efd_lookup_bulk(efd_table, socket_id, rx_count,
291 (const void **) key_ptrs, data);
292 for (i = 0; i < rx_count; i++) {
293 node = (uint8_t) ((uintptr_t)data[i]);
295 if (node >= num_nodes) {
297 * Node is out of range, which means that
298 * flow has not been inserted
300 flow_dist_stats.drop++;
301 rte_pktmbuf_free(pkts[i]);
303 flow_dist_stats.distributed++;
304 enqueue_rx_packet(node, pkts[i]);
308 for (i = 0; i < num_nodes; i++)
313 * Function called by the master lcore of the DPDK process.
316 do_packet_forwarding(void)
318 unsigned int port_num = 0; /* indexes the port[] array */
319 unsigned int socket_id = rte_socket_id();
322 struct rte_mbuf *buf[PACKET_READ_SIZE];
326 rx_count = rte_eth_rx_burst(info->id[port_num], 0,
327 buf, PACKET_READ_SIZE);
328 info->rx_stats.rx[port_num] += rx_count;
330 /* Now process the NIC packets read */
331 if (likely(rx_count > 0))
332 process_packets(port_num, buf, rx_count, socket_id);
334 /* move to next port */
335 if (++port_num == info->num_ports)
341 main(int argc, char *argv[])
343 /* initialise the system */
344 if (init(argc, argv) < 0)
346 RTE_LOG(INFO, APP, "Finished Process Init.\n");
348 cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
350 /* clear statistics */
353 /* put all other cores to sleep bar master */
354 rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
356 do_packet_forwarding();