naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0029-tc-f_flower-add-options-support-for-vxlan.patch

359b1d
From 4bd1eb80a195ce1a64e33f5fc9d5c58bf9f30c8d Mon Sep 17 00:00:00 2001
359b1d
From: Andrea Claudi <aclaudi@redhat.com>
359b1d
Date: Thu, 4 Jun 2020 21:43:50 +0200
359b1d
Subject: [PATCH] tc: f_flower: add options support for vxlan
359b1d
359b1d
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1830485
359b1d
Upstream Status: unknown commit 93c8d5f72f8ce
359b1d
Conflicts: on a removed line due to missing commit 93c8d5f72f8ce
359b1d
           ("tc: f_flower: add options support for vxlan")
359b1d
359b1d
commit 93c8d5f72f8ce4b98c68508e85457f83933302c0
359b1d
Author: Xin Long <lucien.xin@gmail.com>
359b1d
Date:   Mon Apr 27 18:27:50 2020 +0800
359b1d
359b1d
    tc: f_flower: add options support for vxlan
359b1d
359b1d
    This patch is to add TCA_FLOWER_KEY_ENC_OPTS_VXLAN's parse and
359b1d
    print to implement vxlan options support in m_tunnel_key, like
359b1d
    Commit 56155d4df86d ("tc: f_flower: add geneve option match
359b1d
    support to flower") for geneve options support.
359b1d
359b1d
    Option is expressed a 32bit number for gbp only, and vxlan
359b1d
    doesn't support multiple options.
359b1d
359b1d
    With this patch, users can add and dump vxlan options like:
359b1d
359b1d
      # ip link add name vxlan1 type vxlan dstport 0 external
359b1d
      # tc qdisc add dev vxlan1 ingress
359b1d
      # tc filter add dev vxlan1 protocol ip parent ffff: \
359b1d
          flower \
359b1d
            enc_src_ip 10.0.99.192 \
359b1d
            enc_dst_ip 10.0.99.193 \
359b1d
            enc_key_id 11 \
359b1d
            vxlan_opts 65793/4008635966 \
359b1d
            ip_proto udp \
359b1d
            action mirred egress redirect dev eth1
359b1d
      # tc -s filter show dev vxlan1 parent ffff:
359b1d
359b1d
         filter protocol ip pref 49152 flower chain 0 handle 0x1
359b1d
           eth_type ipv4
359b1d
           ip_proto udp
359b1d
           enc_dst_ip 10.0.99.193
359b1d
           enc_src_ip 10.0.99.192
359b1d
           enc_key_id 11
359b1d
           vxlan_opts 65793/4008635966
359b1d
           not_in_hw
359b1d
             action order 1: mirred (Egress Redirect to device eth1) stolen
359b1d
             index 3 ref 1 bind 1
359b1d
             Action statistics:
359b1d
             Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
359b1d
             backlog 0b 0p requeues 0
359b1d
359b1d
    v1->v2:
359b1d
      - get_u32 with base = 0 for gbp.
359b1d
    v2->v3:
359b1d
      - implement proper JSON array for opts.
359b1d
    v3->v4:
359b1d
      - keep the same format between input and output, json and non json.
359b1d
      - print gbp as uint.
359b1d
359b1d
    Signed-off-by: Xin Long <lucien.xin@gmail.com>
359b1d
    Signed-off-by: David Ahern <dsahern@gmail.com>
359b1d
---
359b1d
 man/man8/tc-flower.8 |  12 ++++
359b1d
 tc/f_flower.c        | 130 +++++++++++++++++++++++++++++++++++++------
359b1d
 2 files changed, 126 insertions(+), 16 deletions(-)
359b1d
359b1d
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
359b1d
index adff41e39b006..0efacbfdf9a95 100644
359b1d
--- a/man/man8/tc-flower.8
359b1d
+++ b/man/man8/tc-flower.8
359b1d
@@ -81,7 +81,11 @@ flower \- flow based traffic control filter
359b1d
 .IR TOS " | "
359b1d
 .B enc_ttl
359b1d
 .IR TTL " | "
359b1d
+{
359b1d
 .B geneve_opts
359b1d
+|
359b1d
+.B vxlan_opts
359b1d
+}
359b1d
 .IR OPTIONS " | "
359b1d
 .BR ip_flags
359b1d
 .IR IP_FLAGS
359b1d
@@ -290,6 +294,8 @@ bits is assumed.
359b1d
 .BI enc_ttl " NUMBER"
359b1d
 .TQ
359b1d
 .BI geneve_opts " OPTIONS"
359b1d
+.TQ
359b1d
+.BI vxlan_opts " OPTIONS"
359b1d
 Match on IP tunnel metadata. Key id
359b1d
 .I NUMBER
359b1d
 is a 32 bit tunnel key id (e.g. VNI for VXLAN tunnel).
359b1d
@@ -310,6 +316,12 @@ the masks is missing, \fBtc\fR assumes a full-length match. The options can
359b1d
 be described in the form CLASS:TYPE:DATA/CLASS_MASK:TYPE_MASK:DATA_MASK,
359b1d
 where CLASS is represented as a 16bit hexadecimal value, TYPE as an 8bit
359b1d
 hexadecimal value and DATA as a variable length hexadecimal value.
359b1d
+vxlan_opts
359b1d
+.I OPTIONS
359b1d
+doesn't support multiple options, and it consists of a key followed by a slash
359b1d
+and corresponding mask. If the mask is missing, \fBtc\fR assumes a full-length
359b1d
+match. The option can be described in the form GBP/GBP_MASK, where GBP is
359b1d
+represented as a 32bit number.
359b1d
 .TP
359b1d
 .BI ip_flags " IP_FLAGS"
359b1d
 .I IP_FLAGS
359b1d
diff --git a/tc/f_flower.c b/tc/f_flower.c
359b1d
index 70d40d3b2f2bf..09079cd2c2280 100644
359b1d
--- a/tc/f_flower.c
359b1d
+++ b/tc/f_flower.c
359b1d
@@ -81,6 +81,7 @@ static void explain(void)
359b1d
 		"			enc_tos MASKED-IP_TOS |\n"
359b1d
 		"			enc_ttl MASKED-IP_TTL |\n"
359b1d
 		"			geneve_opts MASKED-OPTIONS |\n"
359b1d
+		"			vxlan_opts MASKED-OPTIONS |\n"
359b1d
 		"			ip_flags IP-FLAGS | \n"
359b1d
 		"			enc_dst_port [ port_number ] }\n"
359b1d
 		"	FILTERID := X:Y:Z\n"
359b1d
@@ -648,7 +649,7 @@ static int flower_parse_enc_port(char *str, int type, struct nlmsghdr *n)
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
-static int flower_parse_geneve_opts(char *str, struct nlmsghdr *n)
359b1d
+static int flower_parse_geneve_opt(char *str, struct nlmsghdr *n)
359b1d
 {
359b1d
 	struct rtattr *nest;
359b1d
 	char *token;
359b1d
@@ -718,14 +719,33 @@ static int flower_parse_geneve_opts(char *str, struct nlmsghdr *n)
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
-static int flower_parse_enc_opt_part(char *str, struct nlmsghdr *n)
359b1d
+static int flower_parse_vxlan_opt(char *str, struct nlmsghdr *n)
359b1d
+{
359b1d
+	struct rtattr *nest;
359b1d
+	__u32 gbp;
359b1d
+	int err;
359b1d
+
359b1d
+	nest = addattr_nest(n, MAX_MSG,
359b1d
+			    TCA_FLOWER_KEY_ENC_OPTS_VXLAN | NLA_F_NESTED);
359b1d
+
359b1d
+	err = get_u32(&gbp, str, 0);
359b1d
+	if (err)
359b1d
+		return err;
359b1d
+	addattr32(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, gbp);
359b1d
+
359b1d
+	addattr_nest_end(n, nest);
359b1d
+
359b1d
+	return 0;
359b1d
+}
359b1d
+
359b1d
+static int flower_parse_geneve_opts(char *str, struct nlmsghdr *n)
359b1d
 {
359b1d
 	char *token;
359b1d
 	int err;
359b1d
 
359b1d
 	token = strsep(&str, ",");
359b1d
 	while (token) {
359b1d
-		err = flower_parse_geneve_opts(token, n);
359b1d
+		err = flower_parse_geneve_opt(token, n);
359b1d
 		if (err)
359b1d
 			return err;
359b1d
 
359b1d
@@ -755,7 +775,7 @@ static int flower_check_enc_opt_key(char *key)
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
-static int flower_parse_enc_opts(char *str, struct nlmsghdr *n)
359b1d
+static int flower_parse_enc_opts_geneve(char *str, struct nlmsghdr *n)
359b1d
 {
359b1d
 	char key[XATTR_SIZE_MAX], mask[XATTR_SIZE_MAX];
359b1d
 	int data_len, key_len, mask_len, err;
359b1d
@@ -807,13 +827,50 @@ static int flower_parse_enc_opts(char *str, struct nlmsghdr *n)
359b1d
 	mask[mask_len - 1] = '\0';
359b1d
 
359b1d
 	nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS);
359b1d
-	err = flower_parse_enc_opt_part(key, n);
359b1d
+	err = flower_parse_geneve_opts(key, n);
359b1d
 	if (err)
359b1d
 		return err;
359b1d
 	addattr_nest_end(n, nest);
359b1d
 
359b1d
 	nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS_MASK);
359b1d
-	err = flower_parse_enc_opt_part(mask, n);
359b1d
+	err = flower_parse_geneve_opts(mask, n);
359b1d
+	if (err)
359b1d
+		return err;
359b1d
+	addattr_nest_end(n, nest);
359b1d
+
359b1d
+	return 0;
359b1d
+}
359b1d
+
359b1d
+static int flower_parse_enc_opts_vxlan(char *str, struct nlmsghdr *n)
359b1d
+{
359b1d
+	char key[XATTR_SIZE_MAX], mask[XATTR_SIZE_MAX];
359b1d
+	struct rtattr *nest;
359b1d
+	char *slash;
359b1d
+	int err;
359b1d
+
359b1d
+	slash = strchr(str, '/');
359b1d
+	if (slash) {
359b1d
+		*slash++ = '\0';
359b1d
+		if (strlen(slash) > XATTR_SIZE_MAX)
359b1d
+			return -1;
359b1d
+		strcpy(mask, slash);
359b1d
+	} else {
359b1d
+		strcpy(mask, "0xffffffff");
359b1d
+	}
359b1d
+
359b1d
+	if (strlen(str) > XATTR_SIZE_MAX)
359b1d
+		return -1;
359b1d
+	strcpy(key, str);
359b1d
+
359b1d
+	nest = addattr_nest(n, MAX_MSG, TCA_FLOWER_KEY_ENC_OPTS | NLA_F_NESTED);
359b1d
+	err = flower_parse_vxlan_opt(str, n);
359b1d
+	if (err)
359b1d
+		return err;
359b1d
+	addattr_nest_end(n, nest);
359b1d
+
359b1d
+	nest = addattr_nest(n, MAX_MSG,
359b1d
+			    TCA_FLOWER_KEY_ENC_OPTS_MASK | NLA_F_NESTED);
359b1d
+	err = flower_parse_vxlan_opt(mask, n);
359b1d
 	if (err)
359b1d
 		return err;
359b1d
 	addattr_nest_end(n, nest);
359b1d
@@ -1275,11 +1332,18 @@ static int flower_parse_opt(struct filter_util *qu, char *handle,
359b1d
 			}
359b1d
 		} else if (matches(*argv, "geneve_opts") == 0) {
359b1d
 			NEXT_ARG();
359b1d
-			ret = flower_parse_enc_opts(*argv, n);
359b1d
+			ret = flower_parse_enc_opts_geneve(*argv, n);
359b1d
 			if (ret < 0) {
359b1d
 				fprintf(stderr, "Illegal \"geneve_opts\"\n");
359b1d
 				return -1;
359b1d
 			}
359b1d
+		} else if (matches(*argv, "vxlan_opts") == 0) {
359b1d
+			NEXT_ARG();
359b1d
+			ret = flower_parse_enc_opts_vxlan(*argv, n);
359b1d
+			if (ret < 0) {
359b1d
+				fprintf(stderr, "Illegal \"vxlan_opts\"\n");
359b1d
+				return -1;
359b1d
+			}
359b1d
 		} else if (matches(*argv, "action") == 0) {
359b1d
 			NEXT_ARG();
359b1d
 			ret = parse_action(&argc, &argv, TCA_FLOWER_ACT, n);
359b1d
@@ -1643,10 +1707,29 @@ static void flower_print_geneve_opts(const char *name, struct rtattr *attr,
359b1d
 	close_json_array(PRINT_JSON, name);
359b1d
 }
359b1d
 
359b1d
-static void flower_print_geneve_parts(const char *name, struct rtattr *attr,
359b1d
-				      char *key, char *mask)
359b1d
+static void flower_print_vxlan_opts(const char *name, struct rtattr *attr,
359b1d
+				    char *strbuf)
359b1d
+{
359b1d
+	struct rtattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
359b1d
+	struct rtattr *i = RTA_DATA(attr);
359b1d
+	int rem = RTA_PAYLOAD(attr);
359b1d
+	__u32 gbp;
359b1d
+
359b1d
+	parse_rtattr(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, i, rem);
359b1d
+	gbp = rta_getattr_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
359b1d
+
359b1d
+	open_json_array(PRINT_JSON, name);
359b1d
+	open_json_object(NULL);
359b1d
+	print_uint(PRINT_JSON, "gbp", NULL, gbp);
359b1d
+	close_json_object();
359b1d
+	close_json_array(PRINT_JSON, name);
359b1d
+
359b1d
+	sprintf(strbuf, "%u", gbp);
359b1d
+}
359b1d
+
359b1d
+static void flower_print_enc_parts(const char *name, const char *namefrm,
359b1d
+				   struct rtattr *attr, char *key, char *mask)
359b1d
 {
359b1d
-	char *namefrm = "\n  geneve_opt %s";
359b1d
 	char *key_token, *mask_token, *out;
359b1d
 	int len;
359b1d
 
359b1d
@@ -1687,14 +1770,29 @@ static void flower_print_enc_opts(const char *name, struct rtattr *attr,
359b1d
 		goto err_key_free;
359b1d
 
359b1d
 	parse_rtattr_nested(key_tb, TCA_FLOWER_KEY_ENC_OPTS_MAX, attr);
359b1d
-	flower_print_geneve_opts("geneve_opt_key",
359b1d
-				 key_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], key);
359b1d
-
359b1d
 	parse_rtattr_nested(msk_tb, TCA_FLOWER_KEY_ENC_OPTS_MAX, mask_attr);
359b1d
-	flower_print_geneve_opts("geneve_opt_mask",
359b1d
-				 msk_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], msk);
359b1d
 
359b1d
-	flower_print_geneve_parts(name, attr, key, msk);
359b1d
+	if (key_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE]) {
359b1d
+		flower_print_geneve_opts("geneve_opt_key",
359b1d
+				key_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], key);
359b1d
+
359b1d
+		if (msk_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE])
359b1d
+			flower_print_geneve_opts("geneve_opt_mask",
359b1d
+				msk_tb[TCA_FLOWER_KEY_ENC_OPTS_GENEVE], msk);
359b1d
+
359b1d
+		flower_print_enc_parts(name, "  geneve_opts %s", attr, key,
359b1d
+				       msk);
359b1d
+	} else if (key_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN]) {
359b1d
+		flower_print_vxlan_opts("vxlan_opt_key",
359b1d
+				key_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN], key);
359b1d
+
359b1d
+		if (msk_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN])
359b1d
+			flower_print_vxlan_opts("vxlan_opt_mask",
359b1d
+				msk_tb[TCA_FLOWER_KEY_ENC_OPTS_VXLAN], msk);
359b1d
+
359b1d
+		flower_print_enc_parts(name, "  vxlan_opts %s", attr, key,
359b1d
+				       msk);
359b1d
+	}
359b1d
 
359b1d
 	free(msk);
359b1d
 err_key_free:
359b1d
-- 
359b1d
2.26.2
359b1d