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