天天看點

vsftpd的逾時與流控機制

最近有個項目需要使用vsftpd 3.0.2,看了少部分代碼,分析了兩個參數。

idle_session_timeout

The timeout, in seconds, which is the maximum time a remote client may spend between FTP commands. If the timeout triggers, the remote client is kicked off.

Default: 300(秒)

這個參數是指等待輸入FTP指令的空閑時間(秒)。初次連上FTP服務後、或上一次FTP指令執行完成後,開始計時。相當于使用FTP用戶端指令行工具時,出現輸入提示符,等待使用者輸入的時間。

這個時間逾時,用戶端的(一個TCP,指令)連接配接會被斷開。

data_connection_timeout

The timeout, in seconds, which is roughly the maximum time we permit data transfers to stall for with no progress.

If the timeout triggers, the remote client is kicked off.

Default: 300(秒)

這個參數是指等待資料傳輸(上傳/下載下傳)的空閑時間(秒)。當FTP服務端每接收/或發送一次資料包(trans_chunk_size大小,預設值是8KB),就會複位一次這個定時器。相當于使用FTP用戶端指令行工具時,出現傳輸速率為0的持續時間。

這個時間逾時,用戶端的(兩個TCP,指令與資料)連接配接都會被斷開。

當 data_connection_timeout 定時器啟動時,idle_session_timeout定時器會停止。即兩個定時,同一時刻隻有一個有效!

trans_chunk_size

You probably don’t want to change this, but try setting it to something like 8192 for a much smoother bandwidth limiter.

Default: 0 (let vsftpd pick a sensible setting)

每次收發資料的大小,位元組/秒。

實際上,每次收發資料的多少,是由函數get_chunk_size()确定的:

#define VSFTP_DATA_BUFSIZE      65536   # 64KB

......

void
tunables_load_defaults()
{
  ......
  tunable_trans_chunk_size = ;
  ......
}

static unsigned int get_chunk_size()
{
  unsigned int ret = VSFTP_DATA_BUFSIZE;
  if (tunable_trans_chunk_size < VSFTP_DATA_BUFSIZE &&
      tunable_trans_chunk_size > )
  {
    ret = tunable_trans_chunk_size;
    if (ret < )
    {
      ret = ;
    }
  }
  return ret;
}
           

當 0 < trans_chunk_size < VSFTP_DATA_BUFSIZE時, get_chunk_size() 最小為 4096;否則get_chunk_size() 為VSFTP_DATA_BUFSIZE。

每次收發完成VSFTP_DATA_BUFSIZE大小的資料

local_max_rate

The maximum data transfer rate permitted, in bytes per second, for local authenticated users.

Default: 0 (unlimited)

本地認證使用者最大傳輸速率,機關:位元組/秒。

每次收發完 get_chunk_size() 大小的資料,vsftpd就開始進行流控 —— 計算需要等待的時間,然後就開始休眠(“nanosleep()”)。醒來後再進行下一次的傳輸。

舉兩個例子,當 get_chunk_size() 為 65536時:

  • 當你把 local_max_rate 設為 1024 位元組/秒時,若每次發送 65536位元組的資料則需要 64秒。例如你在20ms内傳輸完了65536位元組的資料,那麼你還要等64-0.02=63.98秒,才會再次收到資料。
  • 當你把 local_max_rate 設為 10240 位元組/秒時,若每次發送 65536位元組的資料則需要 6.4秒。例如你在20ms内傳輸完了65536位元組的資料,那麼你還要等6.4-0.02=6.38秒,才會再次收到資料。