blob: e980349367a1b21f0fadfe006169fa4ca10c99be [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
Hugo Benichibfc49112020-12-14 12:54:44 +090027constexpr char kArcBridge[] = "arcbr0";
28
Garrick Evansf29f5a32019-12-06 11:34:25 +090029class ArcService {
Garrick Evans5d55f5e2019-07-17 15:28:10 +090030 public:
Garrick Evans69b85872020-02-04 11:40:26 +090031 // All pointers are required and cannot be null, and are owned by the caller.
32 ArcService(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +090033 Datapath* datapath,
34 AddressManager* addr_mgr,
Garrick Evansf5862122020-03-16 09:13:45 +090035 TrafficForwarder* forwarder,
Garrick Evans209a80a2020-11-30 14:42:40 +090036 GuestMessage::GuestType guest,
37 Device::ChangeEventHandler device_changed_handler);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090038 ArcService(const ArcService&) = delete;
39 ArcService& operator=(const ArcService&) = delete;
40
Garrick Evansf29f5a32019-12-06 11:34:25 +090041 ~ArcService();
Garrick Evans5d55f5e2019-07-17 15:28:10 +090042
Garrick Evans015b0d62020-02-07 09:06:38 +090043 bool Start(uint32_t id);
44 void Stop(uint32_t id);
Garrick Evans5d55f5e2019-07-17 15:28:10 +090045
Garrick Evans38b25a42020-04-06 15:17:42 +090046 // Returns a list of device configurations. This method only really is useful
47 // when ARCVM is running as it enables the caller to discover which
48 // configurations, if any, are currently associated to TAP devices.
49 std::vector<const Device::Config*> GetDeviceConfigs() const;
Garrick Evanse94b6de2020-02-20 09:19:13 +090050
Garrick Evans02e6e872020-11-30 11:53:13 +090051 // Walks the current list of devices managed by the service invoking the
52 // callback for each, allowing for safe inspection/evaluation.
53 void ScanDevices(base::RepeatingCallback<void(const Device&)> callback) const;
54
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090055 // Callback from ShillClient, invoked whenever the device list changes.
Hugo Benichif0f10c72020-07-09 10:42:45 +090056 // |shill_devices_| will contain all devices currently connected to shill
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090057 // (e.g. "eth0", "wlan0", etc).
58 void OnDevicesChanged(const std::set<std::string>& added,
59 const std::set<std::string>& removed);
60
Hugo Benichif0f10c72020-07-09 10:42:45 +090061 private:
62 // Returns true if the service has been started for ARC container or ARCVM.
63 bool IsStarted() const;
64
65 // Build and configure the ARC datapath for the physical device |ifname|
66 // provided by Shill.
Hugo Benichi84d96c42021-02-26 14:20:13 +090067 void AddDevice(const std::string& ifname, ShillClient::Device::Type type);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090068
Hugo Benichif0f10c72020-07-09 10:42:45 +090069 // Teardown the ARC datapath associated with the physical device |ifname| and
70 // stops forwarding services.
Hugo Benichi84d96c42021-02-26 14:20:13 +090071 void RemoveDevice(const std::string& ifname, ShillClient::Device::Type type);
Garrick Evans6e4eb3b2020-03-09 07:18:31 +090072
Garrick Evans86c7d9c2020-03-17 09:25:48 +090073 // Creates device configurations for all available IPv4 subnets which will be
74 // assigned to devices as they are added.
75 void AllocateAddressConfigs();
76
Garrick Evans86c7d9c2020-03-17 09:25:48 +090077 // Reserve a configuration for an interface.
Hugo Benichi84d96c42021-02-26 14:20:13 +090078 std::unique_ptr<Device::Config> AcquireConfig(ShillClient::Device::Type type);
Garrick Evans86c7d9c2020-03-17 09:25:48 +090079
80 // Returns a configuration to the pool.
Hugo Benichi84d96c42021-02-26 14:20:13 +090081 void ReleaseConfig(ShillClient::Device::Type type,
Garrick Evans86c7d9c2020-03-17 09:25:48 +090082 std::unique_ptr<Device::Config> config);
83
Garrick Evans69b85872020-02-04 11:40:26 +090084 ShillClient* shill_client_;
Taoyu Li179dcc62019-10-17 11:21:08 +090085 Datapath* datapath_;
Garrick Evans2e5c9ab2020-03-05 14:33:58 +090086 AddressManager* addr_mgr_;
87 TrafficForwarder* forwarder_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +090088 GuestMessage::GuestType guest_;
Garrick Evans209a80a2020-11-30 14:42:40 +090089 Device::ChangeEventHandler device_changed_handler_;
90
Hugo Benichiad1bdd92020-06-12 13:48:37 +090091 // A set of preallocated device configurations keyed by technology type and
92 // used for setting up ARCVM tap devices at VM booting time.
Hugo Benichi84d96c42021-02-26 14:20:13 +090093 std::map<ShillClient::Device::Type,
94 std::deque<std::unique_ptr<Device::Config>>>
Hugo Benichi8e448422020-07-07 10:49:00 +090095 available_configs_;
96 // The list of all Device configurations. Also includes ARC management device
97 // for ARCVM.
98 std::vector<Device::Config*> all_configs_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +090099 // The ARC device configurations corresponding to the host physical devices,
100 // keyed by device interface name.
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900101 std::map<std::string, std::unique_ptr<Device>> devices_;
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900102 // The ARC management device used for legacy adb-over-tcp support and VPN
103 // forwarding.
104 std::unique_ptr<Device> arc_device_;
Hugo Benichi4d4bb8f2020-07-07 12:16:07 +0900105 // The PID of the ARC container instance or the CID of ARCVM instance.
106 uint32_t id_;
Hugo Benichif0f10c72020-07-09 10:42:45 +0900107 // All devices currently managed by shill.
Hugo Benichi84d96c42021-02-26 14:20:13 +0900108 std::map<std::string, ShillClient::Device::Type> shill_devices_;
Garrick Evans6e4eb3b2020-03-09 07:18:31 +0900109
Hugo Benichiad1bdd92020-06-12 13:48:37 +0900110 FRIEND_TEST(ArcServiceTest, NotStarted_AddDevice);
111 FRIEND_TEST(ArcServiceTest, NotStarted_AddRemoveDevice);
Garrick Evans54861622019-07-19 09:05:09 +0900112
113 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900114};
115
Garrick Evans3388a032020-03-24 11:25:55 +0900116} // namespace patchpanel
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900117
Garrick Evans3388a032020-03-24 11:25:55 +0900118#endif // PATCHPANEL_ARC_SERVICE_H_