Files
MeshPort/outputs/validation/subnet-tcp-buffers-20261015.md
T
shenandshen b656c1896a fix: improve SDK UDP throughput and transport selection
Yield during UDP receive bursts to let session consumers drain bounded queues. Prioritize measured RTT over protocol preference and enlarge user-space subnet TCP buffers with validated limits.

Add packet-level throughput diagnostics, transport selection coverage, and investigation records. SDK regression suite: 21 tests passed; local delay and throughput experiments documented.
2026-10-15 23:53:10 -07:00

96 lines
6.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 用户态子网 TCP 缓冲优化,2026-10-15
## 最终修改
`gateway/smoltcp/stack.rs` 由收发各 16 KiB 调整为接收 1 MiB、发送 128 KiB。参考 SDK 数据面扩大接收窗口,但没有照搬其 512 KiB 发送缓冲:后者在本地短下载中出现明显回退。SDK 数据面自身的 1 MiB / 512 KiB 参数保持不变。
作用范围是运行修改后代码的用户态子网网关。仅更新本机 MeshPort,不会改变远端原版 EasyTier 网关的缓冲;内核子网转发路径也不受这段修改影响。本轮未部署到远端、未打包或替换已安装应用、未提交。原有工作区修改保留。
## 测试结论
相同加密 EasyTier UDP 本地拓扑、附加 58 ms RTT、每方向 8 MiB:
| 参数 | 下载 Mbps | 上传 Mbps |
|---|---:|---:|
| 原收发各 16 KiB | 2.06 | 2.03 |
| 接收 1 MiB / 发送 512 KiB,第 1 轮 | 2.46 | 9.81 |
| 接收 1 MiB / 发送 512 KiB,第 2 轮 | 10.25 | 8.60 |
| 最终接收 1 MiB / 发送 128 KiB,第 1 轮 | 16.03 | 8.39 |
| 最终接收 1 MiB / 发送 128 KiB,第 2 轮 | 16.19 | 5.55 |
最终参数的下载在两轮样本中约为原参数的 7.8 倍;上传改善但仍有波动,不能承诺稳定倍率。2 MiB 三入口对照中,最终参数在 58 ms 下下载 SDK 16.30、SOCKS 15.76、sing-box 15.81 Mbps,接近相同量级。
无附加延迟的 2 MiB 下载中,512 KiB 发送缓冲出现约 16 Mbps 样本,低于原子网约 43 Mbps 基线;最终 128 KiB 发送缓冲两轮三入口样本均为 226–258 Mbps。这是选择较小发送缓冲的实测依据,尚未对回退采集 TCP 重传或 UDP 丢包,不能断言其底层原因,也不能保证所有网络均无回退。
这些是本地隔离短传输结果,不是现场公网、系统 TUN 或稳态 iperf 结果。远端实际转发模式尚未确认,不能据此声称解决现场唯一根因。
## 内存代价
子网每个 TCP socket 的收发缓冲从 32 KiB 增为 1.125 MiB,增加 1.09375 MiB;100 个 socket 的缓冲合计约 112.5 MiB,不含对象和其他队列。缓冲在分配 socket 时建立,尚在握手中的连接也占用;当前单监听 pending 上限 16,16 个 pending 的缓冲约 18 MiB。没有修改 backlog、超时、MTU、拥塞控制或全局默认 socket 缓冲。
## 复现
```sh
MESHPORT_TEST_TARGET=subnet MESHPORT_TEST_ENTRIES=sdk \
MESHPORT_TEST_DELAY_MS=29 MESHPORT_TEST_MIB=8 \
sh scripts/cargo-local.sh test -p meshport-socks --release --locked \
--test bridge subnet_throughput_diagnostic -- --ignored --nocapture
```
测试逐字节校验下载/上传内容,每次新建连接。三入口测试设置 `MESHPORT_TEST_ENTRIES=sdk,socks,singbox` 和 `MESHPORT_SINGBOX_BIN="$PWD/target/debug/meshport-singbox"`;0 ms 基线将延迟参数设为 0。旧参数与最终参数使用相同诊断代码;旧参数基线在更改源文件前已启动,之后的源码修改不会改变该运行中的二进制。所有性能轮次串行运行。
## 完整性能样本
| 轮次 | 入口 | 附加 RTT ms | 每方向 MiB | 方向 | Mbps |
|---|---|---:|---:|---|---:|
| 原参数 16/16 KiB | sdk | 58 | 8 | download | 2.06 |
| 原参数 16/16 KiB | sdk | 58 | 8 | upload | 2.03 |
| 接收 1 MiB / 发送 512 KiB | sdk | 58 | 8 | download | 2.46 |
| 接收 1 MiB / 发送 512 KiB | sdk | 58 | 8 | upload | 9.81 |
| 接收 1 MiB / 发送 512 KiB,重复 | sdk | 58 | 8 | download | 10.25 |
| 接收 1 MiB / 发送 512 KiB,重复 | sdk | 58 | 8 | upload | 8.60 |
| 512 KiB 发送,0 ms | sdk | 0 | 2 | download | 887.28 |
| 512 KiB 发送,0 ms | sdk | 0 | 2 | upload | 744.98 |
| 512 KiB 发送,0 ms | socks | 0 | 2 | download | 1001.04 |
| 512 KiB 发送,0 ms | socks | 0 | 2 | upload | 755.04 |
| 512 KiB 发送,0 ms | singbox | 0 | 2 | download | 15.95 |
| 512 KiB 发送,0 ms | singbox | 0 | 2 | upload | 574.83 |
| 512 KiB 发送,0 ms 反序重复 | singbox | 0 | 2 | download | 16.25 |
| 512 KiB 发送,0 ms 反序重复 | singbox | 0 | 2 | upload | 492.73 |
| 512 KiB 发送,0 ms 反序重复 | socks | 0 | 2 | download | 757.99 |
| 512 KiB 发送,0 ms 反序重复 | socks | 0 | 2 | upload | 637.07 |
| 512 KiB 发送,0 ms 反序重复 | sdk | 0 | 2 | download | 16.27 |
| 512 KiB 发送,0 ms 反序重复 | sdk | 0 | 2 | upload | 409.34 |
| 512 KiB 发送,三入口 | sdk | 58 | 2 | download | 11.43 |
| 512 KiB 发送,三入口 | sdk | 58 | 2 | upload | 4.66 |
| 512 KiB 发送,三入口 | socks | 58 | 2 | download | 4.75 |
| 512 KiB 发送,三入口 | socks | 58 | 2 | upload | 4.81 |
| 512 KiB 发送,三入口 | singbox | 58 | 2 | download | 11.32 |
| 512 KiB 发送,三入口 | singbox | 58 | 2 | upload | 6.13 |
| 最终参数,0 ms | sdk | 0 | 2 | download | 233.67 |
| 最终参数,0 ms | sdk | 0 | 2 | upload | 730.31 |
| 最终参数,0 ms | socks | 0 | 2 | download | 226.22 |
| 最终参数,0 ms | socks | 0 | 2 | upload | 694.12 |
| 最终参数,0 ms | singbox | 0 | 2 | download | 248.07 |
| 最终参数,0 ms | singbox | 0 | 2 | upload | 706.50 |
| 最终参数,8 MiB | sdk | 58 | 8 | download | 16.03 |
| 最终参数,8 MiB | sdk | 58 | 8 | upload | 8.39 |
| 最终参数,8 MiB 重复 | sdk | 58 | 8 | download | 16.19 |
| 最终参数,8 MiB 重复 | sdk | 58 | 8 | upload | 5.55 |
| 最终参数,0 ms 反序重复 | singbox | 0 | 2 | download | 258.43 |
| 最终参数,0 ms 反序重复 | singbox | 0 | 2 | upload | 666.80 |
| 最终参数,0 ms 反序重复 | socks | 0 | 2 | download | 248.18 |
| 最终参数,0 ms 反序重复 | socks | 0 | 2 | upload | 698.08 |
| 最终参数,0 ms 反序重复 | sdk | 0 | 2 | download | 240.14 |
| 最终参数,0 ms 反序重复 | sdk | 0 | 2 | upload | 559.92 |
| 最终参数,三入口 | sdk | 58 | 2 | download | 16.30 |
| 最终参数,三入口 | sdk | 58 | 2 | upload | 51.57 |
| 最终参数,三入口 | socks | 58 | 2 | download | 15.76 |
| 最终参数,三入口 | socks | 58 | 2 | upload | 12.02 |
| 最终参数,三入口 | singbox | 58 | 2 | download | 15.81 |
| 最终参数,三入口 | singbox | 58 | 2 | upload | 10.97 |
## 回归验证
`MESHPORT_SINGBOX_BIN=... cargo test -p meshport-socks --release --locked -- --include-ignored --skip subnet_throughput_diagnostic` 全部通过:库单元测试 1 项、bridge 3 项(包括真实 Go sing-box 集成)、选路 6 项、相邻子网 UDP 1 项,共 11 项。性能诊断单独串行执行,不包含在此计数。`git diff --check` 通过。