netfilter: nf_tables: avoid chain re-validation if possible
Subsystems (2)
Florian Westphal
maintainerPATCH 1/1 netfilter: nf_tables: avoid chain re-validation if possible
Consider:
input -> j2 -> j3
input -> j2 -> j3
input -> j1 -> j2 -> j3
Then the second rule does not need to revalidate j2, and, by extension j3. We need to validate it only for rule 3.
This is needed because chain loop detection also ensures we do not exceed the jump stack: Just because we know that j2 is cycle free, its last jump might now exceed the allowed stack. We also need to update the new largest call depth for all the reachable nodes.
Care has to be taken to revalidate even if the chain depth won't be an issue, as the chain validation also ensures that expressions are not called from invalid context (e.g., masquerade from a filter chain or NAT prerouting hook).
Therefore we also need to stash the base chain context (type, hooknum) and revalidate if the chain became reachable from a different hook or type.
---
Not urgent, can be deferred to next cycle.
include/net/netfilter/nf_tables.h | 30 +++++++++---- net/netfilter/nf_tables_api.c | 70 +++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 12 deletions(-)
include/net/netfilter/nf_tables.h +22 -8
@@ -1091,6 +1091,26 @@ struct nft_rule_blob { __attribute__((aligned(__alignof__(struct nft_rule_dp)))); }; +enum nft_chain_types { +NFT_CHAIN_T_DEFAULT =0 , +NFT_CHAIN_T_ROUTE , +NFT_CHAIN_T_NAT , +NFT_CHAIN_T_MAX +}; + +/** +* struct nft_chain_validate_state - cache validation state +* +* @depth: chain was validated for call level <= depth +* @basetype_mask: chain receives packets from basetypes +* @hook_mask: chain receives packets from hook numbers +*/ +struct nft_chain_validate_state { +u8 basetype_mask; +u8 hook_mask[NFT_CHAIN_T_MAX ]; +u8 depth; +}; +/** * struct nft_chain - nf_tables chain *
@@ -1128,9 +1148,10 @@ struct nft_chain {/* Only used during control plane commit phase: */ struct nft_rule_blob *blob_next; +struct nft_chain_validate_state vstate; }; -int nft_chain_validate (const struct nft_ctx *ctx,const struct nft_chain *chain); +int nft_chain_validate (const struct nft_ctx *ctx,struct nft_chain *chain);int nft_setelem_validate (const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter,struct nft_elem_priv *elem_priv);
@@ -1138,13 +1159,6 @@ int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);int nf_tables_bind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);void nf_tables_unbind_chain (const struct nft_ctx *ctx,struct nft_chain *chain); -enum nft_chain_types { -NFT_CHAIN_T_DEFAULT =0 , -NFT_CHAIN_T_ROUTE , -NFT_CHAIN_T_NAT , -NFT_CHAIN_T_MAX -}; -/** * struct nft_chain_type - nf_tables chain type info *
net/netfilter/nf_tables_api.c +66 -4
118 lines not on this page · raw message
Signature · 1 line
2.51.2
Quoted 13 lines · Florian Westphal
Consider:
input -> j2 -> j3
input -> j2 -> j3
input -> j1 -> j2 -> j3Then the second rule does not need to revalidate j2, and, by extension j3. We need to validate it only for rule 3.
This is needed because chain loop detection also ensures we do not exceed the jump stack: Just because we know that j2 is cycle free, its last jump might now exceed the allowed stack. We also need to update the new largest call depth for all the reachable nodes.
Quoted 7 lines
Care has to be taken to revalidate even if the chain depth won't be an issue, as the chain validation also ensures that expressions are not called from invalid context (e.g., masquerade from a filter chain or NAT prerouting hook).
Therefore we also need to stash the base chain context (type, hooknum) and revalidate if the chain became reachable from a different hook or type.
The issue is reproducible with this version of the patch applied, unless I make the following change:
net/netfilter/nf_tables_api.c +2 -8
@@ -4145,14 +4145,8 @@ int nft_chain_validate(const struct nft_ctx *ctx, struct nft_chain *chain)if (ctx->level ==NFT_JUMP_STACK_SIZE )return -EMLINK ; -if (ctx->level >0 ) { -/* jumps to base chains are not allowed. */ -if (nft_is_base_chain (chain)) -return -ELOOP ; - -if (nft_chain_vstate_valid (ctx, chain)) -return 0 ; - } +if (ctx->level &&nft_chain_vstate_valid (ctx, chain)) +return 0 ;list_for_each_entry (rule, &chain->rules, list) {if (fatal_signal_pending (current))
It is also worth noting that I'm still seeing the cpu usage spike up to 100% for a couple of seconds (attributed to an iptables process) with this version of the patch (even with the above change), while the previous rendition seemd to have resolved that.
Hamza
Florian Westphal
maintainerQuoted 11 lines · Hamza Mahfooz
The issue is reproducible with this version of the patch applied, unless I make the following change:
net/netfilter/nf_tables_api.c +0
-0@@ -4145,14 +4145,8 @@ int nft_chain_validate(const struct nft_ctx *ctx, struct nft_chain *chain)if (ctx->level ==NFT_JUMP_STACK_SIZE )return -EMLINK ;
net/netfilter/nf_tables_api.c int nft_chain_validate(const struct nft_ctx *ctx, struct nft_chain *chain) · line 4149
Quoted 10 lines
net/netfilter/nf_tables_api.c +2
-8-if (ctx->level >0 ) { -/* jumps to base chains are not allowed. */ -if (nft_is_base_chain (chain)) -return -ELOOP ; - -if (nft_chain_vstate_valid (ctx, chain)) -return 0 ; - } +if (ctx->level &&nft_chain_vstate_valid (ctx, chain)) +return 0 ;
Looks like a placebo change to me?
Also, the nft_is_base_chain(chain) check is required.
Quoted 4 lines
It is also worth noting that I'm still seeing the cpu usage spike up to 100% for a couple of seconds (attributed to an iptables process) with this version of the patch (even with the above change), while the previous rendition seemd to have resolved that.
The previous version makes illegal shortcuts (as in, not validating when it has to), it cannot be applied.
That said, I have flagged this patch as deferred anyway, there are too many conflicting changes flying around.
I'll resubmit in a few weeks when -next opens up again.
Hamza Mahfooz
Tested-byQuoted 3 lines · Florian Westphal
Looks like a placebo change to me?
Also, thenft_is_base_chain(chain)check is required.
Ya, seems like the delta was due to variances in test runs. Sorry about the noise.
Quoted 8 lines
The previous version makes illegal shortcuts (as in, not validating when it has to), it cannot be applied.
That said, I have flagged this patch as deferred anyway, there are too many conflicting changes flying around.
I'll resubmit in a few weeks when -next opens up again.
Makes sense, in any case this patch significantly reduces the soft lockup rate, so feel free to add:
When you get around to resubmitting it after the merge window closes.
Hamza
Florian Westphal
maintainerQuoted 7 lines · Hamza Mahfooz
Quoted 1 line · Florian Westphal
I'll resubmit in a few weeks when -next opens up again.
Makes sense, in any case this patch significantly reduces the soft lockup rate, so feel free to add:
Tested-by Hamza Mahfooz <[email protected]>
Thanks for testing!
Now that you mention this: you could try to revert314c82841602 ("netfilter: nf_tables: can't schedule in nft_chain_validate") to get rid of all softlockup warnings:
As of a60a5abe19d6 ("netfilter: nf_tables: allow iter callbacks to sleep") the iterator-cant-schedule no longer applies.
If i'm right you could submit a bug fix revert for nf tree.
Thanks!