bbr

bbr

bbr 是 Google 开发的 TCP 阻塞控制算法。仅就速度而言(不管公平性),甩了 Linux 默认的 cubic 十条街。

Linux 4.9+ 以上版本内核默认已经集成了 bbr。在内核源代码里的位置是 net/ipv4/tcp_bbr.c

Install bbr

https://gist.github.com/xterat/cefccb42f7d4b2055368ebb00454861f

Ubuntu 18.04

(备注:Oracle Cloud 等 UEFI 启动的 VPS 请额外安装 grub-efi 这个包: apt install --install-recommends linux-generic-hwe-18.04 grub-efi)

apt install --install-recommends linux-generic-hwe-18.04
modprobe tcp_bbr
echo "tcp_bbr" >> /etc/modules-load.d/modules.conf
echo "net.core.default_qdisc=cake" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p

检测当前内核是否支持bbr

sysctl net.ipv4.tcp_available_congestion_control
sysctl net.ipv4.tcp_congestion_control

部分系统bbr以内核模块形式安装,需要手动启用。

modprobe tcp_bbr
echo "tcp_bbr" | tee --append /etc/modules-load.d/modules.conf

注:测试 Ubuntu 22.04 启用 bbr 无需手动加载内核模块。

启用 bbr

修改 /etc/sysctl.conf:

# default_qdisc 指的是默认的 TCP 队列算法
# 推荐使用 cake (4.19+ 内核内置)。旧版内核可以使用 fq (Google 推荐的算法)。

net.core.default_qdisc=cake
net.ipv4.tcp_congestion_control=bbr

直接写入配置到文件:

echo "net.core.default_qdisc=cake" | tee --append /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | tee --append /etc/sysctl.conf

sysctl -p 使修改立即生效。

魔改

网上有很多魔改的 bbr,比如 bbrplus。主要是修改了原版 bbr (net/ipv4/tcp_bbr.c) 里的一些常量值:

原版 bbr:

static const u32 bbr_probe_rtt_mode_ms = 200;
static const int bbr_high_gain  = BBR_UNIT * 2885 / 1000 + 1;
static const int bbr_drain_gain = BBR_UNIT * 1000 / 2885;
static const u32 bbr_lt_bw_ratio = BBR_UNIT / 8;
static const u32 bbr_lt_bw_diff = 4000 / 8;

static const int bbr_pacing_gain[] = {
    BBR_UNIT * 5 / 4,   /* probe for more available bw */
    BBR_UNIT * 3 / 4,   /* drain queue and/or yield bw to other flows */
    BBR_UNIT, BBR_UNIT, BBR_UNIT,   /* cruise at 1.0*bw to utilize pipe, */
    BBR_UNIT, BBR_UNIT, BBR_UNIT    /* without creating excess queue... */
};

tcp_nanqinlang (魔改版 bbr):

static const u32 bbr_probe_rtt_mode_ms = 100;
static const int bbr_high_gain  = BBR_UNIT * 3000 / 1000 + 1;
static const int bbr_drain_gain = BBR_UNIT * 1000 / 3000;
static const u32 bbr_lt_bw_ratio = BBR_UNIT / 4;
static const u32 bbr_lt_bw_diff = 4000 / 4;

static const int bbr_pacing_gain[] = {
    BBR_UNIT * 6 / 4,   /* probe for more available bw */
    BBR_UNIT * 3 / 4,   /* drain queue and/or yield bw to other flows */
    BBR_UNIT * 5 / 4, BBR_UNIT * 5 / 4, BBR_UNIT * 5 / 4,   /* cruise at 1.0*bw to utilize pipe, */
    BBR_UNIT * 6 / 4, BBR_UNIT * 6 / 4, BBR_UNIT * 6 / 4    /* without creating excess queue... */
};

编译魔改版的 bbr 可以参考:编译 Linux 内核模块


Last update: 2023-03-09 06:28:16 UTC