tcp拥塞算法分析二(bic) 您所在的位置:网站首页 tcp拥塞算法哪个好 tcp拥塞算法分析二(bic)

tcp拥塞算法分析二(bic)

2024-07-16 15:32| 来源: 网络整理| 查看: 265

本文分析linux-4.14.69代码的bic拥塞算法

首先回顾下基础的慢启动和拥塞避免函数,慢启动阶段(tcp_slow_start)更新窗口的速度是加acked,acked就是这个ack包对应确认的包个数;拥塞避免阶段(tcp_cong_avoid_ai)更新窗口的速度是加acked/w, w一般情况下就是当前的窗口大小.(在bic中,相当与一个权重,bic会根据当前窗口到最大窗口的差距动态设置这个w.)

u32 tcp_slow_start(struct tcp_sock *tp, u32 acked) { u32 cwnd = min(tp->snd_cwnd + acked, tp->snd_ssthresh); acked -= cwnd - tp->snd_cwnd; tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp); return acked; } EXPORT_SYMBOL_GPL(tcp_slow_start); void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked) { /* If credits accumulated at a higher w, apply them gently now. */ if (tp->snd_cwnd_cnt >= w) { tp->snd_cwnd_cnt = 0; tp->snd_cwnd++; } tp->snd_cwnd_cnt += acked; if (tp->snd_cwnd_cnt >= w) { u32 delta = tp->snd_cwnd_cnt / w; tp->snd_cwnd_cnt -= delta * w; tp->snd_cwnd += delta; } tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_cwnd_clamp); } EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);

再看看bic的拥塞避免函数.

static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked) { struct tcp_sock *tp = tcp_sk(sk); struct bictcp *ca = inet_csk_ca(sk); if (!tcp_is_cwnd_limited(sk)) return; if (tcp_in_slow_start(tp)) tcp_slow_start(tp, acked); else { bictcp_update(ca, tp->snd_cwnd); tcp_cong_avoid_ai(tp, ca->cnt, 1); } }

慢启动就是调用默认的慢启动函数, 拥塞避免阶段则多了一个更新窗口的处理,并将tcp_cong_avoid_ai的第三个参数acked设置固定值1.更新函数就是在更新ca->cnt, 从而控制了拥塞避免阶段增长速度。其中涉及到很多变量:

/* BIC TCP Parameters */ struct bictcp { u32 cnt; /* increase cwnd by 1 after ACKs */ u32 last_max_cwnd; /* last maximum snd_cwnd */ 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 last_cwnd == cwnd && (s32)(tcp_jiffies32 - ca->last_time) cnt设置为当前发送窗口,即普通拥塞避免模式.

ca->last_cwnd = cwnd; ca->last_time = tcp_jiffies32; if (ca->epoch_start == 0) /* record the beginning of an epoch */ ca->epoch_start = tcp_jiffies32;

3.二分查找.         3.1 cwnd < last_max_cwnd             3.1.1当max_cwnd - cwnd大于BICTCP_B*max_increment, cnt =cwnd/max_increment。离最大窗口很远的时候,快速增长,一个rtt增max_increment个窗口             3.1.2当max_cwnd - cwnd小于BICTCP_B, cnt = cwnd *smooth_part / BICTCP_B,一个rtt增(BICTCP_B/smooth_part )个窗口. 接近最大窗口时候非常缓慢的增长,大约经过smooth_part个rtt才增大到最大窗口。             3.1.3当max_cwnd - cwnd在[BICTCP_B, BICTCP_B*max_increment], cnt = cwnd / (max_cwnd - cwnd),一个rtt增max_cwnd-cwnd个窗口,也就是一个rtt达到最大窗口.        3.2 cwnd >= last_max_cwnd              3.2.1当cwnd - max_cwnd小于BICTCP_B,  超过的不多,cnt = cwnd *smooth_part / BICTCP_B,缓慢长,同3.1.2,一个rtt增(BICTCP_B/smooth_part )个窗口              3.2.2当cwnd - max_cwnd在[BICTCP_B, max_increment*(BICTCP_B-1)], cnt = (cwnd * (BICTCP_B-1)) / (cwnd-last_max_cwnd); 一个rtt增大(cwnd-last_max_cwnd)/(BICTCP_B-1)个窗口.速度会越来越快,因为cwnd不断增大.根据if条件范围可以算出1个rtt增加的窗口范围在[BICTCP_B/(BICTCP_B-1), max_increment]              3.2.3当cwnd - max_cwnd大于max_increment,cnt = cwnd / max_increment; ,同3.1.1,一个rtt增max_increment个窗口

/* binary increase */ if (cwnd < ca->last_max_cwnd) { __u32 dist = (ca->last_max_cwnd - cwnd) / BICTCP_B; if (dist > max_increment) /* linear increase */ ca->cnt = cwnd / max_increment; else if (dist cnt = (cwnd * smooth_part) / BICTCP_B; else /* binary search increase */ ca->cnt = cwnd / dist; } else { /* slow start AMD linear increase */ if (cwnd < ca->last_max_cwnd + BICTCP_B) /* slow start */ ca->cnt = (cwnd * smooth_part) / BICTCP_B; else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1)) /* slow start */ ca->cnt = (cwnd * (BICTCP_B-1)) / (cwnd - ca->last_max_cwnd); else /* linear increase */ ca->cnt = cwnd / max_increment; }

4. 当last_max_cwnd == 0。即在慢启动开始或出现拥塞的时候,控制cnt不超过20, 即一个rtt至少增cwnd/20个窗口

/* if in slow start or link utilization is very low */ if (ca->last_max_cwnd == 0) { if (ca->cnt > 20) /* increase cwnd 5% per RTT */ ca->cnt = 20; }

5.对延迟确认的处理。延迟确认的时候,一个ack不止是确认一个报文,作者的意思是,根据延迟ack的比例1cnt = 1;

 ACK_RATIO_SHIFT为常数4, delayed_ack的设置涉及到bictcp_reset函数和bictcp_acked函数. 

bictcp_reset对bictcp结构体做初始化,可以看到初始化的时候delayed_ack设置为32,即默认延迟比例为50%。 bictcp_reset在两种情况下被调用: 初始化时(bictcp_init )、进入拥塞处理时(bictcp_state 状态为TCP_CA_Loss)

static inline void bictcp_reset(struct bictcp *ca) { ca->cnt = 0; ca->last_max_cwnd = 0; ca->last_cwnd = 0; ca->last_time = 0; ca->epoch_start = 0; ca->delayed_ack = 2 icsk_ca_state == TCP_CA_Open) { struct bictcp *ca = inet_csk_ca(sk); ca->delayed_ack += sample->pkts_acked - (ca->delayed_ack >> ACK_RATIO_SHIFT); } }

最后要关注的就是bic算法对ssthresh的计算了。 每收到一个ack,就会调用tcp_ack(这个函数有点复杂后面慢慢看)。tcp_ack会调用.pkts_acked和.cong_avoid,pkts_acked对应bictcp_acked, cong_avoid对应bictcp_cong_avoid。   tcp_ack中如果检测到丢包,则进入拥塞阶段,调用.ssthresh,对应bic的bictcp_recalc_ssthresh函数,tcp_ack完成重传后,退回到拥塞阶段,调用.undo_cwnd函数,即tcp_reno_undo_cwnd。

bictcp_recalc_ssthresh用于拥塞后计算慢启动阈值ssthresh。 里面有几个参数要注意下 #define BICTCP_BETA_SCALE    1024    /* Scale factor beta calculation static int beta = 819;        /* = 819/1024 (BICTCP_BETA_SCALE) */ 1.设置last_max_cwnd     当 snd_cwnd < last_max_cwnd && fast_convergence==1. last_max_cwnd=snd_cwnd*(819+1024)/(2*1024)=0.9*snd_cwnd      当 snd_cwnd >= last_max_cwnd . last_max_cwnd=snd_cwnd 2.设置snd_ssthresh      当snd_cwnd low_window. snd_ssthresh = max(snd_cwnd*beta/BICTCP_BETA_SCALE, 2)=max(snd_cwnd*0.8, 2)

/* * behave like Reno until low_window is reached, * then increase congestion window slowly */ static u32 bictcp_recalc_ssthresh(struct sock *sk) { const struct tcp_sock *tp = tcp_sk(sk); struct bictcp *ca = inet_csk_ca(sk); ca->epoch_start = 0; /* end of epoch */ /* Wmax and fast convergence */ if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence) ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta)) / (2 * BICTCP_BETA_SCALE); else ca->last_max_cwnd = tp->snd_cwnd; if (tp->snd_cwnd snd_cwnd >> 1U, 2U); else return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U); }

要深入了解需要了解收到ack后的相关处理细节

           

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有