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;
}
* @data: pointer to pass data to callback
* @seqnum: sequence number
* @octx: output context
+ * @cache: cache context
*/
struct netlink_ctx {
struct mnl_socket *nf_sock;
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);
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,
struct netlink_ctx *ctx;
const struct location *loc;
bool cache_needed;
+ struct nft_cache *cache;
};
extern int netlink_monitor(struct netlink_mon_handler *monhandler,
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];
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);
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);
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
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);
* @rule: current rule
* @set: current set
* @stmt: current statement
+ * @cache: cache context
* @ectx: expression context
* @pctx: payload context
*/
struct rule *rule;
struct set *set;
struct stmt *stmt;
+ struct nft_cache *cache;
struct expr_ctx ectx;
struct proto_ctx pctx;
};
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,
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;
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();
}
}
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;
if (ctx->table != NULL)
return ctx->table;
- table = table_lookup(&ctx->cmd->handle);
+ table = table_lookup(&ctx->cmd->handle, ctx->cache);
if (table == NULL)
return NULL;
new = expr_clone(sym->expr);
break;
case SYMBOL_SET:
- ret = cache_update(ctx->nf_sock, ctx->cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, ctx->cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
struct chain *chain;
struct set *set;
- if (table_lookup(&ctx->cmd->handle) == NULL) {
+ if (table_lookup(&ctx->cmd->handle, ctx->cache) == NULL) {
if (table == NULL) {
table = table_alloc();
handle_merge(&table->handle, &ctx->cmd->handle);
- table_add_hash(table);
+ table_add_hash(table, ctx->cache);
} else {
- table_add_hash(table_get(table));
+ table_add_hash(table_get(table), ctx->cache);
}
}
switch (cmd->obj) {
case CMD_OBJ_SETELEM:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
return setelem_evaluate(ctx, &cmd->expr);
case CMD_OBJ_SET:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
handle_merge(&cmd->set->handle, &cmd->handle);
return set_evaluate(ctx, cmd->set);
case CMD_OBJ_RULE:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
handle_merge(&cmd->rule->handle, &cmd->handle);
return rule_evaluate(ctx, cmd->rule);
case CMD_OBJ_CHAIN:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
case CMD_OBJ_COUNTER:
case CMD_OBJ_QUOTA:
case CMD_OBJ_CT_HELPER:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
switch (cmd->obj) {
case CMD_OBJ_SETELEM:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
if (obj_type == NFT_OBJECT_UNSPEC)
obj_type = NFT_OBJECT_COUNTER;
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
struct set *set;
int ret;
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs);
if (ret < 0)
return ret;
if (cmd->handle.table == NULL)
return 0;
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
return 0;
case CMD_OBJ_SET:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
cmd->handle.set);
return 0;
case CMD_OBJ_FLOWTABLE:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
cmd->handle.set);
return 0;
case CMD_OBJ_MAP:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
cmd->handle.set);
return 0;
case CMD_OBJ_CHAIN:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
case CMD_OBJ_SETS:
if (cmd->handle.table == NULL)
return 0;
- if (table_lookup(&cmd->handle) == NULL)
+ if (table_lookup(&cmd->handle, ctx->cache) == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
return 0;
{
int ret;
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs);
if (ret < 0)
return ret;
case CMD_OBJ_QUOTAS:
if (cmd->handle.table == NULL)
return 0;
- if (table_lookup(&cmd->handle) == NULL)
+ if (table_lookup(&cmd->handle, ctx->cache) == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
return 0;
struct set *set;
int ret;
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs);
if (ret < 0)
return ret;
switch (cmd->obj) {
case CMD_OBJ_RULESET:
- cache_flush();
+ cache_flush(&ctx->cache->list);
break;
case CMD_OBJ_TABLE:
/* Flushing a table does not empty the sets in the table nor remove
/* Chains don't hold sets */
break;
case CMD_OBJ_SET:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
cmd->handle.set);
return 0;
case CMD_OBJ_MAP:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
cmd->handle.set);
return 0;
case CMD_OBJ_FLOWTABLE:
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
cmd->handle.table);
switch (cmd->obj) {
case CMD_OBJ_CHAIN:
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
+ ctx->msgs);
if (ret < 0)
return ret;
- table = table_lookup(&ctx->cmd->handle);
+ table = table_lookup(&ctx->cmd->handle, ctx->cache);
if (table == NULL)
return cmd_error(ctx, "Could not process rule: Table '%s' does not exist",
ctx->cmd->handle.table);
uint32_t event;
int ret;
- ret = cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs);
if (ret < 0)
return ret;
static int cmd_evaluate_export(struct eval_ctx *ctx, struct cmd *cmd)
{
- return cache_update(ctx->nf_sock, cmd->op, ctx->msgs);
+ return cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs);
}
#ifdef DEBUG
.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;
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)
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;
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);
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;
int i, val, rc = NFT_EXIT_SUCCESS;
struct mnl_socket *nf_sock;
+ memset(&cache, 0, sizeof(cache));
+ init_list_head(&cache.list);
+
nft_init();
nf_sock = netlink_open_sock();
while (1) {
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);
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();
}
static int netlink_delinearize_setelem(struct nftnl_set_elem *nlse,
- const struct set *set)
+ const struct set *set,
+ struct nft_cache *cache)
{
struct nft_data_delinearize nld;
struct expr *expr, *key, *data;
const struct nftnl_expr *nle;
nle = nftnl_set_elem_get(nlse, NFTNL_SET_ELEM_EXPR, NULL);
- expr->stmt = netlink_parse_set_expr(set, nle);
+ expr->stmt = netlink_parse_set_expr(set, cache, nle);
}
if (flags & NFT_SET_ELEM_INTERVAL_END)
expr->flags |= EXPR_F_INTERVAL_END;
static int list_setelem_cb(struct nftnl_set_elem *nlse, void *arg)
{
struct netlink_ctx *ctx = arg;
- return netlink_delinearize_setelem(nlse, ctx->set);
+ return netlink_delinearize_setelem(nlse, ctx->set, ctx->cache);
}
int netlink_get_setelems(struct netlink_ctx *ctx, const struct handle *h,
static void nlr_for_each_set(struct nftnl_rule *nlr,
void (*cb)(struct set *s, void *data),
- void *data)
+ void *data, struct nft_cache *cache)
{
struct nftnl_expr_iter *nlrei;
struct nftnl_expr *nlre;
goto next;
set_name = nftnl_expr_get_str(nlre, NFTNL_EXPR_LOOKUP_SET);
- s = set_lookup_global(family, table, set_name);
+ s = set_lookup_global(family, table, set_name, cache);
if (s == NULL)
goto next;
setname = nftnl_set_get_str(nls, NFTNL_SET_NAME);
family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
- set = set_lookup_global(family, table, setname);
+ set = set_lookup_global(family, table, setname, monh->cache);
if (set == NULL) {
fprintf(stderr, "W: Received event for an unknown set.");
goto out;
nftnl_set_elems_iter_destroy(nlsei);
goto out;
}
- if (netlink_delinearize_setelem(nlse, dummyset) < 0) {
+ if (netlink_delinearize_setelem(nlse, dummyset,
+ monh->cache) < 0) {
set_free(dummyset);
nftnl_set_elems_iter_destroy(nlsei);
goto out;
switch (type) {
case NFT_MSG_NEWRULE:
r = netlink_delinearize_rule(monh->ctx, nlr);
- nlr_for_each_set(nlr, rule_map_decompose_cb, NULL);
+ nlr_for_each_set(nlr, rule_map_decompose_cb, NULL,
+ monh->cache);
printf("add rule %s %s %s ", family, table, chain);
rule_print(r, monh->ctx->octx);
t = netlink_delinearize_table(monh->ctx, nlt);
nftnl_table_free(nlt);
- table_add_hash(t);
+ table_add_hash(t, monh->cache);
}
static void netlink_events_cache_deltable(struct netlink_mon_handler *monh,
h.family = nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY);
h.table = nftnl_table_get_str(nlt, NFTNL_TABLE_NAME);
- t = table_lookup(&h);
+ t = table_lookup(&h, monh->cache);
if (t == NULL)
goto out;
goto out;
s->init = set_expr_alloc(monh->loc, s);
- t = table_lookup(&s->handle);
+ t = table_lookup(&s->handle, monh->cache);
if (t == NULL) {
fprintf(stderr, "W: Unable to cache set: table not found.\n");
set_free(s);
table = nftnl_set_get_str(nls, NFTNL_SET_TABLE);
setname = nftnl_set_get_str(nls, NFTNL_SET_NAME);
- set = set_lookup_global(family, table, setname);
+ set = set_lookup_global(family, table, setname, monh->cache);
if (set == NULL) {
fprintf(stderr,
"W: Unable to cache set_elem. Set not found.\n");
nlse = nftnl_set_elems_iter_next(nlsei);
while (nlse != NULL) {
- if (netlink_delinearize_setelem(nlse, set) < 0) {
+ if (netlink_delinearize_setelem(nlse, set, monh->cache) < 0) {
fprintf(stderr,
"W: Unable to cache set_elem. "
"Delinearize failed.\n");
{
struct nftnl_rule *nlr = netlink_rule_alloc(nlh);
- nlr_for_each_set(nlr, netlink_events_cache_delset_cb, NULL);
+ nlr_for_each_set(nlr, netlink_events_cache_delset_cb, NULL,
+ monh->cache);
nftnl_rule_free(nlr);
}
if (obj == NULL)
goto out;
- t = table_lookup(&obj->handle);
+ t = table_lookup(&obj->handle, monh->cache);
if (t == NULL) {
fprintf(stderr, "W: Unable to cache object: table not found.\n");
obj_free(obj);
name = nftnl_obj_get_str(nlo, NFTNL_OBJ_NAME);
type = nftnl_obj_get_u32(nlo, NFTNL_OBJ_TYPE);
- t = table_lookup(&h);
+ t = table_lookup(&h, monh->cache);
if (t == NULL) {
fprintf(stderr, "W: Unable to cache object: table not found.\n");
goto out;
}
static void trace_print_rule(const struct nftnl_trace *nlt,
- struct output_ctx *octx)
+ struct output_ctx *octx, struct nft_cache *cache)
{
const struct table *table;
uint64_t rule_handle;
if (!h.table)
return;
- table = table_lookup(&h);
+ table = table_lookup(&h, cache);
if (!table)
return;
trace_print_packet(nlt, monh->ctx->octx);
if (nftnl_trace_is_set(nlt, NFTNL_TRACE_RULE_HANDLE))
- trace_print_rule(nlt, monh->ctx->octx);
+ trace_print_rule(nlt, monh->ctx->octx, monh->cache);
break;
case NFT_TRACETYPE_POLICY:
case NFT_TRACETYPE_RETURN:
}
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)
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)) {
#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;
}
dst->position = src->position;
}
-static LIST_HEAD(table_list);
-
-static int cache_init_tables(struct netlink_ctx *ctx, struct handle *h)
+static int cache_init_tables(struct netlink_ctx *ctx, struct handle *h,
+ struct nft_cache *cache)
{
int ret;
if (ret < 0)
return -1;
- list_splice_tail_init(&ctx->list, &table_list);
+ list_splice_tail_init(&ctx->list, &cache->list);
return 0;
}
struct set *set;
int ret;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &ctx->cache->list, list) {
ret = netlink_list_sets(ctx, &table->handle,
&internal_location);
list_splice_tail_init(&ctx->list, &table->sets);
return 0;
}
-static int cache_init(struct mnl_socket *nf_sock, enum cmd_ops cmd,
- struct list_head *msgs)
+static int cache_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
+ enum cmd_ops cmd, struct list_head *msgs)
{
struct handle handle = {
.family = NFPROTO_UNSPEC,
memset(&ctx, 0, sizeof(ctx));
init_list_head(&ctx.list);
ctx.nf_sock = nf_sock;
+ ctx.cache = cache;
ctx.msgs = msgs;
- ret = cache_init_tables(&ctx, &handle);
+ ret = cache_init_tables(&ctx, &handle, cache);
if (ret < 0)
return ret;
ret = cache_init_objects(&ctx, cmd);
return 0;
}
-static bool cache_initialized;
-
-int cache_update(struct mnl_socket *nf_sock, enum cmd_ops cmd,
- struct list_head *msgs)
+int cache_update(struct mnl_socket *nf_sock, struct nft_cache *cache,
+ enum cmd_ops cmd, struct list_head *msgs)
{
int ret;
- if (cache_initialized)
+ if (cache->initialized)
return 0;
replay:
netlink_genid_get(nf_sock);
- ret = cache_init(nf_sock, cmd, msgs);
+ ret = cache_init(nf_sock, cache, cmd, msgs);
if (ret < 0) {
- cache_release();
+ cache_release(cache);
if (errno == EINTR) {
netlink_restart(nf_sock);
goto replay;
}
return -1;
}
- cache_initialized = true;
+ cache->initialized = true;
return 0;
}
-void cache_flush(void)
+void cache_flush(struct list_head *table_list)
{
struct table *table, *next;
- list_for_each_entry_safe(table, next, &table_list, list) {
+ list_for_each_entry_safe(table, next, table_list, list) {
list_del(&table->list);
table_free(table);
}
}
-void cache_release(void)
+void cache_release(struct nft_cache *cache)
{
- cache_flush();
- cache_initialized = false;
+ cache_flush(&cache->list);
+ cache->initialized = false;
}
/* internal ID to uniquely identify a set in the batch */
}
struct set *set_lookup_global(uint32_t family, const char *table,
- const char *name)
+ const char *name, struct nft_cache *cache)
{
struct handle h;
struct table *t;
h.family = family;
h.table = table;
- t = table_lookup(&h);
+ t = table_lookup(&h, cache);
if (t == NULL)
return NULL;
return table;
}
-void table_add_hash(struct table *table)
+void table_add_hash(struct table *table, struct nft_cache *cache)
{
- list_add_tail(&table->list, &table_list);
+ list_add_tail(&table->list, &cache->list);
}
-struct table *table_lookup(const struct handle *h)
+struct table *table_lookup(const struct handle *h,
+ const struct nft_cache *cache)
{
struct table *table;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &cache->list, list) {
if (table->handle.family == h->family &&
!strcmp(table->handle.table, h->table))
return table;
struct table *table;
struct set *set;
- table = table_lookup(h);
+ table = table_lookup(h, ctx->cache);
set = set_lookup(table, h->set);
if (set->flags & NFT_SET_INTERVAL &&
struct table *table;
struct set *set;
- table = table_lookup(h);
+ table = table_lookup(h, ctx->cache);
set = set_lookup(table, h->set);
if (set->flags & NFT_SET_INTERVAL &&
struct table *table;
struct set *set;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &ctx->cache->list, list) {
if (cmd->handle.family != NFPROTO_UNSPEC &&
cmd->handle.family != table->handle.family)
continue;
struct table *table;
struct obj *obj;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &ctx->cache->list, list) {
if (cmd->handle.family != NFPROTO_UNSPEC &&
cmd->handle.family != table->handle.family)
continue;
unsigned int family = cmd->handle.family;
struct table *table;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &ctx->cache->list, list) {
if (family != NFPROTO_UNSPEC &&
table->handle.family != family)
continue;
{
struct table *table;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &ctx->cache->list, list) {
if (cmd->handle.family != NFPROTO_UNSPEC &&
cmd->handle.family != table->handle.family)
continue;
struct table *table;
struct chain *chain;
- list_for_each_entry(table, &table_list, list) {
+ list_for_each_entry(table, &ctx->cache->list, list) {
if (cmd->handle.family != NFPROTO_UNSPEC &&
cmd->handle.family != table->handle.family)
continue;
struct table *table = NULL;
if (cmd->handle.table != NULL)
- table = table_lookup(&cmd->handle);
+ table = table_lookup(&cmd->handle, ctx->cache);
switch (cmd->obj) {
case CMD_OBJ_TABLE:
ret = netlink_reset_objs(ctx, &cmd->handle, &cmd->location, type, dump);
list_for_each_entry_safe(obj, next, &ctx->list, list) {
- table = table_lookup(&obj->handle);
+ table = table_lookup(&obj->handle, ctx->cache);
list_move(&obj->list, &table->objs);
}
if (ret < 0)
static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd)
{
- struct table *table = table_lookup(&cmd->handle);
+ struct table *table = table_lookup(&cmd->handle, ctx->cache);
struct chain *chain;
switch (cmd->obj) {
struct chain *chain;
int ret;
- list_for_each_entry(t, &table_list, list) {
+ list_for_each_entry(t, &ctx->cache->list, list) {
list_for_each_entry(s, &t->sets, list)
s->init = set_expr_alloc(&cmd->location, s);
monhandler.format = cmd->monitor->format;
monhandler.ctx = ctx;
monhandler.loc = &cmd->location;
+ monhandler.cache = ctx->cache;
return netlink_monitor(&monhandler, ctx->nf_sock);
}