Skip to content

Commit

Permalink
Implement endio to Request (#56)
Browse files Browse the repository at this point in the history
* Implement endio to Request
  • Loading branch information
akiradeveloper authored May 18, 2024
1 parent 07a6346 commit 531578f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
32 changes: 13 additions & 19 deletions azbuse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,36 +104,31 @@ pub struct Request {
pub len: u64,
pub io_vecs: Vec<IOVec>,
pub request_id: u64,
fd: i32,
}

pub struct Response {
pub errorno: i32,
pub request_id: u64,
impl Request {
pub fn endio(self, error: i32) {
let cmplt = AzbuseCompletion {
id: self.request_id,
result: error,
};
unsafe { azbuse_put_req(self.fd, &cmplt) }.expect("failed to put req");
}
}

#[async_trait]
pub trait StorageEngine: Send + Sync + 'static {
async fn call(&mut self, req: Request) -> Response;
async fn call(&mut self, req: Request);
}

struct RequestHandler<Engine: StorageEngine> {
fd: i32,
rx: tokio::sync::mpsc::UnboundedReceiver<Request>,
engine: Engine,
}
impl <Engine: StorageEngine> RequestHandler<Engine> {
async fn run_once(&mut self, req: Request) {
let req_id = req.request_id;
let res = self.engine.call(req).await;
let cmplt = AzbuseCompletion {
id: req_id,
result: res.errorno,
};
unsafe { azbuse_put_req(self.fd, &cmplt) }.expect("failed to put req");
}
async fn run(mut self) {
while let Some(req) = self.rx.recv().await {
self.run_once(req).await
self.engine.call(req).await;
}
}
}
Expand Down Expand Up @@ -183,7 +178,6 @@ pub async fn run_on(config: Config, engine: impl StorageEngine) {

let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let request_handler = RequestHandler {
fd,
rx,
engine,
};
Expand Down Expand Up @@ -223,8 +217,6 @@ pub async fn run_on(config: Config, engine: impl StorageEngine) {
out
};

let null_p = unsafe { std::mem::transmute::<usize, *mut c_void>(0) };

let mut tot_n_pages = 0;
for i in 0..n {
tot_n_pages += xfr_io_vec[i].n_pages;
Expand Down Expand Up @@ -252,7 +244,9 @@ pub async fn run_on(config: Config, engine: impl StorageEngine) {
start: xfr.offset,
len: xfr.len,
request_id: xfr.id,
fd,
};

tx.send(req).unwrap();
}
}
Expand Down
19 changes: 6 additions & 13 deletions tests/ramdisk/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use azbuse::{CmdFlags, IOVec, Request, Response, StorageEngine};
use azbuse::{CmdFlags, IOVec, Request, StorageEngine};
use async_trait::async_trait;
use clap::Parser;
use core::ffi::c_void;
Expand Down Expand Up @@ -27,29 +27,22 @@ struct Engine {
}
#[async_trait]
impl StorageEngine for Engine {
async fn call(&mut self, req: Request) -> Response {
async fn call(&mut self, req: Request) {
let id = req.request_id;
let req_op = req.cmd_flags & CmdFlags::OP_MASK;
match req_op {
CmdFlags::OP_WRITE => {
let m = &mut self.mem;
m.write(req.start as usize, &req.io_vecs);
Response {
request_id: id,
errorno: 0,
}
req.endio(0);
}
CmdFlags::OP_READ => {
let m = &self.mem;
m.read(req.start as usize, &req.io_vecs);
Response {
request_id: id,
errorno: 0,
}
req.endio(0);
}
_ => Response {
request_id: id,
errorno: -libc::EOPNOTSUPP,
_ => {
req.endio(-libc::EOPNOTSUPP);
},
}
}
Expand Down

0 comments on commit 531578f

Please # to comment.