TCP窗口擴大選項Window Scale


窗口擴大選項使TCP的窗口定義從16bit增加到32bit。這並不是通過修改TCP首部來實現的,TCP首部仍然使用16bit,而是通過定義一個選項實現對16bit的擴大操作來完成的。於是TCP在內部將實際的窗口大小維持為32bit的值;

窗口擴大選項的取值范圍為0<=shif.cnt<=14;最大值為14的原因如下:

【這段內容為本人理解,非書中原文】TCP需要保留一半的序號用於判斷是否是以前的舊數據段,(2^32-1)/2<2^31,也就是現在用於處理新數據段小於2^31個;假如窗口大小為wnd,發送方第一次發送[0,wnd-1]一個窗口的數據,這時接收方收到數據,並統一發送一個ack確認這個窗口的數據,確認完之后,窗口移動准備接收[wnd, 2wnd-1]段的數據;若發送方如果沒有收到這個ack,需要重傳[0, wnd-1]的數據,若收到了ack,則發送[wnd, 2wnd-1]的數據,這兩種情況下,接收方都需要滿足序號,也就是序號需要滿足2*wnd;前面說了序號的一半用於新數據,也就是2*wnd<2^31,wnd<2^30,即(2^16-1)*2^shif < 2^30,得出shift<=14,即除了TCP首部的16bit,這里最多也就是30-16=14bit;

【這段是找到的RFC原文】大概意思是發送和接收窗口,一個窗口的左邊沿到另外一個窗口的右邊沿最大不能超過2^31,如果超過這個值就與保留的舊序號重疊了,也就是2*wnd < 2^31,wnd  < 2^30,得出shift<=14;

      RFC1323
      TCP determines if a data segment is "old" or "new" by testing
      whether its sequence number is within 2**31 bytes of the left edge
      of the window, and if it is not, discarding the data as "old".  To
      insure that new data is never mistakenly considered old and vice-
      versa, the left edge of the sender's window has to be at most
      2**31 away from the right edge of the receiver's window.
      Similarly with the sender's right edge and receiver's left edge.
      Since the right and left edges of either the sender's or
      receiver's window differ by the window size, and since the sender
      and receiver windows can be out of phase by at most the window
      size, the above constraints imply that 2 * the max window size
      must be less than 2**31, or
      
           max window < 2**30
           
      Since the max window is 2**S (where S is the scaling shift count)
      times at most 2**16 - 1 (the maximum unscaled window), the maximum
      window is guaranteed to be < 2*30 if S <= 14.  Thus, the shift
      count must be limited to 14 (which allows windows of 2**30 = 1
      Gbyte).  If a Window Scale option is received with a shift.cnt
      value exceeding 14, the TCP should log the error but use 14
      instead of the specified value.

 

這個選項只能出現在SYN報文段中,因此當連接建立起來后,每個方向上的擴大因子是固定的。為了使用窗口擴大,兩端必須在他們的SYN報文段中發送該選項。主動建立連接的一方在其SYN中發送這個選項,但是被動建立連接的一方只能夠在接收到帶這個選項的SYN之后才可以發送這個選項。每個方向上的擴大因此可以不同;

如果主動連接的一方發送一個非零的擴大因子,但是沒有從另一端收到一個擴大選項,它就將發送和接收的移位計數器設置為0。這就允許較新的系統能夠與較舊的系統進行互相操作;

假定我們正在使用窗口擴大選項,發送移位計數為S,而接收到的移位計數為R。於是我們從另一端接收到的16bit的通告窗口將被左移R位以獲得實際的通告窗口大小。每次當我們向對方發送一個窗口通告的時候,我們將實際的32bit窗口大小右移S位,然后用它來替換TCP首部中的16bit窗口值;

TCP根據接收緩存的大小自動選擇位移技術。這個大小是由系統設置的,但是通常向應用程序提供了修改途徑;

 

文章來源: <TCP/IP詳解>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM