Skip to contentSkip to conversation

src: rule: Remove variable cache_initialized.

PATCH Applied Varsha Rao 2 participants 2 messages Aug 13, 2017 – Aug 14, 2017 netfilter

lore
Share & tools
lore

Varsha Rao

author
#0 lore raw

PATCH 1/1 src: rule: Remove variable cache_initialized.

Pass variable cache_initialized and structure list_head as members of structure nft_cache.

---
 include/cli.h             |  4 +--
 include/netlink.h         |  4 +++
 include/nftables.h        | 10 +++++--
 include/parser.h          |  4 +--
 include/rule.h            | 17 ++++++-----
 src/cli.c                 | 11 +++----
 src/evaluate.c            | 62 +++++++++++++++++++++-----------------
 src/main.c                | 29 +++++++++++-------
 src/netlink.c             | 42 ++++++++++++++------------
 src/netlink_delinearize.c |  5 ++--
 src/parser_bison.y        |  5 ++--
 src/rule.c                | 76 +++++++++++++++++++++++------------------------
 12 files changed, 151 insertions(+), 118 deletions(-)
include/cli.h +2 -2
index 21052e3..e577400 100644
@@ -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 nft_cache *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 nft_cache *cache, struct parser_state *state)
 {
         return -1;
 }
include/netlink.h +4 -0
index 7865186..bde0b5d 100644
@@ -39,6 +39,7 @@ extern const struct location netlink_location;
  * @data:	pointer to pass data to callback
  * @seqnum:	sequence number
  * @octx:	output context
+ * @cache:	cache context
  */
 struct netlink_ctx {
 	struct mnl_socket	*nf_sock;
@@ -50,6 +51,7 @@ struct netlink_ctx {
 	struct nftnl_batch	*batch;
 	bool			batch_supported;
 	struct output_ctx	*octx;
+	struct nft_cache	*cache;
 };
 
 extern struct nftnl_table *alloc_nftnl_table(const struct handle *h);
@@ -162,6 +164,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 nft_cache *cache,
 					   const struct nftnl_expr *nle);
 
 extern int netlink_add_setelems(struct netlink_ctx *ctx, const struct handle *h,
@@ -216,6 +219,7 @@ struct netlink_mon_handler {
 	struct netlink_ctx	*ctx;
 	const struct location	*loc;
 	bool			cache_needed;
+	struct nft_cache	*cache;
 };
 
 extern int netlink_monitor(struct netlink_mon_handler *monhandler,
include/nftables.h +8 -2
index 640d3c7..b7bc8fe 100644
@@ -36,6 +36,11 @@ struct nft_ctx {
 	bool			check;
 };
 
+struct nft_cache {
+	bool initialized;
+	struct list_head list;
+};
+
 extern unsigned int max_errors;
 extern unsigned int debug_level;
 extern const char *include_paths[INCLUDE_PATHS_MAX];
@@ -116,8 +121,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 nft_cache *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
index 1815ea1..5a452f7 100644
@@ -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 nft_cache *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 +10 -7
index a0edda2..10ac0e2 100644
@@ -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 nft_cache *cache);
+extern struct table *table_lookup(const struct handle *h,
+				  const struct nft_cache *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 nft_cache *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);
 
@@ -468,6 +469,7 @@ extern void cmd_free(struct cmd *cmd);
  * @rule:	current rule
  * @set:	current set
  * @stmt:	current statement
+ * @cache:	cache context
  * @ectx:	expression context
  * @pctx:	payload context
  */
@@ -479,6 +481,7 @@ struct eval_ctx {
 	struct rule		*rule;
 	struct set		*set;
 	struct stmt		*stmt;
+	struct nft_cache	*cache;
 	struct expr_ctx		ectx;
 	struct proto_ctx	pctx;
 };
@@ -490,10 +493,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 nft_cache *cache,
+			 enum cmd_ops cmd, struct list_head *msgs);
+extern void cache_flush(struct list_head *table_list);
+extern void cache_release(struct nft_cache *cache);
 
 enum udata_type {
 	UDATA_TYPE_COMMENT,
src/cli.c +6 -5
index abb6bf3..a50fc58 100644
@@ -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 nft_cache *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 nft_cache *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 +18 -11
index 1535153..02363c9 100644
@@ -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 nft_cache *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 nft_cache *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);
@@ -288,6 +291,7 @@ void nft_exit(void)
 int main(int argc, char * const *argv)
 {
 	struct parser_state state;
+	struct nft_cache cache;
 	void *scanner;
 	LIST_HEAD(msgs);
 	char *buf = NULL, *filename = NULL;
@@ -296,6 +300,9 @@ int main(int argc, char * const *argv)
 	int i, val, rc = NFT_EXIT_SUCCESS;
 	struct mnl_socket *nf_sock;
 
+	memset(&cache, 0, sizeof(cache));
+	init_list_head(&cache.list);
+	cache.initialized = false;
 	nft_init();
 	nf_sock = netlink_open_sock();
 	while (1) {
@@ -391,20 +398,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 +422,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
index 3ee07c0..5317a83 100644
@@ -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 nft_cache *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
index e7bb909..783b72f 100644
@@ -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 nft_cache *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 +38 -38

235 lines not on this page · raw message

Signature · 1 line
2.13.4
maintainer Applied
#1 lore raw
Quoted 2 lines · Varsha Rao

Pass variable cache_initialized and structure list_head as members of structure nft_cache.

Applied, thanks.

I have renamed this patch title to: "src: introduce struct nft_cache"

For the record: This supersedes my previous patch:

http://patchwork.ozlabs.org/patch/800141/

Thanks.