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 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 } | 942 } |
943 | 943 |
944 void QuicConnection::MaybeSendInResponseToPacket() { | 944 void QuicConnection::MaybeSendInResponseToPacket() { |
945 if (!connected_) { | 945 if (!connected_) { |
946 return; | 946 return; |
947 } | 947 } |
948 ScopedPacketBundler bundler(this, ack_queued_ ? SEND_ACK : NO_ACK); | 948 ScopedPacketBundler bundler(this, ack_queued_ ? SEND_ACK : NO_ACK); |
949 | 949 |
950 // Now that we have received an ack, we might be able to send packets which | 950 // Now that we have received an ack, we might be able to send packets which |
951 // are queued locally, or drain streams which are blocked. | 951 // are queued locally, or drain streams which are blocked. |
952 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( | 952 if (CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
953 time_of_last_received_packet_, NOT_RETRANSMISSION, | 953 OnCanWrite(); |
954 HAS_RETRANSMITTABLE_DATA); | |
955 if (delay.IsZero()) { | |
956 send_alarm_->Cancel(); | |
957 WriteIfNotBlocked(); | |
958 } else if (!delay.IsInfinite()) { | |
959 send_alarm_->Cancel(); | |
960 send_alarm_->Set(time_of_last_received_packet_.Add(delay)); | |
961 } | 954 } |
962 } | 955 } |
963 | 956 |
964 void QuicConnection::SendVersionNegotiationPacket() { | 957 void QuicConnection::SendVersionNegotiationPacket() { |
965 // TODO(alyssar): implement zero server state negotiation. | 958 // TODO(alyssar): implement zero server state negotiation. |
966 pending_version_negotiation_packet_ = true; | 959 pending_version_negotiation_packet_ = true; |
967 if (writer_->IsWriteBlocked()) { | 960 if (writer_->IsWriteBlocked()) { |
968 visitor_->OnWriteBlocked(); | 961 visitor_->OnWriteBlocked(); |
969 return; | 962 return; |
970 } | 963 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1059 | 1052 |
1060 const QuicConnectionStats& QuicConnection::GetStats() { | 1053 const QuicConnectionStats& QuicConnection::GetStats() { |
1061 // Update rtt and estimated bandwidth. | 1054 // Update rtt and estimated bandwidth. |
1062 stats_.min_rtt_us = | 1055 stats_.min_rtt_us = |
1063 sent_packet_manager_.GetRttStats()->min_rtt().ToMicroseconds(); | 1056 sent_packet_manager_.GetRttStats()->min_rtt().ToMicroseconds(); |
1064 stats_.srtt_us = | 1057 stats_.srtt_us = |
1065 sent_packet_manager_.GetRttStats()->SmoothedRtt().ToMicroseconds(); | 1058 sent_packet_manager_.GetRttStats()->SmoothedRtt().ToMicroseconds(); |
1066 stats_.estimated_bandwidth = | 1059 stats_.estimated_bandwidth = |
1067 sent_packet_manager_.BandwidthEstimate().ToBytesPerSecond(); | 1060 sent_packet_manager_.BandwidthEstimate().ToBytesPerSecond(); |
1068 stats_.congestion_window = sent_packet_manager_.GetCongestionWindow(); | 1061 stats_.congestion_window = sent_packet_manager_.GetCongestionWindow(); |
1069 stats_.max_packet_size = options()->max_packet_length; | 1062 stats_.max_packet_size = packet_creator_.max_packet_length(); |
1070 return stats_; | 1063 return stats_; |
1071 } | 1064 } |
1072 | 1065 |
1073 void QuicConnection::ProcessUdpPacket(const IPEndPoint& self_address, | 1066 void QuicConnection::ProcessUdpPacket(const IPEndPoint& self_address, |
1074 const IPEndPoint& peer_address, | 1067 const IPEndPoint& peer_address, |
1075 const QuicEncryptedPacket& packet) { | 1068 const QuicEncryptedPacket& packet) { |
1076 if (!connected_) { | 1069 if (!connected_) { |
1077 return; | 1070 return; |
1078 } | 1071 } |
1079 if (debug_visitor_) { | 1072 if (debug_visitor_) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1138 void QuicConnection::OnCanWrite() { | 1131 void QuicConnection::OnCanWrite() { |
1139 DCHECK(!writer_->IsWriteBlocked()); | 1132 DCHECK(!writer_->IsWriteBlocked()); |
1140 | 1133 |
1141 WriteQueuedPackets(); | 1134 WriteQueuedPackets(); |
1142 WritePendingRetransmissions(); | 1135 WritePendingRetransmissions(); |
1143 | 1136 |
1144 // Sending queued packets may have caused the socket to become write blocked, | 1137 // Sending queued packets may have caused the socket to become write blocked, |
1145 // or the congestion manager to prohibit sending. If we've sent everything | 1138 // or the congestion manager to prohibit sending. If we've sent everything |
1146 // we had queued and we're still not blocked, let the visitor know it can | 1139 // we had queued and we're still not blocked, let the visitor know it can |
1147 // write more. | 1140 // write more. |
1148 if (!CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA)) { | 1141 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
1149 return; | 1142 return; |
1150 } | 1143 } |
1151 | 1144 |
1152 { // Limit the scope of the bundler. | 1145 { // Limit the scope of the bundler. |
1153 // Set |include_ack| to false in bundler; ack inclusion happens elsewhere. | 1146 // Set |include_ack| to false in bundler; ack inclusion happens elsewhere. |
1154 ScopedPacketBundler bundler(this, NO_ACK); | 1147 ScopedPacketBundler bundler(this, NO_ACK); |
1155 visitor_->OnCanWrite(); | 1148 visitor_->OnCanWrite(); |
1156 } | 1149 } |
1157 | 1150 |
1158 // After the visitor writes, it may have caused the socket to become write | 1151 // After the visitor writes, it may have caused the socket to become write |
1159 // blocked or the congestion manager to prohibit sending, so check again. | 1152 // blocked or the congestion manager to prohibit sending, so check again. |
1160 if (visitor_->WillingAndAbleToWrite() && | 1153 if (visitor_->WillingAndAbleToWrite() && |
1161 !resume_writes_alarm_->IsSet() && | 1154 !resume_writes_alarm_->IsSet() && |
1162 CanWrite(NOT_RETRANSMISSION, HAS_RETRANSMITTABLE_DATA)) { | 1155 CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
1163 // We're not write blocked, but some stream didn't write out all of its | 1156 // We're not write blocked, but some stream didn't write out all of its |
1164 // bytes. Register for 'immediate' resumption so we'll keep writing after | 1157 // bytes. Register for 'immediate' resumption so we'll keep writing after |
1165 // other connections and events have had a chance to use the thread. | 1158 // other connections and events have had a chance to use the thread. |
1166 resume_writes_alarm_->Set(clock_->ApproximateNow()); | 1159 resume_writes_alarm_->Set(clock_->ApproximateNow()); |
1167 } | 1160 } |
1168 } | 1161 } |
1169 | 1162 |
1170 void QuicConnection::WriteIfNotBlocked() { | 1163 void QuicConnection::WriteIfNotBlocked() { |
1171 if (!writer_->IsWriteBlocked()) { | 1164 if (!writer_->IsWriteBlocked()) { |
1172 OnCanWrite(); | 1165 OnCanWrite(); |
(...skipping 16 matching lines...) Expand all Loading... |
1189 << peer_address_.port() << " to " << migrating_peer_port_ | 1182 << peer_address_.port() << " to " << migrating_peer_port_ |
1190 << ", migrating connection."; | 1183 << ", migrating connection."; |
1191 peer_address_ = IPEndPoint(peer_address_.address(), migrating_peer_port_); | 1184 peer_address_ = IPEndPoint(peer_address_.address(), migrating_peer_port_); |
1192 } | 1185 } |
1193 | 1186 |
1194 time_of_last_received_packet_ = clock_->Now(); | 1187 time_of_last_received_packet_ = clock_->Now(); |
1195 DVLOG(1) << ENDPOINT << "time of last received packet: " | 1188 DVLOG(1) << ENDPOINT << "time of last received packet: " |
1196 << time_of_last_received_packet_.ToDebuggingValue(); | 1189 << time_of_last_received_packet_.ToDebuggingValue(); |
1197 | 1190 |
1198 if (is_server_ && encryption_level_ == ENCRYPTION_NONE && | 1191 if (is_server_ && encryption_level_ == ENCRYPTION_NONE && |
1199 last_size_ > options()->max_packet_length) { | 1192 last_size_ > packet_creator_.max_packet_length()) { |
1200 options()->max_packet_length = last_size_; | 1193 packet_creator_.set_max_packet_length(last_size_); |
1201 } | 1194 } |
1202 return true; | 1195 return true; |
1203 } | 1196 } |
1204 | 1197 |
1205 void QuicConnection::WriteQueuedPackets() { | 1198 void QuicConnection::WriteQueuedPackets() { |
1206 DCHECK(!writer_->IsWriteBlocked()); | 1199 DCHECK(!writer_->IsWriteBlocked()); |
1207 | 1200 |
1208 if (pending_version_negotiation_packet_) { | 1201 if (pending_version_negotiation_packet_) { |
1209 SendVersionNegotiationPacket(); | 1202 SendVersionNegotiationPacket(); |
1210 } | 1203 } |
(...skipping 12 matching lines...) Expand all Loading... |
1223 } | 1216 } |
1224 } | 1217 } |
1225 | 1218 |
1226 void QuicConnection::WritePendingRetransmissions() { | 1219 void QuicConnection::WritePendingRetransmissions() { |
1227 // Keep writing as long as there's a pending retransmission which can be | 1220 // Keep writing as long as there's a pending retransmission which can be |
1228 // written. | 1221 // written. |
1229 while (sent_packet_manager_.HasPendingRetransmissions()) { | 1222 while (sent_packet_manager_.HasPendingRetransmissions()) { |
1230 const QuicSentPacketManager::PendingRetransmission pending = | 1223 const QuicSentPacketManager::PendingRetransmission pending = |
1231 sent_packet_manager_.NextPendingRetransmission(); | 1224 sent_packet_manager_.NextPendingRetransmission(); |
1232 if (GetPacketType(&pending.retransmittable_frames) == NORMAL && | 1225 if (GetPacketType(&pending.retransmittable_frames) == NORMAL && |
1233 !CanWrite(pending.transmission_type, HAS_RETRANSMITTABLE_DATA)) { | 1226 !CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
1234 break; | 1227 break; |
1235 } | 1228 } |
1236 | 1229 |
1237 // Re-packetize the frames with a new sequence number for retransmission. | 1230 // Re-packetize the frames with a new sequence number for retransmission. |
1238 // Retransmitted data packets do not use FEC, even when it's enabled. | 1231 // Retransmitted data packets do not use FEC, even when it's enabled. |
1239 // Retransmitted packets use the same sequence number length as the | 1232 // Retransmitted packets use the same sequence number length as the |
1240 // original. | 1233 // original. |
1241 // Flush the packet creator before making a new packet. | 1234 // Flush the packet creator before making a new packet. |
1242 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | 1235 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
1243 // does not require the creator to be flushed. | 1236 // does not require the creator to be flushed. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1281 bool QuicConnection::ShouldGeneratePacket( | 1274 bool QuicConnection::ShouldGeneratePacket( |
1282 TransmissionType transmission_type, | 1275 TransmissionType transmission_type, |
1283 HasRetransmittableData retransmittable, | 1276 HasRetransmittableData retransmittable, |
1284 IsHandshake handshake) { | 1277 IsHandshake handshake) { |
1285 // We should serialize handshake packets immediately to ensure that they | 1278 // We should serialize handshake packets immediately to ensure that they |
1286 // end up sent at the right encryption level. | 1279 // end up sent at the right encryption level. |
1287 if (handshake == IS_HANDSHAKE) { | 1280 if (handshake == IS_HANDSHAKE) { |
1288 return true; | 1281 return true; |
1289 } | 1282 } |
1290 | 1283 |
1291 return CanWrite(transmission_type, retransmittable); | 1284 return CanWrite(retransmittable); |
1292 } | 1285 } |
1293 | 1286 |
1294 bool QuicConnection::CanWrite(TransmissionType transmission_type, | 1287 bool QuicConnection::CanWrite(HasRetransmittableData retransmittable) { |
1295 HasRetransmittableData retransmittable) { | |
1296 if (writer_->IsWriteBlocked()) { | 1288 if (writer_->IsWriteBlocked()) { |
1297 visitor_->OnWriteBlocked(); | 1289 visitor_->OnWriteBlocked(); |
1298 return false; | 1290 return false; |
1299 } | 1291 } |
1300 | 1292 |
1301 // TODO(rch): consider removing this check so that if an ACK comes in | 1293 send_alarm_->Cancel(); |
1302 // before the alarm goes it, we might be able send out a packet. | |
1303 // This check assumes that if the send alarm is set, it applies equally to all | |
1304 // types of transmissions. | |
1305 if (send_alarm_->IsSet()) { | |
1306 DVLOG(1) << "Send alarm set. Not sending."; | |
1307 return false; | |
1308 } | |
1309 | |
1310 QuicTime now = clock_->Now(); | 1294 QuicTime now = clock_->Now(); |
1311 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( | 1295 QuicTime::Delta delay = sent_packet_manager_.TimeUntilSend( |
1312 now, transmission_type, retransmittable); | 1296 now, retransmittable); |
1313 if (delay.IsInfinite()) { | 1297 if (delay.IsInfinite()) { |
1314 return false; | 1298 return false; |
1315 } | 1299 } |
1316 | 1300 |
1317 // If the scheduler requires a delay, then we can not send this packet now. | 1301 // If the scheduler requires a delay, then we can not send this packet now. |
1318 if (!delay.IsZero()) { | 1302 if (!delay.IsZero()) { |
1319 send_alarm_->Cancel(); | |
1320 send_alarm_->Set(now.Add(delay)); | 1303 send_alarm_->Set(now.Add(delay)); |
1321 DVLOG(1) << "Delaying sending."; | 1304 DVLOG(1) << "Delaying sending."; |
1322 return false; | 1305 return false; |
1323 } | 1306 } |
1324 return true; | 1307 return true; |
1325 } | 1308 } |
1326 | 1309 |
1327 bool QuicConnection::WritePacket(QueuedPacket packet) { | 1310 bool QuicConnection::WritePacket(QueuedPacket packet) { |
1328 QuicPacketSequenceNumber sequence_number = packet.sequence_number; | 1311 QuicPacketSequenceNumber sequence_number = packet.sequence_number; |
1329 if (ShouldDiscardPacket(packet.encryption_level, | 1312 if (ShouldDiscardPacket(packet.encryption_level, |
1330 sequence_number, | 1313 sequence_number, |
1331 packet.retransmittable)) { | 1314 packet.retransmittable)) { |
1332 ++stats_.packets_discarded; | 1315 ++stats_.packets_discarded; |
1333 return true; | 1316 return true; |
1334 } | 1317 } |
1335 | 1318 |
1336 // If the packet is CONNECTION_CLOSE, we need to try to send it immediately | 1319 // If the packet is CONNECTION_CLOSE, we need to try to send it immediately |
1337 // and encrypt it to hand it off to TimeWaitListManager. | 1320 // and encrypt it to hand it off to TimeWaitListManager. |
1338 // If the packet is QUEUED, we don't re-consult the congestion control. | 1321 // If the packet is QUEUED, we don't re-consult the congestion control. |
1339 // This ensures packets are sent in sequence number order. | 1322 // This ensures packets are sent in sequence number order. |
1340 // TODO(ianswett): The congestion control should have been consulted before | 1323 // TODO(ianswett): The congestion control should have been consulted before |
1341 // serializing the packet, so this could be turned into a LOG_IF(DFATAL). | 1324 // serializing the packet, so this could be turned into a LOG_IF(DFATAL). |
1342 if (packet.type == NORMAL && !CanWrite(packet.transmission_type, | 1325 if (packet.type == NORMAL && !CanWrite(packet.retransmittable)) { |
1343 packet.retransmittable)) { | |
1344 return false; | 1326 return false; |
1345 } | 1327 } |
1346 | 1328 |
1347 // Some encryption algorithms require the packet sequence numbers not be | 1329 // Some encryption algorithms require the packet sequence numbers not be |
1348 // repeated. | 1330 // repeated. |
1349 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); | 1331 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); |
1350 sequence_number_of_last_sent_packet_ = sequence_number; | 1332 sequence_number_of_last_sent_packet_ = sequence_number; |
1351 | 1333 |
1352 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( | 1334 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( |
1353 packet.encryption_level, sequence_number, *packet.packet); | 1335 packet.encryption_level, sequence_number, *packet.packet); |
(...skipping 14 matching lines...) Expand all Loading... |
1368 // This assures we won't try to write *forced* packets when blocked. | 1350 // This assures we won't try to write *forced* packets when blocked. |
1369 // Return true to stop processing. | 1351 // Return true to stop processing. |
1370 if (writer_->IsWriteBlocked()) { | 1352 if (writer_->IsWriteBlocked()) { |
1371 visitor_->OnWriteBlocked(); | 1353 visitor_->OnWriteBlocked(); |
1372 return true; | 1354 return true; |
1373 } | 1355 } |
1374 } else { | 1356 } else { |
1375 encrypted_deleter.reset(encrypted); | 1357 encrypted_deleter.reset(encrypted); |
1376 } | 1358 } |
1377 | 1359 |
1378 LOG_IF(DFATAL, encrypted->length() > options()->max_packet_length) | 1360 LOG_IF(DFATAL, encrypted->length() > |
| 1361 packet_creator_.max_packet_length()) |
1379 << "Writing an encrypted packet larger than max_packet_length:" | 1362 << "Writing an encrypted packet larger than max_packet_length:" |
1380 << options()->max_packet_length << " encrypted length: " | 1363 << packet_creator_.max_packet_length() << " encrypted length: " |
1381 << encrypted->length(); | 1364 << encrypted->length(); |
1382 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number | 1365 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number |
1383 << " : " << (packet.packet->is_fec_packet() ? "FEC " : | 1366 << " : " << (packet.packet->is_fec_packet() ? "FEC " : |
1384 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA | 1367 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA |
1385 ? "data bearing " : " ack only ")) | 1368 ? "data bearing " : " ack only ")) |
1386 << ", encryption level: " | 1369 << ", encryption level: " |
1387 << QuicUtils::EncryptionLevelToString(packet.encryption_level) | 1370 << QuicUtils::EncryptionLevelToString(packet.encryption_level) |
1388 << ", length:" << packet.packet->length() << ", encrypted length:" | 1371 << ", length:" << packet.packet->length() << ", encrypted length:" |
1389 << encrypted->length(); | 1372 << encrypted->length(); |
1390 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | 1373 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1972 // If we changed the generator's batch state, restore original batch state. | 1955 // If we changed the generator's batch state, restore original batch state. |
1973 if (!already_in_batch_mode_) { | 1956 if (!already_in_batch_mode_) { |
1974 DVLOG(1) << "Leaving Batch Mode."; | 1957 DVLOG(1) << "Leaving Batch Mode."; |
1975 connection_->packet_generator_.FinishBatchOperations(); | 1958 connection_->packet_generator_.FinishBatchOperations(); |
1976 } | 1959 } |
1977 DCHECK_EQ(already_in_batch_mode_, | 1960 DCHECK_EQ(already_in_batch_mode_, |
1978 connection_->packet_generator_.InBatchMode()); | 1961 connection_->packet_generator_.InBatchMode()); |
1979 } | 1962 } |
1980 | 1963 |
1981 } // namespace net | 1964 } // namespace net |
OLD | NEW |