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