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