QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 <netinet/in.h> |
| 11 | // Include here to guarantee this header gets included (for MSG_WAITFORONE) |
| 12 | // regardless of how the below transitive header include set may change. |
| 13 | #include <sys/socket.h> |
| 14 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_process_packet_interface.h" |
| 17 | #include "net/third_party/quiche/src/quic/platform/api/quic_clock.h" |
| 18 | #include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h" |
| 19 | #include "net/quic/platform/impl/quic_socket_utils.h" |
| 20 | |
| 21 | namespace quic { |
| 22 | |
| 23 | #if MMSG_MORE |
| 24 | // Read in larger batches to minimize recvmmsg overhead. |
| 25 | const int kNumPacketsPerReadMmsgCall = 16; |
| 26 | #endif |
| 27 | |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 28 | class QUIC_EXPORT_PRIVATE QuicPacketReader { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | public: |
| 30 | QuicPacketReader(); |
| 31 | QuicPacketReader(const QuicPacketReader&) = delete; |
| 32 | QuicPacketReader& operator=(const QuicPacketReader&) = delete; |
| 33 | |
| 34 | virtual ~QuicPacketReader(); |
| 35 | |
| 36 | // Reads a number of packets from the given fd, and then passes them off to |
| 37 | // the PacketProcessInterface. Returns true if there may be additional |
| 38 | // packets available on the socket. |
| 39 | // Populates |packets_dropped| if it is non-null and the socket is configured |
| 40 | // to track dropped packets and some packets are read. |
| 41 | // If the socket has timestamping enabled, the per packet timestamps will be |
| 42 | // passed to the processor. Otherwise, |clock| will be used. |
| 43 | virtual bool ReadAndDispatchPackets(int fd, |
| 44 | int port, |
| 45 | const QuicClock& clock, |
| 46 | ProcessPacketInterface* processor, |
| 47 | QuicPacketCount* packets_dropped); |
| 48 | |
| 49 | private: |
| 50 | // Initialize the internal state of the reader. |
| 51 | void Initialize(); |
| 52 | |
| 53 | // Reads and dispatches many packets using recvmmsg. |
| 54 | bool ReadAndDispatchManyPackets(int fd, |
| 55 | int port, |
| 56 | const QuicClock& clock, |
| 57 | ProcessPacketInterface* processor, |
| 58 | QuicPacketCount* packets_dropped); |
| 59 | |
| 60 | // Reads and dispatches a single packet using recvmsg. |
| 61 | static bool ReadAndDispatchSinglePacket(int fd, |
| 62 | int port, |
| 63 | const QuicClock& clock, |
| 64 | ProcessPacketInterface* processor, |
| 65 | QuicPacketCount* packets_dropped); |
| 66 | |
| 67 | #if MMSG_MORE |
| 68 | // Storage only used when recvmmsg is available. |
| 69 | // TODO(danzh): change it to be a pointer to avoid the allocation on the stack |
| 70 | // from exceeding maximum allowed frame size. |
| 71 | // packets_ and mmsg_hdr_ are used to supply cbuf and buf to the recvmmsg |
| 72 | // call. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 73 | struct QUIC_EXPORT_PRIVATE PacketData { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | iovec iov; |
| 75 | // raw_address is used for address information provided by the recvmmsg |
| 76 | // call on the packets. |
| 77 | struct sockaddr_storage raw_address; |
| 78 | // cbuf is used for ancillary data from the kernel on recvmmsg. |
| 79 | char cbuf[kCmsgSpaceForReadPacket]; |
| 80 | // buf is used for the data read from the kernel on recvmmsg. |
| 81 | char buf[kMaxV4PacketSize]; |
| 82 | }; |
| 83 | PacketData packets_[kNumPacketsPerReadMmsgCall]; |
| 84 | mmsghdr mmsg_hdr_[kNumPacketsPerReadMmsgCall]; |
| 85 | #endif |
| 86 | }; |
| 87 | |
| 88 | } // namespace quic |
| 89 | |
| 90 | #endif // QUICHE_QUIC_CORE_QUIC_PACKET_READER_H_ |