blob: 273b54d260707b07f0e49329f6c964137f296865 [file] [log] [blame]
Alice Wangc206b9b2023-08-28 14:13:51 +00001// 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 Wang734801c2023-09-05 11:46:50 +000015//! This module contains the functions to start, stop and communicate with the
Alice Wangc206b9b2023-08-28 14:13:51 +000016//! Service VM.
17
18use 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 Wangfbdc85b2023-09-07 12:56:46 +000027use anyhow::{anyhow, ensure, Context, Result};
Alice Wang19723ca2023-09-08 11:13:52 +000028use lazy_static::lazy_static;
Alice Wanga4486592023-09-05 08:25:59 +000029use log::{info, warn};
Alice Wangfbdc85b2023-09-07 12:56:46 +000030use service_vm_comm::{Request, Response, ServiceVmRequest, VmType};
Alice Wang419ff692024-03-19 15:11:42 +000031use std::fs::{self, File, OpenOptions};
32use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
Alice Wang977b64b2023-09-07 14:04:26 +000033use std::os::unix::io::FromRawFd;
Alice Wang17dc76e2023-09-06 09:43:52 +000034use std::path::{Path, PathBuf};
Alice Wang5daec072024-03-15 15:31:17 +000035use std::sync::{Condvar, Mutex};
Alice Wang977b64b2023-09-07 14:04:26 +000036use std::thread;
Alice Wanga4486592023-09-05 08:25:59 +000037use std::time::Duration;
Alice Wangfbdc85b2023-09-07 12:56:46 +000038use vmclient::{DeathReason, VmInstance};
Alice Wanga4486592023-09-05 08:25:59 +000039use vsock::{VsockListener, VsockStream, VMADDR_CID_HOST};
Alice Wangc206b9b2023-08-28 14:13:51 +000040
41const VIRT_DATA_DIR: &str = "/data/misc/apexdata/com.android.virt";
42const RIALTO_PATH: &str = "/apex/com.android.virt/etc/rialto.bin";
43const INSTANCE_IMG_NAME: &str = "service_vm_instance.img";
Alice Wang419ff692024-03-19 15:11:42 +000044const INSTANCE_ID_FILENAME: &str = "service_vm_instance_id";
Alice Wangc206b9b2023-08-28 14:13:51 +000045const INSTANCE_IMG_SIZE_BYTES: i64 = 1 << 20; // 1MB
46const MEMORY_MB: i32 = 300;
Alice Wanga4486592023-09-05 08:25:59 +000047const WRITE_BUFFER_CAPACITY: usize = 512;
48const READ_TIMEOUT: Duration = Duration::from_secs(10);
49const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
Alice Wang19723ca2023-09-08 11:13:52 +000050lazy_static! {
Alice Wang5daec072024-03-15 15:31:17 +000051 static ref PENDING_REQUESTS: AtomicCounter = AtomicCounter::default();
52 static ref SERVICE_VM: Mutex<Option<ServiceVm>> = Mutex::new(None);
53 static ref SERVICE_VM_SHUTDOWN: Condvar = Condvar::new();
Alice Wang19723ca2023-09-08 11:13:52 +000054}
55
Alice Wang5daec072024-03-15 15:31:17 +000056/// Atomic counter with a condition variable that is used to wait for the counter
57/// to become positive within a timeout.
Alice Wang19723ca2023-09-08 11:13:52 +000058#[derive(Debug, Default)]
Alice Wang5daec072024-03-15 15:31:17 +000059struct AtomicCounter {
60 num: Mutex<usize>,
61 num_increased: Condvar,
Alice Wang19723ca2023-09-08 11:13:52 +000062}
63
Alice Wang5daec072024-03-15 15:31:17 +000064impl AtomicCounter {
65 /// Checks if the counter becomes positive within the given timeout.
66 fn is_positive_within_timeout(&self, timeout: Duration) -> bool {
67 let (guard, _wait_result) = self
68 .num_increased
69 .wait_timeout_while(self.num.lock().unwrap(), timeout, |&mut x| x == 0)
Alice Wang19723ca2023-09-08 11:13:52 +000070 .unwrap();
Alice Wang5daec072024-03-15 15:31:17 +000071 *guard > 0
Alice Wang19723ca2023-09-08 11:13:52 +000072 }
73
Alice Wang5daec072024-03-15 15:31:17 +000074 fn increment(&self) {
75 let mut num = self.num.lock().unwrap();
76 *num = num.checked_add(1).unwrap();
77 self.num_increased.notify_all();
Alice Wang19723ca2023-09-08 11:13:52 +000078 }
Alice Wang5daec072024-03-15 15:31:17 +000079
80 fn decrement(&self) {
81 let mut num = self.num.lock().unwrap();
82 *num = num.checked_sub(1).unwrap();
83 }
84}
85
86/// Processes the request in the service VM.
87pub fn process_request(request: Request) -> Result<Response> {
88 PENDING_REQUESTS.increment();
89 let result = process_request_in_service_vm(request);
90 PENDING_REQUESTS.decrement();
91 thread::spawn(stop_service_vm_if_idle);
92 result
93}
94
95fn process_request_in_service_vm(request: Request) -> Result<Response> {
96 let mut service_vm = SERVICE_VM.lock().unwrap();
97 if service_vm.is_none() {
98 *service_vm = Some(ServiceVm::start()?);
99 }
100 service_vm.as_mut().unwrap().process_request(request)
101}
102
103fn stop_service_vm_if_idle() {
104 if PENDING_REQUESTS.is_positive_within_timeout(Duration::from_secs(1)) {
105 info!("Service VM has pending requests, keeping it running.");
106 } else {
107 info!("Service VM is idle, shutting it down.");
108 *SERVICE_VM.lock().unwrap() = None;
109 SERVICE_VM_SHUTDOWN.notify_all();
110 }
111}
112
113/// Waits until the service VM shuts down.
114/// This function is only used in tests.
115pub fn wait_until_service_vm_shuts_down() -> Result<()> {
116 const WAIT_FOR_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
117
118 let (_guard, wait_result) = SERVICE_VM_SHUTDOWN
119 .wait_timeout_while(SERVICE_VM.lock().unwrap(), WAIT_FOR_SHUTDOWN_TIMEOUT, |x| x.is_some())
120 .unwrap();
121 ensure!(!wait_result.timed_out(), "Service VM didn't shut down within the timeout");
122 Ok(())
Alice Wang19723ca2023-09-08 11:13:52 +0000123}
124
Alice Wanga4486592023-09-05 08:25:59 +0000125/// Service VM.
126pub struct ServiceVm {
127 vsock_stream: VsockStream,
128 /// VmInstance will be dropped when ServiceVm goes out of scope, which will kill the VM.
129 vm: VmInstance,
130}
Alice Wangc206b9b2023-08-28 14:13:51 +0000131
Alice Wanga4486592023-09-05 08:25:59 +0000132impl ServiceVm {
133 /// Starts the service VM and returns its instance.
134 /// The same instance image is used for different VMs.
Alice Wang5daec072024-03-15 15:31:17 +0000135 /// TODO(b/27593612): Remove instance image usage for Service VM.
Alice Wanga4486592023-09-05 08:25:59 +0000136 pub fn start() -> Result<Self> {
Alice Wanga6357692023-09-07 14:59:37 +0000137 let instance_img_path = Path::new(VIRT_DATA_DIR).join(INSTANCE_IMG_NAME);
138 let vm = protected_vm_instance(instance_img_path)?;
Alice Wang19723ca2023-09-08 11:13:52 +0000139
140 let vm = Self::start_vm(vm, VmType::ProtectedVm)?;
Alice Wang19723ca2023-09-08 11:13:52 +0000141 Ok(vm)
Alice Wang17dc76e2023-09-06 09:43:52 +0000142 }
143
144 /// Starts the given VM instance and sets up the vsock connection with it.
145 /// Returns a `ServiceVm` instance.
146 /// This function is exposed for testing.
147 pub fn start_vm(vm: VmInstance, vm_type: VmType) -> Result<Self> {
Alice Wanga4486592023-09-05 08:25:59 +0000148 // Sets up the vsock server on the host.
Alice Wang17dc76e2023-09-06 09:43:52 +0000149 let vsock_listener = VsockListener::bind_with_cid_port(VMADDR_CID_HOST, vm_type.port())?;
Alice Wangc206b9b2023-08-28 14:13:51 +0000150
Alice Wanga4486592023-09-05 08:25:59 +0000151 // Starts the service VM.
Alice Wanga4486592023-09-05 08:25:59 +0000152 vm.start().context("Failed to start service VM")?;
153 info!("Service VM started");
154
155 // Accepts the connection from the service VM.
156 // TODO(b/299427101): Introduce a timeout for the accept.
157 let (vsock_stream, peer_addr) = vsock_listener.accept().context("Failed to accept")?;
158 info!("Accepted connection {:?}", vsock_stream);
159 ensure!(
160 peer_addr.cid() == u32::try_from(vm.cid()).unwrap(),
161 "The CID of the peer address {} doesn't match the service VM CID {}",
162 peer_addr,
163 vm.cid()
164 );
165 vsock_stream.set_read_timeout(Some(READ_TIMEOUT))?;
166 vsock_stream.set_write_timeout(Some(WRITE_TIMEOUT))?;
167
168 Ok(Self { vsock_stream, vm })
169 }
170
171 /// Processes the request in the service VM.
Alice Wangfbdc85b2023-09-07 12:56:46 +0000172 pub fn process_request(&mut self, request: Request) -> Result<Response> {
173 self.write_request(&ServiceVmRequest::Process(request))?;
Alice Wanga4486592023-09-05 08:25:59 +0000174 self.read_response()
175 }
176
177 /// Sends the request to the service VM.
Alice Wangfbdc85b2023-09-07 12:56:46 +0000178 fn write_request(&mut self, request: &ServiceVmRequest) -> Result<()> {
Alice Wanga4486592023-09-05 08:25:59 +0000179 let mut buffer = BufWriter::with_capacity(WRITE_BUFFER_CAPACITY, &mut self.vsock_stream);
180 ciborium::into_writer(request, &mut buffer)?;
181 buffer.flush().context("Failed to flush the buffer")?;
182 info!("Sent request to the service VM.");
183 Ok(())
184 }
185
186 /// Reads the response from the service VM.
187 fn read_response(&mut self) -> Result<Response> {
188 let response: Response = ciborium::from_reader(&mut self.vsock_stream)
189 .context("Failed to read the response from the service VM")?;
190 info!("Received response from the service VM.");
191 Ok(response)
192 }
Alice Wangfbdc85b2023-09-07 12:56:46 +0000193
194 /// Shuts down the service VM.
195 fn shutdown(&mut self) -> Result<DeathReason> {
196 self.write_request(&ServiceVmRequest::Shutdown)?;
197 self.vm
198 .wait_for_death_with_timeout(Duration::from_secs(10))
199 .ok_or_else(|| anyhow!("Timed out to exit the service VM"))
200 }
Alice Wanga4486592023-09-05 08:25:59 +0000201}
202
203impl Drop for ServiceVm {
204 fn drop(&mut self) {
205 // Wait till the service VM finishes releasing all the resources.
Alice Wangfbdc85b2023-09-07 12:56:46 +0000206 match self.shutdown() {
207 Ok(reason) => info!("Exit the service VM successfully: {reason:?}"),
208 Err(e) => warn!("Service VM shutdown request failed '{e:?}', killing it."),
Alice Wanga4486592023-09-05 08:25:59 +0000209 }
210 }
Alice Wangc206b9b2023-08-28 14:13:51 +0000211}
212
Alice Wanga6357692023-09-07 14:59:37 +0000213/// Returns a `VmInstance` of a protected VM with the instance image from the given path.
214pub fn protected_vm_instance(instance_img_path: PathBuf) -> Result<VmInstance> {
Alice Wang17dc76e2023-09-06 09:43:52 +0000215 let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
216 let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
217 info!("Connected to VirtMgr for service VM");
218
Alice Wang17dc76e2023-09-06 09:43:52 +0000219 let instance_img = instance_img(service.as_ref(), instance_img_path)?;
Alice Wangc206b9b2023-08-28 14:13:51 +0000220 let writable_partitions = vec![Partition {
221 label: "vm-instance".to_owned(),
222 image: Some(instance_img),
223 writable: true,
224 }];
225 let rialto = File::open(RIALTO_PATH).context("Failed to open Rialto kernel binary")?;
Alice Wang419ff692024-03-19 15:11:42 +0000226 let instance_id_file = Path::new(VIRT_DATA_DIR).join(INSTANCE_ID_FILENAME);
227 let instance_id = get_or_allocate_instance_id(service.as_ref(), instance_id_file)?;
Alice Wangc206b9b2023-08-28 14:13:51 +0000228 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
229 name: String::from("Service VM"),
230 bootloader: Some(ParcelFileDescriptor::new(rialto)),
231 disks: vec![DiskImage { image: None, partitions: writable_partitions, writable: true }],
Alice Wang419ff692024-03-19 15:11:42 +0000232 instanceId: instance_id,
Alice Wangc206b9b2023-08-28 14:13:51 +0000233 protectedVm: true,
234 memoryMib: MEMORY_MB,
235 cpuTopology: CpuTopology::ONE_CPU,
236 platformVersion: "~1.0".to_string(),
237 gdbPort: 0, // No gdb
238 ..Default::default()
239 });
Alice Wang977b64b2023-09-07 14:04:26 +0000240 let console_out = Some(android_log_fd()?);
Alice Wangc206b9b2023-08-28 14:13:51 +0000241 let console_in = None;
Alice Wang977b64b2023-09-07 14:04:26 +0000242 let log = Some(android_log_fd()?);
Alice Wangc206b9b2023-08-28 14:13:51 +0000243 let callback = None;
Alice Wang17dc76e2023-09-06 09:43:52 +0000244 VmInstance::create(service.as_ref(), &config, console_out, console_in, log, callback)
Alice Wangc206b9b2023-08-28 14:13:51 +0000245 .context("Failed to create service VM")
246}
247
Alice Wang419ff692024-03-19 15:11:42 +0000248/// TODO(b/291213394): Reuse this method in other places such as vm and compos.
249fn get_or_allocate_instance_id(
250 service: &dyn IVirtualizationService,
251 instance_id_file: PathBuf,
252) -> Result<[u8; 64]> {
253 let mut instance_id = [0; 64];
254 if instance_id_file.exists() {
255 let mut file = File::open(&instance_id_file)?;
256 file.read_exact(&mut instance_id)?;
257 } else {
258 info!("Allocating a new instance ID for the Service VM");
259 instance_id = service.allocateInstanceId()?;
260 fs::write(instance_id_file, instance_id)?;
261 }
262 Ok(instance_id)
263}
264
Alice Wang17dc76e2023-09-06 09:43:52 +0000265/// Returns the file descriptor of the instance image at the given path.
Alice Wanga6357692023-09-07 14:59:37 +0000266fn instance_img(
Alice Wang17dc76e2023-09-06 09:43:52 +0000267 service: &dyn IVirtualizationService,
268 instance_img_path: PathBuf,
269) -> Result<ParcelFileDescriptor> {
Alice Wangc206b9b2023-08-28 14:13:51 +0000270 if instance_img_path.exists() {
271 // TODO(b/298174584): Try to recover if the service VM is triggered by rkpd.
272 return Ok(OpenOptions::new()
273 .read(true)
274 .write(true)
275 .open(instance_img_path)
276 .map(ParcelFileDescriptor::new)?);
277 }
278 let instance_img = OpenOptions::new()
279 .create(true)
Charisee13fc9ee2024-03-20 19:05:10 +0000280 .truncate(true)
Alice Wangc206b9b2023-08-28 14:13:51 +0000281 .read(true)
282 .write(true)
283 .open(instance_img_path)
284 .map(ParcelFileDescriptor::new)?;
285 service.initializeWritablePartition(
286 &instance_img,
287 INSTANCE_IMG_SIZE_BYTES,
288 PartitionType::ANDROID_VM_INSTANCE,
289 )?;
290 Ok(instance_img)
291}
Alice Wang977b64b2023-09-07 14:04:26 +0000292
293/// This function is only exposed for testing.
294pub fn android_log_fd() -> io::Result<File> {
295 let (reader_fd, writer_fd) = nix::unistd::pipe()?;
296
297 // SAFETY: These are new FDs with no previous owner.
298 let reader = unsafe { File::from_raw_fd(reader_fd) };
299 // SAFETY: These are new FDs with no previous owner.
300 let writer = unsafe { File::from_raw_fd(writer_fd) };
301
302 thread::spawn(|| {
303 for line in BufReader::new(reader).lines() {
304 match line {
305 Ok(l) => info!("{}", l),
306 Err(e) => {
307 warn!("Failed to read line: {e:?}");
308 break;
309 }
310 }
311 }
312 });
313 Ok(writer)
314}