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 <array>
19 #include <memory>
20 #include <string>
21 #include <set>
22 #include <vector>
23 
24 namespace Json {
25 class Value;
26 }
27 
28 namespace cuttlefish {
29 constexpr char kLogcatSerialMode[] = "serial";
30 constexpr char kLogcatVsockMode[] = "vsock";
31 }
32 
33 namespace cuttlefish {
34 
35 constexpr char kDefaultUuidPrefix[] = "699acfc4-c8c4-11e7-882b-5065f31dc1";
36 constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";
37 constexpr char kVsocUserPrefix[] = "vsoc-";
38 constexpr char kBootStartedMessage[] ="VIRTUAL_DEVICE_BOOT_STARTED";
39 constexpr char kBootCompletedMessage[] = "VIRTUAL_DEVICE_BOOT_COMPLETED";
40 constexpr char kBootFailedMessage[] = "VIRTUAL_DEVICE_BOOT_FAILED";
41 constexpr char kMobileNetworkConnectedMessage[] =
42     "VIRTUAL_DEVICE_NETWORK_MOBILE_CONNECTED";
43 constexpr char kWifiConnectedMessage[] =
44     "VIRTUAL_DEVICE_NETWORK_WIFI_CONNECTED";
45 constexpr char kInternalDirName[] = "internal";
46 constexpr char kSharedDirName[] = "shared";
47 constexpr char kCrosvmVarEmptyDir[] = "/var/empty";
48 
49 enum class AdbMode {
50   VsockTunnel,
51   VsockHalfTunnel,
52   NativeVsock,
53   Unknown,
54 };
55 
56 // Holds the configuration of the cuttlefish instances.
57 class CuttlefishConfig {
58  public:
59   static const CuttlefishConfig* Get();
60   static bool ConfigExists();
61 
62   CuttlefishConfig();
63   CuttlefishConfig(CuttlefishConfig&&);
64   ~CuttlefishConfig();
65   CuttlefishConfig& operator=(CuttlefishConfig&&);
66 
67   // Saves the configuration object in a file, it can then be read in other
68   // processes by passing the --config_file option.
69   bool SaveToFile(const std::string& file) const;
70 
71   std::string assembly_dir() const;
72   void set_assembly_dir(const std::string& assembly_dir);
73 
74   std::string AssemblyPath(const std::string&) const;
75 
76   std::string composite_disk_path() const;
77 
78   std::string vm_manager() const;
79   void set_vm_manager(const std::string& name);
80 
81   std::string gpu_mode() const;
82   void set_gpu_mode(const std::string& name);
83 
84   int cpus() const;
85   void set_cpus(int cpus);
86 
87   int memory_mb() const;
88   void set_memory_mb(int memory_mb);
89 
90   int dpi() const;
91   void set_dpi(int dpi);
92 
93   int x_res() const;
94   void set_x_res(int x_res);
95 
96   int y_res() const;
97   void set_y_res(int y_res);
98 
99   int refresh_rate_hz() const;
100   void set_refresh_rate_hz(int refresh_rate_hz);
101 
102   // Returns kernel image extracted from the boot image or the user-provided one
103   // if given by command line to the launcher. This function should not be used
104   // to get the kernel image the vmm should boot, GetKernelImageToUse() should
105   // be used instead.
106   std::string kernel_image_path() const;
107   void set_kernel_image_path(const std::string& kernel_image_path);
108 
109   bool decompress_kernel() const;
110   void set_decompress_kernel(bool decompress_kernel);
111 
112   // Returns the path to the kernel image that should be given to the vm manager
113   // to boot, takes into account whether the original image was decompressed or
114   // not.
GetKernelImageToUse()115   std::string GetKernelImageToUse() const {
116     return decompress_kernel() ? decompressed_kernel_image_path()
117                                : kernel_image_path();
118   }
119 
120   std::string decompressed_kernel_image_path() const;
121   void set_decompressed_kernel_image_path(const std::string& path);
122 
123   bool use_unpacked_kernel() const;
124   void set_use_unpacked_kernel(bool use_unpacked_kernel);
125 
126   std::string gdb_flag() const;
127   void set_gdb_flag(const std::string& gdb);
128 
129   std::string ramdisk_image_path() const;
130   void set_ramdisk_image_path(const std::string& ramdisk_image_path);
131 
132   std::string initramfs_path() const;
133   void set_initramfs_path(const std::string& initramfs_path);
134 
135   std::string final_ramdisk_path() const;
136   void set_final_ramdisk_path(const std::string& final_ramdisk_path);
137 
138   std::string vendor_ramdisk_image_path() const;
139   void set_vendor_ramdisk_image_path(const std::string&
140     vendor_ramdisk_image_path);
141 
142   bool deprecated_boot_completed() const;
143   void set_deprecated_boot_completed(bool deprecated_boot_completed);
144 
145   std::string logcat_receiver_binary() const;
146   void set_logcat_receiver_binary(const std::string& binary);
147 
148   std::string config_server_binary() const;
149   void set_config_server_binary(const std::string& binary);
150 
151   void set_cuttlefish_env_path(const std::string& path);
152   std::string cuttlefish_env_path() const;
153 
154   void set_adb_mode(const std::set<std::string>& modes);
155   std::set<AdbMode> adb_mode() const;
156 
157   void set_setupwizard_mode(const std::string& title);
158   std::string setupwizard_mode() const;
159 
160   void set_qemu_binary(const std::string& qemu_binary);
161   std::string qemu_binary() const;
162 
163   void set_crosvm_binary(const std::string& crosvm_binary);
164   std::string crosvm_binary() const;
165 
166   void set_tpm_binary(const std::string& tpm_binary);
167   std::string tpm_binary() const;
168 
169   void set_tpm_device(const std::string& tpm_device);
170   std::string tpm_device() const;
171 
172   void set_console_forwarder_binary(const std::string& crosvm_binary);
173   std::string console_forwarder_binary() const;
174 
175   void set_kernel_log_monitor_binary(
176       const std::string& kernel_log_monitor_binary);
177   std::string kernel_log_monitor_binary() const;
178 
179   void set_enable_vnc_server(bool enable_vnc_server);
180   bool enable_vnc_server() const;
181 
182   void set_vnc_server_binary(const std::string& vnc_server_binary);
183   std::string vnc_server_binary() const;
184 
185   void set_enable_sandbox(const bool enable_sandbox);
186   bool enable_sandbox() const;
187 
188   void set_seccomp_policy_dir(const std::string& seccomp_policy_dir);
189   std::string seccomp_policy_dir() const;
190 
191   void set_enable_webrtc(bool enable_webrtc);
192   bool enable_webrtc() const;
193 
194   void set_webrtc_binary(const std::string& webrtc_binary);
195   std::string webrtc_binary() const;
196 
197   void set_webrtc_assets_dir(const std::string& webrtc_assets_dir);
198   std::string webrtc_assets_dir() const;
199 
200   void set_webrtc_enable_adb_websocket(bool enable);
201   bool webrtc_enable_adb_websocket() const;
202 
203   void set_restart_subprocesses(bool restart_subprocesses);
204   bool restart_subprocesses() const;
205 
206   void set_run_adb_connector(bool run_adb_connector);
207   bool run_adb_connector() const;
208 
209   void set_adb_connector_binary(const std::string& adb_connector_binary);
210   std::string adb_connector_binary() const;
211 
212   void set_socket_vsock_proxy_binary(const std::string& binary);
213   std::string socket_vsock_proxy_binary() const;
214 
215   void set_run_as_daemon(bool run_as_daemon);
216   bool run_as_daemon() const;
217 
218   void set_data_policy(const std::string& data_policy);
219   std::string data_policy() const;
220 
221   void set_blank_data_image_mb(int blank_data_image_mb);
222   int blank_data_image_mb() const;
223 
224   void set_blank_data_image_fmt(const std::string& blank_data_image_fmt);
225   std::string blank_data_image_fmt() const;
226 
227   void set_enable_tombstone_receiver(bool enable_tombstone_receiver);
228   bool enable_tombstone_receiver() const;
229 
230   void set_tombstone_receiver_binary(const std::string& binary);
231   std::string tombstone_receiver_binary() const;
232 
233   void set_use_bootloader(bool use_bootloader);
234   bool use_bootloader() const;
235 
236   void set_bootloader(const std::string& bootloader_path);
237   std::string bootloader() const;
238 
239   void set_boot_slot(const std::string& boot_slot);
240   std::string boot_slot() const;
241 
242   void set_loop_max_part(int loop_max_part);
243   int loop_max_part() const;
244 
245   void set_guest_enforce_security(bool guest_enforce_security);
246   bool guest_enforce_security() const;
247 
248   void set_guest_audit_security(bool guest_audit_security);
249   bool guest_audit_security() const;
250 
251   void set_guest_force_normal_boot(bool guest_force_normal_boot);
252   bool guest_force_normal_boot() const;
253 
254   enum Answer {
255     kUnknown = 0,
256     kYes,
257     kNo,
258   };
259 
260   void set_enable_metrics(std::string enable_metrics);
261   CuttlefishConfig::Answer enable_metrics() const;
262 
263   void set_metrics_binary(const std::string& metrics_binary);
264   std::string metrics_binary() const;
265 
266   void set_boot_image_kernel_cmdline(std::string boot_image_kernel_cmdline);
267   std::vector<std::string> boot_image_kernel_cmdline() const;
268 
269   void set_extra_kernel_cmdline(std::string extra_cmdline);
270   std::vector<std::string> extra_kernel_cmdline() const;
271 
272   void set_vm_manager_kernel_cmdline(std::string vm_manager_cmdline);
273   std::vector<std::string> vm_manager_kernel_cmdline() const;
274 
275   // A directory containing the SSL certificates for the signaling server
276   void set_webrtc_certs_dir(const std::string& certs_dir);
277   std::string webrtc_certs_dir() const;
278 
279   // The path to the webrtc signaling server binary
280   void set_sig_server_binary(const std::string& sig_server_binary);
281   std::string sig_server_binary() const;
282 
283   // The port for the webrtc signaling server. It's used by the signaling server
284   // to bind to it and by the webrtc process to connect to and register itself
285   void set_sig_server_port(int port);
286   int sig_server_port() const;
287 
288   // The range of UDP ports available for webrtc sessions.
289   void set_webrtc_udp_port_range(std::pair<uint16_t, uint16_t> range);
290   std::pair<uint16_t, uint16_t> webrtc_udp_port_range() const;
291 
292   // The range of TCP ports available for webrtc sessions.
293   void set_webrtc_tcp_port_range(std::pair<uint16_t, uint16_t> range);
294   std::pair<uint16_t, uint16_t> webrtc_tcp_port_range() const;
295 
296   // The address of the signaling server
297   void set_sig_server_address(const std::string& addr);
298   std::string sig_server_address() const;
299 
300   // The path section of the url where the webrtc process registers itself with
301   // the signaling server
302   void set_sig_server_path(const std::string& path);
303   std::string sig_server_path() const;
304 
305   // Whether the webrtc process should attempt to verify the authenticity of the
306   // signaling server (reject self signed certificates)
307   void set_sig_server_strict(bool strict);
308   bool sig_server_strict() const;
309 
310   // The dns address of mobile network (RIL)
311   void set_ril_dns(const std::string& ril_dns);
312   std::string ril_dns() const;
313 
314   // KGDB configuration for kernel debugging
315   void set_kgdb(bool kgdb);
316   bool kgdb() const;
317 
318   void set_enable_modem_simulator(bool enable_modem_simulator);
319   bool enable_modem_simulator() const;
320 
321   void set_modem_simulator_binary(const std::string& binary);
322   std::string modem_simulator_binary() const;
323 
324   void set_modem_simulator_instance_number(int instance_numbers);
325   int modem_simulator_instance_number() const;
326 
327   class InstanceSpecific;
328   class MutableInstanceSpecific;
329 
330   MutableInstanceSpecific ForInstance(int instance_num);
331   InstanceSpecific ForInstance(int instance_num) const;
332   InstanceSpecific ForDefaultInstance() const;
333 
334   std::vector<InstanceSpecific> Instances() const;
335 
336   // A view into an existing CuttlefishConfig object for a particular instance.
337   class InstanceSpecific {
338     const CuttlefishConfig* config_;
339     std::string id_;
340     friend InstanceSpecific CuttlefishConfig::ForInstance(int num) const;
341     friend InstanceSpecific CuttlefishConfig::ForDefaultInstance() const;
342     friend std::vector<InstanceSpecific> CuttlefishConfig::Instances() const;
343 
InstanceSpecific(const CuttlefishConfig * config,const std::string & id)344     InstanceSpecific(const CuttlefishConfig* config, const std::string& id)
345         : config_(config), id_(id) {}
346 
347     Json::Value* Dictionary();
348     const Json::Value* Dictionary() const;
349   public:
350     std::string serial_number() const;
351     // If any of the following port numbers is 0, the relevant service is not
352     // running on the guest.
353 
354     // Port number to connect to vnc server on the host
355     int vnc_server_port() const;
356     // Port number to connect to the tombstone receiver on the host
357     int tombstone_receiver_port() const;
358     // Port number to connect to the config server on the host
359     int config_server_port() const;
360     // Port number to connect to the keyboard server on the host. (Only
361     // operational if QEMU is the vmm.)
362     int keyboard_server_port() const;
363     // Port number to connect to the touch server on the host. (Only
364     // operational if QEMU is the vmm.)
365     int touch_server_port() const;
366     // Port number to connect to the frame server on the host. (Only
367     // operational if using swiftshader as the GPU.)
368     int frames_server_port() const;
369     // Port number to connect to the adb server on the host
370     int host_port() const;
371     // Port number to connect to the tpm server on the host
372     int tpm_port() const;
373     // Port number to connect to the gatekeeper server on the host
374     int gatekeeper_vsock_port() const;
375     // Port number to connect to the keymaster server on the host
376     int keymaster_vsock_port() const;
377     std::string adb_ip_and_port() const;
378     std::string adb_device_name() const;
379     std::string device_title() const;
380     std::string mobile_bridge_name() const;
381     std::string mobile_tap_name() const;
382     std::string wifi_tap_name() const;
383     int vsock_guest_cid() const;
384     std::string uuid() const;
385     std::string instance_name() const;
386     std::vector<std::string> virtual_disk_paths() const;
387 
388     // Returns the path to a file with the given name in the instance directory..
389     std::string PerInstancePath(const char* file_name) const;
390     std::string PerInstanceInternalPath(const char* file_name) const;
391 
392     std::string instance_dir() const;
393 
394     std::string instance_internal_dir() const;
395 
396     std::string touch_socket_path() const;
397     std::string keyboard_socket_path() const;
398     std::string frames_socket_path() const;
399 
400     std::string access_kregistry_path() const;
401 
402     std::string pstore_path() const;
403 
404     std::string console_path() const;
405 
406     std::string logcat_path() const;
407 
408     std::string kernel_log_pipe_name() const;
409 
410     std::string console_in_pipe_name() const;
411     std::string console_out_pipe_name() const;
412 
413     std::string logcat_pipe_name() const;
414 
415     std::string launcher_log_path() const;
416 
417     std::string launcher_monitor_socket_path() const;
418 
419     std::string sdcard_path() const;
420 
421     // modem simulator related
422     std::string modem_simulator_ports() const;
423 
424     // The device id the webrtc process should use to register with the
425     // signaling server
426     std::string webrtc_device_id() const;
427 
428     // Whether this instance should start the webrtc signaling server
429     bool start_webrtc_sig_server() const;
430 
431     // Wifi MAC address inside the guest
432     std::array<unsigned char, 6> wifi_mac_address() const;
433   };
434 
435   // A view into an existing CuttlefishConfig object for a particular instance.
436   class MutableInstanceSpecific {
437     CuttlefishConfig* config_;
438     std::string id_;
439     friend MutableInstanceSpecific CuttlefishConfig::ForInstance(int num);
440 
MutableInstanceSpecific(CuttlefishConfig * config,const std::string & id)441     MutableInstanceSpecific(CuttlefishConfig* config, const std::string& id)
442         : config_(config), id_(id) {}
443 
444     Json::Value* Dictionary();
445   public:
446     void set_serial_number(const std::string& serial_number);
447     void set_vnc_server_port(int vnc_server_port);
448     void set_tombstone_receiver_port(int tombstone_receiver_port);
449     void set_config_server_port(int config_server_port);
450     void set_frames_server_port(int config_server_port);
451     void set_touch_server_port(int config_server_port);
452     void set_keyboard_server_port(int config_server_port);
453     void set_gatekeeper_vsock_port(int gatekeeper_vsock_port);
454     void set_keymaster_vsock_port(int keymaster_vsock_port);
455     void set_host_port(int host_port);
456     void set_tpm_port(int tpm_port);
457     void set_adb_ip_and_port(const std::string& ip_port);
458     void set_device_title(const std::string& title);
459     void set_mobile_bridge_name(const std::string& mobile_bridge_name);
460     void set_mobile_tap_name(const std::string& mobile_tap_name);
461     void set_wifi_tap_name(const std::string& wifi_tap_name);
462     void set_vsock_guest_cid(int vsock_guest_cid);
463     void set_uuid(const std::string& uuid);
464     void set_instance_dir(const std::string& instance_dir);
465     // modem simulator related
466     void set_modem_simulator_ports(const std::string& modem_simulator_ports);
467     void set_virtual_disk_paths(const std::vector<std::string>& disk_paths);
468     void set_webrtc_device_id(const std::string& id);
469     void set_start_webrtc_signaling_server(bool start);
470     // Wifi MAC address inside the guest
471     void set_wifi_mac_address(const std::array<unsigned char, 6>&);
472   };
473 
474  private:
475   std::unique_ptr<Json::Value> dictionary_;
476 
477   void SetPath(const std::string& key, const std::string& path);
478   bool LoadFromFile(const char* file);
479   static CuttlefishConfig* BuildConfigImpl();
480 
481   CuttlefishConfig(const CuttlefishConfig&) = delete;
482   CuttlefishConfig& operator=(const CuttlefishConfig&) = delete;
483 };
484 
485 // Returns the instance number as obtained from the CUTTLEFISH_INSTANCE
486 // environment variable or the username.
487 int GetInstance();
488 // Returns a path where the launhcer puts a link to the config file which makes
489 // it easily discoverable regardless of what vm manager is in use
490 std::string GetGlobalConfigFileLink();
491 
492 // These functions modify a given base value to make it different accross
493 // different instances by appending the instance id in case of strings or adding
494 // it in case of integers.
495 std::string ForCurrentInstance(const char* prefix);
496 int ForCurrentInstance(int base);
497 
498 // Returns a random serial number appeneded to a given prefix.
499 std::string RandomSerialNumber(const std::string& prefix);
500 
501 std::string GetDefaultMempath();
502 int GetDefaultPerInstanceVsockCid();
503 
504 std::string DefaultHostArtifactsPath(const std::string& file);
505 std::string DefaultGuestImagePath(const std::string& file);
506 std::string DefaultEnvironmentPath(const char* environment_key,
507                                    const char* default_value,
508                                    const char* path);
509 
510 // Whether the host supports qemu
511 bool HostSupportsQemuCli();
512 bool HostSupportsVsock();
513 
514 // GPU modes
515 extern const char* const kGpuModeGuestSwiftshader;
516 extern const char* const kGpuModeDrmVirgl;
517 extern const char* const kGpuModeGfxStream;
518 }  // namespace cuttlefish
519