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 #include "quiche/quic/core/quic_packet_reader.h"
6
7 #include "absl/base/macros.h"
8 #include "quiche/quic/core/quic_packets.h"
9 #include "quiche/quic/core/quic_process_packet_interface.h"
10 #include "quiche/quic/core/quic_udp_socket.h"
11 #include "quiche/quic/core/quic_utils.h"
12 #include "quiche/quic/platform/api/quic_bug_tracker.h"
13 #include "quiche/quic/platform/api/quic_flag_utils.h"
14 #include "quiche/quic/platform/api/quic_flags.h"
15 #include "quiche/quic/platform/api/quic_ip_address.h"
16 #include "quiche/quic/platform/api/quic_logging.h"
17 #include "quiche/quic/platform/api/quic_server_stats.h"
18 #include "quiche/quic/platform/api/quic_socket_address.h"
19
20 namespace quic {
21
QuicPacketReader()22 QuicPacketReader::QuicPacketReader()
23 : read_buffers_(kNumPacketsPerReadMmsgCall),
24 read_results_(kNumPacketsPerReadMmsgCall) {
25 QUICHE_DCHECK_EQ(read_buffers_.size(), read_results_.size());
26 for (size_t i = 0; i < read_results_.size(); ++i) {
27 read_results_[i].packet_buffer.buffer = read_buffers_[i].packet_buffer;
28 read_results_[i].packet_buffer.buffer_len =
29 sizeof(read_buffers_[i].packet_buffer);
30
31 read_results_[i].control_buffer.buffer = read_buffers_[i].control_buffer;
32 read_results_[i].control_buffer.buffer_len =
33 sizeof(read_buffers_[i].control_buffer);
34 }
35 }
36
37 QuicPacketReader::~QuicPacketReader() = default;
38
ReadAndDispatchPackets(int fd,int port,const QuicClock & clock,ProcessPacketInterface * processor,QuicPacketCount *)39 bool QuicPacketReader::ReadAndDispatchPackets(
40 int fd, int port, const QuicClock& clock, ProcessPacketInterface* processor,
41 QuicPacketCount* /*packets_dropped*/) {
42 // Reset all read_results for reuse.
43 for (size_t i = 0; i < read_results_.size(); ++i) {
44 read_results_[i].Reset(
45 /*packet_buffer_length=*/sizeof(read_buffers_[i].packet_buffer));
46 }
47
48 // Use clock.Now() as the packet receipt time, the time between packet
49 // arriving at the host and now is considered part of the network delay.
50 QuicTime now = clock.Now();
51
52 QuicUdpPacketInfoBitMask info_bits(
53 {QuicUdpPacketInfoBit::DROPPED_PACKETS,
54 QuicUdpPacketInfoBit::PEER_ADDRESS, QuicUdpPacketInfoBit::V4_SELF_IP,
55 QuicUdpPacketInfoBit::V6_SELF_IP, QuicUdpPacketInfoBit::RECV_TIMESTAMP,
56 QuicUdpPacketInfoBit::TTL, QuicUdpPacketInfoBit::GOOGLE_PACKET_HEADER,
57 QuicUdpPacketInfoBit::ECN});
58 size_t packets_read =
59 socket_api_.ReadMultiplePackets(fd, info_bits, &read_results_);
60 for (size_t i = 0; i < packets_read; ++i) {
61 auto& result = read_results_[i];
62 if (!result.ok) {
63 QUIC_CODE_COUNT(quic_packet_reader_read_failure);
64 continue;
65 }
66
67 if (!result.packet_info.HasValue(QuicUdpPacketInfoBit::PEER_ADDRESS)) {
68 QUIC_BUG(quic_bug_10329_1) << "Unable to get peer socket address.";
69 continue;
70 }
71
72 QuicSocketAddress peer_address =
73 result.packet_info.peer_address().Normalized();
74
75 QuicIpAddress self_ip = GetSelfIpFromPacketInfo(
76 result.packet_info, peer_address.host().IsIPv6());
77 if (!self_ip.IsInitialized()) {
78 QUIC_BUG(quic_bug_10329_2) << "Unable to get self IP address.";
79 continue;
80 }
81
82 bool has_ttl = result.packet_info.HasValue(QuicUdpPacketInfoBit::TTL);
83 int ttl = has_ttl ? result.packet_info.ttl() : 0;
84 if (!has_ttl) {
85 QUIC_CODE_COUNT(quic_packet_reader_no_ttl);
86 }
87
88 char* headers = nullptr;
89 size_t headers_length = 0;
90 if (result.packet_info.HasValue(
91 QuicUdpPacketInfoBit::GOOGLE_PACKET_HEADER)) {
92 headers = result.packet_info.google_packet_headers().buffer;
93 headers_length = result.packet_info.google_packet_headers().buffer_len;
94 } else {
95 QUIC_CODE_COUNT(quic_packet_reader_no_google_packet_header);
96 }
97
98 QuicReceivedPacket packet(
99 result.packet_buffer.buffer, result.packet_buffer.buffer_len, now,
100 /*owns_buffer=*/false, ttl, has_ttl, headers, headers_length,
101 /*owns_header_buffer=*/false, result.packet_info.ecn_codepoint());
102 QuicSocketAddress self_address(self_ip, port);
103 processor->ProcessPacket(self_address, peer_address, packet);
104 }
105
106 // We may not have read all of the packets available on the socket.
107 return packets_read == kNumPacketsPerReadMmsgCall;
108 }
109
110 // static
GetSelfIpFromPacketInfo(const QuicUdpPacketInfo & packet_info,bool prefer_v6_ip)111 QuicIpAddress QuicPacketReader::GetSelfIpFromPacketInfo(
112 const QuicUdpPacketInfo& packet_info, bool prefer_v6_ip) {
113 if (prefer_v6_ip) {
114 if (packet_info.HasValue(QuicUdpPacketInfoBit::V6_SELF_IP)) {
115 return packet_info.self_v6_ip();
116 }
117 if (packet_info.HasValue(QuicUdpPacketInfoBit::V4_SELF_IP)) {
118 return packet_info.self_v4_ip();
119 }
120 } else {
121 if (packet_info.HasValue(QuicUdpPacketInfoBit::V4_SELF_IP)) {
122 return packet_info.self_v4_ip();
123 }
124 if (packet_info.HasValue(QuicUdpPacketInfoBit::V6_SELF_IP)) {
125 return packet_info.self_v6_ip();
126 }
127 }
128 return QuicIpAddress();
129 }
130
131 } // namespace quic
132