blob: 70a428ad541dc165140beebecb77c0477bf75676 [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
5#ifndef ARC_NETWORK_ARC_SERVICE_H_
6#define ARC_NETWORK_ARC_SERVICE_H_
7
Garrick Evans1b1f67c2020-02-04 16:21:25 +09008#include <map>
Garrick Evans3915af32019-07-25 15:44:34 +09009#include <memory>
Garrick Evans3915af32019-07-25 15:44:34 +090010#include <string>
Garrick Evans54861622019-07-19 09:05:09 +090011
12#include <base/memory/weak_ptr.h>
Garrick Evans54861622019-07-19 09:05:09 +090013
14#include "arc/network/datapath.h"
Garrick Evansf29f5a32019-12-06 11:34:25 +090015#include "arc/network/device.h"
16#include "arc/network/device_manager.h"
17#include "arc/network/ipc.pb.h"
Garrick Evans69b85872020-02-04 11:40:26 +090018#include "arc/network/shill_client.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090019
20namespace arc_networkd {
21
Garrick Evansf29f5a32019-12-06 11:34:25 +090022class ArcService {
Garrick Evans5d55f5e2019-07-17 15:28:10 +090023 public:
Garrick Evans2c263102019-07-26 16:07:18 +090024 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 Evansb4eb3892019-11-13 12:07:07 +090034 // For ARCVM only.
35 const std::string& TAP() const;
36 void SetTAP(const std::string& tap);
37
Garrick Evans2c263102019-07-26 16:07:18 +090038 private:
39 // Indicates the device was started.
40 bool started_;
Garrick Evansb4eb3892019-11-13 12:07:07 +090041 // 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 Evans015b0d62020-02-07 09:06:38 +090050 virtual uint32_t id() const = 0;
Garrick Evansb4eb3892019-11-13 12:07:07 +090051
Garrick Evans015b0d62020-02-07 09:06:38 +090052 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 Evansb4eb3892019-11-13 12:07:07 +090055 virtual bool OnStartDevice(Device* device) = 0;
56 virtual void OnStopDevice(Device* device) = 0;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090057 virtual void OnDefaultInterfaceChanged(const std::string& new_ifname,
58 const std::string& prev_ifname) = 0;
Garrick Evansb4eb3892019-11-13 12:07:07 +090059
Garrick Evanse94b6de2020-02-20 09:19:13 +090060 // Returns the ARC management interface.
61 Device* ArcDevice() const { return arc_device_.get(); }
62
Garrick Evansb4eb3892019-11-13 12:07:07 +090063 protected:
64 Impl() = default;
Garrick Evanse94b6de2020-02-20 09:19:13 +090065
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 Evans2c263102019-07-26 16:07:18 +090069 };
70
Garrick Evansd90a3822019-11-12 17:53:08 +090071 // Encapsulates all ARC++ container-specific logic.
Garrick Evansb4eb3892019-11-13 12:07:07 +090072 class ContainerImpl : public Impl {
Garrick Evansd90a3822019-11-12 17:53:08 +090073 public:
Garrick Evans2e5c9ab2020-03-05 14:33:58 +090074 ContainerImpl(Datapath* datapath,
75 AddressManager* addr_mgr,
76 TrafficForwarder* forwarder,
Garrick Evansd90a3822019-11-12 17:53:08 +090077 GuestMessage::GuestType guest);
78 ~ContainerImpl() = default;
79
Garrick Evansb4eb3892019-11-13 12:07:07 +090080 GuestMessage::GuestType guest() const override;
Garrick Evans015b0d62020-02-07 09:06:38 +090081 uint32_t id() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090082
Garrick Evans015b0d62020-02-07 09:06:38 +090083 bool Start(uint32_t pid) override;
84 void Stop(uint32_t pid) override;
85 bool IsStarted(uint32_t* pid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090086 bool OnStartDevice(Device* device) override;
87 void OnStopDevice(Device* device) override;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090088 void OnDefaultInterfaceChanged(const std::string& new_ifname,
89 const std::string& prev_ifname) override;
Garrick Evansd90a3822019-11-12 17:53:08 +090090
91 private:
Garrick Evans015b0d62020-02-07 09:06:38 +090092 uint32_t pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +090093 Datapath* datapath_;
Garrick Evans2e5c9ab2020-03-05 14:33:58 +090094 AddressManager* addr_mgr_;
95 TrafficForwarder* forwarder_;
Garrick Evansd90a3822019-11-12 17:53:08 +090096 GuestMessage::GuestType guest_;
97
Garrick Evansd90a3822019-11-12 17:53:08 +090098 base::WeakPtrFactory<ContainerImpl> weak_factory_{this};
99 DISALLOW_COPY_AND_ASSIGN(ContainerImpl);
100 };
101
Garrick Evansb4eb3892019-11-13 12:07:07 +0900102 // Encapsulates all ARC VM-specific logic.
103 class VmImpl : public Impl {
104 public:
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900105 VmImpl(ShillClient* shill_client,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900106 Datapath* datapath,
107 AddressManager* addr_mgr,
108 TrafficForwarder* forwarder);
Garrick Evansb4eb3892019-11-13 12:07:07 +0900109 ~VmImpl() = default;
110
111 GuestMessage::GuestType guest() const override;
Garrick Evans015b0d62020-02-07 09:06:38 +0900112 uint32_t id() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900113
Garrick Evans015b0d62020-02-07 09:06:38 +0900114 bool Start(uint32_t cid) override;
115 void Stop(uint32_t cid) override;
116 bool IsStarted(uint32_t* cid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900117 bool OnStartDevice(Device* device) override;
118 void OnStopDevice(Device* device) override;
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900119 void OnDefaultInterfaceChanged(const std::string& new_ifname,
120 const std::string& prev_ifname) override;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900121
122 private:
Garrick Evans015b0d62020-02-07 09:06:38 +0900123 uint32_t cid_;
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900124 const ShillClient* const shill_client_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900125 Datapath* datapath_;
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900126 AddressManager* addr_mgr_;
127 TrafficForwarder* forwarder_;
Garrick Evanse94b6de2020-02-20 09:19:13 +0900128 std::string tap_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900129
130 DISALLOW_COPY_AND_ASSIGN(VmImpl);
131 };
132
Garrick Evans69b85872020-02-04 11:40:26 +0900133 // All pointers are required and cannot be null, and are owned by the caller.
134 ArcService(ShillClient* shill_client,
135 DeviceManagerBase* dev_mgr,
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900136 Datapath* datapath,
137 AddressManager* addr_mgr,
138 TrafficForwarder* forwarder);
Garrick Evansf29f5a32019-12-06 11:34:25 +0900139 ~ArcService();
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900140
Garrick Evans015b0d62020-02-07 09:06:38 +0900141 bool Start(uint32_t id);
142 void Stop(uint32_t id);
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900143
Garrick Evansf29f5a32019-12-06 11:34:25 +0900144 void OnDeviceAdded(Device* device);
145 void OnDeviceRemoved(Device* device);
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900146 void OnDefaultInterfaceChanged(const std::string& new_ifname,
147 const std::string& prev_ifname);
Garrick Evans54861622019-07-19 09:05:09 +0900148
Garrick Evanse94b6de2020-02-20 09:19:13 +0900149 Device* ArcDevice() const;
150
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900151 private:
Garrick Evans54861622019-07-19 09:05:09 +0900152 void StartDevice(Device* device);
153 void StopDevice(Device* device);
154
Garrick Evans8ff08452019-11-25 09:24:26 +0900155 // Returns true if the device should be processed by the service.
156 bool AllowDevice(Device* device) const;
157
Garrick Evans69b85872020-02-04 11:40:26 +0900158 ShillClient* shill_client_;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900159 DeviceManagerBase* dev_mgr_;
Taoyu Li179dcc62019-10-17 11:21:08 +0900160 Datapath* datapath_;
Garrick Evans2e5c9ab2020-03-05 14:33:58 +0900161 AddressManager* addr_mgr_;
162 TrafficForwarder* forwarder_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900163 std::unique_ptr<Impl> impl_;
Garrick Evans54861622019-07-19 09:05:09 +0900164
165 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900166 DISALLOW_COPY_AND_ASSIGN(ArcService);
167};
168
Garrick Evansf29f5a32019-12-06 11:34:25 +0900169namespace test {
170extern GuestMessage::GuestType guest;
171} // namespace test
172
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900173} // namespace arc_networkd
174
175#endif // ARC_NETWORK_ARC_SERVICE_H_