Skip to content

Commit

Permalink
feat: Faster UDP/IO on Apple platforms
Browse files Browse the repository at this point in the history
This uses Apple's private sendmsg_x and recvmsg_x system calls for
multi-packet UDP I/O.
  • Loading branch information
larseggert authored and djc committed Oct 25, 2024
1 parent 91a639f commit adc4a06
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 119 deletions.
5 changes: 5 additions & 0 deletions quinn-udp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ default = ["tracing", "log"]
# Configure `tracing` to log events via `log` if no `tracing` subscriber exists.
log = ["tracing/log"]
direct-log = ["dep:log"]
# Use private Apple APIs to send multiple packets in a single syscall.
fast-apple-datapath = []

[dependencies]
libc = "0.2.158"
Expand All @@ -33,6 +35,9 @@ windows-sys = { workspace = true }
criterion = { version = "0.5", default-features = false, features = ["async_tokio"] }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "net"] }

[build-dependencies]
cfg_aliases = "0.2"

[lib]
# See https://github.com/bheisler/criterion.rs/blob/master/book/src/faq.md#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options
bench = false
Expand Down
2 changes: 1 addition & 1 deletion quinn-udp/benches/throughput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let mut permutations = vec![];
for gso_enabled in [
false,
#[cfg(any(target_os = "linux", target_os = "windows"))]
#[cfg(any(target_os = "linux", target_os = "windows", apple))]
true,
] {
for gro_enabled in [false, true] {
Expand Down
26 changes: 26 additions & 0 deletions quinn-udp/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use cfg_aliases::cfg_aliases;

fn main() {
// Setup cfg aliases
cfg_aliases! {
// Platforms
apple: {
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "visionos"
)
},
bsd: {
any(
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd"
)
},
// Convenience aliases
apple_fast: { all(apple, feature = "fast-apple-datapath") },
apple_slow: { all(apple, not(feature = "fast-apple-datapath")) },
}
}
23 changes: 23 additions & 0 deletions quinn-udp/src/cmsg/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ impl MsgHdr for libc::msghdr {
}
}

#[cfg(apple_fast)]
impl MsgHdr for crate::imp::msghdr_x {
type ControlMessage = libc::cmsghdr;

fn cmsg_first_hdr(&self) -> *mut Self::ControlMessage {
let selfp = self as *const _ as *mut libc::msghdr;
unsafe { libc::CMSG_FIRSTHDR(selfp) }
}

fn cmsg_nxt_hdr(&self, cmsg: &Self::ControlMessage) -> *mut Self::ControlMessage {
let selfp = self as *const _ as *mut libc::msghdr;
unsafe { libc::CMSG_NXTHDR(selfp, cmsg) }
}

fn set_control_len(&mut self, len: usize) {
self.msg_controllen = len as _;
}

fn control_len(&self) -> usize {
self.msg_controllen as _
}
}

/// Helpers for [`libc::cmsghdr`]
impl CMsgHdr for libc::cmsghdr {
fn cmsg_len(length: usize) -> usize {
Expand Down
Loading

0 comments on commit adc4a06

Please sign in to comment.