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