io-uring: add sendmsg
This commit is contained in:
parent
9416efeabe
commit
e4f97287bc
17 changed files with 493 additions and 191 deletions
|
|
@ -1,10 +1,13 @@
|
|||
use {
|
||||
crate::{
|
||||
io_uring::IoUring,
|
||||
utils::buffd::{BufFdError, BUF_SIZE, CMSG_BUF_SIZE},
|
||||
wheel::{Wheel, WheelTimeoutFuture},
|
||||
io_uring::{IoUring, IoUringError},
|
||||
time::Time,
|
||||
utils::{
|
||||
buf::Buf,
|
||||
buffd::{BufFdError, BUF_SIZE, CMSG_BUF_SIZE},
|
||||
oserror::OsError,
|
||||
},
|
||||
},
|
||||
futures_util::{future::Fuse, select, FutureExt},
|
||||
std::{
|
||||
collections::VecDeque,
|
||||
mem::{self, MaybeUninit},
|
||||
|
|
@ -21,37 +24,33 @@ pub(super) struct MsgFds {
|
|||
pub(super) fds: Vec<Rc<OwnedFd>>,
|
||||
}
|
||||
|
||||
pub struct OutBuffer {
|
||||
pub(super) struct OutBufferMeta {
|
||||
pub(super) read_pos: usize,
|
||||
pub(super) write_pos: usize,
|
||||
pub(super) buf: *mut [MaybeUninit<u8>; OUT_BUF_SIZE],
|
||||
pub(super) fds: VecDeque<MsgFds>,
|
||||
}
|
||||
|
||||
pub struct OutBuffer {
|
||||
pub(super) meta: OutBufferMeta,
|
||||
pub(super) buf: Buf,
|
||||
}
|
||||
|
||||
impl Default for OutBuffer {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
read_pos: 0,
|
||||
write_pos: 0,
|
||||
buf: Box::into_raw(Box::new([MaybeUninit::<u32>::uninit(); OUT_BUF_SIZE / 4])) as _,
|
||||
fds: Default::default(),
|
||||
meta: OutBufferMeta {
|
||||
read_pos: 0,
|
||||
write_pos: 0,
|
||||
fds: Default::default(),
|
||||
},
|
||||
buf: Buf::new(OUT_BUF_SIZE),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl OutBuffer {
|
||||
pub fn write(&mut self, bytes: &[MaybeUninit<u8>]) {
|
||||
if bytes.len() > OUT_BUF_SIZE - self.write_pos {
|
||||
panic!("Out buffer overflow");
|
||||
}
|
||||
unsafe {
|
||||
(*self.buf)[self.write_pos..self.write_pos + bytes.len()].copy_from_slice(bytes);
|
||||
}
|
||||
self.write_pos += bytes.len();
|
||||
}
|
||||
|
||||
pub fn is_full(&self) -> bool {
|
||||
self.write_pos > BUF_SIZE
|
||||
self.meta.write_pos > BUF_SIZE
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +69,7 @@ impl OutBufferSwapchain {
|
|||
}
|
||||
|
||||
pub fn commit(&mut self) {
|
||||
if self.cur.write_pos > 0 {
|
||||
if self.cur.meta.write_pos > 0 {
|
||||
let new = self.free.pop().unwrap_or_default();
|
||||
let old = mem::replace(&mut self.cur, new);
|
||||
self.pending.push_back(old);
|
||||
|
|
@ -81,103 +80,66 @@ impl OutBufferSwapchain {
|
|||
pub struct BufFdOut {
|
||||
fd: Rc<OwnedFd>,
|
||||
ring: Rc<IoUring>,
|
||||
wheel: Rc<Wheel>,
|
||||
cmsg_buf: Box<[MaybeUninit<u8>; CMSG_BUF_SIZE]>,
|
||||
fd_ids: Vec<i32>,
|
||||
}
|
||||
|
||||
impl BufFdOut {
|
||||
pub fn new(fd: &Rc<OwnedFd>, ring: &Rc<IoUring>, wheel: &Rc<Wheel>) -> Self {
|
||||
pub fn new(fd: &Rc<OwnedFd>, ring: &Rc<IoUring>) -> Self {
|
||||
Self {
|
||||
fd: fd.clone(),
|
||||
ring: ring.clone(),
|
||||
wheel: wheel.clone(),
|
||||
cmsg_buf: Box::new([MaybeUninit::uninit(); CMSG_BUF_SIZE]),
|
||||
fd_ids: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn flush(
|
||||
&mut self,
|
||||
buf: &mut OutBuffer,
|
||||
timeout: &mut Option<Fuse<WheelTimeoutFuture>>,
|
||||
) -> Result<(), BufFdError> {
|
||||
while buf.read_pos < buf.write_pos {
|
||||
if self.flush_sync(buf)? {
|
||||
if timeout.is_none() {
|
||||
*timeout = Some(self.wheel.timeout(5000).fuse());
|
||||
}
|
||||
select! {
|
||||
_ = timeout.as_mut().unwrap() => {
|
||||
return Err(BufFdError::Timeout);
|
||||
},
|
||||
res = self.ring.writable(&self.fd).fuse() => {
|
||||
res?;
|
||||
},
|
||||
}
|
||||
}
|
||||
pub async fn flush(&mut self, buf: &mut OutBuffer, timeout: Time) -> Result<(), BufFdError> {
|
||||
while buf.meta.read_pos < buf.meta.write_pos {
|
||||
self.flush_buffer(buf, Some(timeout)).await?;
|
||||
}
|
||||
buf.read_pos = 0;
|
||||
buf.write_pos = 0;
|
||||
buf.meta.read_pos = 0;
|
||||
buf.meta.write_pos = 0;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn flush_no_timeout(&mut self, buf: &mut OutBuffer) -> Result<(), BufFdError> {
|
||||
while buf.read_pos < buf.write_pos {
|
||||
if self.flush_sync(buf)? {
|
||||
let _ = self.ring.writable(&self.fd).await?;
|
||||
}
|
||||
while buf.meta.read_pos < buf.meta.write_pos {
|
||||
self.flush_buffer(buf, None).await?;
|
||||
}
|
||||
buf.read_pos = 0;
|
||||
buf.write_pos = 0;
|
||||
buf.meta.read_pos = 0;
|
||||
buf.meta.write_pos = 0;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn flush_sync(&mut self, buffer: &mut OutBuffer) -> Result<bool, BufFdError> {
|
||||
while buffer.read_pos < buffer.write_pos {
|
||||
let mut buf = unsafe { &(*buffer.buf)[buffer.read_pos..buffer.write_pos] };
|
||||
let mut cmsg_len = 0;
|
||||
let mut fds_opt = None;
|
||||
{
|
||||
let mut f = buffer.fds.front().map(|f| f.pos);
|
||||
if f == Some(buffer.read_pos) {
|
||||
let fds = buffer.fds.pop_front().unwrap();
|
||||
self.fd_ids.clear();
|
||||
self.fd_ids.extend(fds.fds.iter().map(|f| f.raw()));
|
||||
let hdr = c::cmsghdr {
|
||||
cmsg_len: 0,
|
||||
cmsg_level: c::SOL_SOCKET,
|
||||
cmsg_type: c::SCM_RIGHTS,
|
||||
};
|
||||
let mut cmsg_buf = &mut self.cmsg_buf[..];
|
||||
cmsg_len = uapi::cmsg_write(&mut cmsg_buf, hdr, &self.fd_ids[..]).unwrap();
|
||||
fds_opt = Some(fds);
|
||||
f = buffer.fds.front().map(|f| f.pos)
|
||||
}
|
||||
if let Some(next_pos) = f {
|
||||
buf = &buf[..next_pos - buffer.read_pos];
|
||||
}
|
||||
async fn flush_buffer(
|
||||
&mut self,
|
||||
buffer: &mut OutBuffer,
|
||||
timeout: Option<Time>,
|
||||
) -> Result<(), BufFdError> {
|
||||
let mut buf = buffer
|
||||
.buf
|
||||
.slice(buffer.meta.read_pos..buffer.meta.write_pos);
|
||||
let mut fds = vec![];
|
||||
{
|
||||
let mut f = buffer.meta.fds.front().map(|f| f.pos);
|
||||
if f == Some(buffer.meta.read_pos) {
|
||||
fds = buffer.meta.fds.pop_front().unwrap().fds;
|
||||
f = buffer.meta.fds.front().map(|f| f.pos)
|
||||
}
|
||||
if let Some(next_pos) = f {
|
||||
buf = buffer.buf.slice(buffer.meta.read_pos..next_pos);
|
||||
}
|
||||
let hdr = uapi::Msghdr {
|
||||
iov: slice::from_ref(&buf),
|
||||
control: Some(&self.cmsg_buf[..cmsg_len]),
|
||||
name: uapi::sockaddr_none_ref(),
|
||||
};
|
||||
let bytes_sent =
|
||||
match uapi::sendmsg(self.fd.raw(), &hdr, c::MSG_DONTWAIT | c::MSG_NOSIGNAL) {
|
||||
Ok(b) => b,
|
||||
Err(Errno(c::EAGAIN)) => {
|
||||
if let Some(fds) = fds_opt {
|
||||
buffer.fds.push_front(fds);
|
||||
}
|
||||
return Ok(true);
|
||||
}
|
||||
Err(Errno(c::ECONNRESET)) => return Err(BufFdError::Closed),
|
||||
Err(e) => return Err(BufFdError::Io(e.into())),
|
||||
};
|
||||
buffer.read_pos += bytes_sent;
|
||||
}
|
||||
Ok(false)
|
||||
match self.ring.sendmsg(&self.fd, buf, fds, timeout).await {
|
||||
Ok(n) => {
|
||||
buffer.meta.read_pos += n;
|
||||
Ok(())
|
||||
}
|
||||
Err(IoUringError::OsError(OsError(c::ECONNRESET))) => return Err(BufFdError::Closed),
|
||||
Err(IoUringError::OsError(OsError(c::ETIME))) => return Err(BufFdError::Timeout),
|
||||
Err(e) => return Err(BufFdError::Ring(e)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn flush2(
|
||||
|
|
@ -238,11 +200,3 @@ impl BufFdOut {
|
|||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OutBuffer {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
Box::from_raw(self.buf as *mut [MaybeUninit<u32>; OUT_BUF_SIZE / 4]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue