《TCP/IP详解 卷2》 笔记:TCP的定时器

版权声明:本文为博主原创文章,转载请注明原文出处。 https://blog.csdn.net/woay2008/article/details/79649931
TCP为每条连接建立了七个定时器。按照它们在一条连接生存期内出现的次序,简要介绍如下。
    1. “连接建立(connection establishment)”定时器在发送SYN报文段建立一条新连接时启动。如果没有在75秒内收到响应,连接建立将中止。
    2. “重传(retransmission)”定时器在TCP发送数据时设定。如果定时器已超时而对端的确认还未到达,TCP将重传数据。重传定时器的值(即TCP等待对端确认的时间,常称RTO)是动态计算的,取决于TCP为该连接测量的RTT和该报文段已被重传的次数。
    3. “延迟ACK(delayed ACK)”定时器在TCP收到必须被确认但无需马上发出确认的数据时设定。TCP等待200ms后发送确认响应。如果,在这200ms内,有数据要在该连接上发送,延迟的ACK响应就可随着数据一起发送回对端,称为“捎带确认”。
    4. “持续(persist)”定时器在连接对端通告接收窗口为0,阻止TCP继续发送数据时设定。由于连接对端发送的窗口通告不可靠(只有数据才会被确认,ACK不会被确认),允许TCP继续发送数据的后续窗口更新有可能丢失。因此,如果TCP有数据要发送,但对端通告接收窗口为0,则持续定时器启动,超时后向对端发送1字节的数据,判定对端接收窗口是否已打开。与重传定时器类似,持续定时器的值也是动态计算的,取决于连接的往返时间,在5秒到60秒之间取值。
    5. “保活(keepalive)”定时器在应用进程设置了socket的SO_KEEPALIVE选项时生效。如果连接的连续空闲时间超过2小时,保活定时器超时,向对端发送保活探测报文段,强迫对端响应。如果收到了期待的响应,TCP可确定对端主机工作正常,在该连接再次空闲超过2小时之前,TCP不会再进行保活测试。如果收到的是其他响应,TCP可确定对端主机已重启。如果连续若干次保活测试都未收到响应,TCP就假定对端主机已崩溃,尽管它无法区分是主机故障(例如,系统崩溃而尚未重启),还是连接故障(例如,中间的路由器发生故障或电话线断了)。
    6. FIN_WAIT_2定时器。当某个连接从FIN_WAIT_1状态变迁到FIN_WAIT_2状态,并且不能再接收任何新数据时(意味着应用进程调用了close,而非shutdown,没有使用TCP的半关闭功能),FIN_WAIT_2定时器启动,设为10分钟。定时器超时后,重新设为75秒,第二次超时后连接被关闭。加入这个定时器的目的是为了避免如果对端一直不发送FIN,某个连接会永远滞留在FIN_WAIT_2状态。

    7. TIME_WAIT定时器,一般也称为2MSL定时器。2MSL指两倍的MSL,MSL指的是最大报文段生存时间。当连接转移到TIME_WAIT状态,即连接主动关闭时,定时器启动。连接进入TIME_WAIT状态时,定时器设定为1分钟(Net/3选用30秒的MSL),超时后,TCP相关的资源被释放,端口号可重新使用。

TCP包括两个定时器函数:tcp_fasttimo函数每200ms调用一次(快速定时器,用于发送延迟的ACK);tcp_slowtimo函数每500ms调用一次(慢速定时器)。延迟ACK定时器与其他六个定时器有所不同:如果某个连接上设定了延迟ACK标志,那么下一次200ms定时器超时后,延迟的ACK必须被发送(ACK的延迟时间必须在0~200ms之间)。其他的定时器是靠计数器实现的,计数器每500ms递减一次,当减为0时,就触发相关定时器的动作。

前面提到过,TCP协议的控制块tcpcb结构中的t_timer数组中包括四个计数器,这四个计数器用于实现除延迟ACK定时器之外的其他六个定时器。四个计数器能实现六个定时器是因为保活定时器和连接建立定时器是互斥的,2MSL定时器和FIN_WAIT_2定时器是互斥的。

#define	TCPT_NTIMERS	4

#define	TCPT_REXMT	0		/*重传定时器*/
#define	TCPT_PERSIST	1		/*持续定时器*/
#define	TCPT_KEEP	2		/*保活定时器或连接建立定时器*/
#define	TCPT_2MSL	3		/*2MSL定时器或FIN_WAIT_2定时器*/

与定时器相关的宏和全局变量如下:

#define	PR_SLOWHZ	2		        /*1秒内慢速定时器(500ms)的滴答数*/
#define	PR_FASTHZ	5		        /*1秒内快速定时器(200ms)的滴答数*/

#define	TCPTV_MSL	( 30*PR_SLOWHZ)		/*MSL,最大报文段生存时间 30秒*/

#define	TCPTV_MIN	(  1*PR_SLOWHZ)		/*重传定时器最小值 1秒*/
#define	TCPTV_REXMTMAX	( 64*PR_SLOWHZ)		/*重传定时器最大值 64秒*/

#define	TCPTV_PERSMIN	(  5*PR_SLOWHZ)		/*持续定时器最小值 5秒*/
#define	TCPTV_PERSMAX	( 60*PR_SLOWHZ)		/*持续定时器最大值 60秒*/

#define	TCPTV_KEEP_INIT	( 75*PR_SLOWHZ)		/*连接建立定时器的值 75秒*/
#define	TCPTV_KEEP_IDLE	(120*60*PR_SLOWHZ)	/*第一次保活测试前连接的空闲时间 2小时*/
#define	TCPTV_KEEPINTVL	( 75*PR_SLOWHZ)		/*对端无响应时保活探测的时间间隔 75秒*/

/*这两个宏,定义了第一次超时重传时间RTO = A + 2 * D,6秒*/
#define	TCPTV_SRTTBASE	0			/*平滑的RTT(估计器)初始值*/
#define	TCPTV_SRTTDFLT	(  3*PR_SLOWHZ)		/*平滑的RTT平均偏差(估计器)初始值 3秒*/

#define	TCPTV_KEEPCNT	8			/*最大保活测试次数*/
#define	TCP_LINGERTIME	120			/*SO_LINGER选项设置的最大时间 120秒*/
#define	TCP_MAXRXTSHIFT	12			/*最大重传次数*/

int	tcp_keepidle = TCPTV_KEEP_IDLE;         /*第一次保活测试前连接的空闲时间 2小时*/
int	tcp_keepintvl = TCPTV_KEEPINTVL;        /*对端无响应时保活探测的时间间隔 75秒*/
int	tcp_maxidle;                            /*连接的最大空闲时间 10分钟*/
int	tcp_keepcnt = TCPTV_KEEPCNT;		/*最大保活测试次数*/
int	tcp_maxpersistidle = TCPTV_KEEP_IDLE;	/*第一次保活测试前连接的空闲时间 2小时*/

tcp_fasttimo函数的代码如下:

/*
 * Fast timeout routine for processing delayed acks
 */
void
tcp_fasttimo()
{
	register struct inpcb *inp;
	register struct tcpcb *tp;
	int s = splnet();

	inp = tcb.inp_next;
	if (inp)
	for (; inp != &tcb; inp = inp->inp_next) /*循环遍历每个inpcb*/
		if ((tp = (struct tcpcb *)inp->inp_ppcb) && /*tcpcb不为空*/
		    (tp->t_flags & TF_DELACK)) { /*如果连接上设置了延迟的ACK标志*/
			tp->t_flags &= ~TF_DELACK;
			tp->t_flags |= TF_ACKNOW;
			tcpstat.tcps_delack++;
			(void) tcp_output(tp); /*调用tcp_output函数发送延迟的ACK*/
		}
	splx(s);
}

tcp_slowtimo函数的代码如下:

/*
 * Tcp protocol timeout routine called every 500 ms.
 * Updates the timers in all active tcb's and
 * causes finite state machine actions if timers expire.
 */
void
tcp_slowtimo()
{
	register struct inpcb *ip, *ipnxt;
	register struct tcpcb *tp;
	int s = splnet();
	register int i;

	tcp_maxidle = tcp_keepcnt * tcp_keepintvl;
	/*
	 * Search through tcb's and update active timers.
	 */
	ip = tcb.inp_next;
	if (ip == 0) {
		splx(s);
		return;
	}
	for (; ip != &tcb; ip = ipnxt) { /*遍历每个inpcb*/
		ipnxt = ip->inp_next;
		tp = intotcpcb(ip);
		if (tp == 0 || tp->t_state == TCPS_LISTEN) 
			continue;
		for (i = 0; i < TCPT_NTIMERS; i++) { /*遍历tcpcb中的每个计数器*/
			if (tp->t_timer[i] && --tp->t_timer[i] == 0) { /*计数器的值本次递减为0,表示相应的定时器超时*/
				(void) tcp_usrreq(tp->t_inpcb->inp_socket,
				    PRU_SLOWTIMO, (struct mbuf *)0,
				    (struct mbuf *)i, (struct mbuf *)0); /*以PRU_SLOWTIMO命令调用tcp_usrreq函数*/
				if (ipnxt->inp_prev != ip)
					goto tpgone;
			}
		}
		tp->t_idle++; /*更新连接的空闲时间*/
		if (tp->t_rtt)
			tp->t_rtt++; /*更新连接中正被计时的报文时间,用于无时间戳选项时测量RTT*/
tpgone:
		;
	}
	tcp_iss += TCP_ISSINCR/PR_SLOWHZ;		/*更新全局的tcp_iss,用于新建连接时初始的发送序列号*/
	tcp_now++;					/*更新全局的时间戳*/
	splx(s);
}

对于PRU_SLOWTIMO命令,tcp_usrreq函数将调用tcp_timers函数处理,传入的参数是相应的计数器。tcp_timers函数的代码如下:

/* 
 * TCP timer processing. 
 */  
struct tcpcb *  
tcp_timers(tp, timer)  
	register struct tcpcb *tp;  
	int timer;  
{  
	register int rexmt;  
  
	switch (timer) {  
  
	/* 
	 * 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: /*2MSL定时器或FIN_WAIT_2定时器*/  
		if (tp->t_state != TCPS_TIME_WAIT &&  
			tp->t_idle <= tcp_maxidle) /*当连接处于FIN_WAIT_2状态时超时且连接空闲时间小于10分钟,定时器设置成75秒*/  
			tp->t_timer[TCPT_2MSL] = tcp_keepintvl;  
		else /*当连接处于TIME_WAIT状态时超时或处于FIN_WAIT_2状态时超时且连接空闲时间大于10分钟,关闭连接*/  
			tp = tcp_close(tp);  
		break;  
  
	/* 
	 * Retransmission timer went off.  Message has not 
	 * been acked within retransmit interval.  Back off 
	 * to a longer retransmit interval and retransmit one segment. 
	 */  
	case TCPT_REXMT: /*重传定时器*/  
		if (++tp->t_rxtshift > TCP_MAXRXTSHIFT) { /*超过最大重传次数12*/  
			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) { /*如果已重传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; /*下一个要发送的数据是最早的未确认的数据*/  
		/* 
		 * If timing a segment in this window, stop the timer. 
		 */  
		tp->t_rtt = 0; /*不对重传的报文计时*/  
		/* 
		 * Close the congestion window down to one segment 
		 * (we'll open it by one segment for each ack we get). 
		 * Since we probably have a window's worth of unacked 
		 * data accumulated, this "slow start" keeps us from 
		 * dumping all that data as back-to-back packets (which 
		 * might overwhelm an intermediate gateway). 
		 * 
		 * There are two phases to the opening: Initially we 
		 * open by one mss on each ack.  This makes the window 
		 * size increase exponentially with time.  If the 
		 * window is larger than the path can handle, this 
		 * exponential growth results in dropped packet(s) 
		 * almost immediately.  To get more time between  
		 * drops but still "push" the network to take advantage 
		 * of improving conditions, we switch from exponential 
		 * to linear window opening at some threshhold size. 
		 * For a threshhold, we use half the current window 
		 * size, truncated to a multiple of the mss. 
		 * 
		 * (the minimum cwnd that will give us exponential 
		 * growth is 2 mss.  We don't allow the threshhold 
		 * to go below this.) 
		 */  
		{ /*慢启动和拥塞避免!*/  
		u_int win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;  
		if (win < 2)  
			win = 2;  
		tp->snd_cwnd = tp->t_maxseg; /*拥塞窗口设置为一个报文段大小,强迫执行慢启动*/
		tp->snd_ssthresh = win * tp->t_maxseg;  /*令慢启动门限为当前拥塞窗口的一半,最小值为两个报文段*/  
		tp->t_dupacks = 0;  
		}  
		(void) tcp_output(tp); /*重传报文*/  
		break;  
  
	/* 
	 * Persistance timer into zero window. 
	 * Force a byte to be output, if possible. 
	 */  
	case TCPT_PERSIST: /*持续定时器*/  
		tcpstat.tcps_persisttimeo++;  
		tcp_setpersist(tp); /*设置持续定时器下一次超时时间*/  
		tp->t_force = 1;  
		(void) tcp_output(tp); /*发送窗口探测报文*/  
		tp->t_force = 0;  
		break;  
  
	/* 
	 * 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;  
		if (tp->t_inpcb->inp_socket->so_options & SO_KEEPALIVE &&  
			tp->t_state <= TCPS_CLOSE_WAIT) { /*当设置了SO_KEEPALIVE选项并且连接处于ESTABLISHED或CLOSE_WAIT状态,将发送保活探测报文*/  
				if (tp->t_idle >= tcp_keepidle + tcp_maxidle) /*连接空闲时间超过2小时10分钟,丢弃连接*/  
				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_respond(tp, tp->t_template, (struct mbuf *)NULL,  
				tp->rcv_nxt, tp->snd_una - 1, 0); /*发送保活探测报文,确认序列号字段是rcv_nxt,序列号字段是snd_una-1 
					,由于这一序号落在对端的接收窗口之外,对端必然发送ACK*/  
			tp->t_timer[TCPT_KEEP] = tcp_keepintvl; /*设置超时时间为75秒*/  
		} else  
			tp->t_timer[TCPT_KEEP] = tcp_keepidle;   
		break;  
	dropit:  
		tcpstat.tcps_keepdrops++;  
		tp = tcp_drop(tp, ETIMEDOUT);  
		break;  
	}  
	return (tp);  
}    


猜你喜欢

转载自blog.csdn.net/woay2008/article/details/79649931
今日推荐