Skip to contentSkip to conversation

net: dsa: qca8k: expose cached per-port MIB counters via get_stats64

PATCH In review Salah Ahmed 2 participants 2 messages Oct 30

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: dsa: qca8k: expose cached per-port MIB counters via get_stats64

DSA user port netdev statistics count only CPU-seen traffic, so bridge offloaded forwarding is invisible to /sys/class/net/<port>/statistics, to rtnetlink IFLA_STATS and to every kernel consumer of them, including the netdev LED trigger. The switch MIB counters see every frame crossing the port, but reading them costs a register transaction per counter on a bus that is either MDIO or MDIO-over-Ethernet, while consumers such as the LED trigger call dev_get_stats() several times per second.

Use the pattern that is already established for switch drivers: ar9331 in this same directory caches a rtnl_link_stats64 behind a spinlock, refreshes it from a delayed work and serves it from .get_stats64, and ksz does the same. Poll RxGoodByte/TxByte per user port and serve the cached values.

The poll is adaptive rather than fixed. A consumer that derives a rate from these counters only sees the wire when the cache moves, so a fixed period sets the latency of every rate-based consumer to that period. Refresh four times a second while the counters move and once a second while they are idle: an idle switch stays cheap, and a busy one is reported within a quarter of a second. The work also runs once as soon as it is armed, so the first consumer after a link comes up does not read zeros for a whole period.

The work is scheduled with schedule_delayed_work(), i.e. on system_wq like ar9331 does, and not on system_power_efficient_wq. With CONFIG_WQ_POWER_EFFICIENT_DEFAULT the power-efficient workqueue arms deferrable timers, which do not wake an idle CPU; on a single-core switch SoC that is mostly idle this deferred a nominal 3 s poll by several more seconds, and consumers saw the wire that late.

DSA serves a user port either from the driver's .get_stats64 or from dev_get_tstats64(), never from both, so the cache has to be layered on top of the software counters: start from dev_get_tstats64() and override only the byte counters with the wire values. Otherwise installing .get_stats64 would drop rx_packets/tx_packets to zero and break every consumer that looks at packets, including the netdev LED trigger in tx/rx mode. The MIB counts every frame crossing the port including the CPU-bound ones, so the wire byte counters are a superset of the software ones and replacing them loses nothing.

Each 64-bit counter is read with a single regmap_bulk_read() so that no other register access can land between the low and the high half of the same counter.

The cache fields sit at the top of struct qca8k_priv to stay clear of target-specific patches that extend its tail, such as the ipq40xx IPQ4019 built-in switch support. The poll work is armed at the end of qca8k_setup(): a failing setup never reaches the remove callback, so arming it earlier would leave the delayed work running after the driver is gone.

The poll work is armed at the end of qca8k_setup() and stopped from a new .teardown op. DSA calls .teardown both on the dsa_switch_setup() error paths that follow a successful ops->setup() - the tag protocol setup and the user MDIO bus allocation and registration - and from dsa_unregister_switch(), so the work cannot outlive the driver data it reads. Stopping it in qca8k_sw_remove() alone misses the probe error
path: dsa_register_switch() fails, probe fails, devm frees priv and the armed work then runs on freed memory.

---
 drivers/net/dsa/qca/qca8k-8xxx.c   |  17 +++++
 drivers/net/dsa/qca/qca8k-common.c | 110 +++++++++++++++++++++++++++++
 drivers/net/dsa/qca/qca8k.h        |  15 ++++
 3 files changed, 142 insertions(+)
drivers/net/dsa/qca/qca8k-8xxx.c +17 -0
index 07227ae8bc..08f3ef00e6 100644
@@ -2020,6 +2020,14 @@ qca8k_setup(struct dsa_switch *ds)
 	/* Set max number of LAGs supported */
 	ds->num_lag_ids = QCA8K_NUM_LAGS;
 
+	/* Arm the MIB poll work only after every failure path in this
+	 * function is behind us. DSA calls .teardown both on the
+	 * dsa_switch_setup() error paths that follow a successful
+	 * ops->setup() and from dsa_unregister_switch(), so the work is
+	 * always stopped again.
+	 */
+	qca8k_mib_poll_start(priv);
+
 	return 0;
 }
 
@@ -2030,11 +2038,20 @@ static const struct phylink_mac_ops qca8k_phylink_mac_ops = {
 	.mac_link_up	= qca8k_phylink_mac_link_up,
 };
 
+static void qca8k_teardown(struct dsa_switch *ds)
+{
+	struct qca8k_priv *priv = ds->priv;
+
+	qca8k_mib_poll_stop(priv);
+}
+
 static const struct dsa_switch_ops qca8k_switch_ops = {
 	.get_tag_protocol	= qca8k_get_tag_protocol,
 	.setup			= qca8k_setup,
+	.teardown		= qca8k_teardown,
 	.get_strings		= qca8k_get_strings,
 	.get_ethtool_stats	= qca8k_get_ethtool_stats,
+	.get_stats64		= qca8k_get_stats64,
 	.get_sset_count		= qca8k_get_sset_count,
 	.set_ageing_time	= qca8k_set_ageing_time,
 	.support_eee		= dsa_supports_eee,
drivers/net/dsa/qca/qca8k-common.c +110 -0

116 lines not on this page · raw message

drivers/net/dsa/qca/qca8k.h +15 -0
index 9563388930..edab7e1218 100644
@@ -443,6 +443,15 @@ struct qca8k_led {
 };
 
 struct qca8k_priv {
+	/* Cached per-port wire statistics from the switch MIB, refreshed by
+	 * mib_poll_work so that .get_stats64 (atomic context) never sleeps.
+	 * Placed at the top of the struct to stay clear of target-specific
+	 * patches that extend its tail (e.g. ipq40xx IPQ4019 support).
+	 */
+	struct rtnl_link_stats64 port_stats[QCA8K_NUM_PORTS];
+	spinlock_t stats_lock; /* guards port_stats[] */
+	struct delayed_work mib_poll_work;
+
 	u8 switch_id;
 	u8 switch_revision;
 	u8 mirror_rx;
@@ -513,6 +522,12 @@ int qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 write_val);
 /* Common ops function */
 void qca8k_fdb_flush(struct qca8k_priv *priv);
 
+/* Cached MIB statistics served from atomic context */
+void qca8k_get_stats64(struct dsa_switch *ds, int port,
+		       struct rtnl_link_stats64 *s);
+void qca8k_mib_poll_start(struct qca8k_priv *priv);
+void qca8k_mib_poll_stop(struct qca8k_priv *priv);
+
 /* Common ethtool stats function */
 void qca8k_get_strings(struct dsa_switch *ds, int port, u32 stringset, uint8_t *data);
 void qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 3 lines · On Sat, 31 Oct 2026 00:58:31 +0300 Salah Ahmed wrote:

DSA user port netdev statistics count only CPU-seen traffic, so bridge offloaded forwarding is invisible to /sys/class/net/<port>/statistics, to rtnetlink IFLA_STATS and to every kernel consumer of them

net-next is closed, please repost in a week.

And when you do please trim the slop form the commit msg.