Skip to contentSkip to conversation

src: use uint64_t for flags fields

PATCH In review Fernando Fernandez Mancera 3 participants 12 messages May 27, 2025 – Jun 12, 2025 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 src: use uint64_t for flags fields

The flags for the object tunnel already reach out 31, therefore, in order to be able to extend the flags field must be uint64_t. Otherwise, we will shift by more of the type size.

---
 include/obj.h   |  2 +-
 include/rule.h  |  2 +-
 include/set.h   |  2 +-
 include/utils.h |  2 +-
 src/chain.c     |  2 +-
 src/flowtable.c |  2 +-
 src/object.c    | 10 +++++-----
 src/table.c     |  2 +-
 src/utils.c     |  2 +-
 9 files changed, 13 insertions(+), 13 deletions(-)
include/obj.h +1 -1
index d217737..fc78e2a 100644
@@ -19,7 +19,7 @@ struct nftnl_obj {
 	uint32_t		family;
 	uint32_t		use;
 
-	uint32_t		flags;
+	uint64_t		flags;
 	uint64_t		handle;
 
 	struct {
include/rule.h +1 -1
index 036c722..6432786 100644
@@ -4,7 +4,7 @@
 struct nftnl_rule {
 	struct list_head head;
 
-	uint32_t	flags;
+	uint64_t	flags;
 	uint32_t	family;
 	const char	*table;
 	const char	*chain;
include/set.h +1 -1
index 55018b6..179f6ad 100644
@@ -30,7 +30,7 @@ struct nftnl_set {
 	} desc;
 	struct list_head	element_list;
 
-	uint32_t		flags;
+	uint64_t		flags;
 	uint32_t		gc_interval;
 	uint64_t		timeout;
 	struct list_head	expr_list;
include/utils.h +1 -1
index eed6127..5da2ddb 100644
@@ -79,7 +79,7 @@ int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type,
 			  	     uint32_t cmd, uint32_t type,
 				     uint32_t flags));
 
-int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
+int nftnl_set_str_attr(const char **dptr, uint64_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len);
 
 #endif
src/chain.c +1 -1
index 895108c..a9e18dc 100644
@@ -43,7 +43,7 @@ struct nftnl_chain {
 	uint64_t	packets;
 	uint64_t	bytes;
 	uint64_t	handle;
-	uint32_t	flags;
+	uint64_t	flags;
 	uint32_t	chain_id;
 
 	struct {
src/flowtable.c +1 -1
index fbbe0a8..c52ba0e 100644
@@ -29,7 +29,7 @@ struct nftnl_flowtable {
 	struct nftnl_str_array	dev_array;
 	uint32_t		ft_flags;
 	uint32_t		use;
-	uint32_t		flags;
+	uint64_t		flags;
 	uint64_t		handle;
 };
 
src/object.c +5 -5
index bfcceb9..f307815 100644
@@ -62,13 +62,13 @@ void nftnl_obj_free(const struct nftnl_obj *obj)
 EXPORT_SYMBOL(nftnl_obj_is_set);
 bool nftnl_obj_is_set(const struct nftnl_obj *obj, uint16_t attr)
 {
-	return obj->flags & (1 << attr);
+	return obj->flags & (1ULL << attr);
 }
 
 EXPORT_SYMBOL(nftnl_obj_unset);
 void nftnl_obj_unset(struct nftnl_obj *obj, uint16_t attr)
 {
-	if (!(obj->flags & (1 << attr)))
+	if (!(obj->flags & (1ULL << attr)))
 		return;
 
 	switch (attr) {
@@ -90,7 +90,7 @@ void nftnl_obj_unset(struct nftnl_obj *obj, uint16_t attr)
 		break;
 	}
 
-	obj->flags &= ~(1 << attr);
+	obj->flags &= ~(1ULL << attr);
 }
 
 static uint32_t nftnl_obj_validate[NFTNL_OBJ_MAX + 1] = {
@@ -153,7 +153,7 @@ int nftnl_obj_set_data(struct nftnl_obj *obj, uint16_t attr,
 		if (obj->ops->set(obj, attr, data, data_len) < 0)
 			return -1;
 	}
-	obj->flags |= (1 << attr);
+	obj->flags |= (1ULL << attr);
 	return 0;
 }
 
@@ -197,7 +197,7 @@ EXPORT_SYMBOL(nftnl_obj_get_data);
 const void *nftnl_obj_get_data(const struct nftnl_obj *obj, uint16_t attr,
 			       uint32_t *data_len)
 {
-	if (!(obj->flags & (1 << attr)))
+	if (!(obj->flags & (1ULL << attr)))
 		return NULL;
 
 	switch(attr) {
src/table.c +1 -1
index 9870dca..e183e2e 100644
@@ -29,7 +29,7 @@ struct nftnl_table {
 	uint32_t	table_flags;
 	uint64_t 	handle;
 	uint32_t	use;
-	uint32_t	flags;
+	uint64_t	flags;
 	uint32_t	owner;
 	struct {
 		void		*data;
src/utils.c +1 -1
index 5f2c5bf..7942d67 100644
@@ -133,7 +133,7 @@ void __noreturn __abi_breakage(const char *file, int line, const char *reason)
        exit(EXIT_FAILURE);
 }
 
-int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
+int nftnl_set_str_attr(const char **dptr, uint64_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len)
 {
 	if (*flags & (1 << attr))
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 2/2 tunnel: add support to geneve options

---
 include/libnftnl/object.h |   7 ++
 include/obj.h             |   9 +++
 src/libnftnl.map          |   4 +
 src/obj/tunnel.c          | 156 ++++++++++++++++++++++++++++++--------
 src/object.c              |  10 +++
 5 files changed, 156 insertions(+), 30 deletions(-)
include/libnftnl/object.h +7 -0
index 9930355..14a42cd 100644
@@ -117,15 +117,19 @@ enum {
 	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_GENEVE_OPTS,
 	__NFTNL_OBJ_TUNNEL_MAX,
 };
 
+#define NFTNL_TUNNEL_GENEVE_DATA_MAXLEN	127
+
 enum {
 	NFTNL_OBJ_SECMARK_CTX	= NFTNL_OBJ_BASE,
 	__NFTNL_OBJ_SECMARK_MAX,
 };
 
 struct nftnl_obj;
+struct nftnl_obj_tunnel_geneve;
 
 struct nftnl_obj *nftnl_obj_alloc(void);
 void nftnl_obj_free(const struct nftnl_obj *ne);
@@ -171,6 +175,9 @@ void nftnl_obj_list_del(struct nftnl_obj *t);
 int nftnl_obj_list_foreach(struct nftnl_obj_list *table_list,
 			   int (*cb)(struct nftnl_obj *t, void *data),
 			   void *data);
+int nftnl_obj_tunnel_geneve_opts_foreach(struct nftnl_obj *e,
+					 int (*cb)(struct nftnl_obj_tunnel_geneve *e, void *data),
+					 void *data);
 
 struct nftnl_obj_list_iter;
 struct nftnl_obj_list_iter *nftnl_obj_list_iter_create(struct nftnl_obj_list *l);
include/obj.h +9 -0
index fc78e2a..7c9898e 100644
@@ -9,6 +9,14 @@ struct nlattr;
 struct nlmsghdr;
 struct nftnl_obj;
 
+struct nftnl_obj_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 nftnl_obj {
 	struct list_head	head;
 	struct obj_ops		*ops;
@@ -92,6 +100,7 @@ struct nftnl_obj {
 						} v2;
 					} u;
 				} tun_erspan;
+				struct list_head	tun_geneve;
 			} u;
 		} tunnel;
 		struct nftnl_obj_secmark {
src/libnftnl.map +4 -0
index 8fffff1..e125017 100644
@@ -383,3 +383,7 @@ LIBNFTNL_16 {
 LIBNFTNL_17 {
   nftnl_set_elem_nlmsg_build;
 } LIBNFTNL_16;
+
+LIBNFTNL_18 {
+  nftnl_obj_tunnel_geneve_opts_foreach;
+} LIBNFTNL_17;
src/obj/tunnel.c +126 -30

198 lines not on this page · raw message

src/object.c +10 -0
index f307815..b5bdbbf 100644
@@ -49,12 +49,22 @@ struct nftnl_obj *nftnl_obj_alloc(void)
 EXPORT_SYMBOL(nftnl_obj_free);
 void nftnl_obj_free(const struct nftnl_obj *obj)
 {
+	struct nftnl_obj_tunnel_geneve *geneve, *next;
+
 	if (obj->flags & (1 << NFTNL_OBJ_TABLE))
 		xfree(obj->table);
 	if (obj->flags & (1 << NFTNL_OBJ_NAME))
 		xfree(obj->name);
 	if (obj->flags & (1 << NFTNL_OBJ_USERDATA))
 		xfree(obj->user.data);
+	if (obj->flags & (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_OPTS)) {
+		list_for_each_entry_safe(geneve, next,
+					 &obj->data.tunnel.u.tun_geneve,
+					 list) {
+			list_del(&geneve->list);
+			xfree(geneve);
+		}
+	}
 
 	xfree(obj);
 }
Signature · 1 line
2.49.0
maintainer
#2 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Fernando Fernandez Mancera <[email protected]> wrote:

Hi Fernando

Thanks for working on this, I got inquiries as to nft_tunnel.c and how to make use of this stuff...

include/libnftnl/object.h enum { · line 120

Quoted 9 lines
include/libnftnl/object.h +1 -0
index 9930355..14a42cd 100644
@@ -117,15 +117,19 @@ enum {
 	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_GENEVE_OPTS,

If every flavour gets its own flag in the tunnel namespace we'll run out of u64 in no time.

AFAICS these are mutually exclusive, e.g. NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX and NFTNL_OBJ_TUNNEL_VXLAN_GBP cannot be active at the same time.

Is there a way to re-use the internal flag namespace depending on the tunnel subtype?

Or to have distinct tunnel object types?

object -> tunnel -> {vxlan, erspan, ...} ?

As-is, how is this API supposed to be used? The internal union seems to be asking for trouble later, when e.g. 'getting' NFTNL_OBJ_TUNNEL_GENEVE_OPTS on something that was instantiated as vxlan tunnel and fields aliasing to unexpected values.

Perhaps the first use of any of the NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX etc values in a setter should interally "lock" the object to the given subtype?

That might allow to NOT use ->flags for those enum values and instead keep track of them via overlapping bits.

We'd need some internal 'enum nft_obj_tunnel_type' that marks which part of the union is useable/instantiated so we can reject requests to set bits that are not available for the specific tunnel type.

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 77

Quoted 8 lines
include/libnftnl/object.h +2 -0
 	switch (type) {
 	case NFTNL_OBJ_TUNNEL_ID:
@@ -72,6 +73,15 @@ nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type,
 	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
 		memcpy(&tun->u.tun_erspan.u.v2.dir, data, data_len);
 		break;
+	case NFTNL_OBJ_TUNNEL_GENEVE_OPTS:
+		geneve = malloc(sizeof(struct nftnl_obj_tunnel_geneve));

No null check. Applies to a few other spots too.

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 78

Quoted 1 line
include/libnftnl/object.h +1 -0
+		memcpy(geneve, data, data_len);

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

I don't like exposing the struct and also the list abstraction getting exposed to libnftnl users.

As-is, there is:

json/bison -> struct tunnel_geneve -> nftnl_obj_set_data(&obj, NFTNL_OBJ_TUNNEL_GENEVE_OPTS, &blob);
-> nftnl_obj_tunnel_build -> mnl_attr_put_u16( ...

I think it will be better if we make this internal to nftables and have pass-through for the entire geneve blob,i.e.

json/bison -> struct tunnel_geneve ->
(re)alloc buffer: mnl_nlmsg_put_header(), mnl_attr_put_u16 -> etc. nftnl_obj_set_data(obj, NFTNL_OBJ_TUNNEL_GENEVE_OPTS, blob) -> nftnl_obj_tunnel_build -> memcpy into NFTA_TUNNEL_KEY_OPTS nest container.

Less elegant but I have a hard time coming up with an api that is extensible and doesn't need shared binary structures.

Another option would be to use udata in the frontend and then make libnftnl xlate the udata to netlink proper. But I don't like this because a) udata is supposed to be "DO NOT TOUCH" and b) it means doubling the work compared to just doing mnl_attr_put() calls in nftables.

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 84

Quoted 6 lines
include/libnftnl/object.h +6 -0
+	if (tb[NFTA_TUNNEL_KEY_GENEVE_DATA]) {
+		uint32_t len = mnl_attr_get_payload_len(tb[NFTA_TUNNEL_KEY_GENEVE_DATA]);
+
+		memcpy(geneve->data,
+		       mnl_attr_get_payload(tb[NFTA_TUNNEL_KEY_GENEVE_DATA]),
+		       len);

This should cap 'len' by sizeof(geneve->data).
But I'm not sure this deserialization should be done in libnftnl in the first place, see above.

#3 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 24 lines · Florian Westphal

Fernando Fernandez Mancera <[email protected]> wrote:

Hi Fernando

Thanks for working on this, I got inquiries as to nft_tunnel.c and how to make use of this stuff...

include/libnftnl/object.h enum { · line 120

Quoted 9 lines
include/libnftnl/object.h +1 -0
index 9930355..14a42cd 100644
@@ -117,15 +117,19 @@ enum {
  	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
  	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
  	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_GENEVE_OPTS,

If every flavour gets its own flag in the tunnel namespace we'll run out of u64 in no time.

AFAICS these are mutually exclusive, e.g. NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX and NFTNL_OBJ_TUNNEL_VXLAN_GBP cannot be active at the same time.

Quoted 7 lines

Is there a way to re-use the internal flag namespace depending on the tunnel subtype?

Or to have distinct tunnel object types?

object -> tunnel -> {vxlan, erspan, ...} ?

IMHO, this could be done by providing libnftnl its own object tunnel API. Although that would require to expose more functions, but tunnel object could have its own flags field.

In addition, I am afraid that ERSPAN and VXLAN enum fields have been exposed in a released version so removing them would be a breaking change. I am not sure whether that is acceptable or not.

Quoted 9 lines

As-is, how is this API supposed to be used? The internal union seems to be asking for trouble later, when e.g. 'getting' NFTNL_OBJ_TUNNEL_GENEVE_OPTS on something that was instantiated as vxlan tunnel and fields aliasing to unexpected values.

Perhaps the first use of any of the NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX etc values in a setter should interally "lock" the object to the given subtype?

Quoted 7 lines

That might allow to NOT use ->flags for those enum values and instead keep track of them via overlapping bits.

We'd need some internal 'enum nft_obj_tunnel_type' that marks which part of the union is useable/instantiated so we can reject requests to set bits that are not available for the specific tunnel type.

Yes, that would help but isn't that the reason why the flags field is there? I mean, currently the same problem would exist with the different object variants e.g the union of "struct nftnl_obj_*". When trying to get the attribute we always check that the flag is set.

Quoted 11 lines

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 77

Quoted 8 lines
include/libnftnl/object.h +2 -0
  	switch (type) {
  	case NFTNL_OBJ_TUNNEL_ID:
@@ -72,6 +73,15 @@ nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type,
  	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
  		memcpy(&tun->u.tun_erspan.u.v2.dir, data, data_len);
  		break;
+	case NFTNL_OBJ_TUNNEL_GENEVE_OPTS:
+		geneve = malloc(sizeof(struct nftnl_obj_tunnel_geneve));

No null check. Applies to a few other spots too.

Ah right. Yes, will fix that. Thanks.

Quoted 5 lines

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 78

Quoted 1 line
include/libnftnl/object.h +1 -0
+		memcpy(geneve, data, data_len);

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I agree that exposing the list abstraction to the user is problematic and shouldn't be done. As you mentioned below, I couldn't come up with a better API on libnftnl. Thinking about it, we could expose only the relevant fields by putting them in a isolated struct.

Quoted 13 lines

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

I don't like exposing the struct and also the list abstraction getting exposed to libnftnl users.

As-is, there is:

json/bison -> struct tunnel_geneve -> nftnl_obj_set_data(&obj, NFTNL_OBJ_TUNNEL_GENEVE_OPTS, &blob);
-> nftnl_obj_tunnel_build -> mnl_attr_put_u16( ...

Quoted 9 lines

I think it will be better if we make this internal to nftables and have pass-through for the entire geneve blob,i.e.

json/bison -> struct tunnel_geneve ->
(re)alloc buffer: mnl_nlmsg_put_header(), mnl_attr_put_u16 -> etc. nftnl_obj_set_data(obj, NFTNL_OBJ_TUNNEL_GENEVE_OPTS, blob) -> nftnl_obj_tunnel_build -> memcpy into NFTA_TUNNEL_KEY_OPTS nest container.

I am fine with this, although just as a note I chose the linked list path mainly to make it more flexible for users. AFAICS, libnftnl users usually do not need to build the netlink blob themselves.

So, the problem in essence is the shared binary structure. If I do not expose the list abstraction, copying the allocated struct would still be problematic, right?

Thank you for all the comments, Florian.

maintainer
#4 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 15 lines · Fernando Fernandez Mancera
Quoted 7 lines

If every flavour gets its own flag in the tunnel namespace we'll run out of u64 in no time.

AFAICS these are mutually exclusive, e.g. NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX and NFTNL_OBJ_TUNNEL_VXLAN_GBP cannot be active at the same time.

Quoted 7 lines

Is there a way to re-use the internal flag namespace depending on the tunnel subtype?

Or to have distinct tunnel object types?

object -> tunnel -> {vxlan, erspan, ...} ?

Quoted 7 lines

IMHO, this could be done by providing libnftnl its own object tunnel API. Although that would require to expose more functions, but tunnel object could have its own flags field.

In addition, I am afraid that ERSPAN and VXLAN enum fields have been exposed in a released version so removing them would be a breaking change. I am not sure whether that is acceptable or not.

Yep, looks like ERSPAN and VXLAN have to remain in place.

Quoted 17 lines
Quoted 9 lines

As-is, how is this API supposed to be used? The internal union seems to be asking for trouble later, when e.g. 'getting' NFTNL_OBJ_TUNNEL_GENEVE_OPTS on something that was instantiated as vxlan tunnel and fields aliasing to unexpected values.

Perhaps the first use of any of the NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX etc values in a setter should interally "lock" the object to the given subtype?

Quoted 7 lines

That might allow to NOT use ->flags for those enum values and instead keep track of them via overlapping bits.

We'd need some internal 'enum nft_obj_tunnel_type' that marks which part of the union is useable/instantiated so we can reject requests to set bits that are not available for the specific tunnel type.

Quoted 4 lines

Yes, that would help but isn't that the reason why the flags field is there? I mean, currently the same problem would exist with the different object variants e.g the union of "struct nftnl_obj_*". When trying to get the attribute we always check that the flag is set.

Right :-/

Quoted 10 lines
Quoted 5 lines

src/obj/tunnel.c nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 78

Quoted 1 line
src/obj/tunnel.c +1 -0
+		memcpy(geneve, data, data_len);

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I agree that exposing the list abstraction to the user is problematic and shouldn't be done. As you mentioned below, I couldn't come up with a better API on libnftnl. Thinking about it, we could expose only the relevant fields by putting them in a isolated struct.

Looking at the nftables patches, I think some of what is done there should be moved to libnftl, e.g. the $<obj>0->tunnel.type = TUNNEL_GENEVE; thing. I mean, it makes sense to track the type of the object but wouldn't it make more sense to do this in libnftnl?

Thinking about it some more, I'm not sure if NFT_OBJECT_TUNNEL was a good idea.

We have:
NFT_OBJECT_CT_HELPER
NFT_OBJECT_CT_TIMEOUT
NFT_OBJECT_CT_EXPECT

What about following that model, e.g.:

#define NFT_OBJECT_TUNNEL_GENEVE 11
#define NFT_OBJECT_TUNNEL_ERSPAN 12
...

?
Pablo, what do you think? Maybe you tried this before and it wasn't good either because of lots of duplication with common tunnel attributes?

Back to the geneve option handling, I think it would be best to expose all the fields in the struct via deticated setters/getters, i.e. from

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;
};

expose: geneve_class, type, and data.

This could be done by moving some of the nftables code you added to libnftnl.

Instead of this in nftables:

  list_for_each_entry(geneve, &obj->tunnel.geneve_opts, list) {
        nftnl_obj_set_data(nlo, NFTNL_OBJ_TUNNEL_GENEVE_OPTS,
                           geneve, sizeof(struct tunnel_geneve));
        }

}

do this (error handling omitted for brevity):

  list_for_each_entry(geneve, &obj->tunnel.geneve_opts, list) {
	struct nftnl_obj_tunnel_geneve_opts *geneve;

	geneve = nftnl_obj_tunnel_geneve_opts_alloc();

	nftnl_obj_tunnel_geneve_opts_set_u32(geneve, NFT_OBJECT_TUNNEL_GENEVE_OPT_CLASS, geneve->geneve_class);
	nftnl_obj_tunnel_geneve_opts_set_u32(geneve, NFT_OBJECT_TUNNEL_GENEVE_OPT_TYPE, geneve->geneve_type);
        nftnl_obj_tunnel_geneve_opts_set_data(geneve, NFT_OBJECT_TUNNEL_GENEVE_OPT_DATA,
					      geneve->data, geneve->data_len);

        nftnl_obj_tunnel_add_geneve_opts(nlo, geneve);

(nlo/nftnl_tunnel and nftnl_obj_tunnel_geneve_opts would be similar to
nftnl_chain vs nftnl_rule).

The major downside is a lot of more API calls that need to be added to libnftnl :-(

I don't see any other solutions at this time.

#5 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

In reply to Florian Westphal

Hi,

Quoted 63 lines · Florian Westphal

Fernando Fernandez Mancera <[email protected]> wrote:

Hi Fernando

Thanks for working on this, I got inquiries as to nft_tunnel.c and how to make use of this stuff...

include/libnftnl/object.h enum { · line 120

Quoted 9 lines
include/libnftnl/object.h +1 -0
index 9930355..14a42cd 100644
@@ -117,15 +117,19 @@ enum {
 	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_GENEVE_OPTS,

If every flavour gets its own flag in the tunnel namespace we'll run out of u64 in no time.

AFAICS these are mutually exclusive, e.g. NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX and NFTNL_OBJ_TUNNEL_VXLAN_GBP cannot be active at the same time.

Is there a way to re-use the internal flag namespace depending on the tunnel subtype?

Or to have distinct tunnel object types?

object -> tunnel -> {vxlan, erspan, ...} ?

As-is, how is this API supposed to be used? The internal union seems to be asking for trouble later, when e.g. 'getting' NFTNL_OBJ_TUNNEL_GENEVE_OPTS on something that was instantiated as vxlan tunnel and fields aliasing to unexpected values.

Perhaps the first use of any of the NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX etc values in a setter should interally "lock" the object to the given subtype?

That might allow to NOT use ->flags for those enum values and instead keep track of them via overlapping bits.

We'd need some internal 'enum nft_obj_tunnel_type' that marks which part of the union is useable/instantiated so we can reject requests to set bits that are not available for the specific tunnel type.

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 77

Quoted 8 lines
include/libnftnl/object.h +2 -0
 	switch (type) {
 	case NFTNL_OBJ_TUNNEL_ID:
@@ -72,6 +73,15 @@ nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type,
 	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
 		memcpy(&tun->u.tun_erspan.u.v2.dir, data, data_len);
 		break;
+	case NFTNL_OBJ_TUNNEL_GENEVE_OPTS:
+		geneve = malloc(sizeof(struct nftnl_obj_tunnel_geneve));

No null check. Applies to a few other spots too.

include/libnftnl/object.h nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 78

Quoted 1 line
include/libnftnl/object.h +1 -0
+		memcpy(geneve, data, data_len);

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

Quoted 4 lines

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

Maybe this API for tunnel options are proposed in this patch?

Consider this a sketch/proposal, this is compiled tested only.

struct obj_ops also needs a .free interface to release the tunnel options object.

From 56362a22008911873bd8b8a2f55e68406e55e0de Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Tue, 10 Jun 2025 01:43:28 +0200
Subject: [PATCH libnftnl] tunnel: rework options

Only vxlan gbp can work before this patch because NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR is off by one in the internal object flags.

Replace them by NFTNL_OBJ_TUNNEL_OPTS and add a new opaque nftnl_tunnel_opts struct and nftnl_tunnel_opts_set() to set up tunnel options.

---
 include/libnftnl/object.h |  32 ++++-
 include/obj.h             |  16 +--
 src/obj/tunnel.c          | 275 +++++++++++++++++++++++++++-----------
 3 files changed, 222 insertions(+), 101 deletions(-)
include/libnftnl/object.h +27 -5
index 9930355bb8f0..0331cf7ac5d8 100644
@@ -112,14 +112,36 @@ enum {
 	NFTNL_OBJ_TUNNEL_FLAGS,
 	NFTNL_OBJ_TUNNEL_TOS,
 	NFTNL_OBJ_TUNNEL_TTL,
-	NFTNL_OBJ_TUNNEL_VXLAN_GBP,
-	NFTNL_OBJ_TUNNEL_ERSPAN_VERSION,
-	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
-	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
-	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_OPTS,
 	__NFTNL_OBJ_TUNNEL_MAX,
 };
 
+#define NFTNL_TUNNEL_TYPE	0
+#define NFTNL_TUNNEL_BASE	4
+
+enum nftnl_tunnel_type {
+	NFTNL_TUNNEL_TYPE_VXLAN,
+	NFTNL_TUNNEL_TYPE_ERSPAN,
+};
+
+enum {
+	NFTNL_TUNNEL_VXLAN_GBP		= NFTNL_TUNNEL_BASE,
+	__NFTNL_TUNNEL_VXLAN_MAX,
+};
+
+enum {
+	NFTNL_TUNNEL_ERSPAN_VERSION	= NFTNL_TUNNEL_BASE,
+	NFTNL_TUNNEL_ERSPAN_V1_INDEX,
+	NFTNL_TUNNEL_ERSPAN_V2_HWID,
+	NFTNL_TUNNEL_ERSPAN_V2_DIR,
+	__NFTNL_TUNNEL_ERSPAN_MAX,
+};
+
+struct nftnl_tunnel_opts;
+struct nftnl_tunnel_opts *nftnl_tunnel_opts_alloc(enum nftnl_tunnel_type type);
+int nftnl_tunnel_opts_set(struct nftnl_tunnel_opts *opts, uint16_t type,
+			  const void *data, uint32_t data_len);
+
 enum {
 	NFTNL_OBJ_SECMARK_CTX	= NFTNL_OBJ_BASE,
 	__NFTNL_OBJ_SECMARK_MAX,
include/obj.h +1 -15
index d2177377860d..5d3c4eced199 100644
@@ -78,21 +78,7 @@ struct nftnl_obj {
 			uint32_t	tun_flags;
 			uint8_t		tun_tos;
 			uint8_t		tun_ttl;
-			union {
-				struct {
-					uint32_t	gbp;
-				} tun_vxlan;
-				struct {
-					uint32_t	version;
-					union {
-						uint32_t	v1_index;
-						struct {
-							uint8_t	hwid;
-							uint8_t	dir;
-						} v2;
-					} u;
-				} tun_erspan;
-			} u;
+			struct nftnl_tunnel_opts *tun_opts;
 		} tunnel;
 		struct nftnl_obj_secmark {
 			char		ctx[NFT_SECMARK_CTX_MAXLEN];
src/obj/tunnel.c +194 -81

374 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer
#6 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

Maybe this API for tunnel options are proposed in this patch?

Looks good, thanks Pablo!

Quoted 4 lines

Consider this a sketch/proposal, this is compiled tested only.

struct obj_ops also needs a .free interface to release the tunnel options object.

nftnl_tunnel_opts_set() seems to be useable for erspan and vxlan.

Do you have a suggestion for the geneve case where 'infinite' options get added?

Maybe add nftnl_tunnel_opts_append() ? Or nftnl_tunnel_opts_add(), so api user can push multiple option objects to a tunnel, similar to how rules get added to chains?

Would probably require a few more api calls including iterators.

Fernando, do you spot anything else thats missing for your use cases?

#7 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 18 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

Maybe this API for tunnel options are proposed in this patch?

Looks good, thanks Pablo!

Quoted 4 lines

Consider this a sketch/proposal, this is compiled tested only.

struct obj_ops also needs a .free interface to release the tunnel options object.

Quoted 8 lines

nftnl_tunnel_opts_set() seems to be useable for erspan and vxlan.

Do you have a suggestion for the geneve case where 'infinite' options get added?

Maybe add nftnl_tunnel_opts_append() ? Or nftnl_tunnel_opts_add(), so api user can push multiple option objects to a tunnel, similar to how rules get added to chains?

nftnl_tunnel_opts_add() sounds good.

It should be possible to replace nftnl_tunnel_opts_set() by nftnl_tunnel_opts_add(), then a single function for this purpose is provided. As for vxlan and erpan, allow only one single call to nftnl_tunnel_opts_add().

See attachment, compile tested only.

Quoted 3 lines

Would probably require a few more api calls including iterators.

Fernando, do you spot anything else thats missing for your use cases?

From a8bc6cc58ae4728134e7dc16bf4e553c2c58e176 Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Tue, 10 Jun 2025 01:43:28 +0200
Subject: [PATCH libnftnl,v2,WIP] tunnel: rework options

Only vxlan gbp can work before this patch because NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR is off by one in the internal object flags.

Replace them by NFTNL_OBJ_TUNNEL_OPTS and add two new opaque nftnl_tunnel_opts and nftnl_tunnel_opt structs to represent tunnel options.

- nftnl_tunnel_opt_alloc() allocates one tunnel option.
- nftnl_tunnel_opt_set() to sets it up.

Then, to manage the list of options:

- nftnl_tunnel_opts_alloc() allocates a list of tunnel options.
- nftnl_tunnel_opts_add() adds a option to the list.

Although vxlan and erspan support for a single tunnel option at this stage, this API prepares for supporting gevene which allows for more tunnel options.

TODO: Missing .free interface for objects to release this list of objects.

---
 include/libnftnl/object.h |  38 +++-
 include/obj.h             |  16 +-
 src/obj/tunnel.c          | 353 +++++++++++++++++++++++++++++---------
 3 files changed, 306 insertions(+), 101 deletions(-)
include/libnftnl/object.h +33 -5
index 9930355bb8f0..8ce62b7d97a2 100644
@@ -112,14 +112,42 @@ enum {
 	NFTNL_OBJ_TUNNEL_FLAGS,
 	NFTNL_OBJ_TUNNEL_TOS,
 	NFTNL_OBJ_TUNNEL_TTL,
-	NFTNL_OBJ_TUNNEL_VXLAN_GBP,
-	NFTNL_OBJ_TUNNEL_ERSPAN_VERSION,
-	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
-	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
-	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_OPTS,
 	__NFTNL_OBJ_TUNNEL_MAX,
 };
 
+#define NFTNL_TUNNEL_TYPE	0
+#define NFTNL_TUNNEL_BASE	4
+
+enum nftnl_tunnel_type {
+	NFTNL_TUNNEL_TYPE_VXLAN,
+	NFTNL_TUNNEL_TYPE_ERSPAN,
+};
+
+enum {
+	NFTNL_TUNNEL_VXLAN_GBP		= NFTNL_TUNNEL_BASE,
+	__NFTNL_TUNNEL_VXLAN_MAX,
+};
+
+enum {
+	NFTNL_TUNNEL_ERSPAN_VERSION	= NFTNL_TUNNEL_BASE,
+	NFTNL_TUNNEL_ERSPAN_V1_INDEX,
+	NFTNL_TUNNEL_ERSPAN_V2_HWID,
+	NFTNL_TUNNEL_ERSPAN_V2_DIR,
+	__NFTNL_TUNNEL_ERSPAN_MAX,
+};
+
+struct nftnl_tunnel_opt;
+struct nftnl_tunnel_opts;
+
+struct nftnl_tunnel_opts *nftnl_tunnel_opts_alloc(enum nftnl_tunnel_type type);
+int nftnl_tunnel_opts_add(struct nftnl_tunnel_opts *opts,
+			  struct nftnl_tunnel_opt *opt);
+
+struct nftnl_tunnel_opt *nftnl_tunnel_opt_alloc(enum nftnl_tunnel_type type);
+int nftnl_tunnel_opt_set(struct nftnl_tunnel_opt *opt, uint16_t type,
+			 const void *data, uint32_t data_len);
+
 enum {
 	NFTNL_OBJ_SECMARK_CTX	= NFTNL_OBJ_BASE,
 	__NFTNL_OBJ_SECMARK_MAX,
include/obj.h +1 -15
index d2177377860d..5d3c4eced199 100644
@@ -78,21 +78,7 @@ struct nftnl_obj {
 			uint32_t	tun_flags;
 			uint8_t		tun_tos;
 			uint8_t		tun_ttl;
-			union {
-				struct {
-					uint32_t	gbp;
-				} tun_vxlan;
-				struct {
-					uint32_t	version;
-					union {
-						uint32_t	v1_index;
-						struct {
-							uint8_t	hwid;
-							uint8_t	dir;
-						} v2;
-					} u;
-				} tun_erspan;
-			} u;
+			struct nftnl_tunnel_opts *tun_opts;
 		} tunnel;
 		struct nftnl_obj_secmark {
 			char		ctx[NFT_SECMARK_CTX_MAXLEN];
src/obj/tunnel.c +272 -81

459 lines not on this page · raw message

Signature · 1 line
2.30.2
#8 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 28 lines · Pablo Neira Ayuso
Quoted 18 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

Maybe this API for tunnel options are proposed in this patch?

Looks good, thanks Pablo!

Quoted 4 lines

Consider this a sketch/proposal, this is compiled tested only.

struct obj_ops also needs a .free interface to release the tunnel options object.

Quoted 8 lines

nftnl_tunnel_opts_set() seems to be useable for erspan and vxlan.

Do you have a suggestion for the geneve case where 'infinite' options get added?

Maybe add nftnl_tunnel_opts_append() ? Or nftnl_tunnel_opts_add(), so api user can push multiple option objects to a tunnel, similar to how rules get added to chains?

Quoted 9 lines

nftnl_tunnel_opts_add() sounds good.

It should be possible to replace nftnl_tunnel_opts_set() by nftnl_tunnel_opts_add(), then a single function for this purpose is provided. As for vxlan and erpan, allow only one single call to nftnl_tunnel_opts_add().

See attachment, compile tested only.

This looks good to me. Let me include it on my series and extend the free interface.

Thanks Pablo!

Quoted 3 lines
Quoted 3 lines

Would probably require a few more api calls including iterators.

Fernando, do you spot anything else thats missing for your use cases?

#9 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 38 lines · Fernando Fernandez Mancera
Quoted 35 lines · Pablo Neira Ayuso
Quoted 18 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines

Hmm, this looks like the API leaks internal data layout from nftables to libnftnl and vice versa? IMO thats a non-starter, sorry.

I see that options are essentially unlimited values, so perhaps nftables should build the netlink blob(s) directly, similar to nftnl_udata()?

Pablo, any better idea?

Maybe this API for tunnel options are proposed in this patch?

Looks good, thanks Pablo!

Quoted 4 lines

Consider this a sketch/proposal, this is compiled tested only.

struct obj_ops also needs a .free interface to release the tunnel options object.

Quoted 8 lines

nftnl_tunnel_opts_set() seems to be useable for erspan and vxlan.

Do you have a suggestion for the geneve case where 'infinite' options get added?

Maybe add nftnl_tunnel_opts_append() ? Or nftnl_tunnel_opts_add(), so api user can push multiple option objects to a tunnel, similar to how rules get added to chains?

nftnl_tunnel_opts_add() sounds good.

It should be possible to replace nftnl_tunnel_opts_set() by nftnl_tunnel_opts_add(), then a single function for this purpose is provided. As for vxlan and erpan, allow only one single call to nftnl_tunnel_opts_add().

Quoted 5 lines
Quoted 2 lines

See attachment, compile tested only.

This looks good to me. Let me include it on my series and extend the free interface.

Sure, go ahead, thanks.

maintainer
#10 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

In reply to Fernando Fernandez Mancera

src/obj/tunnel.c nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 84

Quoted 9 lines · Fernando Fernandez Mancera
src/obj/tunnel.c +9 -0
+	case NFTNL_OBJ_TUNNEL_GENEVE_OPTS:
+		geneve = malloc(sizeof(struct nftnl_obj_tunnel_geneve));
+		memcpy(geneve, data, data_len);
+
+		if (!(e->flags & (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_OPTS)))
+			INIT_LIST_HEAD(&tun->u.tun_geneve);
+
+		list_add_tail(&geneve->list, &tun->u.tun_geneve);
+		break;

I missed this earlier. Do we have precedence for this?

AFAIK for all existing setters, if you do

nftnl_foo_set_data(obj, OPT_FOO, &d, len);
nftnl_foo_set_data(obj, OPT_FOO, &d2, len2);

Then d2 replaces d, it doesn't silently append/add to the object.

#11 lore raw

Re: [PATCH 2/2 libnftnl v2] tunnel: add support to geneve options

Quoted 13 lines · Florian Westphal

src/obj/tunnel.c nftnl_obj_tunnel_set(struct nftnl_obj *e, uint16_t type, · line 84

Quoted 9 lines · Fernando Fernandez Mancera
src/obj/tunnel.c +9 -0
+	case NFTNL_OBJ_TUNNEL_GENEVE_OPTS:
+		geneve = malloc(sizeof(struct nftnl_obj_tunnel_geneve));
+		memcpy(geneve, data, data_len);
+
+		if (!(e->flags & (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_OPTS)))
+			INIT_LIST_HEAD(&tun->u.tun_geneve);
+
+		list_add_tail(&geneve->list, &tun->u.tun_geneve);
+		break;

I missed this earlier. Do we have precedence for this?

Quoted 8 lines

AFAIK for all existing setters, if you do

nftnl_foo_set_data(obj, OPT_FOO, &d, len);
nftnl_foo_set_data(obj, OPT_FOO, &d2, len2);

Then d2 replaces d, it doesn't silently append/add to the object.

Good catch. I thought yes, but no, there is no precedence for this. Other similar situations expose a dedicated function to append items to a list.

So we should do the same thing on this situation.