1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
11 #include <ethdev_driver.h>
12 #include <rte_common.h>
13 #include <rte_malloc.h>
15 #include <mlx5_common.h>
17 #include "mlx5_defs.h"
21 #include "mlx5_malloc.h"
24 * DPDK callback to get extended device statistics.
27 * Pointer to Ethernet device.
29 * Pointer to rte extended stats table.
31 * The size of the stats table.
34 * Number of extended stats on success and stats is filled,
35 * negative on error and rte_errno is set.
38 mlx5_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
41 struct mlx5_priv *priv = dev->data->dev_private;
44 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
45 uint16_t mlx5_stats_n = xstats_ctrl->mlx5_stats_n;
47 if (n >= mlx5_stats_n && stats) {
51 stats_n = mlx5_os_get_stats_n(dev);
54 if (xstats_ctrl->stats_n != stats_n)
55 mlx5_os_stats_init(dev);
56 ret = mlx5_os_read_dev_counters(dev, counters);
59 for (i = 0; i != mlx5_stats_n; ++i) {
61 if (xstats_ctrl->info[i].dev) {
63 uint64_t hw_stat = xstats_ctrl->hw_stats[i];
65 stats[i].value = (counters[i] -
66 xstats_ctrl->base[i]) &
68 wrap_n = hw_stat >> 32;
70 (hw_stat & (uint64_t)UINT32_MAX))
72 stats[i].value |= (wrap_n) << 32;
73 xstats_ctrl->hw_stats[i] = stats[i].value;
76 (counters[i] - xstats_ctrl->base[i]);
80 mlx5_stats_n = mlx5_txpp_xstats_get(dev, stats, n, mlx5_stats_n);
85 * DPDK callback to get device statistics.
88 * Pointer to Ethernet device structure.
90 * Stats structure output buffer.
93 * 0 on success and stats is filled, negative errno value otherwise and
97 mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
99 struct mlx5_priv *priv = dev->data->dev_private;
100 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
101 struct rte_eth_stats tmp;
107 memset(&tmp, 0, sizeof(tmp));
108 /* Add software counters. */
109 for (i = 0; (i != priv->rxqs_n); ++i) {
110 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
115 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
116 #ifdef MLX5_PMD_SOFT_COUNTERS
117 tmp.q_ipackets[idx] += rxq->stats.ipackets;
118 tmp.q_ibytes[idx] += rxq->stats.ibytes;
120 tmp.q_errors[idx] += (rxq->stats.idropped +
121 rxq->stats.rx_nombuf);
123 #ifdef MLX5_PMD_SOFT_COUNTERS
124 tmp.ipackets += rxq->stats.ipackets;
125 tmp.ibytes += rxq->stats.ibytes;
127 tmp.ierrors += rxq->stats.idropped;
128 tmp.rx_nombuf += rxq->stats.rx_nombuf;
130 for (i = 0; (i != priv->txqs_n); ++i) {
131 struct mlx5_txq_data *txq = (*priv->txqs)[i];
136 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
137 #ifdef MLX5_PMD_SOFT_COUNTERS
138 tmp.q_opackets[idx] += txq->stats.opackets;
139 tmp.q_obytes[idx] += txq->stats.obytes;
142 #ifdef MLX5_PMD_SOFT_COUNTERS
143 tmp.opackets += txq->stats.opackets;
144 tmp.obytes += txq->stats.obytes;
146 tmp.oerrors += txq->stats.oerrors;
148 ret = mlx5_os_read_dev_stat(priv, "out_of_buffer", &tmp.imissed);
150 tmp.imissed = (tmp.imissed - stats_ctrl->imissed_base) &
151 (uint64_t)UINT32_MAX;
152 wrap_n = stats_ctrl->imissed >> 32;
153 if (tmp.imissed < (stats_ctrl->imissed & (uint64_t)UINT32_MAX))
155 tmp.imissed |= (wrap_n) << 32;
156 stats_ctrl->imissed = tmp.imissed;
158 tmp.imissed = stats_ctrl->imissed;
160 #ifndef MLX5_PMD_SOFT_COUNTERS
161 /* FIXME: retrieve and add hardware counters. */
168 * DPDK callback to clear device statistics.
171 * Pointer to Ethernet device structure.
174 * always 0 on success and stats is reset
177 mlx5_stats_reset(struct rte_eth_dev *dev)
179 struct mlx5_priv *priv = dev->data->dev_private;
180 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
183 for (i = 0; (i != priv->rxqs_n); ++i) {
184 if ((*priv->rxqs)[i] == NULL)
186 memset(&(*priv->rxqs)[i]->stats, 0,
187 sizeof(struct mlx5_rxq_stats));
189 for (i = 0; (i != priv->txqs_n); ++i) {
190 if ((*priv->txqs)[i] == NULL)
192 memset(&(*priv->txqs)[i]->stats, 0,
193 sizeof(struct mlx5_txq_stats));
195 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
196 stats_ctrl->imissed = 0;
197 #ifndef MLX5_PMD_SOFT_COUNTERS
198 /* FIXME: reset hardware counters. */
205 * DPDK callback to clear device extended statistics.
208 * Pointer to Ethernet device structure.
211 * 0 on success and stats is reset, negative errno value otherwise and
215 mlx5_xstats_reset(struct rte_eth_dev *dev)
217 struct mlx5_priv *priv = dev->data->dev_private;
218 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
224 stats_n = mlx5_os_get_stats_n(dev);
226 DRV_LOG(ERR, "port %u cannot get stats: %s", dev->data->port_id,
230 if (xstats_ctrl->stats_n != stats_n)
231 mlx5_os_stats_init(dev);
232 counters = mlx5_malloc(MLX5_MEM_SYS, sizeof(*counters) *
233 xstats_ctrl->mlx5_stats_n, 0,
236 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats "
242 ret = mlx5_os_read_dev_counters(dev, counters);
244 DRV_LOG(ERR, "port %u cannot read device counters: %s",
245 dev->data->port_id, strerror(rte_errno));
249 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
250 xstats_ctrl->base[i] = counters[i];
251 xstats_ctrl->hw_stats[i] = 0;
253 mlx5_txpp_xstats_reset(dev);
259 * DPDK callback to retrieve names of extended device statistics
262 * Pointer to Ethernet device structure.
263 * @param[out] xstats_names
264 * Buffer to insert names into.
269 * Number of xstats names.
272 mlx5_xstats_get_names(struct rte_eth_dev *dev,
273 struct rte_eth_xstat_name *xstats_names, unsigned int n)
276 struct mlx5_priv *priv = dev->data->dev_private;
277 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
278 unsigned int mlx5_xstats_n = xstats_ctrl->mlx5_stats_n;
280 if (n >= mlx5_xstats_n && xstats_names) {
281 for (i = 0; i != mlx5_xstats_n; ++i) {
282 strncpy(xstats_names[i].name,
283 xstats_ctrl->info[i].dpdk_name,
284 RTE_ETH_XSTATS_NAME_SIZE);
285 xstats_names[i].name[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;
288 mlx5_xstats_n = mlx5_txpp_xstats_get_names(dev, xstats_names,
290 return mlx5_xstats_n;