4.9.4 网络吞吐与延迟优化(MTU、拥塞控制、队列)
本节聚焦链路层到传输层的协同调优,通过 MTU、拥塞控制与队列管理提升吞吐并降低延迟。以下给出原理草图、可执行示例、安装与排错流程。
1. 工具安装与基线准备#
# Debian/Ubuntu
sudo apt update
sudo apt install -y iproute2 ethtool iperf3 tcpdump
# RHEL/CentOS
sudo yum install -y iproute ethtool iperf3 tcpdump
iperf3:吞吐压测ethtool:网卡能力/统计/环形缓冲ss/tc:连接与队列管理
基线测量:
# 服务端
iperf3 -s
# 客户端(10秒、4并发)
iperf3 -c 10.0.0.2 -t 10 -P 4
预期:记录吞吐(Gbps/Mbps)与重传数,作为调优前基线。
2. MTU 优化(避免分片与黑洞)#
检查当前 MTU:
ip link show dev eth0
# 关键字段: mtu 1500
路径 MTU 探测(禁止分片):
# 1472 = 1500 - 28(IP+ICMP)
ping -M do -s 1472 10.0.0.2
# 若失败:提示 "Frag needed"
设置 MTU(全链路一致后再改):
sudo ip link set dev eth0 mtu 9000
ip link show dev eth0 | grep mtu
排错提示:
- 只改单端会导致连接抖动或黑洞。
- 交换机/虚拟化网络不支持 9000 时应回退到 1500。
3. TCP 拥塞控制与窗口#
查看当前算法:
sysctl net.ipv4.tcp_congestion_control
# 输出示例: net.ipv4.tcp_congestion_control = cubic
启用 BBR(需内核支持):
sudo modprobe tcp_bbr
sudo sysctl -w net.ipv4.tcp_congestion_control=bbr
sudo sysctl -w net.core.default_qdisc=fq
写入持久配置:
sudo tee /etc/sysctl.d/99-net-tuning.conf <<'EOF'
net.ipv4.tcp_congestion_control=bbr
net.core.default_qdisc=fq
net.core.rmem_max=268435456
net.core.wmem_max=268435456
net.ipv4.tcp_rmem=4096 87380 134217728
net.ipv4.tcp_wmem=4096 65536 134217728
EOF
sudo sysctl --system
验证效果:
ss -ti dst 10.0.0.2 | head -n 3
# 关注: cubic/bbr, cwnd, rtt, rto
4. 队列管理(降低排队延迟)#
查看当前 qdisc:
tc qdisc show dev eth0
设置 fq_codel(通用低延迟):
sudo tc qdisc replace dev eth0 root fq_codel
tc -s qdisc show dev eth0
高带宽低延迟链路可尝试 cake:
sudo tc qdisc replace dev eth0 root cake bandwidth 1gbit
5. 网卡 Ring 与多队列#
查看/调整环形缓冲:
sudo ethtool -g eth0
# 关注: RX/TX 当前与最大
# 调大 RX/TX ring(示例值需<=最大值)
sudo ethtool -G eth0 rx 2048 tx 2048
多队列与中断分散:
# 查看队列数
ethtool -l eth0
# 开启 irqbalance(自动均衡中断)
sudo systemctl enable --now irqbalance
6. 典型问题排错流程#
1) 丢包/重传
ss -s
nstat -az | egrep "TcpRetransSegs|TcpInSegs|TcpOutSegs"
ethtool -S eth0 | egrep "drop|err|miss"
- 若
TcpRetransSegs飙升:检查 MTU、交换机拥塞、队列过深。
2) 抖动/高延迟
tc -s qdisc show dev eth0
ping -i 0.2 10.0.0.2
- 若 qdisc backlog 高:切换 fq_codel/cake,或降低 ring。
3) 路径 MTU 黑洞
ping -M do -s 1472 10.0.0.2
# 逐步减小 -s,找到可通的最大值
7. 练习#
1) 在测试环境中将 MTU 从 1500 改为 9000,使用 ping -M do 验证;再回退到 1500 并记录差异。
2) 对同一链路分别使用 cubic 与 bbr,用 iperf3 -P 4 对比吞吐与重传数。
3) 将 qdisc 从 pfifo_fast 切换到 fq_codel,观察 tc -s 统计与 ping 延迟变化。