diff --git a/hypervisor/src/device.rs b/hypervisor/src/device.rs index 109ecbaf6..b25e42102 100644 --- a/hypervisor/src/device.rs +++ b/hypervisor/src/device.rs @@ -10,6 +10,7 @@ // use crate::DeviceAttr; +use std::any::Any; use std::os::unix::io::AsRawFd; use thiserror::Error; @@ -44,4 +45,6 @@ pub trait Device: Send + Sync + AsRawFd { fn set_device_attr(&self, attr: &DeviceAttr) -> Result<()>; /// Get device attribute. fn get_device_attr(&self, attr: &mut DeviceAttr) -> Result<()>; + /// Provide a way to downcast to the device fd. + fn as_any(&self) -> &dyn Any; } diff --git a/hypervisor/src/kvm/mod.rs b/hypervisor/src/kvm/mod.rs index e9ff2a752..ea0073035 100644 --- a/hypervisor/src/kvm/mod.rs +++ b/hypervisor/src/kvm/mod.rs @@ -26,6 +26,7 @@ use crate::vm::{self, InterruptSourceConfig, VmOps}; use crate::{arm64_core_reg_id, offset__of}; use kvm_ioctls::{NoDatamatch, VcpuFd, VmFd}; use serde::{Deserialize, Serialize}; +use std::any::Any; use std::collections::HashMap; #[cfg(target_arch = "aarch64")] use std::convert::TryInto; @@ -2004,6 +2005,12 @@ impl device::Device for KvmDevice { .get_device_attr(attr) .map_err(|e| device::HypervisorDeviceError::GetDeviceAttribute(e.into())) } + /// + /// Cast to the underlying KVM device fd + /// + fn as_any(&self) -> &dyn Any { + self + } } impl AsRawFd for KvmDevice { diff --git a/hypervisor/src/mshv/mod.rs b/hypervisor/src/mshv/mod.rs index 05cc1aab4..509da4757 100644 --- a/hypervisor/src/mshv/mod.rs +++ b/hypervisor/src/mshv/mod.rs @@ -16,6 +16,7 @@ pub use mshv_bindings::*; pub use mshv_ioctls::IoEventAddress; use mshv_ioctls::{set_registers_64, Mshv, NoDatamatch, VcpuFd, VmFd}; use serde::{Deserialize, Serialize}; +use std::any::Any; use std::collections::HashMap; use std::sync::{Arc, RwLock}; use vm::DataMatch; @@ -687,6 +688,12 @@ impl device::Device for MshvDevice { .get_device_attr(attr) .map_err(|e| device::HypervisorDeviceError::GetDeviceAttribute(e.into())) } + /// + /// Cast to the underlying MSHV device fd + /// + fn as_any(&self) -> &dyn Any { + self + } } impl AsRawFd for MshvDevice {