Pet*_*nen 29 linux network-programming tcp
对于不耐烦:
如何改变的价值/proc/sys/net/ipv4/tcp_retries2在Linux中单个连接,使用setsockopt(),ioctl()或者这样,或者是这可能吗?
更长的解密:
我正在开发一个使用长轮询HTTP请求的应用程序.在服务器端,需要知道客户端何时关闭连接.准确性并不重要,但肯定不能是15分钟.接近一分钟就可以了.
对于不熟悉该概念的人,长轮询HTTP请求的工作方式如下:
在我的应用程序中,服务器每隔一段时间就向客户端发送"心跳"(默认为30秒).心跳只是一个作为响应块发送的换行符.这是为了保持线路忙,以便我们通知连接丢失.
客户端正常关闭时没有问题.但是当它强制关闭时(例如客户端机器断电),不会发送TCP重置.在这种情况下,服务器发送心跳,客户端不会发送确认.在此之后,服务器在放弃并向应用层(我们的HTTP服务器)报告失败后,继续重传该数据包大约15分钟.在我的情况下等待15分钟太久了.
我可以通过写入以下文件来控制重传时间/proc/sys/net/ipv4/:
tcp_retries1 - INTEGER
This value influences the time, after which TCP decides, that
something is wrong due to unacknowledged RTO retransmissions,
and reports this suspicion to the network layer.
See tcp_retries2 for more details.
RFC 1122 recommends at least 3 retransmissions, which is the
default.
tcp_retries2 - INTEGER
This value influences the timeout of an alive TCP connection,
when RTO retransmissions remain unacknowledged.
Given a value of N, a hypothetical TCP connection following
exponential backoff with an initial RTO of TCP_RTO_MIN would
retransmit N times before killing the connection at the (N+1)th RTO.
The default value of 15 yields a hypothetical timeout of 924.6
seconds and is a lower bound for the effective timeout.
TCP will effectively time out at the first RTO which exceeds the
hypothetical timeout.
RFC 1122 recommends at least 100 seconds for the timeout,
which corresponds to a value of at least 8.
Run Code Online (Sandbox Code Playgroud)
默认值tcp_retries2确实是8,我的15分钟(900秒)重传的体验符合上面引用的内核文档.
tcp_retries2例如,如果我将值更改为5,则连接会更快地消失.但是这样设置会影响系统中的所有连接,我真的只想为这一个长轮询连接设置它.
来自RFC 1122的引用:
4.2.3.5 TCP Connection Failures
Excessive retransmission of the same segment by TCP
indicates some failure of the remote host or the Internet
path. This failure may be of short or long duration. The
following procedure MUST be used to handle excessive
retransmissions of data segments [IP:11]:
(a) There are two thresholds R1 and R2 measuring the amount
of retransmission that has occurred for the same
segment. R1 and R2 might be measured in time units or
as a count of retransmissions.
(b) When the number of transmissions of the same segment
reaches or exceeds threshold R1, pass negative advice
(see Section 3.3.1.4) to the IP layer, to trigger
dead-gateway diagnosis.
(c) When the number of transmissions of the same segment
reaches a threshold R2 greater than R1, close the
connection.
(d) An application MUST be able to set the value for R2 for
a particular connection. For example, an interactive
application might set R2 to "infinity," giving the user
control over when to disconnect.
(e) TCP SHOULD inform the application of the delivery
problem (unless such information has been disabled by
the application; see Section 4.2.4.1), when R1 is
reached and before R2. This will allow a remote login
(User Telnet) application program to inform the user,
for example.
Run Code Online (Sandbox Code Playgroud)
在我看来tcp_retries1,tcp_retries2在Linux中对应R1和R2在RFC中.RFC明确指出(在项目d中)符合条件的实现必须允许设置值R2,但我发现没有办法使用setsockopt(),ioctl()或者这样做.
另一种选择是在R1超出时获得通知(项目e).R2然而,这不如设置好,因为我认为R1很快就会出现(几秒钟内),并且R1无法为每个连接设置值,或者至少RFC不需要它.
Kim*_*ais 24
看起来这是在内核2.6.37中添加的. 从内核Git和摘录中提交diff来自下面的更改日志 ;
commit dca43c75e7e545694a9dd6288553f55c53e2a3a3作者:Jerry Chu日期:8月27日星期五19:13:28 +0000
Run Code Online (Sandbox Code Playgroud)tcp: Add TCP_USER_TIMEOUT socket option. This patch provides a "user timeout" support as described in RFC793. The socket option is also needed for the the local half of RFC5482 "TCP User Timeout Option". TCP_USER_TIMEOUT is a TCP level socket option that takes an unsigned int, when > 0, to specify the maximum amount of time in ms that transmitted data may remain unacknowledged before TCP will forcefully close the corresponding connection and return ETIMEDOUT to the application. If 0 is given, TCP will continue to use the system default. Increasing the user timeouts allows a TCP connection to survive extended periods without end-to-end connectivity. Decreasing the user timeouts allows applications to "fail fast" if so desired. Otherwise it may take upto 20 minutes with the current system defaults in a normal WAN environment. The socket option can be made during any state of a TCP connection, but is only effective during the synchronized states of a connection (ESTABLISHED, FIN-WAIT-1, FIN-WAIT-2, CLOSE-WAIT, CLOSING, or LAST-ACK). Moreover, when used with the TCP keepalive (SO_KEEPALIVE) option, TCP_USER_TIMEOUT will overtake keepalive to determine when to close a connection due to keepalive failure. The option does not change in anyway when TCP retransmits a packet, nor when a keepalive probe will be sent. This option, like many others, will be inherited by an acceptor from its listener. Signed-off-by: H.K. Jerry Chu <hkchu@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>