| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/quic/quic_connection.h" | 5 #include "net/quic/quic_connection.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <iterator> | 11 #include <iterator> |
| 12 #include <limits> | 12 #include <limits> |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <set> | 14 #include <set> |
| 15 #include <utility> | 15 #include <utility> |
| 16 | 16 |
| 17 #include "base/debug/stack_trace.h" | 17 #include "base/debug/stack_trace.h" |
| 18 #include "base/format_macros.h" | 18 #include "base/format_macros.h" |
| 19 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/quic/crypto/quic_decrypter.h" | 23 #include "net/quic/crypto/quic_decrypter.h" |
| 24 #include "net/quic/crypto/quic_encrypter.h" | 24 #include "net/quic/crypto/quic_encrypter.h" |
| 25 #include "net/quic/iovector.h" | 25 #include "net/quic/iovector.h" |
| 26 #include "net/quic/quic_bandwidth.h" | 26 #include "net/quic/quic_bandwidth.h" |
| 27 #include "net/quic/quic_config.h" | 27 #include "net/quic/quic_config.h" |
| 28 #include "net/quic/quic_fec_group.h" | 28 #include "net/quic/quic_fec_group.h" |
| 29 #include "net/quic/quic_flags.h" | 29 #include "net/quic/quic_flags.h" |
| 30 #include "net/quic/quic_packet_generator.h" |
| 30 #include "net/quic/quic_utils.h" | 31 #include "net/quic/quic_utils.h" |
| 31 | 32 |
| 32 using base::StringPiece; | 33 using base::StringPiece; |
| 33 using base::StringPrintf; | 34 using base::StringPrintf; |
| 34 using base::hash_map; | 35 using base::hash_map; |
| 35 using base::hash_set; | 36 using base::hash_set; |
| 36 using std::list; | 37 using std::list; |
| 37 using std::make_pair; | 38 using std::make_pair; |
| 38 using std::max; | 39 using std::max; |
| 39 using std::min; | 40 using std::min; |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 connection_->SendPing(); | 155 connection_->SendPing(); |
| 155 return QuicTime::Zero(); | 156 return QuicTime::Zero(); |
| 156 } | 157 } |
| 157 | 158 |
| 158 private: | 159 private: |
| 159 QuicConnection* connection_; | 160 QuicConnection* connection_; |
| 160 | 161 |
| 161 DISALLOW_COPY_AND_ASSIGN(PingAlarm); | 162 DISALLOW_COPY_AND_ASSIGN(PingAlarm); |
| 162 }; | 163 }; |
| 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 |
| 164 } // namespace | 182 } // namespace |
| 165 | 183 |
| 166 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, | 184 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, |
| 167 EncryptionLevel level) | 185 EncryptionLevel level) |
| 168 : serialized_packet(packet), | 186 : serialized_packet(packet), |
| 169 encryption_level(level), | 187 encryption_level(level), |
| 170 transmission_type(NOT_RETRANSMISSION), | 188 transmission_type(NOT_RETRANSMISSION), |
| 171 original_sequence_number(0) { | 189 original_sequence_number(0) { |
| 172 } | 190 } |
| 173 | 191 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 ack_queued_(false), | 237 ack_queued_(false), |
| 220 num_packets_received_since_last_ack_sent_(0), | 238 num_packets_received_since_last_ack_sent_(0), |
| 221 stop_waiting_count_(0), | 239 stop_waiting_count_(0), |
| 222 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), | 240 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), |
| 223 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), | 241 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), |
| 224 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), | 242 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), |
| 225 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), | 243 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), |
| 226 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), | 244 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), |
| 227 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))), | 245 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))), |
| 228 packet_generator_(connection_id_, &framer_, random_generator_, this), | 246 packet_generator_(connection_id_, &framer_, random_generator_, this), |
| 247 fec_alarm_(helper->CreateAlarm(new FecAlarm(&packet_generator_))), |
| 229 idle_network_timeout_(QuicTime::Delta::Infinite()), | 248 idle_network_timeout_(QuicTime::Delta::Infinite()), |
| 230 overall_connection_timeout_(QuicTime::Delta::Infinite()), | 249 overall_connection_timeout_(QuicTime::Delta::Infinite()), |
| 231 time_of_last_received_packet_(clock_->ApproximateNow()), | 250 time_of_last_received_packet_(clock_->ApproximateNow()), |
| 232 time_of_last_sent_new_packet_(clock_->ApproximateNow()), | 251 time_of_last_sent_new_packet_(clock_->ApproximateNow()), |
| 233 sequence_number_of_last_sent_packet_(0), | 252 sequence_number_of_last_sent_packet_(0), |
| 234 sent_packet_manager_( | 253 sent_packet_manager_( |
| 235 is_server, | 254 is_server, |
| 236 clock_, | 255 clock_, |
| 237 &stats_, | 256 &stats_, |
| 238 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic, | 257 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 | 337 |
| 319 void QuicConnection::OnError(QuicFramer* framer) { | 338 void QuicConnection::OnError(QuicFramer* framer) { |
| 320 // Packets that we can not or have not decrypted are dropped. | 339 // Packets that we can not or have not decrypted are dropped. |
| 321 // TODO(rch): add stats to measure this. | 340 // TODO(rch): add stats to measure this. |
| 322 if (!connected_ || last_packet_decrypted_ == false) { | 341 if (!connected_ || last_packet_decrypted_ == false) { |
| 323 return; | 342 return; |
| 324 } | 343 } |
| 325 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error()); | 344 SendConnectionCloseWithDetails(framer->error(), framer->detailed_error()); |
| 326 } | 345 } |
| 327 | 346 |
| 347 void QuicConnection::MaybeSetFecAlarm( |
| 348 QuicPacketSequenceNumber sequence_number) { |
| 349 if (fec_alarm_->IsSet()) { |
| 350 return; |
| 351 } |
| 352 QuicTime::Delta timeout = packet_generator_.GetFecTimeout(sequence_number); |
| 353 if (!timeout.IsInfinite()) { |
| 354 fec_alarm_->Set(clock_->ApproximateNow().Add(timeout)); |
| 355 } |
| 356 } |
| 357 |
| 328 void QuicConnection::OnPacket() { | 358 void QuicConnection::OnPacket() { |
| 329 DCHECK(last_stream_frames_.empty() && | 359 DCHECK(last_stream_frames_.empty() && |
| 330 last_ack_frames_.empty() && | 360 last_ack_frames_.empty() && |
| 331 last_congestion_frames_.empty() && | 361 last_congestion_frames_.empty() && |
| 332 last_stop_waiting_frames_.empty() && | 362 last_stop_waiting_frames_.empty() && |
| 333 last_rst_frames_.empty() && | 363 last_rst_frames_.empty() && |
| 334 last_goaway_frames_.empty() && | 364 last_goaway_frames_.empty() && |
| 335 last_window_update_frames_.empty() && | 365 last_window_update_frames_.empty() && |
| 336 last_blocked_frames_.empty() && | 366 last_blocked_frames_.empty() && |
| 337 last_ping_frames_.empty() && | 367 last_ping_frames_.empty() && |
| (...skipping 1154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1492 packet->original_sequence_number, | 1522 packet->original_sequence_number, |
| 1493 packet->encryption_level, | 1523 packet->encryption_level, |
| 1494 packet->transmission_type, | 1524 packet->transmission_type, |
| 1495 *encrypted, | 1525 *encrypted, |
| 1496 packet_send_time); | 1526 packet_send_time); |
| 1497 } | 1527 } |
| 1498 if (packet->transmission_type == NOT_RETRANSMISSION) { | 1528 if (packet->transmission_type == NOT_RETRANSMISSION) { |
| 1499 time_of_last_sent_new_packet_ = packet_send_time; | 1529 time_of_last_sent_new_packet_ = packet_send_time; |
| 1500 } | 1530 } |
| 1501 SetPingAlarm(); | 1531 SetPingAlarm(); |
| 1532 MaybeSetFecAlarm(sequence_number); |
| 1502 DVLOG(1) << ENDPOINT << "time " | 1533 DVLOG(1) << ENDPOINT << "time " |
| 1503 << (FLAGS_quic_record_send_time_before_write ? | 1534 << (FLAGS_quic_record_send_time_before_write ? |
| 1504 "we began writing " : "we finished writing ") | 1535 "we began writing " : "we finished writing ") |
| 1505 << "last sent packet: " | 1536 << "last sent packet: " |
| 1506 << packet_send_time.ToDebuggingValue(); | 1537 << packet_send_time.ToDebuggingValue(); |
| 1507 | 1538 |
| 1508 // TODO(ianswett): Change the sequence number length and other packet creator | 1539 // TODO(ianswett): Change the sequence number length and other packet creator |
| 1509 // options by a more explicit API than setting a struct value directly, | 1540 // options by a more explicit API than setting a struct value directly, |
| 1510 // perhaps via the NetworkChangeVisitor. | 1541 // perhaps via the NetworkChangeVisitor. |
| 1511 packet_generator_.UpdateSequenceNumberLength( | 1542 packet_generator_.UpdateSequenceNumberLength( |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1591 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); | 1622 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1592 } | 1623 } |
| 1593 if (serialized_packet.retransmittable_frames) { | 1624 if (serialized_packet.retransmittable_frames) { |
| 1594 serialized_packet.retransmittable_frames-> | 1625 serialized_packet.retransmittable_frames-> |
| 1595 set_encryption_level(encryption_level_); | 1626 set_encryption_level(encryption_level_); |
| 1596 | 1627 |
| 1597 if (FLAGS_quic_ack_notifier_informed_on_serialized) { | 1628 if (FLAGS_quic_ack_notifier_informed_on_serialized) { |
| 1598 sent_packet_manager_.OnSerializedPacket(serialized_packet); | 1629 sent_packet_manager_.OnSerializedPacket(serialized_packet); |
| 1599 } | 1630 } |
| 1600 } | 1631 } |
| 1632 if (serialized_packet.packet->is_fec_packet() && fec_alarm_->IsSet()) { |
| 1633 // If an FEC packet is serialized with the FEC alarm set, cancel the alarm. |
| 1634 fec_alarm_->Cancel(); |
| 1635 } |
| 1601 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_)); | 1636 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_)); |
| 1602 } | 1637 } |
| 1603 | 1638 |
| 1604 void QuicConnection::OnCongestionWindowChange() { | 1639 void QuicConnection::OnCongestionWindowChange() { |
| 1605 packet_generator_.OnCongestionWindowChange( | 1640 packet_generator_.OnCongestionWindowChange( |
| 1606 sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); | 1641 sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); |
| 1607 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); | 1642 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); |
| 1608 } | 1643 } |
| 1609 | 1644 |
| 1610 void QuicConnection::OnRttChange() { | 1645 void QuicConnection::OnRttChange() { |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1889 } | 1924 } |
| 1890 connected_ = false; | 1925 connected_ = false; |
| 1891 if (debug_visitor_.get() != nullptr) { | 1926 if (debug_visitor_.get() != nullptr) { |
| 1892 debug_visitor_->OnConnectionClosed(error, from_peer); | 1927 debug_visitor_->OnConnectionClosed(error, from_peer); |
| 1893 } | 1928 } |
| 1894 visitor_->OnConnectionClosed(error, from_peer); | 1929 visitor_->OnConnectionClosed(error, from_peer); |
| 1895 // Cancel the alarms so they don't trigger any action now that the | 1930 // Cancel the alarms so they don't trigger any action now that the |
| 1896 // connection is closed. | 1931 // connection is closed. |
| 1897 ack_alarm_->Cancel(); | 1932 ack_alarm_->Cancel(); |
| 1898 ping_alarm_->Cancel(); | 1933 ping_alarm_->Cancel(); |
| 1934 fec_alarm_->Cancel(); |
| 1899 resume_writes_alarm_->Cancel(); | 1935 resume_writes_alarm_->Cancel(); |
| 1900 retransmission_alarm_->Cancel(); | 1936 retransmission_alarm_->Cancel(); |
| 1901 send_alarm_->Cancel(); | 1937 send_alarm_->Cancel(); |
| 1902 timeout_alarm_->Cancel(); | 1938 timeout_alarm_->Cancel(); |
| 1903 } | 1939 } |
| 1904 | 1940 |
| 1905 void QuicConnection::SendGoAway(QuicErrorCode error, | 1941 void QuicConnection::SendGoAway(QuicErrorCode error, |
| 1906 QuicStreamId last_good_stream_id, | 1942 QuicStreamId last_good_stream_id, |
| 1907 const string& reason) { | 1943 const string& reason) { |
| 1908 DVLOG(1) << ENDPOINT << "Going away with error " | 1944 DVLOG(1) << ENDPOINT << "Going away with error " |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2111 } | 2147 } |
| 2112 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { | 2148 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { |
| 2113 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { | 2149 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { |
| 2114 return true; | 2150 return true; |
| 2115 } | 2151 } |
| 2116 } | 2152 } |
| 2117 return false; | 2153 return false; |
| 2118 } | 2154 } |
| 2119 | 2155 |
| 2120 } // namespace net | 2156 } // namespace net |
| OLD | NEW |