netfilter: flowtable: Preliminary fixes
Subsystems (4)
cover netfilter: flowtable: Preliminary fixes
This patch series was created based upon the Sashiko review of the Broadcom Flow Accelerator support:
https://lore.kernel.org/all/[email protected]/
These changes were not encountered in real world testing but appear to be valid. Test cases were added to ensure there are no regressions moving forward.
Florian Fainelli (4):
netfilter: flowtable: do not queue offload work after DESTROY
netfilter: flowtable: restore the GRE conntrack timeout on teardown
selftests: netfilter: check hardware offload work after DESTROY
selftests: netfilter: check the GRE timeout after flowtable teardown
net/netfilter/nf_flow_table_core.c | 12 ++ net/netfilter/nf_flow_table_offload.c | 13 ++ .../testing/selftests/net/netfilter/Makefile | 2 + tools/testing/selftests/net/netfilter/config | 4 + .../net/netfilter/nft_flowtable_gre.sh | 137 +++++++++++++++ .../net/netfilter/nft_flowtable_hw_destroy.sh | 162 ++++++++++++++++++ 6 files changed, 330 insertions(+) create mode 100755 tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh create mode 100755 tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh
Signature · 1 line
2.53.0
PATCH 1/4 netfilter: flowtable: do not queue offload work after DESTROY
nf_flow_offload_refresh() can be reached by a packet path reader which looked the flow up before it was torn down. NF_FLOW_PENDING keeps the DESTROY work from being queued while another work item is pending, but once the DESTROY work has run and cleared the bit, a reader can take it again and queue a REPLACE (or STATS) work for a flow which is already being removed.
GC removes a hardware flow after NF_FLOW_HW_DEAD is set, and frees it after an RCU grace period, which the reader can outlive in a preemptible RCU read-side section. The queued work then dereferences a freed flow and the flowtable, and can program the hardware again with a rule keyed by the freed flow's cookie.
NF_FLOW_HW_DYING is set when DESTROY is queued, and DESTROY needs NF_FLOW_PENDING, so a work item which holds the pending bit and finds NF_FLOW_HW_DYING clear prevents DESTROY from being queued until it completes. Check it after taking the pending bit, and do not queue anything other than DESTROY when it is set.
Not reproduced: this is a race between a long RCU read-side section and the GC and offload workers. Found by review of a related change.
--- net/netfilter/nf_flow_table_offload.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
net/netfilter/nf_flow_table_offload.c +13 -0
@@ -1088,6 +1088,19 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,if (test_and_set_bit (NF_FLOW_PENDING , &flow->flags))return NULL ; +/* GC only removes a hardware flow after its DESTROY work has run, and +* NF_FLOW_PENDING keeps another one from being queued, so nothing can +* free the flow while this work is pending, unless DESTROY was queued +* before we took the pending bit. A reader which found the flow before +* it was torn down must not queue more work then, as the flow could be +* freed before that work runs. +*/ +if (cmd !=FLOW_CLS_DESTROY && +test_bit (NF_FLOW_HW_DYING , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +return NULL ; + } + offload = kmalloc_obj(struct flow_offload_work, GFP_ATOMIC);if (!offload) {clear_bit (NF_FLOW_PENDING , &flow->flags);
Signature · 1 line
2.53.0
Pablo Neira Ayuso
maintainerRe: [PATCH net 1/4] netfilter: flowtable: do not queue offload work after DESTROY
Quoted 38 lines · Florian Fainelli
nf_flow_offload_refresh()can be reached by a packet path reader which looked the flow up before it was torn down.NF_FLOW_PENDINGkeeps the DESTROY work from being queued while another work item is pending, but once the DESTROY work has run and cleared the bit, a reader can take it again and queue a REPLACE (or STATS) work for a flow which is already being removed.GC removes a hardware flow after
NF_FLOW_HW_DEADis set, and frees it after an RCU grace period, which the reader can outlive in a preemptible RCU read-side section. The queued work then dereferences a freed flow and the flowtable, and can program the hardware again with a rule keyed by the freed flow's cookie.
NF_FLOW_HW_DYINGis set when DESTROY is queued, and DESTROY needsNF_FLOW_PENDING, so a work item which holds the pending bit and findsNF_FLOW_HW_DYINGclear prevents DESTROY from being queued until it completes. Check it after taking the pending bit, and do not queue anything other than DESTROY when it is set.Not reproduced: this is a race between a long RCU read-side section and the GC and offload workers. Found by review of a related change.
Fixes c29f74e0df7a ("netfilter: nf_flow_table: hardware offload support")Signed-off-by Florian Fainelli <[email protected]>--- net/netfilter/nf_flow_table_offload.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)net/netfilter/nf_flow_table_offload.c +2
-0@@ -1088,6 +1088,19 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,if (test_and_set_bit (NF_FLOW_PENDING , &flow->flags))return NULL ; +/* GC only removes a hardware flow after its DESTROY work has run, and +* NF_FLOW_PENDING keeps another one from being queued, so nothing can
net/netfilter/nf_flow_table_offload.c nf_flow_offload_work_alloc(struct nf_flowtable *flowtable, · line 1102
Quoted 10 lines
net/netfilter/nf_flow_table_offload.c +10
-0+* free the flow while this work is pending, unless DESTROY was queued +* before we took the pending bit. A reader which found the flow before +* it was torn down must not queue more work then, as the flow could be +* freed before that work runs. +*/ +if (cmd !=FLOW_CLS_DESTROY && +test_bit (NF_FLOW_HW_DYING , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +return NULL ; + }
Maybe address by checking for the _CLOSING + _TEARDOWN flag? If those are set on, then hardware will be scheduled for removal soon.
I guess there is also another hypothetical case where _CLOSING is set on, but HW_DYING was not set on yet?
Probably the HW_DYING bit can go away, _CLOSING was added for hw offload specifically, to schedule the removal in hardware while the flow still remains with IPS_OFFLOAD.
net/netfilter/nf_flow_table_offload.c +2 -1
@@ -1132,7 +1132,8 @@ void nf_flow_offload_refresh(struct nf_flowtable *flowtable,if (!offload)return ; -if (test_bit (NF_FLOW_TEARDOWN , &flow->flags)) { +if (test_bit (NF_FLOW_CLOSING , &flow->flags) || +test_bit (NF_FLOW_TEARDOWN , &flow->flags)) {clear_bit (NF_FLOW_PENDING , &flow->flags);kfree (offload);return
Quoted 8 lines
net/netfilter/nf_flow_table_offload.c +1
-0+ offload = kmalloc_obj(struct flow_offload_work, GFP_ATOMIC);if (!offload) {clear_bit (NF_FLOW_PENDING , &flow->flags);Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net 1/4] netfilter: flowtable: do not queue offload work after DESTROY
In reply to Florian FainelliThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The new NF_FLOW_HW_DYING guard in nf_flow_offload_work_alloc() (net/netfilter/nf_flow_table_offload.c) does not close the bug class…
- [Low] The commit message describes the race mechanism and the affected work types imprecisely.
--
netfilter: flowtable: do not queue offload work after DESTROY
This patch makes nf_flow_offload_work_alloc() refuse to queue any work other than FLOW_CLS_DESTROY once NF_FLOW_HW_DYING is set. The goal is to stop a packet path reader from queueing REPLACE work for a flow that GC is already removing, because the offload worker could then use the flow after it has been freed.
Quoted 3 lines
once the DESTROY work has run and cleared the bit, a reader can take it again and queue a REPLACE (or STATS) work for a flow which is already being removed.
[Severity: Low]
Is the STATS part accurate? The packet path only reaches nf_flow_offload_refresh() through flow_offload_refresh(), and that queues FLOW_CLS_REPLACE.
nf_flow_offload_stats() has a single caller, nf_flow_offload_gc_step(), and GC skips any flow that has NF_FLOW_PENDING set:
if (test_bit(NF_FLOW_PENDING, &flow->flags)) return;
GC also never runs concurrently with itself. So STATS work that GC queues cannot race with GC freeing the same flow.
For STATS, the only effect of this patch is that GC stops queueing STATS for CLOSING && HW && DYING flows whose hardware rule DESTROY has already removed.
Quoted 3 lines
GC removes a hardware flow after
NF_FLOW_HW_DEADis set, and frees it after an RCU grace period, which the reader can outlive in a preemptible RCU read-side section.
Could this be worded more precisely? A reader cannot outlive a grace period that started while it was inside its read-side section, because the call_rcu() in flow_offload_free() waits for it.
What outlives the grace period is the REPLACE work that the reader queued on nf_flow_offload_add_wq before rcu_read_unlock().
Quoted 10 lines
net/netfilter/nf_flow_table_offload.c +2
-0@@ -1088,6 +1088,19 @@ nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,if (test_and_set_bit (NF_FLOW_PENDING , &flow->flags))return NULL ; +/* GC only removes a hardware flow after its DESTROY work has run, and +* NF_FLOW_PENDING keeps another one from being queued, so nothing can
net/netfilter/nf_flow_table_offload.c nf_flow_offload_work_alloc(struct nf_flowtable *flowtable, · line 1102
Quoted 10 lines
net/netfilter/nf_flow_table_offload.c +10
-0+* free the flow while this work is pending, unless DESTROY was queued +* before we took the pending bit. A reader which found the flow before +* it was torn down must not queue more work then, as the flow could be +* freed before that work runs. +*/ +if (cmd !=FLOW_CLS_DESTROY && +test_bit (NF_FLOW_HW_DYING , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +return NULL ; + }
[Severity: High]
Does the claim that "nothing can free the flow while this work is pending" hold for flows that GC frees through its software-only branch?
flow_offload_add() publishes both tuples in the rhashtable before nf_flow_offload_add() sets NF_FLOW_HW:
net/netfilter/nf_flow_table_core.c:flow_offload_add() {
...
err = rhashtable_insert_fast(&flow_table->rhashtable,
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
nf_flow_offload_rhash_params);
...
nf_ct_refresh(flow->ct, NF_CT_DAY);
if (nf_flowtable_hw_offload(flow_table))
nf_flow_offload_add(flow_table, flow);
...
}In that window, nf_flow_offload_gc_step() can decide to tear the flow down while NF_FLOW_HW is still clear. The teardown can come from a dying ct, a stale dst, or NF_FLOW_TEARDOWN set by another CPU after a FIN/RST arrives on the already-published REPLY tuple. GC then takes this branch:
if (teardown) {
if (test_bit(NF_FLOW_HW, &flow->flags)) {
...
} else {
flow_offload_del(flow_table, flow);
}That path removes the rhashtable entries, releases the dsts and calls call_rcu() on the flow. It never takes NF_FLOW_PENDING and never sets NF_FLOW_HW_DYING.
The creating CPU then carries on:
CPU1 (creator) CPU2 (GC)flow_offload_add()
rhashtable_insert_fast(REPLY) rhashtable_insert_fast(ORIGINAL)
nf_flow_offload_gc_step()
teardown, NF_FLOW_HW clear flow_offload_del()
flow_offload_free()
call_rcu()
nf_flow_offload_add()
set_bit(NF_FLOW_HW)
nf_flow_offload_refresh()
nf_flow_offload_work_alloc()
test_and_set_bit(NF_FLOW_PENDING) succeeds NF_FLOW_HW_DYING clear, new check passes
queue FLOW_CLS_REPLACE
Suppose the work runs after CPU1 leaves its RCU read-side section and the grace period ends. Would flow_offload_work_handler() then use the freed flow in flow_offload_work_add(), in nf_flow_offload_alloc(), in set_bit(IPS_HW_OFFLOAD_BIT, &flow->ct->status), and in clear_bit(NF_FLOW_PENDING, &offload->flow->flags)?
If the work runs earlier, it seems it would still install a hardware rule for a flow that is no longer in the table. GC would never DESTROY that rule.
That is the same use-after-free and stale rule outcome the commit message describes. As far as I can tell, no later change in this series touches this path.
Would it make sense for GC's non-HW branch to claim NF_FLOW_PENDING, or set a dying marker, before freeing the flow? Another option would be to set NF_FLOW_HW before the flow is published.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118201250.2075007-1-florian.fainelli%40broadcom.com
Pablo Neira Ayuso
maintainerRe: [PATCH net 1/4] netfilter: flowtable: do not queue offload work after DESTROY
Quoted 18 lines · On Sun, Nov 22, 2026 at 09:17:24PM +0000, [email protected] wrote:
The creating CPU then carries on:
CPU1 (creator) CPU2 (GC)
flow_offload_add()
rhashtable_insert_fast(REPLY)rhashtable_insert_fast(ORIGINAL)
nf_flow_offload_gc_step()
teardown,NF_FLOW_HWclearflow_offload_del()
flow_offload_free()
call_rcu()
nf_flow_offload_add()
set_bit(NF_FLOW_HW)
nf_flow_offload_refresh()
nf_flow_offload_work_alloc()
test_and_set_bit(NF_FLOW_PENDING)succeedsNF_FLOW_HW_DYINGclear, new check passes
queueFLOW_CLS_REPLACE
Yes, PENDING needs to be held from flow_offload_add() path for this.
PATCH 2/4 netfilter: flowtable: restore the GRE conntrack timeout on teardown
In reply to Florian Fainelliflow_offload_add() sets the timeout of the conntrack entry to NF_CT_DAY for every offloaded flow, and the GC keeps extending it for GRE too. flow_offload_fixup_ct() only restores the conntrack timeout of TCP and UDP flows when the flow is torn down, so an offloaded GRE entry which does not see any other packet on the slow path stays for up to a day instead of expiring after the GRE conntrack timeout.
Restore the GRE timeout, replied or unreplied, as for UDP. GRE has no offload timeout of its own, flow_offload_get_timeout() uses the default flowtable timeout for it, so use that one when the flow expired.
--- net/netfilter/nf_flow_table_core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
net/netfilter/nf_flow_table_core.c +12 -0
@@ -237,6 +237,18 @@ static void flow_offload_fixup_ct(struct flow_offload *flow) timeout =READ_ONCE (tn->timeouts[state]); expired =nf_flow_has_expired (flow); offload_timeout =READ_ONCE (tn->offload_timeout); +#ifdef CONFIG_NF_CT_PROTO_GRE + }else if (l4num ==IPPROTO_GRE ) { +const struct nf_gre_net *tn =nf_gre_pernet (net); +enum gre_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +GRE_CT_REPLIED :GRE_CT_UNREPLIED ; + + timeout =READ_ONCE (tn->timeouts[state]); + expired =nf_flow_has_expired (flow); +/* GRE has no offload timeout: see flow_offload_get_timeout(). */ + offload_timeout =NF_FLOW_TIMEOUT ; +#endif }else {return ; }
Signature · 1 line
2.53.0
Pablo Neira Ayuso
maintainerRe: [PATCH net 2/4] netfilter: flowtable: restore the GRE conntrack timeout on teardown
Hi Florian,
Quoted 10 lines · Florian Fainelli
flow_offload_add()sets the timeout of the conntrack entry toNF_CT_DAYfor every offloaded flow, and the GC keeps extending it for GRE too.flow_offload_fixup_ct()only restores the conntrack timeout of TCP and UDP flows when the flow is torn down, so an offloaded GRE entry which does not see any other packet on the slow path stays for up to a day instead of expiring after the GRE conntrack timeout.Restore the GRE timeout, replied or unreplied, as for UDP. GRE has no offload timeout of its own,
flow_offload_get_timeout()uses the default flowtable timeout for it, so use that one when the flow expired.
I think GRE has never worked then. I have been a bit skeptical about this support, but tc/ct developers wanted to have this in.
I think no crash is going, so let's better route this through nf-next.git.
Quoted 33 lines
Fixes 4e8d9584d154 ("netfilter: flowtable: Support GRE")Signed-off-by Florian Fainelli <[email protected]>--- net/netfilter/nf_flow_table_core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)net/netfilter/nf_flow_table_core.c +12
-0@@ -237,6 +237,18 @@ static void flow_offload_fixup_ct(struct flow_offload *flow) timeout =READ_ONCE (tn->timeouts[state]); expired =nf_flow_has_expired (flow); offload_timeout =READ_ONCE (tn->offload_timeout); +#ifdef CONFIG_NF_CT_PROTO_GRE + }else if (l4num ==IPPROTO_GRE ) { +const struct nf_gre_net *tn =nf_gre_pernet (net); +enum gre_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +GRE_CT_REPLIED :GRE_CT_UNREPLIED ; + + timeout =READ_ONCE (tn->timeouts[state]); + expired =nf_flow_has_expired (flow); +/* GRE has no offload timeout: see flow_offload_get_timeout(). */ + offload_timeout =NF_FLOW_TIMEOUT ; +#endif }else {return ; }Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net 2/4] netfilter: flowtable: restore the GRE conntrack timeout on teardown
In reply to Florian FainelliThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The patch adds a GRE branch to flow_offload_fixup_ct() (net/netfilter/nf_flow_table_core.c).
--
netfilter: flowtable: restore the GRE conntrack timeout on teardown
This commit adds a GRE branch to flow_offload_fixup_ct(). When a GRE flow is torn down, its conntrack timeout is restored to the GRE replied or unreplied timeout. Without this, the entry keeps the NF_CT_DAY timeout it got at offload time. GRE has no offload timeout of its own, so NF_FLOW_TIMEOUT is subtracted when the flow has expired.
Quoted 6 lines
net/netfilter/nf_flow_table_core.c +0
-0@@ -237,6 +237,18 @@ static void flow_offload_fixup_ct(struct flow_offload *flow) timeout =READ_ONCE (tn->timeouts[state]);
net/netfilter/nf_flow_table_core.c static void flow_offload_fixup_ct(struct flow_offload *flow) · line 247
Quoted 10 lines
net/netfilter/nf_flow_table_core.c +8
-0expired =nf_flow_has_expired (flow); offload_timeout =READ_ONCE (tn->offload_timeout); +#ifdef CONFIG_NF_CT_PROTO_GRE + }else if (l4num ==IPPROTO_GRE ) { +const struct nf_gre_net *tn =nf_gre_pernet (net); +enum gre_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +GRE_CT_REPLIED :GRE_CT_UNREPLIED ; + + timeout =READ_ONCE (tn->timeouts[state]);
[Severity: Medium]
Should this read the per-conntrack GRE timeouts instead of the per-netns defaults?
The GRE tracker does not refresh entries with the pernet values directly. In nf_conntrack_gre_packet(), the per-ct fields are set from an attached timeout policy when one exists. The pernet defaults are only the fallback:
net/netfilter/nf_conntrack_proto_gre.c:nf_conntrack_gre_packet() {
...
if (!nf_ct_is_confirmed(ct)) {
unsigned int *timeouts = nf_ct_timeout_lookup(ct);
if (!timeouts)
timeouts = gre_get_timeouts(nf_ct_net(ct));
...
ct->proto.gre.stream_timeout = timeouts[GRE_CT_REPLIED];
ct->proto.gre.timeout = timeouts[GRE_CT_UNREPLIED];
}
...
}Every later refresh then uses ct->proto.gre.stream_timeout or ct->proto.gre.timeout.
GRE registers .ctnl_timeout, so an nft ct timeout object or an nfnetlink_cttimeout policy can be attached. nft_flow_offload_eval() rejects GRE flows that are NATed, keyed, or have a helper. It does not reject flows that have a timeout policy.
As an example, take a policy that sets the GRE replied timeout to 2h. An offloaded GRE v0 flow goes idle, and the GC tears it down with expired set to true. This branch then computes the default 180s minus NF_FLOW_TIMEOUT, which is 150s. Since nf_flow_timeout_delta(ct->timeout) is larger than that, nf_ct_refresh() makes the entry expire in about 2.5 minutes instead of 2h.
The reverse also happens: with a policy shorter than the sysctl value, the longer default replaces it.
Would reading ct->proto.gre.stream_timeout and ct->proto.gre.timeout here match what the GRE tracker actually uses? The UDP branch above has the same pattern, but that code predates this patch.
Quoted 7 lines
net/netfilter/nf_flow_table_core.c +4
-0+ expired =nf_flow_has_expired (flow); +/* GRE has no offload timeout: see flow_offload_get_timeout(). */ + offload_timeout =NF_FLOW_TIMEOUT ; +#endif }else {return ; }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118201250.2075007-1-florian.fainelli%40broadcom.com
PATCH 3/4 selftests: netfilter: check hardware offload work after DESTROY
In reply to Florian FainelliAdd a test for "netfilter: flowtable: do not queue offload work after DESTROY". A TCP flow in a hardware offload flowtable is closed: its hardware rule is destroyed, and the flow stays in the flowtable until it expires, where the garbage collector keeps finding it. No STATS or REPLACE work may be queued for it once DESTROY was.
The flowtable devices are netdevsim ports, the only devices which accept a hardware offload flowtable, and the work is told apart by its workqueue with the workqueue_queue_work tracepoint, as netdevsim does not show it. The REPLACE case of the fix is a race between a packet reader and the garbage collector which cannot be triggered from user space; this checks the same rule through the STATS work, which the garbage collector queues after DESTROY without the fix.
Without the fix, 6 work items are queued after DESTROY in a 20 seconds TCP flowtable timeout run in a VM, and the test fails. With it, none are.
--- .../testing/selftests/net/netfilter/Makefile | 1 + tools/testing/selftests/net/netfilter/config | 1 + .../net/netfilter/nft_flowtable_hw_destroy.sh | 162 ++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100755 tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
@@ -30,6 +30,7 @@ TEST_PROGS := \ nft_fib.sh \ nft_fib_nexthop.sh \ nft_flowtable.sh \ + nft_flowtable_hw_destroy.sh \ nft_interface_stress.sh \ nft_meta.sh \ nft_nat.sh \
tools/testing/selftests/net/netfilter/config +1 -0
@@ -16,6 +16,7 @@ CONFIG_DUMMY=m CONFIG_FAIL_FUNCTION=y CONFIG_FAULT_INJECTION=y CONFIG_FAULT_INJECTION_DEBUG_FS=y +CONFIG_FTRACE=y CONFIG_FUNCTION_ERROR_INJECTION=y CONFIG_INET_DIAG=m CONFIG_INET_ESP=m
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net 3/4] selftests: netfilter: check hardware offload work after DESTROY
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 4
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The new test nft_flowtable_hw_destroy.sh uses the top-level tracefs instance, which the whole system shares, and draws conclusions…
- [Medium] nft_flowtable_hw_destroy.sh never deletes the four netdevsim devices it creates.
- [Medium] nft_flowtable_hw_destroy.sh reports SKIP instead of FAIL when no DESTROY work is seen, even though every prerequisite has already…
- [Low] nft_flowtable_hw_destroy.sh sets net.netfilter.nf_flowtable_tcp_timeout at line 105, before anything…
- [Low] In nft_flowtable_hw_destroy.sh, tmpfile=$(mktemp) runs at line 19, but trap cleanup EXIT is only installed at line 40.
- [Low] nft_flowtable_hw_destroy.sh starts the socat TCP listener in ns2 in the background (line 131) and then just does sleep 1 (line 132)…
- [Low] selftests: netfilter: CONFIG_FTRACE=y alone does not enable CONFIG_TRACING
--
selftests: netfilter: check hardware offload work after DESTROY
This adds a netdevsim based selftest. It closes a TCP flow in a hardware offload flowtable, then uses the workqueue_queue_work tracepoint to check that no STATS or REPLACE offload work is queued after the DESTROY work. It also adds CONFIG_FTRACE to the netfilter selftest config.
Quoted 7 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +1
-0@@ -0,0 +1,162 @@ +#!/bin/bash
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh line 23
Quoted 8 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +8
-0+ret=0 +tracing=/sys/kernel/tracing +trace_mounted=0 +tmpfile=$(mktemp) +nsim_ids="" + +checktool "nft --version" "run test without nft tool" +checktool "socat -h" "run test without socat"
[Severity: Low]
Is the temporary file leaked when nft or socat is missing?
checktool() in netfilter/lib.sh runs exit $ksft_skip when the tool is missing. The trap cleanup EXIT line is only installed further down, so the rm -f "$tmpfile" in cleanup() never runs on that path.
Could the mktemp be moved after the trap is installed?
[ ... ]
Quoted 9 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +9
-0+nsim_new() { + local id="$1" ns="$2" name + + echo "$id 1" > /sys/bus/netdevsim/new_device || return 1 + nsim_ids="$nsim_ids $id" + name=$(ls /sys/bus/netdevsim/devices/netdevsim"$id"/net) + ip link set "$name" name "nsim$id" netns "$ns" || return 1 + echo "nsim$id" +}
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh line 76
Quoted 5 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +5
-0+base=$((256 + RANDOM % 128 * 4)) +a1=$(nsim_new "$base" "$ns1") || exit $ksft_skip +a2=$(nsim_new $((base + 1)) "$nsr1") || exit $ksft_skip +b1=$(nsim_new $((base + 2)) "$nsr1") || exit $ksft_skip +b2=$(nsim_new $((base + 3)) "$ns2") || exit $ksft_skip
[Severity: Medium]
Are the four netdevsim devices ever deleted?
nsim_new() appends to nsim_ids, but every call here is a command substitution, and each one runs in a subshell. The update is lost and the parent's nsim_ids stays empty. So this loop in cleanup():
for id in $nsim_ids; do
cleanup_netdevsim "$id"
done
never writes anything to /sys/bus/netdevsim/del_device.
netdevsim's rtnl_link_ops has no netns_refund. When cleanup_all_ns deletes the namespaces, default_device_exit_net() moves the netdevs back to init_net, and the netdevsim$base..netdevsim$((base+3)) bus devices stay registered. This happens on every run that gets past device creation, whether it ends in PASS, FAIL or SKIP.
base can only take 128 values. A later run that picks the same base would fail new_device and report SKIP.
Could nsim_ids be updated in the parent shell after each successful call instead?
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh line 107
Quoted 5 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +5
-0+# The statistics of a flow are polled once a tenth of its timeout went by, and +# the flow must stay around for longer than that after it is closed. +ip netns exec "$nsr1" sysctl -q net.netfilter.nf_flowtable_tcp_timeout=20 + +if ! ip netns exec "$nsr1" nft -f - <<EOF
[Severity: Low]
Does this sysctl exist yet at this point?
nf_conntrack registers net.netfilter.nf_flowtable_tcp_timeout in nf_ct_sysctl_table in nf_conntrack_standalone.c. The selftest config has CONFIG_NF_CONNTRACK=m, and nothing before this line loads the module. The first thing that loads it is the ct state rule in the nft -f ruleset below.
On a fresh boot, or when this test runs on its own, the write fails with only a stderr message, and its exit status is ignored. The flow then keeps the default 30 second timeout, not the 20 seconds the comment above relies on. The current sleep values still give the right result, but the test is not running with the timeout it configured.
Could the sysctl move after the ruleset is loaded, or could nf_conntrack be loaded with modprobe first?
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh line 132
Quoted 2 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +2
-0+ip netns exec "$ns2" socat -u TCP-LISTEN:12345,reuseaddr /dev/null & +sleep 1
[Severity: Low]
Is a fixed sleep 1 enough to be sure the listener is bound?
On a slow or loaded machine, the client below can connect before port 12345 is listening. The connection is then refused, no flow is added to the flowtable, no DESTROY is queued, and the test exits with SKIP.
Other tests in this directory, such as nft_flowtable.sh, nft_nat.sh and nft_queue.sh, use wait_local_port_listen or busywait for this. Could the same be used here?
Quoted 7 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +7
-0+ +echo 1 > "$tracing/events/workqueue/workqueue_queue_work/enable" +echo > "$tracing/trace" + +# Send data for the flow to be offloaded, then close the connection. +(for _ in 1 2 3 4 5 6; do echo data; sleep 0.3; done) | \ + ip netns exec "$ns1" socat -u - TCP:10.0.2.2:12345
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh line 152
Quoted 5 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +5
-0+destroy=$(grep -n "workqueue=nf_ft_offload_del" "$tmpfile" | head -n 1 | cut -d: -f1) +if [ -z "$destroy" ]; then + echo "SKIP: the hardware rule of the flow was not destroyed" + exit $ksft_skip +fi
[Severity: Medium]
Should this be a FAIL rather than a SKIP?
By this point the test has already shown that the offload flowtable loaded and that forwarding works through the netdevsim devices. If no nf_ft_offload_del work shows up, there are two possible causes. Either the TCP connection never happened, or the kernel path that should queue DESTROY for the closed flow is broken:
net/netfilter/nf_flow_table_core.c:nf_flow_offload_gc_step() {
...
} else if (test_bit(NF_FLOW_CLOSING, &flow->flags) &&
test_bit(NF_FLOW_HW, &flow->flags) &&
!test_bit(NF_FLOW_HW_DYING, &flow->flags)) {
nf_flow_offload_del(flow_table, flow);
...
}Reporting either case as a skip could hide a regression in the very path this test covers.
The exit status of the socat client pipeline above is never checked either, so a refused or failed connection goes unnoticed. Would it make sense to check that too?
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh
Quoted 2 lines
tools/testing/selftests/net/netfilter/nft_flowtable_hw_destroy.sh new file +2
-0+ +late=$(tail -n +"$((destroy + 1))" "$tmpfile" | grep -c "workqueue=nf_ft_offload_\(stats\|add\)")
[Severity: Medium]
Can unrelated flowtables make this fail on a fixed kernel?
nf_ft_offload_add, nf_ft_offload_del and nf_ft_offload_stats are single module-global workqueues created in nf_flow_table_offload_init(). flow_offload_queue_work() uses them for every flowtable in every netns. The workqueue_queue_work output only carries the work struct, function and workqueue name. Nothing in it identifies the flow or the netns.
If any other hardware offload flowtable is active during the run, its GC STATS work lands after this flow's DESTROY and gets counted here. The DESTROY line picked by head -n 1 above may also belong to a different flow.
The test also uses the top-level tracefs instance, which the whole system shares. The enable and the buffer clear before the client connects both act on it. On top of that, cleanup() runs:
if [ -d "$tracing/events/workqueue" ]; then
echo 0 > "$tracing/events/workqueue/workqueue_queue_work/enable" echo > "$tracing/trace"
fi
This always disables the event and wipes the global buffer, even on early exits that never enabled it, and the previous enable state is never restored. Wouldn't that throw away the trace data and event setup of any other ftrace user?
Would a private instance under $tracing/instances/ work better, removed in cleanup() and filtered on the workqueue name? Telling flows apart would still need some per-flow way to identify the work.
[ ... ]
tools/testing/selftests/net/netfilter/config CONFIG_DUMMY=m · line 22
Quoted 11 lines
tools/testing/selftests/net/netfilter/config +1
-0@@ -16,6 +16,7 @@ CONFIG_DUMMY=m CONFIG_FAIL_FUNCTION=y CONFIG_FAULT_INJECTION=y CONFIG_FAULT_INJECTION_DEBUG_FS=y +CONFIG_FTRACE=y CONFIG_FUNCTION_ERROR_INJECTION=y CONFIG_INET_DIAG=m CONFIG_INET_ESP=m
[Severity: Low]
Is CONFIG_FTRACE=y enough to get the workqueue_queue_work event?
In kernel/trace/Kconfig, FTRACE is the "Tracers" menuconfig. It only opens the if FTRACE block and does not select anything by itself. TRACING is the hidden symbol that pulls in EVENT_TRACING, TRACEPOINTS and tracefs. Only a tracer selects it, either FUNCTION_TRACER and friends through GENERIC_TRACER, or ENABLE_DEFAULT_TRACERS. Nothing else in this config fragment selects it. FTRACE also already defaults to y with DEBUG_KERNEL, so this line adds little on its own.
Suppose a kernel is built from this fragment on top of a base config that has no tracer enabled. It then has no tracefs and no events/workqueue directory. nft_flowtable_hw_destroy.sh takes the "workqueue tracepoints are not available" exit and reports SKIP, so a CI setup that relies on the fragment never actually runs the test.
Would adding CONFIG_ENABLE_DEFAULT_TRACERS=y here pull in TRACING the way you need? The landlock selftest config does this. Or is there another tracer option you would rather depend on?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118201250.2075007-1-florian.fainelli%40broadcom.com
PATCH 4/4 selftests: netfilter: check the GRE timeout after flowtable teardown
In reply to Florian FainelliAdd a test for "netfilter: flowtable: restore the GRE conntrack timeout on teardown". A GRE tunnel between two namespaces goes through a router with a flowtable offloading the GRE flow. The flowtable is removed, which tears the flow down, and the timeout of the conntrack entry must be the GRE one, not the day set when the flow was offloaded.
Without the fix, the entry has 86399 seconds left after teardown and the test fails. With it, the entry has 179 seconds left, the GRE stream timeout minus the time which went by.
Add the GRE tunnel and conntrack helper options to the config.
--- .../testing/selftests/net/netfilter/Makefile | 1 + tools/testing/selftests/net/netfilter/config | 3 + .../net/netfilter/nft_flowtable_gre.sh | 137 ++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100755 tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
@@ -30,6 +30,7 @@ TEST_PROGS := \ nft_fib.sh \ nft_fib_nexthop.sh \ nft_flowtable.sh \ + nft_flowtable_gre.sh \ nft_flowtable_hw_destroy.sh \ nft_interface_stress.sh \ nft_meta.sh \
tools/testing/selftests/net/netfilter/config +3 -0
@@ -55,6 +55,8 @@ CONFIG_NETFILTER_XT_MATCH_STATE=m CONFIG_NETFILTER_XT_MATCH_STRING=m CONFIG_NETFILTER_XT_NAT=m CONFIG_NETFILTER_XT_TARGET_REDIRECT=m +CONFIG_NET_IPGRE=m +CONFIG_NET_IPGRE_DEMUX=m CONFIG_NET_IPIP=m CONFIG_NET_L3_MASTER_DEV=y CONFIG_NET_NS=y
@@ -66,6 +68,7 @@ CONFIG_NF_CONNTRACK=m CONFIG_NF_CONNTRACK_EVENTS=y CONFIG_NF_CONNTRACK_FTP=m CONFIG_NF_CONNTRACK_MARK=y +CONFIG_NF_CONNTRACK_PPTP=m CONFIG_NF_CONNTRACK_PROCFS=y CONFIG_NF_CONNTRACK_ZONES=y CONFIG_NF_CT_NETLINK=m
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh new file +137 -0
137 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH net 4/4] selftests: netfilter: check the GRE timeout after flowtable teardown
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] In nft_flowtable_gre.sh the test treats nft delete table inet filter as tearing down the offloaded GRE flow before the command…
- [Low] The timeout check in nft_flowtable_gre.sh (lines 127-135) fails only when the restored timeout is greater than…
--
selftests: netfilter: check the GRE timeout after flowtable teardown
This adds a selftest that offloads a GRE flow through a flowtable in a router namespace and then deletes the flowtable. It checks that the conntrack entry timeout goes back to the GRE stream timeout instead of the one-day offload timeout. The GRE and PPTP options are also added to the selftest config.
Quoted 6 lines
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh new file +0
-0@@ -0,0 +1,137 @@
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh line 123
Quoted 13 lines
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh new file +13
-0+# The entry has the timeout of an offloaded flow: remove the flowtable, which +# tears down its flows. Nothing else refreshes the conntrack entry. +if ! ip netns exec "$nsr1" nft delete table inet filter; then + echo "FAIL: could not remove the flowtable" + exit 1 +fi + +if gre_entry_offloaded; then + echo "FAIL: the GRE entry is still marked as offloaded" + ret=1 +fi + +timeout=$(gre_entry_timeout)
[Severity: Medium]
Is the flowtable teardown guaranteed to be finished by the time nft delete table returns?
In net/netfilter/nf_tables_api.c, flowtable destruction seems to be deferred to a workqueue:
nf_tables_commit_release()
schedule_work(&nft_net->destroy_work);
The work item also waits for an RCU grace period before it frees the flowtable:
nf_tables_trans_destroy_work()
synchronize_rcu();
nft_commit_release()
nf_tables_flowtable_destroy()
nf_flow_table_free()
... flow_offload_teardown()IPS_OFFLOAD_BIT is cleared and the GRE timeout restored only in flow_offload_teardown():
net/netfilter/nf_flow_table_core.c:flow_offload_teardown() {
clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
if (!test_and_set_bit(NF_FLOW_TEARDOWN, &flow->flags))
flow_offload_fixup_ct(flow);
}If conntrack -L runs before the deferred work completes, gre_entry_offloaded() would still see OFFLOAD and print "still marked as offloaded".
gre_entry_timeout() would also return nothing. While the entry is offloaded, ctnetlink_dump_info() in nf_conntrack_netlink.c does not dump the timeout:
if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) && (ctnetlink_dump_timeout(skb, ct, false) < 0 ||
In that case the test also prints the misleading "no conntrack entry for the GRE flow" message, even though the entry exists.
Can this make the test fail at random on a fixed kernel? That seems most likely on loaded or virtualized CI, where synchronize_rcu() can be slow. Would polling with busywait or slowwait from lib.sh, until OFFLOAD clears and a timeout is printed, avoid the race?
The comment above ("remove the flowtable, which tears down its flows") and the commit message both describe the teardown as synchronous.
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh line 130
Quoted 7 lines
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh new file +7
-0+max=$(ip netns exec "$nsr1" sysctl -n net.netfilter.nf_conntrack_gre_timeout_stream 2>/dev/null) +[ -z "$max" ] && max=180 + +if [ -z "$timeout" ]; then + echo "FAIL: no conntrack entry for the GRE flow" + ret=1 +elif [ "$timeout" -gt "$max" ]; then
[Severity: Low]
This isn't a bug, but only an upper bound is checked here, so any timeout from 0 to 180 is reported as PASS. The check does catch the 86399 regression the commit targets.
The "ct state established" rule requires IPS_SEEN_REPLY, and the flow has not expired. So flow_offload_fixup_ct() should pick tn->timeouts[GRE_CT_REPLIED] without subtracting NF_FLOW_TIMEOUT, giving about 179-180 seconds.
The following broken fixups would all still pass this check:
- one that picks GRE_CT_UNREPLIED (30s)
- one that wrongly subtracts NF_FLOW_TIMEOUT (about 150s)
- one that clamps the timeout to 0
Would a lower bound, such as max minus a small slack, better match the "restored to the GRE timeout" behaviour described in the header comment?
Quoted 5 lines
tools/testing/selftests/net/netfilter/nft_flowtable_gre.sh new file +5
-0+ echo "FAIL: the GRE entry timeout is $timeout seconds after teardown, expected at most $max" + ret=1 +else + echo "PASS: the GRE entry timeout was restored to $timeout seconds" +fi
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118201250.2075007-1-florian.fainelli%40broadcom.com