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 #include <algorithm> | 9 #include <algorithm> |
10 #include <iterator> | 10 #include <iterator> |
11 #include <limits> | 11 #include <limits> |
12 #include <memory> | 12 #include <memory> |
13 #include <set> | 13 #include <set> |
14 #include <utility> | 14 #include <utility> |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
19 #include "net/quic/crypto/quic_decrypter.h" | 19 #include "net/quic/crypto/quic_decrypter.h" |
20 #include "net/quic/crypto/quic_encrypter.h" | 20 #include "net/quic/crypto/quic_encrypter.h" |
21 #include "net/quic/quic_ack_notifier_manager.h" | |
22 #include "net/quic/quic_bandwidth.h" | 21 #include "net/quic/quic_bandwidth.h" |
23 #include "net/quic/quic_utils.h" | 22 #include "net/quic/quic_utils.h" |
24 | 23 |
25 using base::hash_map; | 24 using base::hash_map; |
26 using base::hash_set; | 25 using base::hash_set; |
27 using base::StringPiece; | 26 using base::StringPiece; |
28 using std::list; | 27 using std::list; |
29 using std::make_pair; | 28 using std::make_pair; |
30 using std::min; | 29 using std::min; |
31 using std::max; | 30 using std::max; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 clock_(helper->GetClock()), | 212 clock_(helper->GetClock()), |
214 random_generator_(helper->GetRandomGenerator()), | 213 random_generator_(helper->GetRandomGenerator()), |
215 guid_(guid), | 214 guid_(guid), |
216 peer_address_(address), | 215 peer_address_(address), |
217 largest_seen_packet_with_ack_(0), | 216 largest_seen_packet_with_ack_(0), |
218 write_blocked_(false), | 217 write_blocked_(false), |
219 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), | 218 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), |
220 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), | 219 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), |
221 abandon_fec_alarm_(helper->CreateAlarm(new AbandonFecAlarm(this))), | 220 abandon_fec_alarm_(helper->CreateAlarm(new AbandonFecAlarm(this))), |
222 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), | 221 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), |
222 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), | |
223 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), | 223 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), |
224 debug_visitor_(NULL), | 224 debug_visitor_(NULL), |
225 packet_creator_(guid_, &framer_, random_generator_, is_server), | 225 packet_creator_(guid_, &framer_, random_generator_, is_server), |
226 packet_generator_(this, NULL, &packet_creator_), | 226 packet_generator_(this, NULL, &packet_creator_), |
227 idle_network_timeout_( | 227 idle_network_timeout_( |
228 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), | 228 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), |
229 overall_connection_timeout_(QuicTime::Delta::Infinite()), | 229 overall_connection_timeout_(QuicTime::Delta::Infinite()), |
230 creation_time_(clock_->ApproximateNow()), | 230 creation_time_(clock_->ApproximateNow()), |
231 time_of_last_received_packet_(clock_->ApproximateNow()), | 231 time_of_last_received_packet_(clock_->ApproximateNow()), |
232 time_of_last_sent_packet_(clock_->ApproximateNow()), | 232 time_of_last_sent_packet_(clock_->ApproximateNow()), |
233 sequence_number_of_last_inorder_packet_(0), | |
233 congestion_manager_(clock_, kTCP), | 234 congestion_manager_(clock_, kTCP), |
234 sent_packet_manager_(is_server, this), | 235 sent_packet_manager_(is_server, this), |
235 version_negotiation_state_(START_NEGOTIATION), | 236 version_negotiation_state_(START_NEGOTIATION), |
236 consecutive_rto_count_(0), | 237 consecutive_rto_count_(0), |
237 is_server_(is_server), | 238 is_server_(is_server), |
238 connected_(true), | 239 connected_(true), |
239 received_truncated_ack_(false), | 240 received_truncated_ack_(false), |
240 send_ack_in_response_to_packet_(false), | 241 send_ack_in_response_to_packet_(false), |
241 address_migrating_(false) { | 242 address_migrating_(false) { |
242 helper_->SetConnection(this); | 243 helper_->SetConnection(this); |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 CloseFecGroupsBefore(incoming_ack.sent_info.least_unacked + 1); | 515 CloseFecGroupsBefore(incoming_ack.sent_info.least_unacked + 1); |
515 | 516 |
516 sent_entropy_manager_.ClearEntropyBefore( | 517 sent_entropy_manager_.ClearEntropyBefore( |
517 received_packet_manager_.least_packet_awaited_by_peer() - 1); | 518 received_packet_manager_.least_packet_awaited_by_peer() - 1); |
518 | 519 |
519 retransmitted_nacked_packet_count_ = 0; | 520 retransmitted_nacked_packet_count_ = 0; |
520 SequenceNumberSet acked_packets; | 521 SequenceNumberSet acked_packets; |
521 sent_packet_manager_.OnIncomingAck( | 522 sent_packet_manager_.OnIncomingAck( |
522 incoming_ack.received_info, received_truncated_ack_, &acked_packets); | 523 incoming_ack.received_info, received_truncated_ack_, &acked_packets); |
523 if (acked_packets.size() > 0) { | 524 if (acked_packets.size() > 0) { |
524 // The AckNotifierManager should be informed of every ACKed sequence number. | |
525 ack_notifier_manager_.OnIncomingAck(acked_packets); | |
526 | |
527 // Reset the RTO timeout for each packet when an ack is received. | 525 // Reset the RTO timeout for each packet when an ack is received. |
528 if (retransmission_alarm_->IsSet()) { | 526 if (retransmission_alarm_->IsSet()) { |
529 retransmission_alarm_->Cancel(); | 527 retransmission_alarm_->Cancel(); |
530 // Only reschedule the timer if there are outstanding packets. | 528 // Only reschedule the timer if there are outstanding packets. |
531 if (sent_packet_manager_.HasUnackedPackets()) { | 529 if (sent_packet_manager_.HasUnackedPackets()) { |
532 QuicTime::Delta retransmission_delay = | 530 QuicTime::Delta retransmission_delay = |
533 congestion_manager_.GetRetransmissionDelay( | 531 congestion_manager_.GetRetransmissionDelay( |
534 sent_packet_manager_.GetNumUnackedPackets(), 0); | 532 sent_packet_manager_.GetNumUnackedPackets(), 0); |
535 retransmission_alarm_->Set(clock_->ApproximateNow().Add( | 533 retransmission_alarm_->Set(clock_->ApproximateNow().Add( |
536 retransmission_delay)); | 534 retransmission_delay)); |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
910 if (!fin && iov_count == 0) { | 908 if (!fin && iov_count == 0) { |
911 LOG(DFATAL) << "Attempt to send empty stream frame"; | 909 LOG(DFATAL) << "Attempt to send empty stream frame"; |
912 } | 910 } |
913 | 911 |
914 // This notifier will be owned by the AckNotifierManager (or deleted below if | 912 // This notifier will be owned by the AckNotifierManager (or deleted below if |
915 // no data was consumed). | 913 // no data was consumed). |
916 QuicAckNotifier* notifier = new QuicAckNotifier(delegate); | 914 QuicAckNotifier* notifier = new QuicAckNotifier(delegate); |
917 QuicConsumedData consumed_data = | 915 QuicConsumedData consumed_data = |
918 SendvStreamDataInner(id, iov, iov_count, offset, fin, notifier); | 916 SendvStreamDataInner(id, iov, iov_count, offset, fin, notifier); |
919 | 917 |
920 if (consumed_data.bytes_consumed > 0) { | 918 if (consumed_data.bytes_consumed <= 0) { |
wtc
2013/10/01 14:06:59
Nit: consumed_data.bytes_consumed is a size_t, whi
ramant (doing other things)
2013/10/01 22:29:32
Done.
| |
921 // If some data was consumed, then the delegate should be registered for | |
922 // notification when the data is ACKed. | |
923 ack_notifier_manager_.AddAckNotifier(notifier); | |
924 DLOG(INFO) << "Registered AckNotifier."; | |
925 } else { | |
926 // No data was consumed, delete the notifier. | 919 // No data was consumed, delete the notifier. |
927 delete notifier; | 920 delete notifier; |
928 } | 921 } |
929 | 922 |
930 return consumed_data; | 923 return consumed_data; |
931 } | 924 } |
932 | 925 |
933 void QuicConnection::SendRstStream(QuicStreamId id, | 926 void QuicConnection::SendRstStream(QuicStreamId id, |
934 QuicRstStreamErrorCode error) { | 927 QuicRstStreamErrorCode error) { |
935 LOG(INFO) << "Sending RST_STREAM: " << id << " code: " << error; | 928 LOG(INFO) << "Sending RST_STREAM: " << id << " code: " << error; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1019 pending_handshake)) { | 1012 pending_handshake)) { |
1020 // Set |include_ack| to false in bundler; ack inclusion happens elsewhere. | 1013 // Set |include_ack| to false in bundler; ack inclusion happens elsewhere. |
1021 scoped_ptr<ScopedPacketBundler> bundler( | 1014 scoped_ptr<ScopedPacketBundler> bundler( |
1022 new ScopedPacketBundler(this, false)); | 1015 new ScopedPacketBundler(this, false)); |
1023 bool all_bytes_written = visitor_->OnCanWrite(); | 1016 bool all_bytes_written = visitor_->OnCanWrite(); |
1024 bundler.reset(); | 1017 bundler.reset(); |
1025 // After the visitor writes, it may have caused the socket to become write | 1018 // After the visitor writes, it may have caused the socket to become write |
1026 // blocked or the congestion manager to prohibit sending, so check again. | 1019 // blocked or the congestion manager to prohibit sending, so check again. |
1027 pending_handshake = visitor_->HasPendingHandshake() ? IS_HANDSHAKE | 1020 pending_handshake = visitor_->HasPendingHandshake() ? IS_HANDSHAKE |
1028 : NOT_HANDSHAKE; | 1021 : NOT_HANDSHAKE; |
1029 if (!write_blocked_ && !all_bytes_written && | 1022 if (!all_bytes_written && !resume_writes_alarm_->IsSet() && |
1030 CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, | 1023 CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA, |
1031 pending_handshake)) { | 1024 pending_handshake)) { |
1032 // We're not write blocked, but some stream didn't write out all of its | 1025 // We're not write blocked, but some stream didn't write out all of its |
1033 // bytes. Register for 'immediate' resumption so we'll keep writing after | 1026 // bytes. Register for 'immediate' resumption so we'll keep writing after |
1034 // other quic connections have had a chance to use the socket. | 1027 // other quic connections have had a chance to use the socket. |
1035 send_alarm_->Cancel(); | 1028 resume_writes_alarm_->Set(clock_->ApproximateNow()); |
1036 send_alarm_->Set(clock_->ApproximateNow()); | |
1037 } | 1029 } |
1038 } | 1030 } |
1039 | 1031 |
1040 return !write_blocked_; | 1032 return !write_blocked_; |
1041 } | 1033 } |
1042 | 1034 |
1043 bool QuicConnection::ProcessValidatedPacket() { | 1035 bool QuicConnection::ProcessValidatedPacket() { |
1044 if (address_migrating_) { | 1036 if (address_migrating_) { |
1045 SendConnectionCloseWithDetails( | 1037 SendConnectionCloseWithDetails( |
1046 QUIC_ERROR_MIGRATING_ADDRESS, | 1038 QUIC_ERROR_MIGRATING_ADDRESS, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1092 // Retransmitted packets use the same sequence number length as the | 1084 // Retransmitted packets use the same sequence number length as the |
1093 // original. | 1085 // original. |
1094 // Flush the packet creator before making a new packet. | 1086 // Flush the packet creator before making a new packet. |
1095 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | 1087 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
1096 // does not require the creator to be flushed. | 1088 // does not require the creator to be flushed. |
1097 Flush(); | 1089 Flush(); |
1098 SerializedPacket serialized_packet = packet_creator_.ReserializeAllFrames( | 1090 SerializedPacket serialized_packet = packet_creator_.ReserializeAllFrames( |
1099 pending.retransmittable_frames.frames(), | 1091 pending.retransmittable_frames.frames(), |
1100 pending.sequence_number_length); | 1092 pending.sequence_number_length); |
1101 | 1093 |
1102 // A notifier may be waiting to hear about ACKs for the original sequence | |
1103 // number. Inform them that the sequence number has changed. | |
1104 ack_notifier_manager_.UpdateSequenceNumber( | |
1105 pending.sequence_number, serialized_packet.sequence_number); | |
1106 | |
1107 DLOG(INFO) << ENDPOINT << "Retransmitting " << pending.sequence_number | 1094 DLOG(INFO) << ENDPOINT << "Retransmitting " << pending.sequence_number |
1108 << " as " << serialized_packet.sequence_number; | 1095 << " as " << serialized_packet.sequence_number; |
1109 if (debug_visitor_) { | 1096 if (debug_visitor_) { |
1110 debug_visitor_->OnPacketRetransmitted( | 1097 debug_visitor_->OnPacketRetransmitted( |
1111 pending.sequence_number, serialized_packet.sequence_number); | 1098 pending.sequence_number, serialized_packet.sequence_number); |
1112 } | 1099 } |
1113 sent_packet_manager_.OnRetransmittedPacket( | 1100 sent_packet_manager_.OnRetransmittedPacket( |
1114 pending.sequence_number, serialized_packet.sequence_number); | 1101 pending.sequence_number, serialized_packet.sequence_number); |
1115 | 1102 |
1116 SendOrQueuePacket(pending.retransmittable_frames.encryption_level(), | 1103 SendOrQueuePacket(pending.retransmittable_frames.encryption_level(), |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1178 if (handshake == IS_HANDSHAKE) { | 1165 if (handshake == IS_HANDSHAKE) { |
1179 return true; | 1166 return true; |
1180 } | 1167 } |
1181 | 1168 |
1182 return CanWrite(transmission_type, retransmittable, handshake); | 1169 return CanWrite(transmission_type, retransmittable, handshake); |
1183 } | 1170 } |
1184 | 1171 |
1185 bool QuicConnection::CanWrite(TransmissionType transmission_type, | 1172 bool QuicConnection::CanWrite(TransmissionType transmission_type, |
1186 HasRetransmittableData retransmittable, | 1173 HasRetransmittableData retransmittable, |
1187 IsHandshake handshake) { | 1174 IsHandshake handshake) { |
1188 // TODO(ianswett): If the packet is a retransmit, the current send alarm may | 1175 // This check assumes that if the send alarm is set, it applies equally to all |
1189 // be too long. | 1176 // types of transmissions. |
1190 if (write_blocked_ || send_alarm_->IsSet()) { | 1177 if (write_blocked_ || send_alarm_->IsSet()) { |
1191 return false; | 1178 return false; |
1192 } | 1179 } |
1193 | 1180 |
1194 QuicTime now = clock_->Now(); | 1181 QuicTime now = clock_->Now(); |
1195 QuicTime::Delta delay = congestion_manager_.TimeUntilSend( | 1182 QuicTime::Delta delay = congestion_manager_.TimeUntilSend( |
1196 now, transmission_type, retransmittable, handshake); | 1183 now, transmission_type, retransmittable, handshake); |
1197 if (delay.IsInfinite()) { | 1184 if (delay.IsInfinite()) { |
1198 return false; | 1185 return false; |
1199 } | 1186 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1298 if (!sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { | 1285 if (!sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { |
1299 DLOG(INFO) << ENDPOINT << "Dropping packet: " << sequence_number | 1286 DLOG(INFO) << ENDPOINT << "Dropping packet: " << sequence_number |
1300 << " since a previous transmission has been acked."; | 1287 << " since a previous transmission has been acked."; |
1301 sent_packet_manager_.DiscardUnackedPacket(sequence_number); | 1288 sent_packet_manager_.DiscardUnackedPacket(sequence_number); |
1302 delete packet; | 1289 delete packet; |
1303 return true; | 1290 return true; |
1304 } | 1291 } |
1305 } | 1292 } |
1306 | 1293 |
1307 // TODO(wtc): use the same logic that is used in the packet generator. | 1294 // TODO(wtc): use the same logic that is used in the packet generator. |
1308 // Namely, a packet is a handshake if it contains a stream frame for the | |
1309 // crypto stream. It should be possible to look at the RetransmittableFrames | 1295 // crypto stream. It should be possible to look at the RetransmittableFrames |
1310 // in the SerializedPacket to determine this for a packet. | 1296 // in the SerializedPacket to determine this for a packet. |
1311 IsHandshake handshake = level == ENCRYPTION_NONE ? IS_HANDSHAKE | 1297 IsHandshake handshake = level == ENCRYPTION_NONE ? IS_HANDSHAKE |
1312 : NOT_HANDSHAKE; | 1298 : NOT_HANDSHAKE; |
1313 | 1299 |
1314 // If we are not forced and we can't write, then simply return false; | 1300 // If we are not forced and we can't write, then simply return false; |
1315 if (forced == NO_FORCE && | 1301 if (forced == NO_FORCE && |
1316 !CanWrite(transmission_type, retransmittable, handshake)) { | 1302 !CanWrite(transmission_type, retransmittable, handshake)) { |
1317 return false; | 1303 return false; |
1318 } | 1304 } |
1319 | 1305 |
1306 DCHECK_LE(sequence_number_of_last_inorder_packet_, sequence_number); | |
1307 // Only increase this when packets have not been queued. Once they're queued | |
1308 // due to a write block, there is the chance of sending forced and other | |
1309 // higher priority packets out of order. | |
1310 if (queued_packets_.empty()) { | |
1311 sequence_number_of_last_inorder_packet_ = sequence_number; | |
1312 } | |
1313 | |
1320 scoped_ptr<QuicEncryptedPacket> encrypted( | 1314 scoped_ptr<QuicEncryptedPacket> encrypted( |
1321 framer_.EncryptPacket(level, sequence_number, *packet)); | 1315 framer_.EncryptPacket(level, sequence_number, *packet)); |
1322 if (encrypted.get() == NULL) { | 1316 if (encrypted.get() == NULL) { |
1323 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " | 1317 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " |
1324 << sequence_number; | 1318 << sequence_number; |
1325 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | 1319 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); |
1326 return false; | 1320 return false; |
1327 } | 1321 } |
1328 DLOG(INFO) << ENDPOINT << "Sending packet number " << sequence_number | 1322 DLOG(INFO) << ENDPOINT << "Sending packet number " << sequence_number |
1329 << " : " << (packet->is_fec_packet() ? "FEC " : | 1323 << " : " << (packet->is_fec_packet() ? "FEC " : |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1408 // WritePacketToWire returned -1, then |error| will be populated with | 1402 // WritePacketToWire returned -1, then |error| will be populated with |
1409 // an error code, which we want to pass along to the visitor. | 1403 // an error code, which we want to pass along to the visitor. |
1410 debug_visitor_->OnPacketSent(sequence_number, level, packet, | 1404 debug_visitor_->OnPacketSent(sequence_number, level, packet, |
1411 bytes_written == -1 ? *error : bytes_written); | 1405 bytes_written == -1 ? *error : bytes_written); |
1412 } | 1406 } |
1413 return bytes_written; | 1407 return bytes_written; |
1414 } | 1408 } |
1415 | 1409 |
1416 bool QuicConnection::OnSerializedPacket( | 1410 bool QuicConnection::OnSerializedPacket( |
1417 const SerializedPacket& serialized_packet) { | 1411 const SerializedPacket& serialized_packet) { |
1418 ack_notifier_manager_.OnSerializedPacket(serialized_packet); | |
1419 | |
1420 if (serialized_packet.retransmittable_frames) { | 1412 if (serialized_packet.retransmittable_frames) { |
1421 serialized_packet.retransmittable_frames-> | 1413 serialized_packet.retransmittable_frames-> |
1422 set_encryption_level(encryption_level_); | 1414 set_encryption_level(encryption_level_); |
1423 } | 1415 } |
1424 sent_packet_manager_.OnSerializedPacket(serialized_packet, | 1416 sent_packet_manager_.OnSerializedPacket(serialized_packet, |
1425 clock_->ApproximateNow()); | 1417 clock_->ApproximateNow()); |
1426 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions | 1418 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions |
1427 // serialize packets and invoke SendOrQueuePacket directly. | 1419 // serialize packets and invoke SendOrQueuePacket directly. |
1428 return SendOrQueuePacket(encryption_level_, | 1420 return SendOrQueuePacket(encryption_level_, |
1429 serialized_packet.sequence_number, | 1421 serialized_packet.sequence_number, |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1853 // If we changed the generator's batch state, restore original batch state. | 1845 // If we changed the generator's batch state, restore original batch state. |
1854 if (!already_in_batch_mode_) { | 1846 if (!already_in_batch_mode_) { |
1855 DVLOG(1) << "Leaving Batch Mode."; | 1847 DVLOG(1) << "Leaving Batch Mode."; |
1856 connection_->packet_generator_.FinishBatchOperations(); | 1848 connection_->packet_generator_.FinishBatchOperations(); |
1857 } | 1849 } |
1858 DCHECK_EQ(already_in_batch_mode_, | 1850 DCHECK_EQ(already_in_batch_mode_, |
1859 connection_->packet_generator_.InBatchMode()); | 1851 connection_->packet_generator_.InBatchMode()); |
1860 } | 1852 } |
1861 | 1853 |
1862 } // namespace net | 1854 } // namespace net |
OLD | NEW |