TCP 状态图

本文详细介绍TCP的三次握手、四次挥手过程以及TCP状态机,包括CLOSE_WAIT堆积的危害、TIME_WAIT问题的排查与解决方法。

TCP的三次握手、TCP四次挥手、TCP状态机

下面三张图展示正常建连和一端先发 FIN 的典型关闭流程,不展开丢包重传、同时打开或同时关闭。时间从上往下,图上间距不代表实际耗时。

TCP的三次握手

窄屏可左右滑动看完整图。

TCP 三次握手时序图 时间从上往下。客户端和服务端初始均为 CLOSED,服务端先进入 LISTEN。客户端发送序列号为 x 的 SYN 后进入 SYN_SENT;服务端回序列号为 y、确认号为 x 加 1 的 SYN 加 ACK 后进入 SYN_RECEIVED。客户端进入 ESTABLISHED 并发出序列号为 x 加 1、确认号为 y 加 1 的 ACK。服务端收到后也进入 ESTABLISHED,双方可以传输数据。 客户端服务端数据传输 CLOSEDCLOSED SYN_SENTLISTEN ESTABLISHEDSYN_RECEIVEDESTABLISHED SYN, seq=x SYN, ACK, seq=y, ack=x+1 ACK, seq=x+1, ack=y+1
x 和 y 分别是客户端和服务端选择的初始序列号。SYN 占用一个序列号,所以对端分别用 x+1 和 y+1 确认。原图的 SYN-RCVD 即 SYN_RECEIVED。
TCP 三次握手原图,标有各报文的序列号和确认号

TCP四次挥手

窄屏可左右滑动看完整图。

TCP 四次挥手时序图 时间从上往下。A 先发 FIN,B 回 ACK,连接进入半关闭:B 仍可发送数据,A 仍可接收并回复 ACK。等 B 也发完数据,再向 A 发 FIN。A 回最后一个 ACK 后进入 TIME_WAIT,B 收到后关闭,A 等待 2MSL 后关闭。 A · 主动关闭方B · 被动关闭方 数据传输(等待 2MSL) 半关闭:B 仍可发送数据 A 仍可接收并回复 ACK ESTABLISHEDESTABLISHED FIN_WAIT_1CLOSE_WAIT FIN_WAIT_2LAST_ACK TIME_WAITCLOSEDCLOSED FIN, seq=pACK, ack=p+1 FIN, ACK, seq=q, ack=p+1ACK, ack=q+1
第一次 FIN 只结束 A 的发送方向。p 和 q 分别是 A 和 B 发送 FIN 时的序列号;FIN 占用一个序列号。原图的 FIN-WAIT2、CLOSED-WAIT 分别写作 FIN_WAIT_2、CLOSE_WAIT。
TCP 四次挥手原图,标有各报文的序列号和确认号

图中只标出关键标志位;如果 B 确认 A 的 FIN 时也已经发完数据,第 2、3 步可以合成一个 FIN + ACK 报文。

窄屏可左右滑动看完整图。

TCP 关闭时两端的状态迁移 客户端和服务端起初都处于 ESTABLISHED。客户端应用发起关闭后,客户端发送 FIN,进入 FIN_WAIT_1。服务端收到 FIN 后回复 ACK,进入 CLOSE_WAIT。服务端应用读完已收数据后读到 EOF,但仍可发送数据。客户端收到对本端 FIN 的 ACK 后进入 FIN_WAIT_2,等待服务端发 FIN。服务端等本端应用关闭发送方向后发 FIN,进入 LAST_ACK。客户端收到 FIN 后回 ACK,进入 TIME_WAIT;服务端收到 ACK 后关闭,客户端等待 2MSL 后关闭。圆形编号标记客户端 FIN 和对应的 ACK,方形编号标记服务端 FIN 和对应的 ACK。 客户端服务端 客户端状态服务端状态 收到本端应用的关闭请求发送 FIN 等待本端 FIN被服务端确认 收到对 FIN 的 ACK等待服务端的 FIN 收到 FIN回复 ACK 等待两倍报文最大生存时间(MSL)即 2MSL 正常通信 收到 FIN回复 ACK读完数据后见 EOF 等待本端应用关闭此时仍可发送数据 应用关闭发送方向发送 FIN 等待对 FIN 的确认(ACK) 收到 ACK ESTABLISHEDESTABLISHED FIN_WAIT_1FIN_WAIT_2TIME_WAITCLOSED CLOSE_WAITLAST_ACKCLOSED #1#2#1#2 FINACK FINACK
圆形:客户端发 FIN,服务端回 ACK。
方形:服务端发 FIN,客户端回 ACK。
两组各自编号:#1 是 FIN,#2 是对它的 ACK。图中以客户端先关闭为例,服务端也可以先发 FIN。
原图来源:The TCP/IP Guide。
TCP 关闭状态迁移原图,展示客户端与服务端的状态及报文交换

CLOSE_WAIT 堆积的危害

每个 CLOSE_WAIT 连接会占据一个文件描述,堆积大量的 CLOSE_WAIT 可能造成文件描述符不够用,导致建连或打开文件失败,报错 too many open files:

1
dial udp 9.215.0.48:9073: socket: too many open files

如何判断?
检查系统 CLOSE_WAIT 连接数:

1
lsof | grep CLOSE_WAIT | wc -l

检查指定进程 CLOSE_WAIT 连接数:

1
lsof -p $PID | grep CLOSE_WAIT | wc -l

主动关闭的一方发出 FIN 包,被动关闭的一方响应 ACK 包,此时,被动关闭的一方就进入了 CLOSE_WAIT 状态。如果一切正常,稍后被动关闭的一方也会发出 FIN 包,然后迁移到 LAST_ACK 状态。

通常,CLOSE_WAIT 状态在服务器停留时间很短,如果你发现大量的 CLOSE_WAIT 状态,那么就意味着被动关闭的一方没有及时发出 FIN 包,一般来说都是被动关闭的一方应用程序有问题。

应用没有 Close

如果 CLOSE_WAIT 堆积的量特别大(比如 10w+),甚至导致文件描述符不够用了,一般就是应用没有 Close 连接导致。

当连接被关闭时,被动关闭方在代码层面没有 close 掉相应的 socket 连接,那么自然不会发出 FIN 包,从而会导致 CLOSE_WAIT 堆积。可能是代码里根本没写 Close,也可能是代码不严谨,出现死循环之类的问题,导致即便后面写了 close 也永远执行不到。

应用迟迟不 accept 连接

如果 CLOSE_WAIT 堆积的量不是很大,可能是全连接队列 (accept queue) 堆积了。我们先看下 TCP 连接建立的过程:

tcp queue

连接建立好之后会被放入 accept queue,等待应用 accept,如果应用迟迟没有从队列里面去 accept 连接,等到 client 超时时间,主动关闭了连接,这时连接在 server 端仍在全连接队列中,状态变为 CLOSE_WAIT。

如果连接一直不被应用 accept 出来,内核也不会自动响应 ACK 去关闭连接的。不过这种情况的堆积量一般也不高,取决于 accept queue 的大小。

TIME_WAIT

补充我一个之前遇到的场景,有次上线服务,发现连接ES的连接有很多TIME_WAIT
cmdb tcp time wait

TIME_WAIT很明显就是本地主动关闭连接,但在等2MSL。

按理说连接ES都是长连接(Keep-Alive),不会有这么多需要关闭的连接。

看了下代码及issue,找到了原因
https://github.com/elastic/go-elasticsearch/issues/123

发现其实是Golang的一个问题
https://pkg.go.dev/net/http#Response

1
2
3
4
5
6
// The http Client and Transport guarantee that Body is always
// non-nil, even on responses without a body or responses with
// a zero-length body. It is the caller's responsibility to
// close Body. The default HTTP client's Transport may not
// reuse HTTP/1.x "keep-alive" TCP connections if the Body is
// not read to completion and closed.

在代码中加上

1
_ = res.String()

问题解决

如果直接使用net/http来发送请求,除了公用一个client之外,发送完数据后,也需要

1
2
3
4
5
6
7
8
9
resp, err := httpc.HttpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// 除了.Close()之后,还需要读取一下body,否则链接的状态还是TIME_WAIT
// 以下两种读取方式,任选其一即可
// _, err = ioutil.ReadAll(resp.Body)
// io.Copy(ioutil.Discard, resp.Body)

关于TIME_WAIT一些其它资料推荐:

TCP状态机

将连接建立和连接断开的两个时序状态图综合起来,就是这个著名的TCP的状态机。学习的时候比较建议将这个状态机和时序状态机对照着看,不然容易晕。

Tcp state diagram

在这个图中,加黑加粗的部分,是上面说到的主要流程,其中阿拉伯数字的序号,是连接过程中的顺序,而大写中文数字的序号,是连接断开过程中的顺序。加粗的实线是客户端A的状态变迁,加粗的虚线是服务端B的状态变迁。

TCP Flags

TCP中Flags字段

  • SYN 表示建立连接,在TCP监听中为: Flags [S];
  • FIN 表示关闭连接,在TCP监听中为: Flags [F];
  • ACK 表示收到请求,返回响应,在TCP监听中为: Flags [.];
  • PSH 表示数据传输,在TCP监听中为: Flags [P];
  • RST 表示连接重置,在TCP监听中为: Flags [R]。

Flags字段组合使用

  • 收到并建立连接为: Flags [S.]
  • 收到并关闭连接为: Flags [F.]
  • 收到并传输数据为: Flags [P.]
  • 收到并连接重置为: Flags [R.]

简称解释

  • SYN - The synchronization flag is used to establish a three-way handshake between two hosts. Only the first packet from both the sender and receiver should have this flag set.
  • ACK - The acknowledgment flag is used to acknowledge the successful receipt of a packet. As we can see from the diagram above, the receiver sends an ACK as well as a SYN in the second step of the three-way handshake process to tell the sender that it received its initial packet.
  • FIN - The finished flag means there is no more data from the sender. Therefore, it is used in the last packet sent from the sender. It frees the reserved resources and gracefully terminates the connection.
  • URG - The urgent flag is used to notify the receiver to process the urgent packets before processing all other packets. The receiver will be notified when all known urgent data has been received. See RFC 6093 for more details.
  • PSH - The push flag is similar to the URG flag and tells the receiver to process these packets as they are received instead of buffering them. Usually, by default, the transport layer waits some time for the application layer to send enough data according to the maximum segment size so that the number of packets transmitted over the network is minimized. However, this is not desirable for certain applications, such as interactive applications (chatting). By using Push, this problem is solved.
  • RST - The reset flag gets sent from the receiver to the sender when a packet is sent to a particular host that was not expecting it.
  • ECE - This flag is responsible for indicating if the TCP peer is ECN capable. See RFC 3168 for more details.
  • CWR - The congestion window reduced flag is used by the sending host to indicate it received a packet with the ECE flag set. See RFC 3168 for more details.
  • NS (experimental) - The nonce sum flag is still an experimental flag used to help protect against accidental, malicious concealment of packets from the sender. See RFC 3540 for more details.

Is it allowed to send FIN, PSH and ACK in a single packet?

是的,这是允许的,也是正常的。如果这是要发送的最后一个数据,那么

  1. 确认先前的数据
  2. 指示没有新数据到来以及
  3. 指示应将数据推送到应用程序而不对即将到来的数据进行任何延迟的最有效方法是设置这三个标志。

推荐阅读

如何提升TCP三次握手的性能?

如何提升TCP四次挥手的性能?