xref: /dpdk/examples/ethtool/lib/rte_ethtool.c (revision 7917b0d38e92e8b9ec5a870415b791420e10f11a)
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 	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 	return rte_eth_dev_start(port_id);
288 }
289 
290 int
291 rte_ethtool_net_stop(uint16_t port_id)
292 {
293 	return rte_eth_dev_stop(port_id);
294 }
295 
296 int
297 rte_ethtool_net_get_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
298 {
299 	int ret;
300 
301 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
302 	if (addr == NULL)
303 		return -EINVAL;
304 
305 	ret = rte_eth_macaddr_get(port_id, addr);
306 	if (ret != 0)
307 		return ret;
308 
309 	return 0;
310 }
311 
312 int
313 rte_ethtool_net_set_mac_addr(uint16_t port_id, struct rte_ether_addr *addr)
314 {
315 	if (addr == NULL)
316 		return -EINVAL;
317 	return rte_eth_dev_default_mac_addr_set(port_id, addr);
318 }
319 
320 int
321 rte_ethtool_net_validate_addr(uint16_t port_id __rte_unused,
322 	struct rte_ether_addr *addr)
323 {
324 	if (addr == NULL)
325 		return -EINVAL;
326 	return rte_is_valid_assigned_ether_addr(addr);
327 }
328 
329 int
330 rte_ethtool_net_change_mtu(uint16_t port_id, int mtu)
331 {
332 	if (mtu < 0 || mtu > UINT16_MAX)
333 		return -EINVAL;
334 	return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
335 }
336 
337 int
338 rte_ethtool_net_get_stats64(uint16_t port_id, struct rte_eth_stats *stats)
339 {
340 	if (stats == NULL)
341 		return -EINVAL;
342 	return rte_eth_stats_get(port_id, stats);
343 }
344 
345 int
346 rte_ethtool_net_vlan_rx_add_vid(uint16_t port_id, uint16_t vid)
347 {
348 	return rte_eth_dev_vlan_filter(port_id, vid, 1);
349 }
350 
351 int
352 rte_ethtool_net_vlan_rx_kill_vid(uint16_t port_id, uint16_t vid)
353 {
354 	return rte_eth_dev_vlan_filter(port_id, vid, 0);
355 }
356 
357 /*
358  * The set_rx_mode provides driver-specific rx mode setting.
359  * This implementation implements rx mode setting based upon
360  * ixgbe/igb drivers. Further improvement is to provide a
361  * callback op field over struct rte_eth_dev::dev_ops so each
362  * driver can register device-specific implementation
363  */
364 int
365 rte_ethtool_net_set_rx_mode(uint16_t port_id)
366 {
367 	uint16_t num_vfs;
368 	struct rte_eth_dev_info dev_info;
369 	uint16_t vf;
370 	int ret;
371 
372 	ret = rte_eth_dev_info_get(port_id, &dev_info);
373 	if (ret != 0)
374 		return ret;
375 
376 	num_vfs = dev_info.max_vfs;
377 
378 	/* Set VF vf_rx_mode, VF unsupport status is discard */
379 	for (vf = 0; vf < num_vfs; vf++) {
380 #ifdef RTE_NET_IXGBE
381 		rte_pmd_ixgbe_set_vf_rxmode(port_id, vf,
382 			RTE_ETH_VMDQ_ACCEPT_UNTAG, 0);
383 #endif
384 	}
385 
386 	/* Enable Rx vlan filter, VF unsupported status is discard */
387 	ret = rte_eth_dev_set_vlan_offload(port_id, RTE_ETH_VLAN_FILTER_MASK);
388 	if (ret != 0)
389 		return ret;
390 
391 	return 0;
392 }
393 
394 
395 int
396 rte_ethtool_get_ringparam(uint16_t port_id,
397 	struct ethtool_ringparam *ring_param)
398 {
399 	struct rte_eth_dev_info dev_info;
400 	struct rte_eth_rxq_info rx_qinfo;
401 	struct rte_eth_txq_info tx_qinfo;
402 	int stat;
403 	int ret;
404 
405 	if (ring_param == NULL)
406 		return -EINVAL;
407 
408 	ret = rte_eth_dev_info_get(port_id, &dev_info);
409 	if (ret != 0)
410 		return ret;
411 
412 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
413 	if (stat != 0)
414 		return stat;
415 
416 	stat = rte_eth_tx_queue_info_get(port_id, 0, &tx_qinfo);
417 	if (stat != 0)
418 		return stat;
419 
420 	memset(ring_param, 0, sizeof(*ring_param));
421 	ring_param->rx_pending = rx_qinfo.nb_desc;
422 	ring_param->rx_max_pending = dev_info.rx_desc_lim.nb_max;
423 	ring_param->tx_pending = tx_qinfo.nb_desc;
424 	ring_param->tx_max_pending = dev_info.tx_desc_lim.nb_max;
425 
426 	return 0;
427 }
428 
429 
430 int
431 rte_ethtool_set_ringparam(uint16_t port_id,
432 	struct ethtool_ringparam *ring_param)
433 {
434 	struct rte_eth_rxq_info rx_qinfo;
435 	int stat;
436 
437 	if (ring_param == NULL)
438 		return -EINVAL;
439 
440 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
441 	if (stat != 0)
442 		return stat;
443 
444 	stat = rte_eth_dev_stop(port_id);
445 	if (stat != 0)
446 		return stat;
447 
448 	stat = rte_eth_tx_queue_setup(port_id, 0, ring_param->tx_pending,
449 		rte_eth_dev_socket_id(port_id), NULL);
450 	if (stat != 0)
451 		return stat;
452 
453 	stat = rte_eth_rx_queue_setup(port_id, 0, ring_param->rx_pending,
454 		rte_eth_dev_socket_id(port_id), NULL, rx_qinfo.mp);
455 	if (stat != 0)
456 		return stat;
457 
458 	return rte_eth_dev_start(port_id);
459 }
460