cache consolidation
Pablo Neira Ayuso
maintainerv3 cover cache consolidation
This is v3 of the cache consolidation patchset.
In this round, I have patch 2/7 to use the scope so nft knows if this is a table declaration that contains a full block of further declarations or not. I have also fixed several problems I found while testing from the previous round.
More info at:
http://marc.info/?l=netfilter-devel&m=143560012416115&w=2
Pablo Neira Ayuso (7):
src: consolidate table cache
src: add table declaration to cache
src: consolidate set 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
include/expression.h | 3 +- include/rule.h | 6 ++ src/evaluate.c | 49 ++++--------- src/main.c | 37 +++++++++- src/netlink.c | 16 +---- src/parser_bison.y | 10 ++- src/rule.c | 191 ++++++++++++++++++++++++++++++++------------------ src/segtree.c | 15 ++-- 8 files changed, 198 insertions(+), 129 deletions(-)
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v3 1/7 src: consolidate table cache
This patch populates the table cache only once through netlink_list_tables() from the initialization step. As a result, there is a single call to netlink_list_tables().
Then, new table declarations are also added to this cache, thus follow up calls to table_lookup() for tables that don't exist yet in the kernel will be found in the cache.
Note that table_alloc() inserts the object in the cache and table_free() removes the object from the cache and it releases it.
Table objects may be released from cmd_free() or after all commands in the file have been processed.
--- include/rule.h | 3 +++ src/main.c | 29 +++++++++++++++++++-- src/netlink.c | 12 +++------ src/rule.c | 78 ++++++++++++++++++++++++++++++++++---------------------- 4 files changed, 81 insertions(+), 41 deletions(-)
include/rule.h +3 -0
@@ -89,6 +89,9 @@ struct table {extern struct table *table_alloc (void );extern void table_free (struct table *table); + +extern int table_init_hash (void ); +extern void table_fini_hash (void );extern void table_add_hash (struct table *table);extern struct table *table_lookup (const struct handle *h);
src/main.c +27 -2
@@ -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,18 +223,43 @@ out:return ret; } +static int nft_cache_init (void ) +{ +netlink_genid_get (); + +return table_init_hash (); +} + +static void nft_cache_fini (void ) +{ +table_fini_hash (); +} + +static int nft_cache_restart (void ) +{ +nft_cache_fini (); +return nft_cache_init (); +} +int nft_run (void *scanner,struct parser_state *state,struct list_head *msgs) {struct cmd *cmd, *next;int ret; +if (nft_cache_init () <0 ) +return -1 ; + ret =nft_parse (scanner, state); -if (ret !=0 || state->nerrs >0 ) +if (ret !=0 || state->nerrs >0 ) { +nft_cache_fini ();return -1 ; + } retry: ret =nft_netlink (state, msgs);if (ret <0 && errno ==EINTR ) {netlink_restart (); +if (nft_cache_restart () <0 ) +return -1 ; goto retry; }
@@ -243,6 +267,7 @@ retry:list_del (&cmd->list);cmd_free (cmd); } +nft_cache_fini ();return ret; }
src/netlink.c +3 -9
@@ -926,10 +926,8 @@ static struct table *netlink_delinearize_table(struct netlink_ctx *ctx,static int list_table_cb (struct nft_table *nlt,void *arg) {struct netlink_ctx *ctx = arg; -struct table *table; - table =netlink_delinearize_table (ctx, nlt); -list_add_tail (&table->list, &ctx->list); +netlink_delinearize_table (ctx, nlt);return 0 ; }
@@ -972,7 +970,7 @@ int netlink_get_table(struct netlink_ctx *ctx, const struct handle *h, ntable =netlink_delinearize_table (ctx, nlt); table->flags = ntable->flags; -xfree (ntable); +table_free (ntable); out:nft_table_free (nlt);return err;
@@ -1963,13 +1961,10 @@ static void netlink_events_cache_addtable(struct netlink_mon_handler *monh, const struct nlmsghdr *nlh) {struct nft_table *nlt; -struct table *t; nlt =netlink_table_alloc (nlh); - t =netlink_delinearize_table (monh->ctx, nlt); +netlink_delinearize_table (monh->ctx, nlt);nft_table_free (nlt); - -table_add_hash (t); } static void netlink_events_cache_deltable(struct netlink_mon_handler *monh,
@@ -1987,7 +1982,6 @@ static void netlink_events_cache_deltable(struct netlink_mon_handler *monh,if (t ==NULL ) goto out; -list_del (&t->list);table_free (t); out:nft_table_free (nlt);
src/rule.c +48 -30
159 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v3 2/7 src: add table declaration to cache
In reply to Pablo Neira AyusoThis patch prepares the set cache consolidation to avoid that sets are added twice, once from do_add_table() which iterates over the set list when cmd->data is set, and then again from the do_add_set().
Table objects that are declared with no table block to the cache are now available through table_lookup().
--- src/evaluate.c | 2 -- src/parser_bison.y | 10 ++++++++-- src/rule.c | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-)
src/evaluate.c +0 -2
@@ -1910,8 +1910,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);
src/parser_bison.y +8 -2
@@ -654,7 +654,10 @@ base_cmd : /* empty */ add_cmd { $$ = $1; } add_cmd : TABLE table_spec { - $$ = cmd_alloc(CMD_ADD, CMD_OBJ_TABLE, &$2, &@$, NULL); + struct table *table = table_alloc(); + + handle_merge(&table->handle, &$2); + $$ = cmd_alloc(CMD_ADD, CMD_OBJ_TABLE, &$2, &@$, table); } | TABLE table_spec table_block_alloc '{' table_block '}'
@@ -705,7 +708,10 @@ add_cmd : TABLE table_spec create_cmd : TABLE table_spec { - $$ = cmd_alloc(CMD_CREATE, CMD_OBJ_TABLE, &$2, &@$, NULL); + struct table *table = table_alloc(); + + handle_merge(&table->handle, &$2); + $$ = cmd_alloc(CMD_CREATE, CMD_OBJ_TABLE, &$2, &@$, table); } | TABLE table_spec table_block_alloc '{' table_block '}'
src/rule.c +1 -1
@@ -759,7 +759,7 @@ static int do_add_table(struct netlink_ctx *ctx, const struct handle *h,if (netlink_add_table (ctx, h, loc, table, excl) <0 )return -1 ; -if (table !=NULL ) { +if (table->scope.parent !=NULL ) {list_for_each_entry (chain, &table->chains, list) { if (netlink_add_chain(ctx, &chain->handle, &chain->location, chain,
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v3 3/7 src: consolidate set cache
In reply to Pablo Neira AyusoThis patch populates the table cache only once through netlink_list_sets() from the initialization step. As a result, there is a single call to netlink_list_sets().
The set cache initialization happens once the table cache is ready. On the other hand, declared sets are added to the cache so they can be referenced from this batch.
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 consulting the kernel doesn't help since they are not yet available.
--- include/rule.h | 3 ++ src/evaluate.c | 47 +++++++++---------------------- src/main.c | 10 ++++++- src/rule.c | 86 ++++++++++++++++++++++++++++++++------------------------ 4 files changed, 76 insertions(+), 70 deletions(-)
include/rule.h +3 -0
@@ -219,6 +219,9 @@ struct set {extern struct set *set_alloc (const struct location *loc);extern struct set *set_get (struct set *set);extern void set_free (struct set *set); + +int set_init_hash (void ); +void set_fini_hash (void );extern void set_add_hash (struct set *set,struct table *table);extern struct set *set_lookup (const struct table *table,const char *name); extern struct set *set_lookup_global(uint32_t family, const char *table,
src/evaluate.c +14 -33
73 lines not on this page · raw message
src/main.c +9 -1
@@ -227,11 +227,19 @@ static int nft_cache_init(void) {netlink_genid_get (); -return table_init_hash (); +if (table_init_hash () <0 ) +return -1 ; + +if (set_init_hash () <0 ) { +table_fini_hash (); +return -1 ; + } +return 0 ; }static void nft_cache_fini (void ) { +set_fini_hash ();table_fini_hash (); }
src/rule.c +50 -36
134 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v3 4/7 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 | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-)
src/netlink.c +0 -4
@@ -1117,9 +1117,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) {
@@ -1143,7 +1140,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 +5 -0
@@ -56,6 +56,9 @@ void handle_merge(struct handle *dst, const struct handle *src) dst->comment =xstrdup (src->comment); } +/* 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;
@@ -64,6 +67,8 @@ struct set *set_alloc(const struct location *loc) set->refcnt =1 ;if (loc !=NULL ) set->location = *loc; + + set->handle.set_id = ++set_id;return set; }
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH v3 5/7 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
@@ -783,7 +783,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 v3 6/7 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
@@ -785,7 +785,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 v3 7/7 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
@@ -769,10 +769,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