blob: 86eaa0ee9ad77a1142b633019f9f8f29a4ada2f0 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
vasilvv89713d02020-02-11 14:33:26 -080010#include "net/third_party/quiche/src/quic/core/quic_clock.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/core/quic_packets.h"
12#include "net/third_party/quiche/src/quic/core/quic_process_packet_interface.h"
wub1dc01cf2020-01-27 13:04:14 -080013#include "net/third_party/quiche/src/quic/core/quic_udp_socket.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_aligned.h"
wub1dc01cf2020-01-27 13:04:14 -080015#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017
18namespace quic {
19
QUICHE teama6ef0a62019-03-07 20:34:33 -050020// Read in larger batches to minimize recvmmsg overhead.
21const int kNumPacketsPerReadMmsgCall = 16;
QUICHE teama6ef0a62019-03-07 20:34:33 -050022
dschinazif25169a2019-10-23 08:12:18 -070023class QUIC_EXPORT_PRIVATE QuicPacketReader {
QUICHE teama6ef0a62019-03-07 20:34:33 -050024 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,
39 int port,
40 const QuicClock& clock,
41 ProcessPacketInterface* processor,
42 QuicPacketCount* packets_dropped);
43
44 private:
wub1dc01cf2020-01-27 13:04:14 -080045 // Return the self ip from |packet_info|.
46 // For dual stack sockets, |packet_info| may contain both a v4 and a v6 ip, in
47 // that case, |prefer_v6_ip| is used to determine which one is used as the
48 // return value. If neither v4 nor v6 ip exists, return an uninitialized ip.
49 static QuicIpAddress GetSelfIpFromPacketInfo(
50 const QuicUdpPacketInfo& packet_info,
51 bool prefer_v6_ip);
52
wub1dc01cf2020-01-27 13:04:14 -080053 struct QUIC_EXPORT_PRIVATE ReadBuffer {
54 QUIC_CACHELINE_ALIGNED char
wuba27a3c92020-01-28 08:43:21 -080055 control_buffer[kDefaultUdpPacketControlBufferSize]; // For ancillary
56 // data.
wub1dc01cf2020-01-27 13:04:14 -080057 QUIC_CACHELINE_ALIGNED char packet_buffer[kMaxIncomingPacketSize];
58 };
wub5dcf0282020-02-28 06:56:00 -080059
wub1dc01cf2020-01-27 13:04:14 -080060 QuicUdpSocketApi socket_api_;
61 std::vector<ReadBuffer> read_buffers_;
62 QuicUdpSocketApi::ReadPacketResults read_results_;
QUICHE teama6ef0a62019-03-07 20:34:33 -050063};
64
65} // namespace quic
66
67#endif // QUICHE_QUIC_CORE_QUIC_PACKET_READER_H_