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