• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2021 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 #pragma once
17 
18 #include <optional>
19 #include <string>
20 
21 #include <json/value.h>
22 
23 #include "common/libs/utils/result.h"
24 #include "common/libs/utils/subprocess.h"
25 #include "host/libs/vm_manager/pci.h"
26 
27 namespace cuttlefish {
28 
29 class CrosvmBuilder {
30  public:
31   CrosvmBuilder();
32 
33   void ApplyProcessRestarter(const std::string& crosvm_binary,
34                              const std::string& first_time_argument,
35                              int exit_code);
36   void AddControlSocket(const std::string&, const std::string&);
37 
38   Result<void> AddCpus(size_t cpus, const std::string& freq_domain_file);
39   Result<void> AddCpus(const Json::Value&);
40   void AddCpus(size_t cpus);
41 
42   void AddHvcSink();
43   void AddHvcReadOnly(const std::string& output, bool console = false);
44   void AddHvcReadWrite(const std::string& output, const std::string& input);
45   void AddHvcSocket(const std::string& socket);
46 
47   void AddKvmPath(const std::string& path);
48 
49   void AddReadOnlyDisk(const std::string& path);
50   void AddReadWriteDisk(const std::string& path);
51 
52   void AddSerialSink();
53   void AddSerialConsoleReadOnly(const std::string& output);
54   void AddSerialConsoleReadWrite(const std::string& output,
55                                  const std::string& input, bool earlycon);
56   // [[deprecated("do not add any more users")]]
57   void AddSerial(const std::string& output, const std::string& input);
58 
59 #ifdef __linux__
60   void AddTap(const std::string& tap_name,
61               std::optional<std::string_view> mac = std::nullopt,
62               const std::optional<pci::Address>& pci = std::nullopt);
63 #endif
64   // Adds a vhost-user device to the crosvm command.
65   // The max_queue_size parameter represents the maximum number of buffers the
66   // virtqueues can hold at a given time and must be a power of 2. It must be
67   // large enough to avoid dropping buffers during peak usage but not so large
68   // that it consumes excesive amounts of guest RAM. Most sources recommend a
69   // value between 256 and 1024, suggesting to start with 256 when in doubt and
70   // increase as needed for performance.
71   void AddVhostUser(const std::string& type, const std::string& socket_path,
72                     int max_queue_size = 256);
73 
74   int HvcNum();
75 
76   Command& Cmd();
77 
78  private:
79   Command command_;
80   int hvc_num_;
81   int serial_num_;
82 };
83 
84 }  // namespace cuttlefish
85