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