blob: 90383bf5b14334ef887e0d236f779c89649fa6de [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
Garrick Evans5d55f5e2019-07-17 15:28:10 +090057 // |dev_mgr| cannot be null.
Garrick Evans54861622019-07-19 09:05:09 +090058 ArcService(DeviceManagerBase* dev_mgr,
59 bool is_legacy,
60 std::unique_ptr<Datapath> datapath = nullptr);
Garrick Evans5d55f5e2019-07-17 15:28:10 +090061 ~ArcService() = default;
62
63 void OnStart() override;
64 void OnStop() override;
65
Garrick Evansba575742019-07-17 15:48:08 +090066 void OnDeviceAdded(Device* device) override;
67 void OnDeviceRemoved(Device* device) override;
Garrick Evans54861622019-07-19 09:05:09 +090068 void OnDefaultInterfaceChanged(const std::string& ifname) override;
69
70 // Handles RT netlink messages in the container net namespace and if it
71 // determines the link status has changed, toggles the device services
72 // accordingly.
73 void LinkMsgHandler(const shill::RTNLMessage& msg);
74
Garrick Evans260ff302019-07-25 11:22:50 +090075 void SetupIPv6(Device* device);
76 void TeardownIPv6(Device* device);
77
Garrick Evans54861622019-07-19 09:05:09 +090078 // Do not use. Only for testing.
79 void SetPIDForTestingOnly();
Garrick Evansba575742019-07-17 15:48:08 +090080
Garrick Evans5d55f5e2019-07-17 15:28:10 +090081 private:
Garrick Evans54861622019-07-19 09:05:09 +090082 void StartDevice(Device* device);
83 void StopDevice(Device* device);
84
85 bool ShouldProcessDevice(const Device& device) const;
86
Garrick Evans260ff302019-07-25 11:22:50 +090087 std::unique_ptr<MinijailedProcessRunner> runner_;
Garrick Evans54861622019-07-19 09:05:09 +090088 std::unique_ptr<Datapath> datapath_;
89
90 pid_t pid_;
91 std::unique_ptr<shill::RTNLHandler> rtnl_handler_;
92 std::unique_ptr<shill::RTNLListener> link_listener_;
Garrick Evans54861622019-07-19 09:05:09 +090093
94 base::WeakPtrFactory<ArcService> weak_factory_{this};
Garrick Evans5d55f5e2019-07-17 15:28:10 +090095 DISALLOW_COPY_AND_ASSIGN(ArcService);
96};
97
98} // namespace arc_networkd
99
100#endif // ARC_NETWORK_ARC_SERVICE_H_