naccyde / rpms / iproute

Forked from rpms/iproute 8 months ago
Clone

Blame SOURCES/0027-tc-m_tunnel_key-add-options-support-for-vxlan.patch

07a51b
From f970b592af7421ce932a788a6a14161c351d75e0 Mon Sep 17 00:00:00 2001
07a51b
From: Andrea Claudi <aclaudi@redhat.com>
07a51b
Date: Thu, 4 Jun 2020 21:43:01 +0200
07a51b
Subject: [PATCH] tc: m_tunnel_key: add options support for vxlan
07a51b
07a51b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1830485
07a51b
Upstream Status: unknown commit f72c3ad00f3b7
07a51b
Conflicts: context change due to missing commit 7b0d424abef16
07a51b
           ("tc: do not output newline in oneline mode")
07a51b
07a51b
commit f72c3ad00f3b7869e90840d0098a83cb88224892
07a51b
Author: Xin Long <lucien.xin@gmail.com>
07a51b
Date:   Mon Apr 27 18:27:48 2020 +0800
07a51b
07a51b
    tc: m_tunnel_key: add options support for vxlan
07a51b
07a51b
    This patch is to add TCA_TUNNEL_KEY_ENC_OPTS_VXLAN's parse and
07a51b
    print to implement vxlan options support in m_tunnel_key, like
07a51b
    Commit 6217917a3826 ("tc: m_tunnel_key: Add tunnel option support
07a51b
    to act_tunnel_key") for geneve options support.
07a51b
07a51b
    Option is expressed a 32bit number for gbp only, and vxlan
07a51b
    doesn't support multiple options.
07a51b
07a51b
    With this patch, users can add and dump vxlan options like:
07a51b
07a51b
      # ip link add name vxlan1 type vxlan dstport 0 external
07a51b
      # tc qdisc add dev eth0 ingress
07a51b
      # tc filter add dev eth0 protocol ip parent ffff: \
07a51b
          flower indev eth0 \
07a51b
            ip_proto udp \
07a51b
            action tunnel_key \
07a51b
              set src_ip 10.0.99.192 \
07a51b
              dst_ip 10.0.99.193 \
07a51b
              dst_port 6081 \
07a51b
              id 11 \
07a51b
              vxlan_opts 65793 \
07a51b
          action mirred egress redirect dev vxlan1
07a51b
      # tc -s filter show dev eth0 parent ffff:
07a51b
07a51b
         filter protocol ip pref 49152 flower chain 0 handle 0x1
07a51b
           indev eth0
07a51b
           eth_type ipv4
07a51b
           ip_proto udp
07a51b
           not_in_hw
07a51b
             action order 1: tunnel_key  set
07a51b
             src_ip 10.0.99.192
07a51b
             dst_ip 10.0.99.193
07a51b
             key_id 11
07a51b
             dst_port 6081
07a51b
             vxlan_opts 65793
07a51b
             ...
07a51b
07a51b
    v1->v2:
07a51b
      - get_u32 with base = 0 for gbp.
07a51b
      - use to print_unint("0x%x") to print gbp.
07a51b
    v2->v3:
07a51b
      - implement proper JSON array for opts.
07a51b
    v3->v4:
07a51b
      - keep the same format between input and output, json and non json.
07a51b
      - print gbp as uint.
07a51b
07a51b
    Signed-off-by: Xin Long <lucien.xin@gmail.com>
07a51b
    Signed-off-by: David Ahern <dsahern@gmail.com>
07a51b
---
07a51b
 man/man8/tc-tunnel_key.8 | 10 ++++-
07a51b
 tc/m_tunnel_key.c        | 85 +++++++++++++++++++++++++++++++++++-----
07a51b
 2 files changed, 85 insertions(+), 10 deletions(-)
07a51b
07a51b
diff --git a/man/man8/tc-tunnel_key.8 b/man/man8/tc-tunnel_key.8
07a51b
index 2145eb62e70e2..c208e2c82a181 100644
07a51b
--- a/man/man8/tc-tunnel_key.8
07a51b
+++ b/man/man8/tc-tunnel_key.8
07a51b
@@ -66,8 +66,10 @@ options.
07a51b
 .B id
07a51b
 ,
07a51b
 .B dst_port
07a51b
-and
07a51b
+,
07a51b
 .B geneve_opts
07a51b
+and
07a51b
+.B vxlan_opts
07a51b
 are optional.
07a51b
 .RS
07a51b
 .TP
07a51b
@@ -91,6 +93,12 @@ is specified in the form CLASS:TYPE:DATA, where CLASS is represented as a
07a51b
 variable length hexadecimal value. Additionally multiple options may be
07a51b
 listed using a comma delimiter.
07a51b
 .TP
07a51b
+.B vxlan_opts
07a51b
+Vxlan metatdata options.
07a51b
+.B vxlan_opts
07a51b
+is specified in the form GBP, as a 32bit number. Multiple options is not
07a51b
+supported.
07a51b
+.TP
07a51b
 .B tos
07a51b
 Outer header TOS
07a51b
 .TP
07a51b
diff --git a/tc/m_tunnel_key.c b/tc/m_tunnel_key.c
07a51b
index 4e65e444776a2..76391d6c85fb2 100644
07a51b
--- a/tc/m_tunnel_key.c
07a51b
+++ b/tc/m_tunnel_key.c
07a51b
@@ -29,7 +29,7 @@ static void explain(void)
07a51b
 		"src_ip <IP> (mandatory)\n"
07a51b
 		"dst_ip <IP> (mandatory)\n"
07a51b
 		"dst_port <UDP_PORT>\n"
07a51b
-		"geneve_opts <OPTIONS>\n"
07a51b
+		"geneve_opts | vxlan_opts <OPTIONS>\n"
07a51b
 		"csum | nocsum (default is \"csum\")\n");
07a51b
 }
07a51b
 
07a51b
@@ -112,6 +112,21 @@ static int tunnel_key_parse_u8(char *str, int base, int type,
07a51b
 	return 0;
07a51b
 }
07a51b
 
07a51b
+static int tunnel_key_parse_u32(char *str, int base, int type,
07a51b
+				struct nlmsghdr *n)
07a51b
+{
07a51b
+	__u32 value;
07a51b
+	int ret;
07a51b
+
07a51b
+	ret = get_u32(&value, str, base);
07a51b
+	if (ret)
07a51b
+		return ret;
07a51b
+
07a51b
+	addattr32(n, MAX_MSG, type, value);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
 static int tunnel_key_parse_geneve_opt(char *str, struct nlmsghdr *n)
07a51b
 {
07a51b
 	char *token, *saveptr = NULL;
07a51b
@@ -190,6 +205,27 @@ static int tunnel_key_parse_geneve_opts(char *str, struct nlmsghdr *n)
07a51b
 	return 0;
07a51b
 }
07a51b
 
07a51b
+static int tunnel_key_parse_vxlan_opt(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	struct rtattr *encap, *nest;
07a51b
+	int ret;
07a51b
+
07a51b
+	encap = addattr_nest(n, MAX_MSG,
07a51b
+			     TCA_TUNNEL_KEY_ENC_OPTS | NLA_F_NESTED);
07a51b
+	nest = addattr_nest(n, MAX_MSG,
07a51b
+			    TCA_TUNNEL_KEY_ENC_OPTS_VXLAN | NLA_F_NESTED);
07a51b
+
07a51b
+	ret = tunnel_key_parse_u32(str, 0,
07a51b
+				   TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, n);
07a51b
+	if (ret)
07a51b
+		return ret;
07a51b
+
07a51b
+	addattr_nest_end(n, nest);
07a51b
+	addattr_nest_end(n, encap);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
 static int tunnel_key_parse_tos_ttl(char *str, int type, struct nlmsghdr *n)
07a51b
 {
07a51b
 	int ret;
07a51b
@@ -287,6 +323,13 @@ static int parse_tunnel_key(struct action_util *a, int *argc_p, char ***argv_p,
07a51b
 				fprintf(stderr, "Illegal \"geneve_opts\"\n");
07a51b
 				return -1;
07a51b
 			}
07a51b
+		} else if (matches(*argv, "vxlan_opts") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+
07a51b
+			if (tunnel_key_parse_vxlan_opt(*argv, n)) {
07a51b
+				fprintf(stderr, "Illegal \"vxlan_opts\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
 		} else if (matches(*argv, "tos") == 0) {
07a51b
 			NEXT_ARG();
07a51b
 			ret = tunnel_key_parse_tos_ttl(*argv,
07a51b
@@ -406,13 +449,13 @@ static void tunnel_key_print_flag(FILE *f, const char *name_on,
07a51b
 		     rta_getattr_u8(attr) ? name_on : name_off);
07a51b
 }
07a51b
 
07a51b
-static void tunnel_key_print_geneve_options(const char *name,
07a51b
-					    struct rtattr *attr)
07a51b
+static void tunnel_key_print_geneve_options(struct rtattr *attr)
07a51b
 {
07a51b
 	struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
07a51b
 	struct rtattr *i = RTA_DATA(attr);
07a51b
 	int ii, data_len = 0, offset = 0;
07a51b
 	int rem = RTA_PAYLOAD(attr);
07a51b
+	char *name = "geneve_opts";
07a51b
 	char strbuf[rem * 2 + 1];
07a51b
 	char data[rem * 2 + 1];
07a51b
 	uint8_t data_r[rem];
07a51b
@@ -420,7 +463,8 @@ static void tunnel_key_print_geneve_options(const char *name,
07a51b
 	uint8_t type;
07a51b
 
07a51b
 	open_json_array(PRINT_JSON, name);
07a51b
-	print_string(PRINT_FP, name, "\n\t%s ", "geneve_opt");
07a51b
+	print_nl();
07a51b
+	print_string(PRINT_FP, name, "\t%s ", name);
07a51b
 
07a51b
 	while (rem) {
07a51b
 		parse_rtattr(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX, i, rem);
07a51b
@@ -453,7 +497,27 @@ static void tunnel_key_print_geneve_options(const char *name,
07a51b
 	close_json_array(PRINT_JSON, name);
07a51b
 }
07a51b
 
07a51b
-static void tunnel_key_print_key_opt(const char *name, struct rtattr *attr)
07a51b
+static void tunnel_key_print_vxlan_options(struct rtattr *attr)
07a51b
+{
07a51b
+	struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1];
07a51b
+	struct rtattr *i = RTA_DATA(attr);
07a51b
+	int rem = RTA_PAYLOAD(attr);
07a51b
+	char *name = "vxlan_opts";
07a51b
+	__u32 gbp;
07a51b
+
07a51b
+	parse_rtattr(tb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX, i, rem);
07a51b
+	gbp = rta_getattr_u32(tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]);
07a51b
+
07a51b
+	print_nl();
07a51b
+	print_string(PRINT_FP, name, "\t%s ", name);
07a51b
+	open_json_array(PRINT_JSON, name);
07a51b
+	open_json_object(NULL);
07a51b
+	print_uint(PRINT_ANY, "gbp", "%u", gbp);
07a51b
+	close_json_object();
07a51b
+	close_json_array(PRINT_JSON, name);
07a51b
+}
07a51b
+
07a51b
+static void tunnel_key_print_key_opt(struct rtattr *attr)
07a51b
 {
07a51b
 	struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1];
07a51b
 
07a51b
@@ -461,8 +525,12 @@ static void tunnel_key_print_key_opt(const char *name, struct rtattr *attr)
07a51b
 		return;
07a51b
 
07a51b
 	parse_rtattr_nested(tb, TCA_TUNNEL_KEY_ENC_OPTS_MAX, attr);
07a51b
-	tunnel_key_print_geneve_options(name,
07a51b
-					tb[TCA_TUNNEL_KEY_ENC_OPTS_GENEVE]);
07a51b
+	if (tb[TCA_TUNNEL_KEY_ENC_OPTS_GENEVE])
07a51b
+		tunnel_key_print_geneve_options(
07a51b
+			tb[TCA_TUNNEL_KEY_ENC_OPTS_GENEVE]);
07a51b
+	else if (tb[TCA_TUNNEL_KEY_ENC_OPTS_VXLAN])
07a51b
+		tunnel_key_print_vxlan_options(
07a51b
+			tb[TCA_TUNNEL_KEY_ENC_OPTS_VXLAN]);
07a51b
 }
07a51b
 
07a51b
 static void tunnel_key_print_tos_ttl(FILE *f, char *name,
07a51b
@@ -518,8 +586,7 @@ static int print_tunnel_key(struct action_util *au, FILE *f, struct rtattr *arg)
07a51b
 					tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
07a51b
 		tunnel_key_print_dst_port(f, "dst_port",
07a51b
 					  tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
07a51b
-		tunnel_key_print_key_opt("geneve_opts",
07a51b
-					 tb[TCA_TUNNEL_KEY_ENC_OPTS]);
07a51b
+		tunnel_key_print_key_opt(tb[TCA_TUNNEL_KEY_ENC_OPTS]);
07a51b
 		tunnel_key_print_flag(f, "nocsum", "csum",
07a51b
 				      tb[TCA_TUNNEL_KEY_NO_CSUM]);
07a51b
 		tunnel_key_print_tos_ttl(f, "tos",
07a51b
-- 
07a51b
2.26.2
07a51b