| OLD | NEW |
| (Empty) |
| 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/quic/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> | |
| 15 #include <utility> | |
| 16 | |
| 17 #include "base/debug/stack_trace.h" | |
| 18 #include "base/format_macros.h" | |
| 19 #include "base/logging.h" | |
| 20 #include "base/stl_util.h" | |
| 21 #include "base/strings/stringprintf.h" | |
| 22 #include "net/base/net_errors.h" | |
| 23 #include "net/quic/crypto/quic_decrypter.h" | |
| 24 #include "net/quic/crypto/quic_encrypter.h" | |
| 25 #include "net/quic/iovector.h" | |
| 26 #include "net/quic/quic_bandwidth.h" | |
| 27 #include "net/quic/quic_config.h" | |
| 28 #include "net/quic/quic_fec_group.h" | |
| 29 #include "net/quic/quic_flags.h" | |
| 30 #include "net/quic/quic_packet_generator.h" | |
| 31 #include "net/quic/quic_utils.h" | |
| 32 | |
| 33 using base::StringPiece; | |
| 34 using base::StringPrintf; | |
| 35 using base::hash_map; | |
| 36 using base::hash_set; | |
| 37 using std::list; | |
| 38 using std::make_pair; | |
| 39 using std::max; | |
| 40 using std::min; | |
| 41 using std::numeric_limits; | |
| 42 using std::set; | |
| 43 using std::string; | |
| 44 using std::vector; | |
| 45 | |
| 46 namespace net { | |
| 47 | |
| 48 class QuicDecrypter; | |
| 49 class QuicEncrypter; | |
| 50 | |
| 51 namespace { | |
| 52 | |
| 53 // The largest gap in packets we'll accept without closing the connection. | |
| 54 // This will likely have to be tuned. | |
| 55 const QuicPacketSequenceNumber kMaxPacketGap = 5000; | |
| 56 | |
| 57 // Limit the number of FEC groups to two. If we get enough out of order packets | |
| 58 // that this becomes limiting, we can revisit. | |
| 59 const size_t kMaxFecGroups = 2; | |
| 60 | |
| 61 // Maximum number of acks received before sending an ack in response. | |
| 62 const QuicPacketCount kMaxPacketsReceivedBeforeAckSend = 20; | |
| 63 | |
| 64 // Maximum number of tracked packets. | |
| 65 const QuicPacketCount kMaxTrackedPackets = 5 * kMaxTcpCongestionWindow; | |
| 66 | |
| 67 bool Near(QuicPacketSequenceNumber a, QuicPacketSequenceNumber b) { | |
| 68 QuicPacketSequenceNumber delta = (a > b) ? a - b : b - a; | |
| 69 return delta <= kMaxPacketGap; | |
| 70 } | |
| 71 | |
| 72 // An alarm that is scheduled to send an ack if a timeout occurs. | |
| 73 class AckAlarm : public QuicAlarm::Delegate { | |
| 74 public: | |
| 75 explicit AckAlarm(QuicConnection* connection) | |
| 76 : connection_(connection) { | |
| 77 } | |
| 78 | |
| 79 QuicTime OnAlarm() override { | |
| 80 connection_->SendAck(); | |
| 81 return QuicTime::Zero(); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 QuicConnection* connection_; | |
| 86 | |
| 87 DISALLOW_COPY_AND_ASSIGN(AckAlarm); | |
| 88 }; | |
| 89 | |
| 90 // This alarm will be scheduled any time a data-bearing packet is sent out. | |
| 91 // When the alarm goes off, the connection checks to see if the oldest packets | |
| 92 // have been acked, and retransmit them if they have not. | |
| 93 class RetransmissionAlarm : public QuicAlarm::Delegate { | |
| 94 public: | |
| 95 explicit RetransmissionAlarm(QuicConnection* connection) | |
| 96 : connection_(connection) { | |
| 97 } | |
| 98 | |
| 99 QuicTime OnAlarm() override { | |
| 100 connection_->OnRetransmissionTimeout(); | |
| 101 return QuicTime::Zero(); | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 QuicConnection* connection_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(RetransmissionAlarm); | |
| 108 }; | |
| 109 | |
| 110 // An alarm that is scheduled when the sent scheduler requires a | |
| 111 // a delay before sending packets and fires when the packet may be sent. | |
| 112 class SendAlarm : public QuicAlarm::Delegate { | |
| 113 public: | |
| 114 explicit SendAlarm(QuicConnection* connection) | |
| 115 : connection_(connection) { | |
| 116 } | |
| 117 | |
| 118 QuicTime OnAlarm() override { | |
| 119 connection_->WriteIfNotBlocked(); | |
| 120 // Never reschedule the alarm, since CanWrite does that. | |
| 121 return QuicTime::Zero(); | |
| 122 } | |
| 123 | |
| 124 private: | |
| 125 QuicConnection* connection_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(SendAlarm); | |
| 128 }; | |
| 129 | |
| 130 class TimeoutAlarm : public QuicAlarm::Delegate { | |
| 131 public: | |
| 132 explicit TimeoutAlarm(QuicConnection* connection) | |
| 133 : connection_(connection) { | |
| 134 } | |
| 135 | |
| 136 QuicTime OnAlarm() override { | |
| 137 connection_->CheckForTimeout(); | |
| 138 // Never reschedule the alarm, since CheckForTimeout does that. | |
| 139 return QuicTime::Zero(); | |
| 140 } | |
| 141 | |
| 142 private: | |
| 143 QuicConnection* connection_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(TimeoutAlarm); | |
| 146 }; | |
| 147 | |
| 148 class PingAlarm : public QuicAlarm::Delegate { | |
| 149 public: | |
| 150 explicit PingAlarm(QuicConnection* connection) | |
| 151 : connection_(connection) { | |
| 152 } | |
| 153 | |
| 154 QuicTime OnAlarm() override { | |
| 155 connection_->SendPing(); | |
| 156 return QuicTime::Zero(); | |
| 157 } | |
| 158 | |
| 159 private: | |
| 160 QuicConnection* connection_; | |
| 161 | |
| 162 DISALLOW_COPY_AND_ASSIGN(PingAlarm); | |
| 163 }; | |
| 164 | |
| 165 // This alarm may be scheduled when an FEC protected packet is sent out. | |
| 166 class FecAlarm : public QuicAlarm::Delegate { | |
| 167 public: | |
| 168 explicit FecAlarm(QuicPacketGenerator* packet_generator) | |
| 169 : packet_generator_(packet_generator) {} | |
| 170 | |
| 171 QuicTime OnAlarm() override { | |
| 172 packet_generator_->OnFecTimeout(); | |
| 173 return QuicTime::Zero(); | |
| 174 } | |
| 175 | |
| 176 private: | |
| 177 QuicPacketGenerator* packet_generator_; | |
| 178 | |
| 179 DISALLOW_COPY_AND_ASSIGN(FecAlarm); | |
| 180 }; | |
| 181 | |
| 182 } // namespace | |
| 183 | |
| 184 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, | |
| 185 EncryptionLevel level) | |
| 186 : serialized_packet(packet), | |
| 187 encryption_level(level), | |
| 188 transmission_type(NOT_RETRANSMISSION), | |
| 189 original_sequence_number(0) { | |
| 190 } | |
| 191 | |
| 192 QuicConnection::QueuedPacket::QueuedPacket( | |
| 193 SerializedPacket packet, | |
| 194 EncryptionLevel level, | |
| 195 TransmissionType transmission_type, | |
| 196 QuicPacketSequenceNumber original_sequence_number) | |
| 197 : serialized_packet(packet), | |
| 198 encryption_level(level), | |
| 199 transmission_type(transmission_type), | |
| 200 original_sequence_number(original_sequence_number) { | |
| 201 } | |
| 202 | |
| 203 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | |
| 204 | |
| 205 QuicConnection::QuicConnection(QuicConnectionId connection_id, | |
| 206 IPEndPoint address, | |
| 207 QuicConnectionHelperInterface* helper, | |
| 208 const PacketWriterFactory& writer_factory, | |
| 209 bool owns_writer, | |
| 210 bool is_server, | |
| 211 bool is_secure, | |
| 212 const QuicVersionVector& supported_versions) | |
| 213 : framer_(supported_versions, | |
| 214 helper->GetClock()->ApproximateNow(), | |
| 215 is_server), | |
| 216 helper_(helper), | |
| 217 writer_(writer_factory.Create(this)), | |
| 218 owns_writer_(owns_writer), | |
| 219 encryption_level_(ENCRYPTION_NONE), | |
| 220 has_forward_secure_encrypter_(false), | |
| 221 first_required_forward_secure_packet_(0), | |
| 222 clock_(helper->GetClock()), | |
| 223 random_generator_(helper->GetRandomGenerator()), | |
| 224 connection_id_(connection_id), | |
| 225 peer_address_(address), | |
| 226 migrating_peer_port_(0), | |
| 227 last_packet_decrypted_(false), | |
| 228 last_packet_revived_(false), | |
| 229 last_size_(0), | |
| 230 last_decrypted_packet_level_(ENCRYPTION_NONE), | |
| 231 largest_seen_packet_with_ack_(0), | |
| 232 largest_seen_packet_with_stop_waiting_(0), | |
| 233 max_undecryptable_packets_(0), | |
| 234 pending_version_negotiation_packet_(false), | |
| 235 silent_close_enabled_(false), | |
| 236 received_packet_manager_(&stats_), | |
| 237 ack_queued_(false), | |
| 238 num_packets_received_since_last_ack_sent_(0), | |
| 239 stop_waiting_count_(0), | |
| 240 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), | |
| 241 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), | |
| 242 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), | |
| 243 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), | |
| 244 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), | |
| 245 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))), | |
| 246 packet_generator_(connection_id_, &framer_, random_generator_, this), | |
| 247 fec_alarm_(helper->CreateAlarm(new FecAlarm(&packet_generator_))), | |
| 248 idle_network_timeout_(QuicTime::Delta::Infinite()), | |
| 249 overall_connection_timeout_(QuicTime::Delta::Infinite()), | |
| 250 time_of_last_received_packet_(clock_->ApproximateNow()), | |
| 251 time_of_last_sent_new_packet_(clock_->ApproximateNow()), | |
| 252 sequence_number_of_last_sent_packet_(0), | |
| 253 sent_packet_manager_( | |
| 254 is_server, | |
| 255 clock_, | |
| 256 &stats_, | |
| 257 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic, | |
| 258 FLAGS_quic_use_time_loss_detection ? kTime : kNack, | |
| 259 is_secure), | |
| 260 version_negotiation_state_(START_NEGOTIATION), | |
| 261 is_server_(is_server), | |
| 262 connected_(true), | |
| 263 peer_ip_changed_(false), | |
| 264 peer_port_changed_(false), | |
| 265 self_ip_changed_(false), | |
| 266 self_port_changed_(false), | |
| 267 can_truncate_connection_ids_(true), | |
| 268 is_secure_(is_secure) { | |
| 269 DVLOG(1) << ENDPOINT << "Created connection with connection_id: " | |
| 270 << connection_id; | |
| 271 framer_.set_visitor(this); | |
| 272 framer_.set_received_entropy_calculator(&received_packet_manager_); | |
| 273 stats_.connection_creation_time = clock_->ApproximateNow(); | |
| 274 sent_packet_manager_.set_network_change_visitor(this); | |
| 275 if (FLAGS_quic_small_default_packet_size && is_server_) { | |
| 276 set_max_packet_length(kDefaultServerMaxPacketSize); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 QuicConnection::~QuicConnection() { | |
| 281 if (owns_writer_) { | |
| 282 delete writer_; | |
| 283 } | |
| 284 STLDeleteElements(&undecryptable_packets_); | |
| 285 STLDeleteValues(&group_map_); | |
| 286 for (QueuedPacketList::iterator it = queued_packets_.begin(); | |
| 287 it != queued_packets_.end(); ++it) { | |
| 288 delete it->serialized_packet.retransmittable_frames; | |
| 289 delete it->serialized_packet.packet; | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 void QuicConnection::SetFromConfig(const QuicConfig& config) { | |
| 294 if (config.negotiated()) { | |
| 295 SetNetworkTimeouts(QuicTime::Delta::Infinite(), | |
| 296 config.IdleConnectionStateLifetime()); | |
| 297 if (config.SilentClose()) { | |
| 298 silent_close_enabled_ = true; | |
| 299 } | |
| 300 } else { | |
| 301 SetNetworkTimeouts(config.max_time_before_crypto_handshake(), | |
| 302 config.max_idle_time_before_crypto_handshake()); | |
| 303 } | |
| 304 | |
| 305 sent_packet_manager_.SetFromConfig(config); | |
| 306 if (config.HasReceivedBytesForConnectionId() && | |
| 307 can_truncate_connection_ids_) { | |
| 308 packet_generator_.SetConnectionIdLength( | |
| 309 config.ReceivedBytesForConnectionId()); | |
| 310 } | |
| 311 max_undecryptable_packets_ = config.max_undecryptable_packets(); | |
| 312 } | |
| 313 | |
| 314 bool QuicConnection::ResumeConnectionState( | |
| 315 const CachedNetworkParameters& cached_network_params) { | |
| 316 return sent_packet_manager_.ResumeConnectionState(cached_network_params); | |
| 317 } | |
| 318 | |
| 319 void QuicConnection::SetNumOpenStreams(size_t num_streams) { | |
| 320 sent_packet_manager_.SetNumOpenStreams(num_streams); | |
| 321 } | |
| 322 | |
| 323 bool QuicConnection::SelectMutualVersion( | |
| 324 const QuicVersionVector& available_versions) { | |
| 325 // Try to find the highest mutual version by iterating over supported | |
| 326 // versions, starting with the highest, and breaking out of the loop once we | |
| 327 // find a matching version in the provided available_versions vector. | |
| 328 const QuicVersionVector& supported_versions = framer_.supported_versions(); | |
| 329 for (size_t i = 0; i < supported_versions.size(); ++i) { | |
| 330 const QuicVersion& version = supported_versions[i]; | |
| 331 if (std::find(available_versions.begin(), available_versions.end(), | |
| 332 version) != available_versions.end()) { | |
| 333 framer_.set_version(version); | |
| 334 return true; | |
| 335 } | |
| 336 } | |
| 337 | |
| 338 return false; | |
| 339 } | |
| 340 | |
| 341 void QuicConnection::OnError(QuicFramer* framer) { | |
| 342 // Packets that we can not or have not decrypted are dropped. | |
| 343 // TODO(rch): add stats to measure this. | |
| 344 if (!connected_ || last_packet_decrypted_ == false) { | |
| 345 return; | |
| 346 } | |
| 347 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error()); | |
| 348 } | |
| 349 | |
| 350 void QuicConnection::MaybeSetFecAlarm( | |
| 351 QuicPacketSequenceNumber sequence_number) { | |
| 352 if (fec_alarm_->IsSet()) { | |
| 353 return; | |
| 354 } | |
| 355 QuicTime::Delta timeout = packet_generator_.GetFecTimeout(sequence_number); | |
| 356 if (!timeout.IsInfinite()) { | |
| 357 fec_alarm_->Set(clock_->ApproximateNow().Add(timeout)); | |
| 358 } | |
| 359 } | |
| 360 | |
| 361 void QuicConnection::OnPacket() { | |
| 362 DCHECK(last_stream_frames_.empty() && | |
| 363 last_ack_frames_.empty() && | |
| 364 last_stop_waiting_frames_.empty() && | |
| 365 last_rst_frames_.empty() && | |
| 366 last_goaway_frames_.empty() && | |
| 367 last_window_update_frames_.empty() && | |
| 368 last_blocked_frames_.empty() && | |
| 369 last_ping_frames_.empty() && | |
| 370 last_close_frames_.empty()); | |
| 371 last_packet_decrypted_ = false; | |
| 372 last_packet_revived_ = false; | |
| 373 } | |
| 374 | |
| 375 void QuicConnection::OnPublicResetPacket( | |
| 376 const QuicPublicResetPacket& packet) { | |
| 377 if (debug_visitor_.get() != nullptr) { | |
| 378 debug_visitor_->OnPublicResetPacket(packet); | |
| 379 } | |
| 380 CloseConnection(QUIC_PUBLIC_RESET, true); | |
| 381 | |
| 382 DVLOG(1) << ENDPOINT << "Connection " << connection_id() | |
| 383 << " closed via QUIC_PUBLIC_RESET from peer."; | |
| 384 } | |
| 385 | |
| 386 bool QuicConnection::OnProtocolVersionMismatch(QuicVersion received_version) { | |
| 387 DVLOG(1) << ENDPOINT << "Received packet with mismatched version " | |
| 388 << received_version; | |
| 389 // TODO(satyamshekhar): Implement no server state in this mode. | |
| 390 if (!is_server_) { | |
| 391 LOG(DFATAL) << ENDPOINT << "Framer called OnProtocolVersionMismatch. " | |
| 392 << "Closing connection."; | |
| 393 CloseConnection(QUIC_INTERNAL_ERROR, false); | |
| 394 return false; | |
| 395 } | |
| 396 DCHECK_NE(version(), received_version); | |
| 397 | |
| 398 if (debug_visitor_.get() != nullptr) { | |
| 399 debug_visitor_->OnProtocolVersionMismatch(received_version); | |
| 400 } | |
| 401 | |
| 402 switch (version_negotiation_state_) { | |
| 403 case START_NEGOTIATION: | |
| 404 if (!framer_.IsSupportedVersion(received_version)) { | |
| 405 SendVersionNegotiationPacket(); | |
| 406 version_negotiation_state_ = NEGOTIATION_IN_PROGRESS; | |
| 407 return false; | |
| 408 } | |
| 409 break; | |
| 410 | |
| 411 case NEGOTIATION_IN_PROGRESS: | |
| 412 if (!framer_.IsSupportedVersion(received_version)) { | |
| 413 SendVersionNegotiationPacket(); | |
| 414 return false; | |
| 415 } | |
| 416 break; | |
| 417 | |
| 418 case NEGOTIATED_VERSION: | |
| 419 // Might be old packets that were sent by the client before the version | |
| 420 // was negotiated. Drop these. | |
| 421 return false; | |
| 422 | |
| 423 default: | |
| 424 DCHECK(false); | |
| 425 } | |
| 426 | |
| 427 version_negotiation_state_ = NEGOTIATED_VERSION; | |
| 428 visitor_->OnSuccessfulVersionNegotiation(received_version); | |
| 429 if (debug_visitor_.get() != nullptr) { | |
| 430 debug_visitor_->OnSuccessfulVersionNegotiation(received_version); | |
| 431 } | |
| 432 DVLOG(1) << ENDPOINT << "version negotiated " << received_version; | |
| 433 | |
| 434 // Store the new version. | |
| 435 framer_.set_version(received_version); | |
| 436 | |
| 437 // TODO(satyamshekhar): Store the sequence number of this packet and close the | |
| 438 // connection if we ever received a packet with incorrect version and whose | |
| 439 // sequence number is greater. | |
| 440 return true; | |
| 441 } | |
| 442 | |
| 443 // Handles version negotiation for client connection. | |
| 444 void QuicConnection::OnVersionNegotiationPacket( | |
| 445 const QuicVersionNegotiationPacket& packet) { | |
| 446 if (is_server_) { | |
| 447 LOG(DFATAL) << ENDPOINT << "Framer parsed VersionNegotiationPacket." | |
| 448 << " Closing connection."; | |
| 449 CloseConnection(QUIC_INTERNAL_ERROR, false); | |
| 450 return; | |
| 451 } | |
| 452 if (debug_visitor_.get() != nullptr) { | |
| 453 debug_visitor_->OnVersionNegotiationPacket(packet); | |
| 454 } | |
| 455 | |
| 456 if (version_negotiation_state_ != START_NEGOTIATION) { | |
| 457 // Possibly a duplicate version negotiation packet. | |
| 458 return; | |
| 459 } | |
| 460 | |
| 461 if (std::find(packet.versions.begin(), | |
| 462 packet.versions.end(), version()) != | |
| 463 packet.versions.end()) { | |
| 464 DLOG(WARNING) << ENDPOINT << "The server already supports our version. " | |
| 465 << "It should have accepted our connection."; | |
| 466 // Just drop the connection. | |
| 467 CloseConnection(QUIC_INVALID_VERSION_NEGOTIATION_PACKET, false); | |
| 468 return; | |
| 469 } | |
| 470 | |
| 471 if (!SelectMutualVersion(packet.versions)) { | |
| 472 SendConnectionCloseWithDetails(QUIC_INVALID_VERSION, | |
| 473 "no common version found"); | |
| 474 return; | |
| 475 } | |
| 476 | |
| 477 DVLOG(1) << ENDPOINT | |
| 478 << "Negotiated version: " << QuicVersionToString(version()); | |
| 479 server_supported_versions_ = packet.versions; | |
| 480 version_negotiation_state_ = NEGOTIATION_IN_PROGRESS; | |
| 481 RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION); | |
| 482 } | |
| 483 | |
| 484 void QuicConnection::OnRevivedPacket() { | |
| 485 } | |
| 486 | |
| 487 bool QuicConnection::OnUnauthenticatedPublicHeader( | |
| 488 const QuicPacketPublicHeader& header) { | |
| 489 return true; | |
| 490 } | |
| 491 | |
| 492 bool QuicConnection::OnUnauthenticatedHeader(const QuicPacketHeader& header) { | |
| 493 return true; | |
| 494 } | |
| 495 | |
| 496 void QuicConnection::OnDecryptedPacket(EncryptionLevel level) { | |
| 497 last_decrypted_packet_level_ = level; | |
| 498 last_packet_decrypted_ = true; | |
| 499 // If this packet was foward-secure encrypted and the forward-secure encrypter | |
| 500 // is not being used, start using it. | |
| 501 if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && | |
| 502 has_forward_secure_encrypter_ && level == ENCRYPTION_FORWARD_SECURE) { | |
| 503 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); | |
| 504 } | |
| 505 } | |
| 506 | |
| 507 bool QuicConnection::OnPacketHeader(const QuicPacketHeader& header) { | |
| 508 if (debug_visitor_.get() != nullptr) { | |
| 509 debug_visitor_->OnPacketHeader(header); | |
| 510 } | |
| 511 | |
| 512 if (!ProcessValidatedPacket()) { | |
| 513 return false; | |
| 514 } | |
| 515 | |
| 516 // Will be decrement below if we fall through to return true; | |
| 517 ++stats_.packets_dropped; | |
| 518 | |
| 519 if (header.public_header.connection_id != connection_id_) { | |
| 520 DVLOG(1) << ENDPOINT << "Ignoring packet from unexpected ConnectionId: " | |
| 521 << header.public_header.connection_id << " instead of " | |
| 522 << connection_id_; | |
| 523 if (debug_visitor_.get() != nullptr) { | |
| 524 debug_visitor_->OnIncorrectConnectionId( | |
| 525 header.public_header.connection_id); | |
| 526 } | |
| 527 return false; | |
| 528 } | |
| 529 | |
| 530 if (!Near(header.packet_sequence_number, | |
| 531 last_header_.packet_sequence_number)) { | |
| 532 DVLOG(1) << ENDPOINT << "Packet " << header.packet_sequence_number | |
| 533 << " out of bounds. Discarding"; | |
| 534 SendConnectionCloseWithDetails(QUIC_INVALID_PACKET_HEADER, | |
| 535 "Packet sequence number out of bounds"); | |
| 536 return false; | |
| 537 } | |
| 538 | |
| 539 // If this packet has already been seen, or that the sender | |
| 540 // has told us will not be retransmitted, then stop processing the packet. | |
| 541 if (!received_packet_manager_.IsAwaitingPacket( | |
| 542 header.packet_sequence_number)) { | |
| 543 DVLOG(1) << ENDPOINT << "Packet " << header.packet_sequence_number | |
| 544 << " no longer being waited for. Discarding."; | |
| 545 if (debug_visitor_.get() != nullptr) { | |
| 546 debug_visitor_->OnDuplicatePacket(header.packet_sequence_number); | |
| 547 } | |
| 548 return false; | |
| 549 } | |
| 550 | |
| 551 if (version_negotiation_state_ != NEGOTIATED_VERSION) { | |
| 552 if (is_server_) { | |
| 553 if (!header.public_header.version_flag) { | |
| 554 DLOG(WARNING) << ENDPOINT << "Packet " << header.packet_sequence_number | |
| 555 << " without version flag before version negotiated."; | |
| 556 // Packets should have the version flag till version negotiation is | |
| 557 // done. | |
| 558 CloseConnection(QUIC_INVALID_VERSION, false); | |
| 559 return false; | |
| 560 } else { | |
| 561 DCHECK_EQ(1u, header.public_header.versions.size()); | |
| 562 DCHECK_EQ(header.public_header.versions[0], version()); | |
| 563 version_negotiation_state_ = NEGOTIATED_VERSION; | |
| 564 visitor_->OnSuccessfulVersionNegotiation(version()); | |
| 565 if (debug_visitor_.get() != nullptr) { | |
| 566 debug_visitor_->OnSuccessfulVersionNegotiation(version()); | |
| 567 } | |
| 568 } | |
| 569 } else { | |
| 570 DCHECK(!header.public_header.version_flag); | |
| 571 // If the client gets a packet without the version flag from the server | |
| 572 // it should stop sending version since the version negotiation is done. | |
| 573 packet_generator_.StopSendingVersion(); | |
| 574 version_negotiation_state_ = NEGOTIATED_VERSION; | |
| 575 visitor_->OnSuccessfulVersionNegotiation(version()); | |
| 576 if (debug_visitor_.get() != nullptr) { | |
| 577 debug_visitor_->OnSuccessfulVersionNegotiation(version()); | |
| 578 } | |
| 579 } | |
| 580 } | |
| 581 | |
| 582 DCHECK_EQ(NEGOTIATED_VERSION, version_negotiation_state_); | |
| 583 | |
| 584 --stats_.packets_dropped; | |
| 585 DVLOG(1) << ENDPOINT << "Received packet header: " << header; | |
| 586 last_header_ = header; | |
| 587 DCHECK(connected_); | |
| 588 return true; | |
| 589 } | |
| 590 | |
| 591 void QuicConnection::OnFecProtectedPayload(StringPiece payload) { | |
| 592 DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group); | |
| 593 DCHECK_NE(0u, last_header_.fec_group); | |
| 594 QuicFecGroup* group = GetFecGroup(); | |
| 595 if (group != nullptr) { | |
| 596 group->Update(last_decrypted_packet_level_, last_header_, payload); | |
| 597 } | |
| 598 } | |
| 599 | |
| 600 bool QuicConnection::OnStreamFrame(const QuicStreamFrame& frame) { | |
| 601 DCHECK(connected_); | |
| 602 if (debug_visitor_.get() != nullptr) { | |
| 603 debug_visitor_->OnStreamFrame(frame); | |
| 604 } | |
| 605 if (frame.stream_id != kCryptoStreamId && | |
| 606 last_decrypted_packet_level_ == ENCRYPTION_NONE) { | |
| 607 DLOG(WARNING) << ENDPOINT | |
| 608 << "Received an unencrypted data frame: closing connection"; | |
| 609 SendConnectionClose(QUIC_UNENCRYPTED_STREAM_DATA); | |
| 610 return false; | |
| 611 } | |
| 612 last_stream_frames_.push_back(frame); | |
| 613 return true; | |
| 614 } | |
| 615 | |
| 616 bool QuicConnection::OnAckFrame(const QuicAckFrame& incoming_ack) { | |
| 617 DCHECK(connected_); | |
| 618 if (debug_visitor_.get() != nullptr) { | |
| 619 debug_visitor_->OnAckFrame(incoming_ack); | |
| 620 } | |
| 621 DVLOG(1) << ENDPOINT << "OnAckFrame: " << incoming_ack; | |
| 622 | |
| 623 if (last_header_.packet_sequence_number <= largest_seen_packet_with_ack_) { | |
| 624 DVLOG(1) << ENDPOINT << "Received an old ack frame: ignoring"; | |
| 625 return true; | |
| 626 } | |
| 627 | |
| 628 if (!ValidateAckFrame(incoming_ack)) { | |
| 629 SendConnectionClose(QUIC_INVALID_ACK_DATA); | |
| 630 return false; | |
| 631 } | |
| 632 | |
| 633 last_ack_frames_.push_back(incoming_ack); | |
| 634 return connected_; | |
| 635 } | |
| 636 | |
| 637 void QuicConnection::ProcessAckFrame(const QuicAckFrame& incoming_ack) { | |
| 638 largest_seen_packet_with_ack_ = last_header_.packet_sequence_number; | |
| 639 sent_packet_manager_.OnIncomingAck(incoming_ack, | |
| 640 time_of_last_received_packet_); | |
| 641 sent_entropy_manager_.ClearEntropyBefore( | |
| 642 sent_packet_manager_.least_packet_awaited_by_peer() - 1); | |
| 643 if (sent_packet_manager_.HasPendingRetransmissions()) { | |
| 644 WriteIfNotBlocked(); | |
| 645 } | |
| 646 | |
| 647 // Always reset the retransmission alarm when an ack comes in, since we now | |
| 648 // have a better estimate of the current rtt than when it was set. | |
| 649 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); | |
| 650 retransmission_alarm_->Update(retransmission_time, | |
| 651 QuicTime::Delta::FromMilliseconds(1)); | |
| 652 } | |
| 653 | |
| 654 void QuicConnection::ProcessStopWaitingFrame( | |
| 655 const QuicStopWaitingFrame& stop_waiting) { | |
| 656 largest_seen_packet_with_stop_waiting_ = last_header_.packet_sequence_number; | |
| 657 received_packet_manager_.UpdatePacketInformationSentByPeer(stop_waiting); | |
| 658 // Possibly close any FecGroups which are now irrelevant. | |
| 659 CloseFecGroupsBefore(stop_waiting.least_unacked + 1); | |
| 660 } | |
| 661 | |
| 662 bool QuicConnection::OnStopWaitingFrame(const QuicStopWaitingFrame& frame) { | |
| 663 DCHECK(connected_); | |
| 664 | |
| 665 if (last_header_.packet_sequence_number <= | |
| 666 largest_seen_packet_with_stop_waiting_) { | |
| 667 DVLOG(1) << ENDPOINT << "Received an old stop waiting frame: ignoring"; | |
| 668 return true; | |
| 669 } | |
| 670 | |
| 671 if (!ValidateStopWaitingFrame(frame)) { | |
| 672 SendConnectionClose(QUIC_INVALID_STOP_WAITING_DATA); | |
| 673 return false; | |
| 674 } | |
| 675 | |
| 676 if (debug_visitor_.get() != nullptr) { | |
| 677 debug_visitor_->OnStopWaitingFrame(frame); | |
| 678 } | |
| 679 | |
| 680 last_stop_waiting_frames_.push_back(frame); | |
| 681 return connected_; | |
| 682 } | |
| 683 | |
| 684 bool QuicConnection::OnPingFrame(const QuicPingFrame& frame) { | |
| 685 DCHECK(connected_); | |
| 686 if (debug_visitor_.get() != nullptr) { | |
| 687 debug_visitor_->OnPingFrame(frame); | |
| 688 } | |
| 689 last_ping_frames_.push_back(frame); | |
| 690 return true; | |
| 691 } | |
| 692 | |
| 693 bool QuicConnection::ValidateAckFrame(const QuicAckFrame& incoming_ack) { | |
| 694 if (incoming_ack.largest_observed > packet_generator_.sequence_number()) { | |
| 695 DLOG(ERROR) << ENDPOINT << "Peer's observed unsent packet:" | |
| 696 << incoming_ack.largest_observed << " vs " | |
| 697 << packet_generator_.sequence_number(); | |
| 698 // We got an error for data we have not sent. Error out. | |
| 699 return false; | |
| 700 } | |
| 701 | |
| 702 if (incoming_ack.largest_observed < sent_packet_manager_.largest_observed()) { | |
| 703 DLOG(ERROR) << ENDPOINT << "Peer's largest_observed packet decreased:" | |
| 704 << incoming_ack.largest_observed << " vs " | |
| 705 << sent_packet_manager_.largest_observed(); | |
| 706 // A new ack has a diminished largest_observed value. Error out. | |
| 707 // If this was an old packet, we wouldn't even have checked. | |
| 708 return false; | |
| 709 } | |
| 710 | |
| 711 if (!incoming_ack.missing_packets.empty() && | |
| 712 *incoming_ack.missing_packets.rbegin() > incoming_ack.largest_observed) { | |
| 713 DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " | |
| 714 << *incoming_ack.missing_packets.rbegin() | |
| 715 << " which is greater than largest observed: " | |
| 716 << incoming_ack.largest_observed; | |
| 717 return false; | |
| 718 } | |
| 719 | |
| 720 if (!incoming_ack.missing_packets.empty() && | |
| 721 *incoming_ack.missing_packets.begin() < | |
| 722 sent_packet_manager_.least_packet_awaited_by_peer()) { | |
| 723 DLOG(ERROR) << ENDPOINT << "Peer sent missing packet: " | |
| 724 << *incoming_ack.missing_packets.begin() | |
| 725 << " which is smaller than least_packet_awaited_by_peer_: " | |
| 726 << sent_packet_manager_.least_packet_awaited_by_peer(); | |
| 727 return false; | |
| 728 } | |
| 729 | |
| 730 if (!sent_entropy_manager_.IsValidEntropy( | |
| 731 incoming_ack.largest_observed, | |
| 732 incoming_ack.missing_packets, | |
| 733 incoming_ack.entropy_hash)) { | |
| 734 DLOG(ERROR) << ENDPOINT << "Peer sent invalid entropy."; | |
| 735 return false; | |
| 736 } | |
| 737 | |
| 738 for (SequenceNumberSet::const_iterator iter = | |
| 739 incoming_ack.revived_packets.begin(); | |
| 740 iter != incoming_ack.revived_packets.end(); ++iter) { | |
| 741 if (!ContainsKey(incoming_ack.missing_packets, *iter)) { | |
| 742 DLOG(ERROR) << ENDPOINT | |
| 743 << "Peer specified revived packet which was not missing."; | |
| 744 return false; | |
| 745 } | |
| 746 } | |
| 747 return true; | |
| 748 } | |
| 749 | |
| 750 bool QuicConnection::ValidateStopWaitingFrame( | |
| 751 const QuicStopWaitingFrame& stop_waiting) { | |
| 752 if (stop_waiting.least_unacked < | |
| 753 received_packet_manager_.peer_least_packet_awaiting_ack()) { | |
| 754 DLOG(ERROR) << ENDPOINT << "Peer's sent low least_unacked: " | |
| 755 << stop_waiting.least_unacked << " vs " | |
| 756 << received_packet_manager_.peer_least_packet_awaiting_ack(); | |
| 757 // We never process old ack frames, so this number should only increase. | |
| 758 return false; | |
| 759 } | |
| 760 | |
| 761 if (stop_waiting.least_unacked > | |
| 762 last_header_.packet_sequence_number) { | |
| 763 DLOG(ERROR) << ENDPOINT << "Peer sent least_unacked:" | |
| 764 << stop_waiting.least_unacked | |
| 765 << " greater than the enclosing packet sequence number:" | |
| 766 << last_header_.packet_sequence_number; | |
| 767 return false; | |
| 768 } | |
| 769 | |
| 770 return true; | |
| 771 } | |
| 772 | |
| 773 void QuicConnection::OnFecData(const QuicFecData& fec) { | |
| 774 DCHECK_EQ(IN_FEC_GROUP, last_header_.is_in_fec_group); | |
| 775 DCHECK_NE(0u, last_header_.fec_group); | |
| 776 QuicFecGroup* group = GetFecGroup(); | |
| 777 if (group != nullptr) { | |
| 778 group->UpdateFec(last_decrypted_packet_level_, | |
| 779 last_header_.packet_sequence_number, fec); | |
| 780 } | |
| 781 } | |
| 782 | |
| 783 bool QuicConnection::OnRstStreamFrame(const QuicRstStreamFrame& frame) { | |
| 784 DCHECK(connected_); | |
| 785 if (debug_visitor_.get() != nullptr) { | |
| 786 debug_visitor_->OnRstStreamFrame(frame); | |
| 787 } | |
| 788 DVLOG(1) << ENDPOINT << "Stream reset with error " | |
| 789 << QuicUtils::StreamErrorToString(frame.error_code); | |
| 790 last_rst_frames_.push_back(frame); | |
| 791 return connected_; | |
| 792 } | |
| 793 | |
| 794 bool QuicConnection::OnConnectionCloseFrame( | |
| 795 const QuicConnectionCloseFrame& frame) { | |
| 796 DCHECK(connected_); | |
| 797 if (debug_visitor_.get() != nullptr) { | |
| 798 debug_visitor_->OnConnectionCloseFrame(frame); | |
| 799 } | |
| 800 DVLOG(1) << ENDPOINT << "Connection " << connection_id() | |
| 801 << " closed with error " | |
| 802 << QuicUtils::ErrorToString(frame.error_code) | |
| 803 << " " << frame.error_details; | |
| 804 last_close_frames_.push_back(frame); | |
| 805 return connected_; | |
| 806 } | |
| 807 | |
| 808 bool QuicConnection::OnGoAwayFrame(const QuicGoAwayFrame& frame) { | |
| 809 DCHECK(connected_); | |
| 810 if (debug_visitor_.get() != nullptr) { | |
| 811 debug_visitor_->OnGoAwayFrame(frame); | |
| 812 } | |
| 813 DVLOG(1) << ENDPOINT << "Go away received with error " | |
| 814 << QuicUtils::ErrorToString(frame.error_code) | |
| 815 << " and reason:" << frame.reason_phrase; | |
| 816 last_goaway_frames_.push_back(frame); | |
| 817 return connected_; | |
| 818 } | |
| 819 | |
| 820 bool QuicConnection::OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) { | |
| 821 DCHECK(connected_); | |
| 822 if (debug_visitor_.get() != nullptr) { | |
| 823 debug_visitor_->OnWindowUpdateFrame(frame); | |
| 824 } | |
| 825 DVLOG(1) << ENDPOINT << "WindowUpdate received for stream: " | |
| 826 << frame.stream_id << " with byte offset: " << frame.byte_offset; | |
| 827 last_window_update_frames_.push_back(frame); | |
| 828 return connected_; | |
| 829 } | |
| 830 | |
| 831 bool QuicConnection::OnBlockedFrame(const QuicBlockedFrame& frame) { | |
| 832 DCHECK(connected_); | |
| 833 if (debug_visitor_.get() != nullptr) { | |
| 834 debug_visitor_->OnBlockedFrame(frame); | |
| 835 } | |
| 836 DVLOG(1) << ENDPOINT << "Blocked frame received for stream: " | |
| 837 << frame.stream_id; | |
| 838 last_blocked_frames_.push_back(frame); | |
| 839 return connected_; | |
| 840 } | |
| 841 | |
| 842 void QuicConnection::OnPacketComplete() { | |
| 843 // Don't do anything if this packet closed the connection. | |
| 844 if (!connected_) { | |
| 845 ClearLastFrames(); | |
| 846 return; | |
| 847 } | |
| 848 | |
| 849 DVLOG(1) << ENDPOINT << (last_packet_revived_ ? "Revived" : "Got") | |
| 850 << " packet " << last_header_.packet_sequence_number | |
| 851 << " with " << last_stream_frames_.size()<< " stream frames " | |
| 852 << last_ack_frames_.size() << " acks, " | |
| 853 << last_stop_waiting_frames_.size() << " stop_waiting, " | |
| 854 << last_rst_frames_.size() << " rsts, " | |
| 855 << last_goaway_frames_.size() << " goaways, " | |
| 856 << last_window_update_frames_.size() << " window updates, " | |
| 857 << last_blocked_frames_.size() << " blocked, " | |
| 858 << last_ping_frames_.size() << " pings, " | |
| 859 << last_close_frames_.size() << " closes, " | |
| 860 << "for " << last_header_.public_header.connection_id; | |
| 861 | |
| 862 ++num_packets_received_since_last_ack_sent_; | |
| 863 | |
| 864 // Call MaybeQueueAck() before recording the received packet, since we want | |
| 865 // to trigger an ack if the newly received packet was previously missing. | |
| 866 MaybeQueueAck(); | |
| 867 | |
| 868 // Record received or revived packet to populate ack info correctly before | |
| 869 // processing stream frames, since the processing may result in a response | |
| 870 // packet with a bundled ack. | |
| 871 if (last_packet_revived_) { | |
| 872 received_packet_manager_.RecordPacketRevived( | |
| 873 last_header_.packet_sequence_number); | |
| 874 } else { | |
| 875 received_packet_manager_.RecordPacketReceived( | |
| 876 last_size_, last_header_, time_of_last_received_packet_); | |
| 877 } | |
| 878 | |
| 879 if (!last_stream_frames_.empty()) { | |
| 880 visitor_->OnStreamFrames(last_stream_frames_); | |
| 881 } | |
| 882 | |
| 883 for (size_t i = 0; i < last_stream_frames_.size(); ++i) { | |
| 884 stats_.stream_bytes_received += | |
| 885 last_stream_frames_[i].data.TotalBufferSize(); | |
| 886 } | |
| 887 | |
| 888 // Process window updates, blocked, stream resets, acks, then congestion | |
| 889 // feedback. | |
| 890 if (!last_window_update_frames_.empty()) { | |
| 891 visitor_->OnWindowUpdateFrames(last_window_update_frames_); | |
| 892 } | |
| 893 if (!last_blocked_frames_.empty()) { | |
| 894 visitor_->OnBlockedFrames(last_blocked_frames_); | |
| 895 } | |
| 896 for (size_t i = 0; i < last_goaway_frames_.size(); ++i) { | |
| 897 visitor_->OnGoAway(last_goaway_frames_[i]); | |
| 898 } | |
| 899 for (size_t i = 0; i < last_rst_frames_.size(); ++i) { | |
| 900 visitor_->OnRstStream(last_rst_frames_[i]); | |
| 901 } | |
| 902 for (size_t i = 0; i < last_ack_frames_.size(); ++i) { | |
| 903 ProcessAckFrame(last_ack_frames_[i]); | |
| 904 } | |
| 905 for (size_t i = 0; i < last_stop_waiting_frames_.size(); ++i) { | |
| 906 ProcessStopWaitingFrame(last_stop_waiting_frames_[i]); | |
| 907 } | |
| 908 if (!last_close_frames_.empty()) { | |
| 909 CloseConnection(last_close_frames_[0].error_code, true); | |
| 910 DCHECK(!connected_); | |
| 911 } | |
| 912 | |
| 913 // If there are new missing packets to report, send an ack immediately. | |
| 914 if (received_packet_manager_.HasNewMissingPackets()) { | |
| 915 ack_queued_ = true; | |
| 916 ack_alarm_->Cancel(); | |
| 917 } | |
| 918 | |
| 919 UpdateStopWaitingCount(); | |
| 920 ClearLastFrames(); | |
| 921 MaybeCloseIfTooManyOutstandingPackets(); | |
| 922 } | |
| 923 | |
| 924 void QuicConnection::MaybeQueueAck() { | |
| 925 // If the incoming packet was missing, send an ack immediately. | |
| 926 ack_queued_ = received_packet_manager_.IsMissing( | |
| 927 last_header_.packet_sequence_number); | |
| 928 | |
| 929 if (!ack_queued_ && ShouldLastPacketInstigateAck()) { | |
| 930 if (ack_alarm_->IsSet()) { | |
| 931 ack_queued_ = true; | |
| 932 } else { | |
| 933 // Send an ack much more quickly for crypto handshake packets. | |
| 934 QuicTime::Delta delayed_ack_time = sent_packet_manager_.DelayedAckTime(); | |
| 935 ack_alarm_->Set(clock_->ApproximateNow().Add(delayed_ack_time)); | |
| 936 DVLOG(1) << "Ack timer set; next packet or timer will trigger ACK."; | |
| 937 } | |
| 938 } | |
| 939 | |
| 940 if (ack_queued_) { | |
| 941 ack_alarm_->Cancel(); | |
| 942 } | |
| 943 } | |
| 944 | |
| 945 void QuicConnection::ClearLastFrames() { | |
| 946 last_stream_frames_.clear(); | |
| 947 last_ack_frames_.clear(); | |
| 948 last_stop_waiting_frames_.clear(); | |
| 949 last_rst_frames_.clear(); | |
| 950 last_goaway_frames_.clear(); | |
| 951 last_window_update_frames_.clear(); | |
| 952 last_blocked_frames_.clear(); | |
| 953 last_ping_frames_.clear(); | |
| 954 last_close_frames_.clear(); | |
| 955 } | |
| 956 | |
| 957 void QuicConnection::MaybeCloseIfTooManyOutstandingPackets() { | |
| 958 // This occurs if we don't discard old packets we've sent fast enough. | |
| 959 // It's possible largest observed is less than least unacked. | |
| 960 if (sent_packet_manager_.largest_observed() > | |
| 961 (sent_packet_manager_.GetLeastUnacked() + kMaxTrackedPackets)) { | |
| 962 SendConnectionCloseWithDetails( | |
| 963 QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS, | |
| 964 StringPrintf("More than %" PRIu64 " outstanding.", kMaxTrackedPackets)); | |
| 965 } | |
| 966 // This occurs if there are received packet gaps and the peer does not raise | |
| 967 // the least unacked fast enough. | |
| 968 if (received_packet_manager_.NumTrackedPackets() > kMaxTrackedPackets) { | |
| 969 SendConnectionCloseWithDetails( | |
| 970 QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS, | |
| 971 StringPrintf("More than %" PRIu64 " outstanding.", kMaxTrackedPackets)); | |
| 972 } | |
| 973 } | |
| 974 | |
| 975 void QuicConnection::PopulateAckFrame(QuicAckFrame* ack) { | |
| 976 received_packet_manager_.UpdateReceivedPacketInfo(ack, | |
| 977 clock_->ApproximateNow()); | |
| 978 } | |
| 979 | |
| 980 void QuicConnection::PopulateStopWaitingFrame( | |
| 981 QuicStopWaitingFrame* stop_waiting) { | |
| 982 stop_waiting->least_unacked = GetLeastUnacked(); | |
| 983 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( | |
| 984 stop_waiting->least_unacked - 1); | |
| 985 } | |
| 986 | |
| 987 bool QuicConnection::ShouldLastPacketInstigateAck() const { | |
| 988 if (!last_stream_frames_.empty() || | |
| 989 !last_goaway_frames_.empty() || | |
| 990 !last_rst_frames_.empty() || | |
| 991 !last_window_update_frames_.empty() || | |
| 992 !last_blocked_frames_.empty() || | |
| 993 !last_ping_frames_.empty()) { | |
| 994 return true; | |
| 995 } | |
| 996 | |
| 997 if (!last_ack_frames_.empty() && last_ack_frames_.back().is_truncated) { | |
| 998 return true; | |
| 999 } | |
| 1000 // Always send an ack every 20 packets in order to allow the peer to discard | |
| 1001 // information from the SentPacketManager and provide an RTT measurement. | |
| 1002 if (num_packets_received_since_last_ack_sent_ >= | |
| 1003 kMaxPacketsReceivedBeforeAckSend) { | |
| 1004 return true; | |
| 1005 } | |
| 1006 return false; | |
| 1007 } | |
| 1008 | |
| 1009 void QuicConnection::UpdateStopWaitingCount() { | |
| 1010 if (last_ack_frames_.empty()) { | |
| 1011 return; | |
| 1012 } | |
| 1013 | |
| 1014 // If the peer is still waiting for a packet that we are no longer planning to | |
| 1015 // send, send an ack to raise the high water mark. | |
| 1016 if (!last_ack_frames_.back().missing_packets.empty() && | |
| 1017 GetLeastUnacked() > *last_ack_frames_.back().missing_packets.begin()) { | |
| 1018 ++stop_waiting_count_; | |
| 1019 } else { | |
| 1020 stop_waiting_count_ = 0; | |
| 1021 } | |
| 1022 } | |
| 1023 | |
| 1024 QuicPacketSequenceNumber QuicConnection::GetLeastUnacked() const { | |
| 1025 return sent_packet_manager_.GetLeastUnacked(); | |
| 1026 } | |
| 1027 | |
| 1028 void QuicConnection::MaybeSendInResponseToPacket() { | |
| 1029 if (!connected_) { | |
| 1030 return; | |
| 1031 } | |
| 1032 ScopedPacketBundler bundler(this, ack_queued_ ? SEND_ACK : NO_ACK); | |
| 1033 | |
| 1034 // Now that we have received an ack, we might be able to send packets which | |
| 1035 // are queued locally, or drain streams which are blocked. | |
| 1036 if (CanWrite(HAS_RETRANSMITTABLE_DATA)) { | |
| 1037 OnCanWrite(); | |
| 1038 } | |
| 1039 } | |
| 1040 | |
| 1041 void QuicConnection::SendVersionNegotiationPacket() { | |
| 1042 // TODO(alyssar): implement zero server state negotiation. | |
| 1043 pending_version_negotiation_packet_ = true; | |
| 1044 if (writer_->IsWriteBlocked()) { | |
| 1045 visitor_->OnWriteBlocked(); | |
| 1046 return; | |
| 1047 } | |
| 1048 DVLOG(1) << ENDPOINT << "Sending version negotiation packet: {" | |
| 1049 << QuicVersionVectorToString(framer_.supported_versions()) << "}"; | |
| 1050 scoped_ptr<QuicEncryptedPacket> version_packet( | |
| 1051 packet_generator_.SerializeVersionNegotiationPacket( | |
| 1052 framer_.supported_versions())); | |
| 1053 WriteResult result = writer_->WritePacket( | |
| 1054 version_packet->data(), version_packet->length(), | |
| 1055 self_address().address(), peer_address()); | |
| 1056 | |
| 1057 if (result.status == WRITE_STATUS_ERROR) { | |
| 1058 // We can't send an error as the socket is presumably borked. | |
| 1059 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | |
| 1060 return; | |
| 1061 } | |
| 1062 if (result.status == WRITE_STATUS_BLOCKED) { | |
| 1063 visitor_->OnWriteBlocked(); | |
| 1064 if (writer_->IsWriteBlockedDataBuffered()) { | |
| 1065 pending_version_negotiation_packet_ = false; | |
| 1066 } | |
| 1067 return; | |
| 1068 } | |
| 1069 | |
| 1070 pending_version_negotiation_packet_ = false; | |
| 1071 } | |
| 1072 | |
| 1073 QuicConsumedData QuicConnection::SendStreamData( | |
| 1074 QuicStreamId id, | |
| 1075 const IOVector& data, | |
| 1076 QuicStreamOffset offset, | |
| 1077 bool fin, | |
| 1078 FecProtection fec_protection, | |
| 1079 QuicAckNotifier::DelegateInterface* delegate) { | |
| 1080 if (!fin && data.Empty()) { | |
| 1081 LOG(DFATAL) << "Attempt to send empty stream frame"; | |
| 1082 return QuicConsumedData(0, false); | |
| 1083 } | |
| 1084 | |
| 1085 // Opportunistically bundle an ack with every outgoing packet. | |
| 1086 // Particularly, we want to bundle with handshake packets since we don't know | |
| 1087 // which decrypter will be used on an ack packet following a handshake | |
| 1088 // packet (a handshake packet from client to server could result in a REJ or a | |
| 1089 // SHLO from the server, leading to two different decrypters at the server.) | |
| 1090 // | |
| 1091 // TODO(jri): Note that ConsumeData may cause a response packet to be sent. | |
| 1092 // We may end up sending stale ack information if there are undecryptable | |
| 1093 // packets hanging around and/or there are revivable packets which may get | |
| 1094 // handled after this packet is sent. Change ScopedPacketBundler to do the | |
| 1095 // right thing: check ack_queued_, and then check undecryptable packets and | |
| 1096 // also if there is possibility of revival. Only bundle an ack if there's no | |
| 1097 // processing left that may cause received_info_ to change. | |
| 1098 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | |
| 1099 return packet_generator_.ConsumeData(id, data, offset, fin, fec_protection, | |
| 1100 delegate); | |
| 1101 } | |
| 1102 | |
| 1103 void QuicConnection::SendRstStream(QuicStreamId id, | |
| 1104 QuicRstStreamErrorCode error, | |
| 1105 QuicStreamOffset bytes_written) { | |
| 1106 // Opportunistically bundle an ack with this outgoing packet. | |
| 1107 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | |
| 1108 packet_generator_.AddControlFrame(QuicFrame(new QuicRstStreamFrame( | |
| 1109 id, AdjustErrorForVersion(error, version()), bytes_written))); | |
| 1110 } | |
| 1111 | |
| 1112 void QuicConnection::SendWindowUpdate(QuicStreamId id, | |
| 1113 QuicStreamOffset byte_offset) { | |
| 1114 // Opportunistically bundle an ack with this outgoing packet. | |
| 1115 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | |
| 1116 packet_generator_.AddControlFrame( | |
| 1117 QuicFrame(new QuicWindowUpdateFrame(id, byte_offset))); | |
| 1118 } | |
| 1119 | |
| 1120 void QuicConnection::SendBlocked(QuicStreamId id) { | |
| 1121 // Opportunistically bundle an ack with this outgoing packet. | |
| 1122 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | |
| 1123 packet_generator_.AddControlFrame(QuicFrame(new QuicBlockedFrame(id))); | |
| 1124 } | |
| 1125 | |
| 1126 const QuicConnectionStats& QuicConnection::GetStats() { | |
| 1127 const RttStats* rtt_stats = sent_packet_manager_.GetRttStats(); | |
| 1128 | |
| 1129 // Update rtt and estimated bandwidth. | |
| 1130 QuicTime::Delta min_rtt = rtt_stats->min_rtt(); | |
| 1131 if (min_rtt.IsZero()) { | |
| 1132 // If min RTT has not been set, use initial RTT instead. | |
| 1133 min_rtt = QuicTime::Delta::FromMicroseconds(rtt_stats->initial_rtt_us()); | |
| 1134 } | |
| 1135 stats_.min_rtt_us = min_rtt.ToMicroseconds(); | |
| 1136 | |
| 1137 QuicTime::Delta srtt = rtt_stats->smoothed_rtt(); | |
| 1138 if (srtt.IsZero()) { | |
| 1139 // If SRTT has not been set, use initial RTT instead. | |
| 1140 srtt = QuicTime::Delta::FromMicroseconds(rtt_stats->initial_rtt_us()); | |
| 1141 } | |
| 1142 stats_.srtt_us = srtt.ToMicroseconds(); | |
| 1143 | |
| 1144 stats_.estimated_bandwidth = sent_packet_manager_.BandwidthEstimate(); | |
| 1145 stats_.max_packet_size = packet_generator_.max_packet_length(); | |
| 1146 return stats_; | |
| 1147 } | |
| 1148 | |
| 1149 void QuicConnection::ProcessUdpPacket(const IPEndPoint& self_address, | |
| 1150 const IPEndPoint& peer_address, | |
| 1151 const QuicEncryptedPacket& packet) { | |
| 1152 if (!connected_) { | |
| 1153 return; | |
| 1154 } | |
| 1155 if (debug_visitor_.get() != nullptr) { | |
| 1156 debug_visitor_->OnPacketReceived(self_address, peer_address, packet); | |
| 1157 } | |
| 1158 last_size_ = packet.length(); | |
| 1159 | |
| 1160 CheckForAddressMigration(self_address, peer_address); | |
| 1161 | |
| 1162 stats_.bytes_received += packet.length(); | |
| 1163 ++stats_.packets_received; | |
| 1164 | |
| 1165 if (!framer_.ProcessPacket(packet)) { | |
| 1166 // If we are unable to decrypt this packet, it might be | |
| 1167 // because the CHLO or SHLO packet was lost. | |
| 1168 if (framer_.error() == QUIC_DECRYPTION_FAILURE) { | |
| 1169 if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && | |
| 1170 undecryptable_packets_.size() < max_undecryptable_packets_) { | |
| 1171 QueueUndecryptablePacket(packet); | |
| 1172 } else if (debug_visitor_.get() != nullptr) { | |
| 1173 debug_visitor_->OnUndecryptablePacket(); | |
| 1174 } | |
| 1175 } | |
| 1176 DVLOG(1) << ENDPOINT << "Unable to process packet. Last packet processed: " | |
| 1177 << last_header_.packet_sequence_number; | |
| 1178 return; | |
| 1179 } | |
| 1180 | |
| 1181 ++stats_.packets_processed; | |
| 1182 MaybeProcessUndecryptablePackets(); | |
| 1183 MaybeProcessRevivedPacket(); | |
| 1184 MaybeSendInResponseToPacket(); | |
| 1185 SetPingAlarm(); | |
| 1186 } | |
| 1187 | |
| 1188 void QuicConnection::CheckForAddressMigration( | |
| 1189 const IPEndPoint& self_address, const IPEndPoint& peer_address) { | |
| 1190 peer_ip_changed_ = false; | |
| 1191 peer_port_changed_ = false; | |
| 1192 self_ip_changed_ = false; | |
| 1193 self_port_changed_ = false; | |
| 1194 | |
| 1195 if (peer_address_.address().empty()) { | |
| 1196 peer_address_ = peer_address; | |
| 1197 } | |
| 1198 if (self_address_.address().empty()) { | |
| 1199 self_address_ = self_address; | |
| 1200 } | |
| 1201 | |
| 1202 if (!peer_address.address().empty() && !peer_address_.address().empty()) { | |
| 1203 peer_ip_changed_ = (peer_address.address() != peer_address_.address()); | |
| 1204 peer_port_changed_ = (peer_address.port() != peer_address_.port()); | |
| 1205 | |
| 1206 // Store in case we want to migrate connection in ProcessValidatedPacket. | |
| 1207 migrating_peer_port_ = peer_address.port(); | |
| 1208 } | |
| 1209 | |
| 1210 if (!self_address.address().empty() && !self_address_.address().empty()) { | |
| 1211 self_ip_changed_ = (self_address.address() != self_address_.address()); | |
| 1212 self_port_changed_ = (self_address.port() != self_address_.port()); | |
| 1213 } | |
| 1214 } | |
| 1215 | |
| 1216 void QuicConnection::OnCanWrite() { | |
| 1217 DCHECK(!writer_->IsWriteBlocked()); | |
| 1218 | |
| 1219 WriteQueuedPackets(); | |
| 1220 WritePendingRetransmissions(); | |
| 1221 | |
| 1222 // Sending queued packets may have caused the socket to become write blocked, | |
| 1223 // or the congestion manager to prohibit sending. If we've sent everything | |
| 1224 // we had queued and we're still not blocked, let the visitor know it can | |
| 1225 // write more. | |
| 1226 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { | |
| 1227 return; | |
| 1228 } | |
| 1229 | |
| 1230 { // Limit the scope of the bundler. ACK inclusion happens elsewhere. | |
| 1231 ScopedPacketBundler bundler(this, NO_ACK); | |
| 1232 visitor_->OnCanWrite(); | |
| 1233 } | |
| 1234 | |
| 1235 // After the visitor writes, it may have caused the socket to become write | |
| 1236 // blocked or the congestion manager to prohibit sending, so check again. | |
| 1237 if (visitor_->WillingAndAbleToWrite() && | |
| 1238 !resume_writes_alarm_->IsSet() && | |
| 1239 CanWrite(HAS_RETRANSMITTABLE_DATA)) { | |
| 1240 // We're not write blocked, but some stream didn't write out all of its | |
| 1241 // bytes. Register for 'immediate' resumption so we'll keep writing after | |
| 1242 // other connections and events have had a chance to use the thread. | |
| 1243 resume_writes_alarm_->Set(clock_->ApproximateNow()); | |
| 1244 } | |
| 1245 } | |
| 1246 | |
| 1247 void QuicConnection::WriteIfNotBlocked() { | |
| 1248 if (!writer_->IsWriteBlocked()) { | |
| 1249 OnCanWrite(); | |
| 1250 } | |
| 1251 } | |
| 1252 | |
| 1253 bool QuicConnection::ProcessValidatedPacket() { | |
| 1254 if (peer_ip_changed_ || self_ip_changed_ || self_port_changed_) { | |
| 1255 SendConnectionCloseWithDetails( | |
| 1256 QUIC_ERROR_MIGRATING_ADDRESS, | |
| 1257 "Neither IP address migration, nor self port migration are supported."); | |
| 1258 return false; | |
| 1259 } | |
| 1260 | |
| 1261 // Peer port migration is supported, do it now if port has changed. | |
| 1262 if (peer_port_changed_) { | |
| 1263 DVLOG(1) << ENDPOINT << "Peer's port changed from " | |
| 1264 << peer_address_.port() << " to " << migrating_peer_port_ | |
| 1265 << ", migrating connection."; | |
| 1266 peer_address_ = IPEndPoint(peer_address_.address(), migrating_peer_port_); | |
| 1267 } | |
| 1268 | |
| 1269 time_of_last_received_packet_ = clock_->Now(); | |
| 1270 DVLOG(1) << ENDPOINT << "time of last received packet: " | |
| 1271 << time_of_last_received_packet_.ToDebuggingValue(); | |
| 1272 | |
| 1273 if (is_server_ && encryption_level_ == ENCRYPTION_NONE && | |
| 1274 last_size_ > packet_generator_.max_packet_length()) { | |
| 1275 set_max_packet_length(last_size_); | |
| 1276 } | |
| 1277 return true; | |
| 1278 } | |
| 1279 | |
| 1280 void QuicConnection::WriteQueuedPackets() { | |
| 1281 DCHECK(!writer_->IsWriteBlocked()); | |
| 1282 | |
| 1283 if (pending_version_negotiation_packet_) { | |
| 1284 SendVersionNegotiationPacket(); | |
| 1285 } | |
| 1286 | |
| 1287 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); | |
| 1288 while (packet_iterator != queued_packets_.end() && | |
| 1289 WritePacket(&(*packet_iterator))) { | |
| 1290 packet_iterator = queued_packets_.erase(packet_iterator); | |
| 1291 } | |
| 1292 } | |
| 1293 | |
| 1294 void QuicConnection::WritePendingRetransmissions() { | |
| 1295 // Keep writing as long as there's a pending retransmission which can be | |
| 1296 // written. | |
| 1297 while (sent_packet_manager_.HasPendingRetransmissions()) { | |
| 1298 const QuicSentPacketManager::PendingRetransmission pending = | |
| 1299 sent_packet_manager_.NextPendingRetransmission(); | |
| 1300 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { | |
| 1301 break; | |
| 1302 } | |
| 1303 | |
| 1304 // Re-packetize the frames with a new sequence number for retransmission. | |
| 1305 // Retransmitted data packets do not use FEC, even when it's enabled. | |
| 1306 // Retransmitted packets use the same sequence number length as the | |
| 1307 // original. | |
| 1308 // Flush the packet generator before making a new packet. | |
| 1309 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | |
| 1310 // does not require the creator to be flushed. | |
| 1311 packet_generator_.FlushAllQueuedFrames(); | |
| 1312 SerializedPacket serialized_packet = packet_generator_.ReserializeAllFrames( | |
| 1313 pending.retransmittable_frames, pending.sequence_number_length); | |
| 1314 if (serialized_packet.packet == nullptr) { | |
| 1315 // We failed to serialize the packet, so close the connection. | |
| 1316 // CloseConnection does not send close packet, so no infinite loop here. | |
| 1317 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | |
| 1318 return; | |
| 1319 } | |
| 1320 | |
| 1321 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number | |
| 1322 << " as " << serialized_packet.sequence_number; | |
| 1323 SendOrQueuePacket( | |
| 1324 QueuedPacket(serialized_packet, | |
| 1325 pending.retransmittable_frames.encryption_level(), | |
| 1326 pending.transmission_type, | |
| 1327 pending.sequence_number)); | |
| 1328 } | |
| 1329 } | |
| 1330 | |
| 1331 void QuicConnection::RetransmitUnackedPackets( | |
| 1332 TransmissionType retransmission_type) { | |
| 1333 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); | |
| 1334 | |
| 1335 WriteIfNotBlocked(); | |
| 1336 } | |
| 1337 | |
| 1338 void QuicConnection::NeuterUnencryptedPackets() { | |
| 1339 sent_packet_manager_.NeuterUnencryptedPackets(); | |
| 1340 // This may have changed the retransmission timer, so re-arm it. | |
| 1341 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); | |
| 1342 retransmission_alarm_->Update(retransmission_time, | |
| 1343 QuicTime::Delta::FromMilliseconds(1)); | |
| 1344 } | |
| 1345 | |
| 1346 bool QuicConnection::ShouldGeneratePacket( | |
| 1347 TransmissionType transmission_type, | |
| 1348 HasRetransmittableData retransmittable, | |
| 1349 IsHandshake handshake) { | |
| 1350 // We should serialize handshake packets immediately to ensure that they | |
| 1351 // end up sent at the right encryption level. | |
| 1352 if (handshake == IS_HANDSHAKE) { | |
| 1353 return true; | |
| 1354 } | |
| 1355 | |
| 1356 return CanWrite(retransmittable); | |
| 1357 } | |
| 1358 | |
| 1359 bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) { | |
| 1360 if (!connected_) { | |
| 1361 return false; | |
| 1362 } | |
| 1363 | |
| 1364 if (writer_->IsWriteBlocked()) { | |
| 1365 visitor_->OnWriteBlocked(); | |
| 1366 return false; | |
| 1367 } | |
| 1368 | |
| 1369 QuicTime now = clock_->Now(); | |
| 1370 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( | |
| 1371 now, retransmittable); | |
| 1372 if (delay.IsInfinite()) { | |
| 1373 send_alarm_->Cancel(); | |
| 1374 return false; | |
| 1375 } | |
| 1376 | |
| 1377 // If the scheduler requires a delay, then we can not send this packet now. | |
| 1378 if (!delay.IsZero()) { | |
| 1379 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); | |
| 1380 DVLOG(1) << ENDPOINT << "Delaying sending " << delay.ToMilliseconds() | |
| 1381 << "ms"; | |
| 1382 return false; | |
| 1383 } | |
| 1384 send_alarm_->Cancel(); | |
| 1385 return true; | |
| 1386 } | |
| 1387 | |
| 1388 bool QuicConnection::WritePacket(QueuedPacket* packet) { | |
| 1389 if (!WritePacketInner(packet)) { | |
| 1390 return false; | |
| 1391 } | |
| 1392 delete packet->serialized_packet.retransmittable_frames; | |
| 1393 delete packet->serialized_packet.packet; | |
| 1394 packet->serialized_packet.retransmittable_frames = nullptr; | |
| 1395 packet->serialized_packet.packet = nullptr; | |
| 1396 return true; | |
| 1397 } | |
| 1398 | |
| 1399 bool QuicConnection::WritePacketInner(QueuedPacket* packet) { | |
| 1400 if (ShouldDiscardPacket(*packet)) { | |
| 1401 ++stats_.packets_discarded; | |
| 1402 return true; | |
| 1403 } | |
| 1404 // Connection close packets are encrypted and saved, so don't exit early. | |
| 1405 const bool is_connection_close = IsConnectionClose(*packet); | |
| 1406 if (writer_->IsWriteBlocked() && !is_connection_close) { | |
| 1407 return false; | |
| 1408 } | |
| 1409 | |
| 1410 QuicPacketSequenceNumber sequence_number = | |
| 1411 packet->serialized_packet.sequence_number; | |
| 1412 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); | |
| 1413 sequence_number_of_last_sent_packet_ = sequence_number; | |
| 1414 | |
| 1415 QuicEncryptedPacket* encrypted = packet->serialized_packet.packet; | |
| 1416 // Connection close packets are eventually owned by TimeWaitListManager. | |
| 1417 // Others are deleted at the end of this call. | |
| 1418 if (is_connection_close) { | |
| 1419 DCHECK(connection_close_packet_.get() == nullptr); | |
| 1420 connection_close_packet_.reset(encrypted); | |
| 1421 packet->serialized_packet.packet = nullptr; | |
| 1422 // This assures we won't try to write *forced* packets when blocked. | |
| 1423 // Return true to stop processing. | |
| 1424 if (writer_->IsWriteBlocked()) { | |
| 1425 visitor_->OnWriteBlocked(); | |
| 1426 return true; | |
| 1427 } | |
| 1428 } | |
| 1429 | |
| 1430 if (!FLAGS_quic_allow_oversized_packets_for_test) { | |
| 1431 DCHECK_LE(encrypted->length(), kMaxPacketSize); | |
| 1432 } | |
| 1433 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); | |
| 1434 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : " | |
| 1435 << (packet->serialized_packet.is_fec_packet | |
| 1436 ? "FEC " | |
| 1437 : (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA | |
| 1438 ? "data bearing " | |
| 1439 : " ack only ")) << ", encryption level: " | |
| 1440 << QuicUtils::EncryptionLevelToString(packet->encryption_level) | |
| 1441 << ", encrypted length:" << encrypted->length(); | |
| 1442 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | |
| 1443 << QuicUtils::StringToHexASCIIDump(encrypted->AsStringPiece()); | |
| 1444 | |
| 1445 QuicTime packet_send_time = QuicTime::Zero(); | |
| 1446 if (FLAGS_quic_record_send_time_before_write) { | |
| 1447 // Measure the RTT from before the write begins to avoid underestimating the | |
| 1448 // min_rtt_, especially in cases where the thread blocks or gets swapped out | |
| 1449 // during the WritePacket below. | |
| 1450 packet_send_time = clock_->Now(); | |
| 1451 } | |
| 1452 WriteResult result = writer_->WritePacket(encrypted->data(), | |
| 1453 encrypted->length(), | |
| 1454 self_address().address(), | |
| 1455 peer_address()); | |
| 1456 if (result.error_code == ERR_IO_PENDING) { | |
| 1457 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); | |
| 1458 } | |
| 1459 | |
| 1460 if (result.status == WRITE_STATUS_BLOCKED) { | |
| 1461 visitor_->OnWriteBlocked(); | |
| 1462 // If the socket buffers the the data, then the packet should not | |
| 1463 // be queued and sent again, which would result in an unnecessary | |
| 1464 // duplicate packet being sent. The helper must call OnCanWrite | |
| 1465 // when the write completes, and OnWriteError if an error occurs. | |
| 1466 if (!writer_->IsWriteBlockedDataBuffered()) { | |
| 1467 return false; | |
| 1468 } | |
| 1469 } | |
| 1470 if (!FLAGS_quic_record_send_time_before_write) { | |
| 1471 packet_send_time = clock_->Now(); | |
| 1472 } | |
| 1473 if (!packet_send_time.IsInitialized()) { | |
| 1474 // TODO(jokulik): This is only needed because of the two code paths for | |
| 1475 // initializing packet_send_time. Once "quic_record_send_time_before_write" | |
| 1476 // is deprecated, this check can be removed. | |
| 1477 LOG(DFATAL) << "The packet send time should never be zero. " | |
| 1478 << "This is a programming bug, please report it."; | |
| 1479 } | |
| 1480 if (result.status != WRITE_STATUS_ERROR && debug_visitor_.get() != nullptr) { | |
| 1481 // Pass the write result to the visitor. | |
| 1482 debug_visitor_->OnPacketSent(packet->serialized_packet, | |
| 1483 packet->original_sequence_number, | |
| 1484 packet->encryption_level, | |
| 1485 packet->transmission_type, | |
| 1486 *encrypted, | |
| 1487 packet_send_time); | |
| 1488 } | |
| 1489 if (packet->transmission_type == NOT_RETRANSMISSION) { | |
| 1490 time_of_last_sent_new_packet_ = packet_send_time; | |
| 1491 } | |
| 1492 SetPingAlarm(); | |
| 1493 MaybeSetFecAlarm(sequence_number); | |
| 1494 DVLOG(1) << ENDPOINT << "time " | |
| 1495 << (FLAGS_quic_record_send_time_before_write ? | |
| 1496 "we began writing " : "we finished writing ") | |
| 1497 << "last sent packet: " | |
| 1498 << packet_send_time.ToDebuggingValue(); | |
| 1499 | |
| 1500 // TODO(ianswett): Change the sequence number length and other packet creator | |
| 1501 // options by a more explicit API than setting a struct value directly, | |
| 1502 // perhaps via the NetworkChangeVisitor. | |
| 1503 packet_generator_.UpdateSequenceNumberLength( | |
| 1504 sent_packet_manager_.least_packet_awaited_by_peer(), | |
| 1505 sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); | |
| 1506 | |
| 1507 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( | |
| 1508 &packet->serialized_packet, | |
| 1509 packet->original_sequence_number, | |
| 1510 packet_send_time, | |
| 1511 encrypted->length(), | |
| 1512 packet->transmission_type, | |
| 1513 IsRetransmittable(*packet)); | |
| 1514 | |
| 1515 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | |
| 1516 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), | |
| 1517 QuicTime::Delta::FromMilliseconds(1)); | |
| 1518 } | |
| 1519 | |
| 1520 stats_.bytes_sent += result.bytes_written; | |
| 1521 ++stats_.packets_sent; | |
| 1522 if (packet->transmission_type != NOT_RETRANSMISSION) { | |
| 1523 stats_.bytes_retransmitted += result.bytes_written; | |
| 1524 ++stats_.packets_retransmitted; | |
| 1525 } | |
| 1526 | |
| 1527 if (result.status == WRITE_STATUS_ERROR) { | |
| 1528 OnWriteError(result.error_code); | |
| 1529 DLOG(ERROR) << ENDPOINT << "failed writing " << encrypted->length() | |
| 1530 << "bytes " | |
| 1531 << " from host " << self_address().ToStringWithoutPort() | |
| 1532 << " to address " << peer_address().ToString(); | |
| 1533 return false; | |
| 1534 } | |
| 1535 | |
| 1536 return true; | |
| 1537 } | |
| 1538 | |
| 1539 bool QuicConnection::ShouldDiscardPacket(const QueuedPacket& packet) { | |
| 1540 if (!connected_) { | |
| 1541 DVLOG(1) << ENDPOINT | |
| 1542 << "Not sending packet as connection is disconnected."; | |
| 1543 return true; | |
| 1544 } | |
| 1545 | |
| 1546 QuicPacketSequenceNumber sequence_number = | |
| 1547 packet.serialized_packet.sequence_number; | |
| 1548 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && | |
| 1549 packet.encryption_level == ENCRYPTION_NONE) { | |
| 1550 // Drop packets that are NULL encrypted since the peer won't accept them | |
| 1551 // anymore. | |
| 1552 DVLOG(1) << ENDPOINT << "Dropping NULL encrypted packet: " | |
| 1553 << sequence_number << " since the connection is forward secure."; | |
| 1554 return true; | |
| 1555 } | |
| 1556 | |
| 1557 // If a retransmission has been acked before sending, don't send it. | |
| 1558 // This occurs if a packet gets serialized, queued, then discarded. | |
| 1559 if (packet.transmission_type != NOT_RETRANSMISSION && | |
| 1560 (!sent_packet_manager_.IsUnacked(packet.original_sequence_number) || | |
| 1561 !sent_packet_manager_.HasRetransmittableFrames( | |
| 1562 packet.original_sequence_number))) { | |
| 1563 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number | |
| 1564 << " A previous transmission was acked while write blocked."; | |
| 1565 return true; | |
| 1566 } | |
| 1567 | |
| 1568 return false; | |
| 1569 } | |
| 1570 | |
| 1571 void QuicConnection::OnWriteError(int error_code) { | |
| 1572 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code | |
| 1573 << " (" << ErrorToString(error_code) << ")"; | |
| 1574 // We can't send an error as the socket is presumably borked. | |
| 1575 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | |
| 1576 } | |
| 1577 | |
| 1578 void QuicConnection::OnSerializedPacket( | |
| 1579 const SerializedPacket& serialized_packet) { | |
| 1580 if (serialized_packet.packet == nullptr) { | |
| 1581 // We failed to serialize the packet, so close the connection. | |
| 1582 // CloseConnection does not send close packet, so no infinite loop here. | |
| 1583 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | |
| 1584 return; | |
| 1585 } | |
| 1586 if (serialized_packet.retransmittable_frames) { | |
| 1587 serialized_packet.retransmittable_frames-> | |
| 1588 set_encryption_level(encryption_level_); | |
| 1589 | |
| 1590 sent_packet_manager_.OnSerializedPacket(serialized_packet); | |
| 1591 } | |
| 1592 if (serialized_packet.is_fec_packet && fec_alarm_->IsSet()) { | |
| 1593 // If an FEC packet is serialized with the FEC alarm set, cancel the alarm. | |
| 1594 fec_alarm_->Cancel(); | |
| 1595 } | |
| 1596 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_)); | |
| 1597 } | |
| 1598 | |
| 1599 void QuicConnection::OnCongestionWindowChange() { | |
| 1600 packet_generator_.OnCongestionWindowChange( | |
| 1601 sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); | |
| 1602 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); | |
| 1603 } | |
| 1604 | |
| 1605 void QuicConnection::OnRttChange() { | |
| 1606 // Uses the connection's smoothed RTT. If zero, uses initial_rtt. | |
| 1607 QuicTime::Delta rtt = sent_packet_manager_.GetRttStats()->smoothed_rtt(); | |
| 1608 if (rtt.IsZero()) { | |
| 1609 rtt = QuicTime::Delta::FromMicroseconds( | |
| 1610 sent_packet_manager_.GetRttStats()->initial_rtt_us()); | |
| 1611 } | |
| 1612 packet_generator_.OnRttChange(rtt); | |
| 1613 } | |
| 1614 | |
| 1615 void QuicConnection::OnHandshakeComplete() { | |
| 1616 sent_packet_manager_.SetHandshakeConfirmed(); | |
| 1617 // The client should immediately ack the SHLO to confirm the handshake is | |
| 1618 // complete with the server. | |
| 1619 if (!is_server_ && !ack_queued_) { | |
| 1620 ack_alarm_->Cancel(); | |
| 1621 ack_alarm_->Set(clock_->ApproximateNow()); | |
| 1622 } | |
| 1623 } | |
| 1624 | |
| 1625 void QuicConnection::SendOrQueuePacket(QueuedPacket packet) { | |
| 1626 // The caller of this function is responsible for checking CanWrite(). | |
| 1627 if (packet.serialized_packet.packet == nullptr) { | |
| 1628 LOG(DFATAL) | |
| 1629 << "packet.serialized_packet.packet == nullptr in to SendOrQueuePacket"; | |
| 1630 return; | |
| 1631 } | |
| 1632 | |
| 1633 sent_entropy_manager_.RecordPacketEntropyHash( | |
| 1634 packet.serialized_packet.sequence_number, | |
| 1635 packet.serialized_packet.entropy_hash); | |
| 1636 if (!WritePacket(&packet)) { | |
| 1637 queued_packets_.push_back(packet); | |
| 1638 } | |
| 1639 | |
| 1640 // If a forward-secure encrypter is available but is not being used and the | |
| 1641 // next sequence number is the first packet which requires | |
| 1642 // forward security, start using the forward-secure encrypter. | |
| 1643 if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && | |
| 1644 has_forward_secure_encrypter_ && | |
| 1645 packet.serialized_packet.sequence_number >= | |
| 1646 first_required_forward_secure_packet_ - 1) { | |
| 1647 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); | |
| 1648 } | |
| 1649 } | |
| 1650 | |
| 1651 void QuicConnection::SendPing() { | |
| 1652 if (retransmission_alarm_->IsSet()) { | |
| 1653 return; | |
| 1654 } | |
| 1655 packet_generator_.AddControlFrame(QuicFrame(new QuicPingFrame)); | |
| 1656 } | |
| 1657 | |
| 1658 void QuicConnection::SendAck() { | |
| 1659 ack_alarm_->Cancel(); | |
| 1660 stop_waiting_count_ = 0; | |
| 1661 num_packets_received_since_last_ack_sent_ = 0; | |
| 1662 | |
| 1663 packet_generator_.SetShouldSendAck(true); | |
| 1664 } | |
| 1665 | |
| 1666 void QuicConnection::OnRetransmissionTimeout() { | |
| 1667 if (!sent_packet_manager_.HasUnackedPackets()) { | |
| 1668 return; | |
| 1669 } | |
| 1670 | |
| 1671 sent_packet_manager_.OnRetransmissionTimeout(); | |
| 1672 WriteIfNotBlocked(); | |
| 1673 | |
| 1674 // A write failure can result in the connection being closed, don't attempt to | |
| 1675 // write further packets, or to set alarms. | |
| 1676 if (!connected_) { | |
| 1677 return; | |
| 1678 } | |
| 1679 | |
| 1680 // In the TLP case, the SentPacketManager gives the connection the opportunity | |
| 1681 // to send new data before retransmitting. | |
| 1682 if (sent_packet_manager_.MaybeRetransmitTailLossProbe()) { | |
| 1683 // Send the pending retransmission now that it's been queued. | |
| 1684 WriteIfNotBlocked(); | |
| 1685 } | |
| 1686 | |
| 1687 // Ensure the retransmission alarm is always set if there are unacked packets | |
| 1688 // and nothing waiting to be sent. | |
| 1689 if (!HasQueuedData() && !retransmission_alarm_->IsSet()) { | |
| 1690 QuicTime rto_timeout = sent_packet_manager_.GetRetransmissionTime(); | |
| 1691 if (rto_timeout.IsInitialized()) { | |
| 1692 retransmission_alarm_->Set(rto_timeout); | |
| 1693 } | |
| 1694 } | |
| 1695 } | |
| 1696 | |
| 1697 void QuicConnection::SetEncrypter(EncryptionLevel level, | |
| 1698 QuicEncrypter* encrypter) { | |
| 1699 framer_.SetEncrypter(level, encrypter); | |
| 1700 if (level == ENCRYPTION_FORWARD_SECURE) { | |
| 1701 has_forward_secure_encrypter_ = true; | |
| 1702 first_required_forward_secure_packet_ = | |
| 1703 sequence_number_of_last_sent_packet_ + | |
| 1704 // 3 times the current congestion window (in slow start) should cover | |
| 1705 // about two full round trips worth of packets, which should be | |
| 1706 // sufficient. | |
| 1707 3 * sent_packet_manager_.EstimateMaxPacketsInFlight( | |
| 1708 max_packet_length()); | |
| 1709 } | |
| 1710 } | |
| 1711 | |
| 1712 const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const { | |
| 1713 return framer_.encrypter(level); | |
| 1714 } | |
| 1715 | |
| 1716 void QuicConnection::SetDefaultEncryptionLevel(EncryptionLevel level) { | |
| 1717 encryption_level_ = level; | |
| 1718 packet_generator_.set_encryption_level(level); | |
| 1719 } | |
| 1720 | |
| 1721 void QuicConnection::SetDecrypter(QuicDecrypter* decrypter, | |
| 1722 EncryptionLevel level) { | |
| 1723 framer_.SetDecrypter(decrypter, level); | |
| 1724 } | |
| 1725 | |
| 1726 void QuicConnection::SetAlternativeDecrypter(QuicDecrypter* decrypter, | |
| 1727 EncryptionLevel level, | |
| 1728 bool latch_once_used) { | |
| 1729 framer_.SetAlternativeDecrypter(decrypter, level, latch_once_used); | |
| 1730 } | |
| 1731 | |
| 1732 const QuicDecrypter* QuicConnection::decrypter() const { | |
| 1733 return framer_.decrypter(); | |
| 1734 } | |
| 1735 | |
| 1736 const QuicDecrypter* QuicConnection::alternative_decrypter() const { | |
| 1737 return framer_.alternative_decrypter(); | |
| 1738 } | |
| 1739 | |
| 1740 void QuicConnection::QueueUndecryptablePacket( | |
| 1741 const QuicEncryptedPacket& packet) { | |
| 1742 DVLOG(1) << ENDPOINT << "Queueing undecryptable packet."; | |
| 1743 undecryptable_packets_.push_back(packet.Clone()); | |
| 1744 } | |
| 1745 | |
| 1746 void QuicConnection::MaybeProcessUndecryptablePackets() { | |
| 1747 if (undecryptable_packets_.empty() || encryption_level_ == ENCRYPTION_NONE) { | |
| 1748 return; | |
| 1749 } | |
| 1750 | |
| 1751 while (connected_ && !undecryptable_packets_.empty()) { | |
| 1752 DVLOG(1) << ENDPOINT << "Attempting to process undecryptable packet"; | |
| 1753 QuicEncryptedPacket* packet = undecryptable_packets_.front(); | |
| 1754 if (!framer_.ProcessPacket(*packet) && | |
| 1755 framer_.error() == QUIC_DECRYPTION_FAILURE) { | |
| 1756 DVLOG(1) << ENDPOINT << "Unable to process undecryptable packet..."; | |
| 1757 break; | |
| 1758 } | |
| 1759 DVLOG(1) << ENDPOINT << "Processed undecryptable packet!"; | |
| 1760 ++stats_.packets_processed; | |
| 1761 delete packet; | |
| 1762 undecryptable_packets_.pop_front(); | |
| 1763 } | |
| 1764 | |
| 1765 // Once forward secure encryption is in use, there will be no | |
| 1766 // new keys installed and hence any undecryptable packets will | |
| 1767 // never be able to be decrypted. | |
| 1768 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE) { | |
| 1769 if (debug_visitor_.get() != nullptr) { | |
| 1770 // TODO(rtenneti): perhaps more efficient to pass the number of | |
| 1771 // undecryptable packets as the argument to OnUndecryptablePacket so that | |
| 1772 // we just need to call OnUndecryptablePacket once? | |
| 1773 for (size_t i = 0; i < undecryptable_packets_.size(); ++i) { | |
| 1774 debug_visitor_->OnUndecryptablePacket(); | |
| 1775 } | |
| 1776 } | |
| 1777 STLDeleteElements(&undecryptable_packets_); | |
| 1778 } | |
| 1779 } | |
| 1780 | |
| 1781 void QuicConnection::MaybeProcessRevivedPacket() { | |
| 1782 QuicFecGroup* group = GetFecGroup(); | |
| 1783 if (!connected_ || group == nullptr || !group->CanRevive()) { | |
| 1784 return; | |
| 1785 } | |
| 1786 QuicPacketHeader revived_header; | |
| 1787 char revived_payload[kMaxPacketSize]; | |
| 1788 size_t len = group->Revive(&revived_header, revived_payload, kMaxPacketSize); | |
| 1789 revived_header.public_header.connection_id = connection_id_; | |
| 1790 revived_header.public_header.connection_id_length = | |
| 1791 last_header_.public_header.connection_id_length; | |
| 1792 revived_header.public_header.version_flag = false; | |
| 1793 revived_header.public_header.reset_flag = false; | |
| 1794 revived_header.public_header.sequence_number_length = | |
| 1795 last_header_.public_header.sequence_number_length; | |
| 1796 revived_header.fec_flag = false; | |
| 1797 revived_header.is_in_fec_group = NOT_IN_FEC_GROUP; | |
| 1798 revived_header.fec_group = 0; | |
| 1799 group_map_.erase(last_header_.fec_group); | |
| 1800 last_decrypted_packet_level_ = group->effective_encryption_level(); | |
| 1801 DCHECK_LT(last_decrypted_packet_level_, NUM_ENCRYPTION_LEVELS); | |
| 1802 delete group; | |
| 1803 | |
| 1804 last_packet_revived_ = true; | |
| 1805 if (debug_visitor_.get() != nullptr) { | |
| 1806 debug_visitor_->OnRevivedPacket(revived_header, | |
| 1807 StringPiece(revived_payload, len)); | |
| 1808 } | |
| 1809 | |
| 1810 ++stats_.packets_revived; | |
| 1811 framer_.ProcessRevivedPacket(&revived_header, | |
| 1812 StringPiece(revived_payload, len)); | |
| 1813 } | |
| 1814 | |
| 1815 QuicFecGroup* QuicConnection::GetFecGroup() { | |
| 1816 QuicFecGroupNumber fec_group_num = last_header_.fec_group; | |
| 1817 if (fec_group_num == 0) { | |
| 1818 return nullptr; | |
| 1819 } | |
| 1820 if (!ContainsKey(group_map_, fec_group_num)) { | |
| 1821 if (group_map_.size() >= kMaxFecGroups) { // Too many groups | |
| 1822 if (fec_group_num < group_map_.begin()->first) { | |
| 1823 // The group being requested is a group we've seen before and deleted. | |
| 1824 // Don't recreate it. | |
| 1825 return nullptr; | |
| 1826 } | |
| 1827 // Clear the lowest group number. | |
| 1828 delete group_map_.begin()->second; | |
| 1829 group_map_.erase(group_map_.begin()); | |
| 1830 } | |
| 1831 group_map_[fec_group_num] = new QuicFecGroup(); | |
| 1832 } | |
| 1833 return group_map_[fec_group_num]; | |
| 1834 } | |
| 1835 | |
| 1836 void QuicConnection::SendConnectionClose(QuicErrorCode error) { | |
| 1837 SendConnectionCloseWithDetails(error, string()); | |
| 1838 } | |
| 1839 | |
| 1840 void QuicConnection::SendConnectionCloseWithDetails(QuicErrorCode error, | |
| 1841 const string& details) { | |
| 1842 // If we're write blocked, WritePacket() will not send, but will capture the | |
| 1843 // serialized packet. | |
| 1844 SendConnectionClosePacket(error, details); | |
| 1845 if (connected_) { | |
| 1846 // It's possible that while sending the connection close packet, we get a | |
| 1847 // socket error and disconnect right then and there. Avoid a double | |
| 1848 // disconnect in that case. | |
| 1849 CloseConnection(error, false); | |
| 1850 } | |
| 1851 } | |
| 1852 | |
| 1853 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, | |
| 1854 const string& details) { | |
| 1855 DVLOG(1) << ENDPOINT << "Force closing " << connection_id() | |
| 1856 << " with error " << QuicUtils::ErrorToString(error) | |
| 1857 << " (" << error << ") " << details; | |
| 1858 // Don't send explicit connection close packets for timeouts. | |
| 1859 // This is particularly important on mobile, where connections are short. | |
| 1860 if (silent_close_enabled_ && | |
| 1861 error == QuicErrorCode::QUIC_CONNECTION_TIMED_OUT) { | |
| 1862 return; | |
| 1863 } | |
| 1864 ScopedPacketBundler ack_bundler(this, SEND_ACK); | |
| 1865 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); | |
| 1866 frame->error_code = error; | |
| 1867 frame->error_details = details; | |
| 1868 packet_generator_.AddControlFrame(QuicFrame(frame)); | |
| 1869 packet_generator_.FlushAllQueuedFrames(); | |
| 1870 } | |
| 1871 | |
| 1872 void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) { | |
| 1873 if (!connected_) { | |
| 1874 DLOG(DFATAL) << "Error: attempt to close an already closed connection" | |
| 1875 << base::debug::StackTrace().ToString(); | |
| 1876 return; | |
| 1877 } | |
| 1878 connected_ = false; | |
| 1879 if (debug_visitor_.get() != nullptr) { | |
| 1880 debug_visitor_->OnConnectionClosed(error, from_peer); | |
| 1881 } | |
| 1882 visitor_->OnConnectionClosed(error, from_peer); | |
| 1883 // Cancel the alarms so they don't trigger any action now that the | |
| 1884 // connection is closed. | |
| 1885 ack_alarm_->Cancel(); | |
| 1886 ping_alarm_->Cancel(); | |
| 1887 fec_alarm_->Cancel(); | |
| 1888 resume_writes_alarm_->Cancel(); | |
| 1889 retransmission_alarm_->Cancel(); | |
| 1890 send_alarm_->Cancel(); | |
| 1891 timeout_alarm_->Cancel(); | |
| 1892 } | |
| 1893 | |
| 1894 void QuicConnection::SendGoAway(QuicErrorCode error, | |
| 1895 QuicStreamId last_good_stream_id, | |
| 1896 const string& reason) { | |
| 1897 DVLOG(1) << ENDPOINT << "Going away with error " | |
| 1898 << QuicUtils::ErrorToString(error) | |
| 1899 << " (" << error << ")"; | |
| 1900 | |
| 1901 // Opportunistically bundle an ack with this outgoing packet. | |
| 1902 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | |
| 1903 packet_generator_.AddControlFrame( | |
| 1904 QuicFrame(new QuicGoAwayFrame(error, last_good_stream_id, reason))); | |
| 1905 } | |
| 1906 | |
| 1907 void QuicConnection::CloseFecGroupsBefore( | |
| 1908 QuicPacketSequenceNumber sequence_number) { | |
| 1909 FecGroupMap::iterator it = group_map_.begin(); | |
| 1910 while (it != group_map_.end()) { | |
| 1911 // If this is the current group or the group doesn't protect this packet | |
| 1912 // we can ignore it. | |
| 1913 if (last_header_.fec_group == it->first || | |
| 1914 !it->second->ProtectsPacketsBefore(sequence_number)) { | |
| 1915 ++it; | |
| 1916 continue; | |
| 1917 } | |
| 1918 QuicFecGroup* fec_group = it->second; | |
| 1919 DCHECK(!fec_group->CanRevive()); | |
| 1920 FecGroupMap::iterator next = it; | |
| 1921 ++next; | |
| 1922 group_map_.erase(it); | |
| 1923 delete fec_group; | |
| 1924 it = next; | |
| 1925 } | |
| 1926 } | |
| 1927 | |
| 1928 QuicByteCount QuicConnection::max_packet_length() const { | |
| 1929 return packet_generator_.max_packet_length(); | |
| 1930 } | |
| 1931 | |
| 1932 void QuicConnection::set_max_packet_length(QuicByteCount length) { | |
| 1933 return packet_generator_.set_max_packet_length(length); | |
| 1934 } | |
| 1935 | |
| 1936 bool QuicConnection::HasQueuedData() const { | |
| 1937 return pending_version_negotiation_packet_ || | |
| 1938 !queued_packets_.empty() || packet_generator_.HasQueuedFrames(); | |
| 1939 } | |
| 1940 | |
| 1941 bool QuicConnection::CanWriteStreamData() { | |
| 1942 // Don't write stream data if there are negotiation or queued data packets | |
| 1943 // to send. Otherwise, continue and bundle as many frames as possible. | |
| 1944 if (pending_version_negotiation_packet_ || !queued_packets_.empty()) { | |
| 1945 return false; | |
| 1946 } | |
| 1947 | |
| 1948 IsHandshake pending_handshake = visitor_->HasPendingHandshake() ? | |
| 1949 IS_HANDSHAKE : NOT_HANDSHAKE; | |
| 1950 // Sending queued packets may have caused the socket to become write blocked, | |
| 1951 // or the congestion manager to prohibit sending. If we've sent everything | |
| 1952 // we had queued and we're still not blocked, let the visitor know it can | |
| 1953 // write more. | |
| 1954 return ShouldGeneratePacket(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, | |
| 1955 pending_handshake); | |
| 1956 } | |
| 1957 | |
| 1958 void QuicConnection::SetNetworkTimeouts(QuicTime::Delta overall_timeout, | |
| 1959 QuicTime::Delta idle_timeout) { | |
| 1960 LOG_IF(DFATAL, idle_timeout > overall_timeout) | |
| 1961 << "idle_timeout:" << idle_timeout.ToMilliseconds() | |
| 1962 << " overall_timeout:" << overall_timeout.ToMilliseconds(); | |
| 1963 // Adjust the idle timeout on client and server to prevent clients from | |
| 1964 // sending requests to servers which have already closed the connection. | |
| 1965 if (is_server_) { | |
| 1966 idle_timeout = idle_timeout.Add(QuicTime::Delta::FromSeconds(3)); | |
| 1967 } else if (idle_timeout > QuicTime::Delta::FromSeconds(1)) { | |
| 1968 idle_timeout = idle_timeout.Subtract(QuicTime::Delta::FromSeconds(1)); | |
| 1969 } | |
| 1970 overall_connection_timeout_ = overall_timeout; | |
| 1971 idle_network_timeout_ = idle_timeout; | |
| 1972 | |
| 1973 SetTimeoutAlarm(); | |
| 1974 } | |
| 1975 | |
| 1976 void QuicConnection::CheckForTimeout() { | |
| 1977 QuicTime now = clock_->ApproximateNow(); | |
| 1978 QuicTime time_of_last_packet = max(time_of_last_received_packet_, | |
| 1979 time_of_last_sent_new_packet_); | |
| 1980 | |
| 1981 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| | |
| 1982 // is accurate time. However, this should not change the behavior of | |
| 1983 // timeout handling. | |
| 1984 QuicTime::Delta idle_duration = now.Subtract(time_of_last_packet); | |
| 1985 DVLOG(1) << ENDPOINT << "last packet " | |
| 1986 << time_of_last_packet.ToDebuggingValue() | |
| 1987 << " now:" << now.ToDebuggingValue() | |
| 1988 << " idle_duration:" << idle_duration.ToMicroseconds() | |
| 1989 << " idle_network_timeout: " | |
| 1990 << idle_network_timeout_.ToMicroseconds(); | |
| 1991 if (idle_duration >= idle_network_timeout_) { | |
| 1992 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; | |
| 1993 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); | |
| 1994 return; | |
| 1995 } | |
| 1996 | |
| 1997 if (!overall_connection_timeout_.IsInfinite()) { | |
| 1998 QuicTime::Delta connected_duration = | |
| 1999 now.Subtract(stats_.connection_creation_time); | |
| 2000 DVLOG(1) << ENDPOINT << "connection time: " | |
| 2001 << connected_duration.ToMicroseconds() << " overall timeout: " | |
| 2002 << overall_connection_timeout_.ToMicroseconds(); | |
| 2003 if (connected_duration >= overall_connection_timeout_) { | |
| 2004 DVLOG(1) << ENDPOINT << | |
| 2005 "Connection timedout due to overall connection timeout."; | |
| 2006 SendConnectionClose(QUIC_CONNECTION_OVERALL_TIMED_OUT); | |
| 2007 return; | |
| 2008 } | |
| 2009 } | |
| 2010 | |
| 2011 SetTimeoutAlarm(); | |
| 2012 } | |
| 2013 | |
| 2014 void QuicConnection::SetTimeoutAlarm() { | |
| 2015 QuicTime time_of_last_packet = max(time_of_last_received_packet_, | |
| 2016 time_of_last_sent_new_packet_); | |
| 2017 | |
| 2018 QuicTime deadline = time_of_last_packet.Add(idle_network_timeout_); | |
| 2019 if (!overall_connection_timeout_.IsInfinite()) { | |
| 2020 deadline = min(deadline, | |
| 2021 stats_.connection_creation_time.Add( | |
| 2022 overall_connection_timeout_)); | |
| 2023 } | |
| 2024 | |
| 2025 timeout_alarm_->Cancel(); | |
| 2026 timeout_alarm_->Set(deadline); | |
| 2027 } | |
| 2028 | |
| 2029 void QuicConnection::SetPingAlarm() { | |
| 2030 if (is_server_) { | |
| 2031 // Only clients send pings. | |
| 2032 return; | |
| 2033 } | |
| 2034 if (!visitor_->HasOpenDataStreams()) { | |
| 2035 ping_alarm_->Cancel(); | |
| 2036 // Don't send a ping unless there are open streams. | |
| 2037 return; | |
| 2038 } | |
| 2039 QuicTime::Delta ping_timeout = QuicTime::Delta::FromSeconds(kPingTimeoutSecs); | |
| 2040 ping_alarm_->Update(clock_->ApproximateNow().Add(ping_timeout), | |
| 2041 QuicTime::Delta::FromSeconds(1)); | |
| 2042 } | |
| 2043 | |
| 2044 QuicConnection::ScopedPacketBundler::ScopedPacketBundler( | |
| 2045 QuicConnection* connection, | |
| 2046 AckBundling send_ack) | |
| 2047 : connection_(connection), | |
| 2048 already_in_batch_mode_(connection != nullptr && | |
| 2049 connection->packet_generator_.InBatchMode()) { | |
| 2050 if (connection_ == nullptr) { | |
| 2051 return; | |
| 2052 } | |
| 2053 // Move generator into batch mode. If caller wants us to include an ack, | |
| 2054 // check the delayed-ack timer to see if there's ack info to be sent. | |
| 2055 if (!already_in_batch_mode_) { | |
| 2056 DVLOG(1) << "Entering Batch Mode."; | |
| 2057 connection_->packet_generator_.StartBatchOperations(); | |
| 2058 } | |
| 2059 // Bundle an ack if the alarm is set or with every second packet if we need to | |
| 2060 // raise the peer's least unacked. | |
| 2061 bool ack_pending = | |
| 2062 connection_->ack_alarm_->IsSet() || connection_->stop_waiting_count_ > 1; | |
| 2063 if (send_ack == SEND_ACK || (send_ack == BUNDLE_PENDING_ACK && ack_pending)) { | |
| 2064 DVLOG(1) << "Bundling ack with outgoing packet."; | |
| 2065 connection_->SendAck(); | |
| 2066 } | |
| 2067 } | |
| 2068 | |
| 2069 QuicConnection::ScopedPacketBundler::~ScopedPacketBundler() { | |
| 2070 if (connection_ == nullptr) { | |
| 2071 return; | |
| 2072 } | |
| 2073 // If we changed the generator's batch state, restore original batch state. | |
| 2074 if (!already_in_batch_mode_) { | |
| 2075 DVLOG(1) << "Leaving Batch Mode."; | |
| 2076 connection_->packet_generator_.FinishBatchOperations(); | |
| 2077 } | |
| 2078 DCHECK_EQ(already_in_batch_mode_, | |
| 2079 connection_->packet_generator_.InBatchMode()); | |
| 2080 } | |
| 2081 | |
| 2082 HasRetransmittableData QuicConnection::IsRetransmittable( | |
| 2083 const QueuedPacket& packet) { | |
| 2084 // Retransmitted packets retransmittable frames are owned by the unacked | |
| 2085 // packet map, but are not present in the serialized packet. | |
| 2086 if (packet.transmission_type != NOT_RETRANSMISSION || | |
| 2087 packet.serialized_packet.retransmittable_frames != nullptr) { | |
| 2088 return HAS_RETRANSMITTABLE_DATA; | |
| 2089 } else { | |
| 2090 return NO_RETRANSMITTABLE_DATA; | |
| 2091 } | |
| 2092 } | |
| 2093 | |
| 2094 bool QuicConnection::IsConnectionClose(const QueuedPacket& packet) { | |
| 2095 const RetransmittableFrames* retransmittable_frames = | |
| 2096 packet.serialized_packet.retransmittable_frames; | |
| 2097 if (retransmittable_frames == nullptr) { | |
| 2098 return false; | |
| 2099 } | |
| 2100 for (const QuicFrame& frame : retransmittable_frames->frames()) { | |
| 2101 if (frame.type == CONNECTION_CLOSE_FRAME) { | |
| 2102 return true; | |
| 2103 } | |
| 2104 } | |
| 2105 return false; | |
| 2106 } | |
| 2107 | |
| 2108 } // namespace net | |
| OLD | NEW |