Skip to contentSkip to conversation

cache: check for NFT_CACHE_REFRESH in current requested cache

PATCH Superseded Pablo Neira Ayuso 1 participant 2 messages May 27, 2024 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 cache: check for NFT_CACHE_REFRESH in current requested cache

NFT_CACHE_REFRESH is set on inconditionally by ruleset listing commands to deal with stateful information in this ruleset. This flag results in dropping the existing cache and fully fetching all objects from the kernel.

Check if this flag is requested by the current list of commands, instead of checking at cache->flags which represents the cache after the _previous_ list of commands.

Fixes 407c54f71255 ("src: cache gets out of sync in interactive mode")
---
 src/cache.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/cache.c +3 -3
index c000e32c497f..7cc84d714b08 100644
@@ -1177,9 +1177,9 @@ static bool nft_cache_is_complete(struct nft_cache *cache, unsigned int flags)
 	return (cache->flags & flags) == flags;
 }
 
-static bool nft_cache_needs_refresh(struct nft_cache *cache)
+static bool nft_cache_needs_refresh(unsigned int flags)
 {
-	return cache->flags & NFT_CACHE_REFRESH;
+	return flags & NFT_CACHE_REFRESH;
 }
 
 static bool nft_cache_is_updated(struct nft_cache *cache, uint16_t genid)
@@ -1207,7 +1207,7 @@ int nft_cache_update(struct nft_ctx *nft, unsigned int flags,
 replay:
 	ctx.seqnum = cache->seqnum++;
 	genid = mnl_genid_get(&ctx);
-	if (!nft_cache_needs_refresh(cache) &&
+	if (!nft_cache_needs_refresh(flags) &&
 	    nft_cache_is_complete(cache, flags) &&
 	    nft_cache_is_updated(cache, genid))
 		return 0;
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 cache: recycle existing cache with incremental updates

Cache tracking has improved over time by incrementally adding/deleting objects when evaluating commands that are going to be sent to the kernel.

nft_cache_is_complete() already checks that the cache contains objects that are required to handle this batch of commands by comparing cache flags.

Infer from the current generation ID if no other transaction has invalidated the existing cache, this allows to skip unnecessary cache flush then refill situations which slow down incremental updates.

---
 src/cache.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
src/cache.c +15 -3
index 7cc84d714b08..2cfc4af7280e 100644
@@ -1182,9 +1182,21 @@ static bool nft_cache_needs_refresh(unsigned int flags)
 	return flags & NFT_CACHE_REFRESH;
 }
 
-static bool nft_cache_is_updated(struct nft_cache *cache, uint16_t genid)
+static bool nft_cache_is_updated(struct nft_cache *cache, unsigned int flags,
+				 uint16_t genid)
 {
-	return genid && genid == cache->genid;
+	if (!genid)
+		return false;
+
+	if (genid == cache->genid)
+		return true;
+
+	if (genid == cache->genid + 1) {
+		cache->genid++;
+		return true;
+	}
+
+	return false;
 }
 
 bool nft_cache_needs_update(struct nft_cache *cache)
@@ -1209,7 +1221,7 @@ replay:
 	genid = mnl_genid_get(&ctx);
 	if (!nft_cache_needs_refresh(flags) &&
 	    nft_cache_is_complete(cache, flags) &&
-	    nft_cache_is_updated(cache, genid))
+	    nft_cache_is_updated(cache, flags, genid))
 		return 0;
 
 	if (cache->genid)
Signature · 1 line
2.30.2