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 "host/commands/run_cvd/launch/launch.h"
17 #include "host/commands/run_cvd/launch/wmediumd_server.h"
18
19 #include <string>
20 #include <unordered_set>
21 #include <utility>
22 #include <vector>
23
24 #include <android-base/logging.h>
25 #include <fruit/fruit.h>
26
27 #include "common/libs/utils/files.h"
28 #include "common/libs/utils/json.h"
29 #include "common/libs/utils/network.h"
30 #include "common/libs/utils/result.h"
31 #include "host/libs/command_util/snapshot_utils.h"
32 #include "host/libs/config/command_source.h"
33 #include "host/libs/config/known_paths.h"
34 #include "host/libs/config/openwrt_args.h"
35 #include "host/libs/vm_manager/crosvm_builder.h"
36 #include "host/libs/vm_manager/crosvm_manager.h"
37
38 namespace cuttlefish {
39 namespace {
40
41 using APBootFlow = CuttlefishConfig::InstanceSpecific::APBootFlow;
42
43 // TODO(b/288987294) Remove dependency to InstanceSpecific config when moving
44 // to run_env is completed.
45 class OpenWrt : public CommandSource {
46 public:
INJECT(OpenWrt (const CuttlefishConfig & config,const CuttlefishConfig::EnvironmentSpecific & environment,const CuttlefishConfig::InstanceSpecific & instance,LogTeeCreator & log_tee,WmediumdServer & wmediumd_server))47 INJECT(OpenWrt(const CuttlefishConfig& config,
48 const CuttlefishConfig::EnvironmentSpecific& environment,
49 const CuttlefishConfig::InstanceSpecific& instance,
50 LogTeeCreator& log_tee, WmediumdServer& wmediumd_server))
51 : config_(config),
52 environment_(environment),
53 instance_(instance),
54 log_tee_(log_tee),
55 wmediumd_server_(wmediumd_server) {}
56
57 // CommandSource
Commands()58 Result<std::vector<MonitorCommand>> Commands() override {
59 constexpr auto crosvm_for_ap_socket = "ap_control.sock";
60
61 CrosvmBuilder ap_cmd;
62
63 ap_cmd.Cmd().AddPrerequisite([this]() -> Result<void> {
64 return wmediumd_server_.WaitForAvailability();
65 });
66
67 std::string first_time_argument;
68 if (IsRestoring(config_)) {
69 const std::string snapshot_dir_path = config_.snapshot_path();
70 auto meta_info_json = CF_EXPECT(LoadMetaJson(snapshot_dir_path));
71 const std::vector<std::string> selectors{kGuestSnapshotField,
72 instance_.id()};
73 const auto guest_snapshot_dir_suffix =
74 CF_EXPECT(GetValue<std::string>(meta_info_json, selectors));
75 // guest_snapshot_dir_suffix is a relative to
76 // the snapshot_path
77 const auto restore_path = snapshot_dir_path + "/" +
78 guest_snapshot_dir_suffix + "/" +
79 kGuestSnapshotBase + "_openwrt";
80 first_time_argument = "--restore=" + restore_path;
81 }
82
83 /* TODO(b/305102099): Due to hostapd issue of OpenWRT 22.03.X versions,
84 * OpenWRT instance should be rebooted.
85 */
86 LOG(DEBUG) << "Restart OpenWRT due to hostapd issue";
87 ap_cmd.ApplyProcessRestarter(instance_.crosvm_binary(), first_time_argument,
88 kOpenwrtVmResetExitCode);
89 ap_cmd.Cmd().AddParameter("run");
90 ap_cmd.AddControlSocket(
91 instance_.PerInstanceInternalUdsPath(crosvm_for_ap_socket),
92 instance_.crosvm_binary());
93
94 ap_cmd.Cmd().AddParameter("--no-usb");
95 ap_cmd.Cmd().AddParameter("--core-scheduling=false");
96
97 if (!environment_.vhost_user_mac80211_hwsim().empty()) {
98 ap_cmd.Cmd().AddParameter("--vhost-user=mac80211-hwsim,socket=",
99 environment_.vhost_user_mac80211_hwsim());
100 }
101 if (environment_.enable_wifi()) {
102 ap_cmd.AddTap(instance_.wifi_tap_name());
103 }
104
105 /* TODO(kwstephenkim): delete this code when Minidroid completely disables
106 * the AP VM itself
107 */
108 if (!instance_.crosvm_use_balloon()) {
109 ap_cmd.Cmd().AddParameter("--no-balloon");
110 }
111
112 /* TODO(kwstephenkim): delete this code when Minidroid completely disables
113 * the AP VM itself
114 */
115 if (!instance_.crosvm_use_rng()) {
116 ap_cmd.Cmd().AddParameter("--no-rng");
117 }
118
119 if (instance_.enable_sandbox()) {
120 ap_cmd.Cmd().AddParameter("--seccomp-policy-dir=",
121 instance_.seccomp_policy_dir());
122 } else {
123 ap_cmd.Cmd().AddParameter("--disable-sandbox");
124 }
125 ap_cmd.AddReadWriteDisk(instance_.PerInstancePath("ap_overlay.img"));
126
127 auto boot_logs_path =
128 instance_.PerInstanceLogPath("crosvm_openwrt_boot.log");
129 auto logs_path = instance_.PerInstanceLogPath("crosvm_openwrt.log");
130 ap_cmd.AddSerialConsoleReadOnly(boot_logs_path);
131 ap_cmd.AddHvcReadOnly(logs_path);
132
133 auto openwrt_args = OpenwrtArgsFromConfig(instance_);
134 switch (instance_.ap_boot_flow()) {
135 case APBootFlow::Grub:
136 if (config_.vm_manager() == VmmMode::kQemu) {
137 ap_cmd.AddReadWriteDisk(
138 instance_.persistent_ap_composite_overlay_path());
139 } else {
140 ap_cmd.AddReadWriteDisk(
141 instance_.persistent_ap_composite_disk_path());
142 }
143 ap_cmd.Cmd().AddParameter("--bios=", instance_.bootloader());
144 break;
145 case APBootFlow::LegacyDirect:
146 ap_cmd.Cmd().AddParameter("--params=\"root=/dev/vda1\"");
147 for (auto& openwrt_arg : openwrt_args) {
148 ap_cmd.Cmd().AddParameter("--params=" + openwrt_arg.first + "=" +
149 openwrt_arg.second);
150 }
151 ap_cmd.Cmd().AddParameter(config_.ap_kernel_image());
152 break;
153 default:
154 // must not be happened
155 break;
156 }
157
158 std::vector<MonitorCommand> commands;
159 commands.emplace_back(
160 CF_EXPECT(log_tee_.CreateLogTee(ap_cmd.Cmd(), "openwrt")));
161 commands.emplace_back(std::move(ap_cmd.Cmd()));
162 return commands;
163 }
164
165 // SetupFeature
Name() const166 std::string Name() const override { return "OpenWrt"; }
Enabled() const167 bool Enabled() const override {
168 return instance_.ap_boot_flow() != APBootFlow::None &&
169 config_.vm_manager() == VmmMode::kCrosvm;
170 }
171
172 private:
Dependencies() const173 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()174 Result<void> ResultSetup() override { return {}; }
175
176 const CuttlefishConfig& config_;
177 const CuttlefishConfig::EnvironmentSpecific& environment_;
178 const CuttlefishConfig::InstanceSpecific& instance_;
179 LogTeeCreator& log_tee_;
180 WmediumdServer& wmediumd_server_;
181
182 static constexpr int kOpenwrtVmResetExitCode = 32;
183 };
184
185 } // namespace
186
187 fruit::Component<fruit::Required<
188 const CuttlefishConfig, const CuttlefishConfig::EnvironmentSpecific,
189 const CuttlefishConfig::InstanceSpecific, LogTeeCreator, WmediumdServer>>
OpenWrtComponent()190 OpenWrtComponent() {
191 return fruit::createComponent()
192 .addMultibinding<CommandSource, OpenWrt>()
193 .addMultibinding<SetupFeature, OpenWrt>();
194 }
195
196 } // namespace cuttlefish
197