From d656058c219a482eec7e5a3281b4efbf63a39a59 Mon Sep 17 00:00:00 2001 From: Bo Chen Date: Fri, 14 Apr 2023 17:47:58 -0700 Subject: [PATCH] vmm: Add valid FDs for TAP devices to 'VmConfig::preserved_fds' In this way, valid FDs for TAP devices will be closed when the holding VmConfig instance is destroyed. Signed-off-by: Bo Chen --- vmm/src/device_manager.rs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/vmm/src/device_manager.rs b/vmm/src/device_manager.rs index 009e6ab72..7319ebfdd 100644 --- a/vmm/src/device_manager.rs +++ b/vmm/src/device_manager.rs @@ -2317,23 +2317,28 @@ impl DeviceManager { .map_err(DeviceManagerError::CreateVirtioNet)?, )) } else if let Some(fds) = &net_cfg.fds { - Arc::new(Mutex::new( - virtio_devices::Net::from_tap_fds( - id.clone(), - fds, - Some(net_cfg.mac), - net_cfg.mtu, - self.force_iommu | net_cfg.iommu, - net_cfg.queue_size, - self.seccomp_action.clone(), - net_cfg.rate_limiter_config, - self.exit_evt - .try_clone() - .map_err(DeviceManagerError::EventFd)?, - state, - ) - .map_err(DeviceManagerError::CreateVirtioNet)?, - )) + let net = virtio_devices::Net::from_tap_fds( + id.clone(), + fds, + Some(net_cfg.mac), + net_cfg.mtu, + self.force_iommu | net_cfg.iommu, + net_cfg.queue_size, + self.seccomp_action.clone(), + net_cfg.rate_limiter_config, + self.exit_evt + .try_clone() + .map_err(DeviceManagerError::EventFd)?, + state, + ) + .map_err(DeviceManagerError::CreateVirtioNet)?; + + // SAFETY: 'fds' are valid because TAP devices are created successfully + unsafe { + self.config.lock().unwrap().add_preserved_fds(fds.clone()); + } + + Arc::new(Mutex::new(net)) } else { Arc::new(Mutex::new( virtio_devices::Net::new(