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 | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 8 | #include <memory> |
Garrick Evans | 3915af3 | 2019-07-25 15:44:34 +0900 | [diff] [blame] | 9 | #include <string> |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 10 | |
| 11 | #include <base/memory/weak_ptr.h> |
| 12 | #include <shill/net/rtnl_handler.h> |
| 13 | #include <shill/net/rtnl_listener.h> |
| 14 | |
| 15 | #include "arc/network/datapath.h" |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 16 | #include "arc/network/guest_service.h" |
| 17 | |
| 18 | namespace arc_networkd { |
| 19 | |
| 20 | class ArcService : public GuestService { |
| 21 | public: |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 22 | class Context : public Device::Context { |
| 23 | public: |
| 24 | Context(); |
| 25 | ~Context() = default; |
| 26 | |
| 27 | // Tracks the lifetime of the ARC++ container. |
| 28 | void Start(); |
| 29 | void Stop(); |
| 30 | bool IsStarted() const; |
| 31 | |
| 32 | bool IsLinkUp() const override; |
| 33 | // Returns true if the internal state changed. |
| 34 | bool SetLinkUp(bool link_up); |
| 35 | |
| 36 | bool HasIPv6() const; |
| 37 | // Returns false if |routing_tid| is invalid. |
| 38 | bool SetHasIPv6(int routing_tid); |
Garrick Evans | dc3fc45 | 2019-09-06 14:15:04 +0900 | [diff] [blame] | 39 | // Resets the IPv6 attributes. |
| 40 | void ClearIPv6(); |
| 41 | |
Garrick Evans | 2c26310 | 2019-07-26 16:07:18 +0900 | [diff] [blame] | 42 | int RoutingTableID() const; |
| 43 | // Returns the current value and increments the counter. |
| 44 | int RoutingTableAttempts(); |
| 45 | |
| 46 | private: |
| 47 | // Indicates the device was started. |
| 48 | bool started_; |
| 49 | // Indicates Android has brought up the interface. |
| 50 | bool link_up_; |
| 51 | // The routing table ID found for the interface. |
| 52 | int routing_table_id_; |
| 53 | // The number of times table ID lookup was attempted. |
| 54 | int routing_table_attempts_; |
| 55 | }; |
| 56 | |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 57 | // |dev_mgr| and |datapath| cannot be null. |
Garrick Evans | 1f5a361 | 2019-11-08 12:59:03 +0900 | [diff] [blame] | 58 | // |is_legacy| is temporary and intended to be used for testing only. |
| 59 | ArcService(DeviceManagerBase* dev_mgr, |
| 60 | Datapath* datapath, |
| 61 | bool* is_legacy = nullptr); |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 62 | ~ArcService() = default; |
| 63 | |
| 64 | void OnStart() override; |
| 65 | void OnStop() override; |
| 66 | |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 67 | void OnDeviceAdded(Device* device) override; |
| 68 | void OnDeviceRemoved(Device* device) override; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 69 | void OnDefaultInterfaceChanged(const std::string& ifname) override; |
| 70 | |
| 71 | // Handles RT netlink messages in the container net namespace and if it |
| 72 | // determines the link status has changed, toggles the device services |
| 73 | // accordingly. |
| 74 | void LinkMsgHandler(const shill::RTNLMessage& msg); |
| 75 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 76 | void SetupIPv6(Device* device); |
| 77 | void TeardownIPv6(Device* device); |
| 78 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 79 | // Do not use. Only for testing. |
| 80 | void SetPIDForTestingOnly(); |
Garrick Evans | ba57574 | 2019-07-17 15:48:08 +0900 | [diff] [blame] | 81 | |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 82 | private: |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 83 | void StartDevice(Device* device); |
| 84 | void StopDevice(Device* device); |
| 85 | |
Garrick Evans | cb791e7 | 2019-11-11 15:44:34 +0900 | [diff] [blame^] | 86 | // ARC++ specific functiona. |
| 87 | |
| 88 | bool OnStartContainer(); |
| 89 | void OnStopContainer(); |
| 90 | bool OnStartContainerDevice(Device* device); |
| 91 | void OnStopContainerDevice(Device* device); |
| 92 | void OnContainerDefaultInterfaceChanged(const std::string& ifname); |
| 93 | |
Taoyu Li | 179dcc6 | 2019-10-17 11:21:08 +0900 | [diff] [blame] | 94 | Datapath* datapath_; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 95 | |
| 96 | pid_t pid_; |
| 97 | std::unique_ptr<shill::RTNLHandler> rtnl_handler_; |
| 98 | std::unique_ptr<shill::RTNLListener> link_listener_; |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 99 | |
| 100 | base::WeakPtrFactory<ArcService> weak_factory_{this}; |
Garrick Evans | 5d55f5e | 2019-07-17 15:28:10 +0900 | [diff] [blame] | 101 | DISALLOW_COPY_AND_ASSIGN(ArcService); |
| 102 | }; |
| 103 | |
| 104 | } // namespace arc_networkd |
| 105 | |
| 106 | #endif // ARC_NETWORK_ARC_SERVICE_H_ |