Skip to content

tun: add lightweight "system" TCP/IP stack as an alternative to gVisor - #6795

Closed
RPRX with Copilot wants to merge 2 commits into
mainfrom
copilot/tcpip-stack-for-tun
Closed

RPRX with Copilot wants to merge 2 commits into
mainfrom
copilot/tcpip-stack-for-tun

Conversation

Copilot AI commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

gVisor's stack.Stack is a general-purpose TCP/IP implementation built for a lossy, reordering public network (congestion control, SACK/RACK, retransmission timers, extra copies). TUN traffic instead flows over a local kernel↔userspace channel that neither drops nor reorders packets, so that generality is pure overhead. This adds an optional, purpose-built stack for TUN that drops the unneeded machinery in exchange for less complexity and fewer copies.

Backend selection

  • New stack config option (proxy/tun/config.proto, infra/conf/tun.go): "gvisor" (default, unchanged behavior) or "system" (new).
  • proxy/tun/stack.go: NewStack() dispatches on StackOptions.Backend; stack_gvisor.go is otherwise untouched (renamed constructor only).

New "system" stack (proxy/tun/stack_system.go, stack_system_tcp.go)

  • Reads/parses IPv4/IPv6 directly off the tun device via the existing GVisorDevice interface (now also implemented for this path in tun_linux.go/tun_android.go), bypassing gVisor's NIC/routing/stack.Stack layer entirely.
  • UDP and ICMP echo reuse the existing udpConnectionHandler/tun/icmp handlers as-is — that code was already backend-agnostic.
  • TCP gets a small dedicated state machine instead of gVisor's endpoint:
    • In-order-only segment acceptance (no reassembly/SACK); out-of-order or unacceptable segments are dropped and re-acked.
    • Single unified RTO timer covering retransmission, zero-window probing, and lone-FIN resend.
    • Graceful close with a short bounded linger in place of full RFC793 TIME_WAIT.
    • No RST bookkeeping on abort: once a connection is dropped from the table, any stray follow-up packet is naturally answered with a raw RST by the "unknown connection" path.
    • No window scaling/congestion control — advertised window capped at 65535, buffers sized to match gVisor's existing defaults (1 MiB) for comparable throughput on the effectively zero-RTT tun hop.
  • Deliberately exposes only plain net.Conn Read/Write (never ReadMultiBuffer/WriteMultiBuffer) so stat.CounterConnection's byte accounting in handler.go isn't silently bypassed.

Tests

  • proxy/tun/stack_system_test.go exercises the new stack against a fake in-memory GVisorDevice: full handshake + bidirectional echo + graceful close, retransmission on a dropped segment, and idle-connection reaping.

Example

{
  "inbounds": [{
    "protocol": "tun",
    "settings": {
      "stack": "system"
    }
  }]
}

@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

基于 #6771 (comment) 的理念,为 TUN 写一个精简高效的 TCP/IP 栈作为可选项

Claude Sonnet 5 (Max reasoning) · 1017.4 AI credits,干了一个小时直接干掉了 2/3 的每月免费额度,不过不用白不用

@yuhan6665 @Fangliding @LjhAUMEM @Meo597 @yiguodev @KobeArthurScofield 来测测性能有没有提升

@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

看起来 tests 炸了,不过本来就是抛砖引玉而已,等别人 vibe 出一个更好的,选项也不用加了,直接换掉 gVisor 吧

我怀疑这个 PR 现在的代码仍未将“减少 copy 次数”做到极致,此外还看到大量对 gVisor 组件的引用,这些引用也得去掉

话说在群里看到 @Fangliding 说在搓 SS2022,你那个这几天能 PR 吗,精简实现就行,那些 relay 啥的都没人用,不用搞

@LjhAUMEM

Copy link
Copy Markdown
Contributor

link #6399

@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

link #6399

他这个 PR 的“system”指的是交给操作系统解析还是自己写的 TCP/IP 栈,难道 Xray 差点遥遥领先了但 PR 被 @Fangliding 毙了

@Fangliding

Copy link
Copy Markdown
Member

就是天天和gvisor对标的那个system stack而已 他还标个 "lightweight userspace TCP/IP stack" 估计还是经典的vibe到不知道自己在说啥 还带了一堆有的没的其他东西

@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

那还好,所以谁来测测本 PR,话说这里的“system”字样应该不能是那种吧

@therealtunge

Copy link
Copy Markdown

oh no, rprx using ai...

@KobeArthurScofield

Copy link
Copy Markdown
Collaborator

Windows Server 2012 R2 能用
Windows Server 2008 R2 能用但是没东西测
性能测试没有跑,粗略看有点半斤八两

@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

@4RH1T3CT0R7 我看到你近期有几个 PR 被 close 了,你比较有空的话按这里讨论的优化一下 TUN 的性能吧,直接 PR 到 main

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor

ok, tun is on me

@RPRX

RPRX commented Oct 10, 2026

Copy link
Copy Markdown
Member

@4RH1T3CT0R7 即将成为 Xray-core 性能优化专员

@RPRX

RPRX commented Oct 12, 2026

Copy link
Copy Markdown
Member

#7130 70ef97d

@RPRX RPRX closed this Oct 12, 2026
@RPRX
RPRX deleted the copilot/tcpip-stack-for-tun branch November 21, 2026 20:46
@RPRX RPRX mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants