Skip to content

Commit f07ca11

Browse files
committed
feat(tdx): select KvmVmType::TDX when TDX is enabled
Signed-off-by: Changyuan Lyu <changyuanl@google.com>
1 parent f6f22bc commit f07ca11

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

alioth/src/hv/kvm/vm/vm_x86_64.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ impl VmArch {
6868

6969
impl KvmVm {
7070
pub fn determine_vm_type(config: &VmConfig) -> KvmVmType {
71-
match &config.coco {
72-
Some(Coco::AmdSnp { .. }) => KvmVmType::SNP,
73-
_ => KvmVmType::DEFAULT,
71+
let Some(coco) = &config.coco else {
72+
return KvmVmType::DEFAULT;
73+
};
74+
match coco {
75+
Coco::AmdSev { .. } => KvmVmType::DEFAULT,
76+
Coco::AmdSnp { .. } => KvmVmType::SNP,
77+
Coco::IntelTdx { .. } => KvmVmType::TDX,
7478
}
7579
}
7680

alioth/src/hv/kvm/vm/vm_x86_64_test.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
use rstest::rstest;
1616

17+
use crate::arch::sev::{SevPolicy, SnpPolicy};
18+
use crate::arch::tdx::TdAttr;
19+
use crate::hv::kvm::vm::KvmVm;
1720
use crate::hv::kvm::vm::x86_64::translate_msi_addr;
21+
use crate::hv::{Coco, VmConfig};
22+
use crate::sys::kvm::KvmVmType;
1823

1924
#[rstest]
2025
#[case(0, 0)]
@@ -26,3 +31,33 @@ fn test_translate_msi_addr(#[case] addr: u64, #[case] expected: u64) {
2631
let (lo, hi) = translate_msi_addr(addr as u32, (addr >> 32) as u32);
2732
assert_eq!((lo as u64) | ((hi as u64) << 32), expected);
2833
}
34+
35+
#[rstest]
36+
#[case(VmConfig { coco: None }, KvmVmType::DEFAULT)]
37+
#[case(
38+
VmConfig {
39+
coco: Some(Coco::AmdSev {
40+
policy: SevPolicy(0x5)
41+
})
42+
},
43+
KvmVmType::DEFAULT
44+
)]
45+
#[case(
46+
VmConfig {
47+
coco: Some(Coco::AmdSnp {
48+
policy: SnpPolicy(0x30000)
49+
})
50+
},
51+
KvmVmType::SNP
52+
)]
53+
#[case(
54+
VmConfig {
55+
coco: Some(Coco::IntelTdx {
56+
attr: TdAttr::empty()
57+
})
58+
},
59+
KvmVmType::TDX
60+
)]
61+
fn test_determine_vm_type(#[case] config: VmConfig, #[case] vm_type: KvmVmType) {
62+
assert_eq!(KvmVm::determine_vm_type(&config), vm_type)
63+
}

0 commit comments

Comments
 (0)