cache consolidation
Pablo Neira Ayuso
maintainerv4 cover cache consolidation
Hi,
This is another round of the patchset to consolidate the nft cache. The idea consists of creating a cache of tables that is populated with chains, rules, sets and elements before parsing/evaluation.
This comes with several advantages:
1) We can now keep the ruleset file in a linear list fashion. We can also apply incremental set declaration updates in a file in an atomic fashion, eg.
-o-FILE:nft-ruleset-o-
add table filter
add chain filter input { type filter hook input priority 0; }
add set filter blacklist { type ipv4_addr; }
add element filter blacklist { 4.4.4.10 }
-o-EOF-o-2) We have a single point to create a consistent cache, thus, we can handle EINTR and validate generation counter to make sure we operate with a ruleset that is up-to-date.
3) We can provide better error reporting from the evaluation step, eg.
# nft add element filter blacklist { 1.1.1.1 }
<cmdline>:1:1-36: Error: Could not process rule: Table 'filter' does not exist
add element filter blacklist { 1.1.1.1 }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^instead of:
# nft add element filter blacklist { 1.1.1.1 }
<cmdline>:1:1-36: Error: Could not process rule: No such file or directory
add element filter blacklist { 1.1.1.1 }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^In follow up patches, it should be possible to reduce the number of object lookups by attaching the corresponding object to struct cmd, so we don't need to look it up again from the final command execution step.
4) We can later on use the cache to perform ruleset transformations as Patrick already suggested.
I will keep testing this here a bit more, then if no objections, I'll push this to master.
Thanks.
Pablo Neira Ayuso (16):
src: consolidate table cache
src: add cmd_evaluate_list()
rule: add reference counter to the table object
src: add table declaration to cache
src: consolidate set cache
src: add set declaration to cache
src: early allocation of the set ID
segtree: pass element expression as parameter to set_to_intervals()
rule: use netlink_add_setelems() when creating literal sets
rule: fix use of intervals in set declarations
rule: add chain reference counter
src: consolidate chain cache
evaluate: add cmd_evaluate_rename()
src: add chain declarations to cache
rule: consolidate rule cache
src: consolidate set element cache
include/expression.h | 3 +- include/rule.h | 9 ++ src/evaluate.c | 142 +++++++++++++++++------- src/main.c | 30 +++++- src/netlink.c | 4 - src/rule.c | 294 ++++++++++++++++++++++++++------------------------ src/segtree.c | 15 +-- 7 files changed, 300 insertions(+), 197 deletions(-)
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 1/16 src: consolidate table cache
This patch introduces the table cache that is populated before parsing and evaluation. As a result, there is a single call to netlink_list_tables().
Follow up patches use this new table cache to store other objects that are contained by the tables.
--- include/rule.h | 3 +++ src/main.c | 30 +++++++++++++++++++++++--- src/rule.c | 64 ++++++++++++++++++++++++++++++++++++-------------------- 3 files changed, 71 insertions(+), 26 deletions(-)
include/rule.h +3 -0
@@ -63,6 +63,9 @@ extern void symbol_bind(struct scope *scope, const char *identifier,extern struct symbol *symbol_lookup (const struct scope *scope,const char *identifier); +extern int cache_init (struct list_head *msgs); +extern void cache_fini (void ); +enum table_flags {TABLE_F_DORMANT = (1 <<0 ), };
src/main.c +27 -3
@@ -182,7 +182,6 @@ static int nft_netlink(struct parser_state *state, struct list_head *msgs)bool batch_supported =netlink_batch_supported ();int ret =0 ; -netlink_genid_get ();mnl_batch_init (); batch_seqnum =mnl_batch_begin ();
@@ -224,25 +223,50 @@ out:return ret; } +static int nft_cache_init (struct list_head *msgs) +{ +netlink_genid_get (); +return cache_init (msgs); +} + +static void nft_cache_fini (void ) +{ +cache_fini (); +} +int nft_run (void *scanner,struct parser_state *state,struct list_head *msgs) {struct cmd *cmd, *next;int ret; +retry: + ret =nft_cache_init (msgs); +if (ret <0 ) { +nft_cache_fini (); +if (errno ==EINTR ) { +netlink_restart (); + goto retry; + } +return -1 ; + } + ret =nft_parse (scanner, state);if (ret !=0 || state->nerrs >0 ) -return -1 ; -retry: + goto err1; + ret =nft_netlink (state, msgs);if (ret <0 && errno ==EINTR ) { +nft_cache_fini ();netlink_restart (); goto retry; } +err1:list_for_each_entry_safe (cmd, next, &state->cmds, list) {list_del (&cmd->list);cmd_free (cmd); } +nft_cache_fini ();return ret; }
src/rule.c +41 -23
113 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 2/16 src: add cmd_evaluate_list()
In reply to Pablo Neira AyusoThis function validates that the table that we want to list already exists by looking up from the new table cache.
This also adds cmd_error() to display an error from the evaluation step, when the objects that the rule indicates do not exist.
We can now simplify the later handling at do_command_list() since we're now sure that the table exists at that stage.
--- src/evaluate.c | 21 +++++++++++++++++++++ src/rule.c | 9 +-------- 2 files changed, 22 insertions(+), 8 deletions(-)
src/evaluate.c +21 -0
@@ -62,6 +62,8 @@ static int __fmtstring(4, 5) __stmt_binary_error(struct eval_ctx *ctx, __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) #define monitor_error(ctx, s1, fmt, args...) \ __stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args) +#define cmd_error(ctx, fmt, args...) \ + __stmt_binary_error(ctx, &(ctx->cmd)->location, NULL, fmt, ## args) static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx, const struct set *set,
@@ -1933,6 +1935,24 @@ static int cmd_evaluate_delete(struct eval_ctx *ctx, struct cmd *cmd) } } +static int cmd_evaluate_list (struct eval_ctx *ctx,struct cmd *cmd) +{ +switch (cmd->obj) { +case CMD_OBJ_TABLE : +case CMD_OBJ_CHAIN : +case CMD_OBJ_SETS : +case CMD_OBJ_SET : +if (table_lookup (&cmd->handle) ==NULL ) +return cmd_error (ctx,"Could not process rule: Table '%s' does not exist" , + cmd->handle.table); +return 0 ; +case CMD_OBJ_RULESET : +return 0 ; +default : +BUG ("invalid command object type %u\n" , cmd->obj); + } +} +enum {CMD_MONITOR_EVENT_ANY ,CMD_MONITOR_EVENT_NEW ,
@@ -2018,6 +2038,7 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)case CMD_DELETE :return cmd_evaluate_delete (ctx, cmd);case CMD_LIST : +return cmd_evaluate_list (ctx, cmd);case CMD_FLUSH :case CMD_RENAME :case CMD_EXPORT :
src/rule.c +1 -8
@@ -940,15 +940,8 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)struct table *table =NULL ;struct set *set; -/* No need to allocate the table object when listing all tables */ -if (cmd->handle.table !=NULL ) { +if (cmd->handle.table !=NULL ) table =table_lookup (&cmd->handle); -if (table ==NULL ) { - table =table_alloc (); -handle_merge (&table->handle, &cmd->handle); -table_add_hash (table); - } - }switch (cmd->obj) {case CMD_OBJ_TABLE :
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 3/16 rule: add reference counter to the table object
In reply to Pablo Neira AyusoWe may hold multiple references to table objects in follow up patches when adding object declarations to the cache.
--- include/rule.h | 3 +++ src/rule.c | 10 ++++++++++ 2 files changed, 13 insertions(+)
include/rule.h +3 -0
@@ -79,6 +79,7 @@ enum table_flags {* @chains: chains contained in the table * @sets: sets contained in the table * @flags: table flags +* @refcnt: table reference counter */ struct table {struct list_head list;
@@ -88,9 +89,11 @@ struct table {struct list_head chains;struct list_head sets;enum table_flags flags; +unsigned int refcnt; };extern struct table *table_alloc (void ); +extern struct table *table_get (struct table *table);extern void table_free (struct table *table);extern void table_add_hash (struct table *table);extern struct table *table_lookup (const struct handle *h);
src/rule.c +10 -0
@@ -535,6 +535,8 @@ struct table *table_alloc(void)init_list_head (&table->chains);init_list_head (&table->sets);init_list_head (&table->scope.symbols); + table->refcnt =1 ; +return table; }
@@ -542,6 +544,8 @@ void table_free(struct table *table) {struct chain *chain, *next; +if (--table->refcnt >0 ) +return ;list_for_each_entry_safe (chain, next, &table->chains, list)chain_free (chain);handle_free (&table->handle);
@@ -549,6 +553,12 @@ void table_free(struct table *table)xfree (table); } +struct table *table_get (struct table *table) +{ + table->refcnt++; +return table; +} +void table_add_hash (struct table *table) {list_add_tail (&table->list, &table_list);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 4/16 src: add table declaration to cache
In reply to Pablo Neira AyusoDeclared table objects are added to the cache, thus we can reference objects that come in this batch, but that are not yet available in the kernel. This happens from the evaluation step.
--- src/evaluate.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
src/evaluate.c +13 -2
@@ -1881,6 +1881,19 @@ static int table_evaluate(struct eval_ctx *ctx, struct table *table)struct chain *chain;struct set *set; +if (table_lookup (&ctx->cmd->handle) ==NULL ) { +if (table ==NULL ) { + table =table_alloc (); +handle_merge (&table->handle, &ctx->cmd->handle); +table_add_hash (table); + }else { +table_add_hash (table_get (table)); + } + } + +if (ctx->cmd->table ==NULL ) +return 0 ; + ctx->table = table;list_for_each_entry (set, &table->sets, list) {handle_merge (&set->handle, &table->handle);
@@ -1912,8 +1925,6 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)return 0 ;return chain_evaluate (ctx, cmd->chain);case CMD_OBJ_TABLE : -if (cmd->data ==NULL ) -return 0 ;return table_evaluate (ctx, cmd->table);default :BUG ("invalid command object type %u\n" , cmd->obj);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 5/16 src: consolidate set cache
In reply to Pablo Neira AyusoThis patch populates the table cache only once through netlink_list_sets() before parsing and evaluation. As a result, there is a single call to netlink_list_sets().
After this change, we can rid of get_set(). This function was fine by the time we had no transaction support, but this doesn't work for set objects that are declared in this batch, so inquiring the kernel doesn't help since they are not yet available.
This also adds cmd_error() to display an error from the evaluation step, when the objects that the rule indicates do not exist.
--- src/evaluate.c | 53 ++++++++++------------------- src/rule.c | 103 ++++++++++++++++++++++++++++---------------------------- 2 files changed, 70 insertions(+), 86 deletions(-)
src/evaluate.c +18 -35
@@ -109,37 +109,6 @@ static struct expr *implicit_set_declaration(struct eval_ctx *ctx,return set_ref_expr_alloc (&expr->location, set); } -// FIXME -#include <netlink.h> -static struct set *get_set (struct eval_ctx *ctx,const struct handle *h, -const char *identifier) -{ -struct netlink_ctx nctx = { - .msgs = ctx->msgs, - }; -struct handle handle; -struct set *set; -int err; - -if (ctx->table !=NULL ) { - set =set_lookup (ctx->table, identifier); -if (set !=NULL ) -return set; - } - -init_list_head (&nctx.list); - -memset (&handle,0 ,sizeof (handle)); -handle_merge (&handle, h); - handle.set =xstrdup (identifier); - err =netlink_get_set (&nctx, &handle, &internal_location); -handle_free (&handle); - -if (err <0 ) -return NULL ; - return list_first_entry(&nctx.list, struct set, list); -} -static enum ops byteorder_conversion_op (struct expr *expr,enum byteorder byteorder) {
@@ -192,6 +161,7 @@ static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr) {struct error_record *erec;struct symbol *sym; +struct table *table;struct set *set;struct expr *new;
@@ -213,9 +183,15 @@ static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr) new =expr_clone (sym->expr);break ;case SYMBOL_SET : - set =get_set (ctx, &ctx->cmd->handle, (*expr)->identifier); + table =table_lookup (&ctx->cmd->handle); +if (table ==NULL ) +return cmd_error (ctx,"Could not process rule: Table '%s' does not exist" , + ctx->cmd->handle.table); + + set =set_lookup (table, (*expr)->identifier);if (set ==NULL ) -return -1 ; +return cmd_error (ctx,"Could not process rule: Set '%s' does not exist" , + (*expr)->identifier); new =set_ref_expr_alloc (&(*expr)->location, set);break ; }
@@ -1737,11 +1713,18 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)static int setelem_evaluate (struct eval_ctx *ctx,struct expr **expr) { +struct table *table;struct set *set; - set =get_set (ctx, &ctx->cmd->handle, ctx->cmd->handle.set); + table =table_lookup (&ctx->cmd->handle); +if (table ==NULL ) +return cmd_error (ctx,"Could not process rule: Table '%s' does not exist" , + ctx->cmd->handle.table); + + set =set_lookup (table, ctx->cmd->handle.set);if (set ==NULL ) -return -1 ; +return cmd_error (ctx,"Could not process rule: Set '%s' does not exist" , + ctx->cmd->handle.set); ctx->set = set;expr_set_context (&ctx->ectx, set->keytype, set->keylen);
src/rule.c +52 -51
177 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 6/16 src: add set declaration to cache
In reply to Pablo Neira AyusoThis patch adds set objects to the cache if they don't exist in the kernel, so they can be referenced from this batch. This occurs from the evaluation step.
--- src/evaluate.c | 9 +++++++++ 1 file changed, 9 insertions(+)
src/evaluate.c +9 -0
@@ -1736,8 +1736,17 @@ static int setelem_evaluate(struct eval_ctx *ctx, struct expr **expr)static int set_evaluate (struct eval_ctx *ctx,struct set *set) { +struct table *table;const char *type; + table =table_lookup (&ctx->cmd->handle); +if (table ==NULL ) +return cmd_error (ctx,"Could not process rule: Table '%s' does not exist" , + ctx->cmd->handle.table); + +if (set_lookup (table, set->handle.set) ==NULL ) +set_add_hash (set_get (set), table); + type = set->flags &SET_F_MAP ?"map" :"set" ;if (set->keytype ==NULL )
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 7/16 src: early allocation of the set ID
In reply to Pablo Neira AyusoBy when the set is created, so element in the batch use this set ID as reference.
--- src/netlink.c | 4 ---- src/rule.c | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-)
src/netlink.c +0 -4
@@ -1121,9 +1121,6 @@ static int netlink_add_set_compat(struct netlink_ctx *ctx,return err; } -/* internal ID to uniquely identify a set in the batch */ -static uint32_t set_id; -static int netlink_add_set_batch (struct netlink_ctx *ctx,const struct handle *h,struct set *set) {
@@ -1147,7 +1144,6 @@ static int netlink_add_set_batch(struct netlink_ctx *ctx,if (set->gc_int)nft_set_attr_set_u32 (nls,NFT_SET_ATTR_GC_INTERVAL , set->gc_int); - set->handle.set_id = ++set_id;nft_set_attr_set_u32 (nls,NFT_SET_ATTR_ID , set->handle.set_id);if (!(set->flags & (SET_F_CONSTANT ))) {
src/rule.c +4 -0
@@ -96,12 +96,16 @@ void cache_fini(void) } } +/* internal ID to uniquely identify a set in the batch */ +static uint32_t set_id; +struct set *set_alloc (const struct location *loc) {struct set *set; set =xzalloc (sizeof (*set)); set->refcnt =1 ; + set->handle.set_id = ++set_id;if (loc !=NULL ) set->location = *loc;return set;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 8/16 segtree: pass element expression as parameter to set_to_intervals()
In reply to Pablo Neira AyusoSo we can reuse this code from set declarations.
--- include/expression.h | 3 ++- src/rule.c | 2 +- src/segtree.c | 15 ++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-)
include/expression.h +2 -1
@@ -362,7 +362,8 @@ extern struct expr *concat_expr_alloc(const struct location *loc);extern struct expr *list_expr_alloc (const struct location *loc);extern struct expr *set_expr_alloc (const struct location *loc); -extern int set_to_intervals (struct list_head *msgs,struct set *set); +extern int set_to_intervals (struct list_head *msgs,struct set *set, +struct expr *init);extern struct expr *mapping_expr_alloc (const struct location *loc,struct expr *from,struct expr *to);
src/rule.c +1 -1
@@ -760,7 +760,7 @@ static int do_add_set(struct netlink_ctx *ctx, const struct handle *h,return -1 ;if (set->init !=NULL ) {if (set->flags &SET_F_INTERVAL && -set_to_intervals (ctx->msgs, set) <0 ) +set_to_intervals (ctx->msgs, set, set->init) <0 )return -1 ;if (do_add_setelems (ctx, &set->handle, set->init) <0 )return -1 ;
src/segtree.c +8 -7
@@ -64,11 +64,12 @@ struct elementary_interval {struct expr *expr; }; -static void seg_tree_init (struct seg_tree *tree,const struct set *set) +static void seg_tree_init (struct seg_tree *tree,const struct set *set, +struct expr *init) {struct expr *first; - first = list_entry(set->init->expressions.next, struct expr, list); + first = list_entry(init->expressions.next, struct expr, list); tree->root =RB_ROOT ; tree->keytype = set->keytype; tree->keylen = set->keylen;
@@ -431,14 +432,14 @@ static void set_insert_interval(struct expr *set, struct seg_tree *tree,compound_expr_add (set, expr); } -int set_to_intervals (struct list_head *errs,struct set *set) +int set_to_intervals (struct list_head *errs,struct set *set,struct expr *init) {struct elementary_interval *ei, *next;struct seg_tree tree;LIST_HEAD (list); -seg_tree_init (&tree, set); -if (set_to_segtree (errs, set->init, &tree) <0 ) +seg_tree_init (&tree, set, init); +if (set_to_segtree (errs, init, &tree) <0 )return -1 ;segtree_linearize (&list, &tree);
@@ -448,12 +449,12 @@ int set_to_intervals(struct list_head *errs, struct set *set)2 * tree.keylen /BITS_PER_BYTE , ei->left, 2 * tree.keylen / BITS_PER_BYTE, ei->right); } -set_insert_interval (set->init, &tree, ei); +set_insert_interval (init, &tree, ei);ei_destroy (ei); }if (segtree_debug ()) { -expr_print (set->init); +expr_print (init);pr_gmp_debug ("\n" ); }return 0 ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 9/16 rule: use netlink_add_setelems() when creating literal sets
In reply to Pablo Neira AyusoThus, do_add_setelems() is only used for set declarations. This comes in preparation to the follow up patch, to avoid resending back to userspace the list of existing elements.
--- src/rule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/rule.c +1 -1
@@ -762,7 +762,7 @@ static int do_add_set(struct netlink_ctx *ctx, const struct handle *h,if (set->flags &SET_F_INTERVAL &&set_to_intervals (ctx->msgs, set, set->init) <0 )return -1 ; -if (do_add_setelems (ctx, &set->handle, set->init) <0 ) +if (netlink_add_setelems (ctx, &set->handle, set->init) <0 )return -1 ; }return 0 ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 10/16 rule: fix use of intervals in set declarations
In reply to Pablo Neira Ayuso # nft add table test
# nft add set test myset { type ipv4_addr\; flags interval\; }
# nft add element test myset { 1.2.3.0/24 }Then the listing shows:
set myset2 {
type ipv4_addr
flags interval
elements = { 1.2.3.0/24}}
This patch relies on the table and set caches.
--- src/rule.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
src/rule.c +17 -1
@@ -746,10 +746,26 @@ static int do_add_chain(struct netlink_ctx *ctx, const struct handle *h, }static int do_add_setelems (struct netlink_ctx *ctx,const struct handle *h, -const struct expr *expr) +struct expr *expr) { +struct table *table; +struct set *set; + + table =table_lookup (h); +if (table ==NULL ) +return -1 ; + + set =set_lookup (table, h->set); +if (set ==NULL ) +return -1 ; + +if (set->flags &SET_F_INTERVAL && +set_to_intervals (ctx->msgs, set, expr) <0 ) +return -1 ; +if (netlink_add_setelems (ctx, h, expr) <0 )return -1 ; +return 0 ; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 11/16 rule: add chain reference counter
In reply to Pablo Neira AyusoWhen adding declared chains to the cache, we may hold more than one single reference from struct cmd and the cache.
--- include/rule.h | 3 +++ src/rule.c | 9 +++++++++ 2 files changed, 12 insertions(+)
include/rule.h +3 -0
@@ -113,6 +113,7 @@ enum chain_flags {* @list: list node in table list * @handle: chain handle * @location: location the chain was defined at +* @refcnt: reference counter * @flags: chain flags * @hookstr: unified and human readable hook name (base chains) * @hooknum: hook number (base chains)
@@ -125,6 +126,7 @@ struct chain {struct list_head list;struct handle handle;struct location location; +unsigned int refcnt;uint32_t flags;const char *hookstr;unsigned int hooknum;
@@ -138,6 +140,7 @@ struct chain {extern const char *chain_type_name_lookup (const char *name);extern const char *chain_hookname_lookup (const char *name);extern struct chain *chain_alloc (const char *name); +extern struct chain *chain_get (struct chain *chain);extern void chain_free (struct chain *chain);extern void chain_add_hash (struct chain *chain,struct table *table); extern struct chain *chain_lookup(const struct table *table,
src/rule.c +9 -0
@@ -400,6 +400,7 @@ struct chain *chain_alloc(const char *name)struct chain *chain; chain =xzalloc (sizeof (*chain)); + chain->refcnt =1 ;init_list_head (&chain->rules);init_list_head (&chain->scope.symbols);if (name !=NULL )
@@ -409,10 +410,18 @@ struct chain *chain_alloc(const char *name)return chain; } +struct chain *chain_get (struct chain *chain) +{ + chain->refcnt++; +return chain; +} +void chain_free (struct chain *chain) {struct rule *rule, *next; +if (--chain->refcnt >0 ) +return ;list_for_each_entry_safe (rule, next, &chain->rules, list)rule_free (rule);handle_free (&chain->handle);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 12/16 src: consolidate chain cache
In reply to Pablo Neira AyusoDump the list of existing chains and populate the cache from the initialization step.
This also include some extra code to validate that the table and chain exists from the evaluation step.
--- src/rule.c | 51 ++++++++++----------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-)
src/rule.c +10 -41
@@ -81,6 +81,13 @@ int cache_init(struct list_head *msgs)if (ret <0 )return -1 ; + + ret =netlink_list_chains (&ctx, &table->handle, + &internal_location); +list_splice_tail_init (&ctx.list, &table->chains); + +if (ret <0 ) +return -1 ; }return 0 ;
@@ -902,22 +909,6 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)return 0 ; } -static void table_cleanup (struct table *table) -{ -struct chain *chain, *nchain; -struct set *set, *nset; - -list_for_each_entry_safe (chain, nchain, &table->chains, list) { -list_del (&chain->list); -chain_free (chain); - } - -list_for_each_entry_safe (set, nset, &table->sets, list) { -list_del (&set->list); -set_free (set); - } -} -static int do_list_table (struct netlink_ctx *ctx,struct cmd *cmd,struct table *table) {
@@ -925,30 +916,16 @@ static int do_list_table(struct netlink_ctx *ctx, struct cmd *cmd,struct chain *chain;if (do_list_sets (ctx, &cmd->location, table) <0 ) - goto err; -if (netlink_list_chains (ctx, &cmd->handle, &cmd->location) <0 ) - goto err; -list_splice_tail_init (&ctx->list, &table->chains); +return -1 ;if (netlink_list_table (ctx, &cmd->handle, &cmd->location) <0 ) - goto err; +return -1 ;list_for_each_entry_safe (rule, nrule, &ctx->list, list) { - table =table_lookup (&rule->handle); chain =chain_lookup (table, &rule->handle); -if (chain ==NULL ) { - chain =chain_alloc (rule->handle.chain); -chain_add_hash (chain, table); - } -list_move_tail (&rule->list, &chain->rules); } -table_print (table); -table_cleanup (table);return 0 ; -err: -table_cleanup (table); -return -1 ; }static int do_list_sets_global (struct netlink_ctx *ctx,struct cmd *cmd)
@@ -1062,18 +1039,10 @@ static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd) {struct table *table;struct chain *chain; -int err; - - table =table_alloc (); -handle_merge (&table->handle, &cmd->handle); -table_add_hash (table);switch (cmd->obj) {case CMD_OBJ_CHAIN : - err =netlink_get_chain (ctx, &cmd->handle, &cmd->location); -if (err <0 ) -return err; -list_splice_tail_init (&ctx->list, &table->chains); + table =table_lookup (&cmd->handle); chain =chain_lookup (table, &cmd->handle); return netlink_rename_chain(ctx, &chain->handle, &cmd->location,
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 13/16 evaluate: add cmd_evaluate_rename()
In reply to Pablo Neira AyusoMake sure the table and the original chain that we want to rename already exist.
--- src/evaluate.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
src/evaluate.c +24 -0
@@ -1956,6 +1956,28 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd) } } +static int cmd_evaluate_rename (struct eval_ctx *ctx,struct cmd *cmd) +{ +struct table *table; +struct chain *chain; + +switch (cmd->obj) { +case CMD_OBJ_CHAIN : + table =table_lookup (&ctx->cmd->handle); +if (table ==NULL ) { +return cmd_error (ctx,"Table '%s' does not exist" , + ctx->cmd->handle.table); + } + chain =chain_lookup (table, &ctx->cmd->handle); +if (chain ==NULL ) +return cmd_error (ctx,"Chain '%s' does not exist" , + ctx->cmd->handle.chain); +return 0 ; +default : +BUG ("invalid command object type %u\n" , cmd->obj); + } +} +enum {CMD_MONITOR_EVENT_ANY ,CMD_MONITOR_EVENT_NEW ,
@@ -2043,7 +2065,9 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)case CMD_LIST :return cmd_evaluate_list (ctx, cmd);case CMD_FLUSH : +return 0 ;case CMD_RENAME : +return cmd_evaluate_rename (ctx, cmd);case CMD_EXPORT :case CMD_DESCRIBE :return 0 ;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 14/16 src: add chain declarations to cache
In reply to Pablo Neira Ayuso--- src/evaluate.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
src/evaluate.c +18 -2
@@ -1850,8 +1850,26 @@ static uint32_t str2hooknum(uint32_t family, const char *hook)static int chain_evaluate (struct eval_ctx *ctx,struct chain *chain) { +struct table *table;struct rule *rule; + table =table_lookup (&ctx->cmd->handle); +if (table ==NULL ) +return cmd_error (ctx,"Table '%s' does not exist" , + ctx->cmd->handle.table); + +if (chain ==NULL ) { +if (chain_lookup (table, &ctx->cmd->handle) ==NULL ) { + chain =chain_alloc (NULL ); +handle_merge (&chain->handle, &ctx->cmd->handle); +chain_add_hash (chain, table); + } +return 0 ; + }else { +if (chain_lookup (table, &chain->handle) ==NULL ) +chain_add_hash (chain_get (chain), table); + } +if (chain->flags &CHAIN_F_BASECHAIN ) { chain->hooknum =str2hooknum (chain->handle.family, chain->hookstr);
@@ -1913,8 +1931,6 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)handle_merge (&cmd->rule->handle, &cmd->handle);return rule_evaluate (ctx, cmd->rule);case CMD_OBJ_CHAIN : -if (cmd->data ==NULL ) -return 0 ;return chain_evaluate (ctx, cmd->chain);case CMD_OBJ_TABLE :return table_evaluate (ctx, cmd->table);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 15/16 rule: consolidate rule cache
In reply to Pablo Neira Ayuso--- src/rule.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
src/rule.c +12 -10
@@ -60,8 +60,10 @@ int cache_init(struct list_head *msgs)struct handle handle = { .family =NFPROTO_UNSPEC , }; +struct rule *rule, *nrule;struct netlink_ctx ctx;struct table *table; +struct chain *chain;int ret;memset (&ctx,0 ,sizeof (ctx));
@@ -88,6 +90,16 @@ int cache_init(struct list_head *msgs)if (ret <0 )return -1 ; + + ret =netlink_list_table (&ctx, &table->handle, + &internal_location); +list_for_each_entry_safe (rule, nrule, &ctx.list, list) { + chain =chain_lookup (table, &rule->handle); +list_move_tail (&rule->list, &chain->rules); + } + +if (ret <0 ) +return -1 ; }return 0 ;
@@ -912,18 +924,8 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)static int do_list_table (struct netlink_ctx *ctx,struct cmd *cmd,struct table *table) { -struct rule *rule, *nrule; -struct chain *chain; -if (do_list_sets (ctx, &cmd->location, table) <0 )return -1 ; -if (netlink_list_table (ctx, &cmd->handle, &cmd->location) <0 ) -return -1 ; - -list_for_each_entry_safe (rule, nrule, &ctx->list, list) { - chain =chain_lookup (table, &rule->handle); -list_move_tail (&rule->list, &chain->rules); - }table_print (table);return 0 ; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v4 16/16 src: consolidate set element cache
In reply to Pablo Neira Ayuso--- src/rule.c | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-)
src/rule.c +11 -25
@@ -64,6 +64,7 @@ int cache_init(struct list_head *msgs)struct netlink_ctx ctx;struct table *table;struct chain *chain; +struct set *set;int ret;memset (&ctx,0 ,sizeof (ctx));
@@ -84,6 +85,13 @@ int cache_init(struct list_head *msgs)if (ret <0 )return -1 ; +list_for_each_entry (set, &table->sets, list) { + ret =netlink_get_setelems (&ctx, &set->handle, + &internal_location, set); +if (ret <0 ) +return -1 ; + } + ret =netlink_list_chains (&ctx, &table->handle, &internal_location);list_splice_tail_init (&ctx.list, &table->chains);
@@ -894,18 +902,6 @@ static int do_command_delete(struct netlink_ctx *ctx, struct cmd *cmd) } } -static int do_list_sets (struct netlink_ctx *ctx,const struct location *loc, -struct table *table) -{ -struct set *set; - -list_for_each_entry (set, &table->sets, list) { -if (netlink_get_setelems (ctx, &set->handle, loc, set) <0 ) -return -1 ; - } -return 0 ; -} -static int do_command_export (struct netlink_ctx *ctx,struct cmd *cmd) { struct nft_ruleset *rs = netlink_dump_ruleset(ctx, &cmd->handle,
@@ -924,25 +920,18 @@ static int do_command_export(struct netlink_ctx *ctx, struct cmd *cmd)static int do_list_table (struct netlink_ctx *ctx,struct cmd *cmd,struct table *table) { -if (do_list_sets (ctx, &cmd->location, table) <0 ) -return -1 ;table_print (table);return 0 ; } -static int do_list_sets_global (struct netlink_ctx *ctx,struct cmd *cmd) +static int do_list_sets (struct netlink_ctx *ctx,struct cmd *cmd) {struct table *table;struct set *set;list_for_each_entry (table, &table_list, list) { -list_for_each_entry (set, &table->sets, list) { -if (netlink_get_setelems (ctx, &set->handle, - &cmd->location, set) <0 ) -return -1 ; - +list_for_each_entry (set, &table->sets, list)set_print (set); - } }return 0 ; }
@@ -988,9 +977,6 @@ static int do_list_set(struct netlink_ctx *ctx, struct cmd *cmd,if (set ==NULL )return -1 ; -if (netlink_get_setelems (ctx, &cmd->handle, &cmd->location, set) <0 ) -return -1 ; -set_print (set);return 0 ; }
@@ -1010,7 +996,7 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_OBJ_CHAIN :return do_list_table (ctx, cmd, table);case CMD_OBJ_SETS : -return do_list_sets_global (ctx, cmd); +return do_list_sets (ctx, cmd);case CMD_OBJ_SET :return do_list_set (ctx, cmd, table);case CMD_OBJ_RULESET :
Signature · 1 line
1.7.10.4