vmm: Make the virtio identifier mandatory

Because we know we will need every virtio device to be identified with a
unique id, we can simplify the code by making the identifier mandatory.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2020-04-27 14:38:24 +02:00
committed by Rob Bradford
parent 14350f5de4
commit 9d84ef5073

View File

@@ -567,7 +567,7 @@ pub struct DeviceManager {
memory_manager: Arc<Mutex<MemoryManager>>, memory_manager: Arc<Mutex<MemoryManager>>,
// The virtio devices on the system // The virtio devices on the system
virtio_devices: Vec<(VirtioDeviceArc, bool, Option<String>)>, virtio_devices: Vec<(VirtioDeviceArc, bool, String)>,
// List of bus devices // List of bus devices
// Let the DeviceManager keep strong references to the BusDevice devices. // Let the DeviceManager keep strong references to the BusDevice devices.
@@ -627,7 +627,7 @@ impl DeviceManager {
reset_evt: &EventFd, reset_evt: &EventFd,
vmm_path: PathBuf, vmm_path: PathBuf,
) -> DeviceManagerResult<Arc<Mutex<Self>>> { ) -> DeviceManagerResult<Arc<Mutex<Self>>> {
let mut virtio_devices: Vec<(VirtioDeviceArc, bool, Option<String>)> = Vec::new(); let mut virtio_devices: Vec<(VirtioDeviceArc, bool, String)> = Vec::new();
let migratable_devices: Vec<(String, Arc<Mutex<dyn Migratable>>)> = Vec::new(); let migratable_devices: Vec<(String, Arc<Mutex<dyn Migratable>>)> = Vec::new();
let mut bus_devices: Vec<Arc<Mutex<dyn BusDevice>>> = Vec::new(); let mut bus_devices: Vec<Arc<Mutex<dyn BusDevice>>> = Vec::new();
@@ -778,7 +778,7 @@ impl DeviceManager {
#[allow(unused_variables)] #[allow(unused_variables)]
fn add_pci_devices( fn add_pci_devices(
&mut self, &mut self,
virtio_devices: Vec<(VirtioDeviceArc, bool, Option<String>)>, virtio_devices: Vec<(VirtioDeviceArc, bool, String)>,
) -> DeviceManagerResult<()> { ) -> DeviceManagerResult<()> {
#[cfg(feature = "pci_support")] #[cfg(feature = "pci_support")]
{ {
@@ -843,7 +843,7 @@ impl DeviceManager {
&mut pci_bus, &mut pci_bus,
&None, &None,
&interrupt_manager, &interrupt_manager,
Some(iommu_id), iommu_id,
)?; )?;
} }
@@ -876,7 +876,7 @@ impl DeviceManager {
#[allow(unused_variables, unused_mut)] #[allow(unused_variables, unused_mut)]
fn add_mmio_devices( fn add_mmio_devices(
&mut self, &mut self,
virtio_devices: Vec<(VirtioDeviceArc, bool, Option<String>)>, virtio_devices: Vec<(VirtioDeviceArc, bool, String)>,
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>, interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
) -> DeviceManagerResult<()> { ) -> DeviceManagerResult<()> {
#[cfg(feature = "mmio_support")] #[cfg(feature = "mmio_support")]
@@ -1040,7 +1040,7 @@ impl DeviceManager {
fn add_console_device( fn add_console_device(
&mut self, &mut self,
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>, interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = LegacyIrqGroupConfig>>,
virtio_devices: &mut Vec<(VirtioDeviceArc, bool, Option<String>)>, virtio_devices: &mut Vec<(VirtioDeviceArc, bool, String)>,
) -> DeviceManagerResult<Arc<Console>> { ) -> DeviceManagerResult<Arc<Console>> {
let serial_config = self.config.lock().unwrap().serial.clone(); let serial_config = self.config.lock().unwrap().serial.clone();
let serial_writer: Option<Box<dyn io::Write + Send>> = match serial_config.mode { let serial_writer: Option<Box<dyn io::Write + Send>> = match serial_config.mode {
@@ -1108,7 +1108,7 @@ impl DeviceManager {
virtio_devices.push(( virtio_devices.push((
Arc::new(Mutex::new(virtio_console_device)) as VirtioDeviceArc, Arc::new(Mutex::new(virtio_console_device)) as VirtioDeviceArc,
console_config.iommu, console_config.iommu,
Some(id), id,
)); ));
Some(console_input) Some(console_input)
} else { } else {
@@ -1123,10 +1123,8 @@ impl DeviceManager {
})) }))
} }
fn make_virtio_devices( fn make_virtio_devices(&mut self) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
&mut self, let mut devices: Vec<(VirtioDeviceArc, bool, String)> = Vec::new();
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> {
let mut devices: Vec<(VirtioDeviceArc, bool, Option<String>)> = Vec::new();
// Create "standard" virtio devices (net/block/rng) // Create "standard" virtio devices (net/block/rng)
devices.append(&mut self.make_virtio_block_devices()?); devices.append(&mut self.make_virtio_block_devices()?);
@@ -1183,7 +1181,7 @@ impl DeviceManager {
fn make_virtio_block_device( fn make_virtio_block_device(
&mut self, &mut self,
disk_cfg: &mut DiskConfig, disk_cfg: &mut DiskConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> { ) -> DeviceManagerResult<(VirtioDeviceArc, bool, String)> {
let id = if let Some(id) = &disk_cfg.id { let id = if let Some(id) = &disk_cfg.id {
id.clone() id.clone()
} else { } else {
@@ -1204,7 +1202,7 @@ impl DeviceManager {
queue_size: disk_cfg.queue_size, queue_size: disk_cfg.queue_size,
}; };
let vhost_user_block_device = Arc::new(Mutex::new( let vhost_user_block_device = Arc::new(Mutex::new(
vm_virtio::vhost_user::Blk::new(id, disk_cfg.wce, vu_cfg) vm_virtio::vhost_user::Blk::new(id.clone(), disk_cfg.wce, vu_cfg)
.map_err(DeviceManagerError::CreateVhostUserBlk)?, .map_err(DeviceManagerError::CreateVhostUserBlk)?,
)); ));
@@ -1215,7 +1213,7 @@ impl DeviceManager {
Ok(( Ok((
Arc::clone(&vhost_user_block_device) as VirtioDeviceArc, Arc::clone(&vhost_user_block_device) as VirtioDeviceArc,
false, false,
disk_cfg.id.clone(), id,
)) ))
} else { } else {
let mut options = OpenOptions::new(); let mut options = OpenOptions::new();
@@ -1242,7 +1240,7 @@ impl DeviceManager {
match image_type { match image_type {
ImageType::Raw => { ImageType::Raw => {
let dev = vm_virtio::Block::new( let dev = vm_virtio::Block::new(
id, id.clone(),
raw_img, raw_img,
disk_cfg disk_cfg
.path .path
@@ -1260,17 +1258,13 @@ impl DeviceManager {
self.add_migratable_device(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>); self.add_migratable_device(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>);
Ok(( Ok((Arc::clone(&block) as VirtioDeviceArc, disk_cfg.iommu, id))
Arc::clone(&block) as VirtioDeviceArc,
disk_cfg.iommu,
disk_cfg.id.clone(),
))
} }
ImageType::Qcow2 => { ImageType::Qcow2 => {
let qcow_img = let qcow_img =
QcowFile::from(raw_img).map_err(DeviceManagerError::QcowDeviceCreate)?; QcowFile::from(raw_img).map_err(DeviceManagerError::QcowDeviceCreate)?;
let dev = vm_virtio::Block::new( let dev = vm_virtio::Block::new(
id, id.clone(),
qcow_img, qcow_img,
disk_cfg disk_cfg
.path .path
@@ -1288,11 +1282,7 @@ impl DeviceManager {
self.add_migratable_device(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>); self.add_migratable_device(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>);
Ok(( Ok((Arc::clone(&block) as VirtioDeviceArc, disk_cfg.iommu, id))
Arc::clone(&block) as VirtioDeviceArc,
disk_cfg.iommu,
disk_cfg.id.clone(),
))
} }
} }
} }
@@ -1300,7 +1290,7 @@ impl DeviceManager {
fn make_virtio_block_devices( fn make_virtio_block_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
let mut block_devices = self.config.lock().unwrap().disks.clone(); let mut block_devices = self.config.lock().unwrap().disks.clone();
@@ -1342,7 +1332,7 @@ impl DeviceManager {
fn make_virtio_net_device( fn make_virtio_net_device(
&mut self, &mut self,
net_cfg: &mut NetConfig, net_cfg: &mut NetConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> { ) -> DeviceManagerResult<(VirtioDeviceArc, bool, String)> {
let id = if let Some(id) = &net_cfg.id { let id = if let Some(id) = &net_cfg.id {
id.clone() id.clone()
} else { } else {
@@ -1363,7 +1353,7 @@ impl DeviceManager {
queue_size: net_cfg.queue_size, queue_size: net_cfg.queue_size,
}; };
let vhost_user_net_device = Arc::new(Mutex::new( let vhost_user_net_device = Arc::new(Mutex::new(
vm_virtio::vhost_user::Net::new(id, net_cfg.mac, vu_cfg) vm_virtio::vhost_user::Net::new(id.clone(), net_cfg.mac, vu_cfg)
.map_err(DeviceManagerError::CreateVhostUserNet)?, .map_err(DeviceManagerError::CreateVhostUserNet)?,
)); ));
self.add_migratable_device( self.add_migratable_device(
@@ -1372,13 +1362,13 @@ impl DeviceManager {
Ok(( Ok((
Arc::clone(&vhost_user_net_device) as VirtioDeviceArc, Arc::clone(&vhost_user_net_device) as VirtioDeviceArc,
net_cfg.iommu, net_cfg.iommu,
net_cfg.id.clone(), id,
)) ))
} else { } else {
let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap { let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap {
Arc::new(Mutex::new( Arc::new(Mutex::new(
vm_virtio::Net::new( vm_virtio::Net::new(
id, id.clone(),
Some(tap_if_name), Some(tap_if_name),
None, None,
None, None,
@@ -1392,7 +1382,7 @@ impl DeviceManager {
} else { } else {
Arc::new(Mutex::new( Arc::new(Mutex::new(
vm_virtio::Net::new( vm_virtio::Net::new(
id, id.clone(),
None, None,
Some(net_cfg.ip), Some(net_cfg.ip),
Some(net_cfg.mask), Some(net_cfg.mask),
@@ -1408,7 +1398,7 @@ impl DeviceManager {
Ok(( Ok((
Arc::clone(&virtio_net_device) as VirtioDeviceArc, Arc::clone(&virtio_net_device) as VirtioDeviceArc,
net_cfg.iommu, net_cfg.iommu,
net_cfg.id.clone(), id,
)) ))
} }
} }
@@ -1416,7 +1406,7 @@ impl DeviceManager {
/// Add virto-net and vhost-user-net devices /// Add virto-net and vhost-user-net devices
fn make_virtio_net_devices( fn make_virtio_net_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
let mut net_devices = self.config.lock().unwrap().net.clone(); let mut net_devices = self.config.lock().unwrap().net.clone();
if let Some(net_list_cfg) = &mut net_devices { if let Some(net_list_cfg) = &mut net_devices {
@@ -1431,7 +1421,7 @@ impl DeviceManager {
fn make_virtio_rng_devices( fn make_virtio_rng_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
// Add virtio-rng if required // Add virtio-rng if required
@@ -1446,7 +1436,7 @@ impl DeviceManager {
devices.push(( devices.push((
Arc::clone(&virtio_rng_device) as VirtioDeviceArc, Arc::clone(&virtio_rng_device) as VirtioDeviceArc,
rng_config.iommu, rng_config.iommu,
Some(id), id,
)); ));
self.add_migratable_device( self.add_migratable_device(
@@ -1460,7 +1450,7 @@ impl DeviceManager {
fn make_virtio_fs_device( fn make_virtio_fs_device(
&mut self, &mut self,
fs_cfg: &mut FsConfig, fs_cfg: &mut FsConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> { ) -> DeviceManagerResult<(VirtioDeviceArc, bool, String)> {
let id = if let Some(id) = &fs_cfg.id { let id = if let Some(id) = &fs_cfg.id {
id.clone() id.clone()
} else { } else {
@@ -1526,7 +1516,7 @@ impl DeviceManager {
let virtio_fs_device = Arc::new(Mutex::new( let virtio_fs_device = Arc::new(Mutex::new(
vm_virtio::vhost_user::Fs::new( vm_virtio::vhost_user::Fs::new(
id, id.clone(),
fs_sock, fs_sock,
&fs_cfg.tag, &fs_cfg.tag,
fs_cfg.num_queues, fs_cfg.num_queues,
@@ -1538,11 +1528,7 @@ impl DeviceManager {
self.add_migratable_device(Arc::clone(&virtio_fs_device) as Arc<Mutex<dyn Migratable>>); self.add_migratable_device(Arc::clone(&virtio_fs_device) as Arc<Mutex<dyn Migratable>>);
Ok(( Ok((Arc::clone(&virtio_fs_device) as VirtioDeviceArc, false, id))
Arc::clone(&virtio_fs_device) as VirtioDeviceArc,
false,
fs_cfg.id.clone(),
))
} else { } else {
Err(DeviceManagerError::NoVirtioFsSock) Err(DeviceManagerError::NoVirtioFsSock)
} }
@@ -1550,7 +1536,7 @@ impl DeviceManager {
fn make_virtio_fs_devices( fn make_virtio_fs_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
let mut fs_devices = self.config.lock().unwrap().fs.clone(); let mut fs_devices = self.config.lock().unwrap().fs.clone();
@@ -1567,7 +1553,7 @@ impl DeviceManager {
fn make_virtio_pmem_device( fn make_virtio_pmem_device(
&mut self, &mut self,
pmem_cfg: &mut PmemConfig, pmem_cfg: &mut PmemConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> { ) -> DeviceManagerResult<(VirtioDeviceArc, bool, String)> {
let id = if let Some(id) = &pmem_cfg.id { let id = if let Some(id) = &pmem_cfg.id {
id.clone() id.clone()
} else { } else {
@@ -1659,7 +1645,7 @@ impl DeviceManager {
let virtio_pmem_device = Arc::new(Mutex::new( let virtio_pmem_device = Arc::new(Mutex::new(
vm_virtio::Pmem::new( vm_virtio::Pmem::new(
id, id.clone(),
file, file,
pmem_guest_addr, pmem_guest_addr,
mapping, mapping,
@@ -1674,13 +1660,13 @@ impl DeviceManager {
Ok(( Ok((
Arc::clone(&virtio_pmem_device) as VirtioDeviceArc, Arc::clone(&virtio_pmem_device) as VirtioDeviceArc,
pmem_cfg.iommu, pmem_cfg.iommu,
pmem_cfg.id.clone(), id,
)) ))
} }
fn make_virtio_pmem_devices( fn make_virtio_pmem_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
// Add virtio-pmem if required // Add virtio-pmem if required
let mut pmem_devices = self.config.lock().unwrap().pmem.clone(); let mut pmem_devices = self.config.lock().unwrap().pmem.clone();
@@ -1697,7 +1683,7 @@ impl DeviceManager {
fn make_virtio_vsock_device( fn make_virtio_vsock_device(
&mut self, &mut self,
vsock_cfg: &mut VsockConfig, vsock_cfg: &mut VsockConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> { ) -> DeviceManagerResult<(VirtioDeviceArc, bool, String)> {
let id = if let Some(id) = &vsock_cfg.id { let id = if let Some(id) = &vsock_cfg.id {
id.clone() id.clone()
} else { } else {
@@ -1724,13 +1710,13 @@ impl DeviceManager {
Ok(( Ok((
Arc::clone(&vsock_device) as VirtioDeviceArc, Arc::clone(&vsock_device) as VirtioDeviceArc,
vsock_cfg.iommu, vsock_cfg.iommu,
Some(id), id,
)) ))
} }
fn make_virtio_vsock_devices( fn make_virtio_vsock_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
let mut vsock = self.config.lock().unwrap().vsock.clone(); let mut vsock = self.config.lock().unwrap().vsock.clone();
@@ -1744,7 +1730,7 @@ impl DeviceManager {
fn make_virtio_mem_devices( fn make_virtio_mem_devices(
&mut self, &mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> { ) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, String)>> {
let mut devices = Vec::new(); let mut devices = Vec::new();
let mm = self.memory_manager.clone(); let mm = self.memory_manager.clone();
@@ -1763,11 +1749,7 @@ impl DeviceManager {
.map_err(DeviceManagerError::CreateVirtioMem)?, .map_err(DeviceManagerError::CreateVirtioMem)?,
)); ));
devices.push(( devices.push((Arc::clone(&virtio_mem_device) as VirtioDeviceArc, false, id));
Arc::clone(&virtio_mem_device) as VirtioDeviceArc,
false,
Some(id),
));
self.add_migratable_device(Arc::clone(&virtio_mem_device) as Arc<Mutex<dyn Migratable>>); self.add_migratable_device(Arc::clone(&virtio_mem_device) as Arc<Mutex<dyn Migratable>>);
} }
@@ -1948,7 +1930,7 @@ impl DeviceManager {
pci: &mut PciBus, pci: &mut PciBus,
iommu_mapping: &Option<Arc<IommuMapping>>, iommu_mapping: &Option<Arc<IommuMapping>>,
interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = MsiIrqGroupConfig>>, interrupt_manager: &Arc<dyn InterruptManager<GroupConfig = MsiIrqGroupConfig>>,
id: Option<String>, id: String,
) -> DeviceManagerResult<u32> { ) -> DeviceManagerResult<u32> {
// Allows support for one MSI-X vector per queue. It also adds 1 // Allows support for one MSI-X vector per queue. It also adds 1
// as we need to take into account the dedicated vector to notify // as we need to take into account the dedicated vector to notify
@@ -2020,12 +2002,10 @@ impl DeviceManager {
self.bus_devices self.bus_devices
.push(Arc::clone(&virtio_pci_device) as Arc<Mutex<dyn BusDevice>>); .push(Arc::clone(&virtio_pci_device) as Arc<Mutex<dyn BusDevice>>);
if let Some(id) = id { if self.pci_id_list.contains_key(&id) {
if self.pci_id_list.contains_key(&id) { return Err(DeviceManagerError::DeviceIdAlreadyInUse);
return Err(DeviceManagerError::DeviceIdAlreadyInUse);
}
self.pci_id_list.insert(id, pci_device_bdf);
} }
self.pci_id_list.insert(id, pci_device_bdf);
pci.register_mapping( pci.register_mapping(
virtio_pci_device.clone(), virtio_pci_device.clone(),
@@ -2366,7 +2346,7 @@ impl DeviceManager {
&mut self, &mut self,
device: VirtioDeviceArc, device: VirtioDeviceArc,
iommu_attached: bool, iommu_attached: bool,
id: Option<String>, id: String,
) -> DeviceManagerResult<()> { ) -> DeviceManagerResult<()> {
if iommu_attached { if iommu_attached {
warn!("Placing device behind vIOMMU is not available for hotplugged devices"); warn!("Placing device behind vIOMMU is not available for hotplugged devices");