[PATCH net-next v20 3/4] hinic3: Add per-queue netdev-genl stats

From: Fan Gong

Date: Sat Dec 05 2026 - 03:40:32 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 | 89 +++++++++++++++++++
1 file changed, 89 insertions(+)

diff --git a/drivers/net/ethernet/huawei/hinic3/hinic3_netdev_ops.c b/drivers/net/ethernet/huawei/hinic3/hinic3_netdev_ops.c
index bc7b733e308b..9925c1337119 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,95 @@ 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 = u64_stats_read(&rxq_stats->bytes);
+ stats_rx->packets = u64_stats_read(&rxq_stats->packets);
+ stats_rx->csum_bad = u64_stats_read(&rxq_stats->csum_errors);
+ stats_rx->alloc_fail = u64_stats_read(&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 = u64_stats_read(&txq_stats->bytes);
+ stats_tx->packets = u64_stats_read(&txq_stats->packets);
+ stats_tx->stop = u64_stats_read(&txq_stats->busy);
+ stats_tx->hw_gso_wire_packets =
+ u64_stats_read(&txq_stats->gso_wire_packets);
+ stats_tx->hw_gso_wire_bytes =
+ u64_stats_read(&txq_stats->gso_wire_bytes);
+ stats_tx->hw_gso_packets =
+ u64_stats_read(&txq_stats->gso_packets);
+ stats_tx->hw_gso_bytes =
+ u64_stats_read(&txq_stats->gso_bytes);
+ stats_tx->wake = u64_stats_read(&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);
+
+ 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.
+ */
+ netdev_stat_queue_sum(netdev,
+ netdev->real_num_rx_queues, nic_dev->max_qps,
+ stats_rx,
+ netdev->real_num_tx_queues, nic_dev->max_qps,
+ stats_tx);
+}
+
+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