naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone

Blame SOURCES/0028-tc-m_tunnel_key-add-options-support-for-erpsan.patch

359b1d
From 2fb8c115a8031d893fff588181cc42764391e4d5 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: m_tunnel_key: add options support for erpsan
359b1d
359b1d
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1830485
359b1d
Upstream Status: unknown commit 668fd9b25d9ec
359b1d
359b1d
commit 668fd9b25d9eca3067040273239f7825db95442b
359b1d
Author: Xin Long <lucien.xin@gmail.com>
359b1d
Date:   Mon Apr 27 18:27:49 2020 +0800
359b1d
359b1d
    tc: m_tunnel_key: add options support for erpsan
359b1d
359b1d
    This patch is to add TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN's parse and
359b1d
    print to implement erspan options support in m_tunnel_key, like
359b1d
    Commit 6217917a3826 ("tc: m_tunnel_key: Add tunnel option support
359b1d
    to act_tunnel_key") for geneve options support.
359b1d
359b1d
    Option is expressed as version:index:dir:hwid, dir and hwid will
359b1d
    be parsed when version is 2, while index will be parsed when
359b1d
    version is 1. erspan doesn't support multiple options.
359b1d
359b1d
    With this patch, users can add and dump erspan options like:
359b1d
359b1d
      # ip link add name erspan1 type erspan external
359b1d
      # tc qdisc add dev eth0 ingress
359b1d
      # tc filter add dev eth0 protocol ip parent ffff: \
359b1d
          flower indev eth0 \
359b1d
            ip_proto udp \
359b1d
            action tunnel_key \
359b1d
              set src_ip 10.0.99.192 \
359b1d
              dst_ip 10.0.99.193 \
359b1d
              dst_port 6081 \
359b1d
              id 11 \
359b1d
              erspan_opts 1:2:0:0 \
359b1d
          action mirred egress redirect dev erspan1
359b1d
      # tc -s filter show dev eth0 parent ffff:
359b1d
359b1d
         filter protocol ip pref 49151 flower chain 0 handle 0x1
359b1d
           indev eth0
359b1d
           eth_type ipv4
359b1d
           ip_proto udp
359b1d
           not_in_hw
359b1d
             action order 1: tunnel_key  set
359b1d
             src_ip 10.0.99.192
359b1d
             dst_ip 10.0.99.193
359b1d
             key_id 11
359b1d
             dst_port 6081
359b1d
             erspan_opts 1:2:0:0
359b1d
             csum pipe
359b1d
               index 2 ref 1 bind 1
359b1d
             ...
359b1d
    v1->v2:
359b1d
      - no change.
359b1d
    v2->v3:
359b1d
      - no change.
359b1d
    v3->v4:
359b1d
      - keep the same format between input and output, json and non json.
359b1d
      - print version, index, dir and hwid 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-tunnel_key.8 |  12 +++-
359b1d
 tc/m_tunnel_key.c        | 117 ++++++++++++++++++++++++++++++++++++++-
359b1d
 2 files changed, 127 insertions(+), 2 deletions(-)
359b1d
359b1d
diff --git a/man/man8/tc-tunnel_key.8 b/man/man8/tc-tunnel_key.8
359b1d
index c208e2c82a181..ad9972402c0e5 100644
359b1d
--- a/man/man8/tc-tunnel_key.8
359b1d
+++ b/man/man8/tc-tunnel_key.8
359b1d
@@ -68,8 +68,10 @@ options.
359b1d
 .B dst_port
359b1d
 ,
359b1d
 .B geneve_opts
359b1d
-and
359b1d
+,
359b1d
 .B vxlan_opts
359b1d
+and
359b1d
+.B erspan_opts
359b1d
 are optional.
359b1d
 .RS
359b1d
 .TP
359b1d
@@ -99,6 +101,14 @@ Vxlan metatdata options.
359b1d
 is specified in the form GBP, as a 32bit number. Multiple options is not
359b1d
 supported.
359b1d
 .TP
359b1d
+.B erspan_opts
359b1d
+Erspan metatdata options.
359b1d
+.B erspan_opts
359b1d
+is specified in the form VERSION:INDEX:DIR:HWID, where VERSION is represented
359b1d
+as a 8bit number, INDEX as an 32bit number, DIR and HWID as a 8bit number.
359b1d
+Multiple options is not supported. Note INDEX is used when VERSION is 1,
359b1d
+and DIR and HWID are used when VERSION is 2.
359b1d
+.TP
359b1d
 .B tos
359b1d
 Outer header TOS
359b1d
 .TP
359b1d
diff --git a/tc/m_tunnel_key.c b/tc/m_tunnel_key.c
359b1d
index 76391d6c85fb2..a56fe24413fa0 100644
359b1d
--- a/tc/m_tunnel_key.c
359b1d
+++ b/tc/m_tunnel_key.c
359b1d
@@ -29,7 +29,7 @@ static void explain(void)
359b1d
 		"src_ip <IP> (mandatory)\n"
359b1d
 		"dst_ip <IP> (mandatory)\n"
359b1d
 		"dst_port <UDP_PORT>\n"
359b1d
-		"geneve_opts | vxlan_opts <OPTIONS>\n"
359b1d
+		"geneve_opts | vxlan_opts | erspan_opts <OPTIONS>\n"
359b1d
 		"csum | nocsum (default is \"csum\")\n");
359b1d
 }
359b1d
 
359b1d
@@ -97,6 +97,21 @@ static int tunnel_key_parse_be16(char *str, int base, int type,
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
+static int tunnel_key_parse_be32(char *str, int base, int type,
359b1d
+				 struct nlmsghdr *n)
359b1d
+{
359b1d
+	__be32 value;
359b1d
+	int ret;
359b1d
+
359b1d
+	ret = get_be32(&value, str, base);
359b1d
+	if (ret)
359b1d
+		return ret;
359b1d
+
359b1d
+	addattr32(n, MAX_MSG, type, value);
359b1d
+
359b1d
+	return 0;
359b1d
+}
359b1d
+
359b1d
 static int tunnel_key_parse_u8(char *str, int base, int type,
359b1d
 			       struct nlmsghdr *n)
359b1d
 {
359b1d
@@ -226,6 +241,63 @@ static int tunnel_key_parse_vxlan_opt(char *str, struct nlmsghdr *n)
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
+static int tunnel_key_parse_erspan_opt(char *str, struct nlmsghdr *n)
359b1d
+{
359b1d
+	char *token, *saveptr = NULL;
359b1d
+	struct rtattr *encap, *nest;
359b1d
+	int i, ret;
359b1d
+
359b1d
+	encap = addattr_nest(n, MAX_MSG,
359b1d
+			     TCA_TUNNEL_KEY_ENC_OPTS | NLA_F_NESTED);
359b1d
+	nest = addattr_nest(n, MAX_MSG,
359b1d
+			    TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN | NLA_F_NESTED);
359b1d
+
359b1d
+	token = strtok_r(str, ":", &saveptr);
359b1d
+	i = 1;
359b1d
+	while (token) {
359b1d
+		switch (i) {
359b1d
+		case TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER:
359b1d
+		{
359b1d
+			ret = tunnel_key_parse_u8(token, 0, i, n);
359b1d
+			if (ret)
359b1d
+				return ret;
359b1d
+			break;
359b1d
+		}
359b1d
+		case TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX:
359b1d
+		{
359b1d
+			ret = tunnel_key_parse_be32(token, 0, i, n);
359b1d
+			if (ret)
359b1d
+				return ret;
359b1d
+			break;
359b1d
+		}
359b1d
+		case TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR:
359b1d
+		{
359b1d
+			ret = tunnel_key_parse_u8(token, 0, i, n);
359b1d
+			if (ret)
359b1d
+				return ret;
359b1d
+			break;
359b1d
+		}
359b1d
+		case TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID:
359b1d
+		{
359b1d
+			ret = tunnel_key_parse_u8(token, 0, i, n);
359b1d
+			if (ret)
359b1d
+				return ret;
359b1d
+			break;
359b1d
+		}
359b1d
+		default:
359b1d
+			return -1;
359b1d
+		}
359b1d
+
359b1d
+		token = strtok_r(NULL, ":", &saveptr);
359b1d
+		i++;
359b1d
+	}
359b1d
+
359b1d
+	addattr_nest_end(n, nest);
359b1d
+	addattr_nest_end(n, encap);
359b1d
+
359b1d
+	return 0;
359b1d
+}
359b1d
+
359b1d
 static int tunnel_key_parse_tos_ttl(char *str, int type, struct nlmsghdr *n)
359b1d
 {
359b1d
 	int ret;
359b1d
@@ -330,6 +402,13 @@ static int parse_tunnel_key(struct action_util *a, int *argc_p, char ***argv_p,
359b1d
 				fprintf(stderr, "Illegal \"vxlan_opts\"\n");
359b1d
 				return -1;
359b1d
 			}
359b1d
+		} else if (matches(*argv, "erspan_opts") == 0) {
359b1d
+			NEXT_ARG();
359b1d
+
359b1d
+			if (tunnel_key_parse_erspan_opt(*argv, n)) {
359b1d
+				fprintf(stderr, "Illegal \"erspan_opts\"\n");
359b1d
+				return -1;
359b1d
+			}
359b1d
 		} else if (matches(*argv, "tos") == 0) {
359b1d
 			NEXT_ARG();
359b1d
 			ret = tunnel_key_parse_tos_ttl(*argv,
359b1d
@@ -517,6 +596,39 @@ static void tunnel_key_print_vxlan_options(struct rtattr *attr)
359b1d
 	close_json_array(PRINT_JSON, name);
359b1d
 }
359b1d
 
359b1d
+static void tunnel_key_print_erspan_options(struct rtattr *attr)
359b1d
+{
359b1d
+	struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1];
359b1d
+	struct rtattr *i = RTA_DATA(attr);
359b1d
+	int rem = RTA_PAYLOAD(attr);
359b1d
+	char *name = "erspan_opts";
359b1d
+	__u8 ver, hwid, dir;
359b1d
+	__u32 idx;
359b1d
+
359b1d
+	parse_rtattr(tb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX, i, rem);
359b1d
+	ver = rta_getattr_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]);
359b1d
+	if (ver == 1) {
359b1d
+		idx = rta_getattr_be32(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX]);
359b1d
+		dir = 0;
359b1d
+		hwid = 0;
359b1d
+	} else {
359b1d
+		idx = 0;
359b1d
+		dir = rta_getattr_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR]);
359b1d
+		hwid = rta_getattr_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID]);
359b1d
+	}
359b1d
+
359b1d
+	print_nl();
359b1d
+	print_string(PRINT_FP, name, "\t%s ", name);
359b1d
+	open_json_array(PRINT_JSON, name);
359b1d
+	open_json_object(NULL);
359b1d
+	print_uint(PRINT_ANY, "ver", "%u", ver);
359b1d
+	print_uint(PRINT_ANY, "index", ":%u", idx);
359b1d
+	print_uint(PRINT_ANY, "dir", ":%u", dir);
359b1d
+	print_uint(PRINT_ANY, "hwid", ":%u", hwid);
359b1d
+	close_json_object();
359b1d
+	close_json_array(PRINT_JSON, name);
359b1d
+}
359b1d
+
359b1d
 static void tunnel_key_print_key_opt(struct rtattr *attr)
359b1d
 {
359b1d
 	struct rtattr *tb[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1];
359b1d
@@ -531,6 +643,9 @@ static void tunnel_key_print_key_opt(struct rtattr *attr)
359b1d
 	else if (tb[TCA_TUNNEL_KEY_ENC_OPTS_VXLAN])
359b1d
 		tunnel_key_print_vxlan_options(
359b1d
 			tb[TCA_TUNNEL_KEY_ENC_OPTS_VXLAN]);
359b1d
+	else if (tb[TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN])
359b1d
+		tunnel_key_print_erspan_options(
359b1d
+			tb[TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN]);
359b1d
 }
359b1d
 
359b1d
 static void tunnel_key_print_tos_ttl(FILE *f, char *name,
359b1d
-- 
359b1d
2.26.2
359b1d