| 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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 connection_->SendPing(); | 147 connection_->SendPing(); |
| 148 return QuicTime::Zero(); | 148 return QuicTime::Zero(); |
| 149 } | 149 } |
| 150 | 150 |
| 151 private: | 151 private: |
| 152 QuicConnection* connection_; | 152 QuicConnection* connection_; |
| 153 | 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(PingAlarm); | 154 DISALLOW_COPY_AND_ASSIGN(PingAlarm); |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 bool IsConnectionClose( | |
| 158 const RetransmittableFrames* retransmittable_frames) { | |
| 159 if (!retransmittable_frames) { | |
| 160 return false; | |
| 161 } | |
| 162 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { | |
| 163 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { | |
| 164 return true; | |
| 165 } | |
| 166 } | |
| 167 return false; | |
| 168 } | |
| 169 | |
| 170 } // namespace | 157 } // namespace |
| 171 | 158 |
| 172 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, | 159 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, |
| 173 EncryptionLevel level, | 160 EncryptionLevel level, |
| 174 TransmissionType transmission_type) | 161 TransmissionType transmission_type) |
| 175 : sequence_number(packet.sequence_number), | 162 : serialized_packet(packet), |
| 176 packet(packet.packet), | |
| 177 encryption_level(level), | 163 encryption_level(level), |
| 178 transmission_type(transmission_type), | 164 transmission_type(transmission_type) { |
| 179 retransmittable((transmission_type != NOT_RETRANSMISSION || | |
| 180 packet.retransmittable_frames != NULL) ? | |
| 181 HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA), | |
| 182 handshake(packet.retransmittable_frames == NULL ? | |
| 183 NOT_HANDSHAKE : packet.retransmittable_frames->HasCryptoHandshake()), | |
| 184 is_connection_close(IsConnectionClose(packet.retransmittable_frames)), | |
| 185 length(packet.packet->length()) { | |
| 186 } | 165 } |
| 187 | 166 |
| 188 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | 167 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
| 189 | 168 |
| 190 QuicConnection::QuicConnection(QuicConnectionId connection_id, | 169 QuicConnection::QuicConnection(QuicConnectionId connection_id, |
| 191 IPEndPoint address, | 170 IPEndPoint address, |
| 192 QuicConnectionHelperInterface* helper, | 171 QuicConnectionHelperInterface* helper, |
| 193 const PacketWriterFactory& writer_factory, | 172 const PacketWriterFactory& writer_factory, |
| 194 bool owns_writer, | 173 bool owns_writer, |
| 195 bool is_server, | 174 bool is_server, |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 234 } |
| 256 | 235 |
| 257 QuicConnection::~QuicConnection() { | 236 QuicConnection::~QuicConnection() { |
| 258 if (owns_writer_) { | 237 if (owns_writer_) { |
| 259 delete writer_; | 238 delete writer_; |
| 260 } | 239 } |
| 261 STLDeleteElements(&undecryptable_packets_); | 240 STLDeleteElements(&undecryptable_packets_); |
| 262 STLDeleteValues(&group_map_); | 241 STLDeleteValues(&group_map_); |
| 263 for (QueuedPacketList::iterator it = queued_packets_.begin(); | 242 for (QueuedPacketList::iterator it = queued_packets_.begin(); |
| 264 it != queued_packets_.end(); ++it) { | 243 it != queued_packets_.end(); ++it) { |
| 265 delete it->packet; | 244 delete it->serialized_packet.packet; |
| 266 } | 245 } |
| 267 } | 246 } |
| 268 | 247 |
| 269 void QuicConnection::SetFromConfig(const QuicConfig& config) { | 248 void QuicConnection::SetFromConfig(const QuicConfig& config) { |
| 270 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); | 249 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); |
| 271 sent_packet_manager_.SetFromConfig(config); | 250 sent_packet_manager_.SetFromConfig(config); |
| 272 } | 251 } |
| 273 | 252 |
| 274 bool QuicConnection::SelectMutualVersion( | 253 bool QuicConnection::SelectMutualVersion( |
| 275 const QuicVersionVector& available_versions) { | 254 const QuicVersionVector& available_versions) { |
| (...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1223 DCHECK(!writer_->IsWriteBlocked()); | 1202 DCHECK(!writer_->IsWriteBlocked()); |
| 1224 | 1203 |
| 1225 if (pending_version_negotiation_packet_) { | 1204 if (pending_version_negotiation_packet_) { |
| 1226 SendVersionNegotiationPacket(); | 1205 SendVersionNegotiationPacket(); |
| 1227 } | 1206 } |
| 1228 | 1207 |
| 1229 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); | 1208 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); |
| 1230 while (!writer_->IsWriteBlocked() && | 1209 while (!writer_->IsWriteBlocked() && |
| 1231 packet_iterator != queued_packets_.end()) { | 1210 packet_iterator != queued_packets_.end()) { |
| 1232 if (WritePacket(*packet_iterator)) { | 1211 if (WritePacket(*packet_iterator)) { |
| 1233 delete packet_iterator->packet; | 1212 delete packet_iterator->serialized_packet.packet; |
| 1234 packet_iterator = queued_packets_.erase(packet_iterator); | 1213 packet_iterator = queued_packets_.erase(packet_iterator); |
| 1235 } else { | 1214 } else { |
| 1236 // Continue, because some queued packets may still be writable. | 1215 // Continue, because some queued packets may still be writable. |
| 1237 // This can happen if a retransmit send fails. | 1216 // This can happen if a retransmit send fails. |
| 1238 ++packet_iterator; | 1217 ++packet_iterator; |
| 1239 } | 1218 } |
| 1240 } | 1219 } |
| 1241 } | 1220 } |
| 1242 | 1221 |
| 1243 void QuicConnection::WritePendingRetransmissions() { | 1222 void QuicConnection::WritePendingRetransmissions() { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1324 if (!delay.IsZero()) { | 1303 if (!delay.IsZero()) { |
| 1325 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); | 1304 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); |
| 1326 DVLOG(1) << "Delaying sending."; | 1305 DVLOG(1) << "Delaying sending."; |
| 1327 return false; | 1306 return false; |
| 1328 } | 1307 } |
| 1329 send_alarm_->Cancel(); | 1308 send_alarm_->Cancel(); |
| 1330 return true; | 1309 return true; |
| 1331 } | 1310 } |
| 1332 | 1311 |
| 1333 bool QuicConnection::WritePacket(QueuedPacket packet) { | 1312 bool QuicConnection::WritePacket(QueuedPacket packet) { |
| 1334 QuicPacketSequenceNumber sequence_number = packet.sequence_number; | 1313 QuicPacketSequenceNumber sequence_number = |
| 1314 packet.serialized_packet.sequence_number; |
| 1335 if (ShouldDiscardPacket(packet.encryption_level, | 1315 if (ShouldDiscardPacket(packet.encryption_level, |
| 1336 sequence_number, | 1316 sequence_number, |
| 1337 packet.retransmittable)) { | 1317 IsRetransmittable(packet))) { |
| 1338 ++stats_.packets_discarded; | 1318 ++stats_.packets_discarded; |
| 1339 return true; | 1319 return true; |
| 1340 } | 1320 } |
| 1341 | 1321 |
| 1342 // Some encryption algorithms require the packet sequence numbers not be | 1322 // Some encryption algorithms require the packet sequence numbers not be |
| 1343 // repeated. | 1323 // repeated. |
| 1344 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); | 1324 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); |
| 1345 sequence_number_of_last_sent_packet_ = sequence_number; | 1325 sequence_number_of_last_sent_packet_ = sequence_number; |
| 1346 | 1326 |
| 1347 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( | 1327 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( |
| 1348 packet.encryption_level, sequence_number, *packet.packet); | 1328 packet.encryption_level, |
| 1329 sequence_number, |
| 1330 *packet.serialized_packet.packet); |
| 1349 if (encrypted == NULL) { | 1331 if (encrypted == NULL) { |
| 1350 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " | 1332 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " |
| 1351 << sequence_number; | 1333 << sequence_number; |
| 1352 // CloseConnection does not send close packet, so no infinite loop here. | 1334 // CloseConnection does not send close packet, so no infinite loop here. |
| 1353 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | 1335 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); |
| 1354 return false; | 1336 return false; |
| 1355 } | 1337 } |
| 1356 | 1338 |
| 1357 // Connection close packets are eventually owned by TimeWaitListManager. | 1339 // Connection close packets are eventually owned by TimeWaitListManager. |
| 1358 // Others are deleted at the end of this call. | 1340 // Others are deleted at the end of this call. |
| 1359 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; | 1341 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; |
| 1360 if (packet.is_connection_close) { | 1342 if (IsConnectionClose(packet)) { |
| 1361 DCHECK(connection_close_packet_.get() == NULL); | 1343 DCHECK(connection_close_packet_.get() == NULL); |
| 1362 connection_close_packet_.reset(encrypted); | 1344 connection_close_packet_.reset(encrypted); |
| 1363 // This assures we won't try to write *forced* packets when blocked. | 1345 // This assures we won't try to write *forced* packets when blocked. |
| 1364 // Return true to stop processing. | 1346 // Return true to stop processing. |
| 1365 if (writer_->IsWriteBlocked()) { | 1347 if (writer_->IsWriteBlocked()) { |
| 1366 visitor_->OnWriteBlocked(); | 1348 visitor_->OnWriteBlocked(); |
| 1367 return true; | 1349 return true; |
| 1368 } | 1350 } |
| 1369 } else { | 1351 } else { |
| 1370 encrypted_deleter.reset(encrypted); | 1352 encrypted_deleter.reset(encrypted); |
| 1371 } | 1353 } |
| 1372 | 1354 |
| 1373 if (!FLAGS_quic_allow_oversized_packets_for_test) { | 1355 if (!FLAGS_quic_allow_oversized_packets_for_test) { |
| 1374 DCHECK_LE(encrypted->length(), kMaxPacketSize); | 1356 DCHECK_LE(encrypted->length(), kMaxPacketSize); |
| 1375 } | 1357 } |
| 1376 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); | 1358 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); |
| 1377 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number | 1359 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : " |
| 1378 << " : " << (packet.packet->is_fec_packet() ? "FEC " : | 1360 << (packet.serialized_packet.packet->is_fec_packet() ? "FEC " : |
| 1379 (packet.retransmittable == HAS_RETRANSMITTABLE_DATA | 1361 (IsRetransmittable(packet) == HAS_RETRANSMITTABLE_DATA |
| 1380 ? "data bearing " : " ack only ")) | 1362 ? "data bearing " : " ack only ")) |
| 1381 << ", encryption level: " | 1363 << ", encryption level: " |
| 1382 << QuicUtils::EncryptionLevelToString(packet.encryption_level) | 1364 << QuicUtils::EncryptionLevelToString(packet.encryption_level) |
| 1383 << ", length:" << packet.packet->length() << ", encrypted length:" | 1365 << ", length:" |
| 1366 << packet.serialized_packet.packet->length() |
| 1367 << ", encrypted length:" |
| 1384 << encrypted->length(); | 1368 << encrypted->length(); |
| 1385 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl | 1369 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl |
| 1386 << QuicUtils::StringToHexASCIIDump(packet.packet->AsStringPiece()); | 1370 << QuicUtils::StringToHexASCIIDump( |
| 1371 packet.serialized_packet.packet->AsStringPiece()); |
| 1387 | 1372 |
| 1388 WriteResult result = writer_->WritePacket(encrypted->data(), | 1373 WriteResult result = writer_->WritePacket(encrypted->data(), |
| 1389 encrypted->length(), | 1374 encrypted->length(), |
| 1390 self_address().address(), | 1375 self_address().address(), |
| 1391 peer_address()); | 1376 peer_address()); |
| 1392 if (result.error_code == ERR_IO_PENDING) { | 1377 if (result.error_code == ERR_IO_PENDING) { |
| 1393 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); | 1378 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); |
| 1394 } | 1379 } |
| 1395 if (debug_visitor_.get() != NULL) { | 1380 if (debug_visitor_.get() != NULL) { |
| 1396 // Pass the write result to the visitor. | 1381 // Pass the write result to the visitor. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1419 DVLOG(1) << ENDPOINT << "time of last sent packet: " | 1404 DVLOG(1) << ENDPOINT << "time of last sent packet: " |
| 1420 << now.ToDebuggingValue(); | 1405 << now.ToDebuggingValue(); |
| 1421 | 1406 |
| 1422 // TODO(ianswett): Change the sequence number length and other packet creator | 1407 // TODO(ianswett): Change the sequence number length and other packet creator |
| 1423 // options by a more explicit API than setting a struct value directly, | 1408 // options by a more explicit API than setting a struct value directly, |
| 1424 // perhaps via the NetworkChangeVisitor. | 1409 // perhaps via the NetworkChangeVisitor. |
| 1425 packet_generator_.UpdateSequenceNumberLength( | 1410 packet_generator_.UpdateSequenceNumberLength( |
| 1426 sent_packet_manager_.least_packet_awaited_by_peer(), | 1411 sent_packet_manager_.least_packet_awaited_by_peer(), |
| 1427 sent_packet_manager_.GetCongestionWindow()); | 1412 sent_packet_manager_.GetCongestionWindow()); |
| 1428 | 1413 |
| 1429 bool reset_retransmission_alarm = | 1414 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( |
| 1430 sent_packet_manager_.OnPacketSent(sequence_number, | 1415 sequence_number, |
| 1431 now, | 1416 now, |
| 1432 encrypted->length(), | 1417 encrypted->length(), |
| 1433 packet.transmission_type, | 1418 packet.transmission_type, |
| 1434 packet.retransmittable); | 1419 IsRetransmittable(packet)); |
| 1435 | 1420 |
| 1436 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | 1421 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
| 1437 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), | 1422 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), |
| 1438 QuicTime::Delta::FromMilliseconds(1)); | 1423 QuicTime::Delta::FromMilliseconds(1)); |
| 1439 } | 1424 } |
| 1440 | 1425 |
| 1441 stats_.bytes_sent += result.bytes_written; | 1426 stats_.bytes_sent += result.bytes_written; |
| 1442 ++stats_.packets_sent; | 1427 ++stats_.packets_sent; |
| 1443 if (packet.transmission_type != NOT_RETRANSMISSION) { | 1428 if (packet.transmission_type != NOT_RETRANSMISSION) { |
| 1444 stats_.bytes_retransmitted += result.bytes_written; | 1429 stats_.bytes_retransmitted += result.bytes_written; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1531 if (packet.packet == NULL) { | 1516 if (packet.packet == NULL) { |
| 1532 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; | 1517 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; |
| 1533 return true; | 1518 return true; |
| 1534 } | 1519 } |
| 1535 | 1520 |
| 1536 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, | 1521 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, |
| 1537 packet.entropy_hash); | 1522 packet.entropy_hash); |
| 1538 QueuedPacket queued_packet(packet, level, transmission_type); | 1523 QueuedPacket queued_packet(packet, level, transmission_type); |
| 1539 // If there are already queued packets, put this at the end, | 1524 // If there are already queued packets, put this at the end, |
| 1540 // unless it's ConnectionClose, in which case it is written immediately. | 1525 // unless it's ConnectionClose, in which case it is written immediately. |
| 1541 if ((queued_packet.is_connection_close || queued_packets_.empty()) && | 1526 if ((IsConnectionClose(queued_packet) |
| 1527 || queued_packets_.empty()) && |
| 1542 WritePacket(queued_packet)) { | 1528 WritePacket(queued_packet)) { |
| 1543 delete packet.packet; | 1529 delete packet.packet; |
| 1544 return true; | 1530 return true; |
| 1545 } | 1531 } |
| 1546 queued_packets_.push_back(queued_packet); | 1532 queued_packets_.push_back(queued_packet); |
| 1547 return false; | 1533 return false; |
| 1548 } | 1534 } |
| 1549 | 1535 |
| 1550 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { | 1536 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { |
| 1551 stop_waiting->least_unacked = GetLeastUnacked(); | 1537 stop_waiting->least_unacked = GetLeastUnacked(); |
| (...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1988 } | 1974 } |
| 1989 // If we changed the generator's batch state, restore original batch state. | 1975 // If we changed the generator's batch state, restore original batch state. |
| 1990 if (!already_in_batch_mode_) { | 1976 if (!already_in_batch_mode_) { |
| 1991 DVLOG(1) << "Leaving Batch Mode."; | 1977 DVLOG(1) << "Leaving Batch Mode."; |
| 1992 connection_->packet_generator_.FinishBatchOperations(); | 1978 connection_->packet_generator_.FinishBatchOperations(); |
| 1993 } | 1979 } |
| 1994 DCHECK_EQ(already_in_batch_mode_, | 1980 DCHECK_EQ(already_in_batch_mode_, |
| 1995 connection_->packet_generator_.InBatchMode()); | 1981 connection_->packet_generator_.InBatchMode()); |
| 1996 } | 1982 } |
| 1997 | 1983 |
| 1984 HasRetransmittableData QuicConnection::IsRetransmittable( |
| 1985 QueuedPacket packet) { |
| 1986 // TODO(cyr): Understand why the first check here is necessary. Without it, |
| 1987 // DiscardRetransmit test fails. |
| 1988 if (packet.transmission_type != NOT_RETRANSMISSION || |
| 1989 packet.serialized_packet.retransmittable_frames != NULL) { |
| 1990 return HAS_RETRANSMITTABLE_DATA; |
| 1991 } else { |
| 1992 return NO_RETRANSMITTABLE_DATA; |
| 1993 } |
| 1994 } |
| 1995 |
| 1996 bool QuicConnection::IsConnectionClose( |
| 1997 QueuedPacket packet) { |
| 1998 RetransmittableFrames* retransmittable_frames = |
| 1999 packet.serialized_packet.retransmittable_frames; |
| 2000 if (!retransmittable_frames) { |
| 2001 return false; |
| 2002 } |
| 2003 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { |
| 2004 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { |
| 2005 return true; |
| 2006 } |
| 2007 } |
| 2008 return false; |
| 2009 } |
| 2010 |
| 1998 } // namespace net | 2011 } // namespace net |
| OLD | NEW |