evaluate: flush set cache from the evaluation phase
Pablo Neira Ayuso
maintainerPATCH 1/3 evaluate: flush set cache from the evaluation phase
This patch reworks 40ef308e19b6 ("rule: flush set cache before flush command"). This patch flushes the set cache earlier, from the command evaluation step.
--- src/evaluate.c | 15 +++++++++++++++ src/rule.c | 16 ---------------- 2 files changed, 15 insertions(+), 16 deletions(-)
src/evaluate.c +15 -0
@@ -4375,6 +4375,14 @@ static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd) } } +static void __flush_set_cache (struct set *set) +{ +if (set->init !=NULL ) { +expr_free (set->init); + set->init =NULL ; + } +} +static int cmd_evaluate_flush (struct eval_ctx *ctx,struct cmd *cmd) {struct table *table;
@@ -4402,6 +4410,9 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)else if (!set_is_literal (set->flags))return cmd_error (ctx, &ctx->cmd->handle.set.location,"%s" ,strerror (ENOENT )); + +__flush_set_cache (set); +return 0 ;case CMD_OBJ_MAP : table =table_lookup (&cmd->handle, &ctx->nft->cache);
@@ -4416,6 +4427,8 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)return cmd_error (ctx, &ctx->cmd->handle.set.location,"%s" ,strerror (ENOENT )); +__flush_set_cache (set); +return 0 ;case CMD_OBJ_METER : table =table_lookup (&cmd->handle, &ctx->nft->cache);
@@ -4430,6 +4443,8 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)return cmd_error (ctx, &ctx->cmd->handle.set.location,"%s" ,strerror (ENOENT )); +__flush_set_cache (set); +return 0 ;default :BUG ("invalid command object type %u\n" , cmd->obj);
src/rule.c +0 -16
@@ -2693,21 +2693,6 @@ static int do_command_reset(struct netlink_ctx *ctx, struct cmd *cmd)return do_list_obj (ctx, cmd, type); } -static void flush_set_cache (struct netlink_ctx *ctx,struct cmd *cmd) -{ -struct table *table; -struct set *set; - - table =table_lookup (&cmd->handle, &ctx->nft->cache); -assert (table); - set =set_lookup (table, cmd->handle.set.name); -assert (set); -if (set->init !=NULL ) { -expr_free (set->init); - set->init =NULL ; - } -} -static int do_command_flush (struct netlink_ctx *ctx,struct cmd *cmd) {switch (cmd->obj) {
@@ -2717,7 +2702,6 @@ static int do_command_flush(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_OBJ_SET :case CMD_OBJ_MAP :case CMD_OBJ_METER : -flush_set_cache (ctx, cmd);return mnl_nft_setelem_flush (ctx, cmd);case CMD_OBJ_RULESET :return mnl_nft_table_del (ctx, cmd);
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 3/3 evaluate: remove table from cache on delete table
The following ruleset crashes nft if loaded twice, via nft -ef:
add table inet filter
delete table inet filter
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
iifname { "eth0" } counter accept
}}
If the table contains anonymous sets, such as __set0, then delete + add table might result in nft reusing the existing stale __set0 in the cache. The problem is that nft gets confused and it reuses the existing stale __set0 instead of the new anonymous set __set0 with the same name.
--- src/evaluate.c | 15 +++++++++++++++ tests/shell/testcases/sets/0053echo_0 | 16 ++++++++++++++++ tests/shell/testcases/sets/dumps/0053echo_0.nft | 6 ++++++ 3 files changed, 37 insertions(+) create mode 100755 tests/shell/testcases/sets/0053echo_0 create mode 100644 tests/shell/testcases/sets/dumps/0053echo_0.nft
src/evaluate.c +15 -0
@@ -4172,6 +4172,18 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd) } } +static void table_del_cache (struct eval_ctx *ctx,struct cmd *cmd) +{ +struct table *table; + + table =table_lookup (&cmd->handle, &ctx->nft->cache); +if (!table) +return ; + +list_del (&table->list); +table_free (table); +} +static int cmd_evaluate_delete (struct eval_ctx *ctx,struct cmd *cmd) {switch (cmd->obj) {
@@ -4180,7 +4192,10 @@ static int cmd_evaluate_delete(struct eval_ctx *ctx, struct cmd *cmd)case CMD_OBJ_SET :case CMD_OBJ_RULE :case CMD_OBJ_CHAIN : +return 0 ;case CMD_OBJ_TABLE : +table_del_cache (ctx, cmd); +return 0 ;case CMD_OBJ_FLOWTABLE :case CMD_OBJ_COUNTER :case CMD_OBJ_QUOTA :
tests/shell/testcases/sets/0053echo_0 new file +16 -0
@@ -0,0 +1,16 @@ +#!/bin/bash + +set -e + +EXPECTED="add table inet filter +delete table inet filter + +table inet filter { + chain input { + type filter hook input priority filter; policy drop; + iifname { lo } ip saddr { 10.0.0.0/8 } ip daddr { 192.168.100.62 } tcp dport { 2001 } counter accept + } +} +" + +$NFT -ef - <<< "$EXPECTED"
tests/shell/testcases/sets/dumps/0053echo_0.nft new file +6 -0
@@ -0,0 +1,6 @@ +table inet filter { + chain input { + type filter hook input priority filter; policy drop; + iifname { "lo" } ip saddr { 10.0.0.0/8 } ip daddr { 192.168.100.62 } tcp dport { 2001 } counter packets 0 bytes 0 accept + } +}
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 2/3 src: remove cache lookups after the evaluation phase
In reply to Pablo Neira AyusoThis patch adds a new field to the cmd structure for elements to store a reference to the set. This saves an extra lookup in the netlink bytecode generation step.
This patch also allows to incrementally update during the evaluation phase according to the command actions, which is required by the follow up ("evaluate: remove table from cache on delete table") bugfix patch.
--- include/expression.h | 2 +- include/netlink.h | 2 +- include/rule.h | 4 ++++ src/evaluate.c | 13 ++++++++----- src/netlink.c | 4 ++-- src/rule.c | 43 +++++++++++++++++-------------------------- src/segtree.c | 17 ++++++----------- 7 files changed, 39 insertions(+), 46 deletions(-)
include/expression.h +1 -1
@@ -479,7 +479,7 @@ extern void interval_map_decompose(struct expr *set);extern struct expr *get_set_intervals (const struct set *set,const struct expr *init);struct table ; -extern int get_set_decompose (struct table *table,struct set *set); +extern int get_set_decompose (struct set *cache_set,struct set *set);extern struct expr *mapping_expr_alloc (const struct location *loc,struct expr *from,struct expr *to);
include/netlink.h +1 -1
@@ -149,7 +149,7 @@ extern struct stmt *netlink_parse_set_expr(const struct set *set,extern int netlink_list_setelems (struct netlink_ctx *ctx,const struct handle *h,struct set *set);extern int netlink_get_setelem (struct netlink_ctx *ctx,const struct handle *h, -const struct location *loc,struct table *table, +const struct location *loc,struct set *cache_set,struct set *set,struct expr *init); extern int netlink_delinearize_setelem(struct nftnl_set_elem *nlse, struct set *set,
include/rule.h +4 -0
@@ -683,6 +683,10 @@ struct cmd {void *data;struct expr *expr;struct set *set; +struct { +struct expr *expr;/* same offset as cmd->expr */ +struct set *set; + } elem;struct rule *rule;struct chain *chain;struct table *table;
src/evaluate.c +8 -5
@@ -3560,7 +3560,7 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt) } } -static int setelem_evaluate (struct eval_ctx *ctx,struct expr **expr) +static int setelem_evaluate (struct eval_ctx *ctx,struct cmd *cmd) {struct table *table;struct set *set;
@@ -3576,9 +3576,12 @@ static int setelem_evaluate(struct eval_ctx *ctx, struct expr **expr) ctx->set = set;expr_set_context (&ctx->ectx, set->key->dtype, set->key->len); -if (expr_evaluate (ctx, expr) <0 ) +if (expr_evaluate (ctx, &cmd->expr) <0 )return -1 ; ctx->set =NULL ; + + cmd->elem.set =set_get (set); +return 0 ; }
@@ -4141,7 +4144,7 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd) {switch (cmd->obj) {case CMD_OBJ_ELEMENTS : -return setelem_evaluate (ctx, &cmd->expr); +return setelem_evaluate (ctx, cmd);case CMD_OBJ_SET :handle_merge (&cmd->set->handle, &cmd->handle);return set_evaluate (ctx, cmd->set);
@@ -4173,7 +4176,7 @@ static int cmd_evaluate_delete(struct eval_ctx *ctx, struct cmd *cmd) {switch (cmd->obj) {case CMD_OBJ_ELEMENTS : -return setelem_evaluate (ctx, &cmd->expr); +return setelem_evaluate (ctx, cmd);case CMD_OBJ_SET :case CMD_OBJ_RULE :case CMD_OBJ_CHAIN :
@@ -4197,7 +4200,7 @@ static int cmd_evaluate_get(struct eval_ctx *ctx, struct cmd *cmd) {switch (cmd->obj) {case CMD_OBJ_ELEMENTS : -return setelem_evaluate (ctx, &cmd->expr); +return setelem_evaluate (ctx, cmd);default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/netlink.c +2 -2
@@ -1228,7 +1228,7 @@ int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h, }int netlink_get_setelem (struct netlink_ctx *ctx,const struct handle *h, -const struct location *loc,struct table *table, +const struct location *loc,struct set *cache_set,struct set *set,struct expr *init) {struct nftnl_set *nls, *nls_out =NULL ;
@@ -1261,7 +1261,7 @@ int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,if (set->flags &NFT_SET_INTERVAL && set->desc.field_count >1 )concat_range_aggregate (set->init);else if (set->flags &NFT_SET_INTERVAL ) - err =get_set_decompose (table, set); + err =get_set_decompose (cache_set, set);else list_expr_sort (&ctx->set->init->expressions);
src/rule.c +17 -26
102 lines not on this page · raw message
src/segtree.c +6 -11
@@ -687,17 +687,15 @@ struct expr *get_set_intervals(const struct set *set, const struct expr *init)return new_init; } -static struct expr *get_set_interval_find (const struct table *table, -const char *set_name, +static struct expr *get_set_interval_find (const struct set *cache_set,struct expr *left,struct expr *right) { +const struct set *set = cache_set;struct expr *range =NULL ; -struct set *set;struct expr *i;mpz_t val; - set =set_lookup (table, set_name);mpz_init2 (val, set->key->len);list_for_each_entry (i, &set->init->expressions, list) {
@@ -724,7 +722,7 @@ out:return range; } -int get_set_decompose (struct table *table,struct set *set) +int get_set_decompose (struct set *cache_set,struct set *set) {struct expr *i, *next, *range;struct expr *left =NULL ;
@@ -737,8 +735,7 @@ int get_set_decompose(struct table *table, struct set *set)list_del (&left->list);list_del (&i->list);mpz_sub_ui (i->key->value, i->key->value,1 ); - range =get_set_interval_find (table, set->handle.set.name, - left, i); + range =get_set_interval_find (cache_set, left, i);if (!range) {expr_free (new_init); errno =ENOENT ;
@@ -751,8 +748,7 @@ int get_set_decompose(struct table *table, struct set *set) left =NULL ; }else {if (left) { - range =get_set_interval_find (table, - set->handle.set.name, + range =get_set_interval_find (cache_set, left,NULL );if (range)compound_expr_add (new_init, range);
@@ -764,8 +760,7 @@ int get_set_decompose(struct table *table, struct set *set) } }if (left) { - range =get_set_interval_find (table, set->handle.set.name, - left,NULL ); + range =get_set_interval_find (cache_set, left,NULL );if (range)compound_expr_add (new_init, range);else
Signature · 1 line
2.20.1