updated ctnetlink patches
For some reason my mail didn't make it to netfilter-devel, i assume its because of the big patches. For this reason, i'm sending the patches serperated from the mail. -------------------[original mail]------------------------------------------------
I've done some work on ctnetlink, here is what i've got so far. The patch is still incomplete, i just hope for some comments. I'll try to sum up the changes and some thoughts as good as i can. To keep the reader motivated, attached is a small program which enables you to do basic state synchronisation between two conntracks.
Changes:
- Attributes:
Removed CTA_IIF and CTA_OIF, there is no relation between conntrack and interfaces. I also don't see what meaning CTA_INFO (ctinfo) could have outside of packet processing context, but i left it in for now.
- Creation and manipulation of conntrack entries:
It is possible now to create and change conntrack entries. Only confirmed entries are accessible trough ctnetlink, also only confirmed ones can be created. All attributes except CTA_HELPINFO and CTA_NATINFO are implemented. For NATINFO probably another ugly pointer somewhere is required to access either functions inside ip_nat_core or nat_lock without requiring nat to be loaded.
- Conntrack event notifications:
The patch adds event notifications to conntrack. Since there are very much events, it is tried to cache them where possible and to deliver all cached ones at once. Still, there are a lot. The notifications can be disabled at compile time while keeping parts of ctnetlink that don't rely on them working. Also noteworthy is the fact that the NEW event is in fact CONFIRM since there is no way to access unconfirmed connections. The DESTROY event is currently in destroy_conntrack, this should probably move to clean_from_lists. ip_ct_refresh is changed to only update the timer if timer.expires != jiffies+extra_jiffies. This reduces timeout change messages to at most HZ/s instead of 1/packet. I'm unclear if this change introduces a race condition of some kind.
- ctnetlink event messages:
ctnetlink sends out a message for each conntrack event. There are two types of messages,CTNL_MSG_NEWCONNTRACK and CTNL_MSG_DELCONNTRACK. Each message includes at least
the CTA_ORIG/CTA_RPLY attributes, plus all attributes that changed. Messages for new conntrack
entries include all available data, but at least CTA_ORIG/CTA_RPLY/CTA_TIMEOUT/
CTA_PROTOINFO/CTA_STATUS. These are also the minimum required attributes to create new
entries. There are no special flags set for this message at the moment, NLM_F_CREATE | NLM_F_EXCL would make sense to distinguish new entries from changes. I have to check what rtnetlink is doing. There are currently no messages for things done on behalf of ctnetlink itself.
-Table dumping:
Previous code crashed (known issue i think), also if it worked entries would have been dumped multiple times if there was no room in the skb while in the middle of dumping a hash chain. With my current solution confirmed entries are assigned a unique increasing id and held in an ordered list. The last id successfully dumped is stored in the struct netlink_callback and after an interruption it is continued with the next one. Unfortunately this adds two new fields to struct ip_conntrack. The id field could be useful for ctnetlink itself (since tuples can be reused it is the only unique identifier).
Some thoughts/problems for discussion:
- There are too much messages, messages get lost if the socket receive buffer is exhausted. I only tested on UML so it might have been just too slow.
- If NEW message is lost all further changes are pointless. One could make messages redundant by always including the attributes required to create new entries. OTOH, this is only interesting for state synchronisation, not for other listeners. Different messages for different multicast groups ? This is probably needlessly expensive, the overhead of the extra attributes is not much. It could of course also be handled in userspace (but with high overhead).
- Helpers: should it be possible to assign arbitary helpers to connections (respecting protocol, disrespecting ports) or not ? Should connections created on behalf of ctnetlink be assigned a helper automatically if there is a matching one ?
- nat: masq_index is current not included in NATINFO. could it be useful ?
Ok my brain is empty now, if i remember some more i'll write a new message ..
There are three patches:
ctnetlink-0.11-0.12.diff - incremental, expects CONNMARK and ctnetlink-0.11 patched kernel ctnetlink-0.12.diff - expects CONNMARK patched kernel
connmark.diff - patch to CONNMARK for event notifications
Sorry about expecting a connmark patched kernel, but i started to work on ctnetlink because i wanted to change connection marks ..
For state-syncronization testing there is a small programm. It encapsulates netlink messages in udp and sends them out. The receiver sets NLM_F_REQUEST and passes them to the kernel.
Compile with (in libctnetlink directory):
gcc -I../include -I/usr/src/linux/include -lnfnetlink ctsyncd.c -o ctsyncd
On one side: ./ctsynd listen
Other side: ./ctsynd <ip>
Looking forward for suggestions/comments.
Best regards,
Patrick
PS: I guess many here are aware of it, there is a draft on "Netlink as an IP services protocol" with many information on netlink:
http://www.ietf.org/proceedings/01dec/I-D/draft-ietf-forces-netlink-01.txt
Quoted 3 lines · Patrick McHardy
I've done some work on ctnetlink, here is what i've got so far. The patch is still incomplete,
First of all, thanks a lot for contributing to ctnetlink.
Quoted 3 lines
- Attributes:
RemovedCTA_IIFandCTA_OIF, there is no relation between conntrack and interfaces.
oh yes, this is I think my fault. At that time I was doing a project that required interface tracking in conntrack.
Quoted 4 lines
- Creation and manipulation of conntrack entries:
It is possible now to create and change conntrack entries. Only confirmed entries are accessible trough ctnetlink, also only confirmed ones can be created.
Makes sense, I don't see a need for this to be changed
Quoted 1 line
All attributes except
CTA_HELPINFOandCTA_NATINFOare implemented.
we should get HELPINFO implemented,
Quoted 3 lines
For NATINFO probably another ugly pointer somewhere is required to access either functions inside
ip_nat_coreornat_lockwithout requiring nat to be loaded.
mh.
Quoted 6 lines
- Conntrack event notifications:
The patch adds event notifications to conntrack. Since there are very much events, it is tried to cache them where possible and to deliver all cached ones at once. Still, there are a lot. The notifications can be disabled at compile time while keeping parts of ctnetlink that don't rely on them working.
Yes, this is the way to go.
Quoted 4 lines
Also noteworthy is the fact that the NEW event is in fact CONFIRM since there is no way to access unconfirmed connections. The DESTROY event is currently in
destroy_conntrack, this should probably move toclean_from_lists.
ACK. We can have conntracks that are forced to live longer because some sibling is still holding a refcount on it. But userspace should get notified once the entry is deactivated - that is, at the time it is removed from the lists. This change would also make it symmetric (NEW==confirm, DESTROY==remove_from_lists
Quoted 5 lines
ip_ct_refreshis changed to only update the timer if timer.expires != jiffies+extra_jiffies. This reduces timeout change messages to at most HZ/s instead of 1/packet.I'm unclear if this change introduces a race condition of some kind.
I don't think so.
Quoted 2 lines
There are currently no messages for things done on behalf of ctnetlink itself.
This means that listener B doesn't get notified if listener A adds/changes something via ctnetlink. This needs to get implemented in order to sanely support multiple listeners
Quoted 2 lines
-Table dumping:
Previous code crashed (known issue i think),
Yes, Martin has a fix for it.
Quoted 3 lines
also if it worked entries would have been dumped multiple times if there was no room in the skb while in the middle of dumping a hash chain.
true, but not too problematic. If we send the address of the conntrack entry as ID to userspace, userspace could detect those copies. But it's not nice, of course.
Quoted 2 lines
With my current solution confirmed entries are assigned a unique increasing id and held in an ordered list.
Mh, this is the part i really dislike (sorry). The unique ID is IMHO going to be expensive (the cacheline of this ID will always ping-pong, as heavy contention is to be expected). And the ordered list.. is it really necessarry to yet again increase the size of ip_conntrack?
Quoted 5 lines
The last id successfully dumped is stored in the struct
netlink_callbackand after an interruption it is continued with the next one. Unfortunately this adds two new fields to structip_conntrack. The id field could be useful for ctnetlink itself (since tuples can be reused it is the only unique identifier).
yes, but passing the address of the conntrack as ID would be much easier than than a unique sequential ID.
Or what about using a timestamp as ID? it would not be unique, but at least you can say something like 'entries created before jiffies==foo have already been sent'.
This still doesn't eliminate the need for the ordered list. We need to find a way around that.
Quoted 4 lines
Some thoughts/problems for discussion:
- There are too much messages, messages get lost if the socket receive buffer is exhausted. I only tested on UML so it might have been just too slow.
did you try to increase the buffer?
Quoted 4 lines
- If NEW message is lost all further changes are pointless. One could make messages redundant by always including the attributes required to create new entries. OTOH, this is only interesting for state synchronisation, not for other listeners.
No, I don't think this is necessarry. If a sync receiver gets updates for an entry it doesn't have, it can issue an request to receive a full [new] copy of this entry.
Quoted 4 lines
Different messages for different multicast groups ? This is probably needlessly expensive, the overhead of the extra attributes is not much. It could of course also be handled in userspace (but with high overhead).
Well, I don't care about the overhead in userspace if it's about a rare case (message loss).
Quoted 2 lines
- Helpers: should it be possible to assign arbitary helpers to connections (respecting protocol, disrespecting ports) or not ?
might be a nice feature to have, but I wouldn't consider this important
Quoted 2 lines
Should connections created on behalf of ctnetlink be assigned a helper automatically if there is a matching one ?
yes.
Quoted 4 lines
- nat:
masq_indexis current not included in NATINFO. could it be useful ?Ok my brain is empty now, if i remember some more i'll write a new message ..
I can imagine...
a couple of thoughts:
- stuff like 'conntrack->proto.tcp = *(struct ip_ct_tcp *)p;' makes the compiler to generate inline memcpy, which is less optimal than the kernel-provided asm memcpy implementation, so let's not do this.
- you add two new callback functions 'new' and 'change' to ip_conntrack_protocol. Can't we unify those two somehow?
Quoted 2 lines
Sorry about expecting a connmark patched kernel, but i started to work on ctnetlink because i wanted to change connection marks ..
no problem, we can 'fix' this later.
Thanks once again, keep up the good work.
Quoted 2 lines
Best regards,
Patrick
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
Hi Harald,
thanks for you suggestions.
Quoted 5 lines · Harald Welte
Quoted 2 lines
All attributes except
CTA_HELPINFOandCTA_NATINFOare implemented.we should get HELPINFO implemented,
already done. i'm still reading into nat and building a userspace utility for testing, but i will probably update the patches (without nat) in the next couple of days.
Quoted 7 lines
Quoted 6 lines
Also noteworthy is the fact that the NEW event is in fact CONFIRM since there is no way to access unconfirmed connections. The DESTROY event is currently in
destroy_conntrack, this should probably move toclean_from_lists.
Quoted 7 lines
ACK. We can have conntracks that are forced to live longer because some sibling is still holding a refcount on it. But userspace should get notified once the entry is deactivated - that is, at the time it is removed from the lists. This change would also make it symmetric (NEW==confirm, DESTROY==remove_from_lists
also done.
Quoted 5 lines
Quoted 5 lines
ip_ct_refreshis changed to only update the timer if timer.expires != jiffies+extra_jiffies. This reduces timeout change messages to at most HZ/s instead of 1/packet.
Quoted 7 lines
Quoted 3 lines
I'm unclear if this change introduces a race condition of some kind.
I don't think so.
then this change would probably also make sense for mainline kernel. i was even thinking about going a bit further and only doing changes if they are bigger than say 1s, it doesn't seem to matter. if not that, maybe reduce only event messages to 1/s for timer changes.
Quoted 9 lines
Quoted 4 lines
There are currently no messages for things done on behalf of ctnetlink itself.
This means that listener B doesn't get notified if listener A adds/changes something via ctnetlink. This needs to get implemented in order to sanely support multiple listeners
yes i'm aware of that. i delayed it for now because i have to make sure there are no "indirect" event messages by calling conntrack functions from nfnetlink_conntrack that send some, so i'm waiting until that part stablizes.
Quoted 13 lines
Quoted 4 lines
-Table dumping:
Previous code crashed (known issue i think),Yes, Martin has a fix for it.
Quoted 5 lines
also if it worked entries would have been dumped multiple times if there was no room in the skb while in the middle of dumping a hash chain.
Quoted 4 lines
true, but not too problematic. If we send the address of the conntrack entry as ID to userspace, userspace could detect those copies. But it's not nice, of course.
problems can occur if one chain contains more entries that there is room in a skb. that chain will be dumped over and over again. i don't see how this can be prevented without my changes.
Quoted 11 lines
Quoted 4 lines
With my current solution confirmed entries are assigned a unique increasing id and held in an ordered list.
Mh, this is the part i really dislike (sorry). The unique ID is IMHO going to be expensive (the cacheline of this ID will always ping-pong, as heavy contention is to be expected). And the ordered list.. is it really necessarry to yet again increase the size of
ip_conntrack?
i don't like that part either, but how could the problem mentioned before be solved without it ?
Quoted 11 lines
Quoted 7 lines
The last id successfully dumped is stored in the struct
netlink_callbackand after an interruption it is continued with the next one. Unfortunately this adds two new fields to structip_conntrack. The id field could be useful for ctnetlink itself (since tuples can be reused it is the only unique identifier).yes, but passing the address of the conntrack as ID would be much easier than than a unique sequential ID.
Quoted 7 lines
Or what about using a timestamp as ID? it would not be unique, but at least you can say something like 'entries created before jiffies==foo have already been sent'.
This still doesn't eliminate the need for the ordered list. We need to find a way around that.
yes i would like that too. the unique id doesn't seem to be very important, i just mentioned it to sweeten the bigger ip_conntrack structure ;)
Quoted 9 lines
Quoted 6 lines
Some thoughts/problems for discussion:
- There are too much messages, messages get lost if the socket receive buffer is exhausted. I only tested on UML so it might have been just too slow.did you try to increase the buffer?
yes, with uml it didn't help much. now i'm running it between my gateway and my workstation and there are no drops until now.
Quoted 7 lines
Quoted 6 lines
- If NEW message is lost all further changes are pointless. One could make messages redundant by always including the attributes required to create new entries. OTOH, this is only interesting for state synchronisation, not for other listeners.
Quoted 5 lines
No, I don't think this is necessarry. If a sync receiver gets updates for an entry it doesn't have, it can issue an request to receive a full [new] copy of this entry.
some firewalls with failover-capabilities also seem to do periodic polls of the state table. i suppose it could be cheaper than event messages under some circumstances.
Quoted 10 lines
Quoted 6 lines
Different messages for different multicast groups ? This is probably needlessly expensive, the overhead of the extra attributes is not much. It could of course also be handled in userspace (but with high overhead).
Well, I don't care about the overhead in userspace if it's about a rare case (message loss).
Currently it is not necessary to be aware of the conntrack table in userspace. If you want to make messages redundant in userspace you'll have to replicate it.
Quoted 8 lines
Quoted 4 lines
- Helpers: should it be possible to assign arbitary helpers to connections (respecting protocol, disrespecting ports) or not ?
might be a nice feature to have, but I wouldn't consider this important
i left it out for know.
Quoted 8 lines
Quoted 4 lines
Should connections created on behalf of ctnetlink be assigned a helper automatically if there is a matching one ?
yes.
i'll change that.
Quoted 6 lines
a couple of thoughts:
- stuff like '
conntrack->proto.tcp= *(structip_ct_tcp*)p;' makes the compiler to generate inline memcpy, which is less optimal than the kernel-provided asm memcpy implementation, so let's not do this.
i'll also change that.
Quoted 4 lines
- you add two new callback functions 'new' and 'change' to
ip_conntrack_protocol. Can't we unify those two somehow?
currently only difference is that change takes locks for some protocols. we could of course always take the lock.
Quoted 3 lines
Thanks once again, keep up the good work.
Thanks ;)
Patrick
Quoted 9 lines · Patrick McHardy
Quoted 9 lines
Quoted 8 lines
ip_ct_refreshis changed to only update the timer if timer.expires != jiffies+extra_jiffies. This reduces timeout change messages to at most HZ/s instead of 1/packet.I'm unclear if this change introduces a race condition of some kind.
Quoted 8 lines
Quoted 3 lines
I don't think so.
then this change would probably also make sense for mainline kernel. i was even thinking about going a bit further and only doing changes if they are bigger than say 1s, it doesn't seem to matter. if not that, maybe reduce only event messages to 1/s for timer changes.
Look at the last part of optimizations/ip_ct_refresh_optimization.patch I've been running with that patch for a long time and it works fine.
Signature · 1 line
/Martin
Quoted 10 lines · Martin Josefsson
Quoted 6 lines
then this change would probably also make sense for mainline kernel. i was even thinking about going a bit further and only doing changes if they are bigger than say 1s, it doesn't seem to matter. if not that, maybe reduce only event messages to 1/s for timer changes.
Look at the last part of
optimizations/ip_ct_refresh_optimization.patchI've been running with that patch for a long time and it works fine.
thanks, i will add it to the patch.
Best regards,
Patrick