Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MaybeUninit more properly in streamer::batch_send() #3325

Merged
merged 6 commits into from
Oct 29, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions streamer/src/sendmmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#[cfg(target_os = "linux")]
use {
itertools::izip,
libc::{iovec, mmsghdr, sockaddr_in, sockaddr_in6, sockaddr_storage},
libc::{iovec, mmsghdr, msghdr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t},
std::mem::MaybeUninit,
std::os::unix::io::AsRawFd,
};
use {
Expand Down Expand Up @@ -58,41 +59,51 @@ where
fn mmsghdr_for_packet(
packet: &[u8],
dest: &SocketAddr,
iov: &mut iovec,
addr: &mut sockaddr_storage,
hdr: &mut mmsghdr,
iov: &mut MaybeUninit<iovec>,
addr: &mut MaybeUninit<sockaddr_storage>,
hdr: &mut MaybeUninit<mmsghdr>,
) {
const SIZE_OF_SOCKADDR_IN: usize = std::mem::size_of::<sockaddr_in>();
const SIZE_OF_SOCKADDR_IN6: usize = std::mem::size_of::<sockaddr_in6>();

*iov = iovec {
iov.write(iovec {
iov_base: packet.as_ptr() as *mut libc::c_void,
iov_len: packet.len(),
};
hdr.msg_hdr.msg_iov = iov;
hdr.msg_hdr.msg_iovlen = 1;
hdr.msg_hdr.msg_name = addr as *mut _ as *mut _;
});

match dest {
let msg_namelen = match dest {
SocketAddr::V4(socket_addr_v4) => {
unsafe {
std::ptr::write(
addr as *mut _ as *mut _,
addr.as_mut_ptr() as *mut _,
*nix::sys::socket::SockaddrIn::from(*socket_addr_v4).as_ref(),
);
}
hdr.msg_hdr.msg_namelen = SIZE_OF_SOCKADDR_IN as u32;
SIZE_OF_SOCKADDR_IN as socklen_t
}
SocketAddr::V6(socket_addr_v6) => {
unsafe {
std::ptr::write(
addr as *mut _ as *mut _,
addr.as_mut_ptr() as *mut _,
*nix::sys::socket::SockaddrIn6::from(*socket_addr_v6).as_ref(),
);
}
hdr.msg_hdr.msg_namelen = SIZE_OF_SOCKADDR_IN6 as u32;
SIZE_OF_SOCKADDR_IN6 as socklen_t
}
}
};

hdr.write(mmsghdr {
msg_len: 0,
msg_hdr: msghdr {
msg_name: addr as *mut _ as *mut _,
msg_namelen,
msg_iov: iov.as_mut_ptr(),
msg_iovlen: 1,
msg_control: std::ptr::null::<libc::c_void>() as *mut _,
msg_controllen: 0,
msg_flags: 0,
},
});
}

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -135,14 +146,20 @@ where
T: AsRef<[u8]>,
{
let size = packets.len();
#[allow(clippy::uninit_assumed_init)]
let iovec = std::mem::MaybeUninit::<iovec>::uninit();
let mut iovs = vec![unsafe { iovec.assume_init() }; size];
let mut addrs = vec![unsafe { std::mem::zeroed() }; size];
let mut hdrs = vec![unsafe { std::mem::zeroed() }; size];
let mut iovs = vec![MaybeUninit::<iovec>::uninit(); size];
let mut addrs = vec![MaybeUninit::<sockaddr_storage>::zeroed(); size];
steviez marked this conversation as resolved.
Show resolved Hide resolved
let mut hdrs = vec![MaybeUninit::<mmsghdr>::uninit(); size];
for ((pkt, dest), hdr, iov, addr) in izip!(packets, &mut hdrs, &mut iovs, &mut addrs) {
mmsghdr_for_packet(pkt.as_ref(), dest.borrow(), iov, addr, hdr);
}
// mmsghdr_for_packet() performs initialization so we can safely transmute
// the Vecs to their initialized counterparts
let _iovs = unsafe { std::mem::transmute::<Vec<MaybeUninit<iovec>>, Vec<iovec>>(iovs) };
steviez marked this conversation as resolved.
Show resolved Hide resolved
let _addrs = unsafe {
std::mem::transmute::<Vec<MaybeUninit<sockaddr_storage>>, Vec<sockaddr_storage>>(addrs)
};
let mut hdrs = unsafe { std::mem::transmute::<Vec<MaybeUninit<mmsghdr>>, Vec<mmsghdr>>(hdrs) };

sendmmsg_retry(sock, &mut hdrs)
}

Expand Down
Loading