Skip to content

VLESS: run multiplexing directly on transport - #7364

Open
svlavr wants to merge 45 commits into
XTLS:mainfrom
svlavr:codex/vless-mux-direct
Open

svlavr wants to merge 45 commits into
XTLS:mainfrom
svlavr:codex/vless-mux-direct

Conversation

@svlavr

@svlavr svlavr commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Move multiplexing to proxy/vless/multiplex and Reverse to proxy/vless/reverse. VLESS inbound, outbound and Reverse use direct transport paths. Intermediate pipes are removed from all VLESS Multiplex and Reverse paths and from the VMess Multiplex inbound adapter.

Configure VLESS outbound through settings.multiplex. The old generic outbound Mux and classic Reverse paths are removed.

Preserve incremental TCP delivery, whole UDP packets and XUDP reconnection. Simplify shutdown and VMess response startup, and retain actual framing and transport errors.

First working version for review.

Medium1992 and others added 3 commits November 9, 2026 17:19
…rough

Since 5745dea the workers' common.Interrupt(link.Writer) reaches a
MutexWriter, which embeds only buf.Writer, and does nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Let VLESS own the configured Mux and XUDP pools and feed Mux frames through the VLESS codecs without the carrier pipe bridge. Keep legacy factories for other outbounds and preserve the existing logical link handoff.

Close the carrier independently of frame writes and retire the pools and preconnections with their handler. Preserve first-flight processing and final bytes returned with a terminal read error.
Abort an XDrive transport independently of a blocked frame writer while preserving graceful EOF for an idle carrier. Cancel the owned storage session and announcement, reject writes that reach admission after cancellation, and keep graceful WAL drain behavior for live sessions.

Exercise a full unacknowledged slot ring through the direct VLESS Mux handler, idle peer EOF, slot and WAL admission, and cancellation of a pending announcement.
@svlavr svlavr closed this Nov 10, 2026
@svlavr svlavr reopened this Nov 10, 2026
@Fangliding

Copy link
Copy Markdown
Member

没什么必要继续穿透封装 这里不会有多大开销的

@Fangliding Fangliding closed this Nov 10, 2026
@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

@svlavr rebase main

#7355 (comment) 的意思是把 Mux 的代码移到 proxy/vless/multiplex 文件夹,然后出入站、reverse 都直接调用它

其它出站不给开 mux.cool 了,入站的话就 VMess 留一个兼容吧,且等新 Mux 稳定后出站不需要 common/xudp/xudp.go 了

@RPRX RPRX reopened this Nov 11, 2026
@svlavr

svlavr commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@RPRX

This is a draft, still under development. I reworked it around your clarification: move Mux into VLESS and remove the old generic connection path.

Mux now lives in proxy/vless/multiplex. VLESS inbound, outbound and Reverse call it directly. VLESS owns its Mux/XUDP pools. Enabling mux.cool on other outbounds is rejected at startup. Compatibility with existing VMess Mux clients is retained.

On the transmit side, frames go directly through the VLESS codec. The receive side retains one existing pipe, allowing transport reads to run independently of frame parsing and delivery to child connections. This part of the existing buffering mechanism remains.

I compared several alternatives:

  • Fully direct receiving lost about 15% of throughput and increased latency under load.
  • Channel-based read-ahead and a wider ReadV vector recovered only part of that loss. Rebuilding the existing pipe's accumulation, backpressure and shutdown logic under a different name would add more code.
  • Writing a whole input batch under one lock could let a large response occupy the shared writer for too long. The current implementation submits at most two existing 8 KiB frames per write, then releases the lock. It does not wait for another input batch.
  • Increasing stream frames to 32 KiB did not provide enough benefit to justify changing the existing framing and increasing head-of-line blocking.
  • I did not include the full VMess rewrite from VMess inbound: Refactor code to use DispatchLink() #5143. It changes termination signalling and authenticated inbound-user timeout handling. Their existing behavior is retained here.

The bounded writes follow your concern in #7345. The receive buffer addresses the separate read/parse overlap measured above.

Direct writes also require an interruptible XDrive connection, so stopping Mux can unblock a write waiting on a full ring. A quiescent connection still closes with EOF. VLESS now closes its owned Mux pools and cancels unfinished connection creation during shutdown. The original receive-buffer thresholds are retained: 64 KiB for ordinary Mux and 16 KiB for Reverse.

I compared both updated endpoints with main 14df073 on Windows loopback. Six paired runs used eight bulk streams plus a small request/response stream sharing one carrier. Medians were:

  • Throughput: approximately 264 → 276 MiB/s.
  • Small-request p95 under load: 27.6 → 23.5 ms.
  • Combined client/server CPU: approximately 9.96 → 9.73 CPU-seconds per GiB of verified echo payload, roughly unchanged.
  • Client Private Bytes in separate memory runs: 57.0 → 56.5 MiB, a small difference.

Single-stream results varied substantially. These local measurements do not establish a speedup for every workload or transport.

The fixes from #7359 and #7375 are preserved. Ordinary transports default to concurrency 8 and keep explicit positive values. XDrive retains the minimum of 128. XUDP settings are unchanged. Retirement of common/xudp remains a later step after the new Mux stabilizes.

The full PR diff against main 14df073 is 32 files, +588/-502 lines, net +86, including 11 file moves.

This is the first working version. I am still working on it. Comments, suggestions, criticism.

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

@svlavr 原有的 mux 文件夹全删了,outbound 不要新开一个文件,直接改新 mux 文件夹,reverse 调用 mux 的方式也改

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

一步到位再弄个 proxy/vless/reverse 文件夹吧

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

类似 reverse,配置方式是 "multiplex": {},代码里主要写 multiplex 而非 mux,就像代码里很少写 rvs

@svlavr

svlavr commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@RPRX

Updated the draft following your latest comments.

Removed outbound/mux.go and the generic outbound ConfigureMux hook. Multiplex connection management now lives in proxy/vless/multiplex. Reverse is consolidated in proxy/vless/reverse, removing the duplicate implementations from VLESS inbound and outbound.

Added "multiplex": {} under VLESS outbound settings, alongside reverse. Legacy VLESS JSON mux is converted to this configuration. The old protobuf Mux setting is rejected with a migration message.

Direct writes and one receive pipe are retained. The fixes from #7359 and #7375 are preserved.

I am continuing to optimize the implementation and compare alternatives.

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

其实我本来准备今天自己搞这些的,幸好有你,不过有一条,这么改应当是更省代码的,删掉的代码应是新增代码的 1.5 倍

@svlavr

svlavr commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@RPRX

I am continuing to simplify the draft. In commit 2abab46, I included a small reduction that preserves the existing functionality, after comparing several ways to remove pipes and change how Link is used.

I removed the unused response buffer from the Reverse control session. It was created for replies, but nobody read from it. Incoming data on that side is now released through buf.Discard. Control-message transmission, session completion and reconnection are retained.

I also removed duplicate Bridge and Portal initialization. The shared constructors are now NewBridge and NewPortal. Legacy protobuf configuration is handled by newLegacyBridge and newLegacyPortal, using the same initialization.

Five unreachable lines following the error for the removed legacy reverse setting were deleted.

This commit touches 10 functions and one struct field:

  • infra/conf/xray.go: +0/-5, file length 758 → 753 lines. Removed unreachable code after the unconditional return in Config.Build().
  • proxy/vless/inbound/inbound.go: +1/-1, 648 → 648 lines. Handler.GetReverse() calls the shared NewPortal.
  • proxy/vless/outbound/outbound.go: +1/-1, 491 → 491 lines. New() calls the shared NewBridge.
  • proxy/vless/reverse/bridge.go: +5/-13, 297 → 289 lines. The constructors share initialization; legacy settings go through newLegacyBridge.
  • proxy/vless/reverse/portal.go: +7/-18, 338 → 327 lines. Simplified the constructors, removed the unused pipe from NewPortalWorker(), and removed the reader field and its interruption in heartbeat().
  • proxy/vless/reverse/reverse.go: +2/-2, 94 → 94 lines. Reverse.Init() calls the legacy-configuration constructors.

In a separate construction/close measurement, the Reverse control worker went from 89 to 78 allocations, with allocated bytes falling from approximately 13,647 to 13,032 bytes per operation. This measures control-worker construction and cleanup.

Alternatives compared:

  • Direct receiving with no receive pipe. In the local eight-stream comparison, median throughput fell from approximately 267 to 220 MiB/s, and latency increased. Not included.
  • Explicit TCP receive-buffer sizes of 64/128/256 KiB. Throughput was approximately 125/191/227 MiB/s. They did not recover the loss.
  • Collecting an entire frame before delivering it. Throughput rose to approximately 293 MiB/s, but available data was held until the frame completed. The first version also retained excessive memory with small fragments. Compacting the buffers addressed memory usage, but the partial-frame delivery delay remained.
  • A simpler incremental stream-frame reader. It did not recover throughput without the pipe. With the existing pipe, some runs showed a CPU improvement, but a consistent overall throughput/latency advantage was not established.
  • DispatchLink with direct child-response writes. The first version stalled on a failed route when a physical write was blocked. The corrected version added 80 net lines and changed traffic accounting on failed routes. I did not include that broader change in this cleanup.
  • Returning a prepared Link instead of waiting for readiness. It added 31 net lines and failed the shutdown case: a blocked child close could leave the physical connection open.

For now, direct transmission and one receive pipe remain. The other buffers have separate responsibilities, including backpressure, session termination and XUDP reassociation.

This commit: 6 files, +16/-40, net -24 lines. The full PR against main f082058: 47 files, +1066/-845, net +221 lines. The PR as a whole still does not reduce source size.

This remains a draft, and I am continuing to work on it. Comments, suggestions, criticism.

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

若非必要,pipe 最好别留吧,你那测试是不是因为缺了 readv 导致的

@svlavr

svlavr commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

@RPRX

This is the first working version for review, updated in 39bafa6. All comparisons below are against upstream f082058.

Mux now lives in proxy/vless/multiplex, and Reverse in proxy/vless/reverse. VLESS inbound, outbound and Reverse call them directly. The old folders and the separate outbound/mux.go are removed. Configuration is now under VLESS settings.multiplex, and the main implementation names also use multiplex.

Client and server share frame parsing and connection lifecycle code. The old generic Mux connection machinery and duplicate shutdown paths are removed.

Ordinary child responses go through DispatchLink directly into the Mux encoder. Their individual response pipes and copy loops are removed. One sender per carrier serializes encoded frames through a bounded queue. When that queue is full, subsequent writes wait for space. END waits for the underlying write to complete before retiring the old session identity, preserving DATA/END order and valid ID reuse.

One receive pipe remains on each outgoing carrier so transport reads can proceed independently of frame parsing and delivery. The original thresholds remain: 64 KiB for ordinary connections and 16 KiB for Reverse. XUDP retains its own association queues for reconnection; the old binding stops reading before the new binding takes over. No new server session-count or rate limit is introduced.

Reverse no longer has duplicate initialization, the unused control-response buffer or a periodic collector for completed Portal handlers. Closing a handler removes that exact handler. Heartbeats and reconnection are retained.

Close and interruption now pass through the reader and UDP destination-override wrappers. Native timeout-capable readers use their existing timeout mechanism without an extra reader goroutine. Canceling a blocked XDrive write unblocks it; normal shutdown of an idle connection retains EOF.

Incoming VMess Mux compatibility is retained. The existing VMess UDP-cone path through common/xudp remains for now; its removal is deferred until the new implementation stabilizes. Legacy mux configuration must be replaced with settings.multiplex; the old SenderConfig Mux format is rejected with a migration message. The remaining classic Reverse binary-configuration path is removed, while native VLESS Reverse is retained.

Local Windows loopback measurements used both matching endpoints, six paired runs per workload, and byte-verified echo traffic. The single-stream workload transferred 512 MiB; the mixed workload transferred 1 GiB across eight bulk streams, with a small request/response stream sharing the carrier. Medians were:

  • Single-stream throughput: 185 → 196 MiB/s.
  • Mixed-load throughput in the repeat series: 253 → 305 MiB/s.
  • Small-request p95 under mixed load: 28.1 → 19.9 ms.
  • Combined client/server CPU under mixed load: 9.97 → 9.38 CPU-seconds per GiB of verified echo payload.
  • Server Private Bytes under mixed load: 76.1 → 64.8 MiB.

Non-generated production Go code against upstream f082058: 35 files, +1600/−1836 lines, net −236. The complete PR diff shown by GitHub is 60 files, +2122/−2818 lines, net −696.

Comments, suggestions, criticism.

@svlavr

svlavr commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

I’m working on removing the remaining pipe usage and simplifying the implementation.

@Fangliding

Copy link
Copy Markdown
Member

@svlavr rebase main

#7355 (comment) 的意思是把 Mux 的代码移到 proxy/vless/multiplex 文件夹,然后出入站、reverse 都直接调用它

其它出站不给开 mux.cool 了,入站的话就 VMess 留一个兼容吧,且等新 Mux 稳定后出站不需要 common/xudp/xudp.go 了

vmess出都要删? 这是vmess的东西的东西啊甚至有mux的协议指令

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

vmess出都要删? 这是vmess的东西的东西啊甚至有mux的协议指令

#4231 (comment) 本来就不会是原版 mux 了,且你没发现今年都没什么人用 VMess 了吗,VLESS Encryption 正在逐步取代它

真要继续支持的话无非是加个调用加些参数的事,但有什么必要,一年前 Reverse 只留 VLESS 了也没人说还要给 VMess 留着

@Fangliding

Fangliding commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

如果真是一个全新东西那break都炸 如果兼容老的那么本质还是一样的
如果一个东西没什么人在说 要么是没人用 还有可能是跑的很稳定以至于没人提 mux.cool 是 vmess 协议的一部分 砍了等于把vmess协议砍残了
旧reverse那是本来用的就少 怎么可能和 vmess mux 比用户量 最近core限这砍那的最后结果就是很多人不敢升级了呆在旧版 甚至有人整理旧版本xray还支持的特性和交互性 或者有的人就跑去用别的核心了

@RPRX

RPRX commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

我先合了 #5143 就是想看一下,现在 proxy/vmess/inbound/inbound.go 明显写复杂了吧,另外 reverse_test.go 直接删掉即可

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

我只是想知道 VLESS inbound 那么简单,VMess inbound 为什么要加那么多代码,你让 AI 清理一下,然后测测有没有问题

@svlavr

svlavr commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@RPRX, I'm working on exactly that now. I'm simplifying VMess inbound and testing the changes.

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

主要是这个 PR 跑通就行,合并后 VLESS 内的代码我肯定还要再整理的,而 VMess 那边可能没人改,怕复杂代码一直遗留下去

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

#4231 (comment) VMess 出站兼容原配置位置的 Mux,调用 VLESS 内的 multiplex 代码

SpofyJet added a commit to SpofyJet/xray-core that referenced this pull request Nov 19, 2026
…fter local session termination; Fix race (#7411)

XTLS/Xray-core#4621 (comment)
XTLS/Xray-core#7411 (comment)
XTLS/Xray-core#7364 (comment)

---------

Co-authored-by: Meow <197331664+Meo597@users.noreply.github.com>
(cherry picked from commit b1905088c3195992c345dfd35d2fbef515c3bd49)

Co-authored-by: Mishalski <100153948+wywywywycloud@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RPRX

RPRX commented Nov 19, 2026 •

Copy link
Copy Markdown
Member
	if request.Command == protocol.RequestCommandMux {
		worker := multiplex.NewServerWorker(ctx, dispatch.DispatchLink, &transport.Link{Reader: clientReader, Writer: clientWriter})
		<-worker.WaitClosed()
		return nil
	}

VLESS inbound 就只是这样,然而 VMess inbound。。。

Complete the outer TLS handshake before checking the negotiated Vision version. Reuse the decoded inbound Link and select SingleXUDP without a synthetic destination. Use one condition variable to notify carrier work and capacity.
@svlavr

svlavr commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@RPRX, I finished the cleanup and pushed the changes. I removed another 28 lines from VMess inbound. Ordinary TCP and UDP use the DispatchLink path from #5143. The remaining VMess code serializes concurrent replies before encryption, completes the response according to the protocol, and sends the VMess response header before Mux frames.

VMess outbound accepts mux in its original configuration location and uses the shared VLESS Multiplex implementation. I simplified Multiplex shutdown and Reverse, removed the long-running Reverse test, and kept the main Reverse scenario. I also fixed the TLS version check that broke Vision over mKCP.

All 11 protocol scenarios, interoperability checks with the earlier build, and targeted Multiplex tests with the race detector passed. The full PR is now 403 lines smaller than main.

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

Mux 已经没有 concurrent write 了, VMess 入站多那一百行代码是干嘛

Use the upstream first-write response framing, keep serialization for ordinary TCP and UDP replies, and rely on the shared Multiplex worker for Mux. Restore the upstream VMess Mux scenarios.
Keep physical write serialization on the carrier, share endpoint framing, and consolidate VLESS outbound setup. Reuse VMess body codec initialization in each direction and remove redundant outbound handling.
Check VMess body codecs in both directions, verify actual Mux carrier reuse and user removal, and exercise Vision over mKCP. Simplify VMess email indexing and request selection, and remove unused VLESS handler state.
@RPRX

RPRX commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

这 AI 咋把 VMess 重构了,我的意思是 codecBody 的代码应该没必要?VMess 入站参考 VLESS 进行最小修改即可,其它的别动

Use the decoded and encoded endpoints directly for native Multiplex. Keep the ordinary post-read cancellation guard while removing duplicate writer and socket lifecycle handling. Restore upstream test fixtures outside the migration.
@svlavr

svlavr commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@RPRX, I made the encoding change on my own initiative. I saw the same VMess codec setup repeated in four places on the client and server, so I moved it into one shared function. That cut 161 lines of production code. The protocol behavior stays the same, and I verified interoperability between the old and new builds in both client/server directions.

If you'd prefer to keep encoding as it is on main for #7364, I'll restore it here.

@RPRX

RPRX commented Nov 20, 2026

Copy link
Copy Markdown
Member

有些代码可能是以前出于防探测的考虑,不过后来光是全随机数外观就已经等于翻墙了所以

你想弄的话弄成独立的 PR 吧,我还是更关心 codecBody 那块代码能不能去掉

Keep codec factoring out of the Multiplex migration. Restore the four encoding files from the upstream base, including the original tests.
Pass the decoded reader directly to DispatchLink for ordinary VMess, matching upstream. Native Freedom closes the backend on cancellation in the tested cases. The direct Multiplex path already bypassed the wrapper.
Keep the Mux carrier counter with the affected tests. Name both XDrive configuration keys in the diagnostic and restore the original upstream fixtures.
Restore deferred dispatcher registration and retain legacy VLESS Mux settings across API replay. Cancel Reverse without waiting under the outbound manager lock. Make the UDP/443 test detect unexpected carrier opens and drop redundant XUDP cleanup.
Select native Multiplex explicitly and drop redundant mode and response codec checks. Preserve retry, stream termination and physical connection ownership.
@svlavr

svlavr commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@RPRX

Changes and checks
  • VMess. I removed codecBody, cancelReader, and the writer wrapper. Ordinary TCP and UDP go directly through DispatchLink. Inbound Mux passes the decoder and encoder to the shared Multiplex implementation. I restored encoding to match main. VMess outbound now selects Mux explicitly, and the legacy mux key still works in its original location.
  • Multiplex and Reverse. I removed the PhysicalWriter wrapper. The carrier now serializes writes itself. I fixed the Reverse shutdown hang with dialerProxy. VLESS preserves the legacy mux setting when listing and re-adding outbounds through the API. I also restored support for late dispatcher registration in embedded Core.
  • Checks. I fixed the UDP/443 test that passed even with the skip case removed. I also disabled Mux in a control run to confirm that the VLESS and VMess tests actually check for one physical carrier. Interoperability passed in both old/new peer directions. Two-hop XUDP traffic resumed after a carrier disconnect. Reverse opened a second carrier after 260 sessions, and the stream on the original carrier continued working.

The DNS Hijack and TestTagsCache issues also reproduce on main and are separate work.

Please take a look. I think everything is ready.

@RPRX

RPRX commented Nov 21, 2026

Copy link
Copy Markdown
Member

不过 userByEmail 那些咋删掉了,你改完得看一下,不要动无关的代码

@svlavr

svlavr commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@RPRX, you suggested simplifying the VMess code. I replaced userByEmail with a sync.Map in VMess inbound. User management stayed the same, and that block went from 114 to 55 lines.

Got it. I'll restore the original implementation and keep the changes in this PR to a minimum.

@RPRX

RPRX commented Nov 22, 2026

Copy link
Copy Markdown
Member

@svlavr 是不是忘了 commit

@RPRX RPRX mentioned this pull request Nov 22, 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.

5 participants