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