diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs index 578e5d35c2..8ed6d43864 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vfio.rs @@ -20,6 +20,7 @@ use crate::{ device::{ pci_path::PciPath, topology::{do_add_pcie_endpoint, PCIeTopology}, + util::do_increase_count, Device, DeviceType, PCIeDevice, }, register_pcie_device, unregister_pcie_device, update_pcie_device, Hypervisor as hypervisor, @@ -522,18 +523,7 @@ impl Device for VfioDevice { } async fn increase_attach_count(&mut self) -> Result { - match self.attach_count { - 0 => { - // do real attach - self.attach_count += 1; - Ok(false) - } - std::u64::MAX => Err(anyhow!("device was attached too many times")), - _ => { - self.attach_count += 1; - Ok(true) - } - } + do_increase_count(&mut self.attach_count) } async fn decrease_attach_count(&mut self) -> Result { diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs index b2a1d90f92..3ff92ae6da 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/vhost_user_blk.rs @@ -9,7 +9,7 @@ use async_trait::async_trait; use super::VhostUserConfig; use crate::{ - device::{topology::PCIeTopology, Device, DeviceType}, + device::{topology::PCIeTopology, util::do_increase_count, Device, DeviceType}, Hypervisor as hypervisor, }; @@ -104,18 +104,7 @@ impl Device for VhostUserBlkDevice { } async fn increase_attach_count(&mut self) -> Result { - match self.attach_count { - 0 => { - // do real attach - self.attach_count += 1; - Ok(false) - } - std::u64::MAX => Err(anyhow!("device was attached too many times")), - _ => { - self.attach_count += 1; - Ok(true) - } - } + do_increase_count(&mut self.attach_count) } async fn decrease_attach_count(&mut self) -> Result { diff --git a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs index fdf0f7ea2d..27388c29bf 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/driver/virtio_blk.rs @@ -6,6 +6,7 @@ use crate::device::pci_path::PciPath; use crate::device::topology::PCIeTopology; +use crate::device::util::do_increase_count; use crate::device::Device; use crate::device::DeviceType; use crate::Hypervisor as hypervisor; @@ -135,18 +136,7 @@ impl Device for BlockDevice { } async fn increase_attach_count(&mut self) -> Result { - match self.attach_count { - 0 => { - // do real attach - self.attach_count += 1; - Ok(false) - } - std::u64::MAX => Err(anyhow!("device was attached too many times")), - _ => { - self.attach_count += 1; - Ok(true) - } - } + do_increase_count(&mut self.attach_count) } async fn decrease_attach_count(&mut self) -> Result {