Skip to content

Commit

Permalink
[WIP] impl uring cmd feature
Browse files Browse the repository at this point in the history
  • Loading branch information
SidongYang committed Oct 8, 2024
1 parent cf5417f commit 8c33e48
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::buf::fixed::FixedBuf;
use crate::buf::{BoundedBuf, BoundedBufMut, IoBuf, IoBufMut, Slice};
use crate::fs::OpenOptions;
use crate::io::cmd::UnsubmittedCmd;
use crate::io::SharedFd;

use crate::runtime::driver::op::{Op, Submit};
Expand Down Expand Up @@ -879,6 +880,11 @@ impl File {
pub async fn close(mut self) -> io::Result<()> {
self.fd.close().await
}

#[allow(missing_docs)]
pub fn cmd<T>(&self, op: u32, cmd: [u8; 16], data: T) -> UnsubmittedCmd<T> {
UnsubmittedCmd::cmd(&self.fd, op, cmd, data)
}
}

impl FromRawFd for File {
Expand Down
54 changes: 54 additions & 0 deletions src/io/cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::marker::PhantomData;

use io_uring::cqueue::Entry;
use io_uring::{opcode, types};

use crate::buf::BoundedBuf;
use crate::io::SharedFd;

use crate::{OneshotOutputTransform, UnsubmittedOneshot};

/// An unsubmitted read operation.
pub type UnsubmittedCmd<T> = UnsubmittedOneshot<CmdData<T>, CmdTransform<T>>;

impl<T> UnsubmittedCmd<T> {
pub(crate) fn cmd(fd: &SharedFd, op: u32, cmd: [u8; 16], data: T) -> Self {
Self::new(
CmdData { data },
CmdTransform {
_phantom: PhantomData,
},
opcode::UringCmd16::new(types::Fd(fd.raw_fd()), op)
.cmd(cmd)
.build(),
)
}
}

#[allow(missing_docs)]
pub struct CmdData<T> {
data: T,
}

#[allow(missing_docs)]
pub struct CmdTransform<B> {
_phantom: PhantomData<B>,
}

impl<T> OneshotOutputTransform for CmdTransform<T> {
type Output = (i32, T);
type StoredData = CmdData<T>;

fn transform_oneshot_output(self, mut data: Self::StoredData, cqe: Entry) -> Self::Output {
// let result = cqe.result();
// let res = if n >= 0 {
// // Safety: the kernel wrote `n` bytes to the buffer.
// unsafe { data.buf.set_init(n as usize) };
// Ok(n as usize)
// } else {
// Err(io::Error::from_raw_os_error(-n))
// };

(cqe.result(), data.data)
}
}
2 changes: 2 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ pub(crate) mod writev;

mod writev_all;
pub(crate) use writev_all::writev_at_all;

pub(crate) mod cmd;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub mod buf;
pub mod fs;
pub mod net;

pub use io::cmd::*;
pub use io::read::*;
pub use io::readv::*;
pub use io::write::*;
Expand Down

0 comments on commit 8c33e48

Please sign in to comment.