xref: /dpdk/examples/ethtool/ethtool-app/main.c (revision 200bc52e5aa0d72e70464c9cd22b55cf536ed13c)
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 
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 
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 
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 
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 
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 
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 
99 	memset(&cfg_port, 0, sizeof(cfg_port));
100 	cfg_port.txmode.mq_mode = ETH_MQ_TX_NONE;
101 
102 	for (idx_port = 0; idx_port < cnt_ports; idx_port++) {
103 		struct app_port *ptr_port = &app_cfg->ports[idx_port];
104 
105 		rte_eth_dev_info_get(idx_port, &dev_info);
106 		size_pktpool = dev_info.rx_desc_lim.nb_max +
107 			dev_info.tx_desc_lim.nb_max + PKTPOOL_EXTRA_SIZE;
108 
109 		snprintf(str_name, 16, "pkt_pool%i", idx_port);
110 		ptr_port->pkt_pool = rte_pktmbuf_pool_create(
111 			str_name,
112 			size_pktpool, PKTPOOL_CACHE,
113 			0,
114 			RTE_MBUF_DEFAULT_BUF_SIZE,
115 			rte_socket_id()
116 			);
117 		if (ptr_port->pkt_pool == NULL)
118 			rte_exit(EXIT_FAILURE,
119 				"rte_pktmbuf_pool_create failed"
120 				);
121 
122 		printf("Init port %i..\n", idx_port);
123 		ptr_port->port_active = 1;
124 		ptr_port->port_dirty = 0;
125 		ptr_port->idx_port = idx_port;
126 
127 		if (rte_eth_dev_configure(idx_port, 1, 1, &cfg_port) < 0)
128 			rte_exit(EXIT_FAILURE,
129 				 "rte_eth_dev_configure failed");
130 		if (rte_eth_dev_adjust_nb_rx_tx_desc(idx_port, &nb_rxd,
131 						     &nb_txd) < 0)
132 			rte_exit(EXIT_FAILURE,
133 				 "rte_eth_dev_adjust_nb_rx_tx_desc failed");
134 
135 		if (rte_eth_rx_queue_setup(
136 			    idx_port, 0, nb_rxd,
137 			    rte_eth_dev_socket_id(idx_port), NULL,
138 			    ptr_port->pkt_pool) < 0)
139 			rte_exit(EXIT_FAILURE,
140 				 "rte_eth_rx_queue_setup failed"
141 				);
142 		if (rte_eth_tx_queue_setup(
143 			    idx_port, 0, nb_txd,
144 			    rte_eth_dev_socket_id(idx_port), NULL) < 0)
145 			rte_exit(EXIT_FAILURE,
146 				 "rte_eth_tx_queue_setup failed"
147 				);
148 		if (rte_eth_dev_start(idx_port) < 0)
149 			rte_exit(EXIT_FAILURE,
150 				 "%s:%i: rte_eth_dev_start failed",
151 				 __FILE__, __LINE__
152 				);
153 		rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
154 		rte_spinlock_init(&ptr_port->lock);
155 	}
156 }
157 
158 static void process_frame(struct app_port *ptr_port,
159 	struct rte_mbuf *ptr_frame)
160 {
161 	struct rte_ether_hdr *ptr_mac_hdr;
162 
163 	ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct rte_ether_hdr *);
164 	rte_ether_addr_copy(&ptr_mac_hdr->s_addr, &ptr_mac_hdr->d_addr);
165 	rte_ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->s_addr);
166 }
167 
168 static int slave_main(__attribute__((unused)) void *ptr_data)
169 {
170 	struct app_port *ptr_port;
171 	struct rte_mbuf *ptr_frame;
172 	struct txq_port *txq;
173 
174 	uint16_t cnt_recv_frames;
175 	uint16_t idx_frame;
176 	uint16_t cnt_sent;
177 	uint16_t idx_port;
178 	uint16_t lock_result;
179 
180 	while (app_cfg.exit_now == 0) {
181 		for (idx_port = 0; idx_port < app_cfg.cnt_ports; idx_port++) {
182 			/* Check that port is active and unlocked */
183 			ptr_port = &app_cfg.ports[idx_port];
184 			lock_result = rte_spinlock_trylock(&ptr_port->lock);
185 			if (lock_result == 0)
186 				continue;
187 			if (ptr_port->port_active == 0) {
188 				rte_spinlock_unlock(&ptr_port->lock);
189 				continue;
190 			}
191 			txq = &ptr_port->txq;
192 
193 			/* MAC address was updated */
194 			if (ptr_port->port_dirty == 1) {
195 				rte_eth_macaddr_get(ptr_port->idx_port,
196 					&ptr_port->mac_addr);
197 				ptr_port->port_dirty = 0;
198 			}
199 
200 			/* Incoming frames */
201 			cnt_recv_frames = rte_eth_rx_burst(
202 				ptr_port->idx_port, 0,
203 				&txq->buf_frames[txq->cnt_unsent],
204 				RTE_DIM(txq->buf_frames) - txq->cnt_unsent
205 				);
206 			if (cnt_recv_frames > 0) {
207 				for (idx_frame = 0;
208 					idx_frame < cnt_recv_frames;
209 					idx_frame++) {
210 					ptr_frame = txq->buf_frames[
211 						idx_frame + txq->cnt_unsent];
212 					process_frame(ptr_port, ptr_frame);
213 				}
214 				txq->cnt_unsent += cnt_recv_frames;
215 			}
216 
217 			/* Outgoing frames */
218 			if (txq->cnt_unsent > 0) {
219 				cnt_sent = rte_eth_tx_burst(
220 					ptr_port->idx_port, 0,
221 					txq->buf_frames,
222 					txq->cnt_unsent
223 					);
224 				/* Shuffle up unsent frame pointers */
225 				for (idx_frame = cnt_sent;
226 					idx_frame < txq->cnt_unsent;
227 					idx_frame++)
228 					txq->buf_frames[idx_frame - cnt_sent] =
229 						txq->buf_frames[idx_frame];
230 				txq->cnt_unsent -= cnt_sent;
231 			}
232 			rte_spinlock_unlock(&ptr_port->lock);
233 		} /* end for( idx_port ) */
234 	} /* end for(;;) */
235 
236 	return 0;
237 }
238 
239 int main(int argc, char **argv)
240 {
241 	int cnt_args_parsed;
242 	uint32_t id_core;
243 	uint32_t cnt_ports;
244 
245 	/* Init runtime environment */
246 	cnt_args_parsed = rte_eal_init(argc, argv);
247 	if (cnt_args_parsed < 0)
248 		rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");
249 
250 	cnt_ports = rte_eth_dev_count_avail();
251 	printf("Number of NICs: %i\n", cnt_ports);
252 	if (cnt_ports == 0)
253 		rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
254 	if (cnt_ports > MAX_PORTS) {
255 		printf("Info: Using only %i of %i ports\n",
256 			cnt_ports, MAX_PORTS
257 			);
258 		cnt_ports = MAX_PORTS;
259 	}
260 
261 	setup_ports(&app_cfg, cnt_ports);
262 
263 	app_cfg.exit_now = 0;
264 	app_cfg.cnt_ports = cnt_ports;
265 
266 	if (rte_lcore_count() < 2)
267 		rte_exit(EXIT_FAILURE, "No available slave core!\n");
268 	/* Assume there is an available slave.. */
269 	id_core = rte_lcore_id();
270 	id_core = rte_get_next_lcore(id_core, 1, 1);
271 	rte_eal_remote_launch(slave_main, NULL, id_core);
272 
273 	ethapp_main();
274 
275 	app_cfg.exit_now = 1;
276 	RTE_LCORE_FOREACH_SLAVE(id_core) {
277 		if (rte_eal_wait_lcore(id_core) < 0)
278 			return -1;
279 	}
280 
281 	return 0;
282 }
283