From e493a7cf1cf42aa2784b4fa4203a4f2c667b12dc Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Sat, 16 Nov 2024 21:30:46 -0800 Subject: [PATCH] fix(vm): let device builders take immutable self Signed-off-by: Changyuan Lyu --- alioth-cli/src/main.rs | 2 +- alioth/src/vm.rs | 37 +++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/alioth-cli/src/main.rs b/alioth-cli/src/main.rs index fcaef87..a14e589 100644 --- a/alioth-cli/src/main.rs +++ b/alioth-cli/src/main.rs @@ -329,7 +329,7 @@ fn main_run(args: RunArgs) -> Result<(), Error> { num_cpu: args.num_cpu, coco, }; - let mut vm = Machine::new(hypervisor, board_config).context(error::CreateVm)?; + let vm = Machine::new(hypervisor, board_config).context(error::CreateVm)?; #[cfg(target_arch = "x86_64")] vm.add_com1().context(error::CreateDevice)?; #[cfg(target_arch = "aarch64")] diff --git a/alioth/src/vm.rs b/alioth/src/vm.rs index 8052c15..93c94c6 100644 --- a/alioth/src/vm.rs +++ b/alioth/src/vm.rs @@ -109,7 +109,7 @@ where { board: Arc>, #[cfg(target_os = "linux")] - iommu: Option>, + iommu: Mutex>>, event_rx: Receiver, _event_tx: Sender, } @@ -177,7 +177,7 @@ where event_rx, _event_tx: event_tx, #[cfg(target_os = "linux")] - iommu: None, + iommu: Mutex::new(None), }; Ok(machine) @@ -205,7 +205,7 @@ where Ok(()) } - pub fn add_pci_dev(&mut self, bdf: Option, dev: PciDevice) -> Result<(), Error> { + pub fn add_pci_dev(&self, bdf: Option, dev: PciDevice) -> Result<(), Error> { let name = dev.name.clone(); let bdf = if let Some(bdf) = bdf { bdf @@ -220,7 +220,7 @@ where Ok(()) } - pub fn add_pvpanic(&mut self) -> Result<(), Error> { + pub fn add_pvpanic(&self) -> Result<(), Error> { let dev = PvPanic::new(); let pci_dev = PciDevice::new("pvpanic", Arc::new(dev)); self.add_pci_dev(None, pci_dev) @@ -228,7 +228,7 @@ where #[cfg(target_arch = "x86_64")] pub fn add_fw_cfg( - &mut self, + &self, params: impl Iterator, ) -> Result>, Error> { let items = params @@ -245,7 +245,7 @@ where } pub fn add_virtio_dev( - &mut self, + &self, name: impl Into>, param: P, ) -> Result>, Error> @@ -284,11 +284,11 @@ where Ok(dev) } - pub fn add_payload(&mut self, payload: Payload) { + pub fn add_payload(&self, payload: Payload) { *self.board.payload.write() = Some(payload) } - pub fn boot(&mut self) -> Result<(), Error> { + pub fn boot(&self) -> Result<(), Error> { let vcpus = self.board.vcpus.read(); self.board.state.store(STATE_RUNNING, Ordering::Release); for (_, boot_tx) in vcpus.iter() { @@ -297,7 +297,7 @@ where Ok(()) } - pub fn wait(&mut self) -> Vec> { + pub fn wait(&self) -> Vec> { self.event_rx.recv().unwrap(); let vcpus = self.board.vcpus.read(); for _ in 1..vcpus.len() { @@ -323,12 +323,13 @@ where impl Machine { const DEFAULT_NAME: &str = "default"; - pub fn add_vfio_ioas(&mut self, param: IoasParam) -> Result, Error> { + pub fn add_vfio_ioas(&self, param: IoasParam) -> Result, Error> { let mut ioases = self.board.vfio_ioases.lock(); if ioases.contains_key(¶m.name) { return error::AlreadyExists { name: param.name }.fail(); } - let iommu = if let Some(iommu) = &self.iommu { + let maybe_iommu = &mut *self.iommu.lock(); + let iommu = if let Some(iommu) = maybe_iommu { iommu.clone() } else { let iommu_path = if let Some(dev_iommu) = ¶m.dev_iommu { @@ -337,7 +338,7 @@ impl Machine { Path::new("/dev/iommu") }; let iommu = Arc::new(Iommu::new(iommu_path)?); - self.iommu.replace(iommu.clone()); + maybe_iommu.replace(iommu.clone()); iommu }; let ioas = Arc::new(Ioas::alloc_on(iommu)?); @@ -347,7 +348,7 @@ impl Machine { Ok(ioas) } - fn get_ioas(&mut self, name: Option<&str>) -> Result> { + fn get_ioas(&self, name: Option<&str>) -> Result> { let ioas_name = name.unwrap_or(Self::DEFAULT_NAME); if let Some(ioas) = self.board.vfio_ioases.lock().get(ioas_name) { return Ok(ioas.clone()); @@ -362,7 +363,7 @@ impl Machine { } } - pub fn add_vfio_cdev(&mut self, name: Arc, param: CdevParam) -> Result<(), Error> { + pub fn add_vfio_cdev(&self, name: Arc, param: CdevParam) -> Result<(), Error> { let ioas = self.get_ioas(param.ioas.as_deref())?; let mut cdev = Cdev::new(¶m.path)?; @@ -379,7 +380,7 @@ impl Machine { Ok(()) } - pub fn add_vfio_container(&mut self, param: ContainerParam) -> Result, Error> { + pub fn add_vfio_container(&self, param: ContainerParam) -> Result, Error> { let mut containers = self.board.vfio_containers.lock(); if containers.contains_key(¶m.name) { return error::AlreadyExists { name: param.name }.fail(); @@ -398,7 +399,7 @@ impl Machine { Ok(container) } - fn get_container(&mut self, name: Option<&str>) -> Result> { + fn get_container(&self, name: Option<&str>) -> Result> { let container_name = name.unwrap_or(Self::DEFAULT_NAME); if let Some(container) = self.board.vfio_containers.lock().get(container_name) { return Ok(container.clone()); @@ -416,7 +417,7 @@ impl Machine { } } - pub fn add_vfio_devs_in_group(&mut self, name: &str, param: GroupParam) -> Result<()> { + pub fn add_vfio_devs_in_group(&self, name: &str, param: GroupParam) -> Result<()> { let container = self.get_container(param.container.as_deref())?; let mut group = Group::new(¶m.path)?; group.attach(container, VfioIommu::TYPE1_V2)?; @@ -431,7 +432,7 @@ impl Machine { Ok(()) } - fn add_vfio_devfd(&mut self, name: Arc, devfd: DevFd) -> Result<()> { + fn add_vfio_devfd(&self, name: Arc, devfd: DevFd) -> Result<()> { let bdf = self.board.pci_bus.reserve(None, name.clone()).unwrap(); let msi_sender = self.board.vm.create_msi_sender( #[cfg(target_arch = "aarch64")]