Skip to content

Commit

Permalink
examples: convert to run_target() interface
Browse files Browse the repository at this point in the history
UblkSession::run_target() is more friendly and flexible, and should be
preferred.

Signed-off-by: Ming Lei <tom.leiming@gmail.com>
  • Loading branch information
ming1 committed Oct 14, 2023
1 parent 2c0d466 commit 498e522
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 33 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ libublk = "0.1"
```

Next we can start using `libublk` crate.
The following is quick introduction for adding ublk-null block device, which
is built over libublk high level APIs.
The following is quick introduction for creating one ublk-null target,
and ublk block device(/dev/ublkbN) will be created after the code is
run.

``` rust
use libublk::io::{UblkDev, UblkIOCtx, UblkQueue};
Expand All @@ -54,15 +55,23 @@ fn main() {
};
let wh = {
let (mut ctrl, dev) = sess.create_devices(tgt_init).unwrap();
let handle_io = move |q: &UblkQueue, tag: u16, _io: &UblkIOCtx| {
let iod = q.get_iod(tag);
let res = Ok(UblkIORes::Result(
(unsafe { (*iod).nr_sectors << 9 } as i32),
));
q.complete_io_cmd(tag, res);
// queue level logic
let q_handler = move |qid: u16, _dev: &UblkDev| {
// logic for io handling
let io_handler = move |q: &UblkQueue, tag: u16, _io: &UblkIOCtx| {
let iod = q.get_iod(tag);
let bytes = unsafe { (*iod).nr_sectors << 9 } as i32;

q.complete_io_cmd(tag, Ok(UblkIORes::Result(bytes)));
};

UblkQueue::new(qid, _dev)
.unwrap()
.wait_and_handle_io(io_handler);
};

sess.run(&mut ctrl, &dev, handle_io, |dev_id| {
// Now start this ublk target
sess.run_target(&mut ctrl, &dev, q_handler, |dev_id| {
let mut d_ctrl = UblkCtrl::new_simple(dev_id, 0).unwrap();
d_ctrl.dump();
})
Expand Down
12 changes: 9 additions & 3 deletions examples/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,16 @@ fn test_add() {

let tgt_init = |dev: &mut UblkDev| lo_init_tgt(dev, &lo);
let (mut ctrl, dev) = sess.create_devices(tgt_init).unwrap();
let lo_handle_io =
move |q: &UblkQueue, tag: u16, io: &UblkIOCtx| _lo_handle_io(q, tag, io);
let q_fn = move |qid: u16, _dev: &UblkDev| {
let lo_io_handler =
move |q: &UblkQueue, tag: u16, io: &UblkIOCtx| _lo_handle_io(q, tag, io);

sess.run(&mut ctrl, &dev, lo_handle_io, |dev_id| {
UblkQueue::new(qid, _dev)
.unwrap()
.wait_and_handle_io(lo_io_handler);
};

sess.run_target(&mut ctrl, &dev, q_fn, |dev_id| {
let mut d_ctrl = UblkCtrl::new_simple(dev_id, 0).unwrap();
d_ctrl.dump();
})
Expand Down
11 changes: 8 additions & 3 deletions examples/ramdisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ fn rd_add_dev(dev_id: i32, buf_addr: u64, size: u64, for_add: bool) {
Ok(serde_json::json!({}))
};
let (mut ctrl, dev) = sess.create_devices(tgt_init).unwrap();
let lo_handle_io = move |q: &UblkQueue, tag: u16, io: &UblkIOCtx| {
handle_io(q, tag, io, buf_addr);
let q_fn = move |qid: u16, _dev: &UblkDev| {
let rd_io_handler = move |q: &UblkQueue, tag: u16, io: &UblkIOCtx| {
handle_io(q, tag, io, buf_addr);
};
UblkQueue::new(qid, _dev)
.unwrap()
.wait_and_handle_io(rd_io_handler);
};

sess.run(&mut ctrl, &dev, lo_handle_io, |dev_id| {
sess.run_target(&mut ctrl, &dev, q_fn, |dev_id| {
let mut d_ctrl = UblkCtrl::new_simple(dev_id, 0).unwrap();
d_ctrl.dump();
})
Expand Down
47 changes: 29 additions & 18 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
use std::env;
use std::path::Path;

fn __test_ublk_null(dev_flags: u32, handler: fn(&UblkQueue, tag: u16, &UblkIOCtx)) {
fn __test_ublk_null(dev_flags: u32, q_handler: fn(u16, &UblkDev)) {
let sess = UblkSessionBuilder::default()
.name("null")
.depth(64_u32)
Expand All @@ -24,9 +24,11 @@ mod tests {

let wh = {
let (mut ctrl, dev) = sess.create_devices(tgt_init).unwrap();
let handle_io = move |q: &UblkQueue, tag: u16, io: &UblkIOCtx| handler(q, tag, io);
let q_fn = move |qid: u16, _dev: &UblkDev| {
q_handler(qid, _dev);
};

sess.run(&mut ctrl, &dev, handle_io, move |dev_id| {
sess.run_target(&mut ctrl, &dev, q_fn, move |dev_id| {
let mut ctrl = UblkCtrl::new_simple(dev_id, 0).unwrap();
let dev_path = format!("{}{}", libublk::BDEV_PATH, dev_id);

Expand All @@ -50,35 +52,44 @@ mod tests {
/// make one ublk-null and test if /dev/ublkbN can be created successfully
#[test]
fn test_ublk_null() {
fn null_handle_io(q: &UblkQueue, tag: u16, _io: &UblkIOCtx) {
let iod = q.get_iod(tag);
let bytes = unsafe { (*iod).nr_sectors << 9 } as i32;
fn null_handle_queue(qid: u16, _dev: &UblkDev) {
let io_handler = move |q: &UblkQueue, tag: u16, _io: &UblkIOCtx| {
let iod = q.get_iod(tag);
let bytes = unsafe { (*iod).nr_sectors << 9 } as i32;

q.complete_io_cmd(tag, Ok(UblkIORes::Result(bytes)));
};

q.complete_io_cmd(tag, Ok(UblkIORes::Result(bytes)));
UblkQueue::new(qid, _dev)
.unwrap()
.wait_and_handle_io(io_handler);
}

__test_ublk_null(UBLK_DEV_F_ADD_DEV, null_handle_io);
__test_ublk_null(UBLK_DEV_F_ADD_DEV, null_handle_queue);
}

/// make one ublk-null and test if /dev/ublkbN can be created successfully
#[cfg(feature = "fat_complete")]
#[test]
fn test_ublk_null_comp_batch() {
use libublk::UblkFatRes;
fn null_handle_io_batch(q: &UblkQueue, tag: u16, io: &UblkIOCtx) {
let iod = q.get_iod(tag);
let bytes = unsafe { (*iod).nr_sectors << 9 } as i32;

let res = Ok(UblkIORes::FatRes(UblkFatRes::BatchRes(vec![(
io.get_tag() as u16,
bytes,
)])));
q.complete_io_cmd(tag, res);
fn null_handle_queue_batch(qid: u16, _dev: &UblkDev) {
let io_handler = move |q: &UblkQueue, tag: u16, _io: &UblkIOCtx| {
let iod = q.get_iod(tag);
let bytes = unsafe { (*iod).nr_sectors << 9 } as i32;

let res = Ok(UblkIORes::FatRes(UblkFatRes::BatchRes(vec![(tag, bytes)])));
q.complete_io_cmd(tag, res);
};

UblkQueue::new(qid, _dev)
.unwrap()
.wait_and_handle_io(io_handler);
}

__test_ublk_null(
UBLK_DEV_F_ADD_DEV | UBLK_DEV_F_COMP_BATCH,
null_handle_io_batch,
null_handle_queue_batch,
);
}

Expand Down

0 comments on commit 498e522

Please sign in to comment.