Repository navigation
Conversation
c0b88f0 to
45822ce
Compare
| }; | ||
| let mut cmsg = SegmentSizeCmsg { | ||
| hdr: CMSGHDR { | ||
| cmsg_len: mem::offset_of!(SegmentSizeCmsg, segment_size) + |
There was a problem hiding this comment.
does windows have equivalents of the CMSG_*() macros to set up and consume cmsg control messages?
There was a problem hiding this comment.
also, why do you need offset_of instead of using size_of::<SegmentSizeCmsg>()?
There was a problem hiding this comment.
there's no Rust binding for WSA_CMSG_*, so I copied the macros as helpers
| self.poll_recv_from(cx) | ||
| } | ||
|
|
||
| #[cfg(windows)] |
There was a problem hiding this comment.
I think we should split this into three functions (Linux, windows, other) and gate the functions directly.
|
/bonk |
|
LGTM! |
| socket: &tokio::net::UdpSocket, to: SocketAddr, send_buf: &[u8], | ||
| segment_size: usize, | ||
| ) -> io::Result<usize> { | ||
| let (mut addr, addr_len) = to_sockaddr(to); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
same for the receive case. Though, I don't know how much work is needed to plumb that through layers.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Its not a big deal. feel free to ignore
There was a problem hiding this comment.
If linux also always just reads the remote address, which seems like so, then there is not much point doing it at all.
| /// 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> { |
There was a problem hiding this comment.
I think this should be named as get_wsa_recv_msg_fn.
| /// 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); |
There was a problem hiding this comment.
why calling set_max_coalesced_size? if we fail to retrieve the fn, this should fail too.
| func | ||
| } | ||
|
|
||
| fn set_max_coalesced_size(socket: &UdpSocket, size: u32) -> io::Result<()> { |
There was a problem hiding this comment.
do we actually set this to a non-zero value anywhere?
45822ce to
ca2c84c
Compare
| /// [`BufFactory::MAX_BUF_SIZE`](crate::buf_factory::BufFactory::MAX_BUF_SIZE) | ||
| /// holds it. | ||
| #[cfg(windows)] | ||
| const MAX_COALESCED_SIZE: u32 = u16::MAX as u32; |
There was a problem hiding this comment.
I'd do the same 65535 - 8. I don't remember why but msquic is the golden standard for Windows UDP usage.
ca2c84c to
cedbf17
Compare
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.
cedbf17 to
400a05f
Compare
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
SocketCapabilitiesopt-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
SocketCapabilitiesdetects USO by queryingUDP_SEND_MSG_SIZE, as msquic does.gso::send_tothen passes the segment size with eachWSASendMsgas aUDP_SEND_MSG_SIZEcontrol message, so the socket keeps no state between sends.apply_max_capabilities(), and thereforelisten(), enable it on Windows the way they enable GSO on Linux.URO
apply_all_and_get_compatibility()setsUDP_RECV_MAX_COALESCED_SIZEand reports it ashas_gro, so a socket only coalesces when the caller asked for capabilities;connect()gets neither offload. The router receives throughWSARecvMsgand puts the segment size fromUDP_COALESCED_INFOinto the samegrofield Linux GRO fills, so the IO worker and the client connector split batches as they already do on Linux.WSARecvMsgis 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 thanMAX_DATAGRAM_SIZEwithout 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'sMAX_DATAGRAM_SIZEreceive 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.