Struct kvm_ioctls::DeviceFd
source · pub struct DeviceFd { /* private fields */ }
Expand description
Wrapper over the file descriptor obtained when creating an emulated device in the kernel.
Implementations§
source§impl DeviceFd
impl DeviceFd
sourcepub fn has_device_attr(
&self,
device_attr: &kvm_device_attr
) -> Result<(), Error>
pub fn has_device_attr( &self, device_attr: &kvm_device_attr ) -> Result<(), Error>
Tests whether a device supports a particular attribute.
See the documentation for KVM_HAS_DEVICE_ATTR
.
§Arguments
device_attr
- The device attribute to be tested.addr
field is ignored.
sourcepub fn set_device_attr(
&self,
device_attr: &kvm_device_attr
) -> Result<(), Error>
pub fn set_device_attr( &self, device_attr: &kvm_device_attr ) -> Result<(), Error>
Sets a specified piece of device configuration and/or state.
See the documentation for KVM_SET_DEVICE_ATTR
.
§Arguments
device_attr
- The device attribute to be set.
§Example
kvm_device_type_KVM_DEV_TYPE_VFIO,
KVM_DEV_VFIO_GROUP, KVM_DEV_VFIO_GROUP_ADD, KVM_CREATE_DEVICE_TEST
};
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
let mut device = kvm_bindings::kvm_create_device {
type_: kvm_device_type_KVM_DEV_TYPE_VFIO,
fd: 0,
flags: KVM_CREATE_DEVICE_TEST,
};
let device_fd = vm
.create_device(&mut device)
.expect("Cannot create KVM device");
let dist_attr = kvm_bindings::kvm_device_attr {
group: KVM_DEV_VFIO_GROUP,
attr: u64::from(KVM_DEV_VFIO_GROUP_ADD),
addr: 0x0,
flags: 0,
};
if (device_fd.has_device_attr(&dist_attr).is_ok()) {
device_fd.set_device_attr(&dist_attr).unwrap();
}
sourcepub fn get_device_attr(
&self,
device_attr: &mut kvm_device_attr
) -> Result<(), Error>
pub fn get_device_attr( &self, device_attr: &mut kvm_device_attr ) -> Result<(), Error>
Gets a specified piece of device configuration and/or state.
See the documentation for KVM_GET_DEVICE_ATTR
.
§Arguments
device_attr
- The device attribute to be get. Note: This argument serves as both input and output. When calling this function, the user should explicitly provide valid values for thegroup
and theattr
field of thekvm_device_attr
structure, and a valid userspace address (i.e. theaddr
field) to access the returned device attribute data.
§Returns
- Returns the last occured
errno
wrapped in anErr
. device_attr
- Theaddr
field of thedevice_attr
structure will point to the device attribute data.
§Examples
let kvm = Kvm::new().unwrap();
let vm = kvm.create_vm().unwrap();
// As on x86_64, `get_device_attr` is not necessarily needed. Therefore here
// the code example is only for AArch64.
#[cfg(any(target_arch = "aarch64"))]
{
use kvm_bindings::{
kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2, kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
KVM_DEV_ARM_VGIC_GRP_NR_IRQS,
};
// Create a GIC device.
let mut gic_device = kvm_bindings::kvm_create_device {
type_: kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
fd: 0,
flags: 0,
};
let device_fd = match vm.create_device(&mut gic_device) {
Ok(fd) => fd,
Err(_) => {
gic_device.type_ = kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2;
vm.create_device(&mut gic_device)
.expect("Cannot create KVM vGIC device")
}
};
let mut data: u32 = 0;
let mut gic_attr = kvm_bindings::kvm_device_attr::default();
gic_attr.group = KVM_DEV_ARM_VGIC_GRP_NR_IRQS;
gic_attr.addr = &mut data as *const u32 as u64;
device_fd.get_device_attr(&mut gic_attr).unwrap();
}
Trait Implementations§
source§impl FromRawFd for DeviceFd
impl FromRawFd for DeviceFd
source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
§Safety
This function is unsafe as the primitives currently returned have the contract that they are the sole owner of the file descriptor they are wrapping. Usage of this function could accidentally allow violating this contract which can cause memory unsafety in code that relies on it being true.
Auto Trait Implementations§
impl Freeze for DeviceFd
impl RefUnwindSafe for DeviceFd
impl Send for DeviceFd
impl Sync for DeviceFd
impl Unpin for DeviceFd
impl UnwindSafe for DeviceFd
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more