• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2023 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 "vhost_device_vsock.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/known_paths.h"
29 #include "common/libs/utils/result.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 
VhostDeviceVsock(LogTeeCreator & log_tee,const CuttlefishConfig::InstanceSpecific & instance,const CuttlefishConfig & cfconfig)38 VhostDeviceVsock::VhostDeviceVsock(
39     LogTeeCreator& log_tee, const CuttlefishConfig::InstanceSpecific& instance,
40     const CuttlefishConfig& cfconfig)
41     : log_tee_(log_tee), instance_(instance), cfconfig_(cfconfig) {}
42 
Commands()43 Result<std::vector<MonitorCommand>> VhostDeviceVsock::Commands() {
44   auto instances = cfconfig_.Instances();
45   if (instance_.serial_number() != instances[0].serial_number()) {
46     return {};
47   }
48 
49   Command command(ProcessRestarterBinary());
50   command.AddParameter("-when_killed");
51   command.AddParameter("-when_dumped");
52   command.AddParameter("-when_exited_with_failure");
53   command.AddParameter("--");
54   command.AddParameter(HostBinaryPath("vhost_device_vsock"));
55   command.AddEnvironmentVariable("RUST_BACKTRACE", "1");
56   command.AddEnvironmentVariable("RUST_LOG", "debug");
57 
58   for (auto i : instances) {
59     std::string isolation_groups = "";
60     if (i.vsock_guest_group().length()) {
61       isolation_groups = ",groups=" + i.vsock_guest_group();
62     }
63 
64     auto param = fmt::format(
65         "guest-cid={1},socket={0}/vsock_{1}_{2}/vhost.socket,uds-path={0}/"
66         "vsock_{1}_{2}/vm.vsock{3}",
67         TempDir(), i.vsock_guest_cid(), getuid(), isolation_groups);
68     command.AddParameter("--vm");
69     command.AddParameter(param);
70     LOG(INFO) << "VhostDeviceVsock::vhost param is:" << param;
71   }
72 
73   std::vector<MonitorCommand> commands;
74   commands.emplace_back(
75       CF_EXPECT(log_tee_.CreateFullLogTee(command, "vhost_device_vsock")));
76   commands.emplace_back(std::move(command));
77   return commands;
78 }
79 
Name() const80 std::string VhostDeviceVsock::Name() const { return "VhostDeviceVsock"; }
81 
Enabled() const82 bool VhostDeviceVsock::Enabled() const { return instance_.vhost_user_vsock(); }
83 
WaitForAvailability() const84 Result<void> VhostDeviceVsock::WaitForAvailability() const {
85   if (Enabled()) {
86     CF_EXPECT(WaitForUnixSocket(
87         fmt::format("{}/vsock_{}_{}/vm.vsock", TempDir(),
88                     instance_.vsock_guest_cid(), std::to_string(getuid())),
89         30));
90   }
91   return {};
92 }
93 
94 fruit::Component<fruit::Required<const CuttlefishConfig, LogTeeCreator,
95                                  const CuttlefishConfig::InstanceSpecific>>
VhostDeviceVsockComponent()96 VhostDeviceVsockComponent() {
97   return fruit::createComponent()
98       .addMultibinding<vm_manager::VmmDependencyCommand, VhostDeviceVsock>()
99       .addMultibinding<CommandSource, VhostDeviceVsock>()
100       .addMultibinding<SetupFeature, VhostDeviceVsock>();
101 }
102 
103 }  // namespace cuttlefish
104