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> |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include <base/memory/weak_ptr.h> |
| 13 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 14 | #include "patchpanel/address_manager.h" |
| 15 | #include "patchpanel/datapath.h" |
| 16 | #include "patchpanel/device.h" |
| 17 | #include "patchpanel/shill_client.h" |
| 18 | #include "patchpanel/traffic_forwarder.h" |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 21 | |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 22 | // Crostini networking service handling address allocation and TAP device |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 23 | // management for Crostini VMs. |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 24 | class CrostiniService { |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 25 | public: |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 26 | // All pointers are required and must not be null, and are owned by the |
| 27 | // caller. |
| 28 | CrostiniService(ShillClient* shill_client, |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 29 | AddressManager* addr_mgr, |
| 30 | Datapath* datapath, |
| 31 | TrafficForwarder* forwarder); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 32 | CrostiniService(const CrostiniService&) = delete; |
| 33 | CrostiniService& operator=(const CrostiniService&) = delete; |
| 34 | |
Garrick Evans | c1ac5c4 | 2020-03-31 15:31:22 +0900 | [diff] [blame] | 35 | ~CrostiniService(); |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 36 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 37 | bool Start(uint64_t vm_id, bool is_termina, int subnet_index); |
| 38 | void Stop(uint64_t vm_id, bool is_termina); |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 39 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 40 | const Device* const TAP(uint64_t vm_id, bool is_termina) const; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 41 | |
| 42 | private: |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 43 | std::unique_ptr<Device> AddTAP(bool is_termina, int subnet_index); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 44 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 45 | const std::string& prev_ifname); |
| 46 | void StartForwarding(const std::string& phys_ifname, |
Jason Jeremy Iman | 0e9f826 | 2020-03-06 14:50:49 +0900 | [diff] [blame] | 47 | const std::string& virt_ifname); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 48 | void StopForwarding(const std::string& phys_ifname, |
| 49 | const std::string& virt_ifname); |
Garrick Evans | b1c9371 | 2020-01-22 09:28:25 +0900 | [diff] [blame] | 50 | |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 51 | // Checks ADB sideloading status and set it to |adb_sideloading_enabled_|. |
| 52 | // This function will call itself again if ADB sideloading status is not |
| 53 | // known yet. Otherwise, it will process all currently running Crostini VMs. |
| 54 | void CheckAdbSideloadingStatus(); |
| 55 | |
| 56 | // Start and stop ADB traffic forwarding from Crostini's TAP device |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 57 | // 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] | 58 | // will be forwarded. These methods call permission broker DBUS APIs to port |
| 59 | // forward and accept traffic. |
| 60 | void StartAdbPortForwarding(const std::string& ifname); |
| 61 | void StopAdbPortForwarding(const std::string& ifname); |
| 62 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 63 | ShillClient* shill_client_; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 64 | AddressManager* addr_mgr_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 65 | Datapath* datapath_; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 66 | TrafficForwarder* forwarder_; |
| 67 | |
Garrick Evans | 51d5b55 | 2020-01-30 10:42:06 +0900 | [diff] [blame] | 68 | // Mapping of VM IDs to TAP devices |
| 69 | std::map<std::string, std::unique_ptr<Device>> taps_; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 70 | |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 71 | bool adb_sideloading_enabled_; |
| 72 | scoped_refptr<dbus::Bus> bus_; |
Jason Jeremy Iman | fa8b6d2 | 2020-02-20 03:44:21 +0000 | [diff] [blame] | 73 | |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 74 | base::WeakPtrFactory<CrostiniService> weak_factory_{this}; |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 75 | }; |
| 76 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 77 | } // namespace patchpanel |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 78 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 79 | #endif // PATCHPANEL_CROSTINI_SERVICE_H_ |