naccyde / rpms / iproute

Forked from rpms/iproute 9 months ago
Clone

Blame SOURCES/0030-tc-f_flower-add-options-support-for-erspan.patch

359b1d
From 6784a916b142c3bd5cf7c20a30b23e362bd4908a Mon Sep 17 00:00:00 2001
359b1d
From: Andrea Claudi <aclaudi@redhat.com>
359b1d
Date: Thu, 4 Jun 2020 21:44:22 +0200
359b1d
Subject: [PATCH] tc: f_flower: add options support for erspan
359b1d
359b1d
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1830485
359b1d
Upstream Status: unknown commit 4e578c78fedfe
359b1d
359b1d
commit 4e578c78fedfe6ffa5fa5fde56778b264485829b
359b1d
Author: Xin Long <lucien.xin@gmail.com>
359b1d
Date:   Mon Apr 27 18:27:51 2020 +0800
359b1d
359b1d
    tc: f_flower: add options support for erspan
359b1d
359b1d
    This patch is to add TCA_FLOWER_KEY_ENC_OPTS_ERSPAN's parse and
359b1d
    print to implement erspan 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 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 erspan1 ingress
359b1d
      # tc filter add dev erspan1 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
            erspan_opts 1:2:0:0/1:255:0:0 \
359b1d
            ip_proto udp \
359b1d
            action mirred egress redirect dev eth1
359b1d
      # tc -s filter show dev erspan1 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
           erspan_opts 1:2:0:0/1:255:0:0
359b1d
           not_in_hw
359b1d
             action order 1: mirred (Egress Redirect to device eth1) stolen
359b1d
             index 1 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
      - 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-flower.8 |  13 ++++
359b1d
 tc/f_flower.c        | 171 +++++++++++++++++++++++++++++++++++++++++++
359b1d
 2 files changed, 184 insertions(+)
359b1d
359b1d
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
359b1d
index 0efacbfdf9a95..f41b0f7f1ef13 100644
359b1d
--- a/man/man8/tc-flower.8
359b1d
+++ b/man/man8/tc-flower.8
359b1d
@@ -85,6 +85,8 @@ flower \- flow based traffic control filter
359b1d
 .B geneve_opts
359b1d
 |
359b1d
 .B vxlan_opts
359b1d
+|
359b1d
+.B erspan_opts
359b1d
 }
359b1d
 .IR OPTIONS " | "
359b1d
 .BR ip_flags
359b1d
@@ -296,6 +298,8 @@ bits is assumed.
359b1d
 .BI geneve_opts " OPTIONS"
359b1d
 .TQ
359b1d
 .BI vxlan_opts " OPTIONS"
359b1d
+.TQ
359b1d
+.BI erspan_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
@@ -322,6 +326,15 @@ 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
+erspan_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
359b1d
+VERSION:INDEX:DIR:HWID/VERSION:INDEX_MASK:DIR_MASK:HWID_MASK, where VERSION is
359b1d
+represented as a 8bit number, INDEX as an 32bit number, DIR and HWID as a 8bit
359b1d
+number. Multiple options is not supported. Note INDEX/INDEX_MASK is used when
359b1d
+VERSION is 1, and DIR/DIR_MASK and HWID/HWID_MASK are used when VERSION is 2.
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 09079cd2c2280..691541ec59d4c 100644
359b1d
--- a/tc/f_flower.c
359b1d
+++ b/tc/f_flower.c
359b1d
@@ -82,6 +82,7 @@ static void explain(void)
359b1d
 		"			enc_ttl MASKED-IP_TTL |\n"
359b1d
 		"			geneve_opts MASKED-OPTIONS |\n"
359b1d
 		"			vxlan_opts MASKED-OPTIONS |\n"
359b1d
+		"                       erspan_opts MASKED-OPTIONS |\n"
359b1d
 		"			ip_flags IP-FLAGS | \n"
359b1d
 		"			enc_dst_port [ port_number ] }\n"
359b1d
 		"	FILTERID := X:Y:Z\n"
359b1d
@@ -738,6 +739,84 @@ static int flower_parse_vxlan_opt(char *str, struct nlmsghdr *n)
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
+static int flower_parse_erspan_opt(char *str, struct nlmsghdr *n)
359b1d
+{
359b1d
+	struct rtattr *nest;
359b1d
+	char *token;
359b1d
+	int i, err;
359b1d
+
359b1d
+	nest = addattr_nest(n, MAX_MSG,
359b1d
+			    TCA_FLOWER_KEY_ENC_OPTS_ERSPAN | NLA_F_NESTED);
359b1d
+
359b1d
+	i = 1;
359b1d
+	token = strsep(&str, ":");
359b1d
+	while (token) {
359b1d
+		switch (i) {
359b1d
+		case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER:
359b1d
+		{
359b1d
+			__u8 opt_type;
359b1d
+
359b1d
+			if (!strlen(token))
359b1d
+				break;
359b1d
+			err = get_u8(&opt_type, token, 0);
359b1d
+			if (err)
359b1d
+				return err;
359b1d
+
359b1d
+			addattr8(n, MAX_MSG, i, opt_type);
359b1d
+			break;
359b1d
+		}
359b1d
+		case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX:
359b1d
+		{
359b1d
+			__be32 opt_index;
359b1d
+
359b1d
+			if (!strlen(token))
359b1d
+				break;
359b1d
+			err = get_be32(&opt_index, token, 0);
359b1d
+			if (err)
359b1d
+				return err;
359b1d
+
359b1d
+			addattr32(n, MAX_MSG, i, opt_index);
359b1d
+			break;
359b1d
+		}
359b1d
+		case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR:
359b1d
+		{
359b1d
+			__u8 opt_type;
359b1d
+
359b1d
+			if (!strlen(token))
359b1d
+				break;
359b1d
+			err = get_u8(&opt_type, token, 0);
359b1d
+			if (err)
359b1d
+				return err;
359b1d
+
359b1d
+			addattr8(n, MAX_MSG, i, opt_type);
359b1d
+			break;
359b1d
+		}
359b1d
+		case TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID:
359b1d
+		{
359b1d
+			__u8 opt_type;
359b1d
+
359b1d
+			if (!strlen(token))
359b1d
+				break;
359b1d
+			err = get_u8(&opt_type, token, 0);
359b1d
+			if (err)
359b1d
+				return err;
359b1d
+
359b1d
+			addattr8(n, MAX_MSG, i, opt_type);
359b1d
+			break;
359b1d
+		}
359b1d
+		default:
359b1d
+			fprintf(stderr, "Unknown \"geneve_opts\" type\n");
359b1d
+			return -1;
359b1d
+		}
359b1d
+
359b1d
+		token = strsep(&str, ":");
359b1d
+		i++;
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
@@ -878,6 +957,49 @@ static int flower_parse_enc_opts_vxlan(char *str, struct nlmsghdr *n)
359b1d
 	return 0;
359b1d
 }
359b1d
 
359b1d
+static int flower_parse_enc_opts_erspan(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
+
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
+		int index;
359b1d
+
359b1d
+		slash = strchr(str, ':');
359b1d
+		index = (int)(slash - str);
359b1d
+		memcpy(mask, str, index);
359b1d
+		strcpy(mask + index, ":0xffffffff:0xff:0xff");
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_erspan_opt(key, 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_erspan_opt(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_opt(struct filter_util *qu, char *handle,
359b1d
 			    int argc, char **argv, struct nlmsghdr *n)
359b1d
 {
359b1d
@@ -1344,6 +1466,13 @@ static int flower_parse_opt(struct filter_util *qu, char *handle,
359b1d
 				fprintf(stderr, "Illegal \"vxlan_opts\"\n");
359b1d
 				return -1;
359b1d
 			}
359b1d
+		} else if (matches(*argv, "erspan_opts") == 0) {
359b1d
+			NEXT_ARG();
359b1d
+			ret = flower_parse_enc_opts_erspan(*argv, n);
359b1d
+			if (ret < 0) {
359b1d
+				fprintf(stderr, "Illegal \"erspan_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
@@ -1727,6 +1856,38 @@ static void flower_print_vxlan_opts(const char *name, struct rtattr *attr,
359b1d
 	sprintf(strbuf, "%u", gbp);
359b1d
 }
359b1d
 
359b1d
+static void flower_print_erspan_opts(const char *name, struct rtattr *attr,
359b1d
+				     char *strbuf)
359b1d
+{
359b1d
+	struct rtattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
359b1d
+	__u8 ver, hwid, dir;
359b1d
+	__u32 idx;
359b1d
+
359b1d
+	parse_rtattr(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, RTA_DATA(attr),
359b1d
+		     RTA_PAYLOAD(attr));
359b1d
+	ver = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
359b1d
+	if (ver == 1) {
359b1d
+		idx = rta_getattr_be32(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]);
359b1d
+		hwid = 0;
359b1d
+		dir = 0;
359b1d
+	} else {
359b1d
+		idx = 0;
359b1d
+		hwid = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]);
359b1d
+		dir = rta_getattr_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]);
359b1d
+	}
359b1d
+
359b1d
+	open_json_array(PRINT_JSON, name);
359b1d
+	open_json_object(NULL);
359b1d
+	print_uint(PRINT_JSON, "ver", NULL, ver);
359b1d
+	print_uint(PRINT_JSON, "index", NULL, idx);
359b1d
+	print_uint(PRINT_JSON, "dir", NULL, dir);
359b1d
+	print_uint(PRINT_JSON, "hwid", NULL, hwid);
359b1d
+	close_json_object();
359b1d
+	close_json_array(PRINT_JSON, name);
359b1d
+
359b1d
+	sprintf(strbuf, "%u:%u:%u:%u", ver, idx, dir, hwid);
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
@@ -1792,6 +1953,16 @@ static void flower_print_enc_opts(const char *name, struct rtattr *attr,
359b1d
 
359b1d
 		flower_print_enc_parts(name, "  vxlan_opts %s", attr, key,
359b1d
 				       msk);
359b1d
+	} else if (key_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN]) {
359b1d
+		flower_print_erspan_opts("erspan_opt_key",
359b1d
+				key_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN], key);
359b1d
+
359b1d
+		if (msk_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN])
359b1d
+			flower_print_erspan_opts("erspan_opt_mask",
359b1d
+				msk_tb[TCA_FLOWER_KEY_ENC_OPTS_ERSPAN], msk);
359b1d
+
359b1d
+		flower_print_enc_parts(name, "  erspan_opts %s", attr, key,
359b1d
+				       msk);
359b1d
 	}
359b1d
 
359b1d
 	free(msk);
359b1d
-- 
359b1d
2.26.2
359b1d