Skip to content

Commit d8f78a1

Browse files
michael2012zalxiord
authored andcommitted
Implement 'check_extension()' for Vm
'check_extension()' was defined in Kvm to perform KVM_CHECK_EXTENSION ioctl only, but it is also possible in Vm. Signed-off-by: Michael Zhao <[email protected]>
1 parent 53a2cec commit d8f78a1

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 91.3,
2+
"coverage_score": 91.4,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/ioctls/vm.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::os::raw::c_void;
1111
use std::os::raw::{c_int, c_ulong};
1212
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
1313

14+
use cap::Cap;
1415
use ioctls::device::new_device;
1516
use ioctls::device::DeviceFd;
1617
use ioctls::vcpu::new_vcpu;
@@ -1216,6 +1217,40 @@ impl VmFd {
12161217
pub fn run_size(&self) -> usize {
12171218
self.run_size
12181219
}
1220+
1221+
/// Wrapper over `KVM_CHECK_EXTENSION`.
1222+
///
1223+
/// Returns 0 if the capability is not available and a positive integer otherwise.
1224+
fn check_extension_int(&self, c: Cap) -> i32 {
1225+
// Safe because we know that our file is a VM fd and that the extension is one of the ones
1226+
// defined by kernel.
1227+
unsafe { ioctl_with_val(self, KVM_CHECK_EXTENSION(), c as c_ulong) }
1228+
}
1229+
1230+
/// Checks if a particular `Cap` is available.
1231+
///
1232+
/// Returns true if the capability is supported and false otherwise.
1233+
/// See the documentation for `KVM_CHECK_EXTENSION`.
1234+
///
1235+
/// # Arguments
1236+
///
1237+
/// * `c` - VM capability to check.
1238+
///
1239+
/// # Example
1240+
///
1241+
/// ```
1242+
/// # use kvm_ioctls::Kvm;
1243+
/// use kvm_ioctls::Cap;
1244+
///
1245+
/// let kvm = Kvm::new().unwrap();
1246+
/// let vm = kvm.create_vm().unwrap();
1247+
/// // Check if `KVM_CAP_MP_STATE` is supported.
1248+
/// assert!(vm.check_extension(Cap::MpState));
1249+
/// ```
1250+
///
1251+
pub fn check_extension(&self, c: Cap) -> bool {
1252+
self.check_extension_int(c) > 0
1253+
}
12191254
}
12201255

12211256
/// Helper function to create a new `VmFd`.
@@ -1751,4 +1786,11 @@ mod tests {
17511786
let irq_routing = kvm_irq_routing::default();
17521787
assert!(vm.set_gsi_routing(&irq_routing).is_ok());
17531788
}
1789+
1790+
#[test]
1791+
fn test_check_extension() {
1792+
let kvm = Kvm::new().unwrap();
1793+
let vm = kvm.create_vm().unwrap();
1794+
assert!(vm.check_extension(Cap::MpState));
1795+
}
17541796
}

0 commit comments

Comments
 (0)