src: place object cache in context structure
Pablo Neira Ayuso
maintainerPATCH 1/1 src: place object cache in context structure
Place object cache in context structure, so this is not global anymore. Another step towards libnftables.
Joint work with Varsha Rao.
Signed-off-by Varsha Rao <[email protected]>
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/cli.h | 4 +-- include/netlink.h | 3 +++ include/nftables.h | 5 ++-- include/parser.h | 4 +-- include/rule.h | 16 ++++++----- src/cli.c | 11 ++++---- src/evaluate.c | 62 +++++++++++++++++++++++-------------------- src/main.c | 26 ++++++++++-------- src/netlink.c | 42 +++++++++++++++-------------- src/netlink_delinearize.c | 5 ++-- src/parser_bison.y | 5 ++-- src/rule.c | 67 ++++++++++++++++++++++++----------------------- 12 files changed, 137 insertions(+), 113 deletions(-)
include/cli.h +2 -2
@@ -6,10 +6,10 @@struct parser_state ;#ifdef HAVE_LIBREADLINE extern int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct parser_state *state); +struct list_head *cache,struct parser_state *state);#else static inline int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct parser_state *state) +struct list_head *cache,struct parser_state *state) {return -1 ; }
include/netlink.h +3 -0
@@ -50,6 +50,7 @@ struct netlink_ctx {struct nftnl_batch *batch;bool batch_supported;struct output_ctx *octx; +struct list_head *cache; };extern struct nftnl_table *alloc_nftnl_table (const struct handle *h);
@@ -162,6 +163,7 @@ extern int netlink_get_set(struct netlink_ctx *ctx, const struct handle *h, const struct location *loc);extern struct stmt *netlink_parse_set_expr (const struct set *set, +const struct list_head *cache,const struct nftnl_expr *nle); extern int netlink_add_setelems(struct netlink_ctx *ctx, const struct handle *h,
@@ -216,6 +218,7 @@ struct netlink_mon_handler {struct netlink_ctx *ctx;const struct location *loc;bool cache_needed; +struct list_head *cache; }; extern int netlink_monitor(struct netlink_mon_handler *monhandler,
include/nftables.h +3 -2
@@ -116,8 +116,9 @@ struct input_descriptor {struct parser_state ;struct mnl_socket ; -int nft_run (struct nft_ctx *nft,struct mnl_socket *nf_sock,void *scanner, -struct parser_state *state,struct list_head *msgs); +int nft_run (struct nft_ctx *nft,struct mnl_socket *nf_sock, +struct list_head *cache,void *scanner,struct parser_state *state, +struct list_head *msgs);void ct_label_table_init (void );void mark_table_init (void );
include/parser.h +2 -2
@@ -31,8 +31,8 @@ struct parser_state {struct mnl_socket ; -extern void parser_init (struct mnl_socket *nf_sock,struct parser_state *state, -struct list_head *msgs); +extern void parser_init (struct mnl_socket *nf_sock,struct list_head *cache, +struct parser_state *state,struct list_head *msgs);extern int nft_parse (void *,struct parser_state *state);extern void *scanner_init (struct parser_state *state);
include/rule.h +9 -7
@@ -116,8 +116,9 @@ struct table {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); +extern void table_add_hash (struct table *table,struct list_head *cache); +extern struct table *table_lookup (const struct handle *h, +const struct list_head *cache);/** * enum chain_flags - chain flags
@@ -248,7 +249,7 @@ extern void set_free(struct set *set);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, -const char *name); +const char *name,struct list_head *cache);extern void set_print (const struct set *set,struct output_ctx *octx);extern void set_print_plain (const struct set *s,struct output_ctx *octx);
@@ -479,6 +480,7 @@ struct eval_ctx {struct rule *rule;struct set *set;struct stmt *stmt; +struct list_head *cache;struct expr_ctx ectx;struct proto_ctx pctx; };
@@ -490,10 +492,10 @@ 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_update (struct mnl_socket *nf_sock,enum cmd_ops cmd, -struct list_head *msgs); -extern void cache_flush (void ); -extern void cache_release (void ); +extern int cache_update (struct mnl_socket *nf_sock,struct list_head *cache, +enum cmd_ops cmd,struct list_head *msgs); +extern void cache_flush (struct list_head *table_list); +extern void cache_release (struct list_head *table_list);enum udata_type {UDATA_TYPE_COMMENT ,
src/cli.c +6 -5
@@ -43,8 +43,8 @@ static const struct input_descriptor indesc_cli = {static struct parser_state *state;static struct nft_ctx cli_nft;static struct mnl_socket *cli_nf_sock; +static struct list_head *cli_cache;static void *scanner; -static char histfile[PATH_MAX ];static char *multiline;static bool eof;
@@ -135,12 +135,12 @@ static void cli_complete(char *line)xfree (line); line = s; -parser_init (cli_nf_sock, state, &msgs); +parser_init (cli_nf_sock, cli_cache, state, &msgs);scanner_push_buffer (scanner, &indesc_cli, line); -nft_run (&cli_nft, cli_nf_sock, scanner, state, &msgs); +nft_run (&cli_nft, cli_nf_sock, cli_cache, scanner, state, &msgs);erec_print_list (stdout, &msgs);xfree (line); -cache_release (); +cache_release (cli_cache);iface_cache_release (); }
@@ -176,12 +176,13 @@ void __fmtstring(1, 0) cli_display(const char *fmt, va_list ap) }int cli_init (struct nft_ctx *nft,struct mnl_socket *nf_sock, -struct parser_state *_state) +struct list_head *cache,struct parser_state *_state) {const char *home; cli_nf_sock = nf_sock; cli_nft = *nft; + cli_cache = cache; rl_readline_name ="nft" ; rl_instream = stdin; rl_outstream = stdout;
src/evaluate.c +34 -28
216 lines not on this page · raw message
src/main.c +15 -11
@@ -181,8 +181,9 @@ static const struct input_descriptor indesc_cmdline = { .name = "<cmdline>", }; -static int nft_netlink (struct nft_ctx *nft,struct parser_state *state, -struct list_head *msgs,struct mnl_socket *nf_sock) +static int nft_netlink (struct nft_ctx *nft,struct list_head *cache, +struct parser_state *state,struct list_head *msgs, +struct mnl_socket *nf_sock) {struct nftnl_batch *batch;struct netlink_ctx ctx;
@@ -204,6 +205,7 @@ static int nft_netlink(struct nft_ctx *nft, struct parser_state *state, ctx.batch_supported = batch_supported; ctx.octx = &nft->output; ctx.nf_sock = nf_sock; + ctx.cache = cache;init_list_head (&ctx.list); ret =do_command (&ctx, cmd);if (ret <0 )
@@ -238,8 +240,9 @@ out:return ret; } -int nft_run (struct nft_ctx *nft,struct mnl_socket *nf_sock,void *scanner, -struct parser_state *state,struct list_head *msgs) +int nft_run (struct nft_ctx *nft,struct mnl_socket *nf_sock, +struct list_head *cache,void *scanner,struct parser_state *state, +struct list_head *msgs) {struct cmd *cmd, *next;int ret;
@@ -253,7 +256,7 @@ int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock, void *scanner,list_for_each_entry (cmd, &state->cmds, list)nft_cmd_expand (cmd); - ret =nft_netlink (nft, state, msgs, nf_sock); + ret =nft_netlink (nft, cache, state, msgs, nf_sock); err1:list_for_each_entry_safe (cmd, next, &state->cmds, list) {list_del (&cmd->list);
@@ -289,6 +292,7 @@ int main(int argc, char * const *argv) {struct parser_state state;void *scanner; +LIST_HEAD (cache);LIST_HEAD (msgs);char *buf =NULL , *filename =NULL ;unsigned int len;
@@ -391,20 +395,20 @@ int main(int argc, char * const *argv)strcat (buf," " ); }strcat (buf,"\n" ); -parser_init (nf_sock, &state, &msgs); +parser_init (nf_sock, &cache, &state, &msgs); scanner =scanner_init (&state);scanner_push_buffer (scanner, &indesc_cmdline, buf); }else if (filename !=NULL ) { - rc =cache_update (nf_sock,CMD_INVALID , &msgs); + rc =cache_update (nf_sock, &cache,CMD_INVALID , &msgs);if (rc <0 )return rc; -parser_init (nf_sock, &state, &msgs); +parser_init (nf_sock, &cache, &state, &msgs); scanner =scanner_init (&state);if (scanner_read_file (scanner, filename, &internal_location) <0 ) goto out; }else if (interactive) { -if (cli_init (&nft, nf_sock, &state) <0 ) { +if (cli_init (&nft, nf_sock, &cache, &state) <0 ) {fprintf (stderr,"%s: interactive CLI not supported in this build\n" , argv[0 ]);exit (NFT_EXIT_FAILURE );
@@ -415,13 +419,13 @@ int main(int argc, char * const *argv)exit (NFT_EXIT_FAILURE ); } -if (nft_run (&nft, nf_sock, scanner, &state, &msgs) !=0 ) +if (nft_run (&nft, nf_sock, &cache, scanner, &state, &msgs) !=0 ) rc =NFT_EXIT_FAILURE ; out:scanner_destroy (scanner);erec_print_list (stderr, &msgs);xfree (buf); -cache_release (); +cache_release (&cache);iface_cache_release ();netlink_close_sock (nf_sock);nft_exit ();
src/netlink.c +23 -19
156 lines not on this page · raw message
src/netlink_delinearize.c +3 -2
@@ -1275,12 +1275,13 @@ static int netlink_parse_rule_expr(struct nftnl_expr *nle, void *arg) }struct stmt *netlink_parse_set_expr (const struct set *set, +const struct list_head *cache,const struct nftnl_expr *nle) {struct netlink_parse_ctx ctx, *pctx = &ctx; pctx->rule =rule_alloc (&netlink_location, &set->handle); - pctx->table =table_lookup (&set->handle); + pctx->table =table_lookup (&set->handle, cache);assert (pctx->table !=NULL );if (netlink_parse_expr (nle, pctx) <0 )
@@ -2306,7 +2307,7 @@ struct rule *netlink_delinearize_rule(struct netlink_ctx *ctx, h.position.id =nftnl_rule_get_u64 (nlr,NFTNL_RULE_POSITION ); pctx->rule =rule_alloc (&netlink_location, &h); - pctx->table =table_lookup (&h); + pctx->table =table_lookup (&h, ctx->cache);assert (pctx->table !=NULL );if (nftnl_rule_is_set (nlr,NFTNL_RULE_USERDATA )) {
src/parser_bison.y +3 -2
@@ -35,14 +35,15 @@ #include "parser_bison.h" -void parser_init(struct mnl_socket *nf_sock, struct parser_state *state, - struct list_head *msgs) +void parser_init(struct mnl_socket *nf_sock, struct list_head *cache, + struct parser_state *state, struct list_head *msgs) { memset(state, 0, sizeof(*state)); init_list_head(&state->cmds); init_list_head(&state->top_scope.symbols); state->msgs = msgs; state->scopes[0] = scope_init(&state->top_scope, NULL); + state->ectx.cache = cache; state->ectx.msgs = msgs; state->ectx.nf_sock = nf_sock; }
src/rule.c +34 -33
217 lines not on this page · raw message
Signature · 1 line
2.1.4