With hardware offload and the flowtable counter flag, conntrack accounting is only updated by flow_offload_work_stats(), which the gc queues once 10% of the flow timeout has elapsed since the last refresh, so roughly every 3 seconds with the default timeout. flow_offload_work_del() removes the hardware entries without asking the driver for its counters one last time, so whatever the hardware forwarded since the last refresh never reaches conntrack. A bulk transfer that closes right after the last byte loses up to ~3 seconds of traffic, which at line rate can be the whole transfer. On a MT7622 router (mtk_ppe with accounting, 6.12.112), a 100 MiB HTTPS download that is closed as soon as the body is read ends up in conntrack with ~40 KB in the reply direction. The same download kept open for a few seconds before closing is accounted in full. Fetch the counters before removing the hardware entries when the flowtable has the counter flag. Fixes: ef803b3cf96a ("netfilter: flowtable: add counter support in HW offload") Signed-off-by: Dario Andrei --- Notes (not for the commit message): I tested this on a Xiaomi AX3200 (MT7622, mtk_ppe with accounting) running OpenWrt 25.12.6 with kernel 6.12.112, nft flowtable with "flags offload" and "counter". The test downloads a file from a public mirror through the router and compares the reply bytes in conntrack with the IP bytes received by the client (rx bytes minus 14 per packet). On 6.12.y the patch needs a small context change, since flow_offload_work_del() does not set NF_FLOW_HW_DEAD there. conntrack reply bytes / IP bytes on the wire: before after 100 MiB, closed right away (x5) 0.04% 101.2% 100 MiB, 8 s idle, then closed (x3) 101% 101% 60 x 10 MiB, 6 at a time, closed 0.3% 101.2% 100 MiB, 40 s idle, then closed 101% 101% No new kernel messages, no PPE entries left behind after the tests. The extra ~1% is the L2 overhead the PPE includes in its byte counters (Ethernet + the VLAN tag on my WAN port). It is the same with or without this patch, and it is what Daniel Golle's RFC about the byte counter semantics deals with. One thing to know if someone tries to reproduce this on OpenWrt: its mtk_eth_soc carries an out-of-tree accounting patch for L2 flows that returns stale cached counters once an entry leaves the bind state, and counts idle flows several times over. With that bug this patch alone makes no visible difference, so the "before" column above already has the driver fixed: https://github.com/openwrt/openwrt/pull/27266 As far as I can tell mainline mtk_ppe reads the hardware counters on every request and is not affected, but I could not test a mainline kernel on this device. Ahmed Zaki's series "Update (DSA) netdev stats with offloaded flows" also reads the counters in flow_offload_work_del(), for the netdev stats only. The two should not get in each other's way: if that series goes in first, I am happy to rebase on top of it. I can share the test scripts and the raw numbers if they help. net/netfilter/nf_flow_table_offload.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c index 4365859220e6..a233fbabd969 100644 --- a/net/netfilter/nf_flow_table_offload.c +++ b/net/netfilter/nf_flow_table_offload.c @@ -989,15 +989,6 @@ static void flow_offload_work_add(struct flow_offload_work *offload) nf_flow_offload_destroy(flow_rule); } -static void flow_offload_work_del(struct flow_offload_work *offload) -{ - clear_bit(IPS_HW_OFFLOAD_BIT, &offload->flow->ct->status); - flow_offload_tuple_del(offload, FLOW_OFFLOAD_DIR_ORIGINAL); - if (test_bit(NF_FLOW_HW_BIDIRECTIONAL, &offload->flow->flags)) - flow_offload_tuple_del(offload, FLOW_OFFLOAD_DIR_REPLY); - set_bit(NF_FLOW_HW_DEAD, &offload->flow->flags); -} - static void flow_offload_tuple_stats(struct flow_offload_work *offload, enum flow_offload_tuple_dir dir, struct flow_stats *stats) @@ -1034,6 +1025,21 @@ static void flow_offload_work_stats(struct flow_offload_work *offload) } } +static void flow_offload_work_del(struct flow_offload_work *offload) +{ + /* Fetch the counters that the hardware accumulated since the last + * FLOW_CLS_STATS request: they are lost once the entry is removed. + */ + if (offload->flowtable->flags & NF_FLOWTABLE_COUNTER) + flow_offload_work_stats(offload); + + clear_bit(IPS_HW_OFFLOAD_BIT, &offload->flow->ct->status); + flow_offload_tuple_del(offload, FLOW_OFFLOAD_DIR_ORIGINAL); + if (test_bit(NF_FLOW_HW_BIDIRECTIONAL, &offload->flow->flags)) + flow_offload_tuple_del(offload, FLOW_OFFLOAD_DIR_REPLY); + set_bit(NF_FLOW_HW_DEAD, &offload->flow->flags); +} + static void flow_offload_work_handler(struct work_struct *work) { struct flow_offload_work *offload; -- 2.56.1