[PATCH iproute2-next 2/2] flower: support a mask on enc_key_id

From
Alexandre Cassen <hidden>
Date
2026-11-09 13:11:58
Reply to
Alexandre Cassen, 2026-11-09
A block of tunnel keys takes one filter per key. Add a mask:

  enc_key_id 0x40000000/0xc0000000

The key also accepts a 0x prefix. A plain number stays decimal. The dump
prints a partial mask.

Signed-off-by: Alexandre Cassen <redacted>
---
 man/man8/tc-flower.8 | 15 +++++++++++----
 tc/f_flower.c        | 41 ++++++++++++++++++++++-------------------
 tc/tc_util.c         | 12 ++++++++++++
 tc/tc_util.h         |  2 ++
 4 files changed, 47 insertions(+), 23 deletions(-)
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
index 3f8ba465..d64873b5 100644
--- a/man/man8/tc-flower.8
+++ b/man/man8/tc-flower.8
@@ -80,7 +80,7 @@ flower \- flow based traffic control filter
 .BR arp_tha " | " arp_sha " } "
 .IR MASKED_LLADDR " | "
 .B enc_key_id
-.IR KEY-ID " | {"
+.IR MASKED_KEY_ID " | {"
 .BR enc_dst_ip " | " enc_src_ip " } { "
 .IR ipv4_address " | " ipv6_address " } | "
 .B enc_dst_port
@@ -407,7 +407,7 @@ provided in LLADDR format, in which case it is a bitwise mask, or as a
 number of high bits to match. If the mask is missing then a match on all
 bits is assumed.
 .TP
-.BI enc_key_id " NUMBER"
+.BI enc_key_id " MASKED_KEY_ID"
 .TQ
 .BI enc_dst_ip " PREFIX"
 .TQ
@@ -468,8 +468,15 @@ Match the connection zone, and can be masked.
 .TQ
 .BI pfcp_opts " OPTIONS"
 Match on IP tunnel metadata. Key id
-.I NUMBER
-is a 32 bit tunnel key id (e.g. VNI for VXLAN tunnel).
+.I MASKED_KEY_ID
+is a 32 bit tunnel key id (e.g. VNI for VXLAN tunnel), in decimal or in
+hexadecimal with a 0x prefix. A mask may be optionally provided to limit the
+bits which are matched. A mask is provided by following the value with a slash
+and then the mask. If the mask is missing then a match on all bits is assumed.
+Some drivers also offload the key id on a physical port, where nothing
+decapsulates the packet and the hardware reads the key from its tunnel header.
+Such a filter has no software equivalent and may require
+.BR skip_sw .
 .I PREFIX
 must be a valid IPv4 or IPv6 address optionally followed by a slash and the
 prefix length. If the prefix is missing, \fBtc\fR assumes a full-length
diff --git a/tc/f_flower.c b/tc/f_flower.c
index 6fc2c6a1..54643fd7 100644
--- a/tc/f_flower.c
+++ b/tc/f_flower.c
@@ -88,7 +88,7 @@ static void explain(void)
 		"			arp_sha MASKED-LLADDR |\n"
 		"			enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
 		"			enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
-		"			enc_key_id [ KEY-ID ] |\n"
+		"			enc_key_id [ MASKED-KEY-ID ] |\n"
 		"			enc_tos MASKED-IP_TOS |\n"
 		"			enc_ttl MASKED-IP_TTL |\n"
 		"			geneve_opts MASKED-OPTIONS |\n"
@@ -925,16 +925,25 @@ err:
 	return err;
 }
 
-static int flower_parse_key_id(const char *str, int type, struct nlmsghdr *n)
+static int flower_parse_key_id(char *str, int type, int mask_type,
+			       struct nlmsghdr *n)
 {
-	int ret;
-	__be32 key_id;
+	__be32 key_id, mask = htonl(UINT32_MAX);
+	char *slash;
+
+	slash = strchr(str, '/');
+	if (slash)
+		*slash = '\0';
+
+	/* A plain number stays decimal, as it was before masks */
+	if (get_be32(&key_id, str, strncmp(str, "0x", 2) ? 10 : 16) ||
+	    (slash && get_be32(&mask, slash + 1, 0)))
+		return -1;
 
-	ret = get_be32(&key_id, str, 10);
-	if (!ret)
-		addattr32(n, MAX_MSG, type, key_id);
+	addattr32(n, MAX_MSG, type, key_id);
+	addattr32(n, MAX_MSG, mask_type, mask);
 
-	return ret;
+	return 0;
 }
 
 static int flower_parse_enc_port(char *str, int type, struct nlmsghdr *n)
@@ -2193,7 +2202,9 @@ static int flower_parse_opt(const struct filter_util *qu, char *handle,
 		} else if (matches(*argv, "enc_key_id") == 0) {
 			NEXT_ARG();
 			ret = flower_parse_key_id(*argv,
-						  TCA_FLOWER_KEY_ENC_KEY_ID, n);
+						  TCA_FLOWER_KEY_ENC_KEY_ID,
+						  TCA_FLOWER_KEY_ENC_KEY_ID_MASK,
+						  n);
 			if (ret < 0) {
 				fprintf(stderr, "Illegal \"enc_key_id\"\n");
 				return -1;
@@ -2662,15 +2673,6 @@ static void flower_print_ct_mark(struct rtattr *attr,
 	print_masked_u32("ct_mark", attr, mask_attr, true);
 }
 
-static void flower_print_key_id(const char *name, struct rtattr *attr)
-{
-	if (!attr)
-		return;
-
-	print_nl();
-	print_uint_indent_name_value(name, rta_getattr_be32(attr));
-}
-
 static void flower_print_geneve_opts(const char *name, struct rtattr *attr,
 				     char *strbuf)
 {
@@ -3281,7 +3283,8 @@ static int flower_print_opt(const struct filter_util *qu, FILE *f,
 			     tb[TCA_FLOWER_KEY_ENC_IPV6_SRC],
 			     tb[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]);
 
-	flower_print_key_id("enc_key_id", tb[TCA_FLOWER_KEY_ENC_KEY_ID]);
+	print_masked_be32("enc_key_id", tb[TCA_FLOWER_KEY_ENC_KEY_ID],
+			  tb[TCA_FLOWER_KEY_ENC_KEY_ID_MASK], true);
 
 	flower_print_port("enc_dst_port", tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT],
 			  tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]);
diff --git a/tc/tc_util.c b/tc/tc_util.c
index 45d76e75..291f0d9e 100644
--- a/tc/tc_util.c
+++ b/tc/tc_util.c
@@ -940,6 +940,18 @@ void print_masked_be16(const char *name, struct rtattr *attr,
 			  mask_attr, newline);
 }
 
+static __u32 __rta_getattr_be32_u32(const struct rtattr *attr)
+{
+	return rta_getattr_be32(attr);
+}
+
+void print_masked_be32(const char *name, struct rtattr *attr,
+		       struct rtattr *mask_attr, bool newline)
+{
+	print_masked_type(UINT32_MAX, __rta_getattr_be32_u32, name, attr,
+			  mask_attr, newline);
+}
+
 void print_ext_msg(struct rtattr **tb)
 {
 	if (!tb[TCA_EXT_WARN_MSG])
diff --git a/tc/tc_util.h b/tc/tc_util.h
index 8ebca396..e46696a9 100644
--- a/tc/tc_util.h
+++ b/tc/tc_util.h
@@ -135,6 +135,8 @@ void print_masked_u8(const char *name, struct rtattr *attr,
 		     struct rtattr *mask_attr, bool newline);
 void print_masked_be16(const char *name, struct rtattr *attr,
 		       struct rtattr *mask_attr, bool newline);
+void print_masked_be32(const char *name, struct rtattr *attr,
+		       struct rtattr *mask_attr, bool newline);
 
 void print_ext_msg(struct rtattr **tb);
 #endif
-- 
2.47.3
Thread (3 messages) 3 messages, 1 author, last reply 6d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help