relax cache requirements, speed up incremental updates
Pablo Neira Ayuso
maintainercover relax cache requirements, speed up incremental updates
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is a new attempt to speed up incremental updates following a different approach, after reverting:
e791dbe109b6 ("cache: recycle existing cache with incremental updates")
which is fragile because cache consistency checking needs more, it should be still possible to explore in the future, but this seems a more simple approach at this stage.
This is passing tests/shell and tests/py.
Pablo Neira Ayuso (5):
cache: rule by index requires full cache
cache: populate chains on demand from error path
cache: populate objecs on demand from error path
cache: populate flowtable on demand from error path
cache: do not fetch set inconditionally on delete
include/cache.h | 1 - src/cache.c | 23 ++++++----------------- src/cmd.c | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 18 deletions(-)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 1/5 cache: rule by index requires full cache
In preparation for on-demand cache population with errors, set on NFT_CACHE_FULL if rule index is used since this requires a full cache with rules.
This is not a fix, follow up patches relax cache requirements, add this patch in first place to make sure index does not break.
--- src/cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/cache.c +1 -1
@@ -68,7 +68,7 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)if (cmd->handle.index.id || cmd->handle.position.id) - flags |=NFT_CACHE_RULE |NFT_CACHE_UPDATE ; + flags |=NFT_CACHE_FULL |NFT_CACHE_UPDATE ;break ;default :break ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/5 cache: populate chains on demand from error path
In reply to Pablo Neira AyusoUpdates on verdict maps that require many non-base chains are slowed down due to fetching existing non-base chains into the cache.
Chains are only required for error reporting hints if kernel reports ENOENT. Populate the cache from this error path only.
Similar approach already exists from rule ENOENT error path since:
deb7c5927fad ("cmd: add misspelling suggestions for rule commands")
however, NFT_CACHE_CHAIN was toggled inconditionally for rule commands, rendering this on-demand cache population useless.
before this patch, running Neels' nft_slew benchmark (peak values):
created idx 4992 in 52587950 ns (128 in 7122 ms)
...
deleted idx 128 in 43542500 ns (127 in 6187 ms)
after this patch:
created idx 4992 in 11361299 ns (128 in 1612 ms)
...
deleted idx 1664 in 5239633 ns (128 in 733 ms)
---
v2: fetch cache for EOPNOTSUPP errors too.
update nft_slew results.
include/cache.h | 1 - src/cache.c | 4 ---- src/cmd.c | 11 +++++++++++ 3 files changed, 11 insertions(+), 5 deletions(-)
include/cache.h +0 -1
@@ -31,7 +31,6 @@ enum cache_level_flags {NFT_CACHE_SET_BIT |NFT_CACHE_SETELEM_BIT ,NFT_CACHE_RULE =NFT_CACHE_TABLE_BIT | -NFT_CACHE_CHAIN_BIT |NFT_CACHE_RULE_BIT ,NFT_CACHE_FULL = __NFT_CACHE_MAX_BIT -1 , NFT_CACHE_TERSE = (1 << 27),
src/cache.c +0 -4
@@ -30,7 +30,6 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)break ; flags |=NFT_CACHE_TABLE | -NFT_CACHE_CHAIN |NFT_CACHE_SET |NFT_CACHE_OBJECT |NFT_CACHE_FLOWTABLE ;
@@ -54,14 +53,12 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)break ;case CMD_OBJ_ELEMENTS : flags |=NFT_CACHE_TABLE | -NFT_CACHE_CHAIN |NFT_CACHE_SET |NFT_CACHE_OBJECT |NFT_CACHE_SETELEM_MAYBE ;break ;case CMD_OBJ_RULE : flags |=NFT_CACHE_TABLE | -NFT_CACHE_CHAIN |NFT_CACHE_SET |NFT_CACHE_OBJECT |NFT_CACHE_FLOWTABLE ;
@@ -435,7 +432,6 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,case CMD_DELETE :case CMD_DESTROY : flags |=NFT_CACHE_TABLE | -NFT_CACHE_CHAIN |NFT_CACHE_SET |NFT_CACHE_FLOWTABLE |NFT_CACHE_OBJECT ;
src/cmd.c +11 -0
@@ -75,6 +75,10 @@ static int nft_cmd_enoent_chain(struct netlink_ctx *ctx, const struct cmd *cmd,if (!cmd->handle.chain.name)return 0 ; +if (nft_cache_update (ctx->nft,NFT_CACHE_TABLE |NFT_CACHE_CHAIN , + ctx->msgs,NULL ) <0 ) +return 0 ; + chain =chain_lookup_fuzzy (&cmd->handle, &ctx->nft->cache, &table);/* check table first. */ if (!table)
@@ -271,6 +275,13 @@ static int nft_cmd_chain_error(struct netlink_ctx *ctx, struct cmd *cmd,return netlink_io_error (ctx, &chain->priority.loc,"Chains of type \"nat\" must have a priority value above -200" ); +if (nft_cache_update (ctx->nft,NFT_CACHE_TABLE |NFT_CACHE_CHAIN , + ctx->msgs,NULL ) <0 ) { +return netlink_io_error (ctx, &chain->loc, +"Chain of type \"%s\" is not supported, perhaps kernel support is missing?" , + chain->type.str); + } + table =table_cache_find (&ctx->nft->cache.table_cache, cmd->handle.table.name, cmd->handle.family);if (table) {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/5 cache: populate objecs on demand from error path
In reply to Pablo Neira AyusoObjects are only required for error reporting hints if kernel reports ENOENT. Populate the cache from this error path only.
--- src/cache.c | 6 +----- src/cmd.c | 4 ++++ 2 files changed, 5 insertions(+), 5 deletions(-)
src/cache.c +1 -5
@@ -31,7 +31,6 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags) flags |=NFT_CACHE_TABLE |NFT_CACHE_SET | -NFT_CACHE_OBJECT |NFT_CACHE_FLOWTABLE ;list_for_each_entry (set, &cmd->table->sets, list) {if (set->automerge)
@@ -54,13 +53,11 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)case CMD_OBJ_ELEMENTS : flags |=NFT_CACHE_TABLE |NFT_CACHE_SET | -NFT_CACHE_OBJECT |NFT_CACHE_SETELEM_MAYBE ;break ;case CMD_OBJ_RULE : flags |=NFT_CACHE_TABLE |NFT_CACHE_SET | -NFT_CACHE_OBJECT |NFT_CACHE_FLOWTABLE ; if (cmd->handle.index.id ||
@@ -433,8 +430,7 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,case CMD_DESTROY : flags |=NFT_CACHE_TABLE |NFT_CACHE_SET | -NFT_CACHE_FLOWTABLE | -NFT_CACHE_OBJECT ; +NFT_CACHE_FLOWTABLE ; flags =evaluate_cache_del (cmd, flags);break ;
src/cmd.c +4 -0
@@ -169,6 +169,10 @@ static int nft_cmd_enoent_obj(struct netlink_ctx *ctx, const struct cmd *cmd,if (!cmd->handle.obj.name)return 0 ; +if (nft_cache_update (ctx->nft,NFT_CACHE_TABLE |NFT_CACHE_OBJECT , + ctx->msgs,NULL ) <0 ) +return 0 ; + obj =obj_lookup_fuzzy (cmd->handle.obj.name, &ctx->nft->cache, &table);/* check table first. */ if (!table)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/5 cache: populate flowtable on demand from error path
In reply to Pablo Neira AyusoFlowtables are only required for error reporting hints if kernel reports ENOENT. Populate the cache from this error path only.
--- src/cache.c | 9 +++------ src/cmd.c | 4 ++++ 2 files changed, 7 insertions(+), 6 deletions(-)
src/cache.c +3 -6
@@ -30,8 +30,7 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)break ; flags |=NFT_CACHE_TABLE | -NFT_CACHE_SET | -NFT_CACHE_FLOWTABLE ; +NFT_CACHE_SET ;list_for_each_entry (set, &cmd->table->sets, list) {if (set->automerge) flags |=NFT_CACHE_SETELEM_MAYBE ;
@@ -57,8 +56,7 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)break ;case CMD_OBJ_RULE : flags |=NFT_CACHE_TABLE | -NFT_CACHE_SET | -NFT_CACHE_FLOWTABLE ; +NFT_CACHE_SET ;if (cmd->handle.index.id || cmd->handle.position.id)
@@ -429,8 +427,7 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,case CMD_DELETE :case CMD_DESTROY : flags |=NFT_CACHE_TABLE | -NFT_CACHE_SET | -NFT_CACHE_FLOWTABLE ; +NFT_CACHE_SET ; flags =evaluate_cache_del (cmd, flags);break ;
src/cmd.c +4 -0
@@ -201,6 +201,10 @@ static int nft_cmd_enoent_flowtable(struct netlink_ctx *ctx,if (!cmd->handle.flowtable.name)return 0 ; +if (nft_cache_update (ctx->nft,NFT_CACHE_TABLE |NFT_CACHE_FLOWTABLE , + ctx->msgs,NULL ) <0 ) +return 0 ; + ft =flowtable_lookup_fuzzy (cmd->handle.flowtable.name, &ctx->nft->cache, &table);/* check table first. */
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/5 cache: do not fetch set inconditionally on delete
In reply to Pablo Neira AyusoThis is only required to remove elements, relax cache requirements for anything else.
--- src/cache.c | 6 +++--- src/cmd.c | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-)
src/cache.c +3 -3
@@ -73,7 +73,8 @@ static unsigned int evaluate_cache_del(struct cmd *cmd, unsigned int flags) {switch (cmd->obj) {case CMD_OBJ_ELEMENTS : - flags |=NFT_CACHE_SETELEM_MAYBE ; + flags |=NFT_CACHE_SET | +NFT_CACHE_SETELEM_MAYBE ;break ;default :break ;
@@ -426,8 +427,7 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,break ;case CMD_DELETE :case CMD_DESTROY : - flags |=NFT_CACHE_TABLE | -NFT_CACHE_SET ; + flags |=NFT_CACHE_TABLE ; flags =evaluate_cache_del (cmd, flags);break ;
src/cmd.c +4 -0
@@ -140,6 +140,10 @@ static int nft_cmd_enoent_set(struct netlink_ctx *ctx, const struct cmd *cmd,if (!cmd->handle.set.name)return 0 ; +if (nft_cache_update (ctx->nft,NFT_CACHE_TABLE |NFT_CACHE_SET , + ctx->msgs,NULL ) <0 ) +return 0 ; + set =set_lookup_fuzzy (cmd->handle.set.name, &ctx->nft->cache, &table);/* check table first. */ if (!table)
Signature · 1 line
2.30.2
Phil Sutter
reviewerQuoted 4 lines · Pablo Neira Ayuso
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is nice as it applies to error path only, though the second cache fetch is prone to race conditions. Did you consider retrying the whole transaction with beefed-up cache in error case? I was about to mention how it nicely integrates with transaction refresh in ERESTART case, but then realized this is iptables code and nft doesn't retry in that case?!
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 8 lines · Phil Sutter
Quoted 4 lines · Pablo Neira Ayuso
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is nice as it applies to error path only, though the second cache fetch is prone to race conditions.
The call to nft_cache_update() ensures cache is consistent, old cache is dropped and a new consistent cache is obtained. The hint could be misleading (worst case) though since the cache could have different generation ID that the transaction itself, but it is just a hint.
Quoted 2 lines
Did you consider retrying the whole transaction with beefed-up cache in error case?
Why retry? I am assuming a batch where the user made a mistake, retry will fail again.
Quoted 3 lines
I was about to mention how it nicely integrates with transaction refresh in ERESTART case, but then realized this is iptables code and nft doesn't retry in that case?!
I think you are talking about different scenario, that is, userspace sends an update but generation ID mismatches, kernel reports ERESTART and nftables revamps, this is to catch an interference with another process, that needs to be done in nft, but it is a different issue.
Phil Sutter
reviewerQuoted 21 lines · Pablo Neira Ayuso
Quoted 8 lines · Phil Sutter
Quoted 4 lines · Pablo Neira Ayuso
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is nice as it applies to error path only, though the second cache fetch is prone to race conditions.
The call to
nft_cache_update()ensures cache is consistent, old cache is dropped and a new consistent cache is obtained. The hint could be misleading (worst case) though since the cache could have different generation ID that the transaction itself, but it is just a hint.Quoted 2 lines
Did you consider retrying the whole transaction with beefed-up cache in error case?
Why retry? I am assuming a batch where the user made a mistake, retry will fail again.
Quoted 8 lines
Quoted 3 lines
I was about to mention how it nicely integrates with transaction refresh in ERESTART case, but then realized this is iptables code and nft doesn't retry in that case?!
I think you are talking about different scenario, that is, userspace sends an update but generation ID mismatches, kernel reports ERESTART and nftables revamps, this is to catch an interference with another process, that needs to be done in nft, but it is a different issue.
Yes, I had incorrect error reporting in mind: Kernel reports ENOENT for a chain which another process creates concurrently. The error path cache update fetches the newly created chain and error reporting suggests to use the exact chain user specified (I assume). It is indeed a corner-case issue, though.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 31 lines · Phil Sutter
Quoted 21 lines · Pablo Neira Ayuso
Quoted 8 lines · Phil Sutter
Quoted 4 lines · Pablo Neira Ayuso
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is nice as it applies to error path only, though the second cache fetch is prone to race conditions.
The call to
nft_cache_update()ensures cache is consistent, old cache is dropped and a new consistent cache is obtained. The hint could be misleading (worst case) though since the cache could have different generation ID that the transaction itself, but it is just a hint.Quoted 2 lines
Did you consider retrying the whole transaction with beefed-up cache in error case?
Why retry? I am assuming a batch where the user made a mistake, retry will fail again.
Quoted 8 lines
Quoted 3 lines
I was about to mention how it nicely integrates with transaction refresh in ERESTART case, but then realized this is iptables code and nft doesn't retry in that case?!
I think you are talking about different scenario, that is, userspace sends an update but generation ID mismatches, kernel reports ERESTART and nftables revamps, this is to catch an interference with another process, that needs to be done in nft, but it is a different issue.
Quoted 4 lines
Yes, I had incorrect error reporting in mind: Kernel reports ENOENT for a chain which another process creates concurrently. The error path cache update fetches the newly created chain and error reporting suggests to use the exact chain user specified (I assume).
IIRC, the fuzzy match code skips exact matches, worst case can be a very hint.
Quoted 1 line
It is indeed a corner-case issue, though.
ERESTART handling can be useful for your rule index feature, where consistency is fundamental to ensure that rule is added where the user really wants.
Quoted 23 lines · Pablo Neira Ayuso
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is a new attempt to speed up incremental updates following a different approach, after reverting:
e791dbe109b6("cache: recycle existing cache with incremental updates")which is fragile because cache consistency checking needs more, it should be still possible to explore in the future, but this seems a more simple approach at this stage.
This is passing tests/shell and tests/py.
Pablo Neira Ayuso (5):
cache: rule by index requires full cache
cache: populate chains on demand from error path
cache: populate objecs on demand from error path
cache: populate flowtable on demand from error path
cache: do not fetch set inconditionally on delete
Quoted 4 lines
include/cache.h | 1 - src/cache.c | 23 ++++++----------------- src/cmd.c | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 18 deletions(-)
I applied this series to nft master and tested it against the latest net-next and RHEL-9 kernels. No issues or regressions found.
Thanks Pablo!
Pablo Neira Ayuso
maintainerQuoted 24 lines · Eric Garver
Quoted 23 lines · Pablo Neira Ayuso
Hi,
The following patchset relaxes cache requirements, this is based on the observation that objects are fetched to report errors and provide hints.
This is a new attempt to speed up incremental updates following a different approach, after reverting:
e791dbe109b6("cache: recycle existing cache with incremental updates")which is fragile because cache consistency checking needs more, it should be still possible to explore in the future, but this seems a more simple approach at this stage.
This is passing tests/shell and tests/py.
Pablo Neira Ayuso (5):
cache: rule by index requires full cache
cache: populate chains on demand from error path
cache: populate objecs on demand from error path
cache: populate flowtable on demand from error path
cache: do not fetch set inconditionally on delete
Quoted 7 lines
Quoted 4 lines
include/cache.h | 1 - src/cache.c | 23 ++++++----------------- src/cmd.c | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 18 deletions(-)I applied this series to nft master and tested it against the latest net-next and RHEL-9 kernels. No issues or regressions found.
Pushed out, thanks for testing.