網頁

2009年6月5日 星期五

TCP keepalive under linux

TCP keepalive During TCP connection idle
Main goal:
  • Checking for dead peers
  • Preventing disconnection due to network inactivity- TCP connections through NATs or proxy but they keep limited memory resources (mantain a few TCP connections, discard old connections)
How to use TCP keepalive under Linux
  • tcp_keepalive_time - the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive
  • tcp_keepalive_intvl - the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime
  • tcp_keepalive_probes - the number of unacknowledged probes to send before considering the connection dead and notifying the application layer
procfs interface

echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes

echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time

echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl
sysctl interface


sysctl -w \

> net.ipv4.tcp_keepalive_time=600 \

> net.ipv4.tcp_keepalive_intvl=60 \

> net.ipv4.tcp_keepalive_probes=20

How to program network applications with TCP keepalive
  • System call :setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen)
  • Other three socket options for the current socket overide system-wide variables as the follwoing
TCP_KEEPCNT: overrides tcp_keepalive_probes
TCP_KEEPIDLE: overrides tcp_keepalive_time
TCP_KEEPINTVL: overrides tcp_keepalive_intvl
reference from http://tldp.org/HOWTO/html_single/TCP-Keepalive-HOWTO/#whatis


沒有留言:

張貼留言