Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 1 | // Copyright 2023, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Alice Wang | 734801c | 2023-09-05 11:46:50 +0000 | [diff] [blame] | 15 | //! This module contains the functions to start, stop and communicate with the |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 16 | //! Service VM. |
| 17 | |
| 18 | use android_system_virtualizationservice::{ |
| 19 | aidl::android::system::virtualizationservice::{ |
| 20 | CpuTopology::CpuTopology, DiskImage::DiskImage, |
| 21 | IVirtualizationService::IVirtualizationService, Partition::Partition, |
| 22 | PartitionType::PartitionType, VirtualMachineConfig::VirtualMachineConfig, |
| 23 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 24 | }, |
| 25 | binder::ParcelFileDescriptor, |
| 26 | }; |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 27 | use anyhow::{anyhow, ensure, Context, Result}; |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 28 | use lazy_static::lazy_static; |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 29 | use log::{info, warn}; |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 30 | use service_vm_comm::{Request, Response, ServiceVmRequest, VmType}; |
Alice Wang | 419ff69 | 2024-03-19 15:11:42 +0000 | [diff] [blame] | 31 | use std::fs::{self, File, OpenOptions}; |
| 32 | use std::io::{self, BufRead, BufReader, BufWriter, Read, Write}; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 33 | use std::path::{Path, PathBuf}; |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 34 | use std::sync::{Condvar, Mutex}; |
Alice Wang | 977b64b | 2023-09-07 14:04:26 +0000 | [diff] [blame] | 35 | use std::thread; |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 36 | use std::time::Duration; |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 37 | use vmclient::{DeathReason, VmInstance}; |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 38 | use vsock::{VsockListener, VsockStream, VMADDR_CID_HOST}; |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 39 | |
| 40 | const VIRT_DATA_DIR: &str = "/data/misc/apexdata/com.android.virt"; |
| 41 | const RIALTO_PATH: &str = "/apex/com.android.virt/etc/rialto.bin"; |
| 42 | const INSTANCE_IMG_NAME: &str = "service_vm_instance.img"; |
Alice Wang | 419ff69 | 2024-03-19 15:11:42 +0000 | [diff] [blame] | 43 | const INSTANCE_ID_FILENAME: &str = "service_vm_instance_id"; |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 44 | const INSTANCE_IMG_SIZE_BYTES: i64 = 1 << 20; // 1MB |
| 45 | const MEMORY_MB: i32 = 300; |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 46 | const WRITE_BUFFER_CAPACITY: usize = 512; |
| 47 | const READ_TIMEOUT: Duration = Duration::from_secs(10); |
| 48 | const WRITE_TIMEOUT: Duration = Duration::from_secs(10); |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 49 | lazy_static! { |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 50 | static ref PENDING_REQUESTS: AtomicCounter = AtomicCounter::default(); |
| 51 | static ref SERVICE_VM: Mutex<Option<ServiceVm>> = Mutex::new(None); |
| 52 | static ref SERVICE_VM_SHUTDOWN: Condvar = Condvar::new(); |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 55 | /// Atomic counter with a condition variable that is used to wait for the counter |
| 56 | /// to become positive within a timeout. |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 57 | #[derive(Debug, Default)] |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 58 | struct AtomicCounter { |
| 59 | num: Mutex<usize>, |
| 60 | num_increased: Condvar, |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 63 | impl AtomicCounter { |
| 64 | /// Checks if the counter becomes positive within the given timeout. |
| 65 | fn is_positive_within_timeout(&self, timeout: Duration) -> bool { |
| 66 | let (guard, _wait_result) = self |
| 67 | .num_increased |
| 68 | .wait_timeout_while(self.num.lock().unwrap(), timeout, |&mut x| x == 0) |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 69 | .unwrap(); |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 70 | *guard > 0 |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 73 | fn increment(&self) { |
| 74 | let mut num = self.num.lock().unwrap(); |
| 75 | *num = num.checked_add(1).unwrap(); |
| 76 | self.num_increased.notify_all(); |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 77 | } |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 78 | |
| 79 | fn decrement(&self) { |
| 80 | let mut num = self.num.lock().unwrap(); |
| 81 | *num = num.checked_sub(1).unwrap(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Processes the request in the service VM. |
| 86 | pub fn process_request(request: Request) -> Result<Response> { |
| 87 | PENDING_REQUESTS.increment(); |
| 88 | let result = process_request_in_service_vm(request); |
| 89 | PENDING_REQUESTS.decrement(); |
| 90 | thread::spawn(stop_service_vm_if_idle); |
| 91 | result |
| 92 | } |
| 93 | |
| 94 | fn process_request_in_service_vm(request: Request) -> Result<Response> { |
| 95 | let mut service_vm = SERVICE_VM.lock().unwrap(); |
| 96 | if service_vm.is_none() { |
| 97 | *service_vm = Some(ServiceVm::start()?); |
| 98 | } |
| 99 | service_vm.as_mut().unwrap().process_request(request) |
| 100 | } |
| 101 | |
| 102 | fn stop_service_vm_if_idle() { |
| 103 | if PENDING_REQUESTS.is_positive_within_timeout(Duration::from_secs(1)) { |
| 104 | info!("Service VM has pending requests, keeping it running."); |
| 105 | } else { |
| 106 | info!("Service VM is idle, shutting it down."); |
| 107 | *SERVICE_VM.lock().unwrap() = None; |
| 108 | SERVICE_VM_SHUTDOWN.notify_all(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// Waits until the service VM shuts down. |
| 113 | /// This function is only used in tests. |
| 114 | pub fn wait_until_service_vm_shuts_down() -> Result<()> { |
| 115 | const WAIT_FOR_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5); |
| 116 | |
| 117 | let (_guard, wait_result) = SERVICE_VM_SHUTDOWN |
| 118 | .wait_timeout_while(SERVICE_VM.lock().unwrap(), WAIT_FOR_SHUTDOWN_TIMEOUT, |x| x.is_some()) |
| 119 | .unwrap(); |
| 120 | ensure!(!wait_result.timed_out(), "Service VM didn't shut down within the timeout"); |
| 121 | Ok(()) |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 124 | /// Service VM. |
| 125 | pub struct ServiceVm { |
| 126 | vsock_stream: VsockStream, |
| 127 | /// VmInstance will be dropped when ServiceVm goes out of scope, which will kill the VM. |
| 128 | vm: VmInstance, |
| 129 | } |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 130 | |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 131 | impl ServiceVm { |
| 132 | /// Starts the service VM and returns its instance. |
| 133 | /// The same instance image is used for different VMs. |
Alice Wang | 5daec07 | 2024-03-15 15:31:17 +0000 | [diff] [blame] | 134 | /// TODO(b/27593612): Remove instance image usage for Service VM. |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 135 | pub fn start() -> Result<Self> { |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 136 | let instance_img_path = Path::new(VIRT_DATA_DIR).join(INSTANCE_IMG_NAME); |
| 137 | let vm = protected_vm_instance(instance_img_path)?; |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 138 | |
| 139 | let vm = Self::start_vm(vm, VmType::ProtectedVm)?; |
Alice Wang | 19723ca | 2023-09-08 11:13:52 +0000 | [diff] [blame] | 140 | Ok(vm) |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /// Starts the given VM instance and sets up the vsock connection with it. |
| 144 | /// Returns a `ServiceVm` instance. |
| 145 | /// This function is exposed for testing. |
| 146 | pub fn start_vm(vm: VmInstance, vm_type: VmType) -> Result<Self> { |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 147 | // Sets up the vsock server on the host. |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 148 | let vsock_listener = VsockListener::bind_with_cid_port(VMADDR_CID_HOST, vm_type.port())?; |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 149 | |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 150 | // Starts the service VM. |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 151 | vm.start().context("Failed to start service VM")?; |
| 152 | info!("Service VM started"); |
| 153 | |
| 154 | // Accepts the connection from the service VM. |
| 155 | // TODO(b/299427101): Introduce a timeout for the accept. |
| 156 | let (vsock_stream, peer_addr) = vsock_listener.accept().context("Failed to accept")?; |
| 157 | info!("Accepted connection {:?}", vsock_stream); |
| 158 | ensure!( |
| 159 | peer_addr.cid() == u32::try_from(vm.cid()).unwrap(), |
| 160 | "The CID of the peer address {} doesn't match the service VM CID {}", |
| 161 | peer_addr, |
| 162 | vm.cid() |
| 163 | ); |
| 164 | vsock_stream.set_read_timeout(Some(READ_TIMEOUT))?; |
| 165 | vsock_stream.set_write_timeout(Some(WRITE_TIMEOUT))?; |
| 166 | |
| 167 | Ok(Self { vsock_stream, vm }) |
| 168 | } |
| 169 | |
| 170 | /// Processes the request in the service VM. |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 171 | pub fn process_request(&mut self, request: Request) -> Result<Response> { |
| 172 | self.write_request(&ServiceVmRequest::Process(request))?; |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 173 | self.read_response() |
| 174 | } |
| 175 | |
| 176 | /// Sends the request to the service VM. |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 177 | fn write_request(&mut self, request: &ServiceVmRequest) -> Result<()> { |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 178 | let mut buffer = BufWriter::with_capacity(WRITE_BUFFER_CAPACITY, &mut self.vsock_stream); |
| 179 | ciborium::into_writer(request, &mut buffer)?; |
| 180 | buffer.flush().context("Failed to flush the buffer")?; |
| 181 | info!("Sent request to the service VM."); |
| 182 | Ok(()) |
| 183 | } |
| 184 | |
| 185 | /// Reads the response from the service VM. |
| 186 | fn read_response(&mut self) -> Result<Response> { |
| 187 | let response: Response = ciborium::from_reader(&mut self.vsock_stream) |
| 188 | .context("Failed to read the response from the service VM")?; |
| 189 | info!("Received response from the service VM."); |
| 190 | Ok(response) |
| 191 | } |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 192 | |
| 193 | /// Shuts down the service VM. |
| 194 | fn shutdown(&mut self) -> Result<DeathReason> { |
| 195 | self.write_request(&ServiceVmRequest::Shutdown)?; |
| 196 | self.vm |
| 197 | .wait_for_death_with_timeout(Duration::from_secs(10)) |
| 198 | .ok_or_else(|| anyhow!("Timed out to exit the service VM")) |
| 199 | } |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | impl Drop for ServiceVm { |
| 203 | fn drop(&mut self) { |
| 204 | // Wait till the service VM finishes releasing all the resources. |
Alice Wang | fbdc85b | 2023-09-07 12:56:46 +0000 | [diff] [blame] | 205 | match self.shutdown() { |
| 206 | Ok(reason) => info!("Exit the service VM successfully: {reason:?}"), |
| 207 | Err(e) => warn!("Service VM shutdown request failed '{e:?}', killing it."), |
Alice Wang | a448659 | 2023-09-05 08:25:59 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 212 | /// Returns a `VmInstance` of a protected VM with the instance image from the given path. |
| 213 | pub fn protected_vm_instance(instance_img_path: PathBuf) -> Result<VmInstance> { |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 214 | let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?; |
| 215 | let service = virtmgr.connect().context("Failed to connect to VirtMgr")?; |
| 216 | info!("Connected to VirtMgr for service VM"); |
| 217 | |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 218 | let instance_img = instance_img(service.as_ref(), instance_img_path)?; |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 219 | let writable_partitions = vec![Partition { |
| 220 | label: "vm-instance".to_owned(), |
| 221 | image: Some(instance_img), |
| 222 | writable: true, |
| 223 | }]; |
| 224 | let rialto = File::open(RIALTO_PATH).context("Failed to open Rialto kernel binary")?; |
Alice Wang | 419ff69 | 2024-03-19 15:11:42 +0000 | [diff] [blame] | 225 | let instance_id_file = Path::new(VIRT_DATA_DIR).join(INSTANCE_ID_FILENAME); |
| 226 | let instance_id = get_or_allocate_instance_id(service.as_ref(), instance_id_file)?; |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 227 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
| 228 | name: String::from("Service VM"), |
| 229 | bootloader: Some(ParcelFileDescriptor::new(rialto)), |
| 230 | disks: vec![DiskImage { image: None, partitions: writable_partitions, writable: true }], |
Alice Wang | 419ff69 | 2024-03-19 15:11:42 +0000 | [diff] [blame] | 231 | instanceId: instance_id, |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 232 | protectedVm: true, |
| 233 | memoryMib: MEMORY_MB, |
| 234 | cpuTopology: CpuTopology::ONE_CPU, |
| 235 | platformVersion: "~1.0".to_string(), |
| 236 | gdbPort: 0, // No gdb |
| 237 | ..Default::default() |
| 238 | }); |
Alice Wang | 977b64b | 2023-09-07 14:04:26 +0000 | [diff] [blame] | 239 | let console_out = Some(android_log_fd()?); |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 240 | let console_in = None; |
Alice Wang | 977b64b | 2023-09-07 14:04:26 +0000 | [diff] [blame] | 241 | let log = Some(android_log_fd()?); |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 242 | let callback = None; |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 243 | VmInstance::create(service.as_ref(), &config, console_out, console_in, log, callback) |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 244 | .context("Failed to create service VM") |
| 245 | } |
| 246 | |
Alice Wang | 419ff69 | 2024-03-19 15:11:42 +0000 | [diff] [blame] | 247 | /// TODO(b/291213394): Reuse this method in other places such as vm and compos. |
| 248 | fn get_or_allocate_instance_id( |
| 249 | service: &dyn IVirtualizationService, |
| 250 | instance_id_file: PathBuf, |
| 251 | ) -> Result<[u8; 64]> { |
| 252 | let mut instance_id = [0; 64]; |
| 253 | if instance_id_file.exists() { |
| 254 | let mut file = File::open(&instance_id_file)?; |
| 255 | file.read_exact(&mut instance_id)?; |
| 256 | } else { |
| 257 | info!("Allocating a new instance ID for the Service VM"); |
| 258 | instance_id = service.allocateInstanceId()?; |
| 259 | fs::write(instance_id_file, instance_id)?; |
| 260 | } |
| 261 | Ok(instance_id) |
| 262 | } |
| 263 | |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 264 | /// Returns the file descriptor of the instance image at the given path. |
Alice Wang | a635769 | 2023-09-07 14:59:37 +0000 | [diff] [blame] | 265 | fn instance_img( |
Alice Wang | 17dc76e | 2023-09-06 09:43:52 +0000 | [diff] [blame] | 266 | service: &dyn IVirtualizationService, |
| 267 | instance_img_path: PathBuf, |
| 268 | ) -> Result<ParcelFileDescriptor> { |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 269 | if instance_img_path.exists() { |
| 270 | // TODO(b/298174584): Try to recover if the service VM is triggered by rkpd. |
| 271 | return Ok(OpenOptions::new() |
| 272 | .read(true) |
| 273 | .write(true) |
| 274 | .open(instance_img_path) |
| 275 | .map(ParcelFileDescriptor::new)?); |
| 276 | } |
| 277 | let instance_img = OpenOptions::new() |
| 278 | .create(true) |
Charisee | 13fc9ee | 2024-03-20 19:05:10 +0000 | [diff] [blame] | 279 | .truncate(true) |
Alice Wang | c206b9b | 2023-08-28 14:13:51 +0000 | [diff] [blame] | 280 | .read(true) |
| 281 | .write(true) |
| 282 | .open(instance_img_path) |
| 283 | .map(ParcelFileDescriptor::new)?; |
| 284 | service.initializeWritablePartition( |
| 285 | &instance_img, |
| 286 | INSTANCE_IMG_SIZE_BYTES, |
| 287 | PartitionType::ANDROID_VM_INSTANCE, |
| 288 | )?; |
| 289 | Ok(instance_img) |
| 290 | } |
Alice Wang | 977b64b | 2023-09-07 14:04:26 +0000 | [diff] [blame] | 291 | |
| 292 | /// This function is only exposed for testing. |
| 293 | pub fn android_log_fd() -> io::Result<File> { |
| 294 | let (reader_fd, writer_fd) = nix::unistd::pipe()?; |
| 295 | |
Frederick Mayle | fbbcfcd | 2024-04-08 16:31:54 -0700 | [diff] [blame] | 296 | let reader = File::from(reader_fd); |
| 297 | let writer = File::from(writer_fd); |
Alice Wang | 977b64b | 2023-09-07 14:04:26 +0000 | [diff] [blame] | 298 | |
| 299 | thread::spawn(|| { |
| 300 | for line in BufReader::new(reader).lines() { |
| 301 | match line { |
| 302 | Ok(l) => info!("{}", l), |
| 303 | Err(e) => { |
| 304 | warn!("Failed to read line: {e:?}"); |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | }); |
| 310 | Ok(writer) |
| 311 | } |