Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kvm-ioctls/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [[#288](https://github.com/rust-vmm/kvm-ioctls/pull/288)]: Introduce `Cap::GuestMemfd`, `Cap::MemoryAttributes` and
`Cap::UserMemory2` capabilities enum variants for use with `VmFd::check_extension`.
- [[#288](https://github.com/rust-vmm/kvm-ioctls/pull/288)]: Introduce `VmFd::check_extension_raw` and `VmFd::check_extension_int` to allow `KVM_CHECK_EXTENSION` to return integer.

### Fixed

Expand Down
48 changes: 44 additions & 4 deletions kvm-ioctls/src/ioctls/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,10 +1402,50 @@ impl VmFd {
/// Wrapper over `KVM_CHECK_EXTENSION`.
///
/// Returns 0 if the capability is not available and a positive integer otherwise.
fn check_extension_int(&self, c: Cap) -> i32 {
// SAFETY: Safe because we know that our file is a VM fd and that the extension is one of
// the ones defined by kernel.
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
/// See the documentation for `KVM_CHECK_EXTENSION`.
///
/// # Arguments
///
/// * `c` - KVM capability to check.
///
/// # Example
///
/// ```
/// # use kvm_ioctls::Kvm;
/// use kvm_ioctls::Cap;
///
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// assert!(vm.check_extension_int(Cap::MaxVcpus) > 0);
/// ```
pub fn check_extension_int(&self, c: Cap) -> i32 {
self.check_extension_raw(c as c_ulong)
}

/// Wrapper over `KVM_CHECK_EXTENSION`.
///
/// Returns 0 if the capability is not available and a positive integer otherwise.
/// See the documentation for `KVM_CHECK_EXTENSION`.
///
/// # Arguments
///
/// * `c` - KVM capability to check in a form of a raw integer.
///
/// # Example
///
/// ```
/// # use kvm_ioctls::Kvm;
/// # use std::os::raw::c_ulong;
/// use kvm_ioctls::Cap;
///
/// let kvm = Kvm::new().unwrap();
/// let vm = kvm.create_vm().unwrap();
/// assert!(vm.check_extension_raw(Cap::MaxVcpus as c_ulong) > 0);
/// ```
pub fn check_extension_raw(&self, c: c_ulong) -> i32 {
// SAFETY: Safe because we know that our file is a KVM fd.
// If `c` is not a known kernel extension, kernel will return 0.
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c) }
}

/// Checks if a particular `Cap` is available.
Expand Down