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: |
| 74 | ContainerImpl(DeviceManagerBase* dev_mgr, |
| 75 | Datapath* datapath, |
| 76 | GuestMessage::GuestType guest); |
| 77 | ~ContainerImpl() = default; |
| 78 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 79 | GuestMessage::GuestType guest() const override; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 80 | uint32_t id() const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 81 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 82 | bool Start(uint32_t pid) override; |
| 83 | void Stop(uint32_t pid) override; |
| 84 | bool IsStarted(uint32_t* pid = nullptr) const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 85 | bool OnStartDevice(Device* device) override; |
| 86 | void OnStopDevice(Device* device) override; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 87 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 88 | const std::string& prev_ifname) override; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 89 | |
| 90 | private: |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 91 | uint32_t pid_; |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 92 | DeviceManagerBase* dev_mgr_; |
| 93 | Datapath* datapath_; |
| 94 | GuestMessage::GuestType guest_; |
| 95 | |
Garrick Evans | d90a382 | 2019-11-12 17:53:08 +0900 | [diff] [blame] | 96 | base::WeakPtrFactory<ContainerImpl> weak_factory_{this}; |
| 97 | DISALLOW_COPY_AND_ASSIGN(ContainerImpl); |
| 98 | }; |
| 99 | |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 100 | // Encapsulates all ARC VM-specific logic. |
| 101 | class VmImpl : public Impl { |
| 102 | public: |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 103 | VmImpl(ShillClient* shill_client, |
| 104 | DeviceManagerBase* dev_mgr, |
| 105 | Datapath* datapath); |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 106 | ~VmImpl() = default; |
| 107 | |
| 108 | GuestMessage::GuestType guest() const override; |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 109 | uint32_t id() const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 110 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 111 | bool Start(uint32_t cid) override; |
| 112 | void Stop(uint32_t cid) override; |
| 113 | bool IsStarted(uint32_t* cid = nullptr) const override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 114 | bool OnStartDevice(Device* device) override; |
| 115 | void OnStopDevice(Device* device) override; |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 116 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 117 | const std::string& prev_ifname) override; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 118 | |
| 119 | private: |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 120 | uint32_t cid_; |
Garrick Evans | bbdf4b4 | 2020-03-05 12:59:06 +0900 | [diff] [blame] | 121 | const ShillClient* const shill_client_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 122 | DeviceManagerBase* dev_mgr_; |
| 123 | Datapath* datapath_; |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 124 | std::string tap_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 125 | |
| 126 | DISALLOW_COPY_AND_ASSIGN(VmImpl); |
| 127 | }; |
| 128 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 129 | // All pointers are required and cannot be null, and are owned by the caller. |
| 130 | ArcService(ShillClient* shill_client, |
| 131 | DeviceManagerBase* dev_mgr, |
| 132 | Datapath* datapath); |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 133 | ~ArcService(); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 134 | |
Garrick Evans | 015b0d6 | 2020-02-07 09:06:38 +0900 | [diff] [blame] | 135 | bool Start(uint32_t id); |
| 136 | void Stop(uint32_t id); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 137 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 138 | void OnDeviceAdded(Device* device); |
| 139 | void OnDeviceRemoved(Device* device); |
Garrick Evans | 1b1f67c | 2020-02-04 16:21:25 +0900 | [diff] [blame] | 140 | void OnDefaultInterfaceChanged(const std::string& new_ifname, |
| 141 | const std::string& prev_ifname); |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 142 | |
Garrick Evans | e94b6de | 2020-02-20 09:19:13 +0900 | [diff] [blame] | 143 | Device* ArcDevice() const; |
| 144 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 145 | private: |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 146 | void StartDevice(Device* device); |
| 147 | void StopDevice(Device* device); |
| 148 | |
Garrick Evans | 8ff0845 | 2019-11-25 09:24:26 +0900 | [diff] [blame] | 149 | // Returns true if the device should be processed by the service. |
| 150 | bool AllowDevice(Device* device) const; |
| 151 | |
Garrick Evans | 69b8587 | 2020-02-04 11:40:26 +0900 | [diff] [blame] | 152 | ShillClient* shill_client_; |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 153 | DeviceManagerBase* dev_mgr_; |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 154 | Datapath* datapath_; |
Garrick Evans | b4eb389 | 2019-11-13 12:07:07 +0900 | [diff] [blame] | 155 | std::unique_ptr<Impl> impl_; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 156 | |
| 157 | base::WeakPtrFactory<ArcService> weak_factory_{this}; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 158 | DISALLOW_COPY_AND_ASSIGN(ArcService); |
| 159 | }; |
| 160 | |
Garrick Evans | f29f5a3 | 2019-12-06 11:34:25 +0900 | [diff] [blame] | 161 | namespace test { |
| 162 | extern GuestMessage::GuestType guest; |
| 163 | } // namespace test |
| 164 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 165 | } // namespace arc_networkd |
| 166 | |
| 167 | #endif // ARC_NETWORK_ARC_SERVICE_H_ |