xref: /dpdk/examples/ethtool/lib/rte_ethtool.c (revision 8f1d23ece06adff5eae9f1b4365bdbbd3abee2b2)
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, dev_info.device->name,
53 		sizeof(drvinfo->bus_info));
54 
55 	memset(&reg_info, 0, sizeof(reg_info));
56 	rte_eth_dev_get_reg_info(port_id, &reg_info);
57 	n = reg_info.length;
58 	if (n > 0)
59 		drvinfo->regdump_len = n;
60 	else
61 		drvinfo->regdump_len = 0;
62 
63 	n = rte_eth_dev_get_eeprom_length(port_id);
64 	if (n > 0)
65 		drvinfo->eedump_len = n;
66 	else
67 		drvinfo->eedump_len = 0;
68 
69 	drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
70 	drvinfo->testinfo_len = 0;
71 
72 	return 0;
73 }
74 
75 int
76 rte_ethtool_get_regs_len(uint16_t port_id)
77 {
78 	struct rte_dev_reg_info reg_info;
79 	int ret;
80 
81 	memset(&reg_info, 0, sizeof(reg_info));
82 
83 	ret = rte_eth_dev_get_reg_info(port_id, &reg_info);
84 	if (ret)
85 		return ret;
86 
87 	return reg_info.length * reg_info.width;
88 }
89 
90 int
91 rte_ethtool_get_regs(uint16_t port_id, struct ethtool_regs *regs, void *data)
92 {
93 	struct rte_dev_reg_info reg_info;
94 	int status;
95 
96 	if (regs == NULL || data == NULL)
97 		return -EINVAL;
98 
99 	reg_info.data = data;
100 	reg_info.length = 0;
101 
102 	status = rte_eth_dev_get_reg_info(port_id, &reg_info);
103 	if (status)
104 		return status;
105 	regs->version = reg_info.version;
106 
107 	return 0;
108 }
109 
110 int
111 rte_ethtool_get_link(uint16_t port_id)
112 {
113 	struct rte_eth_link link;
114 	int ret;
115 
116 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
117 	ret = rte_eth_link_get(port_id, &link);
118 	if (ret < 0)
119 		return ret;
120 
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_module_info(uint16_t port_id, uint32_t *modinfo)
178 {
179 	struct rte_eth_dev_module_info *info;
180 
181 	info = (struct rte_eth_dev_module_info *)modinfo;
182 	return rte_eth_dev_get_module_info(port_id, info);
183 }
184 
185 int
186 rte_ethtool_get_module_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
187 	void *words)
188 {
189 	struct rte_dev_eeprom_info eeprom_info;
190 	int status;
191 
192 	if (eeprom == NULL || words == NULL)
193 		return -EINVAL;
194 
195 	eeprom_info.offset = eeprom->offset;
196 	eeprom_info.length = eeprom->len;
197 	eeprom_info.data = words;
198 
199 	status = rte_eth_dev_get_module_eeprom(port_id, &eeprom_info);
200 	if (status)
201 		return status;
202 
203 	return 0;
204 }
205 
206 int
207 rte_ethtool_get_pauseparam(uint16_t port_id,
208 	struct ethtool_pauseparam *pause_param)
209 {
210 	struct rte_eth_fc_conf fc_conf;
211 	int status;
212 
213 	if (pause_param == NULL)
214 		return -EINVAL;
215 
216 	status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
217 	if (status)
218 		return status;
219 
220 	pause_param->tx_pause = 0;
221 	pause_param->rx_pause = 0;
222 	switch (fc_conf.mode) {
223 	case RTE_ETH_FC_RX_PAUSE:
224 		pause_param->rx_pause = 1;
225 		break;
226 	case RTE_ETH_FC_TX_PAUSE:
227 		pause_param->tx_pause = 1;
228 		break;
229 	case RTE_ETH_FC_FULL:
230 		pause_param->rx_pause = 1;
231 		pause_param->tx_pause = 1;
232 	default:
233 		/* dummy block to avoid compiler warning */
234 		break;
235 	}
236 	pause_param->autoneg = (uint32_t)fc_conf.autoneg;
237 
238 	return 0;
239 }
240 
241 int
242 rte_ethtool_set_pauseparam(uint16_t port_id,
243 	struct ethtool_pauseparam *pause_param)
244 {
245 	struct rte_eth_fc_conf fc_conf;
246 	int status;
247 
248 	if (pause_param == NULL)
249 		return -EINVAL;
250 
251 	/*
252 	 * Read device flow control parameter first since
253 	 * ethtool set_pauseparam op doesn't have all the information.
254 	 * as defined in struct rte_eth_fc_conf.
255 	 * This API requires the device to support both
256 	 * rte_eth_dev_flow_ctrl_get and rte_eth_dev_flow_ctrl_set, otherwise
257 	 * return -ENOTSUP
258 	 */
259 	status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
260 	if (status)
261 		return status;
262 
263 	fc_conf.autoneg = (uint8_t)pause_param->autoneg;
264 
265 	if (pause_param->tx_pause) {
266 		if (pause_param->rx_pause)
267 			fc_conf.mode = RTE_ETH_FC_FULL;
268 		else
269 			fc_conf.mode = RTE_ETH_FC_TX_PAUSE;
270 	} else {
271 		if (pause_param->rx_pause)
272 			fc_conf.mode = RTE_ETH_FC_RX_PAUSE;
273 		else
274 			fc_conf.mode = RTE_ETH_FC_NONE;
275 	}
276 
277 	status = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
278 	if (status)
279 		return status;
280 
281 	return 0;
282 }
283 
284 int
285 rte_ethtool_net_open(uint16_t port_id)
286 {
287 	int ret;
288 
289 	ret = rte_eth_dev_stop(port_id);
290 	if (ret != 0)
291 		return ret;
292 
293 	return rte_eth_dev_start(port_id);
294 }
295 
296 int
297 rte_ethtool_net_stop(uint16_t port_id)
298 {
299 	return rte_eth_dev_stop(port_id);
300 }
301 
302 int
303 rte_ethtool_net_get_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
304 {
305 	int ret;
306 
307 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
308 	if (addr == NULL)
309 		return -EINVAL;
310 
311 	ret = rte_eth_macaddr_get(port_id, addr);
312 	if (ret != 0)
313 		return ret;
314 
315 	return 0;
316 }
317 
318 int
319 rte_ethtool_net_set_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
320 {
321 	if (addr == NULL)
322 		return -EINVAL;
323 	return rte_eth_dev_default_mac_addr_set(port_id, addr);
324 }
325 
326 int
327 rte_ethtool_net_validate_addr(uint16_t port_id __rte_unused,
328 	struct rte_ether_addr *addr)
329 {
330 	if (addr == NULL)
331 		return -EINVAL;
332 	return rte_is_valid_assigned_ether_addr(addr);
333 }
334 
335 int
336 rte_ethtool_net_change_mtu(uint16_t port_id, int mtu)
337 {
338 	if (mtu < 0 || mtu > UINT16_MAX)
339 		return -EINVAL;
340 	return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
341 }
342 
343 int
344 rte_ethtool_net_get_stats64(uint16_t port_id, struct rte_eth_stats *stats)
345 {
346 	if (stats == NULL)
347 		return -EINVAL;
348 	return rte_eth_stats_get(port_id, stats);
349 }
350 
351 int
352 rte_ethtool_net_vlan_rx_add_vid(uint16_t port_id, uint16_t vid)
353 {
354 	return rte_eth_dev_vlan_filter(port_id, vid, 1);
355 }
356 
357 int
358 rte_ethtool_net_vlan_rx_kill_vid(uint16_t port_id, uint16_t vid)
359 {
360 	return rte_eth_dev_vlan_filter(port_id, vid, 0);
361 }
362 
363 /*
364  * The set_rx_mode provides driver-specific rx mode setting.
365  * This implementation implements rx mode setting based upon
366  * ixgbe/igb drivers. Further improvement is to provide a
367  * callback op field over struct rte_eth_dev::dev_ops so each
368  * driver can register device-specific implementation
369  */
370 int
371 rte_ethtool_net_set_rx_mode(uint16_t port_id)
372 {
373 	uint16_t num_vfs;
374 	struct rte_eth_dev_info dev_info;
375 	uint16_t vf;
376 	int ret;
377 
378 	ret = rte_eth_dev_info_get(port_id, &dev_info);
379 	if (ret != 0)
380 		return ret;
381 
382 	num_vfs = dev_info.max_vfs;
383 
384 	/* Set VF vf_rx_mode, VF unsupport status is discard */
385 	for (vf = 0; vf < num_vfs; vf++) {
386 #ifdef RTE_NET_IXGBE
387 		rte_pmd_ixgbe_set_vf_rxmode(port_id, vf,
388 			RTE_ETH_VMDQ_ACCEPT_UNTAG, 0);
389 #endif
390 	}
391 
392 	/* Enable Rx vlan filter, VF unsupported status is discard */
393 	ret = rte_eth_dev_set_vlan_offload(port_id, RTE_ETH_VLAN_FILTER_MASK);
394 	if (ret != 0)
395 		return ret;
396 
397 	return 0;
398 }
399 
400 
401 int
402 rte_ethtool_get_ringparam(uint16_t port_id,
403 	struct ethtool_ringparam *ring_param)
404 {
405 	struct rte_eth_dev_info dev_info;
406 	struct rte_eth_rxq_info rx_qinfo;
407 	struct rte_eth_txq_info tx_qinfo;
408 	int stat;
409 	int ret;
410 
411 	if (ring_param == NULL)
412 		return -EINVAL;
413 
414 	ret = rte_eth_dev_info_get(port_id, &dev_info);
415 	if (ret != 0)
416 		return ret;
417 
418 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
419 	if (stat != 0)
420 		return stat;
421 
422 	stat = rte_eth_tx_queue_info_get(port_id, 0, &tx_qinfo);
423 	if (stat != 0)
424 		return stat;
425 
426 	memset(ring_param, 0, sizeof(*ring_param));
427 	ring_param->rx_pending = rx_qinfo.nb_desc;
428 	ring_param->rx_max_pending = dev_info.rx_desc_lim.nb_max;
429 	ring_param->tx_pending = tx_qinfo.nb_desc;
430 	ring_param->tx_max_pending = dev_info.tx_desc_lim.nb_max;
431 
432 	return 0;
433 }
434 
435 
436 int
437 rte_ethtool_set_ringparam(uint16_t port_id,
438 	struct ethtool_ringparam *ring_param)
439 {
440 	struct rte_eth_rxq_info rx_qinfo;
441 	int stat;
442 
443 	if (ring_param == NULL)
444 		return -EINVAL;
445 
446 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
447 	if (stat != 0)
448 		return stat;
449 
450 	stat = rte_eth_dev_stop(port_id);
451 	if (stat != 0)
452 		return stat;
453 
454 	stat = rte_eth_tx_queue_setup(port_id, 0, ring_param->tx_pending,
455 		rte_eth_dev_socket_id(port_id), NULL);
456 	if (stat != 0)
457 		return stat;
458 
459 	stat = rte_eth_rx_queue_setup(port_id, 0, ring_param->rx_pending,
460 		rte_eth_dev_socket_id(port_id), NULL, rx_qinfo.mp);
461 	if (stat != 0)
462 		return stat;
463 
464 	return rte_eth_dev_start(port_id);
465 }
466