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