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