QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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_connection.h" |
| 6 | |
| 7 | #include <string.h> |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <iterator> |
| 12 | #include <limits> |
| 13 | #include <memory> |
| 14 | #include <set> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 15 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
dschinazi | 56fb53e | 2019-06-21 15:30:04 -0700 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quic/core/proto/cached_network_parameters_proto.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/core/quic_bandwidth.h" |
| 24 | #include "net/third_party/quiche/src/quic/core/quic_config.h" |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 25 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
ianswett | dc1e7ab | 2019-05-03 16:10:44 -0700 | [diff] [blame] | 26 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 27 | #include "net/third_party/quiche/src/quic/core/quic_packet_generator.h" |
| 28 | #include "net/third_party/quiche/src/quic/core/quic_pending_retransmission.h" |
| 29 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
| 30 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 31 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 32 | #include "net/third_party/quiche/src/quic/platform/api/quic_client_stats.h" |
| 33 | #include "net/third_party/quiche/src/quic/platform/api/quic_error_code_wrappers.h" |
| 34 | #include "net/third_party/quiche/src/quic/platform/api/quic_exported_stats.h" |
| 35 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
| 36 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 37 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 38 | #include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h" |
| 39 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_utils.h" |
| 41 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 42 | |
| 43 | namespace quic { |
| 44 | |
| 45 | class QuicDecrypter; |
| 46 | class QuicEncrypter; |
| 47 | |
| 48 | namespace { |
| 49 | |
| 50 | // Maximum number of consecutive sent nonretransmittable packets. |
| 51 | const QuicPacketCount kMaxConsecutiveNonRetransmittablePackets = 19; |
| 52 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | // The minimum release time into future in ms. |
| 54 | const int kMinReleaseTimeIntoFutureMs = 1; |
| 55 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | // An alarm that is scheduled to send an ack if a timeout occurs. |
| 57 | class AckAlarmDelegate : public QuicAlarm::Delegate { |
| 58 | public: |
| 59 | explicit AckAlarmDelegate(QuicConnection* connection) |
| 60 | : connection_(connection) {} |
| 61 | AckAlarmDelegate(const AckAlarmDelegate&) = delete; |
| 62 | AckAlarmDelegate& operator=(const AckAlarmDelegate&) = delete; |
| 63 | |
| 64 | void OnAlarm() override { |
| 65 | DCHECK(connection_->ack_frame_updated()); |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 66 | QuicConnection::ScopedPacketFlusher flusher(connection_); |
| 67 | if (connection_->SupportsMultiplePacketNumberSpaces()) { |
| 68 | connection_->SendAllPendingAcks(); |
| 69 | } else { |
| 70 | DCHECK(!connection_->GetUpdatedAckFrame().ack_frame->packets.Empty()); |
| 71 | connection_->SendAck(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | QuicConnection* connection_; |
| 77 | }; |
| 78 | |
| 79 | // This alarm will be scheduled any time a data-bearing packet is sent out. |
| 80 | // When the alarm goes off, the connection checks to see if the oldest packets |
| 81 | // have been acked, and retransmit them if they have not. |
| 82 | class RetransmissionAlarmDelegate : public QuicAlarm::Delegate { |
| 83 | public: |
| 84 | explicit RetransmissionAlarmDelegate(QuicConnection* connection) |
| 85 | : connection_(connection) {} |
| 86 | RetransmissionAlarmDelegate(const RetransmissionAlarmDelegate&) = delete; |
| 87 | RetransmissionAlarmDelegate& operator=(const RetransmissionAlarmDelegate&) = |
| 88 | delete; |
| 89 | |
| 90 | void OnAlarm() override { connection_->OnRetransmissionTimeout(); } |
| 91 | |
| 92 | private: |
| 93 | QuicConnection* connection_; |
| 94 | }; |
| 95 | |
| 96 | // An alarm that is scheduled when the SentPacketManager requires a delay |
| 97 | // before sending packets and fires when the packet may be sent. |
| 98 | class SendAlarmDelegate : public QuicAlarm::Delegate { |
| 99 | public: |
| 100 | explicit SendAlarmDelegate(QuicConnection* connection) |
| 101 | : connection_(connection) {} |
| 102 | SendAlarmDelegate(const SendAlarmDelegate&) = delete; |
| 103 | SendAlarmDelegate& operator=(const SendAlarmDelegate&) = delete; |
| 104 | |
| 105 | void OnAlarm() override { connection_->WriteAndBundleAcksIfNotBlocked(); } |
| 106 | |
| 107 | private: |
| 108 | QuicConnection* connection_; |
| 109 | }; |
| 110 | |
| 111 | class PathDegradingAlarmDelegate : public QuicAlarm::Delegate { |
| 112 | public: |
| 113 | explicit PathDegradingAlarmDelegate(QuicConnection* connection) |
| 114 | : connection_(connection) {} |
| 115 | PathDegradingAlarmDelegate(const PathDegradingAlarmDelegate&) = delete; |
| 116 | PathDegradingAlarmDelegate& operator=(const PathDegradingAlarmDelegate&) = |
| 117 | delete; |
| 118 | |
| 119 | void OnAlarm() override { connection_->OnPathDegradingTimeout(); } |
| 120 | |
| 121 | private: |
| 122 | QuicConnection* connection_; |
| 123 | }; |
| 124 | |
| 125 | class TimeoutAlarmDelegate : public QuicAlarm::Delegate { |
| 126 | public: |
| 127 | explicit TimeoutAlarmDelegate(QuicConnection* connection) |
| 128 | : connection_(connection) {} |
| 129 | TimeoutAlarmDelegate(const TimeoutAlarmDelegate&) = delete; |
| 130 | TimeoutAlarmDelegate& operator=(const TimeoutAlarmDelegate&) = delete; |
| 131 | |
| 132 | void OnAlarm() override { connection_->CheckForTimeout(); } |
| 133 | |
| 134 | private: |
| 135 | QuicConnection* connection_; |
| 136 | }; |
| 137 | |
| 138 | class PingAlarmDelegate : public QuicAlarm::Delegate { |
| 139 | public: |
| 140 | explicit PingAlarmDelegate(QuicConnection* connection) |
| 141 | : connection_(connection) {} |
| 142 | PingAlarmDelegate(const PingAlarmDelegate&) = delete; |
| 143 | PingAlarmDelegate& operator=(const PingAlarmDelegate&) = delete; |
| 144 | |
| 145 | void OnAlarm() override { connection_->OnPingTimeout(); } |
| 146 | |
| 147 | private: |
| 148 | QuicConnection* connection_; |
| 149 | }; |
| 150 | |
| 151 | class MtuDiscoveryAlarmDelegate : public QuicAlarm::Delegate { |
| 152 | public: |
| 153 | explicit MtuDiscoveryAlarmDelegate(QuicConnection* connection) |
| 154 | : connection_(connection) {} |
| 155 | MtuDiscoveryAlarmDelegate(const MtuDiscoveryAlarmDelegate&) = delete; |
| 156 | MtuDiscoveryAlarmDelegate& operator=(const MtuDiscoveryAlarmDelegate&) = |
| 157 | delete; |
| 158 | |
| 159 | void OnAlarm() override { connection_->DiscoverMtu(); } |
| 160 | |
| 161 | private: |
| 162 | QuicConnection* connection_; |
| 163 | }; |
| 164 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | class ProcessUndecryptablePacketsAlarmDelegate : public QuicAlarm::Delegate { |
| 166 | public: |
| 167 | explicit ProcessUndecryptablePacketsAlarmDelegate(QuicConnection* connection) |
| 168 | : connection_(connection) {} |
| 169 | ProcessUndecryptablePacketsAlarmDelegate( |
| 170 | const ProcessUndecryptablePacketsAlarmDelegate&) = delete; |
| 171 | ProcessUndecryptablePacketsAlarmDelegate& operator=( |
| 172 | const ProcessUndecryptablePacketsAlarmDelegate&) = delete; |
| 173 | |
rch | 2af16b1 | 2019-03-22 13:52:39 -0700 | [diff] [blame] | 174 | void OnAlarm() override { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 175 | QuicConnection::ScopedPacketFlusher flusher(connection_); |
rch | 2af16b1 | 2019-03-22 13:52:39 -0700 | [diff] [blame] | 176 | connection_->MaybeProcessUndecryptablePackets(); |
| 177 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | |
| 179 | private: |
| 180 | QuicConnection* connection_; |
| 181 | }; |
| 182 | |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 183 | // Whether this incoming packet is allowed to replace our connection ID. |
| 184 | bool PacketCanReplaceConnectionId(const QuicPacketHeader& header, |
| 185 | Perspective perspective) { |
| 186 | return perspective == Perspective::IS_CLIENT && |
| 187 | header.form == IETF_QUIC_LONG_HEADER_PACKET && |
| 188 | QuicUtils::VariableLengthConnectionIdAllowedForVersion( |
| 189 | header.version.transport_version) && |
| 190 | (header.long_packet_type == INITIAL || |
| 191 | header.long_packet_type == RETRY); |
| 192 | } |
| 193 | |
wub | d06ad10 | 2019-07-09 15:38:24 -0700 | [diff] [blame] | 194 | CongestionControlType GetDefaultCongestionControlType() { |
| 195 | if (GetQuicReloadableFlag(quic_default_to_bbr_v2)) { |
| 196 | return kBBRv2; |
| 197 | } |
| 198 | |
| 199 | if (GetQuicReloadableFlag(quic_default_to_bbr)) { |
| 200 | return kBBR; |
| 201 | } |
| 202 | |
| 203 | return kCubicBytes; |
| 204 | } |
| 205 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 206 | } // namespace |
| 207 | |
| 208 | #define ENDPOINT \ |
| 209 | (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ") |
| 210 | |
| 211 | QuicConnection::QuicConnection( |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 212 | QuicConnectionId server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | QuicSocketAddress initial_peer_address, |
| 214 | QuicConnectionHelperInterface* helper, |
| 215 | QuicAlarmFactory* alarm_factory, |
| 216 | QuicPacketWriter* writer, |
| 217 | bool owns_writer, |
| 218 | Perspective perspective, |
| 219 | const ParsedQuicVersionVector& supported_versions) |
| 220 | : framer_(supported_versions, |
| 221 | helper->GetClock()->ApproximateNow(), |
| 222 | perspective, |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 223 | server_connection_id.length()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 224 | current_packet_content_(NO_FRAMES_RECEIVED), |
| 225 | is_current_packet_connectivity_probing_(false), |
| 226 | current_effective_peer_migration_type_(NO_CHANGE), |
| 227 | helper_(helper), |
| 228 | alarm_factory_(alarm_factory), |
| 229 | per_packet_options_(nullptr), |
| 230 | writer_(writer), |
| 231 | owns_writer_(owns_writer), |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 232 | encryption_level_(ENCRYPTION_INITIAL), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 233 | clock_(helper->GetClock()), |
| 234 | random_generator_(helper->GetRandomGenerator()), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 235 | server_connection_id_(server_connection_id), |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 236 | client_connection_id_(EmptyQuicConnectionId()), |
| 237 | client_connection_id_is_set_(false), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 238 | peer_address_(initial_peer_address), |
| 239 | direct_peer_address_(initial_peer_address), |
| 240 | active_effective_peer_migration_type_(NO_CHANGE), |
| 241 | last_packet_decrypted_(false), |
| 242 | last_size_(0), |
| 243 | current_packet_data_(nullptr), |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 244 | last_decrypted_packet_level_(ENCRYPTION_INITIAL), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 245 | should_last_packet_instigate_acks_(false), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 246 | max_undecryptable_packets_(0), |
fayang | a0727f9 | 2019-07-16 13:49:46 -0700 | [diff] [blame] | 247 | max_tracked_packets_(GetQuicFlag(FLAGS_quic_max_tracked_packet_count)), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 248 | pending_version_negotiation_packet_(false), |
| 249 | send_ietf_version_negotiation_packet_(false), |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 250 | send_version_negotiation_packet_with_prefixed_lengths_(false), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 251 | idle_timeout_connection_close_behavior_( |
| 252 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET), |
| 253 | close_connection_after_five_rtos_(false), |
QUICHE team | b23daa7 | 2019-03-21 08:37:48 -0700 | [diff] [blame] | 254 | uber_received_packet_manager_(&stats_), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 255 | stop_waiting_count_(0), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 256 | pending_retransmission_alarm_(false), |
| 257 | defer_send_in_response_to_packets_(false), |
| 258 | ping_timeout_(QuicTime::Delta::FromSeconds(kPingTimeoutSecs)), |
| 259 | retransmittable_on_wire_timeout_(QuicTime::Delta::Infinite()), |
| 260 | arena_(), |
| 261 | ack_alarm_(alarm_factory_->CreateAlarm(arena_.New<AckAlarmDelegate>(this), |
| 262 | &arena_)), |
| 263 | retransmission_alarm_(alarm_factory_->CreateAlarm( |
| 264 | arena_.New<RetransmissionAlarmDelegate>(this), |
| 265 | &arena_)), |
| 266 | send_alarm_( |
| 267 | alarm_factory_->CreateAlarm(arena_.New<SendAlarmDelegate>(this), |
| 268 | &arena_)), |
| 269 | timeout_alarm_( |
| 270 | alarm_factory_->CreateAlarm(arena_.New<TimeoutAlarmDelegate>(this), |
| 271 | &arena_)), |
| 272 | ping_alarm_( |
| 273 | alarm_factory_->CreateAlarm(arena_.New<PingAlarmDelegate>(this), |
| 274 | &arena_)), |
| 275 | mtu_discovery_alarm_(alarm_factory_->CreateAlarm( |
| 276 | arena_.New<MtuDiscoveryAlarmDelegate>(this), |
| 277 | &arena_)), |
| 278 | path_degrading_alarm_(alarm_factory_->CreateAlarm( |
| 279 | arena_.New<PathDegradingAlarmDelegate>(this), |
| 280 | &arena_)), |
| 281 | process_undecryptable_packets_alarm_(alarm_factory_->CreateAlarm( |
| 282 | arena_.New<ProcessUndecryptablePacketsAlarmDelegate>(this), |
| 283 | &arena_)), |
| 284 | visitor_(nullptr), |
| 285 | debug_visitor_(nullptr), |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 286 | packet_generator_(server_connection_id_, |
| 287 | &framer_, |
| 288 | random_generator_, |
| 289 | this), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 290 | idle_network_timeout_(QuicTime::Delta::Infinite()), |
| 291 | handshake_timeout_(QuicTime::Delta::Infinite()), |
zhongyi | c1cab06 | 2019-06-19 12:02:24 -0700 | [diff] [blame] | 292 | time_of_first_packet_sent_after_receiving_(QuicTime::Zero()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 293 | time_of_last_received_packet_(clock_->ApproximateNow()), |
wub | d06ad10 | 2019-07-09 15:38:24 -0700 | [diff] [blame] | 294 | sent_packet_manager_(perspective, |
| 295 | clock_, |
| 296 | random_generator_, |
| 297 | &stats_, |
| 298 | GetDefaultCongestionControlType(), |
| 299 | kNack), |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 300 | version_negotiated_(false), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 301 | perspective_(perspective), |
| 302 | connected_(true), |
| 303 | can_truncate_connection_ids_(perspective == Perspective::IS_SERVER), |
| 304 | mtu_discovery_target_(0), |
| 305 | mtu_probe_count_(0), |
| 306 | packets_between_mtu_probes_(kPacketsBetweenMtuProbesBase), |
| 307 | next_mtu_probe_at_(kPacketsBetweenMtuProbesBase), |
| 308 | largest_received_packet_size_(0), |
| 309 | write_error_occurred_(false), |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 310 | no_stop_waiting_frames_( |
| 311 | VersionHasIetfInvariantHeader(transport_version())), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 312 | consecutive_num_packets_with_no_retransmittable_frames_(0), |
| 313 | max_consecutive_num_packets_with_no_retransmittable_frames_( |
| 314 | kMaxConsecutiveNonRetransmittablePackets), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 315 | fill_up_link_during_probing_(false), |
| 316 | probing_retransmission_pending_(false), |
| 317 | stateless_reset_token_received_(false), |
| 318 | received_stateless_reset_token_(0), |
| 319 | last_control_frame_id_(kInvalidControlFrameId), |
| 320 | is_path_degrading_(false), |
| 321 | processing_ack_frame_(false), |
| 322 | supports_release_time_(false), |
| 323 | release_time_into_future_(QuicTime::Delta::Zero()), |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 324 | retry_has_been_parsed_(false) { |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 325 | QUIC_DLOG(INFO) << ENDPOINT << "Created connection with server connection ID " |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 326 | << server_connection_id |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 327 | << " and version: " << ParsedQuicVersionToString(version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 328 | |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 329 | QUIC_BUG_IF(!QuicUtils::IsConnectionIdValidForVersion(server_connection_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 330 | transport_version())) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 331 | << "QuicConnection: attempted to use server connection ID " |
| 332 | << server_connection_id << " which is invalid with version " |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 333 | << QuicVersionToString(transport_version()); |
| 334 | |
| 335 | framer_.set_visitor(this); |
| 336 | stats_.connection_creation_time = clock_->ApproximateNow(); |
| 337 | // TODO(ianswett): Supply the NetworkChangeVisitor as a constructor argument |
| 338 | // and make it required non-null, because it's always used. |
| 339 | sent_packet_manager_.SetNetworkChangeVisitor(this); |
| 340 | if (GetQuicRestartFlag(quic_offload_pacing_to_usps2)) { |
| 341 | sent_packet_manager_.SetPacingAlarmGranularity(QuicTime::Delta::Zero()); |
| 342 | release_time_into_future_ = |
| 343 | QuicTime::Delta::FromMilliseconds(kMinReleaseTimeIntoFutureMs); |
| 344 | } |
| 345 | // Allow the packet writer to potentially reduce the packet size to a value |
| 346 | // even smaller than kDefaultMaxPacketSize. |
| 347 | SetMaxPacketLength(perspective_ == Perspective::IS_SERVER |
| 348 | ? kDefaultServerMaxPacketSize |
| 349 | : kDefaultMaxPacketSize); |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 350 | uber_received_packet_manager_.set_max_ack_ranges(255); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 351 | MaybeEnableSessionDecidesWhatToWrite(); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 352 | MaybeEnableMultiplePacketNumberSpacesSupport(); |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 353 | DCHECK(perspective_ == Perspective::IS_CLIENT || |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 354 | supported_versions.size() == 1); |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 355 | InstallInitialCrypters(server_connection_id_); |
dschinazi | 6ece500 | 2019-05-22 06:35:49 -0700 | [diff] [blame] | 356 | } |
| 357 | |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 358 | void QuicConnection::InstallInitialCrypters(QuicConnectionId connection_id) { |
dschinazi | 6ece500 | 2019-05-22 06:35:49 -0700 | [diff] [blame] | 359 | if (version().handshake_protocol != PROTOCOL_TLS1_3) { |
| 360 | // Initial crypters are currently only supported with TLS. |
| 361 | return; |
| 362 | } |
| 363 | CrypterPair crypters; |
| 364 | CryptoUtils::CreateTlsInitialCrypters(perspective_, transport_version(), |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 365 | connection_id, &crypters); |
dschinazi | 6ece500 | 2019-05-22 06:35:49 -0700 | [diff] [blame] | 366 | SetEncrypter(ENCRYPTION_INITIAL, std::move(crypters.encrypter)); |
| 367 | InstallDecrypter(ENCRYPTION_INITIAL, std::move(crypters.decrypter)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | QuicConnection::~QuicConnection() { |
| 371 | if (owns_writer_) { |
| 372 | delete writer_; |
| 373 | } |
| 374 | ClearQueuedPackets(); |
| 375 | } |
| 376 | |
| 377 | void QuicConnection::ClearQueuedPackets() { |
| 378 | for (auto it = queued_packets_.begin(); it != queued_packets_.end(); ++it) { |
| 379 | // Delete the buffer before calling ClearSerializedPacket, which sets |
| 380 | // encrypted_buffer to nullptr. |
| 381 | delete[] it->encrypted_buffer; |
| 382 | ClearSerializedPacket(&(*it)); |
| 383 | } |
| 384 | queued_packets_.clear(); |
| 385 | } |
| 386 | |
| 387 | void QuicConnection::SetFromConfig(const QuicConfig& config) { |
| 388 | if (config.negotiated()) { |
| 389 | // Handshake complete, set handshake timeout to Infinite. |
| 390 | SetNetworkTimeouts(QuicTime::Delta::Infinite(), |
| 391 | config.IdleNetworkTimeout()); |
| 392 | if (config.SilentClose()) { |
| 393 | idle_timeout_connection_close_behavior_ = |
| 394 | ConnectionCloseBehavior::SILENT_CLOSE; |
| 395 | } |
| 396 | } else { |
| 397 | SetNetworkTimeouts(config.max_time_before_crypto_handshake(), |
| 398 | config.max_idle_time_before_crypto_handshake()); |
| 399 | } |
| 400 | |
| 401 | sent_packet_manager_.SetFromConfig(config); |
| 402 | if (config.HasReceivedBytesForConnectionId() && |
| 403 | can_truncate_connection_ids_) { |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 404 | packet_generator_.SetServerConnectionIdLength( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 405 | config.ReceivedBytesForConnectionId()); |
| 406 | } |
| 407 | max_undecryptable_packets_ = config.max_undecryptable_packets(); |
| 408 | |
| 409 | if (config.HasClientSentConnectionOption(kMTUH, perspective_)) { |
| 410 | SetMtuDiscoveryTarget(kMtuDiscoveryTargetPacketSizeHigh); |
| 411 | } |
| 412 | if (config.HasClientSentConnectionOption(kMTUL, perspective_)) { |
| 413 | SetMtuDiscoveryTarget(kMtuDiscoveryTargetPacketSizeLow); |
| 414 | } |
| 415 | if (debug_visitor_ != nullptr) { |
| 416 | debug_visitor_->OnSetFromConfig(config); |
| 417 | } |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 418 | uber_received_packet_manager_.SetFromConfig(config, perspective_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 419 | if (config.HasClientSentConnectionOption(k5RTO, perspective_)) { |
| 420 | close_connection_after_five_rtos_ = true; |
| 421 | } |
| 422 | if (config.HasClientSentConnectionOption(kNSTP, perspective_)) { |
| 423 | no_stop_waiting_frames_ = true; |
| 424 | } |
| 425 | if (config.HasReceivedStatelessResetToken()) { |
| 426 | stateless_reset_token_received_ = true; |
| 427 | received_stateless_reset_token_ = config.ReceivedStatelessResetToken(); |
| 428 | } |
fkastenholz | 4dc4ba3 | 2019-07-30 09:55:25 -0700 | [diff] [blame] | 429 | if (config.HasReceivedAckDelayExponent()) { |
| 430 | framer_.set_peer_ack_delay_exponent(config.ReceivedAckDelayExponent()); |
| 431 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 432 | if (GetQuicReloadableFlag(quic_send_timestamps) && |
| 433 | config.HasClientSentConnectionOption(kSTMP, perspective_)) { |
| 434 | QUIC_RELOADABLE_FLAG_COUNT(quic_send_timestamps); |
| 435 | framer_.set_process_timestamps(true); |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 436 | uber_received_packet_manager_.set_save_timestamps(true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | supports_release_time_ = |
| 440 | writer_ != nullptr && writer_->SupportsReleaseTime() && |
| 441 | !config.HasClientSentConnectionOption(kNPCO, perspective_); |
| 442 | |
| 443 | if (supports_release_time_) { |
| 444 | UpdateReleaseTimeIntoFuture(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void QuicConnection::OnSendConnectionState( |
| 449 | const CachedNetworkParameters& cached_network_params) { |
| 450 | if (debug_visitor_ != nullptr) { |
| 451 | debug_visitor_->OnSendConnectionState(cached_network_params); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void QuicConnection::OnReceiveConnectionState( |
| 456 | const CachedNetworkParameters& cached_network_params) { |
| 457 | if (debug_visitor_ != nullptr) { |
| 458 | debug_visitor_->OnReceiveConnectionState(cached_network_params); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void QuicConnection::ResumeConnectionState( |
| 463 | const CachedNetworkParameters& cached_network_params, |
| 464 | bool max_bandwidth_resumption) { |
| 465 | sent_packet_manager_.ResumeConnectionState(cached_network_params, |
| 466 | max_bandwidth_resumption); |
| 467 | } |
| 468 | |
| 469 | void QuicConnection::SetMaxPacingRate(QuicBandwidth max_pacing_rate) { |
| 470 | sent_packet_manager_.SetMaxPacingRate(max_pacing_rate); |
| 471 | } |
| 472 | |
| 473 | void QuicConnection::AdjustNetworkParameters(QuicBandwidth bandwidth, |
fayang | f1b99dc | 2019-05-14 06:29:18 -0700 | [diff] [blame] | 474 | QuicTime::Delta rtt, |
| 475 | bool allow_cwnd_to_decrease) { |
| 476 | sent_packet_manager_.AdjustNetworkParameters(bandwidth, rtt, |
| 477 | allow_cwnd_to_decrease); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | QuicBandwidth QuicConnection::MaxPacingRate() const { |
| 481 | return sent_packet_manager_.MaxPacingRate(); |
| 482 | } |
| 483 | |
| 484 | bool QuicConnection::SelectMutualVersion( |
| 485 | const ParsedQuicVersionVector& available_versions) { |
| 486 | // Try to find the highest mutual version by iterating over supported |
| 487 | // versions, starting with the highest, and breaking out of the loop once we |
| 488 | // find a matching version in the provided available_versions vector. |
| 489 | const ParsedQuicVersionVector& supported_versions = |
| 490 | framer_.supported_versions(); |
| 491 | for (size_t i = 0; i < supported_versions.size(); ++i) { |
| 492 | const ParsedQuicVersion& version = supported_versions[i]; |
| 493 | if (QuicContainsValue(available_versions, version)) { |
| 494 | framer_.set_version(version); |
| 495 | return true; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | void QuicConnection::OnError(QuicFramer* framer) { |
| 503 | // Packets that we can not or have not decrypted are dropped. |
| 504 | // TODO(rch): add stats to measure this. |
| 505 | if (!connected_ || last_packet_decrypted_ == false) { |
| 506 | return; |
| 507 | } |
| 508 | CloseConnection(framer->error(), framer->detailed_error(), |
| 509 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 510 | } |
| 511 | |
| 512 | void QuicConnection::OnPacket() { |
| 513 | last_packet_decrypted_ = false; |
| 514 | } |
| 515 | |
| 516 | void QuicConnection::OnPublicResetPacket(const QuicPublicResetPacket& packet) { |
| 517 | // Check that any public reset packet with a different connection ID that was |
| 518 | // routed to this QuicConnection has been redirected before control reaches |
| 519 | // here. (Check for a bug regression.) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 520 | DCHECK_EQ(server_connection_id_, packet.connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 521 | DCHECK_EQ(perspective_, Perspective::IS_CLIENT); |
| 522 | if (debug_visitor_ != nullptr) { |
| 523 | debug_visitor_->OnPublicResetPacket(packet); |
| 524 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 525 | std::string error_details = "Received public reset."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 526 | if (perspective_ == Perspective::IS_CLIENT && !packet.endpoint_id.empty()) { |
| 527 | QuicStrAppend(&error_details, " From ", packet.endpoint_id, "."); |
| 528 | } |
| 529 | QUIC_DLOG(INFO) << ENDPOINT << error_details; |
| 530 | QUIC_CODE_COUNT(quic_tear_down_local_connection_on_public_reset); |
| 531 | TearDownLocalConnectionState(QUIC_PUBLIC_RESET, error_details, |
| 532 | ConnectionCloseSource::FROM_PEER); |
| 533 | } |
| 534 | |
| 535 | bool QuicConnection::OnProtocolVersionMismatch( |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 536 | ParsedQuicVersion received_version) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 537 | QUIC_DLOG(INFO) << ENDPOINT << "Received packet with mismatched version " |
| 538 | << ParsedQuicVersionToString(received_version); |
| 539 | if (perspective_ == Perspective::IS_CLIENT) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 540 | const std::string error_details = "Protocol version mismatch."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 541 | QUIC_BUG << ENDPOINT << error_details; |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 542 | CloseConnection(QUIC_INTERNAL_ERROR, error_details, |
| 543 | ConnectionCloseBehavior::SILENT_CLOSE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 544 | } |
| 545 | |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 546 | // Server drops old packets that were sent by the client before the version |
| 547 | // was negotiated. |
| 548 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | // Handles version negotiation for client connection. |
| 552 | void QuicConnection::OnVersionNegotiationPacket( |
| 553 | const QuicVersionNegotiationPacket& packet) { |
| 554 | // Check that any public reset packet with a different connection ID that was |
| 555 | // routed to this QuicConnection has been redirected before control reaches |
| 556 | // here. (Check for a bug regression.) |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 557 | DCHECK_EQ(server_connection_id_, packet.connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 558 | if (perspective_ == Perspective::IS_SERVER) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 559 | const std::string error_details = |
dschinazi | 5a354c9 | 2019-05-09 12:18:53 -0700 | [diff] [blame] | 560 | "Server received version negotiation packet."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 561 | QUIC_BUG << error_details; |
| 562 | QUIC_CODE_COUNT(quic_tear_down_local_connection_on_version_negotiation); |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 563 | CloseConnection(QUIC_INTERNAL_ERROR, error_details, |
| 564 | ConnectionCloseBehavior::SILENT_CLOSE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 565 | return; |
| 566 | } |
| 567 | if (debug_visitor_ != nullptr) { |
| 568 | debug_visitor_->OnVersionNegotiationPacket(packet); |
| 569 | } |
| 570 | |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 571 | if (version_negotiated_) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 572 | // Possibly a duplicate version negotiation packet. |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | if (QuicContainsValue(packet.versions, version())) { |
dschinazi | c857986 | 2019-07-24 18:20:20 -0700 | [diff] [blame] | 577 | const std::string error_details = QuicStrCat( |
| 578 | "Server already supports client's version ", |
| 579 | ParsedQuicVersionToString(version()), |
| 580 | " and should have accepted the connection instead of sending {", |
| 581 | ParsedQuicVersionVectorToString(packet.versions), "}."); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 582 | QUIC_DLOG(WARNING) << error_details; |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 583 | CloseConnection(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, error_details, |
| 584 | ConnectionCloseBehavior::SILENT_CLOSE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
| 588 | server_supported_versions_ = packet.versions; |
fayang | 9ed391a | 2019-06-20 11:16:59 -0700 | [diff] [blame] | 589 | CloseConnection( |
| 590 | QUIC_INVALID_VERSION, |
| 591 | QuicStrCat( |
| 592 | "Client may support one of the versions in the server's list, but " |
| 593 | "it's going to close the connection anyway. Supported versions: {", |
| 594 | ParsedQuicVersionVectorToString(framer_.supported_versions()), |
| 595 | "}, peer supported versions: {", |
| 596 | ParsedQuicVersionVectorToString(packet.versions), "}"), |
| 597 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 598 | } |
| 599 | |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 600 | // Handles retry for client connection. |
| 601 | void QuicConnection::OnRetryPacket(QuicConnectionId original_connection_id, |
| 602 | QuicConnectionId new_connection_id, |
| 603 | QuicStringPiece retry_token) { |
dschinazi | 6ece500 | 2019-05-22 06:35:49 -0700 | [diff] [blame] | 604 | DCHECK_EQ(Perspective::IS_CLIENT, perspective_); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 605 | if (original_connection_id != server_connection_id_) { |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 606 | QUIC_DLOG(ERROR) << "Ignoring RETRY with original connection ID " |
| 607 | << original_connection_id << " not matching expected " |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 608 | << server_connection_id_ << " token " |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 609 | << QuicTextUtils::HexEncode(retry_token); |
| 610 | return; |
| 611 | } |
| 612 | if (retry_has_been_parsed_) { |
| 613 | QUIC_DLOG(ERROR) << "Ignoring non-first RETRY with token " |
| 614 | << QuicTextUtils::HexEncode(retry_token); |
| 615 | return; |
| 616 | } |
| 617 | retry_has_been_parsed_ = true; |
| 618 | QUIC_DLOG(INFO) << "Received RETRY, replacing connection ID " |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 619 | << server_connection_id_ << " with " << new_connection_id |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 620 | << ", received token " |
| 621 | << QuicTextUtils::HexEncode(retry_token); |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 622 | server_connection_id_ = new_connection_id; |
| 623 | packet_generator_.SetServerConnectionId(server_connection_id_); |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 624 | packet_generator_.SetRetryToken(retry_token); |
| 625 | |
| 626 | // Reinstall initial crypters because the connection ID changed. |
dschinazi | 5c03085 | 2019-07-11 15:45:53 -0700 | [diff] [blame] | 627 | InstallInitialCrypters(server_connection_id_); |
dschinazi | 244f6dc | 2019-05-06 15:45:16 -0700 | [diff] [blame] | 628 | } |
| 629 | |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 630 | bool QuicConnection::HasIncomingConnectionId(QuicConnectionId connection_id) { |
| 631 | for (QuicConnectionId const& incoming_connection_id : |
| 632 | incoming_connection_ids_) { |
| 633 | if (incoming_connection_id == connection_id) { |
| 634 | return true; |
| 635 | } |
| 636 | } |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | void QuicConnection::AddIncomingConnectionId(QuicConnectionId connection_id) { |
| 641 | if (HasIncomingConnectionId(connection_id)) { |
| 642 | return; |
| 643 | } |
| 644 | incoming_connection_ids_.push_back(connection_id); |
| 645 | } |
| 646 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 647 | bool QuicConnection::OnUnauthenticatedPublicHeader( |
| 648 | const QuicPacketHeader& header) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 649 | QuicConnectionId server_connection_id = |
| 650 | GetServerConnectionIdAsRecipient(header, perspective_); |
| 651 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 652 | if (server_connection_id != server_connection_id_ && |
| 653 | !HasIncomingConnectionId(server_connection_id)) { |
| 654 | if (PacketCanReplaceConnectionId(header, perspective_)) { |
| 655 | QUIC_DLOG(INFO) << ENDPOINT << "Accepting packet with new connection ID " |
| 656 | << server_connection_id << " instead of " |
| 657 | << server_connection_id_; |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | ++stats_.packets_dropped; |
| 662 | QUIC_DLOG(INFO) << ENDPOINT |
| 663 | << "Ignoring packet from unexpected server connection ID " |
| 664 | << server_connection_id << " instead of " |
| 665 | << server_connection_id_; |
| 666 | if (debug_visitor_ != nullptr) { |
| 667 | debug_visitor_->OnIncorrectConnectionId(server_connection_id); |
| 668 | } |
| 669 | // If this is a server, the dispatcher routes each packet to the |
| 670 | // QuicConnection responsible for the packet's connection ID. So if control |
| 671 | // arrives here and this is a server, the dispatcher must be malfunctioning. |
| 672 | DCHECK_NE(Perspective::IS_SERVER, perspective_); |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | if (!version().SupportsClientConnectionIds()) { |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 680 | QuicConnectionId client_connection_id = |
| 681 | GetClientConnectionIdAsRecipient(header, perspective_); |
| 682 | |
| 683 | if (client_connection_id == client_connection_id_) { |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | if (!client_connection_id_is_set_ && perspective_ == Perspective::IS_SERVER) { |
| 688 | QUIC_DLOG(INFO) << ENDPOINT |
| 689 | << "Setting client connection ID from first packet to " |
| 690 | << client_connection_id; |
| 691 | set_client_connection_id(client_connection_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 692 | return true; |
| 693 | } |
| 694 | |
| 695 | ++stats_.packets_dropped; |
| 696 | QUIC_DLOG(INFO) << ENDPOINT |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 697 | << "Ignoring packet from unexpected client connection ID " |
| 698 | << client_connection_id << " instead of " |
| 699 | << client_connection_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 700 | return false; |
| 701 | } |
| 702 | |
| 703 | bool QuicConnection::OnUnauthenticatedHeader(const QuicPacketHeader& header) { |
| 704 | if (debug_visitor_ != nullptr) { |
| 705 | debug_visitor_->OnUnauthenticatedHeader(header); |
| 706 | } |
| 707 | |
| 708 | // Check that any public reset packet with a different connection ID that was |
| 709 | // routed to this QuicConnection has been redirected before control reaches |
| 710 | // here. |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 711 | DCHECK(GetServerConnectionIdAsRecipient(header, perspective_) == |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 712 | server_connection_id_ || |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 713 | HasIncomingConnectionId( |
| 714 | GetServerConnectionIdAsRecipient(header, perspective_)) || |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 715 | PacketCanReplaceConnectionId(header, perspective_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 716 | |
fayang | 914fbe1 | 2019-07-11 06:23:55 -0700 | [diff] [blame] | 717 | if (packet_generator_.HasPendingFrames()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 718 | // Incoming packets may change a queued ACK frame. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 719 | const std::string error_details = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 720 | "Pending frames must be serialized before incoming packets are " |
| 721 | "processed."; |
| 722 | QUIC_BUG << error_details << ", received header: " << header; |
| 723 | CloseConnection(QUIC_INTERNAL_ERROR, error_details, |
| 724 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 725 | return false; |
| 726 | } |
| 727 | |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 728 | if (!version_negotiated_ && perspective_ == Perspective::IS_SERVER) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 729 | if (!header.version_flag) { |
| 730 | // Packets should have the version flag till version negotiation is |
| 731 | // done. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 732 | std::string error_details = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 733 | QuicStrCat(ENDPOINT, "Packet ", header.packet_number.ToUint64(), |
| 734 | " without version flag before version negotiated."); |
| 735 | QUIC_DLOG(WARNING) << error_details; |
| 736 | CloseConnection(QUIC_INVALID_VERSION, error_details, |
| 737 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 738 | return false; |
| 739 | } else { |
| 740 | DCHECK_EQ(header.version, version()); |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 741 | version_negotiated_ = true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 742 | framer_.InferPacketHeaderTypeFromVersion(); |
| 743 | visitor_->OnSuccessfulVersionNegotiation(version()); |
| 744 | if (debug_visitor_ != nullptr) { |
| 745 | debug_visitor_->OnSuccessfulVersionNegotiation(version()); |
| 746 | } |
| 747 | } |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 748 | DCHECK(version_negotiated_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | return true; |
| 752 | } |
| 753 | |
| 754 | void QuicConnection::OnDecryptedPacket(EncryptionLevel level) { |
| 755 | last_decrypted_packet_level_ = level; |
| 756 | last_packet_decrypted_ = true; |
| 757 | |
| 758 | // Once the server receives a forward secure packet, the handshake is |
| 759 | // confirmed. |
| 760 | if (level == ENCRYPTION_FORWARD_SECURE && |
| 761 | perspective_ == Perspective::IS_SERVER) { |
| 762 | sent_packet_manager_.SetHandshakeConfirmed(); |
fayang | bf3d286 | 2019-06-20 14:13:44 -0700 | [diff] [blame] | 763 | // This may have changed the retransmission timer, so re-arm it. |
| 764 | SetRetransmissionAlarm(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
| 768 | QuicSocketAddress QuicConnection::GetEffectivePeerAddressFromCurrentPacket() |
| 769 | const { |
| 770 | // By default, the connection is not proxied, and the effective peer address |
| 771 | // is the packet's source address, i.e. the direct peer address. |
| 772 | return last_packet_source_address_; |
| 773 | } |
| 774 | |
| 775 | bool QuicConnection::OnPacketHeader(const QuicPacketHeader& header) { |
| 776 | if (debug_visitor_ != nullptr) { |
| 777 | debug_visitor_->OnPacketHeader(header); |
| 778 | } |
| 779 | |
| 780 | // Will be decremented below if we fall through to return true. |
| 781 | ++stats_.packets_dropped; |
| 782 | |
| 783 | if (!ProcessValidatedPacket(header)) { |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | // Initialize the current packet content state. |
| 788 | current_packet_content_ = NO_FRAMES_RECEIVED; |
| 789 | is_current_packet_connectivity_probing_ = false; |
| 790 | current_effective_peer_migration_type_ = NO_CHANGE; |
| 791 | |
| 792 | if (perspective_ == Perspective::IS_CLIENT) { |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 793 | if (!GetLargestReceivedPacket().IsInitialized() || |
| 794 | header.packet_number > GetLargestReceivedPacket()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 795 | // Update peer_address_ and effective_peer_address_ immediately for |
| 796 | // client connections. |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 797 | // TODO(fayang): only change peer addresses in application data packet |
| 798 | // number space. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 799 | direct_peer_address_ = last_packet_source_address_; |
| 800 | effective_peer_address_ = GetEffectivePeerAddressFromCurrentPacket(); |
| 801 | } |
| 802 | } else { |
| 803 | // At server, remember the address change type of effective_peer_address |
| 804 | // in current_effective_peer_migration_type_. But this variable alone |
| 805 | // doesn't necessarily starts a migration. A migration will be started |
| 806 | // later, once the current packet is confirmed to meet the following |
| 807 | // conditions: |
| 808 | // 1) current_effective_peer_migration_type_ is not NO_CHANGE. |
| 809 | // 2) The current packet is not a connectivity probing. |
| 810 | // 3) The current packet is not reordered, i.e. its packet number is the |
| 811 | // largest of this connection so far. |
| 812 | // Once the above conditions are confirmed, a new migration will start |
| 813 | // even if there is an active migration underway. |
| 814 | current_effective_peer_migration_type_ = |
| 815 | QuicUtils::DetermineAddressChangeType( |
| 816 | effective_peer_address_, |
| 817 | GetEffectivePeerAddressFromCurrentPacket()); |
| 818 | |
| 819 | QUIC_DLOG_IF(INFO, current_effective_peer_migration_type_ != NO_CHANGE) |
| 820 | << ENDPOINT << "Effective peer's ip:port changed from " |
| 821 | << effective_peer_address_.ToString() << " to " |
| 822 | << GetEffectivePeerAddressFromCurrentPacket().ToString() |
| 823 | << ", active_effective_peer_migration_type is " |
| 824 | << active_effective_peer_migration_type_; |
| 825 | } |
| 826 | |
| 827 | --stats_.packets_dropped; |
| 828 | QUIC_DVLOG(1) << ENDPOINT << "Received packet header: " << header; |
| 829 | last_header_ = header; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 830 | |
| 831 | // Record packet receipt to populate ack info before processing stream |
| 832 | // frames, since the processing may result in sending a bundled ack. |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 833 | uber_received_packet_manager_.RecordPacketReceived( |
| 834 | last_decrypted_packet_level_, last_header_, |
| 835 | time_of_last_received_packet_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 836 | DCHECK(connected_); |
| 837 | return true; |
| 838 | } |
| 839 | |
| 840 | bool QuicConnection::OnStreamFrame(const QuicStreamFrame& frame) { |
| 841 | DCHECK(connected_); |
| 842 | |
| 843 | // Since a stream frame was received, this is not a connectivity probe. |
| 844 | // A probe only contains a PING and full padding. |
| 845 | UpdatePacketContent(NOT_PADDED_PING); |
| 846 | |
| 847 | if (debug_visitor_ != nullptr) { |
| 848 | debug_visitor_->OnStreamFrame(frame); |
| 849 | } |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 850 | if (!QuicUtils::IsCryptoStreamId(transport_version(), frame.stream_id) && |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 851 | last_decrypted_packet_level_ == ENCRYPTION_INITIAL) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 852 | if (MaybeConsiderAsMemoryCorruption(frame)) { |
| 853 | CloseConnection(QUIC_MAYBE_CORRUPTED_MEMORY, |
| 854 | "Received crypto frame on non crypto stream.", |
| 855 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 856 | return false; |
| 857 | } |
| 858 | |
| 859 | QUIC_PEER_BUG << ENDPOINT |
| 860 | << "Received an unencrypted data frame: closing connection" |
| 861 | << " packet_number:" << last_header_.packet_number |
QUICHE team | b23daa7 | 2019-03-21 08:37:48 -0700 | [diff] [blame] | 862 | << " stream_id:" << frame.stream_id |
fayang | 21ffb71 | 2019-05-16 08:39:26 -0700 | [diff] [blame] | 863 | << " received_packets:" << ack_frame(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 864 | CloseConnection(QUIC_UNENCRYPTED_STREAM_DATA, |
| 865 | "Unencrypted stream data seen.", |
| 866 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 867 | return false; |
| 868 | } |
| 869 | visitor_->OnStreamFrame(frame); |
| 870 | stats_.stream_bytes_received += frame.data_length; |
| 871 | should_last_packet_instigate_acks_ = true; |
| 872 | return connected_; |
| 873 | } |
| 874 | |
| 875 | bool QuicConnection::OnCryptoFrame(const QuicCryptoFrame& frame) { |
| 876 | DCHECK(connected_); |
| 877 | |
| 878 | // Since a CRYPTO frame was received, this is not a connectivity probe. |
| 879 | // A probe only contains a PING and full padding. |
| 880 | UpdatePacketContent(NOT_PADDED_PING); |
| 881 | |
| 882 | visitor_->OnCryptoFrame(frame); |
| 883 | should_last_packet_instigate_acks_ = true; |
| 884 | return connected_; |
| 885 | } |
| 886 | |
| 887 | bool QuicConnection::OnAckFrameStart(QuicPacketNumber largest_acked, |
| 888 | QuicTime::Delta ack_delay_time) { |
| 889 | DCHECK(connected_); |
| 890 | |
| 891 | if (processing_ack_frame_) { |
| 892 | CloseConnection(QUIC_INVALID_ACK_DATA, |
| 893 | "Received a new ack while processing an ack frame.", |
| 894 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | // Since an ack frame was received, this is not a connectivity probe. |
| 899 | // A probe only contains a PING and full padding. |
| 900 | UpdatePacketContent(NOT_PADDED_PING); |
| 901 | |
| 902 | QUIC_DVLOG(1) << ENDPOINT |
| 903 | << "OnAckFrameStart, largest_acked: " << largest_acked; |
| 904 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 905 | if (GetLargestReceivedPacketWithAck().IsInitialized() && |
| 906 | last_header_.packet_number <= GetLargestReceivedPacketWithAck()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 907 | QUIC_DLOG(INFO) << ENDPOINT << "Received an old ack frame: ignoring"; |
| 908 | return true; |
| 909 | } |
| 910 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 911 | if (!GetLargestSentPacket().IsInitialized() || |
| 912 | largest_acked > GetLargestSentPacket()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 913 | QUIC_DLOG(WARNING) << ENDPOINT |
| 914 | << "Peer's observed unsent packet:" << largest_acked |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 915 | << " vs " << GetLargestSentPacket(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 916 | // We got an ack for data we have not sent. |
| 917 | CloseConnection(QUIC_INVALID_ACK_DATA, "Largest observed too high.", |
| 918 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 919 | return false; |
| 920 | } |
| 921 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 922 | if (!GetLargestAckedPacket().IsInitialized() || |
| 923 | largest_acked > GetLargestAckedPacket()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 924 | visitor_->OnForwardProgressConfirmed(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 925 | } |
| 926 | processing_ack_frame_ = true; |
| 927 | sent_packet_manager_.OnAckFrameStart(largest_acked, ack_delay_time, |
| 928 | time_of_last_received_packet_); |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | bool QuicConnection::OnAckRange(QuicPacketNumber start, QuicPacketNumber end) { |
| 933 | DCHECK(connected_); |
| 934 | QUIC_DVLOG(1) << ENDPOINT << "OnAckRange: [" << start << ", " << end << ")"; |
| 935 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 936 | if (GetLargestReceivedPacketWithAck().IsInitialized() && |
| 937 | last_header_.packet_number <= GetLargestReceivedPacketWithAck()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 938 | QUIC_DLOG(INFO) << ENDPOINT << "Received an old ack frame: ignoring"; |
| 939 | return true; |
| 940 | } |
| 941 | |
| 942 | sent_packet_manager_.OnAckRange(start, end); |
| 943 | return true; |
| 944 | } |
| 945 | |
| 946 | bool QuicConnection::OnAckTimestamp(QuicPacketNumber packet_number, |
| 947 | QuicTime timestamp) { |
| 948 | DCHECK(connected_); |
| 949 | QUIC_DVLOG(1) << ENDPOINT << "OnAckTimestamp: [" << packet_number << ", " |
| 950 | << timestamp.ToDebuggingValue() << ")"; |
| 951 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 952 | if (GetLargestReceivedPacketWithAck().IsInitialized() && |
| 953 | last_header_.packet_number <= GetLargestReceivedPacketWithAck()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 954 | QUIC_DLOG(INFO) << ENDPOINT << "Received an old ack frame: ignoring"; |
| 955 | return true; |
| 956 | } |
| 957 | |
| 958 | sent_packet_manager_.OnAckTimestamp(packet_number, timestamp); |
| 959 | return true; |
| 960 | } |
| 961 | |
| 962 | bool QuicConnection::OnAckFrameEnd(QuicPacketNumber start) { |
| 963 | DCHECK(connected_); |
| 964 | QUIC_DVLOG(1) << ENDPOINT << "OnAckFrameEnd, start: " << start; |
| 965 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 966 | if (GetLargestReceivedPacketWithAck().IsInitialized() && |
| 967 | last_header_.packet_number <= GetLargestReceivedPacketWithAck()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 968 | QUIC_DLOG(INFO) << ENDPOINT << "Received an old ack frame: ignoring"; |
| 969 | return true; |
| 970 | } |
fayang | 3eb8221 | 2019-04-16 12:05:46 -0700 | [diff] [blame] | 971 | const AckResult ack_result = sent_packet_manager_.OnAckFrameEnd( |
fayang | f8e918b | 2019-07-16 13:03:16 -0700 | [diff] [blame] | 972 | time_of_last_received_packet_, last_header_.packet_number, |
| 973 | last_decrypted_packet_level_); |
fayang | 3eb8221 | 2019-04-16 12:05:46 -0700 | [diff] [blame] | 974 | if (ack_result != PACKETS_NEWLY_ACKED && |
| 975 | ack_result != NO_PACKETS_NEWLY_ACKED) { |
| 976 | // Error occurred (e.g., this ACK tries to ack packets in wrong packet |
| 977 | // number space), and this would cause the connection to be closed. |
| 978 | QUIC_DLOG(ERROR) << ENDPOINT |
| 979 | << "Error occurred when processing an ACK frame: " |
| 980 | << QuicUtils::AckResultToString(ack_result); |
| 981 | return false; |
| 982 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 983 | // Cancel the send alarm because new packets likely have been acked, which |
| 984 | // may change the congestion window and/or pacing rate. Canceling the alarm |
| 985 | // causes CanWrite to recalculate the next send time. |
| 986 | if (send_alarm_->IsSet()) { |
| 987 | send_alarm_->Cancel(); |
| 988 | } |
| 989 | if (supports_release_time_) { |
| 990 | // Update pace time into future because smoothed RTT is likely updated. |
| 991 | UpdateReleaseTimeIntoFuture(); |
| 992 | } |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 993 | SetLargestReceivedPacketWithAck(last_header_.packet_number); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 994 | // If the incoming ack's packets set expresses missing packets: peer is still |
| 995 | // waiting for a packet lower than a packet that we are no longer planning to |
| 996 | // send. |
| 997 | // If the incoming ack's packets set expresses received packets: peer is still |
| 998 | // acking packets which we never care about. |
| 999 | // Send an ack to raise the high water mark. |
fayang | e8b0cab | 2019-07-17 14:23:07 -0700 | [diff] [blame] | 1000 | bool send_stop_waiting = GetLeastUnacked() > start; |
| 1001 | if (GetQuicReloadableFlag(quic_simplify_stop_waiting) && |
| 1002 | no_stop_waiting_frames_) { |
| 1003 | QUIC_RELOADABLE_FLAG_COUNT(quic_simplify_stop_waiting); |
| 1004 | send_stop_waiting = false; |
| 1005 | } |
fayang | 0391669 | 2019-05-22 17:57:18 -0700 | [diff] [blame] | 1006 | PostProcessAfterAckFrame(send_stop_waiting, |
fayang | 3eb8221 | 2019-04-16 12:05:46 -0700 | [diff] [blame] | 1007 | ack_result == PACKETS_NEWLY_ACKED); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1008 | processing_ack_frame_ = false; |
| 1009 | |
| 1010 | return connected_; |
| 1011 | } |
| 1012 | |
| 1013 | bool QuicConnection::OnStopWaitingFrame(const QuicStopWaitingFrame& frame) { |
| 1014 | DCHECK(connected_); |
| 1015 | |
| 1016 | // Since a stop waiting frame was received, this is not a connectivity probe. |
| 1017 | // A probe only contains a PING and full padding. |
| 1018 | UpdatePacketContent(NOT_PADDED_PING); |
| 1019 | |
| 1020 | if (no_stop_waiting_frames_) { |
| 1021 | return true; |
| 1022 | } |
| 1023 | if (largest_seen_packet_with_stop_waiting_.IsInitialized() && |
| 1024 | last_header_.packet_number <= largest_seen_packet_with_stop_waiting_) { |
| 1025 | QUIC_DLOG(INFO) << ENDPOINT |
| 1026 | << "Received an old stop waiting frame: ignoring"; |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | const char* error = ValidateStopWaitingFrame(frame); |
| 1031 | if (error != nullptr) { |
| 1032 | CloseConnection(QUIC_INVALID_STOP_WAITING_DATA, error, |
| 1033 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | if (debug_visitor_ != nullptr) { |
| 1038 | debug_visitor_->OnStopWaitingFrame(frame); |
| 1039 | } |
| 1040 | |
| 1041 | largest_seen_packet_with_stop_waiting_ = last_header_.packet_number; |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1042 | uber_received_packet_manager_.DontWaitForPacketsBefore( |
| 1043 | last_decrypted_packet_level_, frame.least_unacked); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1044 | return connected_; |
| 1045 | } |
| 1046 | |
| 1047 | bool QuicConnection::OnPaddingFrame(const QuicPaddingFrame& frame) { |
| 1048 | DCHECK(connected_); |
| 1049 | UpdatePacketContent(SECOND_FRAME_IS_PADDING); |
| 1050 | |
| 1051 | if (debug_visitor_ != nullptr) { |
| 1052 | debug_visitor_->OnPaddingFrame(frame); |
| 1053 | } |
| 1054 | return true; |
| 1055 | } |
| 1056 | |
| 1057 | bool QuicConnection::OnPingFrame(const QuicPingFrame& frame) { |
| 1058 | DCHECK(connected_); |
| 1059 | UpdatePacketContent(FIRST_FRAME_IS_PING); |
| 1060 | |
| 1061 | if (debug_visitor_ != nullptr) { |
| 1062 | debug_visitor_->OnPingFrame(frame); |
| 1063 | } |
| 1064 | should_last_packet_instigate_acks_ = true; |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1068 | const char* QuicConnection::ValidateStopWaitingFrame( |
| 1069 | const QuicStopWaitingFrame& stop_waiting) { |
QUICHE team | b23daa7 | 2019-03-21 08:37:48 -0700 | [diff] [blame] | 1070 | const QuicPacketNumber peer_least_packet_awaiting_ack = |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1071 | uber_received_packet_manager_.peer_least_packet_awaiting_ack(); |
QUICHE team | b23daa7 | 2019-03-21 08:37:48 -0700 | [diff] [blame] | 1072 | if (peer_least_packet_awaiting_ack.IsInitialized() && |
| 1073 | stop_waiting.least_unacked < peer_least_packet_awaiting_ack) { |
| 1074 | QUIC_DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " |
| 1075 | << stop_waiting.least_unacked << " vs " |
| 1076 | << peer_least_packet_awaiting_ack; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1077 | // We never process old ack frames, so this number should only increase. |
| 1078 | return "Least unacked too small."; |
| 1079 | } |
| 1080 | |
| 1081 | if (stop_waiting.least_unacked > last_header_.packet_number) { |
| 1082 | QUIC_DLOG(ERROR) << ENDPOINT |
| 1083 | << "Peer sent least_unacked:" << stop_waiting.least_unacked |
| 1084 | << " greater than the enclosing packet number:" |
| 1085 | << last_header_.packet_number; |
| 1086 | return "Least unacked too large."; |
| 1087 | } |
| 1088 | |
| 1089 | return nullptr; |
| 1090 | } |
| 1091 | |
| 1092 | bool QuicConnection::OnRstStreamFrame(const QuicRstStreamFrame& frame) { |
| 1093 | DCHECK(connected_); |
| 1094 | |
| 1095 | // Since a reset stream frame was received, this is not a connectivity probe. |
| 1096 | // A probe only contains a PING and full padding. |
| 1097 | UpdatePacketContent(NOT_PADDED_PING); |
| 1098 | |
| 1099 | if (debug_visitor_ != nullptr) { |
| 1100 | debug_visitor_->OnRstStreamFrame(frame); |
| 1101 | } |
| 1102 | QUIC_DLOG(INFO) << ENDPOINT |
| 1103 | << "RST_STREAM_FRAME received for stream: " << frame.stream_id |
| 1104 | << " with error: " |
| 1105 | << QuicRstStreamErrorCodeToString(frame.error_code); |
| 1106 | visitor_->OnRstStream(frame); |
| 1107 | should_last_packet_instigate_acks_ = true; |
| 1108 | return connected_; |
| 1109 | } |
| 1110 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1111 | bool QuicConnection::OnStopSendingFrame(const QuicStopSendingFrame& frame) { |
| 1112 | DCHECK(connected_); |
| 1113 | |
| 1114 | // Since a reset stream frame was received, this is not a connectivity probe. |
| 1115 | // A probe only contains a PING and full padding. |
| 1116 | UpdatePacketContent(NOT_PADDED_PING); |
| 1117 | |
| 1118 | if (debug_visitor_ != nullptr) { |
| 1119 | debug_visitor_->OnStopSendingFrame(frame); |
| 1120 | } |
| 1121 | |
| 1122 | QUIC_DLOG(INFO) << ENDPOINT << "STOP_SENDING frame received for stream: " |
| 1123 | << frame.stream_id |
| 1124 | << " with error: " << frame.application_error_code; |
| 1125 | |
| 1126 | visitor_->OnStopSendingFrame(frame); |
| 1127 | return connected_; |
| 1128 | } |
| 1129 | |
| 1130 | bool QuicConnection::OnPathChallengeFrame(const QuicPathChallengeFrame& frame) { |
| 1131 | // Save the path challenge's payload, for later use in generating the |
| 1132 | // response. |
| 1133 | received_path_challenge_payloads_.push_back(frame.data_buffer); |
| 1134 | |
| 1135 | // For VERSION 99 we define a "Padded PATH CHALLENGE" to be the same thing |
| 1136 | // as a PADDED PING -- it will start a connectivity check and prevent |
| 1137 | // connection migration. Insofar as the connectivity check and connection |
| 1138 | // migration are concerned, logically the PATH CHALLENGE is the same as the |
| 1139 | // PING, so as a stopgap, tell the FSM that determines whether we have a |
| 1140 | // Padded PING or not that we received a PING. |
| 1141 | UpdatePacketContent(FIRST_FRAME_IS_PING); |
| 1142 | should_last_packet_instigate_acks_ = true; |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | bool QuicConnection::OnPathResponseFrame(const QuicPathResponseFrame& frame) { |
| 1147 | should_last_packet_instigate_acks_ = true; |
| 1148 | if (!transmitted_connectivity_probe_payload_ || |
| 1149 | *transmitted_connectivity_probe_payload_ != frame.data_buffer) { |
| 1150 | // Is not for the probe we sent, ignore it. |
| 1151 | return true; |
| 1152 | } |
| 1153 | // Have received the matching PATH RESPONSE, saved payload no longer valid. |
| 1154 | transmitted_connectivity_probe_payload_ = nullptr; |
| 1155 | UpdatePacketContent(FIRST_FRAME_IS_PING); |
| 1156 | return true; |
| 1157 | } |
| 1158 | |
| 1159 | bool QuicConnection::OnConnectionCloseFrame( |
| 1160 | const QuicConnectionCloseFrame& frame) { |
| 1161 | DCHECK(connected_); |
| 1162 | |
| 1163 | // Since a connection close frame was received, this is not a connectivity |
| 1164 | // probe. A probe only contains a PING and full padding. |
| 1165 | UpdatePacketContent(NOT_PADDED_PING); |
| 1166 | |
| 1167 | if (debug_visitor_ != nullptr) { |
| 1168 | debug_visitor_->OnConnectionCloseFrame(frame); |
| 1169 | } |
| 1170 | QUIC_DLOG(INFO) << ENDPOINT << "Received ConnectionClose for connection: " |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 1171 | << connection_id() << ", with error: " |
| 1172 | << QuicErrorCodeToString(frame.quic_error_code) << " (" |
| 1173 | << frame.error_details << ")"; |
| 1174 | if (frame.close_type == GOOGLE_QUIC_CONNECTION_CLOSE && |
| 1175 | frame.quic_error_code == QUIC_BAD_MULTIPATH_FLAG) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1176 | QUIC_LOG_FIRST_N(ERROR, 10) << "Unexpected QUIC_BAD_MULTIPATH_FLAG error." |
| 1177 | << " last_received_header: " << last_header_ |
| 1178 | << " encryption_level: " << encryption_level_; |
| 1179 | } |
fkastenholz | e9d71a8 | 2019-04-09 05:12:13 -0700 | [diff] [blame] | 1180 | TearDownLocalConnectionState(frame.quic_error_code, frame.error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1181 | ConnectionCloseSource::FROM_PEER); |
| 1182 | return connected_; |
| 1183 | } |
| 1184 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1185 | bool QuicConnection::OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) { |
| 1186 | return visitor_->OnMaxStreamsFrame(frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1187 | } |
| 1188 | |
fkastenholz | 3c4eabf | 2019-04-22 07:49:59 -0700 | [diff] [blame] | 1189 | bool QuicConnection::OnStreamsBlockedFrame( |
| 1190 | const QuicStreamsBlockedFrame& frame) { |
| 1191 | return visitor_->OnStreamsBlockedFrame(frame); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { |
| 1195 | DCHECK(connected_); |
| 1196 | |
| 1197 | // Since a go away frame was received, this is not a connectivity probe. |
| 1198 | // A probe only contains a PING and full padding. |
| 1199 | UpdatePacketContent(NOT_PADDED_PING); |
| 1200 | |
| 1201 | if (debug_visitor_ != nullptr) { |
| 1202 | debug_visitor_->OnGoAwayFrame(frame); |
| 1203 | } |
| 1204 | QUIC_DLOG(INFO) << ENDPOINT << "GOAWAY_FRAME received with last good stream: " |
| 1205 | << frame.last_good_stream_id |
| 1206 | << " and error: " << QuicErrorCodeToString(frame.error_code) |
| 1207 | << " and reason: " << frame.reason_phrase; |
| 1208 | |
| 1209 | visitor_->OnGoAway(frame); |
| 1210 | should_last_packet_instigate_acks_ = true; |
| 1211 | return connected_; |
| 1212 | } |
| 1213 | |
| 1214 | bool QuicConnection::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) { |
| 1215 | DCHECK(connected_); |
| 1216 | |
| 1217 | // Since a window update frame was received, this is not a connectivity probe. |
| 1218 | // A probe only contains a PING and full padding. |
| 1219 | UpdatePacketContent(NOT_PADDED_PING); |
| 1220 | |
| 1221 | if (debug_visitor_ != nullptr) { |
| 1222 | debug_visitor_->OnWindowUpdateFrame(frame, time_of_last_received_packet_); |
| 1223 | } |
| 1224 | QUIC_DLOG(INFO) << ENDPOINT << "WINDOW_UPDATE_FRAME received for stream: " |
| 1225 | << frame.stream_id |
| 1226 | << " with byte offset: " << frame.byte_offset; |
| 1227 | visitor_->OnWindowUpdateFrame(frame); |
| 1228 | should_last_packet_instigate_acks_ = true; |
| 1229 | return connected_; |
| 1230 | } |
| 1231 | |
| 1232 | bool QuicConnection::OnNewConnectionIdFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1233 | const QuicNewConnectionIdFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
| 1236 | |
| 1237 | bool QuicConnection::OnRetireConnectionIdFrame( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1238 | const QuicRetireConnectionIdFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1239 | return true; |
| 1240 | } |
| 1241 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1242 | bool QuicConnection::OnNewTokenFrame(const QuicNewTokenFrame& /*frame*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | bool QuicConnection::OnMessageFrame(const QuicMessageFrame& frame) { |
| 1247 | DCHECK(connected_); |
| 1248 | |
| 1249 | // Since a message frame was received, this is not a connectivity probe. |
| 1250 | // A probe only contains a PING and full padding. |
| 1251 | UpdatePacketContent(NOT_PADDED_PING); |
| 1252 | |
| 1253 | if (debug_visitor_ != nullptr) { |
| 1254 | debug_visitor_->OnMessageFrame(frame); |
| 1255 | } |
| 1256 | visitor_->OnMessageReceived( |
| 1257 | QuicStringPiece(frame.data, frame.message_length)); |
| 1258 | should_last_packet_instigate_acks_ = true; |
| 1259 | return connected_; |
| 1260 | } |
| 1261 | |
| 1262 | bool QuicConnection::OnBlockedFrame(const QuicBlockedFrame& frame) { |
| 1263 | DCHECK(connected_); |
| 1264 | |
| 1265 | // Since a blocked frame was received, this is not a connectivity probe. |
| 1266 | // A probe only contains a PING and full padding. |
| 1267 | UpdatePacketContent(NOT_PADDED_PING); |
| 1268 | |
| 1269 | if (debug_visitor_ != nullptr) { |
| 1270 | debug_visitor_->OnBlockedFrame(frame); |
| 1271 | } |
| 1272 | QUIC_DLOG(INFO) << ENDPOINT |
| 1273 | << "BLOCKED_FRAME received for stream: " << frame.stream_id; |
| 1274 | visitor_->OnBlockedFrame(frame); |
| 1275 | stats_.blocked_frames_received++; |
| 1276 | should_last_packet_instigate_acks_ = true; |
| 1277 | return connected_; |
| 1278 | } |
| 1279 | |
| 1280 | void QuicConnection::OnPacketComplete() { |
| 1281 | // Don't do anything if this packet closed the connection. |
| 1282 | if (!connected_) { |
| 1283 | ClearLastFrames(); |
| 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | if (IsCurrentPacketConnectivityProbing()) { |
| 1288 | ++stats_.num_connectivity_probing_received; |
| 1289 | } |
| 1290 | |
| 1291 | QUIC_DVLOG(1) << ENDPOINT << "Got packet " << last_header_.packet_number |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1292 | << " for " |
| 1293 | << GetServerConnectionIdAsRecipient(last_header_, perspective_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1294 | |
| 1295 | QUIC_DLOG_IF(INFO, current_packet_content_ == SECOND_FRAME_IS_PADDING) |
| 1296 | << ENDPOINT << "Received a padded PING packet. is_probing: " |
| 1297 | << IsCurrentPacketConnectivityProbing(); |
| 1298 | |
| 1299 | if (perspective_ == Perspective::IS_CLIENT) { |
| 1300 | QUIC_DVLOG(1) << ENDPOINT |
| 1301 | << "Received a speculative connectivity probing packet for " |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1302 | << GetServerConnectionIdAsRecipient(last_header_, |
| 1303 | perspective_) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1304 | << " from ip:port: " << last_packet_source_address_.ToString() |
| 1305 | << " to ip:port: " |
| 1306 | << last_packet_destination_address_.ToString(); |
| 1307 | // TODO(zhongyi): change the method name. |
| 1308 | visitor_->OnConnectivityProbeReceived(last_packet_destination_address_, |
| 1309 | last_packet_source_address_); |
| 1310 | } else if (IsCurrentPacketConnectivityProbing()) { |
| 1311 | // This node is not a client (is a server) AND the received packet was |
| 1312 | // connectivity-probing, send an appropriate response. |
| 1313 | QUIC_DVLOG(1) << ENDPOINT << "Received a connectivity probing packet for " |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 1314 | << GetServerConnectionIdAsRecipient(last_header_, |
| 1315 | perspective_) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1316 | << " from ip:port: " << last_packet_source_address_.ToString() |
| 1317 | << " to ip:port: " |
| 1318 | << last_packet_destination_address_.ToString(); |
| 1319 | visitor_->OnConnectivityProbeReceived(last_packet_destination_address_, |
| 1320 | last_packet_source_address_); |
| 1321 | } else { |
| 1322 | // This node is not a client (is a server) AND the received packet was |
| 1323 | // NOT connectivity-probing. If the packet had PATH CHALLENGES, send |
| 1324 | // appropriate RESPONSE. Then deal with possible peer migration. |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 1325 | if (VersionHasIetfQuicFrames(transport_version()) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1326 | !received_path_challenge_payloads_.empty()) { |
| 1327 | // If a PATH CHALLENGE was in a "Padded PING (or PATH CHALLENGE)" |
| 1328 | // then it is taken care of above. This handles the case where a PATH |
| 1329 | // CHALLENGE appeared someplace else (eg, the peer randomly added a PATH |
| 1330 | // CHALLENGE frame to some other packet. |
| 1331 | // There was at least one PATH CHALLENGE in the received packet, |
| 1332 | // Generate the required PATH RESPONSE. |
| 1333 | SendGenericPathProbePacket(nullptr, last_packet_source_address_, |
| 1334 | /* is_response= */ true); |
| 1335 | } |
| 1336 | |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 1337 | if (last_header_.packet_number == GetLargestReceivedPacket()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1338 | direct_peer_address_ = last_packet_source_address_; |
| 1339 | if (current_effective_peer_migration_type_ != NO_CHANGE) { |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 1340 | // TODO(fayang): When multiple packet number spaces is supported, only |
| 1341 | // start peer migration for the application data. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1342 | StartEffectivePeerMigration(current_effective_peer_migration_type_); |
| 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | current_effective_peer_migration_type_ = NO_CHANGE; |
| 1348 | |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1349 | // Some encryption levels share a packet number space, it is therefore |
| 1350 | // possible for us to want to ack some packets even though we do not yet |
| 1351 | // have the appropriate keys to encrypt the acks. In this scenario we |
| 1352 | // do not update the ACK timeout. This can happen for example with |
| 1353 | // IETF QUIC on the server when we receive 0-RTT packets and do not yet |
| 1354 | // have 1-RTT keys (0-RTT packets are acked at the 1-RTT level). |
| 1355 | // Note that this could cause slight performance degradations in the edge |
| 1356 | // case where one packet is received, then the encrypter is installed, |
| 1357 | // then a second packet is received; as that could cause the ACK for the |
| 1358 | // second packet to be delayed instead of immediate. This is currently |
| 1359 | // considered to be small enough of an edge case to not be optimized for. |
| 1360 | if (!SupportsMultiplePacketNumberSpaces() || |
| 1361 | framer_.HasEncrypterOfEncryptionLevel(QuicUtils::GetEncryptionLevel( |
| 1362 | QuicUtils::GetPacketNumberSpace(last_decrypted_packet_level_)))) { |
| 1363 | uber_received_packet_manager_.MaybeUpdateAckTimeout( |
| 1364 | should_last_packet_instigate_acks_, last_decrypted_packet_level_, |
| 1365 | last_header_.packet_number, time_of_last_received_packet_, |
ianswett | 309987e | 2019-08-02 13:16:26 -0700 | [diff] [blame] | 1366 | clock_->ApproximateNow(), sent_packet_manager_.GetRttStats()); |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1367 | } else { |
| 1368 | QUIC_DLOG(INFO) << ENDPOINT << "Not updating ACK timeout for " |
| 1369 | << QuicUtils::EncryptionLevelToString( |
| 1370 | last_decrypted_packet_level_) |
| 1371 | << " as we do not have the corresponding encrypter"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | ClearLastFrames(); |
| 1375 | CloseIfTooManyOutstandingSentPackets(); |
| 1376 | } |
| 1377 | |
| 1378 | bool QuicConnection::IsValidStatelessResetToken(QuicUint128 token) const { |
| 1379 | return stateless_reset_token_received_ && |
| 1380 | token == received_stateless_reset_token_; |
| 1381 | } |
| 1382 | |
| 1383 | void QuicConnection::OnAuthenticatedIetfStatelessResetPacket( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 1384 | const QuicIetfStatelessResetPacket& /*packet*/) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1385 | // TODO(fayang): Add OnAuthenticatedIetfStatelessResetPacket to |
| 1386 | // debug_visitor_. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 1387 | const std::string error_details = "Received stateless reset."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1388 | QUIC_CODE_COUNT(quic_tear_down_local_connection_on_stateless_reset); |
| 1389 | TearDownLocalConnectionState(QUIC_PUBLIC_RESET, error_details, |
| 1390 | ConnectionCloseSource::FROM_PEER); |
| 1391 | } |
| 1392 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1393 | void QuicConnection::ClearLastFrames() { |
| 1394 | should_last_packet_instigate_acks_ = false; |
| 1395 | } |
| 1396 | |
| 1397 | void QuicConnection::CloseIfTooManyOutstandingSentPackets() { |
| 1398 | // This occurs if we don't discard old packets we've seen fast enough. It's |
| 1399 | // possible largest observed is less than leaset unacked. |
| 1400 | if (sent_packet_manager_.GetLargestObserved().IsInitialized() && |
| 1401 | sent_packet_manager_.GetLargestObserved() > |
| 1402 | sent_packet_manager_.GetLeastUnacked() + max_tracked_packets_) { |
| 1403 | CloseConnection( |
| 1404 | QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS, |
fayang | 75d2396 | 2019-06-03 08:35:49 -0700 | [diff] [blame] | 1405 | QuicStrCat( |
| 1406 | "More than ", max_tracked_packets_, " outstanding, least_unacked: ", |
| 1407 | sent_packet_manager_.GetLeastUnacked().ToUint64(), |
| 1408 | ", packets_processed: ", stats_.packets_processed, |
| 1409 | ", last_decrypted_packet_level: ", |
| 1410 | QuicUtils::EncryptionLevelToString(last_decrypted_packet_level_)), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1411 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | const QuicFrame QuicConnection::GetUpdatedAckFrame() { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1416 | return uber_received_packet_manager_.GetUpdatedAckFrame( |
| 1417 | QuicUtils::GetPacketNumberSpace(encryption_level_), |
| 1418 | clock_->ApproximateNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | void QuicConnection::PopulateStopWaitingFrame( |
| 1422 | QuicStopWaitingFrame* stop_waiting) { |
| 1423 | stop_waiting->least_unacked = GetLeastUnacked(); |
| 1424 | } |
| 1425 | |
| 1426 | QuicPacketNumber QuicConnection::GetLeastUnacked() const { |
| 1427 | return sent_packet_manager_.GetLeastUnacked(); |
| 1428 | } |
| 1429 | |
| 1430 | bool QuicConnection::HandleWriteBlocked() { |
| 1431 | if (!writer_->IsWriteBlocked()) { |
| 1432 | return false; |
| 1433 | } |
| 1434 | |
| 1435 | visitor_->OnWriteBlocked(); |
| 1436 | return true; |
| 1437 | } |
| 1438 | |
| 1439 | void QuicConnection::MaybeSendInResponseToPacket() { |
| 1440 | if (!connected_) { |
| 1441 | return; |
| 1442 | } |
| 1443 | |
| 1444 | // If the writer is blocked, don't attempt to send packets now or in the send |
| 1445 | // alarm. When the writer unblocks, OnCanWrite() will be called for this |
| 1446 | // connection to send. |
| 1447 | if (HandleWriteBlocked()) { |
| 1448 | return; |
| 1449 | } |
| 1450 | |
| 1451 | // Now that we have received an ack, we might be able to send packets which |
| 1452 | // are queued locally, or drain streams which are blocked. |
| 1453 | if (defer_send_in_response_to_packets_) { |
| 1454 | send_alarm_->Update(clock_->ApproximateNow(), QuicTime::Delta::Zero()); |
| 1455 | } else { |
| 1456 | WriteAndBundleAcksIfNotBlocked(); |
| 1457 | } |
| 1458 | } |
| 1459 | |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 1460 | void QuicConnection::SendVersionNegotiationPacket(bool ietf_quic, |
| 1461 | bool has_length_prefix) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1462 | pending_version_negotiation_packet_ = true; |
| 1463 | send_ietf_version_negotiation_packet_ = ietf_quic; |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 1464 | send_version_negotiation_packet_with_prefixed_lengths_ = has_length_prefix; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1465 | |
| 1466 | if (HandleWriteBlocked()) { |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | QUIC_DLOG(INFO) << ENDPOINT << "Sending version negotiation packet: {" |
| 1471 | << ParsedQuicVersionVectorToString( |
| 1472 | framer_.supported_versions()) |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 1473 | << "}, " << (ietf_quic ? "" : "!") << "ietf_quic"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1474 | std::unique_ptr<QuicEncryptedPacket> version_packet( |
| 1475 | packet_generator_.SerializeVersionNegotiationPacket( |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 1476 | ietf_quic, has_length_prefix, framer_.supported_versions())); |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 1477 | QUIC_DVLOG(2) << ENDPOINT << "Sending version negotiation packet: {" |
| 1478 | << ParsedQuicVersionVectorToString(framer_.supported_versions()) |
| 1479 | << "}, " << (ietf_quic ? "" : "!") << "ietf_quic:" << std::endl |
| 1480 | << QuicTextUtils::HexDump(QuicStringPiece( |
| 1481 | version_packet->data(), version_packet->length())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1482 | WriteResult result = writer_->WritePacket( |
| 1483 | version_packet->data(), version_packet->length(), self_address().host(), |
| 1484 | peer_address(), per_packet_options_); |
| 1485 | |
| 1486 | if (IsWriteError(result.status)) { |
| 1487 | OnWriteError(result.error_code); |
| 1488 | return; |
| 1489 | } |
| 1490 | if (IsWriteBlockedStatus(result.status)) { |
| 1491 | visitor_->OnWriteBlocked(); |
| 1492 | if (result.status == WRITE_STATUS_BLOCKED_DATA_BUFFERED) { |
| 1493 | pending_version_negotiation_packet_ = false; |
| 1494 | } |
| 1495 | return; |
| 1496 | } |
| 1497 | |
| 1498 | pending_version_negotiation_packet_ = false; |
| 1499 | } |
| 1500 | |
| 1501 | size_t QuicConnection::SendCryptoData(EncryptionLevel level, |
| 1502 | size_t write_length, |
| 1503 | QuicStreamOffset offset) { |
| 1504 | if (write_length == 0) { |
| 1505 | QUIC_BUG << "Attempt to send empty crypto frame"; |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1509 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1510 | return packet_generator_.ConsumeCryptoData(level, write_length, offset); |
| 1511 | } |
| 1512 | |
| 1513 | QuicConsumedData QuicConnection::SendStreamData(QuicStreamId id, |
| 1514 | size_t write_length, |
| 1515 | QuicStreamOffset offset, |
| 1516 | StreamSendingState state) { |
| 1517 | if (state == NO_FIN && write_length == 0) { |
| 1518 | QUIC_BUG << "Attempt to send empty stream frame"; |
| 1519 | return QuicConsumedData(0, false); |
| 1520 | } |
| 1521 | |
| 1522 | // Opportunistically bundle an ack with every outgoing packet. |
| 1523 | // Particularly, we want to bundle with handshake packets since we don't know |
| 1524 | // which decrypter will be used on an ack packet following a handshake |
| 1525 | // packet (a handshake packet from client to server could result in a REJ or a |
| 1526 | // SHLO from the server, leading to two different decrypters at the server.) |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1527 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1528 | return packet_generator_.ConsumeData(id, write_length, offset, state); |
| 1529 | } |
| 1530 | |
| 1531 | bool QuicConnection::SendControlFrame(const QuicFrame& frame) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1532 | ScopedPacketFlusher flusher(this); |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1533 | const bool consumed = |
| 1534 | packet_generator_.ConsumeRetransmittableControlFrame(frame); |
fayang | 914fbe1 | 2019-07-11 06:23:55 -0700 | [diff] [blame] | 1535 | if (!consumed) { |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 1536 | QUIC_DVLOG(1) << ENDPOINT << "Failed to send control frame: " << frame; |
| 1537 | return false; |
| 1538 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1539 | if (frame.type == PING_FRAME) { |
| 1540 | // Flush PING frame immediately. |
| 1541 | packet_generator_.FlushAllQueuedFrames(); |
| 1542 | if (debug_visitor_ != nullptr) { |
| 1543 | debug_visitor_->OnPingSent(); |
| 1544 | } |
| 1545 | } |
| 1546 | if (frame.type == BLOCKED_FRAME) { |
| 1547 | stats_.blocked_frames_sent++; |
| 1548 | } |
| 1549 | return true; |
| 1550 | } |
| 1551 | |
| 1552 | void QuicConnection::OnStreamReset(QuicStreamId id, |
| 1553 | QuicRstStreamErrorCode error) { |
| 1554 | if (error == QUIC_STREAM_NO_ERROR) { |
| 1555 | // All data for streams which are reset with QUIC_STREAM_NO_ERROR must |
| 1556 | // be received by the peer. |
| 1557 | return; |
| 1558 | } |
| 1559 | // Flush stream frames of reset stream. |
| 1560 | if (packet_generator_.HasPendingStreamFramesOfStream(id)) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1561 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1562 | packet_generator_.FlushAllQueuedFrames(); |
| 1563 | } |
| 1564 | |
| 1565 | sent_packet_manager_.CancelRetransmissionsForStream(id); |
| 1566 | // Remove all queued packets which only contain data for the reset stream. |
| 1567 | // TODO(fayang): consider removing this because it should be rarely executed. |
| 1568 | auto packet_iterator = queued_packets_.begin(); |
| 1569 | while (packet_iterator != queued_packets_.end()) { |
| 1570 | QuicFrames* retransmittable_frames = |
| 1571 | &packet_iterator->retransmittable_frames; |
| 1572 | if (retransmittable_frames->empty()) { |
| 1573 | ++packet_iterator; |
| 1574 | continue; |
| 1575 | } |
| 1576 | // NOTE THAT RemoveFramesForStream removes only STREAM frames |
| 1577 | // for the specified stream. |
| 1578 | RemoveFramesForStream(retransmittable_frames, id); |
| 1579 | if (!retransmittable_frames->empty()) { |
| 1580 | ++packet_iterator; |
| 1581 | continue; |
| 1582 | } |
| 1583 | delete[] packet_iterator->encrypted_buffer; |
| 1584 | ClearSerializedPacket(&(*packet_iterator)); |
| 1585 | packet_iterator = queued_packets_.erase(packet_iterator); |
| 1586 | } |
| 1587 | // TODO(ianswett): Consider checking for 3 RTOs when the last stream is |
| 1588 | // cancelled as well. |
| 1589 | } |
| 1590 | |
| 1591 | const QuicConnectionStats& QuicConnection::GetStats() { |
| 1592 | const RttStats* rtt_stats = sent_packet_manager_.GetRttStats(); |
| 1593 | |
| 1594 | // Update rtt and estimated bandwidth. |
| 1595 | QuicTime::Delta min_rtt = rtt_stats->min_rtt(); |
| 1596 | if (min_rtt.IsZero()) { |
| 1597 | // If min RTT has not been set, use initial RTT instead. |
| 1598 | min_rtt = rtt_stats->initial_rtt(); |
| 1599 | } |
| 1600 | stats_.min_rtt_us = min_rtt.ToMicroseconds(); |
| 1601 | |
| 1602 | QuicTime::Delta srtt = rtt_stats->SmoothedOrInitialRtt(); |
| 1603 | stats_.srtt_us = srtt.ToMicroseconds(); |
| 1604 | |
| 1605 | stats_.estimated_bandwidth = sent_packet_manager_.BandwidthEstimate(); |
| 1606 | stats_.max_packet_size = packet_generator_.GetCurrentMaxPacketLength(); |
| 1607 | stats_.max_received_packet_size = largest_received_packet_size_; |
| 1608 | return stats_; |
| 1609 | } |
| 1610 | |
| 1611 | void QuicConnection::OnCoalescedPacket(const QuicEncryptedPacket& packet) { |
| 1612 | QueueCoalescedPacket(packet); |
| 1613 | } |
| 1614 | |
| 1615 | void QuicConnection::ProcessUdpPacket(const QuicSocketAddress& self_address, |
| 1616 | const QuicSocketAddress& peer_address, |
| 1617 | const QuicReceivedPacket& packet) { |
| 1618 | if (!connected_) { |
| 1619 | return; |
| 1620 | } |
dschinazi | d9467b5 | 2019-04-03 16:47:08 -0700 | [diff] [blame] | 1621 | QUIC_DVLOG(2) << ENDPOINT << "Received encrypted " << packet.length() |
| 1622 | << " bytes:" << std::endl |
| 1623 | << QuicTextUtils::HexDump( |
| 1624 | QuicStringPiece(packet.data(), packet.length())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1625 | QUIC_BUG_IF(current_packet_data_ != nullptr) |
| 1626 | << "ProcessUdpPacket must not be called while processing a packet."; |
| 1627 | if (debug_visitor_ != nullptr) { |
| 1628 | debug_visitor_->OnPacketReceived(self_address, peer_address, packet); |
| 1629 | } |
| 1630 | last_size_ = packet.length(); |
| 1631 | current_packet_data_ = packet.data(); |
| 1632 | |
| 1633 | last_packet_destination_address_ = self_address; |
| 1634 | last_packet_source_address_ = peer_address; |
| 1635 | if (!self_address_.IsInitialized()) { |
| 1636 | self_address_ = last_packet_destination_address_; |
| 1637 | } |
| 1638 | |
| 1639 | if (!direct_peer_address_.IsInitialized()) { |
| 1640 | direct_peer_address_ = last_packet_source_address_; |
| 1641 | } |
| 1642 | |
| 1643 | if (!effective_peer_address_.IsInitialized()) { |
| 1644 | const QuicSocketAddress effective_peer_addr = |
| 1645 | GetEffectivePeerAddressFromCurrentPacket(); |
| 1646 | |
| 1647 | // effective_peer_address_ must be initialized at the beginning of the |
| 1648 | // first packet processed(here). If effective_peer_addr is uninitialized, |
| 1649 | // just set effective_peer_address_ to the direct peer address. |
| 1650 | effective_peer_address_ = effective_peer_addr.IsInitialized() |
| 1651 | ? effective_peer_addr |
| 1652 | : direct_peer_address_; |
| 1653 | } |
| 1654 | |
| 1655 | stats_.bytes_received += packet.length(); |
| 1656 | ++stats_.packets_received; |
| 1657 | |
| 1658 | // Ensure the time coming from the packet reader is within 2 minutes of now. |
| 1659 | if (std::abs((packet.receipt_time() - clock_->ApproximateNow()).ToSeconds()) > |
| 1660 | 2 * 60) { |
| 1661 | QUIC_BUG << "Packet receipt time:" |
| 1662 | << packet.receipt_time().ToDebuggingValue() |
| 1663 | << " too far from current time:" |
| 1664 | << clock_->ApproximateNow().ToDebuggingValue(); |
| 1665 | } |
| 1666 | time_of_last_received_packet_ = packet.receipt_time(); |
| 1667 | QUIC_DVLOG(1) << ENDPOINT << "time of last received packet: " |
| 1668 | << time_of_last_received_packet_.ToDebuggingValue(); |
| 1669 | |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1670 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1671 | if (!framer_.ProcessPacket(packet)) { |
| 1672 | // If we are unable to decrypt this packet, it might be |
| 1673 | // because the CHLO or SHLO packet was lost. |
| 1674 | if (framer_.error() == QUIC_DECRYPTION_FAILURE) { |
dschinazi | 6ece500 | 2019-05-22 06:35:49 -0700 | [diff] [blame] | 1675 | ++stats_.undecryptable_packets_received; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1676 | if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && |
| 1677 | undecryptable_packets_.size() < max_undecryptable_packets_) { |
| 1678 | QueueUndecryptablePacket(packet); |
| 1679 | } else if (debug_visitor_ != nullptr) { |
| 1680 | debug_visitor_->OnUndecryptablePacket(); |
| 1681 | } |
| 1682 | } |
| 1683 | QUIC_DVLOG(1) << ENDPOINT |
| 1684 | << "Unable to process packet. Last packet processed: " |
| 1685 | << last_header_.packet_number; |
| 1686 | current_packet_data_ = nullptr; |
| 1687 | is_current_packet_connectivity_probing_ = false; |
| 1688 | |
| 1689 | MaybeProcessCoalescedPackets(); |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | ++stats_.packets_processed; |
| 1694 | |
| 1695 | QUIC_DLOG_IF(INFO, active_effective_peer_migration_type_ != NO_CHANGE) |
| 1696 | << "sent_packet_manager_.GetLargestObserved() = " |
| 1697 | << sent_packet_manager_.GetLargestObserved() |
| 1698 | << ", highest_packet_sent_before_effective_peer_migration_ = " |
| 1699 | << highest_packet_sent_before_effective_peer_migration_; |
| 1700 | if (active_effective_peer_migration_type_ != NO_CHANGE && |
| 1701 | sent_packet_manager_.GetLargestObserved().IsInitialized() && |
| 1702 | (!highest_packet_sent_before_effective_peer_migration_.IsInitialized() || |
| 1703 | sent_packet_manager_.GetLargestObserved() > |
| 1704 | highest_packet_sent_before_effective_peer_migration_)) { |
| 1705 | if (perspective_ == Perspective::IS_SERVER) { |
| 1706 | OnEffectivePeerMigrationValidated(); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | MaybeProcessCoalescedPackets(); |
| 1711 | MaybeProcessUndecryptablePackets(); |
| 1712 | MaybeSendInResponseToPacket(); |
| 1713 | SetPingAlarm(); |
| 1714 | current_packet_data_ = nullptr; |
| 1715 | is_current_packet_connectivity_probing_ = false; |
| 1716 | } |
| 1717 | |
| 1718 | void QuicConnection::OnBlockedWriterCanWrite() { |
| 1719 | writer_->SetWritable(); |
| 1720 | OnCanWrite(); |
| 1721 | } |
| 1722 | |
| 1723 | void QuicConnection::OnCanWrite() { |
fayang | 0f0c4e6 | 2019-07-16 08:55:54 -0700 | [diff] [blame] | 1724 | if (!connected_) { |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 1725 | return; |
| 1726 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1727 | DCHECK(!writer_->IsWriteBlocked()); |
| 1728 | |
| 1729 | // Add a flusher to ensure the connection is marked app-limited. |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1730 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1731 | |
| 1732 | WriteQueuedPackets(); |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 1733 | const QuicTime ack_timeout = |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1734 | uber_received_packet_manager_.GetEarliestAckTimeout(); |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 1735 | if (ack_timeout.IsInitialized() && ack_timeout <= clock_->ApproximateNow()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1736 | // Send an ACK now because either 1) we were write blocked when we last |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 1737 | // tried to send an ACK, or 2) both ack alarm and send alarm were set to |
| 1738 | // go off together. |
| 1739 | if (SupportsMultiplePacketNumberSpaces()) { |
| 1740 | SendAllPendingAcks(); |
| 1741 | } else { |
| 1742 | SendAck(); |
| 1743 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1744 | } |
| 1745 | if (!session_decides_what_to_write()) { |
| 1746 | WritePendingRetransmissions(); |
| 1747 | } |
| 1748 | |
| 1749 | WriteNewData(); |
| 1750 | } |
| 1751 | |
| 1752 | void QuicConnection::WriteNewData() { |
| 1753 | // Sending queued packets may have caused the socket to become write blocked, |
| 1754 | // or the congestion manager to prohibit sending. If we've sent everything |
| 1755 | // we had queued and we're still not blocked, let the visitor know it can |
| 1756 | // write more. |
| 1757 | if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 1758 | return; |
| 1759 | } |
| 1760 | |
| 1761 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1762 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1763 | visitor_->OnCanWrite(); |
| 1764 | } |
| 1765 | |
| 1766 | // After the visitor writes, it may have caused the socket to become write |
| 1767 | // blocked or the congestion manager to prohibit sending, so check again. |
| 1768 | if (visitor_->WillingAndAbleToWrite() && !send_alarm_->IsSet() && |
| 1769 | CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 1770 | // We're not write blocked, but some stream didn't write out all of its |
| 1771 | // bytes. Register for 'immediate' resumption so we'll keep writing after |
| 1772 | // other connections and events have had a chance to use the thread. |
| 1773 | send_alarm_->Set(clock_->ApproximateNow()); |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | void QuicConnection::WriteIfNotBlocked() { |
| 1778 | if (!HandleWriteBlocked()) { |
| 1779 | OnCanWrite(); |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | void QuicConnection::WriteAndBundleAcksIfNotBlocked() { |
| 1784 | if (!HandleWriteBlocked()) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1785 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1786 | WriteIfNotBlocked(); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | bool QuicConnection::ProcessValidatedPacket(const QuicPacketHeader& header) { |
| 1791 | if (perspective_ == Perspective::IS_SERVER && self_address_.IsInitialized() && |
| 1792 | last_packet_destination_address_.IsInitialized() && |
| 1793 | self_address_ != last_packet_destination_address_) { |
| 1794 | // Allow change between pure IPv4 and equivalent mapped IPv4 address. |
| 1795 | if (self_address_.port() != last_packet_destination_address_.port() || |
| 1796 | self_address_.host().Normalized() != |
| 1797 | last_packet_destination_address_.host().Normalized()) { |
| 1798 | if (!visitor_->AllowSelfAddressChange()) { |
| 1799 | CloseConnection( |
| 1800 | QUIC_ERROR_MIGRATING_ADDRESS, |
| 1801 | "Self address migration is not supported at the server.", |
| 1802 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 1803 | return false; |
| 1804 | } |
| 1805 | } |
| 1806 | self_address_ = last_packet_destination_address_; |
| 1807 | } |
| 1808 | |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 1809 | if (PacketCanReplaceConnectionId(header, perspective_) && |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 1810 | server_connection_id_ != header.source_connection_id) { |
| 1811 | QUIC_DLOG(INFO) << ENDPOINT << "Replacing connection ID " |
| 1812 | << server_connection_id_ << " with " |
| 1813 | << header.source_connection_id; |
| 1814 | server_connection_id_ = header.source_connection_id; |
| 1815 | packet_generator_.SetServerConnectionId(server_connection_id_); |
QUICHE team | c65d1d1 | 2019-03-19 20:58:04 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
QUICHE team | d791e2c | 2019-03-15 10:28:21 -0700 | [diff] [blame] | 1818 | if (!ValidateReceivedPacketNumber(header.packet_number)) { |
| 1819 | return false; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1820 | } |
| 1821 | |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 1822 | if (!version_negotiated_) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1823 | if (perspective_ == Perspective::IS_CLIENT) { |
| 1824 | DCHECK(!header.version_flag || header.form != GOOGLE_QUIC_PACKET); |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 1825 | if (!VersionHasIetfInvariantHeader(framer_.transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1826 | // If the client gets a packet without the version flag from the server |
| 1827 | // it should stop sending version since the version negotiation is done. |
| 1828 | // IETF QUIC stops sending version once encryption level switches to |
| 1829 | // forward secure. |
| 1830 | packet_generator_.StopSendingVersion(); |
| 1831 | } |
fayang | 8aba1ff | 2019-06-21 12:00:54 -0700 | [diff] [blame] | 1832 | version_negotiated_ = true; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1833 | visitor_->OnSuccessfulVersionNegotiation(version()); |
| 1834 | if (debug_visitor_ != nullptr) { |
| 1835 | debug_visitor_->OnSuccessfulVersionNegotiation(version()); |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | if (last_size_ > largest_received_packet_size_) { |
| 1841 | largest_received_packet_size_ = last_size_; |
| 1842 | } |
| 1843 | |
| 1844 | if (perspective_ == Perspective::IS_SERVER && |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 1845 | encryption_level_ == ENCRYPTION_INITIAL && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1846 | last_size_ > packet_generator_.GetCurrentMaxPacketLength()) { |
| 1847 | SetMaxPacketLength(last_size_); |
| 1848 | } |
| 1849 | return true; |
| 1850 | } |
| 1851 | |
QUICHE team | d791e2c | 2019-03-15 10:28:21 -0700 | [diff] [blame] | 1852 | bool QuicConnection::ValidateReceivedPacketNumber( |
| 1853 | QuicPacketNumber packet_number) { |
fayang | 08878bd | 2019-06-20 09:21:43 -0700 | [diff] [blame] | 1854 | // If this packet has already been seen, or the sender has told us that it |
| 1855 | // will not be retransmitted, then stop processing the packet. |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1856 | if (!uber_received_packet_manager_.IsAwaitingPacket( |
| 1857 | last_decrypted_packet_level_, packet_number)) { |
| 1858 | QUIC_DLOG(INFO) << ENDPOINT << "Packet " << packet_number |
| 1859 | << " no longer being waited for at level " |
| 1860 | << static_cast<int>(last_decrypted_packet_level_) |
| 1861 | << ". Discarding."; |
fayang | 08878bd | 2019-06-20 09:21:43 -0700 | [diff] [blame] | 1862 | if (debug_visitor_ != nullptr) { |
| 1863 | debug_visitor_->OnDuplicatePacket(packet_number); |
| 1864 | } |
| 1865 | return false; |
QUICHE team | 692750b | 2019-03-17 17:57:46 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
fayang | 6dba490 | 2019-06-17 10:04:23 -0700 | [diff] [blame] | 1868 | return true; |
QUICHE team | d791e2c | 2019-03-15 10:28:21 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1871 | void QuicConnection::WriteQueuedPackets() { |
| 1872 | DCHECK(!writer_->IsWriteBlocked()); |
| 1873 | |
| 1874 | if (pending_version_negotiation_packet_) { |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 1875 | SendVersionNegotiationPacket( |
| 1876 | send_ietf_version_negotiation_packet_, |
| 1877 | send_version_negotiation_packet_with_prefixed_lengths_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | QUIC_CLIENT_HISTOGRAM_COUNTS("QuicSession.NumQueuedPacketsBeforeWrite", |
| 1881 | queued_packets_.size(), 1, 1000, 50, ""); |
| 1882 | while (!queued_packets_.empty()) { |
| 1883 | // WritePacket() can potentially clear all queued packets, so we need to |
| 1884 | // save the first queued packet to a local variable before calling it. |
| 1885 | SerializedPacket packet(std::move(queued_packets_.front())); |
| 1886 | queued_packets_.pop_front(); |
| 1887 | |
| 1888 | const bool write_result = WritePacket(&packet); |
| 1889 | |
| 1890 | if (connected_ && !write_result) { |
| 1891 | // Write failed but connection is open, re-insert |packet| into the |
| 1892 | // front of the queue, it will be retried later. |
| 1893 | queued_packets_.emplace_front(std::move(packet)); |
| 1894 | break; |
| 1895 | } |
| 1896 | |
| 1897 | delete[] packet.encrypted_buffer; |
| 1898 | ClearSerializedPacket(&packet); |
| 1899 | if (!connected_) { |
| 1900 | DCHECK(queued_packets_.empty()) << "Queued packets should have been " |
| 1901 | "cleared while closing connection"; |
| 1902 | break; |
| 1903 | } |
| 1904 | |
| 1905 | // Continue to send the next packet in queue. |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | void QuicConnection::WritePendingRetransmissions() { |
| 1910 | DCHECK(!session_decides_what_to_write()); |
| 1911 | // Keep writing as long as there's a pending retransmission which can be |
| 1912 | // written. |
| 1913 | while (sent_packet_manager_.HasPendingRetransmissions() && |
| 1914 | CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 1915 | const QuicPendingRetransmission pending = |
| 1916 | sent_packet_manager_.NextPendingRetransmission(); |
| 1917 | |
| 1918 | // Re-packetize the frames with a new packet number for retransmission. |
| 1919 | // Retransmitted packets use the same packet number length as the |
| 1920 | // original. |
| 1921 | // Flush the packet generator before making a new packet. |
| 1922 | // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
| 1923 | // does not require the creator to be flushed. |
| 1924 | // TODO(fayang): FlushAllQueuedFrames should only be called once, and should |
| 1925 | // be moved outside of the loop. Also, CanWrite is not checked after the |
| 1926 | // generator is flushed. |
| 1927 | { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 1928 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1929 | packet_generator_.FlushAllQueuedFrames(); |
| 1930 | } |
fayang | 914fbe1 | 2019-07-11 06:23:55 -0700 | [diff] [blame] | 1931 | DCHECK(!packet_generator_.HasPendingFrames()); |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 1932 | char buffer[kMaxOutgoingPacketSize]; |
| 1933 | packet_generator_.ReserializeAllFrames(pending, buffer, |
| 1934 | kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | void QuicConnection::SendProbingRetransmissions() { |
| 1939 | while (sent_packet_manager_.GetSendAlgorithm()->ShouldSendProbingPacket() && |
| 1940 | CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
QUICHE team | b834325 | 2019-04-29 13:58:01 -0700 | [diff] [blame] | 1941 | if (!visitor_->SendProbingData()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1942 | QUIC_DVLOG(1) |
| 1943 | << "Cannot send probing retransmissions: nothing to retransmit."; |
| 1944 | break; |
| 1945 | } |
| 1946 | |
| 1947 | if (!session_decides_what_to_write()) { |
| 1948 | DCHECK(sent_packet_manager_.HasPendingRetransmissions()); |
| 1949 | WritePendingRetransmissions(); |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | void QuicConnection::RetransmitUnackedPackets( |
| 1955 | TransmissionType retransmission_type) { |
| 1956 | sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); |
| 1957 | |
| 1958 | WriteIfNotBlocked(); |
| 1959 | } |
| 1960 | |
| 1961 | void QuicConnection::NeuterUnencryptedPackets() { |
| 1962 | sent_packet_manager_.NeuterUnencryptedPackets(); |
| 1963 | // This may have changed the retransmission timer, so re-arm it. |
| 1964 | SetRetransmissionAlarm(); |
| 1965 | } |
| 1966 | |
| 1967 | bool QuicConnection::ShouldGeneratePacket( |
| 1968 | HasRetransmittableData retransmittable, |
| 1969 | IsHandshake handshake) { |
| 1970 | // We should serialize handshake packets immediately to ensure that they |
| 1971 | // end up sent at the right encryption level. |
| 1972 | if (handshake == IS_HANDSHAKE) { |
| 1973 | return true; |
| 1974 | } |
| 1975 | |
| 1976 | return CanWrite(retransmittable); |
| 1977 | } |
| 1978 | |
| 1979 | const QuicFrames QuicConnection::MaybeBundleAckOpportunistically() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1980 | QuicFrames frames; |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 1981 | const bool has_pending_ack = |
| 1982 | uber_received_packet_manager_ |
| 1983 | .GetAckTimeout(QuicUtils::GetPacketNumberSpace(encryption_level_)) |
| 1984 | .IsInitialized(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1985 | if (!has_pending_ack && stop_waiting_count_ <= 1) { |
| 1986 | // No need to send an ACK. |
| 1987 | return frames; |
| 1988 | } |
| 1989 | ResetAckStates(); |
| 1990 | |
| 1991 | QUIC_DVLOG(1) << ENDPOINT << "Bundle an ACK opportunistically"; |
dschinazi | 8cf087e | 2019-05-23 05:27:48 -0700 | [diff] [blame] | 1992 | QuicFrame updated_ack_frame = GetUpdatedAckFrame(); |
| 1993 | QUIC_BUG_IF(updated_ack_frame.ack_frame->packets.Empty()) |
| 1994 | << ENDPOINT << "Attempted to opportunistically bundle an empty " |
| 1995 | << QuicUtils::EncryptionLevelToString(encryption_level_) << " ACK, " |
| 1996 | << (has_pending_ack ? "" : "!") << "has_pending_ack, stop_waiting_count_ " |
| 1997 | << stop_waiting_count_; |
| 1998 | frames.push_back(updated_ack_frame); |
| 1999 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2000 | if (!no_stop_waiting_frames_) { |
| 2001 | QuicStopWaitingFrame stop_waiting; |
| 2002 | PopulateStopWaitingFrame(&stop_waiting); |
| 2003 | frames.push_back(QuicFrame(stop_waiting)); |
| 2004 | } |
| 2005 | return frames; |
| 2006 | } |
| 2007 | |
| 2008 | bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) { |
| 2009 | if (!connected_) { |
| 2010 | return false; |
| 2011 | } |
| 2012 | |
| 2013 | if (session_decides_what_to_write() && |
| 2014 | sent_packet_manager_.pending_timer_transmission_count() > 0) { |
| 2015 | // Force sending the retransmissions for HANDSHAKE, TLP, RTO, PROBING cases. |
| 2016 | return true; |
| 2017 | } |
| 2018 | |
| 2019 | if (HandleWriteBlocked()) { |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
| 2023 | // Allow acks to be sent immediately. |
| 2024 | if (retransmittable == NO_RETRANSMITTABLE_DATA) { |
| 2025 | return true; |
| 2026 | } |
| 2027 | // If the send alarm is set, wait for it to fire. |
| 2028 | if (send_alarm_->IsSet()) { |
| 2029 | return false; |
| 2030 | } |
| 2031 | |
| 2032 | QuicTime now = clock_->Now(); |
| 2033 | QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend(now); |
| 2034 | if (delay.IsInfinite()) { |
| 2035 | send_alarm_->Cancel(); |
| 2036 | return false; |
| 2037 | } |
| 2038 | |
| 2039 | // Scheduler requires a delay. |
| 2040 | if (!delay.IsZero()) { |
| 2041 | if (delay <= release_time_into_future_) { |
| 2042 | // Required delay is within pace time into future, send now. |
| 2043 | return true; |
| 2044 | } |
| 2045 | // Cannot send packet now because delay is too far in the future. |
| 2046 | send_alarm_->Update(now + delay, QuicTime::Delta::FromMilliseconds(1)); |
| 2047 | QUIC_DVLOG(1) << ENDPOINT << "Delaying sending " << delay.ToMilliseconds() |
| 2048 | << "ms"; |
| 2049 | return false; |
| 2050 | } |
| 2051 | return true; |
| 2052 | } |
| 2053 | |
| 2054 | bool QuicConnection::WritePacket(SerializedPacket* packet) { |
| 2055 | if (ShouldDiscardPacket(*packet)) { |
| 2056 | ++stats_.packets_discarded; |
| 2057 | return true; |
| 2058 | } |
| 2059 | if (sent_packet_manager_.GetLargestSentPacket().IsInitialized() && |
| 2060 | packet->packet_number < sent_packet_manager_.GetLargestSentPacket()) { |
| 2061 | QUIC_BUG << "Attempt to write packet:" << packet->packet_number |
| 2062 | << " after:" << sent_packet_manager_.GetLargestSentPacket(); |
| 2063 | QUIC_CLIENT_HISTOGRAM_COUNTS("QuicSession.NumQueuedPacketsAtOutOfOrder", |
| 2064 | queued_packets_.size(), 1, 1000, 50, ""); |
| 2065 | CloseConnection(QUIC_INTERNAL_ERROR, "Packet written out of order.", |
| 2066 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 2067 | return true; |
| 2068 | } |
| 2069 | // Termination packets are encrypted and saved, so don't exit early. |
| 2070 | const bool is_termination_packet = IsTerminationPacket(*packet); |
| 2071 | if (HandleWriteBlocked() && !is_termination_packet) { |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| 2075 | QuicPacketNumber packet_number = packet->packet_number; |
| 2076 | |
| 2077 | QuicPacketLength encrypted_length = packet->encrypted_length; |
| 2078 | // Termination packets are eventually owned by TimeWaitListManager. |
| 2079 | // Others are deleted at the end of this call. |
| 2080 | if (is_termination_packet) { |
| 2081 | if (termination_packets_ == nullptr) { |
| 2082 | termination_packets_.reset( |
| 2083 | new std::vector<std::unique_ptr<QuicEncryptedPacket>>); |
| 2084 | } |
| 2085 | // Copy the buffer so it's owned in the future. |
| 2086 | char* buffer_copy = CopyBuffer(*packet); |
| 2087 | termination_packets_->emplace_back( |
| 2088 | new QuicEncryptedPacket(buffer_copy, encrypted_length, true)); |
| 2089 | // This assures we won't try to write *forced* packets when blocked. |
| 2090 | // Return true to stop processing. |
| 2091 | if (HandleWriteBlocked()) { |
| 2092 | return true; |
| 2093 | } |
| 2094 | } |
| 2095 | |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 2096 | DCHECK_LE(encrypted_length, kMaxOutgoingPacketSize); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2097 | DCHECK_LE(encrypted_length, packet_generator_.GetCurrentMaxPacketLength()); |
| 2098 | QUIC_DVLOG(1) << ENDPOINT << "Sending packet " << packet_number << " : " |
| 2099 | << (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA |
| 2100 | ? "data bearing " |
| 2101 | : " ack only ") |
| 2102 | << ", encryption level: " |
| 2103 | << QuicUtils::EncryptionLevelToString(packet->encryption_level) |
| 2104 | << ", encrypted length:" << encrypted_length; |
| 2105 | QUIC_DVLOG(2) << ENDPOINT << "packet(" << packet_number << "): " << std::endl |
| 2106 | << QuicTextUtils::HexDump(QuicStringPiece( |
| 2107 | packet->encrypted_buffer, encrypted_length)); |
| 2108 | |
| 2109 | // Measure the RTT from before the write begins to avoid underestimating the |
| 2110 | // min_rtt_, especially in cases where the thread blocks or gets swapped out |
| 2111 | // during the WritePacket below. |
| 2112 | QuicTime packet_send_time = clock_->Now(); |
| 2113 | if (supports_release_time_ && per_packet_options_ != nullptr) { |
| 2114 | QuicTime next_release_time = sent_packet_manager_.GetNextReleaseTime(); |
| 2115 | QuicTime::Delta release_time_delay = QuicTime::Delta::Zero(); |
| 2116 | QuicTime now = packet_send_time; |
| 2117 | if (next_release_time > now) { |
| 2118 | release_time_delay = next_release_time - now; |
| 2119 | // Set packet_send_time to the future to make the RTT estimation accurate. |
| 2120 | packet_send_time = next_release_time; |
| 2121 | } |
| 2122 | per_packet_options_->release_time_delay = release_time_delay; |
| 2123 | } |
| 2124 | WriteResult result = writer_->WritePacket( |
| 2125 | packet->encrypted_buffer, encrypted_length, self_address().host(), |
| 2126 | peer_address(), per_packet_options_); |
| 2127 | |
| 2128 | QUIC_HISTOGRAM_ENUM( |
| 2129 | "QuicConnection.WritePacketStatus", result.status, |
| 2130 | WRITE_STATUS_NUM_VALUES, |
| 2131 | "Status code returned by writer_->WritePacket() in QuicConnection."); |
| 2132 | |
| 2133 | if (IsWriteBlockedStatus(result.status)) { |
| 2134 | // Ensure the writer is still write blocked, otherwise QUIC may continue |
| 2135 | // trying to write when it will not be able to. |
| 2136 | DCHECK(writer_->IsWriteBlocked()); |
| 2137 | visitor_->OnWriteBlocked(); |
| 2138 | // If the socket buffers the data, then the packet should not |
| 2139 | // be queued and sent again, which would result in an unnecessary |
| 2140 | // duplicate packet being sent. The helper must call OnCanWrite |
| 2141 | // when the write completes, and OnWriteError if an error occurs. |
| 2142 | if (result.status != WRITE_STATUS_BLOCKED_DATA_BUFFERED) { |
| 2143 | return false; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | // In some cases, an MTU probe can cause EMSGSIZE. This indicates that the |
| 2148 | // MTU discovery is permanently unsuccessful. |
| 2149 | if (IsMsgTooBig(result) && packet->retransmittable_frames.empty() && |
| 2150 | packet->encrypted_length > long_term_mtu_) { |
| 2151 | mtu_discovery_target_ = 0; |
| 2152 | mtu_discovery_alarm_->Cancel(); |
| 2153 | // The write failed, but the writer is not blocked, so return true. |
| 2154 | return true; |
| 2155 | } |
| 2156 | |
| 2157 | if (IsWriteError(result.status)) { |
| 2158 | OnWriteError(result.error_code); |
| 2159 | QUIC_LOG_FIRST_N(ERROR, 10) |
| 2160 | << ENDPOINT << "failed writing " << encrypted_length |
| 2161 | << " bytes from host " << self_address().host().ToString() |
| 2162 | << " to address " << peer_address().ToString() << " with error code " |
| 2163 | << result.error_code; |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
| 2167 | if (debug_visitor_ != nullptr) { |
| 2168 | // Pass the write result to the visitor. |
| 2169 | debug_visitor_->OnPacketSent(*packet, packet->original_packet_number, |
| 2170 | packet->transmission_type, packet_send_time); |
| 2171 | } |
| 2172 | if (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA) { |
| 2173 | if (!is_path_degrading_ && !path_degrading_alarm_->IsSet()) { |
| 2174 | // This is the first retransmittable packet on the working path. |
| 2175 | // Start the path degrading alarm to detect new path degrading. |
| 2176 | SetPathDegradingAlarm(); |
| 2177 | } |
| 2178 | |
zhongyi | c1cab06 | 2019-06-19 12:02:24 -0700 | [diff] [blame] | 2179 | // Update |time_of_first_packet_sent_after_receiving_| if this is the |
| 2180 | // first packet sent after the last packet was received. If it were |
| 2181 | // updated on every sent packet, then sending into a black hole might |
| 2182 | // never timeout. |
| 2183 | if (time_of_first_packet_sent_after_receiving_ < |
| 2184 | time_of_last_received_packet_) { |
| 2185 | time_of_first_packet_sent_after_receiving_ = packet_send_time; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | MaybeSetMtuAlarm(packet_number); |
| 2190 | QUIC_DVLOG(1) << ENDPOINT << "time we began writing last sent packet: " |
| 2191 | << packet_send_time.ToDebuggingValue(); |
| 2192 | |
| 2193 | bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( |
| 2194 | packet, packet->original_packet_number, packet_send_time, |
| 2195 | packet->transmission_type, IsRetransmittable(*packet)); |
| 2196 | |
| 2197 | if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
| 2198 | SetRetransmissionAlarm(); |
| 2199 | } |
| 2200 | SetPingAlarm(); |
| 2201 | |
| 2202 | // The packet number length must be updated after OnPacketSent, because it |
| 2203 | // may change the packet number length in packet. |
| 2204 | packet_generator_.UpdatePacketNumberLength( |
| 2205 | sent_packet_manager_.GetLeastUnacked(), |
| 2206 | sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); |
| 2207 | |
| 2208 | stats_.bytes_sent += result.bytes_written; |
| 2209 | ++stats_.packets_sent; |
| 2210 | if (packet->transmission_type != NOT_RETRANSMISSION) { |
| 2211 | stats_.bytes_retransmitted += result.bytes_written; |
| 2212 | ++stats_.packets_retransmitted; |
| 2213 | } |
| 2214 | |
| 2215 | return true; |
| 2216 | } |
| 2217 | |
| 2218 | void QuicConnection::FlushPackets() { |
| 2219 | if (!connected_) { |
| 2220 | return; |
| 2221 | } |
| 2222 | |
| 2223 | if (!writer_->IsBatchMode()) { |
| 2224 | return; |
| 2225 | } |
| 2226 | |
| 2227 | if (HandleWriteBlocked()) { |
| 2228 | QUIC_DLOG(INFO) << ENDPOINT << "FlushPackets called while blocked."; |
| 2229 | return; |
| 2230 | } |
| 2231 | |
| 2232 | WriteResult result = writer_->Flush(); |
| 2233 | |
| 2234 | if (HandleWriteBlocked()) { |
| 2235 | DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status) |
| 2236 | << "Unexpected flush result:" << result; |
| 2237 | QUIC_DLOG(INFO) << ENDPOINT << "Write blocked in FlushPackets."; |
| 2238 | return; |
| 2239 | } |
| 2240 | |
| 2241 | if (IsWriteError(result.status)) { |
| 2242 | OnWriteError(result.error_code); |
| 2243 | } |
| 2244 | } |
| 2245 | |
| 2246 | bool QuicConnection::IsMsgTooBig(const WriteResult& result) { |
| 2247 | return (result.status == WRITE_STATUS_MSG_TOO_BIG) || |
| 2248 | (IsWriteError(result.status) && result.error_code == QUIC_EMSGSIZE); |
| 2249 | } |
| 2250 | |
| 2251 | bool QuicConnection::ShouldDiscardPacket(const SerializedPacket& packet) { |
| 2252 | if (!connected_) { |
| 2253 | QUIC_DLOG(INFO) << ENDPOINT |
| 2254 | << "Not sending packet as connection is disconnected."; |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
| 2258 | QuicPacketNumber packet_number = packet.packet_number; |
| 2259 | if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 2260 | packet.encryption_level == ENCRYPTION_INITIAL) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2261 | // Drop packets that are NULL encrypted since the peer won't accept them |
| 2262 | // anymore. |
| 2263 | QUIC_DLOG(INFO) << ENDPOINT |
| 2264 | << "Dropping NULL encrypted packet: " << packet_number |
| 2265 | << " since the connection is forward secure."; |
| 2266 | return true; |
| 2267 | } |
| 2268 | |
| 2269 | return false; |
| 2270 | } |
| 2271 | |
| 2272 | void QuicConnection::OnWriteError(int error_code) { |
| 2273 | if (write_error_occurred_) { |
| 2274 | // A write error already occurred. The connection is being closed. |
| 2275 | return; |
| 2276 | } |
| 2277 | write_error_occurred_ = true; |
| 2278 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 2279 | const std::string error_details = QuicStrCat( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2280 | "Write failed with error: ", error_code, " (", strerror(error_code), ")"); |
| 2281 | QUIC_LOG_FIRST_N(ERROR, 2) << ENDPOINT << error_details; |
| 2282 | switch (error_code) { |
| 2283 | case QUIC_EMSGSIZE: |
ianswett | dc1e7ab | 2019-05-03 16:10:44 -0700 | [diff] [blame] | 2284 | CloseConnection(QUIC_PACKET_WRITE_ERROR, error_details, |
| 2285 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2286 | break; |
| 2287 | default: |
| 2288 | // We can't send an error as the socket is presumably borked. |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 2289 | if (VersionHasIetfInvariantHeader(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2290 | QUIC_CODE_COUNT(quic_tear_down_local_connection_on_write_error_ietf); |
| 2291 | } else { |
| 2292 | QUIC_CODE_COUNT( |
| 2293 | quic_tear_down_local_connection_on_write_error_non_ietf); |
| 2294 | } |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 2295 | CloseConnection(QUIC_PACKET_WRITE_ERROR, error_details, |
| 2296 | ConnectionCloseBehavior::SILENT_CLOSE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | char* QuicConnection::GetPacketBuffer() { |
| 2301 | return writer_->GetNextWriteLocation(self_address().host(), peer_address()); |
| 2302 | } |
| 2303 | |
| 2304 | void QuicConnection::OnSerializedPacket(SerializedPacket* serialized_packet) { |
| 2305 | if (serialized_packet->encrypted_buffer == nullptr) { |
| 2306 | // We failed to serialize the packet, so close the connection. |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 2307 | // Specify that the close is silent, that no packet be sent, so no infinite |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2308 | // loop here. |
| 2309 | // TODO(ianswett): This is actually an internal error, not an |
| 2310 | // encryption failure. |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 2311 | if (VersionHasIetfInvariantHeader(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2312 | QUIC_CODE_COUNT( |
| 2313 | quic_tear_down_local_connection_on_serialized_packet_ietf); |
| 2314 | } else { |
| 2315 | QUIC_CODE_COUNT( |
| 2316 | quic_tear_down_local_connection_on_serialized_packet_non_ietf); |
| 2317 | } |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 2318 | CloseConnection(QUIC_ENCRYPTION_FAILURE, |
| 2319 | "Serialized packet does not have an encrypted buffer.", |
| 2320 | ConnectionCloseBehavior::SILENT_CLOSE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2321 | return; |
| 2322 | } |
| 2323 | |
| 2324 | if (serialized_packet->retransmittable_frames.empty() && |
| 2325 | !serialized_packet->original_packet_number.IsInitialized()) { |
| 2326 | // Increment consecutive_num_packets_with_no_retransmittable_frames_ if |
| 2327 | // this packet is a new transmission with no retransmittable frames. |
| 2328 | ++consecutive_num_packets_with_no_retransmittable_frames_; |
| 2329 | } else { |
| 2330 | consecutive_num_packets_with_no_retransmittable_frames_ = 0; |
| 2331 | } |
| 2332 | SendOrQueuePacket(serialized_packet); |
| 2333 | } |
| 2334 | |
| 2335 | void QuicConnection::OnUnrecoverableError(QuicErrorCode error, |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 2336 | const std::string& error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2337 | // The packet creator or generator encountered an unrecoverable error: tear |
| 2338 | // down local connection state immediately. |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 2339 | if (VersionHasIetfInvariantHeader(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2340 | QUIC_CODE_COUNT( |
| 2341 | quic_tear_down_local_connection_on_unrecoverable_error_ietf); |
| 2342 | } else { |
| 2343 | QUIC_CODE_COUNT( |
| 2344 | quic_tear_down_local_connection_on_unrecoverable_error_non_ietf); |
| 2345 | } |
fkastenholz | 85f1890 | 2019-05-28 12:47:00 -0700 | [diff] [blame] | 2346 | CloseConnection(error, error_details, ConnectionCloseBehavior::SILENT_CLOSE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | void QuicConnection::OnCongestionChange() { |
| 2350 | visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); |
| 2351 | |
| 2352 | // Uses the connection's smoothed RTT. If zero, uses initial_rtt. |
| 2353 | QuicTime::Delta rtt = sent_packet_manager_.GetRttStats()->smoothed_rtt(); |
| 2354 | if (rtt.IsZero()) { |
| 2355 | rtt = sent_packet_manager_.GetRttStats()->initial_rtt(); |
| 2356 | } |
| 2357 | |
| 2358 | if (debug_visitor_ != nullptr) { |
| 2359 | debug_visitor_->OnRttChanged(rtt); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | void QuicConnection::OnPathMtuIncreased(QuicPacketLength packet_size) { |
| 2364 | if (packet_size > max_packet_length()) { |
| 2365 | SetMaxPacketLength(packet_size); |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | void QuicConnection::OnHandshakeComplete() { |
| 2370 | sent_packet_manager_.SetHandshakeConfirmed(); |
fayang | bf3d286 | 2019-06-20 14:13:44 -0700 | [diff] [blame] | 2371 | // This may have changed the retransmission timer, so re-arm it. |
| 2372 | SetRetransmissionAlarm(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2373 | // The client should immediately ack the SHLO to confirm the handshake is |
| 2374 | // complete with the server. |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2375 | if (perspective_ == Perspective::IS_CLIENT && ack_frame_updated()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2376 | ack_alarm_->Update(clock_->ApproximateNow(), QuicTime::Delta::Zero()); |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | void QuicConnection::SendOrQueuePacket(SerializedPacket* packet) { |
| 2381 | // The caller of this function is responsible for checking CanWrite(). |
| 2382 | if (packet->encrypted_buffer == nullptr) { |
| 2383 | QUIC_BUG << "packet.encrypted_buffer == nullptr in to SendOrQueuePacket"; |
| 2384 | return; |
| 2385 | } |
| 2386 | // If there are already queued packets, queue this one immediately to ensure |
| 2387 | // it's written in sequence number order. |
| 2388 | if (!queued_packets_.empty() || !WritePacket(packet)) { |
| 2389 | // Take ownership of the underlying encrypted packet. |
| 2390 | packet->encrypted_buffer = CopyBuffer(*packet); |
| 2391 | queued_packets_.push_back(*packet); |
| 2392 | packet->retransmittable_frames.clear(); |
| 2393 | } |
| 2394 | |
| 2395 | ClearSerializedPacket(packet); |
| 2396 | } |
| 2397 | |
| 2398 | void QuicConnection::OnPingTimeout() { |
| 2399 | if (!retransmission_alarm_->IsSet()) { |
| 2400 | visitor_->SendPing(); |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | void QuicConnection::SendAck() { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 2405 | DCHECK(!SupportsMultiplePacketNumberSpaces()); |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2406 | QUIC_DVLOG(1) << ENDPOINT << "Sending an ACK proactively"; |
| 2407 | QuicFrames frames; |
| 2408 | frames.push_back(GetUpdatedAckFrame()); |
| 2409 | if (!no_stop_waiting_frames_) { |
| 2410 | QuicStopWaitingFrame stop_waiting; |
| 2411 | PopulateStopWaitingFrame(&stop_waiting); |
| 2412 | frames.push_back(QuicFrame(stop_waiting)); |
| 2413 | } |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 2414 | if (!packet_generator_.FlushAckFrame(frames)) { |
| 2415 | return; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2416 | } |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 2417 | ResetAckStates(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2418 | if (consecutive_num_packets_with_no_retransmittable_frames_ < |
| 2419 | max_consecutive_num_packets_with_no_retransmittable_frames_) { |
| 2420 | return; |
| 2421 | } |
| 2422 | consecutive_num_packets_with_no_retransmittable_frames_ = 0; |
| 2423 | if (packet_generator_.HasRetransmittableFrames() || |
| 2424 | visitor_->WillingAndAbleToWrite()) { |
| 2425 | // There are pending retransmittable frames. |
| 2426 | return; |
| 2427 | } |
| 2428 | |
| 2429 | visitor_->OnAckNeedsRetransmittableFrame(); |
| 2430 | } |
| 2431 | |
| 2432 | void QuicConnection::OnPathDegradingTimeout() { |
| 2433 | is_path_degrading_ = true; |
| 2434 | visitor_->OnPathDegrading(); |
| 2435 | } |
| 2436 | |
| 2437 | void QuicConnection::OnRetransmissionTimeout() { |
| 2438 | DCHECK(!sent_packet_manager_.unacked_packets().empty()); |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 2439 | const QuicPacketNumber previous_created_packet_number = |
| 2440 | packet_generator_.packet_number(); |
| 2441 | const size_t previous_crypto_retransmit_count = |
| 2442 | stats_.crypto_retransmit_count; |
| 2443 | const size_t previous_loss_timeout_count = stats_.loss_timeout_count; |
| 2444 | const size_t previous_tlp_count = stats_.tlp_count; |
| 2445 | const size_t pervious_rto_count = stats_.rto_count; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2446 | if (close_connection_after_five_rtos_ && |
| 2447 | sent_packet_manager_.GetConsecutiveRtoCount() >= 4) { |
| 2448 | // Close on the 5th consecutive RTO, so after 4 previous RTOs have occurred. |
| 2449 | CloseConnection(QUIC_TOO_MANY_RTOS, "5 consecutive retransmission timeouts", |
| 2450 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 2451 | return; |
| 2452 | } |
| 2453 | |
| 2454 | sent_packet_manager_.OnRetransmissionTimeout(); |
| 2455 | WriteIfNotBlocked(); |
| 2456 | |
| 2457 | // A write failure can result in the connection being closed, don't attempt to |
| 2458 | // write further packets, or to set alarms. |
| 2459 | if (!connected_) { |
| 2460 | return; |
| 2461 | } |
| 2462 | |
| 2463 | // In the TLP case, the SentPacketManager gives the connection the opportunity |
| 2464 | // to send new data before retransmitting. |
| 2465 | if (sent_packet_manager_.MaybeRetransmitTailLossProbe()) { |
| 2466 | // Send the pending retransmission now that it's been queued. |
| 2467 | WriteIfNotBlocked(); |
| 2468 | } |
| 2469 | |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 2470 | if (sent_packet_manager_.fix_rto_retransmission()) { |
| 2471 | // Making sure at least one packet is created when retransmission timer |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 2472 | // fires in TLP. It is possible that loss algorithm invokes timer based loss |
| 2473 | // but the packet does not need to be retransmitted. And no packets will be |
| 2474 | // sent if timer fires in HANDSHAKE or RTO mode but writer is blocked. |
| 2475 | QUIC_BUG_IF(packet_generator_.packet_number() == |
| 2476 | previous_created_packet_number && |
| 2477 | stats_.tlp_count != previous_tlp_count) |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 2478 | << "previous_crypto_retransmit_count: " |
| 2479 | << previous_crypto_retransmit_count |
| 2480 | << ", crypto_retransmit_count: " << stats_.crypto_retransmit_count |
| 2481 | << ", previous_loss_timeout_count: " << previous_loss_timeout_count |
| 2482 | << ", loss_timeout_count: " << stats_.loss_timeout_count |
| 2483 | << ", previous_tlp_count: " << previous_tlp_count |
| 2484 | << ", tlp_count: " << stats_.tlp_count |
| 2485 | << ", pervious_rto_count: " << pervious_rto_count |
| 2486 | << ", rto_count: " << stats_.rto_count |
| 2487 | << ", previous_created_packet_number: " |
| 2488 | << previous_created_packet_number |
| 2489 | << ", packet_number: " << packet_generator_.packet_number() |
fayang | d301683 | 2019-08-08 07:24:45 -0700 | [diff] [blame] | 2490 | << ", session has data to write: " << visitor_->WillingAndAbleToWrite() |
| 2491 | << ", writer is blocked: " << writer_->IsWriteBlocked(); |
fayang | a29eb24 | 2019-07-16 12:25:38 -0700 | [diff] [blame] | 2492 | } |
| 2493 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2494 | // Ensure the retransmission alarm is always set if there are unacked packets |
| 2495 | // and nothing waiting to be sent. |
| 2496 | // This happens if the loss algorithm invokes a timer based loss, but the |
| 2497 | // packet doesn't need to be retransmitted. |
| 2498 | if (!HasQueuedData() && !retransmission_alarm_->IsSet()) { |
| 2499 | SetRetransmissionAlarm(); |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | void QuicConnection::SetEncrypter(EncryptionLevel level, |
| 2504 | std::unique_ptr<QuicEncrypter> encrypter) { |
| 2505 | packet_generator_.SetEncrypter(level, std::move(encrypter)); |
| 2506 | } |
| 2507 | |
| 2508 | void QuicConnection::SetDiversificationNonce( |
| 2509 | const DiversificationNonce& nonce) { |
| 2510 | DCHECK_EQ(Perspective::IS_SERVER, perspective_); |
| 2511 | packet_generator_.SetDiversificationNonce(nonce); |
| 2512 | } |
| 2513 | |
| 2514 | void QuicConnection::SetDefaultEncryptionLevel(EncryptionLevel level) { |
fayang | 914fbe1 | 2019-07-11 06:23:55 -0700 | [diff] [blame] | 2515 | if (level != encryption_level_ && packet_generator_.HasPendingFrames()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2516 | // Flush all queued frames when encryption level changes. |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2517 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2518 | packet_generator_.FlushAllQueuedFrames(); |
| 2519 | } |
| 2520 | encryption_level_ = level; |
| 2521 | packet_generator_.set_encryption_level(level); |
| 2522 | } |
| 2523 | |
| 2524 | void QuicConnection::SetDecrypter(EncryptionLevel level, |
| 2525 | std::unique_ptr<QuicDecrypter> decrypter) { |
| 2526 | framer_.SetDecrypter(level, std::move(decrypter)); |
| 2527 | |
| 2528 | if (!undecryptable_packets_.empty() && |
| 2529 | !process_undecryptable_packets_alarm_->IsSet()) { |
| 2530 | process_undecryptable_packets_alarm_->Set(clock_->ApproximateNow()); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | void QuicConnection::SetAlternativeDecrypter( |
| 2535 | EncryptionLevel level, |
| 2536 | std::unique_ptr<QuicDecrypter> decrypter, |
| 2537 | bool latch_once_used) { |
| 2538 | framer_.SetAlternativeDecrypter(level, std::move(decrypter), latch_once_used); |
| 2539 | |
| 2540 | if (!undecryptable_packets_.empty() && |
| 2541 | !process_undecryptable_packets_alarm_->IsSet()) { |
| 2542 | process_undecryptable_packets_alarm_->Set(clock_->ApproximateNow()); |
| 2543 | } |
| 2544 | } |
| 2545 | |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 2546 | void QuicConnection::InstallDecrypter( |
| 2547 | EncryptionLevel level, |
| 2548 | std::unique_ptr<QuicDecrypter> decrypter) { |
| 2549 | framer_.InstallDecrypter(level, std::move(decrypter)); |
| 2550 | if (!undecryptable_packets_.empty() && |
| 2551 | !process_undecryptable_packets_alarm_->IsSet()) { |
| 2552 | process_undecryptable_packets_alarm_->Set(clock_->ApproximateNow()); |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | void QuicConnection::RemoveDecrypter(EncryptionLevel level) { |
| 2557 | framer_.RemoveDecrypter(level); |
| 2558 | } |
| 2559 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2560 | const QuicDecrypter* QuicConnection::decrypter() const { |
| 2561 | return framer_.decrypter(); |
| 2562 | } |
| 2563 | |
| 2564 | const QuicDecrypter* QuicConnection::alternative_decrypter() const { |
| 2565 | return framer_.alternative_decrypter(); |
| 2566 | } |
| 2567 | |
| 2568 | void QuicConnection::QueueUndecryptablePacket( |
| 2569 | const QuicEncryptedPacket& packet) { |
| 2570 | QUIC_DVLOG(1) << ENDPOINT << "Queueing undecryptable packet."; |
| 2571 | undecryptable_packets_.push_back(packet.Clone()); |
| 2572 | } |
| 2573 | |
| 2574 | void QuicConnection::MaybeProcessUndecryptablePackets() { |
| 2575 | process_undecryptable_packets_alarm_->Cancel(); |
| 2576 | |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 2577 | if (undecryptable_packets_.empty() || |
| 2578 | encryption_level_ == ENCRYPTION_INITIAL) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | while (connected_ && !undecryptable_packets_.empty()) { |
| 2583 | // Making sure there is no pending frames when processing next undecrypted |
| 2584 | // packet because the queued ack frame may change. |
| 2585 | packet_generator_.FlushAllQueuedFrames(); |
| 2586 | if (!connected_) { |
| 2587 | return; |
| 2588 | } |
| 2589 | QUIC_DVLOG(1) << ENDPOINT << "Attempting to process undecryptable packet"; |
| 2590 | QuicEncryptedPacket* packet = undecryptable_packets_.front().get(); |
| 2591 | if (!framer_.ProcessPacket(*packet) && |
| 2592 | framer_.error() == QUIC_DECRYPTION_FAILURE) { |
| 2593 | QUIC_DVLOG(1) << ENDPOINT << "Unable to process undecryptable packet..."; |
| 2594 | break; |
| 2595 | } |
| 2596 | QUIC_DVLOG(1) << ENDPOINT << "Processed undecryptable packet!"; |
| 2597 | ++stats_.packets_processed; |
| 2598 | undecryptable_packets_.pop_front(); |
| 2599 | } |
| 2600 | |
| 2601 | // Once forward secure encryption is in use, there will be no |
| 2602 | // new keys installed and hence any undecryptable packets will |
| 2603 | // never be able to be decrypted. |
| 2604 | if (encryption_level_ == ENCRYPTION_FORWARD_SECURE) { |
| 2605 | if (debug_visitor_ != nullptr) { |
| 2606 | // TODO(rtenneti): perhaps more efficient to pass the number of |
| 2607 | // undecryptable packets as the argument to OnUndecryptablePacket so that |
| 2608 | // we just need to call OnUndecryptablePacket once? |
| 2609 | for (size_t i = 0; i < undecryptable_packets_.size(); ++i) { |
| 2610 | debug_visitor_->OnUndecryptablePacket(); |
| 2611 | } |
| 2612 | } |
| 2613 | undecryptable_packets_.clear(); |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | void QuicConnection::QueueCoalescedPacket(const QuicEncryptedPacket& packet) { |
| 2618 | QUIC_DVLOG(1) << ENDPOINT << "Queueing coalesced packet."; |
| 2619 | coalesced_packets_.push_back(packet.Clone()); |
| 2620 | } |
| 2621 | |
| 2622 | void QuicConnection::MaybeProcessCoalescedPackets() { |
| 2623 | bool processed = false; |
dschinazi | 8d55113 | 2019-08-02 19:17:16 -0700 | [diff] [blame] | 2624 | while (connected_ && !coalesced_packets_.empty()) { |
| 2625 | std::unique_ptr<QuicEncryptedPacket> packet = |
| 2626 | std::move(coalesced_packets_.front()); |
| 2627 | coalesced_packets_.pop_front(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2628 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2629 | QUIC_DVLOG(1) << ENDPOINT << "Processing coalesced packet"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2630 | if (framer_.ProcessPacket(*packet)) { |
| 2631 | processed = true; |
| 2632 | } else { |
| 2633 | // If we are unable to decrypt this packet, it might be |
| 2634 | // because the CHLO or SHLO packet was lost. |
| 2635 | if (framer_.error() == QUIC_DECRYPTION_FAILURE) { |
dschinazi | 6ece500 | 2019-05-22 06:35:49 -0700 | [diff] [blame] | 2636 | ++stats_.undecryptable_packets_received; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2637 | if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && |
| 2638 | undecryptable_packets_.size() < max_undecryptable_packets_) { |
| 2639 | QueueUndecryptablePacket(*packet); |
| 2640 | } else if (debug_visitor_ != nullptr) { |
| 2641 | debug_visitor_->OnUndecryptablePacket(); |
| 2642 | } |
| 2643 | } |
| 2644 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2645 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2646 | if (processed) { |
| 2647 | MaybeProcessUndecryptablePackets(); |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | void QuicConnection::CloseConnection( |
| 2652 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 2653 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2654 | ConnectionCloseBehavior connection_close_behavior) { |
| 2655 | DCHECK(!error_details.empty()); |
| 2656 | if (!connected_) { |
| 2657 | QUIC_DLOG(INFO) << "Connection is already closed."; |
| 2658 | return; |
| 2659 | } |
| 2660 | |
| 2661 | QUIC_DLOG(INFO) << ENDPOINT << "Closing connection: " << connection_id() |
| 2662 | << ", with error: " << QuicErrorCodeToString(error) << " (" |
| 2663 | << error << "), and details: " << error_details; |
| 2664 | |
ianswett | dc1e7ab | 2019-05-03 16:10:44 -0700 | [diff] [blame] | 2665 | if (connection_close_behavior != ConnectionCloseBehavior::SILENT_CLOSE) { |
| 2666 | SendConnectionClosePacket(error, error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2667 | } |
| 2668 | |
wub | 5f64ec4 | 2019-06-06 07:31:19 -0700 | [diff] [blame] | 2669 | TearDownLocalConnectionState(error, error_details, |
| 2670 | ConnectionCloseSource::FROM_SELF); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, |
ianswett | dc1e7ab | 2019-05-03 16:10:44 -0700 | [diff] [blame] | 2674 | const std::string& details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2675 | QUIC_DLOG(INFO) << ENDPOINT << "Sending connection close packet."; |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 2676 | SetDefaultEncryptionLevel(GetConnectionCloseEncryptionLevel()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2677 | ClearQueuedPackets(); |
ianswett | dc1e7ab | 2019-05-03 16:10:44 -0700 | [diff] [blame] | 2678 | // If there was a packet write error, write the smallest close possible. |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2679 | ScopedPacketFlusher flusher(this); |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 2680 | // When multiple packet number spaces is supported, an ACK frame will be |
| 2681 | // bundled when connection is not write blocked. |
| 2682 | if (!SupportsMultiplePacketNumberSpaces() && |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2683 | error != QUIC_PACKET_WRITE_ERROR && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2684 | !GetUpdatedAckFrame().ack_frame->packets.Empty()) { |
| 2685 | SendAck(); |
| 2686 | } |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 2687 | QuicConnectionCloseFrame* frame; |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 2688 | if (VersionHasIetfQuicFrames(transport_version())) { |
fkastenholz | 0d6554a | 2019-08-05 12:20:35 -0700 | [diff] [blame] | 2689 | QuicErrorCodeToIetfMapping mapping = |
| 2690 | QuicErrorCodeToTransportErrorCode(error); |
| 2691 | if (mapping.is_transport_close_) { |
| 2692 | // Maps to a transport close |
| 2693 | // TODO(fkastenholz) need to change "0" to get the frame type currently |
| 2694 | // being processed so that it can be inserted into the frame. |
| 2695 | frame = new QuicConnectionCloseFrame(error, details, |
| 2696 | mapping.transport_error_code_, 0); |
| 2697 | } else { |
| 2698 | // Maps to an application close. |
| 2699 | frame = new QuicConnectionCloseFrame(error, details, |
| 2700 | mapping.application_error_code_); |
| 2701 | } |
| 2702 | } else { |
| 2703 | frame = new QuicConnectionCloseFrame(error, details); |
fkastenholz | 72f509b | 2019-04-10 09:17:49 -0700 | [diff] [blame] | 2704 | } |
fayang | 3203f25 | 2019-05-03 06:00:03 -0700 | [diff] [blame] | 2705 | packet_generator_.ConsumeRetransmittableControlFrame(QuicFrame(frame)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2706 | packet_generator_.FlushAllQueuedFrames(); |
nharper | ef46896 | 2019-07-02 14:15:38 -0700 | [diff] [blame] | 2707 | if (GetQuicReloadableFlag(quic_clear_queued_packets_on_connection_close)) { |
| 2708 | QUIC_RELOADABLE_FLAG_COUNT(quic_clear_queued_packets_on_connection_close); |
| 2709 | ClearQueuedPackets(); |
| 2710 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | void QuicConnection::TearDownLocalConnectionState( |
| 2714 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 2715 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2716 | ConnectionCloseSource source) { |
| 2717 | if (!connected_) { |
| 2718 | QUIC_DLOG(INFO) << "Connection is already closed."; |
| 2719 | return; |
| 2720 | } |
| 2721 | |
| 2722 | // If we are using a batch writer, flush packets queued in it, if any. |
| 2723 | FlushPackets(); |
| 2724 | connected_ = false; |
| 2725 | DCHECK(visitor_ != nullptr); |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 2726 | // TODO(fkastenholz): When the IETF Transport Connection Close information |
| 2727 | // gets plumbed in, expand this constructor to include that information. |
| 2728 | QuicConnectionCloseFrame frame(error, error_details); |
| 2729 | visitor_->OnConnectionClosed(frame, source); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2730 | if (debug_visitor_ != nullptr) { |
fkastenholz | ac11db0 | 2019-06-24 06:22:04 -0700 | [diff] [blame] | 2731 | debug_visitor_->OnConnectionClosed(frame, source); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2732 | } |
| 2733 | // Cancel the alarms so they don't trigger any action now that the |
| 2734 | // connection is closed. |
| 2735 | CancelAllAlarms(); |
| 2736 | } |
| 2737 | |
| 2738 | void QuicConnection::CancelAllAlarms() { |
| 2739 | QUIC_DVLOG(1) << "Cancelling all QuicConnection alarms."; |
| 2740 | |
| 2741 | ack_alarm_->Cancel(); |
| 2742 | ping_alarm_->Cancel(); |
| 2743 | retransmission_alarm_->Cancel(); |
| 2744 | send_alarm_->Cancel(); |
| 2745 | timeout_alarm_->Cancel(); |
| 2746 | mtu_discovery_alarm_->Cancel(); |
| 2747 | path_degrading_alarm_->Cancel(); |
renjietang | 11e4a3d | 2019-05-03 11:27:26 -0700 | [diff] [blame] | 2748 | process_undecryptable_packets_alarm_->Cancel(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | QuicByteCount QuicConnection::max_packet_length() const { |
| 2752 | return packet_generator_.GetCurrentMaxPacketLength(); |
| 2753 | } |
| 2754 | |
| 2755 | void QuicConnection::SetMaxPacketLength(QuicByteCount length) { |
| 2756 | long_term_mtu_ = length; |
| 2757 | packet_generator_.SetMaxPacketLength(GetLimitedMaxPacketSize(length)); |
| 2758 | } |
| 2759 | |
| 2760 | bool QuicConnection::HasQueuedData() const { |
| 2761 | return pending_version_negotiation_packet_ || !queued_packets_.empty() || |
fayang | 914fbe1 | 2019-07-11 06:23:55 -0700 | [diff] [blame] | 2762 | packet_generator_.HasPendingFrames(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2763 | } |
| 2764 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2765 | bool QuicConnection::CanWriteStreamData() { |
| 2766 | // Don't write stream data if there are negotiation or queued data packets |
| 2767 | // to send. Otherwise, continue and bundle as many frames as possible. |
| 2768 | if (pending_version_negotiation_packet_ || !queued_packets_.empty()) { |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
| 2772 | IsHandshake pending_handshake = |
| 2773 | visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE; |
| 2774 | // Sending queued packets may have caused the socket to become write blocked, |
| 2775 | // or the congestion manager to prohibit sending. If we've sent everything |
| 2776 | // we had queued and we're still not blocked, let the visitor know it can |
| 2777 | // write more. |
| 2778 | return ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, pending_handshake); |
| 2779 | } |
| 2780 | |
| 2781 | void QuicConnection::SetNetworkTimeouts(QuicTime::Delta handshake_timeout, |
| 2782 | QuicTime::Delta idle_timeout) { |
| 2783 | QUIC_BUG_IF(idle_timeout > handshake_timeout) |
| 2784 | << "idle_timeout:" << idle_timeout.ToMilliseconds() |
| 2785 | << " handshake_timeout:" << handshake_timeout.ToMilliseconds(); |
| 2786 | // Adjust the idle timeout on client and server to prevent clients from |
| 2787 | // sending requests to servers which have already closed the connection. |
| 2788 | if (perspective_ == Perspective::IS_SERVER) { |
| 2789 | idle_timeout = idle_timeout + QuicTime::Delta::FromSeconds(3); |
| 2790 | } else if (idle_timeout > QuicTime::Delta::FromSeconds(1)) { |
| 2791 | idle_timeout = idle_timeout - QuicTime::Delta::FromSeconds(1); |
| 2792 | } |
| 2793 | handshake_timeout_ = handshake_timeout; |
| 2794 | idle_network_timeout_ = idle_timeout; |
| 2795 | |
| 2796 | SetTimeoutAlarm(); |
| 2797 | } |
| 2798 | |
| 2799 | void QuicConnection::CheckForTimeout() { |
| 2800 | QuicTime now = clock_->ApproximateNow(); |
| 2801 | QuicTime time_of_last_packet = |
| 2802 | std::max(time_of_last_received_packet_, |
| 2803 | time_of_first_packet_sent_after_receiving_); |
| 2804 | |
| 2805 | // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| |
| 2806 | // is accurate time. However, this should not change the behavior of |
| 2807 | // timeout handling. |
| 2808 | QuicTime::Delta idle_duration = now - time_of_last_packet; |
| 2809 | QUIC_DVLOG(1) << ENDPOINT << "last packet " |
| 2810 | << time_of_last_packet.ToDebuggingValue() |
| 2811 | << " now:" << now.ToDebuggingValue() |
| 2812 | << " idle_duration:" << idle_duration.ToMicroseconds() |
| 2813 | << " idle_network_timeout: " |
| 2814 | << idle_network_timeout_.ToMicroseconds(); |
| 2815 | if (idle_duration >= idle_network_timeout_) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 2816 | const std::string error_details = "No recent network activity."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2817 | QUIC_DVLOG(1) << ENDPOINT << error_details; |
| 2818 | if ((sent_packet_manager_.GetConsecutiveTlpCount() > 0 || |
| 2819 | sent_packet_manager_.GetConsecutiveRtoCount() > 0 || |
| 2820 | visitor_->ShouldKeepConnectionAlive())) { |
| 2821 | CloseConnection(QUIC_NETWORK_IDLE_TIMEOUT, error_details, |
| 2822 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 2823 | } else { |
| 2824 | CloseConnection(QUIC_NETWORK_IDLE_TIMEOUT, error_details, |
| 2825 | idle_timeout_connection_close_behavior_); |
| 2826 | } |
| 2827 | return; |
| 2828 | } |
| 2829 | |
| 2830 | if (!handshake_timeout_.IsInfinite()) { |
| 2831 | QuicTime::Delta connected_duration = now - stats_.connection_creation_time; |
| 2832 | QUIC_DVLOG(1) << ENDPOINT |
| 2833 | << "connection time: " << connected_duration.ToMicroseconds() |
| 2834 | << " handshake timeout: " |
| 2835 | << handshake_timeout_.ToMicroseconds(); |
| 2836 | if (connected_duration >= handshake_timeout_) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 2837 | const std::string error_details = "Handshake timeout expired."; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2838 | QUIC_DVLOG(1) << ENDPOINT << error_details; |
| 2839 | CloseConnection(QUIC_HANDSHAKE_TIMEOUT, error_details, |
| 2840 | ConnectionCloseBehavior::SEND_CONNECTION_CLOSE_PACKET); |
| 2841 | return; |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | SetTimeoutAlarm(); |
| 2846 | } |
| 2847 | |
| 2848 | void QuicConnection::SetTimeoutAlarm() { |
| 2849 | QuicTime time_of_last_packet = |
| 2850 | std::max(time_of_last_received_packet_, |
| 2851 | time_of_first_packet_sent_after_receiving_); |
| 2852 | |
| 2853 | QuicTime deadline = time_of_last_packet + idle_network_timeout_; |
| 2854 | if (!handshake_timeout_.IsInfinite()) { |
| 2855 | deadline = std::min(deadline, |
| 2856 | stats_.connection_creation_time + handshake_timeout_); |
| 2857 | } |
| 2858 | |
| 2859 | timeout_alarm_->Update(deadline, QuicTime::Delta::Zero()); |
| 2860 | } |
| 2861 | |
| 2862 | void QuicConnection::SetPingAlarm() { |
| 2863 | if (perspective_ == Perspective::IS_SERVER) { |
ianswett | b7f7cd2 | 2019-05-01 08:04:16 -0700 | [diff] [blame] | 2864 | // Only clients send pings to avoid NATs from timing out. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2865 | return; |
| 2866 | } |
| 2867 | if (!visitor_->ShouldKeepConnectionAlive()) { |
| 2868 | ping_alarm_->Cancel(); |
ianswett | b7f7cd2 | 2019-05-01 08:04:16 -0700 | [diff] [blame] | 2869 | // Don't send a ping unless the application (ie: HTTP/3) says to, usually |
| 2870 | // because it is expecting a response from the server. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2871 | return; |
| 2872 | } |
| 2873 | if (retransmittable_on_wire_timeout_.IsInfinite() || |
| 2874 | sent_packet_manager_.HasInFlightPackets()) { |
| 2875 | // Extend the ping alarm. |
| 2876 | ping_alarm_->Update(clock_->ApproximateNow() + ping_timeout_, |
| 2877 | QuicTime::Delta::FromSeconds(1)); |
| 2878 | return; |
| 2879 | } |
| 2880 | DCHECK_LT(retransmittable_on_wire_timeout_, ping_timeout_); |
| 2881 | // If it's already set to an earlier time, then don't update it. |
| 2882 | if (ping_alarm_->IsSet() && |
| 2883 | ping_alarm_->deadline() < |
| 2884 | clock_->ApproximateNow() + retransmittable_on_wire_timeout_) { |
| 2885 | return; |
| 2886 | } |
| 2887 | // Use a shorter timeout if there are open streams, but nothing on the wire. |
| 2888 | ping_alarm_->Update( |
| 2889 | clock_->ApproximateNow() + retransmittable_on_wire_timeout_, |
| 2890 | QuicTime::Delta::FromMilliseconds(1)); |
| 2891 | } |
| 2892 | |
| 2893 | void QuicConnection::SetRetransmissionAlarm() { |
| 2894 | if (packet_generator_.PacketFlusherAttached()) { |
| 2895 | pending_retransmission_alarm_ = true; |
| 2896 | return; |
| 2897 | } |
| 2898 | QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); |
| 2899 | retransmission_alarm_->Update(retransmission_time, |
| 2900 | QuicTime::Delta::FromMilliseconds(1)); |
| 2901 | } |
| 2902 | |
| 2903 | void QuicConnection::SetPathDegradingAlarm() { |
| 2904 | if (perspective_ == Perspective::IS_SERVER) { |
| 2905 | return; |
| 2906 | } |
| 2907 | const QuicTime::Delta delay = sent_packet_manager_.GetPathDegradingDelay(); |
| 2908 | path_degrading_alarm_->Update(clock_->ApproximateNow() + delay, |
| 2909 | QuicTime::Delta::FromMilliseconds(1)); |
| 2910 | } |
| 2911 | |
| 2912 | void QuicConnection::MaybeSetMtuAlarm(QuicPacketNumber sent_packet_number) { |
| 2913 | // Do not set the alarm if the target size is less than the current size. |
| 2914 | // This covers the case when |mtu_discovery_target_| is at its default value, |
| 2915 | // zero. |
| 2916 | if (mtu_discovery_target_ <= max_packet_length()) { |
| 2917 | return; |
| 2918 | } |
| 2919 | |
| 2920 | if (mtu_probe_count_ >= kMtuDiscoveryAttempts) { |
| 2921 | return; |
| 2922 | } |
| 2923 | |
| 2924 | if (mtu_discovery_alarm_->IsSet()) { |
| 2925 | return; |
| 2926 | } |
| 2927 | |
| 2928 | if (sent_packet_number >= next_mtu_probe_at_) { |
| 2929 | // Use an alarm to send the MTU probe to ensure that no ScopedPacketFlushers |
| 2930 | // are active. |
| 2931 | mtu_discovery_alarm_->Set(clock_->ApproximateNow()); |
| 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | void QuicConnection::MaybeSetAckAlarmTo(QuicTime time) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2936 | if (!ack_alarm_->IsSet() || ack_alarm_->deadline() > time) { |
| 2937 | ack_alarm_->Update(time, QuicTime::Delta::Zero()); |
| 2938 | } |
| 2939 | } |
| 2940 | |
| 2941 | QuicConnection::ScopedPacketFlusher::ScopedPacketFlusher( |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2942 | QuicConnection* connection) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2943 | : connection_(connection), |
| 2944 | flush_and_set_pending_retransmission_alarm_on_delete_(false) { |
| 2945 | if (connection_ == nullptr) { |
| 2946 | return; |
| 2947 | } |
| 2948 | |
| 2949 | if (!connection_->packet_generator_.PacketFlusherAttached()) { |
| 2950 | flush_and_set_pending_retransmission_alarm_on_delete_ = true; |
| 2951 | connection->packet_generator_.AttachPacketFlusher(); |
| 2952 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | QuicConnection::ScopedPacketFlusher::~ScopedPacketFlusher() { |
fayang | 0f0c4e6 | 2019-07-16 08:55:54 -0700 | [diff] [blame] | 2956 | if (connection_ == nullptr || !connection_->connected()) { |
fayang | 40ec3ac | 2019-06-05 09:07:54 -0700 | [diff] [blame] | 2957 | return; |
| 2958 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2959 | |
| 2960 | if (flush_and_set_pending_retransmission_alarm_on_delete_) { |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 2961 | const QuicTime ack_timeout = |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 2962 | connection_->uber_received_packet_manager_.GetEarliestAckTimeout(); |
fayang | f477f73 | 2019-06-20 07:03:06 -0700 | [diff] [blame] | 2963 | if (ack_timeout.IsInitialized()) { |
| 2964 | if (ack_timeout <= connection_->clock_->ApproximateNow() && |
| 2965 | !connection_->CanWrite(NO_RETRANSMITTABLE_DATA)) { |
| 2966 | // Cancel ACK alarm if connection is write blocked, and ACK will be |
| 2967 | // sent when connection gets unblocked. |
| 2968 | connection_->ack_alarm_->Cancel(); |
| 2969 | } else { |
| 2970 | connection_->MaybeSetAckAlarmTo(ack_timeout); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2971 | } |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2972 | } |
| 2973 | if (connection_->ack_alarm_->IsSet() && |
| 2974 | connection_->ack_alarm_->deadline() <= |
| 2975 | connection_->clock_->ApproximateNow()) { |
| 2976 | // An ACK needs to be sent right now. This ACK did not get bundled |
| 2977 | // because either there was no data to write or packets were marked as |
| 2978 | // received after frames were queued in the generator. |
| 2979 | if (connection_->send_alarm_->IsSet() && |
| 2980 | connection_->send_alarm_->deadline() <= |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2981 | connection_->clock_->ApproximateNow()) { |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2982 | // If send alarm will go off soon, let send alarm send the ACK. |
| 2983 | connection_->ack_alarm_->Cancel(); |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 2984 | } else if (connection_->SupportsMultiplePacketNumberSpaces()) { |
| 2985 | connection_->SendAllPendingAcks(); |
| 2986 | } else { |
| 2987 | connection_->SendAck(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | connection_->packet_generator_.Flush(); |
| 2991 | connection_->FlushPackets(); |
| 2992 | if (connection_->session_decides_what_to_write()) { |
| 2993 | // Reset transmission type. |
| 2994 | connection_->SetTransmissionType(NOT_RETRANSMISSION); |
| 2995 | } |
| 2996 | |
| 2997 | // Once all transmissions are done, check if there is any outstanding data |
| 2998 | // to send and notify the congestion controller if not. |
| 2999 | // |
| 3000 | // Note that this means that the application limited check will happen as |
| 3001 | // soon as the last flusher gets destroyed, which is typically after a |
| 3002 | // single stream write is finished. This means that if all the data from a |
| 3003 | // single write goes through the connection, the application-limited signal |
| 3004 | // will fire even if the caller does a write operation immediately after. |
| 3005 | // There are two important approaches to remedy this situation: |
| 3006 | // (1) Instantiate ScopedPacketFlusher before performing multiple subsequent |
| 3007 | // writes, thus deferring this check until all writes are done. |
| 3008 | // (2) Write data in chunks sufficiently large so that they cause the |
| 3009 | // connection to be limited by the congestion control. Typically, this |
| 3010 | // would mean writing chunks larger than the product of the current |
| 3011 | // pacing rate and the pacer granularity. So, for instance, if the |
| 3012 | // pacing rate of the connection is 1 Gbps, and the pacer granularity is |
| 3013 | // 1 ms, the caller should send at least 125k bytes in order to not |
| 3014 | // be marked as application-limited. |
| 3015 | connection_->CheckIfApplicationLimited(); |
| 3016 | |
| 3017 | if (connection_->pending_retransmission_alarm_) { |
| 3018 | connection_->SetRetransmissionAlarm(); |
| 3019 | connection_->pending_retransmission_alarm_ = false; |
| 3020 | } |
| 3021 | } |
| 3022 | DCHECK_EQ(flush_and_set_pending_retransmission_alarm_on_delete_, |
| 3023 | !connection_->packet_generator_.PacketFlusherAttached()); |
| 3024 | } |
| 3025 | |
| 3026 | HasRetransmittableData QuicConnection::IsRetransmittable( |
| 3027 | const SerializedPacket& packet) { |
| 3028 | // Retransmitted packets retransmittable frames are owned by the unacked |
| 3029 | // packet map, but are not present in the serialized packet. |
| 3030 | if (packet.transmission_type != NOT_RETRANSMISSION || |
| 3031 | !packet.retransmittable_frames.empty()) { |
| 3032 | return HAS_RETRANSMITTABLE_DATA; |
| 3033 | } else { |
| 3034 | return NO_RETRANSMITTABLE_DATA; |
| 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | bool QuicConnection::IsTerminationPacket(const SerializedPacket& packet) { |
| 3039 | if (packet.retransmittable_frames.empty()) { |
| 3040 | return false; |
| 3041 | } |
| 3042 | for (const QuicFrame& frame : packet.retransmittable_frames) { |
| 3043 | if (frame.type == CONNECTION_CLOSE_FRAME) { |
| 3044 | return true; |
| 3045 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3046 | } |
| 3047 | return false; |
| 3048 | } |
| 3049 | |
| 3050 | void QuicConnection::SetMtuDiscoveryTarget(QuicByteCount target) { |
| 3051 | mtu_discovery_target_ = GetLimitedMaxPacketSize(target); |
| 3052 | } |
| 3053 | |
| 3054 | QuicByteCount QuicConnection::GetLimitedMaxPacketSize( |
| 3055 | QuicByteCount suggested_max_packet_size) { |
| 3056 | if (!peer_address_.IsInitialized()) { |
| 3057 | QUIC_BUG << "Attempted to use a connection without a valid peer address"; |
| 3058 | return suggested_max_packet_size; |
| 3059 | } |
| 3060 | |
| 3061 | const QuicByteCount writer_limit = writer_->GetMaxPacketSize(peer_address()); |
| 3062 | |
| 3063 | QuicByteCount max_packet_size = suggested_max_packet_size; |
| 3064 | if (max_packet_size > writer_limit) { |
| 3065 | max_packet_size = writer_limit; |
| 3066 | } |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 3067 | if (max_packet_size > kMaxOutgoingPacketSize) { |
| 3068 | max_packet_size = kMaxOutgoingPacketSize; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3069 | } |
| 3070 | return max_packet_size; |
| 3071 | } |
| 3072 | |
| 3073 | void QuicConnection::SendMtuDiscoveryPacket(QuicByteCount target_mtu) { |
| 3074 | // Currently, this limit is ensured by the caller. |
| 3075 | DCHECK_EQ(target_mtu, GetLimitedMaxPacketSize(target_mtu)); |
| 3076 | |
| 3077 | // Send the probe. |
| 3078 | packet_generator_.GenerateMtuDiscoveryPacket(target_mtu); |
| 3079 | } |
| 3080 | |
| 3081 | // TODO(zhongyi): change this method to generate a connectivity probing packet |
| 3082 | // and let the caller to call writer to write the packet and handle write |
| 3083 | // status. |
| 3084 | bool QuicConnection::SendConnectivityProbingPacket( |
| 3085 | QuicPacketWriter* probing_writer, |
| 3086 | const QuicSocketAddress& peer_address) { |
| 3087 | return SendGenericPathProbePacket(probing_writer, peer_address, |
| 3088 | /* is_response= */ false); |
| 3089 | } |
| 3090 | |
| 3091 | void QuicConnection::SendConnectivityProbingResponsePacket( |
| 3092 | const QuicSocketAddress& peer_address) { |
| 3093 | SendGenericPathProbePacket(nullptr, peer_address, |
| 3094 | /* is_response= */ true); |
| 3095 | } |
| 3096 | |
| 3097 | bool QuicConnection::SendGenericPathProbePacket( |
| 3098 | QuicPacketWriter* probing_writer, |
| 3099 | const QuicSocketAddress& peer_address, |
| 3100 | bool is_response) { |
| 3101 | DCHECK(peer_address.IsInitialized()); |
| 3102 | if (!connected_) { |
| 3103 | QUIC_BUG << "Not sending connectivity probing packet as connection is " |
| 3104 | << "disconnected."; |
| 3105 | return false; |
| 3106 | } |
| 3107 | if (perspective_ == Perspective::IS_SERVER && probing_writer == nullptr) { |
| 3108 | // Server can use default packet writer to write packet. |
| 3109 | probing_writer = writer_; |
| 3110 | } |
| 3111 | DCHECK(probing_writer); |
| 3112 | |
| 3113 | if (probing_writer->IsWriteBlocked()) { |
| 3114 | QUIC_DLOG(INFO) |
| 3115 | << ENDPOINT |
| 3116 | << "Writer blocked when sending connectivity probing packet."; |
| 3117 | if (probing_writer == writer_) { |
| 3118 | // Visitor should not be write blocked if the probing writer is not the |
| 3119 | // default packet writer. |
| 3120 | visitor_->OnWriteBlocked(); |
| 3121 | } |
| 3122 | return true; |
| 3123 | } |
| 3124 | |
| 3125 | QUIC_DLOG(INFO) << ENDPOINT |
| 3126 | << "Sending path probe packet for connection_id = " |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 3127 | << server_connection_id_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3128 | |
| 3129 | OwningSerializedPacketPointer probing_packet; |
fkastenholz | 305e173 | 2019-06-18 05:01:22 -0700 | [diff] [blame] | 3130 | if (!VersionHasIetfQuicFrames(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3131 | // Non-IETF QUIC, generate a padded ping regardless of whether this is a |
| 3132 | // request or a response. |
| 3133 | probing_packet = packet_generator_.SerializeConnectivityProbingPacket(); |
| 3134 | } else { |
| 3135 | if (is_response) { |
| 3136 | // Respond using IETF QUIC PATH_RESPONSE frame |
| 3137 | if (IsCurrentPacketConnectivityProbing()) { |
| 3138 | // Pad the response if the request was a google connectivity probe |
| 3139 | // (padded). |
| 3140 | probing_packet = |
| 3141 | packet_generator_.SerializePathResponseConnectivityProbingPacket( |
| 3142 | received_path_challenge_payloads_, /* is_padded = */ true); |
| 3143 | received_path_challenge_payloads_.clear(); |
| 3144 | } else { |
| 3145 | // Do not pad the response if the path challenge was not a google |
| 3146 | // connectivity probe. |
| 3147 | probing_packet = |
| 3148 | packet_generator_.SerializePathResponseConnectivityProbingPacket( |
| 3149 | received_path_challenge_payloads_, |
| 3150 | /* is_padded = */ false); |
| 3151 | received_path_challenge_payloads_.clear(); |
| 3152 | } |
| 3153 | } else { |
| 3154 | // Request using IETF QUIC PATH_CHALLENGE frame |
| 3155 | transmitted_connectivity_probe_payload_ = |
| 3156 | QuicMakeUnique<QuicPathFrameBuffer>(); |
| 3157 | probing_packet = |
| 3158 | packet_generator_.SerializePathChallengeConnectivityProbingPacket( |
| 3159 | transmitted_connectivity_probe_payload_.get()); |
| 3160 | if (!probing_packet) { |
| 3161 | transmitted_connectivity_probe_payload_ = nullptr; |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | DCHECK_EQ(IsRetransmittable(*probing_packet), NO_RETRANSMITTABLE_DATA); |
| 3167 | |
| 3168 | const QuicTime packet_send_time = clock_->Now(); |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 3169 | QUIC_DVLOG(2) << ENDPOINT |
| 3170 | << "Sending path probe packet for server connection ID " |
| 3171 | << server_connection_id_ << std::endl |
| 3172 | << QuicTextUtils::HexDump( |
| 3173 | QuicStringPiece(probing_packet->encrypted_buffer, |
| 3174 | probing_packet->encrypted_length)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3175 | WriteResult result = probing_writer->WritePacket( |
| 3176 | probing_packet->encrypted_buffer, probing_packet->encrypted_length, |
| 3177 | self_address().host(), peer_address, per_packet_options_); |
| 3178 | |
| 3179 | // If using a batch writer and the probing packet is buffered, flush it. |
| 3180 | if (probing_writer->IsBatchMode() && result.status == WRITE_STATUS_OK && |
| 3181 | result.bytes_written == 0) { |
| 3182 | result = probing_writer->Flush(); |
| 3183 | } |
| 3184 | |
| 3185 | if (IsWriteError(result.status)) { |
| 3186 | // Write error for any connectivity probe should not affect the connection |
| 3187 | // as it is sent on a different path. |
| 3188 | QUIC_DLOG(INFO) << ENDPOINT << "Write probing packet failed with error = " |
| 3189 | << result.error_code; |
| 3190 | return false; |
| 3191 | } |
| 3192 | |
| 3193 | if (debug_visitor_ != nullptr) { |
| 3194 | debug_visitor_->OnPacketSent( |
| 3195 | *probing_packet, probing_packet->original_packet_number, |
| 3196 | probing_packet->transmission_type, packet_send_time); |
| 3197 | } |
| 3198 | |
| 3199 | // Call OnPacketSent regardless of the write result. |
| 3200 | sent_packet_manager_.OnPacketSent( |
| 3201 | probing_packet.get(), probing_packet->original_packet_number, |
| 3202 | packet_send_time, probing_packet->transmission_type, |
| 3203 | NO_RETRANSMITTABLE_DATA); |
| 3204 | |
| 3205 | if (IsWriteBlockedStatus(result.status)) { |
| 3206 | if (probing_writer == writer_) { |
| 3207 | // Visitor should not be write blocked if the probing writer is not the |
| 3208 | // default packet writer. |
| 3209 | visitor_->OnWriteBlocked(); |
| 3210 | } |
| 3211 | if (result.status == WRITE_STATUS_BLOCKED_DATA_BUFFERED) { |
| 3212 | QUIC_DLOG(INFO) << ENDPOINT << "Write probing packet blocked"; |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | return true; |
| 3217 | } |
| 3218 | |
| 3219 | void QuicConnection::DiscoverMtu() { |
| 3220 | DCHECK(!mtu_discovery_alarm_->IsSet()); |
| 3221 | |
| 3222 | // Check if the MTU has been already increased. |
| 3223 | if (mtu_discovery_target_ <= max_packet_length()) { |
| 3224 | return; |
| 3225 | } |
| 3226 | |
| 3227 | // Calculate the packet number of the next probe *before* sending the current |
| 3228 | // one. Otherwise, when SendMtuDiscoveryPacket() is called, |
| 3229 | // MaybeSetMtuAlarm() will not realize that the probe has been just sent, and |
| 3230 | // will reschedule this probe again. |
| 3231 | packets_between_mtu_probes_ *= 2; |
| 3232 | next_mtu_probe_at_ = sent_packet_manager_.GetLargestSentPacket() + |
| 3233 | packets_between_mtu_probes_ + 1; |
| 3234 | ++mtu_probe_count_; |
| 3235 | |
| 3236 | QUIC_DVLOG(2) << "Sending a path MTU discovery packet #" << mtu_probe_count_; |
| 3237 | SendMtuDiscoveryPacket(mtu_discovery_target_); |
| 3238 | |
| 3239 | DCHECK(!mtu_discovery_alarm_->IsSet()); |
| 3240 | } |
| 3241 | |
| 3242 | void QuicConnection::OnEffectivePeerMigrationValidated() { |
| 3243 | if (active_effective_peer_migration_type_ == NO_CHANGE) { |
| 3244 | QUIC_BUG << "No migration underway."; |
| 3245 | return; |
| 3246 | } |
| 3247 | highest_packet_sent_before_effective_peer_migration_.Clear(); |
| 3248 | active_effective_peer_migration_type_ = NO_CHANGE; |
| 3249 | } |
| 3250 | |
| 3251 | void QuicConnection::StartEffectivePeerMigration(AddressChangeType type) { |
| 3252 | // TODO(fayang): Currently, all peer address change type are allowed. Need to |
| 3253 | // add a method ShouldAllowPeerAddressChange(PeerAddressChangeType type) to |
| 3254 | // determine whether |type| is allowed. |
| 3255 | if (type == NO_CHANGE) { |
| 3256 | QUIC_BUG << "EffectivePeerMigration started without address change."; |
| 3257 | return; |
| 3258 | } |
| 3259 | QUIC_DLOG(INFO) << ENDPOINT << "Effective peer's ip:port changed from " |
| 3260 | << effective_peer_address_.ToString() << " to " |
| 3261 | << GetEffectivePeerAddressFromCurrentPacket().ToString() |
| 3262 | << ", address change type is " << type |
| 3263 | << ", migrating connection."; |
| 3264 | |
| 3265 | highest_packet_sent_before_effective_peer_migration_ = |
| 3266 | sent_packet_manager_.GetLargestSentPacket(); |
| 3267 | effective_peer_address_ = GetEffectivePeerAddressFromCurrentPacket(); |
| 3268 | active_effective_peer_migration_type_ = type; |
| 3269 | |
| 3270 | // TODO(wub): Move these calls to OnEffectivePeerMigrationValidated. |
| 3271 | OnConnectionMigration(type); |
| 3272 | } |
| 3273 | |
| 3274 | void QuicConnection::OnConnectionMigration(AddressChangeType addr_change_type) { |
| 3275 | visitor_->OnConnectionMigration(addr_change_type); |
| 3276 | sent_packet_manager_.OnConnectionMigration(addr_change_type); |
| 3277 | } |
| 3278 | |
| 3279 | bool QuicConnection::IsCurrentPacketConnectivityProbing() const { |
| 3280 | return is_current_packet_connectivity_probing_; |
| 3281 | } |
| 3282 | |
| 3283 | bool QuicConnection::ack_frame_updated() const { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3284 | return uber_received_packet_manager_.IsAckFrameUpdated(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | QuicStringPiece QuicConnection::GetCurrentPacket() { |
| 3288 | if (current_packet_data_ == nullptr) { |
| 3289 | return QuicStringPiece(); |
| 3290 | } |
| 3291 | return QuicStringPiece(current_packet_data_, last_size_); |
| 3292 | } |
| 3293 | |
| 3294 | bool QuicConnection::MaybeConsiderAsMemoryCorruption( |
| 3295 | const QuicStreamFrame& frame) { |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 3296 | if (QuicUtils::IsCryptoStreamId(transport_version(), frame.stream_id) || |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 3297 | last_decrypted_packet_level_ != ENCRYPTION_INITIAL) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3298 | return false; |
| 3299 | } |
| 3300 | |
| 3301 | if (perspective_ == Perspective::IS_SERVER && |
| 3302 | frame.data_length >= sizeof(kCHLO) && |
| 3303 | strncmp(frame.data_buffer, reinterpret_cast<const char*>(&kCHLO), |
| 3304 | sizeof(kCHLO)) == 0) { |
| 3305 | return true; |
| 3306 | } |
| 3307 | |
| 3308 | if (perspective_ == Perspective::IS_CLIENT && |
| 3309 | frame.data_length >= sizeof(kREJ) && |
| 3310 | strncmp(frame.data_buffer, reinterpret_cast<const char*>(&kREJ), |
| 3311 | sizeof(kREJ)) == 0) { |
| 3312 | return true; |
| 3313 | } |
| 3314 | |
| 3315 | return false; |
| 3316 | } |
| 3317 | |
| 3318 | void QuicConnection::MaybeSendProbingRetransmissions() { |
| 3319 | DCHECK(fill_up_link_during_probing_); |
| 3320 | |
| 3321 | // Don't send probing retransmissions until the handshake has completed. |
| 3322 | if (!sent_packet_manager_.handshake_confirmed() || |
| 3323 | sent_packet_manager().HasUnackedCryptoPackets()) { |
| 3324 | return; |
| 3325 | } |
| 3326 | |
| 3327 | if (probing_retransmission_pending_) { |
| 3328 | QUIC_BUG << "MaybeSendProbingRetransmissions is called while another call " |
| 3329 | "to it is already in progress"; |
| 3330 | return; |
| 3331 | } |
| 3332 | |
| 3333 | probing_retransmission_pending_ = true; |
| 3334 | SendProbingRetransmissions(); |
| 3335 | probing_retransmission_pending_ = false; |
| 3336 | } |
| 3337 | |
| 3338 | void QuicConnection::CheckIfApplicationLimited() { |
| 3339 | if (session_decides_what_to_write() && probing_retransmission_pending_) { |
| 3340 | return; |
| 3341 | } |
| 3342 | |
| 3343 | bool application_limited = |
| 3344 | queued_packets_.empty() && |
| 3345 | !sent_packet_manager_.HasPendingRetransmissions() && |
| 3346 | !visitor_->WillingAndAbleToWrite(); |
| 3347 | |
| 3348 | if (!application_limited) { |
| 3349 | return; |
| 3350 | } |
| 3351 | |
| 3352 | if (fill_up_link_during_probing_) { |
| 3353 | MaybeSendProbingRetransmissions(); |
| 3354 | if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 3355 | return; |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | sent_packet_manager_.OnApplicationLimited(); |
| 3360 | } |
| 3361 | |
| 3362 | void QuicConnection::UpdatePacketContent(PacketContent type) { |
| 3363 | if (current_packet_content_ == NOT_PADDED_PING) { |
| 3364 | // We have already learned the current packet is not a connectivity |
| 3365 | // probing packet. Peer migration should have already been started earlier |
| 3366 | // if needed. |
| 3367 | return; |
| 3368 | } |
| 3369 | |
| 3370 | if (type == NO_FRAMES_RECEIVED) { |
| 3371 | return; |
| 3372 | } |
| 3373 | |
| 3374 | if (type == FIRST_FRAME_IS_PING) { |
| 3375 | if (current_packet_content_ == NO_FRAMES_RECEIVED) { |
| 3376 | current_packet_content_ = FIRST_FRAME_IS_PING; |
| 3377 | return; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | // In Google QUIC we look for a packet with just a PING and PADDING. |
| 3382 | // For IETF QUIC, the packet must consist of just a PATH_CHALLENGE frame, |
| 3383 | // followed by PADDING. If the condition is met, mark things as |
| 3384 | // connectivity-probing, causing later processing to generate the correct |
| 3385 | // response. |
| 3386 | if (type == SECOND_FRAME_IS_PADDING && |
| 3387 | current_packet_content_ == FIRST_FRAME_IS_PING) { |
| 3388 | current_packet_content_ = SECOND_FRAME_IS_PADDING; |
| 3389 | if (perspective_ == Perspective::IS_SERVER) { |
| 3390 | is_current_packet_connectivity_probing_ = |
| 3391 | current_effective_peer_migration_type_ != NO_CHANGE; |
| 3392 | } else { |
| 3393 | is_current_packet_connectivity_probing_ = |
| 3394 | (last_packet_source_address_ != peer_address_) || |
| 3395 | (last_packet_destination_address_ != self_address_); |
| 3396 | } |
| 3397 | return; |
| 3398 | } |
| 3399 | |
| 3400 | current_packet_content_ = NOT_PADDED_PING; |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 3401 | if (GetLargestReceivedPacket().IsInitialized() && |
| 3402 | last_header_.packet_number == GetLargestReceivedPacket()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3403 | direct_peer_address_ = last_packet_source_address_; |
| 3404 | if (current_effective_peer_migration_type_ != NO_CHANGE) { |
| 3405 | // Start effective peer migration immediately when the current packet is |
| 3406 | // confirmed not a connectivity probing packet. |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 3407 | // TODO(fayang): When multiple packet number spaces is supported, only |
| 3408 | // start peer migration for the application data. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3409 | StartEffectivePeerMigration(current_effective_peer_migration_type_); |
| 3410 | } |
| 3411 | } |
| 3412 | current_effective_peer_migration_type_ = NO_CHANGE; |
| 3413 | } |
| 3414 | |
| 3415 | void QuicConnection::MaybeEnableSessionDecidesWhatToWrite() { |
| 3416 | // Only enable session decides what to write code path for version 42+, |
| 3417 | // because it needs the receiver to allow receiving overlapping stream data. |
| 3418 | const bool enable_session_decides_what_to_write = |
| 3419 | transport_version() > QUIC_VERSION_39; |
| 3420 | sent_packet_manager_.SetSessionDecideWhatToWrite( |
| 3421 | enable_session_decides_what_to_write); |
| 3422 | packet_generator_.SetCanSetTransmissionType( |
| 3423 | enable_session_decides_what_to_write); |
| 3424 | } |
| 3425 | |
| 3426 | void QuicConnection::PostProcessAfterAckFrame(bool send_stop_waiting, |
| 3427 | bool acked_new_packet) { |
| 3428 | if (no_stop_waiting_frames_) { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3429 | uber_received_packet_manager_.DontWaitForPacketsBefore( |
| 3430 | last_decrypted_packet_level_, |
fayang | 39915f9 | 2019-07-11 13:08:40 -0700 | [diff] [blame] | 3431 | SupportsMultiplePacketNumberSpaces() |
| 3432 | ? sent_packet_manager_.GetLargestPacketPeerKnowsIsAcked( |
| 3433 | last_decrypted_packet_level_) |
| 3434 | : sent_packet_manager_.largest_packet_peer_knows_is_acked()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3435 | } |
| 3436 | // Always reset the retransmission alarm when an ack comes in, since we now |
| 3437 | // have a better estimate of the current rtt than when it was set. |
| 3438 | SetRetransmissionAlarm(); |
| 3439 | MaybeSetPathDegradingAlarm(acked_new_packet); |
| 3440 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3441 | if (send_stop_waiting) { |
| 3442 | ++stop_waiting_count_; |
| 3443 | } else { |
| 3444 | stop_waiting_count_ = 0; |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | void QuicConnection::MaybeSetPathDegradingAlarm(bool acked_new_packet) { |
| 3449 | if (!sent_packet_manager_.HasInFlightPackets()) { |
| 3450 | // There are no retransmittable packets on the wire, so it's impossible to |
| 3451 | // say if the connection has degraded. |
| 3452 | path_degrading_alarm_->Cancel(); |
| 3453 | } else if (acked_new_packet) { |
| 3454 | // A previously-unacked packet has been acked, which means forward progress |
| 3455 | // has been made. Unset |is_path_degrading| if the path was considered as |
| 3456 | // degrading previously. Set/update the path degrading alarm. |
| 3457 | is_path_degrading_ = false; |
| 3458 | SetPathDegradingAlarm(); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | void QuicConnection::SetSessionNotifier( |
| 3463 | SessionNotifierInterface* session_notifier) { |
| 3464 | sent_packet_manager_.SetSessionNotifier(session_notifier); |
| 3465 | } |
| 3466 | |
| 3467 | void QuicConnection::SetDataProducer( |
| 3468 | QuicStreamFrameDataProducer* data_producer) { |
| 3469 | framer_.set_data_producer(data_producer); |
| 3470 | } |
| 3471 | |
| 3472 | void QuicConnection::SetTransmissionType(TransmissionType type) { |
| 3473 | packet_generator_.SetTransmissionType(type); |
| 3474 | } |
| 3475 | |
| 3476 | bool QuicConnection::session_decides_what_to_write() const { |
| 3477 | return sent_packet_manager_.session_decides_what_to_write(); |
| 3478 | } |
| 3479 | |
| 3480 | void QuicConnection::UpdateReleaseTimeIntoFuture() { |
| 3481 | DCHECK(supports_release_time_); |
| 3482 | |
| 3483 | release_time_into_future_ = std::max( |
| 3484 | QuicTime::Delta::FromMilliseconds(kMinReleaseTimeIntoFutureMs), |
| 3485 | std::min( |
| 3486 | QuicTime::Delta::FromMilliseconds( |
| 3487 | GetQuicFlag(FLAGS_quic_max_pace_time_into_future_ms)), |
| 3488 | sent_packet_manager_.GetRttStats()->SmoothedOrInitialRtt() * |
| 3489 | GetQuicFlag(FLAGS_quic_pace_time_into_future_srtt_fraction))); |
| 3490 | } |
| 3491 | |
| 3492 | void QuicConnection::ResetAckStates() { |
| 3493 | ack_alarm_->Cancel(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3494 | stop_waiting_count_ = 0; |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3495 | uber_received_packet_manager_.ResetAckStates(encryption_level_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | MessageStatus QuicConnection::SendMessage(QuicMessageId message_id, |
| 3499 | QuicMemSliceSpan message) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 3500 | if (!VersionSupportsMessageFrames(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3501 | QUIC_BUG << "MESSAGE frame is not supported for version " |
| 3502 | << transport_version(); |
| 3503 | return MESSAGE_STATUS_UNSUPPORTED; |
| 3504 | } |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 3505 | if (message.total_length() > GetCurrentLargestMessagePayload()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3506 | return MESSAGE_STATUS_TOO_LARGE; |
| 3507 | } |
| 3508 | if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
| 3509 | return MESSAGE_STATUS_BLOCKED; |
| 3510 | } |
fayang | a4b37b2 | 2019-06-18 13:37:47 -0700 | [diff] [blame] | 3511 | ScopedPacketFlusher flusher(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3512 | return packet_generator_.AddMessageFrame(message_id, message); |
| 3513 | } |
| 3514 | |
ianswett | b239f86 | 2019-04-05 09:15:06 -0700 | [diff] [blame] | 3515 | QuicPacketLength QuicConnection::GetCurrentLargestMessagePayload() const { |
| 3516 | return packet_generator_.GetCurrentLargestMessagePayload(); |
| 3517 | } |
| 3518 | |
| 3519 | QuicPacketLength QuicConnection::GetGuaranteedLargestMessagePayload() const { |
| 3520 | return packet_generator_.GetGuaranteedLargestMessagePayload(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3521 | } |
| 3522 | |
zhongyi | 546cc45 | 2019-04-12 15:27:49 -0700 | [diff] [blame] | 3523 | uint32_t QuicConnection::cipher_id() const { |
| 3524 | if (version().KnowsWhichDecrypterToUse()) { |
| 3525 | return framer_.GetDecrypter(last_decrypted_packet_level_)->cipher_id(); |
| 3526 | } |
| 3527 | return framer_.decrypter()->cipher_id(); |
| 3528 | } |
| 3529 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3530 | EncryptionLevel QuicConnection::GetConnectionCloseEncryptionLevel() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3531 | if (perspective_ == Perspective::IS_CLIENT) { |
| 3532 | return encryption_level_; |
| 3533 | } |
| 3534 | if (sent_packet_manager_.handshake_confirmed()) { |
| 3535 | // A forward secure packet has been received. |
dschinazi | 965ce09 | 2019-05-23 06:29:01 -0700 | [diff] [blame] | 3536 | QUIC_BUG_IF(encryption_level_ != ENCRYPTION_FORWARD_SECURE) |
| 3537 | << ENDPOINT << "Unexpected connection close encryption level " |
| 3538 | << QuicUtils::EncryptionLevelToString(encryption_level_); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3539 | return ENCRYPTION_FORWARD_SECURE; |
| 3540 | } |
| 3541 | if (framer_.HasEncrypterOfEncryptionLevel(ENCRYPTION_ZERO_RTT)) { |
| 3542 | if (encryption_level_ != ENCRYPTION_ZERO_RTT) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 3543 | if (VersionHasIetfInvariantHeader(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3544 | QUIC_CODE_COUNT(quic_wrong_encryption_level_connection_close_ietf); |
| 3545 | } else { |
| 3546 | QUIC_CODE_COUNT(quic_wrong_encryption_level_connection_close); |
| 3547 | } |
| 3548 | } |
| 3549 | return ENCRYPTION_ZERO_RTT; |
| 3550 | } |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 3551 | return ENCRYPTION_INITIAL; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3552 | } |
| 3553 | |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3554 | void QuicConnection::SendAllPendingAcks() { |
| 3555 | DCHECK(SupportsMultiplePacketNumberSpaces()); |
| 3556 | QUIC_DVLOG(1) << ENDPOINT << "Trying to send all pending ACKs"; |
| 3557 | // Latches current encryption level. |
| 3558 | const EncryptionLevel current_encryption_level = encryption_level_; |
| 3559 | for (int8_t i = INITIAL_DATA; i <= APPLICATION_DATA; ++i) { |
| 3560 | const QuicTime ack_timeout = uber_received_packet_manager_.GetAckTimeout( |
| 3561 | static_cast<PacketNumberSpace>(i)); |
| 3562 | if (!ack_timeout.IsInitialized() || |
| 3563 | ack_timeout > clock_->ApproximateNow()) { |
| 3564 | continue; |
| 3565 | } |
dschinazi | 05e62b1 | 2019-04-18 15:43:41 -0700 | [diff] [blame] | 3566 | if (!framer_.HasEncrypterOfEncryptionLevel( |
| 3567 | QuicUtils::GetEncryptionLevel(static_cast<PacketNumberSpace>(i)))) { |
| 3568 | QUIC_BUG << ENDPOINT << "Cannot send ACKs for packet number space " |
| 3569 | << static_cast<uint32_t>(i) |
| 3570 | << " without corresponding encrypter"; |
| 3571 | continue; |
| 3572 | } |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3573 | QUIC_DVLOG(1) << ENDPOINT << "Sending ACK of packet number space: " |
| 3574 | << static_cast<uint32_t>(i); |
| 3575 | // Switch to the appropriate encryption level. |
| 3576 | SetDefaultEncryptionLevel( |
| 3577 | QuicUtils::GetEncryptionLevel(static_cast<PacketNumberSpace>(i))); |
| 3578 | QuicFrames frames; |
| 3579 | frames.push_back(uber_received_packet_manager_.GetUpdatedAckFrame( |
| 3580 | static_cast<PacketNumberSpace>(i), clock_->ApproximateNow())); |
| 3581 | const bool flushed = packet_generator_.FlushAckFrame(frames); |
| 3582 | if (!flushed) { |
| 3583 | // Connection is write blocked. |
QUICHE team | db06153 | 2019-03-23 18:23:05 -0700 | [diff] [blame] | 3584 | QUIC_BUG_IF(!writer_->IsWriteBlocked()) |
| 3585 | << "Writer not blocked, but ACK not flushed for packet space:" << i; |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3586 | break; |
| 3587 | } |
| 3588 | ResetAckStates(); |
| 3589 | } |
| 3590 | // Restores encryption level. |
| 3591 | SetDefaultEncryptionLevel(current_encryption_level); |
| 3592 | |
| 3593 | const QuicTime timeout = |
| 3594 | uber_received_packet_manager_.GetEarliestAckTimeout(); |
| 3595 | if (timeout.IsInitialized()) { |
| 3596 | // If there are ACKs pending, re-arm ack alarm. |
| 3597 | ack_alarm_->Set(timeout); |
| 3598 | } |
| 3599 | // Only try to bundle retransmittable data with ACK frame if default |
| 3600 | // encryption level is forward secure. |
| 3601 | if (encryption_level_ != ENCRYPTION_FORWARD_SECURE || |
| 3602 | consecutive_num_packets_with_no_retransmittable_frames_ < |
| 3603 | max_consecutive_num_packets_with_no_retransmittable_frames_) { |
| 3604 | return; |
| 3605 | } |
| 3606 | consecutive_num_packets_with_no_retransmittable_frames_ = 0; |
| 3607 | if (packet_generator_.HasRetransmittableFrames() || |
| 3608 | visitor_->WillingAndAbleToWrite()) { |
| 3609 | // There are pending retransmittable frames. |
| 3610 | return; |
| 3611 | } |
| 3612 | |
| 3613 | visitor_->OnAckNeedsRetransmittableFrame(); |
| 3614 | } |
| 3615 | |
| 3616 | void QuicConnection::MaybeEnableMultiplePacketNumberSpacesSupport() { |
fayang | bf3d286 | 2019-06-20 14:13:44 -0700 | [diff] [blame] | 3617 | if (version().handshake_protocol != PROTOCOL_TLS1_3) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3618 | return; |
| 3619 | } |
| 3620 | QUIC_DVLOG(1) << ENDPOINT << "connection " << connection_id() |
| 3621 | << " supports multiple packet number spaces"; |
| 3622 | framer_.EnableMultiplePacketNumberSpacesSupport(); |
| 3623 | sent_packet_manager_.EnableMultiplePacketNumberSpacesSupport(); |
| 3624 | uber_received_packet_manager_.EnableMultiplePacketNumberSpacesSupport(); |
| 3625 | } |
| 3626 | |
| 3627 | bool QuicConnection::SupportsMultiplePacketNumberSpaces() const { |
| 3628 | return sent_packet_manager_.supports_multiple_packet_number_spaces(); |
| 3629 | } |
| 3630 | |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 3631 | void QuicConnection::SetLargestReceivedPacketWithAck( |
| 3632 | QuicPacketNumber new_value) { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3633 | if (SupportsMultiplePacketNumberSpaces()) { |
| 3634 | largest_seen_packets_with_ack_[QuicUtils::GetPacketNumberSpace( |
| 3635 | last_decrypted_packet_level_)] = new_value; |
| 3636 | } else { |
| 3637 | largest_seen_packet_with_ack_ = new_value; |
| 3638 | } |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | QuicPacketNumber QuicConnection::GetLargestReceivedPacketWithAck() const { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3642 | if (SupportsMultiplePacketNumberSpaces()) { |
| 3643 | return largest_seen_packets_with_ack_[QuicUtils::GetPacketNumberSpace( |
| 3644 | last_decrypted_packet_level_)]; |
| 3645 | } |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 3646 | return largest_seen_packet_with_ack_; |
| 3647 | } |
| 3648 | |
| 3649 | QuicPacketNumber QuicConnection::GetLargestSentPacket() const { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3650 | if (SupportsMultiplePacketNumberSpaces()) { |
| 3651 | return sent_packet_manager_.GetLargestSentPacket( |
| 3652 | last_decrypted_packet_level_); |
| 3653 | } |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 3654 | return sent_packet_manager_.GetLargestSentPacket(); |
| 3655 | } |
| 3656 | |
| 3657 | QuicPacketNumber QuicConnection::GetLargestAckedPacket() const { |
QUICHE team | cd09802 | 2019-03-22 18:49:55 -0700 | [diff] [blame] | 3658 | if (SupportsMultiplePacketNumberSpaces()) { |
| 3659 | return sent_packet_manager_.GetLargestAckedPacket( |
| 3660 | last_decrypted_packet_level_); |
| 3661 | } |
QUICHE team | 76e1c62 | 2019-03-19 14:36:39 -0700 | [diff] [blame] | 3662 | return sent_packet_manager_.GetLargestObserved(); |
| 3663 | } |
| 3664 | |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 3665 | QuicPacketNumber QuicConnection::GetLargestReceivedPacket() const { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3666 | return uber_received_packet_manager_.GetLargestObserved( |
| 3667 | last_decrypted_packet_level_); |
QUICHE team | 1f3de24 | 2019-03-20 07:24:48 -0700 | [diff] [blame] | 3668 | } |
| 3669 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3670 | size_t QuicConnection::min_received_before_ack_decimation() const { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3671 | return uber_received_packet_manager_.min_received_before_ack_decimation(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | void QuicConnection::set_min_received_before_ack_decimation(size_t new_value) { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3675 | uber_received_packet_manager_.set_min_received_before_ack_decimation( |
| 3676 | new_value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | size_t QuicConnection::ack_frequency_before_ack_decimation() const { |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3680 | return uber_received_packet_manager_.ack_frequency_before_ack_decimation(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | void QuicConnection::set_ack_frequency_before_ack_decimation(size_t new_value) { |
| 3684 | DCHECK_GT(new_value, 0u); |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3685 | uber_received_packet_manager_.set_ack_frequency_before_ack_decimation( |
| 3686 | new_value); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3687 | } |
| 3688 | |
fayang | 21ffb71 | 2019-05-16 08:39:26 -0700 | [diff] [blame] | 3689 | const QuicAckFrame& QuicConnection::ack_frame() const { |
| 3690 | if (SupportsMultiplePacketNumberSpaces()) { |
| 3691 | return uber_received_packet_manager_.GetAckFrame( |
| 3692 | QuicUtils::GetPacketNumberSpace(last_decrypted_packet_level_)); |
| 3693 | } |
fayang | 5f46430 | 2019-06-20 12:57:33 -0700 | [diff] [blame] | 3694 | return uber_received_packet_manager_.ack_frame(); |
fayang | 21ffb71 | 2019-05-16 08:39:26 -0700 | [diff] [blame] | 3695 | } |
| 3696 | |
dschinazi | 346b7ce | 2019-06-05 01:38:18 -0700 | [diff] [blame] | 3697 | void QuicConnection::set_client_connection_id( |
| 3698 | QuicConnectionId client_connection_id) { |
| 3699 | if (!version().SupportsClientConnectionIds()) { |
| 3700 | QUIC_BUG_IF(!client_connection_id.IsEmpty()) |
| 3701 | << ENDPOINT << "Attempted to use client connection ID " |
| 3702 | << client_connection_id << " with unsupported version " << version(); |
| 3703 | return; |
| 3704 | } |
| 3705 | client_connection_id_ = client_connection_id; |
| 3706 | client_connection_id_is_set_ = true; |
| 3707 | QUIC_DLOG(INFO) << ENDPOINT << "setting client connection ID to " |
| 3708 | << client_connection_id_ |
| 3709 | << " for connection with server connection ID " |
| 3710 | << server_connection_id_; |
| 3711 | packet_generator_.SetClientConnectionId(client_connection_id_); |
| 3712 | framer_.SetExpectedClientConnectionIdLength(client_connection_id_.length()); |
| 3713 | } |
| 3714 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 3715 | #undef ENDPOINT // undef for jumbo builds |
| 3716 | } // namespace quic |