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/config_fragment.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 constexpr char kDefaultUuidPrefix[] = "699acfc4-c8c4-11e7-882b-5065f31dc1"; 40 constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE"; 41 constexpr char kVsocUserPrefix[] = "vsoc-"; 42 constexpr char kCvdNamePrefix[] = "cvd-"; 43 constexpr char kBootStartedMessage[] ="VIRTUAL_DEVICE_BOOT_STARTED"; 44 constexpr char kBootCompletedMessage[] = "VIRTUAL_DEVICE_BOOT_COMPLETED"; 45 constexpr char kBootFailedMessage[] = "VIRTUAL_DEVICE_BOOT_FAILED"; 46 constexpr char kMobileNetworkConnectedMessage[] = 47 "VIRTUAL_DEVICE_NETWORK_MOBILE_CONNECTED"; 48 constexpr char kWifiConnectedMessage[] = 49 "VIRTUAL_DEVICE_NETWORK_WIFI_CONNECTED"; 50 constexpr char kEthernetConnectedMessage[] = 51 "VIRTUAL_DEVICE_NETWORK_ETHERNET_CONNECTED"; 52 constexpr char kScreenChangedMessage[] = "VIRTUAL_DEVICE_SCREEN_CHANGED"; 53 constexpr char kDisplayPowerModeChangedMessage[] = 54 "VIRTUAL_DEVICE_DISPLAY_POWER_MODE_CHANGED"; 55 constexpr char kInternalDirName[] = "internal"; 56 constexpr char kSharedDirName[] = "shared"; 57 constexpr char kLogDirName[] = "logs"; 58 constexpr char kCrosvmVarEmptyDir[] = "/var/empty"; 59 constexpr char kKernelLoadedMessage[] = "] Linux version"; 60 constexpr char kBootloaderLoadedMessage[] = "U-Boot 20"; 61 62 enum class SecureHal { 63 Unknown, 64 Keymint, 65 Gatekeeper, 66 }; 67 68 // Holds the configuration of the cuttlefish instances. 69 class CuttlefishConfig { 70 public: 71 static const CuttlefishConfig* Get(); 72 static std::unique_ptr<const CuttlefishConfig> GetFromFile( 73 const std::string& path); 74 static bool ConfigExists(); 75 76 CuttlefishConfig(); 77 CuttlefishConfig(CuttlefishConfig&&); 78 ~CuttlefishConfig(); 79 CuttlefishConfig& operator=(CuttlefishConfig&&); 80 81 // Saves the configuration object in a file, it can then be read in other 82 // processes by passing the --config_file option. 83 bool SaveToFile(const std::string& file) const; 84 85 bool SaveFragment(const ConfigFragment&); 86 bool LoadFragment(ConfigFragment&) const; 87 88 std::string root_dir() const; 89 void set_root_dir(const std::string& root_dir); 90 91 std::string instances_dir() const; 92 std::string InstancesPath(const std::string&) const; 93 94 std::string assembly_dir() const; 95 std::string AssemblyPath(const std::string&) const; 96 97 std::string os_composite_disk_path() const; 98 99 std::string vm_manager() const; 100 void set_vm_manager(const std::string& name); 101 102 std::string gpu_mode() const; 103 void set_gpu_mode(const std::string& name); 104 105 std::string gpu_capture_binary() const; 106 void set_gpu_capture_binary(const std::string&); 107 108 std::string hwcomposer() const; 109 void set_hwcomposer(const std::string&); 110 111 void set_enable_gpu_udmabuf(const bool enable_gpu_udmabuf); 112 bool enable_gpu_udmabuf() const; 113 114 void set_enable_gpu_angle(const bool enable_gpu_angle); 115 bool enable_gpu_angle() const; 116 117 int cpus() const; 118 void set_cpus(int cpus); 119 120 int memory_mb() const; 121 void set_memory_mb(int memory_mb); 122 123 struct DisplayConfig { 124 int width; 125 int height; 126 int dpi; 127 int refresh_rate_hz; 128 }; 129 130 std::vector<DisplayConfig> display_configs() const; 131 void set_display_configs(const std::vector<DisplayConfig>& display_configs); 132 133 int gdb_port() const; 134 void set_gdb_port(int gdb_port); 135 136 bool deprecated_boot_completed() const; 137 void set_deprecated_boot_completed(bool deprecated_boot_completed); 138 139 void set_cuttlefish_env_path(const std::string& path); 140 std::string cuttlefish_env_path() const; 141 142 void set_secure_hals(const std::set<std::string>& hals); 143 std::set<SecureHal> secure_hals() const; 144 145 void set_setupwizard_mode(const std::string& title); 146 std::string setupwizard_mode() const; 147 148 void set_qemu_binary_dir(const std::string& qemu_binary_dir); 149 std::string qemu_binary_dir() const; 150 151 void set_crosvm_binary(const std::string& crosvm_binary); 152 std::string crosvm_binary() const; 153 154 void set_gem5_binary_dir(const std::string& gem5_binary_dir); 155 std::string gem5_binary_dir() const; 156 157 void set_enable_sandbox(const bool enable_sandbox); 158 bool enable_sandbox() const; 159 160 void set_seccomp_policy_dir(const std::string& seccomp_policy_dir); 161 std::string seccomp_policy_dir() const; 162 163 void set_enable_webrtc(bool enable_webrtc); 164 bool enable_webrtc() const; 165 166 void set_webrtc_assets_dir(const std::string& webrtc_assets_dir); 167 std::string webrtc_assets_dir() const; 168 169 void set_webrtc_enable_adb_websocket(bool enable); 170 bool webrtc_enable_adb_websocket() const; 171 172 void set_enable_vehicle_hal_grpc_server(bool enable_vhal_server); 173 bool enable_vehicle_hal_grpc_server() const; 174 175 void set_restart_subprocesses(bool restart_subprocesses); 176 bool restart_subprocesses() 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_bootloader(const std::string& bootloader_path); 191 std::string bootloader() const; 192 193 // TODO (b/163575714) add virtio console support to the bootloader so the 194 // virtio console path for the console device can be taken again. When that 195 // happens, this function can be deleted along with all the code paths it 196 // forces. use_bootloader()197 bool use_bootloader() const { return true; }; 198 199 void set_boot_slot(const std::string& boot_slot); 200 std::string boot_slot() const; 201 202 void set_guest_enforce_security(bool guest_enforce_security); 203 bool guest_enforce_security() const; 204 205 void set_enable_host_bluetooth(bool enable_host_bluetooth); 206 bool enable_host_bluetooth() const; 207 208 enum Answer { 209 kUnknown = 0, 210 kYes, 211 kNo, 212 }; 213 214 void set_enable_metrics(std::string enable_metrics); 215 CuttlefishConfig::Answer enable_metrics() const; 216 217 void set_metrics_binary(const std::string& metrics_binary); 218 std::string metrics_binary() const; 219 220 void set_extra_kernel_cmdline(const std::string& extra_cmdline); 221 std::vector<std::string> extra_kernel_cmdline() const; 222 223 void set_extra_bootconfig_args(const std::string& extra_bootconfig_args); 224 std::vector<std::string> extra_bootconfig_args() const; 225 226 // A directory containing the SSL certificates for the signaling server 227 void set_webrtc_certs_dir(const std::string& certs_dir); 228 std::string webrtc_certs_dir() const; 229 230 // The port for the webrtc signaling server. It's used by the signaling server 231 // to bind to it and by the webrtc process to connect to and register itself 232 void set_sig_server_port(int port); 233 int sig_server_port() const; 234 235 // The range of UDP ports available for webrtc sessions. 236 void set_webrtc_udp_port_range(std::pair<uint16_t, uint16_t> range); 237 std::pair<uint16_t, uint16_t> webrtc_udp_port_range() const; 238 239 // The range of TCP ports available for webrtc sessions. 240 void set_webrtc_tcp_port_range(std::pair<uint16_t, uint16_t> range); 241 std::pair<uint16_t, uint16_t> webrtc_tcp_port_range() const; 242 243 // The address of the signaling server 244 void set_sig_server_address(const std::string& addr); 245 std::string sig_server_address() const; 246 247 // The path section of the url where the webrtc process registers itself with 248 // the signaling server 249 void set_sig_server_path(const std::string& path); 250 std::string sig_server_path() const; 251 252 // Whether the webrtc process should use a secure connection (WSS) to the 253 // signaling server. 254 void set_sig_server_secure(bool secure); 255 bool sig_server_secure() const; 256 257 // Whether the webrtc process should attempt to verify the authenticity of the 258 // signaling server (reject self signed certificates) 259 void set_sig_server_strict(bool strict); 260 bool sig_server_strict() const; 261 262 // A file containing http headers to include in the connection to the 263 // signaling server 264 void set_sig_server_headers_path(const std::string& path); 265 std::string sig_server_headers_path() const; 266 267 // The dns address of mobile network (RIL) 268 void set_ril_dns(const std::string& ril_dns); 269 std::string ril_dns() const; 270 271 // KGDB configuration for kernel debugging 272 void set_kgdb(bool kgdb); 273 bool kgdb() const; 274 275 // Serial console 276 void set_console(bool console); 277 bool console() const; 278 std::string console_dev() const; 279 280 // Configuration flags for a minimal device 281 bool enable_minimal_mode() const; 282 void set_enable_minimal_mode(bool enable_minimal_mode); 283 284 void set_enable_modem_simulator(bool enable_modem_simulator); 285 bool enable_modem_simulator() const; 286 287 void set_modem_simulator_instance_number(int instance_numbers); 288 int modem_simulator_instance_number() const; 289 290 void set_modem_simulator_sim_type(int sim_type); 291 int modem_simulator_sim_type() const; 292 293 void set_host_tools_version(const std::map<std::string, uint32_t>&); 294 std::map<std::string, uint32_t> host_tools_version() const; 295 296 void set_vhost_net(bool vhost_net); 297 bool vhost_net() const; 298 299 void set_vhost_user_mac80211_hwsim(const std::string& path); 300 std::string vhost_user_mac80211_hwsim() const; 301 302 void set_wmediumd_api_server_socket(const std::string& path); 303 std::string wmediumd_api_server_socket() const; 304 305 void set_ap_rootfs_image(const std::string& path); 306 std::string ap_rootfs_image() const; 307 308 void set_ap_kernel_image(const std::string& path); 309 std::string ap_kernel_image() const; 310 311 void set_wmediumd_config(const std::string& path); 312 std::string wmediumd_config() const; 313 314 void set_rootcanal_hci_port(int rootcanal_hci_port); 315 int rootcanal_hci_port() const; 316 317 void set_rootcanal_link_port(int rootcanal_link_port); 318 int rootcanal_link_port() const; 319 320 void set_rootcanal_test_port(int rootcanal_test_port); 321 int rootcanal_test_port() const; 322 323 void set_rootcanal_config_file(const std::string& rootcanal_config_file); 324 std::string rootcanal_config_file() const; 325 326 void set_rootcanal_default_commands_file( 327 const std::string& rootcanal_default_commands_file); 328 std::string rootcanal_default_commands_file() const; 329 330 void set_record_screen(bool record_screen); 331 bool record_screen() const; 332 333 void set_smt(bool smt); 334 bool smt() const; 335 336 void set_enable_audio(bool enable); 337 bool enable_audio() const; 338 339 void set_protected_vm(bool protected_vm); 340 bool protected_vm() const; 341 342 void set_target_arch(Arch target_arch); 343 Arch target_arch() const; 344 345 void set_bootconfig_supported(bool bootconfig_supported); 346 bool bootconfig_supported() const; 347 348 void set_userdata_format(const std::string& userdata_format); 349 std::string userdata_format() const; 350 351 // The path of an AP image in composite disk 352 std::string ap_image_dev_path() const; 353 void set_ap_image_dev_path(const std::string& dev_path); 354 355 class InstanceSpecific; 356 class MutableInstanceSpecific; 357 358 MutableInstanceSpecific ForInstance(int instance_num); 359 InstanceSpecific ForInstance(int instance_num) const; 360 InstanceSpecific ForInstanceName(const std::string& name) const; 361 InstanceSpecific ForDefaultInstance() const; 362 363 std::vector<InstanceSpecific> Instances() const; 364 std::vector<std::string> instance_dirs() const; 365 366 void set_instance_names(const std::vector<std::string>& instance_names); 367 std::vector<std::string> instance_names() const; 368 369 // A view into an existing CuttlefishConfig object for a particular instance. 370 class InstanceSpecific { 371 const CuttlefishConfig* config_; 372 std::string id_; 373 friend InstanceSpecific CuttlefishConfig::ForInstance(int num) const; 374 friend std::vector<InstanceSpecific> CuttlefishConfig::Instances() const; 375 InstanceSpecific(const CuttlefishConfig * config,const std::string & id)376 InstanceSpecific(const CuttlefishConfig* config, const std::string& id) 377 : config_(config), id_(id) {} 378 379 Json::Value* Dictionary(); 380 const Json::Value* Dictionary() const; 381 public: 382 std::string serial_number() const; 383 // If any of the following port numbers is 0, the relevant service is not 384 // running on the guest. 385 386 // Port number for qemu to run a vnc server on the host 387 int qemu_vnc_server_port() const; 388 // Port number to connect to the tombstone receiver on the host 389 int tombstone_receiver_port() const; 390 // Port number to connect to the config server on the host 391 int config_server_port() const; 392 // Port number to connect to the keyboard server on the host. (Only 393 // operational if QEMU is the vmm.) 394 int keyboard_server_port() const; 395 // Port number to connect to the touch server on the host. (Only 396 // operational if QEMU is the vmm.) 397 int touch_server_port() const; 398 // Port number to connect to the vehicle HAL server on the host 399 int vehicle_hal_server_port() const; 400 // Port number to connect to the audiocontrol server on the guest 401 int audiocontrol_server_port() const; 402 // Port number to connect to the adb server on the host 403 int adb_host_port() const; 404 // Device-specific ID to distinguish modem simulators. Must be 4 digits. 405 int modem_simulator_host_id() const; 406 // Port number to connect to the gnss grpc proxy server on the host 407 int gnss_grpc_proxy_server_port() const; 408 std::string adb_ip_and_port() const; 409 // Port number to connect to the camera hal on the guest 410 int camera_server_port() const; 411 412 std::string adb_device_name() const; 413 std::string gnss_file_path() const; 414 std::string mobile_bridge_name() const; 415 std::string mobile_tap_name() const; 416 std::string wifi_tap_name() const; 417 std::string ethernet_tap_name() const; 418 uint32_t session_id() const; 419 bool use_allocd() const; 420 int vsock_guest_cid() const; 421 std::string uuid() const; 422 std::string instance_name() const; 423 std::vector<std::string> virtual_disk_paths() const; 424 425 // Returns the path to a file with the given name in the instance 426 // directory.. 427 std::string PerInstancePath(const char* file_name) const; 428 std::string PerInstanceInternalPath(const char* file_name) const; 429 std::string PerInstanceLogPath(const std::string& file_name) const; 430 431 std::string instance_dir() const; 432 433 std::string instance_internal_dir() const; 434 435 std::string touch_socket_path(int screen_idx) const; 436 std::string keyboard_socket_path() const; 437 std::string switches_socket_path() const; 438 std::string frames_socket_path() const; 439 440 int confui_host_vsock_port() const; 441 442 std::string access_kregistry_path() const; 443 444 std::string hwcomposer_pmem_path() const; 445 446 std::string pstore_path() const; 447 448 std::string console_path() const; 449 450 std::string logcat_path() const; 451 452 std::string kernel_log_pipe_name() const; 453 454 std::string console_pipe_prefix() const; 455 std::string console_in_pipe_name() const; 456 std::string console_out_pipe_name() const; 457 458 std::string gnss_pipe_prefix() const; 459 std::string gnss_in_pipe_name() const; 460 std::string gnss_out_pipe_name() const; 461 462 std::string logcat_pipe_name() const; 463 464 std::string launcher_log_path() const; 465 466 std::string launcher_monitor_socket_path() const; 467 468 std::string sdcard_path() const; 469 470 std::string persistent_composite_disk_path() const; 471 472 std::string uboot_env_image_path() const; 473 474 std::string audio_server_path() const; 475 476 // modem simulator related 477 std::string modem_simulator_ports() const; 478 479 // The device id the webrtc process should use to register with the 480 // signaling server 481 std::string webrtc_device_id() const; 482 483 // Whether this instance should start the webrtc signaling server 484 bool start_webrtc_sig_server() const; 485 486 // Whether to start a reverse proxy to the webrtc signaling server already 487 // running in the host 488 bool start_webrtc_sig_server_proxy() const; 489 490 // Whether this instance should start the wmediumd process 491 bool start_wmediumd() const; 492 493 // Whether this instance should start a rootcanal instance 494 bool start_rootcanal() const; 495 496 // Whether this instance should start an ap instance 497 bool start_ap() const; 498 499 // Wifi MAC address inside the guest 500 int wifi_mac_prefix() const; 501 502 std::string factory_reset_protected_path() const; 503 504 std::string persistent_bootconfig_path() const; 505 506 std::string vbmeta_path() const; 507 508 std::string id() const; 509 }; 510 511 // A view into an existing CuttlefishConfig object for a particular instance. 512 class MutableInstanceSpecific { 513 CuttlefishConfig* config_; 514 std::string id_; 515 friend MutableInstanceSpecific CuttlefishConfig::ForInstance(int num); 516 517 MutableInstanceSpecific(CuttlefishConfig* config, const std::string& id); 518 519 Json::Value* Dictionary(); 520 public: 521 void set_serial_number(const std::string& serial_number); 522 void set_qemu_vnc_server_port(int qemu_vnc_server_port); 523 void set_tombstone_receiver_port(int tombstone_receiver_port); 524 void set_config_server_port(int config_server_port); 525 void set_frames_server_port(int config_server_port); 526 void set_touch_server_port(int config_server_port); 527 void set_keyboard_server_port(int config_server_port); 528 void set_gatekeeper_vsock_port(int gatekeeper_vsock_port); 529 void set_keymaster_vsock_port(int keymaster_vsock_port); 530 void set_vehicle_hal_server_port(int vehicle_server_port); 531 void set_audiocontrol_server_port(int audiocontrol_server_port); 532 void set_adb_host_port(int adb_host_port); 533 void set_modem_simulator_host_id(int modem_simulator_id); 534 void set_adb_ip_and_port(const std::string& ip_port); 535 void set_confui_host_vsock_port(int confui_host_port); 536 void set_camera_server_port(int camera_server_port); 537 void set_mobile_bridge_name(const std::string& mobile_bridge_name); 538 void set_mobile_tap_name(const std::string& mobile_tap_name); 539 void set_wifi_tap_name(const std::string& wifi_tap_name); 540 void set_ethernet_tap_name(const std::string& ethernet_tap_name); 541 void set_session_id(uint32_t session_id); 542 void set_use_allocd(bool use_allocd); 543 void set_vsock_guest_cid(int vsock_guest_cid); 544 void set_uuid(const std::string& uuid); 545 // modem simulator related 546 void set_modem_simulator_ports(const std::string& modem_simulator_ports); 547 void set_virtual_disk_paths(const std::vector<std::string>& disk_paths); 548 void set_webrtc_device_id(const std::string& id); 549 void set_start_webrtc_signaling_server(bool start); 550 void set_start_webrtc_sig_server_proxy(bool start); 551 void set_start_wmediumd(bool start); 552 void set_start_rootcanal(bool start); 553 void set_start_ap(bool start); 554 // Wifi MAC address inside the guest 555 void set_wifi_mac_prefix(const int wifi_mac_prefix); 556 // Gnss grpc proxy server port inside the host 557 void set_gnss_grpc_proxy_server_port(int gnss_grpc_proxy_server_port); 558 // Gnss grpc proxy local file path 559 void set_gnss_file_path(const std::string &gnss_file_path); 560 }; 561 562 private: 563 std::unique_ptr<Json::Value> dictionary_; 564 565 void SetPath(const std::string& key, const std::string& path); 566 bool LoadFromFile(const char* file); 567 static CuttlefishConfig* BuildConfigImpl(const std::string& path); 568 569 CuttlefishConfig(const CuttlefishConfig&) = delete; 570 CuttlefishConfig& operator=(const CuttlefishConfig&) = delete; 571 }; 572 573 // Returns the instance number as obtained from the CUTTLEFISH_INSTANCE 574 // environment variable or the username. 575 int GetInstance(); 576 577 // Returns default Vsock CID, which is 578 // GetInstance() + 2 579 int GetDefaultVsockCid(); 580 581 // Calculates vsock server port number 582 // return base + (vsock_guest_cid - 3) 583 int GetVsockServerPort(const int base, 584 const int vsock_guest_cid); 585 586 // Returns a path where the launhcer puts a link to the config file which makes 587 // it easily discoverable regardless of what vm manager is in use 588 std::string GetGlobalConfigFileLink(); 589 590 // These functions modify a given base value to make it different accross 591 // different instances by appending the instance id in case of strings or adding 592 // it in case of integers. 593 std::string ForCurrentInstance(const char* prefix); 594 int ForCurrentInstance(int base); 595 596 // Returns a random serial number appeneded to a given prefix. 597 std::string RandomSerialNumber(const std::string& prefix); 598 599 std::string DefaultHostArtifactsPath(const std::string& file); 600 std::string HostBinaryPath(const std::string& file); 601 std::string DefaultGuestImagePath(const std::string& file); 602 std::string DefaultEnvironmentPath(const char* environment_key, 603 const char* default_value, 604 const char* path); 605 606 // Whether the host supports qemu 607 bool HostSupportsQemuCli(); 608 609 // GPU modes 610 extern const char* const kGpuModeAuto; 611 extern const char* const kGpuModeGuestSwiftshader; 612 extern const char* const kGpuModeDrmVirgl; 613 extern const char* const kGpuModeGfxStream; 614 615 // HwComposer modes 616 extern const char* const kHwComposerAuto; 617 extern const char* const kHwComposerDrm; 618 extern const char* const kHwComposerRanchu; 619 } // namespace cuttlefish 620