Skip to content

Commit 69d03d9

Browse files
gabhijitalexandruag
authored andcommitted
system: Added an API get_max_vcpu_id to kvm
Added a new API `get_max_vcpu_id` that returns the `KVM_CAP_MAX_VCPU_ID` if the Extension is present or `get_max_vcpus`. Also updated the test case for `vm.create_cpu` to use `max_vcpu_id` obtained using the above API. Updated coverage score to 91.3 to fix build issue Signed-off-by: Abhijit Gadgil <[email protected]>
1 parent 4f61bba commit 69d03d9

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
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.2,
2+
"coverage_score": 91.3,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/ioctls/system.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,27 @@ impl Kvm {
227227
}
228228
}
229229

230+
/// Gets the Maximum VCPU ID per VM.
231+
///
232+
/// See the documentation for `KVM_CAP_MAX_VCPU_ID`
233+
/// Returns [get_max_vcpus()](struct.Kvm.html#method.get_max_vcpus) when
234+
/// `KVM_CAP_MAX_VCPU_ID` is not implemented
235+
///
236+
/// # Example
237+
///
238+
/// ```
239+
/// # use kvm_ioctls::Kvm;
240+
/// let kvm = Kvm::new().unwrap();
241+
/// assert!(kvm.get_max_vcpu_id() > 0);
242+
/// ```
243+
///
244+
pub fn get_max_vcpu_id(&self) -> usize {
245+
match self.check_extension_int(Cap::MaxVcpuId) {
246+
0 => self.get_max_vcpus(),
247+
x => x as usize,
248+
}
249+
}
250+
230251
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
231252
fn get_cpuid(&self, kind: u64, num_entries: usize) -> Result<CpuId> {
232253
if num_entries > KVM_MAX_CPUID_ENTRIES {

src/ioctls/vm.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,34 +1800,19 @@ mod tests {
18001800
}
18011801

18021802
#[test]
1803-
fn create_vcpu_different_cpuids() {
1803+
fn test_create_vcpu_different_ids() {
18041804
let kvm = Kvm::new().unwrap();
18051805
let vm = kvm.create_vm().unwrap();
18061806

18071807
// Fails when an arbitrarily large value
18081808
let err = vm.create_vcpu(65537 as u64).err();
18091809
assert_eq!(err.unwrap().errno(), libc::EINVAL);
18101810

1811-
// Note: We can request up to KVM_MAX_VCPU_ID if it exists or up to KVM_MAX_VCPUS or
1812-
// NR_CPUS or 4. This is determined by the appropriate capability being present.
1813-
// We check near boundry conditions `max_vcpus - 1` should succeed but `max_vcpus` as
1814-
// determined by the appropriate capability should fail.
1815-
//
1816-
// Ref: https://www.kernel.org/doc/html/latest/virt/kvm/api.html#kvm-create-vcpu
1817-
//
1818-
let mut max_vcpus = vm.check_extension_int(Cap::MaxVcpuId);
1819-
if max_vcpus == 0 {
1820-
max_vcpus = vm.check_extension_int(Cap::MaxVcpus);
1821-
}
1822-
if max_vcpus == 0 {
1823-
max_vcpus = vm.check_extension_int(Cap::NrVcpus);
1824-
}
1825-
if max_vcpus == 0 {
1826-
max_vcpus = 4
1827-
}
1828-
let vcpu = vm.create_vcpu((max_vcpus - 1) as u64);
1811+
// Fails when input `id` = `max_vcpu_id`
1812+
let max_vcpu_id = kvm.get_max_vcpu_id();
1813+
let vcpu = vm.create_vcpu((max_vcpu_id - 1) as u64);
18291814
assert!(vcpu.is_ok());
1830-
let vcpu_err = vm.create_vcpu(max_vcpus as u64).err();
1815+
let vcpu_err = vm.create_vcpu(max_vcpu_id as u64).err();
18311816
assert_eq!(vcpu_err.unwrap().errno(), libc::EINVAL);
18321817
}
18331818

0 commit comments

Comments
 (0)