Linux - TCP connect() 失败并出现 ETIMEDOUT
对于 TCP 客户端对 TCP 服务器的 connect() 调用。Richard
Stevens 的《UNIX® 网络编程》一书中有如下内容:
如果客户端 TCP 未收到对其 SYN 段的响应,则返回 ETIMEDOUT。 4.4BSD, 例如,调用 connect 时发送一个 SYN,6 秒后发送另一个 SYN,然后再发送一个 SYN 24 秒后(TCPv2 第 828 页)。如果总共 75 秒后未收到响应,则 返回错误。
在Linux中我想知道什么是重试机制(多少次和间隔多远)。询问是因为对于 TCP 客户端 connect() 调用,我收到 ETIMEDOUT 错误。该套接字具有 O_NONBLOCK 选项,并由 epoll() 监视事件。
如果有人能指出我在代码中的何处实现了重试逻辑,那也会很有帮助。我尝试从 net/ipv4/tcp_ipv4.c 中的 tcp_v4_connect() 开始,但很快就迷失了方向。
For a TCP client connect() call to a TCP server..
UNIX® Network Programming book by Richard Stevens says the following..
If the client TCP receives no response to its SYN segment, ETIMEDOUT is returned. 4.4BSD,
for example, sends one SYN when connect is called, another 6 seconds later, and another
24 seconds later (p. 828 of TCPv2). If no response is received after a total of 75 seconds, the
error is returned.
In Linux I would like know what is the retry mechanism (how many times and how far apart). Asking because for a TCP client connect() call I am getting ETIMEDOUT error. This socket has O_NONBLOCK option and monitored by epoll() for the events.
If someone can point to me where in the code this retry logic is implemented that would be helpful too. I tried following a bit starting with tcp_v4_connect() from net/ipv4/tcp_ipv4.c, but lost my way pretty soon..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
超时根据测量的往返时间进行调整。
tcp_connect()
设置计时器:icsk_rto
将使用每个目标 重传超时;如果来自目标的先前指标可从先前的连接获得,则将重新使用它。 (有关详细信息,请参阅tcp(7)
中的tcp_no_metrics_save
讨论。)如果未保存任何指标,则内核将回退到默认 RTO 值:tcp_retransmit_timer ()
在底部附近有一些代码用于重新计算延迟:retransmits_timed_out()
将首先执行线性退避,然后执行指数退避。我认为总而言之,您可以合理地预期大约 120 秒后才会从
connect(2)
返回ETIMEDOUT
错误,除非内核有充分的理由怀疑远程对等点应该早点回复。The timeout is scaled based on the measured round-trip time.
tcp_connect()
sets up a timer:The
icsk_rto
will use a per-destination re-transmission timeout; if previous metrics from the destination is available from previous connections, it is re-used. (See thetcp_no_metrics_save
discussion intcp(7)
for details.) If no metrics are saved, then the kernel will fall back to a default RTO value:tcp_retransmit_timer()
has some code near the bottom for recalculating the delay:retransmits_timed_out()
will first perform a linear backoff then an exponential backoff.I think the long and the short of it is that you can reasonably expect roughly 120 seconds before getting
ETIMEDOUT
error returns fromconnect(2)
unless the kernel has good reason to suspect that the remote peer should have replied sooner.ETIMEOUT 的一个典型原因是防火墙简单地吞下数据包,而不是回复 ICMP 目标无法到达。
这是防止黑客探测网络中主机的常见设置。
A typical reason for ETIMEOUT is a firewall which simply swallows the packets instead of replying with ICMP Destination Unreachable.
This is a common setup to prevent hackers from probing a network for hosts.