Skip to contentSkip to conversation

src: add tunnel template support

PATCH In review Fernando Fernandez Mancera 4 participants 15 messages Aug 21, 2025 – Jun 24 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/7 src: add tunnel template support

From: Pablo Neira Ayuso <[email protected]>

This patch adds tunnel template support, this allows to attach a metadata template that provides the configuration for the tunnel driver.

Example of generic tunnel configuration:

 table netdev x {
        tunnel y {
                id 10
                ip saddr 192.168.2.10
                ip daddr 192.168.2.11
                sport 10
                dport 20
                ttl 10
        }

}

This still requires the tunnel statement to attach this metadata template, this comes in a follow up patch.

Joint work with Fernando.

---
v3: support for ipv6 by Pablo

---
 include/parser.h   |   1 +
 include/rule.h     |  13 ++++++
 src/cache.c        |   2 +
 src/evaluate.c     |  26 ++++++++++++
 src/json.c         |   7 +++
 src/mnl.c          |  40 +++++++++++++++++
 src/netlink.c      |  81 +++++++++++++++++++++++++++++++++++
 src/parser_bison.y | 104 +++++++++++++++++++++++++++++++++++++++++++--
 src/parser_json.c  |   8 ++++
 src/rule.c         |  74 +++++++++++++++++++++++++++++++-
 src/scanner.l      |  12 ++++++
 11 files changed, 364 insertions(+), 4 deletions(-)
include/parser.h +1 -0
index 576e5e43..8cfd22e9 100644
@@ -51,6 +51,7 @@ enum startcond_type {
 	PARSER_SC_SECMARK,
 	PARSER_SC_TCP,
 	PARSER_SC_TYPE,
+	PARSER_SC_TUNNEL,
 	PARSER_SC_VLAN,
 	PARSER_SC_XT,
 	PARSER_SC_CMD_DESTROY,
include/rule.h +13 -0
index 470ae107..0fa87b52 100644
@@ -492,6 +492,16 @@ struct secmark {
 	char		ctx[NFT_SECMARK_CTX_MAXLEN];
 };
 
+struct tunnel {
+	uint32_t	id;
+	struct expr	*src;
+	struct expr	*dst;
+	uint16_t	sport;
+	uint16_t	dport;
+	uint8_t		tos;
+	uint8_t		ttl;
+};
+
 /**
  * struct obj - nftables stateful object statement
  *
@@ -518,6 +528,7 @@ struct obj {
 		struct secmark		secmark;
 		struct ct_expect	ct_expect;
 		struct synproxy		synproxy;
+		struct tunnel		tunnel;
 	};
 };
 
@@ -664,6 +675,8 @@ enum cmd_obj {
 	CMD_OBJ_CT_EXPECTATIONS,
 	CMD_OBJ_SYNPROXY,
 	CMD_OBJ_SYNPROXYS,
+	CMD_OBJ_TUNNEL,
+	CMD_OBJ_TUNNELS,
 	CMD_OBJ_HOOKS,
 };
 
src/cache.c +2 -0
index d58fb59f..09aa20bf 100644
@@ -443,6 +443,8 @@ static int nft_handle_validate(const struct cmd *cmd, struct list_head *msgs)
 	case CMD_OBJ_CT_TIMEOUTS:
 	case CMD_OBJ_CT_EXPECT:
 	case CMD_OBJ_CT_EXPECTATIONS:
+	case CMD_OBJ_TUNNEL:
+	case CMD_OBJ_TUNNELS:
 		if (h->table.name &&
 		    strlen(h->table.name) > NFT_NAME_MAXLEN) {
 			loc = &h->table.location;
src/evaluate.c +26 -0
index aaeb7b4e..da8794dd 100644
@@ -5856,6 +5856,29 @@ static int ct_timeout_evaluate(struct eval_ctx *ctx, struct obj *obj)
 	return 0;
 }
 
+static int tunnel_evaluate(struct eval_ctx *ctx, struct obj *obj)
+{
+	if (obj->tunnel.src) {
+		expr_set_context(&ctx->ectx, obj->tunnel.src->dtype,
+				 obj->tunnel.src->dtype->size);
+		if (expr_evaluate(ctx, &obj->tunnel.src) < 0)
+			return -1;
+	}
+
+	if (obj->tunnel.dst) {
+		expr_set_context(&ctx->ectx, obj->tunnel.dst->dtype,
+				 obj->tunnel.dst->dtype->size);
+		if (expr_evaluate(ctx, &obj->tunnel.dst) < 0)
+			return -1;
+	}
+
+	if (obj->tunnel.src->dtype != obj->tunnel.dst->dtype)
+		return __stmt_binary_error(ctx, &obj->location, NULL,
+					  "specify either ip or ip6 for address");
+
+	return 0;
+}
+
 static int obj_evaluate(struct eval_ctx *ctx, struct obj *obj)
 {
 	struct table *table;
@@ -5874,6 +5897,8 @@ static int obj_evaluate(struct eval_ctx *ctx, struct obj *obj)
 		return ct_timeout_evaluate(ctx, obj);
 	case NFT_OBJECT_CT_EXPECT:
 		return ct_expect_evaluate(ctx, obj);
+	case NFT_OBJECT_TUNNEL:
+		return tunnel_evaluate(ctx, obj);
 	default:
 		break;
 	}
@@ -5926,6 +5951,7 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)
 	case CMD_OBJ_SECMARK:
 	case CMD_OBJ_CT_EXPECT:
 	case CMD_OBJ_SYNPROXY:
+	case CMD_OBJ_TUNNEL:
 		handle_merge(&cmd->object->handle, &cmd->handle);
 		return obj_evaluate(ctx, cmd->object);
 	default:
src/json.c +7 -0
index a1e8c047..bb14e1bd 100644
@@ -487,6 +487,9 @@ static json_t *obj_print_json(const struct obj *obj)
 		json_object_update(root, tmp);
 		json_decref(tmp);
 		break;
+	case NFT_OBJECT_TUNNEL:
+		/* TODO */
+		break;
 	}
 
 	return nft_json_pack("{s:o}", type, root);
@@ -2035,6 +2038,10 @@ int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	case CMD_OBJ_SYNPROXYS:
 		root = do_list_obj_json(ctx, cmd, NFT_OBJECT_SYNPROXY);
 		break;
+	case CMD_OBJ_TUNNEL:
+	case CMD_OBJ_TUNNELS:
+		root = do_list_obj_json(ctx, cmd, NFT_OBJECT_TUNNEL);
+		break;
 	case CMD_OBJ_FLOWTABLE:
 		root = do_list_flowtable_json(ctx, cmd, table);
 		break;
src/mnl.c +40 -0
index 66840296..e17f2c2f 100644
@@ -1478,6 +1478,7 @@ int mnl_nft_obj_add(struct netlink_ctx *ctx, struct cmd *cmd,
 		    unsigned int flags)
 {
 	struct obj *obj = cmd->object;
+	struct nft_data_linearize nld;
 	struct nftnl_udata_buf *udbuf;
 	struct nftnl_obj *nlo;
 	struct nlmsghdr *nlh;
@@ -1566,6 +1567,45 @@ int mnl_nft_obj_add(struct netlink_ctx *ctx, struct cmd *cmd,
 		nftnl_obj_set_u32(nlo, NFTNL_OBJ_SYNPROXY_FLAGS,
 				  obj->synproxy.flags);
 		break;
+	case NFT_OBJECT_TUNNEL:
+		nftnl_obj_set_u32(nlo, NFTNL_OBJ_TUNNEL_ID, obj->tunnel.id);
+		if (obj->tunnel.sport)
+			nftnl_obj_set_u16(nlo, NFTNL_OBJ_TUNNEL_SPORT,
+					  obj->tunnel.sport);
+		if (obj->tunnel.dport)
+			nftnl_obj_set_u16(nlo, NFTNL_OBJ_TUNNEL_DPORT,
+					  obj->tunnel.dport);
+		if (obj->tunnel.tos)
+			nftnl_obj_set_u8(nlo, NFTNL_OBJ_TUNNEL_TOS,
+					  obj->tunnel.tos);
+		if (obj->tunnel.ttl)
+			nftnl_obj_set_u8(nlo, NFTNL_OBJ_TUNNEL_TTL,
+					  obj->tunnel.ttl);
+		if (obj->tunnel.src) {
+			netlink_gen_data(obj->tunnel.src, &nld);
+			if (nld.len == sizeof(struct in_addr)) {
+				nftnl_obj_set_u32(nlo,
+						  NFTNL_OBJ_TUNNEL_IPV4_SRC,
+						  nld.value[0]);
+			} else {
+				nftnl_obj_set_data(nlo,
+						   NFTNL_OBJ_TUNNEL_IPV6_SRC,
+						   nld.value, nld.len);
+			}
+		}
+		if (obj->tunnel.dst) {
+			netlink_gen_data(obj->tunnel.dst, &nld);
+			if (nld.len == sizeof(struct in_addr)) {
+				nftnl_obj_set_u32(nlo,
+						  NFTNL_OBJ_TUNNEL_IPV4_DST,
+						  nld.value[0]);
+		        } else {
+				nftnl_obj_set_data(nlo,
+						   NFTNL_OBJ_TUNNEL_IPV6_DST,
+						   nld.value, nld.len);
+			}
+		}
+		break;
 	default:
 		BUG("Unknown type %d\n", obj->type);
 		break;
src/netlink.c +81 -0

93 lines not on this page · raw message

src/parser_bison.y +101 -3

173 lines not on this page · raw message

src/parser_json.c +8 -0
index 71e44f19..ebb96d79 100644
@@ -3212,6 +3212,7 @@ static int string_to_nft_object(const char *str)
 		[NFT_OBJECT_SECMARK]	= "secmark",
 		[NFT_OBJECT_CT_EXPECT]	= "ct expectation",
 		[NFT_OBJECT_SYNPROXY]	= "synproxy",
+		[NFT_OBJECT_TUNNEL]	= "tunnel",
 	};
 	unsigned int i;
 
@@ -3712,6 +3713,9 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,
 
 		obj->synproxy.flags |= flags;
 		break;
+	case CMD_OBJ_TUNNEL:
+		/* TODO */
+		break;
 	default:
 		BUG("Invalid CMD '%d'", cmd_obj);
 	}
@@ -3748,6 +3752,7 @@ static struct cmd *json_parse_cmd_add(struct json_ctx *ctx,
 		{ "ct helper", NFT_OBJECT_CT_HELPER, json_parse_cmd_add_object },
 		{ "ct timeout", NFT_OBJECT_CT_TIMEOUT, json_parse_cmd_add_object },
 		{ "ct expectation", NFT_OBJECT_CT_EXPECT, json_parse_cmd_add_object },
+		{ "tunnel", NFT_OBJECT_TUNNEL, json_parse_cmd_add_object },
 		{ "limit", CMD_OBJ_LIMIT, json_parse_cmd_add_object },
 		{ "secmark", CMD_OBJ_SECMARK, json_parse_cmd_add_object },
 		{ "synproxy", CMD_OBJ_SYNPROXY, json_parse_cmd_add_object }
@@ -3883,6 +3888,7 @@ static struct cmd *json_parse_cmd_list_multiple(struct json_ctx *ctx,
 	case CMD_OBJ_SETS:
 	case CMD_OBJ_COUNTERS:
 	case CMD_OBJ_CT_HELPERS:
+	case CMD_OBJ_TUNNELS:
 		if (!json_unpack(root, "{s:s}", "table", &tmp))
 			h.table.name = xstrdup(tmp);
 		break;
@@ -3921,6 +3927,8 @@ static struct cmd *json_parse_cmd_list(struct json_ctx *ctx,
 		{ "ct helpers", CMD_OBJ_CT_HELPERS, json_parse_cmd_list_multiple },
 		{ "ct timeout", NFT_OBJECT_CT_TIMEOUT, json_parse_cmd_add_object },
 		{ "ct expectation", NFT_OBJECT_CT_EXPECT, json_parse_cmd_add_object },
+		{ "tunnel", NFT_OBJECT_TUNNEL, json_parse_cmd_add_object },
+		{ "tunnels", CMD_OBJ_TUNNELS, json_parse_cmd_list_multiple },
 		{ "limit", CMD_OBJ_LIMIT, json_parse_cmd_add_object },
 		{ "limits", CMD_OBJ_LIMIT, json_parse_cmd_list_multiple },
 		{ "ruleset", CMD_OBJ_RULESET, json_parse_cmd_list_multiple },
src/rule.c +73 -1

128 lines not on this page · raw message

src/scanner.l +12 -0
index b69d8e81..5e848890 100644
@@ -223,6 +223,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 %s SCANSTATE_SECMARK
 %s SCANSTATE_TCP
 %s SCANSTATE_TYPE
+%s SCANSTATE_TUNNEL
 %s SCANSTATE_VLAN
 %s SCANSTATE_XT
 %s SCANSTATE_CMD_DESTROY
@@ -403,6 +404,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"maps"			{ return MAPS; }
 	"secmarks"		{ return SECMARKS; }
 	"synproxys"		{ return SYNPROXYS; }
+	"tunnel"		{ return TUNNEL; }
+	"tunnels"		{ return TUNNELS; }
 	"hooks"			{ return HOOKS; }
 }
 
@@ -811,6 +814,15 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"sack-perm"		{ return SACK_PERM; }
 }
 
+"tunnel"		{ scanner_push_start_cond(yyscanner, SCANSTATE_TUNNEL); return TUNNEL; }
+<SCANSTATE_TUNNEL>{
+	"id"			{ return ID; }
+	"sport"			{ return SPORT; }
+	"dport"			{ return DPORT; }
+	"ttl"			{ return TTL; }
+	"tos"			{ return TOS; }
+}
+
 "notrack"		{ return NOTRACK; }
 
 "all"			{ return ALL; }
Signature · 1 line
2.50.1
#1 lore raw

PATCH v3 2/7 tunnel: add erspan support

From: Pablo Neira Ayuso <[email protected]>

This patch extends the tunnel metadata object to define erspan tunnel specific configurations:

 table netdev x {
        tunnel y {
                id 10
                ip saddr 192.168.2.10
                ip daddr 192.168.2.11
                sport 10
                dport 20
                ttl 10
                erspan {
                        version 1
                        index 2
                }
        }

}

Joint work with Fernando.

---
v3: rebased

---
 include/rule.h     | 18 ++++++++++++++++++
 src/mnl.c          | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/netlink.c      | 38 ++++++++++++++++++++++++++++++++++++++
 src/parser_bison.y | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 src/rule.c         | 26 ++++++++++++++++++++++++++
 src/scanner.l      |  5 +++++
 6 files changed, 175 insertions(+), 1 deletion(-)
include/rule.h +18 -0
index 0fa87b52..71e9a07e 100644
@@ -492,6 +492,11 @@ struct secmark {
 	char		ctx[NFT_SECMARK_CTX_MAXLEN];
 };
 
+enum tunnel_type {
+	TUNNEL_UNSPEC = 0,
+	TUNNEL_ERSPAN,
+};
+
 struct tunnel {
 	uint32_t	id;
 	struct expr	*src;
@@ -500,6 +505,19 @@ struct tunnel {
 	uint16_t	dport;
 	uint8_t		tos;
 	uint8_t		ttl;
+	enum tunnel_type type;
+	union {
+		struct {
+			uint32_t	version;
+			struct {
+				uint32_t	index;
+			} v1;
+			struct {
+				uint8_t		direction;
+				uint8_t		hwid;
+			} v2;
+		} erspan;
+	};
 };
 
 /**
src/mnl.c +43 -0

55 lines not on this page · raw message

src/netlink.c +38 -0
index f0a9c02b..4ef88402 100644
@@ -1808,6 +1808,41 @@ static int obj_parse_udata_cb(const struct nftnl_udata *attr, void *data)
 	return 0;
 }
 
+static int tunnel_parse_opt_cb(struct nftnl_tunnel_opt *opt, void *data) {
+
+	struct obj *obj = data;
+
+	switch (nftnl_tunnel_opt_get_type(opt)) {
+	case NFTNL_TUNNEL_TYPE_ERSPAN:
+		obj->tunnel.type = TUNNEL_ERSPAN;
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_VERSION)) {
+			obj->tunnel.erspan.version =
+				nftnl_tunnel_opt_get_u32(opt,
+							 NFTNL_TUNNEL_ERSPAN_VERSION);
+		}
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_V1_INDEX)) {
+			obj->tunnel.erspan.v1.index =
+				nftnl_tunnel_opt_get_u32(opt,
+							 NFTNL_TUNNEL_ERSPAN_V1_INDEX);
+		}
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_V2_HWID)) {
+			obj->tunnel.erspan.v2.hwid =
+				nftnl_tunnel_opt_get_u8(opt,
+							NFTNL_TUNNEL_ERSPAN_V2_HWID);
+		}
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_ERSPAN_V2_DIR)) {
+			obj->tunnel.erspan.v2.direction =
+				nftnl_tunnel_opt_get_u8(opt,
+							NFTNL_TUNNEL_ERSPAN_V2_DIR);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
 struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx,
 				    struct nftnl_obj *nlo)
 {
@@ -1938,6 +1973,9 @@ struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx,
 			obj->tunnel.dst =
 				netlink_obj_tunnel_parse_addr(nlo, NFTNL_OBJ_TUNNEL_IPV6_DST);
 		}
+		if (nftnl_obj_is_set(nlo, NFTNL_OBJ_TUNNEL_OPTS)) {
+			nftnl_obj_tunnel_opts_foreach(nlo, tunnel_parse_opt_cb, obj);
+		}
 		break;
 	default:
 		netlink_io_error(ctx, NULL, "Unknown object type %u", type);
src/parser_bison.y +45 -1

70 lines not on this page · raw message

src/rule.c +26 -0
index 5b79facb..2557f4cc 100644
@@ -2021,6 +2021,32 @@ static void obj_print_data(const struct obj *obj,
 				  opts->nl, opts->tab, opts->tab,
 				  obj->tunnel.ttl);
 		}
+		switch (obj->tunnel.type) {
+		case TUNNEL_ERSPAN:
+			nft_print(octx, "%s%s%serspan {",
+				  opts->nl, opts->tab, opts->tab);
+			nft_print(octx, "%s%s%s%sversion %u",
+				  opts->nl, opts->tab, opts->tab, opts->tab,
+				  obj->tunnel.erspan.version);
+			if (obj->tunnel.erspan.version == 1) {
+				nft_print(octx, "%s%s%s%sindex %u",
+					  opts->nl, opts->tab, opts->tab, opts->tab,
+					  obj->tunnel.erspan.v1.index);
+			} else {
+				nft_print(octx, "%s%s%s%sdirection %s",
+					  opts->nl, opts->tab, opts->tab, opts->tab,
+					  obj->tunnel.erspan.v2.direction ? "egress"
+									  : "ingress");
+				nft_print(octx, "%s%s%s%sid %u",
+					  opts->nl, opts->tab, opts->tab, opts->tab,
+					  obj->tunnel.erspan.v2.hwid);
+			}
+			nft_print(octx, "%s%s%s}",
+				  opts->nl, opts->tab, opts->tab);
+		default:
+			break;
+		}
+
 		nft_print(octx, "%s", opts->stmt_separator);
 		break;
 	default:
src/scanner.l +5 -0
index 5e848890..def0ac0e 100644
@@ -821,6 +821,11 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"dport"			{ return DPORT; }
 	"ttl"			{ return TTL; }
 	"tos"			{ return TOS; }
+	"version"		{ return HDRVERSION; }
+	"direction"		{ return DIRECTION; }
+	"erspan"		{ return ERSPAN; }
+	"egress"		{ return EGRESS; }
+	"ingress"		{ return INGRESS; }
 }
 
 "notrack"		{ return NOTRACK; }
Signature · 1 line
2.50.1
#2 lore raw

PATCH v3 3/7 src: add tunnel statement and expression support

In reply to Fernando Fernandez Mancera

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to attach tunnel metadata through the tunnel statement.

The following example shows how to redirect traffic to the erspan0 tunnel device which will take the tunnel configuration that is specified by the ruleset.

     table netdev x {
            tunnel y {
                    id 10
                    ip saddr 192.168.2.10
                    ip daddr 192.168.2.11
                    sport 10
                    dport 20
                    ttl 10
                    erspan {
                            version 1
                            index 2
                    }
            }

	    chain x {
		    type filter hook ingress device veth0 priority 0;

		    ip daddr 10.141.10.123 tunnel name y fwd to erspan0
	    }
     }

This patch also allows to match on tunnel metadata via tunnel expression.

Joint work with Fernando.

---
v3: rebased

---
 Makefile.am               |  2 +
 include/expression.h      |  6 +++
 include/tunnel.h          | 33 ++++++++++++++++
 src/evaluate.c            |  8 ++++
 src/expression.c          |  1 +
 src/netlink_delinearize.c | 17 ++++++++
 src/netlink_linearize.c   | 14 +++++++
 src/parser_bison.y        | 33 +++++++++++++---
 src/scanner.l             |  3 +-
 src/statement.c           |  1 +
 src/tunnel.c              | 81 +++++++++++++++++++++++++++++++++++++++
 11 files changed, 193 insertions(+), 6 deletions(-)
 create mode 100644 include/tunnel.h
 create mode 100644 src/tunnel.c
Makefile.am +2 -0
index 4909abfe..152a80d6 100644
@@ -100,6 +100,7 @@ noinst_HEADERS = \
 	include/statement.h \
 	include/tcpopt.h \
 	include/trace.h \
+	include/tunnel.h \
 	include/utils.h \
 	include/xfrm.h \
 	include/xt.h \
@@ -243,6 +244,7 @@ src_libnftables_la_SOURCES = \
 	src/socket.c \
 	src/statement.c \
 	src/tcpopt.c \
+	src/tunnel.c \
 	src/utils.c \
 	src/xfrm.c \
 	$(NULL)
include/expression.h +6 -0
index e483b7e7..7185ee66 100644
@@ -77,6 +77,7 @@ enum expr_types {
 	EXPR_NUMGEN,
 	EXPR_HASH,
 	EXPR_RT,
+	EXPR_TUNNEL,
 	EXPR_FIB,
 	EXPR_XFRM,
 	EXPR_SET_ELEM_CATCHALL,
@@ -229,6 +230,7 @@ enum expr_flags {
 #include <hash.h>
 #include <ct.h>
 #include <socket.h>
+#include <tunnel.h>
 #include <osf.h>
 #include <xfrm.h>
 
@@ -368,6 +370,10 @@ struct expr {
 			enum nft_socket_keys	key;
 			uint32_t		level;
 		} socket;
+		struct {
+			/* EXPR_TUNNEL */
+			enum nft_tunnel_keys	key;
+		} tunnel;
 		struct {
 			/* EXPR_RT */
 			enum nft_rt_keys	key;
include/tunnel.h new file +33 -0
new file mode 100644
index 00000000..9e6bd97a
@@ -0,0 +1,33 @@
+#ifndef NFTABLES_TUNNEL_H
+#define NFTABLES_TUNNEL_H
+
+/**
+ * struct tunnel_template - template for tunnel expressions
+ *
+ * @token:	parser token for the expression
+ * @dtype:	data type of the expression
+ * @len:	length of the expression
+ * @byteorder:	byteorder
+ */
+struct tunnel_template {
+	const char		*token;
+	const struct datatype	*dtype;
+	enum byteorder		byteorder;
+	unsigned int		len;
+};
+
+extern const struct tunnel_template tunnel_templates[];
+
+#define TUNNEL_TEMPLATE(__token, __dtype, __len, __byteorder) {	\
+	.token		= (__token),				\
+	.dtype		= (__dtype),				\
+	.len		= (__len),				\
+	.byteorder	= (__byteorder),			\
+}
+
+extern struct expr *tunnel_expr_alloc(const struct location *loc,
+				      enum nft_tunnel_keys key);
+
+extern const struct expr_ops tunnel_expr_ops;
+
+#endif /* NFTABLES_TUNNEL_H */
src/evaluate.c +8 -0
index da8794dd..6bf14b0c 100644
@@ -1737,6 +1737,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 		case EXPR_SOCKET:
 		case EXPR_OSF:
 		case EXPR_XFRM:
+		case EXPR_TUNNEL:
 			break;
 		case EXPR_RANGE:
 		case EXPR_PREFIX:
@@ -3053,6 +3054,11 @@ static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
 	return expr_evaluate_primary(ctx, expr);
 }
 
+static int expr_evaluate_tunnel(struct eval_ctx *ctx, struct expr **exprp)
+{
+	return expr_evaluate_primary(ctx, exprp);
+}
+
 static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
 {
 	struct symbol *sym = (*exprp)->sym;
@@ -3170,6 +3176,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
 		return expr_evaluate_meta(ctx, expr);
 	case EXPR_SOCKET:
 		return expr_evaluate_socket(ctx, expr);
+	case EXPR_TUNNEL:
+		return expr_evaluate_tunnel(ctx, expr);
 	case EXPR_OSF:
 		return expr_evaluate_osf(ctx, expr);
 	case EXPR_FIB:
src/expression.c +1 -0
index 8cb63979..e3c27a13 100644
@@ -1762,6 +1762,7 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
 	case EXPR_NUMGEN: return &numgen_expr_ops;
 	case EXPR_HASH: return &hash_expr_ops;
 	case EXPR_RT: return &rt_expr_ops;
+	case EXPR_TUNNEL: return &tunnel_expr_ops;
 	case EXPR_FIB: return &fib_expr_ops;
 	case EXPR_XFRM: return &xfrm_expr_ops;
 	case EXPR_SET_ELEM_CATCHALL: return &set_elem_catchall_expr_ops;
src/netlink_delinearize.c +17 -0
index b97962a3..5627826d 100644
@@ -940,6 +940,21 @@ static void netlink_parse_osf(struct netlink_parse_ctx *ctx,
 	netlink_set_register(ctx, dreg, expr);
 }
 
+static void netlink_parse_tunnel(struct netlink_parse_ctx *ctx,
+				 const struct location *loc,
+				 const struct nftnl_expr *nle)
+{
+	enum nft_registers dreg;
+	struct expr * expr;
+	uint32_t key;
+
+	key = nftnl_expr_get_u32(nle, NFTNL_EXPR_TUNNEL_KEY);
+	expr = tunnel_expr_alloc(loc, key);
+
+	dreg = netlink_parse_register(nle, NFTNL_EXPR_TUNNEL_DREG);
+	netlink_set_register(ctx, dreg, expr);
+}
+
 static void netlink_parse_meta_stmt(struct netlink_parse_ctx *ctx,
 				    const struct location *loc,
 				    const struct nftnl_expr *nle)
@@ -1922,6 +1937,7 @@ static const struct expr_handler netlink_parsers[] = {
 	{ .name = "exthdr",	.parse = netlink_parse_exthdr },
 	{ .name = "meta",	.parse = netlink_parse_meta },
 	{ .name = "socket",	.parse = netlink_parse_socket },
+	{ .name = "tunnel",	.parse = netlink_parse_tunnel },
 	{ .name = "osf",	.parse = netlink_parse_osf },
 	{ .name = "rt",		.parse = netlink_parse_rt },
 	{ .name = "ct",		.parse = netlink_parse_ct },
@@ -3023,6 +3039,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
 	case EXPR_NUMGEN:
 	case EXPR_FIB:
 	case EXPR_SOCKET:
+	case EXPR_TUNNEL:
 	case EXPR_OSF:
 	case EXPR_XFRM:
 		break;
src/netlink_linearize.c +14 -0
index 8ac33d34..d01cadf8 100644
@@ -334,6 +334,18 @@ static void netlink_gen_osf(struct netlink_linearize_ctx *ctx,
 	nft_rule_add_expr(ctx, nle, &expr->location);
 }
 
+static void netlink_gen_tunnel(struct netlink_linearize_ctx *ctx,
+			       const struct expr *expr,
+			       enum nft_registers dreg)
+{
+	struct nftnl_expr *nle;
+
+	nle = alloc_nft_expr("tunnel");
+	netlink_put_register(nle, NFTNL_EXPR_TUNNEL_DREG, dreg);
+	nftnl_expr_set_u32(nle, NFTNL_EXPR_TUNNEL_KEY, expr->tunnel.key);
+	nftnl_rule_add_expr(ctx->nlr, nle);
+}
+
 static void netlink_gen_numgen(struct netlink_linearize_ctx *ctx,
 			    const struct expr *expr,
 			    enum nft_registers dreg)
@@ -932,6 +944,8 @@ static void netlink_gen_expr(struct netlink_linearize_ctx *ctx,
 		return netlink_gen_fib(ctx, expr, dreg);
 	case EXPR_SOCKET:
 		return netlink_gen_socket(ctx, expr, dreg);
+	case EXPR_TUNNEL:
+		return netlink_gen_tunnel(ctx, expr, dreg);
 	case EXPR_OSF:
 		return netlink_gen_osf(ctx, expr, dreg);
 	case EXPR_XFRM:
src/parser_bison.y +28 -5
index 557977e2..08d75dbb 100644
@@ -321,6 +321,8 @@ int nft_lex(void *, void *, void *);
 %token RULESET			"ruleset"
 %token TRACE			"trace"
 
+%token PATH			"path"
+
 %token INET			"inet"
 %token NETDEV			"netdev"
 
@@ -779,8 +781,8 @@ int nft_lex(void *, void *, void *);
 %destructor { stmt_free($$); }	counter_stmt counter_stmt_alloc stateful_stmt last_stmt
 %type <stmt>			limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
 %destructor { stmt_free($$); }	limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
-%type <stmt>			objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
-%destructor { stmt_free($$); }	objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
+%type <stmt>			objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
+%destructor { stmt_free($$); }	objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
 
 %type <stmt>			payload_stmt
 %destructor { stmt_free($$); }	payload_stmt
@@ -940,9 +942,9 @@ int nft_lex(void *, void *, void *);
 %destructor { expr_free($$); }	mh_hdr_expr
 %type <val>			mh_hdr_field
 
-%type <expr>			meta_expr
-%destructor { expr_free($$); }	meta_expr
-%type <val>			meta_key	meta_key_qualified	meta_key_unqualified	numgen_type
+%type <expr>			meta_expr	tunnel_expr
+%destructor { expr_free($$); }	meta_expr	tunnel_expr
+%type <val>			meta_key	meta_key_qualified	meta_key_unqualified	numgen_type	tunnel_key
 
 %type <expr>			socket_expr
 %destructor { expr_free($$); } socket_expr
@@ -3206,6 +3208,14 @@ objref_stmt_synproxy	: 	SYNPROXY	NAME	stmt_expr close_scope_synproxy
 			}
 			;
 
+objref_stmt_tunnel	:	TUNNEL	NAME	stmt_expr	close_scope_tunnel
+			{
+				$$ = objref_stmt_alloc(&@$);
+				$$->objref.type = NFT_OBJECT_TUNNEL;
+				$$->objref.expr = $3;
+			}
+			;
+
 objref_stmt_ct		:	CT	TIMEOUT		SET	stmt_expr	close_scope_ct
 			{
 				$$ = objref_stmt_alloc(&@$);
@@ -3226,6 +3236,7 @@ objref_stmt		:	objref_stmt_counter
 			|	objref_stmt_quota
 			|	objref_stmt_synproxy
 			|	objref_stmt_ct
+			|	objref_stmt_tunnel
 			;
 
 stateful_stmt		:	counter_stmt	close_scope_counter
@@ -3904,6 +3915,7 @@ primary_stmt_expr	:	symbol_expr			{ $$ = $1; }
 			|	boolean_expr			{ $$ = $1; }
 			|	meta_expr			{ $$ = $1; }
 			|	rt_expr				{ $$ = $1; }
+			|	tunnel_expr			{ $$ = $1; }
 			|	ct_expr				{ $$ = $1; }
 			|	numgen_expr             	{ $$ = $1; }
 			|	hash_expr               	{ $$ = $1; }
@@ -4381,6 +4393,7 @@ selector_expr		:	payload_expr			{ $$ = $1; }
 			|	exthdr_expr			{ $$ = $1; }
 			|	exthdr_exists_expr		{ $$ = $1; }
 			|	meta_expr			{ $$ = $1; }
+			|	tunnel_expr			{ $$ = $1; }
 			|	socket_expr			{ $$ = $1; }
 			|	rt_expr				{ $$ = $1; }
 			|	ct_expr				{ $$ = $1; }
@@ -5493,6 +5506,16 @@ socket_key 		: 	TRANSPARENT	{ $$ = NFT_SOCKET_TRANSPARENT; }
 			|	WILDCARD	{ $$ = NFT_SOCKET_WILDCARD; }
 			;
 
+tunnel_key		:	PATH		{ $$ = NFT_TUNNEL_PATH; }
+			|	ID		{ $$ = NFT_TUNNEL_ID; }
+			;
+
+tunnel_expr		:	TUNNEL	tunnel_key
+			{
+				$$ = tunnel_expr_alloc(&@$, $2);
+			}
+			;
+
 offset_opt		:	/* empty */	{ $$ = 0; }
 			|	OFFSET	NUM	{ $$ = $2; }
 			;
src/scanner.l +2 -1
index def0ac0e..9695d710 100644
@@ -410,7 +410,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 }
 
 "counter"		{ scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
-<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF>"name"			{ return NAME; }
+<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF,SCANSTATE_TUNNEL>"name"			{ return NAME; }
 <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"		{ return PACKETS; }
 <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"	{ return BYTES; }
 
@@ -826,6 +826,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"erspan"		{ return ERSPAN; }
 	"egress"		{ return EGRESS; }
 	"ingress"		{ return INGRESS; }
+	"path"			{ return PATH; }
 }
 
 "notrack"		{ return NOTRACK; }
src/statement.c +1 -0
index 2bfed4ac..20241f68 100644
@@ -290,6 +290,7 @@ static const char *objref_type[NFT_OBJECT_MAX + 1] = {
 	[NFT_OBJECT_QUOTA]	= "quota",
 	[NFT_OBJECT_CT_HELPER]	= "ct helper",
 	[NFT_OBJECT_LIMIT]	= "limit",
+	[NFT_OBJECT_TUNNEL]	= "tunnel",
 	[NFT_OBJECT_CT_TIMEOUT] = "ct timeout",
 	[NFT_OBJECT_SECMARK]	= "secmark",
 	[NFT_OBJECT_SYNPROXY]	= "synproxy",
src/tunnel.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

Quoted 481 lines · Fernando Fernandez Mancera

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to attach tunnel metadata through the tunnel statement.

The following example shows how to redirect traffic to the erspan0 tunnel device which will take the tunnel configuration that is specified by the ruleset.

     table netdev x {
            tunnel y {
                    id 10
                    ip saddr 192.168.2.10
                    ip daddr 192.168.2.11
                    sport 10
                    dport 20
                    ttl 10
                    erspan {
                            version 1
                            index 2
                    }
            }

            chain x {
                    type filter hook ingress device veth0 priority 0;

                    ip daddr 10.141.10.123 tunnel name y fwd to erspan0
            }
     }

This patch also allows to match on tunnel metadata via tunnel expression.

Joint work with Fernando.

---
v3: rebased

---
 Makefile.am               |  2 +
 include/expression.h      |  6 +++
 include/tunnel.h          | 33 ++++++++++++++++
 src/evaluate.c            |  8 ++++
 src/expression.c          |  1 +
 src/netlink_delinearize.c | 17 ++++++++
 src/netlink_linearize.c   | 14 +++++++
 src/parser_bison.y        | 33 +++++++++++++---
 src/scanner.l             |  3 +-
 src/statement.c           |  1 +
 src/tunnel.c              | 81 +++++++++++++++++++++++++++++++++++++++
 11 files changed, 193 insertions(+), 6 deletions(-)
 create mode 100644 include/tunnel.h
 create mode 100644 src/tunnel.c
Makefile.am +2 -0
index 4909abfe..152a80d6 100644
@@ -100,6 +100,7 @@ noinst_HEADERS = \
        include/statement.h \
        include/tcpopt.h \
        include/trace.h \
+       include/tunnel.h \
        include/utils.h \
        include/xfrm.h \
        include/xt.h \
@@ -243,6 +244,7 @@ src_libnftables_la_SOURCES = \
        src/socket.c \
        src/statement.c \
        src/tcpopt.c \
+       src/tunnel.c \
        src/utils.c \
        src/xfrm.c \
        $(NULL)
include/expression.h +6 -0
index e483b7e7..7185ee66 100644
@@ -77,6 +77,7 @@ enum expr_types {
        EXPR_NUMGEN,
        EXPR_HASH,
        EXPR_RT,
+       EXPR_TUNNEL,
        EXPR_FIB,
        EXPR_XFRM,
        EXPR_SET_ELEM_CATCHALL,
@@ -229,6 +230,7 @@ enum expr_flags {
 #include <hash.h>
 #include <ct.h>
 #include <socket.h>
+#include <tunnel.h>
 #include <osf.h>
 #include <xfrm.h>
 
@@ -368,6 +370,10 @@ struct expr {
                        enum nft_socket_keys    key;
                        uint32_t                level;
                } socket;
+               struct {
+                       /* EXPR_TUNNEL */
+                       enum nft_tunnel_keys    key;
+               } tunnel;
                struct {
                        /* EXPR_RT */
                        enum nft_rt_keys        key;
include/tunnel.h new file +33 -0
new file mode 100644
index 00000000..9e6bd97a
@@ -0,0 +1,33 @@
+#ifndef NFTABLES_TUNNEL_H
+#define NFTABLES_TUNNEL_H
+
+/**
+ * struct tunnel_template - template for tunnel expressions
+ *
+ * @token:     parser token for the expression
+ * @dtype:     data type of the expression
+ * @len:       length of the expression
+ * @byteorder: byteorder
+ */
+struct tunnel_template {
+       const char              *token;
+       const struct datatype   *dtype;
+       enum byteorder          byteorder;
+       unsigned int            len;
+};
+
+extern const struct tunnel_template tunnel_templates[];
+
+#define TUNNEL_TEMPLATE(__token, __dtype, __len, __byteorder) {        \
+       .token          = (__token),                            \
+       .dtype          = (__dtype),                            \
+       .len            = (__len),                              \
+       .byteorder      = (__byteorder),                        \
+}
+
+extern struct expr *tunnel_expr_alloc(const struct location *loc,
+                                     enum nft_tunnel_keys key);
+
+extern const struct expr_ops tunnel_expr_ops;
+
+#endif /* NFTABLES_TUNNEL_H */
src/evaluate.c +8 -0
index da8794dd..6bf14b0c 100644
@@ -1737,6 +1737,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
                case EXPR_SOCKET:
                case EXPR_OSF:
                case EXPR_XFRM:
+               case EXPR_TUNNEL:
                        break;
                case EXPR_RANGE:
                case EXPR_PREFIX:
@@ -3053,6 +3054,11 @@ static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
        return expr_evaluate_primary(ctx, expr);
 }
 
+static int expr_evaluate_tunnel(struct eval_ctx *ctx, struct expr **exprp)
+{
+       return expr_evaluate_primary(ctx, exprp);
+}
+
 static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
 {
        struct symbol *sym = (*exprp)->sym;
@@ -3170,6 +3176,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
                return expr_evaluate_meta(ctx, expr);
        case EXPR_SOCKET:
                return expr_evaluate_socket(ctx, expr);
+       case EXPR_TUNNEL:
+               return expr_evaluate_tunnel(ctx, expr);
        case EXPR_OSF:
                return expr_evaluate_osf(ctx, expr);
        case EXPR_FIB:
src/expression.c +1 -0
index 8cb63979..e3c27a13 100644
@@ -1762,6 +1762,7 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
        case EXPR_NUMGEN: return &numgen_expr_ops;
        case EXPR_HASH: return &hash_expr_ops;
        case EXPR_RT: return &rt_expr_ops;
+       case EXPR_TUNNEL: return &tunnel_expr_ops;
        case EXPR_FIB: return &fib_expr_ops;
        case EXPR_XFRM: return &xfrm_expr_ops;
        case EXPR_SET_ELEM_CATCHALL: return &set_elem_catchall_expr_ops;
src/netlink_delinearize.c +17 -0
index b97962a3..5627826d 100644
@@ -940,6 +940,21 @@ static void netlink_parse_osf(struct netlink_parse_ctx *ctx,
        netlink_set_register(ctx, dreg, expr);
 }
 
+static void netlink_parse_tunnel(struct netlink_parse_ctx *ctx,
+                                const struct location *loc,
+                                const struct nftnl_expr *nle)
+{
+       enum nft_registers dreg;
+       struct expr * expr;
+       uint32_t key;
+
+       key = nftnl_expr_get_u32(nle, NFTNL_EXPR_TUNNEL_KEY);
+       expr = tunnel_expr_alloc(loc, key);
+
+       dreg = netlink_parse_register(nle, NFTNL_EXPR_TUNNEL_DREG);
+       netlink_set_register(ctx, dreg, expr);
+}
+
 static void netlink_parse_meta_stmt(struct netlink_parse_ctx *ctx,
                                    const struct location *loc,
                                    const struct nftnl_expr *nle)
@@ -1922,6 +1937,7 @@ static const struct expr_handler netlink_parsers[] = {
        { .name = "exthdr",     .parse = netlink_parse_exthdr },
        { .name = "meta",       .parse = netlink_parse_meta },
        { .name = "socket",     .parse = netlink_parse_socket },
+       { .name = "tunnel",     .parse = netlink_parse_tunnel },
        { .name = "osf",        .parse = netlink_parse_osf },
        { .name = "rt",         .parse = netlink_parse_rt },
        { .name = "ct",         .parse = netlink_parse_ct },
@@ -3023,6 +3039,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
        case EXPR_NUMGEN:
        case EXPR_FIB:
        case EXPR_SOCKET:
+       case EXPR_TUNNEL:
        case EXPR_OSF:
        case EXPR_XFRM:
                break;
src/netlink_linearize.c +14 -0
index 8ac33d34..d01cadf8 100644
@@ -334,6 +334,18 @@ static void netlink_gen_osf(struct netlink_linearize_ctx *ctx,
        nft_rule_add_expr(ctx, nle, &expr->location);
 }
 
+static void netlink_gen_tunnel(struct netlink_linearize_ctx *ctx,
+                              const struct expr *expr,
+                              enum nft_registers dreg)
+{
+       struct nftnl_expr *nle;
+
+       nle = alloc_nft_expr("tunnel");
+       netlink_put_register(nle, NFTNL_EXPR_TUNNEL_DREG, dreg);
+       nftnl_expr_set_u32(nle, NFTNL_EXPR_TUNNEL_KEY, expr->tunnel.key);
+       nftnl_rule_add_expr(ctx->nlr, nle);
+}
+
 static void netlink_gen_numgen(struct netlink_linearize_ctx *ctx,
                            const struct expr *expr,
                            enum nft_registers dreg)
@@ -932,6 +944,8 @@ static void netlink_gen_expr(struct netlink_linearize_ctx *ctx,
                return netlink_gen_fib(ctx, expr, dreg);
        case EXPR_SOCKET:
                return netlink_gen_socket(ctx, expr, dreg);
+       case EXPR_TUNNEL:
+               return netlink_gen_tunnel(ctx, expr, dreg);
        case EXPR_OSF:
                return netlink_gen_osf(ctx, expr, dreg);
        case EXPR_XFRM:
src/parser_bison.y +28 -5
index 557977e2..08d75dbb 100644
@@ -321,6 +321,8 @@ int nft_lex(void *, void *, void *);
 %token RULESET                 "ruleset"
 %token TRACE                   "trace"
 
+%token PATH                    "path"
+
 %token INET                    "inet"
 %token NETDEV                  "netdev"
 
@@ -779,8 +781,8 @@ int nft_lex(void *, void *, void *);
 %destructor { stmt_free($$); } counter_stmt counter_stmt_alloc stateful_stmt last_stmt
 %type <stmt>                   limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
 %destructor { stmt_free($$); } limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
-%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
-%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
+%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
+%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
 
 %type <stmt>                   payload_stmt
 %destructor { stmt_free($$); } payload_stmt
@@ -940,9 +942,9 @@ int nft_lex(void *, void *, void *);
 %destructor { expr_free($$); } mh_hdr_expr
 %type <val>                    mh_hdr_field
 
-%type <expr>                   meta_expr
-%destructor { expr_free($$); } meta_expr
-%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type
+%type <expr>                   meta_expr       tunnel_expr
+%destructor { expr_free($$); } meta_expr       tunnel_expr
+%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type     tunnel_key
 
 %type <expr>                   socket_expr
 %destructor { expr_free($$); } socket_expr
@@ -3206,6 +3208,14 @@ objref_stmt_synproxy     :       SYNPROXY        NAME    stmt_expr close_scope_synproxy
                        }
                        ;
 
+objref_stmt_tunnel     :       TUNNEL  NAME    stmt_expr       close_scope_tunnel
+                       {
+                               $$ = objref_stmt_alloc(&@$);
+                               $$->objref.type = NFT_OBJECT_TUNNEL;
+                               $$->objref.expr = $3;
+                       }
+                       ;
+
 objref_stmt_ct         :       CT      TIMEOUT         SET     stmt_expr       close_scope_ct
                        {
                                $$ = objref_stmt_alloc(&@$);
@@ -3226,6 +3236,7 @@ objref_stmt               :       objref_stmt_counter
                        |       objref_stmt_quota
                        |       objref_stmt_synproxy
                        |       objref_stmt_ct
+                       |       objref_stmt_tunnel
                        ;
 
 stateful_stmt          :       counter_stmt    close_scope_counter
@@ -3904,6 +3915,7 @@ primary_stmt_expr :       symbol_expr                     { $$ = $1; }
                        |       boolean_expr                    { $$ = $1; }
                        |       meta_expr                       { $$ = $1; }
                        |       rt_expr                         { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                        |       ct_expr                         { $$ = $1; }
                        |       numgen_expr                     { $$ = $1; }
                        |       hash_expr                       { $$ = $1; }
@@ -4381,6 +4393,7 @@ selector_expr             :       payload_expr                    { $$ = $1; }
                        |       exthdr_expr                     { $$ = $1; }
                        |       exthdr_exists_expr              { $$ = $1; }
                        |       meta_expr                       { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                        |       socket_expr                     { $$ = $1; }
                        |       rt_expr                         { $$ = $1; }
                        |       ct_expr                         { $$ = $1; }
@@ -5493,6 +5506,16 @@ socket_key               :       TRANSPARENT     { $$ = NFT_SOCKET_TRANSPARENT; }
                        |       WILDCARD        { $$ = NFT_SOCKET_WILDCARD; }
                        ;
 
+tunnel_key             :       PATH            { $$ = NFT_TUNNEL_PATH; }
+                       |       ID              { $$ = NFT_TUNNEL_ID; }
+                       ;
+
+tunnel_expr            :       TUNNEL  tunnel_key
+                       {
+                               $$ = tunnel_expr_alloc(&@$, $2);
+                       }
+                       ;
+
 offset_opt             :       /* empty */     { $$ = 0; }
                        |       OFFSET  NUM     { $$ = $2; }
                        ;
src/scanner.l +2 -1
index def0ac0e..9695d710 100644
@@ -410,7 +410,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
 }
 
 "counter"              { scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
-<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF>"name"                   { return NAME; }
+<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF,SCANSTATE_TUNNEL>"name"                  { return NAME; }
 <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"              { return PACKETS; }
 <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"        { return BYTES; }
 
@@ -826,6 +826,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
        "erspan"                { return ERSPAN; }
        "egress"                { return EGRESS; }
        "ingress"               { return INGRESS; }
+       "path"                  { return PATH; }
 }
 
 "notrack"              { return NOTRACK; }
src/statement.c +1 -0
index 2bfed4ac..20241f68 100644
@@ -290,6 +290,7 @@ static const char *objref_type[NFT_OBJECT_MAX + 1] = {
        [NFT_OBJECT_QUOTA]      = "quota",
        [NFT_OBJECT_CT_HELPER]  = "ct helper",
        [NFT_OBJECT_LIMIT]      = "limit",
+       [NFT_OBJECT_TUNNEL]     = "tunnel",
        [NFT_OBJECT_CT_TIMEOUT] = "ct timeout",
        [NFT_OBJECT_SECMARK]    = "secmark",
        [NFT_OBJECT_SYNPROXY]   = "synproxy",
src/tunnel.c new file +81 -1
new file mode 100644
index 00000000..cd92d039
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018 Pablo Neira Ayuso <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <pwd.h>
+#include <grp.h>
+#include <arpa/inet.h>
+#include <linux/netfilter.h>
+#include <linux/pkt_sched.h>
+#include <linux/if_packet.h>
+
+#include <nftables.h>
+#include <expression.h>
+#include <datatype.h>
+#include <tunnel.h>
+#include <gmputil.h>
+#include <utils.h>
+#include <erec.h>
+
+const struct tunnel_template tunnel_templates[] = {
+       [NFT_TUNNEL_PATH]       = META_TEMPLATE("path", &boolean_type,
+                                               BITS_PER_BYTE, BYTEORDER_HOST_ENDIAN),
+       [NFT_TUNNEL_ID]         = META_TEMPLATE("id",  &integer_type,
+                                               4 * 8, BYTEORDER_HOST_ENDIAN),
+};
+
+static void tunnel_expr_print(const struct expr *expr, struct output_ctx *octx)
+{
+       uint32_t key = expr->tunnel.key;
+       const char *token = "unknown";
+
+       if (key < array_size(tunnel_templates))
+               token = tunnel_templates[key].token;
+
+       nft_print(octx, "tunnel %s", token);
+}
+
+static bool tunnel_expr_cmp(const struct expr *e1, const struct expr *e2)
+{
+       return e1->tunnel.key == e2->tunnel.key;
+}
+
+static void tunnel_expr_clone(struct expr *new, const struct expr *expr)
+{
+       new->tunnel.key = expr->tunnel.key;
+}
+
+const struct expr_ops tunnel_expr_ops = {
+       .type           = EXPR_TUNNEL,
+       .name           = "tunnel",
+       .print          = tunnel_expr_print,
+       .cmp            = tunnel_expr_cmp,
+       .clone          = tunnel_expr_clone,
+};
+
+struct expr *tunnel_expr_alloc(const struct location *loc,
+                              enum nft_tunnel_keys key)
+{
+       const struct tunnel_template *tmpl = &tunnel_templates[key];
+       struct expr *expr;
+
+       expr = expr_alloc(loc, EXPR_TUNNEL, tmpl->dtype, tmpl->byteorder,
+                         tmpl->len);
+       expr->tunnel.key = key;
+
+       return expr;
+}
--

2.50.1

#4 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

Quoted 5 lines · Yi Chen

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

Quoted 7 lines

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

Hi Yi Chen,

I am working on a patch adding proper documentation to nft manpage that will clarify all these questions. I plan to send it out this week. I will CC you in the patch.

Thanks,
Fernando.

Quoted 484 lines
Quoted 481 lines · Fernando Fernandez Mancera

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to attach tunnel metadata through the tunnel statement.

The following example shows how to redirect traffic to the erspan0 tunnel device which will take the tunnel configuration that is specified by the ruleset.

      table netdev x {
             tunnel y {
                     id 10
                     ip saddr 192.168.2.10
                     ip daddr 192.168.2.11
                     sport 10
                     dport 20
                     ttl 10
                     erspan {
                             version 1
                             index 2
                     }
             }

             chain x {
                     type filter hook ingress device veth0 priority 0;

                     ip daddr 10.141.10.123 tunnel name y fwd to erspan0
             }
      }

This patch also allows to match on tunnel metadata via tunnel expression.

Joint work with Fernando.

---
v3: rebased

---
  Makefile.am               |  2 +
  include/expression.h      |  6 +++
  include/tunnel.h          | 33 ++++++++++++++++
  src/evaluate.c            |  8 ++++
  src/expression.c          |  1 +
  src/netlink_delinearize.c | 17 ++++++++
  src/netlink_linearize.c   | 14 +++++++
  src/parser_bison.y        | 33 +++++++++++++---
  src/scanner.l             |  3 +-
  src/statement.c           |  1 +
  src/tunnel.c              | 81 +++++++++++++++++++++++++++++++++++++++
  11 files changed, 193 insertions(+), 6 deletions(-)

create mode 100644 include/tunnel.h
create mode 100644 src/tunnel.c

Makefile.am +2 -0
index 4909abfe..152a80d6 100644
@@ -100,6 +100,7 @@ noinst_HEADERS = \
         include/statement.h \
         include/tcpopt.h \
         include/trace.h \
+       include/tunnel.h \
         include/utils.h \
         include/xfrm.h \
         include/xt.h \
@@ -243,6 +244,7 @@ src_libnftables_la_SOURCES = \
         src/socket.c \
         src/statement.c \
         src/tcpopt.c \
+       src/tunnel.c \
         src/utils.c \
         src/xfrm.c \
         $(NULL)
include/expression.h +6 -0
index e483b7e7..7185ee66 100644
@@ -77,6 +77,7 @@ enum expr_types {
         EXPR_NUMGEN,
         EXPR_HASH,
         EXPR_RT,
+       EXPR_TUNNEL,
         EXPR_FIB,
         EXPR_XFRM,
         EXPR_SET_ELEM_CATCHALL,
@@ -229,6 +230,7 @@ enum expr_flags {
  #include <hash.h>
  #include <ct.h>
  #include <socket.h>
+#include <tunnel.h>
  #include <osf.h>
  #include <xfrm.h>
 
@@ -368,6 +370,10 @@ struct expr {
                         enum nft_socket_keys    key;
                         uint32_t                level;
                 } socket;
+               struct {
+                       /* EXPR_TUNNEL */
+                       enum nft_tunnel_keys    key;
+               } tunnel;
                 struct {
                         /* EXPR_RT */
                         enum nft_rt_keys        key;
include/tunnel.h new file +33 -0
new file mode 100644
index 00000000..9e6bd97a
@@ -0,0 +1,33 @@
+#ifndef NFTABLES_TUNNEL_H
+#define NFTABLES_TUNNEL_H
+
+/**
+ * struct tunnel_template - template for tunnel expressions
+ *
+ * @token:     parser token for the expression
+ * @dtype:     data type of the expression
+ * @len:       length of the expression
+ * @byteorder: byteorder
+ */
+struct tunnel_template {
+       const char              *token;
+       const struct datatype   *dtype;
+       enum byteorder          byteorder;
+       unsigned int            len;
+};
+
+extern const struct tunnel_template tunnel_templates[];
+
+#define TUNNEL_TEMPLATE(__token, __dtype, __len, __byteorder) {        \
+       .token          = (__token),                            \
+       .dtype          = (__dtype),                            \
+       .len            = (__len),                              \
+       .byteorder      = (__byteorder),                        \
+}
+
+extern struct expr *tunnel_expr_alloc(const struct location *loc,
+                                     enum nft_tunnel_keys key);
+
+extern const struct expr_ops tunnel_expr_ops;
+
+#endif /* NFTABLES_TUNNEL_H */
src/evaluate.c +8 -0
index da8794dd..6bf14b0c 100644
@@ -1737,6 +1737,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
                 case EXPR_SOCKET:
                 case EXPR_OSF:
                 case EXPR_XFRM:
+               case EXPR_TUNNEL:
                         break;
                 case EXPR_RANGE:
                 case EXPR_PREFIX:
@@ -3053,6 +3054,11 @@ static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
         return expr_evaluate_primary(ctx, expr);
  }
 
+static int expr_evaluate_tunnel(struct eval_ctx *ctx, struct expr **exprp)
+{
+       return expr_evaluate_primary(ctx, exprp);
+}
+
  static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
  {
         struct symbol *sym = (*exprp)->sym;
@@ -3170,6 +3176,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
                 return expr_evaluate_meta(ctx, expr);
         case EXPR_SOCKET:
                 return expr_evaluate_socket(ctx, expr);
+       case EXPR_TUNNEL:
+               return expr_evaluate_tunnel(ctx, expr);
         case EXPR_OSF:
                 return expr_evaluate_osf(ctx, expr);
         case EXPR_FIB:
src/expression.c +1 -0
index 8cb63979..e3c27a13 100644
@@ -1762,6 +1762,7 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
         case EXPR_NUMGEN: return &numgen_expr_ops;
         case EXPR_HASH: return &hash_expr_ops;
         case EXPR_RT: return &rt_expr_ops;
+       case EXPR_TUNNEL: return &tunnel_expr_ops;
         case EXPR_FIB: return &fib_expr_ops;
         case EXPR_XFRM: return &xfrm_expr_ops;
         case EXPR_SET_ELEM_CATCHALL: return &set_elem_catchall_expr_ops;
src/netlink_delinearize.c +17 -0
index b97962a3..5627826d 100644
@@ -940,6 +940,21 @@ static void netlink_parse_osf(struct netlink_parse_ctx *ctx,
         netlink_set_register(ctx, dreg, expr);
  }
 
+static void netlink_parse_tunnel(struct netlink_parse_ctx *ctx,
+                                const struct location *loc,
+                                const struct nftnl_expr *nle)
+{
+       enum nft_registers dreg;
+       struct expr * expr;
+       uint32_t key;
+
+       key = nftnl_expr_get_u32(nle, NFTNL_EXPR_TUNNEL_KEY);
+       expr = tunnel_expr_alloc(loc, key);
+
+       dreg = netlink_parse_register(nle, NFTNL_EXPR_TUNNEL_DREG);
+       netlink_set_register(ctx, dreg, expr);
+}
+
  static void netlink_parse_meta_stmt(struct netlink_parse_ctx *ctx,
                                     const struct location *loc,
                                     const struct nftnl_expr *nle)
@@ -1922,6 +1937,7 @@ static const struct expr_handler netlink_parsers[] = {
         { .name = "exthdr",     .parse = netlink_parse_exthdr },
         { .name = "meta",       .parse = netlink_parse_meta },
         { .name = "socket",     .parse = netlink_parse_socket },
+       { .name = "tunnel",     .parse = netlink_parse_tunnel },
         { .name = "osf",        .parse = netlink_parse_osf },
         { .name = "rt",         .parse = netlink_parse_rt },
         { .name = "ct",         .parse = netlink_parse_ct },
@@ -3023,6 +3039,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
         case EXPR_NUMGEN:
         case EXPR_FIB:
         case EXPR_SOCKET:
+       case EXPR_TUNNEL:
         case EXPR_OSF:
         case EXPR_XFRM:
                 break;
src/netlink_linearize.c +14 -0
index 8ac33d34..d01cadf8 100644
@@ -334,6 +334,18 @@ static void netlink_gen_osf(struct netlink_linearize_ctx *ctx,
         nft_rule_add_expr(ctx, nle, &expr->location);
  }
 
+static void netlink_gen_tunnel(struct netlink_linearize_ctx *ctx,
+                              const struct expr *expr,
+                              enum nft_registers dreg)
+{
+       struct nftnl_expr *nle;
+
+       nle = alloc_nft_expr("tunnel");
+       netlink_put_register(nle, NFTNL_EXPR_TUNNEL_DREG, dreg);
+       nftnl_expr_set_u32(nle, NFTNL_EXPR_TUNNEL_KEY, expr->tunnel.key);
+       nftnl_rule_add_expr(ctx->nlr, nle);
+}
+
  static void netlink_gen_numgen(struct netlink_linearize_ctx *ctx,
                             const struct expr *expr,
                             enum nft_registers dreg)
@@ -932,6 +944,8 @@ static void netlink_gen_expr(struct netlink_linearize_ctx *ctx,
                 return netlink_gen_fib(ctx, expr, dreg);
         case EXPR_SOCKET:
                 return netlink_gen_socket(ctx, expr, dreg);
+       case EXPR_TUNNEL:
+               return netlink_gen_tunnel(ctx, expr, dreg);
         case EXPR_OSF:
                 return netlink_gen_osf(ctx, expr, dreg);
         case EXPR_XFRM:
src/parser_bison.y +28 -5
index 557977e2..08d75dbb 100644
@@ -321,6 +321,8 @@ int nft_lex(void *, void *, void *);
  %token RULESET                 "ruleset"
  %token TRACE                   "trace"
 
+%token PATH                    "path"
+
  %token INET                    "inet"
  %token NETDEV                  "netdev"
 
@@ -779,8 +781,8 @@ int nft_lex(void *, void *, void *);
  %destructor { stmt_free($$); } counter_stmt counter_stmt_alloc stateful_stmt last_stmt
  %type <stmt>                   limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
  %destructor { stmt_free($$); } limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
-%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
-%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
+%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
+%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
 
  %type <stmt>                   payload_stmt
  %destructor { stmt_free($$); } payload_stmt
@@ -940,9 +942,9 @@ int nft_lex(void *, void *, void *);
  %destructor { expr_free($$); } mh_hdr_expr
  %type <val>                    mh_hdr_field
 
-%type <expr>                   meta_expr
-%destructor { expr_free($$); } meta_expr
-%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type
+%type <expr>                   meta_expr       tunnel_expr
+%destructor { expr_free($$); } meta_expr       tunnel_expr
+%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type     tunnel_key
 
  %type <expr>                   socket_expr
  %destructor { expr_free($$); } socket_expr
@@ -3206,6 +3208,14 @@ objref_stmt_synproxy     :       SYNPROXY        NAME    stmt_expr close_scope_synproxy
                         }
                         ;
 
+objref_stmt_tunnel     :       TUNNEL  NAME    stmt_expr       close_scope_tunnel
+                       {
+                               $$ = objref_stmt_alloc(&@$);
+                               $$->objref.type = NFT_OBJECT_TUNNEL;
+                               $$->objref.expr = $3;
+                       }
+                       ;
+
  objref_stmt_ct         :       CT      TIMEOUT         SET     stmt_expr       close_scope_ct
                         {
                                 $$ = objref_stmt_alloc(&@$);
@@ -3226,6 +3236,7 @@ objref_stmt               :       objref_stmt_counter
                         |       objref_stmt_quota
                         |       objref_stmt_synproxy
                         |       objref_stmt_ct
+                       |       objref_stmt_tunnel
                         ;
 
  stateful_stmt          :       counter_stmt    close_scope_counter
@@ -3904,6 +3915,7 @@ primary_stmt_expr :       symbol_expr                     { $$ = $1; }
                         |       boolean_expr                    { $$ = $1; }
                         |       meta_expr                       { $$ = $1; }
                         |       rt_expr                         { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                         |       ct_expr                         { $$ = $1; }
                         |       numgen_expr                     { $$ = $1; }
                         |       hash_expr                       { $$ = $1; }
@@ -4381,6 +4393,7 @@ selector_expr             :       payload_expr                    { $$ = $1; }
                         |       exthdr_expr                     { $$ = $1; }
                         |       exthdr_exists_expr              { $$ = $1; }
                         |       meta_expr                       { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                         |       socket_expr                     { $$ = $1; }
                         |       rt_expr                         { $$ = $1; }
                         |       ct_expr                         { $$ = $1; }
@@ -5493,6 +5506,16 @@ socket_key               :       TRANSPARENT     { $$ = NFT_SOCKET_TRANSPARENT; }
                         |       WILDCARD        { $$ = NFT_SOCKET_WILDCARD; }
                         ;
 
+tunnel_key             :       PATH            { $$ = NFT_TUNNEL_PATH; }
+                       |       ID              { $$ = NFT_TUNNEL_ID; }
+                       ;
+
+tunnel_expr            :       TUNNEL  tunnel_key
+                       {
+                               $$ = tunnel_expr_alloc(&@$, $2);
+                       }
+                       ;
+
  offset_opt             :       /* empty */     { $$ = 0; }
                         |       OFFSET  NUM     { $$ = $2; }
                         ;
src/scanner.l +2 -1
index def0ac0e..9695d710 100644
@@ -410,7 +410,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
  }
 
  "counter"              { scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
-<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF>"name"                   { return NAME; }
+<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF,SCANSTATE_TUNNEL>"name"                  { return NAME; }
  <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"              { return PACKETS; }
  <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"        { return BYTES; }
 
@@ -826,6 +826,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
         "erspan"                { return ERSPAN; }
         "egress"                { return EGRESS; }
         "ingress"               { return INGRESS; }
+       "path"                  { return PATH; }
  }
 
  "notrack"              { return NOTRACK; }
src/statement.c +1 -0
index 2bfed4ac..20241f68 100644
@@ -290,6 +290,7 @@ static const char *objref_type[NFT_OBJECT_MAX + 1] = {
         [NFT_OBJECT_QUOTA]      = "quota",
         [NFT_OBJECT_CT_HELPER]  = "ct helper",
         [NFT_OBJECT_LIMIT]      = "limit",
+       [NFT_OBJECT_TUNNEL]     = "tunnel",
         [NFT_OBJECT_CT_TIMEOUT] = "ct timeout",
         [NFT_OBJECT_SECMARK]    = "secmark",
         [NFT_OBJECT_SYNPROXY]   = "synproxy",
src/tunnel.c new file +81 -1
new file mode 100644
index 00000000..cd92d039
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018 Pablo Neira Ayuso <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <pwd.h>
+#include <grp.h>
+#include <arpa/inet.h>
+#include <linux/netfilter.h>
+#include <linux/pkt_sched.h>
+#include <linux/if_packet.h>
+
+#include <nftables.h>
+#include <expression.h>
+#include <datatype.h>
+#include <tunnel.h>
+#include <gmputil.h>
+#include <utils.h>
+#include <erec.h>
+
+const struct tunnel_template tunnel_templates[] = {
+       [NFT_TUNNEL_PATH]       = META_TEMPLATE("path", &boolean_type,
+                                               BITS_PER_BYTE, BYTEORDER_HOST_ENDIAN),
+       [NFT_TUNNEL_ID]         = META_TEMPLATE("id",  &integer_type,
+                                               4 * 8, BYTEORDER_HOST_ENDIAN),
+};
+
+static void tunnel_expr_print(const struct expr *expr, struct output_ctx *octx)
+{
+       uint32_t key = expr->tunnel.key;
+       const char *token = "unknown";
+
+       if (key < array_size(tunnel_templates))
+               token = tunnel_templates[key].token;
+
+       nft_print(octx, "tunnel %s", token);
+}
+
+static bool tunnel_expr_cmp(const struct expr *e1, const struct expr *e2)
+{
+       return e1->tunnel.key == e2->tunnel.key;
+}
+
+static void tunnel_expr_clone(struct expr *new, const struct expr *expr)
+{
+       new->tunnel.key = expr->tunnel.key;
+}
+
+const struct expr_ops tunnel_expr_ops = {
+       .type           = EXPR_TUNNEL,
+       .name           = "tunnel",
+       .print          = tunnel_expr_print,
+       .cmp            = tunnel_expr_cmp,
+       .clone          = tunnel_expr_clone,
+};
+
+struct expr *tunnel_expr_alloc(const struct location *loc,
+                              enum nft_tunnel_keys key)
+{
+       const struct tunnel_template *tmpl = &tunnel_templates[key];
+       struct expr *expr;
+
+       expr = expr_alloc(loc, EXPR_TUNNEL, tmpl->dtype, tmpl->byteorder,
+                         tmpl->len);
+       expr->tunnel.key = key;
+
+       return expr;
+}
--

2.50.1

#5 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

In reply to Yi Chen
Quoted 5 lines · Yi Chen

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

Quoted 7 lines

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

Hi Yi Chen,

As my patch is taking longer than expected because I am polishing all the details related to the tunnel object let me explain it here briefly to unblock you.

The tunnel expression/object is used to attach tunnel metadata into a packet so in essence support Lightweight Tunneling (LWT) using Nftables. The LWT support is useful on virtualization environments where the users need to created a lot of tunnels to interconnect containers that are inside different VMs. Instead of creating one interface per container, the idea is that the user can create a single one and then attach the metadata as needed. Imagine the topology described below.

+------------------------+                   +------------------------+
|--------+          VM A |                   | VM B          +--------|
|Box     | +------+ +---+|(192.168.124.49)   +----+ +------+ |Box     |
|10.0.0.1|-|vxlan0|-|eth0|-------------------|eth0|-|vxlan0|-|10.0.0.2|
|--------+ +------+ +---+|  (192.168.124.134)+----+ +------+ +--------|
|                        |                   |                        |
|                        |                   |                        |
+------------------------+                   +------------------------+

We want to reach 10.0.0.2 from 10.0.0.1, the nftables ruleset on VM A will look like this:

```
table netdev filter_tunnel {
	tunnel vxlan_tmpl {
		id 100
		ip saddr 192.168.124.49
		ip daddr 192.168.124.134
		dport 8472
		ttl 255
		vxlan {
			gbp 100
		}
	}

	chain redirect_to_tunnel {
		type filter hook ingress device "veth_host" priority filter; policy 
accept;
		ip daddr 10.0.0.2 tunnel name "vxlan_tmpl" fwd to "vxlan0"
	}

	chain redirect_from_tunnel {
		type filter hook ingress device "vxlan0" priority filter; policy accept;
		ip daddr 10.0.0.1 fwd to "veth_host"
	}
}
```

On VM B the ruleset will be exactly the same but swapping saddr/daddr everywhere, both the external and internal one.

The idea behind this feature is to scale up the number of containers/namespaces without creating more VXLAN interfaces. Also, keep on mind that you need to mark the VXLAN as external when creating it, i.e

ip link add dev vxlan0 type vxlan external

After that, you should be able to ping between them:

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.198 ms 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.364 ms 64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.369 ms

Please, let me know if something is missing.

Thanks,
Fernando.

Quoted 484 lines
Quoted 481 lines · Fernando Fernandez Mancera

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to attach tunnel metadata through the tunnel statement.

The following example shows how to redirect traffic to the erspan0 tunnel device which will take the tunnel configuration that is specified by the ruleset.

      table netdev x {
             tunnel y {
                     id 10
                     ip saddr 192.168.2.10
                     ip daddr 192.168.2.11
                     sport 10
                     dport 20
                     ttl 10
                     erspan {
                             version 1
                             index 2
                     }
             }

             chain x {
                     type filter hook ingress device veth0 priority 0;

                     ip daddr 10.141.10.123 tunnel name y fwd to erspan0
             }
      }

This patch also allows to match on tunnel metadata via tunnel expression.

Joint work with Fernando.

---
v3: rebased

---
  Makefile.am               |  2 +
  include/expression.h      |  6 +++
  include/tunnel.h          | 33 ++++++++++++++++
  src/evaluate.c            |  8 ++++
  src/expression.c          |  1 +
  src/netlink_delinearize.c | 17 ++++++++
  src/netlink_linearize.c   | 14 +++++++
  src/parser_bison.y        | 33 +++++++++++++---
  src/scanner.l             |  3 +-
  src/statement.c           |  1 +
  src/tunnel.c              | 81 +++++++++++++++++++++++++++++++++++++++
  11 files changed, 193 insertions(+), 6 deletions(-)

create mode 100644 include/tunnel.h
create mode 100644 src/tunnel.c

Makefile.am +2 -0
index 4909abfe..152a80d6 100644
@@ -100,6 +100,7 @@ noinst_HEADERS = \
         include/statement.h \
         include/tcpopt.h \
         include/trace.h \
+       include/tunnel.h \
         include/utils.h \
         include/xfrm.h \
         include/xt.h \
@@ -243,6 +244,7 @@ src_libnftables_la_SOURCES = \
         src/socket.c \
         src/statement.c \
         src/tcpopt.c \
+       src/tunnel.c \
         src/utils.c \
         src/xfrm.c \
         $(NULL)
include/expression.h +6 -0
index e483b7e7..7185ee66 100644
@@ -77,6 +77,7 @@ enum expr_types {
         EXPR_NUMGEN,
         EXPR_HASH,
         EXPR_RT,
+       EXPR_TUNNEL,
         EXPR_FIB,
         EXPR_XFRM,
         EXPR_SET_ELEM_CATCHALL,
@@ -229,6 +230,7 @@ enum expr_flags {
  #include <hash.h>
  #include <ct.h>
  #include <socket.h>
+#include <tunnel.h>
  #include <osf.h>
  #include <xfrm.h>
 
@@ -368,6 +370,10 @@ struct expr {
                         enum nft_socket_keys    key;
                         uint32_t                level;
                 } socket;
+               struct {
+                       /* EXPR_TUNNEL */
+                       enum nft_tunnel_keys    key;
+               } tunnel;
                 struct {
                         /* EXPR_RT */
                         enum nft_rt_keys        key;
include/tunnel.h new file +33 -0
new file mode 100644
index 00000000..9e6bd97a
@@ -0,0 +1,33 @@
+#ifndef NFTABLES_TUNNEL_H
+#define NFTABLES_TUNNEL_H
+
+/**
+ * struct tunnel_template - template for tunnel expressions
+ *
+ * @token:     parser token for the expression
+ * @dtype:     data type of the expression
+ * @len:       length of the expression
+ * @byteorder: byteorder
+ */
+struct tunnel_template {
+       const char              *token;
+       const struct datatype   *dtype;
+       enum byteorder          byteorder;
+       unsigned int            len;
+};
+
+extern const struct tunnel_template tunnel_templates[];
+
+#define TUNNEL_TEMPLATE(__token, __dtype, __len, __byteorder) {        \
+       .token          = (__token),                            \
+       .dtype          = (__dtype),                            \
+       .len            = (__len),                              \
+       .byteorder      = (__byteorder),                        \
+}
+
+extern struct expr *tunnel_expr_alloc(const struct location *loc,
+                                     enum nft_tunnel_keys key);
+
+extern const struct expr_ops tunnel_expr_ops;
+
+#endif /* NFTABLES_TUNNEL_H */
src/evaluate.c +8 -0
index da8794dd..6bf14b0c 100644
@@ -1737,6 +1737,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
                 case EXPR_SOCKET:
                 case EXPR_OSF:
                 case EXPR_XFRM:
+               case EXPR_TUNNEL:
                         break;
                 case EXPR_RANGE:
                 case EXPR_PREFIX:
@@ -3053,6 +3054,11 @@ static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
         return expr_evaluate_primary(ctx, expr);
  }
 
+static int expr_evaluate_tunnel(struct eval_ctx *ctx, struct expr **exprp)
+{
+       return expr_evaluate_primary(ctx, exprp);
+}
+
  static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
  {
         struct symbol *sym = (*exprp)->sym;
@@ -3170,6 +3176,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
                 return expr_evaluate_meta(ctx, expr);
         case EXPR_SOCKET:
                 return expr_evaluate_socket(ctx, expr);
+       case EXPR_TUNNEL:
+               return expr_evaluate_tunnel(ctx, expr);
         case EXPR_OSF:
                 return expr_evaluate_osf(ctx, expr);
         case EXPR_FIB:
src/expression.c +1 -0
index 8cb63979..e3c27a13 100644
@@ -1762,6 +1762,7 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
         case EXPR_NUMGEN: return &numgen_expr_ops;
         case EXPR_HASH: return &hash_expr_ops;
         case EXPR_RT: return &rt_expr_ops;
+       case EXPR_TUNNEL: return &tunnel_expr_ops;
         case EXPR_FIB: return &fib_expr_ops;
         case EXPR_XFRM: return &xfrm_expr_ops;
         case EXPR_SET_ELEM_CATCHALL: return &set_elem_catchall_expr_ops;
src/netlink_delinearize.c +17 -0
index b97962a3..5627826d 100644
@@ -940,6 +940,21 @@ static void netlink_parse_osf(struct netlink_parse_ctx *ctx,
         netlink_set_register(ctx, dreg, expr);
  }
 
+static void netlink_parse_tunnel(struct netlink_parse_ctx *ctx,
+                                const struct location *loc,
+                                const struct nftnl_expr *nle)
+{
+       enum nft_registers dreg;
+       struct expr * expr;
+       uint32_t key;
+
+       key = nftnl_expr_get_u32(nle, NFTNL_EXPR_TUNNEL_KEY);
+       expr = tunnel_expr_alloc(loc, key);
+
+       dreg = netlink_parse_register(nle, NFTNL_EXPR_TUNNEL_DREG);
+       netlink_set_register(ctx, dreg, expr);
+}
+
  static void netlink_parse_meta_stmt(struct netlink_parse_ctx *ctx,
                                     const struct location *loc,
                                     const struct nftnl_expr *nle)
@@ -1922,6 +1937,7 @@ static const struct expr_handler netlink_parsers[] = {
         { .name = "exthdr",     .parse = netlink_parse_exthdr },
         { .name = "meta",       .parse = netlink_parse_meta },
         { .name = "socket",     .parse = netlink_parse_socket },
+       { .name = "tunnel",     .parse = netlink_parse_tunnel },
         { .name = "osf",        .parse = netlink_parse_osf },
         { .name = "rt",         .parse = netlink_parse_rt },
         { .name = "ct",         .parse = netlink_parse_ct },
@@ -3023,6 +3039,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
         case EXPR_NUMGEN:
         case EXPR_FIB:
         case EXPR_SOCKET:
+       case EXPR_TUNNEL:
         case EXPR_OSF:
         case EXPR_XFRM:
                 break;
src/netlink_linearize.c +14 -0
index 8ac33d34..d01cadf8 100644
@@ -334,6 +334,18 @@ static void netlink_gen_osf(struct netlink_linearize_ctx *ctx,
         nft_rule_add_expr(ctx, nle, &expr->location);
  }
 
+static void netlink_gen_tunnel(struct netlink_linearize_ctx *ctx,
+                              const struct expr *expr,
+                              enum nft_registers dreg)
+{
+       struct nftnl_expr *nle;
+
+       nle = alloc_nft_expr("tunnel");
+       netlink_put_register(nle, NFTNL_EXPR_TUNNEL_DREG, dreg);
+       nftnl_expr_set_u32(nle, NFTNL_EXPR_TUNNEL_KEY, expr->tunnel.key);
+       nftnl_rule_add_expr(ctx->nlr, nle);
+}
+
  static void netlink_gen_numgen(struct netlink_linearize_ctx *ctx,
                             const struct expr *expr,
                             enum nft_registers dreg)
@@ -932,6 +944,8 @@ static void netlink_gen_expr(struct netlink_linearize_ctx *ctx,
                 return netlink_gen_fib(ctx, expr, dreg);
         case EXPR_SOCKET:
                 return netlink_gen_socket(ctx, expr, dreg);
+       case EXPR_TUNNEL:
+               return netlink_gen_tunnel(ctx, expr, dreg);
         case EXPR_OSF:
                 return netlink_gen_osf(ctx, expr, dreg);
         case EXPR_XFRM:
src/parser_bison.y +28 -5
index 557977e2..08d75dbb 100644
@@ -321,6 +321,8 @@ int nft_lex(void *, void *, void *);
  %token RULESET                 "ruleset"
  %token TRACE                   "trace"
 
+%token PATH                    "path"
+
  %token INET                    "inet"
  %token NETDEV                  "netdev"
 
@@ -779,8 +781,8 @@ int nft_lex(void *, void *, void *);
  %destructor { stmt_free($$); } counter_stmt counter_stmt_alloc stateful_stmt last_stmt
  %type <stmt>                   limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
  %destructor { stmt_free($$); } limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
-%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
-%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
+%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
+%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
 
  %type <stmt>                   payload_stmt
  %destructor { stmt_free($$); } payload_stmt
@@ -940,9 +942,9 @@ int nft_lex(void *, void *, void *);
  %destructor { expr_free($$); } mh_hdr_expr
  %type <val>                    mh_hdr_field
 
-%type <expr>                   meta_expr
-%destructor { expr_free($$); } meta_expr
-%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type
+%type <expr>                   meta_expr       tunnel_expr
+%destructor { expr_free($$); } meta_expr       tunnel_expr
+%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type     tunnel_key
 
  %type <expr>                   socket_expr
  %destructor { expr_free($$); } socket_expr
@@ -3206,6 +3208,14 @@ objref_stmt_synproxy     :       SYNPROXY        NAME    stmt_expr close_scope_synproxy
                         }
                         ;
 
+objref_stmt_tunnel     :       TUNNEL  NAME    stmt_expr       close_scope_tunnel
+                       {
+                               $$ = objref_stmt_alloc(&@$);
+                               $$->objref.type = NFT_OBJECT_TUNNEL;
+                               $$->objref.expr = $3;
+                       }
+                       ;
+
  objref_stmt_ct         :       CT      TIMEOUT         SET     stmt_expr       close_scope_ct
                         {
                                 $$ = objref_stmt_alloc(&@$);
@@ -3226,6 +3236,7 @@ objref_stmt               :       objref_stmt_counter
                         |       objref_stmt_quota
                         |       objref_stmt_synproxy
                         |       objref_stmt_ct
+                       |       objref_stmt_tunnel
                         ;
 
  stateful_stmt          :       counter_stmt    close_scope_counter
@@ -3904,6 +3915,7 @@ primary_stmt_expr :       symbol_expr                     { $$ = $1; }
                         |       boolean_expr                    { $$ = $1; }
                         |       meta_expr                       { $$ = $1; }
                         |       rt_expr                         { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                         |       ct_expr                         { $$ = $1; }
                         |       numgen_expr                     { $$ = $1; }
                         |       hash_expr                       { $$ = $1; }
@@ -4381,6 +4393,7 @@ selector_expr             :       payload_expr                    { $$ = $1; }
                         |       exthdr_expr                     { $$ = $1; }
                         |       exthdr_exists_expr              { $$ = $1; }
                         |       meta_expr                       { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                         |       socket_expr                     { $$ = $1; }
                         |       rt_expr                         { $$ = $1; }
                         |       ct_expr                         { $$ = $1; }
@@ -5493,6 +5506,16 @@ socket_key               :       TRANSPARENT     { $$ = NFT_SOCKET_TRANSPARENT; }
                         |       WILDCARD        { $$ = NFT_SOCKET_WILDCARD; }
                         ;
 
+tunnel_key             :       PATH            { $$ = NFT_TUNNEL_PATH; }
+                       |       ID              { $$ = NFT_TUNNEL_ID; }
+                       ;
+
+tunnel_expr            :       TUNNEL  tunnel_key
+                       {
+                               $$ = tunnel_expr_alloc(&@$, $2);
+                       }
+                       ;
+
  offset_opt             :       /* empty */     { $$ = 0; }
                         |       OFFSET  NUM     { $$ = $2; }
                         ;
src/scanner.l +2 -1
index def0ac0e..9695d710 100644
@@ -410,7 +410,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
  }
 
  "counter"              { scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
-<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF>"name"                   { return NAME; }
+<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF,SCANSTATE_TUNNEL>"name"                  { return NAME; }
  <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"              { return PACKETS; }
  <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"        { return BYTES; }
 
@@ -826,6 +826,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
         "erspan"                { return ERSPAN; }
         "egress"                { return EGRESS; }
         "ingress"               { return INGRESS; }
+       "path"                  { return PATH; }
  }
 
  "notrack"              { return NOTRACK; }
src/statement.c +1 -0
index 2bfed4ac..20241f68 100644
@@ -290,6 +290,7 @@ static const char *objref_type[NFT_OBJECT_MAX + 1] = {
         [NFT_OBJECT_QUOTA]      = "quota",
         [NFT_OBJECT_CT_HELPER]  = "ct helper",
         [NFT_OBJECT_LIMIT]      = "limit",
+       [NFT_OBJECT_TUNNEL]     = "tunnel",
         [NFT_OBJECT_CT_TIMEOUT] = "ct timeout",
         [NFT_OBJECT_SECMARK]    = "secmark",
         [NFT_OBJECT_SYNPROXY]   = "synproxy",
src/tunnel.c new file +81 -1
new file mode 100644
index 00000000..cd92d039
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018 Pablo Neira Ayuso <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <pwd.h>
+#include <grp.h>
+#include <arpa/inet.h>
+#include <linux/netfilter.h>
+#include <linux/pkt_sched.h>
+#include <linux/if_packet.h>
+
+#include <nftables.h>
+#include <expression.h>
+#include <datatype.h>
+#include <tunnel.h>
+#include <gmputil.h>
+#include <utils.h>
+#include <erec.h>
+
+const struct tunnel_template tunnel_templates[] = {
+       [NFT_TUNNEL_PATH]       = META_TEMPLATE("path", &boolean_type,
+                                               BITS_PER_BYTE, BYTEORDER_HOST_ENDIAN),
+       [NFT_TUNNEL_ID]         = META_TEMPLATE("id",  &integer_type,
+                                               4 * 8, BYTEORDER_HOST_ENDIAN),
+};
+
+static void tunnel_expr_print(const struct expr *expr, struct output_ctx *octx)
+{
+       uint32_t key = expr->tunnel.key;
+       const char *token = "unknown";
+
+       if (key < array_size(tunnel_templates))
+               token = tunnel_templates[key].token;
+
+       nft_print(octx, "tunnel %s", token);
+}
+
+static bool tunnel_expr_cmp(const struct expr *e1, const struct expr *e2)
+{
+       return e1->tunnel.key == e2->tunnel.key;
+}
+
+static void tunnel_expr_clone(struct expr *new, const struct expr *expr)
+{
+       new->tunnel.key = expr->tunnel.key;
+}
+
+const struct expr_ops tunnel_expr_ops = {
+       .type           = EXPR_TUNNEL,
+       .name           = "tunnel",
+       .print          = tunnel_expr_print,
+       .cmp            = tunnel_expr_cmp,
+       .clone          = tunnel_expr_clone,
+};
+
+struct expr *tunnel_expr_alloc(const struct location *loc,
+                              enum nft_tunnel_keys key)
+{
+       const struct tunnel_template *tmpl = &tunnel_templates[key];
+       struct expr *expr;
+
+       expr = expr_alloc(loc, EXPR_TUNNEL, tmpl->dtype, tmpl->byteorder,
+                         tmpl->len);
+       expr->tunnel.key = key;
+
+       return expr;
+}
--

2.50.1

A message that is not in the archive (sent off-list or not yet received): CAJsUoE24NEe65atDs58dgwgxir8vLtEbrRkKp0nXpUVHFD6E_g@mail.gmail.com

#7 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

Hi Fernando,

Thanks for the detailed explanation, that helps a lot to understand the intended design and the use case.
While going through the example ruleset, I didn't see how the initial traffic initiation from VM A is supposed to work in practice, and I would like to check whether I am missing something.

As far as I can see, all the rules are added to netdev ingress hooks (both on veth_host and vxlan0),
but it is unclear to me how locally generated traffic is handled. For example, when VM A actively sends traffic (e.g. ping 10.0.0.2), the packets are outgoing and won't match in an ingress hook chain. so they would not match the redirect_to_tunnel chain.

Related to this, I am also unsure how ARP is expected to work. Before any ICMP packet can be sent, ARP resolution for 10.0.0.2 must happen, but ARP packets would not match the ip daddr 10.0.0.2 rule either. and won't be encapsulated.

Could you clarify how egress traffic and ARP/neighbor resolution are expected to be handled in this way?
Thanks again, and sorry if I am missing something obvious.

Best regards,
Yi

Quoted 595 lines · Yi Chen

Hi Fernando,

Thanks for the detailed explanation, that helps a lot to understand the intended design and the use case.
While going through the example ruleset, I am still struggling to see how the initial traffic initiation from VM A is supposed to work in practice, and I would like to check whether I am missing something.

As far as I can see, all the rules are added to netdev ingress hooks (both on veth_host and vxlan0),
but it is unclear to me how locally generated traffic is handled.
For example, when VM A actively sends traffic (e.g. ping 10.0.0.2), the packets are outgoing and won't match in a ingress hook chain. so they would not match the redirect_to_tunnel chain.

Related to this, I am also unsure how ARP is expected to work. Before any ICMP packet can be sent, ARP resolution for 10.0.0.2 must happen, but ARP packets would not match the ip daddr 10.0.0.2 rule either. and won't be encapsulated.

Could you clarify how egress traffic and ARP/neighbor resolution are expected to be handled in this way? Thanks again, and sorry if I am missing something obvious.

Best regards,
Yi

Quoted 573 lines · Fernando Fernandez Mancera
Quoted 5 lines · Yi Chen

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

Quoted 7 lines

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

Hi Yi Chen,

As my patch is taking longer than expected because I am polishing all the details related to the tunnel object let me explain it here briefly to unblock you.

The tunnel expression/object is used to attach tunnel metadata into a packet so in essence support Lightweight Tunneling (LWT) using Nftables. The LWT support is useful on virtualization environments where the users need to created a lot of tunnels to interconnect containers that are inside different VMs. Instead of creating one interface per container, the idea is that the user can create a single one and then attach the metadata as needed. Imagine the topology described below.

+------------------------+                   +------------------------+
|--------+          VM A |                   | VM B          +--------|
|Box     | +------+ +---+|(192.168.124.49)   +----+ +------+ |Box     |
|10.0.0.1|-|vxlan0|-|eth0|-------------------|eth0|-|vxlan0|-|10.0.0.2|
|--------+ +------+ +---+|  (192.168.124.134)+----+ +------+ +--------|
|                        |                   |                        |
|                        |                   |                        |
+------------------------+                   +------------------------+

We want to reach 10.0.0.2 from 10.0.0.1, the nftables ruleset on VM A will look like this:

```
table netdev filter_tunnel {
        tunnel vxlan_tmpl {
                id 100
                ip saddr 192.168.124.49
                ip daddr 192.168.124.134
                dport 8472
                ttl 255
                vxlan {
                        gbp 100
                }
        }

        chain redirect_to_tunnel {
                type filter hook ingress device "veth_host" priority filter; policy
accept;
                ip daddr 10.0.0.2 tunnel name "vxlan_tmpl" fwd to "vxlan0"
        }

        chain redirect_from_tunnel {
                type filter hook ingress device "vxlan0" priority filter; policy accept;
                ip daddr 10.0.0.1 fwd to "veth_host"
        }
}
```

On VM B the ruleset will be exactly the same but swapping saddr/daddr everywhere, both the external and internal one.

The idea behind this feature is to scale up the number of containers/namespaces without creating more VXLAN interfaces. Also, keep on mind that you need to mark the VXLAN as external when creating it, i.e

ip link add dev vxlan0 type vxlan external

After that, you should be able to ping between them:

PING 10.0.0.2 (10.0.0.2) 56(84) bytes of data. 64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.198 ms 64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.364 ms 64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.369 ms

Please, let me know if something is missing.

Thanks,
Fernando.

Quoted 484 lines
Quoted 481 lines · Fernando Fernandez Mancera

From: Pablo Neira Ayuso <[email protected]>

This patch allows you to attach tunnel metadata through the tunnel statement.

The following example shows how to redirect traffic to the erspan0 tunnel device which will take the tunnel configuration that is specified by the ruleset.

      table netdev x {
             tunnel y {
                     id 10
                     ip saddr 192.168.2.10
                     ip daddr 192.168.2.11
                     sport 10
                     dport 20
                     ttl 10
                     erspan {
                             version 1
                             index 2
                     }
             }

             chain x {
                     type filter hook ingress device veth0 priority 0;

                     ip daddr 10.141.10.123 tunnel name y fwd to erspan0
             }
      }

This patch also allows to match on tunnel metadata via tunnel expression.

Joint work with Fernando.

---
v3: rebased

---
  Makefile.am               |  2 +
  include/expression.h      |  6 +++
  include/tunnel.h          | 33 ++++++++++++++++
  src/evaluate.c            |  8 ++++
  src/expression.c          |  1 +
  src/netlink_delinearize.c | 17 ++++++++
  src/netlink_linearize.c   | 14 +++++++
  src/parser_bison.y        | 33 +++++++++++++---
  src/scanner.l             |  3 +-
  src/statement.c           |  1 +
  src/tunnel.c              | 81 +++++++++++++++++++++++++++++++++++++++
  11 files changed, 193 insertions(+), 6 deletions(-)

create mode 100644 include/tunnel.h
create mode 100644 src/tunnel.c

Makefile.am +2 -0
index 4909abfe..152a80d6 100644
@@ -100,6 +100,7 @@ noinst_HEADERS = \
         include/statement.h \
         include/tcpopt.h \
         include/trace.h \
+       include/tunnel.h \
         include/utils.h \
         include/xfrm.h \
         include/xt.h \
@@ -243,6 +244,7 @@ src_libnftables_la_SOURCES = \
         src/socket.c \
         src/statement.c \
         src/tcpopt.c \
+       src/tunnel.c \
         src/utils.c \
         src/xfrm.c \
         $(NULL)
include/expression.h +6 -0
index e483b7e7..7185ee66 100644
@@ -77,6 +77,7 @@ enum expr_types {
         EXPR_NUMGEN,
         EXPR_HASH,
         EXPR_RT,
+       EXPR_TUNNEL,
         EXPR_FIB,
         EXPR_XFRM,
         EXPR_SET_ELEM_CATCHALL,
@@ -229,6 +230,7 @@ enum expr_flags {
  #include <hash.h>
  #include <ct.h>
  #include <socket.h>
+#include <tunnel.h>
  #include <osf.h>
  #include <xfrm.h>
 
@@ -368,6 +370,10 @@ struct expr {
                         enum nft_socket_keys    key;
                         uint32_t                level;
                 } socket;
+               struct {
+                       /* EXPR_TUNNEL */
+                       enum nft_tunnel_keys    key;
+               } tunnel;
                 struct {
                         /* EXPR_RT */
                         enum nft_rt_keys        key;
include/tunnel.h new file +33 -0
new file mode 100644
index 00000000..9e6bd97a
@@ -0,0 +1,33 @@
+#ifndef NFTABLES_TUNNEL_H
+#define NFTABLES_TUNNEL_H
+
+/**
+ * struct tunnel_template - template for tunnel expressions
+ *
+ * @token:     parser token for the expression
+ * @dtype:     data type of the expression
+ * @len:       length of the expression
+ * @byteorder: byteorder
+ */
+struct tunnel_template {
+       const char              *token;
+       const struct datatype   *dtype;
+       enum byteorder          byteorder;
+       unsigned int            len;
+};
+
+extern const struct tunnel_template tunnel_templates[];
+
+#define TUNNEL_TEMPLATE(__token, __dtype, __len, __byteorder) {        \
+       .token          = (__token),                            \
+       .dtype          = (__dtype),                            \
+       .len            = (__len),                              \
+       .byteorder      = (__byteorder),                        \
+}
+
+extern struct expr *tunnel_expr_alloc(const struct location *loc,
+                                     enum nft_tunnel_keys key);
+
+extern const struct expr_ops tunnel_expr_ops;
+
+#endif /* NFTABLES_TUNNEL_H */
src/evaluate.c +8 -0
index da8794dd..6bf14b0c 100644
@@ -1737,6 +1737,7 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
                 case EXPR_SOCKET:
                 case EXPR_OSF:
                 case EXPR_XFRM:
+               case EXPR_TUNNEL:
                         break;
                 case EXPR_RANGE:
                 case EXPR_PREFIX:
@@ -3053,6 +3054,11 @@ static int expr_evaluate_osf(struct eval_ctx *ctx, struct expr **expr)
         return expr_evaluate_primary(ctx, expr);
  }
 
+static int expr_evaluate_tunnel(struct eval_ctx *ctx, struct expr **exprp)
+{
+       return expr_evaluate_primary(ctx, exprp);
+}
+
  static int expr_evaluate_variable(struct eval_ctx *ctx, struct expr **exprp)
  {
         struct symbol *sym = (*exprp)->sym;
@@ -3170,6 +3176,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
                 return expr_evaluate_meta(ctx, expr);
         case EXPR_SOCKET:
                 return expr_evaluate_socket(ctx, expr);
+       case EXPR_TUNNEL:
+               return expr_evaluate_tunnel(ctx, expr);
         case EXPR_OSF:
                 return expr_evaluate_osf(ctx, expr);
         case EXPR_FIB:
src/expression.c +1 -0
index 8cb63979..e3c27a13 100644
@@ -1762,6 +1762,7 @@ static const struct expr_ops *__expr_ops_by_type(enum expr_types etype)
         case EXPR_NUMGEN: return &numgen_expr_ops;
         case EXPR_HASH: return &hash_expr_ops;
         case EXPR_RT: return &rt_expr_ops;
+       case EXPR_TUNNEL: return &tunnel_expr_ops;
         case EXPR_FIB: return &fib_expr_ops;
         case EXPR_XFRM: return &xfrm_expr_ops;
         case EXPR_SET_ELEM_CATCHALL: return &set_elem_catchall_expr_ops;
src/netlink_delinearize.c +17 -0
index b97962a3..5627826d 100644
@@ -940,6 +940,21 @@ static void netlink_parse_osf(struct netlink_parse_ctx *ctx,
         netlink_set_register(ctx, dreg, expr);
  }
 
+static void netlink_parse_tunnel(struct netlink_parse_ctx *ctx,
+                                const struct location *loc,
+                                const struct nftnl_expr *nle)
+{
+       enum nft_registers dreg;
+       struct expr * expr;
+       uint32_t key;
+
+       key = nftnl_expr_get_u32(nle, NFTNL_EXPR_TUNNEL_KEY);
+       expr = tunnel_expr_alloc(loc, key);
+
+       dreg = netlink_parse_register(nle, NFTNL_EXPR_TUNNEL_DREG);
+       netlink_set_register(ctx, dreg, expr);
+}
+
  static void netlink_parse_meta_stmt(struct netlink_parse_ctx *ctx,
                                     const struct location *loc,
                                     const struct nftnl_expr *nle)
@@ -1922,6 +1937,7 @@ static const struct expr_handler netlink_parsers[] = {
         { .name = "exthdr",     .parse = netlink_parse_exthdr },
         { .name = "meta",       .parse = netlink_parse_meta },
         { .name = "socket",     .parse = netlink_parse_socket },
+       { .name = "tunnel",     .parse = netlink_parse_tunnel },
         { .name = "osf",        .parse = netlink_parse_osf },
         { .name = "rt",         .parse = netlink_parse_rt },
         { .name = "ct",         .parse = netlink_parse_ct },
@@ -3023,6 +3039,7 @@ static void expr_postprocess(struct rule_pp_ctx *ctx, struct expr **exprp)
         case EXPR_NUMGEN:
         case EXPR_FIB:
         case EXPR_SOCKET:
+       case EXPR_TUNNEL:
         case EXPR_OSF:
         case EXPR_XFRM:
                 break;
src/netlink_linearize.c +14 -0
index 8ac33d34..d01cadf8 100644
@@ -334,6 +334,18 @@ static void netlink_gen_osf(struct netlink_linearize_ctx *ctx,
         nft_rule_add_expr(ctx, nle, &expr->location);
  }
 
+static void netlink_gen_tunnel(struct netlink_linearize_ctx *ctx,
+                              const struct expr *expr,
+                              enum nft_registers dreg)
+{
+       struct nftnl_expr *nle;
+
+       nle = alloc_nft_expr("tunnel");
+       netlink_put_register(nle, NFTNL_EXPR_TUNNEL_DREG, dreg);
+       nftnl_expr_set_u32(nle, NFTNL_EXPR_TUNNEL_KEY, expr->tunnel.key);
+       nftnl_rule_add_expr(ctx->nlr, nle);
+}
+
  static void netlink_gen_numgen(struct netlink_linearize_ctx *ctx,
                             const struct expr *expr,
                             enum nft_registers dreg)
@@ -932,6 +944,8 @@ static void netlink_gen_expr(struct netlink_linearize_ctx *ctx,
                 return netlink_gen_fib(ctx, expr, dreg);
         case EXPR_SOCKET:
                 return netlink_gen_socket(ctx, expr, dreg);
+       case EXPR_TUNNEL:
+               return netlink_gen_tunnel(ctx, expr, dreg);
         case EXPR_OSF:
                 return netlink_gen_osf(ctx, expr, dreg);
         case EXPR_XFRM:
src/parser_bison.y +28 -5
index 557977e2..08d75dbb 100644
@@ -321,6 +321,8 @@ int nft_lex(void *, void *, void *);
  %token RULESET                 "ruleset"
  %token TRACE                   "trace"
 
+%token PATH                    "path"
+
  %token INET                    "inet"
  %token NETDEV                  "netdev"
 
@@ -779,8 +781,8 @@ int nft_lex(void *, void *, void *);
  %destructor { stmt_free($$); } counter_stmt counter_stmt_alloc stateful_stmt last_stmt
  %type <stmt>                   limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
  %destructor { stmt_free($$); } limit_stmt_alloc quota_stmt_alloc last_stmt_alloc ct_limit_stmt_alloc
-%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
-%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy
+%type <stmt>                   objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
+%destructor { stmt_free($$); } objref_stmt objref_stmt_counter objref_stmt_limit objref_stmt_quota objref_stmt_ct objref_stmt_synproxy objref_stmt_tunnel
 
  %type <stmt>                   payload_stmt
  %destructor { stmt_free($$); } payload_stmt
@@ -940,9 +942,9 @@ int nft_lex(void *, void *, void *);
  %destructor { expr_free($$); } mh_hdr_expr
  %type <val>                    mh_hdr_field
 
-%type <expr>                   meta_expr
-%destructor { expr_free($$); } meta_expr
-%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type
+%type <expr>                   meta_expr       tunnel_expr
+%destructor { expr_free($$); } meta_expr       tunnel_expr
+%type <val>                    meta_key        meta_key_qualified      meta_key_unqualified    numgen_type     tunnel_key
 
  %type <expr>                   socket_expr
  %destructor { expr_free($$); } socket_expr
@@ -3206,6 +3208,14 @@ objref_stmt_synproxy     :       SYNPROXY        NAME    stmt_expr close_scope_synproxy
                         }
                         ;
 
+objref_stmt_tunnel     :       TUNNEL  NAME    stmt_expr       close_scope_tunnel
+                       {
+                               $$ = objref_stmt_alloc(&@$);
+                               $$->objref.type = NFT_OBJECT_TUNNEL;
+                               $$->objref.expr = $3;
+                       }
+                       ;
+
  objref_stmt_ct         :       CT      TIMEOUT         SET     stmt_expr       close_scope_ct
                         {
                                 $$ = objref_stmt_alloc(&@$);
@@ -3226,6 +3236,7 @@ objref_stmt               :       objref_stmt_counter
                         |       objref_stmt_quota
                         |       objref_stmt_synproxy
                         |       objref_stmt_ct
+                       |       objref_stmt_tunnel
                         ;
 
  stateful_stmt          :       counter_stmt    close_scope_counter
@@ -3904,6 +3915,7 @@ primary_stmt_expr :       symbol_expr                     { $$ = $1; }
                         |       boolean_expr                    { $$ = $1; }
                         |       meta_expr                       { $$ = $1; }
                         |       rt_expr                         { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                         |       ct_expr                         { $$ = $1; }
                         |       numgen_expr                     { $$ = $1; }
                         |       hash_expr                       { $$ = $1; }
@@ -4381,6 +4393,7 @@ selector_expr             :       payload_expr                    { $$ = $1; }
                         |       exthdr_expr                     { $$ = $1; }
                         |       exthdr_exists_expr              { $$ = $1; }
                         |       meta_expr                       { $$ = $1; }
+                       |       tunnel_expr                     { $$ = $1; }
                         |       socket_expr                     { $$ = $1; }
                         |       rt_expr                         { $$ = $1; }
                         |       ct_expr                         { $$ = $1; }
@@ -5493,6 +5506,16 @@ socket_key               :       TRANSPARENT     { $$ = NFT_SOCKET_TRANSPARENT; }
                         |       WILDCARD        { $$ = NFT_SOCKET_WILDCARD; }
                         ;
 
+tunnel_key             :       PATH            { $$ = NFT_TUNNEL_PATH; }
+                       |       ID              { $$ = NFT_TUNNEL_ID; }
+                       ;
+
+tunnel_expr            :       TUNNEL  tunnel_key
+                       {
+                               $$ = tunnel_expr_alloc(&@$, $2);
+                       }
+                       ;
+
  offset_opt             :       /* empty */     { $$ = 0; }
                         |       OFFSET  NUM     { $$ = $2; }
                         ;
src/scanner.l +2 -1
index def0ac0e..9695d710 100644
@@ -410,7 +410,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
  }
 
  "counter"              { scanner_push_start_cond(yyscanner, SCANSTATE_COUNTER); return COUNTER; }
-<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF>"name"                   { return NAME; }
+<SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF,SCANSTATE_TUNNEL>"name"                  { return NAME; }
  <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"              { return PACKETS; }
  <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"        { return BYTES; }
 
@@ -826,6 +826,7 @@ addrstring  ({macaddr}|{ip4addr}|{ip6addr})
         "erspan"                { return ERSPAN; }
         "egress"                { return EGRESS; }
         "ingress"               { return INGRESS; }
+       "path"                  { return PATH; }
  }
 
  "notrack"              { return NOTRACK; }
src/statement.c +1 -0
index 2bfed4ac..20241f68 100644
@@ -290,6 +290,7 @@ static const char *objref_type[NFT_OBJECT_MAX + 1] = {
         [NFT_OBJECT_QUOTA]      = "quota",
         [NFT_OBJECT_CT_HELPER]  = "ct helper",
         [NFT_OBJECT_LIMIT]      = "limit",
+       [NFT_OBJECT_TUNNEL]     = "tunnel",
         [NFT_OBJECT_CT_TIMEOUT] = "ct timeout",
         [NFT_OBJECT_SECMARK]    = "secmark",
         [NFT_OBJECT_SYNPROXY]   = "synproxy",
src/tunnel.c new file +81 -1
new file mode 100644
index 00000000..cd92d039
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2018 Pablo Neira Ayuso <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <pwd.h>
+#include <grp.h>
+#include <arpa/inet.h>
+#include <linux/netfilter.h>
+#include <linux/pkt_sched.h>
+#include <linux/if_packet.h>
+
+#include <nftables.h>
+#include <expression.h>
+#include <datatype.h>
+#include <tunnel.h>
+#include <gmputil.h>
+#include <utils.h>
+#include <erec.h>
+
+const struct tunnel_template tunnel_templates[] = {
+       [NFT_TUNNEL_PATH]       = META_TEMPLATE("path", &boolean_type,
+                                               BITS_PER_BYTE, BYTEORDER_HOST_ENDIAN),
+       [NFT_TUNNEL_ID]         = META_TEMPLATE("id",  &integer_type,
+                                               4 * 8, BYTEORDER_HOST_ENDIAN),
+};
+
+static void tunnel_expr_print(const struct expr *expr, struct output_ctx *octx)
+{
+       uint32_t key = expr->tunnel.key;
+       const char *token = "unknown";
+
+       if (key < array_size(tunnel_templates))
+               token = tunnel_templates[key].token;
+
+       nft_print(octx, "tunnel %s", token);
+}
+
+static bool tunnel_expr_cmp(const struct expr *e1, const struct expr *e2)
+{
+       return e1->tunnel.key == e2->tunnel.key;
+}
+
+static void tunnel_expr_clone(struct expr *new, const struct expr *expr)
+{
+       new->tunnel.key = expr->tunnel.key;
+}
+
+const struct expr_ops tunnel_expr_ops = {
+       .type           = EXPR_TUNNEL,
+       .name           = "tunnel",
+       .print          = tunnel_expr_print,
+       .cmp            = tunnel_expr_cmp,
+       .clone          = tunnel_expr_clone,
+};
+
+struct expr *tunnel_expr_alloc(const struct location *loc,
+                              enum nft_tunnel_keys key)
+{
+       const struct tunnel_template *tmpl = &tunnel_templates[key];
+       struct expr *expr;
+
+       expr = expr_alloc(loc, EXPR_TUNNEL, tmpl->dtype, tmpl->byteorder,
+                         tmpl->len);
+       expr->tunnel.key = key;
+
+       return expr;
+}
--

2.50.1

maintainer
#8 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

In reply to Fernando Fernandez Mancera

Hi Fernando

Quoted 18 lines · Fernando Fernandez Mancera
Quoted 5 lines · Yi Chen

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

Quoted 7 lines

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

As my patch is taking longer than expected because I am polishing all the details related to the tunnel object let me explain it here briefly to unblock you.

Quoted 7 lines

The tunnel expression/object is used to attach tunnel metadata into a packet so in essence support Lightweight Tunneling (LWT) using Nftables. The LWT support is useful on virtualization environments where the users need to created a lot of tunnels to interconnect containers that are inside different VMs. Instead of creating one interface per container, the idea is that the user can create a single one and then attach the metadata as needed. Imagine the topology described below.

I'm trying to get Yi's test script to work but I am failing as well. AFAICS the entire feature doesn't work *by design*.

Quoted 8 lines
+------------------------+                   +------------------------+
|--------+          VM A |                   | VM B          +--------|
|Box     | +------+ +---+|(192.168.124.49)   +----+ +------+ |Box     |
|10.0.0.1|-|vxlan0|-|eth0|-------------------|eth0|-|vxlan0|-|10.0.0.2|
|--------+ +------+ +---+|  (192.168.124.134)+----+ +------+ +--------|
|                        |                   |                        |
|                        |                   |                        |
+------------------------+                   +------------------------+

How do I read this diagram?
Are these 4 computers or 2?
What is "Box" ? Is that a container inside of VM A / B ? And if so, how does it connect to VM A? veth? The diagram reads like its a container connected to VM A via a vxlan tunnel...

Which makes no sense to me.

Quoted 17 lines

We want to reach 10.0.0.2 from 10.0.0.1, the nftables ruleset on VM A will look like this:

```

table netdev filter_tunnel {
	tunnel vxlan_tmpl {
		id 100
		ip saddr 192.168.124.49
		ip daddr 192.168.124.134
		dport 8472
		ttl 255
		vxlan {
			gbp 100
		}
	}

	chain redirect_to_tunnel {
Quoted 8 lines
		type filter hook ingress device "veth_host" priority filter; policy
accept;
		ip daddr 10.0.0.2 tunnel name "vxlan_tmpl" fwd to "vxlan0"
	}

	chain redirect_from_tunnel {
		type filter hook ingress device "vxlan0" priority filter; policy accept;
		ip daddr 10.0.0.1 fwd to "veth_host"

How can this work? I tried to get this to run but *ingress* sees no packets. Which is not surprising to me, as packets are *egressing* from VM A, not coming in.

The only way that I can get it to work is via normal tunnel device + routes, no nftables rules needed.

Can you make a test script for packetpath?
Or add documentation that explains how to use this feature?

Thanks.

#9 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

Quoted 29 lines · Florian Westphal

Hi Fernando

Quoted 18 lines · Fernando Fernandez Mancera
Quoted 5 lines · Yi Chen

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

Quoted 7 lines

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

As my patch is taking longer than expected because I am polishing all the details related to the tunnel object let me explain it here briefly to unblock you.

Quoted 7 lines

The tunnel expression/object is used to attach tunnel metadata into a packet so in essence support Lightweight Tunneling (LWT) using Nftables. The LWT support is useful on virtualization environments where the users need to created a lot of tunnels to interconnect containers that are inside different VMs. Instead of creating one interface per container, the idea is that the user can create a single one and then attach the metadata as needed. Imagine the topology described below.

Quoted 3 lines

I'm trying to get Yi's test script to work but I am failing as well. AFAICS the entire feature doesn't work *by design*.

It does? The idea is to be able to use a single tunnel encapsulate traffic from different endpoints. Let me write a script this week and will share it with you here.

Quoted 9 lines
Quoted 8 lines
+------------------------+                   +------------------------+
|--------+          VM A |                   | VM B          +--------|
|Box     | +------+ +---+|(192.168.124.49)   +----+ +------+ |Box     |
|10.0.0.1|-|vxlan0|-|eth0|-------------------|eth0|-|vxlan0|-|10.0.0.2|
|--------+ +------+ +---+|  (192.168.124.134)+----+ +------+ +--------|
|                        |                   |                        |
|                        |                   |                        |
+------------------------+                   +------------------------+
Quoted 8 lines

How do I read this diagram?
Are these 4 computers or 2?
What is "Box" ? Is that a container inside of VM A / B ? And if so, how does it connect to VM A? veth? The diagram reads like its a container connected to VM A via a vxlan tunnel...

Which makes no sense to me.

Yeah that diagram could get some improvement. That is not accurate.

Quoted 30 lines
Quoted 17 lines

We want to reach 10.0.0.2 from 10.0.0.1, the nftables ruleset on VM A will look like this:

```

table netdev filter_tunnel {
	tunnel vxlan_tmpl {
		id 100
		ip saddr 192.168.124.49
		ip daddr 192.168.124.134
		dport 8472
		ttl 255
		vxlan {
			gbp 100
		}
	}

	chain redirect_to_tunnel {
Quoted 8 lines
		type filter hook ingress device "veth_host" priority filter; policy
accept;
		ip daddr 10.0.0.2 tunnel name "vxlan_tmpl" fwd to "vxlan0"
	}

	chain redirect_from_tunnel {
		type filter hook ingress device "vxlan0" priority filter; policy accept;
		ip daddr 10.0.0.1 fwd to "veth_host"

How can this work? I tried to get this to run but *ingress* sees no packets. Which is not surprising to me, as packets are *egressing* from VM A, not coming in.

Quoted 6 lines

The only way that I can get it to work is via normal tunnel device + routes, no nftables rules needed.

Can you make a test script for packetpath?
Or add documentation that explains how to use this feature?

Yes. Let me prepare some scripts and documentation. Sorry about that.

Quoted 1 line

Thanks.

#10 lore raw

Re: [PATCH 3/7 nft v3] src: add tunnel statement and expression support

In reply to Florian Westphal
Quoted 79 lines · Florian Westphal

Hi Fernando

Quoted 18 lines · Fernando Fernandez Mancera
Quoted 5 lines · Yi Chen

Hello Pablo and Fernando,
I have started working on a test script (attached) to exercise this feature, using a geneve tunnel with an egress hook.
Please let me know if egress is the correct hook to use in this context.

Quoted 7 lines

However, the behavior is not what I expected: the tunnel template does not appear to be attached, and even ARP packets are not being encapsulated.
I would appreciate any guidance on what I might be missing, or suggestions on how this test could be improved.
Thank you for your time and help.

As my patch is taking longer than expected because I am polishing all the details related to the tunnel object let me explain it here briefly to unblock you.

Quoted 7 lines

The tunnel expression/object is used to attach tunnel metadata into a packet so in essence support Lightweight Tunneling (LWT) using Nftables. The LWT support is useful on virtualization environments where the users need to created a lot of tunnels to interconnect containers that are inside different VMs. Instead of creating one interface per container, the idea is that the user can create a single one and then attach the metadata as needed. Imagine the topology described below.

I'm trying to get Yi's test script to work but I am failing as well. AFAICS the entire feature doesn't work *by design*.

Quoted 8 lines
+------------------------+                   +------------------------+
|--------+          VM A |                   | VM B          +--------|
|Box     | +------+ +---+|(192.168.124.49)   +----+ +------+ |Box     |
|10.0.0.1|-|vxlan0|-|eth0|-------------------|eth0|-|vxlan0|-|10.0.0.2|
|--------+ +------+ +---+|  (192.168.124.134)+----+ +------+ +--------|
|                        |                   |                        |
|                        |                   |                        |
+------------------------+                   +------------------------+

How do I read this diagram?
Are these 4 computers or 2?
What is "Box" ? Is that a container inside of VM A / B ? And if so, how does it connect to VM A? veth? The diagram reads like its a container connected to VM A via a vxlan tunnel...

Which makes no sense to me.

Quoted 17 lines

We want to reach 10.0.0.2 from 10.0.0.1, the nftables ruleset on VM A will look like this:

```

table netdev filter_tunnel {
	tunnel vxlan_tmpl {
		id 100
		ip saddr 192.168.124.49
		ip daddr 192.168.124.134
		dport 8472
		ttl 255
		vxlan {
			gbp 100
		}
	}

	chain redirect_to_tunnel {
Quoted 8 lines
		type filter hook ingress device "veth_host" priority filter; policy
accept;
		ip daddr 10.0.0.2 tunnel name "vxlan_tmpl" fwd to "vxlan0"
	}

	chain redirect_from_tunnel {
		type filter hook ingress device "vxlan0" priority filter; policy accept;
		ip daddr 10.0.0.1 fwd to "veth_host"

How can this work? I tried to get this to run but *ingress* sees no packets. Which is not surprising to me, as packets are *egressing* from VM A, not coming in.

Quoted 6 lines

The only way that I can get it to work is via normal tunnel device + routes, no nftables rules needed.

Can you make a test script for packetpath?
Or add documentation that explains how to use this feature?

I am contributing this upstream but this is in essence the simplest example I could find. I have adapted another script I used to test this. I shared that other script with Yi. But that one requires 2 VMs and some other stuff, so let's keep it simple.

New diagram:

OVERLAY (The Virtual Network)
=============================
[ ns1: 10.0.0.1 ] [ ns2: 10.0.0.2 ] [ ns3: 10.0.0.3 ]
| | |
(vxlan0) (vxlan0) (vxlan0)
| | |

   ~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~
           |                 nftables                   |
           |         (attaches the Underlay IP)         |
   ~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~~~~~~~~~~~~~~|~~~~~~~~
           |                      |                     |
    [ 192.168.1.1 ]       [ 192.168.1.2 ]       [ 192.168.1.3 ]
           |                      |                     |
           +------------------[ Bridge ]----------------+

===============================
UNDERLAY (The Physical Network)

Once running the script attached below, you should have 3 namespaces connected to each other through a linux bridge and veths. Then, each of them has a VXLAN interface that allows them to reach other over the overlay network (10.0.0.0/24).

Why is this relevant? Well, usually, one would need one device per remote and also one route for each. Here we use a single device for all the endpoints.

After running the script, you should be able to do:

sudo ip netns exec ns1 ping 10.0.0.2 sudo ip netns exec ns1 ping 10.0.0.3

Please, if it doesn't work for you or something is off, let me know. I have an environment using this feature so it should work.

Thanks,
Fernando.

#!/bin/bash

ip netns del ns1 2>/dev/null || true
ip netns del ns2 2>/dev/null || true
ip netns del ns3 2>/dev/null || true
ip netns del sw  2>/dev/null || true

ip netns add ns1
ip netns add ns2
ip netns add ns3
ip netns add sw

ip -n sw link add br0 type bridge ip -n sw link set br0 up

for i in 1 2 3; do

    # create the link and move ends to appropriate namespaces
    ip link add veth$i type veth peer name veth${i}_sw netns sw
    ip link set veth$i netns ns$i
    # linux bridge (switch)
    ip -n sw link set veth${i}_sw master br0 up
    # configure the node side
    ip -n ns$i addr add 192.168.1.$i/24 dev veth$i
    ip -n ns$i link set veth$i up
    ip -n ns$i link set lo up
    # create the Overlay VXLAN interface
    ip -n ns$i link add vxlan0 type vxlan dstport 4789 external
    ip -n ns$i addr add 10.0.0.$i/24 dev vxlan0
    ip -n ns$i link set vxlan0 up
    # route the ENTIRE overlay subnet into the single VXLAN device
    ip -n ns$i route add 10.0.0.0/24 dev vxlan0
done

# Node 1
ip netns exec ns1 nft -f - <<EOF

table netdev my_overlay {
    tunnel tun_to_ns2 { id 100; ip saddr 192.168.1.1; ip daddr 
192.168.1.2; }
    tunnel tun_to_ns3 { id 100; ip saddr 192.168.1.1; ip daddr 

192.168.1.3; }

    chain egress_filter {
        type filter hook egress device "vxlan0" priority 0; policy accept;

        # IPv4 Traffic
        ip daddr 10.0.0.2 tunnel name "tun_to_ns2"
        ip daddr 10.0.0.3 tunnel name "tun_to_ns3"

        # ARP Traffic (Unicasting ARP directly to the correct node)
        arp daddr ip 10.0.0.2 tunnel name "tun_to_ns2"
        arp daddr ip 10.0.0.3 tunnel name "tun_to_ns3"
    }
}

EOF

# Node 2
ip netns exec ns2 nft -f - <<EOF

table netdev my_overlay {
    tunnel tun_to_ns1 { id 100; ip saddr 192.168.1.2; ip daddr 
192.168.1.1; }
    tunnel tun_to_ns3 { id 100; ip saddr 192.168.1.2; ip daddr 

192.168.1.3; }

    chain egress_filter {
        type filter hook egress device "vxlan0" priority 0; policy accept;

        ip daddr 10.0.0.1 tunnel name "tun_to_ns1"
        ip daddr 10.0.0.3 tunnel name "tun_to_ns3"

        arp daddr ip 10.0.0.1 tunnel name "tun_to_ns1"
        arp daddr ip 10.0.0.3 tunnel name "tun_to_ns3"
    }
}

EOF

# Node 3
ip netns exec ns3 nft -f - <<EOF

table netdev my_overlay {
    tunnel tun_to_ns1 { id 100; ip saddr 192.168.1.3; ip daddr 
192.168.1.1; }
    tunnel tun_to_ns2 { id 100; ip saddr 192.168.1.3; ip daddr 

192.168.1.2; }

    chain egress_filter {
        type filter hook egress device "vxlan0" priority 0; policy accept;

        ip daddr 10.0.0.1 tunnel name "tun_to_ns1"
        ip daddr 10.0.0.2 tunnel name "tun_to_ns2"

        arp daddr ip 10.0.0.1 tunnel name "tun_to_ns1"
        arp daddr ip 10.0.0.2 tunnel name "tun_to_ns2"
    }
}

EOF

#11 lore raw

PATCH v3 4/7 tunnel: add vxlan support

In reply to Fernando Fernandez Mancera

This patch extends the tunnel metadata object to define vxlan tunnel specific configurations:

table netdev x {
	tunnel y {
		id 10
		ip saddr 192.168.2.10
		ip daddr 192.168.2.11
		sport 10
		dport 20
		ttl 10
		vxlan {
			gbp 200
		}
	}
}

---
v3: rebased

---
 include/rule.h     |  4 ++++
 src/mnl.c          | 16 ++++++++++++++++
 src/netlink.c      |  7 +++++++
 src/parser_bison.y | 28 +++++++++++++++++++++++++++-
 src/rule.c         | 10 ++++++++++
 src/scanner.l      |  1 +
 6 files changed, 65 insertions(+), 1 deletion(-)
include/rule.h +4 -0
index 71e9a07e..c52af2c4 100644
@@ -495,6 +495,7 @@ struct secmark {
 enum tunnel_type {
 	TUNNEL_UNSPEC = 0,
 	TUNNEL_ERSPAN,
+	TUNNEL_VXLAN,
 };
 
 struct tunnel {
@@ -517,6 +518,9 @@ struct tunnel {
 				uint8_t		hwid;
 			} v2;
 		} erspan;
+		struct {
+			uint32_t	gbp;
+		} vxlan;
 	};
 };
 
src/mnl.c +16 -0
index 3762b6f9..c0aadf59 100644
@@ -1508,6 +1508,22 @@ static void obj_tunnel_add_opts(struct nftnl_obj *nlo, struct tunnel *tunnel)
 			break;
 		}
 
+		nftnl_tunnel_opts_add(opts, opt);
+		nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *));
+		break;
+	case TUNNEL_VXLAN:
+		opts = nftnl_tunnel_opts_alloc(NFTNL_TUNNEL_TYPE_VXLAN);
+		if (!opts)
+			memory_allocation_error();
+
+		opt = nftnl_tunnel_opt_alloc(NFTNL_TUNNEL_TYPE_VXLAN);
+		if (!opt)
+			memory_allocation_error();
+
+		nftnl_tunnel_opt_set(opt, NFTNL_TUNNEL_VXLAN_GBP,
+				     &tunnel->vxlan.gbp,
+				     sizeof(tunnel->vxlan.gbp));
+
 		nftnl_tunnel_opts_add(opts, opt);
 		nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *));
 		break;
src/netlink.c +7 -0
index 4ef88402..e132362b 100644
@@ -1836,6 +1836,13 @@ static int tunnel_parse_opt_cb(struct nftnl_tunnel_opt *opt, void *data) {
 							NFTNL_TUNNEL_ERSPAN_V2_DIR);
 		}
 		break;
+	case NFTNL_TUNNEL_TYPE_VXLAN:
+		obj->tunnel.type = TUNNEL_VXLAN;
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_VXLAN_GBP)) {
+			obj->tunnel.type = TUNNEL_VXLAN;
+			obj->tunnel.vxlan.gbp = nftnl_tunnel_opt_get_u32(opt, NFTNL_TUNNEL_VXLAN_GBP);
+		}
+		break;
 	default:
 		break;
 	}
src/parser_bison.y +27 -1
index 08d75dbb..ca93a658 100644
@@ -612,6 +612,7 @@ int nft_lex(void *, void *, void *);
 %token ERSPAN			"erspan"
 %token EGRESS			"egress"
 %token INGRESS			"ingress"
+%token GBP			"gbp"
 
 %token COUNTERS			"counters"
 %token QUOTAS			"quotas"
@@ -770,7 +771,7 @@ int nft_lex(void *, void *, void *);
 %type <flowtable>		flowtable_block_alloc flowtable_block
 %destructor { flowtable_free($$); }	flowtable_block_alloc
 
-%type <obj>			obj_block_alloc counter_block quota_block ct_helper_block ct_timeout_block ct_expect_block limit_block secmark_block synproxy_block tunnel_block erspan_block erspan_block_alloc
+%type <obj>			obj_block_alloc counter_block quota_block ct_helper_block ct_timeout_block ct_expect_block limit_block secmark_block synproxy_block tunnel_block erspan_block erspan_block_alloc vxlan_block vxlan_block_alloc
 %destructor { obj_free($$); }	obj_block_alloc
 
 %type <list>			stmt_list stateful_stmt_list set_elem_stmt_list
@@ -5011,6 +5012,27 @@ erspan_config		:	HDRVERSION	NUM
 			}
 			;
 
+vxlan_block		:	/* empty */	{ $$ = $<obj>-1; }
+			|	vxlan_block	common_block
+			|	vxlan_block	stmt_separator
+			|	vxlan_block	vxlan_config	stmt_separator
+			{
+				$$ = $1;
+			}
+			;
+
+vxlan_block_alloc	:	/* empty */
+			{
+				$$ = $<obj>-1;
+			}
+			;
+
+vxlan_config		:	GBP	NUM
+			{
+				$<obj>0->tunnel.vxlan.gbp = $2;
+			}
+			;
+
 tunnel_config		:	ID	NUM
 			{
 				$<obj>0->tunnel.id = $2;
@@ -5055,6 +5077,10 @@ tunnel_config		:	ID	NUM
 			{
 				$<obj>0->tunnel.type = TUNNEL_ERSPAN;
 			}
+			|	VXLAN	vxlan_block_alloc '{' vxlan_block '}'
+			{
+				$<obj>0->tunnel.type = TUNNEL_VXLAN;
+			}
 			;
 
 tunnel_block		:	/* empty */	{ $$ = $<obj>-1; }
src/rule.c +10 -0
index 2557f4cc..0450851c 100644
@@ -2043,6 +2043,16 @@ static void obj_print_data(const struct obj *obj,
 			}
 			nft_print(octx, "%s%s%s}",
 				  opts->nl, opts->tab, opts->tab);
+			break;
+		case TUNNEL_VXLAN:
+			nft_print(octx, "%s%s%svxlan {",
+				  opts->nl, opts->tab, opts->tab);
+			nft_print(octx, "%s%s%s%sgbp %u",
+				  opts->nl, opts->tab, opts->tab, opts->tab,
+				  obj->tunnel.vxlan.gbp);
+			nft_print(octx, "%s%s%s}",
+				  opts->nl, opts->tab, opts->tab);
+			break;
 		default:
 			break;
 		}
src/scanner.l +1 -0
index 9695d710..74ebca3b 100644
@@ -827,6 +827,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"egress"		{ return EGRESS; }
 	"ingress"		{ return INGRESS; }
 	"path"			{ return PATH; }
+	"gbp"			{ return GBP; }
 }
 
 "notrack"		{ return NOTRACK; }
Signature · 1 line
2.50.1
#12 lore raw

PATCH v3 5/7 tunnel: add geneve support

In reply to Fernando Fernandez Mancera

From: Pablo Neira Ayuso <[email protected]>

This patch extends the tunnel metadata object to define geneve tunnel specific configurations:

table netdev x {
	tunnel y {
		id 10
		ip saddr 192.168.2.10
		ip daddr 192.168.2.11
		sport 10
		dport 20
		ttl 10
		geneve {
			class 0x1010 opt-type 0x1 data "0x12345678"
			class 0x1020 opt-type 0x2 data "0x87654321"
			class 0x2020 opt-type 0x3 data "0x87654321abcdeffe"
		}
	}
}

---
v3: rebased

---
 include/rule.h     | 14 ++++++++++
 src/mnl.c          | 25 +++++++++++++++++
 src/netlink.c      | 29 ++++++++++++++++++++
 src/parser_bison.y | 43 ++++++++++++++++++++++++++++-
 src/rule.c         | 67 ++++++++++++++++++++++++++++++++++++++++++++++
 src/scanner.l      |  3 +++
 6 files changed, 180 insertions(+), 1 deletion(-)
include/rule.h +14 -0
index c52af2c4..498a88bf 100644
@@ -496,6 +496,15 @@ enum tunnel_type {
 	TUNNEL_UNSPEC = 0,
 	TUNNEL_ERSPAN,
 	TUNNEL_VXLAN,
+	TUNNEL_GENEVE,
+};
+
+struct tunnel_geneve {
+	struct list_head	list;
+	uint16_t		geneve_class;
+	uint8_t			type;
+	uint8_t			data[NFTNL_TUNNEL_GENEVE_DATA_MAXLEN];
+	uint32_t		data_len;
 };
 
 struct tunnel {
@@ -521,9 +530,14 @@ struct tunnel {
 		struct {
 			uint32_t	gbp;
 		} vxlan;
+		struct list_head	geneve_opts;
 	};
 };
 
+int tunnel_geneve_data_str2array(const char *hexstr,
+				 uint8_t *out_data,
+				 uint32_t *out_len);
+
 /**
  * struct obj - nftables stateful object statement
  *
src/mnl.c +25 -0
index c0aadf59..108d6e55 100644
@@ -1527,6 +1527,31 @@ static void obj_tunnel_add_opts(struct nftnl_obj *nlo, struct tunnel *tunnel)
 		nftnl_tunnel_opts_add(opts, opt);
 		nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *));
 		break;
+	case TUNNEL_GENEVE:
+		struct tunnel_geneve *geneve;
+
+		opts = nftnl_tunnel_opts_alloc(NFTNL_TUNNEL_TYPE_GENEVE);
+		if (!opts)
+			memory_allocation_error();
+
+		list_for_each_entry(geneve, &tunnel->geneve_opts, list) {
+			opt = nftnl_tunnel_opt_alloc(NFTNL_TUNNEL_TYPE_GENEVE);
+			if (!opt)
+				memory_allocation_error();
+
+			nftnl_tunnel_opt_set(opt,
+					     NFTNL_TUNNEL_GENEVE_TYPE,
+					     &geneve->type, sizeof(geneve->type));
+			nftnl_tunnel_opt_set(opt,
+					     NFTNL_TUNNEL_GENEVE_CLASS,
+					     &geneve->geneve_class, sizeof(geneve->geneve_class));
+			nftnl_tunnel_opt_set(opt,
+					     NFTNL_TUNNEL_GENEVE_DATA,
+					     &geneve->data, geneve->data_len);
+			nftnl_tunnel_opts_add(opts, opt);
+		}
+		nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_OPTS, &opts, sizeof(struct nftnl_tunnel_opts *));
+		break;
 	case TUNNEL_UNSPEC:
 		break;
 	}
src/netlink.c +29 -0
index e132362b..5bae3b82 100644
@@ -1843,6 +1843,35 @@ static int tunnel_parse_opt_cb(struct nftnl_tunnel_opt *opt, void *data) {
 			obj->tunnel.vxlan.gbp = nftnl_tunnel_opt_get_u32(opt, NFTNL_TUNNEL_VXLAN_GBP);
 		}
 		break;
+	case NFTNL_TUNNEL_TYPE_GENEVE:
+		struct tunnel_geneve *geneve;
+		const void *data;
+
+		if (!obj->tunnel.type) {
+			init_list_head(&obj->tunnel.geneve_opts);
+			obj->tunnel.type = TUNNEL_GENEVE;
+		}
+
+		geneve = xmalloc(sizeof(struct tunnel_geneve));
+		if (!geneve)
+			memory_allocation_error();
+
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_GENEVE_TYPE))
+			geneve->type = nftnl_tunnel_opt_get_u8(opt, NFTNL_TUNNEL_GENEVE_TYPE);
+
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_GENEVE_CLASS))
+			geneve->geneve_class = nftnl_tunnel_opt_get_u16(opt, NFTNL_TUNNEL_GENEVE_CLASS);
+
+		if (nftnl_tunnel_opt_get_flags(opt) & (1 << NFTNL_TUNNEL_GENEVE_DATA)) {
+			data = nftnl_tunnel_opt_get_data(opt, NFTNL_TUNNEL_GENEVE_DATA,
+							 &geneve->data_len);
+			if (!data)
+				return -1;
+			memcpy(&geneve->data, data, geneve->data_len);
+		}
+
+		list_add_tail(&geneve->list, &obj->tunnel.geneve_opts);
+		break;
 	default:
 		break;
 	}
src/parser_bison.y +42 -1

67 lines not on this page · raw message

src/rule.c +67 -0

85 lines not on this page · raw message

src/scanner.l +3 -0
index 74ebca3b..8085c93b 100644
@@ -828,6 +828,9 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"ingress"		{ return INGRESS; }
 	"path"			{ return PATH; }
 	"gbp"			{ return GBP; }
+	"class"			{ return CLASS; }
+	"opt-type"		{ return OPTTYPE; }
+	"data"			{ return DATA; }
 }
 
 "notrack"		{ return NOTRACK; }
Signature · 1 line
2.50.1
#13 lore raw

PATCH v3 6/7 tunnel: add tunnel object and statement json support

In reply to Fernando Fernandez Mancera

---
v3: added ipv6 support

---
 include/json.h    |   2 +
 include/tunnel.h  |   4 +
 src/json.c        |  96 ++++++++++++++++++-
 src/parser_json.c | 236 +++++++++++++++++++++++++++++++++++++++++++++-
 src/tunnel.c      |  21 +++++
 5 files changed, 351 insertions(+), 8 deletions(-)
include/json.h +2 -0
index b61eeafe..42e1c861 100644
@@ -31,6 +31,7 @@ json_t *binop_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *relational_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *range_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *meta_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *tunnel_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *payload_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *ct_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *concat_expr_json(const struct expr *expr, struct output_ctx *octx);
@@ -160,6 +161,7 @@ EXPR_PRINT_STUB(fib_expr)
 EXPR_PRINT_STUB(constant_expr)
 EXPR_PRINT_STUB(socket_expr)
 EXPR_PRINT_STUB(osf_expr)
+EXPR_PRINT_STUB(tunnel_expr)
 EXPR_PRINT_STUB(xfrm_expr)
 
 EXPR_PRINT_STUB(integer_type)
include/tunnel.h +4 -0
index 9e6bd97a..e2d87d2c 100644
@@ -25,6 +25,10 @@ extern const struct tunnel_template tunnel_templates[];
 	.byteorder	= (__byteorder),			\
 }
 
+struct error_record *tunnel_key_parse(const struct location *loc,
+			       const char *str,
+			       unsigned int *value);
+
 extern struct expr *tunnel_expr_alloc(const struct location *loc,
 				      enum nft_tunnel_keys key);
 
src/json.c +91 -5

132 lines not on this page · raw message

src/parser_json.c +233 -3

279 lines not on this page · raw message

src/tunnel.c +21 -0
index cd92d039..a311246b 100644
@@ -6,6 +6,8 @@
  * published by the Free Software Foundation.
  */
 
+#include <nft.h>
+
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
@@ -38,6 +40,24 @@ const struct tunnel_template tunnel_templates[] = {
 						4 * 8, BYTEORDER_HOST_ENDIAN),
 };
 
+struct error_record *tunnel_key_parse(const struct location *loc,
+				      const char *str,
+				      unsigned int *value)
+{
+	unsigned int i;
+
+	for (i = 0; i < array_size(tunnel_templates); i++) {
+		if (!tunnel_templates[i].token ||
+		    strcmp(tunnel_templates[i].token, str))
+			continue;
+
+		*value = i;
+		return NULL;
+	}
+
+	return error(loc, "syntax error, unexpected %s", str);
+}
+
 static void tunnel_expr_print(const struct expr *expr, struct output_ctx *octx)
 {
 	uint32_t key = expr->tunnel.key;
@@ -63,6 +83,7 @@ const struct expr_ops tunnel_expr_ops = {
 	.type		= EXPR_TUNNEL,
 	.name		= "tunnel",
 	.print		= tunnel_expr_print,
+	.json		= tunnel_expr_json,
 	.cmp		= tunnel_expr_cmp,
 	.clone		= tunnel_expr_clone,
 };
Signature · 1 line
2.50.1
#14 lore raw

PATCH v3 7/7 tests: add tunnel shell and python tests

In reply to Fernando Fernandez Mancera

Add tests for tunnel statement and object support. Shell and python tests both cover standard nft output and json.

---
v3: rebased and adapted tests for new json src/dst keys

---
 tests/py/netdev/tunnel.t                      |   7 +
 tests/py/netdev/tunnel.t.json                 |  45 +++++
 tests/py/netdev/tunnel.t.payload              |  15 ++
 tests/shell/features/tunnel.nft               |  17 ++
 tests/shell/testcases/sets/0075tunnel_0       |  75 ++++++++
 .../sets/dumps/0075tunnel_0.json-nft          | 171 ++++++++++++++++++
 .../testcases/sets/dumps/0075tunnel_0.nft     |  63 +++++++
 7 files changed, 393 insertions(+)
 create mode 100644 tests/py/netdev/tunnel.t
 create mode 100644 tests/py/netdev/tunnel.t.json
 create mode 100644 tests/py/netdev/tunnel.t.payload
 create mode 100644 tests/shell/features/tunnel.nft
 create mode 100755 tests/shell/testcases/sets/0075tunnel_0
 create mode 100644 tests/shell/testcases/sets/dumps/0075tunnel_0.json-nft
 create mode 100644 tests/shell/testcases/sets/dumps/0075tunnel_0.nft
tests/py/netdev/tunnel.t new file +7 -0
new file mode 100644
index 00000000..920d21ff
@@ -0,0 +1,7 @@
+:tunnelchain;type filter hook ingress device lo priority 0
+
+*netdev;test-netdev;tunnelchain
+
+tunnel path exists;ok
+tunnel path missing;ok
+tunnel id 10;ok
tests/py/netdev/tunnel.t.json new file +45 -0

45 lines not on this page · raw message

tests/py/netdev/tunnel.t.payload new file +15 -0
new file mode 100644
index 00000000..9148d0e5
@@ -0,0 +1,15 @@
+# tunnel path exists
+netdev test-netdev tunnelchain
+  [ tunnel load path => reg 1 ]
+  [ cmp eq reg 1 0x00000001 ]
+
+# tunnel path missing
+netdev test-netdev tunnelchain
+  [ tunnel load path => reg 1 ]
+  [ cmp eq reg 1 0x00000000 ]
+
+# tunnel id 10
+netdev test-netdev tunnelchain
+  [ tunnel load id => reg 1 ]
+  [ cmp eq reg 1 0x0000000a ]
+
tests/shell/features/tunnel.nft new file +17 -0
new file mode 100644
index 00000000..64b2f70b
@@ -0,0 +1,17 @@
+# v5.7-rc1~146^2~137^2~26
+# 925d844696d9 ("netfilter: nft_tunnel: add support for geneve opts")
+table netdev x {
+        tunnel y {
+                id 10
+                ip saddr 192.168.2.10
+                ip daddr 192.168.2.11
+                sport 10
+                dport 20
+                ttl 10
+                geneve {
+                        class 0x1010 opt-type 0x1 data "0x12345678"
+                        class 0x2010 opt-type 0x2 data "0x87654321"
+                        class 0x2020 opt-type 0x3 data "0x87654321abcdeffe"
+                }
+        }
+}
tests/shell/testcases/sets/0075tunnel_0 new file +75 -0

75 lines not on this page · raw message

tests/shell/testcases/sets/dumps/0075tunnel_0.json-nft new file +171 -0

171 lines not on this page · raw message

tests/shell/testcases/sets/dumps/0075tunnel_0.nft new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.50.1