• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 <sys/types.h>
19 #include <array>
20 #include <cstdint>
21 #include <map>
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <set>
26 #include <vector>
27 
28 #include "common/libs/utils/environment.h"
29 #include "host/libs/config/custom_actions.h"
30 
31 namespace Json {
32 class Value;
33 }
34 
35 namespace cuttlefish {
36 constexpr char kLogcatSerialMode[] = "serial";
37 constexpr char kLogcatVsockMode[] = "vsock";
38 }
39 
40 namespace cuttlefish {
41 
42 constexpr char kDefaultUuidPrefix[] = "699acfc4-c8c4-11e7-882b-5065f31dc1";
43 constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";
44 constexpr char kVsocUserPrefix[] = "vsoc-";
45 constexpr char kBootStartedMessage[] ="VIRTUAL_DEVICE_BOOT_STARTED";
46 constexpr char kBootCompletedMessage[] = "VIRTUAL_DEVICE_BOOT_COMPLETED";
47 constexpr char kBootFailedMessage[] = "VIRTUAL_DEVICE_BOOT_FAILED";
48 constexpr char kMobileNetworkConnectedMessage[] =
49     "VIRTUAL_DEVICE_NETWORK_MOBILE_CONNECTED";
50 constexpr char kWifiConnectedMessage[] =
51     "VIRTUAL_DEVICE_NETWORK_WIFI_CONNECTED";
52 constexpr char kEthernetConnectedMessage[] =
53     "VIRTUAL_DEVICE_NETWORK_ETHERNET_CONNECTED";
54 constexpr char kScreenChangedMessage[] = "VIRTUAL_DEVICE_SCREEN_CHANGED";
55 constexpr char kInternalDirName[] = "internal";
56 constexpr char kSharedDirName[] = "shared";
57 constexpr char kCrosvmVarEmptyDir[] = "/var/empty";
58 
59 enum class AdbMode {
60   VsockTunnel,
61   VsockHalfTunnel,
62   NativeVsock,
63   Unknown,
64 };
65 
66 enum class SecureHal {
67   Unknown,
68   Keymint,
69   Gatekeeper,
70 };
71 
72 // Holds the configuration of the cuttlefish instances.
73 class CuttlefishConfig {
74  public:
75   static const CuttlefishConfig* Get();
76   static bool ConfigExists();
77 
78   CuttlefishConfig();
79   CuttlefishConfig(CuttlefishConfig&&);
80   ~CuttlefishConfig();
81   CuttlefishConfig& operator=(CuttlefishConfig&&);
82 
83   // Saves the configuration object in a file, it can then be read in other
84   // processes by passing the --config_file option.
85   bool SaveToFile(const std::string& file) const;
86 
87   std::string assembly_dir() const;
88   void set_assembly_dir(const std::string& assembly_dir);
89 
90   std::string AssemblyPath(const std::string&) const;
91 
92   std::string vm_manager() const;
93   void set_vm_manager(const std::string& name);
94 
95   std::string gpu_mode() const;
96   void set_gpu_mode(const std::string& name);
97 
98   int cpus() const;
99   void set_cpus(int cpus);
100 
101   int memory_mb() const;
102   void set_memory_mb(int memory_mb);
103 
104   int dpi() const;
105   void set_dpi(int dpi);
106 
107   int refresh_rate_hz() const;
108   void set_refresh_rate_hz(int refresh_rate_hz);
109 
110   struct DisplayConfig {
111     int width;
112     int height;
113   };
114 
115   std::vector<DisplayConfig> display_configs() const;
116   void set_display_configs(const std::vector<DisplayConfig>& display_configs);
117 
118   int gdb_port() const;
119   void set_gdb_port(int gdb_port);
120 
121   bool deprecated_boot_completed() const;
122   void set_deprecated_boot_completed(bool deprecated_boot_completed);
123 
124   void set_cuttlefish_env_path(const std::string& path);
125   std::string cuttlefish_env_path() const;
126 
127   void set_adb_mode(const std::set<std::string>& modes);
128   std::set<AdbMode> adb_mode() const;
129 
130   void set_secure_hals(const std::set<std::string>& hals);
131   std::set<SecureHal> secure_hals() const;
132 
133   void set_setupwizard_mode(const std::string& title);
134   std::string setupwizard_mode() const;
135 
136   void set_qemu_binary_dir(const std::string& qemu_binary_dir);
137   std::string qemu_binary_dir() const;
138 
139   void set_crosvm_binary(const std::string& crosvm_binary);
140   std::string crosvm_binary() const;
141 
142   void set_tpm_device(const std::string& tpm_device);
143   std::string tpm_device() const;
144 
145   void set_enable_vnc_server(bool enable_vnc_server);
146   bool enable_vnc_server() const;
147 
148   void set_enable_sandbox(const bool enable_sandbox);
149   bool enable_sandbox() const;
150 
151   void set_seccomp_policy_dir(const std::string& seccomp_policy_dir);
152   std::string seccomp_policy_dir() const;
153 
154   void set_enable_webrtc(bool enable_webrtc);
155   bool enable_webrtc() const;
156 
157   void set_webrtc_assets_dir(const std::string& webrtc_assets_dir);
158   std::string webrtc_assets_dir() const;
159 
160   void set_webrtc_enable_adb_websocket(bool enable);
161   bool webrtc_enable_adb_websocket() const;
162 
163   void set_enable_vehicle_hal_grpc_server(bool enable_vhal_server);
164   bool enable_vehicle_hal_grpc_server() const;
165 
166   void set_vehicle_hal_grpc_server_binary(const std::string& vhal_server_binary);
167   std::string vehicle_hal_grpc_server_binary() const;
168 
169   void set_custom_actions(const std::vector<CustomActionConfig>& actions);
170   std::vector<CustomActionConfig> custom_actions() const;
171 
172   void set_restart_subprocesses(bool restart_subprocesses);
173   bool restart_subprocesses() const;
174 
175   void set_run_adb_connector(bool run_adb_connector);
176   bool run_adb_connector() const;
177 
178   void set_enable_gnss_grpc_proxy(const bool enable_gnss_grpc_proxy);
179   bool enable_gnss_grpc_proxy() const;
180 
181   void set_run_as_daemon(bool run_as_daemon);
182   bool run_as_daemon() const;
183 
184   void set_data_policy(const std::string& data_policy);
185   std::string data_policy() const;
186 
187   void set_blank_data_image_mb(int blank_data_image_mb);
188   int blank_data_image_mb() const;
189 
190   void set_blank_data_image_fmt(const std::string& blank_data_image_fmt);
191   std::string blank_data_image_fmt() const;
192 
193   void set_bootloader(const std::string& bootloader_path);
194   std::string bootloader() const;
195 
196   // TODO (b/163575714) add virtio console support to the bootloader so the
197   // virtio console path for the console device can be taken again. When that
198   // happens, this function can be deleted along with all the code paths it
199   // forces.
use_bootloader()200   bool use_bootloader() const { return true; };
201 
202   void set_boot_slot(const std::string& boot_slot);
203   std::string boot_slot() const;
204 
205   void set_guest_enforce_security(bool guest_enforce_security);
206   bool guest_enforce_security() const;
207 
208   void set_guest_audit_security(bool guest_audit_security);
209   bool guest_audit_security() const;
210 
211   void set_enable_host_bluetooth(bool enable_host_bluetooth);
212   bool enable_host_bluetooth() const;
213 
214   enum Answer {
215     kUnknown = 0,
216     kYes,
217     kNo,
218   };
219 
220   void set_enable_metrics(std::string enable_metrics);
221   CuttlefishConfig::Answer enable_metrics() const;
222 
223   void set_metrics_binary(const std::string& metrics_binary);
224   std::string metrics_binary() const;
225 
226   void set_extra_kernel_cmdline(std::string extra_cmdline);
227   std::vector<std::string> extra_kernel_cmdline() const;
228 
229   // A directory containing the SSL certificates for the signaling server
230   void set_webrtc_certs_dir(const std::string& certs_dir);
231   std::string webrtc_certs_dir() const;
232 
233   // The port for the webrtc signaling server. It's used by the signaling server
234   // to bind to it and by the webrtc process to connect to and register itself
235   void set_sig_server_port(int port);
236   int sig_server_port() const;
237 
238   // The range of UDP ports available for webrtc sessions.
239   void set_webrtc_udp_port_range(std::pair<uint16_t, uint16_t> range);
240   std::pair<uint16_t, uint16_t> webrtc_udp_port_range() const;
241 
242   // The range of TCP ports available for webrtc sessions.
243   void set_webrtc_tcp_port_range(std::pair<uint16_t, uint16_t> range);
244   std::pair<uint16_t, uint16_t> webrtc_tcp_port_range() const;
245 
246   // The address of the signaling server
247   void set_sig_server_address(const std::string& addr);
248   std::string sig_server_address() const;
249 
250   // The path section of the url where the webrtc process registers itself with
251   // the signaling server
252   void set_sig_server_path(const std::string& path);
253   std::string sig_server_path() const;
254 
255   // Whether the webrtc process should attempt to verify the authenticity of the
256   // signaling server (reject self signed certificates)
257   void set_sig_server_strict(bool strict);
258   bool sig_server_strict() const;
259 
260   // A file containing http headers to include in the connection to the
261   // signaling server
262   void set_sig_server_headers_path(const std::string& path);
263   std::string sig_server_headers_path() const;
264 
265   // The dns address of mobile network (RIL)
266   void set_ril_dns(const std::string& ril_dns);
267   std::string ril_dns() const;
268 
269   // KGDB configuration for kernel debugging
270   void set_kgdb(bool kgdb);
271   bool kgdb() const;
272 
273   // Serial console
274   void set_console(bool console);
275   bool console() const;
276   std::string console_dev() const;
277 
278   // Configuration flags for a minimal device
279   bool enable_minimal_mode() const;
280   void set_enable_minimal_mode(bool enable_minimal_mode);
281 
282   void set_enable_modem_simulator(bool enable_modem_simulator);
283   bool enable_modem_simulator() const;
284 
285   void set_modem_simulator_instance_number(int instance_numbers);
286   int modem_simulator_instance_number() const;
287 
288   void set_modem_simulator_sim_type(int sim_type);
289   int modem_simulator_sim_type() const;
290 
291   void set_host_tools_version(const std::map<std::string, uint32_t>&);
292   std::map<std::string, uint32_t> host_tools_version() const;
293 
294   void set_vhost_net(bool vhost_net);
295   bool vhost_net() const;
296 
297   void set_ethernet(bool ethernet);
298   bool ethernet() const;
299 
300   void set_record_screen(bool record_screen);
301   bool record_screen() const;
302 
303   void set_smt(bool smt);
304   bool smt() const;
305 
306   void set_enable_audio(bool enable);
307   bool enable_audio() const;
308 
309   void set_protected_vm(bool protected_vm);
310   bool protected_vm() const;
311 
312   void set_target_arch(Arch target_arch);
313   Arch target_arch() const;
314 
315   void set_bootconfig_supported(bool bootconfig_supported);
316   bool bootconfig_supported() const;
317 
318   class InstanceSpecific;
319   class MutableInstanceSpecific;
320 
321   MutableInstanceSpecific ForInstance(int instance_num);
322   InstanceSpecific ForInstance(int instance_num) const;
323   InstanceSpecific ForDefaultInstance() const;
324 
325   std::vector<InstanceSpecific> Instances() const;
326 
327   // A view into an existing CuttlefishConfig object for a particular instance.
328   class InstanceSpecific {
329     const CuttlefishConfig* config_;
330     std::string id_;
331     friend InstanceSpecific CuttlefishConfig::ForInstance(int num) const;
332     friend InstanceSpecific CuttlefishConfig::ForDefaultInstance() const;
333     friend std::vector<InstanceSpecific> CuttlefishConfig::Instances() const;
334 
InstanceSpecific(const CuttlefishConfig * config,const std::string & id)335     InstanceSpecific(const CuttlefishConfig* config, const std::string& id)
336         : config_(config), id_(id) {}
337 
338     Json::Value* Dictionary();
339     const Json::Value* Dictionary() const;
340   public:
341     std::string serial_number() const;
342     // If any of the following port numbers is 0, the relevant service is not
343     // running on the guest.
344 
345     // Port number to connect to vnc server on the host
346     int vnc_server_port() const;
347     // Port number to connect to the tombstone receiver on the host
348     int tombstone_receiver_port() const;
349     // Port number to connect to the config server on the host
350     int config_server_port() const;
351     // Port number to connect to the keyboard server on the host. (Only
352     // operational if QEMU is the vmm.)
353     int keyboard_server_port() const;
354     // Port number to connect to the touch server on the host. (Only
355     // operational if QEMU is the vmm.)
356     int touch_server_port() const;
357     // Port number to connect to the frame server on the host. (Only
358     // operational if using swiftshader as the GPU.)
359     int frames_server_port() const;
360     // Port number to connect to the vehicle HAL server on the host
361     int vehicle_hal_server_port() const;
362     // Port number to connect to the audiocontrol server on the guest
363     int audiocontrol_server_port() const;
364     // Port number to connect to the adb server on the host
365     int host_port() const;
366     // Port number to connect to the gnss grpc proxy server on the host
367     int gnss_grpc_proxy_server_port() const;
368     std::string adb_ip_and_port() const;
369     // Port number to connect to the root-canal on the host
370     int rootcanal_hci_port() const;
371     int rootcanal_link_port() const;
372     int rootcanal_test_port() const;
373     std::string rootcanal_config_file() const;
374     std::string rootcanal_default_commands_file() const;
375 
376     std::string adb_device_name() const;
377     std::string device_title() const;
378     std::string gnss_file_path() const;
379     std::string mobile_bridge_name() const;
380     std::string mobile_tap_name() const;
381     std::string wifi_tap_name() const;
382     std::string ethernet_tap_name() const;
383     uint32_t session_id() const;
384     bool use_allocd() const;
385     int vsock_guest_cid() const;
386     std::string uuid() const;
387     std::string instance_name() const;
388     std::vector<std::string> virtual_disk_paths() const;
389 
390     // Returns the path to a file with the given name in the instance
391     // directory..
392     std::string PerInstancePath(const char* file_name) const;
393     std::string PerInstanceInternalPath(const char* file_name) const;
394 
395     std::string instance_dir() const;
396 
397     std::string instance_internal_dir() const;
398 
399     std::string touch_socket_path() const;
400     std::string keyboard_socket_path() const;
401     std::string switches_socket_path() const;
402     std::string frames_socket_path() const;
403 
404     // mock hal guest socket that will be vsock/virtio later on
405     std::string confui_hal_guest_socket_path() const;
406 
407     std::string access_kregistry_path() const;
408 
409     std::string pstore_path() const;
410 
411     std::string console_path() const;
412 
413     std::string logcat_path() const;
414 
415     std::string kernel_log_pipe_name() const;
416 
417     std::string console_pipe_prefix() const;
418     std::string console_in_pipe_name() const;
419     std::string console_out_pipe_name() const;
420 
421     std::string gnss_pipe_prefix() const;
422     std::string gnss_in_pipe_name() const;
423     std::string gnss_out_pipe_name() const;
424 
425     std::string logcat_pipe_name() const;
426 
427     std::string launcher_log_path() const;
428 
429     std::string launcher_monitor_socket_path() const;
430 
431     std::string sdcard_path() const;
432 
433     std::string os_composite_disk_path() const;
434 
435     std::string persistent_composite_disk_path() const;
436 
437     std::string uboot_env_image_path() const;
438 
439     std::string vendor_boot_image_path() const;
440 
441     std::string audio_server_path() const;
442 
443     // modem simulator related
444     std::string modem_simulator_ports() const;
445 
446     // The device id the webrtc process should use to register with the
447     // signaling server
448     std::string webrtc_device_id() const;
449 
450     // Whether this instance should start the webrtc signaling server
451     bool start_webrtc_sig_server() const;
452 
453     // Wifi MAC address inside the guest
454     std::array<unsigned char, 6> wifi_mac_address() const;
455 
456     std::string factory_reset_protected_path() const;
457 
458     std::string persistent_bootconfig_path() const;
459   };
460 
461   // A view into an existing CuttlefishConfig object for a particular instance.
462   class MutableInstanceSpecific {
463     CuttlefishConfig* config_;
464     std::string id_;
465     friend MutableInstanceSpecific CuttlefishConfig::ForInstance(int num);
466 
MutableInstanceSpecific(CuttlefishConfig * config,const std::string & id)467     MutableInstanceSpecific(CuttlefishConfig* config, const std::string& id)
468         : config_(config), id_(id) {}
469 
470     Json::Value* Dictionary();
471   public:
472     void set_serial_number(const std::string& serial_number);
473     void set_vnc_server_port(int vnc_server_port);
474     void set_tombstone_receiver_port(int tombstone_receiver_port);
475     void set_config_server_port(int config_server_port);
476     void set_frames_server_port(int config_server_port);
477     void set_touch_server_port(int config_server_port);
478     void set_keyboard_server_port(int config_server_port);
479     void set_gatekeeper_vsock_port(int gatekeeper_vsock_port);
480     void set_keymaster_vsock_port(int keymaster_vsock_port);
481     void set_vehicle_hal_server_port(int vehicle_server_port);
482     void set_audiocontrol_server_port(int audiocontrol_server_port);
483     void set_host_port(int host_port);
484     void set_adb_ip_and_port(const std::string& ip_port);
485     void set_rootcanal_hci_port(int rootcanal_hci_port);
486     void set_rootcanal_link_port(int rootcanal_link_port);
487     void set_rootcanal_test_port(int rootcanal_test_port);
488     void set_rootcanal_config_file(const std::string& rootcanal_config_file);
489     void set_rootcanal_default_commands_file(
490         const std::string& rootcanal_default_commands_file);
491     void set_device_title(const std::string& title);
492     void set_mobile_bridge_name(const std::string& mobile_bridge_name);
493     void set_mobile_tap_name(const std::string& mobile_tap_name);
494     void set_wifi_tap_name(const std::string& wifi_tap_name);
495     void set_ethernet_tap_name(const std::string& ethernet_tap_name);
496     void set_session_id(uint32_t session_id);
497     void set_use_allocd(bool use_allocd);
498     void set_vsock_guest_cid(int vsock_guest_cid);
499     void set_uuid(const std::string& uuid);
500     void set_instance_dir(const std::string& instance_dir);
501     // modem simulator related
502     void set_modem_simulator_ports(const std::string& modem_simulator_ports);
503     void set_virtual_disk_paths(const std::vector<std::string>& disk_paths);
504     void set_webrtc_device_id(const std::string& id);
505     void set_start_webrtc_signaling_server(bool start);
506     // Wifi MAC address inside the guest
507     void set_wifi_mac_address(const std::array<unsigned char, 6>&);
508     // Gnss grpc proxy server port inside the host
509     void set_gnss_grpc_proxy_server_port(int gnss_grpc_proxy_server_port);
510     // Gnss grpc proxy local file path
511     void set_gnss_file_path(const std::string &gnss_file_path);
512   };
513 
514  private:
515   std::unique_ptr<Json::Value> dictionary_;
516 
517   void SetPath(const std::string& key, const std::string& path);
518   bool LoadFromFile(const char* file);
519   static CuttlefishConfig* BuildConfigImpl();
520 
521   CuttlefishConfig(const CuttlefishConfig&) = delete;
522   CuttlefishConfig& operator=(const CuttlefishConfig&) = delete;
523 };
524 
525 // Returns the instance number as obtained from the CUTTLEFISH_INSTANCE
526 // environment variable or the username.
527 int GetInstance();
528 
529 // Returns default Vsock CID, which is
530 // GetInstance() + 2
531 int GetDefaultVsockCid();
532 
533 // Calculates vsock server port number
534 // return base + (vsock_guest_cid - 3)
535 int GetVsockServerPort(const int base,
536                        const int vsock_guest_cid);
537 
538 // Returns a path where the launhcer puts a link to the config file which makes
539 // it easily discoverable regardless of what vm manager is in use
540 std::string GetGlobalConfigFileLink();
541 
542 // These functions modify a given base value to make it different accross
543 // different instances by appending the instance id in case of strings or adding
544 // it in case of integers.
545 std::string ForCurrentInstance(const char* prefix);
546 int ForCurrentInstance(int base);
547 
548 // Returns a random serial number appeneded to a given prefix.
549 std::string RandomSerialNumber(const std::string& prefix);
550 
551 std::string DefaultHostArtifactsPath(const std::string& file);
552 std::string HostBinaryPath(const std::string& file);
553 std::string DefaultGuestImagePath(const std::string& file);
554 std::string DefaultEnvironmentPath(const char* environment_key,
555                                    const char* default_value,
556                                    const char* path);
557 
558 // Whether the host supports qemu
559 bool HostSupportsQemuCli();
560 
561 // GPU modes
562 extern const char* const kGpuModeAuto;
563 extern const char* const kGpuModeGuestSwiftshader;
564 extern const char* const kGpuModeDrmVirgl;
565 extern const char* const kGpuModeGfxStream;
566 }  // namespace cuttlefish
567