xref: /dpdk/examples/ethtool/lib/rte_ethtool.c (revision 448c23320fdab9faeb47abb91690d41985327cbf)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <rte_version.h>
37 #include <rte_ethdev.h>
38 #include <rte_ether.h>
39 #include <rte_pci.h>
40 #include <rte_bus_pci.h>
41 #ifdef RTE_LIBRTE_IXGBE_PMD
42 #include <rte_pmd_ixgbe.h>
43 #endif
44 #include "rte_ethtool.h"
45 
46 #define PKTPOOL_SIZE 512
47 #define PKTPOOL_CACHE 32
48 
49 
50 int
51 rte_ethtool_get_drvinfo(uint16_t port_id, struct ethtool_drvinfo *drvinfo)
52 {
53 	struct rte_eth_dev_info dev_info;
54 	struct rte_dev_reg_info reg_info;
55 	int n;
56 	int ret;
57 
58 	if (drvinfo == NULL)
59 		return -EINVAL;
60 
61 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
62 
63 	ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
64 			      sizeof(drvinfo->fw_version));
65 	if (ret < 0)
66 		printf("firmware version get error: (%s)\n", strerror(-ret));
67 	else if (ret > 0)
68 		printf("Insufficient fw version buffer size, "
69 		       "the minimum size should be %d\n", ret);
70 
71 	memset(&dev_info, 0, sizeof(dev_info));
72 	rte_eth_dev_info_get(port_id, &dev_info);
73 
74 	snprintf(drvinfo->driver, sizeof(drvinfo->driver), "%s",
75 		dev_info.driver_name);
76 	snprintf(drvinfo->version, sizeof(drvinfo->version), "%s",
77 		rte_version());
78 	/* TODO: replace bus_info by rte_devargs.name */
79 	if (dev_info.pci_dev)
80 		snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info),
81 			"%04x:%02x:%02x.%x",
82 			dev_info.pci_dev->addr.domain,
83 			dev_info.pci_dev->addr.bus,
84 			dev_info.pci_dev->addr.devid,
85 			dev_info.pci_dev->addr.function);
86 	else
87 		snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info), "N/A");
88 
89 	memset(&reg_info, 0, sizeof(reg_info));
90 	rte_eth_dev_get_reg_info(port_id, &reg_info);
91 	n = reg_info.length;
92 	if (n > 0)
93 		drvinfo->regdump_len = n;
94 	else
95 		drvinfo->regdump_len = 0;
96 
97 	n = rte_eth_dev_get_eeprom_length(port_id);
98 	if (n > 0)
99 		drvinfo->eedump_len = n;
100 	else
101 		drvinfo->eedump_len = 0;
102 
103 	drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
104 	drvinfo->testinfo_len = 0;
105 
106 	return 0;
107 }
108 
109 int
110 rte_ethtool_get_regs_len(uint16_t port_id)
111 {
112 	struct rte_dev_reg_info reg_info;
113 	int ret;
114 
115 	memset(&reg_info, 0, sizeof(reg_info));
116 
117 	ret = rte_eth_dev_get_reg_info(port_id, &reg_info);
118 	if (ret)
119 		return ret;
120 
121 	return reg_info.length * reg_info.width;
122 }
123 
124 int
125 rte_ethtool_get_regs(uint16_t port_id, struct ethtool_regs *regs, void *data)
126 {
127 	struct rte_dev_reg_info reg_info;
128 	int status;
129 
130 	if (regs == NULL || data == NULL)
131 		return -EINVAL;
132 
133 	reg_info.data = data;
134 	reg_info.length = 0;
135 
136 	status = rte_eth_dev_get_reg_info(port_id, &reg_info);
137 	if (status)
138 		return status;
139 	regs->version = reg_info.version;
140 
141 	return 0;
142 }
143 
144 int
145 rte_ethtool_get_link(uint16_t port_id)
146 {
147 	struct rte_eth_link link;
148 
149 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
150 	rte_eth_link_get(port_id, &link);
151 	return link.link_status;
152 }
153 
154 int
155 rte_ethtool_get_eeprom_len(uint16_t port_id)
156 {
157 	return rte_eth_dev_get_eeprom_length(port_id);
158 }
159 
160 int
161 rte_ethtool_get_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
162 	void *words)
163 {
164 	struct rte_dev_eeprom_info eeprom_info;
165 	int status;
166 
167 	if (eeprom == NULL || words == NULL)
168 		return -EINVAL;
169 
170 	eeprom_info.offset = eeprom->offset;
171 	eeprom_info.length = eeprom->len;
172 	eeprom_info.data = words;
173 
174 	status = rte_eth_dev_get_eeprom(port_id, &eeprom_info);
175 	if (status)
176 		return status;
177 
178 	eeprom->magic = eeprom_info.magic;
179 
180 	return 0;
181 }
182 
183 int
184 rte_ethtool_set_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 || eeprom->offset >= eeprom->len)
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_set_eeprom(port_id, &eeprom_info);
198 	if (status)
199 		return status;
200 
201 	eeprom->magic = eeprom_info.magic;
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_FC_RX_PAUSE:
224 		pause_param->rx_pause = 1;
225 		break;
226 	case RTE_FC_TX_PAUSE:
227 		pause_param->tx_pause = 1;
228 		break;
229 	case RTE_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_FC_FULL;
268 		else
269 			fc_conf.mode = RTE_FC_TX_PAUSE;
270 	} else {
271 		if (pause_param->rx_pause)
272 			fc_conf.mode = RTE_FC_RX_PAUSE;
273 		else
274 			fc_conf.mode = RTE_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 	rte_eth_dev_stop(port_id);
288 
289 	return rte_eth_dev_start(port_id);
290 }
291 
292 int
293 rte_ethtool_net_stop(uint16_t port_id)
294 {
295 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
296 	rte_eth_dev_stop(port_id);
297 
298 	return 0;
299 }
300 
301 int
302 rte_ethtool_net_get_mac_addr(uint16_t port_id, struct ether_addr *addr)
303 {
304 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
305 	if (addr == NULL)
306 		return -EINVAL;
307 	rte_eth_macaddr_get(port_id, addr);
308 
309 	return 0;
310 }
311 
312 int
313 rte_ethtool_net_set_mac_addr(uint16_t port_id, struct 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 ether_addr *addr)
323 {
324 	if (addr == NULL)
325 		return -EINVAL;
326 	return 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 
371 	memset(&dev_info, 0, sizeof(dev_info));
372 	rte_eth_dev_info_get(port_id, &dev_info);
373 	num_vfs = dev_info.max_vfs;
374 
375 	/* Set VF vf_rx_mode, VF unsupport status is discard */
376 	for (vf = 0; vf < num_vfs; vf++) {
377 #ifdef RTE_LIBRTE_IXGBE_PMD
378 		rte_pmd_ixgbe_set_vf_rxmode(port_id, vf,
379 			ETH_VMDQ_ACCEPT_UNTAG, 0);
380 #endif
381 	}
382 
383 	/* Enable Rx vlan filter, VF unspport status is discard */
384 	rte_eth_dev_set_vlan_offload(port_id, ETH_VLAN_FILTER_MASK);
385 
386 	return 0;
387 }
388 
389 
390 int
391 rte_ethtool_get_ringparam(uint16_t port_id,
392 	struct ethtool_ringparam *ring_param)
393 {
394 	struct rte_eth_dev_info dev_info;
395 	struct rte_eth_rxq_info rx_qinfo;
396 	struct rte_eth_txq_info tx_qinfo;
397 	int stat;
398 
399 	if (ring_param == NULL)
400 		return -EINVAL;
401 
402 	rte_eth_dev_info_get(port_id, &dev_info);
403 
404 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
405 	if (stat != 0)
406 		return stat;
407 
408 	stat = rte_eth_tx_queue_info_get(port_id, 0, &tx_qinfo);
409 	if (stat != 0)
410 		return stat;
411 
412 	memset(ring_param, 0, sizeof(*ring_param));
413 	ring_param->rx_pending = rx_qinfo.nb_desc;
414 	ring_param->rx_max_pending = dev_info.rx_desc_lim.nb_max;
415 	ring_param->tx_pending = tx_qinfo.nb_desc;
416 	ring_param->tx_max_pending = dev_info.tx_desc_lim.nb_max;
417 
418 	return 0;
419 }
420 
421 
422 int
423 rte_ethtool_set_ringparam(uint16_t port_id,
424 	struct ethtool_ringparam *ring_param)
425 {
426 	struct rte_eth_rxq_info rx_qinfo;
427 	int stat;
428 
429 	if (ring_param == NULL)
430 		return -EINVAL;
431 
432 	stat = rte_eth_rx_queue_info_get(port_id, 0, &rx_qinfo);
433 	if (stat != 0)
434 		return stat;
435 
436 	rte_eth_dev_stop(port_id);
437 
438 	stat = rte_eth_tx_queue_setup(port_id, 0, ring_param->tx_pending,
439 		rte_socket_id(), NULL);
440 	if (stat != 0)
441 		return stat;
442 
443 	stat = rte_eth_rx_queue_setup(port_id, 0, ring_param->rx_pending,
444 		rte_socket_id(), NULL, rx_qinfo.mp);
445 	if (stat != 0)
446 		return stat;
447 
448 	return rte_eth_dev_start(port_id);
449 }
450