Skip to contentSkip to conversation

iproute2: fully support for geneve/vxlan/erspan options

PATCH Superseded Xin Long 4 participants 27 messages Feb 14, 2020 – Apr 28, 2020

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xin Long

author
#0 lore raw

v3 cover iproute2: fully support for geneve/vxlan/erspan options

Patch 1-3 add the geneve/vxlan/erspan options support for iproute_lwtunnel, and Patch 4-5 add the vxlan/erspan options for tc m_tunnel_key, and Patch 6-7 add the vxlan/erspan options for tc f_flower.

In kernel space, these features have been supported since these patchsets:

https://patchwork.ozlabs.org/cover/1190172/
https://patchwork.ozlabs.org/cover/1198854/

v1->v2:
- improve the bash commands in changelog as David A. suggested.
- use PRINT_ANY to support dummping with json format as Stephen suggested.
v2->v3:
- implement proper JSON array for opts as Stephen suggested.

Xin Long (7):
iproute_lwtunnel: add options support for geneve metadata
iproute_lwtunnel: add options support for vxlan metadata
iproute_lwtunnel: add options support for erspan metadata
tc: m_tunnel_key: add options support for vxlan
tc: m_tunnel_key: add options support for erpsan
tc: f_flower: add options support for vxlan
tc: f_flower: add options support for erspan

 ip/iproute_lwtunnel.c    | 398 ++++++++++++++++++++++++++++++++++++++++++++++-
 man/man8/tc-flower.8     |  26 ++++
 man/man8/tc-tunnel_key.8 |  20 ++-
 tc/f_flower.c            | 301 +++++++++++++++++++++++++++++++++--
 tc/m_tunnel_key.c        | 205 ++++++++++++++++++++++--
 5 files changed, 922 insertions(+), 28 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH v3 1/7 iproute_lwtunnel: add options support for geneve metadata

This patch is to add LWTUNNEL_IP(6)_OPTS and LWTUNNEL_IP_OPTS_GENEVE's parse and print to implement geneve options support in iproute_lwtunnel.

Options are expressed as class:type:data and multiple options may be listed using a comma delimiter.

With this patch, users can add and dump geneve options like:

  # ip netns add a
  # ip netns add b
  # ip -n a link add eth0 type veth peer name eth0 netns b
  # ip -n a link set eth0 up; ip -n b link set eth0 up
  # ip -n a addr add 10.1.0.1/24 dev eth0
  # ip -n b addr add 10.1.0.2/24 dev eth0
  # ip -n b link add geneve1 type geneve id 1 remote 10.1.0.1 ttl 64
  # ip -n b addr add 1.1.1.1/24 dev geneve1
  # ip -n b link set geneve1 up
  # ip -n b route add 2.1.1.0/24 dev geneve1
  # ip -n a link add geneve1 type geneve external
  # ip -n a addr add 2.1.1.1/24 dev geneve1
  # ip -n a link set geneve1 up
  # ip -n a route add 1.1.1.0/24 encap ip id 1 geneve_opts \
    1:1:1212121234567890,1:1:1212121234567890,1:1:1212121234567890 \
    dst 10.1.0.2 dev geneve1
  # ip -n a route show
  # ip netns exec a ping 1.1.1.1 -c 1

1.1.1.0/24 encap ip id 1 src 0.0.0.0 dst 10.1.0.2 ttl 0 tos 0

     geneve_opts 0001:01:1212121234567890,0001:01:1212121234567890 ...

PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data. 64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.079 ms

v1->v2:
- improve the changelog.
- use PRINT_ANY to support dumping with json format.
v2->v3:
- implement proper JSON array for opts instead of just bunch of strings.

---
 ip/iproute_lwtunnel.c | 183 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 181 insertions(+), 2 deletions(-)
ip/iproute_lwtunnel.c +181 -2
index 0d7d714..737d7da 100644
@@ -294,6 +294,63 @@ static void print_encap_mpls(FILE *fp, struct rtattr *encap)
 			rta_getattr_u8(tb[MPLS_IPTUNNEL_TTL]));
 }
 
+static void lwtunnel_print_geneve_opts(struct rtattr *attr)
+{
+	struct rtattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
+	struct rtattr *i = RTA_DATA(attr);
+	int ii, data_len, offset = 0;
+	int rem = RTA_PAYLOAD(attr);
+	char *name = "geneve_opts";
+	char strbuf[rem * 2 + 1];
+	char data[rem * 2 + 1];
+	uint8_t data_r[rem];
+	__u16 class;
+	__u8 type;
+
+	open_json_array(PRINT_JSON, name);
+	print_nl();
+	print_string(PRINT_FP, name, "\t%s ", name);
+
+	while (rem) {
+		parse_rtattr(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, i, rem);
+		class = rta_getattr_be16(tb[LWTUNNEL_IP_OPT_GENEVE_CLASS]);
+		type = rta_getattr_u8(tb[LWTUNNEL_IP_OPT_GENEVE_TYPE]);
+		data_len = RTA_PAYLOAD(tb[LWTUNNEL_IP_OPT_GENEVE_DATA]);
+		hexstring_n2a(RTA_DATA(tb[LWTUNNEL_IP_OPT_GENEVE_DATA]),
+			      data_len, data, sizeof(data));
+		hex2mem(data, data_r, data_len);
+		offset += data_len + 20;
+		rem -= data_len + 20;
+		i = RTA_DATA(attr) + offset;
+
+		open_json_object(NULL);
+		print_uint(PRINT_JSON, "class", NULL, class);
+		print_uint(PRINT_JSON, "type", NULL, type);
+		open_json_array(PRINT_JSON, "data");
+		for (ii = 0; ii < data_len; ii++)
+			print_uint(PRINT_JSON, NULL, NULL, data_r[ii]);
+		close_json_array(PRINT_JSON, "data");
+		close_json_object();
+
+		sprintf(strbuf, "%04x:%02x:%s", class, type, data);
+		if (rem)
+			print_string(PRINT_FP, NULL, "%s,", strbuf);
+		else
+			print_string(PRINT_FP, NULL, "%s ", strbuf);
+	}
+
+	close_json_array(PRINT_JSON, name);
+}
+
+static void lwtunnel_print_opts(struct rtattr *attr)
+{
+	struct rtattr *tb_opt[LWTUNNEL_IP_OPTS_MAX + 1];
+
+	parse_rtattr_nested(tb_opt, LWTUNNEL_IP_OPTS_MAX, attr);
+	if (tb_opt[LWTUNNEL_IP_OPTS_GENEVE])
+		lwtunnel_print_geneve_opts(tb_opt[LWTUNNEL_IP_OPTS_GENEVE]);
+}
+
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
 {
 	struct rtattr *tb[LWTUNNEL_IP_MAX+1];
@@ -332,6 +389,9 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap)
 		if (flags & TUNNEL_SEQ)
 			print_bool(PRINT_ANY, "seq", "seq ", true);
 	}
+
+	if (tb[LWTUNNEL_IP_OPTS])
+		lwtunnel_print_opts(tb[LWTUNNEL_IP_OPTS]);
 }
 
 static void print_encap_ila(FILE *fp, struct rtattr *encap)
@@ -404,6 +464,9 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap)
 		if (flags & TUNNEL_SEQ)
 			print_bool(PRINT_ANY, "seq", "seq ", true);
 	}
+
+	if (tb[LWTUNNEL_IP6_OPTS])
+		lwtunnel_print_opts(tb[LWTUNNEL_IP6_OPTS]);
 }
 
 static void print_encap_bpf(FILE *fp, struct rtattr *encap)
@@ -798,11 +861,97 @@ static int parse_encap_mpls(struct rtattr *rta, size_t len,
 	return 0;
 }
 
+static int lwtunnel_parse_geneve_opt(char *str, size_t len, struct rtattr *rta)
+{
+	struct rtattr *nest;
+	char *token;
+	int i, err;
+
+	nest = rta_nest(rta, len, LWTUNNEL_IP_OPTS_GENEVE | NLA_F_NESTED);
+	i = 1;
+	token = strsep(&str, ":");
+	while (token) {
+		switch (i) {
+		case LWTUNNEL_IP_OPT_GENEVE_CLASS:
+		{
+			__be16 opt_class;
+
+			if (!strlen(token))
+				break;
+			err = get_be16(&opt_class, token, 16);
+			if (err)
+				return err;
+
+			rta_addattr16(rta, len, i, opt_class);
+			break;
+		}
+		case LWTUNNEL_IP_OPT_GENEVE_TYPE:
+		{
+			__u8 opt_type;
+
+			if (!strlen(token))
+				break;
+			err = get_u8(&opt_type, token, 16);
+			if (err)
+				return err;
+
+			rta_addattr8(rta, len, i, opt_type);
+			break;
+		}
+		case LWTUNNEL_IP_OPT_GENEVE_DATA:
+		{
+			size_t token_len = strlen(token);
+			__u8 *opts;
+
+			if (!token_len)
+				break;
+			opts = malloc(token_len / 2);
+			if (!opts)
+				return -1;
+			if (hex2mem(token, opts, token_len / 2) < 0) {
+				free(opts);
+				return -1;
+			}
+			rta_addattr_l(rta, len, i, opts, token_len / 2);
+			free(opts);
+
+			break;
+		}
+		default:
+			fprintf(stderr, "Unknown \"geneve_opts\" type\n");
+			return -1;
+		}
+
+		token = strsep(&str, ":");
+		i++;
+	}
+	rta_nest_end(rta, nest);
+
+	return 0;
+}
+
+static int lwtunnel_parse_geneve_opts(char *str, size_t len, struct rtattr *rta)
+{
+	char *token;
+	int err;
+
+	token = strsep(&str, ",");
+	while (token) {
+		err = lwtunnel_parse_geneve_opt(token, len, rta);
+		if (err)
+			return err;
+
+		token = strsep(&str, ",");
+	}
+
+	return 0;
+}
+
 static int parse_encap_ip(struct rtattr *rta, size_t len,
 			  int *argcp, char ***argvp)
 {
 	int id_ok = 0, dst_ok = 0, src_ok = 0, tos_ok = 0, ttl_ok = 0;
-	int key_ok = 0, csum_ok = 0, seq_ok = 0;
+	int key_ok = 0, csum_ok = 0, seq_ok = 0, opts_ok = 0;
 	char **argv = *argvp;
 	int argc = *argcp;
 	int ret = 0;
@@ -854,6 +1003,21 @@ static int parse_encap_ip(struct rtattr *rta, size_t len,
 			if (get_u8(&ttl, *argv, 0))
 				invarg("\"ttl\" value is invalid\n", *argv);
 			ret = rta_addattr8(rta, len, LWTUNNEL_IP_TTL, ttl);
+		} else if (strcmp(*argv, "geneve_opts") == 0) {
+			struct rtattr *nest;
+
+			if (opts_ok++)
+				duparg2("opts", *argv);
+
+			NEXT_ARG();
+
+			nest = rta_nest(rta, len,
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
+			ret = lwtunnel_parse_geneve_opts(*argv, len, rta);
+			if (ret)
+				invarg("\"geneve_opts\" value is invalid\n",
+				       *argv);
+			rta_nest_end(rta, nest);
 		} else if (strcmp(*argv, "key") == 0) {
 			if (key_ok++)
 				duparg2("key", *argv);
@@ -969,7 +1133,7 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len,
 			   int *argcp, char ***argvp)
 {
 	int id_ok = 0, dst_ok = 0, src_ok = 0, tos_ok = 0, ttl_ok = 0;
-	int key_ok = 0, csum_ok = 0, seq_ok = 0;
+	int key_ok = 0, csum_ok = 0, seq_ok = 0, opts_ok = 0;
 	char **argv = *argvp;
 	int argc = *argcp;
 	int ret = 0;
@@ -1023,6 +1187,21 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len,
 				       *argv);
 			ret = rta_addattr8(rta, len, LWTUNNEL_IP6_HOPLIMIT,
 					   hoplimit);
+		} else if (strcmp(*argv, "geneve_opts") == 0) {
+			struct rtattr *nest;
+
+			if (opts_ok++)
+				duparg2("opts", *argv);
+
+			NEXT_ARG();
+
+			nest = rta_nest(rta, len,
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
+			ret = lwtunnel_parse_geneve_opts(*argv, len, rta);
+			if (ret)
+				invarg("\"geneve_opts\" value is invalid\n",
+				       *argv);
+			rta_nest_end(rta, nest);
 		} else if (strcmp(*argv, "key") == 0) {
 			if (key_ok++)
 				duparg2("key", *argv);
Signature · 1 line
2.1.0
author
#2 lore raw

PATCH v3 2/7 iproute_lwtunnel: add options support for vxlan metadata

This patch is to add LWTUNNEL_IP_OPTS_VXLAN's parse and print to implement vxlan options support in iproute_lwtunnel.

Option is expressed one hex value for gbp only, and vxlan doesn't support multiple options.

With this patch, users can add and dump vxlan options like:

  # ip netns add a
  # ip netns add b
  # ip -n a link add eth0 type veth peer name eth0 netns b
  # ip -n a link set eth0 up
  # ip -n b link set eth0 up
  # ip -n a addr add 10.1.0.1/24 dev eth0
  # ip -n b addr add 10.1.0.2/24 dev eth0
  # ip -n b link add vxlan1 type vxlan id 1 local 10.1.0.2 remote 10.1.0.1 \
    dev eth0 ttl 64 gbp
  # ip -n b addr add 1.1.1.1/24 dev vxlan1
  # ip -n b link set vxlan1 up
  # ip -n b route add 2.1.1.0/24 dev vxlan1
  # ip -n a link add vxlan1 type vxlan local 10.1.0.1 dev eth0 ttl 64 \
    gbp external
  # ip -n a addr add 2.1.1.1/24 dev vxlan1
  # ip -n a link set vxlan1 up
  # ip -n a route add 1.1.1.0/24 encap ip id 1 \
    vxlan_opts 0x456 dst 10.1.0.2 dev vxlan1
  # ip -n a route show
  # ip netns exec a ping 1.1.1.1 -c 1

1.1.1.0/24 encap ip id 1 src 0.0.0.0 dst 10.1.0.2 ttl 0 tos 0

     vxlan_opts 0x456 dev vxlan1 scope link

PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data. 64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.111 ms

v1->v2:
- improve the changelog.
- get_u32 with base = 0 for gbp.
- use PRINT_ANY to support dumping with json format.
v2->v3:
- implement proper JSON array for opts.

---
 ip/iproute_lwtunnel.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
ip/iproute_lwtunnel.c +70 -0
index 737d7da..7691002 100644
@@ -342,6 +342,28 @@ static void lwtunnel_print_geneve_opts(struct rtattr *attr)
 	close_json_array(PRINT_JSON, name);
 }
 
+static void lwtunnel_print_vxlan_opts(struct rtattr *attr)
+{
+        struct rtattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
+        struct rtattr *i = RTA_DATA(attr);
+        int rem = RTA_PAYLOAD(attr);
+        char *name = "vxlan_opts";
+        __u32 gbp;
+
+	parse_rtattr(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, i, rem);
+	gbp = rta_getattr_u32(tb[LWTUNNEL_IP_OPT_VXLAN_GBP]);
+
+	open_json_array(PRINT_JSON, name);
+	open_json_object(NULL);
+	print_uint(PRINT_JSON, "gbp", NULL, gbp);
+	close_json_object();
+	close_json_array(PRINT_JSON, name);
+
+	print_nl();
+	print_string(PRINT_FP, name, "\t%s ", name);
+	print_uint(PRINT_FP, NULL, "0x%x ", gbp);
+}
+
 static void lwtunnel_print_opts(struct rtattr *attr)
 {
 	struct rtattr *tb_opt[LWTUNNEL_IP_OPTS_MAX + 1];
@@ -349,6 +371,8 @@ static void lwtunnel_print_opts(struct rtattr *attr)
 	parse_rtattr_nested(tb_opt, LWTUNNEL_IP_OPTS_MAX, attr);
 	if (tb_opt[LWTUNNEL_IP_OPTS_GENEVE])
 		lwtunnel_print_geneve_opts(tb_opt[LWTUNNEL_IP_OPTS_GENEVE]);
+	else if (tb_opt[LWTUNNEL_IP_OPTS_VXLAN])
+		lwtunnel_print_vxlan_opts(tb_opt[LWTUNNEL_IP_OPTS_VXLAN]);
 }
 
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
@@ -947,6 +971,22 @@ static int lwtunnel_parse_geneve_opts(char *str, size_t len, struct rtattr *rta)
 	return 0;
 }
 
+static int lwtunnel_parse_vxlan_opts(char *str, size_t len, struct rtattr *rta)
+{
+	struct rtattr *nest;
+	__u32 gbp;
+	int err;
+
+	nest = rta_nest(rta, len, LWTUNNEL_IP_OPTS_VXLAN | NLA_F_NESTED);
+	err = get_u32(&gbp, str, 0);
+	if (err)
+		return err;
+	rta_addattr32(rta, len, LWTUNNEL_IP_OPT_VXLAN_GBP, gbp);
+
+	rta_nest_end(rta, nest);
+	return 0;
+}
+
 static int parse_encap_ip(struct rtattr *rta, size_t len,
 			  int *argcp, char ***argvp)
 {
@@ -1018,6 +1058,21 @@ static int parse_encap_ip(struct rtattr *rta, size_t len,
 				invarg("\"geneve_opts\" value is invalid\n",
 				       *argv);
 			rta_nest_end(rta, nest);
+		} else if (strcmp(*argv, "vxlan_opts") == 0) {
+			struct rtattr *nest;
+
+			if (opts_ok++)
+				duparg2("opts", *argv);
+
+			NEXT_ARG();
+
+			nest = rta_nest(rta, len,
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
+			ret = lwtunnel_parse_vxlan_opts(*argv, len, rta);
+			if (ret)
+				invarg("\"vxlan_opts\" value is invalid\n",
+				       *argv);
+			rta_nest_end(rta, nest);
 		} else if (strcmp(*argv, "key") == 0) {
 			if (key_ok++)
 				duparg2("key", *argv);
@@ -1202,6 +1257,21 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len,
 				invarg("\"geneve_opts\" value is invalid\n",
 				       *argv);
 			rta_nest_end(rta, nest);
+		} else if (strcmp(*argv, "vxlan_opts") == 0) {
+			struct rtattr *nest;
+
+			if (opts_ok++)
+				duparg2("opts", *argv);
+
+			NEXT_ARG();
+
+			nest = rta_nest(rta, len,
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
+			ret = lwtunnel_parse_vxlan_opts(*argv, len, rta);
+			if (ret)
+				invarg("\"vxlan_opts\" value is invalid\n",
+				       *argv);
+			rta_nest_end(rta, nest);
 		} else if (strcmp(*argv, "key") == 0) {
 			if (key_ok++)
 				duparg2("key", *argv);
Signature · 1 line
2.1.0
author
#3 lore raw

PATCH v3 3/7 iproute_lwtunnel: add options support for erspan metadata

This patch is to add LWTUNNEL_IP_OPTS_ERSPAN's parse and print to implement erspan options support in iproute_lwtunnel.

Option is expressed as version:index:dir:hwid, dir and hwid will be parsed when version is 2, while index will be parsed when version is 1. erspan doesn't support multiple options.

With this patch, users can add and dump erspan options like:

  # ip netns add a
  # ip netns add b
  # ip -n a link add eth0 type veth peer name eth0 netns b
  # ip -n a link set eth0 up
  # ip -n b link set eth0 up
  # ip -n a addr add 10.1.0.1/24 dev eth0
  # ip -n b addr add 10.1.0.2/24 dev eth0
  # ip -n b link add erspan1 type erspan key 1 seq erspan 123 \
    local 10.1.0.2 remote 10.1.0.1
  # ip -n b addr add 1.1.1.1/24 dev erspan1
  # ip -n b link set erspan1 up
  # ip -n b route add 2.1.1.0/24 dev erspan1
  # ip -n a link add erspan1 type erspan key 1 seq local 10.1.0.1 external
  # ip -n a addr add 2.1.1.1/24 dev erspan1
  # ip -n a link set erspan1 up
  # ip -n a route add 1.1.1.0/24 encap ip id 1 \
    erspan_opts 2:123:1:2 dst 10.1.0.2 dev erspan1
  # ip -n a route show
  # ip netns exec a ping 1.1.1.1 -c 1

1.1.1.0/24 encap ip id 1 src 0.0.0.0 dst 10.1.0.2 ttl 0 tos 0

     erspan_opts 02:00000000:01:02 dev erspan1 scope link

PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data. 64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.124 ms

v1->v2:
- improve the changelog.
- use PRINT_ANY to support dumping with json format.
v2->v3:
- implement proper JSON object for opts instead of just bunch of strings.

---
 ip/iproute_lwtunnel.c | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 145 insertions(+)
ip/iproute_lwtunnel.c +145 -0
index 7691002..f64ccb1 100644
@@ -364,6 +364,43 @@ static void lwtunnel_print_vxlan_opts(struct rtattr *attr)
 	print_uint(PRINT_FP, NULL, "0x%x ", gbp);
 }
 
+static void lwtunnel_print_erspan_opts(struct rtattr *attr)
+{
+	struct rtattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
+        struct rtattr *i = RTA_DATA(attr);
+        int rem = RTA_PAYLOAD(attr);
+        char *name = "erspan_opts";
+        char strbuf[rem * 2 + 1];
+	__u8 ver, hwid, dir;
+	__u32 idx;
+
+	parse_rtattr(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, i, RTA_PAYLOAD(attr));
+	ver = rta_getattr_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
+	if (ver == 1) {
+		idx = rta_getattr_be32(tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]);
+		dir = 0;
+		hwid = 0;
+	} else {
+		idx = 0;
+		dir = rta_getattr_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_DIR]);
+		hwid = rta_getattr_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]);
+	}
+
+	open_json_array(PRINT_JSON, name);
+	open_json_object(NULL);
+	print_uint(PRINT_JSON, "ver", NULL, ver);
+	print_uint(PRINT_JSON, "index", NULL, idx);
+	print_uint(PRINT_JSON, "dir", NULL, dir);
+	print_uint(PRINT_JSON, "hwid", NULL, hwid);
+	close_json_object();
+	close_json_array(PRINT_JSON, name);
+
+	print_nl();
+	print_string(PRINT_FP, name, "\t%s ", name);
+	sprintf(strbuf, "%02x:%08x:%02x:%02x", ver, idx, dir, hwid);
+	print_string(PRINT_FP, NULL, "%s ", strbuf);
+}
+
 static void lwtunnel_print_opts(struct rtattr *attr)
 {
 	struct rtattr *tb_opt[LWTUNNEL_IP_OPTS_MAX + 1];
@@ -373,6 +410,8 @@ static void lwtunnel_print_opts(struct rtattr *attr)
 		lwtunnel_print_geneve_opts(tb_opt[LWTUNNEL_IP_OPTS_GENEVE]);
 	else if (tb_opt[LWTUNNEL_IP_OPTS_VXLAN])
 		lwtunnel_print_vxlan_opts(tb_opt[LWTUNNEL_IP_OPTS_VXLAN]);
+	else if (tb_opt[LWTUNNEL_IP_OPTS_ERSPAN])
+		lwtunnel_print_erspan_opts(tb_opt[LWTUNNEL_IP_OPTS_ERSPAN]);
 }
 
 static void print_encap_ip(FILE *fp, struct rtattr *encap)
@@ -987,6 +1026,82 @@ static int lwtunnel_parse_vxlan_opts(char *str, size_t len, struct rtattr *rta)
 	return 0;
 }
 
+static int lwtunnel_parse_erspan_opts(char *str, size_t len, struct rtattr *rta)
+{
+	struct rtattr *nest;
+	char *token;
+	int i, err;
+
+	nest = rta_nest(rta, len, LWTUNNEL_IP_OPTS_ERSPAN | NLA_F_NESTED);
+	i = 1;
+	token = strsep(&str, ":");
+	while (token) {
+		switch (i) {
+		case LWTUNNEL_IP_OPT_ERSPAN_VER:
+		{
+			__u8 opt_type;
+
+			if (!strlen(token))
+				break;
+			err = get_u8(&opt_type, token, 16);
+			if (err)
+				return err;
+
+			rta_addattr8(rta, len, i, opt_type);
+			break;
+		}
+		case LWTUNNEL_IP_OPT_ERSPAN_INDEX:
+		{
+			__be32 opt_class;
+
+			if (!strlen(token))
+				break;
+			err = get_be32(&opt_class, token, 16);
+			if (err)
+				return err;
+
+			rta_addattr32(rta, len, i, opt_class);
+			break;
+		}
+		case LWTUNNEL_IP_OPT_ERSPAN_DIR:
+		{
+			__u8 opt_type;
+
+			if (!strlen(token))
+				break;
+			err = get_u8(&opt_type, token, 16);
+			if (err)
+				return err;
+
+			rta_addattr8(rta, len, i, opt_type);
+			break;
+		}
+		case LWTUNNEL_IP_OPT_ERSPAN_HWID:
+		{
+			__u8 opt_type;
+
+			if (!strlen(token))
+				break;
+			err = get_u8(&opt_type, token, 16);
+			if (err)
+				return err;
+
+			rta_addattr8(rta, len, i, opt_type);
+			break;
+		}
+		default:
+			fprintf(stderr, "Unknown \"geneve_opts\" type\n");
+			return -1;
+		}
+
+		token = strsep(&str, ":");
+		i++;
+	}
+
+	rta_nest_end(rta, nest);
+	return 0;
+}
+
 static int parse_encap_ip(struct rtattr *rta, size_t len,
 			  int *argcp, char ***argvp)
 {
@@ -1073,6 +1188,21 @@ static int parse_encap_ip(struct rtattr *rta, size_t len,
 				invarg("\"vxlan_opts\" value is invalid\n",
 				       *argv);
 			rta_nest_end(rta, nest);
+		} else if (strcmp(*argv, "erspan_opts") == 0) {
+			struct rtattr *nest;
+
+			if (opts_ok++)
+				duparg2("opts", *argv);
+
+			NEXT_ARG();
+
+			nest = rta_nest(rta, len,
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
+			ret = lwtunnel_parse_erspan_opts(*argv, len, rta);
+			if (ret)
+				invarg("\"erspan_opts\" value is invalid\n",
+				       *argv);
+			rta_nest_end(rta, nest);
 		} else if (strcmp(*argv, "key") == 0) {
 			if (key_ok++)
 				duparg2("key", *argv);
@@ -1272,6 +1402,21 @@ static int parse_encap_ip6(struct rtattr *rta, size_t len,
 				invarg("\"vxlan_opts\" value is invalid\n",
 				       *argv);
 			rta_nest_end(rta, nest);
+		} else if (strcmp(*argv, "erspan_opts") == 0) {
+			struct rtattr *nest;
+
+			if (opts_ok++)
+				duparg2("opts", *argv);
+
+			NEXT_ARG();
+
+			nest = rta_nest(rta, len,
+					LWTUNNEL_IP_OPTS | NLA_F_NESTED);
+			ret = lwtunnel_parse_erspan_opts(*argv, len, rta);
+			if (ret)
+				invarg("\"erspan_opts\" value is invalid\n",
+				       *argv);
+			rta_nest_end(rta, nest);
 		} else if (strcmp(*argv, "key") == 0) {
 			if (key_ok++)
 				duparg2("key", *argv);
Signature · 1 line
2.1.0
author
#4 lore raw

PATCH v3 4/7 tc: m_tunnel_key: add options support for vxlan

This patch is to add TCA_TUNNEL_KEY_ENC_OPTS_VXLAN's parse and print to implement vxlan options support in m_tunnel_key, like Commit 6217917a3826 ("tc: m_tunnel_key: Add tunnel option support to act_tunnel_key") for geneve options support.

Option is expressed a 32bit hexadecimal value for gbp only, and vxlan doesn't support multiple options.

With this patch, users can add and dump vxlan options like:

  # ip link add name vxlan1 type vxlan dstport 0 external
  # tc qdisc add dev eth0 ingress
  # tc filter add dev eth0 protocol ip parent ffff: \
      flower indev eth0 \
        ip_proto udp \
        action tunnel_key \
          set src_ip 10.0.99.192 \
          dst_ip 10.0.99.193 \
          dst_port 6081 \
          id 11 \
          vxlan_opts 0x10101 \
      action mirred egress redirect dev vxlan1
  # tc -s filter show dev eth0 parent ffff:
     filter protocol ip pref 49152 flower chain 0 handle 0x1
       indev eth0
       eth_type ipv4
       ip_proto udp
       not_in_hw
         action order 1: tunnel_key  set
         src_ip 10.0.99.192
         dst_ip 10.0.99.193
         key_id 11
         dst_port 6081
         vxlan_opts 0x10101
         ...

v1->v2:
- get_u32 with base = 0 for gbp.
- use to print_unint("0x%x") to print gbp.
v2->v3:
- implement proper JSON array for opts.

---
 man/man8/tc-tunnel_key.8 | 10 +++++-
 tc/m_tunnel_key.c        | 86 +++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 86 insertions(+), 10 deletions(-)
man/man8/tc-tunnel_key.8 +9 -1
index 2145eb6..f78227c 100644
@@ -66,8 +66,10 @@ options.
 .B id
 ,
 .B dst_port
-and
+,
 .B geneve_opts
+and
+.B vxlan_opts
 are optional.
 .RS
 .TP
@@ -91,6 +93,12 @@ is specified in the form CLASS:TYPE:DATA, where CLASS is represented as a
 variable length hexadecimal value. Additionally multiple options may be
 listed using a comma delimiter.
 .TP
+.B vxlan_opts
+Vxlan metatdata options.
+.B vxlan_opts
+is specified in the form GBP, as a 32bit hexadecimal value. Multiple options
+is not supported.
+.TP
 .B tos
 Outer header TOS
 .TP
tc/m_tunnel_key.c +77 -9

145 lines not on this page · raw message

Signature · 1 line
2.1.0
author
#5 lore raw

PATCH v3 5/7 tc: m_tunnel_key: add options support for erpsan

This patch is to add TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN's parse and print to implement erspan options support in m_tunnel_key, like Commit 6217917a3826 ("tc: m_tunnel_key: Add tunnel option support to act_tunnel_key") for geneve options support.

Option is expressed as version:index:dir:hwid, dir and hwid will be parsed when version is 2, while index will be parsed when version is 1. erspan doesn't support multiple options.

With this patch, users can add and dump erspan options like:

  # ip link add name erspan1 type erspan external
  # tc qdisc add dev eth0 ingress
  # tc filter add dev eth0 protocol ip parent ffff: \
      flower indev eth0 \
        ip_proto udp \
        action tunnel_key \
          set src_ip 10.0.99.192 \
          dst_ip 10.0.99.193 \
          dst_port 6081 \
          id 11 \
          erspan_opts 1:2:0:0 \
      action mirred egress redirect dev erspan1
  # tc -s filter show dev eth0 parent ffff:
     filter protocol ip pref 49151 flower chain 0 handle 0x1
       indev eth0
       eth_type ipv4
       ip_proto udp
       not_in_hw
         action order 1: tunnel_key  set
         src_ip 10.0.99.192
         dst_ip 10.0.99.193
         key_id 11
         dst_port 6081
         erspan_opts 01:00000002:00:00
         csum pipe
           index 2 ref 1 bind 1
         ...

v1->v2:
- no change.
v2->v3:
- no change.

---
 man/man8/tc-tunnel_key.8 |  12 ++++-
 tc/m_tunnel_key.c        | 121 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 131 insertions(+), 2 deletions(-)
man/man8/tc-tunnel_key.8 +11 -1
index f78227c..bf2ebaa 100644
@@ -68,8 +68,10 @@ options.
 .B dst_port
 ,
 .B geneve_opts
-and
+,
 .B vxlan_opts
+and
+.B erspan_opts
 are optional.
 .RS
 .TP
@@ -99,6 +101,14 @@ Vxlan metatdata options.
 is specified in the form GBP, as a 32bit hexadecimal value. Multiple options
 is not supported.
 .TP
+.B erspan_opts
+Erspan metatdata options.
+.B erspan_opts
+is specified in the form VERSION:INDEX:DIR:HWID, where VERSION is represented
+as a 8bit hexadecimal value, INDEX as an 32bit hexadecimal value, DIR and HWID
+as a 8bit hexadecimal value. Multiple options is not supported. Note INDEX is
+used when VERSION is 1, and DIR and HWID are used when VERSION is 2.
+.TP
 .B tos
 Outer header TOS
 .TP
tc/m_tunnel_key.c +120 -1

157 lines not on this page · raw message

Signature · 1 line
2.1.0
author
#6 lore raw

PATCH v3 6/7 tc: f_flower: add options support for vxlan

This patch is to add TCA_FLOWER_KEY_ENC_OPTS_VXLAN's parse and print to implement vxlan options support in m_tunnel_key, like Commit 56155d4df86d ("tc: f_flower: add geneve option match support to flower") for geneve options support.

Option is expressed a 32bit hexadecimal value for gbp only, and vxlan doesn't support multiple options.

With this patch, users can add and dump vxlan options like:

  # ip link add name vxlan1 type vxlan dstport 0 external
  # tc qdisc add dev vxlan1 ingress
  # tc filter add dev vxlan1 protocol ip parent ffff: \
      flower \
        enc_src_ip 10.0.99.192 \
        enc_dst_ip 10.0.99.193 \
        enc_key_id 11 \
        vxlan_opts 0x10101/0xeeeeee3e \
        ip_proto udp \
        action mirred egress redirect dev eth1
  # tc -s filter show dev vxlan1 parent ffff:
     filter protocol ip pref 49152 flower chain 0 handle 0x1
       eth_type ipv4
       ip_proto udp
       enc_dst_ip 10.0.99.193
       enc_src_ip 10.0.99.192
       enc_key_id 11
       vxlan_opts 0x10101/0xeeeeee3e
       not_in_hw
         action order 1: mirred (Egress Redirect to device eth1) stolen
         index 3 ref 1 bind 1
         Action statistics:
         Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
         backlog 0b 0p requeues 0

v1->v2:
- get_u32 with base = 0 for gbp.
v2->v3:
- implement proper JSON array for opts.

---
 man/man8/tc-flower.8 |  12 +++++
 tc/f_flower.c        | 130 ++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 126 insertions(+), 16 deletions(-)
man/man8/tc-flower.8 +12 -0
index eb9eb5f..819932d 100644
@@ -81,7 +81,11 @@ flower \- flow based traffic control filter
 .IR TOS " | "
 .B enc_ttl
 .IR TTL " | "
+{
 .B geneve_opts
+|
+.B vxlan_opts
+}
 .IR OPTIONS " | "
 .BR ip_flags
 .IR IP_FLAGS
@@ -326,6 +330,8 @@ Match the connection zone, and can be masked.
 .RE
 .TP
 .BI geneve_opts " OPTIONS"
+.TQ
+.BI vxlan_opts " OPTIONS"
 Match on IP tunnel metadata. Key id
 .I NUMBER
 is a 32 bit tunnel key id (e.g. VNI for VXLAN tunnel).
@@ -346,6 +352,12 @@ the masks is missing, \fBtc\fR assumes a full-length match. The options can
 be described in the form CLASS:TYPE:DATA/CLASS_MASK:TYPE_MASK:DATA_MASK,
 where CLASS is represented as a 16bit hexadecimal value, TYPE as an 8bit
 hexadecimal value and DATA as a variable length hexadecimal value.
+vxlan_opts
+.I OPTIONS
+doesn't support multiple options, and it consists of a key followed by a slash
+and corresponding mask. If the mask is missing, \fBtc\fR assumes a full-length
+match. The option can be described in the form GBP/GBP_MASK, where GBP is
+represented as a 32bit hexadecimal value.
 .TP
 .BI ip_flags " IP_FLAGS"
 .I IP_FLAGS
tc/f_flower.c +114 -16

196 lines not on this page · raw message

Signature · 1 line
2.1.0
author
#7 lore raw

PATCH v3 7/7 tc: f_flower: add options support for erspan

This patch is to add TCA_FLOWER_KEY_ENC_OPTS_ERSPAN's parse and print to implement erspan options support in m_tunnel_key, like Commit 56155d4df86d ("tc: f_flower: add geneve option match support to flower") for geneve options support.

Option is expressed as version:index:dir:hwid, dir and hwid will be parsed when version is 2, while index will be parsed when version is 1. erspan doesn't support multiple options.

With this patch, users can add and dump erspan options like:

  # ip link add name erspan1 type erspan external
  # tc qdisc add dev erspan1 ingress
  # tc filter add dev erspan1 protocol ip parent ffff: \
      flower \
        enc_src_ip 10.0.99.192 \
        enc_dst_ip 10.0.99.193 \
        enc_key_id 11 \
        erspan_opts 1:2:0:0/01:ffffffff:00:00 \
        ip_proto udp \
        action mirred egress redirect dev eth1
  # tc -s filter show dev erspan1 parent ffff:
     filter protocol ip pref 49152 flower chain 0 handle 0x1
       eth_type ipv4
       ip_proto udp
       enc_dst_ip 10.0.99.193
       enc_src_ip 10.0.99.192
       enc_key_id 11
       erspan_opts 01:00000002:00:00/01:ffffffff:00:00
       not_in_hw
         action order 1: mirred (Egress Redirect to device eth1) stolen
         index 1 ref 1 bind 1
         Action statistics:
         Sent 0 bytes 0 pkt (dropped 0, overlimits 0 requeues 0)
         backlog 0b 0p requeues 0

v1->v2:
- no change.
v2->v3:
- no change.

---
 man/man8/tc-flower.8 |  14 +++++
 tc/f_flower.c        | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 185 insertions(+)
man/man8/tc-flower.8 +14 -0
index 819932d..5e678ba 100644
@@ -85,6 +85,8 @@ flower \- flow based traffic control filter
 .B geneve_opts
 |
 .B vxlan_opts
+|
+.B erspan_opts
 }
 .IR OPTIONS " | "
 .BR ip_flags
@@ -332,6 +334,8 @@ Match the connection zone, and can be masked.
 .BI geneve_opts " OPTIONS"
 .TQ
 .BI vxlan_opts " OPTIONS"
+.TQ
+.BI erspan_opts " OPTIONS"
 Match on IP tunnel metadata. Key id
 .I NUMBER
 is a 32 bit tunnel key id (e.g. VNI for VXLAN tunnel).
@@ -358,6 +362,16 @@ doesn't support multiple options, and it consists of a key followed by a slash
 and corresponding mask. If the mask is missing, \fBtc\fR assumes a full-length
 match. The option can be described in the form GBP/GBP_MASK, where GBP is
 represented as a 32bit hexadecimal value.
+erspan_opts
+.I OPTIONS
+doesn't support multiple options, and it consists of a key followed by a slash
+and corresponding mask. If the mask is missing, \fBtc\fR assumes a full-length
+match. The option can be described in the form
+VERSION:INDEX:DIR:HWID/VERSION:INDEX_MASK:DIR_MASK:HWID_MASK, where VERSION is
+represented as a 8bit hexadecimal value, INDEX as an 32bit hexadecimal value,
+DIR and HWID as a 8bit hexadecimal value. Multiple options is not supported.
+Note INDEX/INDEX_MASK is used when VERSION is 1, and DIR/DIR_MASK and
+HWID/HWID_MASK are used when VERSION is 2.
 .TP
 .BI ip_flags " IP_FLAGS"
 .I IP_FLAGS
tc/f_flower.c +171 -0

207 lines not on this page · raw message

Signature · 1 line
2.1.0
#8 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

In reply to Xin Long
Quoted 5 lines · On Fri, 14 Feb 2020 18:30:47 +0800 Xin Long <[email protected]> wrote:
ip/iproute_lwtunnel.c +5 -0
+
+	open_json_array(PRINT_JSON, name);
+	open_json_object(NULL);
+	print_uint(PRINT_JSON, "ver", NULL, ver);
+	print_uint(PRINT_JSON, "index", NULL, idx);

ip/iproute_lwtunnel.c static void lwtunnel_print_vxlan_opts(struct rtattr *attr) · line 402

Quoted 10 lines
ip/iproute_lwtunnel.c +10 -0
+	print_uint(PRINT_JSON, "dir", NULL, dir);
+	print_uint(PRINT_JSON, "hwid", NULL, hwid);
+	close_json_object();
+	close_json_array(PRINT_JSON, name);
+
+	print_nl();
+	print_string(PRINT_FP, name, "\t%s ", name);
+	sprintf(strbuf, "%02x:%08x:%02x:%02x", ver, idx, dir, hwid);
+	print_string(PRINT_FP, NULL, "%s ", strbuf);
+}

Instead of having two sets of prints, is it possible to do this

	print_nl();
	print_string(PRINT_FP, NULL, "\t", NULL);

	open_json_array(PRINT_ANY, name);
	open_json_object(NULL);
	print_0xhex(PRINT_ANY, "ver", " %02x", ver);
	print_0xhex(PRINT_ANY, "idx", ":%08x", idx);
	print_0xhex(PRINT_ANY, "dir", ":%02x", dir);
	print_0xhex(PRINT_ANY, "hwid", ":%02x", hwid)
	close_json_object();
	close_json_array(PRINT_ANY, " ");

Also, you seem to not hear the request to not use opaque hex values in the iproute2 interface. The version, index, etc should be distinct parameter values not a hex string.

I think this still needs more work before merging.

author
#9 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 21 lines · Stephen Hemminger
Quoted 5 lines · On Fri, 14 Feb 2020 18:30:47 +0800 Xin Long <[email protected]> wrote:
ip/iproute_lwtunnel.c +5 -0
+
+     open_json_array(PRINT_JSON, name);
+     open_json_object(NULL);
+     print_uint(PRINT_JSON, "ver", NULL, ver);
+     print_uint(PRINT_JSON, "index", NULL, idx);

ip/iproute_lwtunnel.c static void lwtunnel_print_vxlan_opts(struct rtattr *attr) · line 402

Quoted 10 lines
ip/iproute_lwtunnel.c +10 -0
+     print_uint(PRINT_JSON, "dir", NULL, dir);
+     print_uint(PRINT_JSON, "hwid", NULL, hwid);
+     close_json_object();
+     close_json_array(PRINT_JSON, name);
+
+     print_nl();
+     print_string(PRINT_FP, name, "\t%s ", name);
+     sprintf(strbuf, "%02x:%08x:%02x:%02x", ver, idx, dir, hwid);
+     print_string(PRINT_FP, NULL, "%s ", strbuf);
+}

Instead of having two sets of prints, is it possible to do this

Quoted 11 lines
        print_nl();
        print_string(PRINT_FP, NULL, "\t", NULL);

        open_json_array(PRINT_ANY, name);
        open_json_object(NULL);
        print_0xhex(PRINT_ANY, "ver", " %02x", ver);
        print_0xhex(PRINT_ANY, "idx", ":%08x", idx);
        print_0xhex(PRINT_ANY, "dir", ":%02x", dir);
        print_0xhex(PRINT_ANY, "hwid", ":%02x", hwid)
        close_json_object();
        close_json_array(PRINT_ANY, " ");

Hi Stephen,

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

Quoted 4 lines

Also, you seem to not hear the request to not use opaque hex values in the iproute2 interface. The version, index, etc should be distinct parameter values not a hex string.

The opts STRING, especially these like "XX:YY:ZZ" are represented as hex string on both adding and dumping. It is to keep consistent with geneve_opts in m_tunnel_key and f_flower, see

commit 6217917a382682d8e8a7ecdeb0c6626f701a0933

Author: Simon Horman <[email protected]>
Date:   Thu Jul 5 17:12:00 2018 -0700

tc: m_tunnel_key: Add tunnel option support to act_tunnel_key

and
commit 56155d4df86d489c4207444c8a90ce4e0e22e49f

Author: Pieter Jansen van Vuuren <[email protected]>
Date:   Fri Sep 28 16:03:39 2018 +0200

tc: f_flower: add geneve option match support to flower

and actually, the code type I'm using is exactly from these 2 patches. please take a look.

I don't think this patchset should go to another type of format for opts.

Thanks.

Quoted 2 lines

I think this still needs more work before merging.

#10 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

author
#11 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Thanks.

#12 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 32 lines · Xin Long
Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Quoted 7 lines

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Stephen: why do you think all of the numbers should be in hex?

It seems like consistency with existing output should matter more. ip/link_gre.c for instance prints index as an int, version as an int, direction as a string and only hwid in hex.

Xin: any reason you did not follow the output of the existing netdev based solutions?

author
#13 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 42 lines · David Ahern
Quoted 32 lines · Xin Long
Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Quoted 7 lines

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Quoted 8 lines

Stephen: why do you think all of the numbers should be in hex?

It seems like consistency with existing output should matter more. ip/link_gre.c for instance prints index as an int, version as an int, direction as a string and only hwid in hex.

Xin: any reason you did not follow the output of the existingg netdev based solutions?

Hi David,

Option is expressed as "version:index:dir:hwid", I made all fields in this string of hex, just like "class:type:data" in:

commit 0ed5269f9e41f495c8e9020c85f5e1644c1afc57

Author: Simon Horman <[email protected]>
Date:   Tue Jun 26 21:39:37 2018 -0700

net/sched: add tunnel option support to act_tunnel_key

I'm not sure if it's good to mix multiple types in this string. wdyt?

but for the JSON data, of course, these are all numbers(not string).

Thanks.

#14 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 57 lines · Xin Long
Quoted 42 lines · David Ahern
Quoted 32 lines · Xin Long
Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Quoted 7 lines

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Quoted 8 lines

Stephen: why do you think all of the numbers should be in hex?

It seems like consistency with existing output should matter more. ip/link_gre.c for instance prints index as an int, version as an int, direction as a string and only hwid in hex.

Xin: any reason you did not follow the output of the existingg netdev based solutions?

Hi David,

Option is expressed as "version:index:dir:hwid", I made all fields in this string of hex, just like "class:type:data" in:

commit 0ed5269f9e41f495c8e9020c85f5e1644c1afc57

Quoted 9 lines
Author: Simon Horman <[email protected]>
Date:   Tue Jun 26 21:39:37 2018 -0700

net/sched: add tunnel option support to act_tunnel_key

I'm not sure if it's good to mix multiple types in this string. wdyt?

but for the JSON data, of course, these are all numbers(not string).

I don't understand why Stephen is pushing for hex; it does not make sense for version, index or direction. I don't have a clear understanding of hwid to know uint vs hex, so your current JSON prints seem fine.

As for the stdout print and hex fields, staring at the tc and lwtunnel code, it seems like those 2 have a lot of parallels in expressing options for encoding vs lwtunnel and netdev based code. ie., I think this latest set is correct.

Stephen?

#15 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 73 lines · On Mon, 17 Feb 2020 12:53:14 -0700 David Ahern <[email protected]> wrote:
Quoted 57 lines · Xin Long
Quoted 42 lines · David Ahern
Quoted 32 lines · Xin Long
Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Quoted 7 lines

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Quoted 8 lines

Stephen: why do you think all of the numbers should be in hex?

It seems like consistency with existing output should matter more. ip/link_gre.c for instance prints index as an int, version as an int, direction as a string and only hwid in hex.

Xin: any reason you did not follow the output of the existingg netdev based solutions?

Hi David,

Option is expressed as "version:index:dir:hwid", I made all fields in this string of hex, just like "class:type:data" in:

commit 0ed5269f9e41f495c8e9020c85f5e1644c1afc57

Quoted 9 lines
Author: Simon Horman <[email protected]>
Date:   Tue Jun 26 21:39:37 2018 -0700

net/sched: add tunnel option support to act_tunnel_key

I'm not sure if it's good to mix multiple types in this string. wdyt?

but for the JSON data, of course, these are all numbers(not string).

I don't understand why Stephen is pushing for hex; it does not make sense for version, index or direction. I don't have a clear understanding of hwid to know uint vs hex, so your current JSON prints seem fine.

Quoted 6 lines

As for the stdout print and hex fields, staring at the tc and lwtunnel code, it seems like those 2 have a lot of parallels in expressing options for encoding vs lwtunnel and netdev based code. ie., I think this latest set is correct.

Stephen?

I just wanted:
1. The parse and print functions should have the same formats. I.e. if you take the output and do a little massaging of the ifindex it should be accepted as an input set of parameters.

2. As much as possible, the JSON and non-JSON output should be similar. If non-JSON prints in hex, then JSON should display hex and vice/versa.

Ideally all inputs would be human format (not machine formats like hex). But I guess the mistake was already made with some of the other tunnels.

author
#16 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 89 lines · Stephen Hemminger
Quoted 73 lines · On Mon, 17 Feb 2020 12:53:14 -0700 David Ahern <[email protected]> wrote:
Quoted 57 lines · Xin Long
Quoted 42 lines · David Ahern
Quoted 32 lines · Xin Long
Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Quoted 7 lines

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Quoted 8 lines

Stephen: why do you think all of the numbers should be in hex?

It seems like consistency with existing output should matter more. ip/link_gre.c for instance prints index as an int, version as an int, direction as a string and only hwid in hex.

Xin: any reason you did not follow the output of the existingg netdev based solutions?

Hi David,

Option is expressed as "version:index:dir:hwid", I made all fields in this string of hex, just like "class:type:data" in:

commit 0ed5269f9e41f495c8e9020c85f5e1644c1afc57

Quoted 9 lines
Author: Simon Horman <[email protected]>
Date:   Tue Jun 26 21:39:37 2018 -0700

net/sched: add tunnel option support to act_tunnel_key

I'm not sure if it's good to mix multiple types in this string. wdyt?

but for the JSON data, of course, these are all numbers(not string).

I don't understand why Stephen is pushing for hex; it does not make sense for version, index or direction. I don't have a clear understanding of hwid to know uint vs hex, so your current JSON prints seem fine.

Quoted 6 lines

As for the stdout print and hex fields, staring at the tc and lwtunnel code, it seems like those 2 have a lot of parallels in expressing options for encoding vs lwtunnel and netdev based code. ie., I think this latest set is correct.

Stephen?

I just wanted:
1. The parse and print functions should have the same formats. I.e. if you take the output and do a little massaging of the ifindex it should be accepted as an input set of parameters.

Quoted 5 lines

2. As much as possible, the JSON and non-JSON output should be similar. If non-JSON prints in hex, then JSON should display hex and vice/versa.

Ideally all inputs would be human format (not machine formats like hex). But I guess the mistake was already made with some of the other tunnels.

I guess we can't 'fix' these in other tunnels in tc.

So I'm thinking we can either use the latest patchset, or keep the geneve opts format in lwtunnel consistent with the geneve opts in tc only and parse all with unint in the new erspan/vxlan tunnels opts.

author
#17 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 95 lines · Xin Long
Quoted 92 lines · Stephen Hemminger
Quoted 73 lines · On Mon, 17 Feb 2020 12:53:14 -0700 David Ahern <[email protected]> wrote:
Quoted 57 lines · Xin Long
Quoted 42 lines · David Ahern
Quoted 32 lines · Xin Long
Quoted 10 lines · Stephen Hemminger
Quoted 4 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

JSON is typeless. Lots of values are already printed in hex

You may mean JSON data itself is typeless.
But JSON objects are typed when parsing JSON data, which includes string, number, array, boolean. So it matters how to define the members' 'type' in JSON data.

For example, in python's 'json' module:

#!/usr/bin/python2
import json
json_data_1 = '{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"}' json_data_2 = '{"ver":2,"index":0,"dir":1,"hwid":2}' parsed_json_1 = (json.loads(json_data_1)) parsed_json_2 = (json.loads(json_data_2))
print type(parsed_json_1["hwid"])
print type(parsed_json_2["hwid"])

The output is:
<type 'unicode'>
<type 'int'>

Quoted 7 lines

Also, '{"result": true}' is different from '{"result": "true"}' when loading it in a 3rd-party lib.

I think the JSON data coming from iproute2 is designed to be used by a 3rd-party lib to parse, not just to show to users. To keep these members' original type (numbers) is more appropriate, IMO.

Quoted 8 lines

Stephen: why do you think all of the numbers should be in hex?

It seems like consistency with existing output should matter more. ip/link_gre.c for instance prints index as an int, version as an int, direction as a string and only hwid in hex.

Xin: any reason you did not follow the output of the existingg netdev based solutions?

Hi David,

Option is expressed as "version:index:dir:hwid", I made all fields in this string of hex, just like "class:type:data" in:

commit 0ed5269f9e41f495c8e9020c85f5e1644c1afc57

Quoted 9 lines
Author: Simon Horman <[email protected]>
Date:   Tue Jun 26 21:39:37 2018 -0700

net/sched: add tunnel option support to act_tunnel_key

I'm not sure if it's good to mix multiple types in this string. wdyt?

but for the JSON data, of course, these are all numbers(not string).

I don't understand why Stephen is pushing for hex; it does not make sense for version, index or direction. I don't have a clear understanding of hwid to know uint vs hex, so your current JSON prints seem fine.

Quoted 6 lines

As for the stdout print and hex fields, staring at the tc and lwtunnel code, it seems like those 2 have a lot of parallels in expressing options for encoding vs lwtunnel and netdev based code. ie., I think this latest set is correct.

Stephen?

I just wanted:
1. The parse and print functions should have the same formats. I.e. if you take the output and do a little massaging of the ifindex it should be accepted as an input set of parameters.

2. As much as possible, the JSON and non-JSON output should be similar. If non-JSON prints in hex, then JSON should display hex and vice/versa.

Quoted 8 lines
Quoted 2 lines

Ideally all inputs would be human format (not machine formats like hex). But I guess the mistake was already made with some of the other tunnels.

I guess we can't 'fix' these in other tunnels in tc.

So I'm thinking we can either use the latest patchset, or keep the geneve opts format in lwtunnel consistent with the geneve opts in tc only and parse all with unint in the new erspan/vxlan tunnels opts.

Hi, Stephen and David A.

This patchset is in "deferred" status for a long time.
What should we do about this one?
should I improve something then repost or the lastest one will be fine.

Thanks.

#18 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 3 lines · Xin Long

This patchset is in "deferred" status for a long time.
What should we do about this one?
should I improve something then repost or the lastest one will be fine.

I am fine with this set as is; Stephen had some concerns.

author
#19 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Hi, Stephen,

any update?

This is your last reply:

"""
1. The parse and print functions should have the same formats. I.e. if you take the output and do a little massaging of the ifindex it should be accepted as an input set of parameters.

2. As much as possible, the JSON and non-JSON output should be similar. If non-JSON prints in hex, then JSON should display hex and vice/versa. """

Do you still hope to change things like that (just note it won't be consistent with:)

6217917a tc: m_tunnel_key: Add tunnel option support to act_tunnel_key 56155d4d tc: f_flower: add geneve option match support to flower

or go with the current set? I'm now okay to do either of them.

Thanks.

Quoted 7 lines · David Ahern
Quoted 3 lines · Xin Long

This patchset is in "deferred" status for a long time.
What should we do about this one?
should I improve something then repost or the lastest one will be fine.

I am fine with this set as is; Stephen had some concerns.

#20 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

In reply to David Ahern
Quoted 7 lines · David Ahern
Quoted 3 lines · Xin Long

This patchset is in "deferred" status for a long time.
What should we do about this one?
should I improve something then repost or the lastest one will be fine.

I am fine with this set as is; Stephen had some concerns.

Please re-send the patches.

author
#21 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 11 lines · David Ahern
Quoted 7 lines · David Ahern
Quoted 3 lines · Xin Long

This patchset is in "deferred" status for a long time.
What should we do about this one?
should I improve something then repost or the lastest one will be fine.

I am fine with this set as is; Stephen had some concerns.

Please re-send the patches.

OK, will post v4. thanks.

#22 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

In reply to Xin Long
Quoted 42 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:
Quoted 21 lines · Stephen Hemminger
Quoted 5 lines · On Fri, 14 Feb 2020 18:30:47 +0800 Xin Long <[email protected]> wrote:
ip/iproute_lwtunnel.c +5 -0
+
+     open_json_array(PRINT_JSON, name);
+     open_json_object(NULL);
+     print_uint(PRINT_JSON, "ver", NULL, ver);
+     print_uint(PRINT_JSON, "index", NULL, idx);

ip/iproute_lwtunnel.c static void lwtunnel_print_vxlan_opts(struct rtattr *attr) · line 402

Quoted 10 lines
ip/iproute_lwtunnel.c +10 -0
+     print_uint(PRINT_JSON, "dir", NULL, dir);
+     print_uint(PRINT_JSON, "hwid", NULL, hwid);
+     close_json_object();
+     close_json_array(PRINT_JSON, name);
+
+     print_nl();
+     print_string(PRINT_FP, name, "\t%s ", name);
+     sprintf(strbuf, "%02x:%08x:%02x:%02x", ver, idx, dir, hwid);
+     print_string(PRINT_FP, NULL, "%s ", strbuf);
+}  

Instead of having two sets of prints, is it possible to do this

Quoted 11 lines
        print_nl();
        print_string(PRINT_FP, NULL, "\t", NULL);

        open_json_array(PRINT_ANY, name);
        open_json_object(NULL);
        print_0xhex(PRINT_ANY, "ver", " %02x", ver);
        print_0xhex(PRINT_ANY, "idx", ":%08x", idx);
        print_0xhex(PRINT_ANY, "dir", ":%02x", dir);
        print_0xhex(PRINT_ANY, "hwid", ":%02x", hwid)
        close_json_object();
        close_json_array(PRINT_ANY, " ");  

Hi Stephen,

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Also, you seem to not hear the request to not use opaque hex values in the iproute2 interface. The version, index, etc should be distinct parameter values not a hex string.

The opts STRING, especially these like "XX:YY:ZZ" are represented as hex string on both adding and dumping. It is to keep consistent with geneve_opts in m_tunnel_key and f_flower, see

There are several different requests.

1. The format of the output must match the input.
2. Printing values in hex would be nice if they are bit fields
3. If non json uses hex, then json should use hex

    json is type less so { "ver":2 } and { "ver":"0x2" } are the same
maintainer
#23 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 2 lines · On Thu, 23 Apr 2020 08:23:51 -0700 Stephen Hemminger wrote:

3. If non json uses hex, then json should use hex

    json is type less so { "ver":2 } and { "ver":"0x2" } are the same

I may be missing something or misunderstanding you, but in my humble experience that's emphatically not true:

$ echo '{ "a" : 2 }' | python -c 'import sys, json; print(json.load(sys.stdin)["a"] + 1)'
3
$ echo '{ "a" : "2" }' | python -c 'import sys, json; print(json.load(sys.stdin)["a"] + 1)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
#24 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 7 lines · Jakub Kicinski
Quoted 2 lines · On Thu, 23 Apr 2020 08:23:51 -0700 Stephen Hemminger wrote:

3. If non json uses hex, then json should use hex

    json is type less so { "ver":2 } and { "ver":"0x2" } are the same

I may be missing something or misunderstanding you, but in my humble experience that's emphatically not true:

Quoted 7 lines
$ echo '{ "a" : 2 }' | python -c 'import sys, json; print(json.load(sys.stdin)["a"] + 1)'
3
$ echo '{ "a" : "2" }' | python -c 'import sys, json; print(json.load(sys.stdin)["a"] + 1)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

I don't know which site is the definitive source for json, but several do state json has several types - strings, number, true / false / null, object, array.

#25 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

Quoted 16 lines · On Mon, 27 Apr 2020 06:38:03 -0600 David Ahern <[email protected]> wrote:
Quoted 7 lines · Jakub Kicinski
Quoted 2 lines · On Thu, 23 Apr 2020 08:23:51 -0700 Stephen Hemminger wrote:

3. If non json uses hex, then json should use hex

    json is type less so { "ver":2 } and { "ver":"0x2" } are the same  

I may be missing something or misunderstanding you, but in my humble experience that's emphatically not true:

Quoted 7 lines
$ echo '{ "a" : 2 }' | python -c 'import sys, json; print(json.load(sys.stdin)["a"] + 1)'
3
$ echo '{ "a" : "2" }' | python -c 'import sys, json; print(json.load(sys.stdin)["a"] + 1)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
Quoted 3 lines

I don't know which site is the definitive source for json, but several do state json has several types - strings, number, true / false / null, object, array.

Probably I got confused because Python tries to be helpful...
JSON is ossified/standardized http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

Best answer on the web was https://stackoverflow.com/questions/52671719/can-hex-format-be-used-with-json-files-if-so-how

"JSON does not support hexadecimal numbers but they are supported in JSON5. json5.org"

author
#26 lore raw

Re: [PATCHv3 iproute2-next 3/7] iproute_lwtunnel: add options support for erspan metadata

In reply to Stephen Hemminger
Quoted 53 lines · Stephen Hemminger
Quoted 42 lines · On Sat, 15 Feb 2020 01:40:27 +0800 Xin Long <[email protected]> wrote:
Quoted 21 lines · Stephen Hemminger
Quoted 5 lines · On Fri, 14 Feb 2020 18:30:47 +0800 Xin Long <[email protected]> wrote:
ip/iproute_lwtunnel.c +5 -0
+
+     open_json_array(PRINT_JSON, name);
+     open_json_object(NULL);
+     print_uint(PRINT_JSON, "ver", NULL, ver);
+     print_uint(PRINT_JSON, "index", NULL, idx);

ip/iproute_lwtunnel.c static void lwtunnel_print_vxlan_opts(struct rtattr *attr) · line 402

Quoted 10 lines
ip/iproute_lwtunnel.c +10 -0
+     print_uint(PRINT_JSON, "dir", NULL, dir);
+     print_uint(PRINT_JSON, "hwid", NULL, hwid);
+     close_json_object();
+     close_json_array(PRINT_JSON, name);
+
+     print_nl();
+     print_string(PRINT_FP, name, "\t%s ", name);
+     sprintf(strbuf, "%02x:%08x:%02x:%02x", ver, idx, dir, hwid);
+     print_string(PRINT_FP, NULL, "%s ", strbuf);
+}

Instead of having two sets of prints, is it possible to do this

Quoted 11 lines
        print_nl();
        print_string(PRINT_FP, NULL, "\t", NULL);

        open_json_array(PRINT_ANY, name);
        open_json_object(NULL);
        print_0xhex(PRINT_ANY, "ver", " %02x", ver);
        print_0xhex(PRINT_ANY, "idx", ":%08x", idx);
        print_0xhex(PRINT_ANY, "dir", ":%02x", dir);
        print_0xhex(PRINT_ANY, "hwid", ":%02x", hwid)
        close_json_object();
        close_json_array(PRINT_ANY, " ");

Hi Stephen,

This's not gonna work. as the output will be:
{"ver":"0x2","idx":"0","dir":"0x1","hwid":"0x2"} (string) instead of
{"ver":2,"index":0,"dir":1,"hwid":2} (number)

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Also, you seem to not hear the request to not use opaque hex values in the iproute2 interface. The version, index, etc should be distinct parameter values not a hex string.

The opts STRING, especially these like "XX:YY:ZZ" are represented as hex string on both adding and dumping. It is to keep consistent with geneve_opts in m_tunnel_key and f_flower, see

Quoted 7 lines

There are several different requests.

1. The format of the output must match the input.
2. Printing values in hex would be nice if they are bit fields
3. If non json uses hex, then json should use hex

    json is type less so { "ver":2 } and { "ver":"0x2" } are the same

V4 has been posted, with respect to these 3 rules.
And all numbers are in uint type, no problems about { "ver":2 } and { "ver":"0x2" } things.

Thanks.