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 | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 8 | #include <map> |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 9 | #include <memory> |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 10 | #include <string> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 11 | |
| 12 | #include <base/memory/weak_ptr.h> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 13 | |
| 14 | #include "arc/network/datapath.h" |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 15 | #include "arc/network/device.h" |
| 16 | #include "arc/network/device_manager.h" |
| 17 | #include "arc/network/ipc.pb.h" |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 18 | #include "arc/network/shill_client.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 19 | |
| 20 | namespace arc_networkd { |
| 21 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 22 | class ArcService { |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 23 | public: |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 24 | class Context : public Device::Context { |
| 25 | public: |
| 26 | Context(); |
| 27 | ~Context() = default; |
| 28 | |
| 29 | // Tracks the lifetime of the ARC++ container. |
| 30 | void Start(); |
| 31 | void Stop(); |
| 32 | bool IsStarted() const; |
| 33 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 34 | // For ARCVM only. |
| 35 | const std::string& TAP() const; |
| 36 | void SetTAP(const std::string& tap); |
| 37 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 38 | private: |
| 39 | // Indicates the device was started. |
| 40 | bool started_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 41 | // For ARCVM, the name of the bound TAP device. |
| 42 | std::string tap_; |
| 43 | }; |
| 44 | |
| 45 | class Impl { |
| 46 | public: |
| 47 | virtual ~Impl() = default; |
| 48 | |
| 49 | virtual GuestMessage::GuestType guest() const = 0; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 50 | virtual uint32_t id() const = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 51 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 52 | virtual bool Start(uint32_t id) = 0; |
| 53 | virtual void Stop(uint32_t id) = 0; |
| 54 | virtual bool IsStarted(uint32_t* id = nullptr) const = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 55 | virtual bool OnStartDevice(Device* device) = 0; |
| 56 | virtual void OnStopDevice(Device* device) = 0; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 57 | virtual void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 58 | const std::string& prev_ifname) = 0; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 59 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 60 | // Returns the ARC management interface. |
| 61 | Device* ArcDevice() const { return arc_device_.get(); } |
| 62 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 63 | protected: |
| 64 | Impl() = default; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 65 | |
| 66 | // For now each implementation manages its own ARC device since ARCVM is |
| 67 | // still single-networked. |
| 68 | std::unique_ptr<Device> arc_device_; |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 71 | // Encapsulates all ARC++ container-specific logic. |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 72 | class ContainerImpl : public Impl { |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 73 | public: |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame^] | 74 | ContainerImpl(Datapath* datapath, |
| 75 | AddressManager* addr_mgr, |
| 76 | TrafficForwarder* forwarder, |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 77 | GuestMessage::GuestType guest); |
| 78 | ~ContainerImpl() = default; |
| 79 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 80 | GuestMessage::GuestType guest() const override; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 81 | uint32_t id() const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 82 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 83 | bool Start(uint32_t pid) override; |
| 84 | void Stop(uint32_t pid) override; |
| 85 | bool IsStarted(uint32_t* pid = nullptr) const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 86 | bool OnStartDevice(Device* device) override; |
| 87 | void OnStopDevice(Device* device) override; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 88 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 89 | const std::string& prev_ifname) override; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 90 | |
| 91 | private: |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 92 | uint32_t pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 93 | Datapath* datapath_; |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame^] | 94 | AddressManager* addr_mgr_; |
| 95 | TrafficForwarder* forwarder_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 96 | GuestMessage::GuestType guest_; |
| 97 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 98 | base::WeakPtrFactory<ContainerImpl> weak_factory_{this}; |
| 99 | DISALLOW_COPY_AND_ASSIGN(ContainerImpl); |
| 100 | }; |
| 101 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 102 | // Encapsulates all ARC VM-specific logic. |
| 103 | class VmImpl : public Impl { |
| 104 | public: |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 105 | VmImpl(ShillClient* shill_client, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame^] | 106 | Datapath* datapath, |
| 107 | AddressManager* addr_mgr, |
| 108 | TrafficForwarder* forwarder); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 109 | ~VmImpl() = default; |
| 110 | |
| 111 | GuestMessage::GuestType guest() const override; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 112 | uint32_t id() const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 113 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 114 | bool Start(uint32_t cid) override; |
| 115 | void Stop(uint32_t cid) override; |
| 116 | bool IsStarted(uint32_t* cid = nullptr) const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 117 | bool OnStartDevice(Device* device) override; |
| 118 | void OnStopDevice(Device* device) override; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 119 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 120 | const std::string& prev_ifname) override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 121 | |
| 122 | private: |
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 | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 128 | std::string tap_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 129 | |
| 130 | DISALLOW_COPY_AND_ASSIGN(VmImpl); |
| 131 | }; |
| 132 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 133 | // All pointers are required and cannot be null, and are owned by the caller. |
| 134 | ArcService(ShillClient* shill_client, |
| 135 | DeviceManagerBase* dev_mgr, |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame^] | 136 | Datapath* datapath, |
| 137 | AddressManager* addr_mgr, |
| 138 | TrafficForwarder* forwarder); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 139 | ~ArcService(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 140 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 141 | bool Start(uint32_t id); |
| 142 | void Stop(uint32_t id); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 143 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 144 | void OnDeviceAdded(Device* device); |
| 145 | void OnDeviceRemoved(Device* device); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 146 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 147 | const std::string& prev_ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 148 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 149 | Device* ArcDevice() const; |
| 150 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 151 | private: |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 152 | void StartDevice(Device* device); |
| 153 | void StopDevice(Device* device); |
| 154 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 155 | // Returns true if the device should be processed by the service. |
| 156 | bool AllowDevice(Device* device) const; |
| 157 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 158 | ShillClient* shill_client_; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 159 | DeviceManagerBase* dev_mgr_; |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 160 | Datapath* datapath_; |
Garrick Evans | 2e5c9ab | 2020-03-05 14:33:58 +0900 | [diff] [blame^] | 161 | AddressManager* addr_mgr_; |
| 162 | TrafficForwarder* forwarder_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 163 | std::unique_ptr<Impl> impl_; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 164 | |
| 165 | base::WeakPtrFactory<ArcService> weak_factory_{this}; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 166 | DISALLOW_COPY_AND_ASSIGN(ArcService); |
| 167 | }; |
| 168 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 169 | namespace test { |
| 170 | extern GuestMessage::GuestType guest; |
| 171 | } // namespace test |
| 172 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 173 | } // namespace arc_networkd |
| 174 | |
| 175 | #endif // ARC_NETWORK_ARC_SERVICE_H_ |