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 sizeof(struct ethtool_stats);
148 struct ethtool_stats et_stats[(stats_sz + (
149 sizeof(struct ethtool_stats) - 1)) /
150 sizeof(struct ethtool_stats)];
152 et_stats->cmd = ETHTOOL_GSTATS;
153 et_stats->n_stats = xstats_ctrl->stats_n;
154 ifr.ifr_data = (caddr_t)et_stats;
155 if (priv_ifreq(priv, SIOCETHTOOL, &ifr) != 0) {
156 WARN("unable to read statistic values from device");
159 for (i = 0; i != xstats_n; ++i) {
160 if (priv_is_ib_cntr(mlx5_counters_init[i].ctr_name))
161 priv_get_cntr_sysfs(priv,
162 mlx5_counters_init[i].ctr_name,
165 stats[i] = (uint64_t)
166 et_stats->data[xstats_ctrl->dev_table_idx[i]];
172 * Query the number of statistics provided by ETHTOOL.
175 * Pointer to private structure.
178 * Number of statistics on success, -1 on error.
181 priv_ethtool_get_stats_n(struct priv *priv) {
182 struct ethtool_drvinfo drvinfo;
185 drvinfo.cmd = ETHTOOL_GDRVINFO;
186 ifr.ifr_data = (caddr_t)&drvinfo;
187 if (priv_ifreq(priv, SIOCETHTOOL, &ifr) != 0) {
188 WARN("unable to query number of statistics");
191 return drvinfo.n_stats;
195 * Init the structures to read device counters.
198 * Pointer to private structure.
201 priv_xstats_init(struct priv *priv)
203 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
207 struct ethtool_gstrings *strings = NULL;
208 unsigned int dev_stats_n;
211 dev_stats_n = priv_ethtool_get_stats_n(priv);
212 if (dev_stats_n < 1) {
213 WARN("no extended statistics available");
216 xstats_ctrl->stats_n = dev_stats_n;
217 /* Allocate memory to grab stat names and values. */
218 str_sz = dev_stats_n * ETH_GSTRING_LEN;
219 strings = (struct ethtool_gstrings *)
220 rte_malloc("xstats_strings",
221 str_sz + sizeof(struct ethtool_gstrings), 0);
223 WARN("unable to allocate memory for xstats");
226 strings->cmd = ETHTOOL_GSTRINGS;
227 strings->string_set = ETH_SS_STATS;
228 strings->len = dev_stats_n;
229 ifr.ifr_data = (caddr_t)strings;
230 if (priv_ifreq(priv, SIOCETHTOOL, &ifr) != 0) {
231 WARN("unable to get statistic names");
234 for (j = 0; j != xstats_n; ++j)
235 xstats_ctrl->dev_table_idx[j] = dev_stats_n;
236 for (i = 0; i != dev_stats_n; ++i) {
237 const char *curr_string = (const char *)
238 &strings->data[i * ETH_GSTRING_LEN];
240 for (j = 0; j != xstats_n; ++j) {
241 if (!strcmp(mlx5_counters_init[j].ctr_name,
243 xstats_ctrl->dev_table_idx[j] = i;
248 for (j = 0; j != xstats_n; ++j) {
249 if (priv_is_ib_cntr(mlx5_counters_init[j].ctr_name))
251 if (xstats_ctrl->dev_table_idx[j] >= dev_stats_n) {
252 WARN("counter \"%s\" is not recognized",
253 mlx5_counters_init[j].dpdk_name);
257 /* Copy to base at first time. */
258 assert(xstats_n <= MLX5_MAX_XSTATS);
259 priv_read_dev_counters(priv, xstats_ctrl->base);
265 * Get device extended statistics.
268 * Pointer to private structure.
270 * Pointer to rte extended stats table.
273 * Number of extended stats on success and stats is filled,
277 priv_xstats_get(struct priv *priv, struct rte_eth_xstat *stats)
279 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
281 unsigned int n = xstats_n;
282 uint64_t counters[n];
284 if (priv_read_dev_counters(priv, counters) < 0)
286 for (i = 0; i != xstats_n; ++i) {
288 stats[i].value = (counters[i] - xstats_ctrl->base[i]);
294 * Reset device extended statistics.
297 * Pointer to private structure.
300 priv_xstats_reset(struct priv *priv)
302 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
304 unsigned int n = xstats_n;
305 uint64_t counters[n];
307 if (priv_read_dev_counters(priv, counters) < 0)
309 for (i = 0; i != n; ++i)
310 xstats_ctrl->base[i] = counters[i];
314 * DPDK callback to get device statistics.
317 * Pointer to Ethernet device structure.
319 * Stats structure output buffer.
322 mlx5_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
324 struct priv *priv = mlx5_get_priv(dev);
325 struct rte_eth_stats tmp = {0};
330 /* Add software counters. */
331 for (i = 0; (i != priv->rxqs_n); ++i) {
332 struct mlx5_rxq_data *rxq = (*priv->rxqs)[i];
336 idx = rxq->stats.idx;
337 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
338 #ifdef MLX5_PMD_SOFT_COUNTERS
339 tmp.q_ipackets[idx] += rxq->stats.ipackets;
340 tmp.q_ibytes[idx] += rxq->stats.ibytes;
342 tmp.q_errors[idx] += (rxq->stats.idropped +
343 rxq->stats.rx_nombuf);
345 #ifdef MLX5_PMD_SOFT_COUNTERS
346 tmp.ipackets += rxq->stats.ipackets;
347 tmp.ibytes += rxq->stats.ibytes;
349 tmp.ierrors += rxq->stats.idropped;
350 tmp.rx_nombuf += rxq->stats.rx_nombuf;
352 for (i = 0; (i != priv->txqs_n); ++i) {
353 struct mlx5_txq_data *txq = (*priv->txqs)[i];
357 idx = txq->stats.idx;
358 if (idx < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
359 #ifdef MLX5_PMD_SOFT_COUNTERS
360 tmp.q_opackets[idx] += txq->stats.opackets;
361 tmp.q_obytes[idx] += txq->stats.obytes;
363 tmp.q_errors[idx] += txq->stats.oerrors;
365 #ifdef MLX5_PMD_SOFT_COUNTERS
366 tmp.opackets += txq->stats.opackets;
367 tmp.obytes += txq->stats.obytes;
369 tmp.oerrors += txq->stats.oerrors;
371 #ifndef MLX5_PMD_SOFT_COUNTERS
372 /* FIXME: retrieve and add hardware counters. */
380 * DPDK callback to clear device statistics.
383 * Pointer to Ethernet device structure.
386 mlx5_stats_reset(struct rte_eth_dev *dev)
388 struct priv *priv = dev->data->dev_private;
393 for (i = 0; (i != priv->rxqs_n); ++i) {
394 if ((*priv->rxqs)[i] == NULL)
396 idx = (*priv->rxqs)[i]->stats.idx;
397 (*priv->rxqs)[i]->stats =
398 (struct mlx5_rxq_stats){ .idx = idx };
400 for (i = 0; (i != priv->txqs_n); ++i) {
401 if ((*priv->txqs)[i] == NULL)
403 idx = (*priv->txqs)[i]->stats.idx;
404 (*priv->txqs)[i]->stats =
405 (struct mlx5_txq_stats){ .idx = idx };
407 #ifndef MLX5_PMD_SOFT_COUNTERS
408 /* FIXME: reset hardware counters. */
414 * DPDK callback to get extended device statistics.
417 * Pointer to Ethernet device structure.
419 * Stats table output buffer.
421 * The size of the stats table.
424 * Number of xstats on success, negative on failure.
427 mlx5_xstats_get(struct rte_eth_dev *dev,
428 struct rte_eth_xstat *stats, unsigned int n)
430 struct priv *priv = mlx5_get_priv(dev);
433 if (n >= xstats_n && stats) {
434 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
438 stats_n = priv_ethtool_get_stats_n(priv);
443 if (xstats_ctrl->stats_n != stats_n)
444 priv_xstats_init(priv);
445 ret = priv_xstats_get(priv, stats);
452 * DPDK callback to clear device extended statistics.
455 * Pointer to Ethernet device structure.
458 mlx5_xstats_reset(struct rte_eth_dev *dev)
460 struct priv *priv = mlx5_get_priv(dev);
461 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
465 stats_n = priv_ethtool_get_stats_n(priv);
468 if (xstats_ctrl->stats_n != stats_n)
469 priv_xstats_init(priv);
470 priv_xstats_reset(priv);
476 * DPDK callback to retrieve names of extended device statistics
479 * Pointer to Ethernet device structure.
480 * @param[out] xstats_names
481 * Buffer to insert names into.
486 * Number of xstats names.
489 mlx5_xstats_get_names(struct rte_eth_dev *dev,
490 struct rte_eth_xstat_name *xstats_names, unsigned int n)
492 struct priv *priv = mlx5_get_priv(dev);
495 if (n >= xstats_n && xstats_names) {
497 for (i = 0; i != xstats_n; ++i) {
498 strncpy(xstats_names[i].name,
499 mlx5_counters_init[i].dpdk_name,
500 RTE_ETH_XSTATS_NAME_SIZE);
501 xstats_names[i].name[RTE_ETH_XSTATS_NAME_SIZE - 1] = 0;