From ea4a95c4f65b949e983b335abbb077c832596381 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Mon, 13 Feb 2023 16:10:50 +0000 Subject: [PATCH] vmm: config: Implement Clone for NetConfig The custom version duplicates any FDs that have been provided so that the validation logic used on hotplug, which takes a clone of the config, can be safely carried out. Signed-off-by: Rob Bradford --- vmm/src/config.rs | 16 ++++++++++++++++ vmm/src/vm_config.rs | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 980bd5242..02aa830e0 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -1134,6 +1134,22 @@ impl NetConfig { } } +impl Clone for NetConfig { + fn clone(&self) -> Self { + NetConfig { + tap: self.tap.clone(), + vhost_socket: self.vhost_socket.clone(), + id: self.id.clone(), + fds: self + .fds + .as_ref() + // SAFETY: We have been handed these FDs through the API + .map(|fds| fds.iter().map(|fd| unsafe { libc::dup(*fd) }).collect()), + ..*self + } + } +} + impl RngConfig { pub fn parse(rng: &str) -> Result { let mut parser = OptionParser::new(); diff --git a/vmm/src/vm_config.rs b/vmm/src/vm_config.rs index f9740e57d..ed7e4c109 100644 --- a/vmm/src/vm_config.rs +++ b/vmm/src/vm_config.rs @@ -183,7 +183,7 @@ impl Default for MemoryConfig { } } -#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, Default)] pub enum VhostMode { #[default] Client, @@ -248,7 +248,7 @@ impl Default for DiskConfig { } } -#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] pub struct NetConfig { #[serde(default = "default_netconfig_tap")] pub tap: Option,