blob: 58452bc2f0110eeb079b5b1514ff43c2f8cc85b7 [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 Evans015b0d62020-02-07 09:06:38 +090035 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 Benichiad1bdd92020-06-12 13:48:37 +090038 // Enables the datapath for a physical Device. This is never called for
39 // the ARC management device.
Garrick Evansb4eb3892019-11-13 12:07:07 +090040 virtual bool OnStartDevice(Device* device) = 0;
Garrick Evansb4eb3892019-11-13 12:07:07 +090041
42 protected:
43 Impl() = default;
Garrick Evans2c263102019-07-26 16:07:18 +090044 };
45
Garrick Evansd90a3822019-11-12 17:53:08 +090046 // Encapsulates all ARC++ container-specific logic.
Garrick Evansb4eb3892019-11-13 12:07:07 +090047 class ContainerImpl : public Impl {
Garrick Evansd90a3822019-11-12 17:53:08 +090048 public:
Hugo Benichi4833fda2020-07-06 14:56:56 +090049 ContainerImpl(Datapath* datapath);
Garrick Evansd90a3822019-11-12 17:53:08 +090050 ~ContainerImpl() = default;
51
Garrick Evans015b0d62020-02-07 09:06:38 +090052 uint32_t id() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090053
Garrick Evans015b0d62020-02-07 09:06:38 +090054 bool Start(uint32_t pid) override;
55 void Stop(uint32_t pid) override;
56 bool IsStarted(uint32_t* pid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090057 bool OnStartDevice(Device* device) override;
Garrick Evansd90a3822019-11-12 17:53:08 +090058
59 private:
Garrick Evans015b0d62020-02-07 09:06:38 +090060 uint32_t pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +090061 Datapath* datapath_;
Garrick Evansd90a3822019-11-12 17:53:08 +090062
Garrick Evansd90a3822019-11-12 17:53:08 +090063 base::WeakPtrFactory<ContainerImpl> weak_factory_{this};
64 DISALLOW_COPY_AND_ASSIGN(ContainerImpl);
65 };
66
Garrick Evansb4eb3892019-11-13 12:07:07 +090067 // Encapsulates all ARC VM-specific logic.
68 class VmImpl : public Impl {
69 public:
Hugo Benichi8e448422020-07-07 10:49:00 +090070 VmImpl(Datapath* datapath);
Garrick Evansb4eb3892019-11-13 12:07:07 +090071 ~VmImpl() = default;
72
Garrick Evans015b0d62020-02-07 09:06:38 +090073 uint32_t id() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090074
Garrick Evans015b0d62020-02-07 09:06:38 +090075 bool Start(uint32_t cid) override;
76 void Stop(uint32_t cid) override;
77 bool IsStarted(uint32_t* cid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090078 bool OnStartDevice(Device* device) override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090079
80 private:
Garrick Evans015b0d62020-02-07 09:06:38 +090081 uint32_t cid_;
Garrick Evansb4eb3892019-11-13 12:07:07 +090082 Datapath* datapath_;
83
84 DISALLOW_COPY_AND_ASSIGN(VmImpl);
85 };
86
Garrick Evans86c7d9c2020-03-17 09:25:48 +090087 enum class InterfaceType {
88 UNKNOWN,
89 ETHERNET,
90 WIFI,
91 CELL,
92 };
93
Garrick Evans69b85872020-02-04 11:40:26 +090094 // All pointers are required and cannot be null, and are owned by the caller.
95 ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +090096 Datapath* datapath,
97 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +090098 TrafficForwarder* forwarder,
Hugo Benichiad1bdd92020-06-12 13:48:37 +090099 GuestMessage::GuestType guest);
Garrick Evansf29f5a32019-12-06 11:34:25 +0900100 ~ArcService();
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900101
Garrick Evans015b0d62020-02-07 09:06:38 +0900102 bool Start(uint32_t id);
103 void Stop(uint32_t id);
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900104
Garrick Evans38b25a42020-04-06 15:17:42 +0900105 // 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 Evanse94b6de2020-02-20 09:19:13 +0900109
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900110 private:
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900111 // 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 Evans6e4eb3b2020-03-09 07:18:31 +0900117 // 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 Evans54861622019-07-19 09:05:09 +0900128 void StartDevice(Device* device);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900129
130 // Stops and cleans up any virtual interfaces and associated datapath.
Garrick Evans54861622019-07-19 09:05:09 +0900131 void StopDevice(Device* device);
132
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900133 // 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 Benichi8e448422020-07-07 10:49:00 +0900141 void ReallocateAddressConfigs();
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900142
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 Evans69b85872020-02-04 11:40:26 +0900150 ShillClient* shill_client_;
Taoyu Li179dcc62019-10-17 11:21:08 +0900151 Datapath* datapath_;
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900152 AddressManager* addr_mgr_;
153 TrafficForwarder* forwarder_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900154 GuestMessage::GuestType guest_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900155 std::unique_ptr<Impl> impl_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900156 // A set of preallocated device configurations keyed by technology type and
157 // used for setting up ARCVM tap devices at VM booting time.
Hugo Benichi8e448422020-07-07 10:49:00 +0900158 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 Benichiad1bdd92020-06-12 13:48:37 +0900163 // The ARC device configurations corresponding to the host physical devices,
164 // keyed by device interface name.
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900165 std::map<std::string, std::unique_ptr<Device>> devices_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900166 // The ARC management device used for legacy adb-over-tcp support and VPN
167 // forwarding.
168 std::unique_ptr<Device> arc_device_;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900169
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900170 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 Evans6e4eb3b2020-03-09 07:18:31 +0900179 FRIEND_TEST(ArcServiceTest, StopDevice);
Garrick Evans86c7d9c2020-03-17 09:25:48 +0900180 FRIEND_TEST(ArcServiceTest, VerifyAddrConfigs);
181 FRIEND_TEST(ArcServiceTest, VerifyAddrOrder);
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900182 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 Evans54861622019-07-19 09:05:09 +0900187
188 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900189 DISALLOW_COPY_AND_ASSIGN(ArcService);
190};
191
Garrick Evans3388a032020-03-24 11:25:55 +0900192} // namespace patchpanel
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900193
Garrick Evans3388a032020-03-24 11:25:55 +0900194#endif // PATCHPANEL_ARC_SERVICE_H_