| 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> |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 94 |
| 95 virtual QuicTime OnAlarm() OVERRIDE { | 95 virtual QuicTime OnAlarm() OVERRIDE { |
| 96 connection_->OnRetransmissionTimeout(); | 96 connection_->OnRetransmissionTimeout(); |
| 97 return QuicTime::Zero(); | 97 return QuicTime::Zero(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 private: | 100 private: |
| 101 QuicConnection* connection_; | 101 QuicConnection* connection_; |
| 102 }; | 102 }; |
| 103 | 103 |
| 104 // This alarm will be scheduled any time a FEC-bearing packet is sent out. | |
| 105 // When the alarm goes off, the connection checks to see if the oldest packets | |
| 106 // have been acked, and removes them from the congestion window if not. | |
| 107 class AbandonFecAlarm : public QuicAlarm::Delegate { | |
| 108 public: | |
| 109 explicit AbandonFecAlarm(QuicConnection* connection) | |
| 110 : connection_(connection) { | |
| 111 } | |
| 112 | |
| 113 virtual QuicTime OnAlarm() OVERRIDE { | |
| 114 return connection_->OnAbandonFecTimeout(); | |
| 115 } | |
| 116 | |
| 117 private: | |
| 118 QuicConnection* connection_; | |
| 119 }; | |
| 120 | |
| 121 // An alarm that is scheduled when the sent scheduler requires a | 104 // An alarm that is scheduled when the sent scheduler requires a |
| 122 // a delay before sending packets and fires when the packet may be sent. | 105 // a delay before sending packets and fires when the packet may be sent. |
| 123 class SendAlarm : public QuicAlarm::Delegate { | 106 class SendAlarm : public QuicAlarm::Delegate { |
| 124 public: | 107 public: |
| 125 explicit SendAlarm(QuicConnection* connection) | 108 explicit SendAlarm(QuicConnection* connection) |
| 126 : connection_(connection) { | 109 : connection_(connection) { |
| 127 } | 110 } |
| 128 | 111 |
| 129 virtual QuicTime OnAlarm() OVERRIDE { | 112 virtual QuicTime OnAlarm() OVERRIDE { |
| 130 connection_->WriteIfNotBlocked(); | 113 connection_->WriteIfNotBlocked(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 clock_(helper->GetClock()), | 184 clock_(helper->GetClock()), |
| 202 random_generator_(helper->GetRandomGenerator()), | 185 random_generator_(helper->GetRandomGenerator()), |
| 203 guid_(guid), | 186 guid_(guid), |
| 204 peer_address_(address), | 187 peer_address_(address), |
| 205 largest_seen_packet_with_ack_(0), | 188 largest_seen_packet_with_ack_(0), |
| 206 pending_version_negotiation_packet_(false), | 189 pending_version_negotiation_packet_(false), |
| 207 write_blocked_(false), | 190 write_blocked_(false), |
| 208 received_packet_manager_(kTCP), | 191 received_packet_manager_(kTCP), |
| 209 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), | 192 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), |
| 210 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), | 193 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), |
| 211 abandon_fec_alarm_(helper->CreateAlarm(new AbandonFecAlarm(this))), | |
| 212 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), | 194 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), |
| 213 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), | 195 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), |
| 214 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), | 196 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), |
| 215 debug_visitor_(NULL), | 197 debug_visitor_(NULL), |
| 216 packet_creator_(guid_, &framer_, random_generator_, is_server), | 198 packet_creator_(guid_, &framer_, random_generator_, is_server), |
| 217 packet_generator_(this, NULL, &packet_creator_), | 199 packet_generator_(this, NULL, &packet_creator_), |
| 218 idle_network_timeout_( | 200 idle_network_timeout_( |
| 219 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), | 201 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), |
| 220 overall_connection_timeout_(QuicTime::Delta::Infinite()), | 202 overall_connection_timeout_(QuicTime::Delta::Infinite()), |
| 221 creation_time_(clock_->ApproximateNow()), | 203 creation_time_(clock_->ApproximateNow()), |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 | 499 |
| 518 bool reset_retransmission_alarm = | 500 bool reset_retransmission_alarm = |
| 519 sent_packet_manager_.OnIncomingAck(incoming_ack.received_info, | 501 sent_packet_manager_.OnIncomingAck(incoming_ack.received_info, |
| 520 time_of_last_received_packet_); | 502 time_of_last_received_packet_); |
| 521 if (sent_packet_manager_.HasPendingRetransmissions()) { | 503 if (sent_packet_manager_.HasPendingRetransmissions()) { |
| 522 WriteIfNotBlocked(); | 504 WriteIfNotBlocked(); |
| 523 } | 505 } |
| 524 | 506 |
| 525 if (reset_retransmission_alarm) { | 507 if (reset_retransmission_alarm) { |
| 526 retransmission_alarm_->Cancel(); | 508 retransmission_alarm_->Cancel(); |
| 527 abandon_fec_alarm_->Cancel(); | |
| 528 // Reset the RTO and FEC alarms if the are unacked packets. | 509 // Reset the RTO and FEC alarms if the are unacked packets. |
| 529 QuicTime::Delta retransmission_delay = | |
| 530 sent_packet_manager_.GetRetransmissionDelay(); | |
| 531 if (sent_packet_manager_.HasUnackedPackets()) { | 510 if (sent_packet_manager_.HasUnackedPackets()) { |
| 511 QuicTime::Delta retransmission_delay = |
| 512 sent_packet_manager_.GetRetransmissionDelay(); |
| 532 retransmission_alarm_->Set( | 513 retransmission_alarm_->Set( |
| 533 clock_->ApproximateNow().Add(retransmission_delay)); | 514 clock_->ApproximateNow().Add(retransmission_delay)); |
| 534 } | 515 } |
| 535 if (sent_packet_manager_.HasUnackedFecPackets()) { | |
| 536 abandon_fec_alarm_->Set( | |
| 537 clock_->ApproximateNow().Add(retransmission_delay)); | |
| 538 } | |
| 539 } | 516 } |
| 540 } | 517 } |
| 541 | 518 |
| 542 bool QuicConnection::OnCongestionFeedbackFrame( | 519 bool QuicConnection::OnCongestionFeedbackFrame( |
| 543 const QuicCongestionFeedbackFrame& feedback) { | 520 const QuicCongestionFeedbackFrame& feedback) { |
| 544 DCHECK(connected_); | 521 DCHECK(connected_); |
| 545 if (debug_visitor_) { | 522 if (debug_visitor_) { |
| 546 debug_visitor_->OnCongestionFeedbackFrame(feedback); | 523 debug_visitor_->OnCongestionFeedbackFrame(feedback); |
| 547 } | 524 } |
| 548 last_congestion_frames_.push_back(feedback); | 525 last_congestion_frames_.push_back(feedback); |
| (...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 // If the scheduler requires a delay, then we can not send this packet now. | 1078 // If the scheduler requires a delay, then we can not send this packet now. |
| 1102 if (!delay.IsZero()) { | 1079 if (!delay.IsZero()) { |
| 1103 send_alarm_->Cancel(); | 1080 send_alarm_->Cancel(); |
| 1104 send_alarm_->Set(now.Add(delay)); | 1081 send_alarm_->Set(now.Add(delay)); |
| 1105 DVLOG(1) << "Delaying sending."; | 1082 DVLOG(1) << "Delaying sending."; |
| 1106 return false; | 1083 return false; |
| 1107 } | 1084 } |
| 1108 return true; | 1085 return true; |
| 1109 } | 1086 } |
| 1110 | 1087 |
| 1111 void QuicConnection::SetupRetransmission( | 1088 void QuicConnection::SetupRetransmissionAlarm( |
| 1112 QuicPacketSequenceNumber sequence_number, | 1089 QuicPacketSequenceNumber sequence_number) { |
| 1113 EncryptionLevel level) { | |
| 1114 if (!sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { | 1090 if (!sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { |
| 1115 DVLOG(1) << ENDPOINT << "Will not retransmit packet " << sequence_number; | 1091 DVLOG(1) << ENDPOINT << "Will not retransmit packet " << sequence_number; |
| 1116 return; | 1092 return; |
| 1117 } | 1093 } |
| 1118 | 1094 |
| 1119 // Do not set the retransmission alarm if we're already handling one, since | 1095 // Do not set the retransmission alarm if we're already handling one, since |
| 1120 // it will be reset when OnRetransmissionTimeout completes. | 1096 // it will be reset when OnRetransmissionTimeout completes. |
| 1121 if (retransmission_alarm_->IsSet()) { | 1097 if (retransmission_alarm_->IsSet()) { |
| 1122 return; | 1098 return; |
| 1123 } | 1099 } |
| 1124 | 1100 |
| 1125 QuicTime::Delta retransmission_delay = | 1101 QuicTime::Delta retransmission_delay = |
| 1126 sent_packet_manager_.GetRetransmissionDelay(); | 1102 sent_packet_manager_.GetRetransmissionDelay(); |
| 1127 retransmission_alarm_->Set( | 1103 retransmission_alarm_->Set( |
| 1128 clock_->ApproximateNow().Add(retransmission_delay)); | 1104 clock_->ApproximateNow().Add(retransmission_delay)); |
| 1129 } | 1105 } |
| 1130 | 1106 |
| 1131 void QuicConnection::SetupAbandonFecTimer( | |
| 1132 QuicPacketSequenceNumber sequence_number) { | |
| 1133 if (abandon_fec_alarm_->IsSet()) { | |
| 1134 return; | |
| 1135 } | |
| 1136 QuicTime::Delta retransmission_delay = | |
| 1137 sent_packet_manager_.GetRetransmissionDelay(); | |
| 1138 abandon_fec_alarm_->Set(clock_->ApproximateNow().Add(retransmission_delay)); | |
| 1139 } | |
| 1140 | |
| 1141 bool QuicConnection::WritePacket(EncryptionLevel level, | 1107 bool QuicConnection::WritePacket(EncryptionLevel level, |
| 1142 QuicPacketSequenceNumber sequence_number, | 1108 QuicPacketSequenceNumber sequence_number, |
| 1143 QuicPacket* packet, | 1109 QuicPacket* packet, |
| 1144 TransmissionType transmission_type, | 1110 TransmissionType transmission_type, |
| 1145 HasRetransmittableData retransmittable, | 1111 HasRetransmittableData retransmittable, |
| 1146 IsHandshake handshake, | 1112 IsHandshake handshake, |
| 1147 Force forced) { | 1113 Force forced) { |
| 1148 if (ShouldDiscardPacket(level, sequence_number, retransmittable)) { | 1114 if (ShouldDiscardPacket(level, sequence_number, retransmittable)) { |
| 1149 delete packet; | 1115 delete packet; |
| 1150 return true; | 1116 return true; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1307 bool QuicConnection::OnPacketSent(WriteResult result) { | 1273 bool QuicConnection::OnPacketSent(WriteResult result) { |
| 1308 DCHECK_NE(WRITE_STATUS_BLOCKED, result.status); | 1274 DCHECK_NE(WRITE_STATUS_BLOCKED, result.status); |
| 1309 if (pending_write_.get() == NULL) { | 1275 if (pending_write_.get() == NULL) { |
| 1310 LOG(DFATAL) << "OnPacketSent called without a pending write."; | 1276 LOG(DFATAL) << "OnPacketSent called without a pending write."; |
| 1311 return false; | 1277 return false; |
| 1312 } | 1278 } |
| 1313 | 1279 |
| 1314 QuicPacketSequenceNumber sequence_number = pending_write_->sequence_number; | 1280 QuicPacketSequenceNumber sequence_number = pending_write_->sequence_number; |
| 1315 TransmissionType transmission_type = pending_write_->transmission_type; | 1281 TransmissionType transmission_type = pending_write_->transmission_type; |
| 1316 HasRetransmittableData retransmittable = pending_write_->retransmittable; | 1282 HasRetransmittableData retransmittable = pending_write_->retransmittable; |
| 1317 EncryptionLevel level = pending_write_->level; | |
| 1318 bool is_fec_packet = pending_write_->is_fec_packet; | 1283 bool is_fec_packet = pending_write_->is_fec_packet; |
| 1319 size_t length = pending_write_->length; | 1284 size_t length = pending_write_->length; |
| 1320 pending_write_.reset(); | 1285 pending_write_.reset(); |
| 1321 | 1286 |
| 1322 if (result.status == WRITE_STATUS_ERROR) { | 1287 if (result.status == WRITE_STATUS_ERROR) { |
| 1323 DVLOG(1) << "Write failed with error code: " << result.error_code; | 1288 DVLOG(1) << "Write failed with error code: " << result.error_code; |
| 1324 // We can't send an error as the socket is presumably borked. | 1289 // We can't send an error as the socket is presumably borked. |
| 1325 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 1290 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
| 1326 return false; | 1291 return false; |
| 1327 } | 1292 } |
| 1328 | 1293 |
| 1329 QuicTime now = clock_->Now(); | 1294 QuicTime now = clock_->Now(); |
| 1330 if (transmission_type == NOT_RETRANSMISSION) { | 1295 if (transmission_type == NOT_RETRANSMISSION) { |
| 1331 time_of_last_sent_packet_ = now; | 1296 time_of_last_sent_packet_ = now; |
| 1332 } | 1297 } |
| 1333 DVLOG(1) << ENDPOINT << "time of last sent packet: " | 1298 DVLOG(1) << ENDPOINT << "time of last sent packet: " |
| 1334 << now.ToDebuggingValue(); | 1299 << now.ToDebuggingValue(); |
| 1335 | 1300 |
| 1336 // Set the retransmit alarm only when we have sent the packet to the client | 1301 // Set the retransmit alarm only when we have sent the packet to the client |
| 1337 // and not when it goes to the pending queue, otherwise we will end up adding | 1302 // and not when it goes to the pending queue, otherwise we will end up adding |
| 1338 // an entry to retransmission_timeout_ every time we attempt a write. | 1303 // an entry to retransmission_timeout_ every time we attempt a write. |
| 1339 if (retransmittable == HAS_RETRANSMITTABLE_DATA) { | 1304 if (retransmittable == HAS_RETRANSMITTABLE_DATA || is_fec_packet) { |
| 1340 SetupRetransmission(sequence_number, level); | 1305 SetupRetransmissionAlarm(sequence_number); |
| 1341 } else if (is_fec_packet) { | |
| 1342 SetupAbandonFecTimer(sequence_number); | |
| 1343 } | 1306 } |
| 1344 | 1307 |
| 1345 // TODO(ianswett): Change the sequence number length and other packet creator | 1308 // TODO(ianswett): Change the sequence number length and other packet creator |
| 1346 // options by a more explicit API than setting a struct value directly. | 1309 // options by a more explicit API than setting a struct value directly. |
| 1347 packet_creator_.UpdateSequenceNumberLength( | 1310 packet_creator_.UpdateSequenceNumberLength( |
| 1348 received_packet_manager_.least_packet_awaited_by_peer(), | 1311 received_packet_manager_.least_packet_awaited_by_peer(), |
| 1349 sent_packet_manager_.BandwidthEstimate().ToBytesPerPeriod( | 1312 sent_packet_manager_.BandwidthEstimate().ToBytesPerPeriod( |
| 1350 sent_packet_manager_.SmoothedRtt())); | 1313 sent_packet_manager_.SmoothedRtt())); |
| 1351 | 1314 |
| 1352 sent_packet_manager_.OnPacketSent(sequence_number, now, length, | 1315 sent_packet_manager_.OnPacketSent(sequence_number, now, length, |
| 1353 transmission_type, retransmittable); | 1316 transmission_type, retransmittable); |
| 1354 | 1317 |
| 1355 stats_.bytes_sent += result.bytes_written; | 1318 stats_.bytes_sent += result.bytes_written; |
| 1356 ++stats_.packets_sent; | 1319 ++stats_.packets_sent; |
| 1357 | 1320 |
| 1358 if (transmission_type == NACK_RETRANSMISSION || | 1321 if (transmission_type == NACK_RETRANSMISSION || |
| 1359 transmission_type == RTO_RETRANSMISSION) { | 1322 transmission_type == RTO_RETRANSMISSION) { |
| 1360 stats_.bytes_retransmitted += result.bytes_written; | 1323 stats_.bytes_retransmitted += result.bytes_written; |
| 1361 ++stats_.packets_retransmitted; | 1324 ++stats_.packets_retransmitted; |
| 1362 } | 1325 } |
| 1363 | 1326 |
| 1364 return true; | 1327 return true; |
| 1365 } | 1328 } |
| 1366 | 1329 |
| 1367 bool QuicConnection::OnSerializedPacket( | 1330 bool QuicConnection::OnSerializedPacket( |
| 1368 const SerializedPacket& serialized_packet) { | 1331 const SerializedPacket& serialized_packet) { |
| 1369 if (serialized_packet.retransmittable_frames) { | 1332 if (serialized_packet.retransmittable_frames) { |
| 1370 serialized_packet.retransmittable_frames-> | 1333 serialized_packet.retransmittable_frames-> |
| 1371 set_encryption_level(encryption_level_); | 1334 set_encryption_level(encryption_level_); |
| 1372 } | 1335 } |
| 1373 sent_packet_manager_.OnSerializedPacket(serialized_packet, | 1336 sent_packet_manager_.OnSerializedPacket(serialized_packet); |
| 1374 clock_->ApproximateNow()); | |
| 1375 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions | 1337 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions |
| 1376 // serialize packets and invoke SendOrQueuePacket directly. | 1338 // serialize packets and invoke SendOrQueuePacket directly. |
| 1377 return SendOrQueuePacket(encryption_level_, | 1339 return SendOrQueuePacket(encryption_level_, |
| 1378 serialized_packet, | 1340 serialized_packet, |
| 1379 NOT_RETRANSMISSION); | 1341 NOT_RETRANSMISSION); |
| 1380 } | 1342 } |
| 1381 | 1343 |
| 1382 QuicPacketSequenceNumber QuicConnection::GetNextPacketSequenceNumber() { | 1344 QuicPacketSequenceNumber QuicConnection::GetNextPacketSequenceNumber() { |
| 1383 return packet_creator_.sequence_number() + 1; | 1345 return packet_creator_.sequence_number() + 1; |
| 1384 } | 1346 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1429 void QuicConnection::OnRetransmissionTimeout() { | 1391 void QuicConnection::OnRetransmissionTimeout() { |
| 1430 if (!sent_packet_manager_.HasUnackedPackets()) { | 1392 if (!sent_packet_manager_.HasUnackedPackets()) { |
| 1431 return; | 1393 return; |
| 1432 } | 1394 } |
| 1433 | 1395 |
| 1434 ++stats_.rto_count; | 1396 ++stats_.rto_count; |
| 1435 | 1397 |
| 1436 sent_packet_manager_.OnRetransmissionTimeout(); | 1398 sent_packet_manager_.OnRetransmissionTimeout(); |
| 1437 | 1399 |
| 1438 WriteIfNotBlocked(); | 1400 WriteIfNotBlocked(); |
| 1439 } | |
| 1440 | 1401 |
| 1441 QuicTime QuicConnection::OnAbandonFecTimeout() { | 1402 // Ensure the retransmission alarm is always set if there are unacked packets. |
| 1442 QuicTime fec_timeout = sent_packet_manager_.OnAbandonFecTimeout(); | 1403 if (sent_packet_manager_.HasUnackedPackets() && !HasQueuedData() && |
| 1443 | 1404 !retransmission_alarm_->IsSet()) { |
| 1444 // If a packet was abandoned, then the congestion window may have | 1405 QuicTime rto_timeout = clock_->ApproximateNow().Add( |
| 1445 // opened up, so attempt to write. | 1406 sent_packet_manager_.GetRetransmissionDelay()); |
| 1446 WriteIfNotBlocked(); | 1407 retransmission_alarm_->Set(rto_timeout); |
| 1447 | 1408 } |
| 1448 return fec_timeout; | |
| 1449 } | 1409 } |
| 1450 | 1410 |
| 1451 void QuicConnection::SetEncrypter(EncryptionLevel level, | 1411 void QuicConnection::SetEncrypter(EncryptionLevel level, |
| 1452 QuicEncrypter* encrypter) { | 1412 QuicEncrypter* encrypter) { |
| 1453 framer_.SetEncrypter(level, encrypter); | 1413 framer_.SetEncrypter(level, encrypter); |
| 1454 } | 1414 } |
| 1455 | 1415 |
| 1456 const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const { | 1416 const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const { |
| 1457 return framer_.encrypter(level); | 1417 return framer_.encrypter(level); |
| 1458 } | 1418 } |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1729 // If we changed the generator's batch state, restore original batch state. | 1689 // If we changed the generator's batch state, restore original batch state. |
| 1730 if (!already_in_batch_mode_) { | 1690 if (!already_in_batch_mode_) { |
| 1731 DVLOG(1) << "Leaving Batch Mode."; | 1691 DVLOG(1) << "Leaving Batch Mode."; |
| 1732 connection_->packet_generator_.FinishBatchOperations(); | 1692 connection_->packet_generator_.FinishBatchOperations(); |
| 1733 } | 1693 } |
| 1734 DCHECK_EQ(already_in_batch_mode_, | 1694 DCHECK_EQ(already_in_batch_mode_, |
| 1735 connection_->packet_generator_.InBatchMode()); | 1695 connection_->packet_generator_.InBatchMode()); |
| 1736 } | 1696 } |
| 1737 | 1697 |
| 1738 } // namespace net | 1698 } // namespace net |
| OLD | NEW |