xref: /dpdk/examples/ethtool/lib/rte_ethtool.c (revision 7d04227433ede0e3fdab1319cadafa46cc28266d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdint.h>
7 #include <rte_string_fns.h>
8 #include <rte_version.h>
9 #include <rte_ethdev.h>
10 #include <rte_ether.h>
11 #ifdef RTE_NET_IXGBE
12 #include <rte_pmd_ixgbe.h>
13 #endif
14 #include "rte_ethtool.h"
15 
16 #define PKTPOOL_SIZE 512
17 #define PKTPOOL_CACHE 32
18 
19 
20 int
21 rte_ethtool_get_drvinfo(uint16_t port_id, struct ethtool_drvinfo *drvinfo)
22 {
23 	struct rte_eth_dev_info dev_info;
24 	struct rte_dev_reg_info reg_info;
25 	int n;
26 	int ret;
27 
28 	if (drvinfo == NULL)
29 		return -EINVAL;
30 
31 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
32 
33 	ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
34 			      sizeof(drvinfo->fw_version));
35 	if (ret < 0)
36 		printf("firmware version get error: (%s)\n", strerror(-ret));
37 	else if (ret > 0)
38 		printf("Insufficient fw version buffer size, "
39 		       "the minimum size should be %d\n", ret);
40 
41 	ret = rte_eth_dev_info_get(port_id, &dev_info);
42 	if (ret != 0) {
43 		printf("Error during getting device (port %u) info: %s\n",
44 		       port_id, strerror(-ret));
45 
46 		return ret;
47 	}
48 
49 	strlcpy(drvinfo->driver, dev_info.driver_name,
50 		sizeof(drvinfo->driver));
51 	strlcpy(drvinfo->version, rte_version(), sizeof(drvinfo->version));
52 	strlcpy(drvinfo->bus_info, rte_dev_name(dev_info.device),
53 		sizeof(drvinfo->bus_info));
54 
55 	memset(&reg_info, 0, sizeof(reg_info));
56 	if (rte_eth_dev_get_reg_info(port_id, &reg_info) == 0)
57 		drvinfo->regdump_len = reg_info.length;
58 	else
59 		drvinfo->regdump_len = 0;
60 
61 	n = rte_eth_dev_get_eeprom_length(port_id);
62 	if (n > 0)
63 		drvinfo->eedump_len = n;
64 	else
65 		drvinfo->eedump_len = 0;
66 
67 	drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
68 	drvinfo->testinfo_len = 0;
69 
70 	return 0;
71 }
72 
73 int
74 rte_ethtool_get_regs_len(uint16_t port_id)
75 {
76 	struct rte_dev_reg_info reg_info;
77 	int ret;
78 
79 	memset(&reg_info, 0, sizeof(reg_info));
80 
81 	ret = rte_eth_dev_get_reg_info(port_id, &reg_info);
82 	if (ret)
83 		return ret;
84 
85 	return reg_info.length * reg_info.width;
86 }
87 
88 int
89 rte_ethtool_get_regs(uint16_t port_id, struct ethtool_regs *regs, void *data)
90 {
91 	struct rte_dev_reg_info reg_info;
92 	int status;
93 
94 	if (regs == NULL || data == NULL)
95 		return -EINVAL;
96 
97 	reg_info.data = data;
98 	reg_info.length = 0;
99 
100 	status = rte_eth_dev_get_reg_info(port_id, &reg_info);
101 	if (status)
102 		return status;
103 	regs->version = reg_info.version;
104 
105 	return 0;
106 }
107 
108 int
109 rte_ethtool_get_link(uint16_t port_id)
110 {
111 	struct rte_eth_link link;
112 	int ret;
113 
114 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
115 	ret = rte_eth_link_get(port_id, &link);
116 	if (ret < 0)
117 		return ret;
118 
119 	return link.link_status;
120 }
121 
122 int
123 rte_ethtool_get_eeprom_len(uint16_t port_id)
124 {
125 	return rte_eth_dev_get_eeprom_length(port_id);
126 }
127 
128 int
129 rte_ethtool_get_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
130 	void *words)
131 {
132 	struct rte_dev_eeprom_info eeprom_info;
133 	int status;
134 
135 	if (eeprom == NULL || words == NULL)
136 		return -EINVAL;
137 
138 	eeprom_info.offset = eeprom->offset;
139 	eeprom_info.length = eeprom->len;
140 	eeprom_info.data = words;
141 
142 	status = rte_eth_dev_get_eeprom(port_id, &eeprom_info);
143 	if (status)
144 		return status;
145 
146 	eeprom->magic = eeprom_info.magic;
147 
148 	return 0;
149 }
150 
151 int
152 rte_ethtool_set_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
153 	void *words)
154 {
155 	struct rte_dev_eeprom_info eeprom_info;
156 	int status;
157 
158 	if (eeprom == NULL || words == NULL || eeprom->offset >= eeprom->len)
159 		return -EINVAL;
160 
161 	eeprom_info.offset = eeprom->offset;
162 	eeprom_info.length = eeprom->len;
163 	eeprom_info.data = words;
164 
165 	status = rte_eth_dev_set_eeprom(port_id, &eeprom_info);
166 	if (status)
167 		return status;
168 
169 	eeprom->magic = eeprom_info.magic;
170 
171 	return 0;
172 }
173 
174 int
175 rte_ethtool_get_module_info(uint16_t port_id, uint32_t *modinfo)
176 {
177 	struct rte_eth_dev_module_info *info;
178 
179 	info = (struct rte_eth_dev_module_info *)modinfo;
180 	return rte_eth_dev_get_module_info(port_id, info);
181 }
182 
183 int
184 rte_ethtool_get_module_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
185 	void *words)
186 {
187 	struct rte_dev_eeprom_info eeprom_info;
188 	int status;
189 
190 	if (eeprom == NULL || words == NULL)
191 		return -EINVAL;
192 
193 	eeprom_info.offset = eeprom->offset;
194 	eeprom_info.length = eeprom->len;
195 	eeprom_info.data = words;
196 
197 	status = rte_eth_dev_get_module_eeprom(port_id, &eeprom_info);
198 	if (status)
199 		return status;
200 
201 	return 0;
202 }
203 
204 int
205 rte_ethtool_get_pauseparam(uint16_t port_id,
206 	struct ethtool_pauseparam *pause_param)
207 {
208 	struct rte_eth_fc_conf fc_conf;
209 	int status;
210 
211 	if (pause_param == NULL)
212 		return -EINVAL;
213 
214 	status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
215 	if (status)
216 		return status;
217 
218 	pause_param->tx_pause = 0;
219 	pause_param->rx_pause = 0;
220 	switch (fc_conf.mode) {
221 	case RTE_ETH_FC_RX_PAUSE:
222 		pause_param->rx_pause = 1;
223 		break;
224 	case RTE_ETH_FC_TX_PAUSE:
225 		pause_param->tx_pause = 1;
226 		break;
227 	case RTE_ETH_FC_FULL:
228 		pause_param->rx_pause = 1;
229 		pause_param->tx_pause = 1;
230 	default:
231 		/* dummy block to avoid compiler warning */
232 		break;
233 	}
234 	pause_param->autoneg = (uint32_t)fc_conf.autoneg;
235 
236 	return 0;
237 }
238 
239 int
240 rte_ethtool_set_pauseparam(uint16_t port_id,
241 	struct ethtool_pauseparam *pause_param)
242 {
243 	struct rte_eth_fc_conf fc_conf;
244 	int status;
245 
246 	if (pause_param == NULL)
247 		return -EINVAL;
248 
249 	/*
250 	 * Read device flow control parameter first since
251 	 * ethtool set_pauseparam op doesn't have all the information.
252 	 * as defined in struct rte_eth_fc_conf.
253 	 * This API requires the device to support both
254 	 * rte_eth_dev_flow_ctrl_get and rte_eth_dev_flow_ctrl_set, otherwise
255 	 * return -ENOTSUP
256 	 */
257 	status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
258 	if (status)
259 		return status;
260 
261 	fc_conf.autoneg = (uint8_t)pause_param->autoneg;
262 
263 	if (pause_param->tx_pause) {
264 		if (pause_param->rx_pause)
265 			fc_conf.mode = RTE_ETH_FC_FULL;
266 		else
267 			fc_conf.mode = RTE_ETH_FC_TX_PAUSE;
268 	} else {
269 		if (pause_param->rx_pause)
270 			fc_conf.mode = RTE_ETH_FC_RX_PAUSE;
271 		else
272 			fc_conf.mode = RTE_ETH_FC_NONE;
273 	}
274 
275 	status = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
276 	if (status)
277 		return status;
278 
279 	return 0;
280 }
281 
282 int
283 rte_ethtool_net_open(uint16_t port_id)
284 {
285 	return rte_eth_dev_start(port_id);
286 }
287 
288 int
289 rte_ethtool_net_stop(uint16_t port_id)
290 {
291 	return rte_eth_dev_stop(port_id);
292 }
293 
294 int
295 rte_ethtool_net_get_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
296 {
297 	int ret;
298 
299 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
300 	if (addr == NULL)
301 		return -EINVAL;
302 
303 	ret = rte_eth_macaddr_get(port_id, addr);
304 	if (ret != 0)
305 		return ret;
306 
307 	return 0;
308 }
309 
310 int
311 rte_ethtool_net_set_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
312 {
313 	if (addr == NULL)
314 		return -EINVAL;
315 	return rte_eth_dev_default_mac_addr_set(port_id, addr);
316 }
317 
318 int
319 rte_ethtool_net_validate_addr(uint16_t port_id __rte_unused,
320 	struct rte_ether_addr *addr)
321 {
322 	if (addr == NULL)
323 		return -EINVAL;
324 	return rte_is_valid_assigned_ether_addr(addr);
325 }
326 
327 int
328 rte_ethtool_net_change_mtu(uint16_t port_id, int mtu)
329 {
330 	if (mtu < 0 || mtu > UINT16_MAX)
331 		return -EINVAL;
332 	return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
333 }
334 
335 int
336 rte_ethtool_net_get_stats64(uint16_t port_id, struct rte_eth_stats *stats)
337 {
338 	if (stats == NULL)
339 		return -EINVAL;
340 	return rte_eth_stats_get(port_id, stats);
341 }
342 
343 int
344 rte_ethtool_net_vlan_rx_add_vid(uint16_t port_id, uint16_t vid)
345 {
346 	return rte_eth_dev_vlan_filter(port_id, vid, 1);
347 }
348 
349 int
350 rte_ethtool_net_vlan_rx_kill_vid(uint16_t port_id, uint16_t vid)
351 {
352 	return rte_eth_dev_vlan_filter(port_id, vid, 0);
353 }
354 
355 /*
356  * The set_rx_mode provides driver-specific rx mode setting.
357  * This implementation implements rx mode setting based upon
358  * ixgbe/igb drivers. Further improvement is to provide a
359  * callback op field over struct rte_eth_dev::dev_ops so each
360  * driver can register device-specific implementation
361  */
362 int
363 rte_ethtool_net_set_rx_mode(uint16_t port_id)
364 {
365 	uint16_t num_vfs;
366 	struct rte_eth_dev_info dev_info;
367 	uint16_t vf;
368 	int ret;
369 
370 	ret = rte_eth_dev_info_get(port_id, &dev_info);
371 	if (ret != 0)
372 		return ret;
373 
374 	num_vfs = dev_info.max_vfs;
375 
376 	/* Set VF vf_rx_mode, VF unsupport status is discard */
377 	for (vf = 0; vf < num_vfs; vf++) {
378 #ifdef RTE_NET_IXGBE
379 		rte_pmd_ixgbe_set_vf_rxmode(port_id, vf,
380 			RTE_ETH_VMDQ_ACCEPT_UNTAG, 0);
381 #endif
382 	}
383 
384 	/* Enable Rx vlan filter, VF unsupported status is discard */
385 	ret = rte_eth_dev_set_vlan_offload(port_id, RTE_ETH_VLAN_FILTER_MASK);
386 	if (ret != 0)
387 		return ret;
388 
389 	return 0;
390 }
391 
392 
393 int
394 rte_ethtool_get_ringparam(uint16_t port_id,
395 	struct ethtool_ringparam *ring_param)
396 {
397 	struct rte_eth_dev_info dev_info;
398 	struct rte_eth_rxq_info rx_qinfo;
399 	struct rte_eth_txq_info tx_qinfo;
400 	int stat;
401 	int ret;
402 
403 	if (ring_param == NULL)
404 		return -EINVAL;
405 
406 	ret = rte_eth_dev_info_get(port_id, &dev_info);
407 	if (ret != 0)
408 		return ret;
409 
410 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
411 	if (stat != 0)
412 		return stat;
413 
414 	stat = rte_eth_tx_queue_info_get(port_id, 0, &tx_qinfo);
415 	if (stat != 0)
416 		return stat;
417 
418 	memset(ring_param, 0, sizeof(*ring_param));
419 	ring_param->rx_pending = rx_qinfo.nb_desc;
420 	ring_param->rx_max_pending = dev_info.rx_desc_lim.nb_max;
421 	ring_param->tx_pending = tx_qinfo.nb_desc;
422 	ring_param->tx_max_pending = dev_info.tx_desc_lim.nb_max;
423 
424 	return 0;
425 }
426 
427 
428 int
429 rte_ethtool_set_ringparam(uint16_t port_id,
430 	struct ethtool_ringparam *ring_param)
431 {
432 	struct rte_eth_rxq_info rx_qinfo;
433 	int stat;
434 
435 	if (ring_param == NULL)
436 		return -EINVAL;
437 
438 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
439 	if (stat != 0)
440 		return stat;
441 
442 	stat = rte_eth_dev_stop(port_id);
443 	if (stat != 0)
444 		return stat;
445 
446 	stat = rte_eth_tx_queue_setup(port_id, 0, ring_param->tx_pending,
447 		rte_eth_dev_socket_id(port_id), NULL);
448 	if (stat != 0)
449 		return stat;
450 
451 	stat = rte_eth_rx_queue_setup(port_id, 0, ring_param->rx_pending,
452 		rte_eth_dev_socket_id(port_id), NULL, rx_qinfo.mp);
453 	if (stat != 0)
454 		return stat;
455 
456 	return rte_eth_dev_start(port_id);
457 }
458