TCP Keepalive(1)

本文深入探讨了TCP Keepalive机制的工作原理及其在网络连接管理和故障检测中的作用。Keepalive通过定期发送探测包来检查对端主机的状态,有助于及时发现连接中断或主机故障。文章详细解释了Keepalive的四种状态,包括对端主机正常运行、对端主机崩溃重启、对端主机不可达以及对端主机虽运行但因网络问题无法到达的情况。同时,文中提供了Linux系统中配置Keepalive参数的方法,并讨论了其可能带来的网络问题。

Introduction 

Under some circumstances, it is useful for a client or server to become aware of the termination or loss of connection with its peer.

In other circumstances, it is desirable to keep a minimal amount of data flowing over a connection, even if the applications do not have any to exchange.

TCP keepalive provides a capability use- ful for both cases. 

Keepalive is a method for TCP to probe its peer without affect- ing the content of the data stream.

It is driven by a keepalive timer.

When the timer fires, a keepalive probe (keepalive for short) is sent, and the peer receiving the probe responds with an ACK

Keepalives are not part of the TCP specification.

The Host Requirements RFC [RFC1122] says that this is because they could

(1) cause perfectly good con- nections to break during transient Internet failures,

(2) consume unnecessary bandwidth, and

(3) cost money for an Internet path that charges for packets. Nev- ertheless, most implementations provide the keepalive capability. 

For example, if the keepalive probes are sent during the time that an intermediate router has crashed and is rebooting, TCP incorrectly thinks its peer host has crashed. 

 

Description 

Anytime it is operating, a TCP using keepalives may find its peer in one of four states: 

  1. The peer host is still up and running and reachable. The peer’s TCP responds normally and the requestor knows that the other end is still up. The requestor’s TCP resets the keepalive timer for later (equal to the value of the keepalive time). If there is application traffic across the connection before the next timer expires, the timer is reset back to the value of keepalive time.

  2. The peer’s host has crashed and is either down or in the process of reboot- ing. In either case, its TCP is not responding. The requestor does not receive a response to its probe, and it times out after a time specified by the keepalive interval. The requestor sends a total of keepalive probes of these probes, kee- palive interval time apart, and if it does not receive a response, the requestor considers the peer’s host as down and terminates the connection.

  3. The client’s host has crashed and rebooted.In this case,the server receives a response to its keepalive probe, but the response is a reset segment, causing the requestor to terminate the connection.

  4. The peer’s host is up and running but is unreachable from the requestor for some reason (e.g., the network cannot deliver traffic and may or may not inform the peers of this fact using ICMP). This is effectively the same as state 2, because TCP cannot distinguish between the two. All TCP can tell is that no replies are received to its probes. 

The requestor does not have to worry about the peer’s host being shut down gracefully and then rebooting (as opposed to crashing).

When the system is shut down by an operator, all application processes are terminated (i.e., the peer’s pro- cess), which causes the peer’s TCP to send a FIN on the connection.

Receiving the FIN would cause the requestor’s TCP to report an end-of-file to the requestor’s process, allowing the requestor to detect this scenario and exit. 

The values of the variables keepalive time, keepalive interval, and keepalive probes can usually be changed.

Some systems allow these changes on a per-connection basis, while others allow them to be set only system-wide (or both in some cases).

In Linux, these values are available as sysctl variables with the names net.ipv4 .tcp_keepalive_time, net.ipv4.tcp_keepalive_intvl, and net.ipv4 .tcp_keepalive_probes, respectively. The defaults are 7200 (seconds, or 2 hours), 75 (seconds), and 9 (probes). 

Note that [RFC1122] places certain restrictions on the use of keepalives.

In particular, the keepalive time must be configurable and must not default to less than 2 hours.

In addition, keepalives must not be enabled unless an application requests one (although this behavior is violated if the net.inet.tcp.always_ keepalive variable is set).

Linux does not provide a native facility for adding keepalives to applications that do not request it, but a special library can be pre- loaded (i.e., loaded prior to ordinary shared libraries) to get this effect [LKA]. 

 

Keepalive Examples 

Other End Crashes 

 

Other End Crashes and Reboots 

Connection reset by peer 

 

Other End Is Unreachable 

 

转载于:https://www.cnblogs.com/geeklove01/p/9770020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值