Skip to contentSkip to conversation

nf_conntrack_core: Updated nf_conntrack to destroy/refresh conn irrespective of del_timer status

PATCH In review Kapil Juneja 2 participants 6 messages Feb 25, 2008 – Feb 29, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nf_conntrack_core: Updated nf_conntrack to destroy/refresh conn irrespective of del_timer status

Currently NF_CONNTRACK assumes that a running timer is present before refreshing the connection or destroying it. This may not be the case when, for example, another forwarding engine hooks up to it to listen to new connections but disables the NF_CONNTRACK timer in order to have more control. In such a scenario, only control packets may be terminated to NF_CONNTRACK for it to decode and update the connection status. It will not impact the present scenario of kernel forwarding without the aid of any forwarding engine.

---
 net/netfilter/nf_conntrack_core.c |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)
net/netfilter/nf_conntrack_core.c +10 -6
index a4d5cde..2d1f83c 100644
@@ -791,10 +791,14 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,
 		/* Only update the timeout if the new timeout is at least
 		   HZ jiffies from the old timeout. Need del_timer for race
 		   avoidance (may already be dying). */
-		if (newtime - ct->timeout.expires >= HZ
-		    && del_timer(&ct->timeout)) {
-			ct->timeout.expires = newtime;
-			add_timer(&ct->timeout);
+		if (newtime - ct->timeout.expires >= HZ) {
+			/*
+			 * The timer could have already been deleted
+			 * while still alive (for example connection
+			 * offloaded to a forwarding module other than
+			 * the kernel stack).
+			 */
+			mod_timer(&ct->timeout, newtime);
 			event = IPCT_REFRESH;
 		}
 	}
@@ -928,8 +932,8 @@ nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
 
 	while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
 		/* Time to push up daises... */
-		if (del_timer(&ct->timeout))
-			death_by_timeout((unsigned long)ct);
+		del_timer(&ct->timeout);
+		death_by_timeout((unsigned long)ct);
 		/* ... else the timer will get him soon. */
 
 		nf_ct_put(ct);
Signature · 1 line
1.5.3.3
#1 lore raw
Quoted 7 lines · Kapil Juneja

Currently NF_CONNTRACK assumes that a running timer is present before refreshing the connection or destroying it. This may not be the case when, for example, another forwarding engine hooks up to it to listen to new connections but disables the NF_CONNTRACK timer in order to have more control. In such a scenario, only control packets may be terminated to NF_CONNTRACK for it to decode and update the connection status. It will not impact the present scenario of kernel forwarding without the aid of any forwarding engine.

Do you have a pointer to the code you're talking about?

net/netfilter/nf_conntrack_core.c void __nf_ct_refresh_acct(struct nf_conn *ct, · line 802

Quoted 9 lines
net/netfilter/nf_conntrack_core.c +8 -0
+		if (newtime - ct->timeout.expires >= HZ) {
+			/*
+			 * The timer could have already been deleted
+			 * while still alive (for example connection
+			 * offloaded to a forwarding module other than
+			 * the kernel stack).
+			 */
+			mod_timer(&ct->timeout, newtime);
 			event = IPCT_REFRESH;

This adds a race, we don't want to update the timer if it already went off this that means the connection is already destroyed. Same problem with the other chunk.

#2 lore raw

Hi,
My responses are inlined..

Quoted 17 lines

-----Original Message-----
From: Patrick McHardy [mailto:[email protected]]
Sent: Monday, February 25, 2008 5:41 PM
To: Juneja Kapil
Cc: [email protected]; Medve Emilian
Subject: Re: [PATCH] nf_conntrack_core: Updated nf_conntrack to destroy/refresh conn irrespective of del_timer status

Quoted 1 line · Kapil Juneja

Currently NF_CONNTRACK assumes that a running timer is

present before refreshing

Quoted 1 line

the connection or destroying it. This may not be the case

when, for example,

Quoted 1 line

another forwarding engine hooks up to it to listen to new

connections

Quoted 2 lines

but disables the NF_CONNTRACK timer in order to have more control. In such a scenario, only control packets may be terminated

Quoted 7 lines

to NF_CONNTRACK for

Quoted 1 line

it to decode and update the connection status. It will not

impact the present

Quoted 1 line

scenario of kernel forwarding without the aid of any

forwarding engine.

Do you have a pointer to the code you're talking about?

The forwarding engine concept is same as the Grand Unified Flow Cache idea mooted by Rusty Russel some time back:
http://lwn.net/Articles/194443/

Our architecture runs on three components - Linux NF_CONNTRACK, a Control (Linux) Module(CM), and a Forwarding Engine or Flow Caching system (FC):
1) The CM registers itself to the NF_CONNTRACK notifier chain. Whenever an assured connection notifier event is received, it extracts all the relevant tuple parameters (Src IP, Dst IP, Protocol, Src Port, Dst Port etc.) and caches them to the FC. Due to reasons mentioned subsequently, it also disables the timer for the said conntrack object. The object, however, remains in the conntrack list as long as it is not destroyed.
3) The FC, sitting at the ethernet driver level, sends all the data packets belonging to the cached connection directly to the outbound port (as identified in step 2), bypassing the Linux stack altogether. All the packets not belonging to either of the cached tuples are terminated to the Linux stack. Also TCP control packets FIN/RST/SYN are terminated irrespective of wether the connection is cached or not.
4) With assistance from the FC, the CM also runs aging on the cached connections (hence the requirement of deleting the NF_CONNTRACK timer in step 2)
5) Cached connections can be terminated (i.e, removed from cache) in two ways:
i) Aging out by the CM: In this scenario, the CM removes the
connection tuple from FC as well as NF_CONNTRACK by calling the corresponding timer destroy function directly.
ii) Destroy via TCP control packet: All the FIN-ACK, RST,
RST-ACK packets are send to conntrack irrespective of the fact that they match a cached tuple. They are picked up by the TCP conntrack module which restarts the accounting and refreshes the connection state. It is at this point that the first chunk of this patch comes into picture.
6) When the NF_CONNTRACK module is removed, it iterates through the list to destroy the detected connections. Currently, it does not remove those connections whose timers have gone off (which is the case with connections cached to FC). This is fixed by the second chunk of the patch.

Quoted 15 lines

net/netfilter/nf_conntrack_core.c void __nf_ct_refresh_acct(struct nf_conn *ct, · line 802

Quoted 10 lines
net/netfilter/nf_conntrack_core.c +8 -0
+		if (newtime - ct->timeout.expires >= HZ) {
+			/*
+			 * The timer could have already been deleted
+			 * while still alive (for example connection
+			 * offloaded to a forwarding module other than
+			 * the kernel stack).
+			 */
+			mod_timer(&ct->timeout, newtime);
 			event = IPCT_REFRESH;

This adds a race, we don't want to update the timer if it already went off this that means the connection is already destroyed. Same problem with the other chunk.

A timer call would have invalidated the conntrack by a call to 'death_by_timeout' (or similar such routine), thereby rendering this check redundant. Theoretically, I think the check is irrelevant unless a hypothetical timeout doesn't really invalidate the conntrack. Can you describe the race scenario mentioned by you?

The other chunk for iterative cleaning of list during module removal holds true on similar reasoning.

#3 lore raw
Quoted 51 lines · Juneja Kapil
Quoted 15 lines

-----Original Message-----
From: Patrick McHardy [mailto:[email protected]] Sent: Monday, February 25, 2008 5:41 PM
To: Juneja Kapil
Cc: [email protected]; Medve Emilian
Subject: Re: [PATCH] nf_conntrack_core: Updated nf_conntrack to destroy/refresh conn irrespective of del_timer status

Quoted 1 line · Kapil Juneja

Currently NF_CONNTRACK assumes that a running timer is

present before refreshing

Quoted 1 line

the connection or destroying it. This may not be the case

when, for example,

Quoted 1 line

another forwarding engine hooks up to it to listen to new

connections

Quoted 2 lines

but disables the NF_CONNTRACK timer in order to have more control. In such a scenario, only control packets may be terminated

Quoted 7 lines

to NF_CONNTRACK for

Quoted 1 line

it to decode and update the connection status. It will not

impact the present

Quoted 1 line

scenario of kernel forwarding without the aid of any

forwarding engine.

Do you have a pointer to the code you're talking about?

The forwarding engine concept is same as the Grand Unified Flow Cache idea mooted by Rusty Russel some time back:
http://lwn.net/Articles/194443/

Our architecture runs on three components - Linux NF_CONNTRACK, a Control (Linux) Module(CM), and a Forwarding Engine or Flow Caching system (FC):
1) The CM registers itself to the NF_CONNTRACK notifier chain. Whenever an assured connection notifier event is received, it extracts all the relevant tuple parameters (Src IP, Dst IP, Protocol, Src Port, Dst Port etc.) and caches them to the FC. Due to reasons mentioned subsequently, it also disables the timer for the said conntrack object. The object, however, remains in the conntrack list as long as it is not destroyed.
3) The FC, sitting at the ethernet driver level, sends all the data packets belonging to the cached connection directly to the outbound port (as identified in step 2), bypassing the Linux stack altogether. All the packets not belonging to either of the cached tuples are terminated to the Linux stack. Also TCP control packets FIN/RST/SYN are terminated irrespective of wether the connection is cached or not.
4) With assistance from the FC, the CM also runs aging on the cached connections (hence the requirement of deleting the NF_CONNTRACK timer in step 2)
5) Cached connections can be terminated (i.e, removed from cache) in two ways:
i) Aging out by the CM: In this scenario, the CM removes the
connection tuple from FC as well as NF_CONNTRACK by calling the corresponding timer destroy function directly.
ii) Destroy via TCP control packet: All the FIN-ACK, RST,

Quoted 8 lines

RST-ACK packets are send to conntrack irrespective of the fact that they
match a cached tuple. They are picked up by the TCP conntrack module
which restarts the accounting and refreshes the connection state. It is
at this point that the first chunk of this patch comes into picture. 6) When the NF_CONNTRACK module is removed, it iterates through the list to destroy the detected connections. Currently, it does not remove those
connections whose timers have gone off (which is the case with
connections cached to FC). This is fixed by the second chunk of the
patch.

That sounds pretty reasonable. Is that code available somewhere?

Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
net/netfilter/nf_conntrack_core.c +4 -0
+		if (newtime - ct->timeout.expires >= HZ) {
+			/*
+			 * The timer could have already been deleted
+			 * while still alive (for example connection
Quoted 9 lines

net/netfilter/nf_conntrack_core.c void __nf_ct_refresh_acct(struct nf_conn *ct, · line 802

Quoted 5 lines
net/netfilter/nf_conntrack_core.c +4 -0
+			 * offloaded to a forwarding module other than
+			 * the kernel stack).
+			 */
+			mod_timer(&ct->timeout, newtime);
 			event = IPCT_REFRESH;

This adds a race, we don't want to update the timer if it already went off this that means the connection is already destroyed. Same problem with the other chunk.

Quoted 5 lines

A timer call would have invalidated the conntrack by a call to 'death_by_timeout' (or similar such routine), thereby rendering this check redundant. Theoretically, I think the check is irrelevant unless a hypothetical timeout doesn't really invalidate the conntrack. Can you describe the race scenario mentioned by you?

Very simple:

CPU0 CPU1
timer goes off
refresh_timer: mod_timer, rearm death_by_timeout()

timer goes off again

Using del_timer prevents us from rearming the timer if it already went off.

#4 lore raw

Hi Patrick,

Quoted 86 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Patrick McHardy
Sent: Wednesday, February 27, 2008 6:31 PM
To: Juneja Kapil
Cc: [email protected]; Medve Emilian
Subject: Re: [PATCH] nf_conntrack_core: Updated nf_conntrack to destroy/refresh conn irrespective of del_timer status

Quoted 18 lines · Juneja Kapil
Quoted 18 lines

-----Original Message-----
From: Patrick McHardy [mailto:[email protected]]
Sent: Monday, February 25, 2008 5:41 PM
To: Juneja Kapil
Cc: [email protected]; Medve Emilian
Subject: Re: [PATCH] nf_conntrack_core: Updated nf_conntrack to destroy/refresh conn irrespective of del_timer status

Quoted 1 line · Kapil Juneja

Currently NF_CONNTRACK assumes that a running timer is

present before refreshing

Quoted 1 line

the connection or destroying it. This may not be the case

when, for example,

Quoted 1 line

another forwarding engine hooks up to it to listen to new

connections

Quoted 2 lines

but disables the NF_CONNTRACK timer in order to have more control. In such a scenario, only control packets may be terminated

to NF_CONNTRACK for

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

it to decode and update the connection status. It will not

impact the present

Quoted 1 line

scenario of kernel forwarding without the aid of any

forwarding engine.

Do you have a pointer to the code you're talking about?

The forwarding engine concept is same as the Grand Unified

Flow Cache

Quoted 8 lines

idea mooted by Rusty Russel some time back:
http://lwn.net/Articles/194443/

Our architecture runs on three components - Linux NF_CONNTRACK, a Control (Linux) Module(CM), and a Forwarding Engine or Flow Caching system (FC):
1) The CM registers itself to the NF_CONNTRACK notifier chain. Whenever an assured connection notifier event is received,

it extracts

Quoted 1 line

all the relevant tuple parameters (Src IP, Dst IP,

Protocol, Src Port,

Quoted 4 lines

Dst Port
etc.) and caches them to the FC. Due to reasons mentioned subsequently, it also disables the timer for the said conntrack object. The object, however, remains in the conntrack list

as long as it is not destroyed.

Quoted 3 lines

3) The FC, sitting at the ethernet driver level, sends all the data packets belonging to the cached connection directly to the outbound port (as identified in step 2), bypassing the Linux stack

altogether.

Quoted 3 lines

All the packets not belonging to either of the cached tuples are terminated to the Linux stack. Also TCP control packets FIN/RST/SYN are terminated irrespective of wether the connection is

cached or not.

Quoted 1 line

4) With assistance from the FC, the CM also runs aging on

the cached

Quoted 1 line

connections (hence the requirement of deleting the

NF_CONNTRACK timer

Quoted 2 lines

in step 2)
5) Cached connections can be terminated (i.e, removed from

cache) in

Quoted 6 lines

two
ways:
i) Aging out by the CM: In this scenario, the CM removes the
connection tuple from FC as well as NF_CONNTRACK by calling the corresponding timer destroy function directly.
ii) Destroy via TCP control packet: All the FIN-ACK,

RST, RST-ACK

Quoted 1 line

packets are send to conntrack irrespective of the fact that

they match

Quoted 1 line

a cached tuple. They are picked up by the TCP conntrack

module which

Quoted 1 line

restarts the accounting and refreshes the connection state.

It is at

Quoted 4 lines

this point that the first chunk of this patch comes into picture.
6) When the NF_CONNTRACK module is removed, it iterates through the list to destroy the detected connections. Currently, it does not remove those connections whose timers have gone off (which

Quoted 7 lines

is the case

Quoted 1 line

with connections cached to FC). This is fixed by the second

chunk of

Quoted 1 line

the patch.

That sounds pretty reasonable. Is that code available somewhere?

We are working on licensing aspects and will be glad to share the code as and when we get approval.

Quoted 25 lines
Quoted 15 lines
Quoted 14 lines

net/netfilter/nf_conntrack_core.c void __nf_ct_refresh_acct(struct nf_conn *ct, · line 802

Quoted 10 lines
net/netfilter/nf_conntrack_core.c +8 -0
+		if (newtime - ct->timeout.expires >= HZ) {
+			/*
+			 * The timer could have already been deleted
+			 * while still alive (for example connection
+			 * offloaded to a forwarding module other than
+			 * the kernel stack).
+			 */
+			mod_timer(&ct->timeout, newtime);
 			event = IPCT_REFRESH;

This adds a race, we don't want to update the timer if it already went off this that means the connection is already destroyed. Same problem with the other chunk.

Quoted 2 lines

A timer call would have invalidated the conntrack by a call to 'death_by_timeout' (or similar such routine), thereby

rendering this

Quoted 2 lines

check redundant. Theoretically, I think the check is irrelevant unless a hypothetical timeout doesn't really invalidate the

conntrack.

Quoted 1 line

Can you describe the race scenario mentioned by you?

Very simple:

Quoted 8 lines

CPU0 CPU1
timer goes off
refresh_timer: mod_timer, rearm death_by_timeout()

timer goes off again

Using del_timer prevents us from rearming the timer if it already went off.

Thanks for the explanation. I was probably only thinking of the non-SMP scenarios. However, I feel that if this cannot be done this simple way, then we are in a bit trouble because our need is to make nf_ct_refresh_acct work independent of the existing timer being dead or alive.
While we think about a possible alternatives on respin of patch/control module, can you provide some insight into any other alternatives.

Quoted 6 lines

-
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at
http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 7 lines · Juneja Kapil

Hi Patrick,

Quoted 1 line

That sounds pretty reasonable. Is that code available somewhere?

We are working on licensing aspects and will be glad to share the code as and when we get approval.

OK thanks.

Quoted 10 lines
Quoted 10 lines
Quoted 1 line

Can you describe the race scenario mentioned by you?

Very simple:

CPU0 CPU1
timer goes off
refresh_timer: mod_timer, rearm death_by_timeout()

timer goes off again

Quoted 8 lines
Quoted 1 line

Using del_timer prevents us from rearming the timer if it already went off.

Thanks for the explanation. I was probably only thinking of the non-SMP scenarios. However, I feel that if this cannot be done this simple way, then we are in a bit trouble because our need is to make nf_ct_refresh_acct work independent of the existing timer being dead or alive. While we think about a possible alternatives on respin of patch/control module, can you provide some insight into any other alternatives.

The IPS_FIXED_TIMEOUT_BIT should work I guess. It skips timer updates, but still does accounting (at least in the current kernel version).