TCP协议详解 定时器

C/C++Linux服务器开发/后台架构师知识体系

连接建立(connection establishment)定时器

这个定时器由TCP_KEEP计数器实现

/*

* Keep-alive timer went off; send something

* or drop connection if idle for too long.

*/

case TCPT_KEEP:

tcpstat.tcps_keeptimeo++;

 if (tp->t_state < TCPS_ESTABLISHED)

 goto dropit;

......

dropit:

tcpstat.tcps_keepdrops++;

tp = tcp_drop(tp, ETIMEDOUT);

 break;

......

如代码所示,如果tcp的state<ESTABLISHED,表明其处于连接建立状态。定时器超时后,调用dropit终止连接。大多数伯克利系统将建立一个连接的最长时间设置为75s。连接建立定时器配合重传定时器一起使用,重传定时器会隔一段时间重传SYN,如下图所示:
在这里插入图片描述
图中可以看出,对于一个新连接,重传定时器初始化为6s,后续值为24s和48s。重传定时器在0s,6s和30s处传送SYN报文。在75s处,连接定时器超时,调用tcp_drop()终止连接。

保活(keepalive)定时器

TCP_KEEP同时也实现了保活定时器。代码如下:

case TCPT_KEEP:

tcpstat.tcps_keeptimeo++;

 if (tp->t_state < TCPS_ESTABLISHED)

 goto dropit;

 if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&

tp->t_state <= TCPS_CLOSE_WAIT) {

 if (tp->t_idle >= tcp_keepidle + tcp_maxidle)

 goto dropit;

 /*

* Send a packet designed to force a response

* if the peer is up and reachable:

* either an ACK if the connection is still alive,

* or an RST if the peer has closed the connection

* due to timeout or reboot.

* Using sequence number tp->snd_una-1

* causes the transmitted zero-length segment

* to lie outside the receive window;

* by the protocol spec, this requires the

* correspondent TCP to respond.

*/

tcpstat.tcps_keepprobe++;

保活定时器的作用和应用层的心跳类似。检测对端tcp连接是否存在。其发送的序号为tcp->snd_una-1,这样就发送了一个0字节的报文段,从而来检测对端tcp连接的情况。所有的保活定时器在连接2小时候空闲后超时。然后以75s为间隔连续发送9个探测报文段。如果皆无响应,则丢弃此连接。所以一共是2小时+75s * 9后(大约为2个小时11分钟),关闭连接。如下图所示:
在这里插入图片描述

重传(retransmission)定时器

重传定时器的取值依赖于连接上测算得到的RTT(RTT测算方法不在本次讨论范围内)。

代码如下:

case TCPT_REXMT:

 if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) {

tp->t_rxtshift = TCP_MAXRXTSHIFT;

tcpstat.tcps_timeoutdrop++;

tp = tcp_drop(tp, tp->t_softerror ?

tp->t_softerror : ETIMEDOUT);

 break;

 }

tcpstat.tcps_rexmttimeo++;

rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift];

TCPT_RANGESET(tp->t_rxtcur, rexmt,

tp->t_rttmin, TCPTV_REXMTMAX);

tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;

 /*

* If losing, let the lower level know and try for

* a better route. Also, if we backed off this far,

* our srtt estimate is probably bogus. Clobber it

* so we'll take the next rtt measurement as our srtt;

* move the current srtt into rttvar to keep the current

* retransmit times until then.

*/

 if (tp->t_rxtshift > TCP_MAXRXTSHIFT / 4) {

in_losing(tp->t_inpcb);

tp->t_rttvar += (tp->t_srtt >> TCP_RTT_SHIFT);

tp->t_srtt = 0;

 }

tp->snd_nxt = tp->snd_una;

 ......

 (void) tcp_output(tp);

 break;
  • 重传移位计数器(t_rxtshift)在每次重传时递增,如果大于TCP_MAXRXTSHIFT(12),连接将被丢弃。
  • 代码中的 TCP_REXMTVAL 宏是为了实现指数退避,即重传超时时间逐渐变为1.5、3、6、12、24、48、64、64、64、64、64、64、64(s)。
  • 如果报文段重传次数>3次(TCP_MAXRXTSHIFT(12)/4),则会释放缓存中的路由以便重新寻找新的路由。
  • 最后强迫重传最早的未确认的数据:
tp->snd_nxt = tp->snd_una;//下一个序号snd_nxt被置为最早的未确认过的报文

(void) tcp_output(tp);//强制发送

重传过程如下图所示:
在这里插入图片描述

持续(persist)定时器

持续定时器超时后,由于对端已通告接收窗口为0,则强制发送1字节的数据以探测对端窗口。 代码如下所示:

/*

* Persistance timer into zero window.

* Force a byte to be output, if possible.

*/

 case TCPT_PERSIST:

tcpstat.tcps_persisttimeo++;

 /*

* Hack: if the peer is dead/unreachable, we do not

* time out if the window is closed. After a full

* backoff, drop the connection if the idle time

* (no responses to probes) reaches the maximum

* backoff that we would use if retransmitting.

*/

 if (tp->t_rxtshift == TCP_MAXRXTSHIFT &&

 (tp->t_idle >= tcp_maxpersistidle ||

tp->t_idle >= TCP_REXMTVAL(tp) * tcp_totbackoff)) {

tcpstat.tcps_persistdrop++;

tp = tcp_drop(tp, ETIMEDOUT);

 break;

 }

tcp_setpersist(tp);

tp->t_force = 1;

 (void) tcp_output(tp);

tp->t_force = 0;

 break;

其也在重传移位计数器>TCP_MAXRXTSHIFT后drop连接。

下图为持续定时器取值时间表(假设连接的重传时限为1.5s):
在这里插入图片描述

FIN_WAIT_2定时器

TCP的TCP2_2MSL定时计数器实现了两种定时器:FIN_WAIT_2定时器和2MSL定时器。

FIN_WAIT_2定时器。当tcp_input从FIN_WAIT_1状态变迁为FIN_WAIT_2状态(调用了close,而不是shutdown),FIN_WAIT_2定时器设定为10分钟(tcp_maxidle)。这样可以防止连接永远停留在FIN_WAIT_2状态。

代码如下所示:

/*

* 2 MSL timeout in shutdown went off. If we're closed but

* still waiting for peer to close and connection has been idle

* too long, or if 2MSL time is up from TIME_WAIT, delete connection

* control block. Otherwise, check again in a bit.

*/

 case TCPT_2MSL:

 if (tp->t_state != TCPS_TIME_WAIT &&

tp->t_idle <= tcp_maxidle)

tp->t_timer[TCPT_2MSL] = tcp_keepintvl;

 else

tp = tcp_close(tp);

 break;

2MSL定时器

TCP的TCP2_2MSL定时计数器同时也实现了2MSL定时器,代码如上一节所示。MSL是指的报文段最大生存时间,设定一个2MSL的等待状态是为了不让网络中幸存的包(可能缓存在路由器中、经过多个路由器导致时间滞后的)重新发送给已经关闭的连接。

总结

TCP作为一个通用的网络协议,其作者们为了可靠性做出了巨大的努力,从而导致TCP内部存在各种机制,异常复杂,本文通过对TCP窗口和定时器所采用的各种算法进行了介绍,从而让大家能够进一步理解TCP协议。


推荐:

C/C++Linux服务器开发/高级架构师群:960994558 群内提供一些免费的C/C++Linux服务器开发/高级架构师学习资料资料包括C/C++,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等)

C/C++Linux服务器开发/高级架构师 系统学习
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值