Skip to content

tokio-quiche: batch UDP sends and receives on Windows - #2913

Open
cf-rhett wants to merge 2 commits into
cloudflare:masterfrom
cf-rhett:rhett/windows-uso-uro
Open

cf-rhett wants to merge 2 commits into
cloudflare:masterfrom
cf-rhett:rhett/windows-uso-uro

Conversation

@cf-rhett

@cf-rhett cf-rhett commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

tokio-quiche only batches UDP I/O on Linux, so on Windows every QUIC packet costs its own send and receive system call. Windows has both offloads: UDP segmentation offload (USO) for sends and UDP receive offload (URO) for receives. This adds them, one commit each, behind the same SocketCapabilities opt-in that GSO and GRO use on Linux. Linux and macOS are unchanged.

I measured both on a Surface Pro (Snapdragon X, Windows 11 Arm64, Wi-Fi), in a client that tunnels traffic over QUIC datagrams. With USO, uploads at the same throughput used 20-25% less CPU in the sending process and about 15% less kernel time, all of it in the send path. With URO, downloads arrived at 33-36 datagrams per receive call instead of one.

USO

SocketCapabilities detects USO by querying UDP_SEND_MSG_SIZE, as msquic does. gso::send_to then passes the segment size with each WSASendMsg as a UDP_SEND_MSG_SIZE control message, so the socket keeps no state between sends. apply_max_capabilities(), and therefore listen(), enable it on Windows the way they enable GSO on Linux.

URO

apply_all_and_get_compatibility() sets UDP_RECV_MAX_COALESCED_SIZE and reports it as has_gro, so a socket only coalesces when the caller asked for capabilities; connect() gets neither offload. The router receives through WSARecvMsg and puts the segment size from UDP_COALESCED_INFO into the same gro field Linux GRO fills, so the IO worker and the client connector split batches as they already do on Linux. WSARecvMsg is resolved per socket, because the pointer belongs to that socket's Winsock provider (quinn-rs/quinn#2825).

Some machines coalesce without attaching UDP_COALESCED_INFO, notably Windows on Arm with WSL2 installed and some NIC drivers (quinn-rs/quinn#2041, https://bugzilla.mozilla.org/show_bug.cgi?id=1916558), which is why quinn-udp leaves URO off by default. Correct coalescing reports the original datagram size, so the router treats a receive larger than MAX_DATAGRAM_SIZE without a segment size that fits as such a train: it drops the receive and turns URO off for that socket. firezone/firezone#14392 ships a similar size check. A single datagram that large is dropped too, which is no change, since it never fit the router's MAX_DATAGRAM_SIZE receive buffer. Trains under 1500 bytes can't be detected by size, but merged short-header packets fail decryption as one packet, so they show up as loss rather than misread data.

Both commits come with Windows tests that go through the real send path and the real router over loopback, on connected and unconnected sockets. Loopback never coalesces, so the cmsg parsing and the size rule are also unit-tested directly.

@cf-rhett
cf-rhett force-pushed the rhett/windows-uso-uro branch from c0b88f0 to 45822ce Compare November 23, 2026 17:36
@cf-rhett
cf-rhett marked this pull request as ready for review November 23, 2026 20:39
@cf-rhett
cf-rhett requested a review from a team as a code owner November 23, 2026 20:39
Comment thread tokio-quiche/src/quic/io/gso.rs Outdated
};
let mut cmsg = SegmentSizeCmsg {
hdr: CMSGHDR {
cmsg_len: mem::offset_of!(SegmentSizeCmsg, segment_size) +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does windows have equivalents of the CMSG_*() macros to set up and consume cmsg control messages?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, why do you need offset_of instead of using size_of::<SegmentSizeCmsg>()?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are. WSA_CMSG_*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no Rust binding for WSA_CMSG_*, so I copied the macros as helpers

Comment thread tokio-quiche/src/quic/router/mod.rs Outdated
self.poll_recv_from(cx)
}

#[cfg(windows)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should split this into three functions (Linux, windows, other) and gate the functions directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split

@gregor-cf

Copy link
Copy Markdown
Contributor

/bonk

@ask-bonk

ask-bonk Bot commented Nov 23, 2026

Copy link
Copy Markdown

LGTM!

github run

socket: &tokio::net::UdpSocket, to: SocketAddr, send_buf: &[u8],
segment_size: usize,
) -> io::Result<usize> {
let (mut addr, addr_len) = to_sockaddr(to);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This address is only needed for unconnected case (server case). I don't remember if tokio-quiche calls connect() for client socket itself or not but for connected sockets, this is redundant and might trigger a failure if the address is somehow different.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for the receive case. Though, I don't know how much work is needed to plumb that through layers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client sockets come in already connected (tokio-quiche doesn't call connect() itself), so it is redundant there. Skipping it needs a connected flag down to send_to and the receiver, and Linux passes it the same way today, so I'd do it for send and receive on all platforms in a follow-up after the macOS batching PR, if that works for you.

@csujedihy csujedihy Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not a big deal. feel free to ignore

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If linux also always just reads the remote address, which seems like so, then there is not much point doing it at all.

Comment thread tokio-quiche/src/quic/router/uro.rs Outdated
/// Returns `WSARecvMsg` of `socket`'s Winsock provider, or `None` if the
/// provider doesn't offer it. The pointer is only valid for that provider's
/// sockets.
fn wsa_recv_msg(socket: &UdpSocket) -> Option<WsaRecvMsg> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be named as get_wsa_recv_msg_fn.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread tokio-quiche/src/quic/router/uro.rs Outdated
/// other receive reports how a coalesced receive splits.
pub(super) fn new(socket: &UdpSocket) -> Option<Self> {
let Some(recv_msg) = wsa_recv_msg(socket) else {
let _ = set_max_coalesced_size(socket, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why calling set_max_coalesced_size? if we fail to retrieve the fn, this should fail too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment thread tokio-quiche/src/quic/router/uro.rs Outdated
func
}

fn set_max_coalesced_size(socket: &UdpSocket, size: u32) -> io::Result<()> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually set this to a non-zero value anywhere?

@cf-rhett
cf-rhett force-pushed the rhett/windows-uso-uro branch from 45822ce to ca2c84c Compare November 24, 2026 12:26
Comment thread tokio-quiche/src/socket/capabilities.rs Outdated
/// [`BufFactory::MAX_BUF_SIZE`](crate::buf_factory::BufFactory::MAX_BUF_SIZE)
/// holds it.
#[cfg(windows)]
const MAX_COALESCED_SIZE: u32 = u16::MAX as u32;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/microsoft/msquic/blob/aa31c3f8641934653a8acfe5f4d22b2465b7d509/src/platform/datapath_winuser.c#L54

I'd do the same 65535 - 8. I don't remember why but msquic is the golden standard for Windows UDP usage.

@cf-rhett
cf-rhett force-pushed the rhett/windows-uso-uro branch from ca2c84c to cedbf17 Compare November 25, 2026 18:29
Windows supports UDP segmentation offload (USO): a single send of up to
64 KB with a segment size leaves the stack as MTU-sized datagrams.
Batched sends were Linux-only, so on Windows the worker made one send
syscall per QUIC packet.

Detect USO in SocketCapabilities by querying UDP_SEND_MSG_SIZE, as
msquic does, and on Windows have gso::send_to pass the segment size with
each WSASendMsg as a UDP_SEND_MSG_SIZE control message, so the socket
keeps no segmentation state between sends. apply_max_capabilities(), and
therefore listen(), now enable this on Windows as they do GSO on Linux.

On a Windows 11 Arm64 laptop, uploads at the same throughput used
20-25% less CPU in the sending process and about 15% less kernel time,
all of it in the send path.
On Windows the router reads one datagram per receive call, so a fast
download pays one system call and one wakeup per QUIC packet. Windows can
merge consecutive datagrams of a flow into one receive (UDP receive
offload, URO), but only for sockets that set UDP_RECV_MAX_COALESCED_SIZE,
and the size of the merged datagrams is only reported in a
UDP_COALESCED_INFO control message, which needs WSARecvMsg. On a Windows
11 Arm64 laptop a download received 33-36 datagrams per call with URO.

SocketCapabilities::apply_all_and_get_compatibility() sets the option and
reports it as has_gro, so URO is opt-in like Linux GRO, and listen() and
apply_max_capabilities() turn it on. The router receives through
WSARecvMsg and puts the reported segment size into the same `gro` field
the Linux GRO path fills, so the IO worker and the client connector split
the batch as they already do on Linux. WSARecvMsg is resolved for each
socket, since the pointer belongs to that socket's Winsock provider.

Some machines coalesce without attaching UDP_COALESCED_INFO, which turns a
train of datagrams into one payload that can't be split (seen with WSL2
on Windows on Arm and with some NIC drivers), so quinn-udp leaves URO off
by default. A receive larger than MAX_DATAGRAM_SIZE without a segment
size that fits is treated as such a train: the router drops it and turns
URO off for that socket. A single datagram that large is dropped the same
way; it never fit the router's MAX_DATAGRAM_SIZE receive buffer.

Linux and macOS are unchanged.
@cf-rhett
cf-rhett force-pushed the rhett/windows-uso-uro branch from cedbf17 to 400a05f Compare November 30, 2026 13:39

This branch has not been deployed

No deployments
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