[PATCH net-next v19 3/4] hinic3: Add per-queue netdev-genl stats
From: Fan Gong
Date: Thu Dec 03 2026 - 23:09:24 EST
This commit adds netdev_stat_ops to the hinic3 driver. It implements
get_queue_stats_rx, get_queue_stats_tx and get_base_stats, so per-queue
and device-level counters are exported through the netdev-genl qstats
interface. Map busy to tx-stop, csum_errors to rx-csum-bad, alloc_fail
to rx-alloc-fail.
get_base_stats() sums the queues between netdev->real_num_{rx,tx}_queues
and nic_dev->max_qps, matching the range the core does not sum itself.
./cli.py --spec netlink/specs/netdev.yaml\
--dump qstats-get --json '{"ifindex": 30}'
[{'ifindex': 30,
'rx-alloc-fail': 0,
'rx-bytes': 13194720024,
'rx-csum-bad': 0,
'rx-packets': 10874477,
'tx-bytes': 150086739354,
'tx-hw-gso-bytes': 143674451028,
'tx-hw-gso-packets': 2261382,
'tx-hw-gso-wire-bytes': 150067172880,
'tx-hw-gso-wire-packets': 99120804,
'tx-packets': 99315841,
'tx-stop': 0,
'tx-wake': 0}]
Co-developed-by: Wu Di <wudi234@xxxxxxxxxx>
Signed-off-by: Wu Di <wudi234@xxxxxxxxxx>
Co-developed-by: Teng Peisen <tengpeisen@xxxxxxxxxx>
Signed-off-by: Teng Peisen <tengpeisen@xxxxxxxxxx>
Co-developed-by: Mingfa Kang <kangmingfa1@xxxxxxxxxx>
Signed-off-by: Mingfa Kang <kangmingfa1@xxxxxxxxxx>
Signed-off-by: Fan Gong <gongfan1@xxxxxxxxxx>
---
.../huawei/hinic3/hinic3_netdev_ops.c | 106 ++++++++++++++++++
1 file changed, 106 insertions(+)
diff --git a/drivers/net/ethernet/huawei/hinic3/hinic3_netdev_ops.c b/drivers/net/ethernet/huawei/hinic3/hinic3_netdev_ops.c
index 621d8b12e247..63eb4189e319 100644
--- a/drivers/net/ethernet/huawei/hinic3/hinic3_netdev_ops.c
+++ b/drivers/net/ethernet/huawei/hinic3/hinic3_netdev_ops.c
@@ -5,6 +5,7 @@
#include <linux/if_vlan.h>
#include <linux/netdevice.h>
#include <net/vxlan.h>
+#include <net/netdev_queues.h>
#include "hinic3_hwif.h"
#include "hinic3_nic_cfg.h"
@@ -857,7 +858,112 @@ static const struct net_device_ops hinic3_netdev_ops = {
.ndo_start_xmit = hinic3_xmit_frame,
};
+static void hinic3_get_queue_stats_rx(struct net_device *netdev, int i,
+ struct netdev_queue_stats_rx *stats_rx)
+{
+ struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
+ struct hinic3_rxq_stats *rxq_stats;
+ struct hinic3_rxq *rxq;
+ unsigned int start;
+
+ if (unlikely(i < 0 || i >= nic_dev->max_qps))
+ return;
+
+ rxq = &nic_dev->rxqs[i];
+ rxq_stats = &rxq->rxq_stats;
+ do {
+ start = u64_stats_fetch_begin(&rxq_stats->syncp);
+ stats_rx->bytes = rxq_stats->bytes;
+ stats_rx->packets = rxq_stats->packets;
+ stats_rx->csum_bad = rxq_stats->csum_errors;
+ stats_rx->alloc_fail = rxq_stats->alloc_fail;
+ } while (u64_stats_fetch_retry(&rxq_stats->syncp, start));
+}
+
+static void hinic3_get_queue_stats_tx(struct net_device *netdev, int i,
+ struct netdev_queue_stats_tx *stats_tx)
+{
+ struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
+ struct hinic3_txq_stats *txq_stats;
+ struct hinic3_txq *txq;
+ unsigned int start;
+
+ if (unlikely(i < 0 || i >= nic_dev->max_qps))
+ return;
+
+ txq = &nic_dev->txqs[i];
+ txq_stats = &txq->txq_stats;
+ do {
+ start = u64_stats_fetch_begin(&txq_stats->syncp);
+ stats_tx->bytes = txq_stats->bytes;
+ stats_tx->packets = txq_stats->packets;
+ stats_tx->stop = txq_stats->busy;
+ stats_tx->hw_gso_wire_packets = txq_stats->gso_wire_packets;
+ stats_tx->hw_gso_wire_bytes = txq_stats->gso_wire_bytes;
+ stats_tx->hw_gso_packets = txq_stats->gso_packets;
+ stats_tx->hw_gso_bytes = txq_stats->gso_bytes;
+ stats_tx->wake = txq_stats->wake;
+ } while (u64_stats_fetch_retry(&txq_stats->syncp, start));
+}
+
+static void hinic3_get_base_stats(struct net_device *netdev,
+ struct netdev_queue_stats_rx *stats_rx,
+ struct netdev_queue_stats_tx *stats_tx)
+{
+ struct hinic3_nic_dev *nic_dev = netdev_priv(netdev);
+ int i;
+
+ stats_rx->bytes = 0;
+ stats_rx->packets = 0;
+ stats_rx->csum_bad = 0;
+ stats_rx->alloc_fail = 0;
+ stats_tx->bytes = 0;
+ stats_tx->packets = 0;
+ stats_tx->stop = 0;
+ stats_tx->hw_gso_wire_packets = 0;
+ stats_tx->hw_gso_wire_bytes = 0;
+ stats_tx->hw_gso_packets = 0;
+ stats_tx->hw_gso_bytes = 0;
+ stats_tx->wake = 0;
+
+ /* Match the core: the base range starts at the active queue
+ * count, not at q_params.num_qps, which can lag behind.
+ */
+ for (i = netdev->real_num_rx_queues; i < nic_dev->max_qps; i++) {
+ struct netdev_queue_stats_rx rx = {0};
+
+ hinic3_get_queue_stats_rx(netdev, i, &rx);
+
+ stats_rx->bytes += rx.bytes;
+ stats_rx->packets += rx.packets;
+ stats_rx->csum_bad += rx.csum_bad;
+ stats_rx->alloc_fail += rx.alloc_fail;
+ }
+
+ for (i = netdev->real_num_tx_queues; i < nic_dev->max_qps; i++) {
+ struct netdev_queue_stats_tx tx = {0};
+
+ hinic3_get_queue_stats_tx(netdev, i, &tx);
+
+ stats_tx->bytes += tx.bytes;
+ stats_tx->packets += tx.packets;
+ stats_tx->stop += tx.stop;
+ stats_tx->hw_gso_wire_packets += tx.hw_gso_wire_packets;
+ stats_tx->hw_gso_wire_bytes += tx.hw_gso_wire_bytes;
+ stats_tx->hw_gso_packets += tx.hw_gso_packets;
+ stats_tx->hw_gso_bytes += tx.hw_gso_bytes;
+ stats_tx->wake += tx.wake;
+ }
+}
+
+static const struct netdev_stat_ops hinic3_stat_ops = {
+ .get_queue_stats_rx = hinic3_get_queue_stats_rx,
+ .get_queue_stats_tx = hinic3_get_queue_stats_tx,
+ .get_base_stats = hinic3_get_base_stats,
+};
+
void hinic3_set_netdev_ops(struct net_device *netdev)
{
netdev->netdev_ops = &hinic3_netdev_ops;
+ netdev->stat_ops = &hinic3_stat_ops;
}
--
2.43.0