Blame SOURCES/0076-mnl-do-not-build-nftnl_set-element-list.patch

252916
From bd940a4efd2b5897f8a8e58ec7733417b3710e1e Mon Sep 17 00:00:00 2001
252916
From: Phil Sutter <psutter@redhat.com>
252916
Date: Wed, 8 Dec 2021 13:28:49 +0100
252916
Subject: [PATCH] mnl: do not build nftnl_set element list
252916
252916
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2047821
252916
Upstream Status: nftables commit b4b234f5a29e8
252916
Conflicts: Context change due to missing commit 66746e7dedeb0
252916
	   ("src: support for nat with interval concatenation").
252916
252916
commit b4b234f5a29e819045679acd95820a7457d4d7de
252916
Author: Pablo Neira Ayuso <pablo@netfilter.org>
252916
Date:   Thu Nov 4 12:53:11 2021 +0100
252916
252916
    mnl: do not build nftnl_set element list
252916
252916
    Do not call alloc_setelem_cache() to build the set element list in
252916
    nftnl_set. Instead, translate one single set element expression to
252916
    nftnl_set_elem object at a time and use this object to build the netlink
252916
    header.
252916
252916
    Using a huge test set containing 1.1 million element blocklist, this
252916
    patch is reducing userspace memory consumption by 40%.
252916
252916
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
252916
---
252916
 include/netlink.h |   2 +
252916
 src/mnl.c         | 112 ++++++++++++++++++++++++++++++++++++----------
252916
 src/netlink.c     |   4 +-
252916
 3 files changed, 93 insertions(+), 25 deletions(-)
252916
252916
diff --git a/include/netlink.h b/include/netlink.h
252916
index 059092e..3443582 100644
252916
--- a/include/netlink.h
252916
+++ b/include/netlink.h
252916
@@ -56,6 +56,8 @@ struct netlink_ctx {
252916
 
252916
 extern struct nftnl_expr *alloc_nft_expr(const char *name);
252916
 extern void alloc_setelem_cache(const struct expr *set, struct nftnl_set *nls);
252916
+struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
252916
+					   const struct expr *expr);
252916
 
252916
 extern struct nftnl_table *netlink_table_alloc(const struct nlmsghdr *nlh);
252916
 extern struct nftnl_chain *netlink_chain_alloc(const struct nlmsghdr *nlh);
252916
diff --git a/src/mnl.c b/src/mnl.c
252916
index 23341e6..44cf1a4 100644
252916
--- a/src/mnl.c
252916
+++ b/src/mnl.c
252916
@@ -1201,33 +1201,102 @@ static int set_elem_cb(const struct nlmsghdr *nlh, void *data)
252916
 	return MNL_CB_OK;
252916
 }
252916
 
252916
-static int mnl_nft_setelem_batch(struct nftnl_set *nls,
252916
+static bool mnl_nft_attr_nest_overflow(struct nlmsghdr *nlh,
252916
+				       const struct nlattr *from,
252916
+				       const struct nlattr *to)
252916
+{
252916
+	int len = (void *)to + to->nla_len - (void *)from;
252916
+
252916
+	/* The attribute length field is 16 bits long, thus the maximum payload
252916
+	 * that an attribute can convey is UINT16_MAX. In case of overflow,
252916
+	 * discard the last attribute that did not fit into the nest.
252916
+	 */
252916
+	if (len > UINT16_MAX) {
252916
+		nlh->nlmsg_len -= to->nla_len;
252916
+		return true;
252916
+	}
252916
+	return false;
252916
+}
252916
+
252916
+static void netlink_dump_setelem(const struct nftnl_set_elem *nlse,
252916
+				 struct netlink_ctx *ctx)
252916
+{
252916
+	FILE *fp = ctx->nft->output.output_fp;
252916
+	char buf[4096];
252916
+
252916
+	if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
252916
+		return;
252916
+
252916
+	nftnl_set_elem_snprintf(buf, sizeof(buf), nlse, NFTNL_OUTPUT_DEFAULT, 0);
252916
+	fprintf(fp, "\t%s", buf);
252916
+}
252916
+
252916
+static void netlink_dump_setelem_done(struct netlink_ctx *ctx)
252916
+{
252916
+	FILE *fp = ctx->nft->output.output_fp;
252916
+
252916
+	if (!(ctx->nft->debug_mask & NFT_DEBUG_NETLINK) || !fp)
252916
+		return;
252916
+
252916
+	fprintf(fp, "\n");
252916
+}
252916
+
252916
+static int mnl_nft_setelem_batch(const struct nftnl_set *nls,
252916
 				 struct nftnl_batch *batch,
252916
 				 enum nf_tables_msg_types cmd,
252916
-				 unsigned int flags, uint32_t seqnum)
252916
+				 unsigned int flags, uint32_t seqnum,
252916
+				 const struct expr *set,
252916
+				 struct netlink_ctx *ctx)
252916
 {
252916
+	struct nlattr *nest1, *nest2;
252916
+	struct nftnl_set_elem *nlse;
252916
 	struct nlmsghdr *nlh;
252916
-	struct nftnl_set_elems_iter *iter;
252916
-	int ret;
252916
-
252916
-	iter = nftnl_set_elems_iter_create(nls);
252916
-	if (iter == NULL)
252916
-		memory_allocation_error();
252916
+	struct expr *expr = NULL;
252916
+	int i = 0;
252916
 
252916
 	if (cmd == NFT_MSG_NEWSETELEM)
252916
 		flags |= NLM_F_CREATE;
252916
 
252916
-	while (nftnl_set_elems_iter_cur(iter)) {
252916
-		nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(batch), cmd,
252916
-					    nftnl_set_get_u32(nls, NFTNL_SET_FAMILY),
252916
-					    flags, seqnum);
252916
-		ret = nftnl_set_elems_nlmsg_build_payload_iter(nlh, iter);
252916
-		mnl_nft_batch_continue(batch);
252916
-		if (ret <= 0)
252916
-			break;
252916
+	if (set)
252916
+		expr = list_first_entry(&set->expressions, struct expr, list);
252916
+
252916
+next:
252916
+	nlh = nftnl_nlmsg_build_hdr(nftnl_batch_buffer(batch), cmd,
252916
+				    nftnl_set_get_u32(nls, NFTNL_SET_FAMILY),
252916
+				    flags, seqnum);
252916
+
252916
+	if (nftnl_set_is_set(nls, NFTNL_SET_TABLE)) {
252916
+                mnl_attr_put_strz(nlh, NFTA_SET_ELEM_LIST_TABLE,
252916
+				  nftnl_set_get_str(nls, NFTNL_SET_TABLE));
252916
+	}
252916
+	if (nftnl_set_is_set(nls, NFTNL_SET_NAME)) {
252916
+		mnl_attr_put_strz(nlh, NFTA_SET_ELEM_LIST_SET,
252916
+				  nftnl_set_get_str(nls, NFTNL_SET_NAME));
252916
 	}
252916
+	if (nftnl_set_is_set(nls, NFTNL_SET_ID)) {
252916
+		mnl_attr_put_u32(nlh, NFTA_SET_ELEM_LIST_SET_ID,
252916
+				 htonl(nftnl_set_get_u32(nls, NFTNL_SET_ID)));
252916
+	}
252916
+
252916
+	if (!set || list_empty(&set->expressions))
252916
+		return 0;
252916
 
252916
-	nftnl_set_elems_iter_destroy(iter);
252916
+	assert(expr);
252916
+	nest1 = mnl_attr_nest_start(nlh, NFTA_SET_ELEM_LIST_ELEMENTS);
252916
+	list_for_each_entry_from(expr, &set->expressions, list) {
252916
+		nlse = alloc_nftnl_setelem(set, expr);
252916
+		nest2 = nftnl_set_elem_nlmsg_build(nlh, nlse, ++i);
252916
+		netlink_dump_setelem(nlse, ctx);
252916
+		nftnl_set_elem_free(nlse);
252916
+		if (mnl_nft_attr_nest_overflow(nlh, nest1, nest2)) {
252916
+			mnl_attr_nest_end(nlh, nest1);
252916
+			mnl_nft_batch_continue(batch);
252916
+			goto next;
252916
+		}
252916
+	}
252916
+	mnl_attr_nest_end(nlh, nest1);
252916
+	mnl_nft_batch_continue(batch);
252916
+	netlink_dump_setelem_done(ctx);
252916
 
252916
 	return 0;
252916
 }
252916
@@ -1249,11 +1318,10 @@ int mnl_nft_setelem_add(struct netlink_ctx *ctx, const struct set *set,
252916
 	if (h->set_id)
252916
 		nftnl_set_set_u32(nls, NFTNL_SET_ID, h->set_id);
252916
 
252916
-	alloc_setelem_cache(expr, nls);
252916
 	netlink_dump_set(nls, ctx);
252916
 
252916
-	err = mnl_nft_setelem_batch(nls, ctx->batch, NFT_MSG_NEWSETELEM, flags,
252916
-				    ctx->seqnum);
252916
+	err = mnl_nft_setelem_batch(nls, ctx->batch, NFT_MSG_NEWSETELEM,
252916
+				    flags, ctx->seqnum, expr, ctx);
252916
 	nftnl_set_free(nls);
252916
 
252916
 	return err;
252916
@@ -1306,12 +1374,10 @@ int mnl_nft_setelem_del(struct netlink_ctx *ctx, const struct cmd *cmd)
252916
 	else if (h->handle.id)
252916
 		nftnl_set_set_u64(nls, NFTNL_SET_HANDLE, h->handle.id);
252916
 
252916
-	if (cmd->expr)
252916
-		alloc_setelem_cache(cmd->expr, nls);
252916
 	netlink_dump_set(nls, ctx);
252916
 
252916
 	err = mnl_nft_setelem_batch(nls, ctx->batch, NFT_MSG_DELSETELEM, 0,
252916
-				    ctx->seqnum);
252916
+				    ctx->seqnum, cmd->expr, ctx);
252916
 	nftnl_set_free(nls);
252916
 
252916
 	return err;
252916
diff --git a/src/netlink.c b/src/netlink.c
252916
index 825c2cc..f8c97d0 100644
252916
--- a/src/netlink.c
252916
+++ b/src/netlink.c
252916
@@ -95,8 +95,8 @@ struct nftnl_expr *alloc_nft_expr(const char *name)
252916
 	return nle;
252916
 }
252916
 
252916
-static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
252916
-						  const struct expr *expr)
252916
+struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
252916
+					   const struct expr *expr)
252916
 {
252916
 	const struct expr *elem, *data;
252916
 	struct nftnl_set_elem *nlse;
252916
-- 
252916
2.31.1
252916