blob: a250c35a4e46f31a3b8f3df4ef255c3fcfd67968 [file] [log] [blame]
Andrew Walbranea9fa482021-03-04 16:11:12 +00001// Copyright 2021, 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
15//! Android VM control tool.
16
Jooyung Hanc221c052022-02-22 05:20:15 +090017mod create_idsig;
Jiyong Park48b354d2021-07-15 15:04:38 +090018mod create_partition;
Andrew Walbranf395b822021-05-05 10:38:59 +000019mod run;
Andrew Walbranea9fa482021-03-04 16:11:12 +000020
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090021use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
David Brazdil7d1e5ec2023-02-06 17:56:29 +000022 CpuTopology::CpuTopology, IVirtualizationService::IVirtualizationService,
23 PartitionType::PartitionType, VirtualMachineAppConfig::DebugLevel::DebugLevel,
Jiyong Parkc2a49cc2021-10-15 00:02:12 +090024};
Shikha Panwar61a74b52024-02-16 13:17:01 +000025#[cfg(not(llpvm_changes))]
26use anyhow::anyhow;
David Brazdil20412d92021-03-18 10:53:06 +000027use anyhow::{Context, Error};
Alan Stokesc4d5def2023-02-14 17:01:59 +000028use binder::{ProcessState, Strong};
Jiyong Parkb1935ef2023-08-10 17:22:39 +090029use clap::{Args, Parser};
Jooyung Hanc221c052022-02-22 05:20:15 +090030use create_idsig::command_create_idsig;
Jiyong Park48b354d2021-07-15 15:04:38 +090031use create_partition::command_create_partition;
Nikita Ioffeb0b67562022-11-22 15:48:06 +000032use run::{command_run, command_run_app, command_run_microdroid};
Jaewan Kim0c99c612024-03-23 00:44:14 +090033use serde::Serialize;
Nikita Ioffe5776f082023-02-10 21:38:26 +000034use std::num::NonZeroU16;
Andrew Walbranc4b1bde2022-02-03 15:26:02 +000035use std::path::{Path, PathBuf};
Andrew Walbranea9fa482021-03-04 16:11:12 +000036
Alan Stokesfda70842023-12-20 17:50:14 +000037#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090038/// Collection of flags that are at VM level and therefore applicable to all subcommands
39pub struct CommonConfig {
40 /// Name of VM
41 #[arg(long)]
42 name: Option<String>,
43
44 /// Run VM with vCPU topology matching that of the host. If unspecified, defaults to 1 vCPU.
45 #[arg(long, default_value = "one_cpu", value_parser = parse_cpu_topology)]
46 cpu_topology: CpuTopology,
47
Jiyong Parkb1935ef2023-08-10 17:22:39 +090048 /// Memory size (in MiB) of the VM. If unspecified, defaults to the value of `memory_mib`
49 /// in the VM config file.
50 #[arg(short, long)]
51 mem: Option<u32>,
52
53 /// Run VM in protected mode.
54 #[arg(short, long)]
55 protected: bool,
Vincent Donnefort538a2c62024-03-20 16:01:10 +000056
57 /// Ask the kernel for transparent huge-pages (THP). This is only a hint and
58 /// the kernel will allocate THP-backed memory only if globally enabled by
59 /// the system and if any can be found. See
60 /// https://docs.kernel.org/admin-guide/mm/transhuge.html
61 #[arg(short, long)]
62 hugepages: bool,
Seungjae Yoo13af0b62024-05-20 14:15:13 +090063
64 /// Run VM with network feature.
65 #[cfg(network)]
66 #[arg(short, long)]
67 network_supported: bool,
David Dai23cff712024-06-13 19:23:45 +000068
69 /// Boost uclamp to stablise results for benchmarks.
70 #[arg(short, long)]
71 boost_uclamp: bool,
Seungjae Yoo13af0b62024-05-20 14:15:13 +090072}
73
74impl CommonConfig {
75 #[cfg(network)]
76 fn network_supported(&self) -> bool {
77 self.network_supported
78 }
79
80 #[cfg(not(network))]
81 fn network_supported(&self) -> bool {
82 false
83 }
Jiyong Parkb1935ef2023-08-10 17:22:39 +090084}
85
Alan Stokesfda70842023-12-20 17:50:14 +000086#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +090087/// Collection of flags for debugging
88pub struct DebugConfig {
89 /// Debug level of the VM. Supported values: "full" (default), and "none".
90 #[arg(long, default_value = "full", value_parser = parse_debug_level)]
91 debug: DebugLevel,
92
93 /// Path to file for VM console output.
94 #[arg(long)]
95 console: Option<PathBuf>,
96
97 /// Path to file for VM console input.
98 #[arg(long)]
99 console_in: Option<PathBuf>,
100
101 /// Path to file for VM log output.
102 #[arg(long)]
103 log: Option<PathBuf>,
104
105 /// Port at which crosvm will start a gdb server to debug guest kernel.
106 /// Note: this is only supported on Android kernels android14-5.15 and higher.
107 #[arg(long)]
108 gdb: Option<NonZeroU16>,
109}
110
Alan Stokesfda70842023-12-20 17:50:14 +0000111#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900112/// Collection of flags that are Microdroid specific
113pub struct MicrodroidConfig {
114 /// Path to the file backing the storage.
115 /// Created if the option is used but the path does not exist in the device.
116 #[arg(long)]
117 storage: Option<PathBuf>,
118
119 /// Size of the storage. Used only if --storage is supplied but path does not exist
120 /// Default size is 10*1024*1024
121 #[arg(long)]
122 storage_size: Option<u64>,
123
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900124 /// Path to disk image containing vendor-specific modules.
Nikita Ioffe631717e2023-09-05 13:38:07 +0100125 #[cfg(vendor_modules)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900126 #[arg(long)]
127 vendor: Option<PathBuf>,
128
129 /// SysFS nodes of devices to assign to VM
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000130 #[cfg(device_assignment)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900131 #[arg(long)]
132 devices: Vec<PathBuf>,
Inseob Kim172f9eb2023-11-06 17:02:08 +0900133
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900134 /// Version of GKI to use. If set, use instead of microdroid kernel
Inseob Kim172f9eb2023-11-06 17:02:08 +0900135 #[cfg(vendor_modules)]
136 #[arg(long)]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900137 gki: Option<String>,
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900138}
139
Nikita Ioffe631717e2023-09-05 13:38:07 +0100140impl MicrodroidConfig {
141 #[cfg(vendor_modules)]
Nikita Ioffe631717e2023-09-05 13:38:07 +0100142 fn vendor(&self) -> &Option<PathBuf> {
143 &self.vendor
144 }
145
146 #[cfg(not(vendor_modules))]
Nikita Ioffe631717e2023-09-05 13:38:07 +0100147 fn vendor(&self) -> Option<PathBuf> {
148 None
149 }
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000150
Inseob Kim172f9eb2023-11-06 17:02:08 +0900151 #[cfg(vendor_modules)]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900152 fn gki(&self) -> Option<&str> {
153 self.gki.as_deref()
Inseob Kim172f9eb2023-11-06 17:02:08 +0900154 }
155
156 #[cfg(not(vendor_modules))]
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900157 fn gki(&self) -> Option<&str> {
158 None
Inseob Kim172f9eb2023-11-06 17:02:08 +0900159 }
160
Nikita Ioffe94a8a182023-11-16 16:37:48 +0000161 #[cfg(device_assignment)]
162 fn devices(&self) -> &Vec<PathBuf> {
163 &self.devices
164 }
165
166 #[cfg(not(device_assignment))]
167 fn devices(&self) -> Vec<PathBuf> {
168 Vec::new()
169 }
Nikita Ioffe631717e2023-09-05 13:38:07 +0100170}
171
Alan Stokesfda70842023-12-20 17:50:14 +0000172#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900173/// Flags for the run_app subcommand
174pub struct RunAppConfig {
175 #[command(flatten)]
176 common: CommonConfig,
177
178 #[command(flatten)]
179 debug: DebugConfig,
180
181 #[command(flatten)]
182 microdroid: MicrodroidConfig,
183
184 /// Path to VM Payload APK
185 apk: PathBuf,
186
187 /// Path to idsig of the APK
188 idsig: PathBuf,
189
190 /// Path to the instance image. Created if not exists.
191 instance: PathBuf,
192
Shikha Panwar61a74b52024-02-16 13:17:01 +0000193 /// Path to file containing instance_id. Required iff llpvm feature is enabled.
194 #[cfg(llpvm_changes)]
195 #[arg(long = "instance-id-file")]
196 instance_id: PathBuf,
197
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900198 /// Path to VM config JSON within APK (e.g. assets/vm_config.json)
199 #[arg(long)]
200 config_path: Option<String>,
201
202 /// Name of VM payload binary within APK (e.g. MicrodroidTestNativeLib.so)
203 #[arg(long)]
204 #[arg(alias = "payload_path")]
205 payload_binary_name: Option<String>,
206
Alan Stokesfda70842023-12-20 17:50:14 +0000207 /// Paths to extra apk files.
208 #[cfg(multi_tenant)]
209 #[arg(long = "extra-apk")]
210 #[clap(conflicts_with = "config_path")]
211 extra_apks: Vec<PathBuf>,
212
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900213 /// Paths to extra idsig files.
214 #[arg(long = "extra-idsig")]
215 extra_idsigs: Vec<PathBuf>,
216}
217
Alan Stokesfda70842023-12-20 17:50:14 +0000218impl RunAppConfig {
219 #[cfg(multi_tenant)]
220 fn extra_apks(&self) -> &[PathBuf] {
221 &self.extra_apks
222 }
223
224 #[cfg(not(multi_tenant))]
225 fn extra_apks(&self) -> &[PathBuf] {
226 &[]
227 }
Shikha Panwar61a74b52024-02-16 13:17:01 +0000228
229 #[cfg(llpvm_changes)]
230 fn instance_id(&self) -> Result<PathBuf, Error> {
231 Ok(self.instance_id.clone())
232 }
233
234 #[cfg(not(llpvm_changes))]
235 fn instance_id(&self) -> Result<PathBuf, Error> {
236 Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported"))
237 }
238
239 #[cfg(llpvm_changes)]
240 fn set_instance_id(&mut self, instance_id_file: PathBuf) -> Result<(), Error> {
241 self.instance_id = instance_id_file;
242 Ok(())
243 }
244
245 #[cfg(not(llpvm_changes))]
246 fn set_instance_id(&mut self, _: PathBuf) -> Result<(), Error> {
247 Err(anyhow!("LLPVM feature is disabled, --instance_id flag not supported"))
248 }
Alan Stokesfda70842023-12-20 17:50:14 +0000249}
250
251#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900252/// Flags for the run_microdroid subcommand
253pub struct RunMicrodroidConfig {
254 #[command(flatten)]
255 common: CommonConfig,
256
257 #[command(flatten)]
258 debug: DebugConfig,
259
260 #[command(flatten)]
261 microdroid: MicrodroidConfig,
262
263 /// Path to the directory where VM-related files (e.g. instance.img, apk.idsig, etc.) will
264 /// be stored. If not specified a random directory under /data/local/tmp/microdroid will be
265 /// created and used.
266 #[arg(long)]
267 work_dir: Option<PathBuf>,
268}
269
Alan Stokesfda70842023-12-20 17:50:14 +0000270#[derive(Args, Default)]
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900271/// Flags for the run subcommand
272pub struct RunCustomVmConfig {
273 #[command(flatten)]
274 common: CommonConfig,
275
276 #[command(flatten)]
277 debug: DebugConfig,
278
279 /// Path to VM config JSON
280 config: PathBuf,
281}
282
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700283#[derive(Parser)]
David Brazdil20412d92021-03-18 10:53:06 +0000284enum Opt {
Shikha Panwar6d306412024-02-17 21:37:49 +0000285 /// Check if the feature is enabled on device.
286 CheckFeatureEnabled { feature: String },
Jooyung Han21e9b922021-06-26 04:14:16 +0900287 /// Run a virtual machine with a config in APK
288 RunApp {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900289 #[command(flatten)]
290 config: RunAppConfig,
Jooyung Han21e9b922021-06-26 04:14:16 +0900291 },
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000292 /// Run a virtual machine with Microdroid inside
293 RunMicrodroid {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900294 #[command(flatten)]
295 config: RunMicrodroidConfig,
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000296 },
David Brazdil20412d92021-03-18 10:53:06 +0000297 /// Run a virtual machine
298 Run {
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900299 #[command(flatten)]
300 config: RunCustomVmConfig,
David Brazdil3c2ddef2021-03-18 13:09:57 +0000301 },
David Brazdil20412d92021-03-18 10:53:06 +0000302 /// List running virtual machines
303 List,
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000304 /// Print information about virtual machine support
305 Info,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000306 /// Create a new empty partition to be used as a writable partition for a VM
307 CreatePartition {
308 /// Path at which to create the image file
Andrew Walbrandff3b942021-06-09 15:20:36 +0000309 path: PathBuf,
310
311 /// The desired size of the partition, in bytes.
312 size: u64,
Jiyong Park9dd389e2021-08-23 20:42:59 +0900313
314 /// Type of the partition
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900315 #[arg(short = 't', long = "type", default_value = "raw",
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700316 value_parser = parse_partition_type)]
Jiyong Park9dd389e2021-08-23 20:42:59 +0900317 partition_type: PartitionType,
Andrew Walbrandff3b942021-06-09 15:20:36 +0000318 },
Jooyung Hanc221c052022-02-22 05:20:15 +0900319 /// Creates or update the idsig file by digesting the input APK file.
320 CreateIdsig {
321 /// Path to VM Payload APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900322 apk: PathBuf,
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700323
Jooyung Hanc221c052022-02-22 05:20:15 +0900324 /// Path to idsig of the APK
Jooyung Hanc221c052022-02-22 05:20:15 +0900325 path: PathBuf,
326 },
David Brazdil20412d92021-03-18 10:53:06 +0000327}
328
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900329fn parse_debug_level(s: &str) -> Result<DebugLevel, String> {
330 match s {
331 "none" => Ok(DebugLevel::NONE),
Jiyong Parkc2a49cc2021-10-15 00:02:12 +0900332 "full" => Ok(DebugLevel::FULL),
333 _ => Err(format!("Invalid debug level {}", s)),
334 }
335}
336
Jiyong Park9dd389e2021-08-23 20:42:59 +0900337fn parse_partition_type(s: &str) -> Result<PartitionType, String> {
338 match s {
339 "raw" => Ok(PartitionType::RAW),
340 "instance" => Ok(PartitionType::ANDROID_VM_INSTANCE),
341 _ => Err(format!("Invalid partition type {}", s)),
342 }
343}
344
David Brazdil7d1e5ec2023-02-06 17:56:29 +0000345fn parse_cpu_topology(s: &str) -> Result<CpuTopology, String> {
346 match s {
347 "one_cpu" => Ok(CpuTopology::ONE_CPU),
348 "match_host" => Ok(CpuTopology::MATCH_HOST),
349 _ => Err(format!("Invalid cpu topology {}", s)),
350 }
351}
352
Alan Stokesc4d5def2023-02-14 17:01:59 +0000353fn get_service() -> Result<Strong<dyn IVirtualizationService>, Error> {
354 let virtmgr =
355 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
356 virtmgr.connect().context("Failed to connect to VirtualizationService")
357}
358
Shikha Panwar6d306412024-02-17 21:37:49 +0000359fn command_check_feature_enabled(feature: &str) {
360 println!(
361 "Feature {feature} is {}",
362 if avf_features::is_feature_enabled(feature) { "enabled" } else { "disabled" }
363 );
364}
365
Andrew Walbranea9fa482021-03-04 16:11:12 +0000366fn main() -> Result<(), Error> {
367 env_logger::init();
Victor Hsiehb5bcfab2022-09-12 13:06:26 -0700368 let opt = Opt::parse();
Andrew Walbranea9fa482021-03-04 16:11:12 +0000369
370 // We need to start the thread pool for Binder to work properly, especially link_to_death.
371 ProcessState::start_thread_pool();
372
David Brazdil20412d92021-03-18 10:53:06 +0000373 match opt {
Shikha Panwar6d306412024-02-17 21:37:49 +0000374 Opt::CheckFeatureEnabled { feature } => {
375 command_check_feature_enabled(&feature);
376 Ok(())
377 }
Jiyong Parkb1935ef2023-08-10 17:22:39 +0900378 Opt::RunApp { config } => command_run_app(config),
379 Opt::RunMicrodroid { config } => command_run_microdroid(config),
380 Opt::Run { config } => command_run(config),
Alan Stokesc4d5def2023-02-14 17:01:59 +0000381 Opt::List => command_list(get_service()?.as_ref()),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000382 Opt::Info => command_info(),
Jiyong Park9dd389e2021-08-23 20:42:59 +0900383 Opt::CreatePartition { path, size, partition_type } => {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000384 command_create_partition(get_service()?.as_ref(), &path, size, partition_type)
Jiyong Park9dd389e2021-08-23 20:42:59 +0900385 }
Alan Stokesc4d5def2023-02-14 17:01:59 +0000386 Opt::CreateIdsig { apk, path } => {
387 command_create_idsig(get_service()?.as_ref(), &apk, &path)
388 }
Andrew Walbranea9fa482021-03-04 16:11:12 +0000389 }
390}
391
Andrew Walbran320b5602021-03-04 16:11:12 +0000392/// List the VMs currently running.
Andrew Walbran616d13f2022-05-12 18:35:55 +0000393fn command_list(service: &dyn IVirtualizationService) -> Result<(), Error> {
Andrew Walbran17de24f2021-05-27 13:27:30 +0000394 let vms = service.debugListVms().context("Failed to get list of VMs")?;
Andrew Walbran320b5602021-03-04 16:11:12 +0000395 println!("Running VMs: {:#?}", vms);
396 Ok(())
397}
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000398
399/// Print information about supported VM types.
400fn command_info() -> Result<(), Error> {
Alan Stokesc4d5def2023-02-14 17:01:59 +0000401 let non_protected_vm_supported = hypervisor_props::is_vm_supported()?;
402 let protected_vm_supported = hypervisor_props::is_protected_vm_supported()?;
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000403 match (non_protected_vm_supported, protected_vm_supported) {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000404 (false, false) => println!("VMs are not supported."),
405 (false, true) => println!("Only protected VMs are supported."),
Alan Stokes8d39a9b2023-01-10 15:01:00 +0000406 (true, false) => println!("Only non-protected VMs are supported."),
407 (true, true) => println!("Both protected and non-protected VMs are supported."),
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000408 }
409
Alan Stokesc4d5def2023-02-14 17:01:59 +0000410 if let Some(version) = hypervisor_props::version()? {
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000411 println!("Hypervisor version: {}", version);
412 } else {
413 println!("Hypervisor version not set.");
414 }
415
416 if Path::new("/dev/kvm").exists() {
417 println!("/dev/kvm exists.");
418 } else {
419 println!("/dev/kvm does not exist.");
420 }
421
Inseob Kim6ef80972023-07-20 17:23:36 +0900422 if Path::new("/dev/vfio/vfio").exists() {
423 println!("/dev/vfio/vfio exists.");
424 } else {
425 println!("/dev/vfio/vfio does not exist.");
426 }
427
428 if Path::new("/sys/bus/platform/drivers/vfio-platform").exists() {
429 println!("VFIO-platform is supported.");
430 } else {
431 println!("VFIO-platform is not supported.");
432 }
433
Jaewan Kim0c99c612024-03-23 00:44:14 +0900434 #[derive(Serialize)]
435 struct AssignableDevice {
436 node: String,
437 dtbo_label: String,
438 }
439
Inseob Kim75460b32023-08-09 13:41:31 +0900440 let devices = get_service()?.getAssignableDevices()?;
Jaewan Kim0c99c612024-03-23 00:44:14 +0900441 let devices: Vec<_> = devices
442 .into_iter()
443 .map(|device| AssignableDevice { node: device.node, dtbo_label: device.dtbo_label })
444 .collect();
Inseob Kim75460b32023-08-09 13:41:31 +0900445 println!("Assignable devices: {}", serde_json::to_string(&devices)?);
446
Inseob Kim46257382024-01-03 15:41:22 +0900447 let os_list = get_service()?.getSupportedOSList()?;
448 println!("Available OS list: {}", serde_json::to_string(&os_list)?);
Inseob Kim7a1fc8f2023-11-22 18:45:28 +0900449
Andrew Walbranc4b1bde2022-02-03 15:26:02 +0000450 Ok(())
451}
Andrew Walbran1f810b62022-08-10 13:33:57 +0000452
453#[cfg(test)]
454mod tests {
455 use super::*;
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000456 use clap::CommandFactory;
Andrew Walbran1f810b62022-08-10 13:33:57 +0000457
458 #[test]
459 fn verify_app() {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000460 // Check that the command parsing has been configured in a valid way.
461 Opt::command().debug_assert();
Andrew Walbran1f810b62022-08-10 13:33:57 +0000462 }
463}