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