Struct kvm_ioctls::DeviceFd[][src]

pub struct DeviceFd { /* fields omitted */ }
Expand description

Wrapper over the file descriptor obtained when creating an emulated device in the kernel.

Implementations

impl DeviceFd[src]

pub fn has_device_attr(
    &self,
    device_attr: &kvm_device_attr
) -> Result<(), Error>
[src]

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.

pub fn set_device_attr(
    &self,
    device_attr: &kvm_device_attr
) -> Result<(), Error>
[src]

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

pub fn get_device_attr(
    &self,
    device_attr: &mut kvm_device_attr
) -> Result<(), Error>
[src]

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_DEV_ARM_VGIC_GRP_NR_IRQS, kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V2,
        kvm_device_type_KVM_DEV_TYPE_ARM_VGIC_V3,
    };

    // 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

impl AsRawFd for DeviceFd[src]

fn as_raw_fd(&self) -> RawFd[src]

Extracts the raw file descriptor. Read more

impl FromRawFd for DeviceFd[src]

unsafe fn from_raw_fd(fd: RawFd) -> Self[src]

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.