2010年12月2日 星期四

TCP Keepalive mechanism

RFC1122 says

"Implementors MAY include "keep-alives" in their TCP implementations, although this practice is not universally accepted. If keep-alives are included, the application MUST be able to turn them on or off for each TCP connection, and they MUST default to off." [1]


In C, use setsockopt() to set the following arguments on a specific socket [2]
* SO_KEEPALIVE: enable tcp keepalive
* TCP_KEEPCNT: overrides tcp_keepalive_probes
* TCP_KEEPIDLE: overrides tcp_keepalive_time
* TCP_KEEPINTVL: overrides tcp_keepalive_intvl

These settings are only for the current socket.

In Linux, to see the default values (OS-wide variables) of the later 3 options:
# cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
# cat /proc/sys/net/ipv4/tcp_keepalive_intvl
75
# cat /proc/sys/net/ipv4/tcp_keepalive_probes
9

This means "wait for two hours (7200 secs) before sending the first keepalive probe, and then resend it every 75 seconds. If no ACK response is received for nine consecutive times, the connection is marked as broken." [2]


Ref:

沒有留言:

張貼留言