iptables/conntrack in enterprise environment.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I am in an enterprise environment and I'm having some problems with conntrack specifically.
We have a system that acts as a router, however any new inbound connection for any machine behind this router is re-directed to a specific port on the local machine, where an application responds as if it were the system behind the router. These systems experience some very high volumes of traffic (sustaining over 30mbit of traffic). Heres a breakdown of TCP socket connections by status at one particular point in time:
ESTABLISHED : 1363LAST_ACK : 27TIME_WAIT : 616FIN_WAIT2 : 8FIN_WAIT1 : 140SYN_RECV : 6188CLOSE_WAIT : 365
LISTEN : 3
CLOSING : 5
We have multiple systems performing this task (essentially for load balancing and to remove a single point of faulure). The systems are dual 1ghz pentium 3's, with 1-2gb of ram, so they're not shy systems. They're running 2.4.20 kernels (mostly vanilla) with iptables 1.2.7a.
Here are some system limits I am tweaking (ie. the commands to do the tweaking):
echo 1 >/proc/sys/net/ip_forward
echo 524280 >/proc/sys/fs/file-max
echo 524280 >/proc/sys/net/ipv4/ip_conntrack_max
echo 65535 >/proc/sys/net/ipv4/ip_queue_maxlen
echo 65535 >/proc/sys/net/ipv4/tcp_max_syn_backlog
# NONE EST SYN_S SYN_R FIN_W TIME_W CLOSE CLOSE_W LAST_A
LISTEN
echo "1800 21600 120 60 30 30 10 30 30
120 " > /proc/sys/net/ipv4/ip_conntrack_tcp_timeouts
ulimit -H -n 524280
ulimit -S -n 524280
iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS
- --clamp-mss-to-pmt
Because every new connection to one of the systems 'behind' these systems need to be re-directed to a local port, which is achieved with the command:/sbin/iptables -t nat -A PREROUTING -j DNAT -i eth0 -p tcp -d <ip-range>
- --destination-port 1024:65535 --to-destination <local_ip>:<local_port>
Every inbound connection incurs an entry in the connection tracking table. It seems, however, that we may be overloading the conntrack system. I can telnet to a different port listening on a secondary (internal) interface (but the same application), that bypasses the above rule, and get an immediate connection, however establishing a connection 'to' a server behind this router can take a number of seconds, and sometimes may never establish. Whats more connecting directly to the port everything else is being re-directed to via. the 'public' interface itself can take some time (though not as long as connecting 'to' a system behind this box).
The conntrack table itself very quickly grows - but it does not clean itself up when the connection itself dissapears, instead it waits for some pre-determined timeout value, which means even though, as shown above, the number of connections in-progress (one way or another) is about 8000 connections, the conntrack table is absolutely huge (hundreds of thousands of entries), and as time goes on, the larger it gets. To try to combat this, I've reduced the biggest timer (how long an established connection stays in conntrack) from 5 days to 6 hours (all the connections we have are, and should be, short lived, so that is plenty of time). This helps a bit, however I'm still at a loss to try to understand why conntrack does not clean itself up when the connection gets closed.
Of course, seeing how big the conntrack table is, itself, impacts the system dramatically. The 'wc -l /proc/net/ip_conntrack' command takes a long time to run, and brings the 'active' processing to its knees while doing this.
- From all appearances, it appears conntrack is hamstringing us. It appears it is not able to properly handle large-traffic systems, especially where essentially every connection going through the system is nat'd.
I'd appreciate ANY help or thoughts on how to remedy this issue, as I have said, this is in use in an enterprise environment (which of course, means I cannot divulge the purpose of the application I mentioned earlier (however all you really need to know is the application does not really know (or care) weather the connection to it is nat'd, and just uses it as a standard socket connection), however I can give details on the system/kernel/netfilter configuration as necessary, just let me know what further information you require).
I thank you in advance, and apologise for mailing all 3 lists, however I figured someone on ONE of these lists would have an idea or a suggestion.
- --
PreZ
Systems Administrator
Shadow Realm
PGP FingerPrint: B3 0C F3 32 DE 5A 7D 90 26 F6 FA 38 CC 0A 2D D8 Finger [email protected] for full PGP public key.
Shadow Realm, a hobbyist ISP supplying real internet services.
http://www.srealm.net.au
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE+1ZcVKFp14D8AGEQRAh4jAJ9mhilrpVsDvakS03re/HsT1jcXcwCcDqFT nHqa0y2UPb9s5JgRsGIhP8o=
=bmA5
-----END PGP SIGNATURE-----
Quoted 6 lines · Preston A. Elder
Hi,
I am in an enterprise environment and I'm having some problems with conntrack specifically.
They're running 2.4.20 kernels (mostly vanilla) with iptables 1.2.7a.
Do not use 2.4.20 if you want to use connection tracking. 2.4.20 connection tracking is totally broken due to a change introduced in the core kernel.
Please do always use patch-o-matic from CVS. The patch you want to apply for fixing this bug is 10_confirm_fix.patch
Also, considering
Quoted 1 line
echo 524280 >/proc/sys/net/ipv4/ip_conntrack_max
without using a larger hash size (modprobe ip_conntrack hashsize=foo, wherer foo should be a prime number and in the range of 524280/2)
Quoted 7 lines
to be re-directed to a local port, which is achieved with the command:
/sbin/iptables-t nat -A PREROUTING -j DNAT -i eth0 -p tcp -d <ip-range>
- --destination-port 1024:65535 --to-destination <local_ip>:<local_port>Every inbound connection incurs an entry in the connection tracking table. It seems, however, that we may be overloading the conntrack system.
I've seen systems with way more conntrack entries and higher bandwith. Using NAT however, might have a big performance impact.
Quoted 3 lines
The conntrack table itself very quickly grows - but it does not clean itself up when the connection itself dissapears, instead it waits for some pre-determined timeout value,
With a non-broken kernel it is 2 minutes, that is TIME_WAIT of a TCP socket.
Quoted 3 lines
PreZ
Systems Administrator
Shadow Realm
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
In message <[email protected]> you write:
Quoted 4 lines
We have a system that acts as a router, however any new inbound connection for any machine behind this router is re-directed to a specific port on the local machine, where an application responds as if it were the system behind the router. These systems experience some very high volumes of traffic
Sounds a lot like something is happening to the FIN or RST packets: as soon as conntrack sees a FIN or RST, it'll leave ESTABLISHED and timeout fairly quickly.
Now, there are several simple things you can do here, as well as dropping the ESTABLISHED timeout:
1) Try 2.4.21-rc5. The hashing algorithm was markedly improved, which you could well be hitting.
2) You set the conntrack_max to 524280, but on a 1GB machine you'll only have about 8192 hash chains, making each chain 64 long. Up the hashsize module parameter to around 100000 (or edit the kernel source if builtin, sorry).
There are certainly more intrusive measures we can investigate, too.
Quoted 2 lines
We have multiple systems performing this task (essentially for load balancing and to remove a single point of faulure).
Um, how are you load balancing? Remember, if the connection tracking code doesn't see all the packets for a connection, it can't work.
Cheers,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 6 lines · Harald Welte
Quoted 5 lines · Preston A. Elder
Hi,
I am in an enterprise environment and I'm having some problems with conntrack specifically.
Quoted 8 lines
Quoted 1 line
They're running 2.4.20 kernels (mostly vanilla) with iptables 1.2.7a.
Do not use 2.4.20 if you want to use connection tracking. 2.4.20 connection tracking is totally broken due to a change introduced in the core kernel.
Please do always use patch-o-matic from CVS. The patch you want to apply for fixing this bug is 10_confirm_fix.patch
This patch was applied already. Infact, I'm using Gentoo (but using the 'vanilla' kernel from gentoo, so its not got every patch under the sun), and I specifically picked out and applied the followint patches:
018_gcc31-compile-optimizations
021_ecc-20020904
701_iptables-20_iptables-proc
702_iptables-24_conntrack-nosysctl
722_iptables-tcplimit
724_iptables-u32
741_iptables-ip_conntrack_find 742_iptables-ip_ct_refresh_optimization 744_iptables-03_ip_conntrack_proto_tcp-lockfix 747_iptables-06_ftp-conntrack-msg-fix 748_iptables-07_ECN-tcpchecksum-littleendian-fix 752_iptables-10_confirm_fix
753_iptables-10_local-nat-expectfn 759_iptables-24_conntrack-modify-after-free-fix 760_iptables-25_ip_tables-comment-fix 766_iptables-33_ipqueue_memoryleak
764_iptables-31_nat_parse_fix
770_iptables-ip_conntrack-timeouts
900_quick-fixes
902_minor_fixes
Please let me know if you think one is missing I should try.
Quoted 6 lines
Also, considering
Quoted 1 line
echo 524280 >/proc/sys/net/ipv4/ip_conntrack_max
without using a larger hash size (modprobe
ip_conntrackhashsize=foo, wherer foo should be a prime number and in the range of 524280/2)
I'll try this and report back.
Quoted 10 lines
Quoted 7 lines
to be re-directed to a local port, which is achieved with the command:
/sbin/iptables-t nat -A PREROUTING -j DNAT -i eth0 -p tcp -d <ip-range>
- --destination-port 1024:65535 --to-destination <local_ip>:<local_port>Every inbound connection incurs an entry in the connection tracking table. It seems, however, that we may be overloading the conntrack system.
I've seen systems with way more conntrack entries and higher bandwith. Using NAT however, might have a big performance impact.
Well, this system itself isn't doing 'nat', however, by implication, the above rule makes every connection nat'd.
Quoted 6 lines
Quoted 3 lines
The conntrack table itself very quickly grows - but it does not clean itself up when the connection itself dissapears, instead it waits for some pre-determined timeout value,
With a non-broken kernel it is 2 minutes, that is
TIME_WAITof a TCP socket.
That was not my point. My point was, for up to 5 days later, the system still has entries in the conntrack table (listed as 'ESTABLISHED'), which have been dead and gone for a long time, conntrack does not realise that connection is utterly closed, and it should drop its conntrack entry. I'm not as worried about the lower-value timeouts, but as I said, I saw ALOT of established connections hanging around in the conntrack table (making the conntrack table about 200,000 entries long, give or take), most of which were entries for connections already closed.
- --
PreZ
Systems Administrator
Shadow Realm
PGP FingerPrint: B3 0C F3 32 DE 5A 7D 90 26 F6 FA 38 CC 0A 2D D8 Finger [email protected] for full PGP public key.
Shadow Realm, a hobbyist ISP supplying real internet services.
http://www.srealm.net.au
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE+1finKFp14D8AGEQRAmT5AJ48csFfxVIMJQykL5mhG7VQzx/79wCgjmQ1 drFKsTbUeJ8F0EEicWgBosQ=
=QWoJ
-----END PGP SIGNATURE-----
Quoted 5 lines · Preston A. Elder
Because every new connection to one of the systems 'behind' these systems need to be re-directed to a local port, which is achieved with the command:
/sbin/iptables-t nat -A PREROUTING -j DNAT -i eth0 -p tcp -d <ip-range>
- --destination-port 1024:65535 --to-destination <local_ip>:<local_port>
Quoted 9 lines
Every inbound connection incurs an entry in the connection tracking table. It seems, however, that we may be overloading the conntrack system. I can telnet to a different port listening on a secondary (internal) interface (but the same application), that bypasses the above rule, and get an immediate connection, however establishing a connection 'to' a server behind this router can take a number of seconds, and sometimes may never establish. Whats more connecting directly to the port everything else is being re-directed to via. the 'public' interface itself can take some time (though not as long as connecting 'to' a system behind this box).
NAT tries hard to find a non-clashing connection entry on works on remapping the ports.
Quoted 2 lines
The conntrack table itself very quickly grows - but it does not clean itself up when the connection itself dissapears, instead it waits for some
But how those connections disappear? Normal TCP closing?
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
Quoted 4 lines · Jozsef Kadlecsik
Quoted 2 lines
The conntrack table itself very quickly grows - but it does not clean itself up when the connection itself dissapears, instead it waits for some
But how those connections disappear? Normal TCP closing?
The majority, I believe so, however, that is beside the point. Should it really matter if it was closed with a RST or a FIN/FINACK/ACK? conntrack should realise the connection is closed either way, and stop tracking it.
Hell, even if it didn't even get a RST *OR* FIN (ie. the lan cable was unplugged or something, which is NOT happening), these are connections to the LOCAL machine. TCP keepalive should take care of it regardless. In any case, the kernel TCP stack knows the connection is no longer valid (and that port is now available for use again), why does connection tracking still keep the connection tracked.
OK, I realise the connection tracking subsystem is keeping track of the connection that is supposed to go to a remote machine, so it can't rely on the local TCP stack, however, I'm pretty sure that every connection in this case is getting either a RST or a FIN/FINACK/ACK to close down the socket, so I really can't see why connection tracking has to wait for the pre-set expiry time (ie. leave stale entries), it just seems sloppy.
Thanks,
PreZ (using a different email client, which is why this email is not signed).
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'd like to add some more to this issue.
I did some more testing, and figured out the exact scenario that is causing me problems, here goes.
I have my router box, say, 192.168.0.1, and a range of IP's behind it, say, 10.0.0.0/24, for arguments sake. I also have an application on the router box listening on 100 ports, say, 5000-5100. My uplink interface is eth0, and the interface with all the 10.0.0.0 addresses is on eth1.
Now, I add an iptables rules something like the following:
iptbales -t nat -A PREROUTING -j REDIRECT -i eth0 -p tcp -d 10.0.0.0/24
- --destination-port 1025:65535 --to-ports 5000-5100
If I then telnet to 10.0.0.1 on port 5050, the connection is immediate, and my application receives a new connection on port 5050. If, however, I telnet to 10.0.0.1 on port 5150, there is a small (but noticable) delay between when the telnet session shows the connection as established, and when the application receives the connection (on a random port between 5000 and 5100 inclusive).
First, I cannot see why this delay is even there, choosing a random port between 5000 and 5100 is not exactly an onerous task, especially not one that should cause a noticable delay. And the connection itself is quite responsive once established (closing it down takes no more time one way or the other).
The problems really start when you multiply this out to enterprise proportions
- - I'm talking a system than can get thousands of new connections a second (the hardware is up to it, we're talking gb's of ram, and SMP machines). As with my above small test case, connecting to a port in the 'to-ports' range gives me an immediate connection (on the application), but the delay between telnet showing the connection established, and the application receiving the connection (and establishing it) grows dramaticaly, even taking over a minute for the application to receive the connection. Hell, even the time for telnet to show the connection as established is much greater when connecting to a port outside the 'to-ports' range than when connecting to one within this range.
This is just an abserd delay, and I really can't see a logical reason that this delay would be there. Can anyone shed some light onto the situation, and more importantly, how to fix it? I'm willing to test some patches (obviously in my test environment) to see if they resolve the problem as necessary.
Thanks in advance for your help,
- --
PreZ
Systems Administrator
Shadow Realm
PGP FingerPrint: B3 0C F3 32 DE 5A 7D 90 26 F6 FA 38 CC 0A 2D D8 Finger [email protected] for full PGP public key.
Shadow Realm, a hobbyist ISP supplying real internet services.
http://www.srealm.net.au
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE+1u04KFp14D8AGEQRAlg3AJ9c4LBfDIZ+3PQ5NzhM7tCdsqCdhQCfdIFz JFA7Y0hHt0bVtuhKiA+qtMY=
=NKyf
-----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 11 lines · Rusty Russell
In message <[email protected]> you write:
Quoted 5 lines
We have a system that acts as a router, however any new inbound connection for any machine behind this router is re-directed to a specific port on the local machine, where an application responds as if it were the system behind the router. These systems experience some very high volumes of traffic
Sounds a lot like something is happening to the FIN or RST packets: as soon as conntrack sees a FIN or RST, it'll leave ESTABLISHED and timeout fairly quickly.
Quoted 5 lines
Now, there are several simple things you can do here, as well as dropping the ESTABLISHED timeout:
1) Try 2.4.21-rc5. The hashing algorithm was markedly improved, which you could well be hitting.
I'll look into this, thanks.
Quoted 4 lines
2) You set the
conntrack_maxto 524280, but on a 1GB machine you'll only have about 8192 hash chains, making each chain 64 long. Up the hashsize module parameter to around 100000 (or edit the kernel source if builtin, sorry).
Already made the hashsize the largest prime less than (524280 / 2), on recommendation from someone on core.
Quoted 5 lines
Quoted 2 lines
We have multiple systems performing this task (essentially for load balancing and to remove a single point of faulure).
Um, how are you load balancing? Remember, if the connection tracking code doesn't see all the packets for a connection, it can't work.
Well, not quite load balancing in that way. I mean we have a whole mess of systems 'behind' the routers, and we're splitting them up, so one router takes portion A, the next takes portion B, etc. I'm not actually load balancing (ie. distributing between servers realtime), so much as splitting the load across servers.
- --
PreZ
Systems Administrator
Shadow Realm
PGP FingerPrint: B3 0C F3 32 DE 5A 7D 90 26 F6 FA 38 CC 0A 2D D8 Finger [email protected] for full PGP public key.
Shadow Realm, a hobbyist ISP supplying real internet services.
http://www.srealm.net.au
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE+1wpcKFp14D8AGEQRAv2BAJ9zMkWSacT2Nk0bmjempwGqfOdsIQCcD8PS WDAt/rDsgN5nHhbufM+FOAY=
=ZzHB
-----END PGP SIGNATURE-----
Quoted 3 lines · Preston A. Elder
752_iptables-10_confirm_fix
Please let me know if you think one is missing I should try.
no, the above one is what I was talking about.
Quoted 11 lines
Quoted 2 lines
With a non-broken kernel it is 2 minutes, that is
TIME_WAITof a TCP socket.That was not my point. My point was, for up to 5 days later, the system still has entries in the conntrack table (listed as 'ESTABLISHED'), which have been dead and gone for a long time, conntrack does not realise that connection is utterly closed, and it should drop its conntrack entry. I'm not as worried about the lower-value timeouts, but as I said, I saw ALOT of established connections hanging around in the conntrack table (making the conntrack table about 200,000 entries long, give or take), most of which were entries for connections already closed.
This is exactly the behaviour shown by kernels that have not the above bug fix applied.
Can you please verify that this is indeed a conntrack bug? Can you do some tcpdump capturing that actually shows that there was a connection teardown (FIN handshake or RST) and after it, conntrack state of the respective connection stays ESTABLISHED?
I just want to make sure that this is really some problem about a possible conntrack bug - not some application that forgets to fully close TCP connections..
Quoted 2 lines
Systems Administrator
Shadow Realm
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 13 lines · Harald Welte
Quoted 9 lines
That was not my point. My point was, for up to 5 days later, the system still has entries in the conntrack table (listed as 'ESTABLISHED'), which have been dead and gone for a long time, conntrack does not realise that connection is utterly closed, and it should drop its conntrack entry. I'm not as worried about the lower-value timeouts, but as I said, I saw ALOT of established connections hanging around in the conntrack table (making the conntrack table about 200,000 entries long, give or take), most of which were entries for connections already closed.
This is exactly the behaviour shown by kernels that have not the above bug fix applied.
Quoted 8 lines
Can you please verify that this is indeed a conntrack bug? Can you do some tcpdump capturing that actually shows that there was a connection teardown (FIN handshake or RST) and after it, conntrack state of the respective connection stays ESTABLISHED?
I just want to make sure that this is really some problem about a possible conntrack bug - not some application that forgets to fully close TCP connections..
OK, just to be sure, I just installed kernel 2.4.21-rc6, with all the 'pending' patches applied to it as well, these are the ONLY patches applied to this kernel.
Netstat is showing me the following connection breakdowns:
SNATs: 0
ESTABLISHED : 1060LAST_ACK : 4865TIME_WAIT : 29FIN_WAIT2 : 1FIN_WAIT1 : 39SYN_RECV : 3277
LISTEN : 100CLOSE_WAIT : 9261
/proc/ip_conntrack, however, is showing me the following:
CLOSE: 176CLOSE_WAIT: 111
ESTABLISHED: 9046FIN_WAIT: 8634SYN_RECV: 134SYN_SENT: 25111TIME_WAIT: 2165
(no state): 128
Remember, the *ONLY* natting this machine is (or should be) doing is re-directing any new TCP connection bound for (remote_range) port range 1025-65535, to a local port range of about 100 ports (using REDIR, I used to use DNAT, same difference though). So the above counts seem very wrong to me. And although the established count is very high, the SYN_SENT count is astronomical.
There are also, as you see above, 128 conntrack entries without a state, which by the look of it, are UDP connections. I'm not even natting anything for UDP. WTF is going on here?
- --
PreZ
Systems Administrator
Shadow Realm
PGP FingerPrint: B3 0C F3 32 DE 5A 7D 90 26 F6 FA 38 CC 0A 2D D8 Finger [email protected] for full PGP public key.
Shadow Realm, a hobbyist ISP supplying real internet services.
http://www.srealm.net.au
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE+15f3KFp14D8AGEQRAhpaAKCVH+L/nPFDd1Q8Z4PxmtYs4LBTygCeOVED XIRAa3VHcNSd+Jc3D8nrXLs=
=9WvM
-----END PGP SIGNATURE-----
Quoted 3 lines · Preston A. Elder
OK, just to be sure, I just installed kernel 2.4.21-rc6, with all the 'pending' patches applied to it as well, these are the ONLY patches applied to this kernel.
that one should be just fine.
Quoted 11 lines
Netstat is showing me the following connection breakdowns:
SNATs: 0
ESTABLISHED : 1060LAST_ACK: 4865TIME_WAIT: 29FIN_WAIT2: 1FIN_WAIT1: 39SYN_RECV: 3277
LISTEN : 100CLOSE_WAIT: 9261
Quoted 9 lines
/proc/ip_conntrack, however, is showing me the following:
CLOSE: 176CLOSE_WAIT: 111
ESTABLISHED: 9046FIN_WAIT: 8634SYN_RECV: 134SYN_SENT: 25111TIME_WAIT: 2165
(no state): 128
you cannot just compare TCP socket states with connection tracking
states. connection tracking tracks all connections (local and
forwarded ones), and if a connection isn't correctly closed by the application (FIN handshake), it will assume that the connection still exists - just one example of why this cannot be compared.
And why do you think there is a 'SNAT' socket state?
Quoted 3 lines
use DNAT, same difference though). So the above counts seem very wrong to me. And although the established count is very high, the
SYN_SENTcount is astronomical.
can you capture the packets with tcpdump and actually show me that the machine doesn't in fact have
Quoted 3 lines
There are also, as you see above, 128 conntrack entries without a state, which by the look of it, are UDP connections. I'm not even natting anything for UDP. WTF is going on here?
What do you mean 'without state'? There are no connections without state. Why can't you show me the respective lines from /proc/net/ip_conntrack.
And why are you under the impression that /proc/net/ip_conntrack shows only nat'ed connections? The name of the file indicates 'conntrack', so it shows you all tracked connections - not all NAT'ed.
As of now, it seems to me that there are some significant misunderstandings about the principles of netfilter/iptables. Please discuss this at the mailinglist and get back to me if you are really sure this is a bug.
Quoted 3 lines
PreZ
Systems Administrator
Shadow Realm
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
Quoted 4 lines · Preston A. Elder
I have my router box, say, 192.168.0.1, and a range of IP's behind it, say, 10.0.0.0/24, for arguments sake. I also have an application on the router box listening on 100 ports, say, 5000-5100. My uplink interface is eth0, and the interface with all the 10.0.0.0 addresses is on eth1.
a perfectly fine setup.
Quoted 11 lines
Now, I add an iptables rules something like the following:
iptbales -t nat -A PREROUTING -j REDIRECT -i eth0 -p tcp -d 10.0.0.0/24
- --destination-port 1025:65535 --to-ports 5000-5100If I then telnet to 10.0.0.1 on port 5050, the connection is immediate, and my application receives a new connection on port 5050. If, however, I telnet to 10.0.0.1 on port 5150, there is a small (but noticable) delay between when the telnet session shows the connection as established, and when the application receives the connection (on a random port between 5000 and 5100 inclusive).
This is _definitely_ not a netfilter issue then. We are not doing transparent proxying but nat. netfilter/iptables _never_ accept connections on their own. So you open a connection:
1. telnet 10.0.0.1 5150
2. syn packet is sent by telnet
3. syn packet is DNAT'ed by netfilter
4. syn packet arrives at server application
5. syn/ack packet is sent by server application
6. syn/ack packet is SNAT'ed by netfilter
7. syn/ack packet is received by telnet
[further handshake goes on]
x. telnet application prints 'Connection established' (connect(2) call
returns)
This is a fundamental tcp/ip operation, and it can certainly by no way be anything that netfilter does, that would introduce a behaviour like
1. telnet 10.0.01 5150
2. telnet shows 'connection established'
3. connection 'arrives' at server
[which is what you have been describing, If I understood you correctly].
Quoted 10 lines
- - I'm talking a system than can get thousands of new connections a second (the hardware is up to it, we're talking gb's of ram, and SMP machines). As with my above small test case, connecting to a port in the 'to-ports' range gives me an immediate connection (on the application), but the delay between telnet showing the connection established, and the application receiving the connection (and establishing it) grows dramaticaly, even taking over a minute for the application to receive the connection. Hell, even the time for telnet to show the connection as established is much greater when connecting to a port outside the 'to-ports' range than when connecting to one within this range.
Again, no way. This seems to be some issue with your server application.
Quoted 4 lines
- --
PreZ
Systems Administrator
Shadow Realm
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
Quoted 4 lines · Geffrey Velasquez
Hi Harald according with this email... It is not recomendable to use Kernel 2.4.20. But RH Linux 9.0 comes with this kernel version, it means that I should not use this kernel too? or maybe RH had fix this problem?
according to my information (I've been talking with davem before rh9 was released), they already have the fix in the kernel.
In any doubt, just install the srpm and look through the patches...
Quoted 2 lines
Regards,
Geffrey
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
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 10 lines · Harald Welte
Quoted 6 lines
If I then telnet to 10.0.0.1 on port 5050, the connection is immediate, and my application receives a new connection on port 5050. If, however, I telnet to 10.0.0.1 on port 5150, there is a small (but noticable) delay between when the telnet session shows the connection as established, and when the application receives the connection (on a random port between 5000 and 5100 inclusive).
This is _definitely_ not a netfilter issue then. We are not doing transparent proxying but nat. netfilter/iptables _never_ accept connections on their own. So you open a connection:
Maybe not, but thats the behavior I'm seeing. But this doesnt bother me so much as it takes a LOT longer for a connection to be established when connecing to a port outside the range, than to one inside the range.
Quoted 11 lines
1. telnet 10.0.0.1 5150
2. syn packet is sent by telnet
3. syn packet is DNAT'ed by netfilter
4. syn packet arrives at server application
5. syn/ack packet is sent by server application
6. syn/ack packet is SNAT'ed by netfilter
7. syn/ack packet is received by telnet
[further handshake goes on]
x. telnet application prints 'Connection established' (connect(2) call
returns)
Quoted 8 lines
This is a fundamental tcp/ip operation, and it can certainly by no way be anything that netfilter does, that would introduce a behaviour like
1. telnet 10.0.01 5150
2. telnet shows 'connection established'
3. connection 'arrives' at server[which is what you have been describing, If I understood you correctly].
I realise this is supposed to happen, however again, its not what I'm observing, though its only noticable under high loads, so maybe its taking a long time to get the initial SYN and ACK to the server from telnet (which would account for both the delay before connection is established by telnet (delay for SYN/ACK to get back from server), and the delay between this and the server showing the connection established (which only happens after it gets the ACK).
OK, so with this information, its taking a LONG time to get the SYN and ACK to the server when I'm connecting to a port outside the range I'm listening on with the application, causing the appearance of the port being connected by telnet a long time before the server shows it being established. In any case, this doesnt change the problem. This delay does *NOT* exist when connecting to a port that is in the listening range (remember that all connections being discussed here are going to systems BEHIND the router box).
I did some kernel diving, I've iscolated the place where the behavior differentiates between a connection 'in' and 'out' of the port range, at this if statement in get_unique_tuple (ip_nat_core.c):
if ((!(rptr->flags & IP_NAT_RANGE_PROTO_SPECIFIED)
|| proto->in_range(tuple, HOOK2MANIP(hooknum),
&rptr->min, &rptr->max))
&& !ip_nat_used_tuple(tuple, conntrack)) {Obviously, anything connecting to a port outside this range will fail the above if. If this fails, it will lead us to do this (one way or another):
if (proto->unique_tuple(tuple, rptr,
HOOK2MANIP(hooknum),
conntrack)) {The unique_tuple function is where the delay is. In this case, its a TCP connection, which translates to tcp_unique_tuple (ip_nat_proto_tcp.c), which after determining the port range, does the following:
for (i = 0; i < range_size; i++, port++) {
*portptr = htons(min + port % range_size);
if (!ip_nat_used_tuple(tuple, conntrack)) {
return 1;
}
}Now I'm assuming it will use this:
min = ntohs(range->min.tcp.port);
range_size = ntohs(range->max.tcp.port) - min + 1;
rather than this:
min = 1024;
range_size = 65535 - 1024 + 1;for my port range, since I specified a local port range, however either way, the range is about 100 ports. Which means the above nat used lookup happens 100 times (as opposed to once for when the port is inside the range). And since ip_nat_used_tuple (ip_nat_core.c) calls ip_conntrack_tuple_taken (ip_conntrack_core.c), which subsequently locks/unlocks the entire conntrack table, and calls __ip_conntrack_find (which is a list lookup!), the above for loop is a very intensive process.
Now, after all that. A few questions arise:
1) Is there a way the above for loop could lock the conntrack table beforehand, do the searches (not locked) and unlock afterwards. This is probably the biggest thing consuming all the speed, constant locking/unlocking, especially when there are many new socket connections coming in every second.
2) Is there a way the above for loop could use a random 'start' place, search through to the end, and then start at the beginning, until it ends up back at the same place it started from, which would avoid it constantly treading the same ground (if the last entry used 1, its pretty assured 1 is still going to be in use when you get the next entry, so you could either skip directly to 2, or randomly choose a new position to start at). (ie. something like:
int port, start_port = (rand() % range) + min_port);
for (port=start_port; port<min_port + range; port++)
{ if (!used) { return 1; } }
if (port == min_port + range)
for (port = min_port; port < start_port; port++)
{ if (!used) { return 1; } }
return 0;). Obviously, the kernel's rand would need to be used, etc.
3) Why does it need to do the unique check in the first place (for DNAT and REDIRECT), since this code should only be triggered on a new connection, and if I have 2 new connections from the same source, to the same destination and the same destination (nat'd) port, who cares? The destination port has to be listening for it anyway, and theres nothing stopping it accepting 2 connections from the same source to the same local port (the source port will always be different). I'm assuming the 'resend' checks are done before this point anyway (using the ORIGINAL destination ip/port, not the nat'd one) in the system's TCP stack.
Anyway, if I'm way off base here, let me know, but I'm just trying to figure out why there is a difference between connecting to a port inside, and a port outside the range I'm listening to on the router. Its not a small difference (time wise) either.
And re: your other email:
1) I know SNATs is not a TCP state, the data was copied from a file I generate every 5 minutes gathering this data, and that was the line above what I ment to copy.
2) When I said 'without state', I mean UDP connections, since they don't have TCP state information.
- --
PreZ
Systems Administrator
Shadow Realm
PGP FingerPrint: B3 0C F3 32 DE 5A 7D 90 26 F6 FA 38 CC 0A 2D D8 Finger [email protected] for full PGP public key.
Shadow Realm, a hobbyist ISP supplying real internet services.
http://www.srealm.net.au
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE+1/2LKFp14D8AGEQRAhvtAKCGLi7R2DhiAGbluUhSxRBcuViR9wCfdU4J +ycatke7LILAa+xRYDPOb7c=
=Ngon
-----END PGP SIGNATURE-----
Quoted 4 lines · Preston A. Elder
Quoted 1 line
But how those connections disappear? Normal TCP closing?
The majority, I believe so, however, that is beside the point. Should it really matter if it was closed with a RST or a FIN/FINACK/ACK? conntrack should realise the connection is closed either way, and stop tracking it.
yes, and it is supposed to do so.
Quoted 6 lines
Hell, even if it didn't even get a RST *OR* FIN (ie. the lan cable was unplugged or something, which is NOT happening), these are connections to the LOCAL machine. TCP keepalive should take care of it regardless. In any case, the kernel TCP stack knows the connection is no longer valid (and that port is now available for use again), why does connection tracking still keep the connection tracked.
Connection tracking has no notion of local or remote connections. And I really don't like the idea of putting some callbacks into the socket code for the very special case somebody is tracking locally terminated connections.
Quoted 6 lines
OK, I realise the connection tracking subsystem is keeping track of the connection that is supposed to go to a remote machine, so it can't rely on the local TCP stack, however, I'm pretty sure that every connection in this case is getting either a RST or a FIN/FINACK/ACK to close down the socket, so I really can't see why connection tracking has to wait for the pre-set expiry time (ie. leave stale entries), it just seems sloppy.
are you talking about the ESTABLISHED expiry time? If this happens, there really is some bug. However, I am still not convinced that you are indeed seeing some malfunction in conntrack.
Quoted 2 lines
PreZ (using a different email client, which is why this email is not signed).
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