TIME_WAIT 和tcp_tw_reuse

tcp_tw_reuse選項的含義如下(http://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt):
tcp_tw_reuse - BOOLEAN
Allow to reuse TIME-WAIT sockets for new connections when it is
safe from protocol viewpoint. Default value is 0.

    
這裏的關鍵在於“協議什麼情況下認爲是安全的”,由於環境限制,沒有辦法進行驗證,通過看源碼簡單分析了一下。
=====linux-2.6.37 net/ipv4/tcp_ipv4.c 114=====
int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
{
const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
struct tcp_sock *tp = tcp_sk(sk);


/* With PAWS, it is safe from the viewpoint
  of data integrity. Even without PAWS it is safe provided sequence
  spaces do not overlap i.e. at data rates <= 80Mbit/sec.


  Actually, the idea is close to VJ's one, only timestamp cache is
  held not per host, but per port pair and TW bucket is used as state
  holder.


  If TW bucket has been already destroyed we fall back to VJ's scheme
  and use initial timestamp retrieved from peer table.
*/
    //從代碼來看,tcp_tw_reuse選項和tcp_timestamps選項也必須同時打開;否則tcp_tw_reuse就不起作用
    //另外,所謂的“協議安全”,從代碼來看應該是收到最後一個包後超過1s

if (tcptw->tw_ts_recent_stamp &&
   (twp == NULL || (sysctl_tcp_tw_reuse &&
    get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
if (tp->write_seq == 0)
tp->write_seq = 1;
tp->rx_opt.ts_recent  = tcptw->tw_ts_recent;
tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
sock_hold(sktw);
return 1;
}


return 0;

}


總結一下:
1)tcp_tw_reuse選項和tcp_timestamps選項也必須同時打開;
2)重用TIME_WAIT的條件是收到最後一個包後超過1s。



官方手冊有一段警告:
It should not be changed without advice/request of technical
experts.
對於大部分局域網或者公司內網應用來說,滿足條件2)都是沒有問題的,因此官方手冊裏面的警告其實也沒那麼可怕:)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章