so_brave 发表于 2011-12-14 20:16

TCP拥塞控制算法内核实现剖析(二)

TCP拥塞控制算法内核实现剖析(二)












内核版本:2.6.37

主要源文件:linux-2.6.37/ net/ ipv4/ tcp_bic.c



======================================================================================================/* BIC TCP Parameters */

struct bictcp {

      u32 cnt ; /* increase cwnd by 1 after ACKs */

      u32 last_max_cwnd ; /* last maximum snd_cwnd */

      u32 loss_cwnd ; /* congestion window at last loss */

      u32 last_cwnd ; /* the last snd_cwnd */

      u32 last_time ; /* time when updated last_cwnd */

      u32 epoch_start ; /* beginning of an epoch */

#define ACK_RATIO_SHIFT 4

      u32 delayed_ack ; /* estimate the ratio of Packets/ACKs << 4 */

} ;



/* Scale factor beta calculation

* max_cwnd = snd_cwnd * beta

*/

#define BICTCP_BETA_SCALE 1024



/* In binary search ,

* go to point (max+min) / N

*/

#define BICTCP_B 4   /*并不是真正的二分*/全局变量static int fast_convergence = 1 ; /* BIC能快速的达到一个平衡值,开关*/

static int max_increment = 16 ; /* 每次增加的MSS 不能超过这个值,防止增长太过剧烈*/

static int low_window = 14 ; /* lower bound on congestion window , for TCP friendliness */

static int beta = 819 ; /* = 819 / 1024(BICTCP_BETA_SCALE) ,beta for multiplicative increase 。?*/

static int initial_ssthresh ; /* 初始的阈值 */

static int smooth_part = 20 ; /* log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax 。?*/

/* initial_ssthresh的初始值被设置成2^31-1=2147483647 */==========================================================================================================struct inet_connection_sock {

      ...

      u32 icsk_ca_priv ;

#define ICSK_CA_PRIV_SIZE (16*sizeof(u32))

}



static inline void *inet_csk_ca( const struct sock *sk )

{

      return (void *)inet_csk(sk)->icsk_ca_priv ;

}============================================================================================================

不明白?!
/* Slow start with delack produces 3 packets of burst , so that it is safe "de facto". This will be

* default - same as the default reordering threshold - but if reordering increases , we must

* be able to allow cwnd to burst at least this much in order to not pull it back when holes

* are filled.

*/

static __inline__ __u32 tcp_max_burst ( const struct tcp_sock *sk )

{

      return tp->reordering ;

}

/* u8 reordering ; Packets reordering metric */



/* RFC2681 Check whether we are limited by application or congestion window

* This is the inverse of cwnd check in tcp_tso_should_defer

*/

/* 返回0,不需要增加cwnd ; 返回1,cwnd被限制,需要增加 */

int tcp_is_cwnd_limited ( const struct sock *sk , u32 in_flight )

{

      const struct tcp_sock *tp = tcp_sk(sk) ;

      u32 left ;

      if( in_flight >= tp->snd_cwnd ) /* 不是规定in_flight < snd_cwnd ? */

                return 1 ;

      left = tp->snd_cwnd - in_flight ;

      if( sk_can_gso(sk) &&

                left * sysctl_tcp_tso_win_divisor < tp->snd_cwnd &&

                left * tp->mss_cache < sk->sk_gso_max_size )

                return 1 ;

      return left <= tcp_max_busrt( tp ) ;

}=============================================================================================================static void bictcp_cong_avoid ( struct sock *sk , u32 ack , u32 in_flight )

{

      struct tcp_sock *tp = tcp_sk(sk) ;

      struct bictcp *ca = inet_csk_ca(sk) ;

      /* 如果发送拥塞窗口不被限制,不能再增加,则返回 */

      if( !tcp_is_cwnd_limited(sk , in_flight))

                return ;

      if( tp->snd_cwnd < tp->snd_ssthresh )

                tcp_slow_start( tp ) ;

      else {

                bictcp_update(ca , tp->snd_cwnd ) ;

                tcp_cong_avoid_ai( tp , ca->cnt ) ;

      }

}从以上函数可以看出,BIC的慢启动和reno相同。在拥塞避免阶段,当snd_cwnd <= low_window ,两者也采用相同方法。

只有当snd_cwnd > low时,BIC才开始显示出它的特性。



在include/ net / tcp.h中,/* TCP timestamps are only 32-bits */

#define tcp_time_stamps ((__u32)(jiffies))



/*

* Compute congestion window to use.

*/

static inline void bictcp_update( struct bictcp *ca , u32 cwnd )

{

      if ( ca->last_cwnd == cwnd &&

                (s32) ( tcp_time_stamp - ca->last_time) <= HZ / 32 )/* 31.25ms以内不更新ca!!!*/

      return ;

      ca->last_cwnd = cwnd ;

      ca->last_time = tcp_time_stamp ;

      if ( ca->epoch_start == 0 ) /* recording the beginning of an epoch */

                ca->epoch_start = tcp_time_stamp ;

      /* start off normal */

      if( cwnd <= low_window ) {/*为了保持友好性*/

                ca->cnt = cwnd ;/*这样16个以内的ack,可使snd_cwnd++ */

                return ;

      }

      /* binary increase */

      if ( cwnd < ca->last_max_cwnd ) {/*上次掉包前一个snd_cwnd */

                __u32 dist = (ca->last_max_cwnd - cwnd) / BICTCP_B ; /* 四分之一 */

                if ( dist > max_increment ) /* linear increase */

                        /*dist > 16,处于线性增长阶段,每收到16个ACK,会使snd_cwnd++ */

                        ca->cnt = cwnd / max_increment ;

                else if ( dist <= 1U ) /* binary search increase */

                        /* dist <=1 , ca->cnt=5*cwnd,会造成snd_cwnd增长极其缓慢,即处于稳定阶段 */

                        ca->cnt = (cwnd * smooth_part ) / BICTCP_B ;

                else /* binary search increase */

                        /* 1 < dist <= 16 ,每收到dist个ACK,会使snd_cwnd++,故增长很快 */

                        ca->cnt = cwnd / dist ;

      } else { /* 进入max_probing阶段 */

                if ( cwnd < ca->last_max_cwnd + BICTCP_B ) /* cwnd < ca->last_max_cwnd + 4 */

                        ca->cnt = (cwnd * smooth_part ) / BICTCP_B ; /* ca->cnt = 5*cwnd ; slow start */

                else if ( cwnd < ca->last_max_cwnd + max_increment * ( BICTCP_B - 1))

                        /* 增长率从5/(3*cwnd)~47/(3*cwnd),snd_cwnd的增长加快*/

                        ca->cnt = (cwnd * (BICTCP_B - 1)) / (cwnd - ca->last_max_cwnd) ;

                else

                        ca->cnt = cwnd / max_increment ; /* 增长率为16/cwnd ,更快 */

      }

      /* if in slow start or link utilization is very low */

      if ( ca->loss_cwnd == 0 ) {/* 没有发生过丢包,所以snd_cwnd增长应该快点*/

                if ( ca->cnt > 20 ) /* increase cwnd 5% per RTT */

                      ca->cnt = 20 ;

      }

      /* 相当于乘与delayed_ack的百分比,delayed得越严重,则snd_cwnd应该增加越快*/

      /* 这样有无delayed对snd_cwnd的影响不大*/

      ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack ;

      /* ca->cnt cannot be zero */

      if ( ca->cnt == 0)

                ca->cnt = 1 ;

}从以上函数可以看出,和reno相比,BIC在拥塞避免阶段snd_cwnd增长极快。

当ca->last_max_cwnd - snd_cwnd >= 4 时,snd_cwnd最慢的增长率为 1/16 。

而当ca->last_max_cwnd - snd_cwnd <4 时,增长率非常低,可以使当前的snd_cwnd维持很长一段时间,

即以最合适的snd_cwnd发送数据。

这两点使BIC在高带宽、长时延的环境下能达到较高的吞吐量。



1. 搜索阶段(1) cwnd < last_max_cwnd - 64, 则cnt = cwnd / 16

(2) last_max_cwnd - 64 <= cwnd < last_max_cwnd -4 ,则cnt = cwnd / dist

(3) last_max_cwnd - 4 <= cwnd < last_max_cwnd ,则cnt = 5*cwnd总体来说,snd_cwnd增长先快后慢,趋于稳定。

2. max probing阶段
(1) last_max_cwnd <= cwnd < last_max_cwnd + 4,则cnt = 5*cwnd

(2) last_max_cwnd + 4 <= cwnd < last_max_cwnd + 48 ,则cnt = 3*cwnd / (cwnd - last_max_cwnd)

(3) cwnd >= last_max_cwnd + 48 ,则cnt = cwnd / 16总体来说,snd_cwnd的增长先慢后快,越来越快。

健康木乃伊 发表于 2011-12-20 14:56

很好阿希望于楼主多多交流哦
页: [1]
查看完整版本: TCP拥塞控制算法内核实现剖析(二)