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