vmm: Use a reference counted VmConfig when creating a new VM

Once passed to the VM creation routine, a VmConfig structure is
immutable. We can simply carry a Arc of it instead of a reference.
This also allows us to remove any lifetime bound from our VM.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2019-09-24 16:00:00 +02:00
parent 2e0f1c2afe
commit 2e9d815701
3 changed files with 10 additions and 9 deletions

View File

@ -11,7 +11,7 @@ extern crate clap;
use clap::{App, Arg};
use log::LevelFilter;
use std::process;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};
use vmm::config;
struct Logger {
@ -285,7 +285,7 @@ fn main() {
vm_config.disks,
);
if let Err(e) = vmm::start_vm_loop(vm_config) {
if let Err(e) = vmm::start_vm_loop(Arc::new(vm_config)) {
println!("Guest boot failed: {}", e);
process::exit(1);
}

View File

@ -8,6 +8,7 @@ extern crate log;
use std::fmt::{self, Display};
use std::result;
use std::sync::Arc;
pub mod config;
pub mod device_manager;
@ -38,9 +39,9 @@ impl Display for Error {
}
}
pub fn start_vm_loop(config: VmConfig) -> Result<()> {
pub fn start_vm_loop(config: Arc<VmConfig>) -> Result<()> {
loop {
let mut vm = Vm::new(&config).map_err(Error::VmNew)?;
let mut vm = Vm::new(config.clone()).map_err(Error::VmNew)?;
if vm.start().map_err(Error::VmStart)? == ExitBehaviour::Shutdown {
break;

View File

@ -503,14 +503,14 @@ pub enum ExitBehaviour {
Reset = 2,
}
pub struct Vm<'a> {
pub struct Vm {
fd: Arc<VmFd>,
kernel: File,
memory: Arc<RwLock<GuestMemoryMmap>>,
threads: Vec<thread::JoinHandle<()>>,
devices: DeviceManager,
cpuid: CpuId,
config: &'a VmConfig,
config: Arc<VmConfig>,
epoll: EpollContext,
on_tty: bool,
creation_ts: std::time::Instant,
@ -535,8 +535,8 @@ fn get_host_cpu_phys_bits() -> u8 {
}
}
impl<'a> Vm<'a> {
pub fn new(config: &'a VmConfig) -> Result<Self> {
impl Vm {
pub fn new(config: Arc<VmConfig>) -> Result<Self> {
let kvm = Kvm::new().map_err(Error::KvmNew)?;
let kernel = File::open(&config.kernel.path).map_err(Error::KernelFile)?;
let fd = kvm.create_vm().map_err(Error::VmCreate)?;
@ -717,7 +717,7 @@ impl<'a> Vm<'a> {
let vm_info = VmInfo {
memory: &guest_memory,
vm_fd: &fd,
vm_cfg: config,
vm_cfg: &config,
};
let exit_evt = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?;