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