blob: 2662a7494c1ac6c1c6640daf93891ecc21ec5631 [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:
74 ContainerImpl(DeviceManagerBase* dev_mgr,
75 Datapath* datapath,
76 GuestMessage::GuestType guest);
77 ~ContainerImpl() = default;
78
Garrick Evansb4eb3892019-11-13 12:07:07 +090079 GuestMessage::GuestType guest() const override;
Garrick Evans015b0d62020-02-07 09:06:38 +090080 uint32_t id() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090081
Garrick Evans015b0d62020-02-07 09:06:38 +090082 bool Start(uint32_t pid) override;
83 void Stop(uint32_t pid) override;
84 bool IsStarted(uint32_t* pid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +090085 bool OnStartDevice(Device* device) override;
86 void OnStopDevice(Device* device) override;
Garrick Evans1b1f67c2020-02-04 16:21:25 +090087 void OnDefaultInterfaceChanged(const std::string& new_ifname,
88 const std::string& prev_ifname) override;
Garrick Evansd90a3822019-11-12 17:53:08 +090089
90 private:
Garrick Evans015b0d62020-02-07 09:06:38 +090091 uint32_t pid_;
Garrick Evansd90a3822019-11-12 17:53:08 +090092 DeviceManagerBase* dev_mgr_;
93 Datapath* datapath_;
94 GuestMessage::GuestType guest_;
95
Garrick Evansd90a3822019-11-12 17:53:08 +090096 base::WeakPtrFactory<ContainerImpl> weak_factory_{this};
97 DISALLOW_COPY_AND_ASSIGN(ContainerImpl);
98 };
99
Garrick Evansb4eb3892019-11-13 12:07:07 +0900100 // Encapsulates all ARC VM-specific logic.
101 class VmImpl : public Impl {
102 public:
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900103 VmImpl(ShillClient* shill_client,
104 DeviceManagerBase* dev_mgr,
105 Datapath* datapath);
Garrick Evansb4eb3892019-11-13 12:07:07 +0900106 ~VmImpl() = default;
107
108 GuestMessage::GuestType guest() const override;
Garrick Evans015b0d62020-02-07 09:06:38 +0900109 uint32_t id() const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900110
Garrick Evans015b0d62020-02-07 09:06:38 +0900111 bool Start(uint32_t cid) override;
112 void Stop(uint32_t cid) override;
113 bool IsStarted(uint32_t* cid = nullptr) const override;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900114 bool OnStartDevice(Device* device) override;
115 void OnStopDevice(Device* device) override;
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900116 void OnDefaultInterfaceChanged(const std::string& new_ifname,
117 const std::string& prev_ifname) override;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900118
119 private:
Garrick Evans015b0d62020-02-07 09:06:38 +0900120 uint32_t cid_;
Garrick Evansbbdf4b42020-03-05 12:59:06 +0900121 const ShillClient* const shill_client_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900122 DeviceManagerBase* dev_mgr_;
123 Datapath* datapath_;
Garrick Evanse94b6de2020-02-20 09:19:13 +0900124 std::string tap_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900125
126 DISALLOW_COPY_AND_ASSIGN(VmImpl);
127 };
128
Garrick Evans69b85872020-02-04 11:40:26 +0900129 // 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 Evansf29f5a32019-12-06 11:34:25 +0900133 ~ArcService();
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900134
Garrick Evans015b0d62020-02-07 09:06:38 +0900135 bool Start(uint32_t id);
136 void Stop(uint32_t id);
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900137
Garrick Evansf29f5a32019-12-06 11:34:25 +0900138 void OnDeviceAdded(Device* device);
139 void OnDeviceRemoved(Device* device);
Garrick Evans1b1f67c2020-02-04 16:21:25 +0900140 void OnDefaultInterfaceChanged(const std::string& new_ifname,
141 const std::string& prev_ifname);
Garrick Evans54861622019-07-19 09:05:09 +0900142
Garrick Evanse94b6de2020-02-20 09:19:13 +0900143 Device* ArcDevice() const;
144
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900145 private:
Garrick Evans54861622019-07-19 09:05:09 +0900146 void StartDevice(Device* device);
147 void StopDevice(Device* device);
148
Garrick Evans8ff08452019-11-25 09:24:26 +0900149 // Returns true if the device should be processed by the service.
150 bool AllowDevice(Device* device) const;
151
Garrick Evans69b85872020-02-04 11:40:26 +0900152 ShillClient* shill_client_;
Garrick Evansf29f5a32019-12-06 11:34:25 +0900153 DeviceManagerBase* dev_mgr_;
Taoyu Li179dcc62019-10-17 11:21:08 +0900154 Datapath* datapath_;
Garrick Evansb4eb3892019-11-13 12:07:07 +0900155 std::unique_ptr<Impl> impl_;
Garrick Evans54861622019-07-19 09:05:09 +0900156
157 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900158 DISALLOW_COPY_AND_ASSIGN(ArcService);
159};
160
Garrick Evansf29f5a32019-12-06 11:34:25 +0900161namespace test {
162extern GuestMessage::GuestType guest;
163} // namespace test
164
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900165} // namespace arc_networkd
166
167#endif // ARC_NETWORK_ARC_SERVICE_H_