Skip to contentSkip to conversation

bug in tcp?

DISCUSSION Sebastian Kuzminsky 4 participants 13 messages Apr 16, 2007 – Apr 23, 2007

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

I'm seeing some weird behavior in TCP. The issue is perfectly reproducible using netcat and other programs. This is what I do:

1. Open a TCP connection over the loopback (over IPv4).

2. Send a couple of bytes of data each way. No problems.

3. Wait about 120 hours with no writes on either side of the connection.

4. write() a few bytes to the server's socket. I'd expect the data to go through, but it doesnt. I see the TCP frame from the server to the client, but instead of an ACK, the client sends back a RST. netstat shows the bytes sitting in the server's socket's send-buffer.

5. write a few bytes to the client's socket. The server gets these immediately.

6. On the next server-to-client retransmit, the client gets the bytes from the server. After this, the connection works normally.

The libpcap capture file is here (only shows steps 4-6):

http://highlab.com/~seb/tcp-idleness-bug

The behavior is reproducible on all kernels I've tried: 2.4.32, 2.6.19.1, and 2.6.20.4. I dont think it's iptables-related, though I'm rerunning the tests on a machine without iptables to be sure. I'll have results for you in 120 hours. ;-)

Signature · 1 line
Sebastian Kuzminsky
#1 lore raw
Quoted 25 lines · On Mon, 16 Apr 2007 16:28:22 -0600 Sebastian Kuzminsky <[email protected]> wrote:

I'm seeing some weird behavior in TCP. The issue is perfectly reproducible using netcat and other programs. This is what I do:

1. Open a TCP connection over the loopback (over IPv4).

2. Send a couple of bytes of data each way. No problems.

3. Wait about 120 hours with no writes on either side of the connection.

4. write() a few bytes to the server's socket. I'd expect the data to go through, but it doesnt. I see the TCP frame from the server to the client, but instead of an ACK, the client sends back a RST. netstat shows the bytes sitting in the server's socket's send-buffer.

5. write a few bytes to the client's socket. The server gets these immediately.

6. On the next server-to-client retransmit, the client gets the bytes from the server. After this, the connection works normally.

The libpcap capture file is here (only shows steps 4-6):

Quoted 9 lines

http://highlab.com/~seb/tcp-idleness-bug

The behavior is reproducible on all kernels I've tried: 2.4.32, 2.6.19.1, and 2.6.20.4. I dont think it's iptables-related, though I'm rerunning the tests on a machine without iptables to be sure. I'll have results for you in 120 hours. ;-)

What server? Some servers do application timeouts.

Signature · 1 line
Stephen Hemminger <[email protected]>
#2 lore raw
Quoted 31 lines · Stephen Hemminger
Quoted 28 lines · On Mon, 16 Apr 2007 16:28:22 -0600 Sebastian Kuzminsky <[email protected]> wrote:

I'm seeing some weird behavior in TCP. The issue is perfectly reproducible using netcat and other programs. This is what I do:

1. Open a TCP connection over the loopback (over IPv4).

2. Send a couple of bytes of data each way. No problems.

3. Wait about 120 hours with no writes on either side of the connection.

4. write() a few bytes to the server's socket. I'd expect the data to go through, but it doesnt. I see the TCP frame from the server to the client, but instead of an ACK, the client sends back a RST. netstat shows the bytes sitting in the server's socket's send-buffer.

5. write a few bytes to the client's socket. The server gets these immediately.

6. On the next server-to-client retransmit, the client gets the bytes from the server. After this, the connection works normally.

The libpcap capture file is here (only shows steps 4-6):

http://highlab.com/~seb/tcp-idleness-bug

Quoted 8 lines
Quoted 6 lines

The behavior is reproducible on all kernels I've tried: 2.4.32, 2.6.19.1, and 2.6.20.4. I dont think it's iptables-related, though I'm rerunning the tests on a machine without iptables to be sure. I'll have results for you in 120 hours. ;-)

What server? Some servers do application timeouts.

I've observed the behavior with the server mode of nc, and with a homebrew application which does not do app-level timeouts.

But anyway, application timeouts wouldnt explain the described behavior, afaik.

Signature · 1 line
Sebastian Kuzminsky
#3 lore raw
Quoted 39 lines · On Mon, 16 Apr 2007 17:05:42 -0600 Sebastian Kuzminsky <[email protected]> wrote:
Quoted 38 lines · Stephen Hemminger
Quoted 34 lines · On Mon, 16 Apr 2007 16:28:22 -0600 Sebastian Kuzminsky <[email protected]> wrote:

I'm seeing some weird behavior in TCP. The issue is perfectly reproducible using netcat and other programs. This is what I do:

1. Open a TCP connection over the loopback (over IPv4).

2. Send a couple of bytes of data each way. No problems.

3. Wait about 120 hours with no writes on either side of the connection.

4. write() a few bytes to the server's socket. I'd expect the data to go through, but it doesnt. I see the TCP frame from the server to the client, but instead of an ACK, the client sends back a RST. netstat shows the bytes sitting in the server's socket's send-buffer.

5. write a few bytes to the client's socket. The server gets these immediately.

6. On the next server-to-client retransmit, the client gets the bytes from the server. After this, the connection works normally.

The libpcap capture file is here (only shows steps 4-6):

http://highlab.com/~seb/tcp-idleness-bug

The behavior is reproducible on all kernels I've tried: 2.4.32, 2.6.19.1, and 2.6.20.4. I dont think it's iptables-related, though I'm rerunning the tests on a machine without iptables to be sure. I'll have results for you in 120 hours. ;-)

Quoted 7 lines
Quoted 1 line

What server? Some servers do application timeouts.

I've observed the behavior with the server mode of nc, and with a homebrew application which does not do app-level timeouts.

But anyway, application timeouts wouldnt explain the described behavior, afaik.

A guess: maybe something related to a PAWS wraparound problem. Does turning off sysctl net.ipv4.tcp_timestamps fix it?

#4 lore raw
Quoted 2 lines · Stephen Hemminger

A guess: maybe something related to a PAWS wraparound problem. Does turning off sysctl net.ipv4.tcp_timestamps fix it?

I just started this test, I'll let you know in 5 days. :-/

Any other things I should try, anyone? I'm doing the new tests under qemu, so it's easy to run lots of tests in parallel. The previous reports of this bug all occurred when running natively.

I'm doing the new tests with 2.6.18, but the behavior does not seem sensitive to the kernel version.

Signature · 1 line
Sebastian Kuzminsky
#5 lore raw
In reply to Stephen Hemminger
Quoted 2 lines · Stephen Hemminger

A guess: maybe something related to a PAWS wraparound problem. Does turning off sysctl net.ipv4.tcp_timestamps fix it?

That was my first thought too (aside from netfilter), but a failed PAWS check should not result in a reset..

-John

#6 lore raw
Quoted 6 lines · John Heffner
Quoted 2 lines · Stephen Hemminger

A guess: maybe something related to a PAWS wraparound problem. Does turning off sysctl net.ipv4.tcp_timestamps fix it?

That was my first thought too (aside from netfilter), but a failed PAWS check should not result in a reset..

All three of the systems where I saw the problems earlier did have some netfilter rules up and running....

All tests I'm doing now have netfilter compiled in but no rules set (INPUT, FORWARD, and OUTPUT are all empty, and the policies are all ACCEPT).

Signature · 1 line
Sebastian Kuzminsky
#7 lore raw
In reply to Sebastian Kuzminsky
Quoted 25 lines · Sebastian Kuzminsky

I'm seeing some weird behavior in TCP. The issue is perfectly reproducible using netcat and other programs. This is what I do:

1. Open a TCP connection over the loopback (over IPv4).

2. Send a couple of bytes of data each way. No problems.

3. Wait about 120 hours with no writes on either side of the connection.

4. write() a few bytes to the server's socket. I'd expect the data to go through, but it doesnt. I see the TCP frame from the server to the client, but instead of an ACK, the client sends back a RST. netstat shows the bytes sitting in the server's socket's send-buffer.

5. write a few bytes to the client's socket. The server gets these immediately.

6. On the next server-to-client retransmit, the client gets the bytes from the server. After this, the connection works normally.

The libpcap capture file is here (only shows steps 4-6):

Quoted 7 lines

http://highlab.com/~seb/tcp-idleness-bug

The behavior is reproducible on all kernels I've tried: 2.4.32, 2.6.19.1, and 2.6.20.4. I dont think it's iptables-related, though I'm rerunning the tests on a machine without iptables to be sure. I'll have results for you in 120 hours. ;-)

It sounds like it could easily be iptables related, if you have iptables rules that only allow new connections in the client to server direction, which is quite normal.

The default iptables timeout for TCP connections is 5 days. So after 5 days of idle, any packets from the server will be treated as a new connection and the iptables rules will drop them.

#8 lore raw
Quoted 3 lines · Philip Craig

It sounds like it could easily be iptables related, if you have iptables rules that only allow new connections in the client to server direction, which is quite normal.

Sure I have those standard rules.

iptables -A INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT iptables -A INPUT -p tcp --syn --dport ssh -j ACCEPT iptables -A INPUT -p tcp --syn --dport http -j ACCEPT ... etc

Quoted 3 lines

The default iptables timeout for TCP connections is 5 days. So after 5 days of idle, any packets from the server will be treated as a new connection and the iptables rules will drop them.

Weird. Why does sending a message from the client make it go again?

If that's the case, it seems like a simple fix would be to enable TCP keepalive in my app, that would keep netfilter from timing out, right? That seems better than extending the netfilter timeout.

How do people normally handle this?

Signature · 1 line
Sebastian Kuzminsky
#9 lore raw
Quoted 1 line · Sebastian Kuzminsky

Weird. Why does sending a message from the client make it go again?

The rule that allows packets with an "ESTABLISHED" state only matches packets for which the connection is in netfilter's conntrack table. The connection is removed from the table after the 5 days of idle. It is only added again if netfilter accepts a packet for that connection. So the packet from the client will cause it to be added.

Quoted 3 lines

If that's the case, it seems like a simple fix would be to enable TCP keepalive in my app, that would keep netfilter from timing out, right? That seems better than extending the netfilter timeout.

Yes that would work.

Quoted 1 line

How do people normally handle this?

Change the timeout or use keepalives. I can't think of any other way. The 5 days is a compromise between keeping valid connections and timing out dead connections. There will always be connections for which it times out too fast or too slow. I don't think there are any drawbacks to increasing the timeout if you aren't a router, but as long as there is a timeout, you need keepalives to be sure.

#10 lore raw
Quoted 8 lines · Philip Craig
Quoted 1 line · Sebastian Kuzminsky

Weird. Why does sending a message from the client make it go again?

The rule that allows packets with an "ESTABLISHED" state only matches packets for which the connection is in netfilter's conntrack table. The connection is removed from the table after the 5 days of idle. It is only added again if netfilter accepts a packet for that connection. So the packet from the client will cause it to be added.

Why did the packet from the client cause the connection to be added back to the conntrack table, but the packet from the server did not?

Quoted 8 lines
Quoted 1 line

How do people normally handle this?

Change the timeout or use keepalives. I can't think of any other way. The 5 days is a compromise between keeping valid connections and timing out dead connections. There will always be connections for which it times out too fast or too slow. I don't think there are any drawbacks to increasing the timeout if you aren't a router, but as long as there is a timeout, you need keepalives to be sure.

Thanks! I'll add keepalives and rerun the tests, and I expect the problem to go away.

Signature · 1 line
Sebastian Kuzminsky
#11 lore raw
Quoted 2 lines · Sebastian Kuzminsky

Why did the packet from the client cause the connection to be added back to the conntrack table, but the packet from the server did not?

Because the packet from the client was accepted (by a different iptables rule).

#12 lore raw

bug in my understanding (was Re: bug in tcp?)

In reply to Sebastian Kuzminsky
Quoted 10 lines · Sebastian Kuzminsky
Quoted 6 lines · Philip Craig

Change the timeout or use keepalives. I can't think of any other way. The 5 days is a compromise between keeping valid connections and timing out dead connections. There will always be connections for which it times out too fast or too slow. I don't think there are any drawbacks to increasing the timeout if you aren't a router, but as long as there is a timeout, you need keepalives to be sure.

Thanks! I'll add keepalives and rerun the tests, and I expect the problem to go away.

I reran the tests with keepalive enabled and it worked just fine. Thanks for all your help, and sorry for the false alarm!

Signature · 1 line
Sebastian Kuzminsky