xref: /dpdk/examples/ethtool/lib/rte_ethtool.c (revision 0499793854f52d0a42aa39a6b8036700f3719735)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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_ethtool.h"
40 
41 #define PKTPOOL_SIZE 512
42 #define PKTPOOL_CACHE 32
43 
44 
45 int
46 rte_ethtool_get_drvinfo(uint8_t port_id, struct ethtool_drvinfo *drvinfo)
47 {
48 	struct rte_eth_dev_info dev_info;
49 	int n;
50 
51 	if (drvinfo == NULL)
52 		return -EINVAL;
53 
54 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
55 
56 	memset(&dev_info, 0, sizeof(dev_info));
57 	rte_eth_dev_info_get(port_id, &dev_info);
58 
59 	snprintf(drvinfo->driver, sizeof(drvinfo->driver), "%s",
60 		dev_info.driver_name);
61 	snprintf(drvinfo->version, sizeof(drvinfo->version), "%s",
62 		rte_version());
63 	snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info),
64 		"%04x:%02x:%02x.%x",
65 		dev_info.pci_dev->addr.domain, dev_info.pci_dev->addr.bus,
66 		dev_info.pci_dev->addr.devid, dev_info.pci_dev->addr.function);
67 
68 	n = rte_eth_dev_get_reg_length(port_id);
69 	if (n > 0)
70 		drvinfo->regdump_len = n;
71 	else
72 		drvinfo->regdump_len = 0;
73 
74 	n = rte_eth_dev_get_eeprom_length(port_id);
75 	if (n > 0)
76 		drvinfo->eedump_len = n;
77 	else
78 		drvinfo->eedump_len = 0;
79 
80 	drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
81 	drvinfo->testinfo_len = 0;
82 
83 	return 0;
84 }
85 
86 int
87 rte_ethtool_get_regs_len(uint8_t port_id)
88 {
89 	int count_regs;
90 
91 	count_regs = rte_eth_dev_get_reg_length(port_id);
92 	if (count_regs > 0)
93 		return count_regs * sizeof(uint32_t);
94 	return count_regs;
95 }
96 
97 int
98 rte_ethtool_get_regs(uint8_t port_id, struct ethtool_regs *regs, void *data)
99 {
100 	struct rte_dev_reg_info reg_info;
101 	int status;
102 
103 	if (regs == NULL || data == NULL)
104 		return -EINVAL;
105 
106 	reg_info.data = data;
107 	reg_info.length = 0;
108 
109 	status = rte_eth_dev_get_reg_info(port_id, &reg_info);
110 	if (status)
111 		return status;
112 	regs->version = reg_info.version;
113 
114 	return 0;
115 }
116 
117 int
118 rte_ethtool_get_link(uint8_t port_id)
119 {
120 	struct rte_eth_link link;
121 
122 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
123 	rte_eth_link_get(port_id, &link);
124 	return link.link_status;
125 }
126 
127 int
128 rte_ethtool_get_eeprom_len(uint8_t port_id)
129 {
130 	return rte_eth_dev_get_eeprom_length(port_id);
131 }
132 
133 int
134 rte_ethtool_get_eeprom(uint8_t port_id, struct ethtool_eeprom *eeprom,
135 	void *words)
136 {
137 	struct rte_dev_eeprom_info eeprom_info;
138 	int status;
139 
140 	if (eeprom == NULL || words == NULL)
141 		return -EINVAL;
142 
143 	eeprom_info.offset = eeprom->offset;
144 	eeprom_info.length = eeprom->len;
145 	eeprom_info.data = words;
146 
147 	status = rte_eth_dev_get_eeprom(port_id, &eeprom_info);
148 	if (status)
149 		return status;
150 
151 	eeprom->magic = eeprom_info.magic;
152 
153 	return 0;
154 }
155 
156 int
157 rte_ethtool_set_eeprom(uint8_t port_id, struct ethtool_eeprom *eeprom,
158 	void *words)
159 {
160 	struct rte_dev_eeprom_info eeprom_info;
161 	int status;
162 
163 	if (eeprom == NULL || words == NULL || eeprom->offset >= eeprom->len)
164 		return -EINVAL;
165 
166 	eeprom_info.offset = eeprom->offset;
167 	eeprom_info.length = eeprom->len;
168 	eeprom_info.data = words;
169 
170 	status = rte_eth_dev_set_eeprom(port_id, &eeprom_info);
171 	if (status)
172 		return status;
173 
174 	eeprom->magic = eeprom_info.magic;
175 
176 	return 0;
177 }
178 
179 int
180 rte_ethtool_get_pauseparam(uint8_t port_id,
181 	struct ethtool_pauseparam *pause_param)
182 {
183 	struct rte_eth_fc_conf fc_conf;
184 	int status;
185 
186 	if (pause_param == NULL)
187 		return -EINVAL;
188 
189 	status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
190 	if (status)
191 		return status;
192 
193 	pause_param->tx_pause = 0;
194 	pause_param->rx_pause = 0;
195 	switch (fc_conf.mode) {
196 	case RTE_FC_RX_PAUSE:
197 		pause_param->rx_pause = 1;
198 		break;
199 	case RTE_FC_TX_PAUSE:
200 		pause_param->tx_pause = 1;
201 		break;
202 	case RTE_FC_FULL:
203 		pause_param->rx_pause = 1;
204 		pause_param->tx_pause = 1;
205 	default:
206 		/* dummy block to avoid compiler warning */
207 		break;
208 	}
209 	pause_param->autoneg = (uint32_t)fc_conf.autoneg;
210 
211 	return 0;
212 }
213 
214 int
215 rte_ethtool_set_pauseparam(uint8_t port_id,
216 	struct ethtool_pauseparam *pause_param)
217 {
218 	struct rte_eth_fc_conf fc_conf;
219 	int status;
220 
221 	if (pause_param == NULL)
222 		return -EINVAL;
223 
224 	/*
225 	 * Read device flow control parameter first since
226 	 * ethtool set_pauseparam op doesn't have all the information.
227 	 * as defined in struct rte_eth_fc_conf.
228 	 * This API requires the device to support both
229 	 * rte_eth_dev_flow_ctrl_get and rte_eth_dev_flow_ctrl_set, otherwise
230 	 * return -ENOTSUP
231 	 */
232 	status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
233 	if (status)
234 		return status;
235 
236 	fc_conf.autoneg = (uint8_t)pause_param->autoneg;
237 
238 	if (pause_param->tx_pause) {
239 		if (pause_param->rx_pause)
240 			fc_conf.mode = RTE_FC_FULL;
241 		else
242 			fc_conf.mode = RTE_FC_TX_PAUSE;
243 	} else {
244 		if (pause_param->rx_pause)
245 			fc_conf.mode = RTE_FC_RX_PAUSE;
246 		else
247 			fc_conf.mode = RTE_FC_NONE;
248 	}
249 
250 	status = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
251 	if (status)
252 		return status;
253 
254 	return 0;
255 }
256 
257 int
258 rte_ethtool_net_open(uint8_t port_id)
259 {
260 	rte_eth_dev_stop(port_id);
261 
262 	return rte_eth_dev_start(port_id);
263 }
264 
265 int
266 rte_ethtool_net_stop(uint8_t port_id)
267 {
268 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
269 	rte_eth_dev_stop(port_id);
270 
271 	return 0;
272 }
273 
274 int
275 rte_ethtool_net_get_mac_addr(uint8_t port_id, struct ether_addr *addr)
276 {
277 	RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
278 	if (addr == NULL)
279 		return -EINVAL;
280 	rte_eth_macaddr_get(port_id, addr);
281 
282 	return 0;
283 }
284 
285 int
286 rte_ethtool_net_set_mac_addr(uint8_t port_id, struct ether_addr *addr)
287 {
288 	if (addr == NULL)
289 		return -EINVAL;
290 	return rte_eth_dev_default_mac_addr_set(port_id, addr);
291 }
292 
293 int
294 rte_ethtool_net_validate_addr(uint8_t port_id __rte_unused,
295 	struct ether_addr *addr)
296 {
297 	if (addr == NULL)
298 		return -EINVAL;
299 	return is_valid_assigned_ether_addr(addr);
300 }
301 
302 int
303 rte_ethtool_net_change_mtu(uint8_t port_id, int mtu)
304 {
305 	if (mtu < 0 || mtu > UINT16_MAX)
306 		return -EINVAL;
307 	return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
308 }
309 
310 int
311 rte_ethtool_net_get_stats64(uint8_t port_id, struct rte_eth_stats *stats)
312 {
313 	if (stats == NULL)
314 		return -EINVAL;
315 	return rte_eth_stats_get(port_id, stats);
316 }
317 
318 int
319 rte_ethtool_net_vlan_rx_add_vid(uint8_t port_id, uint16_t vid)
320 {
321 	return rte_eth_dev_vlan_filter(port_id, vid, 1);
322 }
323 
324 int
325 rte_ethtool_net_vlan_rx_kill_vid(uint8_t port_id, uint16_t vid)
326 {
327 	return rte_eth_dev_vlan_filter(port_id, vid, 0);
328 }
329 
330 /*
331  * The set_rx_mode provides driver-specific rx mode setting.
332  * This implementation implements rx mode setting based upon
333  * ixgbe/igb drivers. Further improvement is to provide a
334  * callback op field over struct rte_eth_dev::dev_ops so each
335  * driver can register device-specific implementation
336  */
337 int
338 rte_ethtool_net_set_rx_mode(uint8_t port_id)
339 {
340 	uint16_t num_vfs;
341 	struct rte_eth_dev_info dev_info;
342 	uint16_t vf;
343 
344 	memset(&dev_info, 0, sizeof(dev_info));
345 	rte_eth_dev_info_get(port_id, &dev_info);
346 	num_vfs = dev_info.max_vfs;
347 
348 	/* Set VF vf_rx_mode, VF unsupport status is discard */
349 	for (vf = 0; vf < num_vfs; vf++)
350 		rte_eth_dev_set_vf_rxmode(port_id, vf,
351 			ETH_VMDQ_ACCEPT_UNTAG, 0);
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(uint8_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(uint8_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