Netdev List
 help / color / mirror / Atom feed
* [PATCH net-next] net: dsa: motorcomm: Add limited ACL flow statistics support
@ 2026-11-26  6:01 David Yang
  2026-11-27  6:33 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: David Yang @ 2026-11-26  6:01 UTC (permalink / raw)
  To: netdev
  Cc: David Yang, Andrew Lunn, Vladimir Oltean, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni

The hardware supports flow statistics, which might be used to implement
.cls_flower_stats(). However, the number of flow counters is limited,
and you must choose between byte mode and packet mode. Spend one only
when needed.

Signed-off-by: David Yang <mmyangfl@gmail.com>
---
 drivers/net/dsa/motorcomm/chip.c |   1 +
 drivers/net/dsa/motorcomm/chip.h |   3 +
 drivers/net/dsa/motorcomm/tc.c   | 153 ++++++++++++++++++++++++++++++-
 drivers/net/dsa/motorcomm/tc.h   |  18 +++-
 4 files changed, 169 insertions(+), 6 deletions(-)

diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c
index a21d4bb5e141..1281396941e5 100644
--- a/drivers/net/dsa/motorcomm/chip.c
+++ b/drivers/net/dsa/motorcomm/chip.c
@@ -2629,6 +2629,7 @@ static const struct dsa_switch_ops yt921x_dsa_switch_ops = {
 	.port_policer_add	= yt921x_dsa_port_policer_add,
 	.port_setup_tc		= yt921x_dsa_port_setup_tc,
 	/* acl */
+	.cls_flower_stats	= yt921x_dsa_cls_flower_stats,
 	.cls_flower_del		= yt921x_dsa_cls_flower_del,
 	.cls_flower_add		= yt921x_dsa_cls_flower_add,
 	/* hsr */
diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h
index fc315f1d8f87..fe6cf03a09c8 100644
--- a/drivers/net/dsa/motorcomm/chip.h
+++ b/drivers/net/dsa/motorcomm/chip.h
@@ -424,6 +424,8 @@ enum yt921x_fdb_entry_status {
 /* Custom meters only, not including dedicated port meters (11) */
 #define YT921X_METER_NUM	64
 
+#define YT921X_FLOWSTAT_NUM	64
+
 #define YT921X_LAG_NUM		2
 #define YT921X_LAG_PORT_NUM	4
 
@@ -527,6 +529,7 @@ struct yt921x_priv {
 	u16 eee_ports_mask;
 
 	DECLARE_BITMAP(meters_map, YT921X_METER_NUM);
+	DECLARE_BITMAP(flowstats_map, YT921X_FLOWSTAT_NUM);
 
 	u8 acl_masks[YT921X_ACL_BLK_NUM];
 	struct yt921x_acl_blk *acl_blks[YT921X_ACL_BLK_NUM];
diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c
index e3f057ed580f..1d927a02e115 100644
--- a/drivers/net/dsa/motorcomm/tc.c
+++ b/drivers/net/dsa/motorcomm/tc.c
@@ -565,6 +565,11 @@ struct yt921x_acl_rule_ext {
 	struct yt921x_acl_rule r;
 
 	struct yt921x_marker marker;
+
+	/* try acquiring a counter */
+	bool stat_applicable:1;
+	/* must acquire a counter if applicable */
+	bool stat_required:1;
 };
 
 static int
@@ -1041,6 +1046,9 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 	int res;
 
 	memset(action, 0, 3 * sizeof(*action));
+	ruleext->stat_applicable = false;
+	ruleext->stat_required = false;
+
 	flow_action_for_each(i, act, flow_action) {
 		/* The flow action entry carries no mirred control, so there
 		 * is no way to tell whether software would continue past a
@@ -1052,6 +1060,12 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 			goto fallback;
 		}
 
+		if (act->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
+			ruleext->stat_applicable = true;
+			ruleext->stat_required = act->hw_stats ==
+						 FLOW_ACTION_HW_STATS_IMMEDIATE;
+		}
+
 		switch (act->id) {
 		case FLOW_ACTION_ACCEPT:
 			action[2] |= YT921X_ACL_ACTc_FWD_EN |
@@ -1059,6 +1073,9 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 			terminal = true;
 			break;
 		case FLOW_ACTION_DROP:
+			/* FLOWSTAT only counts passed frames */
+			ruleext->stat_applicable = false;
+
 			action[2] |= YT921X_ACL_ACTc_FWD_EN |
 				     YT921X_ACL_ACTc_FWD_REDIR;
 			terminal = true;
@@ -1136,6 +1153,7 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 			action[2] |= YT921X_ACL_ACTc_FWD_EN |
 				     YT921X_ACL_ACTc_FWD_TRAP;
 			terminal = true;
+			redir = false;
 			break;
 		}
 
@@ -1143,7 +1161,21 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
 			break;
 	}
 
+	/* When frames are counted by software, a hardware counter would double
+	 * count those. Do not assign a counter when !skip_sw && !redir.
+	 */
+	if (ruleext->stat_applicable && !cls->common.skip_sw && !redir)
+		ruleext->stat_applicable = false;
+	/* Do not check on !skip_sw rules: we can't stop software from
+	 * installing them.
+	 */
+	if (ruleext->stat_applicable && cls->common.skip_sw &&
+	    !flow_action_hw_stats_check(flow_action, extack,
+					FLOW_ACTION_HW_STATS_IMMEDIATE_BIT))
+		return -EOPNOTSUPP;
+
 	ruleext->r.sw_assisted = !cls->common.skip_sw;
+
 	return 0;
 }
 
@@ -1208,6 +1240,9 @@ yt921x_acl_rule_ext_parse_flow(struct yt921x_acl_rule_ext *ruleext, int port,
 	yt921x_acl_rule_set_ports(&ruleext->r, ord, BIT(port));
 	ruleext->r.tag = cls->cookie;
 	ruleext->r.type = TC_SETUP_CLSFLOWER;
+	/* Align with sja1105 */
+	ruleext->r.stat_pkt_mode = true;
+	ruleext->r.laststat = 0;
 	return 0;
 }
 
@@ -1281,6 +1316,45 @@ yt921x_acl_reserve(struct yt921x_priv *priv, unsigned int entscnt,
 	return UINT_MAX;
 }
 
+static int
+yt921x_acl_stat(struct yt921x_priv *priv, enum tc_setup_type type,
+		unsigned long tag, u16 ports_mask, struct flow_stats *stats)
+{
+	struct yt921x_acl_rule *aclrule;
+	const struct yt921x_acl_blk *aclblk;
+	unsigned int statid;
+	unsigned int binid;
+	unsigned int blkid;
+	unsigned int entid;
+	u64 diff;
+	u64 stat;
+	int res;
+
+	entid = yt921x_acl_find(priv, type, tag, ports_mask);
+	if (entid == UINT_MAX)
+		return -ENOENT;
+
+	blkid = entid / YT921X_ACL_ENT_PER_BLK;
+	binid = entid % YT921X_ACL_ENT_PER_BLK;
+	aclblk = priv->acl_blks[blkid];
+	aclrule = aclblk->rules[binid];
+
+	if (!(aclrule->action[0] & YT921X_ACL_ACTa_FLOWSTAT_EN))
+		return -EOPNOTSUPP;
+
+	statid = FIELD_GET(YT921X_ACL_ACTa_FLOWSTAT_ID_M, aclrule->action[0]);
+	res = yt921x_counter_read(priv, YT921X_FLOWSTATn_STAT(statid), &stat);
+	if (res)
+		return res;
+
+	diff = stat - aclrule->laststat;
+	aclrule->laststat = stat;
+	flow_stats_update(stats, aclrule->stat_pkt_mode ? 0 : diff,
+			  !aclrule->stat_pkt_mode ? 0 : diff, 0,
+			  jiffies, FLOW_ACTION_HW_STATS_IMMEDIATE);
+	return 0;
+}
+
 static int
 yt921x_acl_commit(struct yt921x_priv *priv, unsigned int entid, u8 entsmask)
 {
@@ -1413,9 +1487,13 @@ yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type,
 	}
 
 	if (aclrule->action[0] & YT921X_ACL_ACTa_METER_EN)
-		clear_bit(FIELD_GET(YT921X_ACL_ACTa_METER_ID_M,
-				    aclrule->action[0]),
-			  priv->meters_map);
+		__clear_bit(FIELD_GET(YT921X_ACL_ACTa_METER_ID_M,
+				      aclrule->action[0]),
+			    priv->meters_map);
+	if (aclrule->action[0] & YT921X_ACL_ACTa_FLOWSTAT_EN)
+		__clear_bit(FIELD_GET(YT921X_ACL_ACTa_FLOWSTAT_ID_M,
+				      aclrule->action[0]),
+			    priv->flowstats_map);
 	priv->acl_masks[blkid] &= ~aclrule->mask;
 	kvfree(aclrule);
 	if (!priv->acl_masks[blkid]) {
@@ -1433,13 +1511,15 @@ yt921x_acl_add(struct yt921x_priv *priv,
 	unsigned int entscnt = hweight8(ruleext->r.mask);
 	struct yt921x_acl_rule *aclrule;
 	struct yt921x_acl_blk *aclblk;
-	bool use_trap = false;
+	bool stat_applicable;
 	unsigned int meterid;
+	unsigned int statid;
 	unsigned int binid;
 	unsigned int blkid;
 	unsigned int entid;
 	unsigned long m;
 	unsigned int o;
+	bool use_trap;
 	u32 ctrl;
 	int res;
 
@@ -1448,6 +1528,11 @@ yt921x_acl_add(struct yt921x_priv *priv,
 	if (entid == UINT_MAX)
 		return -EOPNOTSUPP;
 
+	stat_applicable = ruleext->stat_applicable;
+	use_trap = (ruleext->r.action[2] & YT921X_ACL_ACTc_FWD_EN) &&
+		   (FIELD_GET(YT921X_ACL_ACTc_FWD_M,
+			      ruleext->r.action[2]) == YT921X_ACL_ACTc_FWD_TRAP);
+
 	if (!(ruleext->r.action[0] & YT921X_ACL_ACTa_METER_EN)) {
 		meterid = YT921X_METER_NUM;
 	} else {
@@ -1459,6 +1544,7 @@ yt921x_acl_add(struct yt921x_priv *priv,
 			if (res)
 				return res;
 		} else if (ruleext->r.sw_assisted) {
+			stat_applicable = false;
 			use_trap = true;
 		} else {
 			NL_SET_ERR_MSG_MOD(extack,
@@ -1467,6 +1553,43 @@ yt921x_acl_add(struct yt921x_priv *priv,
 		}
 	}
 
+	if (!stat_applicable) {
+		statid = YT921X_FLOWSTAT_NUM;
+	} else {
+		statid = find_first_zero_bit(priv->flowstats_map,
+					     YT921X_FLOWSTAT_NUM);
+		if (statid >= YT921X_FLOWSTAT_NUM) {
+			/* Do not refuse !skip_sw rules: we can't stop software
+			 * from installing them.
+			 */
+			if (!ruleext->r.sw_assisted && ruleext->stat_required) {
+				NL_SET_ERR_MSG_MOD(extack,
+						   "No more flowstats, but immediate HW stats requested");
+				return -EOPNOTSUPP;
+			}
+			NL_SET_ERR_MSG_MOD(extack,
+					   "No more flowstats, HW stats disabled");
+		} else {
+			u32 zeros[2] = {};
+
+			ctrl = YT921X_FLOWSTAT_CTRL_TYPE_FLOW |
+			       YT921X_FLOWSTAT_CTRL_EN;
+			if (ruleext->r.stat_pkt_mode)
+				ctrl |= YT921X_FLOWSTAT_CTRL_PKT_MODE;
+			res = yt921x_reg_write(priv,
+					       YT921X_FLOWSTATn_CTRL(statid),
+					       ctrl);
+			if (res)
+				return res;
+
+			res = yt921x_reg64_write(priv,
+						 YT921X_FLOWSTATn_STAT(statid),
+						 zeros);
+			if (res)
+				return res;
+		}
+	}
+
 	/* Prepare acl block ctrlblk */
 	blkid = entid / YT921X_ACL_ENT_PER_BLK;
 	binid = entid % YT921X_ACL_ENT_PER_BLK;
@@ -1507,6 +1630,9 @@ yt921x_acl_add(struct yt921x_priv *priv,
 		aclrule->action[0] |= YT921X_ACL_ACTa_METER_ID(meterid);
 	else
 		aclrule->action[0] &= ~YT921X_ACL_ACTa_METER_EN;
+	if (statid < YT921X_FLOWSTAT_NUM)
+		aclrule->action[0] |= YT921X_ACL_ACTa_FLOWSTAT_EN |
+				      YT921X_ACL_ACTa_FLOWSTAT_ID(statid);
 
 	/* Activate ACL if needed */
 	ctrl = 0;
@@ -1535,7 +1661,9 @@ yt921x_acl_add(struct yt921x_priv *priv,
 		priv->ports[o].acl_cnt++;
 
 	if (meterid < YT921X_METER_NUM)
-		set_bit(meterid, priv->meters_map);
+		__set_bit(meterid, priv->meters_map);
+	if (statid < YT921X_FLOWSTAT_NUM)
+		__set_bit(statid, priv->flowstats_map);
 	priv->acl_masks[blkid] |= aclrule->mask;
 	return 0;
 
@@ -1548,6 +1676,21 @@ yt921x_acl_add(struct yt921x_priv *priv,
 	return res;
 }
 
+int
+yt921x_dsa_cls_flower_stats(struct dsa_switch *ds, int port,
+			    struct flow_cls_offload *cls, bool ingress)
+{
+	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
+	int res;
+
+	mutex_lock(&priv->reg_lock);
+	res = yt921x_acl_stat(priv, TC_SETUP_CLSFLOWER, cls->cookie,
+			      BIT(port), &cls->stats);
+	mutex_unlock(&priv->reg_lock);
+
+	return res;
+}
+
 int
 yt921x_dsa_cls_flower_del(struct dsa_switch *ds, int port,
 			  struct flow_cls_offload *cls, bool ingress)
diff --git a/drivers/net/dsa/motorcomm/tc.h b/drivers/net/dsa/motorcomm/tc.h
index cc53f148d690..9e50285d66f6 100644
--- a/drivers/net/dsa/motorcomm/tc.h
+++ b/drivers/net/dsa/motorcomm/tc.h
@@ -299,6 +299,16 @@ enum yt921x_udf_type {
 #define  YT921X_METER_CTRLa_EIR_M		GENMASK(17, 0)
 #define   YT921X_METER_CTRLa_EIR(x)			FIELD_PREP(YT921X_METER_CTRLa_EIR_M, (x))
 #define YT921X_METERn_STAT(x)		(0x221000 + 8 * (x))
+#define YT921X_FLOWSTATn_STAT(x)	(0x221400 + 8 * (x))
+#define YT921X_FLOWSTATn_CTRL(x)	(0x221c00 + 4 * (x))
+#define  YT921X_FLOWSTAT_CTRL_EN		BIT(3)
+#define  YT921X_FLOWSTAT_CTRL_PKT_MODE		BIT(2)	/* 0: byte mode */
+#define  YT921X_FLOWSTAT_CTRL_TYPE_M		GENMASK(1, 0)
+#define   YT921X_FLOWSTAT_CTRL_TYPE(x)			FIELD_PREP(YT921X_FLOWSTAT_CTRL_TYPE_M, (x))
+#define   YT921X_FLOWSTAT_CTRL_TYPE_FLOW		YT921X_FLOWSTAT_CTRL_TYPE(0)
+#define   YT921X_FLOWSTAT_CTRL_TYPE_CPU_CODE		YT921X_FLOWSTAT_CTRL_TYPE(1)
+#define   YT921X_FLOWSTAT_CTRL_TYPE_DROP_CODE		YT921X_FLOWSTAT_CTRL_TYPE(2)
+#define   YT921X_FLOWSTAT_CTRL_TYPE_PORT		YT921X_FLOWSTAT_CTRL_TYPE(3)
 
 #define YT921X_MIRROR			0x300300
 #define  YT921X_MIRROR_IGR_PORTS_M		GENMASK(26, 16)
@@ -349,7 +359,10 @@ struct yt921x_acl_rule {
 	enum tc_setup_type type;
 
 	u32 action[3];
-	bool sw_assisted;
+	u64 laststat;
+
+	bool sw_assisted:1;
+	bool stat_pkt_mode:1;
 
 	u8 mask;
 	struct yt921x_acl_entry entries[YT921X_ACL_ENT_PER_BLK];
@@ -368,6 +381,9 @@ int
 yt921x_dsa_port_setup_tc(struct dsa_switch *ds, int port,
 			 enum tc_setup_type type, void *type_data);
 int
+yt921x_dsa_cls_flower_stats(struct dsa_switch *ds, int port,
+			    struct flow_cls_offload *cls, bool ingress);
+int
 yt921x_dsa_cls_flower_del(struct dsa_switch *ds, int port,
 			  struct flow_cls_offload *cls, bool ingress);
 int
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH net-next] net: dsa: motorcomm: Add limited ACL flow statistics support
  2026-11-26  6:01 [PATCH net-next] net: dsa: motorcomm: Add limited ACL flow statistics support David Yang
@ 2026-11-27  6:33 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-27  6:33 UTC (permalink / raw)
  To: mmyangfl; +Cc: netdev, andrew, olteanv, davem, edumazet, kuba, pabeni

Thank you for your contribution! Sashiko AI review found 9 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 7

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] Counters are withheld from DROP rules on the premise that
  "FLOWSTAT only counts passed frames", but police rules, which also…
- [Medium] `yt921x_acl_stat()` always passes `jiffies` as `lastused` to
  `flow_stats_update()`, even when `diff == 0` (no new packets since the…
- [Low] Flow counters always run in packet mode, so `tc -s` always shows
  0 bytes for offloaded flower rules.
- [Low] `yt921x_acl_rule_ext_parse_flow_action()` now rejects some
  skip_sw rules based on their tc `hw_stats` setting, and the commit…
- [Low] The feature patch also changes existing code without mentioning
  it.
- [Low] The patch adds a second limited ACL hardware pool,
  `flowstats_map` with `YT921X_FLOWSTAT_NUM` = 64 entries, managed…
- [Low] `yt921x_acl_add()` initialises `use_trap` by comparing an
  unshifted `FIELD_GET()` result with a shifted `FIELD_PREP()` constant:…
- [Low] Flowstat counter IDs are returned to the software allocator even
  when the hardware update that should drop the ACL entry's reference to…
- [Low] When a counter is reused, `yt921x_acl_add()` resets it only by
  writing zeros directly to `YT921X_FLOWSTATn_STAT(statid)` via…

--

net: dsa: motorcomm: Add limited ACL flow statistics support

This commit implements .cls_flower_stats() for the yt921x switch using its
hardware flow counters. A pool of 64 counters is tracked in flowstats_map,
and a counter is only allocated to a flower rule when hardware stats are
applicable. Counters are programmed in packet mode and reported as deltas
against the last read value.

> diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h
> index fc315f1d8f87b..fe6cf03a09c8e 100644
> --- a/drivers/net/dsa/motorcomm/chip.h
> +++ b/drivers/net/dsa/motorcomm/chip.h

[ ... ]

> @@ -527,6 +529,7 @@ struct yt921x_priv {
>  	u16 eee_ports_mask;
>  
>  	DECLARE_BITMAP(meters_map, YT921X_METER_NUM);
> +	DECLARE_BITMAP(flowstats_map, YT921X_FLOWSTAT_NUM);

[Severity: Low]
Should this new pool also be registered as a devlink resource?

yt921x_devlink_resources[] in devlink.c exposes "ACL" and "meters", each
with an occupancy callback, for example:

	count = bitmap_weight(priv->meters_map, YT921X_METER_NUM);

There is no entry for the 64 flow counters. Once they run out, a rule is
either rejected with "No more flowstats, but immediate HW stats requested"
or gets HW stats silently disabled. devlink resource show gives no sign of
either.

> diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c
> index e3f057ed580f5..1d927a02e1153 100644
> --- a/drivers/net/dsa/motorcomm/tc.c
> +++ b/drivers/net/dsa/motorcomm/tc.c

[ ... ]

> @@ -1059,6 +1073,9 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
>  			terminal = true;
>  			break;
>  		case FLOW_ACTION_DROP:
> +			/* FLOWSTAT only counts passed frames */
> +			ruleext->stat_applicable = false;
> +

[Severity: Medium]
If the counter only sees passed frames, what happens with police rules?

The FLOW_ACTION_POLICE case sets YT921X_ACL_ACTa_METER_EN and leaves
stat_applicable alone. yt921x_meter_config() programs the meter with
YT921X_METER_CTRLc_DROP_R, so exceeding frames are dropped in hardware.

For a rule like "flower skip_sw ... action police rate X burst Y
conform-exceed drop/pipe action pass", yt921x_acl_add() allocates both a
meter and a counter. yt921x_acl_stat() then reports the counter as the
filter's packet count, with drops always 0:

	flow_stats_update(stats, aclrule->stat_pkt_mode ? 0 : diff,
			  !aclrule->stat_pkt_mode ? 0 : diff, 0,
			  jiffies, FLOW_ACTION_HW_STATS_IMMEDIATE);

If the comment is accurate, would that undercount the matched packets for
police rules, including for the police action itself? If it isn't
accurate, are drop rules being refused counters unnecessarily?

[ ... ]

> @@ -1143,7 +1161,21 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext,
>  			break;
>  	}
>  
> +	/* When frames are counted by software, a hardware counter would double
> +	 * count those. Do not assign a counter when !skip_sw && !redir.
> +	 */
> +	if (ruleext->stat_applicable && !cls->common.skip_sw && !redir)
> +		ruleext->stat_applicable = false;
> +	/* Do not check on !skip_sw rules: we can't stop software from
> +	 * installing them.
> +	 */
> +	if (ruleext->stat_applicable && cls->common.skip_sw &&
> +	    !flow_action_hw_stats_check(flow_action, extack,
> +					FLOW_ACTION_HW_STATS_IMMEDIATE_BIT))
> +		return -EOPNOTSUPP;

[Severity: Low]
With this check, skip_sw rules using "hw_stats delayed", or rules whose
actions use different hw_stats types, now fail with -EOPNOTSUPP. They were
accepted before this patch. Could the commit message mention this?

The check also only runs while stat_applicable is still true, and the
FLOW_ACTION_DROP case clears it. So a "skip_sw ... action drop hw_stats
immediate" rule is never validated here. yt921x_acl_add() takes the
!stat_applicable branch and installs it without a counter or any extack
warning. After that, yt921x_acl_stat() returns -EOPNOTSUPP for it.

A skip_sw accept or redirect rule with the same explicit "hw_stats
immediate" is refused in yt921x_acl_add() once the counters run out:

	if (!ruleext->r.sw_assisted && ruleext->stat_required) {
		NL_SET_ERR_MSG_MOD(extack,
				   "No more flowstats, but immediate HW stats requested");
		return -EOPNOTSUPP;
	}

Is it intended that the same explicit request is rejected in one case and
silently ignored in the other?

> @@ -1208,6 +1240,9 @@ yt921x_acl_rule_ext_parse_flow(struct yt921x_acl_rule_ext *ruleext, int port,
>  	yt921x_acl_rule_set_ports(&ruleext->r, ord, BIT(port));
>  	ruleext->r.tag = cls->cookie;
>  	ruleext->r.type = TC_SETUP_CLSFLOWER;
> +	/* Align with sja1105 */
> +	ruleext->r.stat_pkt_mode = true;
> +	ruleext->r.laststat = 0;

[Severity: Low]
This is the only place stat_pkt_mode is set. The counters therefore always
run in packet mode, and tc -s will always show 0 bytes for offloaded
flower rules.

As a result, the byte mode paths can never be taken: the
YT921X_FLOWSTAT_CTRL_PKT_MODE check in yt921x_acl_add() and the
stat_pkt_mode ternaries in yt921x_acl_stat().

The commit message says:

    and you must choose between byte mode and packet mode.

but it does not say which mode is used, or that byte counts are never
reported. Could the commit message state this? Is the byte mode code
needed at all?

> @@ -1281,6 +1316,45 @@ yt921x_acl_reserve(struct yt921x_priv *priv, unsigned int entscnt,
>  	return UINT_MAX;
>  }
>  
> +static int
> +yt921x_acl_stat(struct yt921x_priv *priv, enum tc_setup_type type,
> +		unsigned long tag, u16 ports_mask, struct flow_stats *stats)
> +{

[ ... ]

> +	diff = stat - aclrule->laststat;
> +	aclrule->laststat = stat;
> +	flow_stats_update(stats, aclrule->stat_pkt_mode ? 0 : diff,
> +			  !aclrule->stat_pkt_mode ? 0 : diff, 0,
> +			  jiffies, FLOW_ACTION_HW_STATS_IMMEDIATE);

[Severity: Medium]
Does passing jiffies as lastused here, even when diff is 0, mark the rule
as just used on every stats poll?

flow_stats_update() stores max(lastused, jiffies), and
tcf_action_stats_update() copies that into the actions' lastuse. As a
result, tc -s filter show would always report "used 0 sec". Anything that
ages out idle offloaded flows by lastuse while polling stats, such as OVS
tc offload, would never see these rules as idle.

cxgb4 only updates last_used when the packet count has changed, and ocelot
and lan966x pass 0. Could lastused be updated only when diff is non-zero?

[ ... ]

> @@ -1413,9 +1487,13 @@ yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type,
>  	}
>  
>  	if (aclrule->action[0] & YT921X_ACL_ACTa_METER_EN)
> -		clear_bit(FIELD_GET(YT921X_ACL_ACTa_METER_ID_M,
> -				    aclrule->action[0]),
> -			  priv->meters_map);
> +		__clear_bit(FIELD_GET(YT921X_ACL_ACTa_METER_ID_M,
> +				      aclrule->action[0]),
> +			    priv->meters_map);

[Severity: Low]
This isn't a bug, but this patch also changes existing code that flow
statistics does not need:

  - meters_map moves from clear_bit()/set_bit() to
    __clear_bit()/__set_bit()
  - sw_assisted in struct yt921x_acl_rule becomes a bitfield
  - "bool use_trap = false" in yt921x_acl_add() is replaced by a value
    derived from the action word

The non-atomic bitops look fine, since every meters_map user holds
priv->reg_lock. The new use_trap initialisation has no effect, because the
later if (use_trap) block only rewrites TRAP bits that a trap rule already
carries.

Could these changes be split into a separate patch, or at least mentioned
in the commit message?

> +	if (aclrule->action[0] & YT921X_ACL_ACTa_FLOWSTAT_EN)
> +		__clear_bit(FIELD_GET(YT921X_ACL_ACTa_FLOWSTAT_ID_M,
> +				      aclrule->action[0]),
> +			    priv->flowstats_map);

[Severity: Low]
Is it safe to release the counter ID here when yt921x_acl_commit() failed
earlier in yt921x_acl_del()?

	aclblk->rules[binid] = NULL;
	ret = yt921x_acl_commit(priv, entid, aclrule->mask);
	/* the kernel never rolls back on failure */

yt921x_acl_commit() writes the action word first and then does several
more register writes. Any of those later writes can fail and leave the
hardware entry still referencing statid.

The error path of yt921x_acl_add() has the same problem. If
yt921x_acl_commit() fails after writing an action word that contains
YT921X_ACL_ACTa_FLOWSTAT_EN | YT921X_ACL_ACTa_FLOWSTAT_ID(statid), the rule
is freed and statid is never marked as used.

In both cases the next yt921x_acl_add() can pick the same statid via
find_first_zero_bit(), reprogram and zero it, and attach it to a new rule.
yt921x_acl_stat() might then report the stale entry's traffic as the new
rule's. The meter IDs already have the same lifecycle, but this patch adds
it for the new flowstat resource.

[ ... ]

> @@ -1448,6 +1528,11 @@ yt921x_acl_add(struct yt921x_priv *priv,
>  	if (entid == UINT_MAX)
>  		return -EOPNOTSUPP;
>  
> +	stat_applicable = ruleext->stat_applicable;
> +	use_trap = (ruleext->r.action[2] & YT921X_ACL_ACTc_FWD_EN) &&
> +		   (FIELD_GET(YT921X_ACL_ACTc_FWD_M,
> +			      ruleext->r.action[2]) == YT921X_ACL_ACTc_FWD_TRAP);

[Severity: Low]
Can this comparison ever be true? FIELD_GET() returns the unshifted field
value, 0 to 3 for GENMASK(22, 21). YT921X_ACL_ACTc_FWD_TRAP is already
shifted:

#define  YT921X_ACL_ACTc_FWD_M			GENMASK(22, 21)
#define   YT921X_ACL_ACTc_FWD(x)			FIELD_PREP(YT921X_ACL_ACTc_FWD_M, (x))
#define   YT921X_ACL_ACTc_FWD_TRAP			YT921X_ACL_ACTc_FWD(3)

So use_trap always starts out false, even for trap rules. Should this be
(ruleext->r.action[2] & YT921X_ACL_ACTc_FWD_M) == YT921X_ACL_ACTc_FWD_TRAP
instead?

There's no runtime effect today, since the if (use_trap) block would only
rewrite TRAP bits a trap rule already has.

[ ... ]

> @@ -1467,6 +1553,43 @@ yt921x_acl_add(struct yt921x_priv *priv,
>  		}
>  	}
>  
> +	if (!stat_applicable) {
> +		statid = YT921X_FLOWSTAT_NUM;
> +	} else {

[ ... ]

> +		} else {
> +			u32 zeros[2] = {};
> +
> +			ctrl = YT921X_FLOWSTAT_CTRL_TYPE_FLOW |
> +			       YT921X_FLOWSTAT_CTRL_EN;
> +			if (ruleext->r.stat_pkt_mode)
> +				ctrl |= YT921X_FLOWSTAT_CTRL_PKT_MODE;
> +			res = yt921x_reg_write(priv,
> +					       YT921X_FLOWSTATn_CTRL(statid),
> +					       ctrl);
> +			if (res)
> +				return res;
> +
> +			res = yt921x_reg64_write(priv,
> +						 YT921X_FLOWSTATn_STAT(statid),
> +						 zeros);

[Severity: Low]
Does a direct write to the STAT register actually clear the counter?

This is the only place the driver writes a statistics count register.
YT921X_METERn_STAT is never written, and the MIB counters are cleared
through YT921X_MIB_CTRL_CLEAN.

If the FLOWSTAT count register is read-only or ignores writes, a reused
counter keeps its previous owner's total. Since
yt921x_acl_rule_ext_parse_flow() sets laststat = 0, the first
yt921x_acl_stat() call after reuse would report all of the previous rule's
packets as new traffic.

Would it be safer to read the counter at allocation and store that value
as laststat? That works whether or not the register is writable.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126060127.185631-1-mmyangfl%40gmail.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27  6:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  6:01 [PATCH net-next] net: dsa: motorcomm: Add limited ACL flow statistics support David Yang
2026-11-27  6:33 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox