blob: 33340423eafd0b2746a909cd50e350089e6c9bfb [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#include "net/third_party/quiche/src/quic/core/quic_packet_reader.h"
6
QUICHE teama6ef0a62019-03-07 20:34:33 -05007#include "net/third_party/quiche/src/quic/core/quic_packets.h"
8#include "net/third_party/quiche/src/quic/core/quic_process_packet_interface.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
10#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
wub1dc01cf2020-01-27 13:04:14 -080012#include "net/third_party/quiche/src/quic/platform/api/quic_ip_address.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_server_stats.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_socket_address.h"
bnc4e9283d2019-12-17 07:08:57 -080016#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017
QUICHE teama6ef0a62019-03-07 20:34:33 -050018namespace quic {
19
wub1dc01cf2020-01-27 13:04:14 -080020QuicPacketReader::QuicPacketReader()
21 : read_buffers_(kNumPacketsPerReadMmsgCall),
22 read_results_(kNumPacketsPerReadMmsgCall) {
wub1dc01cf2020-01-27 13:04:14 -080023 DCHECK_EQ(read_buffers_.size(), read_results_.size());
24 for (size_t i = 0; i < read_results_.size(); ++i) {
25 read_results_[i].packet_buffer.buffer = read_buffers_[i].packet_buffer;
26 read_results_[i].packet_buffer.buffer_len =
27 sizeof(read_buffers_[i].packet_buffer);
28
29 read_results_[i].control_buffer.buffer = read_buffers_[i].control_buffer;
30 read_results_[i].control_buffer.buffer_len =
31 sizeof(read_buffers_[i].control_buffer);
32 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050033}
34
QUICHE teama6ef0a62019-03-07 20:34:33 -050035QuicPacketReader::~QuicPacketReader() = default;
36
37bool QuicPacketReader::ReadAndDispatchPackets(
38 int fd,
39 int port,
40 const QuicClock& clock,
41 ProcessPacketInterface* processor,
wub5dcf0282020-02-28 06:56:00 -080042 QuicPacketCount* /*packets_dropped*/) {
wub1dc01cf2020-01-27 13:04:14 -080043 // Reset all read_results for reuse.
44 for (size_t i = 0; i < read_results_.size(); ++i) {
45 read_results_[i].Reset(
46 /*packet_buffer_length=*/sizeof(read_buffers_[i].packet_buffer));
47 }
wub87d204e2020-03-30 12:32:27 -070048
49 // Use clock.Now() as the packet receipt time, the time between packet
50 // arriving at the host and now is considered part of the network delay.
51 QuicTime now = clock.Now();
52
wub1dc01cf2020-01-27 13:04:14 -080053 size_t packets_read = socket_api_.ReadMultiplePackets(
54 fd,
55 BitMask64(QuicUdpPacketInfoBit::DROPPED_PACKETS,
56 QuicUdpPacketInfoBit::PEER_ADDRESS,
57 QuicUdpPacketInfoBit::V4_SELF_IP,
58 QuicUdpPacketInfoBit::V6_SELF_IP,
59 QuicUdpPacketInfoBit::RECV_TIMESTAMP, QuicUdpPacketInfoBit::TTL,
60 QuicUdpPacketInfoBit::GOOGLE_PACKET_HEADER),
61 &read_results_);
62 for (size_t i = 0; i < packets_read; ++i) {
63 auto& result = read_results_[i];
64 if (!result.ok) {
wub5dcf0282020-02-28 06:56:00 -080065 QUIC_CODE_COUNT(quic_packet_reader_read_failure);
wub1dc01cf2020-01-27 13:04:14 -080066 continue;
67 }
68
69 if (!result.packet_info.HasValue(QuicUdpPacketInfoBit::PEER_ADDRESS)) {
70 QUIC_BUG << "Unable to get peer socket address.";
71 continue;
72 }
73
74 QuicSocketAddress peer_address =
75 result.packet_info.peer_address().Normalized();
76
77 QuicIpAddress self_ip = GetSelfIpFromPacketInfo(
78 result.packet_info, peer_address.host().IsIPv6());
79 if (!self_ip.IsInitialized()) {
80 QUIC_BUG << "Unable to get self IP address.";
81 continue;
82 }
83
wub1dc01cf2020-01-27 13:04:14 -080084 bool has_ttl = result.packet_info.HasValue(QuicUdpPacketInfoBit::TTL);
85 int ttl = has_ttl ? result.packet_info.ttl() : 0;
86 if (!has_ttl) {
wub5dcf0282020-02-28 06:56:00 -080087 QUIC_CODE_COUNT(quic_packet_reader_no_ttl);
wub1dc01cf2020-01-27 13:04:14 -080088 }
89
90 char* headers = nullptr;
91 size_t headers_length = 0;
92 if (result.packet_info.HasValue(
93 QuicUdpPacketInfoBit::GOOGLE_PACKET_HEADER)) {
94 headers = result.packet_info.google_packet_headers().buffer;
95 headers_length = result.packet_info.google_packet_headers().buffer_len;
96 } else {
wub5dcf0282020-02-28 06:56:00 -080097 QUIC_CODE_COUNT(quic_packet_reader_no_google_packet_header);
wub1dc01cf2020-01-27 13:04:14 -080098 }
99
100 QuicReceivedPacket packet(
wub87d204e2020-03-30 12:32:27 -0700101 result.packet_buffer.buffer, result.packet_buffer.buffer_len, now,
wub77e5c1f2020-02-13 11:43:09 -0800102 /*owns_buffer=*/false, ttl, has_ttl, headers, headers_length,
103 /*owns_header_buffer=*/false);
wub1dc01cf2020-01-27 13:04:14 -0800104
105 QuicSocketAddress self_address(self_ip, port);
106 processor->ProcessPacket(self_address, peer_address, packet);
107 }
108
109 // We may not have read all of the packets available on the socket.
110 return packets_read == kNumPacketsPerReadMmsgCall;
111}
112
113// static
114QuicIpAddress QuicPacketReader::GetSelfIpFromPacketInfo(
115 const QuicUdpPacketInfo& packet_info,
116 bool prefer_v6_ip) {
117 if (prefer_v6_ip) {
118 if (packet_info.HasValue(QuicUdpPacketInfoBit::V6_SELF_IP)) {
119 return packet_info.self_v6_ip();
120 }
121 if (packet_info.HasValue(QuicUdpPacketInfoBit::V4_SELF_IP)) {
122 return packet_info.self_v4_ip();
123 }
124 } else {
125 if (packet_info.HasValue(QuicUdpPacketInfoBit::V4_SELF_IP)) {
126 return packet_info.self_v4_ip();
127 }
128 if (packet_info.HasValue(QuicUdpPacketInfoBit::V6_SELF_IP)) {
129 return packet_info.self_v6_ip();
130 }
131 }
132 return QuicIpAddress();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500133}
134
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135} // namespace quic