Skip to contentSkip to conversation

src: add dynamic register allocation infrastructure

PATCH Superseded Pablo Neira Ayuso 2 participants 4 messages May 2, 2022 – May 15, 2022 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: add dynamic register allocation infrastructure

Starting Linux kernel 5.18-rc, operations on registers that already contain the expected data are turned into noop.

Track operation on registers to use the same register through nftnl_reg_get(). This patch introduces an LRU eviction strategy when all the registers are in used.

nftnl_reg_get_scratch() is used to allocate a register as scratchpad
area: no tracking is performed in this case, although register eviction might occur.

---
 include/expr_ops.h           |   6 +
 include/internal.h           |   1 +
 include/libnftnl/Makefile.am |   1 +
 include/regs.h               |  32 ++++++
 src/Makefile.am              |   1 +
 src/expr/meta.c              |  44 +++++++
 src/expr/payload.c           |  31 +++++
 src/libnftnl.map             |   7 ++
 src/regs.c                   | 216 +++++++++++++++++++++++++++++++++++
 9 files changed, 339 insertions(+)
 create mode 100644 include/regs.h
 create mode 100644 src/regs.c
include/expr_ops.h +6 -0
index 7a6aa23f9bd1..01f6fefd6f3a 100644
@@ -7,6 +7,7 @@
 struct nlattr;
 struct nlmsghdr;
 struct nftnl_expr;
+struct nftnl_reg;
 
 struct expr_ops {
 	const char *name;
@@ -19,6 +20,11 @@ struct expr_ops {
 	int 	(*parse)(struct nftnl_expr *e, struct nlattr *attr);
 	void	(*build)(struct nlmsghdr *nlh, const struct nftnl_expr *e);
 	int	(*snprintf)(char *buf, size_t len, uint32_t flags, const struct nftnl_expr *e);
+	struct {
+		int	(*len)(const struct nftnl_expr *e);
+		bool	(*cmp)(const struct nftnl_reg *reg, const struct nftnl_expr *e);
+		void	(*update)(struct nftnl_reg *reg, const struct nftnl_expr *e);
+	} reg;
 };
 
 struct expr_ops *nftnl_expr_ops_lookup(const char *name);
include/internal.h +1 -0
index 1f96731589c0..9f88828f9039 100644
@@ -12,5 +12,6 @@
 #include "expr.h"
 #include "expr_ops.h"
 #include "rule.h"
+#include "regs.h"
 
 #endif /* _LIBNFTNL_INTERNAL_H_ */
include/libnftnl/Makefile.am +1 -0
index d846a574f438..186f758ab97e 100644
@@ -3,6 +3,7 @@ pkginclude_HEADERS = batch.h		\
 		     trace.h		\
 		     chain.h		\
 		     object.h		\
+		     regs.h		\
 		     rule.h		\
 		     expr.h		\
 		     set.h		\
include/regs.h new file +32 -0
new file mode 100644
index 000000000000..5312f607f692
@@ -0,0 +1,32 @@
+#ifndef _LIBNFTNL_REGS_INTERNAL_H_
+#define _LIBNFTNL_REGS_INTERNAL_H_
+
+enum nftnl_expr_type {
+	NFT_EXPR_UNSPEC	= 0,
+	NFT_EXPR_PAYLOAD,
+	NFT_EXPR_META,
+};
+
+struct nftnl_reg {
+	enum nftnl_expr_type				type;
+	uint32_t					len;
+	uint64_t					genid;
+	uint8_t						word;
+	union {
+		struct {
+			enum nft_meta_keys		key;
+		} meta;
+		struct {
+			enum nft_payload_bases		base;
+			uint32_t			offset;
+		} payload;
+	};
+};
+
+struct nftnl_regs {
+	uint32_t		num_regs;
+	struct nftnl_reg	*reg;
+	uint64_t		genid;
+};
+
+#endif
src/Makefile.am +1 -0
index c3b0ab974bd2..2a26d24ce3e3 100644
@@ -14,6 +14,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      trace.c		\
 		      chain.c		\
 		      object.c		\
+		      regs.c		\
 		      rule.c		\
 		      set.c		\
 		      set_elem.c	\
src/expr/meta.c +44 -0

60 lines not on this page · raw message

src/expr/payload.c +31 -0
index 82747ec8994f..8b41a9d06a26 100644
@@ -203,6 +203,32 @@ nftnl_expr_payload_parse(struct nftnl_expr *e, struct nlattr *attr)
 	return 0;
 }
 
+static int nftnl_payload_reg_len(const struct nftnl_expr *expr)
+{
+	const struct nftnl_expr_payload *payload = nftnl_expr_data(expr);
+
+	return payload->len;
+}
+
+static bool nftnl_payload_reg_cmp(const struct nftnl_reg *reg,
+				  const struct nftnl_expr *e)
+{
+	const struct nftnl_expr_payload *payload = nftnl_expr_data(e);
+
+	return reg->payload.base == payload->base &&
+	       reg->payload.offset == payload->offset &&
+	       reg->len >= payload->len;
+}
+
+static void nftnl_payload_reg_update(struct nftnl_reg *reg,
+				     const struct nftnl_expr *e)
+{
+	const struct nftnl_expr_payload *payload = nftnl_expr_data(e);
+
+	reg->payload.base = payload->base;
+	reg->payload.offset = payload->offset;
+}
+
 static const char *base2str_array[NFT_PAYLOAD_INNER_HEADER + 1] = {
 	[NFT_PAYLOAD_LL_HEADER]		= "link",
 	[NFT_PAYLOAD_NETWORK_HEADER] 	= "network",
@@ -260,4 +286,9 @@ struct expr_ops expr_ops_payload = {
 	.parse		= nftnl_expr_payload_parse,
 	.build		= nftnl_expr_payload_build,
 	.snprintf	= nftnl_expr_payload_snprintf,
+	.reg		= {
+		.len	= nftnl_payload_reg_len,
+		.cmp	= nftnl_payload_reg_cmp,
+		.update	= nftnl_payload_reg_update,
+	},
 };
src/libnftnl.map +7 -0
index ad8f2af060ae..3a85325216aa 100644
@@ -387,3 +387,10 @@ LIBNFTNL_16 {
 LIBNFTNL_17 {
   nftnl_set_elem_nlmsg_build;
 } LIBNFTNL_16;
+
+LIBNFTNL_18 {
+  nftnl_regs_alloc;
+  nftnl_regs_free;
+  nftnl_reg_get;
+  nftnl_reg_get_scratch;
+} LIBNFTNL_17;
src/regs.c new file +216 -0

216 lines not on this page · raw message

Signature · 1 line
2.30.2
reviewer
#1 lore raw

Hi Pablo,

Quoted 12 lines · Pablo Neira Ayuso

Starting Linux kernel 5.18-rc, operations on registers that already contain the expected data are turned into noop.

Track operation on registers to use the same register through nftnl_reg_get(). This patch introduces an LRU eviction strategy when all the registers are in used.

nftnl_reg_get_scratch() is used to allocate a register as scratchpad
area: no tracking is performed in this case, although register eviction might occur.

Quoted 13 lines
---
 include/expr_ops.h           |   6 +
 include/internal.h           |   1 +
 include/libnftnl/Makefile.am |   1 +
 include/regs.h               |  32 ++++++
 src/Makefile.am              |   1 +
 src/expr/meta.c              |  44 +++++++
 src/expr/payload.c           |  31 +++++
 src/libnftnl.map             |   7 ++
 src/regs.c                   | 216 +++++++++++++++++++++++++++++++++++
 9 files changed, 339 insertions(+)
 create mode 100644 include/regs.h
 create mode 100644 src/regs.c

Did you forget to add include/libnftnl/regs.h to this patch? It is referenced from src/regs.c and build fails.

Cheers, Phil

author maintainer
#2 lore raw
Quoted 29 lines · Phil Sutter

Hi Pablo,

Quoted 12 lines · Pablo Neira Ayuso

Starting Linux kernel 5.18-rc, operations on registers that already contain the expected data are turned into noop.

Track operation on registers to use the same register through nftnl_reg_get(). This patch introduces an LRU eviction strategy when all the registers are in used.

nftnl_reg_get_scratch() is used to allocate a register as scratchpad
area: no tracking is performed in this case, although register eviction might occur.

Quoted 13 lines
---
 include/expr_ops.h           |   6 +
 include/internal.h           |   1 +
 include/libnftnl/Makefile.am |   1 +
 include/regs.h               |  32 ++++++
 src/Makefile.am              |   1 +
 src/expr/meta.c              |  44 +++++++
 src/expr/payload.c           |  31 +++++
 src/libnftnl.map             |   7 ++
 src/regs.c                   | 216 +++++++++++++++++++++++++++++++++++
 9 files changed, 339 insertions(+)
 create mode 100644 include/regs.h
 create mode 100644 src/regs.c
Quoted 2 lines

Did you forget to add include/libnftnl/regs.h to this patch? It is referenced from src/regs.c and build fails.

Yes, this is fixed here:

http://git.netfilter.org/libnftnl/commit/?id=e549f5b3239c19f78af2f7c7a582fe5616403ca8

reviewer
#3 lore raw
Quoted 28 lines · Pablo Neira Ayuso
Quoted 27 lines · Phil Sutter
Quoted 25 lines · Pablo Neira Ayuso

Starting Linux kernel 5.18-rc, operations on registers that already contain the expected data are turned into noop.

Track operation on registers to use the same register through nftnl_reg_get(). This patch introduces an LRU eviction strategy when all the registers are in used.

nftnl_reg_get_scratch() is used to allocate a register as scratchpad
area: no tracking is performed in this case, although register eviction might occur.

---
 include/expr_ops.h           |   6 +
 include/internal.h           |   1 +
 include/libnftnl/Makefile.am |   1 +
 include/regs.h               |  32 ++++++
 src/Makefile.am              |   1 +
 src/expr/meta.c              |  44 +++++++
 src/expr/payload.c           |  31 +++++
 src/libnftnl.map             |   7 ++
 src/regs.c                   | 216 +++++++++++++++++++++++++++++++++++
 9 files changed, 339 insertions(+)
 create mode 100644 include/regs.h
 create mode 100644 src/regs.c
Quoted 6 lines
Quoted 2 lines

Did you forget to add include/libnftnl/regs.h to this patch? It is referenced from src/regs.c and build fails.

Yes, this is fixed here:

http://git.netfilter.org/libnftnl/commit/?id=e549f5b3239c19f78af2f7c7a582fe5616403ca8

Thanks for the quick fix!

Cheers, Phil