#ifndef _NFT_CACHE_H_
#define _NFT_CACHE_H_
+#include <string.h>
+
enum cache_level_bits {
NFT_CACHE_TABLE_BIT = (1 << 0),
NFT_CACHE_CHAIN_BIT = (1 << 1),
NFT_CACHE_FLUSHED = (1 << 31),
};
+struct nft_cache {
+ uint32_t genid;
+ struct list_head list;
+ uint32_t seqnum;
+ uint32_t flags;
+};
+
+enum cmd_ops;
+
+unsigned int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds);
+int nft_cache_update(struct nft_ctx *ctx, enum cmd_ops cmd,
+ struct list_head *msgs);
+bool nft_cache_needs_update(struct nft_cache *cache);
+void nft_cache_release(struct nft_cache *cache);
+
static inline uint32_t djb_hash(const char *key)
{
uint32_t i, hash = 5381;
#define NFT_CACHE_HSIZE 8192
-struct netlink_ctx;
struct table;
struct chain;
-struct handle;
-
-int cache_init(struct netlink_ctx *ctx, unsigned int flags);
-int cache_update(struct nft_ctx *nft, unsigned int flags, struct list_head *msgs);
-void cache_release(struct nft_cache *cache);
void chain_cache_add(struct chain *chain, struct table *table);
struct chain *chain_cache_find(const struct table *table, const char *name);
#include <stdarg.h>
#include <limits.h>
#include <utils.h>
+#include <cache.h>
#include <nftables/libnftables.h>
struct cookie {
return octx->flags & NFT_CTX_OUTPUT_TERSE;
}
-struct nft_cache {
- uint32_t genid;
- struct list_head list;
- uint32_t seqnum;
- uint32_t flags;
-};
-
struct mnl_socket;
struct parser_state;
struct scope;
struct netlink_ctx;
extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
-extern unsigned int cache_evaluate(struct nft_ctx *nft, struct list_head *cmds);
-extern int cache_update(struct nft_ctx *ctx, enum cmd_ops cmd,
- struct list_head *msgs);
-extern bool cache_needs_update(struct nft_cache *cache);
-extern void cache_release(struct nft_cache *cache);
-
struct timeout_protocol {
uint32_t array_size;
const char *const *state_to_name;
return flags;
}
-unsigned int cache_evaluate(struct nft_ctx *nft, struct list_head *cmds)
+unsigned int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds)
{
unsigned int flags = NFT_CACHE_EMPTY;
struct cmd *cmd;
return ret;
}
-int cache_init(struct netlink_ctx *ctx, unsigned int flags)
+static int nft_cache_init(struct netlink_ctx *ctx, unsigned int flags)
{
struct handle handle = {
.family = NFPROTO_UNSPEC,
return 0;
}
-static bool cache_is_complete(struct nft_cache *cache, unsigned int flags)
+static bool nft_cache_is_complete(struct nft_cache *cache, unsigned int flags)
{
return (cache->flags & flags) == flags;
}
-static bool cache_needs_refresh(struct nft_cache *cache)
+static bool nft_cache_needs_refresh(struct nft_cache *cache)
{
return cache->flags & NFT_CACHE_REFRESH;
}
-static bool cache_is_updated(struct nft_cache *cache, uint16_t genid)
+static bool nft_cache_is_updated(struct nft_cache *cache, uint16_t genid)
{
return genid && genid == cache->genid;
}
-bool cache_needs_update(struct nft_cache *cache)
+bool nft_cache_needs_update(struct nft_cache *cache)
{
return cache->flags & NFT_CACHE_UPDATE;
}
-int cache_update(struct nft_ctx *nft, unsigned int flags, struct list_head *msgs)
+int nft_cache_update(struct nft_ctx *nft, unsigned int flags,
+ struct list_head *msgs)
{
struct netlink_ctx ctx = {
.list = LIST_HEAD_INIT(ctx.list),
replay:
ctx.seqnum = cache->seqnum++;
genid = mnl_genid_get(&ctx);
- if (!cache_needs_refresh(cache) &&
- cache_is_complete(cache, flags) &&
- cache_is_updated(cache, genid))
+ if (!nft_cache_needs_refresh(cache) &&
+ nft_cache_is_complete(cache, flags) &&
+ nft_cache_is_updated(cache, genid))
return 0;
if (cache->genid)
- cache_release(cache);
+ nft_cache_release(cache);
if (flags & NFT_CACHE_FLUSHED) {
oldflags = flags;
goto skip;
}
- ret = cache_init(&ctx, flags);
+ ret = nft_cache_init(&ctx, flags);
if (ret < 0) {
- cache_release(cache);
+ nft_cache_release(cache);
if (errno == EINTR)
goto replay;
genid_stop = mnl_genid_get(&ctx);
if (genid != genid_stop) {
- cache_release(cache);
+ nft_cache_release(cache);
goto replay;
}
skip:
return 0;
}
-static void __cache_flush(struct list_head *table_list)
+static void nft_cache_flush(struct list_head *table_list)
{
struct table *table, *next;
}
}
-void cache_release(struct nft_cache *cache)
+void nft_cache_release(struct nft_cache *cache)
{
- __cache_flush(&cache->list);
+ nft_cache_flush(&cache->list);
cache->genid = 0;
cache->flags = NFT_CACHE_EMPTY;
}
const struct table *table;
struct chain *chain;
- if (cache_update(ctx->nft, flags, ctx->msgs) < 0)
+ if (nft_cache_update(ctx->nft, flags, ctx->msgs) < 0)
return 0;
table = table_lookup_fuzzy(&cmd->handle, &ctx->nft->cache);
return -1;
}
- if (cache_needs_update(&ctx->nft->cache))
+ if (nft_cache_needs_update(&ctx->nft->cache))
return rule_cache_update(ctx, op);
return 0;
exit_cookie(&ctx->output.output_cookie);
exit_cookie(&ctx->output.error_cookie);
iface_cache_release();
- cache_release(&ctx->cache);
+ nft_cache_release(&ctx->cache);
nft_ctx_clear_include_paths(ctx);
scope_free(ctx->top_scope);
xfree(ctx->state);
unsigned int flags;
struct cmd *cmd;
- flags = cache_evaluate(nft, cmds);
- if (cache_update(nft, flags, msgs) < 0)
+ flags = nft_cache_evaluate(nft, cmds);
+ if (nft_cache_update(nft, flags, msgs) < 0)
return -1;
list_for_each_entry(cmd, cmds, list) {
nft_output_echo(&nft->output))
json_print_echo(nft);
if (rc)
- cache_release(&nft->cache);
+ nft_cache_release(&nft->cache);
return rc;
}
nft_output_echo(&nft->output))
json_print_echo(nft);
if (rc)
- cache_release(&nft->cache);
+ nft_cache_release(&nft->cache);
return rc;
}