Skip to contentSkip to conversation

cache: move list filter under struct

PATCH New Pablo Neira Ayuso 1 participant 3 messages Nov 9, 2021 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v4 1/3 cache: move list filter under struct

Wrap the table and set fields for list filtering to prepare for the introduction element filters.

---
v4: no changes.

 include/cache.h |  6 ++++--
 src/cache.c     | 22 +++++++++++-----------
 2 files changed, 15 insertions(+), 13 deletions(-)
include/cache.h +4 -2
index 0523358889de..7d61701a02b5 100644
@@ -39,8 +39,10 @@ enum cache_level_flags {
 };
 
 struct nft_cache_filter {
-	const char		*table;
-	const char		*set;
+	struct {
+		const char	*table;
+		const char	*set;
+	} list;
 };
 
 struct nft_cache;
src/cache.c +11 -11
index 0cddd1e1cb48..58397551aafc 100644
@@ -134,19 +134,19 @@ static unsigned int evaluate_cache_list(struct nft_ctx *nft, struct cmd *cmd,
 	switch (cmd->obj) {
 	case CMD_OBJ_TABLE:
 		if (filter && cmd->handle.table.name)
-			filter->table = cmd->handle.table.name;
+			filter->list.table = cmd->handle.table.name;
 
 		flags |= NFT_CACHE_FULL;
 		break;
 	case CMD_OBJ_SET:
 	case CMD_OBJ_MAP:
 		if (filter && cmd->handle.table.name && cmd->handle.set.name) {
-			filter->table = cmd->handle.table.name;
-			filter->set = cmd->handle.set.name;
+			filter->list.table = cmd->handle.table.name;
+			filter->list.set = cmd->handle.set.name;
 		}
 		if (nft_output_terse(&nft->output))
 			flags |= (NFT_CACHE_FULL & ~NFT_CACHE_SETELEM_BIT);
-		else if (filter->table && filter->set)
+		else if (filter->list.table && filter->list.set)
 			flags |= NFT_CACHE_TABLE | NFT_CACHE_SET | NFT_CACHE_SETELEM;
 		else
 			flags |= NFT_CACHE_FULL;
@@ -183,8 +183,8 @@ unsigned int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,
 	struct cmd *cmd;
 
 	list_for_each_entry(cmd, cmds, list) {
-		if (filter->table && cmd->op != CMD_LIST)
-			memset(filter, 0, sizeof(*filter));
+		if (filter->list.table && cmd->op != CMD_LIST)
+			memset(&filter->list, 0, sizeof(filter->list));
 
 		switch (cmd->op) {
 		case CMD_ADD:
@@ -377,9 +377,9 @@ static int set_cache_cb(struct nftnl_set *nls, void *arg)
 	if (!set)
 		return -1;
 
-	if (ctx->filter && ctx->filter->set &&
-	    (strcmp(ctx->filter->table, set->handle.table.name) ||
-	     strcmp(ctx->filter->set, set->handle.set.name))) {
+	if (ctx->filter && ctx->filter->list.set &&
+	    (strcmp(ctx->filter->list.table, set->handle.table.name) ||
+	     strcmp(ctx->filter->list.set, set->handle.set.name))) {
 		set_free(set);
 		return 0;
 	}
@@ -637,8 +637,8 @@ static int cache_init_tables(struct netlink_ctx *ctx, struct handle *h,
 	list_for_each_entry_safe(table, next, &ctx->list, list) {
 		list_del(&table->list);
 
-		if (filter && filter->table &&
-		    (strcmp(filter->table, table->handle.table.name))) {
+		if (filter && filter->list.table &&
+		    (strcmp(filter->list.table, table->handle.table.name))) {
 			table_free(table);
 			continue;
 		}
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH v4 2/3 cache: do not populate cache if it is going to be flushed

Skip set element netlink dump if set is flushed, this speeds up set flush + add element operation in a batch file for an existing set.

---
v4: add family.

 include/cache.h   | 16 ++++++++++-
 src/cache.c       | 71 +++++++++++++++++++++++++++++++++++++++++++++--
 src/libnftables.c | 11 ++++++--
 3 files changed, 92 insertions(+), 6 deletions(-)
include/cache.h +15 -1
index 7d61701a02b5..cdf1f4fbf6f7 100644
@@ -38,11 +38,24 @@ enum cache_level_flags {
 	NFT_CACHE_FLUSHED	= (1 << 31),
 };
 
+struct nft_filter_obj {
+	struct list_head list;
+	uint32_t	family;
+	const char	*table;
+	const char	*set;
+};
+
+#define NFT_CACHE_HSIZE	8192
+
 struct nft_cache_filter {
 	struct {
 		const char	*table;
 		const char	*set;
 	} list;
+
+	struct {
+		struct list_head head;
+	} obj[NFT_CACHE_HSIZE];
 };
 
 struct nft_cache;
@@ -66,7 +79,8 @@ static inline uint32_t djb_hash(const char *key)
 	return hash;
 }
 
-#define NFT_CACHE_HSIZE 8192
+struct nft_cache_filter *nft_cache_filter_init(void);
+void nft_cache_filter_fini(struct nft_cache_filter *filter);
 
 struct table;
 struct chain;
src/cache.c +69 -2

101 lines not on this page · raw message

src/libnftables.c +8 -3
index 2b2ed1a44329..7b9d7efaeaae 100644
@@ -459,13 +459,18 @@ static int nft_parse_bison_filename(struct nft_ctx *nft, const char *filename,
 static int nft_evaluate(struct nft_ctx *nft, struct list_head *msgs,
 			struct list_head *cmds)
 {
-	struct nft_cache_filter filter = {};
+	struct nft_cache_filter *filter;
 	unsigned int flags;
 	struct cmd *cmd;
 
-	flags = nft_cache_evaluate(nft, cmds, &filter);
-	if (nft_cache_update(nft, flags, msgs, &filter) < 0)
+	filter = nft_cache_filter_init();
+	flags = nft_cache_evaluate(nft, cmds, filter);
+	if (nft_cache_update(nft, flags, msgs, filter) < 0) {
+		nft_cache_filter_fini(filter);
 		return -1;
+	}
+
+	nft_cache_filter_fini(filter);
 
 	list_for_each_entry(cmd, cmds, list) {
 		struct eval_ctx ectx = {
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH v4 3/3 cache: missing family in cache filtering

In reply to Pablo Neira Ayuso

Check family when filtering out listing of tables and sets.

Fixes 3f1d3912c3a6 ("cache: filter out tables that are not requested")
Fixes 635ee1cad8aa ("cache: filter out sets and maps that are not requested")

---
v4: new in this series.

 include/cache.h |  1 +
 src/cache.c     | 12 ++++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)
include/cache.h +1 -0
index cdf1f4fbf6f7..120a1b8d91b5 100644
@@ -49,6 +49,7 @@ struct nft_filter_obj {
 
 struct nft_cache_filter {
 	struct {
+		uint32_t	family;
 		const char	*table;
 		const char	*set;
 	} list;
src/cache.c +8 -4
index fb4137bc17a6..ac7d8b26a642 100644
@@ -194,14 +194,16 @@ static unsigned int evaluate_cache_list(struct nft_ctx *nft, struct cmd *cmd,
 {
 	switch (cmd->obj) {
 	case CMD_OBJ_TABLE:
-		if (filter && cmd->handle.table.name)
+		if (filter && cmd->handle.table.name) {
+			filter->list.family = cmd->handle.family;
 			filter->list.table = cmd->handle.table.name;
-
+		}
 		flags |= NFT_CACHE_FULL;
 		break;
 	case CMD_OBJ_SET:
 	case CMD_OBJ_MAP:
 		if (filter && cmd->handle.table.name && cmd->handle.set.name) {
+			filter->list.family = cmd->handle.family;
 			filter->list.table = cmd->handle.table.name;
 			filter->list.set = cmd->handle.set.name;
 		}
@@ -439,7 +441,8 @@ static int set_cache_cb(struct nftnl_set *nls, void *arg)
 		return -1;
 
 	if (ctx->filter && ctx->filter->list.set &&
-	    (strcmp(ctx->filter->list.table, set->handle.table.name) ||
+	    (ctx->filter->list.family != set->handle.family ||
+	     strcmp(ctx->filter->list.table, set->handle.table.name) ||
 	     strcmp(ctx->filter->list.set, set->handle.set.name))) {
 		set_free(set);
 		return 0;
@@ -699,7 +702,8 @@ static int cache_init_tables(struct netlink_ctx *ctx, struct handle *h,
 		list_del(&table->list);
 
 		if (filter && filter->list.table &&
-		    (strcmp(filter->list.table, table->handle.table.name))) {
+		    (filter->list.family != table->handle.family &&
+		     strcmp(filter->list.table, table->handle.table.name))) {
 			table_free(table);
 			continue;
 		}
Signature · 1 line
2.30.2