xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_packet_reader.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 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 // A class to read incoming QUIC packets from the UDP socket.
6 
7 #ifndef QUICHE_QUIC_CORE_QUIC_PACKET_READER_H_
8 #define QUICHE_QUIC_CORE_QUIC_PACKET_READER_H_
9 
10 #include "absl/base/optimization.h"
11 #include "quiche/quic/core/quic_clock.h"
12 #include "quiche/quic/core/quic_packets.h"
13 #include "quiche/quic/core/quic_process_packet_interface.h"
14 #include "quiche/quic/core/quic_udp_socket.h"
15 #include "quiche/quic/platform/api/quic_flags.h"
16 #include "quiche/quic/platform/api/quic_socket_address.h"
17 
18 namespace quic {
19 
20 // Read in larger batches to minimize recvmmsg overhead.
21 inline constexpr int kNumPacketsPerReadMmsgCall = 16;
22 
23 class QUICHE_EXPORT QuicPacketReader {
24  public:
25   QuicPacketReader();
26   QuicPacketReader(const QuicPacketReader&) = delete;
27   QuicPacketReader& operator=(const QuicPacketReader&) = delete;
28 
29   virtual ~QuicPacketReader();
30 
31   // Reads a number of packets from the given fd, and then passes them off to
32   // the PacketProcessInterface.  Returns true if there may be additional
33   // packets available on the socket.
34   // Populates |packets_dropped| if it is non-null and the socket is configured
35   // to track dropped packets and some packets are read.
36   // If the socket has timestamping enabled, the per packet timestamps will be
37   // passed to the processor. Otherwise, |clock| will be used.
38   virtual bool ReadAndDispatchPackets(int fd, int port, const QuicClock& clock,
39                                       ProcessPacketInterface* processor,
40                                       QuicPacketCount* packets_dropped);
41 
42  private:
43   // Return the self ip from |packet_info|.
44   // For dual stack sockets, |packet_info| may contain both a v4 and a v6 ip, in
45   // that case, |prefer_v6_ip| is used to determine which one is used as the
46   // return value. If neither v4 nor v6 ip exists, return an uninitialized ip.
47   static QuicIpAddress GetSelfIpFromPacketInfo(
48       const QuicUdpPacketInfo& packet_info, bool prefer_v6_ip);
49 
50   struct QUICHE_EXPORT ReadBuffer {
51     ABSL_CACHELINE_ALIGNED char
52         control_buffer[kDefaultUdpPacketControlBufferSize];  // For ancillary
53                                                              // data.
54     ABSL_CACHELINE_ALIGNED char packet_buffer[kMaxIncomingPacketSize];
55   };
56 
57   QuicUdpSocketApi socket_api_;
58   std::vector<ReadBuffer> read_buffers_;
59   QuicUdpSocketApi::ReadPacketResults read_results_;
60 };
61 
62 }  // namespace quic
63 
64 #endif  // QUICHE_QUIC_CORE_QUIC_PACKET_READER_H_
65