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]);
78 mlx5_stats_n = mlx5_txpp_xstats_get(dev, stats, n, mlx5_stats_n);
83 * DPDK callback to get device statistics.
86 * Pointer to Ethernet device structure.
88 * Stats structure output buffer.
91 * 0 on success and stats is filled, negative errno value otherwise and
95 mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
97 struct mlx5_priv *priv = dev->data->dev_private;
98 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
99 struct rte_eth_stats tmp;
105 memset(&tmp, 0, sizeof(tmp));
106 /* Add software counters. */
107 for (i = 0; (i != priv->rxqs_n); ++i) {
108 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
113 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
114 #ifdef MLX5_PMD_SOFT_COUNTERS
115 tmp.q_ipackets[idx] += rxq->stats.ipackets;
116 tmp.q_ibytes[idx] += rxq->stats.ibytes;
118 tmp.q_errors[idx] += (rxq->stats.idropped +
119 rxq->stats.rx_nombuf);
121 #ifdef MLX5_PMD_SOFT_COUNTERS
122 tmp.ipackets += rxq->stats.ipackets;
123 tmp.ibytes += rxq->stats.ibytes;
125 tmp.ierrors += rxq->stats.idropped;
126 tmp.rx_nombuf += rxq->stats.rx_nombuf;
128 for (i = 0; (i != priv->txqs_n); ++i) {
129 struct mlx5_txq_data *txq = (*priv->txqs)[i];
134 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
135 #ifdef MLX5_PMD_SOFT_COUNTERS
136 tmp.q_opackets[idx] += txq->stats.opackets;
137 tmp.q_obytes[idx] += txq->stats.obytes;
140 #ifdef MLX5_PMD_SOFT_COUNTERS
141 tmp.opackets += txq->stats.opackets;
142 tmp.obytes += txq->stats.obytes;
144 tmp.oerrors += txq->stats.oerrors;
146 ret = mlx5_os_read_dev_stat(priv, "out_of_buffer", &tmp.imissed);
148 tmp.imissed = (tmp.imissed - stats_ctrl->imissed_base) &
149 (uint64_t)UINT32_MAX;
150 wrap_n = stats_ctrl->imissed >> 32;
151 if (tmp.imissed < (stats_ctrl->imissed & (uint64_t)UINT32_MAX))
153 tmp.imissed |= (wrap_n) << 32;
154 stats_ctrl->imissed = tmp.imissed;
156 tmp.imissed = stats_ctrl->imissed;
158 #ifndef MLX5_PMD_SOFT_COUNTERS
159 /* FIXME: retrieve and add hardware counters. */
166 * DPDK callback to clear device statistics.
169 * Pointer to Ethernet device structure.
172 * always 0 on success and stats is reset
175 mlx5_stats_reset(struct rte_eth_dev *dev)
177 struct mlx5_priv *priv = dev->data->dev_private;
178 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
181 for (i = 0; (i != priv->rxqs_n); ++i) {
182 if ((*priv->rxqs)[i] == NULL)
184 memset(&(*priv->rxqs)[i]->stats, 0,
185 sizeof(struct mlx5_rxq_stats));
187 for (i = 0; (i != priv->txqs_n); ++i) {
188 if ((*priv->txqs)[i] == NULL)
190 memset(&(*priv->txqs)[i]->stats, 0,
191 sizeof(struct mlx5_txq_stats));
193 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
194 stats_ctrl->imissed = 0;
195 #ifndef MLX5_PMD_SOFT_COUNTERS
196 /* FIXME: reset hardware counters. */
203 * DPDK callback to clear device extended statistics.
206 * Pointer to Ethernet device structure.
209 * 0 on success and stats is reset, negative errno value otherwise and
213 mlx5_xstats_reset(struct rte_eth_dev *dev)
215 struct mlx5_priv *priv = dev->data->dev_private;
216 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
219 unsigned int n = xstats_ctrl->mlx5_stats_n;
220 uint64_t counters[n];
223 stats_n = mlx5_os_get_stats_n(dev);
225 DRV_LOG(ERR, "port %u cannot get stats: %s", dev->data->port_id,
229 if (xstats_ctrl->stats_n != stats_n)
230 mlx5_os_stats_init(dev);
231 ret = mlx5_os_read_dev_counters(dev, counters);
233 DRV_LOG(ERR, "port %u cannot read device counters: %s",
234 dev->data->port_id, strerror(rte_errno));
237 for (i = 0; i != n; ++i) {
238 xstats_ctrl->base[i] = counters[i];
239 xstats_ctrl->hw_stats[i] = 0;
241 mlx5_txpp_xstats_reset(dev);
246 * DPDK callback to retrieve names of extended device statistics
249 * Pointer to Ethernet device structure.
250 * @param[out] xstats_names
251 * Buffer to insert names into.
256 * Number of xstats names.
259 mlx5_xstats_get_names(struct rte_eth_dev *dev,
260 struct rte_eth_xstat_name *xstats_names, unsigned int n)
263 struct mlx5_priv *priv = dev->data->dev_private;
264 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
265 unsigned int mlx5_xstats_n = xstats_ctrl->mlx5_stats_n;
267 if (n >= mlx5_xstats_n && xstats_names) {
268 for (i = 0; i != mlx5_xstats_n; ++i) {
269 strncpy(xstats_names[i].name,
270 xstats_ctrl->info[i].dpdk_name,
271 RTE_ETH_XSTATS_NAME_SIZE);
272 xstats_names[i].name[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;
275 mlx5_xstats_n = mlx5_txpp_xstats_get_names(dev, xstats_names,
277 return mlx5_xstats_n;