天天看點

tcp短連接配接TIME_WAIT問題解決方法大全(5)——tcp_max_tw_buckets

參考官方文檔(http://www.kernel.org/doc/documentation/networking/ip-sysctl.txt),解釋如下:

tcp_max_tw_buckets - integer

maximal number of timewait sockets held by system simultaneously.

if this number is exceeded time-wait socket is immediately destroyed

and warning is printed. 

官方文檔沒有說明預設值,通過幾個系統的簡單驗證,初步确定預設值是180000。

通過源碼檢視發現,這個選項比較簡單,其實作代碼如下:

=====linux-2.6.37 net/ipv4/tcp_minisocks.c 269======

void tcp_time_wait(struct sock *sk, int state, int timeo)

{

struct inet_timewait_sock *tw = null;

const struct inet_connection_sock *icsk = inet_csk(sk);

const struct tcp_sock *tp = tcp_sk(sk);

int recycle_ok = 0;

if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)

recycle_ok = icsk->icsk_af_ops->remember_stamp(sk);

if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets)

tw = inet_twsk_alloc(sk, state);

if (tw != null) {

        //配置設定成功,進行time_wait狀态處理,此處略去很多代碼

    else {

        //配置設定失敗,不進行處理,隻記錄日志: tcp:

time wait bucket table overflow

/* sorry, if we're out of memory, just close this

* socket up.  we've got bigger problems than

* non-graceful socket closings.

*/

net_inc_stats_bh(sock_net(sk), linux_mib_tcptimewaitoverflow);

}

tcp_update_metrics(sk);

tcp_done(sk);

實測結果驗證,配置為100,time_wait連接配接數就穩定在100,且不受組網和其它配置的影響。

官方手冊中有一段警告:

    this limit exists only to prevent

simple dos attacks, you _must_ not lower the limit artificially,

but rather increase it (probably, after increasing installed memory),

if network conditions require more than default value.

基本意思是這個用于防止dos攻擊,我們不應該人工減少,如果網絡條件需要的話,反而應該增加。

但其實對于我們的區域網路或者公司内網應用來說,這個風險并不大。

繼續閱讀