xref: /dpdk/examples/multi_process/client_server_mp/mp_server/main.c (revision 6f41fe75e2dd8dd38f7bea7b9501edd4f9b72fa5)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <inttypes.h>
42 #include <sys/queue.h>
43 #include <errno.h>
44 #include <netinet/ip.h>
45 
46 #include <rte_common.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_byteorder.h>
52 #include <rte_launch.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_branch_prediction.h>
56 #include <rte_atomic.h>
57 #include <rte_ring.h>
58 #include <rte_log.h>
59 #include <rte_debug.h>
60 #include <rte_mempool.h>
61 #include <rte_memcpy.h>
62 #include <rte_mbuf.h>
63 #include <rte_ether.h>
64 #include <rte_interrupts.h>
65 #include <rte_pci.h>
66 #include <rte_ethdev.h>
67 #include <rte_byteorder.h>
68 #include <rte_malloc.h>
69 #include <rte_fbk_hash.h>
70 #include <rte_string_fns.h>
71 
72 #include "common.h"
73 #include "args.h"
74 #include "init.h"
75 #include "main.h"
76 
77 /*
78  * When doing reads from the NIC or the client queues,
79  * use this batch size
80  */
81 #define PACKET_READ_SIZE 32
82 
83 /*
84  * Local buffers to put packets in, used to send packets in bursts to the
85  * clients
86  */
87 struct client_rx_buf {
88 	struct rte_mbuf *buffer[PACKET_READ_SIZE];
89 	uint16_t count;
90 };
91 
92 /* One buffer per client rx queue - dynamically allocate array */
93 static struct client_rx_buf *cl_rx_buf;
94 
95 static const char *
96 get_printable_mac_addr(uint8_t port)
97 {
98 	static const char err_address[] = "00:00:00:00:00:00";
99 	static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
100 
101 	if (unlikely(port >= RTE_MAX_ETHPORTS))
102 		return err_address;
103 	if (unlikely(addresses[port][0]=='\0')){
104 		struct ether_addr mac;
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], mac.addr_bytes[2],
109 				mac.addr_bytes[3], mac.addr_bytes[4], mac.addr_bytes[5]);
110 	}
111 	return addresses[port];
112 }
113 
114 /*
115  * This function displays the recorded statistics for each port
116  * and for each client. It uses ANSI terminal codes to clear
117  * screen when called. It is called from a single non-master
118  * thread in the server process, when the process is run with more
119  * than one lcore enabled.
120  */
121 static void
122 do_stats_display(void)
123 {
124 	unsigned i, j;
125 	const char clr[] = { 27, '[', '2', 'J', '\0' };
126 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
127 	uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
128 	uint64_t client_tx[MAX_CLIENTS], client_tx_drop[MAX_CLIENTS];
129 
130 	/* to get TX stats, we need to do some summing calculations */
131 	memset(port_tx, 0, sizeof(port_tx));
132 	memset(port_tx_drop, 0, sizeof(port_tx_drop));
133 	memset(client_tx, 0, sizeof(client_tx));
134 	memset(client_tx_drop, 0, sizeof(client_tx_drop));
135 
136 	for (i = 0; i < num_clients; i++){
137 		const volatile struct tx_stats *tx = &ports->tx_stats[i];
138 		for (j = 0; j < ports->num_ports; j++){
139 			/* assign to local variables here, save re-reading volatile vars */
140 			const uint64_t tx_val = tx->tx[ports->id[j]];
141 			const uint64_t drop_val = tx->tx_drop[ports->id[j]];
142 			port_tx[j] += tx_val;
143 			port_tx_drop[j] += drop_val;
144 			client_tx[i] += tx_val;
145 			client_tx_drop[i] += drop_val;
146 		}
147 	}
148 
149 	/* Clear screen and move to top left */
150 	printf("%s%s", clr, topLeft);
151 
152 	printf("PORTS\n");
153 	printf("-----\n");
154 	for (i = 0; i < ports->num_ports; i++)
155 		printf("Port %u: '%s'\t", (unsigned)ports->id[i],
156 				get_printable_mac_addr(ports->id[i]));
157 	printf("\n\n");
158 	for (i = 0; i < ports->num_ports; i++){
159 		printf("Port %u - rx: %9"PRIu64"\t"
160 				"tx: %9"PRIu64"\n",
161 				(unsigned)ports->id[i], ports->rx_stats.rx[i],
162 				port_tx[i]);
163 	}
164 
165 	printf("\nCLIENTS\n");
166 	printf("-------\n");
167 	for (i = 0; i < num_clients; i++){
168 		const unsigned long long rx = clients[i].stats.rx;
169 		const unsigned long long rx_drop = clients[i].stats.rx_drop;
170 		printf("Client %2u - rx: %9llu, rx_drop: %9llu\n"
171 				"            tx: %9"PRIu64", tx_drop: %9"PRIu64"\n",
172 				i, rx, rx_drop, client_tx[i], client_tx_drop[i]);
173 	}
174 
175 	printf("\n");
176 }
177 
178 /*
179  * The function called from each non-master lcore used by the process.
180  * The test_and_set function is used to randomly pick a single lcore on which
181  * the code to display the statistics will run. Otherwise, the code just
182  * repeatedly sleeps.
183  */
184 static int
185 sleep_lcore(__attribute__((unused)) void *dummy)
186 {
187 	/* Used to pick a display thread - static, so zero-initialised */
188 	static rte_atomic32_t display_stats;
189 
190 	/* Only one core should display stats */
191 	if (rte_atomic32_test_and_set(&display_stats)) {
192 		const unsigned sleeptime = 1;
193 		printf("Core %u displaying statistics\n", rte_lcore_id());
194 
195 		/* Longer initial pause so above printf is seen */
196 		sleep(sleeptime * 3);
197 
198 		/* Loop forever: sleep always returns 0 or <= param */
199 		while (sleep(sleeptime) <= sleeptime)
200 			do_stats_display();
201 	}
202 	return 0;
203 }
204 
205 /*
206  * Function to set all the client statistic values to zero.
207  * Called at program startup.
208  */
209 static void
210 clear_stats(void)
211 {
212 	unsigned i;
213 
214 	for (i = 0; i < num_clients; i++)
215 		clients[i].stats.rx = clients[i].stats.rx_drop = 0;
216 }
217 
218 /*
219  * send a burst of traffic to a client, assuming there are packets
220  * available to be sent to this client
221  */
222 static void
223 flush_rx_queue(uint16_t client)
224 {
225 	uint16_t j;
226 	struct client *cl;
227 
228 	if (cl_rx_buf[client].count == 0)
229 		return;
230 
231 	cl = &clients[client];
232 	if (rte_ring_enqueue_bulk(cl->rx_q, (void **)cl_rx_buf[client].buffer,
233 			cl_rx_buf[client].count) != 0){
234 		for (j = 0; j < cl_rx_buf[client].count; j++)
235 			rte_pktmbuf_free(cl_rx_buf[client].buffer[j]);
236 		cl->stats.rx_drop += cl_rx_buf[client].count;
237 	}
238 	else
239 		cl->stats.rx += cl_rx_buf[client].count;
240 
241 	cl_rx_buf[client].count = 0;
242 }
243 
244 /*
245  * marks a packet down to be sent to a particular client process
246  */
247 static inline void
248 enqueue_rx_packet(uint8_t client, struct rte_mbuf *buf)
249 {
250 	cl_rx_buf[client].buffer[cl_rx_buf[client].count++] = buf;
251 }
252 
253 /*
254  * This function takes a group of packets and routes them
255  * individually to the client process. Very simply round-robins the packets
256  * without checking any of the packet contents.
257  */
258 static void
259 process_packets(uint32_t port_num __rte_unused,
260 		struct rte_mbuf *pkts[], uint16_t rx_count)
261 {
262 	uint16_t i;
263 	uint8_t client = 0;
264 
265 	for (i = 0; i < rx_count; i++) {
266 		enqueue_rx_packet(client, pkts[i]);
267 
268 		if (++client == num_clients)
269 			client = 0;
270 	}
271 
272 	for (i = 0; i < num_clients; i++)
273 		flush_rx_queue(i);
274 }
275 
276 /*
277  * Function called by the master lcore of the DPDK process.
278  */
279 static void
280 do_packet_forwarding(void)
281 {
282 	unsigned port_num = 0; /* indexes the port[] array */
283 
284 	for (;;) {
285 		struct rte_mbuf *buf[PACKET_READ_SIZE];
286 		uint16_t rx_count;
287 
288 		/* read a port */
289 		rx_count = rte_eth_rx_burst(ports->id[port_num], 0, \
290 				buf, PACKET_READ_SIZE);
291 		ports->rx_stats.rx[port_num] += rx_count;
292 
293 		/* Now process the NIC packets read */
294 		if (likely(rx_count > 0))
295 			process_packets(port_num, buf, rx_count);
296 
297 		/* move to next port */
298 		if (++port_num == ports->num_ports)
299 			port_num = 0;
300 	}
301 }
302 
303 int
304 MAIN(int argc, char *argv[])
305 {
306 	/* initialise the system */
307 	if (init(argc, argv) < 0 )
308 		return -1;
309 	RTE_LOG(INFO, APP, "Finished Process Init.\n");
310 
311 	cl_rx_buf = calloc(num_clients, sizeof(cl_rx_buf[0]));
312 
313 	/* clear statistics */
314 	clear_stats();
315 
316 	/* put all other cores to sleep bar master */
317 	rte_eal_mp_remote_launch(sleep_lcore, NULL, SKIP_MASTER);
318 
319 	do_packet_forwarding();
320 	return 0;
321 }
322