xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/qbone/bonnet/tun_device_controller.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2020 The Chromium 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 QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_CONTROLLER_H_
6 #define QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_CONTROLLER_H_
7 
8 #include "quiche/quic/platform/api/quic_ip_address.h"
9 #include "quiche/quic/qbone/bonnet/tun_device.h"
10 #include "quiche/quic/qbone/platform/netlink_interface.h"
11 #include "quiche/quic/qbone/qbone_control.pb.h"
12 #include "quiche/quic/qbone/qbone_control_stream.h"
13 #include "quiche/common/quiche_callbacks.h"
14 
15 namespace quic {
16 
17 // TunDeviceController consumes control stream messages from a Qbone server
18 // and applies the given updates to the TUN device.
19 class TunDeviceController {
20  public:
21   // |ifname| is the interface name of the TUN device to be managed. This does
22   // not take ownership of |netlink|.
TunDeviceController(std::string ifname,bool setup_tun,NetlinkInterface * netlink)23   TunDeviceController(std::string ifname, bool setup_tun,
24                       NetlinkInterface* netlink)
25       : ifname_(std::move(ifname)), setup_tun_(setup_tun), netlink_(netlink) {}
26 
27   TunDeviceController(const TunDeviceController&) = delete;
28   TunDeviceController& operator=(const TunDeviceController&) = delete;
29 
30   TunDeviceController(TunDeviceController&&) = delete;
31   TunDeviceController& operator=(TunDeviceController&&) = delete;
32 
33   virtual ~TunDeviceController() = default;
34 
35   // Updates the local address of the TUN device to be the first address in the
36   // given |response.ip_range()|.
37   virtual bool UpdateAddress(const IpRange& desired_range);
38 
39   // Updates the set of routes that the TUN device will provide. All current
40   // routes for the tunnel that do not exist in the |response| will be removed.
41   virtual bool UpdateRoutes(const IpRange& desired_range,
42                             const std::vector<IpRange>& desired_routes);
43 
44   // Same as UpdateRoutes, but will wait and retry up to the number of times
45   // given by |retries| before giving up. This is an unpleasant workaround to
46   // deal with older kernels that aren't always able to set a route with a
47   // source address immediately after adding the address to the interface.
48   //
49   // TODO(b/179430548): Remove this once we've root-caused the underlying issue.
50   virtual bool UpdateRoutesWithRetries(
51       const IpRange& desired_range, const std::vector<IpRange>& desired_routes,
52       int retries);
53 
54   virtual void RegisterAddressUpdateCallback(
55       quiche::MultiUseCallback<void(QuicIpAddress)> cb);
56 
57   virtual QuicIpAddress current_address();
58 
59  private:
60   // Update the IP Rules, this should only be used by UpdateRoutes.
61   bool UpdateRules(IpRange desired_range);
62 
63   const std::string ifname_;
64   const bool setup_tun_;
65 
66   NetlinkInterface* netlink_;
67 
68   QuicIpAddress current_address_;
69 
70   std::vector<quiche::MultiUseCallback<void(QuicIpAddress)>>
71       address_update_cbs_;
72 };
73 
74 }  // namespace quic
75 
76 #endif  // QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_CONTROLLER_H_
77