vmm: Simplify some of the io_uring code

Small patch creating a dedicated `block_io_uring_is_supported()`
function for the non-io_uring case, so that we can simplify the
code in the DeviceManager.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-10-07 11:23:20 +02:00
parent ac32b1e35f
commit 1e3a6cb450
3 changed files with 33 additions and 59 deletions

View File

@ -14,13 +14,15 @@ extern crate log;
extern crate serde_derive;
#[cfg(feature = "io_uring")]
use io_uring::{opcode, squeue, IoUring, Probe};
use io_uring::Probe;
use io_uring::{opcode, squeue, IoUring};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use std::cmp;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::os::linux::fs::MetadataExt;
#[cfg(feature = "io_uring")]
use std::os::unix::io::{AsRawFd, RawFd};
use std::os::unix::io::AsRawFd;
use std::os::unix::io::RawFd;
use std::path::PathBuf;
use std::result;
use virtio_bindings::bindings::virtio_blk::*;
@ -266,7 +268,6 @@ impl Request {
Ok(len)
}
#[cfg(feature = "io_uring")]
pub fn execute_io_uring(
&self,
mem: &GuestMemoryMmap,
@ -565,3 +566,8 @@ pub fn block_io_uring_is_supported() -> bool {
true
}
#[cfg(not(feature = "io_uring"))]
pub fn block_io_uring_is_supported() -> bool {
false
}

View File

@ -31,7 +31,6 @@ use std::io;
mod device;
pub mod balloon;
pub mod block;
#[cfg(feature = "io_uring")]
pub mod block_io_uring;
mod console;
pub mod epoll_helper;
@ -48,7 +47,6 @@ pub mod vsock;
pub use self::balloon::*;
pub use self::block::*;
#[cfg(feature = "io_uring")]
pub use self::block_io_uring::*;
pub use self::console::*;
pub use self::device::*;

View File

@ -36,7 +36,6 @@ use arch::layout;
use arch::layout::{APIC_START, IOAPIC_SIZE, IOAPIC_START};
#[cfg(target_arch = "aarch64")]
use arch::DeviceType;
#[cfg(feature = "io_uring")]
use block_util::block_io_uring_is_supported;
#[cfg(target_arch = "aarch64")]
use devices::gic;
@ -1754,61 +1753,32 @@ impl DeviceManager {
.map_err(DeviceManagerError::DetectImageType)?;
let (virtio_device, migratable_device) = match image_type {
ImageType::Raw => {
#[cfg(feature = "io_uring")]
{
// Use asynchronous backend relying on io_uring if the
// syscalls are supported.
if block_io_uring_is_supported() {
let dev = Arc::new(Mutex::new(
virtio_devices::BlockIoUring::new(
id.clone(),
image,
disk_cfg
.path
.as_ref()
.ok_or(DeviceManagerError::NoDiskPath)?
.clone(),
disk_cfg.readonly,
disk_cfg.iommu,
disk_cfg.num_queues,
disk_cfg.queue_size,
self.seccomp_action.clone(),
)
.map_err(DeviceManagerError::CreateVirtioBlock)?,
));
(
Arc::clone(&dev) as VirtioDeviceArc,
dev as Arc<Mutex<dyn Migratable>>,
// Use asynchronous backend relying on io_uring if the
// syscalls are supported.
if block_io_uring_is_supported() {
let dev = Arc::new(Mutex::new(
virtio_devices::BlockIoUring::new(
id.clone(),
image,
disk_cfg
.path
.as_ref()
.ok_or(DeviceManagerError::NoDiskPath)?
.clone(),
disk_cfg.readonly,
disk_cfg.iommu,
disk_cfg.num_queues,
disk_cfg.queue_size,
self.seccomp_action.clone(),
)
} else {
let dev = Arc::new(Mutex::new(
virtio_devices::Block::new(
id.clone(),
raw_img,
disk_cfg
.path
.as_ref()
.ok_or(DeviceManagerError::NoDiskPath)?
.clone(),
disk_cfg.readonly,
disk_cfg.iommu,
disk_cfg.num_queues,
disk_cfg.queue_size,
self.seccomp_action.clone(),
)
.map_err(DeviceManagerError::CreateVirtioBlock)?,
));
.map_err(DeviceManagerError::CreateVirtioBlock)?,
));
(
Arc::clone(&dev) as VirtioDeviceArc,
dev as Arc<Mutex<dyn Migratable>>,
)
}
}
#[cfg(not(feature = "io_uring"))]
{
(
Arc::clone(&dev) as VirtioDeviceArc,
dev as Arc<Mutex<dyn Migratable>>,
)
} else {
let dev = Arc::new(Mutex::new(
virtio_devices::Block::new(
id.clone(),