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 | #include "net/third_party/quiche/src/quic/core/quic_packet_reader.h" |
| 6 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #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" |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_ip_address.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #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" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | namespace quic { |
| 19 | |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 20 | QuicPacketReader::QuicPacketReader() |
| 21 | : read_buffers_(kNumPacketsPerReadMmsgCall), |
| 22 | read_results_(kNumPacketsPerReadMmsgCall) { |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 23 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | } |
| 34 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | QuicPacketReader::~QuicPacketReader() = default; |
| 36 | |
| 37 | bool QuicPacketReader::ReadAndDispatchPackets( |
| 38 | int fd, |
| 39 | int port, |
| 40 | const QuicClock& clock, |
| 41 | ProcessPacketInterface* processor, |
wub | 5dcf028 | 2020-02-28 06:56:00 -0800 | [diff] [blame] | 42 | QuicPacketCount* /*packets_dropped*/) { |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 43 | // 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 | } |
wub | 87d204e | 2020-03-30 12:32:27 -0700 | [diff] [blame] | 48 | |
| 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 | |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 53 | 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) { |
wub | 5dcf028 | 2020-02-28 06:56:00 -0800 | [diff] [blame] | 65 | QUIC_CODE_COUNT(quic_packet_reader_read_failure); |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 66 | 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 | |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 84 | bool has_ttl = result.packet_info.HasValue(QuicUdpPacketInfoBit::TTL); |
| 85 | int ttl = has_ttl ? result.packet_info.ttl() : 0; |
| 86 | if (!has_ttl) { |
wub | 5dcf028 | 2020-02-28 06:56:00 -0800 | [diff] [blame] | 87 | QUIC_CODE_COUNT(quic_packet_reader_no_ttl); |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 88 | } |
| 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 { |
wub | 5dcf028 | 2020-02-28 06:56:00 -0800 | [diff] [blame] | 97 | QUIC_CODE_COUNT(quic_packet_reader_no_google_packet_header); |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | QuicReceivedPacket packet( |
wub | 87d204e | 2020-03-30 12:32:27 -0700 | [diff] [blame] | 101 | result.packet_buffer.buffer, result.packet_buffer.buffer_len, now, |
wub | 77e5c1f | 2020-02-13 11:43:09 -0800 | [diff] [blame] | 102 | /*owns_buffer=*/false, ttl, has_ttl, headers, headers_length, |
| 103 | /*owns_header_buffer=*/false); |
wub | 1dc01cf | 2020-01-27 13:04:14 -0800 | [diff] [blame] | 104 | |
| 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 |
| 114 | QuicIpAddress 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 133 | } |
| 134 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 135 | } // namespace quic |