Skip to content

Commit c7e705f

Browse files
committed
openwrt's vmm waits for cvdalloc.
openwrt uses a vmm to run a Wi-Fi access point, using a 802.11 hardware simulator connected to a tap device. Without cvdalloc, it assumes this device was already created by cuttlefish-host-resources. With cvdalloc, openwrt's vmm will need to wait for cvdalloc to complete in order to use the tap device it creates. The obvious way to do this is to use WaitForAvailability on cvdalloc's launcher. However, cvdalloc's launcher is already a VmmDependencyCommand for the instance's vmm, and if both openwrt's and cvdalloc's launcher are executing at the same time, we have two contending reads on cvdalloc's socketpair, which complicates matters. We could Wait and Post twice on the socket for notification, but that is rather ugly since that means we couple the number of socket Wait'ers to the cvdalloc binary unnecessarily. Instead, we guard WaitForAvailability by a mutex, so that there is always only one Wait'er at a time, and make WaitForAvailability idempotent, so that the second Wait need not actually attempt to read on the underlying socket.
1 parent 7597120 commit c7e705f

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ cf_cc_library(
288288
deps = [
289289
"//cuttlefish/common/libs/utils:json",
290290
"//cuttlefish/common/libs/utils:result",
291+
"//cuttlefish/host/commands/run_cvd/launch:cvdalloc",
291292
"//cuttlefish/host/commands/run_cvd/launch:log_tee_creator",
292293
"//cuttlefish/host/commands/run_cvd/launch:wmediumd_server",
293294
"//cuttlefish/host/libs/command_util",

base/cvd/cuttlefish/host/commands/run_cvd/launch/cvdalloc.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ constexpr std::chrono::seconds kCvdAllocateTimeout = std::chrono::seconds(30);
4747
constexpr std::chrono::seconds kCvdTeardownTimeout = std::chrono::seconds(2);
4848

4949
Cvdalloc::Cvdalloc(const CuttlefishConfig::InstanceSpecific& instance)
50-
: instance_(instance) {}
50+
: instance_(instance), available_(false) {}
5151

5252
Result<std::vector<MonitorCommand>> Cvdalloc::Commands() {
5353
std::string path = CvdallocBinary();
@@ -67,10 +67,15 @@ bool Cvdalloc::Enabled() const { return instance_.use_cvdalloc(); }
6767
std::unordered_set<SetupFeature *> Cvdalloc::Dependencies() const { return {}; }
6868

6969
Result<void> Cvdalloc::WaitForAvailability() {
70-
LOG(INFO) << "cvdalloc (run_cvd): waiting to finish allocation.";
71-
CF_EXPECT(cvdalloc::Wait(socket_, kCvdAllocateTimeout),
72-
"cvdalloc (run_cvd): Wait failed");
73-
LOG(INFO) << "cvdalloc (run_cvd): allocation is done.";
70+
std::lock_guard<std::mutex> lock(availability_mutex_);
71+
if (!available_) {
72+
LOG(INFO) << "cvdalloc (run_cvd): waiting to finish allocation.";
73+
CF_EXPECT(cvdalloc::Wait(socket_, kCvdAllocateTimeout),
74+
"cvdalloc (run_cvd): Wait failed");
75+
LOG(INFO) << "cvdalloc (run_cvd): allocation is done.";
76+
available_ = true;
77+
}
78+
LOG(INFO) << "cvdalloc (run_cvd): available.";
7479

7580
return {};
7681
}

base/cvd/cuttlefish/host/commands/run_cvd/launch/cvdalloc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Cvdalloc : public vm_manager::VmmDependencyCommand {
4949

5050
const CuttlefishConfig::InstanceSpecific &instance_;
5151
SharedFD socket_, their_socket_;
52+
std::mutex availability_mutex_;
53+
bool available_;
5254
};
5355

5456
fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>>

base/cvd/cuttlefish/host/commands/run_cvd/launch/open_wrt.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ class OpenWrt : public CommandSource {
4848
INJECT(OpenWrt(const CuttlefishConfig& config,
4949
const CuttlefishConfig::EnvironmentSpecific& environment,
5050
const CuttlefishConfig::InstanceSpecific& instance,
51-
LogTeeCreator& log_tee, WmediumdServer& wmediumd_server))
51+
LogTeeCreator& log_tee, WmediumdServer& wmediumd_server,
52+
Cvdalloc& cvdalloc))
5253
: config_(config),
5354
environment_(environment),
5455
instance_(instance),
5556
log_tee_(log_tee),
56-
wmediumd_server_(wmediumd_server) {}
57+
wmediumd_server_(wmediumd_server),
58+
cvdalloc_(cvdalloc) {}
5759

5860
// CommandSource
5961
Result<std::vector<MonitorCommand>> Commands() override {
@@ -62,7 +64,11 @@ class OpenWrt : public CommandSource {
6264
CrosvmBuilder ap_cmd;
6365

6466
ap_cmd.Cmd().AddPrerequisite([this]() -> Result<void> {
65-
return wmediumd_server_.WaitForAvailability();
67+
if (cvdalloc_.Enabled()) {
68+
CF_EXPECT(cvdalloc_.WaitForAvailability());
69+
}
70+
CF_EXPECT(wmediumd_server_.WaitForAvailability());
71+
return {};
6672
});
6773

6874
std::string first_time_argument;
@@ -183,15 +189,17 @@ class OpenWrt : public CommandSource {
183189
const CuttlefishConfig::InstanceSpecific& instance_;
184190
LogTeeCreator& log_tee_;
185191
WmediumdServer& wmediumd_server_;
192+
Cvdalloc& cvdalloc_;
186193

187194
static constexpr int kOpenwrtVmResetExitCode = 32;
188195
};
189196

190197
} // namespace
191198

192-
fruit::Component<fruit::Required<
193-
const CuttlefishConfig, const CuttlefishConfig::EnvironmentSpecific,
194-
const CuttlefishConfig::InstanceSpecific, LogTeeCreator, WmediumdServer>>
199+
fruit::Component<fruit::Required<const CuttlefishConfig,
200+
const CuttlefishConfig::EnvironmentSpecific,
201+
const CuttlefishConfig::InstanceSpecific,
202+
LogTeeCreator, WmediumdServer, Cvdalloc>>
195203
OpenWrtComponent() {
196204
return fruit::createComponent()
197205
.addMultibinding<CommandSource, OpenWrt>()

base/cvd/cuttlefish/host/commands/run_cvd/launch/open_wrt.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717

1818
#include <fruit/fruit.h>
1919

20+
#include "cuttlefish/host/commands/run_cvd/launch/cvdalloc.h"
2021
#include "cuttlefish/host/commands/run_cvd/launch/log_tee_creator.h"
2122
#include "cuttlefish/host/commands/run_cvd/launch/wmediumd_server.h"
2223
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
2324

2425
namespace cuttlefish {
2526

26-
fruit::Component<fruit::Required<
27-
const CuttlefishConfig, const CuttlefishConfig::EnvironmentSpecific,
28-
const CuttlefishConfig::InstanceSpecific, LogTeeCreator, WmediumdServer>>
27+
fruit::Component<fruit::Required<const CuttlefishConfig,
28+
const CuttlefishConfig::EnvironmentSpecific,
29+
const CuttlefishConfig::InstanceSpecific,
30+
LogTeeCreator, WmediumdServer, Cvdalloc>>
2931
OpenWrtComponent();
3032

3133
} // namespace cuttlefish

0 commit comments

Comments
 (0)