d519a50db953a6c799736c47e8f198c3734c550d
[dpdk.git] / examples / ethtool / lib / rte_ethtool.c
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_version.h>
8 #include <rte_ethdev.h>
9 #include <rte_ether.h>
10 #include <rte_bus_pci.h>
11 #ifdef RTE_LIBRTE_IXGBE_PMD
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         const struct rte_pci_device *pci_dev;
26         const struct rte_bus *bus = NULL;
27         int n;
28         int ret;
29
30         if (drvinfo == NULL)
31                 return -EINVAL;
32
33         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
34
35         ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
36                               sizeof(drvinfo->fw_version));
37         if (ret < 0)
38                 printf("firmware version get error: (%s)\n", strerror(-ret));
39         else if (ret > 0)
40                 printf("Insufficient fw version buffer size, "
41                        "the minimum size should be %d\n", ret);
42
43         memset(&dev_info, 0, sizeof(dev_info));
44         rte_eth_dev_info_get(port_id, &dev_info);
45
46         snprintf(drvinfo->driver, sizeof(drvinfo->driver), "%s",
47                 dev_info.driver_name);
48         snprintf(drvinfo->version, sizeof(drvinfo->version), "%s",
49                 rte_version());
50         /* TODO: replace bus_info by rte_devargs.name */
51         if (dev_info.device)
52                 bus = rte_bus_find_by_device(dev_info.device);
53         if (bus && !strcmp(bus->name, "pci")) {
54                 pci_dev = RTE_DEV_TO_PCI(dev_info.device);
55                 snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info),
56                         "%04x:%02x:%02x.%x",
57                         pci_dev->addr.domain, pci_dev->addr.bus,
58                         pci_dev->addr.devid, pci_dev->addr.function);
59         } else {
60                 snprintf(drvinfo->bus_info, sizeof(drvinfo->bus_info), "N/A");
61         }
62
63         memset(&reg_info, 0, sizeof(reg_info));
64         rte_eth_dev_get_reg_info(port_id, &reg_info);
65         n = reg_info.length;
66         if (n > 0)
67                 drvinfo->regdump_len = n;
68         else
69                 drvinfo->regdump_len = 0;
70
71         n = rte_eth_dev_get_eeprom_length(port_id);
72         if (n > 0)
73                 drvinfo->eedump_len = n;
74         else
75                 drvinfo->eedump_len = 0;
76
77         drvinfo->n_stats = sizeof(struct rte_eth_stats) / sizeof(uint64_t);
78         drvinfo->testinfo_len = 0;
79
80         return 0;
81 }
82
83 int
84 rte_ethtool_get_regs_len(uint16_t port_id)
85 {
86         struct rte_dev_reg_info reg_info;
87         int ret;
88
89         memset(&reg_info, 0, sizeof(reg_info));
90
91         ret = rte_eth_dev_get_reg_info(port_id, &reg_info);
92         if (ret)
93                 return ret;
94
95         return reg_info.length * reg_info.width;
96 }
97
98 int
99 rte_ethtool_get_regs(uint16_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(uint16_t port_id)
120 {
121         struct rte_eth_link link;
122
123         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
124         rte_eth_link_get(port_id, &link);
125         return link.link_status;
126 }
127
128 int
129 rte_ethtool_get_eeprom_len(uint16_t port_id)
130 {
131         return rte_eth_dev_get_eeprom_length(port_id);
132 }
133
134 int
135 rte_ethtool_get_eeprom(uint16_t port_id, struct ethtool_eeprom *eeprom,
136         void *words)
137 {
138         struct rte_dev_eeprom_info eeprom_info;
139         int status;
140
141         if (eeprom == NULL || words == NULL)
142                 return -EINVAL;
143
144         eeprom_info.offset = eeprom->offset;
145         eeprom_info.length = eeprom->len;
146         eeprom_info.data = words;
147
148         status = rte_eth_dev_get_eeprom(port_id, &eeprom_info);
149         if (status)
150                 return status;
151
152         eeprom->magic = eeprom_info.magic;
153
154         return 0;
155 }
156
157 int
158 rte_ethtool_set_eeprom(uint16_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 || eeprom->offset >= eeprom->len)
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_set_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_get_pauseparam(uint16_t port_id,
182         struct ethtool_pauseparam *pause_param)
183 {
184         struct rte_eth_fc_conf fc_conf;
185         int status;
186
187         if (pause_param == NULL)
188                 return -EINVAL;
189
190         status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
191         if (status)
192                 return status;
193
194         pause_param->tx_pause = 0;
195         pause_param->rx_pause = 0;
196         switch (fc_conf.mode) {
197         case RTE_FC_RX_PAUSE:
198                 pause_param->rx_pause = 1;
199                 break;
200         case RTE_FC_TX_PAUSE:
201                 pause_param->tx_pause = 1;
202                 break;
203         case RTE_FC_FULL:
204                 pause_param->rx_pause = 1;
205                 pause_param->tx_pause = 1;
206         default:
207                 /* dummy block to avoid compiler warning */
208                 break;
209         }
210         pause_param->autoneg = (uint32_t)fc_conf.autoneg;
211
212         return 0;
213 }
214
215 int
216 rte_ethtool_set_pauseparam(uint16_t port_id,
217         struct ethtool_pauseparam *pause_param)
218 {
219         struct rte_eth_fc_conf fc_conf;
220         int status;
221
222         if (pause_param == NULL)
223                 return -EINVAL;
224
225         /*
226          * Read device flow control parameter first since
227          * ethtool set_pauseparam op doesn't have all the information.
228          * as defined in struct rte_eth_fc_conf.
229          * This API requires the device to support both
230          * rte_eth_dev_flow_ctrl_get and rte_eth_dev_flow_ctrl_set, otherwise
231          * return -ENOTSUP
232          */
233         status = rte_eth_dev_flow_ctrl_get(port_id, &fc_conf);
234         if (status)
235                 return status;
236
237         fc_conf.autoneg = (uint8_t)pause_param->autoneg;
238
239         if (pause_param->tx_pause) {
240                 if (pause_param->rx_pause)
241                         fc_conf.mode = RTE_FC_FULL;
242                 else
243                         fc_conf.mode = RTE_FC_TX_PAUSE;
244         } else {
245                 if (pause_param->rx_pause)
246                         fc_conf.mode = RTE_FC_RX_PAUSE;
247                 else
248                         fc_conf.mode = RTE_FC_NONE;
249         }
250
251         status = rte_eth_dev_flow_ctrl_set(port_id, &fc_conf);
252         if (status)
253                 return status;
254
255         return 0;
256 }
257
258 int
259 rte_ethtool_net_open(uint16_t port_id)
260 {
261         rte_eth_dev_stop(port_id);
262
263         return rte_eth_dev_start(port_id);
264 }
265
266 int
267 rte_ethtool_net_stop(uint16_t port_id)
268 {
269         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
270         rte_eth_dev_stop(port_id);
271
272         return 0;
273 }
274
275 int
276 rte_ethtool_net_get_mac_addr(uint16_t port_id, struct ether_addr *addr)
277 {
278         RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);
279         if (addr == NULL)
280                 return -EINVAL;
281         rte_eth_macaddr_get(port_id, addr);
282
283         return 0;
284 }
285
286 int
287 rte_ethtool_net_set_mac_addr(uint16_t port_id, struct ether_addr *addr)
288 {
289         if (addr == NULL)
290                 return -EINVAL;
291         return rte_eth_dev_default_mac_addr_set(port_id, addr);
292 }
293
294 int
295 rte_ethtool_net_validate_addr(uint16_t port_id __rte_unused,
296         struct ether_addr *addr)
297 {
298         if (addr == NULL)
299                 return -EINVAL;
300         return is_valid_assigned_ether_addr(addr);
301 }
302
303 int
304 rte_ethtool_net_change_mtu(uint16_t port_id, int mtu)
305 {
306         if (mtu < 0 || mtu > UINT16_MAX)
307                 return -EINVAL;
308         return rte_eth_dev_set_mtu(port_id, (uint16_t)mtu);
309 }
310
311 int
312 rte_ethtool_net_get_stats64(uint16_t port_id, struct rte_eth_stats *stats)
313 {
314         if (stats == NULL)
315                 return -EINVAL;
316         return rte_eth_stats_get(port_id, stats);
317 }
318
319 int
320 rte_ethtool_net_vlan_rx_add_vid(uint16_t port_id, uint16_t vid)
321 {
322         return rte_eth_dev_vlan_filter(port_id, vid, 1);
323 }
324
325 int
326 rte_ethtool_net_vlan_rx_kill_vid(uint16_t port_id, uint16_t vid)
327 {
328         return rte_eth_dev_vlan_filter(port_id, vid, 0);
329 }
330
331 /*
332  * The set_rx_mode provides driver-specific rx mode setting.
333  * This implementation implements rx mode setting based upon
334  * ixgbe/igb drivers. Further improvement is to provide a
335  * callback op field over struct rte_eth_dev::dev_ops so each
336  * driver can register device-specific implementation
337  */
338 int
339 rte_ethtool_net_set_rx_mode(uint16_t port_id)
340 {
341         uint16_t num_vfs;
342         struct rte_eth_dev_info dev_info;
343         uint16_t vf;
344
345         memset(&dev_info, 0, sizeof(dev_info));
346         rte_eth_dev_info_get(port_id, &dev_info);
347         num_vfs = dev_info.max_vfs;
348
349         /* Set VF vf_rx_mode, VF unsupport status is discard */
350         for (vf = 0; vf < num_vfs; vf++) {
351 #ifdef RTE_LIBRTE_IXGBE_PMD
352                 rte_pmd_ixgbe_set_vf_rxmode(port_id, vf,
353                         ETH_VMDQ_ACCEPT_UNTAG, 0);
354 #endif
355         }
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(uint16_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(uint16_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 }