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