Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +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_ARC_SERVICE_H_ |
| 6 | #define PATCHPANEL_ARC_SERVICE_H_ |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 7 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 8 | #include <deque> |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 9 | #include <map> |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 10 | #include <memory> |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 11 | #include <set> |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 12 | #include <string> |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 13 | #include <vector> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 14 | |
| 15 | #include <base/memory/weak_ptr.h> |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 17 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 18 | #include "patchpanel/address_manager.h" |
| 19 | #include "patchpanel/datapath.h" |
| 20 | #include "patchpanel/device.h" |
| 21 | #include "patchpanel/ipc.pb.h" |
| 22 | #include "patchpanel/shill_client.h" |
| 23 | #include "patchpanel/traffic_forwarder.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 24 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 25 | namespace patchpanel { |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 26 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 27 | class ArcService { |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 28 | public: |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 29 | enum class InterfaceType { |
| 30 | UNKNOWN, |
| 31 | ETHERNET, |
| 32 | WIFI, |
| 33 | CELL, |
| 34 | }; |
| 35 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 36 | // All pointers are required and cannot be null, and are owned by the caller. |
| 37 | ArcService(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 38 | Datapath* datapath, |
| 39 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 40 | TrafficForwarder* forwarder, |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 41 | GuestMessage::GuestType guest); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 42 | ~ArcService(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 43 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 44 | bool Start(uint32_t id); |
| 45 | void Stop(uint32_t id); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 46 | |
Garrick Evans | 38b25a4 | 2020-04-06 15:17:42 +0900 | [diff] [blame] | 47 | // Returns a list of device configurations. This method only really is useful |
| 48 | // when ARCVM is running as it enables the caller to discover which |
| 49 | // configurations, if any, are currently associated to TAP devices. |
| 50 | std::vector<const Device::Config*> GetDeviceConfigs() const; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 51 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 52 | // Callback from ShillClient, invoked whenever the device list changes. |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 53 | // |shill_devices_| will contain all devices currently connected to shill |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 54 | // (e.g. "eth0", "wlan0", etc). |
| 55 | void OnDevicesChanged(const std::set<std::string>& added, |
| 56 | const std::set<std::string>& removed); |
| 57 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 58 | private: |
| 59 | // Returns true if the service has been started for ARC container or ARCVM. |
| 60 | bool IsStarted() const; |
| 61 | |
| 62 | // Build and configure the ARC datapath for the physical device |ifname| |
| 63 | // provided by Shill. |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 64 | void AddDevice(const std::string& ifname); |
| 65 | |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 66 | // Teardown the ARC datapath associated with the physical device |ifname| and |
| 67 | // stops forwarding services. |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 68 | void RemoveDevice(const std::string& ifname); |
| 69 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 70 | // Creates device configurations for all available IPv4 subnets which will be |
| 71 | // assigned to devices as they are added. |
| 72 | void AllocateAddressConfigs(); |
| 73 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 74 | // Reserve a configuration for an interface. |
| 75 | std::unique_ptr<Device::Config> AcquireConfig(const std::string& ifname); |
| 76 | |
| 77 | // Returns a configuration to the pool. |
| 78 | void ReleaseConfig(const std::string& ifname, |
| 79 | std::unique_ptr<Device::Config> config); |
| 80 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 81 | ShillClient* shill_client_; |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 82 | Datapath* datapath_; |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 83 | AddressManager* addr_mgr_; |
| 84 | TrafficForwarder* forwarder_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 85 | GuestMessage::GuestType guest_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 86 | // A set of preallocated device configurations keyed by technology type and |
| 87 | // used for setting up ARCVM tap devices at VM booting time. |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame] | 88 | std::map<InterfaceType, std::deque<std::unique_ptr<Device::Config>>> |
| 89 | available_configs_; |
| 90 | // The list of all Device configurations. Also includes ARC management device |
| 91 | // for ARCVM. |
| 92 | std::vector<Device::Config*> all_configs_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 93 | // The ARC device configurations corresponding to the host physical devices, |
| 94 | // keyed by device interface name. |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 95 | std::map<std::string, std::unique_ptr<Device>> devices_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 96 | // The ARC management device used for legacy adb-over-tcp support and VPN |
| 97 | // forwarding. |
| 98 | std::unique_ptr<Device> arc_device_; |
Hugo Benichi | 4d4bb8f | 2020-07-07 12:16:07 +0900 | [diff] [blame] | 99 | // The PID of the ARC container instance or the CID of ARCVM instance. |
| 100 | uint32_t id_; |
Hugo Benichi | f0f10c7 | 2020-07-09 10:42:45 +0900 | [diff] [blame^] | 101 | // All devices currently managed by shill. |
| 102 | std::set<std::string> shill_devices_; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 103 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 104 | FRIEND_TEST(ArcServiceTest, NotStarted_AddDevice); |
| 105 | FRIEND_TEST(ArcServiceTest, NotStarted_AddRemoveDevice); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 106 | |
| 107 | base::WeakPtrFactory<ArcService> weak_factory_{this}; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 108 | DISALLOW_COPY_AND_ASSIGN(ArcService); |
| 109 | }; |
| 110 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 111 | } // namespace patchpanel |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 112 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 113 | #endif // PATCHPANEL_ARC_SERVICE_H_ |