Skip to content

Commit 5a11f52

Browse files
committed
Create a VmManagerFlag type
This expresses the dependency in choosing a default VMM based on the guest architecture. It's currently instantiated in two places to avoid changing type signatures on the `flags.cc` members. Bug: b/435031349
1 parent 78214e9 commit 5a11f52

File tree

9 files changed

+172
-68
lines changed

9 files changed

+172
-68
lines changed

base/cvd/cuttlefish/common/libs/utils/architecture.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <sys/utsname.h>
2121

2222
#include <cstdlib>
23+
#include <ostream>
2324
#include <string>
2425

2526
#include <android-base/logging.h>
@@ -63,4 +64,19 @@ bool IsHostCompatible(Arch arch) {
6364
(arch == Arch::X86 && host_arch == Arch::X86_64);
6465
}
6566

67+
std::ostream& operator<<(std::ostream& out, Arch arch) {
68+
switch (arch) {
69+
case Arch::Arm:
70+
return out << "arm";
71+
case Arch::Arm64:
72+
return out << "arm64";
73+
case Arch::RiscV64:
74+
return out << "riscv64";
75+
case Arch::X86:
76+
return out << "x86";
77+
case Arch::X86_64:
78+
return out << "x86_64";
79+
}
80+
}
81+
6682
} // namespace cuttlefish

base/cvd/cuttlefish/common/libs/utils/architecture.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#pragma once
1717

18+
#include <ostream>
1819
#include <string>
1920

2021
namespace cuttlefish {
@@ -31,4 +32,6 @@ const std::string& HostArchStr();
3132
Arch HostArch();
3233
bool IsHostCompatible(Arch arch);
3334

35+
std::ostream& operator<<(std::ostream&, Arch);
36+
3437
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ cf_cc_library(
258258
"//cuttlefish/host/commands/assemble_cvd/flags:initramfs_path",
259259
"//cuttlefish/host/commands/assemble_cvd/flags:kernel_path",
260260
"//cuttlefish/host/commands/assemble_cvd/flags:system_image_dir",
261+
"//cuttlefish/host/commands/assemble_cvd/flags:vm_manager",
261262
"//cuttlefish/host/libs/config:ap_boot_flow",
262263
"//cuttlefish/host/libs/config:config_constants",
263264
"//cuttlefish/host/libs/config:cuttlefish_config",

base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd_flags.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include "cuttlefish/common/libs/utils/known_paths.h"
2525
#include "cuttlefish/host/commands/assemble_cvd/flags_defaults.h"
26-
#include "cuttlefish/host/libs/config/config_constants.h"
2726
#include "cuttlefish/host/libs/config/display.h"
2827

2928
#define DEFINE_vec DEFINE_string
@@ -73,8 +72,6 @@ DEFINE_vec(serial_number, CF_DEFAULTS_SERIAL_NUMBER,
7372
"Serial number to use for the device");
7473
DEFINE_vec(use_random_serial, fmt::format("{}", CF_DEFAULTS_USE_RANDOM_SERIAL),
7574
"Whether to use random serial for the device.");
76-
DEFINE_vec(vm_manager, CF_DEFAULTS_VM_MANAGER,
77-
"What virtual machine manager to use, one of {qemu_cli, crosvm}");
7875
DEFINE_vec(gpu_mode, CF_DEFAULTS_GPU_MODE,
7976
"What gpu configuration to use, one of {auto, custom, drm_virgl, "
8077
"gfxstream, gfxstream_guest_angle, "

base/cvd/cuttlefish/host/commands/assemble_cvd/assemble_cvd_flags.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ DECLARE_vec(guest_enforce_security);
4545
DECLARE_vec(memory_mb);
4646
DECLARE_vec(serial_number);
4747
DECLARE_vec(use_random_serial);
48-
DECLARE_vec(vm_manager);
4948
DECLARE_vec(gpu_mode);
5049
DECLARE_vec(gpu_vhost_user_mode);
5150
DECLARE_vec(hwcomposer);

base/cvd/cuttlefish/host/commands/assemble_cvd/flags.cc

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "cuttlefish/host/commands/assemble_cvd/flags/initramfs_path.h"
5555
#include "cuttlefish/host/commands/assemble_cvd/flags/kernel_path.h"
5656
#include "cuttlefish/host/commands/assemble_cvd/flags/system_image_dir.h"
57+
#include "cuttlefish/host/commands/assemble_cvd/flags/vm_manager.h"
5758
#include "cuttlefish/host/commands/assemble_cvd/graphics_flags.h"
5859
#include "cuttlefish/host/commands/assemble_cvd/guest_config.h"
5960
#include "cuttlefish/host/commands/assemble_cvd/network_flags.h"
@@ -410,31 +411,16 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
410411
auto instance_nums =
411412
CF_EXPECT(InstanceNumsCalculator().FromGlobalGflags().Calculate());
412413

413-
// TODO(weihsu), b/250988697:
414-
// FLAGS_vm_manager used too early, have to handle this vectorized string early
415-
// Currently, all instances should use same vmm, added checking here
416-
std::vector<std::string> vm_manager_vec =
417-
android::base::Split(FLAGS_vm_manager, ",");
418-
for (int i=1; i<vm_manager_vec.size(); i++) {
419-
CF_EXPECT(
420-
vm_manager_vec[0] == vm_manager_vec[i],
421-
"All instances should have same vm_manager, " << FLAGS_vm_manager);
422-
}
423-
CF_EXPECT_GT(vm_manager_vec.size(), 0);
424-
while (vm_manager_vec.size() < instance_nums.size()) {
425-
vm_manager_vec.emplace_back(vm_manager_vec[0]);
426-
}
427-
428414
// TODO(weihsu), b/250988697: moved bootconfig_supported and hctr2_supported
429415
// into each instance, but target_arch is still in todo
430416
// target_arch should be in instance later
431-
auto vmm_mode = CF_EXPECT(ParseVmm(vm_manager_vec[0]));
432-
auto vmm = GetVmManager(vmm_mode, guest_configs[0].target_arch);
433-
if (!vmm) {
434-
LOG(FATAL) << "Invalid vm_manager: " << vm_manager_vec[0];
435-
}
436-
tmp_config_obj.set_vm_manager(vmm_mode);
437-
tmp_config_obj.set_ap_vm_manager(vm_manager_vec[0] + "_openwrt");
417+
VmManagerFlag vm_manager_flag =
418+
CF_EXPECT(VmManagerFlag::FromGlobalGflags(guest_configs));
419+
std::unique_ptr<vm_manager::VmManager> vmm =
420+
GetVmManager(vm_manager_flag.Mode(), guest_configs[0].target_arch);
421+
tmp_config_obj.set_vm_manager(vm_manager_flag.Mode());
422+
tmp_config_obj.set_ap_vm_manager(ToString(vm_manager_flag.Mode()) +
423+
"_openwrt");
438424

439425
// TODO: schuffelen - fix behavior on riscv64
440426
if (guest_configs[0].target_arch == Arch::RiscV64) {
@@ -1092,7 +1078,7 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
10921078
gpu_renderer_features_vec[instance_index],
10931079
gpu_context_types_vec[instance_index],
10941080
guest_hwui_renderer_vec[instance_index],
1095-
guest_renderer_preload_vec[instance_index], vmm_mode,
1081+
guest_renderer_preload_vec[instance_index], vm_manager_flag.Mode(),
10961082
guest_configs[instance_index], instance));
10971083
calculated_gpu_mode_vec[instance_index] = gpu_mode_vec[instance_index];
10981084

@@ -1175,7 +1161,7 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
11751161

11761162
bool os_overlay = true;
11771163
// Gem5 already uses CoW wrappers around disk images
1178-
os_overlay &= vmm_mode != VmmMode::kGem5;
1164+
os_overlay &= vm_manager_flag.Mode() != VmmMode::kGem5;
11791165
os_overlay &= FLAGS_use_overlay;
11801166
if (os_overlay) {
11811167
auto path = const_instance.PerInstancePath("overlay.img");
@@ -1184,7 +1170,7 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
11841170
virtual_disk_paths.push_back(const_instance.os_composite_disk_path());
11851171
}
11861172

1187-
bool persistent_disk = vmm_mode != VmmMode::kGem5;
1173+
bool persistent_disk = vm_manager_flag.Mode() != VmmMode::kGem5;
11881174
if (persistent_disk) {
11891175
#ifdef __APPLE__
11901176
const std::string persistent_composite_img_base =
@@ -1305,7 +1291,7 @@ Result<CuttlefishConfig> InitializeCuttlefishConfiguration(
13051291
auto external_network_mode = CF_EXPECT(
13061292
ParseExternalNetworkMode(device_external_network_vec[instance_index]));
13071293
CF_EXPECT(external_network_mode == ExternalNetworkMode::kTap ||
1308-
vmm_mode == VmmMode::kQemu,
1294+
vm_manager_flag.Mode() == VmmMode::kQemu,
13091295
"TODO(b/286284441): slirp only works on QEMU");
13101296
instance.set_external_network_mode(external_network_mode);
13111297

@@ -1601,50 +1587,30 @@ Result<std::vector<GuestConfig>> GetGuestConfigAndSetDefaults(
16011587
std::vector<GuestConfig> guest_configs =
16021588
CF_EXPECT(ReadGuestConfig(boot_image, kernel_path, system_image_dir));
16031589

1604-
// TODO(weihsu), b/250988697:
1605-
// assume all instances are using same VM manager/app/arch,
1606-
// later that multiple instances may use different VM manager/app/arch
1607-
1608-
// Temporary add this checking to make sure all instances have same target_arch.
1609-
// This checking should be removed later.
1610-
for (int instance_index = 1; instance_index < guest_configs.size(); instance_index++) {
1611-
CF_EXPECT(guest_configs[0].target_arch == guest_configs[instance_index].target_arch,
1612-
"all instance target_arch should be same");
1613-
}
1614-
if (FLAGS_vm_manager.empty()) {
1615-
if (IsHostCompatible(guest_configs[0].target_arch)) {
1616-
FLAGS_vm_manager = ToString(VmmMode::kCrosvm);
1617-
} else {
1618-
FLAGS_vm_manager = ToString(VmmMode::kQemu);
1619-
}
1620-
}
1621-
1622-
std::vector<std::string> vm_manager_vec =
1623-
android::base::Split(FLAGS_vm_manager, ",");
1624-
1625-
// TODO(weihsu), b/250988697:
1626-
// Currently, all instances should use same vmm
1627-
auto vmm = CF_EXPECT(ParseVmm(vm_manager_vec[0]));
1590+
VmManagerFlag vm_manager_flag =
1591+
CF_EXPECT(VmManagerFlag::FromGlobalGflags(guest_configs));
16281592

16291593
// get flag default values and store into map
16301594
auto name_to_default_value = CurrentFlagsToDefaultValue();
16311595

1632-
if (vmm == VmmMode::kQemu) {
1633-
CF_EXPECT(SetDefaultFlagsForQemu(system_image_dir, guest_configs,
1634-
name_to_default_value));
1635-
} else if (vmm == VmmMode::kCrosvm) {
1636-
CF_EXPECT(SetDefaultFlagsForCrosvm(system_image_dir, guest_configs,
1596+
switch (vm_manager_flag.Mode()) {
1597+
case VmmMode::kQemu:
1598+
CF_EXPECT(SetDefaultFlagsForQemu(system_image_dir, guest_configs,
16371599
name_to_default_value));
1638-
} else if (vmm == VmmMode::kGem5) {
1639-
// TODO: Get the other architectures working
1640-
if (guest_configs[0].target_arch != Arch::Arm64) {
1641-
return CF_ERR("Gem5 only supports ARM64");
1642-
}
1643-
SetDefaultFlagsForGem5();
1644-
} else {
1645-
return CF_ERR("Unknown Virtual Machine Manager: " << FLAGS_vm_manager);
1600+
break;
1601+
case VmmMode::kCrosvm:
1602+
CF_EXPECT(SetDefaultFlagsForCrosvm(system_image_dir, guest_configs,
1603+
name_to_default_value));
1604+
break;
1605+
case VmmMode::kGem5:
1606+
CF_EXPECT_EQ(guest_configs[0].target_arch, Arch::Arm64,
1607+
"Gem5 only supports ARM64");
1608+
SetDefaultFlagsForGem5();
1609+
break;
1610+
case VmmMode::kUnknown:
1611+
return CF_ERR("Unknown VM manager");
16461612
}
1647-
if (vmm != VmmMode::kGem5) {
1613+
if (vm_manager_flag.Mode() != VmmMode::kGem5) {
16481614
// After SetCommandLineOptionWithMode in SetDefaultFlagsForCrosvm/Qemu,
16491615
// default flag values changed, need recalculate name_to_default_value
16501616
name_to_default_value = CurrentFlagsToDefaultValue();

base/cvd/cuttlefish/host/commands/assemble_cvd/flags/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,17 @@ cf_cc_library(
8181
"@gflags",
8282
],
8383
)
84+
85+
cf_cc_library(
86+
name = "vm_manager",
87+
srcs = ["vm_manager.cc"],
88+
hdrs = ["vm_manager.h"],
89+
deps = [
90+
"//cuttlefish/common/libs/utils:result",
91+
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
92+
"//cuttlefish/host/commands/assemble_cvd:guest_config",
93+
"//cuttlefish/host/libs/config:cuttlefish_config",
94+
"//libbase",
95+
"@gflags",
96+
],
97+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2019 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "cuttlefish/host/commands/assemble_cvd/flags/vm_manager.h"
17+
18+
#include <string>
19+
#include <string_view>
20+
#include <vector>
21+
22+
#include <android-base/strings.h>
23+
#include <gflags/gflags.h>
24+
25+
#include "cuttlefish/common/libs/utils/result.h"
26+
#include "cuttlefish/host/commands/assemble_cvd/flags_defaults.h"
27+
#include "cuttlefish/host/commands/assemble_cvd/guest_config.h"
28+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
29+
30+
DEFINE_string(vm_manager, CF_DEFAULTS_VM_MANAGER,
31+
"What virtual machine manager to use, one of {qemu_cli, crosvm}");
32+
33+
namespace cuttlefish {
34+
35+
Result<VmManagerFlag> VmManagerFlag::FromGlobalGflags(
36+
const std::vector<GuestConfig>& guest_configs) {
37+
// TODO: b/250988697 - Support multiple multiple VM managers in one group
38+
CF_EXPECT(!guest_configs.empty());
39+
for (const GuestConfig& guest_config : guest_configs) {
40+
CF_EXPECT_EQ(guest_config.target_arch, guest_configs[0].target_arch,
41+
"All instance target architectures should be the same");
42+
}
43+
44+
std::vector<std::string> vm_manager_str_vec =
45+
android::base::Split(FLAGS_vm_manager, ",");
46+
47+
VmmMode default_vmm = IsHostCompatible(guest_configs[0].target_arch)
48+
? VmmMode::kCrosvm
49+
: VmmMode::kQemu;
50+
51+
std::vector<VmmMode> vmm_vec;
52+
for (std::string_view vmm_str : vm_manager_str_vec) {
53+
vmm_vec.emplace_back(vmm_str.empty() ? default_vmm
54+
: CF_EXPECT(ParseVmm(vmm_str)));
55+
}
56+
if (vmm_vec.empty()) {
57+
vmm_vec.emplace_back(default_vmm);
58+
}
59+
for (VmmMode mode : vmm_vec) {
60+
CF_EXPECT_EQ(mode, vmm_vec[0], "All VMMs must be the same");
61+
}
62+
return VmManagerFlag(vmm_vec[0]);
63+
}
64+
65+
VmManagerFlag::VmManagerFlag(VmmMode mode) : mode_(mode) {}
66+
67+
VmmMode VmManagerFlag::Mode() const { return mode_; }
68+
69+
} // namespace cuttlefish
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2019 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <vector>
19+
20+
#include "cuttlefish/common/libs/utils/result.h"
21+
#include "cuttlefish/host/commands/assemble_cvd/guest_config.h"
22+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
23+
24+
namespace cuttlefish {
25+
26+
class VmManagerFlag {
27+
public:
28+
static Result<VmManagerFlag> FromGlobalGflags(
29+
const std::vector<GuestConfig>&);
30+
31+
VmmMode Mode() const;
32+
33+
private:
34+
explicit VmManagerFlag(VmmMode);
35+
36+
VmmMode mode_;
37+
};
38+
39+
} // namespace cuttlefish

0 commit comments

Comments
 (0)