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

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