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