Skip to content

TUN inbound: Read large TCP segments straight into Xray-core's buffers on Linux - #7179

Merged
RPRX merged 2 commits into
XTLS:mainfrom
4RH1T3CT0R7:perf/tun-read-into-buf
Oct 21, 2026
Merged

RPRX merged 2 commits into
XTLS:mainfrom
4RH1T3CT0R7:perf/tun-read-into-buf

Conversation

@4RH1T3CT0R7

@4RH1T3CT0R7 4RH1T3CT0R7 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

As RPRX asked in #7130 and #7156. on linux the tun is opened with offload, so one read gives up to 64 KiB of one TCP stream. that is now read with readv into eight 8 KiB buffers of xray, and a segment of 4 KiB or more that comes in order stays in them and goes on with the headers trimmed, nothing copied. smaller and out of order segments, UDP and ICMP are copied as before. the second commit is about acks: with offload a connection has a 1 MiB window, but the read loop still acked every 64 KiB that the reader had not taken, about every second segment, and each ack written to the tun also makes the kernel send what it frees. now that waits for a quarter of the window, also for WireGuard and MASQUE, which have the same window

on a veth pair with 1500 MTU, 15 samples each, main vs this: one upload stream 3730 to 4385 MB/s (+18%), eight 3374 to 4080 (+21%), CPU of xray per GB of upload 14% and 18% lower, acks 9.3 to 4.6 per MB. downloads are the same, and so are links of 100 Mbit/s with 50 ms and of 50 Mbit/s with 1% loss

@RPRX

RPRX commented Oct 16, 2026

Copy link
Copy Markdown
Member

@Fangliding 你来给他解释吧,不过小 buf、多次 syscall 和大 buf、两次 copy 相比哪个更快还真不好说

@Fangliding

Copy link
Copy Markdown
Member

啥东西
我只觉得这提升好小(

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

@Fangliding added a commit: with offload the read loop acked about every second segment, and each ack written to the tun also makes the kernel send what it frees. now it acks at a quarter of the window, and against main one upload stream is +18% and eight +21%, with 14-18% less CPU per GB

@RPRX

RPRX commented Oct 18, 2026

Copy link
Copy Markdown
Member

@4RH1T3CT0R7 我的意思是能不能比如说先用一两块 buf 接收 TCP/IP header,然后直接用 Xray buf 接收 TCP payload

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

@RPRX it reads close to that now: readv puts the virtio_net_hdr in a small array of its own and the packet in eight xray buffers, the ip and tcp headers at the start of the first. a segment that stays is not copied, the first buffer just starts after the headers and the others are taken as they are, so the payload is in xray buffers after one readv for up to 64 KiB. a buffer of their own for the ip and tcp headers would need their size before the read, and it is 20 or 40 and more for ip and 20 to 60 for tcp. copied are only segments under 4 KiB, onto the end of the buffer before them, and those out of order

@RPRX

RPRX commented Oct 21, 2026

Copy link
Copy Markdown
Member

感觉跟我想的还是不太一样,我想的是跨平台通用的,不过可以先合了看看

@RPRX
RPRX merged commit 557657b into XTLS:main Oct 21, 2026
Jolymmiles added a commit to Jolymmiles/Xray-core that referenced this pull request Oct 27, 2026
On a link that takes large segments (XTLS#7179), a large in-order TCP payload
stays in the buffers the stack read it into and is queued without a copy.
A segment put together from IP fragments, or joined to verify its
checksum, is in a buffer of its own; keeping it queued the buffer of the
last fragment instead, so the connection read other bytes than were sent
while rcvNxt moved past them. Such a segment is now copied.

Found by Greptile on #59.
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.

3 participants