diff --git a/virtio-devices/src/vhost_user/blk.rs b/virtio-devices/src/vhost_user/blk.rs index 2526b36f6..7e434dd9f 100644 --- a/virtio-devices/src/vhost_user/blk.rs +++ b/virtio-devices/src/vhost_user/blk.rs @@ -6,7 +6,6 @@ use std::sync::{Arc, Barrier, Mutex}; use std::{mem, result}; use block::VirtioBlockConfig; -use event_monitor::event; use log::{error, info}; use seccompiler::SeccompAction; use vhost::vhost_user::message::{ @@ -309,30 +308,7 @@ impl VirtioDevice for Blk { } fn reset(&mut self) -> Option> { - // We first must resume the virtio thread if it was paused. - if self.vu_common.virtio_common.pause_evt.take().is_some() { - self.vu_common.virtio_common.resume().ok()?; - } - - if let Some(vu) = &self.vu_common.vu - && let Err(e) = vu.lock().unwrap().reset_vhost_user() - { - error!( - "Failed to reset vhost-user daemon for socket {}: {e:?}", - self.vu_common.socket_path - ); - return None; - } - - if let Some(kill_evt) = self.vu_common.virtio_common.kill_evt.take() { - // Ignore the result because there is nothing we can do about it. - let _ = kill_evt.write(1); - } - - event!("virtio-device", "reset", "id", &self.id); - - // Return the interrupt - Some(self.vu_common.virtio_common.interrupt_cb.take().unwrap()) + self.vu_common.reset(&self.id) } fn shutdown(&mut self) { diff --git a/virtio-devices/src/vhost_user/fs.rs b/virtio-devices/src/vhost_user/fs.rs index c062edf19..60a60a86c 100644 --- a/virtio-devices/src/vhost_user/fs.rs +++ b/virtio-devices/src/vhost_user/fs.rs @@ -286,30 +286,7 @@ impl VirtioDevice for Fs { } fn reset(&mut self) -> Option> { - // We first must resume the virtio thread if it was paused. - if self.vu_common.virtio_common.pause_evt.take().is_some() { - self.vu_common.virtio_common.resume().ok()?; - } - - if let Some(vu) = &self.vu_common.vu - && let Err(e) = vu.lock().unwrap().reset_vhost_user() - { - error!( - "Failed to reset vhost-user daemon for socket {}: {e:?}", - self.vu_common.socket_path - ); - return None; - } - - if let Some(kill_evt) = self.vu_common.virtio_common.kill_evt.take() { - // Ignore the result because there is nothing we can do about it. - let _ = kill_evt.write(1); - } - - event!("virtio-device", "reset", "id", &self.id); - - // Return the interrupt - Some(self.vu_common.virtio_common.interrupt_cb.take().unwrap()) + self.vu_common.reset(&self.id) } fn shutdown(&mut self) { diff --git a/virtio-devices/src/vhost_user/generic_vhost_user.rs b/virtio-devices/src/vhost_user/generic_vhost_user.rs index dda891e91..0d2aaf682 100644 --- a/virtio-devices/src/vhost_user/generic_vhost_user.rs +++ b/virtio-devices/src/vhost_user/generic_vhost_user.rs @@ -309,30 +309,7 @@ impl VirtioDevice for GenericVhostUser { } fn reset(&mut self) -> Option> { - // We first must resume the virtio thread if it was paused. - if self.vu_common.virtio_common.pause_evt.take().is_some() { - self.vu_common.virtio_common.resume().ok()?; - } - - if let Some(vu) = &self.vu_common.vu - && let Err(e) = vu.lock().unwrap().reset_vhost_user() - { - error!( - "Failed to reset vhost-user daemon for socket {}: {e:?}", - self.vu_common.socket_path - ); - return None; - } - - if let Some(kill_evt) = self.vu_common.virtio_common.kill_evt.take() { - // Ignore the result because there is nothing we can do about it. - let _ = kill_evt.write(1); - } - - event!("virtio-device", "reset", "id", &self.id); - - // Return the interrupt - Some(self.vu_common.virtio_common.interrupt_cb.take().unwrap()) + self.vu_common.reset(&self.id) } fn shutdown(&mut self) { diff --git a/virtio-devices/src/vhost_user/mod.rs b/virtio-devices/src/vhost_user/mod.rs index abca12c05..9f2eea423 100644 --- a/virtio-devices/src/vhost_user/mod.rs +++ b/virtio-devices/src/vhost_user/mod.rs @@ -8,6 +8,7 @@ use std::sync::{Arc, Barrier, Mutex}; use std::{io, thread}; use anyhow::anyhow; +use event_monitor::event; use log::error; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -21,7 +22,7 @@ use vm_memory::guest_memory::Error as MmapError; use vm_memory::mmap::MmapRegionError; use vm_memory::{Address, GuestAddressSpace, GuestMemory, GuestMemoryAtomic}; use vm_migration::protocol::MemoryRangeTable; -use vm_migration::{MigratableError, Snapshot}; +use vm_migration::{MigratableError, Pausable, Snapshot}; use vmm_sys_util::eventfd::EventFd; use vu_common_ctrl::VhostUserHandle; @@ -427,6 +428,33 @@ impl VhostUserCommon { Ok(()) } + pub fn reset(&mut self, id: &str) -> Option> { + // We first must resume the virtio thread if it was paused. + if self.virtio_common.pause_evt.take().is_some() { + self.virtio_common.resume().ok()?; + } + + if let Some(vu) = &self.vu + && let Err(e) = vu.lock().unwrap().reset_vhost_user() + { + error!( + "Failed to reset vhost-user daemon for socket {}: {e:?}", + self.socket_path + ); + return None; + } + + if let Some(kill_evt) = self.virtio_common.kill_evt.take() { + // Ignore the result because there is nothing we can do about it. + let _ = kill_evt.write(1); + } + + event!("virtio-device", "reset", "id", id); + + // Return the interrupt + Some(self.virtio_common.interrupt_cb.take().unwrap()) + } + pub fn shutdown(&mut self) { // Signal the epoll thread to exit, unpause it (it may be parked // if the VM was paused for migration), then wait for it to finish. diff --git a/virtio-devices/src/vhost_user/net.rs b/virtio-devices/src/vhost_user/net.rs index e5f9eda7d..8563a2124 100644 --- a/virtio-devices/src/vhost_user/net.rs +++ b/virtio-devices/src/vhost_user/net.rs @@ -5,7 +5,6 @@ use std::sync::atomic::AtomicBool; use std::sync::{Arc, Barrier, Mutex}; use std::{result, thread}; -use event_monitor::event; use log::{error, info}; use net_util::{CtrlQueue, MacAddr, VirtioNetConfig, build_net_config_space}; use seccompiler::SeccompAction; @@ -366,30 +365,7 @@ impl VirtioDevice for Net { } fn reset(&mut self) -> Option> { - // We first must resume the virtio thread if it was paused. - if self.vu_common.virtio_common.pause_evt.take().is_some() { - self.vu_common.virtio_common.resume().ok()?; - } - - if let Some(vu) = &self.vu_common.vu - && let Err(e) = vu.lock().unwrap().reset_vhost_user() - { - error!( - "Failed to reset vhost-user daemon for socket {}: {e:?}", - self.vu_common.socket_path - ); - return None; - } - - if let Some(kill_evt) = self.vu_common.virtio_common.kill_evt.take() { - // Ignore the result because there is nothing we can do about it. - let _ = kill_evt.write(1); - } - - event!("virtio-device", "reset", "id", &self.id); - - // Return the interrupt - Some(self.vu_common.virtio_common.interrupt_cb.take().unwrap()) + self.vu_common.reset(&self.id) } fn shutdown(&mut self) {