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

source

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.
source

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();
}
source

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 the group and the attr field of the kvm_device_attr structure, and a valid userspace address (i.e. the addr field) to access the returned device attribute data.
§Returns
  • Returns the last occured errno wrapped in an Err.
  • device_attr - The addr field of the device_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 AsRawFd for DeviceFd

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for DeviceFd

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromRawFd for DeviceFd

source§

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§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.