Skip to contentSkip to conversation

nft: support for dynamic register allocation

PATCH New Pablo Neira Ayuso 1 participant 1 message May 18, 2022 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 nft: support for dynamic register allocation

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

Use libnftnl dynamic register allocation infrastructure to select the registers to be used for payload and meta expressions.

---
Patch for tests/py still missing, it will be large, coming after this.

 include/netlink.h       |   5 +-
 src/libnftables.c       |  15 ++
 src/mnl.c               |   4 +-
 src/netlink_linearize.c | 327 ++++++++++++++++++----------------------
 4 files changed, 171 insertions(+), 180 deletions(-)
include/netlink.h +4 -1
index e8e0f68ae1a4..c88f34944a1c 100644
@@ -69,6 +69,7 @@ struct netlink_ctx {
 	const void		*data;
 	uint32_t		seqnum;
 	struct nftnl_batch	*batch;
+	struct nftnl_regs	*regs;
 };
 
 extern struct nftnl_expr *alloc_nft_expr(const char *name);
@@ -218,12 +219,14 @@ int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
 enum nft_data_types dtype_map_to_kernel(const struct datatype *dtype);
 
 void netlink_linearize_init(struct netlink_linearize_ctx *lctx,
+			    struct nftnl_regs *regs,
 			    struct nftnl_rule *nlr);
 void netlink_linearize_fini(struct netlink_linearize_ctx *lctx);
 
 struct netlink_linearize_ctx {
 	struct nftnl_rule	*nlr;
-	unsigned int		reg_low;
+	struct nftnl_regs	*regs;
+	bool			concat;
 	struct list_head	*expr_loc_htable;
 };
 
src/libnftables.c +15 -0
index 6a22ea093952..17636dbe4f68 100644
@@ -16,6 +16,18 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <libnftnl/regs.h>
+
+static struct nftnl_regs *nft_regs_alloc(void)
+{
+	struct nftnl_regs *regs;
+
+	regs = nftnl_regs_alloc(16);
+	if (!regs)
+		memory_allocation_error();
+
+	return regs;
+}
 
 static int nft_netlink(struct nft_ctx *nft,
 		       struct list_head *cmds, struct list_head *msgs,
@@ -27,6 +39,7 @@ static int nft_netlink(struct nft_ctx *nft,
 		.msgs = msgs,
 		.list = LIST_HEAD_INIT(ctx.list),
 		.batch = mnl_batch_init(),
+		.regs = nft_regs_alloc(),
 	};
 	struct cmd *cmd;
 	struct mnl_err *err, *tmp;
@@ -97,6 +110,8 @@ static int nft_netlink(struct nft_ctx *nft,
 		mnl_err_list_free(err);
 out:
 	mnl_batch_reset(ctx.batch);
+	nftnl_regs_free(ctx.regs);
+
 	return ret;
 }
 
src/mnl.c +2 -2
index 7dd77be1bec0..c6799e535b8b 100644
@@ -510,7 +510,7 @@ int mnl_nft_rule_add(struct netlink_ctx *ctx, struct cmd *cmd,
 	if (h->position_id)
 		nftnl_rule_set_u32(nlr, NFTNL_RULE_POSITION_ID, h->position_id);
 
-	netlink_linearize_init(&lctx, nlr);
+	netlink_linearize_init(&lctx, ctx->regs, nlr);
 	netlink_linearize_rule(ctx, rule, &lctx);
 	nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),
 				    NFT_MSG_NEWRULE,
@@ -561,7 +561,7 @@ int mnl_nft_rule_replace(struct netlink_ctx *ctx, struct cmd *cmd)
 
 	nftnl_rule_set_u32(nlr, NFTNL_RULE_FAMILY, h->family);
 
-	netlink_linearize_init(&lctx, nlr);
+	netlink_linearize_init(&lctx, ctx->regs, nlr);
 	netlink_linearize_rule(ctx, rule, &lctx);
 	nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(ctx->batch),
 				    NFT_MSG_NEWRULE,
src/netlink_linearize.c +150 -177

747 lines not on this page · raw message

Signature · 1 line
2.30.2