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 | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 29 | class Impl { |
| 30 | public: |
| 31 | virtual ~Impl() = default; |
| 32 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 33 | virtual uint32_t id() const = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 34 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 35 | virtual bool Start(uint32_t id) = 0; |
| 36 | virtual void Stop(uint32_t id) = 0; |
| 37 | virtual bool IsStarted(uint32_t* id = nullptr) const = 0; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 38 | // Enables the datapath for a physical Device. This is never called for |
| 39 | // the ARC management device. |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 40 | virtual bool OnStartDevice(Device* device) = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | Impl() = default; |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 44 | }; |
| 45 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 46 | // Encapsulates all ARC++ container-specific logic. |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 47 | class ContainerImpl : public Impl { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 48 | public: |
Hugo Benichi | 4833fda | 2020-07-06 14:56:56 +0900 | [diff] [blame] | 49 | ContainerImpl(Datapath* datapath); |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 50 | ~ContainerImpl() = default; |
| 51 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 52 | uint32_t id() const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 53 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 54 | bool Start(uint32_t pid) override; |
| 55 | void Stop(uint32_t pid) override; |
| 56 | bool IsStarted(uint32_t* pid = nullptr) const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 57 | bool OnStartDevice(Device* device) override; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 58 | |
| 59 | private: |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 60 | uint32_t pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 61 | Datapath* datapath_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 62 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 63 | base::WeakPtrFactory<ContainerImpl> weak_factory_{this}; |
| 64 | DISALLOW_COPY_AND_ASSIGN(ContainerImpl); |
| 65 | }; |
| 66 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 67 | // Encapsulates all ARC VM-specific logic. |
| 68 | class VmImpl : public Impl { |
| 69 | public: |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 70 | VmImpl(Datapath* datapath); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 71 | ~VmImpl() = default; |
| 72 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 73 | uint32_t id() const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 74 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 75 | bool Start(uint32_t cid) override; |
| 76 | void Stop(uint32_t cid) override; |
| 77 | bool IsStarted(uint32_t* cid = nullptr) const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 78 | bool OnStartDevice(Device* device) override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 79 | |
| 80 | private: |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 81 | uint32_t cid_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 82 | Datapath* datapath_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(VmImpl); |
| 85 | }; |
| 86 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 87 | enum class InterfaceType { |
| 88 | UNKNOWN, |
| 89 | ETHERNET, |
| 90 | WIFI, |
| 91 | CELL, |
| 92 | }; |
| 93 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 94 | // All pointers are required and cannot be null, and are owned by the caller. |
| 95 | ArcService(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 96 | Datapath* datapath, |
| 97 | AddressManager* addr_mgr, |
Garrick Evans | f586212 | 2020-03-16 09:13:45 +0900 | [diff] [blame] | 98 | TrafficForwarder* forwarder, |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 99 | GuestMessage::GuestType guest); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 100 | ~ArcService(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 101 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 102 | bool Start(uint32_t id); |
| 103 | void Stop(uint32_t id); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 104 | |
Garrick Evans | 38b25a4 | 2020-04-06 15:17:42 +0900 | [diff] [blame] | 105 | // Returns a list of device configurations. This method only really is useful |
| 106 | // when ARCVM is running as it enables the caller to discover which |
| 107 | // configurations, if any, are currently associated to TAP devices. |
| 108 | std::vector<const Device::Config*> GetDeviceConfigs() const; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 109 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 110 | private: |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 111 | // Callback from ShillClient, invoked whenever the device list changes. |
| 112 | // |devices_| will contain all devices currently connected to shill |
| 113 | // (e.g. "eth0", "wlan0", etc). |
| 114 | void OnDevicesChanged(const std::set<std::string>& added, |
| 115 | const std::set<std::string>& removed); |
| 116 | |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 117 | // Build and configure an ARC device for the interface |name| provided by |
| 118 | // Shill. The new device will be added to |devices_|. If an implementation is |
| 119 | // already running, the device will be started. |
| 120 | void AddDevice(const std::string& ifname); |
| 121 | |
| 122 | // Deletes the ARC device; if an implementation is running, the device will be |
| 123 | // stopped first. |
| 124 | void RemoveDevice(const std::string& ifname); |
| 125 | |
| 126 | // Starts a device by setting up the bridge and configuring some NAT rules, |
| 127 | // then invoking the implementation-specific start routine. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 128 | void StartDevice(Device* device); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 129 | |
| 130 | // Stops and cleans up any virtual interfaces and associated datapath. |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 131 | void StopDevice(Device* device); |
| 132 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 133 | // Creates device configurations for all available IPv4 subnets which will be |
| 134 | // assigned to devices as they are added. |
| 135 | void AllocateAddressConfigs(); |
| 136 | |
| 137 | // This function will temporarily remove existing devices, reallocate |
| 138 | // address configurations and re-add existing devices. This is necessary to |
| 139 | // properly handle the IPv4 addressing binding difference between ARC++ and |
| 140 | // ARCVM. |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 141 | void ReallocateAddressConfigs(); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 142 | |
| 143 | // Reserve a configuration for an interface. |
| 144 | std::unique_ptr<Device::Config> AcquireConfig(const std::string& ifname); |
| 145 | |
| 146 | // Returns a configuration to the pool. |
| 147 | void ReleaseConfig(const std::string& ifname, |
| 148 | std::unique_ptr<Device::Config> config); |
| 149 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 150 | ShillClient* shill_client_; |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 151 | Datapath* datapath_; |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame] | 152 | AddressManager* addr_mgr_; |
| 153 | TrafficForwarder* forwarder_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 154 | GuestMessage::GuestType guest_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 155 | std::unique_ptr<Impl> impl_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 156 | // A set of preallocated device configurations keyed by technology type and |
| 157 | // used for setting up ARCVM tap devices at VM booting time. |
Hugo Benichi | 8e44842 | 2020-07-07 10:49:00 +0900 | [diff] [blame^] | 158 | std::map<InterfaceType, std::deque<std::unique_ptr<Device::Config>>> |
| 159 | available_configs_; |
| 160 | // The list of all Device configurations. Also includes ARC management device |
| 161 | // for ARCVM. |
| 162 | std::vector<Device::Config*> all_configs_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 163 | // The ARC device configurations corresponding to the host physical devices, |
| 164 | // keyed by device interface name. |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 165 | std::map<std::string, std::unique_ptr<Device>> devices_; |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 166 | // The ARC management device used for legacy adb-over-tcp support and VPN |
| 167 | // forwarding. |
| 168 | std::unique_ptr<Device> arc_device_; |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 169 | |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 170 | FRIEND_TEST(ArcServiceTest, ContainerImpl_OnStartDevice); |
| 171 | FRIEND_TEST(ArcServiceTest, ContainerImpl_FailsToConfigureInterface); |
| 172 | FRIEND_TEST(ArcServiceTest, ContainerImpl_OnStopDevice); |
| 173 | FRIEND_TEST(ArcServiceTest, ContainerImpl_Start); |
| 174 | FRIEND_TEST(ArcServiceTest, ContainerImpl_FailsToCreateInterface); |
| 175 | FRIEND_TEST(ArcServiceTest, ContainerImpl_Stop); |
| 176 | FRIEND_TEST(ArcServiceTest, NotStarted_AddDevice); |
| 177 | FRIEND_TEST(ArcServiceTest, NotStarted_AddRemoveDevice); |
| 178 | FRIEND_TEST(ArcServiceTest, StableArcVmMacAddrs); |
Garrick Evans | 6e4eb3b | 2020-03-09 07:18:31 +0900 | [diff] [blame] | 179 | FRIEND_TEST(ArcServiceTest, StopDevice); |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 180 | FRIEND_TEST(ArcServiceTest, VerifyAddrConfigs); |
| 181 | FRIEND_TEST(ArcServiceTest, VerifyAddrOrder); |
Hugo Benichi | ad1bdd9 | 2020-06-12 13:48:37 +0900 | [diff] [blame] | 182 | FRIEND_TEST(ArcServiceTest, VmImpl_Start); |
| 183 | FRIEND_TEST(ArcServiceTest, VmImpl_StartDevice); |
| 184 | FRIEND_TEST(ArcServiceTest, VmImpl_StartMultipleDevices); |
| 185 | FRIEND_TEST(ArcServiceTest, VmImpl_Stop); |
| 186 | FRIEND_TEST(ArcServiceTest, VmImpl_StopDevice); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 187 | |
| 188 | base::WeakPtrFactory<ArcService> weak_factory_{this}; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 189 | DISALLOW_COPY_AND_ASSIGN(ArcService); |
| 190 | }; |
| 191 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 192 | } // namespace patchpanel |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 193 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 194 | #endif // PATCHPANEL_ARC_SERVICE_H_ |