Skip to content

Commit

Permalink
libublk: add device flag of UBLK_DEV_F_NOT_ALLOC_IO_BUF
Browse files Browse the repository at this point in the history
Add UBLK_DEV_F_NOT_ALLOC_IO_BUF so that target code can allocate their
own buffer for handling io command.

Signed-off-by: Ming Lei <[email protected]>
  • Loading branch information
ming1 committed Oct 11, 2023
1 parent 0d36513 commit f266a24
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,9 @@ impl UblkQueue<'_> {
// extra io slot needn't to allocate buffer
let addr = {
if i < depth {
if (dev.dev_info.flags & (super::sys::UBLK_F_USER_COPY as u64)) == 0 {
if (dev.dev_info.flags & (super::sys::UBLK_F_USER_COPY as u64)) == 0
&& (dev.flags & super::dev_flags::UBLK_DEV_F_NOT_ALLOC_IO_BUF) == 0
{
super::ublk_alloc_buf(dev.dev_info.max_io_buf_bytes as usize, unsafe {
libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap()
})
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ pub mod dev_flags {
/// tell UblkCtrl that we are recovering one old device
pub const UBLK_DEV_F_RECOVER_DEV: u32 = 1u32 << 2;

pub const UBLK_DEV_F_ALL: u32 =
UBLK_DEV_F_COMP_BATCH | UBLK_DEV_F_ADD_DEV | UBLK_DEV_F_RECOVER_DEV;
/// don't allocate io buffer for me
pub const UBLK_DEV_F_NOT_ALLOC_IO_BUF: u32 = 1u32 << 3;

pub const UBLK_DEV_F_ALL: u32 = UBLK_DEV_F_COMP_BATCH
| UBLK_DEV_F_ADD_DEV
| UBLK_DEV_F_RECOVER_DEV
| UBLK_DEV_F_NOT_ALLOC_IO_BUF;
}

/// Ublk Fat completion result
Expand Down
24 changes: 24 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,28 @@ mod tests {
let sz = core::mem::size_of::<Result<UblkIORes, UblkError>>();
assert!(sz == 32);
}

#[test]
fn test_not_alloc_io_buf() {
let depth = 64_u32;
let sess = UblkSessionBuilder::default()
.name("no_buf")
.depth(depth)
.nr_queues(1_u32)
.dev_flags(UBLK_DEV_F_ADD_DEV | UBLK_DEV_F_NOT_ALLOC_IO_BUF)
.build()
.unwrap();

let tgt_init = |dev: &mut UblkDev| {
dev.set_default_params(250_u64 << 30);
Ok(serde_json::json!({}))
};
let (mut ctrl, dev) = sess.create_devices(tgt_init).unwrap();
let q = UblkQueue::new(0, &dev).unwrap();

for i in 0..depth {
assert!(q.get_io_buf_addr(i as u16) == std::ptr::null_mut());
}
ctrl.stop_dev(&dev).unwrap();
}
}

0 comments on commit f266a24

Please sign in to comment.