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 "wmediumd_server.h"
17 
18 #include "host/commands/run_cvd/launch/launch.h"
19 
20 #include <string>
21 #include <unordered_set>
22 #include <utility>
23 #include <vector>
24 
25 #include <fruit/fruit.h>
26 
27 #include "common/libs/utils/files.h"
28 #include "common/libs/utils/result.h"
29 #include "host/commands/run_cvd/launch/grpc_socket_creator.h"
30 #include "host/commands/run_cvd/launch/log_tee_creator.h"
31 #include "host/libs/config/command_source.h"
32 #include "host/libs/config/cuttlefish_config.h"
33 #include "host/libs/config/known_paths.h"
34 #include "host/libs/vm_manager/vm_manager.h"
35 
36 namespace cuttlefish {
37 namespace {
38 
39 // SetupFeature class for waiting wmediumd server to be settled.
40 // This class is used by the instance that does not launches wmediumd.
41 // TODO(b/276832089) remove this when run_env implementation is completed.
42 class ValidateWmediumdService : public SetupFeature {
43  public:
INJECT(ValidateWmediumdService (const CuttlefishConfig & config,const CuttlefishConfig::EnvironmentSpecific & environment,const CuttlefishConfig::InstanceSpecific & instance))44   INJECT(ValidateWmediumdService(
45       const CuttlefishConfig& config,
46       const CuttlefishConfig::EnvironmentSpecific& environment,
47       const CuttlefishConfig::InstanceSpecific& instance))
48       : config_(config), environment_(environment), instance_(instance) {}
Name() const49   std::string Name() const override { return "ValidateWmediumdService"; }
Enabled() const50   bool Enabled() const override {
51     return environment_.enable_wifi() && config_.virtio_mac80211_hwsim() &&
52            !instance_.start_wmediumd_instance();
53   }
54 
55  private:
Dependencies() const56   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()57   Result<void> ResultSetup() override {
58     if (!environment_.wmediumd_api_server_socket().empty()) {
59       CF_EXPECT(
60           WaitForUnixSocket(environment_.wmediumd_api_server_socket(), 30));
61     }
62     CF_EXPECT(WaitForUnixSocket(environment_.vhost_user_mac80211_hwsim(), 30));
63 
64     return {};
65   }
66 
67  private:
68   const CuttlefishConfig& config_;
69   const CuttlefishConfig::EnvironmentSpecific& environment_;
70   const CuttlefishConfig::InstanceSpecific& instance_;
71 };
72 
73 }  // namespace
74 
WmediumdServer(const CuttlefishConfig::EnvironmentSpecific & environment,const CuttlefishConfig::InstanceSpecific & instance,LogTeeCreator & log_tee,GrpcSocketCreator & grpc_socket)75 WmediumdServer::WmediumdServer(
76     const CuttlefishConfig::EnvironmentSpecific& environment,
77     const CuttlefishConfig::InstanceSpecific& instance, LogTeeCreator& log_tee,
78     GrpcSocketCreator& grpc_socket)
79     : environment_(environment),
80       instance_(instance),
81       log_tee_(log_tee),
82       grpc_socket_(grpc_socket) {}
83 
Commands()84 Result<std::vector<MonitorCommand>> WmediumdServer::Commands() {
85   Command cmd(WmediumdBinary());
86   cmd.AddParameter("-u", environment_.vhost_user_mac80211_hwsim());
87   cmd.AddParameter("-a", environment_.wmediumd_api_server_socket());
88   cmd.AddParameter("-c", config_path_);
89 
90   cmd.AddParameter("--grpc_uds_path=", grpc_socket_.CreateGrpcSocket(Name()));
91 
92   std::vector<MonitorCommand> commands;
93   commands.emplace_back(CF_EXPECT(log_tee_.CreateLogTee(cmd, "wmediumd")));
94   commands.emplace_back(std::move(cmd));
95   return commands;
96 }
97 
Name() const98 std::string WmediumdServer::Name() const { return "WmediumdServer"; }
99 
Enabled() const100 bool WmediumdServer::Enabled() const {
101   return instance_.start_wmediumd_instance();
102 }
103 
WaitForAvailability() const104 Result<void> WmediumdServer::WaitForAvailability() const {
105   if (Enabled()) {
106     if (!environment_.wmediumd_api_server_socket().empty()) {
107       CF_EXPECT(
108           WaitForUnixSocket(environment_.wmediumd_api_server_socket(), 30));
109     }
110     CF_EXPECT(WaitForUnixSocket(environment_.vhost_user_mac80211_hwsim(), 30));
111   }
112 
113   return {};
114 }
115 
Dependencies() const116 std::unordered_set<SetupFeature*> WmediumdServer::Dependencies() const {
117   return {};
118 }
119 
ResultSetup()120 Result<void> WmediumdServer::ResultSetup() {
121   // If wmediumd configuration is given, use it
122   if (!environment_.wmediumd_config().empty()) {
123     config_path_ = environment_.wmediumd_config();
124     return {};
125   }
126   // Otherwise, generate wmediumd configuration using the current wifi mac
127   // prefix before start
128   config_path_ = environment_.PerEnvironmentPath("wmediumd.cfg");
129   Command gen_config_cmd(WmediumdGenConfigBinary());
130   gen_config_cmd.AddParameter("-o", config_path_);
131   gen_config_cmd.AddParameter("-p", environment_.wmediumd_mac_prefix());
132 
133   int success = gen_config_cmd.Start().Wait();
134   CF_EXPECT(success == 0, "Unable to run " << gen_config_cmd.Executable()
135                                            << ". Exited with status "
136                                            << success);
137 
138   return {};
139 }
140 
141 fruit::Component<fruit::Required<
142     const CuttlefishConfig, const CuttlefishConfig::EnvironmentSpecific,
143     const CuttlefishConfig::InstanceSpecific, LogTeeCreator, GrpcSocketCreator>>
WmediumdServerComponent()144 WmediumdServerComponent() {
145   return fruit::createComponent()
146       .addMultibinding<vm_manager::VmmDependencyCommand, WmediumdServer>()
147       .addMultibinding<CommandSource, WmediumdServer>()
148       .addMultibinding<SetupFeature, WmediumdServer>()
149       .addMultibinding<SetupFeature, ValidateWmediumdService>();
150 }
151 
152 }  // namespace cuttlefish
153