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