1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
11 #include <rte_ethdev_driver.h>
12 #include <rte_common.h>
13 #include <rte_malloc.h>
15 #include <mlx5_common.h>
17 #include "mlx5_defs.h"
19 #include "mlx5_rxtx.h"
22 * DPDK callback to get extended device statistics.
25 * Pointer to Ethernet device.
27 * Pointer to rte extended stats table.
29 * The size of the stats table.
32 * Number of extended stats on success and stats is filled,
33 * negative on error and rte_errno is set.
36 mlx5_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
39 struct mlx5_priv *priv = dev->data->dev_private;
42 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
43 uint16_t mlx5_stats_n = xstats_ctrl->mlx5_stats_n;
45 if (n >= mlx5_stats_n && stats) {
49 stats_n = mlx5_os_get_stats_n(dev);
52 if (xstats_ctrl->stats_n != stats_n)
53 mlx5_os_stats_init(dev);
54 ret = mlx5_os_read_dev_counters(dev, counters);
57 for (i = 0; i != mlx5_stats_n; ++i) {
59 if (xstats_ctrl->info[i].dev) {
61 uint64_t hw_stat = xstats_ctrl->hw_stats[i];
63 stats[i].value = (counters[i] -
64 xstats_ctrl->base[i]) &
66 wrap_n = hw_stat >> 32;
68 (hw_stat & (uint64_t)UINT32_MAX))
70 stats[i].value |= (wrap_n) << 32;
71 xstats_ctrl->hw_stats[i] = stats[i].value;
74 (counters[i] - xstats_ctrl->base[i]);
82 * DPDK callback to get device statistics.
85 * Pointer to Ethernet device structure.
87 * Stats structure output buffer.
90 * 0 on success and stats is filled, negative errno value otherwise and
94 mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
96 struct mlx5_priv *priv = dev->data->dev_private;
97 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
98 struct rte_eth_stats tmp;
104 memset(&tmp, 0, sizeof(tmp));
105 /* Add software counters. */
106 for (i = 0; (i != priv->rxqs_n); ++i) {
107 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
112 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
113 #ifdef MLX5_PMD_SOFT_COUNTERS
114 tmp.q_ipackets[idx] += rxq->stats.ipackets;
115 tmp.q_ibytes[idx] += rxq->stats.ibytes;
117 tmp.q_errors[idx] += (rxq->stats.idropped +
118 rxq->stats.rx_nombuf);
120 #ifdef MLX5_PMD_SOFT_COUNTERS
121 tmp.ipackets += rxq->stats.ipackets;
122 tmp.ibytes += rxq->stats.ibytes;
124 tmp.ierrors += rxq->stats.idropped;
125 tmp.rx_nombuf += rxq->stats.rx_nombuf;
127 for (i = 0; (i != priv->txqs_n); ++i) {
128 struct mlx5_txq_data *txq = (*priv->txqs)[i];
133 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
134 #ifdef MLX5_PMD_SOFT_COUNTERS
135 tmp.q_opackets[idx] += txq->stats.opackets;
136 tmp.q_obytes[idx] += txq->stats.obytes;
139 #ifdef MLX5_PMD_SOFT_COUNTERS
140 tmp.opackets += txq->stats.opackets;
141 tmp.obytes += txq->stats.obytes;
143 tmp.oerrors += txq->stats.oerrors;
145 ret = mlx5_os_read_dev_stat(priv, "out_of_buffer", &tmp.imissed);
147 tmp.imissed = (tmp.imissed - stats_ctrl->imissed_base) &
148 (uint64_t)UINT32_MAX;
149 wrap_n = stats_ctrl->imissed >> 32;
150 if (tmp.imissed < (stats_ctrl->imissed & (uint64_t)UINT32_MAX))
152 tmp.imissed |= (wrap_n) << 32;
153 stats_ctrl->imissed = tmp.imissed;
155 tmp.imissed = stats_ctrl->imissed;
157 #ifndef MLX5_PMD_SOFT_COUNTERS
158 /* FIXME: retrieve and add hardware counters. */
165 * DPDK callback to clear device statistics.
168 * Pointer to Ethernet device structure.
171 * always 0 on success and stats is reset
174 mlx5_stats_reset(struct rte_eth_dev *dev)
176 struct mlx5_priv *priv = dev->data->dev_private;
177 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
180 for (i = 0; (i != priv->rxqs_n); ++i) {
181 if ((*priv->rxqs)[i] == NULL)
183 memset(&(*priv->rxqs)[i]->stats, 0,
184 sizeof(struct mlx5_rxq_stats));
186 for (i = 0; (i != priv->txqs_n); ++i) {
187 if ((*priv->txqs)[i] == NULL)
189 memset(&(*priv->txqs)[i]->stats, 0,
190 sizeof(struct mlx5_txq_stats));
192 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
193 stats_ctrl->imissed = 0;
194 #ifndef MLX5_PMD_SOFT_COUNTERS
195 /* FIXME: reset hardware counters. */
202 * DPDK callback to clear device extended statistics.
205 * Pointer to Ethernet device structure.
208 * 0 on success and stats is reset, negative errno value otherwise and
212 mlx5_xstats_reset(struct rte_eth_dev *dev)
214 struct mlx5_priv *priv = dev->data->dev_private;
215 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
218 unsigned int n = xstats_ctrl->mlx5_stats_n;
219 uint64_t counters[n];
222 stats_n = mlx5_os_get_stats_n(dev);
224 DRV_LOG(ERR, "port %u cannot get stats: %s", dev->data->port_id,
228 if (xstats_ctrl->stats_n != stats_n)
229 mlx5_os_stats_init(dev);
230 ret = mlx5_os_read_dev_counters(dev, counters);
232 DRV_LOG(ERR, "port %u cannot read device counters: %s",
233 dev->data->port_id, strerror(rte_errno));
236 for (i = 0; i != n; ++i) {
237 xstats_ctrl->base[i] = counters[i];
238 xstats_ctrl->hw_stats[i] = 0;
245 * DPDK callback to retrieve names of extended device statistics
248 * Pointer to Ethernet device structure.
249 * @param[out] xstats_names
250 * Buffer to insert names into.
255 * Number of xstats names.
258 mlx5_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
259 struct rte_eth_xstat_name *xstats_names, unsigned int n)
262 struct mlx5_priv *priv = dev->data->dev_private;
263 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
264 unsigned int mlx5_xstats_n = xstats_ctrl->mlx5_stats_n;
266 if (n >= mlx5_xstats_n && xstats_names) {
267 for (i = 0; i != mlx5_xstats_n; ++i) {
268 strncpy(xstats_names[i].name,
269 xstats_ctrl->info[i].dpdk_name,
270 RTE_ETH_XSTATS_NAME_SIZE);
271 xstats_names[i].name[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;
274 return mlx5_xstats_n;