naccyde / rpms / iproute

Forked from rpms/iproute a year ago
Clone

Blame SOURCES/0024-iproute_lwtunnel-add-options-support-for-geneve-meta.patch

0ac2f3
From c6f909bd0f40c58f39f857e1c57638cb41241fa2 Mon Sep 17 00:00:00 2001
0ac2f3
From: Andrea Claudi <aclaudi@redhat.com>
0ac2f3
Date: Thu, 4 Jun 2020 21:43:01 +0200
0ac2f3
Subject: [PATCH] iproute_lwtunnel: add options support for geneve metadata
0ac2f3
0ac2f3
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1830485
0ac2f3
Upstream Status: unknown commit ca7614d4c6f45
0ac2f3
0ac2f3
commit ca7614d4c6f456187d831a8202bb4a8559a72f8b
0ac2f3
Author: Xin Long <lucien.xin@gmail.com>
0ac2f3
Date:   Mon Apr 27 18:27:45 2020 +0800
0ac2f3
0ac2f3
    iproute_lwtunnel: add options support for geneve metadata
0ac2f3
0ac2f3
    This patch is to add LWTUNNEL_IP(6)_OPTS and LWTUNNEL_IP_OPTS_GENEVE's
0ac2f3
    parse and print to implement geneve options support in iproute_lwtunnel.
0ac2f3
0ac2f3
    Options are expressed as class:type:data and multiple options may be
0ac2f3
    listed using a comma delimiter, class and type are numbers and data
0ac2f3
    is a hex string.
0ac2f3
0ac2f3
    With this patch, users can add and dump geneve options like:
0ac2f3
0ac2f3
      # ip netns add a
0ac2f3
      # ip netns add b
0ac2f3
      # ip -n a link add eth0 type veth peer name eth0 netns b
0ac2f3
      # ip -n a link set eth0 up; ip -n b link set eth0 up
0ac2f3
      # ip -n a addr add 10.1.0.1/24 dev eth0
0ac2f3
      # ip -n b addr add 10.1.0.2/24 dev eth0
0ac2f3
      # ip -n b link add geneve1 type geneve id 1 remote 10.1.0.1 ttl 64
0ac2f3
      # ip -n b addr add 1.1.1.1/24 dev geneve1
0ac2f3
      # ip -n b link set geneve1 up
0ac2f3
      # ip -n b route add 2.1.1.0/24 dev geneve1
0ac2f3
      # ip -n a link add geneve1 type geneve external
0ac2f3
      # ip -n a addr add 2.1.1.1/24 dev geneve1
0ac2f3
      # ip -n a link set geneve1 up
0ac2f3
      # ip -n a route add 1.1.1.0/24 encap ip id 1 geneve_opts \
0ac2f3
        1:1:1212121234567890,1:1:1212121234567890,1:1:1212121234567890 \
0ac2f3
        dst 10.1.0.2 dev geneve1
0ac2f3
      # ip -n a route show
0ac2f3
      # ip netns exec a ping 1.1.1.1 -c 1
0ac2f3
0ac2f3
       1.1.1.0/24  encap ip id 1 src 0.0.0.0 dst 10.1.0.2 ttl 0 tos 0
0ac2f3
         geneve_opts 1:1:1212121234567890,1:1:1212121234567890 ...
0ac2f3
0ac2f3
       PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
0ac2f3
       64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.079 ms
0ac2f3
0ac2f3
    v1->v2:
0ac2f3
      - improve the changelog.
0ac2f3
      - use PRINT_ANY to support dumping with json format.
0ac2f3
    v2->v3:
0ac2f3
      - implement proper JSON array for opts instead of just bunch of strings.
0ac2f3
    v3->v4:
0ac2f3
      - keep the same format between input and output, json and non json.
0ac2f3
      - print class and type as uint and print data as hex string.
0ac2f3
0ac2f3
    Signed-off-by: Xin Long <lucien.xin@gmail.com>
0ac2f3
    Signed-off-by: David Ahern <dsahern@gmail.com>
0ac2f3
---
0ac2f3
 ip/iproute_lwtunnel.c | 174 +++++++++++++++++++++++++++++++++++++++++-
0ac2f3
 1 file changed, 172 insertions(+), 2 deletions(-)
0ac2f3
0ac2f3
diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c
0ac2f3
index 60f34a32a6e5b..76d906c47c44f 100644
0ac2f3
--- a/ip/iproute_lwtunnel.c
0ac2f3
+++ b/ip/iproute_lwtunnel.c
0ac2f3
@@ -291,6 +291,54 @@ static void print_encap_mpls(FILE *fp, struct rtattr *encap)
0ac2f3
 			rta_getattr_u8(tb[MPLS_IPTUNNEL_TTL]));
0ac2f3
 }
0ac2f3
 
0ac2f3
+static void lwtunnel_print_geneve_opts(struct rtattr *attr)
0ac2f3
+{
0ac2f3
+	struct rtattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
0ac2f3
+	struct rtattr *i = RTA_DATA(attr);
0ac2f3
+	int rem = RTA_PAYLOAD(attr);
0ac2f3
+	char *name = "geneve_opts";
0ac2f3
+	int data_len, offset = 0;
0ac2f3
+	char data[rem * 2 + 1];
0ac2f3
+	__u16 class;
0ac2f3
+	__u8 type;
0ac2f3
+
0ac2f3
+	print_nl();
0ac2f3
+	print_string(PRINT_FP, name, "\t%s ", name);
0ac2f3
+	open_json_array(PRINT_JSON, name);
0ac2f3
+
0ac2f3
+	while (rem) {
0ac2f3
+		parse_rtattr(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, i, rem);
0ac2f3
+		class = rta_getattr_be16(tb[LWTUNNEL_IP_OPT_GENEVE_CLASS]);
0ac2f3
+		type = rta_getattr_u8(tb[LWTUNNEL_IP_OPT_GENEVE_TYPE]);
0ac2f3
+		data_len = RTA_PAYLOAD(tb[LWTUNNEL_IP_OPT_GENEVE_DATA]);
0ac2f3
+		hexstring_n2a(RTA_DATA(tb[LWTUNNEL_IP_OPT_GENEVE_DATA]),
0ac2f3
+			      data_len, data, sizeof(data));
0ac2f3
+		offset += data_len + 20;
0ac2f3
+		rem -= data_len + 20;
0ac2f3
+		i = RTA_DATA(attr) + offset;
0ac2f3
+
0ac2f3
+		open_json_object(NULL);
0ac2f3
+		print_uint(PRINT_ANY, "class", "%u", class);
0ac2f3
+		print_uint(PRINT_ANY, "type", ":%u", type);
0ac2f3
+		if (rem)
0ac2f3
+			print_string(PRINT_ANY, "data", ":%s,", data);
0ac2f3
+		else
0ac2f3
+			print_string(PRINT_ANY, "data", ":%s ", data);
0ac2f3
+		close_json_object();
0ac2f3
+	}
0ac2f3
+
0ac2f3
+	close_json_array(PRINT_JSON, name);
0ac2f3
+}
0ac2f3
+
0ac2f3
+static void lwtunnel_print_opts(struct rtattr *attr)
0ac2f3
+{
0ac2f3
+	struct rtattr *tb_opt[LWTUNNEL_IP_OPTS_MAX + 1];
0ac2f3
+
0ac2f3
+	parse_rtattr_nested(tb_opt, LWTUNNEL_IP_OPTS_MAX, attr);
0ac2f3
+	if (tb_opt[LWTUNNEL_IP_OPTS_GENEVE])
0ac2f3
+		lwtunnel_print_geneve_opts(tb_opt[LWTUNNEL_IP_OPTS_GENEVE]);
0ac2f3
+}
0ac2f3
+
0ac2f3
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
0ac2f3
 {
0ac2f3
 	struct rtattr *tb[LWTUNNEL_IP_MAX+1];
0ac2f3
@@ -329,6 +377,9 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap)
0ac2f3
 		if (flags & TUNNEL_SEQ)
0ac2f3
 			print_bool(PRINT_ANY, "seq", "seq ", true);
0ac2f3
 	}
0ac2f3
+
0ac2f3
+	if (tb[LWTUNNEL_IP_OPTS])
0ac2f3
+		lwtunnel_print_opts(tb[LWTUNNEL_IP_OPTS]);
0ac2f3
 }
0ac2f3
 
0ac2f3
 static void print_encap_ila(FILE *fp, struct rtattr *encap)
0ac2f3
@@ -401,6 +452,9 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap)
0ac2f3
 		if (flags & TUNNEL_SEQ)
0ac2f3
 			print_bool(PRINT_ANY, "seq", "seq ", true);
0ac2f3
 	}
0ac2f3
+
0ac2f3
+	if (tb[LWTUNNEL_IP6_OPTS])
0ac2f3
+		lwtunnel_print_opts(tb[LWTUNNEL_IP6_OPTS]);
0ac2f3
 }
0ac2f3
 
0ac2f3
 static void print_encap_bpf(FILE *fp, struct rtattr *encap)
0ac2f3
@@ -795,11 +849,97 @@ static int parse_encap_mpls(struct rtattr *rta, size_t len,
0ac2f3
 	return 0;
0ac2f3
 }
0ac2f3
 
0ac2f3
+static int lwtunnel_parse_geneve_opt(char *str, size_t len, struct rtattr *rta)
0ac2f3
+{
0ac2f3
+	struct rtattr *nest;
0ac2f3
+	char *token;
0ac2f3
+	int i, err;
0ac2f3
+
0ac2f3
+	nest = rta_nest(rta, len, LWTUNNEL_IP_OPTS_GENEVE | NLA_F_NESTED);
0ac2f3
+	i = 1;
0ac2f3
+	token = strsep(&str, ":");
0ac2f3
+	while (token) {
0ac2f3
+		switch (i) {
0ac2f3
+		case LWTUNNEL_IP_OPT_GENEVE_CLASS:
0ac2f3
+		{
0ac2f3
+			__be16 opt_class;
0ac2f3
+
0ac2f3
+			if (!strlen(token))
0ac2f3
+				break;
0ac2f3
+			err = get_be16(&opt_class, token, 0);
0ac2f3
+			if (err)
0ac2f3
+				return err;
0ac2f3
+
0ac2f3
+			rta_addattr16(rta, len, i, opt_class);
0ac2f3
+			break;
0ac2f3
+		}
0ac2f3
+		case LWTUNNEL_IP_OPT_GENEVE_TYPE:
0ac2f3
+		{
0ac2f3
+			__u8 opt_type;
0ac2f3
+
0ac2f3
+			if (!strlen(token))
0ac2f3
+				break;
0ac2f3
+			err = get_u8(&opt_type, token, 0);
0ac2f3
+			if (err)
0ac2f3
+				return err;
0ac2f3
+
0ac2f3
+			rta_addattr8(rta, len, i, opt_type);
0ac2f3
+			break;
0ac2f3
+		}
0ac2f3
+		case LWTUNNEL_IP_OPT_GENEVE_DATA:
0ac2f3
+		{
0ac2f3
+			size_t token_len = strlen(token);
0ac2f3
+			__u8 *opts;
0ac2f3
+
0ac2f3
+			if (!token_len)
0ac2f3
+				break;
0ac2f3
+			opts = malloc(token_len / 2);
0ac2f3
+			if (!opts)
0ac2f3
+				return -1;
0ac2f3
+			if (hex2mem(token, opts, token_len / 2) < 0) {
0ac2f3
+				free(opts);
0ac2f3
+				return -1;
0ac2f3
+			}
0ac2f3
+			rta_addattr_l(rta, len, i, opts, token_len / 2);
0ac2f3
+			free(opts);
0ac2f3
+
0ac2f3
+			break;
0ac2f3
+		}
0ac2f3
+		default:
0ac2f3
+			fprintf(stderr, "Unknown \"geneve_opts\" type\n");
0ac2f3
+			return -1;
0ac2f3
+		}
0ac2f3
+
0ac2f3
+		token = strsep(&str, ":");
0ac2f3
+		i++;
0ac2f3
+	}
0ac2f3
+	rta_nest_end(rta, nest);
0ac2f3
+
0ac2f3
+	return 0;
0ac2f3
+}
0ac2f3
+
0ac2f3
+static int lwtunnel_parse_geneve_opts(char *str, size_t len, struct rtattr *rta)
0ac2f3
+{
0ac2f3
+	char *token;
0ac2f3
+	int err;
0ac2f3
+
0ac2f3
+	token = strsep(&str, ",");
0ac2f3
+	while (token) {
0ac2f3
+		err = lwtunnel_parse_geneve_opt(token, len, rta);
0ac2f3
+		if (err)
0ac2f3
+			return err;
0ac2f3
+
0ac2f3
+		token = strsep(&str, ",");
0ac2f3
+	}
0ac2f3
+
0ac2f3
+	return 0;
0ac2f3
+}
0ac2f3
+
0ac2f3
 static int parse_encap_ip(struct rtattr *rta, size_t len,
0ac2f3
 			  int *argcp, char ***argvp)
0ac2f3
 {
0ac2f3
 	int id_ok = 0, dst_ok = 0, src_ok = 0, tos_ok = 0, ttl_ok = 0;
0ac2f3
-	int key_ok = 0, csum_ok = 0, seq_ok = 0;
0ac2f3
+	int key_ok = 0, csum_ok = 0, seq_ok = 0, opts_ok = 0;
0ac2f3
 	char **argv = *argvp;
0ac2f3
 	int argc = *argcp;
0ac2f3
 	int ret = 0;
0ac2f3
@@ -851,6 +991,21 @@ static int parse_encap_ip(struct rtattr *rta, size_t len,
0ac2f3
 			if (get_u8(&ttl, *argv, 0))
0ac2f3
 				invarg("\"ttl\" value is invalid\n", *argv);
0ac2f3
 			ret = rta_addattr8(rta, len, LWTUNNEL_IP_TTL, ttl);
0ac2f3
+		} else if (strcmp(*argv, "geneve_opts") == 0) {
0ac2f3
+			struct rtattr *nest;
0ac2f3
+
0ac2f3
+			if (opts_ok++)
0ac2f3
+				duparg2("opts", *argv);
0ac2f3
+
0ac2f3
+			NEXT_ARG();
0ac2f3
+
0ac2f3
+			nest = rta_nest(rta, len,
0ac2f3
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
0ac2f3
+			ret = lwtunnel_parse_geneve_opts(*argv, len, rta);
0ac2f3
+			if (ret)
0ac2f3
+				invarg("\"geneve_opts\" value is invalid\n",
0ac2f3
+				       *argv);
0ac2f3
+			rta_nest_end(rta, nest);
0ac2f3
 		} else if (strcmp(*argv, "key") == 0) {
0ac2f3
 			if (key_ok++)
0ac2f3
 				duparg2("key", *argv);
0ac2f3
@@ -966,7 +1121,7 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len,
0ac2f3
 			   int *argcp, char ***argvp)
0ac2f3
 {
0ac2f3
 	int id_ok = 0, dst_ok = 0, src_ok = 0, tos_ok = 0, ttl_ok = 0;
0ac2f3
-	int key_ok = 0, csum_ok = 0, seq_ok = 0;
0ac2f3
+	int key_ok = 0, csum_ok = 0, seq_ok = 0, opts_ok = 0;
0ac2f3
 	char **argv = *argvp;
0ac2f3
 	int argc = *argcp;
0ac2f3
 	int ret = 0;
0ac2f3
@@ -1020,6 +1175,21 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len,
0ac2f3
 				       *argv);
0ac2f3
 			ret = rta_addattr8(rta, len, LWTUNNEL_IP6_HOPLIMIT,
0ac2f3
 					   hoplimit);
0ac2f3
+		} else if (strcmp(*argv, "geneve_opts") == 0) {
0ac2f3
+			struct rtattr *nest;
0ac2f3
+
0ac2f3
+			if (opts_ok++)
0ac2f3
+				duparg2("opts", *argv);
0ac2f3
+
0ac2f3
+			NEXT_ARG();
0ac2f3
+
0ac2f3
+			nest = rta_nest(rta, len,
0ac2f3
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
0ac2f3
+			ret = lwtunnel_parse_geneve_opts(*argv, len, rta);
0ac2f3
+			if (ret)
0ac2f3
+				invarg("\"geneve_opts\" value is invalid\n",
0ac2f3
+				       *argv);
0ac2f3
+			rta_nest_end(rta, nest);
0ac2f3
 		} else if (strcmp(*argv, "key") == 0) {
0ac2f3
 			if (key_ok++)
0ac2f3
 				duparg2("key", *argv);
0ac2f3
-- 
0ac2f3
2.26.2
0ac2f3