mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-12-23 06:05:21 +00:00
virtio-devices: vhost_user: Make vhost-user handle optional
This anticipates the need for creating a new Blk, Fs or Net object without having performed the connection with the vhost-user backend yet. Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
parent
06729bb3ba
commit
a738808604
@ -53,7 +53,7 @@ impl VhostUserMasterReqHandler for SlaveReqHandler {}
|
|||||||
pub struct Blk {
|
pub struct Blk {
|
||||||
common: VirtioCommon,
|
common: VirtioCommon,
|
||||||
id: String,
|
id: String,
|
||||||
vu: Arc<Mutex<VhostUserHandle>>,
|
vu: Option<Arc<Mutex<VhostUserHandle>>>,
|
||||||
config: VirtioBlockConfig,
|
config: VirtioBlockConfig,
|
||||||
guest_memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
guest_memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
||||||
acked_protocol_features: u64,
|
acked_protocol_features: u64,
|
||||||
@ -140,7 +140,7 @@ impl Blk {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
id,
|
id,
|
||||||
vu: Arc::new(Mutex::new(vu)),
|
vu: Some(Arc::new(Mutex::new(vu))),
|
||||||
config,
|
config,
|
||||||
guest_memory: None,
|
guest_memory: None,
|
||||||
acked_protocol_features,
|
acked_protocol_features,
|
||||||
@ -211,8 +211,8 @@ impl VirtioDevice for Blk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.config.writeback = data[0];
|
self.config.writeback = data[0];
|
||||||
if let Err(e) = self
|
if let Some(vu) = &self.vu {
|
||||||
.vu
|
if let Err(e) = vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.socket_handle()
|
.socket_handle()
|
||||||
@ -222,6 +222,7 @@ impl VirtioDevice for Blk {
|
|||||||
error!("Failed setting vhost-user-blk configuration: {:?}", e);
|
error!("Failed setting vhost-user-blk configuration: {:?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn activate(
|
fn activate(
|
||||||
&mut self,
|
&mut self,
|
||||||
@ -249,8 +250,12 @@ impl VirtioDevice for Blk {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
self.vu
|
if self.vu.is_none() {
|
||||||
.lock()
|
error!("Missing vhost-user handle");
|
||||||
|
return Err(ActivateError::BadActivate);
|
||||||
|
}
|
||||||
|
let vu = self.vu.as_ref().unwrap();
|
||||||
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.setup_vhost_user(
|
.setup_vhost_user(
|
||||||
&mem.memory(),
|
&mem.memory(),
|
||||||
@ -268,7 +273,7 @@ impl VirtioDevice for Blk {
|
|||||||
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
||||||
|
|
||||||
let mut handler: VhostUserEpollHandler<SlaveReqHandler> = VhostUserEpollHandler {
|
let mut handler: VhostUserEpollHandler<SlaveReqHandler> = VhostUserEpollHandler {
|
||||||
vu: self.vu.clone(),
|
vu: vu.clone(),
|
||||||
mem,
|
mem,
|
||||||
kill_evt,
|
kill_evt,
|
||||||
pause_evt,
|
pause_evt,
|
||||||
@ -308,8 +313,8 @@ impl VirtioDevice for Blk {
|
|||||||
self.common.resume().ok()?;
|
self.common.resume().ok()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = self
|
if let Some(vu) = &self.vu {
|
||||||
.vu
|
if let Err(e) = vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.reset_vhost_user(self.common.queue_sizes.len())
|
.reset_vhost_user(self.common.queue_sizes.len())
|
||||||
@ -317,6 +322,7 @@ impl VirtioDevice for Blk {
|
|||||||
error!("Failed to reset vhost-user daemon: {:?}", e);
|
error!("Failed to reset vhost-user daemon: {:?}", e);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(kill_evt) = self.common.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.
|
// Ignore the result because there is nothing we can do about it.
|
||||||
@ -330,41 +336,46 @@ impl VirtioDevice for Blk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn shutdown(&mut self) {
|
fn shutdown(&mut self) {
|
||||||
let _ = unsafe { libc::close(self.vu.lock().unwrap().socket_handle().as_raw_fd()) };
|
if let Some(vu) = &self.vu {
|
||||||
|
let _ = unsafe { libc::close(vu.lock().unwrap().socket_handle().as_raw_fd()) };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_memory_region(
|
fn add_memory_region(
|
||||||
&mut self,
|
&mut self,
|
||||||
region: &Arc<GuestRegionMmap>,
|
region: &Arc<GuestRegionMmap>,
|
||||||
) -> std::result::Result<(), crate::Error> {
|
) -> std::result::Result<(), crate::Error> {
|
||||||
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() != 0
|
if let Some(vu) = &self.vu {
|
||||||
|
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits()
|
||||||
|
!= 0
|
||||||
{
|
{
|
||||||
self.vu
|
return vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.add_memory_region(region)
|
.add_memory_region(region)
|
||||||
.map_err(crate::Error::VhostUserAddMemoryRegion)
|
.map_err(crate::Error::VhostUserAddMemoryRegion);
|
||||||
} else if let Some(guest_memory) = &self.guest_memory {
|
} else if let Some(guest_memory) = &self.guest_memory {
|
||||||
self.vu
|
return vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.update_mem_table(guest_memory.memory().deref())
|
.update_mem_table(guest_memory.memory().deref())
|
||||||
.map_err(crate::Error::VhostUserUpdateMemory)
|
.map_err(crate::Error::VhostUserUpdateMemory);
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pausable for Blk {
|
impl Pausable for Blk {
|
||||||
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
||||||
self.vu
|
if let Some(vu) = &self.vu {
|
||||||
.lock()
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.pause_vhost_user(self.vu_num_queues)
|
.pause_vhost_user(self.vu_num_queues)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
MigratableError::Pause(anyhow!("Error pausing vhost-user-blk backend: {:?}", e))
|
MigratableError::Pause(anyhow!("Error pausing vhost-user-blk backend: {:?}", e))
|
||||||
})?;
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
self.common.pause()
|
self.common.pause()
|
||||||
}
|
}
|
||||||
@ -376,13 +387,19 @@ impl Pausable for Blk {
|
|||||||
epoll_thread.thread().unpark();
|
epoll_thread.thread().unpark();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.vu
|
if let Some(vu) = &self.vu {
|
||||||
.lock()
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.resume_vhost_user(self.vu_num_queues)
|
.resume_vhost_user(self.vu_num_queues)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
MigratableError::Resume(anyhow!("Error resuming vhost-user-blk backend: {:?}", e))
|
MigratableError::Resume(anyhow!(
|
||||||
|
"Error resuming vhost-user-blk backend: {:?}",
|
||||||
|
e
|
||||||
|
))
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,10 +421,10 @@ impl Transportable for Blk {}
|
|||||||
|
|
||||||
impl Migratable for Blk {
|
impl Migratable for Blk {
|
||||||
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
||||||
|
if let Some(vu) = &self.vu {
|
||||||
if let Some(guest_memory) = &self.guest_memory {
|
if let Some(guest_memory) = &self.guest_memory {
|
||||||
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
||||||
self.vu
|
vu.lock()
|
||||||
.lock()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.start_dirty_log(last_ram_addr)
|
.start_dirty_log(last_ram_addr)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -421,25 +438,29 @@ impl Migratable for Blk {
|
|||||||
"Missing guest memory"
|
"Missing guest memory"
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
||||||
self.vu.lock().unwrap().stop_dirty_log().map_err(|e| {
|
if let Some(vu) = &self.vu {
|
||||||
|
vu.lock().unwrap().stop_dirty_log().map_err(|e| {
|
||||||
MigratableError::StopDirtyLog(anyhow!(
|
MigratableError::StopDirtyLog(anyhow!(
|
||||||
"Error stopping migration for vhost-user-blk backend: {:?}",
|
"Error stopping migration for vhost-user-blk backend: {:?}",
|
||||||
e
|
e
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
||||||
|
if let Some(vu) = &self.vu {
|
||||||
if let Some(guest_memory) = &self.guest_memory {
|
if let Some(guest_memory) = &self.guest_memory {
|
||||||
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
||||||
self.vu
|
vu.lock().unwrap().dirty_log(last_ram_addr).map_err(|e| {
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.dirty_log(last_ram_addr)
|
|
||||||
.map_err(|e| {
|
|
||||||
MigratableError::DirtyLog(anyhow!(
|
MigratableError::DirtyLog(anyhow!(
|
||||||
"Error retrieving dirty ranges from vhost-user-blk backend: {:?}",
|
"Error retrieving dirty ranges from vhost-user-blk backend: {:?}",
|
||||||
e
|
e
|
||||||
@ -448,5 +469,8 @@ impl Migratable for Blk {
|
|||||||
} else {
|
} else {
|
||||||
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
|
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Ok(MemoryRangeTable::default())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,7 +298,7 @@ unsafe impl ByteValued for VirtioFsConfig {}
|
|||||||
pub struct Fs {
|
pub struct Fs {
|
||||||
common: VirtioCommon,
|
common: VirtioCommon,
|
||||||
id: String,
|
id: String,
|
||||||
vu: Arc<Mutex<VhostUserHandle>>,
|
vu: Option<Arc<Mutex<VhostUserHandle>>>,
|
||||||
config: VirtioFsConfig,
|
config: VirtioFsConfig,
|
||||||
// Hold ownership of the memory that is allocated for the device
|
// Hold ownership of the memory that is allocated for the device
|
||||||
// which will be automatically dropped when the device is dropped
|
// which will be automatically dropped when the device is dropped
|
||||||
@ -388,7 +388,7 @@ impl Fs {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
id,
|
id,
|
||||||
vu: Arc::new(Mutex::new(vu)),
|
vu: Some(Arc::new(Mutex::new(vu))),
|
||||||
config,
|
config,
|
||||||
cache,
|
cache,
|
||||||
slave_req_support,
|
slave_req_support,
|
||||||
@ -493,8 +493,12 @@ impl VirtioDevice for Fs {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
self.vu
|
if self.vu.is_none() {
|
||||||
.lock()
|
error!("Missing vhost-user handle");
|
||||||
|
return Err(ActivateError::BadActivate);
|
||||||
|
}
|
||||||
|
let vu = self.vu.as_ref().unwrap();
|
||||||
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.setup_vhost_user(
|
.setup_vhost_user(
|
||||||
&mem.memory(),
|
&mem.memory(),
|
||||||
@ -511,7 +515,7 @@ impl VirtioDevice for Fs {
|
|||||||
// the backend as well as requests initiated by the backend.
|
// the backend as well as requests initiated by the backend.
|
||||||
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
||||||
let mut handler: VhostUserEpollHandler<SlaveReqHandler> = VhostUserEpollHandler {
|
let mut handler: VhostUserEpollHandler<SlaveReqHandler> = VhostUserEpollHandler {
|
||||||
vu: self.vu.clone(),
|
vu: vu.clone(),
|
||||||
mem,
|
mem,
|
||||||
kill_evt,
|
kill_evt,
|
||||||
pause_evt,
|
pause_evt,
|
||||||
@ -558,8 +562,8 @@ impl VirtioDevice for Fs {
|
|||||||
self.common.resume().ok()?;
|
self.common.resume().ok()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = self
|
if let Some(vu) = &self.vu {
|
||||||
.vu
|
if let Err(e) = vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.reset_vhost_user(self.common.queue_sizes.len())
|
.reset_vhost_user(self.common.queue_sizes.len())
|
||||||
@ -567,6 +571,7 @@ impl VirtioDevice for Fs {
|
|||||||
error!("Failed to reset vhost-user daemon: {:?}", e);
|
error!("Failed to reset vhost-user daemon: {:?}", e);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(kill_evt) = self.common.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.
|
// Ignore the result because there is nothing we can do about it.
|
||||||
@ -580,7 +585,9 @@ impl VirtioDevice for Fs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn shutdown(&mut self) {
|
fn shutdown(&mut self) {
|
||||||
let _ = unsafe { libc::close(self.vu.lock().unwrap().socket_handle().as_raw_fd()) };
|
if let Some(vu) = &self.vu {
|
||||||
|
let _ = unsafe { libc::close(vu.lock().unwrap().socket_handle().as_raw_fd()) };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_shm_regions(&self) -> Option<VirtioSharedMemoryList> {
|
fn get_shm_regions(&self) -> Option<VirtioSharedMemoryList> {
|
||||||
@ -603,23 +610,25 @@ impl VirtioDevice for Fs {
|
|||||||
&mut self,
|
&mut self,
|
||||||
region: &Arc<GuestRegionMmap>,
|
region: &Arc<GuestRegionMmap>,
|
||||||
) -> std::result::Result<(), crate::Error> {
|
) -> std::result::Result<(), crate::Error> {
|
||||||
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() != 0
|
if let Some(vu) = &self.vu {
|
||||||
|
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits()
|
||||||
|
!= 0
|
||||||
{
|
{
|
||||||
self.vu
|
return vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.add_memory_region(region)
|
.add_memory_region(region)
|
||||||
.map_err(crate::Error::VhostUserAddMemoryRegion)
|
.map_err(crate::Error::VhostUserAddMemoryRegion);
|
||||||
} else if let Some(guest_memory) = &self.guest_memory {
|
} else if let Some(guest_memory) = &self.guest_memory {
|
||||||
self.vu
|
return vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.update_mem_table(guest_memory.memory().deref())
|
.update_mem_table(guest_memory.memory().deref())
|
||||||
.map_err(crate::Error::VhostUserUpdateMemory)
|
.map_err(crate::Error::VhostUserUpdateMemory);
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn userspace_mappings(&self) -> Vec<UserspaceMapping> {
|
fn userspace_mappings(&self) -> Vec<UserspaceMapping> {
|
||||||
let mut mappings = Vec::new();
|
let mut mappings = Vec::new();
|
||||||
@ -639,13 +648,14 @@ impl VirtioDevice for Fs {
|
|||||||
|
|
||||||
impl Pausable for Fs {
|
impl Pausable for Fs {
|
||||||
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
||||||
self.vu
|
if let Some(vu) = &self.vu {
|
||||||
.lock()
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.pause_vhost_user(self.vu_num_queues)
|
.pause_vhost_user(self.vu_num_queues)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
MigratableError::Pause(anyhow!("Error pausing vhost-user-fs backend: {:?}", e))
|
MigratableError::Pause(anyhow!("Error pausing vhost-user-fs backend: {:?}", e))
|
||||||
})?;
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
self.common.pause()
|
self.common.pause()
|
||||||
}
|
}
|
||||||
@ -657,13 +667,19 @@ impl Pausable for Fs {
|
|||||||
epoll_thread.thread().unpark();
|
epoll_thread.thread().unpark();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.vu
|
if let Some(vu) = &self.vu {
|
||||||
.lock()
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.resume_vhost_user(self.vu_num_queues)
|
.resume_vhost_user(self.vu_num_queues)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
MigratableError::Resume(anyhow!("Error resuming vhost-user-fs backend: {:?}", e))
|
MigratableError::Resume(anyhow!(
|
||||||
|
"Error resuming vhost-user-fs backend: {:?}",
|
||||||
|
e
|
||||||
|
))
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -685,10 +701,10 @@ impl Transportable for Fs {}
|
|||||||
|
|
||||||
impl Migratable for Fs {
|
impl Migratable for Fs {
|
||||||
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
||||||
|
if let Some(vu) = &self.vu {
|
||||||
if let Some(guest_memory) = &self.guest_memory {
|
if let Some(guest_memory) = &self.guest_memory {
|
||||||
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
||||||
self.vu
|
vu.lock()
|
||||||
.lock()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.start_dirty_log(last_ram_addr)
|
.start_dirty_log(last_ram_addr)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -702,25 +718,29 @@ impl Migratable for Fs {
|
|||||||
"Missing guest memory"
|
"Missing guest memory"
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
||||||
self.vu.lock().unwrap().stop_dirty_log().map_err(|e| {
|
if let Some(vu) = &self.vu {
|
||||||
|
vu.lock().unwrap().stop_dirty_log().map_err(|e| {
|
||||||
MigratableError::StopDirtyLog(anyhow!(
|
MigratableError::StopDirtyLog(anyhow!(
|
||||||
"Error stopping migration for vhost-user-fs backend: {:?}",
|
"Error stopping migration for vhost-user-fs backend: {:?}",
|
||||||
e
|
e
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
||||||
|
if let Some(vu) = &self.vu {
|
||||||
if let Some(guest_memory) = &self.guest_memory {
|
if let Some(guest_memory) = &self.guest_memory {
|
||||||
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
||||||
self.vu
|
vu.lock().unwrap().dirty_log(last_ram_addr).map_err(|e| {
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.dirty_log(last_ram_addr)
|
|
||||||
.map_err(|e| {
|
|
||||||
MigratableError::DirtyLog(anyhow!(
|
MigratableError::DirtyLog(anyhow!(
|
||||||
"Error retrieving dirty ranges from vhost-user-fs backend: {:?}",
|
"Error retrieving dirty ranges from vhost-user-fs backend: {:?}",
|
||||||
e
|
e
|
||||||
@ -729,5 +749,8 @@ impl Migratable for Fs {
|
|||||||
} else {
|
} else {
|
||||||
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
|
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Ok(MemoryRangeTable::default())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +106,7 @@ impl EpollHelperHandler for NetCtrlEpollHandler {
|
|||||||
pub struct Net {
|
pub struct Net {
|
||||||
common: VirtioCommon,
|
common: VirtioCommon,
|
||||||
id: String,
|
id: String,
|
||||||
vu: Arc<Mutex<VhostUserHandle>>,
|
vu: Option<Arc<Mutex<VhostUserHandle>>>,
|
||||||
config: VirtioNetConfig,
|
config: VirtioNetConfig,
|
||||||
guest_memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
guest_memory: Option<GuestMemoryAtomic<GuestMemoryMmap>>,
|
||||||
acked_protocol_features: u64,
|
acked_protocol_features: u64,
|
||||||
@ -198,7 +198,7 @@ impl Net {
|
|||||||
min_queues: DEFAULT_QUEUE_NUMBER as u16,
|
min_queues: DEFAULT_QUEUE_NUMBER as u16,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
vu: Arc::new(Mutex::new(vu)),
|
vu: Some(Arc::new(Mutex::new(vu))),
|
||||||
config,
|
config,
|
||||||
guest_memory: None,
|
guest_memory: None,
|
||||||
acked_protocol_features,
|
acked_protocol_features,
|
||||||
@ -327,8 +327,12 @@ impl VirtioDevice for Net {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
self.vu
|
if self.vu.is_none() {
|
||||||
.lock()
|
error!("Missing vhost-user handle");
|
||||||
|
return Err(ActivateError::BadActivate);
|
||||||
|
}
|
||||||
|
let vu = self.vu.as_ref().unwrap();
|
||||||
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.setup_vhost_user(
|
.setup_vhost_user(
|
||||||
&mem.memory(),
|
&mem.memory(),
|
||||||
@ -346,7 +350,7 @@ impl VirtioDevice for Net {
|
|||||||
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
||||||
|
|
||||||
let mut handler: VhostUserEpollHandler<SlaveReqHandler> = VhostUserEpollHandler {
|
let mut handler: VhostUserEpollHandler<SlaveReqHandler> = VhostUserEpollHandler {
|
||||||
vu: self.vu.clone(),
|
vu: vu.clone(),
|
||||||
mem,
|
mem,
|
||||||
kill_evt,
|
kill_evt,
|
||||||
pause_evt,
|
pause_evt,
|
||||||
@ -386,8 +390,8 @@ impl VirtioDevice for Net {
|
|||||||
self.common.resume().ok()?;
|
self.common.resume().ok()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = self
|
if let Some(vu) = &self.vu {
|
||||||
.vu
|
if let Err(e) = vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.reset_vhost_user(self.common.queue_sizes.len())
|
.reset_vhost_user(self.common.queue_sizes.len())
|
||||||
@ -395,6 +399,7 @@ impl VirtioDevice for Net {
|
|||||||
error!("Failed to reset vhost-user daemon: {:?}", e);
|
error!("Failed to reset vhost-user daemon: {:?}", e);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(kill_evt) = self.common.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.
|
// Ignore the result because there is nothing we can do about it.
|
||||||
@ -408,7 +413,9 @@ impl VirtioDevice for Net {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn shutdown(&mut self) {
|
fn shutdown(&mut self) {
|
||||||
let _ = unsafe { libc::close(self.vu.lock().unwrap().socket_handle().as_raw_fd()) };
|
if let Some(vu) = &self.vu {
|
||||||
|
let _ = unsafe { libc::close(vu.lock().unwrap().socket_handle().as_raw_fd()) };
|
||||||
|
}
|
||||||
|
|
||||||
// Remove socket path if needed
|
// Remove socket path if needed
|
||||||
if self.server {
|
if self.server {
|
||||||
@ -420,34 +427,37 @@ impl VirtioDevice for Net {
|
|||||||
&mut self,
|
&mut self,
|
||||||
region: &Arc<GuestRegionMmap>,
|
region: &Arc<GuestRegionMmap>,
|
||||||
) -> std::result::Result<(), crate::Error> {
|
) -> std::result::Result<(), crate::Error> {
|
||||||
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits() != 0
|
if let Some(vu) = &self.vu {
|
||||||
|
if self.acked_protocol_features & VhostUserProtocolFeatures::CONFIGURE_MEM_SLOTS.bits()
|
||||||
|
!= 0
|
||||||
{
|
{
|
||||||
self.vu
|
return vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.add_memory_region(region)
|
.add_memory_region(region)
|
||||||
.map_err(crate::Error::VhostUserAddMemoryRegion)
|
.map_err(crate::Error::VhostUserAddMemoryRegion);
|
||||||
} else if let Some(guest_memory) = &self.guest_memory {
|
} else if let Some(guest_memory) = &self.guest_memory {
|
||||||
self.vu
|
return vu
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.update_mem_table(guest_memory.memory().deref())
|
.update_mem_table(guest_memory.memory().deref())
|
||||||
.map_err(crate::Error::VhostUserUpdateMemory)
|
.map_err(crate::Error::VhostUserUpdateMemory);
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pausable for Net {
|
impl Pausable for Net {
|
||||||
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
||||||
self.vu
|
if let Some(vu) = &self.vu {
|
||||||
.lock()
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.pause_vhost_user(self.vu_num_queues)
|
.pause_vhost_user(self.vu_num_queues)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
MigratableError::Pause(anyhow!("Error pausing vhost-user-net backend: {:?}", e))
|
MigratableError::Pause(anyhow!("Error pausing vhost-user-net backend: {:?}", e))
|
||||||
})?;
|
})?;
|
||||||
|
}
|
||||||
|
|
||||||
self.common.pause()
|
self.common.pause()
|
||||||
}
|
}
|
||||||
@ -463,13 +473,19 @@ impl Pausable for Net {
|
|||||||
ctrl_queue_epoll_thread.thread().unpark();
|
ctrl_queue_epoll_thread.thread().unpark();
|
||||||
}
|
}
|
||||||
|
|
||||||
self.vu
|
if let Some(vu) = &self.vu {
|
||||||
.lock()
|
vu.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.resume_vhost_user(self.vu_num_queues)
|
.resume_vhost_user(self.vu_num_queues)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
MigratableError::Resume(anyhow!("Error resuming vhost-user-net backend: {:?}", e))
|
MigratableError::Resume(anyhow!(
|
||||||
|
"Error resuming vhost-user-net backend: {:?}",
|
||||||
|
e
|
||||||
|
))
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -491,10 +507,10 @@ impl Transportable for Net {}
|
|||||||
|
|
||||||
impl Migratable for Net {
|
impl Migratable for Net {
|
||||||
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
fn start_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
||||||
|
if let Some(vu) = &self.vu {
|
||||||
if let Some(guest_memory) = &self.guest_memory {
|
if let Some(guest_memory) = &self.guest_memory {
|
||||||
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
||||||
self.vu
|
vu.lock()
|
||||||
.lock()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.start_dirty_log(last_ram_addr)
|
.start_dirty_log(last_ram_addr)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -508,25 +524,29 @@ impl Migratable for Net {
|
|||||||
"Missing guest memory"
|
"Missing guest memory"
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
fn stop_dirty_log(&mut self) -> std::result::Result<(), MigratableError> {
|
||||||
self.vu.lock().unwrap().stop_dirty_log().map_err(|e| {
|
if let Some(vu) = &self.vu {
|
||||||
|
vu.lock().unwrap().stop_dirty_log().map_err(|e| {
|
||||||
MigratableError::StopDirtyLog(anyhow!(
|
MigratableError::StopDirtyLog(anyhow!(
|
||||||
"Error stopping migration for vhost-user-net backend: {:?}",
|
"Error stopping migration for vhost-user-net backend: {:?}",
|
||||||
e
|
e
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
fn dirty_log(&mut self) -> std::result::Result<MemoryRangeTable, MigratableError> {
|
||||||
|
if let Some(vu) = &self.vu {
|
||||||
if let Some(guest_memory) = &self.guest_memory {
|
if let Some(guest_memory) = &self.guest_memory {
|
||||||
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
let last_ram_addr = guest_memory.memory().last_addr().raw_value();
|
||||||
self.vu
|
vu.lock().unwrap().dirty_log(last_ram_addr).map_err(|e| {
|
||||||
.lock()
|
|
||||||
.unwrap()
|
|
||||||
.dirty_log(last_ram_addr)
|
|
||||||
.map_err(|e| {
|
|
||||||
MigratableError::DirtyLog(anyhow!(
|
MigratableError::DirtyLog(anyhow!(
|
||||||
"Error retrieving dirty ranges from vhost-user-net backend: {:?}",
|
"Error retrieving dirty ranges from vhost-user-net backend: {:?}",
|
||||||
e
|
e
|
||||||
@ -535,5 +555,8 @@ impl Migratable for Net {
|
|||||||
} else {
|
} else {
|
||||||
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
|
Err(MigratableError::DirtyLog(anyhow!("Missing guest memory")))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Ok(MemoryRangeTable::default())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user