天天看點

Linux中keepalive的使用

/proc/sys/net/ipv4/tcp_keepalive_time
當keepalive起用的時候,TCP發送keepalive消息的頻度。預設是2小時。


/proc/sys/net/ipv4/tcp_keepalive_intvl
當探測沒有确認時,重新發送探測的頻度。預設是75秒。


/proc/sys/net/ipv4/tcp_keepalive_probes
在認定連接配接失效之前,發送多少個TCP的keepalive探測包。預設值是9。這個值乘以tcp_keepalive_intvl之後決定了,一個連接配接發送了keepalive之後可以有多少時間沒有回應。

tcp_keepalive_time(開啟keepalive的閑置時長)tcp_keepalive_intvl(keepalive探測包的發送間隔) 和tcp_keepalive_probes (如果對方不予應答,探測包的發送次數)

There are two ways to configure keepalive parameters inside the kernel via userspace commands:

  • procfs interface
  • sysctl interface

We mainly discuss how this is accomplished on the procfs interface because it's the most used, recommended and the easiest to understand. The sysctl interface, particularly regarding the sysctl(2) syscall and not the sysctl(8) tool, is only here for the purpose of background knowledge. The procfs interface This interface requires both sysctl and procfs to be built into the kernel, and procfs mounted somewhere in the filesystem (usually on/proc, as in the examples below). You can read the values for the actual parameters by "catting" files in /proc/sys/net/ipv4/directory:

# 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      

The first two parameters are expressed in seconds, and the last is the pure number. This means that the keepalive routines 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. Modifying this value is straightforward: you need to write new values into the files. Suppose you decide to configure the host so that keepalive starts after ten minutes of channel inactivity, and then send probes in intervals of one minute. Because of the high instability of our network trunk and the low value of the interval, suppose you also want to increase the number of probes to 20. Here's how we would change the settings:

# echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time  
# echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl  
# echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes      

To be sure that all succeeds, recheck the files and confirm these new values are showing in place of the old ones. 這樣,上面的三個參數配置完畢。

繼續閱讀