blob: 3a9a5abaacbbf7bbb6f8c18d3cb3dee9bab346e0 [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>
12#include <shill/net/rtnl_handler.h>
13#include <shill/net/rtnl_listener.h>
14
15#include "arc/network/datapath.h"
Garrick Evans5d55f5e2019-07-17 15:28:10 +090016#include "arc/network/guest_service.h"
17
18namespace arc_networkd {
19
20class ArcService : public GuestService {
21 public:
Garrick Evans2c263102019-07-26 16:07:18 +090022 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 Evansdc3fc452019-09-06 14:15:04 +090039 // Resets the IPv6 attributes.
40 void ClearIPv6();
41
Garrick Evans2c263102019-07-26 16:07:18 +090042 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 Li179dcc62019-10-17 11:21:08 +090057 // |dev_mgr| and |datapath| cannot be null.
Garrick Evans1f5a3612019-11-08 12:59:03 +090058 // |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 Evans5d55f5e2019-07-17 15:28:10 +090062 ~ArcService() = default;
63
64 void OnStart() override;
65 void OnStop() override;
66
Garrick Evansba575742019-07-17 15:48:08 +090067 void OnDeviceAdded(Device* device) override;
68 void OnDeviceRemoved(Device* device) override;
Garrick Evans54861622019-07-19 09:05:09 +090069 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 Evans260ff302019-07-25 11:22:50 +090076 void SetupIPv6(Device* device);
77 void TeardownIPv6(Device* device);
78
Garrick Evans54861622019-07-19 09:05:09 +090079 // Do not use. Only for testing.
80 void SetPIDForTestingOnly();
Garrick Evansba575742019-07-17 15:48:08 +090081
Garrick Evans5d55f5e2019-07-17 15:28:10 +090082 private:
Garrick Evans54861622019-07-19 09:05:09 +090083 void StartDevice(Device* device);
84 void StopDevice(Device* device);
85
Garrick Evanscb791e72019-11-11 15:44:34 +090086 // 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 Li179dcc62019-10-17 11:21:08 +090094 Datapath* datapath_;
Garrick Evans54861622019-07-19 09:05:09 +090095
96 pid_t pid_;
97 std::unique_ptr<shill::RTNLHandler> rtnl_handler_;
98 std::unique_ptr<shill::RTNLListener> link_listener_;
Garrick Evans54861622019-07-19 09:05:09 +090099
100 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +0900101 DISALLOW_COPY_AND_ASSIGN(ArcService);
102};
103
104} // namespace arc_networkd
105
106#endif // ARC_NETWORK_ARC_SERVICE_H_