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