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