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.
这样,上面的三个参数配置完毕。
Linux的TCP Keepalive机制是用来检测网络连接是否仍然活动的一种方式。当一个网络连接在一段时间内没有活动时,可能会因为网络故障或者其他原因断开,而Keepalive机制就是为了解决这种问题而存在的。 在LinuxKeepalive是通过一个内核选项来控制的。该选项包含了一些参数,如Keepalive启动的时间、Keepalive间隔、Keepalive探测的次数等。 当一个TCP连接在一段时间内没有活动时,内核会定期发送一个Keepalive探测报文到对端,探测报文包含了一些特殊标志位,用来判断对端是否还活动。如果对端没有回应该探测报文,那么内核会再发送几个探测报文,直到达到一定次数。如果在这些探测报文的发送过程,对端回应了任意一个探测报文,那么说明对端还活动,连接仍然有效。 Keepalive机制可以应用于很多场景,例如在服务器与客户端之间长时间没有数据交换的情况下,可以使用Keepalive来检测连接的可用性,防止连接在未使用的情况下被关闭。 使用Keepalive机制需要注意的是,Keepalive的参数需要根据具体的场景来设置。如果时间间隔设置得太短或者探测次数设置得太多,可能会浪费网络资源;如果时间间隔设置得太长或者次数设置得太少,可能无法及时发现连接断开的情况。因此,合理设置Keepalive参数对于保证连接的可靠性非常重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值