Leadaxe:wireguard-go-awg2-lx 已合并收发队列缓冲区调整

Leadaxe 表示,其 wireguard-go-awg2-lx 发行分支已合入 f1e73df:发送侧按 MTU 分配 TUN 读缓冲区,接收侧把小报文复制到 2 KiB 缓冲区,需要合并写入的 TUN 仍保留完整槽位。在其 NanoPi(Cortex-A55、Linux 6.1)双实例回环测试中,客户端峰值 RSS 从 137 降至 92 MiB。Linux 含 GSO 的 TUN 入站接收侧仍沿用原方案,超过 2 KiB 的消息仍使用槽位;Android 实机和真实链路尚未验证。

作者原文

Outcome

Merged into the release line as f1e73df96b1ed405e957d6e0df7e26c87d362db8. It ships in sing-box-lx v1.14.4-lx.36. The full write-up is SPEC 158.

What was decided

  1. Both directions are fixed, not only receive. The send side turned out to be the larger half: in the router profile from this thread it is 90–100 MB against 47–50 MB of read slots.
  2. Send: the TUN read buffer is sized for the MTU. The size is known in advance, so nothing is copied.
  3. Receive: the message is copied into a 2 KiB buffer at read, and the 64 KiB slot stays with the receive routine. Three ways were compared in one binary on a Cortex-A55. Copy at read costs −3.7…+0.9% per packet against upstream; opening the AEAD straight into the small buffer costs +1.4…+4.5%; the slot budget from the first version of this PR kept 12 MiB where the other two keep 4.
  4. Full-size buffers stay where coalescing needs them: a TUN that batches, a tun.GRODevice, and a device that reports MergesWrites — in sing-box, the WireGuard device with a return path to a GSO TUN.
  5. The queued element owns its buffer; upstream's release sites are not edited. Upstream files get +43 −13 lines of marked insertions, the rest is one new file.

What it achieves

Measured on a NanoPi (4× Cortex-A55, Linux 6.1):

| | before | after |
|---|---:|---:|
| ~1000 packets queued on receive, in-memory bind | 66 MiB | 4 MiB |
| the same over UDP sockets | 117–130 MiB | 4 MiB |
| ~1000 packets queued on send | 64 MiB | 2–3 MiB |
| peak RSS, client, two sing-box instances, 8 TCP streams down then up | 137 MiB | 92 MiB |
| peak RSS, server, same run | 150 MiB | 117 MiB |
| peak RSS, server, upload through a TUN inbound on the client | 270 MiB | 117 MiB |
| heap per endpoint without detour, idle (Linux, Android) | — | 8 MiB less |
| backlog cap on Android | 256 MiB | 8 MiB |

Throughput is the same: 97–102 Mbit/s down and 119–128 up in every variant.

What it does not

  • Linux with a TUN inbound that has GSO: the receive side is unchanged. The return path is attached with the first flow from the TUN and from then on every inbound packet of that endpoint keeps its slot. Nothing regresses there, coalescing is kept, and the send-side savings apply.
  • The receive routines' own batches stay: 2 × 128 slots, 16.4 MiB per endpoint without detour. That is now the largest wireguard-go site in the heap.
  • Messages over 2 KiB still travel in slots. The depth of the queues is not bounded.
  • Not verified: an Android device, a real link.

How it was checked

go test -race on darwin; the full device and conn suites on linux/arm64; the backlog stand in both directions over the in-memory bind and over UDP sockets; three rounds of the two-instance run per variant; a live run of the release candidate with ten start/stop cycles through a SOCKS inbound and through a TUN inbound with direct L3 forwarding, WireGuard and AmneziaWG. Two independent reviews; what they found is fixed in the merged commit.