Skip to contentSkip to conversation

src: collapse set element commands from parser

PATCH In review Pablo Neira Ayuso 2 participants 3 messages Oct 23, 2024 – Oct 31, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: collapse set element commands from parser

498a5f0c219d ("rule: collapse set element commands") does not help to reduce memory consumption in the case of large sets defined by one element per line:

add element ip x y { 1.1.1.1 }
add element ip x y { 1.1.1.2 }
...

This patch collapses set element whenver possible to reduce the number of cmd objects, this reduces memory consumption by ~75%.

This patch also adds a special case for variables for sets similar to:

be055af5c58d ("cmd: skip variable set elements when collapsing commands")

This patch requires this small kernel fix:

commit b53c116642502b0c85ecef78bff4f826a7dd4145
Author: Pablo Neira Ayuso <[email protected]>
Date: Fri May 20 00:02:06 2022 +0200

netfilter: nf_tables: set element extended ACK reporting support

which is included in recent -stable kernels:

 # cat ruleset.nft
 add table ip x
 add chain ip x y
 add set ip x y { type ipv4_addr; }
 create element ip x y { 1.1.1.1 }
 create element ip x y { 1.1.1.1 }
 # nft -f ruleset.nft
 ruleset.nft:5:25-31: Error: Could not process rule: File exists
 create element ip x y { 1.1.1.1 }
                         ^^^^^^^

there is no need to relate commands via sequence number, this allows to remove the uncollapse step too.

Fixes 498a5f0c219d ("rule: collapse set element commands")

---
supersedes: https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

 include/cmd.h        |   7 +--
 include/expression.h |   1 -
 include/list.h       |  11 +++++
 include/rule.h       |   1 -
 src/cmd.c            | 105 +++++++++++--------------------------------
 src/libnftables.c    |   7 ---
 src/parser_bison.y   |  13 ++++++
 src/rule.c           |   1 -
 8 files changed, 54 insertions(+), 92 deletions(-)
include/cmd.h +4 -3
index 92a4152bbaea..0a8779b1ea19 100644
@@ -2,12 +2,13 @@
 #define _NFT_CMD_H_
 
 void cmd_add_loc(struct cmd *cmd, uint16_t offset, const struct location *loc);
+struct mnl_err;
 void nft_cmd_error(struct netlink_ctx *ctx, struct cmd *cmd,
 		   struct mnl_err *err);
 
+bool nft_cmd_collapse_elems(enum cmd_ops op, struct list_head *cmds,
+			    struct handle *handle, struct expr *init);
+
 void nft_cmd_expand(struct cmd *cmd);
-void nft_cmd_post_expand(struct cmd *cmd);
-bool nft_cmd_collapse(struct list_head *cmds);
-void nft_cmd_uncollapse(struct list_head *cmds);
 
 #endif
include/expression.h +0 -1
index 8982110cce95..da2f693e72d3 100644
@@ -255,7 +255,6 @@ struct expr {
 	enum expr_types		etype:8;
 	enum ops		op:8;
 	unsigned int		len;
-	struct cmd		*cmd;
 
 	union {
 		struct {
include/list.h +11 -0
index 857921e34201..37fbe3e275cc 100644
@@ -348,6 +348,17 @@ static inline void list_splice_tail_init(struct list_head *list,
 #define list_first_entry(ptr, type, member) \
 	list_entry((ptr)->next, type, member)
 
+/**
+ * list_last_entry - get the last element from a list
+ * @ptr:        the list head to take the element from.
+ * @type:       the type of the struct this is embedded in.
+ * @member:     the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_last_entry(ptr, type, member) \
+	list_entry((ptr)->prev, type, member)
+
 /**
  * list_next_entry - get the next element in list
  * @pos: the type * to cursor
include/rule.h +0 -1
index 5b3e12b5d7dc..a1628d82d275 100644
@@ -718,7 +718,6 @@ struct cmd {
 	enum cmd_obj		obj;
 	struct handle		handle;
 	uint32_t		seqnum;
-	struct list_head	collapse_list;
 	union {
 		void		*data;
 		struct expr	*expr;
src/cmd.c +26 -79

114 lines not on this page · raw message

src/libnftables.c +0 -7
index 2ae215013cb0..2834c9922486 100644
@@ -513,7 +513,6 @@ static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,
 {
 	struct nft_cache_filter *filter;
 	struct cmd *cmd, *next;
-	bool collapsed = false;
 	unsigned int flags;
 	int err = 0;
 
@@ -529,9 +528,6 @@ static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,
 
 	nft_cache_filter_fini(filter);
 
-	if (nft_cmd_collapse(cmds))
-		collapsed = true;
-
 	list_for_each_entry(cmd, cmds, list) {
 		if (cmd->op != CMD_ADD &&
 		    cmd->op != CMD_CREATE)
@@ -553,9 +549,6 @@ static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,
 		}
 	}
 
-	if (collapsed)
-		nft_cmd_uncollapse(cmds);
-
 	if (err < 0 || nft->state->nerrs)
 		return -1;
 
src/parser_bison.y +13 -0
index e2936d10efe4..602fc60e6de3 100644
@@ -35,6 +35,7 @@
 #include <libnftnl/udata.h>
 
 #include <rule.h>
+#include <cmd.h>
 #include <statement.h>
 #include <expression.h>
 #include <headers.h>
@@ -1219,6 +1220,12 @@ add_cmd			:	TABLE		table_spec
 			}
 			|	ELEMENT		set_spec	set_block_expr
 			{
+				if (nft_cmd_collapse_elems(CMD_ADD, state->cmds, &$2, $3)) {
+					handle_free(&$2);
+					expr_free($3);
+					$$ = NULL;
+					break;
+				}
 				$$ = cmd_alloc(CMD_ADD, CMD_OBJ_ELEMENTS, &$2, &@$, $3);
 			}
 			|	FLOWTABLE	flowtable_spec	flowtable_block_alloc
@@ -1336,6 +1343,12 @@ create_cmd		:	TABLE		table_spec
 			}
 			|	ELEMENT		set_spec	set_block_expr
 			{
+				if (nft_cmd_collapse_elems(CMD_CREATE, state->cmds, &$2, $3)) {
+					handle_free(&$2);
+					expr_free($3);
+					$$ = NULL;
+					break;
+				}
 				$$ = cmd_alloc(CMD_CREATE, CMD_OBJ_ELEMENTS, &$2, &@$, $3);
 			}
 			|	FLOWTABLE	flowtable_spec	flowtable_block_alloc
src/rule.c +0 -1
index 9bc160ec0d88..9536e68c7524 100644
@@ -1332,7 +1332,6 @@ struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj,
 	cmd->attr     = xzalloc_array(NFT_NLATTR_LOC_MAX,
 				      sizeof(struct nlerr_loc));
 	cmd->attr_array_len = NFT_NLATTR_LOC_MAX;
-	init_list_head(&cmd->collapse_list);
 
 	return cmd;
 }
Signature · 1 line
2.30.2
#1 lore raw
Quoted 37 lines · Pablo Neira Ayuso

498a5f0c219d ("rule: collapse set element commands") does not help to reduce memory consumption in the case of large sets defined by one element per line:

add element ip x y { 1.1.1.1 }
add element ip x y { 1.1.1.2 }
...

This patch collapses set element whenver possible to reduce the number of cmd objects, this reduces memory consumption by ~75%.

This patch also adds a special case for variables for sets similar to:

be055af5c58d ("cmd: skip variable set elements when collapsing commands")

This patch requires this small kernel fix:

commit b53c116642502b0c85ecef78bff4f826a7dd4145
Author: Pablo Neira Ayuso <[email protected]>
Date: Fri May 20 00:02:06 2022 +0200

netfilter: nf_tables: set element extended ACK reporting support

which is included in recent -stable kernels:

 # cat ruleset.nft
 add table ip x
 add chain ip x y
 add set ip x y { type ipv4_addr; }
 create element ip x y { 1.1.1.1 }
 create element ip x y { 1.1.1.1 }
 # nft -f ruleset.nft
 ruleset.nft:5:25-31: Error: Could not process rule: File exists
 create element ip x y { 1.1.1.1 }
                         ^^^^^^^
Quoted 5 lines

there is no need to relate commands via sequence number, this allows to remove the uncollapse step too.

Fixes 498a5f0c219d ("rule: collapse set element commands")

Hi Pablo,

This patch appears to introduce a performance regression for set entries in the JSON interface. AFAICS, the collapse code is only called from the CLI parser now.

E.

author maintainer
#2 lore raw

Hi Eric,

Quoted 41 lines · Eric Garver
Quoted 40 lines · Pablo Neira Ayuso

498a5f0c219d ("rule: collapse set element commands") does not help to reduce memory consumption in the case of large sets defined by one element per line:

add element ip x y { 1.1.1.1 }
add element ip x y { 1.1.1.2 }
...

This patch collapses set element whenver possible to reduce the number of cmd objects, this reduces memory consumption by ~75%.

This patch also adds a special case for variables for sets similar to:

be055af5c58d ("cmd: skip variable set elements when collapsing commands")

This patch requires this small kernel fix:

commit b53c116642502b0c85ecef78bff4f826a7dd4145
Author: Pablo Neira Ayuso <[email protected]>
Date: Fri May 20 00:02:06 2022 +0200

netfilter: nf_tables: set element extended ACK reporting support

which is included in recent -stable kernels:

 # cat ruleset.nft
 add table ip x
 add chain ip x y
 add set ip x y { type ipv4_addr; }
 create element ip x y { 1.1.1.1 }
 create element ip x y { 1.1.1.1 }
 # nft -f ruleset.nft
 ruleset.nft:5:25-31: Error: Could not process rule: File exists
 create element ip x y { 1.1.1.1 }
                         ^^^^^^^

there is no need to relate commands via sequence number, this allows to remove the uncollapse step too.

Quoted 8 lines
Quoted 2 lines
Fixes 498a5f0c219d ("rule: collapse set element commands")

Hi Pablo,

This patch appears to introduce a performance regression for set entries in the JSON interface. AFAICS, the collapse code is only called from the CLI parser now.

Indeed, I am working on a fix for the JSON parser.

Thanks for early reporting this issue!