From 9eb7413fab5d8d38e97803e6a6242df7c68ef526 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Mon, 27 Apr 2020 11:29:16 +0200 Subject: [PATCH] vm-virtio: net: Expect an identifier upon device creation This identifier is chosen from the DeviceManager so that it will manage all identifiers across the VM, which will ensure uniqueness. Signed-off-by: Sebastien Boeuf --- vm-virtio/src/net.rs | 19 ++++++++++--------- vmm/src/device_manager.rs | 12 +++++++++--- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/vm-virtio/src/net.rs b/vm-virtio/src/net.rs index 3c7fd1613..65bc99bd4 100644 --- a/vm-virtio/src/net.rs +++ b/vm-virtio/src/net.rs @@ -311,6 +311,7 @@ impl NetEpollHandler { } pub struct Net { + id: String, kill_evt: Option, pause_evt: Option, taps: Option>, @@ -336,6 +337,7 @@ pub struct NetState { impl Net { /// Create a new virtio network device with the given TAP interface. pub fn new_with_tap( + id: String, taps: Vec, guest_mac: Option, iommu: bool, @@ -365,6 +367,7 @@ impl Net { } Ok(Net { + id, kill_evt: None, pause_evt: None, taps: Some(taps), @@ -382,7 +385,9 @@ impl Net { /// Create a new virtio network device with the given IP address and /// netmask. + #[allow(clippy::too_many_arguments)] pub fn new( + id: String, if_name: Option<&str>, ip_addr: Option, netmask: Option, @@ -393,7 +398,7 @@ impl Net { ) -> Result { let taps = open_tap(if_name, ip_addr, netmask, num_queues / 2).map_err(Error::OpenTap)?; - Self::new_with_tap(taps, guest_mac, iommu, num_queues, queue_size) + Self::new_with_tap(id, taps, guest_mac, iommu, num_queues, queue_size) } fn state(&self) -> NetState { @@ -611,19 +616,18 @@ impl VirtioDevice for Net { } virtio_ctrl_q_pausable!(Net); -const NET_SNAPSHOT_ID: &str = "virtio-net"; impl Snapshottable for Net { fn id(&self) -> String { - NET_SNAPSHOT_ID.to_string() + self.id.clone() } fn snapshot(&self) -> std::result::Result { let snapshot = serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?; - let mut net_snapshot = Snapshot::new(NET_SNAPSHOT_ID); + let mut net_snapshot = Snapshot::new(self.id.as_str()); net_snapshot.add_data_section(SnapshotDataSection { - id: format!("{}-section", NET_SNAPSHOT_ID), + id: format!("{}-section", self.id), snapshot, }); @@ -631,10 +635,7 @@ impl Snapshottable for Net { } fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> { - if let Some(net_section) = snapshot - .snapshot_data - .get(&format!("{}-section", NET_SNAPSHOT_ID)) - { + if let Some(net_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) { let net_state = match serde_json::from_slice(&net_section.snapshot) { Ok(state) => state, Err(error) => { diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 46f7db424..36443dd95 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -1340,9 +1340,13 @@ impl DeviceManager { &mut self, net_cfg: &mut NetConfig, ) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option)> { - if net_cfg.id.is_none() { - net_cfg.id = Some(self.next_device_name(NET_DEVICE_NAME_PREFIX)?); - } + let id = if let Some(id) = &net_cfg.id { + id.clone() + } else { + let id = self.next_device_name(NET_DEVICE_NAME_PREFIX)?; + net_cfg.id = Some(id.clone()); + id + }; if net_cfg.vhost_user { let sock = if let Some(sock) = net_cfg.vhost_socket.clone() { @@ -1371,6 +1375,7 @@ impl DeviceManager { let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap { Arc::new(Mutex::new( vm_virtio::Net::new( + id, Some(tap_if_name), None, None, @@ -1384,6 +1389,7 @@ impl DeviceManager { } else { Arc::new(Mutex::new( vm_virtio::Net::new( + id, None, Some(net_cfg.ip), Some(net_cfg.mask),