blob: 2f6052909c4cf4f57e475b502822d0f165d531f2 [file] [log] [blame]
Garrick Evans5d55f5e2019-07-17 15:28:10 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_ARC_SERVICE_H_
6#define PATCHPANEL_ARC_SERVICE_H_
Garrick Evans5d55f5e2019-07-17 15:28:10 +09007
Garrick Evans86c7d9c2020-03-17 09:25:48 +09008#include <deque>
Garrick Evans1b1f67c2020-02-04 16:21:25 +09009#include <map>
Garrick Evans3915af32019-07-25 15:44:34 +090010#include <memory>
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090011#include <set>
Garrick Evans3915af32019-07-25 15:44:34 +090012#include <string>
Garrick Evans2961c7c2020-04-03 11:34:40 +090013#include <vector>
Garrick Evans54861622019-07-19 09:05:09 +090014
15#include <base/memory/weak_ptr.h>
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090016#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evans54861622019-07-19 09:05:09 +090017
Garrick Evans3388a032020-03-24 11:25:55 +090018#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 Evans5d55f5e2019-07-17 15:28:10 +090024
Garrick Evans3388a032020-03-24 11:25:55 +090025namespace patchpanel {
Garrick Evans5d55f5e2019-07-17 15:28:10 +090026
Garrick Evansf29f5a32019-12-06 11:34:25 +090027class ArcService {
Garrick Evans5d55f5e2019-07-17 15:28:10 +090028 public:
Garrick Evansb4eb3892019-11-13 12:07:07 +090029 class Impl {
30 public:
31 virtual ~Impl() = default;
32
Garrick Evans015b0d62020-02-07 09:06:38 +090033 virtual uint32_t id() const = 0;
Garrick Evansb4eb3892019-11-13 12:07:07 +090034
Garrick Evans38b25a42020-04-06 15:17:42 +090035 // 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 Evans015b0d62020-02-07 09:06:38 +090040 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 Benichiad1bdd92020-06-12 13:48:37 +090043 // Enables the datapath for a physical Device. This is never called for
44 // the ARC management device.
Garrick Evansb4eb3892019-11-13 12:07:07 +090045 virtual bool OnStartDevice(Device* device) = 0;
Garrick Evansb4eb3892019-11-13 12:07:07 +090046
47 protected:
48 Impl() = default;
Garrick Evans2c263102019-07-26 16:07:18 +090049 };
50
Garrick Evansd90a3822019-11-12 17:53:08 +090051 // Encapsulates all ARC++ container-specific logic.
Garrick Evansb4eb3892019-11-13 12:07:07 +090052 class ContainerImpl : public Impl {
Garrick Evansd90a3822019-11-12 17:53:08 +090053 public:
Hugo Benichi4833fda2020-07-06 14:56:56 +090054 ContainerImpl(Datapath* datapath);
Garrick Evansd90a3822019-11-12 17:53:08 +090055 ~ContainerImpl() = default;
56
Garrick Evans015b0d62020-02-07 09:06:38 +090057 uint32_t id() const override;
Garrick Evans38b25a42020-04-06 15:17:42 +090058 std::vector<const Device::Config*> GetDeviceConfigs() const override {
59 return {};
60 }
Garrick Evansb4eb3892019-11-13 12:07:07 +090061
Garrick Evans015b0d62020-02-07 09:06:38 +090062 bool Start(uint32_t pid) override;
63 void Stop(uint32_t pid) override;
64 bool IsStarted(uint32_t* pid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090065 bool OnStartDevice(Device* device) override;
Garrick Evansd90a3822019-11-12 17:53:08 +090066
67 private:
Garrick Evans015b0d62020-02-07 09:06:38 +090068 uint32_t pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +090069 Datapath* datapath_;
Garrick Evansd90a3822019-11-12 17:53:08 +090070
Garrick Evansd90a3822019-11-12 17:53:08 +090071 base::WeakPtrFactory<ContainerImpl> weak_factory_{this};
72 DISALLOW_COPY_AND_ASSIGN(ContainerImpl);
73 };
74
Garrick Evansb4eb3892019-11-13 12:07:07 +090075 // Encapsulates all ARC VM-specific logic.
76 class VmImpl : public Impl {
77 public:
Garrick Evans2961c7c2020-04-03 11:34:40 +090078 // |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 Benichi4833fda2020-07-06 14:56:56 +090081 VmImpl(Datapath* datapath, const std::vector<Device::Config*>& configs);
Garrick Evansb4eb3892019-11-13 12:07:07 +090082 ~VmImpl() = default;
83
Garrick Evans015b0d62020-02-07 09:06:38 +090084 uint32_t id() const override;
Garrick Evans38b25a42020-04-06 15:17:42 +090085 std::vector<const Device::Config*> GetDeviceConfigs() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090086
Garrick Evans015b0d62020-02-07 09:06:38 +090087 bool Start(uint32_t cid) override;
88 void Stop(uint32_t cid) override;
89 bool IsStarted(uint32_t* cid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090090 bool OnStartDevice(Device* device) override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090091
92 private:
Garrick Evans015b0d62020-02-07 09:06:38 +090093 uint32_t cid_;
Garrick Evansb4eb3892019-11-13 12:07:07 +090094 Datapath* datapath_;
Garrick Evans2961c7c2020-04-03 11:34:40 +090095 std::vector<Device::Config*> configs_;
Garrick Evansb4eb3892019-11-13 12:07:07 +090096
97 DISALLOW_COPY_AND_ASSIGN(VmImpl);
98 };
99
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900100 enum class InterfaceType {
101 UNKNOWN,
102 ETHERNET,
103 WIFI,
104 CELL,
105 };
106
Garrick Evans69b85872020-02-04 11:40:26 +0900107 // All pointers are required and cannot be null, and are owned by the caller.
108 ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900109 Datapath* datapath,
110 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +0900111 TrafficForwarder* forwarder,
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900112 GuestMessage::GuestType guest);
Garrick Evansf29f5a32019-12-06 11:34:25 +0900113 ~ArcService();
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900114
Garrick Evans015b0d62020-02-07 09:06:38 +0900115 bool Start(uint32_t id);
116 void Stop(uint32_t id);
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900117
Garrick Evans38b25a42020-04-06 15:17:42 +0900118 // 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 Evanse94b6de2020-02-20 09:19:13 +0900122
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900123 private:
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900124 // 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 Evans6e4eb3b2020-03-09 07:18:31 +0900130 // 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 Evans54861622019-07-19 09:05:09 +0900141 void StartDevice(Device* device);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900142
143 // Stops and cleans up any virtual interfaces and associated datapath.
Garrick Evans54861622019-07-19 09:05:09 +0900144 void StopDevice(Device* device);
145
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900146 // 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 Evans2961c7c2020-04-03 11:34:40 +0900154 // Returns the list of all configurations ordered by type.
155 std::vector<Device::Config*> ReallocateAddressConfigs();
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900156
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 Evans69b85872020-02-04 11:40:26 +0900164 ShillClient* shill_client_;
Taoyu Li179dcc62019-10-17 11:21:08 +0900165 Datapath* datapath_;
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900166 AddressManager* addr_mgr_;
167 TrafficForwarder* forwarder_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900168 GuestMessage::GuestType guest_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900169 std::unique_ptr<Impl> impl_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900170 // A set of preallocated device configurations keyed by technology type and
171 // used for setting up ARCVM tap devices at VM booting time.
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900172 std::map<InterfaceType, std::deque<std::unique_ptr<Device::Config>>> configs_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900173 // The ARC device configurations corresponding to the host physical devices,
174 // keyed by device interface name.
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900175 std::map<std::string, std::unique_ptr<Device>> devices_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900176 // The ARC management device used for legacy adb-over-tcp support and VPN
177 // forwarding.
178 std::unique_ptr<Device> arc_device_;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900179
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900180 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 Evans6e4eb3b2020-03-09 07:18:31 +0900189 FRIEND_TEST(ArcServiceTest, StopDevice);
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900190 FRIEND_TEST(ArcServiceTest, VerifyAddrConfigs);
191 FRIEND_TEST(ArcServiceTest, VerifyAddrOrder);
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900192 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 Evans54861622019-07-19 09:05:09 +0900197
198 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900199 DISALLOW_COPY_AND_ASSIGN(ArcService);
200};
201
Garrick Evans3388a032020-03-24 11:25:55 +0900202} // namespace patchpanel
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900203
Garrick Evans3388a032020-03-24 11:25:55 +0900204#endif // PATCHPANEL_ARC_SERVICE_H_