diff --git a/vhost-device-template/src/main.rs b/vhost-device-template/src/main.rs index 349d367..900a5e9 100644 --- a/vhost-device-template/src/main.rs +++ b/vhost-device-template/src/main.rs @@ -7,18 +7,19 @@ mod vhu_foo; -use log::error; -use std::path::PathBuf; -use std::process::exit; -use std::sync::{Arc, RwLock}; -use std::thread::{spawn, JoinHandle}; +use std::{ + path::PathBuf, + process::exit, + sync::{Arc, RwLock}, + thread::{spawn, JoinHandle}, +}; use clap::Parser; +use log::error; use thiserror::Error as ThisError; use vhost_user_backend::VhostUserDaemon; -use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; - use vhu_foo::VhostUserFooBackend; +use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; type Result = std::result::Result; @@ -50,9 +51,10 @@ impl TryFrom for FooConfiguration { type Error = Error; fn try_from(args: FooArgs) -> Result { - // Even though this try_from() conversion always succeeds, in cases where the device's - // configuration type needs to validate arguments and/or make operations that can fail a - // TryFrom<_> implementation will be necessary. + // Even though this try_from() conversion always succeeds, in cases where the + // device's configuration type needs to validate arguments and/or make + // operations that can fail a TryFrom<_> implementation will be + // necessary. Ok(FooConfiguration { socket_path: args.socket_path, }) @@ -82,9 +84,10 @@ fn start_backend(args: FooArgs) -> Result<()> { let info = FooInfo::new(); let handle: JoinHandle> = spawn(move || loop { - // There isn't much value in complicating code here to return an error from the threads, - // and so the code uses unwrap() instead. The panic on a thread won't cause trouble to the - // main() function and should be safe for the daemon. + // There isn't much value in complicating code here to return an error from the + // threads, and so the code uses unwrap() instead. The panic on a thread + // won't cause trouble to the main() function and should be safe for the + // daemon. let backend = Arc::new(RwLock::new( VhostUserFooBackend::new(info).map_err(Error::CouldNotCreateBackend)?, )); @@ -113,9 +116,10 @@ fn main() { #[cfg(test)] mod tests { - use assert_matches::assert_matches; use std::path::Path; + use assert_matches::assert_matches; + use super::*; impl FooArgs { diff --git a/vhost-device-template/src/vhu_foo.rs b/vhost-device-template/src/vhu_foo.rs index 53a2008..04b2f79 100644 --- a/vhost-device-template/src/vhu_foo.rs +++ b/vhost-device-template/src/vhu_foo.rs @@ -5,23 +5,25 @@ // // SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause -use log::{info, warn}; use std::{ convert, io::{self, Result as IoResult}, }; +use log::{info, warn}; use thiserror::Error as ThisError; use vhost::vhost_user::message::{VhostUserProtocolFeatures, VhostUserVirtioFeatures}; use vhost_user_backend::{VhostUserBackendMut, VringRwLock, VringT}; -use virtio_bindings::bindings::virtio_config::{VIRTIO_F_NOTIFY_ON_EMPTY, VIRTIO_F_VERSION_1}; -use virtio_bindings::bindings::virtio_ring::{ - VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC, +use virtio_bindings::bindings::{ + virtio_config::{VIRTIO_F_NOTIFY_ON_EMPTY, VIRTIO_F_VERSION_1}, + virtio_ring::{VIRTIO_RING_F_EVENT_IDX, VIRTIO_RING_F_INDIRECT_DESC}, }; use virtio_queue::{DescriptorChain, QueueOwnedT}; use vm_memory::{GuestAddressSpace, GuestMemoryAtomic, GuestMemoryLoadGuard, GuestMemoryMmap}; -use vmm_sys_util::epoll::EventSet; -use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; +use vmm_sys_util::{ + epoll::EventSet, + eventfd::{EventFd, EFD_NONBLOCK}, +}; use crate::FooInfo; @@ -83,8 +85,9 @@ impl VhostUserFooBackend { // Iterate over each FOO request. // - // The layout of the various structures, to be read from and written into the descriptor - // buffers, is defined in the Virtio specification for each protocol. + // The layout of the various structures, to be read from and written into the + // descriptor buffers, is defined in the Virtio specification for each + // protocol. for desc_chain in requests.clone() { let counter = self.info.counter(); let descriptors: Vec<_> = desc_chain.clone().collect(); @@ -102,10 +105,11 @@ impl VhostUserFooBackend { "read only" }; - // We now can iterate over the set of descriptors and process the messages. There - // will be a number of read only descriptors containing messages as defined by the - // specification. If any replies are needed, the driver should have placed one or - // more writable descriptors at the end for the device to use to reply. + // We now can iterate over the set of descriptors and process the messages. + // There will be a number of read only descriptors containing + // messages as defined by the specification. If any replies are + // needed, the driver should have placed one or more writable + // descriptors at the end for the device to use to reply. info!("Length of the {} descriptor@{} is: {}", perm, i, desc.len()); } } @@ -221,6 +225,7 @@ impl VhostUserBackendMut for VhostUserFooBackend { #[cfg(test)] mod tests { use std::mem::size_of; + use vhost_user_backend::{VhostUserBackendMut, VringRwLock, VringT}; use virtio_bindings::bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE}; use virtio_queue::{