blob: 2a63af25057067b90cb8c0ad4409562743e220de [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 Evans3915af32019-07-25 15:44:34 +09008#include <memory>
Garrick Evans3915af32019-07-25 15:44:34 +09009#include <string>
Garrick Evans54861622019-07-19 09:05:09 +090010
11#include <base/memory/weak_ptr.h>
Garrick Evansd90a3822019-11-12 17:53:08 +090012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Garrick Evans54861622019-07-19 09:05:09 +090013#include <shill/net/rtnl_handler.h>
14#include <shill/net/rtnl_listener.h>
15
16#include "arc/network/datapath.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090017#include "arc/network/guest_service.h"
18
19namespace arc_networkd {
20
21class ArcService : public GuestService {
22 public:
Garrick Evans2c263102019-07-26 16:07:18 +090023 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 Evansdc3fc452019-09-06 14:15:04 +090040 // Resets the IPv6 attributes.
41 void ClearIPv6();
42
Garrick Evans2c263102019-07-26 16:07:18 +090043 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 Evansd90a3822019-11-12 17:53:08 +090058 // 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 Li179dcc62019-10-17 11:21:08 +090098 // |dev_mgr| and |datapath| cannot be null.
Garrick Evans1f5a3612019-11-08 12:59:03 +090099 // |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 Evans5d55f5e2019-07-17 15:28:10 +0900103 ~ArcService() = default;
104
105 void OnStart() override;
106 void OnStop() override;
107
Garrick Evansba575742019-07-17 15:48:08 +0900108 void OnDeviceAdded(Device* device) override;
109 void OnDeviceRemoved(Device* device) override;
Garrick Evans54861622019-07-19 09:05:09 +0900110 void OnDefaultInterfaceChanged(const std::string& ifname) override;
111
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900112 private:
Garrick Evans54861622019-07-19 09:05:09 +0900113 void StartDevice(Device* device);
114 void StopDevice(Device* device);
115
Taoyu Li179dcc62019-10-17 11:21:08 +0900116 Datapath* datapath_;
Garrick Evansd90a3822019-11-12 17:53:08 +0900117 std::unique_ptr<ContainerImpl> impl_;
Garrick Evans54861622019-07-19 09:05:09 +0900118
Garrick Evansd90a3822019-11-12 17:53:08 +0900119 FRIEND_TEST(ArcServiceTest, VerifyOnDeviceAddedDatapathForLegacyAndroid);
120 FRIEND_TEST(ArcServiceTest, VerifyOnDeviceAddedDatapathForAndroid);
121 FRIEND_TEST(ArcServiceTest, VerifyOnDeviceAddedDatapath);
Garrick Evans54861622019-07-19 09:05:09 +0900122
123 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900124 DISALLOW_COPY_AND_ASSIGN(ArcService);
125};
126
127} // namespace arc_networkd
128
129#endif // ARC_NETWORK_ARC_SERVICE_H_