xref: /dpdk/examples/server_node_efd/server/main.c (revision cbc12b0a)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <stdint.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41 #include <sys/queue.h>
42 #include <errno.h>
43 #include <netinet/ip.h>
44 
45 #include <rte_common.h>
46 #include <rte_memory.h>
47 #include <rte_memzone.h>
48 #include <rte_eal.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>
54 #include <rte_ring.h>
55 #include <rte_log.h>
56 #include <rte_debug.h>
57 #include <rte_mempool.h>
58 #include <rte_memcpy.h>
59 #include <rte_mbuf.h>
60 #include <rte_ether.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_ethdev.h>
64 #include <rte_byteorder.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67 #include <rte_efd.h>
68 #include <rte_ip.h>
69 
70 #include "common.h"
71 #include "args.h"
72 #include "init.h"
73 
74 /*
75  * When doing reads from the NIC or the node queues,
76  * use this batch size
77  */
78 #define PACKET_READ_SIZE 32
79 
80 /*
81  * Local buffers to put packets in, used to send packets in bursts to the
82  * nodes
83  */
84 struct node_rx_buf {
85 	struct rte_mbuf *buffer[PACKET_READ_SIZE];
86 	uint16_t count;
87 };
88 
89 struct efd_stats {
90 	uint64_t distributed;
91 	uint64_t drop;
92 } flow_dist_stats;
93 
94 /* One buffer per node rx queue - dynamically allocate array */
95 static struct node_rx_buf *cl_rx_buf;
96 
97 static const char *
98 get_printable_mac_addr(uint16_t port)
99 {
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;
103 
104 	if (unlikely(port >= RTE_MAX_ETHPORTS))
105 		return err_address;
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]);
113 	}
114 	return addresses[port];
115 }
116 
117 /*
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.
123  */
124 static void
125 do_stats_display(void)
126 {
127 	unsigned int i, j;
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];
132 
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));
138 
139 	for (i = 0; i < num_nodes; i++) {
140 		const struct tx_stats *tx = &info->tx_stats[i];
141 
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]];
145 
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;
150 		}
151 	}
152 
153 	/* Clear screen and move to top left */
154 	printf("%s%s", clr, topLeft);
155 
156 	printf("PORTS\n");
157 	printf("-----\n");
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]));
161 	printf("\n\n");
162 	for (i = 0; i < info->num_ports; i++) {
163 		printf("Port %u - rx: %9"PRIu64"\t"
164 				"tx: %9"PRIu64"\n",
165 				(unsigned int)info->id[i], info->rx_stats.rx[i],
166 				port_tx[i]);
167 	}
168 
169 	printf("\nSERVER\n");
170 	printf("-----\n");
171 	printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
172 			flow_dist_stats.distributed, flow_dist_stats.drop);
173 
174 	printf("\nNODES\n");
175 	printf("-------\n");
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];
180 
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);
187 	}
188 
189 	printf("\n");
190 }
191 
192 /*
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
196  * repeatedly sleeps.
197  */
198 static int
199 sleep_lcore(__attribute__((unused)) void *dummy)
200 {
201 	/* Used to pick a display thread - static, so zero-initialised */
202 	static rte_atomic32_t display_stats;
203 
204 	/* Only one core should display stats */
205 	if (rte_atomic32_test_and_set(&display_stats)) {
206 		const unsigned int sleeptime = 1;
207 
208 		printf("Core %u displaying statistics\n", rte_lcore_id());
209 
210 		/* Longer initial pause so above printf is seen */
211 		sleep(sleeptime * 3);
212 
213 		/* Loop forever: sleep always returns 0 or <= param */
214 		while (sleep(sleeptime) <= sleeptime)
215 			do_stats_display();
216 	}
217 	return 0;
218 }
219 
220 /*
221  * Function to set all the node statistic values to zero.
222  * Called at program startup.
223  */
224 static void
225 clear_stats(void)
226 {
227 	unsigned int i;
228 
229 	for (i = 0; i < num_nodes; i++)
230 		nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
231 }
232 
233 /*
234  * send a burst of traffic to a node, assuming there are packets
235  * available to be sent to this node
236  */
237 static void
238 flush_rx_queue(uint16_t node)
239 {
240 	uint16_t j;
241 	struct node *cl;
242 
243 	if (cl_rx_buf[node].count == 0)
244 		return;
245 
246 	cl = &nodes[node];
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;
252 	} else
253 		cl->stats.rx += cl_rx_buf[node].count;
254 
255 	cl_rx_buf[node].count = 0;
256 }
257 
258 /*
259  * marks a packet down to be sent to a particular node process
260  */
261 static inline void
262 enqueue_rx_packet(uint8_t node, struct rte_mbuf *buf)
263 {
264 	cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
265 }
266 
267 /*
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.
271  */
272 static void
273 process_packets(uint32_t port_num __rte_unused, struct rte_mbuf *pkts[],
274 		uint16_t rx_count, unsigned int socket_id)
275 {
276 	uint16_t i;
277 	uint8_t node;
278 	efd_value_t data[RTE_EFD_BURST_MAX];
279 	const void *key_ptrs[RTE_EFD_BURST_MAX];
280 
281 	struct ipv4_hdr *ipv4_hdr;
282 	uint32_t ipv4_dst_ip[RTE_EFD_BURST_MAX];
283 
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];
290 	}
291 
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]);
296 
297 		if (node >= num_nodes) {
298 			/*
299 			 * Node is out of range, which means that
300 			 * flow has not been inserted
301 			 */
302 			flow_dist_stats.drop++;
303 			rte_pktmbuf_free(pkts[i]);
304 		} else {
305 			flow_dist_stats.distributed++;
306 			enqueue_rx_packet(node, pkts[i]);
307 		}
308 	}
309 
310 	for (i = 0; i < num_nodes; i++)
311 		flush_rx_queue(i);
312 }
313 
314 /*
315  * Function called by the master lcore of the DPDK process.
316  */
317 static void
318 do_packet_forwarding(void)
319 {
320 	unsigned int port_num = 0; /* indexes the port[] array */
321 	unsigned int socket_id = rte_socket_id();
322 
323 	for (;;) {
324 		struct rte_mbuf *buf[PACKET_READ_SIZE];
325 		uint16_t rx_count;
326 
327 		/* read a port */
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;
331 
332 		/* Now process the NIC packets read */
333 		if (likely(rx_count > 0))
334 			process_packets(port_num, buf, rx_count, socket_id);
335 
336 		/* move to next port */
337 		if (++port_num == info->num_ports)
338 			port_num = 0;
339 	}
340 }
341 
342 int
343 main(int argc, char *argv[])
344 {
345 	/* initialise the system */
346 	if (init(argc, argv) < 0)
347 		return -1;
348 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
349 
350 	cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
351 
352 	/* clear statistics */
353 	clear_stats();
354 
355 	/* put all other cores to sleep bar master */
356 	rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
357 
358 	do_packet_forwarding();
359 	return 0;
360 }
361