Skip to content

Commit 1f86c9b

Browse files
KeyboardNerdShadowCurse
authored andcommitted
Introduce check_extension_int and check_extension_raw
The two functions are used to return integer value from KVM_CHECK_EXTENSION ioctl commands. This is useful for capabilities returning an integer with each bits representing different configs. Signed-off-by: Sida Chen <sidachen@google.com>
1 parent e4c0ab4 commit 1f86c9b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

kvm-ioctls/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- [[#288](https://github.com/rust-vmm/kvm-ioctls/pull/288)]: Introduce `Cap::GuestMemfd`, `Cap::MemoryAttributes` and
88
`Cap::UserMemory2` capabilities enum variants for use with `VmFd::check_extension`.
9+
- [[#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.
910

1011
### Fixed
1112

kvm-ioctls/src/ioctls/vm.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,10 +1402,50 @@ impl VmFd {
14021402
/// Wrapper over `KVM_CHECK_EXTENSION`.
14031403
///
14041404
/// Returns 0 if the capability is not available and a positive integer otherwise.
1405-
fn check_extension_int(&self, c: Cap) -> i32 {
1406-
// SAFETY: Safe because we know that our file is a VM fd and that the extension is one of
1407-
// the ones defined by kernel.
1408-
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
1405+
/// See the documentation for `KVM_CHECK_EXTENSION`.
1406+
///
1407+
/// # Arguments
1408+
///
1409+
/// * `c` - KVM capability to check.
1410+
///
1411+
/// # Example
1412+
///
1413+
/// ```
1414+
/// # use kvm_ioctls::Kvm;
1415+
/// use kvm_ioctls::Cap;
1416+
///
1417+
/// let kvm = Kvm::new().unwrap();
1418+
/// let vm = kvm.create_vm().unwrap();
1419+
/// assert!(vm.check_extension_int(Cap::MaxVcpus) > 0);
1420+
/// ```
1421+
pub fn check_extension_int(&self, c: Cap) -> i32 {
1422+
self.check_extension_raw(c as c_ulong)
1423+
}
1424+
1425+
/// Wrapper over `KVM_CHECK_EXTENSION`.
1426+
///
1427+
/// Returns 0 if the capability is not available and a positive integer otherwise.
1428+
/// See the documentation for `KVM_CHECK_EXTENSION`.
1429+
///
1430+
/// # Arguments
1431+
///
1432+
/// * `c` - KVM capability to check in a form of a raw integer.
1433+
///
1434+
/// # Example
1435+
///
1436+
/// ```
1437+
/// # use kvm_ioctls::Kvm;
1438+
/// # use std::os::raw::c_ulong;
1439+
/// use kvm_ioctls::Cap;
1440+
///
1441+
/// let kvm = Kvm::new().unwrap();
1442+
/// let vm = kvm.create_vm().unwrap();
1443+
/// assert!(vm.check_extension_raw(Cap::MaxVcpus as c_ulong) > 0);
1444+
/// ```
1445+
pub fn check_extension_raw(&self, c: c_ulong) -> i32 {
1446+
// SAFETY: Safe because we know that our file is a KVM fd.
1447+
// If `c` is not a known kernel extension, kernel will return 0.
1448+
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c) }
14091449
}
14101450

14111451
/// Checks if a particular `Cap` is available.

0 commit comments

Comments
 (0)