vmm: device_manager: Refactor make_virtio_net_devices

Split it into a method that creates a single device which is called by
the multiple device version so this can be used when dynamically adding
a device.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-03-13 09:38:42 +00:00 committed by Sebastien Boeuf
parent 9df601a1df
commit 42a9896fe4

View File

@ -1209,12 +1209,10 @@ impl DeviceManager {
Ok(sock)
}
/// Add virto-net and vhost-user-net devices
fn make_virtio_net_devices(&mut self) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool)>> {
let mut devices = Vec::new();
let net_devices = self.config.lock().unwrap().net.clone();
if let Some(net_list_cfg) = &net_devices {
for net_cfg in net_list_cfg.iter() {
fn make_virtio_net_device(
&mut self,
net_cfg: &NetConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool)> {
if net_cfg.vhost_user {
let sock = if let Some(sock) = net_cfg.vhost_socket.clone() {
sock
@ -1230,13 +1228,12 @@ impl DeviceManager {
vm_virtio::vhost_user::Net::new(net_cfg.mac, vu_cfg)
.map_err(DeviceManagerError::CreateVhostUserNet)?,
));
devices.push((
Arc::clone(&vhost_user_net_device)
as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
net_cfg.iommu,
));
self.migratable_devices
.push(Arc::clone(&vhost_user_net_device) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&vhost_user_net_device) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
net_cfg.iommu,
))
} else {
let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap {
Arc::new(Mutex::new(
@ -1265,14 +1262,23 @@ impl DeviceManager {
.map_err(DeviceManagerError::CreateVirtioNet)?,
))
};
devices.push((
Arc::clone(&virtio_net_device) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
net_cfg.iommu,
));
self.migratable_devices
.push(Arc::clone(&virtio_net_device) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&virtio_net_device) as Arc<Mutex<dyn vm_virtio::VirtioDevice>>,
net_cfg.iommu,
))
}
}
/// Add virto-net and vhost-user-net devices
fn make_virtio_net_devices(&mut self) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool)>> {
let mut devices = Vec::new();
let net_devices = self.config.lock().unwrap().net.clone();
if let Some(net_list_cfg) = &net_devices {
for net_cfg in net_list_cfg.iter() {
devices.push(self.make_virtio_net_device(net_cfg)?);
}
}
Ok(devices)