Skip to contentSkip to conversation

flower: validate control flags

PATCH Superseded Asbjørn Sloth Tønnesen 2 participants 8 messages Apr 10, 2024 – Apr 11, 2024

Subsystems (6)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover flower: validate control flags

I have reviewed the flower control flags code. In all, but one (sfc), the flags field wasn't checked properly for unsupported flags.

In this series I have only included a single example user for each helper function. Once the helpers are in, I will submit patches for all other drivers implementing flower.

After which there will be:
- 6 drivers using flow_rule_is_supp_control_flags()
- 8 drivers using flow_rule_has_control_flags()
- 11 drivers using flow_rule_match_has_control_flags()

v2:
- squashed the 3 helper functions to one commmit (requested by Baowen Zheng)
- renamed helper functions to avoid double negatives (suggested by Louis Peens)
- reverse booleans in some functions and callsites to align with new names
- fix autodoc format

v1: https://lore.kernel.org/netdev/[email protected]/

Asbjørn Sloth Tønnesen (4):
flow_offload: add control flag checking helpers
nfp: flower: fix check for unsupported control flags
net: prestera: flower: validate control flags
net: dsa: microchip: ksz9477: flower: validate control flags

 drivers/net/dsa/microchip/ksz9477_tc_flower.c |  3 +
 .../marvell/prestera/prestera_flower.c        |  4 ++
 .../ethernet/netronome/nfp/flower/offload.c   |  6 +-
 include/net/flow_offload.h                    | 55 +++++++++++++++++++
 4 files changed, 65 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 flow_offload: add control flag checking helpers

These helpers aim to help drivers, with checking for the presence of unsupported control flags.

For drivers supporting at least one control flag:
flow_rule_is_supp_control_flags()

For drivers using flow_rule_match_control(), but not using flags:
flow_rule_has_control_flags()

For drivers not using flow_rule_match_control():
flow_rule_match_has_control_flags()

While primarily aimed at FLOW_DISSECTOR_KEY_CONTROL and flow_rule_match_control(), then the first two can also be used with FLOW_DISSECTOR_KEY_ENC_CONTROL and flow_rule_match_enc_control().

These helpers mirrors the existing check done in sfc:
drivers/net/ethernet/sfc/tc.c +276

Only compile-tested.

---
 include/net/flow_offload.h | 55 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
include/net/flow_offload.h +55 -0
index 314087a5e1818..9ee3ad4a308a8 100644
@@ -449,6 +449,61 @@ static inline bool flow_rule_match_key(const struct flow_rule *rule,
 	return dissector_uses_key(rule->match.dissector, key);
 }
 
+/**
+ * flow_rule_is_supp_control_flags() - check for supported control flags
+ * @supp_flags: control flags supported by driver
+ * @ctrl_flags: control flags present in rule
+ * @extack: The netlink extended ACK for reporting errors.
+ *
+ * @returns true if only supported control flags are set, false otherwise.
+ */
+static inline bool flow_rule_is_supp_control_flags(const u32 supp_flags,
+						   const u32 ctrl_flags,
+						   struct netlink_ext_ack *extack)
+{
+	if (likely((ctrl_flags & ~supp_flags) == 0))
+		return true;
+
+	NL_SET_ERR_MSG_FMT_MOD(extack,
+			       "Unsupported match on control.flags %#x",
+			       ctrl_flags);
+
+	return false;
+}
+
+/**
+ * flow_rule_has_control_flags() - check for presence of any control flags
+ * @ctrl_flags: control flags present in rule
+ * @extack: The netlink extended ACK for reporting errors.
+ *
+ * @returns true if control flags are set, false otherwise.
+ */
+static inline bool flow_rule_has_control_flags(const u32 ctrl_flags,
+					       struct netlink_ext_ack *extack)
+{
+	return !flow_rule_is_supp_control_flags(0, ctrl_flags, extack);
+}
+
+/**
+ * flow_rule_match_has_control_flags() - match and check for any control flags
+ * @rule: The flow_rule under evaluation.
+ * @extack: The netlink extended ACK for reporting errors.
+ *
+ * @returns true if control flags are set, false otherwise.
+ */
+static inline bool flow_rule_match_has_control_flags(struct flow_rule *rule,
+						     struct netlink_ext_ack *extack)
+{
+	struct flow_match_control match;
+
+	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL))
+		return false;
+
+	flow_rule_match_control(rule, &match);
+
+	return flow_rule_has_control_flags(match.mask->flags, extack);
+}
+
 struct flow_stats {
 	u64	pkts;
 	u64	bytes;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net-next v2 1/4] flow_offload: add control flag checking helpers

Quoted 33 lines · Asbjørn Sloth Tønnesen

These helpers aim to help drivers, with checking for the presence of unsupported control flags.

For drivers supporting at least one control flag:
flow_rule_is_supp_control_flags()

For drivers using flow_rule_match_control(), but not using flags:
flow_rule_has_control_flags()

For drivers not using flow_rule_match_control():
flow_rule_match_has_control_flags()

While primarily aimed at FLOW_DISSECTOR_KEY_CONTROL and flow_rule_match_control(), then the first two can also be used with FLOW_DISSECTOR_KEY_ENC_CONTROL and flow_rule_match_enc_control().

These helpers mirrors the existing check done in sfc:

   drivers/net/ethernet/sfc/tc.c +276

Only compile-tested.

---
  include/net/flow_offload.h | 55 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 55 insertions(+)
include/net/flow_offload.h +0 -0
index 314087a5e1818..9ee3ad4a308a8 100644
@@ -449,6 +449,61 @@ static inline bool flow_rule_match_key(const struct flow_rule *rule,
  	return dissector_uses_key(rule->match.dissector, key);

include/net/flow_offload.h static inline bool flow_rule_match_key(const struct flow_rule *rule, · line 459

Quoted 10 lines
include/net/flow_offload.h +8 -0
  }
 
+/**
+ * flow_rule_is_supp_control_flags() - check for supported control flags
+ * @supp_flags: control flags supported by driver
+ * @ctrl_flags: control flags present in rule
+ * @extack: The netlink extended ACK for reporting errors.
+ *
+ * @returns true if only supported control flags are set, false otherwise.
+ */

The kdoc test failed:

Quoted 1 line

include/net/flow_offload.h:463: warning: No description found for return value of 'flow_rule_is_supp_control_flags'

For some reason I didn't find kernel-doc.rst, because I searched for autodoc sphinx stuff.

Will do proper "Return:" in v3.

I wasn't able to reproduce the kdoc failure[1] locally:

$ ./scripts/kernel-doc -none include/net/flow_offload.h
$ ./scripts/kernel-doc -none -v include/net/flow_offload.h
include/net/flow_offload.h:345: info: Scanning doc for function flow_offload_has_one_action
include/net/flow_offload.h:453: info: Scanning doc for function flow_rule_is_supp_control_flags
include/net/flow_offload.h:475: info: Scanning doc for function flow_rule_has_control_flags
include/net/flow_offload.h:488: info: Scanning doc for function flow_rule_match_has_control_flags

[1] https://netdev.bots.linux.dev/static/nipa/843159/13623977/kdoc/

pw-bot: changes-requested

Quoted 50 lines
include/net/flow_offload.h +47 -0
+static inline bool flow_rule_is_supp_control_flags(const u32 supp_flags,
+						   const u32 ctrl_flags,
+						   struct netlink_ext_ack *extack)
+{
+	if (likely((ctrl_flags & ~supp_flags) == 0))
+		return true;
+
+	NL_SET_ERR_MSG_FMT_MOD(extack,
+			       "Unsupported match on control.flags %#x",
+			       ctrl_flags);
+
+	return false;
+}
+
+/**
+ * flow_rule_has_control_flags() - check for presence of any control flags
+ * @ctrl_flags: control flags present in rule
+ * @extack: The netlink extended ACK for reporting errors.
+ *
+ * @returns true if control flags are set, false otherwise.
+ */
+static inline bool flow_rule_has_control_flags(const u32 ctrl_flags,
+					       struct netlink_ext_ack *extack)
+{
+	return !flow_rule_is_supp_control_flags(0, ctrl_flags, extack);
+}
+
+/**
+ * flow_rule_match_has_control_flags() - match and check for any control flags
+ * @rule: The flow_rule under evaluation.
+ * @extack: The netlink extended ACK for reporting errors.
+ *
+ * @returns true if control flags are set, false otherwise.
+ */
+static inline bool flow_rule_match_has_control_flags(struct flow_rule *rule,
+						     struct netlink_ext_ack *extack)
+{
+	struct flow_match_control match;
+
+	if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL))
+		return false;
+
+	flow_rule_match_control(rule, &match);
+
+	return flow_rule_has_control_flags(match.mask->flags, extack);
+}
+
  struct flow_stats {
  	u64	pkts;
  	u64	bytes;
Signature · 4 lines
Best regards
Asbjørn Sloth Tønnesen
Network Engineer
Fiberby - AS42541
Reviewed-by on v2 1/4
#3 lore raw

Re: [PATCH net-next v2 1/4] flow_offload: add control flag checking helpers

In reply to Asbjørn Sloth Tønnesen
Quoted 17 lines · Asbjørn Sloth Tønnesen

These helpers aim to help drivers, with checking for the presence of unsupported control flags.

For drivers supporting at least one control flag:
flow_rule_is_supp_control_flags()

For drivers using flow_rule_match_control(), but not using flags:
flow_rule_has_control_flags()

For drivers not using flow_rule_match_control():
flow_rule_match_has_control_flags()

While primarily aimed at FLOW_DISSECTOR_KEY_CONTROL and flow_rule_match_control(), then the first two can also be used with FLOW_DISSECTOR_KEY_ENC_CONTROL and flow_rule_match_enc_control().

Quoted 7 lines

These helpers mirrors the existing check done in sfc:
drivers/net/ethernet/sfc/tc.c +276

Only compile-tested.

---

Looks good from my perspective, thanks for the naming updates:

#4 lore raw

PATCH v2 2/4 nfp: flower: fix check for unsupported control flags

In reply to Asbjørn Sloth Tønnesen

Use flow_rule_is_supp_control_flags()

Check the mask, not the key, for unsupported control flags.

Only compile-tested, no access to HW

---
 drivers/net/ethernet/netronome/nfp/flower/offload.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/netronome/nfp/flower/offload.c +3 -3
index 0aceef9fe5826..8e0a890381b60 100644
@@ -527,10 +527,10 @@ nfp_flower_calculate_key_layers(struct nfp_app *app,
 		struct flow_match_control ctl;
 
 		flow_rule_match_control(rule, &ctl);
-		if (ctl.key->flags & ~NFP_FLOWER_SUPPORTED_CTLFLAGS) {
-			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: match on unknown control flag");
+
+		if (!flow_rule_is_supp_control_flags(NFP_FLOWER_SUPPORTED_CTLFLAGS,
+						     ctl.mask->flags, extack))
 			return -EOPNOTSUPP;
-		}
 	}
 
 	ret_key_ls->key_layer = key_layer;
Signature · 1 line
2.43.0
Reviewed-by on v2 2/4
#5 lore raw

Re: [PATCH net-next v2 2/4] nfp: flower: fix check for unsupported control flags

Quoted 10 lines · Asbjørn Sloth Tønnesen

Use flow_rule_is_supp_control_flags()

Check the mask, not the key, for unsupported control flags.

Only compile-tested, no access to HW

---
 drivers/net/ethernet/netronome/nfp/flower/offload.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Thanks for updating:

#6 lore raw

PATCH v2 3/4 net: prestera: flower: validate control flags

In reply to Asbjørn Sloth Tønnesen

Add check for unsupported control flags.

Only compile-tested, no access to HW.

---
 drivers/net/ethernet/marvell/prestera/prestera_flower.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/net/ethernet/marvell/prestera/prestera_flower.c +4 -0
index 8b9455d8a4f7a..418101a931490 100644
@@ -229,6 +229,10 @@ static int prestera_flower_parse(struct prestera_flow_block *block,
 
 		flow_rule_match_control(f_rule, &match);
 		addr_type = match.key->addr_type;
+
+		if (flow_rule_has_control_flags(match.mask->flags,
+						f->common.extack))
+			return -EOPNOTSUPP;
 	}
 
 	if (flow_rule_match_key(f_rule, FLOW_DISSECTOR_KEY_BASIC)) {
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 4/4 net: dsa: microchip: ksz9477: flower: validate control flags

In reply to Asbjørn Sloth Tønnesen

Add check for unsupported control flags.

Only compile-tested, no access to HW.

---
 drivers/net/dsa/microchip/ksz9477_tc_flower.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/dsa/microchip/ksz9477_tc_flower.c +3 -0
index 8b2f5be667e01..ca7830ab168ac 100644
@@ -124,6 +124,9 @@ static int ksz9477_flower_parse_key(struct ksz_device *dev, int port,
 		return -EOPNOTSUPP;
 	}
 
+	if (flow_rule_match_has_control_flags(rule, extack))
+		return -EOPNOTSUPP;
+
 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC) ||
 	    flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
 		ret = ksz9477_flower_parse_key_l2(dev, port, extack, rule,
Signature · 1 line
2.43.0