4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
17 * * Neither the name of 6WIND S.A. 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.
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.
34 #include <linux/sockios.h>
35 #include <linux/ethtool.h>
37 #include <rte_ethdev.h>
38 #include <rte_common.h>
39 #include <rte_malloc.h>
42 #include "mlx5_rxtx.h"
43 #include "mlx5_defs.h"
45 struct mlx5_counter_ctrl {
46 /* Name of the counter. */
47 char dpdk_name[RTE_ETH_XSTATS_NAME_SIZE];
48 /* Name of the counter on the device table. */
49 char ctr_name[RTE_ETH_XSTATS_NAME_SIZE];
52 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
54 .dpdk_name = "rx_port_unicast_bytes",
55 .ctr_name = "rx_vport_unicast_bytes",
58 .dpdk_name = "rx_port_multicast_bytes",
59 .ctr_name = "rx_vport_multicast_bytes",
62 .dpdk_name = "rx_port_broadcast_bytes",
63 .ctr_name = "rx_vport_broadcast_bytes",
66 .dpdk_name = "rx_port_unicast_packets",
67 .ctr_name = "rx_vport_unicast_packets",
70 .dpdk_name = "rx_port_multicast_packets",
71 .ctr_name = "rx_vport_multicast_packets",
74 .dpdk_name = "rx_port_broadcast_packets",
75 .ctr_name = "rx_vport_broadcast_packets",
78 .dpdk_name = "tx_port_unicast_bytes",
79 .ctr_name = "tx_vport_unicast_bytes",
82 .dpdk_name = "tx_port_multicast_bytes",
83 .ctr_name = "tx_vport_multicast_bytes",
86 .dpdk_name = "tx_port_broadcast_bytes",
87 .ctr_name = "tx_vport_broadcast_bytes",
90 .dpdk_name = "tx_port_unicast_packets",
91 .ctr_name = "tx_vport_unicast_packets",
94 .dpdk_name = "tx_port_multicast_packets",
95 .ctr_name = "tx_vport_multicast_packets",
98 .dpdk_name = "tx_port_broadcast_packets",
99 .ctr_name = "tx_vport_broadcast_packets",
102 .dpdk_name = "rx_wqe_err",
103 .ctr_name = "rx_wqe_err",
106 .dpdk_name = "rx_crc_errors_phy",
107 .ctr_name = "rx_crc_errors_phy",
110 .dpdk_name = "rx_in_range_len_errors_phy",
111 .ctr_name = "rx_in_range_len_errors_phy",
114 .dpdk_name = "rx_symbol_err_phy",
115 .ctr_name = "rx_symbol_err_phy",
118 .dpdk_name = "tx_errors_phy",
119 .ctr_name = "tx_errors_phy",
122 .dpdk_name = "rx_out_of_buffer",
123 .ctr_name = "out_of_buffer",
127 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
130 * Read device counters table.
133 * Pointer to private structure.
135 * Counters table output buffer.
138 * 0 on success and stats is filled, negative on error.
141 priv_read_dev_counters(struct priv *priv, uint64_t *stats)
143 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
146 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
147 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
148 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
150 et_stats->cmd = ETHTOOL_GSTATS;
151 et_stats->n_stats = xstats_ctrl->stats_n;
152 ifr.ifr_data = (caddr_t)et_stats;
153 if (priv_ifreq(priv, SIOCETHTOOL, &ifr) != 0) {
154 WARN("unable to read statistic values from device");
157 for (i = 0; i != xstats_n; ++i) {
158 if (priv_is_ib_cntr(mlx5_counters_init[i].ctr_name))
159 priv_get_cntr_sysfs(priv,
160 mlx5_counters_init[i].ctr_name,
163 stats[i] = (uint64_t)
164 et_stats->data[xstats_ctrl->dev_table_idx[i]];
170 * Query the number of statistics provided by ETHTOOL.
173 * Pointer to private structure.
176 * Number of statistics on success, -1 on error.
179 priv_ethtool_get_stats_n(struct priv *priv) {
180 struct ethtool_drvinfo drvinfo;
183 drvinfo.cmd = ETHTOOL_GDRVINFO;
184 ifr.ifr_data = (caddr_t)&drvinfo;
185 if (priv_ifreq(priv, SIOCETHTOOL, &ifr) != 0) {
186 WARN("unable to query number of statistics");
189 return drvinfo.n_stats;
193 * Init the structures to read device counters.
196 * Pointer to private structure.
199 priv_xstats_init(struct priv *priv)
201 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
205 struct ethtool_gstrings *strings = NULL;
206 unsigned int dev_stats_n;
209 dev_stats_n = priv_ethtool_get_stats_n(priv);
210 if (dev_stats_n < 1) {
211 WARN("no extended statistics available");
214 xstats_ctrl->stats_n = dev_stats_n;
215 /* Allocate memory to grab stat names and values. */
216 str_sz = dev_stats_n * ETH_GSTRING_LEN;
217 strings = (struct ethtool_gstrings *)
218 rte_malloc("xstats_strings",
219 str_sz + sizeof(struct ethtool_gstrings), 0);
221 WARN("unable to allocate memory for xstats");
224 strings->cmd = ETHTOOL_GSTRINGS;
225 strings->string_set = ETH_SS_STATS;
226 strings->len = dev_stats_n;
227 ifr.ifr_data = (caddr_t)strings;
228 if (priv_ifreq(priv, SIOCETHTOOL, &ifr) != 0) {
229 WARN("unable to get statistic names");
232 for (j = 0; j != xstats_n; ++j)
233 xstats_ctrl->dev_table_idx[j] = dev_stats_n;
234 for (i = 0; i != dev_stats_n; ++i) {
235 const char *curr_string = (const char *)
236 &strings->data[i * ETH_GSTRING_LEN];
238 for (j = 0; j != xstats_n; ++j) {
239 if (!strcmp(mlx5_counters_init[j].ctr_name,
241 xstats_ctrl->dev_table_idx[j] = i;
246 for (j = 0; j != xstats_n; ++j) {
247 if (priv_is_ib_cntr(mlx5_counters_init[j].ctr_name))
249 if (xstats_ctrl->dev_table_idx[j] >= dev_stats_n) {
250 WARN("counter \"%s\" is not recognized",
251 mlx5_counters_init[j].dpdk_name);
255 /* Copy to base at first time. */
256 assert(xstats_n <= MLX5_MAX_XSTATS);
257 priv_read_dev_counters(priv, xstats_ctrl->base);
263 * Get device extended statistics.
266 * Pointer to private structure.
268 * Pointer to rte extended stats table.
271 * Number of extended stats on success and stats is filled,
275 priv_xstats_get(struct priv *priv, struct rte_eth_xstat *stats)
277 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
279 unsigned int n = xstats_n;
280 uint64_t counters[n];
282 if (priv_read_dev_counters(priv, counters) < 0)
284 for (i = 0; i != xstats_n; ++i) {
286 stats[i].value = (counters[i] - xstats_ctrl->base[i]);
292 * Reset device extended statistics.
295 * Pointer to private structure.
298 priv_xstats_reset(struct priv *priv)
300 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
302 unsigned int n = xstats_n;
303 uint64_t counters[n];
305 if (priv_read_dev_counters(priv, counters) < 0)
307 for (i = 0; i != n; ++i)
308 xstats_ctrl->base[i] = counters[i];
312 * DPDK callback to get device statistics.
315 * Pointer to Ethernet device structure.
317 * Stats structure output buffer.
320 mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
322 struct priv *priv = mlx5_get_priv(dev);
323 struct rte_eth_stats tmp = {0};
328 /* Add software counters. */
329 for (i = 0; (i != priv->rxqs_n); ++i) {
330 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
334 idx = rxq->stats.idx;
335 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
336 #ifdef MLX5_PMD_SOFT_COUNTERS
337 tmp.q_ipackets[idx] += rxq->stats.ipackets;
338 tmp.q_ibytes[idx] += rxq->stats.ibytes;
340 tmp.q_errors[idx] += (rxq->stats.idropped +
341 rxq->stats.rx_nombuf);
343 #ifdef MLX5_PMD_SOFT_COUNTERS
344 tmp.ipackets += rxq->stats.ipackets;
345 tmp.ibytes += rxq->stats.ibytes;
347 tmp.ierrors += rxq->stats.idropped;
348 tmp.rx_nombuf += rxq->stats.rx_nombuf;
350 for (i = 0; (i != priv->txqs_n); ++i) {
351 struct mlx5_txq_data *txq = (*priv->txqs)[i];
355 idx = txq->stats.idx;
356 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
357 #ifdef MLX5_PMD_SOFT_COUNTERS
358 tmp.q_opackets[idx] += txq->stats.opackets;
359 tmp.q_obytes[idx] += txq->stats.obytes;
361 tmp.q_errors[idx] += txq->stats.oerrors;
363 #ifdef MLX5_PMD_SOFT_COUNTERS
364 tmp.opackets += txq->stats.opackets;
365 tmp.obytes += txq->stats.obytes;
367 tmp.oerrors += txq->stats.oerrors;
369 #ifndef MLX5_PMD_SOFT_COUNTERS
370 /* FIXME: retrieve and add hardware counters. */
378 * DPDK callback to clear device statistics.
381 * Pointer to Ethernet device structure.
384 mlx5_stats_reset(struct rte_eth_dev *dev)
386 struct priv *priv = dev->data->dev_private;
391 for (i = 0; (i != priv->rxqs_n); ++i) {
392 if ((*priv->rxqs)[i] == NULL)
394 idx = (*priv->rxqs)[i]->stats.idx;
395 (*priv->rxqs)[i]->stats =
396 (struct mlx5_rxq_stats){ .idx = idx };
398 for (i = 0; (i != priv->txqs_n); ++i) {
399 if ((*priv->txqs)[i] == NULL)
401 idx = (*priv->txqs)[i]->stats.idx;
402 (*priv->txqs)[i]->stats =
403 (struct mlx5_txq_stats){ .idx = idx };
405 #ifndef MLX5_PMD_SOFT_COUNTERS
406 /* FIXME: reset hardware counters. */
412 * DPDK callback to get extended device statistics.
415 * Pointer to Ethernet device structure.
417 * Stats table output buffer.
419 * The size of the stats table.
422 * Number of xstats on success, negative on failure.
425 mlx5_xstats_get(struct rte_eth_dev *dev,
426 struct rte_eth_xstat *stats, unsigned int n)
428 struct priv *priv = mlx5_get_priv(dev);
431 if (n >= xstats_n && stats) {
432 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
436 stats_n = priv_ethtool_get_stats_n(priv);
441 if (xstats_ctrl->stats_n != stats_n)
442 priv_xstats_init(priv);
443 ret = priv_xstats_get(priv, stats);
450 * DPDK callback to clear device extended statistics.
453 * Pointer to Ethernet device structure.
456 mlx5_xstats_reset(struct rte_eth_dev *dev)
458 struct priv *priv = mlx5_get_priv(dev);
459 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
463 stats_n = priv_ethtool_get_stats_n(priv);
466 if (xstats_ctrl->stats_n != stats_n)
467 priv_xstats_init(priv);
468 priv_xstats_reset(priv);
474 * DPDK callback to retrieve names of extended device statistics
477 * Pointer to Ethernet device structure.
478 * @param[out] xstats_names
479 * Buffer to insert names into.
484 * Number of xstats names.
487 mlx5_xstats_get_names(struct rte_eth_dev *dev,
488 struct rte_eth_xstat_name *xstats_names, unsigned int n)
490 struct priv *priv = mlx5_get_priv(dev);
493 if (n >= xstats_n && xstats_names) {
495 for (i = 0; i != xstats_n; ++i) {
496 strncpy(xstats_names[i].name,
497 mlx5_counters_init[i].dpdk_name,
498 RTE_ETH_XSTATS_NAME_SIZE);
499 xstats_names[i].name[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;