netfilter: rework conntrack/flowtable interaction
Subsystems (2)
Florian Westphal
maintainercover netfilter: rework conntrack/flowtable interaction
This series resolves a few problems with flowtables when entries are moved from offload (hw/sw offload) back to the conntrack slowpath.
First patch fixes conntrack reset validation, we must clear MAXACK flag on reset packets in the sw flow offload path, conntrack state is stale, it cannot validate reset sequence number.
Second patch adds mandatory locking when manipulating ct state flags.
Third patch is a cleanup patch so existing API can be re-used when we lack an skb.
Patch 4 is a small preparation patch to reuse existing api and get rid of redundant one later.
Patch 5 moves timeout extension logic from conntrack GC to flowtable GC worker.
Patch 6 prevents accidental unwanted growth of conntrack timeout when handling packets of same flow in slowpath at same time.
Patch 7 is an optimization to keep entry in software flowtable when a fin is received.
NB: nftables flowtable selftest needs a minor fixup to exect 300s timeout instead of 5 days after inital move to slowpath, this is the only observed failure with nf kselftests or nftables shell tests.
Florian Westphal (7):
netfilter: nft_flow_offload: clear tcp MAXACK flag before moving to
slowpath
netfilter: nft_flow_offload: update tcp state flags under lock
netfilter: conntrack: remove skb argument from nf_ct_refresh
netfilter: flowtable: prefer plain nf_ct_refresh for setting initial
timeout
netfilter: conntrack: rework offload nf_conn timeout extension logic
netfilter: nft_flow_offload: never grow the timeout when moving
packets back to slowpath
netfilter: nft_flow_offload: do not remove flowtable entry for fin
packets
Patches vs. nf-next, but could be applied to nf too.
include/net/netfilter/nf_conntrack.h | 18 +-- net/netfilter/nf_conntrack_amanda.c | 2 +- net/netfilter/nf_conntrack_broadcast.c | 2 +- net/netfilter/nf_conntrack_core.c | 13 +- net/netfilter/nf_conntrack_h323_main.c | 4 +- net/netfilter/nf_conntrack_sip.c | 4 +- net/netfilter/nf_flow_table_core.c | 200 ++++++++++++++++++++++--- net/netfilter/nf_flow_table_ip.c | 5 +- net/netfilter/nft_ct.c | 2 +- net/netfilter/nft_flow_offload.c | 16 +- 10 files changed, 207 insertions(+), 59 deletions(-)
Signature · 1 line
2.44.2
Florian Westphal
maintainerPATCH 1/7 netfilter: nft_flow_offload: clear tcp MAXACK flag before moving to slowpath
This state reset is racy, no locks are held here.
Since commit8437a6209f76 ("netfilter: nft_flow_offload: set liberal tracking mode for tcp"), the window checks are disabled for normal data packets, but MAXACK flag is checked when validating TCP resets.
Clear the flag so tcp reset validation checks are ignored.
--- net/netfilter/nf_flow_table_core.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
net/netfilter/nf_flow_table_core.c +12 -2
@@ -161,10 +161,20 @@ void flow_offload_route_init(struct flow_offload *flow, }EXPORT_SYMBOL_GPL (flow_offload_route_init); -static void flow_offload_fixup_tcp (struct ip_ct_tcp *tcp) +static void flow_offload_fixup_tcp (struct nf_conn *ct) { +struct ip_ct_tcp *tcp = &ct->proto.tcp; + +spin_lock_bh (&ct->lock); +/* Conntrack state is outdated due to offload bypass. +* Clear IP_CT_TCP_FLAG_MAXACK_SET, otherwise conntracks +* TCP reset validation will fail. +*/ tcp->seen[0 ].td_maxwin =0 ; + tcp->seen[0 ].flags &= ~IP_CT_TCP_FLAG_MAXACK_SET ; tcp->seen[1 ].td_maxwin =0 ; + tcp->seen[1 ].flags &= ~IP_CT_TCP_FLAG_MAXACK_SET ; +spin_unlock_bh (&ct->lock); }static void flow_offload_fixup_ct (struct nf_conn *ct)
@@ -176,7 +186,7 @@ static void flow_offload_fixup_ct(struct nf_conn *ct)if (l4num ==IPPROTO_TCP ) {struct nf_tcp_net *tn =nf_tcp_pernet (net); -flow_offload_fixup_tcp (&ct->proto.tcp); +flow_offload_fixup_tcp (ct); timeout = tn->timeouts[ct->proto.tcp.state]; timeout -= tn->offload_timeout;
Signature · 1 line
2.44.2
Florian Westphal
maintainerPATCH 2/7 netfilter: nft_flow_offload: update tcp state flags under lock
In reply to Florian WestphalThe conntrack entry is already public, there is a small chance that another CPU is handling a packet in reply direction and racing with the tcp state update.
Move this under ct spinlock.
This is done once, when ct is about to be offloaded, so this should not result in a noticeable performance hit.
--- net/netfilter/nft_flow_offload.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
net/netfilter/nft_flow_offload.c +11 -5
@@ -289,6 +289,15 @@ static bool nft_flow_offload_skip(struct sk_buff *skb, int family)return false; } +static void flow_offload_ct_tcp (struct nf_conn *ct) +{ +/* conntrack will not see all packets, disable tcp window validation. */ +spin_lock_bh (&ct->lock); + ct->proto.tcp.seen[0 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; + ct->proto.tcp.seen[1 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; +spin_unlock_bh (&ct->lock); +} +static void nft_flow_offload_eval (const struct nft_expr *expr,struct nft_regs *regs,const struct nft_pktinfo *pkt)
@@ -356,11 +365,8 @@ static void nft_flow_offload_eval(const struct nft_expr *expr, goto err_flow_alloc;flow_offload_route_init (flow, &route); - -if (tcph) { - ct->proto.tcp.seen[0 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; - ct->proto.tcp.seen[1 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; - } +if (tcph) +flow_offload_ct_tcp (ct);__set_bit (NF_FLOW_HW_BIDIRECTIONAL , &flow->flags); ret =flow_offload_add (flowtable, flow);
Signature · 1 line
2.44.2
Florian Westphal
maintainerPATCH 3/7 netfilter: conntrack: remove skb argument from nf_ct_refresh
In reply to Florian WestphalIts not used (and could be NULL), so remove it.
This allows to use nf_ct_refresh in places where we don't have an skb without having to double-check that skb == NULL would be safe.
--- include/net/netfilter/nf_conntrack.h | 8 +++----- net/netfilter/nf_conntrack_amanda.c | 2 +- net/netfilter/nf_conntrack_broadcast.c | 2 +- net/netfilter/nf_conntrack_core.c | 7 +++---- net/netfilter/nf_conntrack_h323_main.c | 4 ++-- net/netfilter/nf_conntrack_sip.c | 4 ++-- net/netfilter/nft_ct.c | 2 +- 7 files changed, 13 insertions(+), 16 deletions(-)
include/net/netfilter/nf_conntrack.h +3 -5
@@ -204,8 +204,7 @@ bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff, struct nf_conntrack_tuple *tuple);void __nf_ct_refresh_acct (struct nf_conn *ct,enum ip_conntrack_info ctinfo, -const struct sk_buff *skb, -u32 extra_jiffies,bool do_acct); +u32 extra_jiffies,unsigned int bytes);/* Refresh conntrack for this many jiffies and do accounting */ static inline void nf_ct_refresh_acct(struct nf_conn *ct,
@@ -213,15 +212,14 @@ static inline void nf_ct_refresh_acct(struct nf_conn *ct,const struct sk_buff *skb, u32 extra_jiffies) { -__nf_ct_refresh_acct (ct ,ctinfo ,skb ,extra_jiffies ,true ); + __nf_ct_refresh_acct(ct, ctinfo, extra_jiffies, skb->len); }/* Refresh conntrack for this many jiffies */ static inline void nf_ct_refresh (struct nf_conn *ct, -const struct sk_buff *skb,u32 extra_jiffies) { -__nf_ct_refresh_acct (ct,0 , skb, extra_jiffies, false); +__nf_ct_refresh_acct (ct,0 , extra_jiffies,0 ); }/* kill conntrack and do accounting */
net/netfilter/nf_conntrack_amanda.c +1 -1
@@ -106,7 +106,7 @@ static int amanda_help(struct sk_buff *skb,/* increase the UDP timeout of the master connection as replies from * Amanda clients to the server can be quite delayed */ -nf_ct_refresh (ct, skb, master_timeout *HZ ); +nf_ct_refresh (ct, master_timeout *HZ );/* No data? */ dataoff = protoff +sizeof (struct udphdr );
net/netfilter/nf_conntrack_broadcast.c +1 -1
@@ -75,7 +75,7 @@ int nf_conntrack_broadcast_help(struct sk_buff *skb,nf_ct_expect_related (exp,0 );nf_ct_expect_put (exp); -nf_ct_refresh (ct, skb, timeout *HZ ); +nf_ct_refresh (ct, timeout *HZ ); out:return NF_ACCEPT ; }
net/netfilter/nf_conntrack_core.c +3 -4
@@ -2043,9 +2043,8 @@ EXPORT_SYMBOL_GPL(nf_conntrack_in);/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ void __nf_ct_refresh_acct (struct nf_conn *ct,enum ip_conntrack_info ctinfo, -const struct sk_buff *skb,u32 extra_jiffies, -bool do_acct) +unsigned int bytes) {/* Only update if this is not a fixed timeout */ if (test_bit (IPS_FIXED_TIMEOUT_BIT , &ct->status))
@@ -2058,8 +2057,8 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,if (READ_ONCE (ct->timeout) != extra_jiffies)WRITE_ONCE (ct->timeout, extra_jiffies); acct: -if (do_acct) -nf_ct_acct_update (ct,CTINFO2DIR (ctinfo), skb->len); +if (bytes) +nf_ct_acct_update (ct,CTINFO2DIR (ctinfo), bytes); }EXPORT_SYMBOL_GPL (__nf_ct_refresh_acct);
net/netfilter/nf_conntrack_h323_main.c +2 -2
@@ -1385,7 +1385,7 @@ static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,if (info->timeout >0 ) {pr_debug ("nf_ct_ras: set RAS connection timeout to " "%u seconds\n" , info->timeout); -nf_ct_refresh (ct, skb, info->timeout *HZ ); +nf_ct_refresh (ct, info->timeout *HZ );/* Set expect timeout */ spin_lock_bh (&nf_conntrack_expect_lock);
@@ -1433,7 +1433,7 @@ static int process_urq(struct sk_buff *skb, struct nf_conn *ct, info->sig_port[!dir] =0 ;/* Give it 30 seconds for UCF or URJ */ -nf_ct_refresh (ct, skb,30 *HZ ); +nf_ct_refresh (ct,30 *HZ );return 0 ; }
net/netfilter/nf_conntrack_sip.c +2 -2
@@ -1553,7 +1553,7 @@ static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,if (dataoff >= skb->len)return NF_ACCEPT ; -nf_ct_refresh (ct, skb, sip_timeout *HZ ); +nf_ct_refresh (ct, sip_timeout *HZ );if (unlikely (skb_linearize (skb)))return NF_DROP ;
@@ -1624,7 +1624,7 @@ static int sip_help_udp(struct sk_buff *skb, unsigned int protoff,if (dataoff >= skb->len)return NF_ACCEPT ; -nf_ct_refresh (ct, skb, sip_timeout *HZ ); +nf_ct_refresh (ct, sip_timeout *HZ );if (unlikely (skb_linearize (skb)))return NF_DROP ;
net/netfilter/nft_ct.c +1 -1
@@ -929,7 +929,7 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj,*/ values =nf_ct_timeout_data (timeout);if (values) -nf_ct_refresh (ct, pkt->skb, values[0 ]); +nf_ct_refresh (ct, values[0 ]); } static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx,
Signature · 1 line
2.44.2
Florian Westphal
maintainerPATCH 4/7 netfilter: flowtable: prefer plain nf_ct_refresh for setting initial timeout
In reply to Florian WestphalThis allows to remove the nf_ct_offload_timeout helper in followup patches.
Its safe to use in the add case, but not on teardown.
--- net/netfilter/nf_flow_table_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_flow_table_core.c +1 -1
@@ -304,7 +304,7 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)return err; } -nf_ct_offload_timeout (flow->ct); +nf_ct_refresh (flow->ct,NF_CT_DAY );if (nf_flowtable_hw_offload (flow_table)) {__set_bit (NF_FLOW_HW , &flow->flags);
Signature · 1 line
2.44.2
Florian Westphal
maintainerPATCH 5/7 netfilter: conntrack: rework offload nf_conn timeout extension logic
In reply to Florian WestphalOffload nf_conn entries may not see traffic for a very long time.
To prevent incorrect 'ct is stale' checks during nf_conntrack table lookup, the gc worker extends the timeout nf_conn entries marked for offload to a large value.
The existing logic suffers from a few problems.
Garbage collection runs without locks, its unlikely but possible that @ct is removed right after the 'offload' bit test.
In that case, the timeout of a new/reallocated nf_conn entry will be increased.
Prevent this by obtaining a reference count on the ct object and re-check of the confirmed and offload bits.
If those are not set, the ct is being removed, skip the timeout extension in this case.
Parallel teardown is also problematic:
cpu1 cpu2
gc_worker
calls flow_offload_teardown()
tests OFFLOAD bit, set
clear OFFLOAD bit
ct->timeout is repaired (e.g. set to timeout[UDP_CT_REPLIED])
nf_ct_offload_timeout() called
expire value is fetched
<INTERRUPT>
-> NF_CT_DAY timeout for flow that isn't offloaded
(and might not see any further packets).
Use cmpxchg: if ct->timeout was repaired after the 2nd 'offload bit' test passed, then ct->timeout will only be updated of ct->timeout was not altered in between.
As we already have a gc worker for flowtable entries, ct->timeout repair can be handled from the flowtable gc worker.
This avoids having flowtable specific logic in the conntrack core and avoids checking entries that were never offloaded.
--- include/net/netfilter/nf_conntrack.h | 10 --- net/netfilter/nf_conntrack_core.c | 6 -- net/netfilter/nf_flow_table_core.c | 99 ++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 16 deletions(-)
include/net/netfilter/nf_conntrack.h +0 -10
@@ -312,16 +312,6 @@ static inline bool nf_ct_should_gc(const struct nf_conn *ct)#define NF_CT_DAY (86400 * HZ) -/* Set an arbitrary timeout large enough not to ever expire, this save -* us a check for the IPS_OFFLOAD_BIT from the packet path via -* nf_ct_is_expired(). -*/ -static inline void nf_ct_offload_timeout (struct nf_conn *ct) -{ -if (nf_ct_expires (ct) <NF_CT_DAY /2 ) -WRITE_ONCE (ct->timeout, nfct_time_stamp +NF_CT_DAY ); -} -struct kernel_param ;int nf_conntrack_set_hashsize (const char *val,const struct kernel_param *kp);
net/netfilter/nf_conntrack_core.c +0 -6
@@ -1498,12 +1498,6 @@ static void gc_worker(struct work_struct *work) tmp =nf_ct_tuplehash_to_ctrack (h); -if (test_bit (IPS_OFFLOAD_BIT , &tmp->status)) { -nf_ct_offload_timeout (tmp); -if (!nf_conntrack_max95) -continue ; - } -if (expired_count >GC_SCAN_EXPIRED_MAX ) {rcu_read_unlock ();
net/netfilter/nf_flow_table_core.c +99 -0
111 lines not on this page · raw message
Signature · 1 line
2.44.2
Florian Westphal
maintainerRe: [PATCH nf-next 5/7] netfilter: conntrack: rework offload nf_conn timeout extension logic
Quoted 5 lines · Florian Westphal
+/**
+ *nf_flow_table_tcp_timeout()- new timeout of offloaded tcp entry
+ * @ct: Flowtable offloaded tcp ct
+ *
+ * Return number of seconds when ct entry should expire.
This needs following kdoc fixup:
- * Return number of seconds when ct entry should expire.
+ * @return: number of seconds when ct entry should expire.
Rest of series doesn't add kdoc warnings.
Florian Westphal
maintainerPATCH 6/7 netfilter: nft_flow_offload: never grow the timeout when moving packets back to slowpath
In reply to Florian WestphalThe timeout for offloaded packets is set to a large value. Once packets are moved back to slowpath again, we must make sure that we don't end up with an nf_conn entry with a huge (i.e., hours) timeout:
Its possible that we do not see any further traffic, in such case we end up with a stale entry for a very long time.
flow_offload_fixup_ct() should reduce the timeout to avoid this, but it has a possible race in case another packet is already handled by software slowpath, e.g. in reverse direction.
This could happen e.g. when the packet in question exceeds the MTU or has IP options set while other direction sees a tcp reset or expired route.
cpu1 (skb pushed to slowpatch) cpu2 (dst expired):
calls nf_conntrack_tcp_packet
calls flow_offload_teardown()
timeout = tn->timeouts[ct->proto.tcp.state];
sets ct->proto.tcp.state = new_state
update timeout
WRITE_ONCE(ct->timeout, nfct_time_stamp + timeout);This could be avoided via cmpxchg tricks, but lets keep it simpler and clamp the new timeout to TCP_UNACKED, similar to what we do in classic conntrack for mid-stream pickups.
--- net/netfilter/nf_flow_table_core.c | 75 ++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 13 deletions(-)
net/netfilter/nf_flow_table_core.c +62 -13
@@ -177,36 +177,71 @@ static void flow_offload_fixup_tcp(struct nf_conn *ct)spin_unlock_bh (&ct->lock); } -static void flow_offload_fixup_ct (struct nf_conn *ct) +/** +* flow_offload_fixup_ct() - fix ct timeout on flow entry removal +* @ct: conntrack entry +* @expired: true if flowtable entry timed out +* +* Offload nf_conn entries have their timeout inflated to a very large +* value to prevent garbage collection from kicking in during lookup. +* +* When the flowtable entry is removed for whatever reason, then the +* ct entry must have the timeout set to a saner value to prevent it +* from remaining in place for a very long time. +* +* If the offload flow expired, also subtract the flow offload timeout, +* this helps to expire conntrack entries faster, especially for UDP. +*/ +static void flow_offload_fixup_ct (struct nf_conn *ct,bool expired) { +u32 expires, offload_timeout =0 ;struct net *net =nf_ct_net (ct);int l4num =nf_ct_protonum (ct);s32 timeout;if (l4num ==IPPROTO_TCP ) { -struct nf_tcp_net *tn =nf_tcp_pernet (net); +const struct nf_tcp_net *tn =nf_tcp_pernet (net); +u8 tcp_state =READ_ONCE (ct->proto.tcp.state); +u32 unacked_timeout;flow_offload_fixup_tcp (ct); - timeout = tn->timeouts[ct->proto.tcp.state]; - timeout -= tn->offload_timeout; + timeout =READ_ONCE (tn->timeouts[tcp_state]); + unacked_timeout =READ_ONCE (tn->timeouts[TCP_CONNTRACK_UNACK ]); + +/* Limit to unack, in case we missed a possible +* ESTABLISHED -> UNACK transition right before, +* forward path could have updated tcp.state now. +* +* This also won't leave nf_conn hanging around forever +* in case no further packets are received while in +* established state. +*/ +if (timeout > unacked_timeout) + timeout = unacked_timeout; + + offload_timeout =READ_ONCE (tn->offload_timeout); }else if (l4num ==IPPROTO_UDP ) { -struct nf_udp_net *tn =nf_udp_pernet (net); -enum udp_conntrack state = -test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? -UDP_CT_REPLIED :UDP_CT_UNREPLIED ; +const struct nf_udp_net *tn =nf_udp_pernet (net); +enum udp_conntrack state =test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +UDP_CT_REPLIED :UDP_CT_UNREPLIED ; - timeout = tn->timeouts[state]; - timeout -= tn->offload_timeout; + offload_timeout =READ_ONCE (tn->offload_timeout); + timeout =READ_ONCE (tn->timeouts[state]); }else {return ; } +if (expired) + timeout -= offload_timeout; +if (timeout <0 ) timeout =0 ; -if (nf_flow_timeout_delta (READ_ONCE (ct->timeout)) > (__s32 )timeout) -WRITE_ONCE (ct->timeout, nfct_time_stamp + timeout); + expires =nf_ct_expires (ct); +/* never increase ct->timeout */ +if (expires > timeout) +nf_ct_refresh (ct, timeout); }static void flow_offload_route_release (struct flow_offload *flow)
@@ -350,11 +385,25 @@ static void flow_offload_del(struct nf_flowtable *flow_table,flow_offload_free (flow); } +/** +* flow_offload_teardown() - un-offload a flow +* @flow: flow that should not be offload anymore +* +* This is called when @flow has been idle for too long or +* when there is a permanent processing problem during +* flow offload processing. +* +* Examples of such errors are: +* - offloaded route/dst entry is stale +* - tx function returns error +* - RST flag set (TCP only) +*/ void flow_offload_teardown (struct flow_offload *flow) {clear_bit (IPS_OFFLOAD_BIT , &flow->ct->status);set_bit (NF_FLOW_TEARDOWN , &flow->flags); -flow_offload_fixup_ct (flow->ct); +flow_offload_fixup_ct (flow->ct, +nf_flow_has_expired (flow)); }EXPORT_SYMBOL_GPL (flow_offload_teardown);
Signature · 1 line
2.44.2
Florian Westphal
maintainerPATCH 7/7 netfilter: nft_flow_offload: do not remove flowtable entry for fin packets
In reply to Florian WestphalTreat fin packets like tcp packets with IP options or packets that would need fragmentation: pass them to slow path, but keep the flowtable entry around.
This allows to keep connections where one peer closes early but keeps receiving data for a long time in forwarding fast path.
Conntrack should be moving the nf_conn entry towards a much lower timeout, (default fin_wait 2 minutes).
--- net/netfilter/nf_flow_table_ip.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/nf_flow_table_ip.c +4 -1
@@ -28,11 +28,14 @@ static int nf_flow_state_check(struct flow_offload *flow, int proto,return 0 ; tcph = (void *)(skb_network_header (skb) + thoff); -if (unlikely (tcph->fin || tcph->rst)) { +if (unlikely (tcph->rst)) {flow_offload_teardown (flow);return -1 ; } +if (unlikely (tcph->fin)) +return -1 ; +return 0 ; }
Signature · 1 line
2.44.2