1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
7 #include <linux/sockios.h>
8 #include <linux/ethtool.h>
12 #include <rte_ethdev_driver.h>
13 #include <rte_common.h>
14 #include <rte_malloc.h>
17 #include "mlx5_rxtx.h"
18 #include "mlx5_defs.h"
20 struct mlx5_counter_ctrl {
21 /* Name of the counter. */
22 char dpdk_name[RTE_ETH_XSTATS_NAME_SIZE];
23 /* Name of the counter on the device table. */
24 char ctr_name[RTE_ETH_XSTATS_NAME_SIZE];
25 uint32_t ib:1; /**< Nonzero for IB counters. */
28 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
30 .dpdk_name = "rx_port_unicast_bytes",
31 .ctr_name = "rx_vport_unicast_bytes",
34 .dpdk_name = "rx_port_multicast_bytes",
35 .ctr_name = "rx_vport_multicast_bytes",
38 .dpdk_name = "rx_port_broadcast_bytes",
39 .ctr_name = "rx_vport_broadcast_bytes",
42 .dpdk_name = "rx_port_unicast_packets",
43 .ctr_name = "rx_vport_unicast_packets",
46 .dpdk_name = "rx_port_multicast_packets",
47 .ctr_name = "rx_vport_multicast_packets",
50 .dpdk_name = "rx_port_broadcast_packets",
51 .ctr_name = "rx_vport_broadcast_packets",
54 .dpdk_name = "tx_port_unicast_bytes",
55 .ctr_name = "tx_vport_unicast_bytes",
58 .dpdk_name = "tx_port_multicast_bytes",
59 .ctr_name = "tx_vport_multicast_bytes",
62 .dpdk_name = "tx_port_broadcast_bytes",
63 .ctr_name = "tx_vport_broadcast_bytes",
66 .dpdk_name = "tx_port_unicast_packets",
67 .ctr_name = "tx_vport_unicast_packets",
70 .dpdk_name = "tx_port_multicast_packets",
71 .ctr_name = "tx_vport_multicast_packets",
74 .dpdk_name = "tx_port_broadcast_packets",
75 .ctr_name = "tx_vport_broadcast_packets",
78 .dpdk_name = "rx_wqe_err",
79 .ctr_name = "rx_wqe_err",
82 .dpdk_name = "rx_crc_errors_phy",
83 .ctr_name = "rx_crc_errors_phy",
86 .dpdk_name = "rx_in_range_len_errors_phy",
87 .ctr_name = "rx_in_range_len_errors_phy",
90 .dpdk_name = "rx_symbol_err_phy",
91 .ctr_name = "rx_symbol_err_phy",
94 .dpdk_name = "tx_errors_phy",
95 .ctr_name = "tx_errors_phy",
98 .dpdk_name = "rx_out_of_buffer",
99 .ctr_name = "out_of_buffer",
103 .dpdk_name = "tx_packets_phy",
104 .ctr_name = "tx_packets_phy",
107 .dpdk_name = "rx_packets_phy",
108 .ctr_name = "rx_packets_phy",
111 .dpdk_name = "tx_bytes_phy",
112 .ctr_name = "tx_bytes_phy",
115 .dpdk_name = "rx_bytes_phy",
116 .ctr_name = "rx_bytes_phy",
120 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
123 * Read device counters table.
126 * Pointer to Ethernet device.
128 * Counters table output buffer.
131 * 0 on success and stats is filled, negative errno value otherwise and
135 mlx5_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
137 struct priv *priv = dev->data->dev_private;
138 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
141 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
142 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
143 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
146 et_stats->cmd = ETHTOOL_GSTATS;
147 et_stats->n_stats = xstats_ctrl->stats_n;
148 ifr.ifr_data = (caddr_t)et_stats;
149 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 0);
152 "port %u unable to read statistic values from device",
156 for (i = 0; i != xstats_n; ++i) {
157 if (mlx5_counters_init[i].ib) {
159 MKSTR(path, "%s/ports/1/hw_counters/%s",
161 mlx5_counters_init[i].ctr_name);
163 file = fopen(path, "rb");
165 int n = fscanf(file, "%" SCNu64, &stats[i]);
172 stats[i] = (uint64_t)
173 et_stats->data[xstats_ctrl->dev_table_idx[i]];
180 * Query the number of statistics provided by ETHTOOL.
183 * Pointer to Ethernet device.
186 * Number of statistics on success, negative errno value otherwise and
190 mlx5_ethtool_get_stats_n(struct rte_eth_dev *dev) {
191 struct ethtool_drvinfo drvinfo;
195 drvinfo.cmd = ETHTOOL_GDRVINFO;
196 ifr.ifr_data = (caddr_t)&drvinfo;
197 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 0);
199 DRV_LOG(WARNING, "port %u unable to query number of statistics",
203 return drvinfo.n_stats;
207 * Init the structures to read device counters.
210 * Pointer to Ethernet device.
213 mlx5_xstats_init(struct rte_eth_dev *dev)
215 struct priv *priv = dev->data->dev_private;
216 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
220 struct ethtool_gstrings *strings = NULL;
221 unsigned int dev_stats_n;
225 ret = mlx5_ethtool_get_stats_n(dev);
227 DRV_LOG(WARNING, "port %u no extended statistics available",
232 /* Allocate memory to grab stat names and values. */
233 str_sz = dev_stats_n * ETH_GSTRING_LEN;
234 strings = (struct ethtool_gstrings *)
235 rte_malloc("xstats_strings",
236 str_sz + sizeof(struct ethtool_gstrings), 0);
238 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
242 strings->cmd = ETHTOOL_GSTRINGS;
243 strings->string_set = ETH_SS_STATS;
244 strings->len = dev_stats_n;
245 ifr.ifr_data = (caddr_t)strings;
246 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr, 0);
248 DRV_LOG(WARNING, "port %u unable to get statistic names",
252 for (j = 0; j != xstats_n; ++j)
253 xstats_ctrl->dev_table_idx[j] = dev_stats_n;
254 for (i = 0; i != dev_stats_n; ++i) {
255 const char *curr_string = (const char *)
256 &strings->data[i * ETH_GSTRING_LEN];
258 for (j = 0; j != xstats_n; ++j) {
259 if (!strcmp(mlx5_counters_init[j].ctr_name,
261 xstats_ctrl->dev_table_idx[j] = i;
266 for (j = 0; j != xstats_n; ++j) {
267 if (mlx5_counters_init[j].ib)
269 if (xstats_ctrl->dev_table_idx[j] >= dev_stats_n) {
271 "port %u counter \"%s\" is not recognized",
273 mlx5_counters_init[j].dpdk_name);
277 xstats_ctrl->stats_n = dev_stats_n;
278 /* Copy to base at first time. */
279 assert(xstats_n <= MLX5_MAX_XSTATS);
280 ret = mlx5_read_dev_counters(dev, xstats_ctrl->base);
282 DRV_LOG(ERR, "port %u cannot read device counters: %s",
283 dev->data->port_id, strerror(rte_errno));
289 * DPDK callback to get extended device statistics.
292 * Pointer to Ethernet device.
294 * Pointer to rte extended stats table.
296 * The size of the stats table.
299 * Number of extended stats on success and stats is filled,
300 * negative on error and rte_errno is set.
303 mlx5_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
306 struct priv *priv = dev->data->dev_private;
308 uint64_t counters[n];
310 if (!priv->xstats_ctrl.stats_n)
312 if (n >= xstats_n && stats) {
313 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
317 stats_n = mlx5_ethtool_get_stats_n(dev);
320 if (xstats_ctrl->stats_n != stats_n)
321 mlx5_xstats_init(dev);
322 ret = mlx5_read_dev_counters(dev, counters);
325 for (i = 0; i != xstats_n; ++i) {
327 stats[i].value = (counters[i] - xstats_ctrl->base[i]);
334 * DPDK callback to get device statistics.
337 * Pointer to Ethernet device structure.
339 * Stats structure output buffer.
342 * 0 on success and stats is filled, negative errno value otherwise and
346 mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
348 struct priv *priv = dev->data->dev_private;
349 struct rte_eth_stats tmp = {0};
353 /* Add software counters. */
354 for (i = 0; (i != priv->rxqs_n); ++i) {
355 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
359 idx = rxq->stats.idx;
360 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
361 #ifdef MLX5_PMD_SOFT_COUNTERS
362 tmp.q_ipackets[idx] += rxq->stats.ipackets;
363 tmp.q_ibytes[idx] += rxq->stats.ibytes;
365 tmp.q_errors[idx] += (rxq->stats.idropped +
366 rxq->stats.rx_nombuf);
368 #ifdef MLX5_PMD_SOFT_COUNTERS
369 tmp.ipackets += rxq->stats.ipackets;
370 tmp.ibytes += rxq->stats.ibytes;
372 tmp.ierrors += rxq->stats.idropped;
373 tmp.rx_nombuf += rxq->stats.rx_nombuf;
375 for (i = 0; (i != priv->txqs_n); ++i) {
376 struct mlx5_txq_data *txq = (*priv->txqs)[i];
380 idx = txq->stats.idx;
381 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
382 #ifdef MLX5_PMD_SOFT_COUNTERS
383 tmp.q_opackets[idx] += txq->stats.opackets;
384 tmp.q_obytes[idx] += txq->stats.obytes;
386 tmp.q_errors[idx] += txq->stats.oerrors;
388 #ifdef MLX5_PMD_SOFT_COUNTERS
389 tmp.opackets += txq->stats.opackets;
390 tmp.obytes += txq->stats.obytes;
392 tmp.oerrors += txq->stats.oerrors;
394 #ifndef MLX5_PMD_SOFT_COUNTERS
395 /* FIXME: retrieve and add hardware counters. */
402 * DPDK callback to clear device statistics.
405 * Pointer to Ethernet device structure.
408 mlx5_stats_reset(struct rte_eth_dev *dev)
410 struct priv *priv = dev->data->dev_private;
414 for (i = 0; (i != priv->rxqs_n); ++i) {
415 if ((*priv->rxqs)[i] == NULL)
417 idx = (*priv->rxqs)[i]->stats.idx;
418 (*priv->rxqs)[i]->stats =
419 (struct mlx5_rxq_stats){ .idx = idx };
421 for (i = 0; (i != priv->txqs_n); ++i) {
422 if ((*priv->txqs)[i] == NULL)
424 idx = (*priv->txqs)[i]->stats.idx;
425 (*priv->txqs)[i]->stats =
426 (struct mlx5_txq_stats){ .idx = idx };
428 #ifndef MLX5_PMD_SOFT_COUNTERS
429 /* FIXME: reset hardware counters. */
434 * DPDK callback to clear device extended statistics.
437 * Pointer to Ethernet device structure.
440 mlx5_xstats_reset(struct rte_eth_dev *dev)
442 struct priv *priv = dev->data->dev_private;
443 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
446 unsigned int n = xstats_n;
447 uint64_t counters[n];
450 stats_n = mlx5_ethtool_get_stats_n(dev);
452 DRV_LOG(ERR, "port %u cannot get stats: %s", dev->data->port_id,
456 if (xstats_ctrl->stats_n != stats_n)
457 mlx5_xstats_init(dev);
458 ret = mlx5_read_dev_counters(dev, counters);
460 DRV_LOG(ERR, "port %u cannot read device counters: %s",
461 dev->data->port_id, strerror(rte_errno));
464 for (i = 0; i != n; ++i)
465 xstats_ctrl->base[i] = counters[i];
469 * DPDK callback to retrieve names of extended device statistics
472 * Pointer to Ethernet device structure.
473 * @param[out] xstats_names
474 * Buffer to insert names into.
479 * Number of xstats names.
482 mlx5_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
483 struct rte_eth_xstat_name *xstats_names, unsigned int n)
485 struct priv *priv = dev->data->dev_private;
488 if (!priv->xstats_ctrl.stats_n)
490 if (n >= xstats_n && xstats_names) {
491 for (i = 0; i != xstats_n; ++i) {
492 strncpy(xstats_names[i].name,
493 mlx5_counters_init[i].dpdk_name,
494 RTE_ETH_XSTATS_NAME_SIZE);
495 xstats_names[i].name[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;