Skip to contentSkip to conversation

nf_conntrack

DISCUSSION Yasuyuki Kozakai 3 participants 6 messages Dec 24, 2003 – Mar 2, 2004 netfilter

lore
Share & tools
lore
#0 lore raw

Hi, All,

This is other version of L3 independent connection tracking.

In previous version, the l3 part in tuple is represented by "union" of IPv4 and IPv6 address.

And in this version, size of the l3 part in tuple varies with layer 3 protocol. Then it's more complex than the previous version, but can save memory spaces where many IPv4 connections are created.

I'll measure the speed of 2 version of nf_conntrack from now.

BTW, Harald, Does this satisfy your requirements ?
And I don't know how large impact to cacheline when using "union". Do you have any measurements ?

----------------------------------------
Yasuyuki KOZAKAI

Communication Platform Laboratory,
Corporate Research & Development Center,
Toshiba Corporation

[email protected]
----------------------------------------

#1 lore raw

[explicitly Cc'ing rusty and jozsef since they did a lot of conntrack work]

Quoted 4 lines · Yasuyuki Kozakai

Hi, All,

This is other version of L3 independent connection tracking.

Thank you very much. Sorry for this extremely late reply, but I missed your email from december and just noted it after the current follow-up on this issue.

Quoted 6 lines

In previous version, the l3 part in tuple is represented by "union" of IPv4 and IPv6 address.

And in this version, size of the l3 part in tuple varies with layer 3 protocol. Then it's more complex than the previous version, but can save memory spaces where many IPv4 connections are created.

Yes, this is definitely something we need. Although I still don't like the idea of having to explicitly allocate three pieces of memory (2*tuple, 1*nf_conntrack) per new connection. This will hurt connection setup rate very badly, I expect :(

What I initially had in mind (but don't know if it can be implemented in any reasonable way) was:

- have a static part of struct np_conntrack without tuples
- this static part contains a bitmask
- this bitmask determines which 'extension structures' are present at the end
- possible 'extension structures' might be:
- ipv4 orig/reply tuple
- ipv6 orig/reply tuple
- ipv4 l3 protocol (and helper) data
- ipv6 l3 protocol (and helper) data

I know it sounds very tricky. We always guarantee the ordering of such extension structures. The nf_conntrack core would maintain a list of slab caches for every reasonable combination of extensions structures. The extension structures are always allocated in one chunk gogether with the static nf_conntrack 'header'.

Extension structures would always be accessed via pre-defined macros (which just add an offset depending on the bitmask)

Even thinking further, this would enable us to put more parts than just the tuple into those extensions (let's say the nat or helper union). This in turn means that we can make conntrack structures for the normal case (ipv4 only, no helper, no nat) even smaller.

I'm not sure if it's worth performance-wise. The benefits of such a solution would be:

1) still one chunk of memory for any conntrack
- one allocation at connection setup time
- conntrack+tuple still physically close in memory instead of having to dereference every tuple
2) potentially smaller conntracks even in the current 'normal' case (one of our long-term wishes)

As disadvantage I can see
1) difficult implementation with lots of macros/offset calculation
2) we'd have to determine which slab we came from at destroy() time [maybe save cachep in nf_contrack?]
3) slightly higher computational cost when we want to access extension structures (like tuples), since relative offset has to be calculated at runtime.

What do you think?

Rusty, Jozsef: Do you think it's worth the effort?

Quoted 1 line

Yasuyuki KOZAKAI

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#2 lore raw

Hi, Harald, thank you for replying.

Sory about long message and poor english.

Quoted 11 lines
Quoted 6 lines

In previous version, the l3 part in tuple is represented by "union" of IPv4 and IPv6 address.

And in this version, size of the l3 part in tuple varies with layer 3 protocol. Then it's more complex than the previous version, but can save memory spaces where many IPv4 connections are created.

Yes, this is definitely something we need. Although I still don't like the idea of having to explicitly allocate three pieces of memory (2*tuple, 1*nf_conntrack) per new connection. This will hurt connection setup rate very badly, I expect :(

No.

When L3 protocol modules are registered, my implementation of "dynamic version" calculate the size of 2 tuple + nf_conntrack, and create caches. So

	$ cat /proc/slabinfo |grep nf_conntrack

prints "nf_conntrack:ipv4" and "nf_conntrack:ipv6".

1 chunk includes static part of nf_conntrack and 2 tuples. But memory spaces for helper, nat are statically allocated. Because I wanted to see the impact of dynamic allocating memory space for tuple, at first.

Quoted 12 lines

What I initially had in mind (but don't know if it can be implemented in any reasonable way) was:

- have a static part of struct np_conntrack without tuples
- this static part contains a bitmask
- this bitmask determines which 'extension structures' are present at the end
- possible 'extension structures' might be:
- ipv4 orig/reply tuple
- ipv6 orig/reply tuple
- ipv4 l3 protocol (and helper) data
- ipv6 l3 protocol (and helper) data

Your idea about extension structure (L3/helper/nat union) is acceptable.

But I think tuple of IPv{4,6} should be represented by one structure -- "struct nf_conntrack_tuple".

Your idea seems that ip_conntrack_tuple and ip6_conntrack_tuple are defined individually. Please see nf_conntrack_proto_tcp.c in my implementation. All functions can handle both tuples for IPv{4,6} . But if ip_conntrack_tuple and ip6_conntrack_tuple are defined individually, we have to define 2 version of each functions which do same thing and have just different type of argument "tuple".

	ex) ip_tcp_print_conntrack(ip_conntrack_tuple *tuple);
	    ip6_tcp_print_conntrack(ip6_conntrack_tuple *tuple);

This does't mean "Layer 3 independent".

BTW, unfortunately, I can't spend time for this work this 2 months. Recently I changed my code to fairly compare 2 implementations, and roughly evaluated. As I expected, the performance of "dynamic version" is more worse than "static version".

So I concluded it is desirable to be statically defined the structure of tuple.

Quoted 8 lines

I know it sounds very tricky. We always guarantee the ordering of such extension structures. The nf_conntrack core would maintain a list of slab caches for every reasonable combination of extensions structures. The extension structures are always allocated in one chunk gogether with the static nf_conntrack 'header'.

Extension structures would always be accessed via pre-defined macros (which just add an offset depending on the bitmask)

It is efficient to keep the pointer to extension structure when allocating nf_conntrack instead of calculating offset every access to it.

Quoted 23 lines

Even thinking further, this would enable us to put more parts than just the tuple into those extensions (let's say the nat or helper union). This in turn means that we can make conntrack structures for the normal case (ipv4 only, no helper, no nat) even smaller.

I'm not sure if it's worth performance-wise. The benefits of such a solution would be:

1) still one chunk of memory for any conntrack
- one allocation at connection setup time
- conntrack+tuple still physically close in memory instead of having to dereference every tuple
2) potentially smaller conntracks even in the current 'normal' case (one of our long-term wishes)

As disadvantage I can see
1) difficult implementation with lots of macros/offset calculation
2) we'd have to determine which slab we came from at destroy() time [maybe save cachep in nf_contrack?]
3) slightly higher computational cost when we want to access extension structures (like tuples), since relative offset has to be calculated at runtime.

Quoted 1 line

What do you think?

I think processing speed is more important for connection tracking than saving memory space. But you got a point that unused memory space should not be allocated.

my opinions are

1. It is desirable to be statically defined the structure of tuple. Because
- tuple should be represented by one structure -- nf_conntrack_tuple to get tcp/udp/icmp part in tuple independent of Layer 3 protocol.

- I can't agree with dynamic structure of tuple due to performance issue and difficulty of handling.

2. Your idea about extension structure (L3/helper/nat union) is acceptable. I expect that this impact to performance is small.But I worry about slowdown in the case of "nat helper union". When allocating conntrack, it is difficult to figure out whether conntrack will be processed or not by nat helper module. So I expect that it is needed to reallocate and copy conntrack before nat helper module processes it.

On the other hand, there is no problem about "nat union" (= ip_nat_info). Because if nat module is loaded, all conntrack certainly have "null-binding" or "binding" which specified by user. So conntrack core module always need to allocate memory space for "nat union" in this case.

Regards,

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#3 lore raw
Quoted 3 lines · Yasuyuki Kozakai

When L3 protocol modules are registered, my implementation of "dynamic version" calculate the size of 2 tuple + nf_conntrack, and create caches. So

Oh sorry, I should have noticed that.

Quoted 8 lines
	$ cat /proc/slabinfo |grep nf_conntrack

prints "nf_conntrack:ipv4" and "nf_conntrack:ipv6".

1 chunk includes static part of nf_conntrack and 2 tuples. But memory spaces for helper, nat are statically allocated. Because I wanted to see the impact of dynamic allocating memory space for tuple, at first.

I think this is fine. The 'extension structures' would be a further optimization that are totally unrelated to ipv4/ipv6 generalization and don't really fit this discussion then.

Quoted 2 lines

But I think tuple of IPv{4,6} should be represented by one structure -- "struct nf_conntrack_tuple".

agreed.

Quoted 6 lines

Your idea seems that ip_conntrack_tuple and ip6_conntrack_tuple are defined individually. Please see nf_conntrack_proto_tcp.c in my implementation. All functions can handle both tuples for IPv{4,6} . But if ip_conntrack_tuple and ip6_conntrack_tuple are defined individually, we have to define 2 version of each functions which do same thing and have just different type of argument "tuple".

I see, again agreed.

Quoted 1 line

This does't mean "Layer 3 independent".

;) well at least not for layer 4 protocol helpers.

Quoted 1 line

BTW, unfortunately, I can't spend time for this work this 2 months.

No problem, we're all busy with other stuff anyway. But I think we should finish the mising parts (I've read some FIXME's with regard to helpers, are they still valid?) at about the time that 2.7.x forks off.

Could you write a small TODO list containing the Items that will yet

btw: I will put your current patch into patch-o-matic-ng. This means it will probably get some more users.

Quoted 2 lines

So I concluded it is desirable to be statically defined the structure of tuple.

Yes, indeed.

Quoted 2 lines

It is efficient to keep the pointer to extension structure when allocating nf_conntrack instead of calculating offset every access to it.

Yes, that is an option.

Quoted 3 lines

I think processing speed is more important for connection tracking than saving memory space. But you got a point that unused memory space should not be allocated.

The issue is mostly about wasting cache lines. Memory is cheap, but cache is very limited... ip_conntrack is currently 304 bytes of size. If we could somehow get this down to 256 bytes, we would save a whole cache line per connection, that's quite a lot.

Also, we have increasing demands for adding information to the conntrack table. Some people want to add counters in order to facilitate connection based accounting.

Quoted 3 lines

my opinions are

1. It is desirable to be statically defined the structure of tuple.

ok, you convinced me ;)

Quoted 6 lines

2. Your idea about extension structure (L3/helper/nat union) is acceptable. I expect that this impact to performance is small.But I worry about slowdown in the case of "nat helper union". When allocating conntrack, it is difficult to figure out whether conntrack will be processed or not by nat helper module. So I expect that it is needed to reallocate and copy conntrack before nat helper module processes it.

it's actually not that difficult. When we allocate a conntrack, we already know the tuple. And from the tuple, we already know if there is a conntrack helper. So let's just assume if iptable_nat is loaded and we have a conntrack helper responsible for this tuple, we will later need the nat helper union. There are few protocols that only have a nat helper but no conntrack helper. In that case one would have to either re-allocate, or force them to have a dummy conntrack helper.

We really should optimize for the non-nat non-helper case, since it is by far the most common case. It's just a pity that we're currently wasting so much memory/cache for 90% of all connections that don't even

Quoted 5 lines

On the other hand, there is no problem about "nat union" (= ip_nat_info). Because if nat module is loaded, all conntrack certainly have "null-binding" or "binding" which specified by user. So conntrack core module always need to allocate memory space for "nat union" in this case.

Yes, but what happens in cases where you alrady have entries in the conntrack table at the time of loading iptable_nat ? Then the old conntracks don't have the nat union, ... - but I think these are minor issues that could be resolved.

Quoted 1 line

Regards,

Thanks for your feedback.

Signature · 5 lines
- Harald Welte <[email protected]>             http://www.netfilter.org/
============================================================================
  "Fragmentation is like classful addressing -- an interesting early
   architectural error that shows how much experimentation was going
   on while IP was being designed."                    -- Paul Vixie
#4 lore raw

Hi, Harald,

From: Harald Welte <[email protected]>
Date: Wed, 25 Feb 2004 16:05:54 +0100

Quoted 8 lines
Quoted 4 lines

1 chunk includes static part of nf_conntrack and 2 tuples. But memory spaces for helper, nat are statically allocated. Because I wanted to see the impact of dynamic allocating memory space for tuple, at first.

I think this is fine. The 'extension structures' would be a further optimization that are totally unrelated to ipv4/ipv6 generalization and don't really fit this discussion then.

I see.

Quoted 7 lines
Quoted 1 line

BTW, unfortunately, I can't spend time for this work this 2 months.

No problem, we're all busy with other stuff anyway. But I think we should finish the mising parts (I've read some FIXME's with regard to helpers, are they still valid?) at about the time that 2.7.x forks off.

Could you write a small TODO list containing the Items that will yet

ok.
1. change to allocate nf_conntrack with various size depended on features.
2. implement the code handling fragmented packets
3. support for various helper. At least, helpers in current main kernel.
4. apply fixes and changes for latest ip_conntrack.
5. wrapper for current ip_conntrack and ip_nat, if I have any time. (or is it more efficient to change all nat and helper modules ?)

Quoted 2 lines

btw: I will put your current patch into patch-o-matic-ng. This means it will probably get some more users.

Thank you. Please notice that I'll implement 1 in TODO list based on "static size of nf_conntrack" version. So I'll change many codes.

Quoted 8 lines
Quoted 3 lines

I think processing speed is more important for connection tracking than saving memory space. But you got a point that unused memory space should not be allocated.

The issue is mostly about wasting cache lines. Memory is cheap, but cache is very limited... ip_conntrack is currently 304 bytes of size. If we could somehow get this down to 256 bytes, we would save a whole cache line per connection, that's quite a lot.

Oh, sorry about missunderstading. I got it.

Quoted 7 lines

it's actually not that difficult. When we allocate a conntrack, we already know the tuple. And from the tuple, we already know if there is a conntrack helper. So let's just assume if iptable_nat is loaded and we have a conntrack helper responsible for this tuple, we will later need the nat helper union. There are few protocols that only have a nat helper but no conntrack helper. In that case one would have to either re-allocate, or force them to have a dummy conntrack helper.

I agree.

Quoted 10 lines
Quoted 5 lines

On the other hand, there is no problem about "nat union" (= ip_nat_info). Because if nat module is loaded, all conntrack certainly have "null-binding" or "binding" which specified by user. So conntrack core module always need to allocate memory space for "nat union" in this case.

Yes, but what happens in cases where you alrady have entries in the conntrack table at the time of loading iptable_nat ? Then the old conntracks don't have the nat union, ... - but I think these are minor issues that could be resolved.

I imagine that bit flags is checked and conntrack is reallocated if needed. The cost is small because reallocating is needed only once per conntrack processed by nat helper.

In the case that nat module is unloaded, cache isn't destroyed until all nat enabled conntrack is free.

Thank you for your help and suggestions.

-----------------------------------------------------------------
Yasuyuki KOZAKAI @ USAGI Project <[email protected]>

#5 lore raw
In reply to Harald Welte

Hi,

Quoted 11 lines · Harald Welte
Quoted 6 lines

In previous version, the l3 part in tuple is represented by "union" of IPv4 and IPv6 address.

And in this version, size of the l3 part in tuple varies with layer 3 protocol. Then it's more complex than the previous version, but can save memory spaces where many IPv4 connections are created.

Yes, this is definitely something we need. Although I still don't like the idea of having to explicitly allocate three pieces of memory (2*tuple, 1*nf_conntrack) per new connection. This will hurt connection setup rate very badly, I expect :(

I don't like allocating in chunks either.

Quoted 45 lines

What I initially had in mind (but don't know if it can be implemented in any reasonable way) was:

- have a static part of struct np_conntrack without tuples
- this static part contains a bitmask
- this bitmask determines which 'extension structures' are present at the end
- possible 'extension structures' might be:
- ipv4 orig/reply tuple
- ipv6 orig/reply tuple
- ipv4 l3 protocol (and helper) data
- ipv6 l3 protocol (and helper) data

I know it sounds very tricky. We always guarantee the ordering of such extension structures. The nf_conntrack core would maintain a list of slab caches for every reasonable combination of extensions structures. The extension structures are always allocated in one chunk gogether with the static nf_conntrack 'header'.

Extension structures would always be accessed via pre-defined macros (which just add an offset depending on the bitmask)

Even thinking further, this would enable us to put more parts than just the tuple into those extensions (let's say the nat or helper union). This in turn means that we can make conntrack structures for the normal case (ipv4 only, no helper, no nat) even smaller.

I'm not sure if it's worth performance-wise. The benefits of such a solution would be:

1) still one chunk of memory for any conntrack
- one allocation at connection setup time
- conntrack+tuple still physically close in memory instead of having to dereference every tuple
2) potentially smaller conntracks even in the current 'normal' case (one of our long-term wishes)

As disadvantage I can see
1) difficult implementation with lots of macros/offset calculation
2) we'd have to determine which slab we came from at destroy() time [maybe save cachep in nf_contrack?]
3) slightly higher computational cost when we want to access extension structures (like tuples), since relative offset has to be calculated at runtime.

Quoted 1 line

What do you think?

Definitely this is the way we should go.

Even NAT-PT could be fitted into it, with the overhead of allocating new conntrack entries at NAT-PT. Maybe Henrik's suggestion to use a special tun device would be more optimal from performance point of view, with the cost of two conntrack entries for each protocol-translated session.

Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics

          H-1525 Budapest 114, POB. 49, Hungary