From a641f8930f81983f4c398e7367fc643d590e5e54 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Fri, 4 Sep 2020 09:37:37 +0100 Subject: [PATCH] virtio-devices: virtio-vsock: Port to VirtioCommon Use VirtioCommon to handle activate() preparation, reset() and Pausable. Signed-off-by: Rob Bradford --- virtio-devices/src/vsock/device.rs | 112 +++++++++++------------------ 1 file changed, 40 insertions(+), 72 deletions(-) diff --git a/virtio-devices/src/vsock/device.rs b/virtio-devices/src/vsock/device.rs index d28c54bf5..8ac23de57 100644 --- a/virtio-devices/src/vsock/device.rs +++ b/virtio-devices/src/vsock/device.rs @@ -37,12 +37,11 @@ use anyhow::anyhow; /// - a backend FD. /// use byteorder::{ByteOrder, LittleEndian}; -use libc::EFD_NONBLOCK; use std::io; use std::os::unix::io::AsRawFd; use std::path::PathBuf; use std::result; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::AtomicBool; use std::sync::{Arc, Barrier, RwLock}; use std::thread; use vm_memory::{GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap}; @@ -300,13 +299,6 @@ pub struct Vsock { id: String, cid: u64, backend: Arc>, - kill_evt: Option, - pause_evt: Option, - queue_evts: Option>, - interrupt_cb: Option>, - epoll_threads: Option>>, - paused: Arc, - paused_sync: Arc, path: PathBuf, } @@ -337,19 +329,15 @@ where Ok(Vsock { common: VirtioCommon { + device_type: VirtioDeviceType::TYPE_VSOCK as u32, avail_features, + paused_sync: Some(Arc::new(Barrier::new(2))), + queue_sizes: QUEUE_SIZES.to_vec(), ..Default::default() }, id, cid, backend: Arc::new(RwLock::new(backend)), - kill_evt: None, - pause_evt: None, - queue_evts: None, - interrupt_cb: None, - epoll_threads: None, - paused: Arc::new(AtomicBool::new(false)), - paused_sync: Arc::new(Barrier::new(2)), path, }) } @@ -374,7 +362,7 @@ where B: VsockBackend, { fn drop(&mut self) { - if let Some(kill_evt) = self.kill_evt.take() { + if let Some(kill_evt) = self.common.kill_evt.take() { // Ignore the result because there is nothing we can do about it. let _ = kill_evt.write(1); } @@ -386,11 +374,11 @@ where B: VsockBackend + Sync + 'static, { fn device_type(&self) -> u32 { - VirtioDeviceType::TYPE_VSOCK as u32 + self.common.device_type } fn queue_max_sizes(&self) -> &[u16] { - QUEUE_SIZES + &self.common.queue_sizes } fn features(&self) -> u64 { @@ -423,45 +411,27 @@ where queues: Vec, queue_evts: Vec, ) -> ActivateResult { - if queues.len() != NUM_QUEUES || queue_evts.len() != NUM_QUEUES { - error!( - "Cannot perform activate. Expected {} queue(s), got {}", - NUM_QUEUES, - queues.len() - ); - return Err(ActivateError::BadActivate); - } - - let (self_kill_evt, kill_evt) = EventFd::new(EFD_NONBLOCK) - .and_then(|e| Ok((e.try_clone()?, e))) + self.common.activate(&queues, &queue_evts, &interrupt_cb)?; + let kill_evt = self + .common + .kill_evt + .as_ref() + .unwrap() + .try_clone() .map_err(|e| { - error!("failed creating kill EventFd pair: {}", e); + error!("failed to clone kill_evt eventfd: {}", e); ActivateError::BadActivate })?; - self.kill_evt = Some(self_kill_evt); - - let (self_pause_evt, pause_evt) = EventFd::new(EFD_NONBLOCK) - .and_then(|e| Ok((e.try_clone()?, e))) + let pause_evt = self + .common + .pause_evt + .as_ref() + .unwrap() + .try_clone() .map_err(|e| { - error!("failed creating pause EventFd pair: {}", e); + error!("failed to clone pause_evt eventfd: {}", e); ActivateError::BadActivate })?; - self.pause_evt = Some(self_pause_evt); - - // Save the interrupt EventFD as we need to return it on reset - // but clone it to pass into the thread. - self.interrupt_cb = Some(interrupt_cb.clone()); - - let mut tmp_queue_evts: Vec = Vec::new(); - for queue_evt in queue_evts.iter() { - // Save the queue EventFD as we need to return it on reset - // but clone it to pass into the thread. - tmp_queue_evts.push(queue_evt.try_clone().map_err(|e| { - error!("failed to clone queue EventFd: {}", e); - ActivateError::BadActivate - })?); - } - self.queue_evts = Some(tmp_queue_evts); let mut handler = VsockEpollHandler { mem, @@ -473,13 +443,13 @@ where backend: self.backend.clone(), }; - let paused = self.paused.clone(); - let paused_sync = self.paused_sync.clone(); + let paused = self.common.paused.clone(); + let paused_sync = self.common.paused_sync.clone(); let mut epoll_threads = Vec::new(); thread::Builder::new() .name("virtio_vsock".to_string()) .spawn(move || { - if let Err(e) = handler.run(paused, paused_sync) { + if let Err(e) = handler.run(paused, paused_sync.unwrap()) { error!("Error running worker: {:?}", e); } }) @@ -489,27 +459,13 @@ where ActivateError::BadActivate })?; - self.epoll_threads = Some(epoll_threads); + self.common.epoll_threads = Some(epoll_threads); Ok(()) } fn reset(&mut self) -> Option<(Arc, Vec)> { - // We first must resume the virtio thread if it was paused. - if self.pause_evt.take().is_some() { - self.resume().ok()?; - } - - if let Some(kill_evt) = self.kill_evt.take() { - // Ignore the result because there is nothing we can do about it. - let _ = kill_evt.write(1); - } - - // Return the interrupt and queue EventFDs - Some(( - self.interrupt_cb.take().unwrap(), - self.queue_evts.take().unwrap(), - )) + self.common.reset() } fn shutdown(&mut self) { @@ -517,7 +473,18 @@ where } } -virtio_pausable!(Vsock, T: 'static + VsockBackend + Sync); +impl Pausable for Vsock +where + B: VsockBackend + Sync + 'static, +{ + fn pause(&mut self) -> result::Result<(), MigratableError> { + self.common.pause() + } + + fn resume(&mut self) -> result::Result<(), MigratableError> { + self.common.resume() + } +} impl Snapshottable for Vsock where @@ -571,6 +538,7 @@ mod tests { use super::super::*; use super::*; use crate::vsock::device::{BACKEND_EVENT, EVT_QUEUE_EVENT, RX_QUEUE_EVENT, TX_QUEUE_EVENT}; + use libc::EFD_NONBLOCK; #[test] fn test_virtio_device() {