Skip to contentSkip to conversation

src: use uint64_t for flags fields

PATCH In review Fernando Fernandez Mancera 1 participant 3 messages Apr 15, 2025 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 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 2/2 tunnel: add support to geneve

---
 include/libnftnl/object.h |   5 ++
 include/obj.h             |   6 +++
 src/obj/tunnel.c          | 103 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
include/libnftnl/object.h +5 -0
index 9930355..0c695ea 100644
@@ -117,9 +117,14 @@ enum {
 	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
 	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_GENEVE_CLASS,
+	NFTNL_OBJ_TUNNEL_GENEVE_TYPE,
+	NFTNL_OBJ_TUNNEL_GENEVE_DATA,
 	__NFTNL_OBJ_TUNNEL_MAX,
 };
 
+#define NFTNL_TUNNEL_GENEVE_DATA_MAXLEN	126
+
 enum {
 	NFTNL_OBJ_SECMARK_CTX	= NFTNL_OBJ_BASE,
 	__NFTNL_OBJ_SECMARK_MAX,
include/obj.h +6 -0
index fc78e2a..e6c1cbf 100644
@@ -92,6 +92,12 @@ struct nftnl_obj {
 						} v2;
 					} u;
 				} tun_erspan;
+				struct {
+					uint16_t	geneve_class;
+					uint8_t		type;
+					uint8_t		data[NFTNL_TUNNEL_GENEVE_DATA_MAXLEN];
+					uint32_t	data_len;
+				} tun_geneve;
 			} u;
 		} tunnel;
 		struct nftnl_obj_secmark {
src/obj/tunnel.c +103 -0

145 lines not on this page · raw message

Signature · 1 line
2.49.0
#2 lore raw

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

Quoted 13 lines · Fernando Fernandez Mancera
---
  include/libnftnl/object.h |   5 ++
  include/obj.h             |   6 +++
  src/obj/tunnel.c          | 103 ++++++++++++++++++++++++++++++++++++++
  3 files changed, 114 insertions(+)
include/libnftnl/object.h +0 -0
index 9930355..0c695ea 100644
@@ -117,9 +117,14 @@ enum {
  	NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX,

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

Quoted 10 lines
include/libnftnl/object.h +5 -0
  	NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID,
  	NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR,
+	NFTNL_OBJ_TUNNEL_GENEVE_CLASS,
+	NFTNL_OBJ_TUNNEL_GENEVE_TYPE,
+	NFTNL_OBJ_TUNNEL_GENEVE_DATA,
  	__NFTNL_OBJ_TUNNEL_MAX,
  };
 
+#define NFTNL_TUNNEL_GENEVE_DATA_MAXLEN	126
+

I just noticed this should be 127.. anyway, it doesn't matter much as any length not divisible by 4 will be an invalid argument on kernel side so 127 won't make a difference. Maybe 124 is better?

Quoted 176 lines
include/libnftnl/object.h +0 -0
  enum {
  	NFTNL_OBJ_SECMARK_CTX	= NFTNL_OBJ_BASE,
  	__NFTNL_OBJ_SECMARK_MAX,
include/obj.h +6 -0
index fc78e2a..e6c1cbf 100644
@@ -92,6 +92,12 @@ struct nftnl_obj {
  						} v2;
  					} u;
  				} tun_erspan;
+				struct {
+					uint16_t	geneve_class;
+					uint8_t		type;
+					uint8_t		data[NFTNL_TUNNEL_GENEVE_DATA_MAXLEN];
+					uint32_t	data_len;
+				} tun_geneve;
  			} u;
  		} tunnel;
  		struct nftnl_obj_secmark {
src/obj/tunnel.c +103 -0
index 8941e39..27a6acd 100644
@@ -72,6 +72,16 @@ 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_CLASS:
+		memcpy(&tun->u.tun_geneve.geneve_class, data, data_len);
+		break;
+	case NFTNL_OBJ_TUNNEL_GENEVE_TYPE:
+		memcpy(&tun->u.tun_geneve.type, data, data_len);
+		break;
+	case NFTNL_OBJ_TUNNEL_GENEVE_DATA:
+		memcpy(&tun->u.tun_geneve.data, data, data_len);
+		tun->u.tun_geneve.data_len = data_len;
+		break;
  	}
  	return 0;
  }
@@ -131,6 +141,15 @@ nftnl_obj_tunnel_get(const struct nftnl_obj *e, uint16_t type,
  	case NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR:
  		*data_len = sizeof(tun->u.tun_erspan.u.v2.dir);
  		return &tun->u.tun_erspan.u.v2.dir;
+	case NFTNL_OBJ_TUNNEL_GENEVE_CLASS:
+		*data_len = sizeof(tun->u.tun_geneve.geneve_class);
+		return &tun->u.tun_geneve.geneve_class;
+	case NFTNL_OBJ_TUNNEL_GENEVE_TYPE:
+		*data_len = sizeof(tun->u.tun_geneve.type);
+		return &tun->u.tun_geneve.type;
+	case NFTNL_OBJ_TUNNEL_GENEVE_DATA:
+		*data_len = tun->u.tun_geneve.data_len;
+		return &tun->u.tun_geneve.data;
  	}
  	return NULL;
  }
@@ -240,6 +259,21 @@ nftnl_obj_tunnel_build(struct nlmsghdr *nlh, const struct nftnl_obj *e)
  		mnl_attr_nest_end(nlh, nest_inner);
  		mnl_attr_nest_end(nlh, nest);
  	}
+	if (e->flags & (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_CLASS) &&
+	    e->flags & (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_TYPE) &&
+	    e->flags & (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_DATA)) {
+		nest = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS);
+		nest_inner = mnl_attr_nest_start(nlh, NFTA_TUNNEL_KEY_OPTS_GENEVE);
+		mnl_attr_put_u16(nlh, NFTA_TUNNEL_KEY_GENEVE_CLASS,
+				 htons(tun->u.tun_geneve.geneve_class));
+		mnl_attr_put_u8(nlh, NFTA_TUNNEL_KEY_GENEVE_TYPE,
+				tun->u.tun_geneve.type);
+		mnl_attr_put(nlh, NFTA_TUNNEL_KEY_GENEVE_DATA,
+			     tun->u.tun_geneve.data_len,
+			     tun->u.tun_geneve.data);
+		mnl_attr_nest_end(nlh, nest_inner);
+		mnl_attr_nest_end(nlh, nest);
+	}
  }
 
  static int nftnl_obj_tunnel_ip_cb(const struct nlattr *attr, void *data)
@@ -335,6 +369,68 @@ static int nftnl_obj_tunnel_parse_ip6(struct nftnl_obj *e, struct nlattr *attr,
  	return 0;
  }
 
+static int nftnl_obj_tunnel_geneve_cb(const struct nlattr *attr, void *data)
+{
+	const struct nlattr **tb = data;
+	int type = mnl_attr_get_type(attr);
+
+	if (mnl_attr_type_valid(attr, NFTA_TUNNEL_KEY_GENEVE_MAX) < 0)
+		return MNL_CB_OK;
+
+	switch (type) {
+	case NFTA_TUNNEL_KEY_GENEVE_CLASS:
+		if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
+			abi_breakage();
+		break;
+	case NFTA_TUNNEL_KEY_GENEVE_TYPE:
+		if (mnl_attr_validate(attr, MNL_TYPE_U8) < 0)
+			abi_breakage();
+		break;
+	case NFTA_TUNNEL_KEY_GENEVE_DATA:
+		if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0)
+			abi_breakage();
+		break;
+	}
+
+	tb[type] = attr;
+	return MNL_CB_OK;
+}
+
+static int
+nftnl_obj_tunnel_parse_geneve(struct nftnl_obj *e, struct nlattr *attr,
+			      struct nftnl_obj_tunnel *tun)
+{
+	struct nlattr *tb[NFTA_TUNNEL_KEY_GENEVE_MAX + 1] = {};
+
+	if (mnl_attr_parse_nested(attr, nftnl_obj_tunnel_geneve_cb, tb) < 0)
+		return -1;
+
+	if (tb[NFTA_TUNNEL_KEY_GENEVE_CLASS]) {
+		tun->u.tun_geneve.geneve_class =
+			ntohs(mnl_attr_get_u16(tb[NFTA_TUNNEL_KEY_GENEVE_CLASS]));
+		e->flags |= (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_CLASS);
+	}
+
+	if (tb[NFTA_TUNNEL_KEY_GENEVE_TYPE]) {
+		tun->u.tun_geneve.type =
+			mnl_attr_get_u8(tb[NFTA_TUNNEL_KEY_GENEVE_TYPE]);
+		e->flags |= (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_TYPE);
+	}
+
+	if (tb[NFTA_TUNNEL_KEY_GENEVE_DATA]) {
+		uint32_t len = mnl_attr_get_payload_len(tb[NFTA_TUNNEL_KEY_GENEVE_DATA]);
+
+		memcpy(tun->u.tun_geneve.data,
+		       mnl_attr_get_payload(tb[NFTA_TUNNEL_KEY_GENEVE_DATA]),
+		       len);
+		tun->u.tun_geneve.data_len = len;
+
+		e->flags |= (1ULL << NFTNL_OBJ_TUNNEL_GENEVE_DATA);
+	}
+
+	return 0;
+}
+
  static int nftnl_obj_tunnel_vxlan_cb(const struct nlattr *attr, void *data)
  {
  	const struct nlattr **tb = data;
@@ -441,6 +537,7 @@ static int nftnl_obj_tunnel_opts_cb(const struct nlattr *attr, void *data)
  	switch (type) {
  	case NFTA_TUNNEL_KEY_OPTS_VXLAN:
  	case NFTA_TUNNEL_KEY_OPTS_ERSPAN:
+	case NFTA_TUNNEL_KEY_OPTS_GENEVE:
  		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
  			abi_breakage();
  		break;
@@ -466,6 +563,9 @@ nftnl_obj_tunnel_parse_opts(struct nftnl_obj *e, struct nlattr *attr,
  	} else if (tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN]) {
  		err = nftnl_obj_tunnel_parse_erspan(e, tb[NFTA_TUNNEL_KEY_OPTS_ERSPAN],
  						    tun);
+	} else if (tb[NFTA_TUNNEL_KEY_OPTS_GENEVE]) {
+		err = nftnl_obj_tunnel_parse_geneve(e, tb[NFTA_TUNNEL_KEY_OPTS_GENEVE],
+						    tun);
  	}
 
  	return err;
@@ -549,6 +649,9 @@ static struct attr_policy obj_tunnel_attr_policy[__NFTNL_OBJ_TUNNEL_MAX] = {
  	[NFTNL_OBJ_TUNNEL_ERSPAN_V1_INDEX] = { .maxlen = sizeof(uint32_t) },
  	[NFTNL_OBJ_TUNNEL_ERSPAN_V2_HWID] = { .maxlen = sizeof(uint8_t) },
  	[NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR] = { .maxlen = sizeof(uint8_t) },
+	[NFTNL_OBJ_TUNNEL_GENEVE_CLASS]	= { .maxlen = sizeof(uint16_t) },
+	[NFTNL_OBJ_TUNNEL_GENEVE_TYPE]	= { .maxlen = sizeof(uint8_t) },
+	[NFTNL_OBJ_TUNNEL_GENEVE_DATA]	= { .maxlen = NFTNL_TUNNEL_GENEVE_DATA_MAXLEN},
  };
 
  struct obj_ops obj_ops_tunnel = {