Skip to content

Commit

Permalink
Add recvmmsg fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Mar 4, 2023
1 parent f4a1b33 commit 5848279
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions quinn-udp/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,58 @@ unsafe fn sendmmsg_with_fallback(
*/
}

/// Fallback implementation of `recvmmsg` using `recvmsg`
/// for systems which do not support `recvmmsg`
/// such as Linux <2.6.33.
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
unsafe fn recvmmsg_fallback(
sockfd: libc::c_int,
msgvec: *mut libc::mmsghdr,
vlen: libc::c_uint,
) -> libc::c_int {
let flags = 0;
if vlen == 0 {
return 0;
}

let n = libc::recvmsg(sockfd, &mut (*msgvec).msg_hdr, flags);
if n == -1 {
-1
} else {
(*msgvec).msg_len = n as libc::c_uint;
1
}
}

#[cfg(not(any(target_os = "macos", target_os = "ios")))]
unsafe fn recvmmsg_with_fallback(
sockfd: libc::c_int,
msgvec: *mut libc::mmsghdr,
vlen: libc::c_uint,
) -> libc::c_int {
if true {
recvmmsg_fallback(sockfd, msgvec, vlen)
} else {
let flags = 0;
let timeout = ptr::null_mut::<libc::timespec>();
let ret =
libc::syscall(libc::SYS_recvmmsg, sockfd, msgvec, vlen, flags, timeout) as libc::c_int;

if ret == -1 {
let e = io::Error::last_os_error();
match e.raw_os_error() {
Some(libc::ENOSYS) => {
// Fallback to `sendmsg`.
recvmmsg_fallback(sockfd, msgvec, vlen)
}
_ => -1,
}
} else {
ret
}
}
}

fn init(io: SockRef<'_>) -> io::Result<()> {
let mut cmsg_platform_space = 0;
if cfg!(target_os = "linux") || cfg!(target_os = "freebsd") || cfg!(target_os = "macos") {
Expand Down Expand Up @@ -358,12 +410,10 @@ fn recv(io: SockRef<'_>, bufs: &mut [IoSliceMut<'_>], meta: &mut [RecvMeta]) ->
}
let msg_count = loop {
let n = unsafe {
libc::recvmmsg(
recvmmsg_with_fallback(
io.as_raw_fd(),
hdrs.as_mut_ptr(),
bufs.len().min(BATCH_SIZE) as _,
0,
ptr::null_mut(),
)
};
if n == -1 {
Expand Down

0 comments on commit 5848279

Please sign in to comment.