Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_CROSTINI_SERVICE_H_ |
| 6 | #define PATCHPANEL_CROSTINI_SERVICE_H_ |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 7 | |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <memory> |
Hugo Benichi | 69c989d | 2021-03-01 00:23:39 +0900 | [diff] [blame^] | 10 | #include <vector> |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include <base/memory/weak_ptr.h> |
| 14 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 15 | #include "patchpanel/address_manager.h" |
| 16 | #include "patchpanel/datapath.h" |
| 17 | #include "patchpanel/device.h" |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 18 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 19 | namespace patchpanel { |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 20 | |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 21 | // Crostini networking service handling address allocation and TAP device |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 22 | // management for Crostini VMs. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 23 | class CrostiniService { |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 24 | public: |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 25 | // All pointers are required and must not be null, and are owned by the |
| 26 | // caller. |
Hugo Benichi | 69c989d | 2021-03-01 00:23:39 +0900 | [diff] [blame^] | 27 | CrostiniService(AddressManager* addr_mgr, |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 28 | Datapath* datapath, |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 29 | Device::ChangeEventHandler device_changed_handler); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 30 | CrostiniService(const CrostiniService&) = delete; |
| 31 | CrostiniService& operator=(const CrostiniService&) = delete; |
| 32 | |
Garrick Evans | c1ac5c4 | 2020-03-31 15:31:22 +0900 | [diff] [blame] | 33 | ~CrostiniService(); |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 34 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 35 | bool Start(uint64_t vm_id, bool is_termina, int subnet_index); |
| 36 | void Stop(uint64_t vm_id, bool is_termina); |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 37 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 38 | const Device* const TAP(uint64_t vm_id, bool is_termina) const; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 39 | |
Garrick Evans | 02e6e87 | 2020-11-30 11:53:13 +0900 | [diff] [blame] | 40 | // Walks the current list of devices managed by the service invoking the |
| 41 | // callback for each, allowing for safe inspection/evaluation. |
| 42 | // The first two callback args correspond to the vm_id and is_termina values |
| 43 | // originally provided to the TAP() function that created the device. |
| 44 | void ScanDevices(base::RepeatingCallback<void(uint64_t, bool, const Device&)> |
| 45 | callback) const; |
| 46 | |
Hugo Benichi | 69c989d | 2021-03-01 00:23:39 +0900 | [diff] [blame^] | 47 | // Returns a list of all tap Devices currently managed by this service. |
| 48 | std::vector<const Device*> GetDevices() const; |
| 49 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 50 | private: |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 51 | std::unique_ptr<Device> AddTAP(bool is_termina, int subnet_index); |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 52 | |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 53 | // Checks ADB sideloading status and set it to |adb_sideloading_enabled_|. |
| 54 | // This function will call itself again if ADB sideloading status is not |
| 55 | // known yet. Otherwise, it will process all currently running Crostini VMs. |
| 56 | void CheckAdbSideloadingStatus(); |
| 57 | |
| 58 | // Start and stop ADB traffic forwarding from Crostini's TAP device |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 59 | // patchpanel's adb-proxy. |ifname| is the Crostini's TAP interface that |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 60 | // will be forwarded. These methods call permission broker DBUS APIs to port |
| 61 | // forward and accept traffic. |
| 62 | void StartAdbPortForwarding(const std::string& ifname); |
| 63 | void StopAdbPortForwarding(const std::string& ifname); |
| 64 | |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 65 | AddressManager* addr_mgr_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 66 | Datapath* datapath_; |
Garrick Evans | 209a80a | 2020-11-30 14:42:40 +0900 | [diff] [blame] | 67 | Device::ChangeEventHandler device_changed_handler_; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 68 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 69 | // Mapping of VM IDs to TAP devices |
| 70 | std::map<std::string, std::unique_ptr<Device>> taps_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 71 | |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 72 | bool adb_sideloading_enabled_; |
| 73 | scoped_refptr<dbus::Bus> bus_; |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 74 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 75 | base::WeakPtrFactory<CrostiniService> weak_factory_{this}; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 76 | }; |
| 77 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 78 | } // namespace patchpanel |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 79 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 80 | #endif // PATCHPANEL_CROSTINI_SERVICE_H_ |