vmm: Factorize net creation

Since both Net and vhost_user::Net implement the Migratable trait, we
can factorize the common part to simplify the code related to the net
creation.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2022-05-05 11:08:32 +02:00
parent 425902b296
commit 058a61148c

View File

@ -2211,7 +2211,7 @@ impl DeviceManager {
}; };
info!("Creating virtio-net device: {:?}", net_cfg); info!("Creating virtio-net device: {:?}", net_cfg);
if net_cfg.vhost_user { let (virtio_device, migratable_device) = if net_cfg.vhost_user {
let socket = net_cfg.vhost_socket.as_ref().unwrap().clone(); let socket = net_cfg.vhost_socket.as_ref().unwrap().clone();
let vu_cfg = VhostUserConfig { let vu_cfg = VhostUserConfig {
socket, socket,
@ -2222,7 +2222,7 @@ impl DeviceManager {
VhostMode::Client => false, VhostMode::Client => false,
VhostMode::Server => true, VhostMode::Server => true,
}; };
let vhost_user_net_device = Arc::new(Mutex::new( let vhost_user_net = Arc::new(Mutex::new(
match virtio_devices::vhost_user::Net::new( match virtio_devices::vhost_user::Net::new(
id.clone(), id.clone(),
net_cfg.mac, net_cfg.mac,
@ -2242,24 +2242,12 @@ impl DeviceManager {
}, },
)); ));
// Fill the device tree with a new node. In case of restore, we (
// know there is nothing to do, so we can simply override the Arc::clone(&vhost_user_net) as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
// existing entry. vhost_user_net as Arc<Mutex<dyn Migratable>>,
self.device_tree )
.lock()
.unwrap()
.insert(id.clone(), device_node!(id, vhost_user_net_device));
Ok(MetaVirtioDevice {
virtio_device: Arc::clone(&vhost_user_net_device)
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
iommu: net_cfg.iommu,
id,
pci_segment: net_cfg.pci_segment,
dma_handler: None,
})
} else { } else {
let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap { let virtio_net = if let Some(ref tap_if_name) = net_cfg.tap {
Arc::new(Mutex::new( Arc::new(Mutex::new(
virtio_devices::Net::new( virtio_devices::Net::new(
id.clone(), id.clone(),
@ -2317,24 +2305,28 @@ impl DeviceManager {
)) ))
}; };
(
Arc::clone(&virtio_net) as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
virtio_net as Arc<Mutex<dyn Migratable>>,
)
};
// Fill the device tree with a new node. In case of restore, we // Fill the device tree with a new node. In case of restore, we
// know there is nothing to do, so we can simply override the // know there is nothing to do, so we can simply override the
// existing entry. // existing entry.
self.device_tree self.device_tree
.lock() .lock()
.unwrap() .unwrap()
.insert(id.clone(), device_node!(id, virtio_net_device)); .insert(id.clone(), device_node!(id, migratable_device));
Ok(MetaVirtioDevice { Ok(MetaVirtioDevice {
virtio_device: Arc::clone(&virtio_net_device) virtio_device,
as Arc<Mutex<dyn virtio_devices::VirtioDevice>>,
iommu: net_cfg.iommu, iommu: net_cfg.iommu,
id, id,
pci_segment: net_cfg.pci_segment, pci_segment: net_cfg.pci_segment,
dma_handler: None, dma_handler: None,
}) })
} }
}
/// Add virto-net and vhost-user-net devices /// Add virto-net and vhost-user-net devices
fn make_virtio_net_devices(&mut self) -> DeviceManagerResult<Vec<MetaVirtioDevice>> { fn make_virtio_net_devices(&mut self) -> DeviceManagerResult<Vec<MetaVirtioDevice>> {