Skip to content

Commit fc75ec7

Browse files
alexandruagacatangiu
authored andcommitted
vmm: added kvm param to Vmm::new
Added a new parameter to the new method of struct Vmm. Previously, the Vmm would attempt to create a new Kvm object itself (which involved opening /dev/kvm) via the KvmContext::new constructor. This constructor has been replaced with KvmContext::with_kvm (also expecting a Kvm param) which gets passed a Kvm object in Kvm::new. This is a step further towards the modularization of the Vmm struct, as we no longer impose a particular way of creating the Kvm object within. Signed-off-by: Alexandru Agache <[email protected]>
1 parent ea0b6f9 commit fc75ec7

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

vmm/src/device_manager/mmio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ mod tests {
406406
&EventFd::new().expect("cannot create eventFD"),
407407
from_api,
408408
0,
409+
kvm_ioctls::Kvm::new().expect("Cannot create KVM object"),
409410
)
410411
.expect("Cannot Create VMM")
411412
}

vmm/src/lib.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,10 @@ pub struct KvmContext {
491491
}
492492

493493
impl KvmContext {
494-
fn new() -> Result<Self> {
494+
fn with_kvm(kvm: Kvm) -> Result<Self> {
495495
use Cap::*;
496496

497-
let kvm = Kvm::new().map_err(Error::Kvm)?;
498-
499-
// Check that KVM has correct version.
497+
// Check that KVM has the correct version.
500498
if kvm.get_api_version() != KVM_API_VERSION as i32 {
501499
return Err(Error::KvmApiVersion(kvm.get_api_version()));
502500
}
@@ -820,6 +818,7 @@ impl Vmm {
820818
control_fd: &AsRawFd,
821819
from_api: Receiver<Box<VmmAction>>,
822820
seccomp_level: u32,
821+
kvm: Kvm,
823822
) -> Result<Self> {
824823
let mut epoll_context = EpollContext::new()?;
825824
// If this fails, it's fatal; using expect() to crash.
@@ -843,7 +842,7 @@ impl Vmm {
843842
NetworkInterfaceConfigs::new(),
844843
None,
845844
);
846-
let kvm = KvmContext::new()?;
845+
let kvm = KvmContext::with_kvm(kvm)?;
847846
let vm = Vm::new(kvm.fd()).map_err(Error::Vm)?;
848847

849848
Ok(Vmm {
@@ -2163,8 +2162,14 @@ pub fn start_vmm_thread(
21632162
.name("fc_vmm".to_string())
21642163
.spawn(move || {
21652164
// If this fails, consider it fatal. Use expect().
2166-
let mut vmm = Vmm::new(api_shared_info, &api_event_fd, from_api, seccomp_level)
2167-
.expect("Cannot create VMM");
2165+
let mut vmm = Vmm::new(
2166+
api_shared_info,
2167+
&api_event_fd,
2168+
from_api,
2169+
seccomp_level,
2170+
Kvm::new().expect("Error creating the Kvm object"),
2171+
)
2172+
.expect("Cannot create VMM");
21682173

21692174
if let Some(json) = config_json {
21702175
if let Err(e) = vmm.configure_from_json(json) {
@@ -2314,6 +2319,13 @@ mod tests {
23142319
}
23152320
}
23162321

2322+
impl KvmContext {
2323+
pub fn new() -> Result<Self> {
2324+
let kvm = Kvm::new().map_err(Error::Kvm)?;
2325+
Self::with_kvm(kvm)
2326+
}
2327+
}
2328+
23172329
struct DummyEpollHandler {
23182330
evt: Option<DeviceEventT>,
23192331
}
@@ -2383,9 +2395,10 @@ mod tests {
23832395
let (_to_vmm, from_api) = channel();
23842396
Vmm::new(
23852397
shared_info,
2386-
&EventFd::new().expect("cannot create eventFD"),
2398+
&EventFd::new().expect("Cannot create eventFD"),
23872399
from_api,
23882400
seccomp::SECCOMP_LEVEL_NONE,
2401+
Kvm::new().expect("Error creating Kvm object"),
23892402
)
23902403
.expect("Cannot Create VMM")
23912404
}

0 commit comments

Comments
 (0)