OpenWrt 测速跑不满 2000M:TCP 接收窗口调优记录
适用环境:OpenWrt / iStoreOS 24.10,CPU 为 Intel N100,8 GB DDR4 内存,光猫桥接后由软路由拨号
网络环境:中国电信 2000M 宽带
问题现象:Speedtest 多线程测速正常,但单连接 iperf3 只能跑到约 400 Mbps
问题现象
先用 Speedtest 测试上海电信节点,下载速度可以达到约 2310 Mbps:
/root/speedtest -s 3633 --accept-license --accept-gdpr Speedtest by Ookla Server: China Telecom - Shanghai (id: 3633) ISP: China TelecomIdle Latency: 10.34 ms (jitter: 0.77ms, low: 9.97ms, high: 11.54ms) Download: 2310.15 Mbps (data used: 2.4 GB) 16.67 ms (jitter: 0.88ms, low: 7.57ms, high: 20.32ms) Upload: 113.22 Mbps (data used: 156.8 MB) 23.41 ms (jitter: 35.45ms, low: 7.29ms, high: 245.11ms) Packet Loss: Not available. Result URL: https://www.speedtest.net/result/c/0a7a3a50-92af-46bd-9388-b0722753bc3f随后使用 iperf3 连接阿里云抢占式 CDT 实例进行反向下载测试。该链路 RTT 约为 47 ms,单连接速度只有约 400 Mbps:
iperf3 -c ali-hk-cdt.xxx.xxx -p 5205 -RConnecting to host ali-hk-cdt.003110.xyz, port 5205Reverse mode, remote host ali-hk-cdt.003110.xyz is sending[ 5] local 106.111.244.145 port 42826 connected to 8.210.203.196 port 5205[ ID] Interval Transfer Bitrate[ 5] 0.00-1.00 sec 27.6 MBytes 231 Mbits/sec[ 5] 1.00-2.00 sec 46.4 MBytes 389 Mbits/sec[ 5] 2.00-3.00 sec 46.1 MBytes 387 Mbits/sec[ 5] 3.00-4.00 sec 47.4 MBytes 397 Mbits/sec[ 5] 4.00-5.00 sec 48.9 MBytes 410 Mbits/sec[ 5] 5.00-6.00 sec 46.0 MBytes 386 Mbits/sec[ 5] 6.00-7.00 sec 45.2 MBytes 380 Mbits/sec[ 5] 7.00-8.00 sec 46.8 MBytes 392 Mbits/sec[ 5] 8.00-9.00 sec 46.1 MBytes 387 Mbits/sec[ 5] 9.00-10.00 sec 48.6 MBytes 408 Mbits/sec- - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval Transfer Bitrate Retr[ 5] 0.00-10.06 sec 473 MBytes 394 Mbits/sec 0 sender[ 5] 0.00-10.00 sec 449 MBytes 377 Mbits/sec receiver原因分析
路由器当前的接收缓冲区参数如下:
net.core.rmem_max = 7500000net.ipv4.tcp_rmem = 4096 131072 6291456| 参数 | 当前最大值 | 说明 |
|---|---|---|
net.core.rmem_max | 7,500,000 字节(约 7.5 MB) | 套接字接收缓冲区的上限 |
net.ipv4.tcp_rmem | 6,291,456 字节(约 6 MB) | TCP 接收缓冲区自动调节的最大值 |
在当前环境下,可用接收缓冲区约为 6–7.5 MB。根据带宽时延积(BDP), 单连接跑满 2 Gbps 所需的理论接收窗口为:
2 Gbps × 47 ms ÷ 8 ≈ 11.75 MB当前约 6–7.5 MB 的缓冲区上限理论上只能支持约 1.0–1.3 Gbps。 实际测速还会受到 RTT 波动、自动调节和链路条件影响,因此单连接速度只有约 400 Mbps。Speedtest 能跑到 2300 Mbps,是因为它使用了大量并行 HTTP 连接, 不会受单连接窗口大小的限制。
调整接收窗口
先临时调大 Socket 和 TCP 的接收缓冲区上限:
sysctl -w net.core.rmem_max=33554432sysctl -w net.ipv4.tcp_rmem='4096 131072 33554432'确认参数是否生效:
sysctl net.core.rmem_maxsysctl net.ipv4.tcp_rmem复测
使用相同参数重新执行 iperf3:
iperf3 -c ali-hk-cdt.xxx.xxx -p 5205 -RConnecting to host ali-hk-cdt.003110.xyz, port 5205Reverse mode, remote host ali-hk-cdt.003110.xyz is sending[ 5] local 106.111.244.145 port 55722 connected to 8.210.203.196 port 5205[ ID] Interval Transfer Bitrate[ 5] 0.00-1.00 sec 163 MBytes 1.36 Gbits/sec[ 5] 1.00-2.00 sec 279 MBytes 2.34 Gbits/sec[ 5] 2.00-3.00 sec 279 MBytes 2.34 Gbits/sec[ 5] 3.00-4.00 sec 236 MBytes 1.98 Gbits/sec[ 5] 4.00-5.00 sec 279 MBytes 2.34 Gbits/sec[ 5] 5.00-6.00 sec 279 MBytes 2.34 Gbits/sec[ 5] 6.00-7.00 sec 260 MBytes 2.18 Gbits/sec[ 5] 7.00-8.00 sec 279 MBytes 2.34 Gbits/sec^C[ 5] 8.00-8.55 sec 153 MBytes 2.34 Gbits/sec- - - - - - - - - - - - - - - - - - - - - - - - -[ ID] Interval Transfer Bitrate[ 5] 0.00-8.55 sec 0.00 Bytes 0.00 bits/sec sender[ 5] 0.00-8.55 sec 2.16 GBytes 2.17 Gbits/sec receiver调整前后的单连接测速结果如下:
| 测试阶段 | 接收端速度 | 结果 |
|---|---|---|
| 调整前 | 377 Mbits/sec | 明显低于 2000M 带宽 |
| 调整后 | 2.17 Gbits/sec | 单连接可以跑满 2000M |
重启后持久化
sysctl -w 的修改只在当前运行期间有效。需要重启后继续生效时,创建配置文件:
cat > /etc/sysctl.d/99-tcp-window.conf <<'EOF'net.core.rmem_max = 33554432net.ipv4.tcp_rmem = 4096 131072 33554432EOF立即加载新配置:
sysctl -p /etc/sysctl.d/99-tcp-window.conf结论
高带宽、高延迟链路中的单 TCP 连接速度会受到接收窗口大小限制。将
net.core.rmem_max 和 net.ipv4.tcp_rmem 的最大值从约 6–7.5 MB 提高到
32 MB 后,单连接 iperf3 速度由约 377 Mbps 提升至约 2.17 Gbps。
Speedtest 的多连接结果只能说明线路总带宽充足,不能单独用于判断单连接性能。 排查类似问题时,应结合链路的 RTT、单连接 iperf3 结果和当前 TCP 缓冲区参数进行分析。