TCP RTO計算方法以及go實現驗證

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

概述

最近在研究tcp的重傳機制,tcp的重傳大概有三種,逾時重傳(rto)、快速重傳(fack)、早期重傳(er)。今天講講rto,並用go實現其演算法,探究一下。主要參考tcp/ip-guid

基本概念

RTO即逾時重傳時間
RTT資料包往返時間
平均偏差是指單項測定值與平均值的偏差(取絕對值)之和,除以測定次數。

RTO計算演算法

RTO的計算依賴於RTT值,或者說一系列RTT值。rto=f(rtt)
在Linux中,最開始實現的是一個比較簡單的經典演算法RFC793,後來1988年提出了新的演算法計算rto值,文檔為RFC6298. 下面對比一下兩種演算法。

經典演算法

rfc原文

    An Example Retransmission Timeout Procedure      Measure the elapsed time between sending a data octet with a      particular sequence number and receiving an acknowledgment that      covers that sequence number (segments sent do not have to match      segments received).  This measured elapsed time is the Round Trip      Time (RTT).  Next compute a Smoothed Round Trip Time (SRTT) as:        SRTT = ( ALPHA * SRTT ) + ((1-ALPHA) * RTT)      and based on this, compute the retransmission timeout (RTO) as:        RTO = min[UBOUND,max[LBOUND,(BETA*SRTT)]]      where UBOUND is an upper bound on the timeout (e.g., 1 minute),      LBOUND is a lower bound on the timeout (e.g., 1 second), ALPHA is      a smoothing factor (e.g., .8 to .9), and BETA is a delay variance      factor (e.g., 1.3 to 2.0).

其中說明了計算rto的計算公式:

SRTT = ( ALPHA * SRTT ) + ((1-ALPHA) * RTT)  RTO = min[UBOUND,max[LBOUND,(BETA*SRTT)]]

先計算SRTT(Smoothed Round Trip Time)平滑RTT時間,然後再比較設定的最大最小RTO來計算RTO值來限定最大最小RTO值。其中 ALPHA是一個平滑因子,取值為0.8或者0.9,BETA也是一個因子,建議是1.3-2.0。這個演算法很簡單我們用go來實現一下,ALPHA取0.8,BETA取1.6。

代碼實現

package mainimport ("fmt")const (ALPHA = 0.8BETA  = 1.6)const (LBOUND = 100UBOUND = 60000)type TCP struct {srtt int32rto  uint32}func main() {tcp := &TCP{}lastRTO := uint32(0)rtts := []int32{100, 103, 105, 102, 100, 103, 105, 100, 103, 105, 102, 100, 102, 105, 100, 103, 105, 104, 100, 103, 105,200, 203, 205, 202, 200, 203, 205, 200, 203, 205, 202, 200, 202, 205, 200, 203, 205, 204, 200, 203, 205,400, 403, 405, 404,100, 103, 105, 102, 100, 103, 105, 100, 103, 105, 102, 100, 102, 105, 100, 103, 105, 104, 100, 103, 105,// 400, 403, 405, 400, 403, 405, 404, 400, 404, 405, 400, 403, 405, 404, 400, 403, 405,}for i, rtt := range rtts {if lastRTO == 0 {lastRTO = uint32(rtt)}rto := tcp.updateRTT(rtt)if uint32(rtt) > lastRTO {fmt.Printf("warn: loss packet, index: %d\n", i-1)}fmt.Printf("index: %d, rtt: %d, rto: %d\n", i, rtt, rto)lastRTO = rto}}func (t *TCP) updateRTT(rtt int32) uint32 {var rto uint32if t.srtt == 0 {t.srtt = rttrto = uint32(rtt)} else {t.srtt = int32(ALPHA*float32(t.srtt) + ((1 - ALPHA) * float32(rtt)))rto = min(UBOUND, max(LBOUND, uint32(BETA*float32(t.srtt))))}return rto}func min(a, b uint32) uint32 {if a <= b {return a}return b}func max(a, b uint32) uint32 {if a >= b {return a}return b}

運行結果

index: 0, rtt: 100, rto: 100warn: loss packet, index: 0index: 1, rtt: 103, rto: 160index: 2, rtt: 105, rto: 161index: 3, rtt: 102, rto: 161index: 4, rtt: 100, rto: 160index: 5, rtt: 103, rto: 160index: 6, rtt: 105, rto: 161index: 7, rtt: 100, rto: 160index: 8, rtt: 103, rto: 160index: 9, rtt: 105, rto: 161index: 10, rtt: 102, rto: 161index: 11, rtt: 100, rto: 160index: 12, rtt: 102, rto: 160index: 13, rtt: 105, rto: 161index: 14, rtt: 100, rto: 160index: 15, rtt: 103, rto: 160index: 16, rtt: 105, rto: 161index: 17, rtt: 104, rto: 161index: 18, rtt: 100, rto: 160index: 19, rtt: 103, rto: 160index: 20, rtt: 105, rto: 161warn: loss packet, index: 20index: 21, rtt: 200, rto: 192warn: loss packet, index: 21index: 22, rtt: 203, rto: 217index: 23, rtt: 205, rto: 238index: 24, rtt: 202, rto: 254index: 25, rtt: 200, rto: 267index: 26, rtt: 203, rto: 278index: 27, rtt: 205, rto: 288index: 28, rtt: 200, rto: 294index: 29, rtt: 203, rto: 299index: 30, rtt: 205, rto: 304index: 31, rtt: 202, rto: 307index: 32, rtt: 200, rto: 308index: 33, rtt: 202, rto: 310index: 34, rtt: 205, rto: 313index: 35, rtt: 200, rto: 313index: 36, rtt: 203, rto: 315index: 37, rtt: 205, rto: 316index: 38, rtt: 204, rto: 318index: 39, rtt: 200, rto: 318index: 40, rtt: 203, rto: 318index: 41, rtt: 205, rto: 320warn: loss packet, index: 41index: 42, rtt: 400, rto: 384warn: loss packet, index: 42index: 43, rtt: 403, rto: 435index: 44, rtt: 405, rto: 476index: 45, rtt: 404, rto: 510index: 46, rtt: 100, rto: 440index: 47, rtt: 103, rto: 384index: 48, rtt: 105, rto: 340index: 49, rtt: 102, rto: 304index: 50, rtt: 100, rto: 275index: 51, rtt: 103, rto: 252index: 52, rtt: 105, rto: 235index: 53, rtt: 100, rto: 219index: 54, rtt: 103, rto: 208index: 55, rtt: 105, rto: 200index: 56, rtt: 102, rto: 192index: 57, rtt: 100, rto: 185index: 58, rtt: 102, rto: 180index: 59, rtt: 105, rto: 177index: 60, rtt: 100, rto: 172index: 61, rtt: 103, rto: 171index: 62, rtt: 105, rto: 169index: 63, rtt: 104, rto: 168index: 64, rtt: 100, rto: 166index: 65, rtt: 103, rto: 164index: 66, rtt: 105, rto: 164

從結果中可以看出,這邊丟失了5個資料包,假設這裡沒有真實的丟包,而僅僅是網路抖動,那麼可以知道這個演算法抗網路的抖動性很低。

Jacobaon/Karels 演算法

這是一種比經典演算法更新的演算法,當然表現也更好,等會親手試試,看看結果就知道了。

rfc原文

The Basic Algorithm   To compute the current RTO, a TCP sender maintains two state   variables, SRTT (smoothed round-trip time) and RTTVAR (round-trip   time variation).  In addition, we assume a clock granularity of G   seconds.   The rules governing the computation of SRTT, RTTVAR, and RTO are as   follows:   (2.1) Until a round-trip time (RTT) measurement has been made for a         segment sent between the sender and receiver, the sender SHOULD         set RTO <- 1 second, though the "backing off" on repeated         retransmission discussed in (5.5) still applies.         Note that the previous version of this document used an initial         RTO of 3 seconds [PA00].  A TCP implementation MAY still use         this value (or any other value > 1 second).  This change in the         lower bound on the initial RTO is discussed in further detail         in Appendix A.   (2.2) When the first RTT measurement R is made, the host MUST set            SRTT <- R            RTTVAR <- R/2            RTO <- SRTT + max (G, K*RTTVAR)         where K = 4.   (2.3) When a subsequent RTT measurement R' is made, a host MUST set            RTTVAR <- (1 - beta) * RTTVAR + beta * |SRTT - R'|            SRTT <- (1 - alpha) * SRTT + alpha * R'         The value of SRTT used in the update to RTTVAR is its value         before updating SRTT itself using the second assignment.  That         is, updating RTTVAR and SRTT MUST be computed in the above         order.         The above SHOULD be computed using alpha=1/8 and beta=1/4 (as         suggested in [JK88]).         After the computation, a host MUST update         RTO <- SRTT + max (G, K*RTTVAR)   (2.4) Whenever RTO is computed, if it is less than 1 second, then the         RTO SHOULD be rounded up to 1 second.         Traditionally, TCP implementations use coarse grain clocks to         measure the RTT and trigger the RTO, which imposes a large         minimum value on the RTO.  Research suggests that a large         minimum RTO is needed to keep TCP conservative and avoid         spurious retransmissions [AP99].  Therefore, this specification         requires a large minimum RTO as a conservative approach, while         at the same time acknowledging that at some future point,         research may show that a smaller minimum RTO is acceptable or         superior.   (2.5) A maximum value MAY be placed on RTO provided it is at least 60         seconds.

rto的計算公式:

第一次rtt計算: SRTT = RRTTVAR = R/2RTO = SRTT + max (G, K*RTTVAR)之後:RTTVAR = (1 - beta) * RTTVAR + beta * |SRTT - R'|SRTT = (1 - alpha) * SRTT + alpha * R'RTO = SRTT + max (G, K*RTTVAR)

SRTT(smoothed round-trip time)平滑RTT時間
RTTVAR(round-trip time variation)RTT變數,其實就是rtt平均偏差
G表示系統時鐘的粒度,一般很小,us層級。beta = 1⁄4, alpha = 1⁄8

代碼實現

package mainimport ("fmt")const (LBOUND = 100UBOUND = 60000)type TCP struct {srtt, rttvar int32rto          uint32}func main() {tcp := &TCP{}lastRTO := uint32(0)rtts := []int32{100, 103, 105, 102, 100, 103, 105, 100, 103, 105, 102, 100, 102, 105, 100, 103, 105, 104, 100, 103, 105,200, 203, 205, 202, 200, 203, 205, 200, 203, 205, 202, 200, 202, 205, 200, 203, 205, 204, 200, 203, 205,400, 403, 405, 404,100, 103, 105, 102, 100, 103, 105, 100, 103, 105, 102, 100, 102, 105, 100, 103, 105, 104, 100, 103, 105,// 400, 403, 405, 400, 403, 405, 404, 400, 404, 405, 400, 403, 405, 404, 400, 403, 405,}for i, rtt := range rtts {if lastRTO == 0 {lastRTO = uint32(rtt)}rto := tcp.updateRTT(rtt)if uint32(rtt) > lastRTO {fmt.Printf("warn: loss packet, index: %d\n", i-1)}fmt.Printf("index: %d, rtt: %d, rto: %d\n", i, rtt, rto)lastRTO = rto}}func (t *TCP) updateRTT(rtt int32) uint32 {var rto uint32if t.srtt == 0 {t.srtt = rttt.rttvar = rtt >> 1} else {delta := rtt - t.srttif delta < 0 {delta = -delta}t.rttvar = (3*t.srtt + delta) >> 2t.srtt = (7*t.srtt + rtt) >> 3}rto = uint32(t.srtt + 4*t.rttvar)rto = min(UBOUND, max(LBOUND, rto))return rto}func min(a, b uint32) uint32 {if a <= b {return a}return b}func max(a, b uint32) uint32 {if a >= b {return a}return b}

運行結果

index: 0, rtt: 100, rto: 300index: 1, rtt: 103, rto: 400index: 2, rtt: 105, rto: 404index: 3, rtt: 102, rto: 400index: 4, rtt: 100, rto: 400index: 5, rtt: 103, rto: 400index: 6, rtt: 105, rto: 404index: 7, rtt: 100, rto: 400index: 8, rtt: 103, rto: 400index: 9, rtt: 105, rto: 404index: 10, rtt: 102, rto: 400index: 11, rtt: 100, rto: 400index: 12, rtt: 102, rto: 400index: 13, rtt: 105, rto: 404index: 14, rtt: 100, rto: 400index: 15, rtt: 103, rto: 400index: 16, rtt: 105, rto: 404index: 17, rtt: 104, rto: 404index: 18, rtt: 100, rto: 400index: 19, rtt: 103, rto: 400index: 20, rtt: 105, rto: 404index: 21, rtt: 200, rto: 512index: 22, rtt: 203, rto: 547index: 23, rtt: 205, rto: 581index: 24, rtt: 202, rto: 609index: 25, rtt: 200, rto: 628index: 26, rtt: 203, rto: 650index: 27, rtt: 205, rto: 672index: 28, rtt: 200, rto: 685index: 29, rtt: 203, rto: 701index: 30, rtt: 205, rto: 713index: 31, rtt: 202, rto: 724index: 32, rtt: 200, rto: 731index: 33, rtt: 202, rto: 741index: 34, rtt: 205, rto: 748index: 35, rtt: 200, rto: 754index: 36, rtt: 203, rto: 760index: 37, rtt: 205, rto: 770index: 38, rtt: 204, rto: 775index: 39, rtt: 200, rto: 772index: 40, rtt: 203, rto: 777index: 41, rtt: 205, rto: 782index: 42, rtt: 400, rto: 1007index: 43, rtt: 403, rto: 1082index: 44, rtt: 405, rto: 1150index: 45, rtt: 404, rto: 1207index: 46, rtt: 100, rto: 1272index: 47, rtt: 103, rto: 1156index: 48, rtt: 105, rto: 1055index: 49, rtt: 102, rto: 976index: 50, rtt: 100, rto: 907index: 51, rtt: 103, rto: 840index: 52, rtt: 105, rto: 782index: 53, rtt: 100, rto: 741index: 54, rtt: 103, rto: 693index: 55, rtt: 105, rto: 651index: 56, rtt: 102, rto: 625index: 57, rtt: 100, rto: 599index: 58, rtt: 102, rto: 566index: 59, rtt: 105, rto: 538index: 60, rtt: 100, rto: 526index: 61, rtt: 103, rto: 503index: 62, rtt: 105, rto: 485index: 63, rtt: 104, rto: 479index: 64, rtt: 100, rto: 473index: 65, rtt: 103, rto: 459index: 66, rtt: 105, rto: 446

即使和經典演算法一樣的rtt樣本,但卻沒有出現任何一個丟包,證明其抗網路抖動性是比較強的,但這裡也有個問題,就是算出來的rto值比真實rtt值大很多,如果網路中出現真實丟包,那重傳將會慢很多,導致速度很慢,我想在Linux中應該不是完全按照這個演算法來計算rto值的,而且上面rfc文檔說當rto小於1s時,應該讓rto等於1s,這個應該也說不過去啊,rto等於1s,會不會有點大了,這裡也沒有說結合快速重傳等,那隻靠rto逾時重傳速度會慢死。

RTO重傳間隔是指數增加的

上面我們介紹的是初次重傳時的RTO,如果重傳後還沒收到另一端的響應,下一次重傳RTO則會指數增加,例如第一次重傳RTO是1,之後分別2,4,8,16…。這種行為叫做指數迴避策略,所以對於tcp來說,當丟包率高時,有可能一個包要很久才能穿過去。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.