xref: /dpdk/examples/ethtool/ethtool-app/main.c (revision 497025dac0aff87693557687e0a37f7342aa2ce4)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015 Intel Corporation
3  */
4 
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 #include <rte_common.h>
10 #include <rte_spinlock.h>
11 #include <rte_eal.h>
12 #include <rte_ethdev.h>
13 #include <rte_ether.h>
14 #include <rte_ip.h>
15 #include <rte_memory.h>
16 #include <rte_mempool.h>
17 #include <rte_mbuf.h>
18 
19 #include "ethapp.h"
20 
21 #define MAX_PORTS RTE_MAX_ETHPORTS
22 #define MAX_BURST_LENGTH 32
23 #define PORT_RX_QUEUE_SIZE 1024
24 #define PORT_TX_QUEUE_SIZE 1024
25 #define PKTPOOL_EXTRA_SIZE 512
26 #define PKTPOOL_CACHE 32
27 
28 
29 struct txq_port {
30 	uint16_t cnt_unsent;
31 	struct rte_mbuf *buf_frames[MAX_BURST_LENGTH];
32 };
33 
34 struct app_port {
35 	struct rte_ether_addr mac_addr;
36 	struct txq_port txq;
37 	rte_spinlock_t lock;
38 	int port_active;
39 	int port_dirty;
40 	int idx_port;
41 	struct rte_mempool *pkt_pool;
42 };
43 
44 struct app_config {
45 	struct app_port ports[MAX_PORTS];
46 	int cnt_ports;
47 	int exit_now;
48 };
49 
50 
51 struct app_config app_cfg;
52 
53 
lock_port(int idx_port)54 void lock_port(int idx_port)
55 {
56 	struct app_port *ptr_port = &app_cfg.ports[idx_port];
57 
58 	rte_spinlock_lock(&ptr_port->lock);
59 }
60 
unlock_port(int idx_port)61 void unlock_port(int idx_port)
62 {
63 	struct app_port *ptr_port = &app_cfg.ports[idx_port];
64 
65 	rte_spinlock_unlock(&ptr_port->lock);
66 }
67 
mark_port_active(int idx_port)68 void mark_port_active(int idx_port)
69 {
70 	struct app_port *ptr_port = &app_cfg.ports[idx_port];
71 
72 	ptr_port->port_active = 1;
73 }
74 
mark_port_inactive(int idx_port)75 void mark_port_inactive(int idx_port)
76 {
77 	struct app_port *ptr_port = &app_cfg.ports[idx_port];
78 
79 	ptr_port->port_active = 0;
80 }
81 
mark_port_newmac(int idx_port)82 void mark_port_newmac(int idx_port)
83 {
84 	struct app_port *ptr_port = &app_cfg.ports[idx_port];
85 
86 	ptr_port->port_dirty = 1;
87 }
88 
setup_ports(struct app_config * app_cfg,int cnt_ports)89 static void setup_ports(struct app_config *app_cfg, int cnt_ports)
90 {
91 	int idx_port;
92 	int size_pktpool;
93 	struct rte_eth_conf cfg_port;
94 	struct rte_eth_dev_info dev_info;
95 	char str_name[16];
96 	uint16_t nb_rxd = PORT_RX_QUEUE_SIZE;
97 	uint16_t nb_txd = PORT_TX_QUEUE_SIZE;
98 	int ret;
99 
100 	memset(&cfg_port, 0, sizeof(cfg_port));
101 	cfg_port.txmode.mq_mode = RTE_ETH_MQ_TX_NONE;
102 
103 	for (idx_port = 0; idx_port < cnt_ports; idx_port++) {
104 		struct app_port *ptr_port = &app_cfg->ports[idx_port];
105 
106 		ret = rte_eth_dev_info_get(idx_port, &dev_info);
107 		if (ret != 0)
108 			rte_exit(EXIT_FAILURE,
109 				"Error during getting device (port %u) info: %s\n",
110 				idx_port, strerror(-ret));
111 
112 		size_pktpool = dev_info.rx_desc_lim.nb_max +
113 			dev_info.tx_desc_lim.nb_max + PKTPOOL_EXTRA_SIZE;
114 
115 		snprintf(str_name, 16, "pkt_pool%i", idx_port);
116 		ptr_port->pkt_pool = rte_pktmbuf_pool_create(
117 			str_name,
118 			size_pktpool, PKTPOOL_CACHE,
119 			0,
120 			RTE_MBUF_DEFAULT_BUF_SIZE,
121 			rte_socket_id()
122 			);
123 		if (ptr_port->pkt_pool == NULL)
124 			rte_exit(EXIT_FAILURE,
125 				"rte_pktmbuf_pool_create failed"
126 				);
127 
128 		printf("Init port %i..\n", idx_port);
129 		ptr_port->port_active = 1;
130 		ptr_port->port_dirty = 0;
131 		ptr_port->idx_port = idx_port;
132 
133 		if (rte_eth_dev_configure(idx_port, 1, 1, &cfg_port) < 0)
134 			rte_exit(EXIT_FAILURE,
135 				 "rte_eth_dev_configure failed");
136 		if (rte_eth_dev_adjust_nb_rx_tx_desc(idx_port, &nb_rxd,
137 						     &nb_txd) < 0)
138 			rte_exit(EXIT_FAILURE,
139 				 "rte_eth_dev_adjust_nb_rx_tx_desc failed");
140 
141 		if (rte_eth_rx_queue_setup(
142 			    idx_port, 0, nb_rxd,
143 			    rte_eth_dev_socket_id(idx_port), NULL,
144 			    ptr_port->pkt_pool) < 0)
145 			rte_exit(EXIT_FAILURE,
146 				 "rte_eth_rx_queue_setup failed"
147 				);
148 		if (rte_eth_tx_queue_setup(
149 			    idx_port, 0, nb_txd,
150 			    rte_eth_dev_socket_id(idx_port), NULL) < 0)
151 			rte_exit(EXIT_FAILURE,
152 				 "rte_eth_tx_queue_setup failed"
153 				);
154 		if (rte_eth_dev_start(idx_port) < 0)
155 			rte_exit(EXIT_FAILURE,
156 				 "%s:%i: rte_eth_dev_start failed",
157 				 __FILE__, __LINE__
158 				);
159 		ret = rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
160 		if (ret != 0)
161 			rte_exit(EXIT_FAILURE,
162 				"rte_eth_macaddr_get failed (port %u): %s\n",
163 				idx_port, rte_strerror(-ret));
164 
165 		rte_spinlock_init(&ptr_port->lock);
166 	}
167 }
168 
process_frame(struct app_port * ptr_port,struct rte_mbuf * ptr_frame)169 static void process_frame(struct app_port *ptr_port,
170 	struct rte_mbuf *ptr_frame)
171 {
172 	struct rte_ether_hdr *ptr_mac_hdr;
173 
174 	ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct rte_ether_hdr *);
175 	rte_ether_addr_copy(&ptr_mac_hdr->src_addr, &ptr_mac_hdr->dst_addr);
176 	rte_ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->src_addr);
177 }
178 
worker_main(__rte_unused void * ptr_data)179 static int worker_main(__rte_unused void *ptr_data)
180 {
181 	struct app_port *ptr_port;
182 	struct rte_mbuf *ptr_frame;
183 	struct txq_port *txq;
184 
185 	uint16_t cnt_recv_frames;
186 	uint16_t idx_frame;
187 	uint16_t cnt_sent;
188 	uint16_t idx_port;
189 	uint16_t lock_result;
190 	int ret;
191 
192 	while (app_cfg.exit_now == 0) {
193 		for (idx_port = 0; idx_port < app_cfg.cnt_ports; idx_port++) {
194 			/* Check that port is active and unlocked */
195 			ptr_port = &app_cfg.ports[idx_port];
196 			lock_result = rte_spinlock_trylock(&ptr_port->lock);
197 			if (lock_result == 0)
198 				continue;
199 			if (ptr_port->port_active == 0) {
200 				rte_spinlock_unlock(&ptr_port->lock);
201 				continue;
202 			}
203 			txq = &ptr_port->txq;
204 
205 			/* MAC address was updated */
206 			if (ptr_port->port_dirty == 1) {
207 				ret = rte_eth_macaddr_get(ptr_port->idx_port,
208 					&ptr_port->mac_addr);
209 				if (ret != 0) {
210 					rte_spinlock_unlock(&ptr_port->lock);
211 					printf("Failed to get MAC address (port %u): %s",
212 					       ptr_port->idx_port,
213 					       rte_strerror(-ret));
214 					return ret;
215 				}
216 
217 				ptr_port->port_dirty = 0;
218 			}
219 
220 			/* Incoming frames */
221 			cnt_recv_frames = rte_eth_rx_burst(
222 				ptr_port->idx_port, 0,
223 				&txq->buf_frames[txq->cnt_unsent],
224 				RTE_DIM(txq->buf_frames) - txq->cnt_unsent
225 				);
226 			if (cnt_recv_frames > 0) {
227 				for (idx_frame = 0;
228 					idx_frame < cnt_recv_frames;
229 					idx_frame++) {
230 					ptr_frame = txq->buf_frames[
231 						idx_frame + txq->cnt_unsent];
232 					process_frame(ptr_port, ptr_frame);
233 				}
234 				txq->cnt_unsent += cnt_recv_frames;
235 			}
236 
237 			/* Outgoing frames */
238 			if (txq->cnt_unsent > 0) {
239 				cnt_sent = rte_eth_tx_burst(
240 					ptr_port->idx_port, 0,
241 					txq->buf_frames,
242 					txq->cnt_unsent
243 					);
244 				/* Shuffle up unsent frame pointers */
245 				for (idx_frame = cnt_sent;
246 					idx_frame < txq->cnt_unsent;
247 					idx_frame++)
248 					txq->buf_frames[idx_frame - cnt_sent] =
249 						txq->buf_frames[idx_frame];
250 				txq->cnt_unsent -= cnt_sent;
251 			}
252 			rte_spinlock_unlock(&ptr_port->lock);
253 		} /* end for( idx_port ) */
254 	} /* end for(;;) */
255 
256 	return 0;
257 }
258 
close_ports(void)259 static void close_ports(void)
260 {
261 	uint16_t portid;
262 	int ret;
263 
264 	for (portid = 0; portid < app_cfg.cnt_ports; portid++) {
265 		printf("Closing port %d...", portid);
266 		ret = rte_eth_dev_stop(portid);
267 		if (ret != 0)
268 			rte_exit(EXIT_FAILURE, "rte_eth_dev_stop: err=%s, port=%u\n",
269 				 strerror(-ret), portid);
270 		rte_eth_dev_close(portid);
271 		printf(" Done\n");
272 	}
273 }
274 
main(int argc,char ** argv)275 int main(int argc, char **argv)
276 {
277 	int cnt_args_parsed;
278 	uint32_t id_core;
279 	uint32_t cnt_ports;
280 
281 	/* Init runtime environment */
282 	cnt_args_parsed = rte_eal_init(argc, argv);
283 	if (cnt_args_parsed < 0)
284 		rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");
285 
286 	cnt_ports = rte_eth_dev_count_avail();
287 	printf("Number of NICs: %i\n", cnt_ports);
288 	if (cnt_ports == 0)
289 		rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
290 	if (cnt_ports > MAX_PORTS) {
291 		printf("Info: Using only %i of %i ports\n",
292 			cnt_ports, MAX_PORTS
293 			);
294 		cnt_ports = MAX_PORTS;
295 	}
296 
297 	setup_ports(&app_cfg, cnt_ports);
298 
299 	app_cfg.exit_now = 0;
300 	app_cfg.cnt_ports = cnt_ports;
301 
302 	if (rte_lcore_count() < 2)
303 		rte_exit(EXIT_FAILURE, "No available worker core!\n");
304 
305 	/* Assume there is an available worker.. */
306 	id_core = rte_lcore_id();
307 	id_core = rte_get_next_lcore(id_core, 1, 1);
308 	rte_eal_remote_launch(worker_main, NULL, id_core);
309 
310 	ethapp_main();
311 
312 	app_cfg.exit_now = 1;
313 	RTE_LCORE_FOREACH_WORKER(id_core) {
314 		if (rte_eal_wait_lcore(id_core) < 0)
315 			return -1;
316 	}
317 
318 	close_ports();
319 
320 	/* clean up the EAL */
321 	rte_eal_cleanup();
322 
323 	return 0;
324 }
325