vmm: vm: Carry information from hotplugged PCI device

Pass from the device manager to the calling code the information about
the PCI device that has just been hotplugged.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-06-11 17:27:46 +02:00 committed by Rob Bradford
parent f08e9b6a73
commit 3316348d4c

View File

@ -32,7 +32,9 @@ use crate::cpu;
use crate::device_manager::{self, get_win_size, Console, DeviceManager, DeviceManagerError}; use crate::device_manager::{self, get_win_size, Console, DeviceManager, DeviceManagerError};
use crate::memory_manager::{Error as MemoryManagerError, MemoryManager}; use crate::memory_manager::{Error as MemoryManagerError, MemoryManager};
use crate::migration::{url_to_path, vm_config_from_snapshot, VM_SNAPSHOT_FILE}; use crate::migration::{url_to_path, vm_config_from_snapshot, VM_SNAPSHOT_FILE};
use crate::{CPU_MANAGER_SNAPSHOT_ID, DEVICE_MANAGER_SNAPSHOT_ID, MEMORY_MANAGER_SNAPSHOT_ID}; use crate::{
PciDeviceInfo, CPU_MANAGER_SNAPSHOT_ID, DEVICE_MANAGER_SNAPSHOT_ID, MEMORY_MANAGER_SNAPSHOT_ID,
};
use anyhow::anyhow; use anyhow::anyhow;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
use arch::BootProtocol; use arch::BootProtocol;
@ -758,37 +760,38 @@ impl Vm {
Ok(()) Ok(())
} }
pub fn add_device(&mut self, mut _device_cfg: DeviceConfig) -> Result<()> { #[cfg(not(feature = "pci_support"))]
if cfg!(feature = "pci_support") { pub fn add_device(&mut self, mut _device_cfg: DeviceConfig) -> Result<PciDeviceInfo> {
#[cfg(feature = "pci_support")] Err(Error::NoPciSupport)
{ }
self.device_manager
.lock()
.unwrap()
.add_device(&mut _device_cfg)
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to #[cfg(feature = "pci_support")]
// ensure the device would be created in case of a reboot. pub fn add_device(&mut self, mut _device_cfg: DeviceConfig) -> Result<PciDeviceInfo> {
{ let pci_device_info = self
let mut config = self.config.lock().unwrap(); .device_manager
if let Some(devices) = config.devices.as_mut() { .lock()
devices.push(_device_cfg); .unwrap()
} else { .add_device(&mut _device_cfg)
config.devices = Some(vec![_device_cfg]); .map_err(Error::DeviceManager)?;
}
}
self.device_manager // Update VmConfig by adding the new device. This is important to
.lock() // ensure the device would be created in case of a reboot.
.unwrap() {
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED) let mut config = self.config.lock().unwrap();
.map_err(Error::DeviceManager)?; if let Some(devices) = config.devices.as_mut() {
devices.push(_device_cfg);
} else {
config.devices = Some(vec![_device_cfg]);
} }
Ok(())
} else {
Err(Error::NoPciSupport)
} }
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
Ok(pci_device_info)
} }
pub fn remove_device(&mut self, _id: String) -> Result<()> { pub fn remove_device(&mut self, _id: String) -> Result<()> {
@ -846,169 +849,174 @@ impl Vm {
} }
} }
pub fn add_disk(&mut self, mut _disk_cfg: DiskConfig) -> Result<()> { #[cfg(not(feature = "pci_support"))]
if cfg!(feature = "pci_support") { pub fn add_disk(&mut self, mut _disk_cfg: DiskConfig) -> Result<PciDeviceInfo> {
#[cfg(feature = "pci_support")] Err(Error::NoPciSupport)
{
self.device_manager
.lock()
.unwrap()
.add_disk(&mut _disk_cfg)
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot.
{
let mut config = self.config.lock().unwrap();
if let Some(disks) = config.disks.as_mut() {
disks.push(_disk_cfg);
} else {
config.disks = Some(vec![_disk_cfg]);
}
}
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
}
Ok(())
} else {
Err(Error::NoPciSupport)
}
} }
pub fn add_fs(&mut self, mut _fs_cfg: FsConfig) -> Result<()> { #[cfg(feature = "pci_support")]
if cfg!(feature = "pci_support") { pub fn add_disk(&mut self, mut _disk_cfg: DiskConfig) -> Result<PciDeviceInfo> {
#[cfg(feature = "pci_support")] let pci_device_info = self
{ .device_manager
self.device_manager .lock()
.lock() .unwrap()
.unwrap() .add_disk(&mut _disk_cfg)
.add_fs(&mut _fs_cfg) .map_err(Error::DeviceManager)?;
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to // Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(fs_config) = config.fs.as_mut() { if let Some(disks) = config.disks.as_mut() {
fs_config.push(_fs_cfg); disks.push(_disk_cfg);
} else { } else {
config.fs = Some(vec![_fs_cfg]); config.disks = Some(vec![_disk_cfg]);
}
}
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
} }
Ok(())
} else {
Err(Error::NoPciSupport)
} }
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
Ok(pci_device_info)
} }
pub fn add_pmem(&mut self, mut _pmem_cfg: PmemConfig) -> Result<()> { #[cfg(not(feature = "pci_support"))]
if cfg!(feature = "pci_support") { pub fn add_fs(&mut self, mut _fs_cfg: FsConfig) -> Result<PciDeviceInfo> {
#[cfg(feature = "pci_support")] Err(Error::NoPciSupport)
{
self.device_manager
.lock()
.unwrap()
.add_pmem(&mut _pmem_cfg)
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot.
{
let mut config = self.config.lock().unwrap();
if let Some(pmem) = config.pmem.as_mut() {
pmem.push(_pmem_cfg);
} else {
config.pmem = Some(vec![_pmem_cfg]);
}
}
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
}
Ok(())
} else {
Err(Error::NoPciSupport)
}
} }
pub fn add_net(&mut self, mut _net_cfg: NetConfig) -> Result<()> { #[cfg(feature = "pci_support")]
if cfg!(feature = "pci_support") { pub fn add_fs(&mut self, mut _fs_cfg: FsConfig) -> Result<PciDeviceInfo> {
#[cfg(feature = "pci_support")] let pci_device_info = self
{ .device_manager
self.device_manager .lock()
.lock() .unwrap()
.unwrap() .add_fs(&mut _fs_cfg)
.add_net(&mut _net_cfg) .map_err(Error::DeviceManager)?;
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to // Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
if let Some(net) = config.net.as_mut() { if let Some(fs_config) = config.fs.as_mut() {
net.push(_net_cfg); fs_config.push(_fs_cfg);
} else { } else {
config.net = Some(vec![_net_cfg]); config.fs = Some(vec![_fs_cfg]);
}
}
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
} }
Ok(())
} else {
Err(Error::NoPciSupport)
} }
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
Ok(pci_device_info)
} }
pub fn add_vsock(&mut self, mut _vsock_cfg: VsockConfig) -> Result<()> { #[cfg(not(feature = "pci_support"))]
if cfg!(feature = "pci_support") { pub fn add_pmem(&mut self, mut _pmem_cfg: PmemConfig) -> Result<PciDeviceInfo> {
#[cfg(feature = "pci_support")] Err(Error::NoPciSupport)
{ }
if self.config.lock().unwrap().vsock.is_some() {
return Err(Error::TooManyVsockDevices);
}
self.device_manager #[cfg(feature = "pci_support")]
.lock() pub fn add_pmem(&mut self, mut _pmem_cfg: PmemConfig) -> Result<PciDeviceInfo> {
.unwrap() let pci_device_info = self
.add_vsock(&mut _vsock_cfg) .device_manager
.map_err(Error::DeviceManager)?; .lock()
.unwrap()
.add_pmem(&mut _pmem_cfg)
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to // Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot. // ensure the device would be created in case of a reboot.
{ {
let mut config = self.config.lock().unwrap(); let mut config = self.config.lock().unwrap();
config.vsock = Some(_vsock_cfg); if let Some(pmem) = config.pmem.as_mut() {
} pmem.push(_pmem_cfg);
} else {
self.device_manager config.pmem = Some(vec![_pmem_cfg]);
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
} }
Ok(())
} else {
Err(Error::NoPciSupport)
} }
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
Ok(pci_device_info)
}
#[cfg(not(feature = "pci_support"))]
pub fn add_net(&mut self, mut _net_cfg: NetConfig) -> Result<PciDeviceInfo> {
Err(Error::NoPciSupport)
}
#[cfg(feature = "pci_support")]
pub fn add_net(&mut self, mut _net_cfg: NetConfig) -> Result<PciDeviceInfo> {
let pci_device_info = self
.device_manager
.lock()
.unwrap()
.add_net(&mut _net_cfg)
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot.
{
let mut config = self.config.lock().unwrap();
if let Some(net) = config.net.as_mut() {
net.push(_net_cfg);
} else {
config.net = Some(vec![_net_cfg]);
}
}
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
Ok(pci_device_info)
}
#[cfg(not(feature = "pci_support"))]
pub fn add_vsock(&mut self, mut _vsock_cfg: VsockConfig) -> Result<PciDeviceInfo> {
Err(Error::NoPciSupport)
}
#[cfg(feature = "pci_support")]
pub fn add_vsock(&mut self, mut _vsock_cfg: VsockConfig) -> Result<PciDeviceInfo> {
if self.config.lock().unwrap().vsock.is_some() {
return Err(Error::TooManyVsockDevices);
}
let pci_device_info = self
.device_manager
.lock()
.unwrap()
.add_vsock(&mut _vsock_cfg)
.map_err(Error::DeviceManager)?;
// Update VmConfig by adding the new device. This is important to
// ensure the device would be created in case of a reboot.
{
let mut config = self.config.lock().unwrap();
config.vsock = Some(_vsock_cfg);
}
self.device_manager
.lock()
.unwrap()
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
.map_err(Error::DeviceManager)?;
Ok(pci_device_info)
} }
fn os_signal_handler(signals: Signals, console_input_clone: Arc<Console>, on_tty: bool) { fn os_signal_handler(signals: Signals, console_input_clone: Arc<Console>, on_tty: bool) {