xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/qbone/bonnet/tun_device_packet_exchanger.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2019 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_PACKET_EXCHANGER_H_
6 #define QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_PACKET_EXCHANGER_H_
7 
8 #include <linux/if_ether.h>
9 
10 #include "quiche/quic/core/quic_packets.h"
11 #include "quiche/quic/qbone/platform/kernel_interface.h"
12 #include "quiche/quic/qbone/platform/netlink_interface.h"
13 #include "quiche/quic/qbone/qbone_client_interface.h"
14 #include "quiche/quic/qbone/qbone_packet_exchanger.h"
15 
16 namespace quic {
17 
18 class TunDevicePacketExchanger : public QbonePacketExchanger {
19  public:
20   class StatsInterface {
21    public:
22     StatsInterface() = default;
23 
24     StatsInterface(const StatsInterface&) = delete;
25     StatsInterface& operator=(const StatsInterface&) = delete;
26 
27     StatsInterface(StatsInterface&&) = delete;
28     StatsInterface& operator=(StatsInterface&&) = delete;
29 
30     virtual ~StatsInterface() = default;
31 
32     virtual void OnPacketRead(size_t count) = 0;
33     virtual void OnPacketWritten(size_t count) = 0;
34     virtual void OnReadError(std::string* error) = 0;
35     virtual void OnWriteError(std::string* error) = 0;
36 
37     ABSL_MUST_USE_RESULT virtual int64_t PacketsRead() const = 0;
38     ABSL_MUST_USE_RESULT virtual int64_t PacketsWritten() const = 0;
39   };
40 
41   // |mtu| is the mtu of the TUN device.
42   // |kernel| is not owned but should out live objects of this class.
43   // |visitor| is not owned but should out live objects of this class.
44   // |max_pending_packets| controls the number of packets to be queued should
45   // the TUN device become blocked.
46   // |stats| is notified about packet read/write statistics. It is not owned,
47   // but should outlive objects of this class.
48   TunDevicePacketExchanger(size_t mtu, KernelInterface* kernel,
49                            NetlinkInterface* netlink,
50                            QbonePacketExchanger::Visitor* visitor,
51                            size_t max_pending_packets, bool is_tap,
52                            StatsInterface* stats, absl::string_view ifname);
53 
54   void set_file_descriptor(int fd);
55 
56   ABSL_MUST_USE_RESULT const StatsInterface* stats_interface() const;
57 
58  private:
59   // From QbonePacketExchanger.
60   std::unique_ptr<QuicData> ReadPacket(bool* blocked,
61                                        std::string* error) override;
62 
63   // From QbonePacketExchanger.
64   bool WritePacket(const char* packet, size_t size, bool* blocked,
65                    std::string* error) override;
66 
67   std::unique_ptr<QuicData> ApplyL2Headers(const QuicData& l3_packet);
68 
69   std::unique_ptr<QuicData> ConsumeL2Headers(const QuicData& l2_packet);
70 
71   int fd_ = -1;
72   size_t mtu_;
73   KernelInterface* kernel_;
74   NetlinkInterface* netlink_;
75   const std::string ifname_;
76 
77   const bool is_tap_;
78   uint8_t tap_mac_[ETH_ALEN]{};
79   bool mac_initialized_ = false;
80 
81   StatsInterface* stats_;
82 };
83 
84 }  // namespace quic
85 
86 #endif  // QUICHE_QUIC_QBONE_BONNET_TUN_DEVICE_PACKET_EXCHANGER_H_
87