cache consolidation
Pablo Neira Ayuso
maintainercover cache consolidation
Hi Patrick,
This patchset creates two caches, one for tables and another for sets, that contain the existing objects in the kernel.
Moreover, this also adds the declared objects that don't exists yet in the kernel to the cache, so they can be referenced from a batch, eg.
-BEGIN of test.ruleset-
add table test
add chain test test
add set test myset { type ipv4_addr; }
add element test myset { 4.4.4.10 }
add element test myset { 4.4.4.11 }
add element test myset { 4.4.4.12 }
add element test myset { 4.4.4.13 }
add rule test test ip saddr @myset
-EOF-
# nft -f test.ruleset
The idea is to use table_lookup() and set_lookup(), instead of inquiring the kernel (which would fail since those objects don't exist yet there). The example above now works and those updates are handled from the same transaction.
This patch also includes the fix of intervals in set declarations by using these caches as you suggested, now that we got rid of the get_set() function.
Let me know if you have any concern with these, thanks!
Pablo Neira Ayuso (6):
src: always allocate table object with no table block
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 | 3 + src/evaluate.c | 60 ++++++++----------- src/main.c | 6 ++ src/netlink.c | 4 -- src/parser_bison.y | 7 ++- src/rule.c | 156 ++++++++++++++++++++++++++++++++------------------ src/segtree.c | 15 ++--- 8 files changed, 147 insertions(+), 107 deletions(-)
--
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 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 | 8 +++++- src/netlink.c | 12 +++------ src/rule.c | 78 ++++++++++++++++++++++++++++++++++++-------------------- 4 files changed, 63 insertions(+), 38 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 +7 -1
@@ -229,9 +229,14 @@ int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs)struct cmd *cmd, *next;int ret; +if (table_init_hash () <0 ) +return -1 ; + ret =nft_parse (scanner, state); -if (ret !=0 || state->nerrs >0 ) +if (ret !=0 || state->nerrs >0 ) { +table_fini_hash ();return -1 ; + } retry: ret =nft_netlink (state, msgs);if (ret <0 && errno ==EINTR ) {
@@ -243,6 +248,7 @@ retry:list_del (&cmd->list);cmd_free (cmd); } +table_fini_hash ();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 +50 -28
153 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerRe: [PATCH 1/7] src: consolidate table cache
I have a new round to revisit the generation ID checks, that were not correct in the previous patch.
Quoted 1 line
From
d55709f87108645269ea478746cf98a63bb2e637Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Fri, 26 Jun 2015 11:33:22 +0200
Subject: [PATCH nft 1/7,v2] 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.
---
v2: Rework v1 to fix generation ID checks.
include/rule.h | 3 +++ src/main.c | 29 ++++++++++++++++++++-- src/netlink.c | 12 +++------ src/rule.c | 75 +++++++++++++++++++++++++++++++++++--------------------- 4 files changed, 80 insertions(+), 39 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 +47 -28
150 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/7 src: always allocate table object with no table block
In reply to Pablo Neira AyusoThis adds the table object to the cache, so it can be reused in the linear list way to express updates.
The CMD_OBJ_RULESET is used to know if the table block, that contains the chain, set and rule declarations, is present.
This 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().
--- src/evaluate.c | 3 +-- src/parser_bison.y | 7 ++++--- src/rule.c | 44 ++++++++++++++++++++++++++++---------------- 3 files changed, 33 insertions(+), 21 deletions(-)
src/evaluate.c +1 -2
@@ -1910,8 +1910,7 @@ 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 ; +case CMD_OBJ_RULESET :return table_evaluate (ctx, cmd->table);default :BUG ("invalid command object type %u\n" , cmd->obj);
src/parser_bison.y +4 -3
@@ -652,16 +652,17 @@ base_cmd : /* empty */ add_cmd { $$ = $1; } | DESCRIBE describe_cmd { $$ = $2; } ; -add_cmd : TABLE table_spec +add_cmd : TABLE table_spec table_block_alloc { - $$ = cmd_alloc(CMD_ADD, CMD_OBJ_TABLE, &$2, &@$, NULL); + handle_merge(&$3->handle, &$2); + $$ = cmd_alloc(CMD_ADD, CMD_OBJ_TABLE, &$2, &@$, $3); } | TABLE table_spec table_block_alloc '{' table_block '}' { handle_merge(&$3->handle, &$2); close_scope(state); - $$ = cmd_alloc(CMD_ADD, CMD_OBJ_TABLE, &$2, &@$, $5); + $$ = cmd_alloc(CMD_ADD, CMD_OBJ_RULESET, &$2, &@$, $5); } | CHAIN chain_spec {
src/rule.c +28 -16
67 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 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 to they can be referenced from this batch.
After this changes, 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 | 57 +++++++++++++++---------------------- src/main.c | 6 ++++ src/rule.c | 85 ++++++++++++++++++++++++++++++++------------------------ 4 files changed, 80 insertions(+), 71 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 +23 -34
101 lines not on this page · raw message
src/main.c +6 -0
@@ -232,6 +232,11 @@ int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs)if (table_init_hash () <0 )return -1 ; +if (set_init_hash () <0 ) { +table_fini_hash (); +return -1 ; + } + ret =nft_parse (scanner, state);if (ret !=0 || state->nerrs >0 ) {table_fini_hash ();
@@ -248,6 +253,7 @@ retry:list_del (&cmd->list);cmd_free (cmd); } +set_fini_hash ();table_fini_hash ();return ret;
src/rule.c +48 -37
133 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerRe: [PATCH 3/7] src: consolidate set cache
Attaching a v2 to rework generation id checks.
Quoted 1 line
From
403a192bb967d6dde3dbfe34ee3fa88f7aca4197Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Fri, 26 Jun 2015 11:33:22 +0200
Subject: [PATCH nft 3/7, v2] src: consolidate set cache
This 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 changes, 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.
---
v2: Rework v1 to fix generation ID checks.
include/rule.h | 3 +++ src/evaluate.c | 57 ++++++++++++++++------------------------ src/main.c | 10 ++++++- src/rule.c | 80 ++++++++++++++++++++++++++++++-------------------------- 4 files changed, 78 insertions(+), 72 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 +23 -34
101 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 +43 -37
128 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/7 src: early allocation of the set ID
In reply to Pablo Neira Ayuso--- 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 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
@@ -795,7 +795,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 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
@@ -797,7 +797,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 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
@@ -781,10 +781,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