TCP为每条连接建立七个定时器,依次为:连接建立定时器、重传定时器、延时ACK定时器、持续定时器、保活定时器、FIN_WAIT_2定时器和TIME_WAIT定时器。实际上,为了提高效率,内核中只使用了四个定时器来完成七个定时器的功能。
TCP定时器的实现涉及以下文件:
net/ipv4/tcp_timer.c TCP的定时器
net/ipv4/inet_connection_sock.c 基于连接的传输控制块实现
net/ipv4/tcp_output.c TCP的输出
net/ipv4/tcp_input.c TCP的输入
初始化
传输控制块定时器的初始化函数tcp_init_xmit_timers()在创建套接口、传输控制块时被调用
void inet_csk_init_xmit_timers(struct sock *sk, void (*retransmit_handler)(unsigned long), void (*delack_handler)(unsigned long), void (*keepalive_handler)(unsigned long)) { struct inet_connection_sock *icsk = inet_csk(sk); setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler, (unsigned long)sk); setup_timer(&icsk->icsk_delack_timer, delack_handler, (unsigned long)sk); setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk); icsk->icsk_pending = icsk->icsk_ack.pending = 0; } void tcp_init_xmit_timers(struct sock *sk) { inet_csk_init_xmit_timers(sk, &tcp_write_timer, &tcp_delack_timer, &tcp_keepalive_timer); }连接建立定时器
连接建立定时器用于被动建立连接时,服务端在接收到客户端的SYN,发送SYN+ACK段后,等待客户端的ACK段,试图建立一个新的连接时启动,超时几次后,连接建立将中止。
连接建立定时器处理函数会删除那些尝试连接的次数达到上限还未完成连接建立的传输控制块,该上限由inet_connection_sock结构的icsk_syn_retries给出。
连接建立定时器处理函数tcp_keepalive_timer(),该函数实现了TCP中的三个定时器:连接建立定时器、保活定时器和FIN_WAIT_2定时器。这是由于这三个定时器分别处于LISTEN、ESTABLISHED和FIN_WAIT2三种状态。
static void tcp_keepalive_timer (unsigned long data) { struct sock *sk = (struct sock *) data; struct inet_connection_sock *icsk = inet_csk(sk); struct tcp_sock *tp = tcp_sk(sk); __u32 elapsed; /* Only process if socket is not in use. */ bh_lock_sock(sk); if (sock_owned_by_user(sk)) { /* Try again later. */ inet_csk_reset_keepalive_timer (sk, HZ/20); goto out; } if (sk->sk_state == TCP_LISTEN) { tcp_synack_timer(sk); goto out; } if (sk->sk_state == TCP_FIN_WAIT2 && sock_flag(sk, SOCK_DEAD)) { if (tp->linger2 >= 0) { const int tmo = tcp_fin_time(sk) - TCP_TIMEWAIT_LEN; if (tmo > 0) { tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); goto out; } } tcp_send_active_reset(sk, GFP_ATOMIC); goto death; } if (!sock_flag(sk, SOCK_KEEPOPEN) || sk->sk_state == TCP_CLOSE) goto out; elapsed = keepalive_time_when(tp); /* It is alive without keepalive 8) */ if (tp->packets_out || tcp_send_head(sk)) goto resched; elapsed = tcp_time_stamp - tp->rcv_tstamp; if (elapsed >= keepalive_time_when(tp)) { if (icsk->icsk_probes_out >= keepalive_probes(tp)) { tcp_send_active_reset(sk, GFP_ATOMIC); tcp_write_err(sk); goto out; } if (tcp_write_wakeup(sk) <= 0) { icsk->icsk_probes_out++; elapsed = keepalive_intvl_when(tp); } else { /* If keepalive was lost due to local congestion, * try harder. */ elapsed = TCP_RESOURCE_PROBE_INTERVAL; } } else { /* It is tp->rcv_tstamp + keepalive_time_when(tp) */ elapsed = keepalive_time_when(tp) - elapsed; } TCP_CHECK_TIMER(sk); sk_mem_reclaim(sk); resched: inet_csk_reset_keepalive_timer (sk, elapsed); goto out; death: tcp_done(sk); out: bh_unlock_sock(sk); sock_put(sk); }重传定时器
重传定时器在TCP发送数据时设定,如果定时器已超时而对端确认还未到达,则TCP将重传数据。重传定时器的超时时间值是动态计算的,取决于TCP为该连接测量的往返时间以及该段已被重传的次数
static void tcp_write_timer(unsigned long data) { struct sock *sk = (struct sock *)data; struct inet_connection_sock *icsk = inet_csk(sk); int event; bh_lock_sock(sk); if (sock_owned_by_user(sk)) { /* Try again later */ sk_reset_timer(sk, &icsk->icsk_retransmit_timer, jiffies + (HZ / 20)); goto out_unlock; } if (sk->sk_state == TCP_CLOSE || !icsk->icsk_pending) goto out; if (time_after(icsk->icsk_timeout, jiffies)) { sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout); goto out; } event = icsk->icsk_pending; icsk->icsk_pending = 0; switch (event) { case ICSK_TIME_RETRANS: tcp_retransmit_timer(sk); break; case ICSK_TIME_PROBE0: tcp_probe_timer(sk); break; } TCP_CHECK_TIMER(sk); out: sk_mem_reclaim(sk); out_unlock: bh_unlock_sock(sk); sock_put(sk); }延时确认定时器
延时ACK定时器在TCP收到必须被确认但无需马上发出确认的段时设定,TCP在200ms后发送确认响应,如果在200ms内,有数据要在该连接上发送,延时ACK响应就可以随数据一起发送回对端,称为捎带确认。
static void tcp_delack_timer(unsigned long data) { struct sock *sk = (struct sock *)data; struct tcp_sock *tp = tcp_sk(sk); struct inet_connection_sock *icsk = inet_csk(sk); bh_lock_sock(sk); if (sock_owned_by_user(sk)) { /* Try again later. */ icsk->icsk_ack.blocked = 1; NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED); sk_reset_timer(sk, &icsk->icsk_delack_timer, jiffies + TCP_DELACK_MIN); goto out_unlock; } sk_mem_reclaim_partial(sk); if (sk->sk_state == TCP_CLOSE || !(icsk->icsk_ack.pending & ICSK_ACK_TIMER)) goto out; if (time_after(icsk->icsk_ack.timeout, jiffies)) { sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout); goto out; } icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER; if (!skb_queue_empty(&tp->ucopy.prequeue)) { struct sk_buff *skb; NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSCHEDULERFAILED); while ((skb = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) sk_backlog_rcv(sk, skb); tp->ucopy.memory = 0; } if (inet_csk_ack_scheduled(sk)) { if (!icsk->icsk_ack.pingpong) { /* Delayed ACK missed: inflate ATO. */ icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1, icsk->icsk_rto); } else { /* Delayed ACK missed: leave pingpong mode and * deflate ATO. */ icsk->icsk_ack.pingpong = 0; icsk->icsk_ack.ato = TCP_ATO_MIN; } tcp_send_ack(sk); NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS); } TCP_CHECK_TIMER(sk); out: if (tcp_memory_pressure) sk_mem_reclaim(sk); out_unlock: bh_unlock_sock(sk); sock_put(sk); }持续定时器
持续定时器在对端通告接收窗口为0,阻止TCP继续发送数据时而设定。由于连接对端发送的窗口通告不可靠(只有数据才会确认,ACK不会被确认),允许TCP继续发送数据的后续窗口更新可能丢失,因此,如果TCP有数据要发送,而对端通告接收窗口为0,则持续定时器启动,超时后向对端发送1字节的数据,以判断对端接收窗口是否已经打开。
static void tcp_probe_timer(struct sock *sk) { struct inet_connection_sock *icsk = inet_csk(sk); struct tcp_sock *tp = tcp_sk(sk); int max_probes; if (tp->packets_out || !tcp_send_head(sk)) { icsk->icsk_probes_out = 0; return; } /* *WARNING* RFC 1122 forbids this * * It doesn't AFAIK, because we kill the retransmit timer -AK * * FIXME: We ought not to do it, Solaris 2.5 actually has fixing * this behaviour in Solaris down as a bug fix. [AC] * * Let me to explain. icsk_probes_out is zeroed by incoming ACKs * even if they advertise zero window. Hence, connection is killed only * if we received no ACKs for normal connection timeout. It is not killed * only because window stays zero for some time, window may be zero * until armageddon and even later. We are in full accordance * with RFCs, only probe timer combines both retransmission timeout * and probe timeout in one bottle. --ANK */ max_probes = sysctl_tcp_retries2; if (sock_flag(sk, SOCK_DEAD)) { const int alive = ((icsk->icsk_rto << icsk->icsk_backoff) < TCP_RTO_MAX); max_probes = tcp_orphan_retries(sk, alive); if (tcp_out_of_resources(sk, alive || icsk->icsk_probes_out <= max_probes)) return; } if (icsk->icsk_probes_out > max_probes) { tcp_write_err(sk); } else { /* Only send another probe if we didn't close things up. */ tcp_send_probe0(sk); } }保活定时器
保活定时器在应用进程选取了套接口SO_KEEPALIVE选项时生效。如果连接的连续空闲时间超过2小时,则保活定时器超时,向对端发送连接探测段,强迫对端相应。相应处理函数tcp_keepalive_timer()。
FIN_WAIT_2定时器
当某个连接从FIN_WAIT_1状态变迁到FIN_WAIT_2状态,且不能再接收任何新数据时,则意味着应用进程调用了close()而非shutdown(),没有利用TCP的半关闭功能,FIN_WAIT_2定时器启动,超时时间为10min,在定时器第一次超时后,重新设置超时时间为75s,第二次超时后关闭连接。加入这个定时器的目的是为了避免对端一直不发FIN,某个连接会永远滞留在FIN_WAIT_2状态。处理函数为tcp_keepalive_timer()。