Skip to contentSkip to conversation

src: remove useless parameter from cache_flush()

PATCH In review Pablo Neira Ayuso 2 participants 8 messages Jun 17, 2019 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v2 1/5 src: remove useless parameter from cache_flush()

Command type is never used in cache_flush().

---
v2: no changes.

 include/rule.h | 3 +--
 src/evaluate.c | 2 +-
 src/rule.c     | 2 +-
 3 files changed, 3 insertions(+), 4 deletions(-)
include/rule.h +1 -2
index b41825d000d6..299485ffeeaa 100644
@@ -639,8 +639,7 @@ extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
 extern int cache_evaluate(struct nft_ctx *nft, struct list_head *cmds);
 extern int cache_update(struct nft_ctx *ctx, enum cmd_ops cmd,
 			struct list_head *msgs);
-extern void cache_flush(struct nft_ctx *ctx, enum cmd_ops cmd,
-			struct list_head *msgs);
+extern void cache_flush(struct nft_ctx *ctx, struct list_head *msgs);
 extern void cache_release(struct nft_cache *cache);
 extern bool cache_is_complete(struct nft_cache *cache, enum cmd_ops cmd);
 
src/evaluate.c +1 -1
index 70c7e597f3b0..73a4be339ce1 100644
@@ -3682,7 +3682,7 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)
 
 	switch (cmd->obj) {
 	case CMD_OBJ_RULESET:
-		cache_flush(ctx->nft, cmd->op, ctx->msgs);
+		cache_flush(ctx->nft, ctx->msgs);
 		break;
 	case CMD_OBJ_TABLE:
 		/* Flushing a table does not empty the sets in the table nor remove
src/rule.c +1 -1
index 0c0fd07ec70c..4407b0b0ceaa 100644
@@ -297,7 +297,7 @@ static void __cache_flush(struct list_head *table_list)
 	}
 }
 
-void cache_flush(struct nft_ctx *nft, enum cmd_ops cmd, struct list_head *msgs)
+void cache_flush(struct nft_ctx *nft, struct list_head *msgs)
 {
 	struct netlink_ctx ctx = {
 		.list		= LIST_HEAD_INIT(ctx.list),
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH v2 2/5 tests: shell: cannot use handle for non-existing rule in kernel

This test invokes the 'replace rule ... handle 2' command. However, there are no rules in the kernel, therefore it always fails.

---
v2: no changes.

 tests/shell/testcases/nft-f/0006action_object_0 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/shell/testcases/nft-f/0006action_object_0 +1 -1
index ffa6c9bda973..fab3070f493f 100755
@@ -16,7 +16,7 @@ generate1()
 	add set $family t s {type inet_service;}
 	add element $family t s {8080}
 	insert rule $family t c meta l4proto tcp tcp dport @s accept
-	replace rule $family t c handle 2 meta l4proto tcp tcp dport {9090, 8080}
+	add rule $family t c meta l4proto tcp tcp dport {9090, 8080}
 	add map $family t m {type inet_service:verdict;}
 	add element $family t m {10080:drop}
 	insert rule $family t c meta l4proto tcp tcp dport vmap @m
Signature · 1 line
2.11.0
reviewer
#2 lore raw

Re: [PATCH nft,v2 2/5] tests: shell: cannot use handle for non-existing rule in kernel

Hey Pablo!

Quoted 2 lines · Pablo Neira Ayuso

This test invokes the 'replace rule ... handle 2' command. However, there are no rules in the kernel, therefore it always fails.

I found the cause for why this stopped working: You forgot to adjust rule_evaluate(), what you need is something like this:

src/evaluate.c +1 -1
index ff0888d0c7842..f17bebe4a5f22 100644
@@ -3295,7 +3295,7 @@ static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule,
        }
 
        /* add rules to cache only if it is complete enough to contain them */
-       if (!cache_is_complete(&ctx->nft->cache, CMD_LIST))
+       if (!(ctx->nft->cache.flags & NFT_CACHE_RULE))
                return 0;
 
        return rule_cache_update(ctx, op);

Then handle guessing works again. :)

Cheers, Phil

author maintainer
#3 lore raw

Re: [PATCH nft,v2 2/5] tests: shell: cannot use handle for non-existing rule in kernel

Quoted 14 lines · Phil Sutter

Hey Pablo!

Quoted 2 lines · Pablo Neira Ayuso

This test invokes the 'replace rule ... handle 2' command. However, there are no rules in the kernel, therefore it always fails.

I found the cause for why this stopped working: You forgot to adjust rule_evaluate(), what you need is something like this:

src/evaluate.c +0 -0
index ff0888d0c7842..f17bebe4a5f22 100644

src/evaluate.c static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule, · line 3299

Quoted 6 lines
src/evaluate.c +1 -1
@@ -3295,7 +3295,7 @@ static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule,
        }
 
        /* add rules to cache only if it is complete enough to contain them */
-       if (!cache_is_complete(&ctx->nft->cache, CMD_LIST))
+       if (!(ctx->nft->cache.flags & NFT_CACHE_RULE))
                return 0;

Thanks! I'll fix this an send a new version.

author maintainer
#4 lore raw

Re: [PATCH nft,v2 2/5] tests: shell: cannot use handle for non-existing rule in kernel

Quoted 17 lines · Pablo Neira Ayuso
Quoted 16 lines · Phil Sutter

Hey Pablo!

Quoted 2 lines · Pablo Neira Ayuso

This test invokes the 'replace rule ... handle 2' command. However, there are no rules in the kernel, therefore it always fails.

I found the cause for why this stopped working: You forgot to adjust rule_evaluate(), what you need is something like this:

src/evaluate.c +0 -0
index ff0888d0c7842..f17bebe4a5f22 100644
@@ -3295,7 +3295,7 @@ static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule,
        }
 
Quoted 6 lines

src/evaluate.c static int rule_evaluate(struct eval_ctx *ctx, struct rule *rule, · line 3299

Quoted 4 lines
src/evaluate.c +1 -1
        /* add rules to cache only if it is complete enough to contain them */
-       if (!cache_is_complete(&ctx->nft->cache, CMD_LIST))
+       if (!(ctx->nft->cache.flags & NFT_CACHE_RULE))
                return 0;

Thanks! I'll fix this an send a new version.

If you're fine with this patchset, I propose to rewrite this commit log to:

tests: shell: cannot use handle for non-existing rule in kernel

Do not guess handle for an unexisting rule.

Thanks!

author maintainer
#5 lore raw

PATCH v2 3/5 rule: skip cache population from do_command_monitor()

In reply to Pablo Neira Ayuso

nft_evaluate() already populates the cache before running the monitor command. Remove this code.

Fixes 7df42800cf89 ("src: single cache_update() call to build cache before evaluation")

---
v2: move patch from 4/5 to 3/5.

 src/rule.c | 32 --------------------------------
 1 file changed, 32 deletions(-)
src/rule.c +0 -32
index 4407b0b0ceaa..bcd1c0bf73e8 100644
@@ -2427,8 +2427,6 @@ static bool need_cache(const struct cmd *cmd)
 
 static int do_command_monitor(struct netlink_ctx *ctx, struct cmd *cmd)
 {
-	struct table *t;
-	struct set *s;
 	struct netlink_mon_handler monhandler = {
 		.monitor_flags	= cmd->monitor->flags,
 		.format		= cmd->monitor->format,
@@ -2442,36 +2440,6 @@ static int do_command_monitor(struct netlink_ctx *ctx, struct cmd *cmd)
 		monhandler.format = NFTNL_OUTPUT_JSON;
 
 	monhandler.cache_needed = need_cache(cmd);
-	if (monhandler.cache_needed) {
-		struct rule *rule, *nrule;
-		struct chain *chain;
-		int ret;
-
-		list_for_each_entry(t, &ctx->nft->cache.list, list) {
-			list_for_each_entry(s, &t->sets, list)
-				s->init = set_expr_alloc(&cmd->location, s);
-
-			if (!(cmd->monitor->flags & (1 << NFT_MSG_TRACE)))
-				continue;
-
-			/* When tracing we'd like to translate the rule handle
-			 * we receive in the trace messages to the actual rule
-			 * struct to print that out.  Populate rule cache now.
-			 */
-			ret = netlink_list_table(ctx, &t->handle);
-
-			if (ret != 0)
-				/* Shouldn't happen and doesn't break things
-				 * too badly
-				 */
-				continue;
-
-			list_for_each_entry_safe(rule, nrule, &ctx->list, list) {
-				chain = chain_lookup(t, &rule->handle);
-				list_move_tail(&rule->list, &chain->rules);
-			}
-		}
-	}
 
 	return netlink_monitor(&monhandler, ctx->nft->nf_sock);
 }
Signature · 1 line
2.11.0
author maintainer
#6 lore raw

PATCH v2 4/5 netlink: remove netlink_list_table()

In reply to Pablo Neira Ayuso

Remove this wrapper, call netlink_list_rules() instead.

---
v2: move patch from 5/5 to 4/5.

 include/netlink.h | 2 +-
 src/netlink.c     | 7 +------
 src/rule.c        | 2 +-
 3 files changed, 3 insertions(+), 8 deletions(-)
include/netlink.h +1 -1
index 0c08b1abbf6a..279723f33d31 100644
@@ -104,6 +104,7 @@ extern struct expr *netlink_alloc_data(const struct location *loc,
 				       const struct nft_data_delinearize *nld,
 				       enum nft_registers dreg);
 
+extern int netlink_list_rules(struct netlink_ctx *ctx, const struct handle *h);
 extern void netlink_linearize_rule(struct netlink_ctx *ctx,
 				   struct nftnl_rule *nlr,
 				   const struct rule *rule);
@@ -115,7 +116,6 @@ extern struct chain *netlink_delinearize_chain(struct netlink_ctx *ctx,
 					       const struct nftnl_chain *nlc);
 
 extern int netlink_list_tables(struct netlink_ctx *ctx, const struct handle *h);
-extern int netlink_list_table(struct netlink_ctx *ctx, const struct handle *h);
 extern struct table *netlink_delinearize_table(struct netlink_ctx *ctx,
 					       const struct nftnl_table *nlt);
 
src/netlink.c +1 -6
index a6d81b4f6424..24d8f03ae4be 100644
@@ -333,7 +333,7 @@ static int list_rule_cb(struct nftnl_rule *nlr, void *arg)
 	return 0;
 }
 
-static int netlink_list_rules(struct netlink_ctx *ctx, const struct handle *h)
+int netlink_list_rules(struct netlink_ctx *ctx, const struct handle *h)
 {
 	struct nftnl_rule_list *rule_cache;
 
@@ -485,11 +485,6 @@ int netlink_list_tables(struct netlink_ctx *ctx, const struct handle *h)
 	return 0;
 }
 
-int netlink_list_table(struct netlink_ctx *ctx, const struct handle *h)
-{
-	return netlink_list_rules(ctx, h);
-}
-
 enum nft_data_types dtype_map_to_kernel(const struct datatype *dtype)
 {
 	switch (dtype->type) {
src/rule.c +1 -1
index bcd1c0bf73e8..ed199889702f 100644
@@ -191,7 +191,7 @@ static int cache_init_objects(struct netlink_ctx *ctx, enum cmd_ops cmd)
 		if (cmd != CMD_LIST)
 			continue;
 
-		ret = netlink_list_table(ctx, &table->handle);
+		ret = netlink_list_rules(ctx, &table->handle);
 		list_for_each_entry_safe(rule, nrule, &ctx->list, list) {
 			chain = chain_lookup(table, &rule->handle);
 			list_move_tail(&rule->list, &chain->rules);
Signature · 1 line
2.11.0
author maintainer
#7 lore raw

PATCH v2 5/5 src: add cache level flags

In reply to Pablo Neira Ayuso

The score approach based on command type is confusing.

This patch introduces cache level flags, each flag specifies what kind of object type is needed. These flags are set on/off depending on the list of commands coming in this batch.

cache_is_complete() now checks if the cache contains the objects that are needed through these new flags.

---
v2: use 'enum cache_level_bits' as proposed by Phil Sutter.

 include/Makefile.am |   1 +
 include/cache.h     |  35 +++++++++++++++++
 include/nftables.h  |   2 +-
 include/rule.h      |   3 +-
 src/cache.c         |  87 ++++++++++++++++++++++--------------------
 src/evaluate.c      |   1 +
 src/libnftables.c   |   6 +--
 src/rule.c          | 106 +++++++++++++++++++++++-----------------------------
 8 files changed, 135 insertions(+), 106 deletions(-)
 create mode 100644 include/cache.h
include/Makefile.am +1 -0
index b1f4fcf29015..2d77a768acb0 100644
@@ -2,6 +2,7 @@ SUBDIRS =		linux		\
 			nftables
 
 noinst_HEADERS = 	cli.h		\
+			cache.h		\
 			datatype.h	\
 			expression.h	\
 			fib.h		\
include/cache.h new file +35 -0
new file mode 100644
index 000000000000..d3502a8a6039
@@ -0,0 +1,35 @@
+#ifndef _NFT_CACHE_H_
+#define _NFT_CACHE_H_
+
+enum cache_level_bits {
+	NFT_CACHE_TABLE_BIT	= (1 << 0),
+	NFT_CACHE_CHAIN_BIT	= (1 << 1),
+	NFT_CACHE_SET_BIT	= (1 << 2),
+	NFT_CACHE_FLOWTABLE_BIT	= (1 << 3),
+	NFT_CACHE_OBJECT_BIT	= (1 << 4),
+	NFT_CACHE_SETELEM_BIT	= (1 << 5),
+	NFT_CACHE_RULE_BIT	= (1 << 6),
+	__NFT_CACHE_MAX_BIT	= (1 << 7),
+};
+
+enum cache_level_flags {
+	NFT_CACHE_EMPTY		= 0,
+	NFT_CACHE_TABLE		= NFT_CACHE_TABLE_BIT,
+	NFT_CACHE_CHAIN		= NFT_CACHE_TABLE_BIT |
+				  NFT_CACHE_CHAIN_BIT,
+	NFT_CACHE_SET		= NFT_CACHE_TABLE_BIT |
+				  NFT_CACHE_SET_BIT,
+	NFT_CACHE_FLOWTABLE	= NFT_CACHE_TABLE_BIT |
+				  NFT_CACHE_FLOWTABLE_BIT,
+	NFT_CACHE_OBJECT	= NFT_CACHE_TABLE_BIT |
+				  NFT_CACHE_OBJECT_BIT,
+	NFT_CACHE_SETELEM	= NFT_CACHE_TABLE_BIT |
+				  NFT_CACHE_SET_BIT |
+				  NFT_CACHE_SETELEM_BIT,
+	NFT_CACHE_RULE		= NFT_CACHE_TABLE_BIT |
+				  NFT_CACHE_CHAIN_BIT |
+				  NFT_CACHE_RULE_BIT,
+	NFT_CACHE_FULL		= __NFT_CACHE_MAX_BIT - 1,
+};
+
+#endif /* _NFT_CACHE_H_ */
include/nftables.h +1 -1
index b7c78572da77..ed446e2d16cf 100644
@@ -81,7 +81,7 @@ struct nft_cache {
 	uint32_t		genid;
 	struct list_head	list;
 	uint32_t		seqnum;
-	uint32_t		cmd;
+	uint32_t		flags;
 };
 
 struct mnl_socket;
include/rule.h +1 -2
index 299485ffeeaa..aefb24d95163 100644
@@ -462,7 +462,6 @@ enum cmd_ops {
 	CMD_EXPORT,
 	CMD_MONITOR,
 	CMD_DESCRIBE,
-	__CMD_FLUSH_RULESET,
 };
 
 /**
@@ -636,7 +635,7 @@ extern struct error_record *rule_postprocess(struct rule *rule);
 struct netlink_ctx;
 extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
 
-extern int cache_evaluate(struct nft_ctx *nft, struct list_head *cmds);
+extern unsigned int cache_evaluate(struct nft_ctx *nft, struct list_head *cmds);
 extern int cache_update(struct nft_ctx *ctx, enum cmd_ops cmd,
 			struct list_head *msgs);
 extern void cache_flush(struct nft_ctx *ctx, struct list_head *msgs);
src/cache.c +46 -41

163 lines not on this page · raw message

src/evaluate.c +1 -0
index 73a4be339ce1..ff0888d0c784 100644
@@ -29,6 +29,7 @@
 #include <netlink.h>
 #include <time.h>
 #include <rule.h>
+#include <cache.h>
 #include <erec.h>
 #include <gmputil.h>
 #include <utils.h>
src/libnftables.c +3 -3
index abd133bee127..dccb8ab4da42 100644
@@ -381,11 +381,11 @@ 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)
 {
-	unsigned int completeness;
+	unsigned int flags;
 	struct cmd *cmd;
 
-	completeness = cache_evaluate(nft, cmds);
-	if (cache_update(nft, completeness, msgs) < 0)
+	flags = cache_evaluate(nft, cmds);
+	if (cache_update(nft, flags, msgs) < 0)
 		return -1;
 
 	list_for_each_entry(cmd, cmds, list) {
src/rule.c +47 -59

193 lines not on this page · raw message

Signature · 1 line
2.11.0