mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 18:25:30 +00:00
udp: uring: change SendBuffer unsafe declarations, add comments
This commit is contained in:
parent
f84d80a7e7
commit
f89bdce7f0
2 changed files with 38 additions and 30 deletions
|
|
@ -320,8 +320,13 @@ impl SocketWorker {
|
||||||
response_counter.fetch_add(1, Ordering::Relaxed);
|
response_counter.fetch_add(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.send_buffers
|
// Safety: OK because cqe using buffer has been
|
||||||
.mark_index_as_free(send_buffer_index as usize);
|
// returned and contents will no longer be accessed
|
||||||
|
// by kernel
|
||||||
|
unsafe {
|
||||||
|
self.send_buffers
|
||||||
|
.mark_index_as_free(send_buffer_index as usize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,9 +39,9 @@ struct SendBuffer {
|
||||||
iovec: UnsafeCell<libc::iovec>,
|
iovec: UnsafeCell<libc::iovec>,
|
||||||
msghdr: UnsafeCell<libc::msghdr>,
|
msghdr: UnsafeCell<libc::msghdr>,
|
||||||
free: bool,
|
free: bool,
|
||||||
// Only used for statistics
|
/// Only used for statistics
|
||||||
receiver_is_ipv4: bool,
|
receiver_is_ipv4: bool,
|
||||||
// Only used for statistics
|
/// Only used for statistics
|
||||||
response_type: ResponseType,
|
response_type: ResponseType,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,32 +81,34 @@ impl SendBuffer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
fn setup_pointers(&mut self, socket_is_ipv4: bool) {
|
||||||
///
|
unsafe {
|
||||||
/// - SendBuffer must be stored at a fixed location in memory
|
let iovec = &mut *self.iovec.get();
|
||||||
unsafe fn setup_pointers(&mut self, socket_is_ipv4: bool) {
|
|
||||||
let iovec = &mut *self.iovec.get();
|
|
||||||
|
|
||||||
iovec.iov_base = self.bytes.get() as *mut libc::c_void;
|
iovec.iov_base = self.bytes.get() as *mut libc::c_void;
|
||||||
iovec.iov_len = (&*self.bytes.get()).len();
|
iovec.iov_len = (&*self.bytes.get()).len();
|
||||||
|
|
||||||
let msghdr = &mut *self.msghdr.get();
|
let msghdr = &mut *self.msghdr.get();
|
||||||
|
|
||||||
msghdr.msg_iov = self.iovec.get();
|
msghdr.msg_iov = self.iovec.get();
|
||||||
|
|
||||||
if socket_is_ipv4 {
|
if socket_is_ipv4 {
|
||||||
msghdr.msg_name = self.name_v4.get() as *mut libc::c_void;
|
msghdr.msg_name = self.name_v4.get() as *mut libc::c_void;
|
||||||
msghdr.msg_namelen = core::mem::size_of::<libc::sockaddr_in>() as u32;
|
msghdr.msg_namelen = core::mem::size_of::<libc::sockaddr_in>() as u32;
|
||||||
} else {
|
} else {
|
||||||
msghdr.msg_name = self.name_v6.get() as *mut libc::c_void;
|
msghdr.msg_name = self.name_v6.get() as *mut libc::c_void;
|
||||||
msghdr.msg_namelen = core::mem::size_of::<libc::sockaddr_in6>() as u32;
|
msghdr.msg_namelen = core::mem::size_of::<libc::sockaddr_in6>() as u32;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
///
|
///
|
||||||
/// - SendBuffer must be stored at a fixed location in memory
|
/// - SendBuffer must be stored at a fixed location in memory
|
||||||
/// - SendBuffer.setup_pointers must have been called previously
|
/// - SendBuffer.setup_pointers must have been called while stored at that
|
||||||
|
/// fixed location
|
||||||
|
/// - Contents of struct fields wrapped in UnsafeCell can NOT be accessed
|
||||||
|
/// simultaneously to this function call
|
||||||
unsafe fn prepare_entry(
|
unsafe fn prepare_entry(
|
||||||
&mut self,
|
&mut self,
|
||||||
response: &Response,
|
response: &Response,
|
||||||
|
|
@ -128,6 +130,7 @@ impl SendBuffer {
|
||||||
name.sin_port = addr.port().to_be();
|
name.sin_port = addr.port().to_be();
|
||||||
name.sin_addr.s_addr = u32::from(*addr.ip()).to_be();
|
name.sin_addr.s_addr = u32::from(*addr.ip()).to_be();
|
||||||
} else {
|
} else {
|
||||||
|
// Set receiver protocol type before calling addr.get_ipv6_mapped()
|
||||||
self.receiver_is_ipv4 = addr.is_ipv4();
|
self.receiver_is_ipv4 = addr.is_ipv4();
|
||||||
|
|
||||||
let addr = if let SocketAddr::V6(addr) = addr.get_ipv6_mapped() {
|
let addr = if let SocketAddr::V6(addr) = addr.get_ipv6_mapped() {
|
||||||
|
|
@ -153,9 +156,7 @@ impl SendBuffer {
|
||||||
self.response_type = ResponseType::from_response(response);
|
self.response_type = ResponseType::from_response(response);
|
||||||
self.free = false;
|
self.free = false;
|
||||||
|
|
||||||
let sqe = SendMsg::new(SOCKET_IDENTIFIER, self.msghdr.get()).build();
|
Ok(SendMsg::new(SOCKET_IDENTIFIER, self.msghdr.get()).build())
|
||||||
|
|
||||||
Ok(sqe)
|
|
||||||
}
|
}
|
||||||
Err(err) => Err(Error::SerializationFailed(err)),
|
Err(err) => Err(Error::SerializationFailed(err)),
|
||||||
}
|
}
|
||||||
|
|
@ -178,10 +179,7 @@ impl SendBuffers {
|
||||||
.into_boxed_slice();
|
.into_boxed_slice();
|
||||||
|
|
||||||
for buffer in buffers.iter_mut() {
|
for buffer in buffers.iter_mut() {
|
||||||
// Safety: OK because buffers are stored in fixed memory location
|
buffer.setup_pointers(socket_is_ipv4);
|
||||||
unsafe {
|
|
||||||
buffer.setup_pointers(socket_is_ipv4);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
|
@ -197,7 +195,11 @@ impl SendBuffers {
|
||||||
(buffer.response_type, buffer.receiver_is_ipv4)
|
(buffer.response_type, buffer.receiver_is_ipv4)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mark_index_as_free(&mut self, index: usize) {
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// Only safe to call once buffer is no longer referenced by in-flight
|
||||||
|
/// io_uring queue entries
|
||||||
|
pub unsafe fn mark_index_as_free(&mut self, index: usize) {
|
||||||
self.buffers[index].free = true;
|
self.buffers[index].free = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -215,8 +217,9 @@ impl SendBuffers {
|
||||||
|
|
||||||
let buffer = self.buffers.index_mut(index);
|
let buffer = self.buffers.index_mut(index);
|
||||||
|
|
||||||
// Safety: OK because buffers are stored in fixed memory location
|
// Safety: OK because buffers are stored in fixed memory location,
|
||||||
// and buffer pointers were set up in SendBuffers::new()
|
// buffer pointers were set up in SendBuffers::new() and pointers to
|
||||||
|
// SendBuffer UnsafeCell contents are not accessed elsewhere
|
||||||
unsafe {
|
unsafe {
|
||||||
match buffer.prepare_entry(response, addr, self.socket_is_ipv4) {
|
match buffer.prepare_entry(response, addr, self.socket_is_ipv4) {
|
||||||
Ok(entry) => {
|
Ok(entry) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue