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