ublk-org / libublk-rs

Rust libublk
Apache License 2.0
27 stars 6 forks source link

Create and run multiple devices #1

Open wwylele opened 1 year ago

wwylele commented 1 year ago

Is there an example of how to properly create and run multiple block devices in a single process? The recommended run_target only accepts one device and dedicates the current thread for that device. I tried running run_target in multiple threads simultaneously, but that seems to invoke some sort of race condition, which causes all threads to block on starting the handler and never create the device. Adding delay between these threads seem to resolve the problem, but obviously that shouldn't be the proper way to do it.

ming1 commented 1 year ago

Hello,

There isn't such example yet, and this feature is planned to be supported in 0.3 by converting control commands into async/await.

However you still can do that with current interfaces, and please refer to examples/ramdisk.rs, in which one ublk device is created, such as:

1) start one device:

let (mut ctrl, dev) = sess.create_devices(); ctrl.configure_queue(&dev, 0, unsafe { libc::gettid() }) let (token, buf) = ctrl.submit_startdev(&dev).unwrap(); let res = loop { let = q_rc.flush_and_wake_io_tasks(&exe, 0); let _res = ctrl.poll_start_dev(token); ... };

2) repeat the above logic for N devices in current process

3) run epoll() on all queue's ring FD, and once there is an event coming, call q_rc.flush_and_wake_io_tasks(exe, 0) for this queue.

The drawback is that you have to start device one by one.

Once control command is converted to async/await, multiple devices can be supported concurrently in single task. And in future, shared (both control and data) uring will be supported too for creating N devices in single task.

Thanks,