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