From ea8bb5100a69d1fd39cf737e3bf3acd6631a10f3 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Mon, 27 Apr 2020 12:08:59 +0200 Subject: nft: cache: Optimize caching for flush command When flushing all chains and verbose mode is not enabled, nft_rule_flush() uses a shortcut: It doesn't specify a chain name for NFT_MSG_DELRULE, so the kernel will flush all existing chains without user space needing to know which they are. The above allows to avoid a chain cache, but there's a caveat: nft_xt_builtin_init() will create base chains as it assumes they are missing and thereby possibly overrides any non-default chain policies. Solve this by making nft_xt_builtin_init() cache-aware: If a command doesn't need a chain cache, there's no need to bother with creating any non-existing builtin chains, either. For the sake of completeness, also do nothing if cache is not initialized (although that shouldn't happen). Signed-off-by: Phil Sutter --- iptables/nft.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'iptables/nft.c') diff --git a/iptables/nft.c b/iptables/nft.c index b807de88..b505d70a 100644 --- a/iptables/nft.c +++ b/iptables/nft.c @@ -737,6 +737,9 @@ static int nft_xt_builtin_init(struct nft_handle *h, const char *table) { const struct builtin_table *t; + if (!h->cache_init) + return 0; + t = nft_table_builtin_find(h, table); if (t == NULL) return -1; @@ -747,6 +750,9 @@ static int nft_xt_builtin_init(struct nft_handle *h, const char *table) if (nft_table_builtin_add(h, t) < 0) return -1; + if (h->cache_req.level < NFT_CL_CHAINS) + return 0; + nft_chain_builtin_init(h, t); h->cache->table[t->type].initialized = true; -- cgit v1.2.3