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 QuicConnection::PacketType GetPacketType( | 157 bool IsConnectionClose( |
158 const RetransmittableFrames* retransmittable_frames) { | 158 const RetransmittableFrames* retransmittable_frames) { |
159 if (!retransmittable_frames) { | 159 if (!retransmittable_frames) { |
160 return QuicConnection::NORMAL; | 160 return false; |
161 } | 161 } |
162 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { | 162 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { |
163 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { | 163 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { |
164 return QuicConnection::CONNECTION_CLOSE; | 164 return true; |
165 } | 165 } |
166 } | 166 } |
167 return QuicConnection::NORMAL; | 167 return false; |
168 } | 168 } |
169 | 169 |
170 } // namespace | 170 } // namespace |
171 | 171 |
172 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, | 172 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, |
173 EncryptionLevel level, | 173 EncryptionLevel level, |
174 TransmissionType transmission_type) | 174 TransmissionType transmission_type) |
175 : sequence_number(packet.sequence_number), | 175 : sequence_number(packet.sequence_number), |
176 packet(packet.packet), | 176 packet(packet.packet), |
177 encryption_level(level), | 177 encryption_level(level), |
178 transmission_type(transmission_type), | 178 transmission_type(transmission_type), |
179 retransmittable((transmission_type != NOT_RETRANSMISSION || | 179 retransmittable((transmission_type != NOT_RETRANSMISSION || |
180 packet.retransmittable_frames != NULL) ? | 180 packet.retransmittable_frames != NULL) ? |
181 HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA), | 181 HAS_RETRANSMITTABLE_DATA : NO_RETRANSMITTABLE_DATA), |
182 handshake(packet.retransmittable_frames == NULL ? | 182 handshake(packet.retransmittable_frames == NULL ? |
183 NOT_HANDSHAKE : packet.retransmittable_frames->HasCryptoHandshake()), | 183 NOT_HANDSHAKE : packet.retransmittable_frames->HasCryptoHandshake()), |
184 type(GetPacketType(packet.retransmittable_frames)), | 184 is_connection_close(IsConnectionClose(packet.retransmittable_frames)), |
185 length(packet.packet->length()) { | 185 length(packet.packet->length()) { |
186 } | 186 } |
187 | 187 |
188 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") | 188 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") |
189 | 189 |
190 QuicConnection::QuicConnection(QuicConnectionId connection_id, | 190 QuicConnection::QuicConnection(QuicConnectionId connection_id, |
191 IPEndPoint address, | 191 IPEndPoint address, |
192 QuicConnectionHelperInterface* helper, | 192 QuicConnectionHelperInterface* helper, |
193 const PacketWriterFactory& writer_factory, | 193 const PacketWriterFactory& writer_factory, |
194 bool owns_writer, | 194 bool owns_writer, |
(...skipping 1044 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1239 } | 1239 } |
1240 } | 1240 } |
1241 } | 1241 } |
1242 | 1242 |
1243 void QuicConnection::WritePendingRetransmissions() { | 1243 void QuicConnection::WritePendingRetransmissions() { |
1244 // Keep writing as long as there's a pending retransmission which can be | 1244 // Keep writing as long as there's a pending retransmission which can be |
1245 // written. | 1245 // written. |
1246 while (sent_packet_manager_.HasPendingRetransmissions()) { | 1246 while (sent_packet_manager_.HasPendingRetransmissions()) { |
1247 const QuicSentPacketManager::PendingRetransmission pending = | 1247 const QuicSentPacketManager::PendingRetransmission pending = |
1248 sent_packet_manager_.NextPendingRetransmission(); | 1248 sent_packet_manager_.NextPendingRetransmission(); |
1249 if (GetPacketType(&pending.retransmittable_frames) == NORMAL && | 1249 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
1250 !CanWrite(HAS_RETRANSMITTABLE_DATA)) { | |
1251 break; | 1250 break; |
1252 } | 1251 } |
1253 | 1252 |
1254 // Re-packetize the frames with a new sequence number for retransmission. | 1253 // Re-packetize the frames with a new sequence number for retransmission. |
1255 // Retransmitted data packets do not use FEC, even when it's enabled. | 1254 // Retransmitted data packets do not use FEC, even when it's enabled. |
1256 // Retransmitted packets use the same sequence number length as the | 1255 // Retransmitted packets use the same sequence number length as the |
1257 // original. | 1256 // original. |
1258 // Flush the packet generator before making a new packet. | 1257 // Flush the packet generator before making a new packet. |
1259 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | 1258 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
1260 // does not require the creator to be flushed. | 1259 // does not require the creator to be flushed. |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1333 | 1332 |
1334 bool QuicConnection::WritePacket(QueuedPacket packet) { | 1333 bool QuicConnection::WritePacket(QueuedPacket packet) { |
1335 QuicPacketSequenceNumber sequence_number = packet.sequence_number; | 1334 QuicPacketSequenceNumber sequence_number = packet.sequence_number; |
1336 if (ShouldDiscardPacket(packet.encryption_level, | 1335 if (ShouldDiscardPacket(packet.encryption_level, |
1337 sequence_number, | 1336 sequence_number, |
1338 packet.retransmittable)) { | 1337 packet.retransmittable)) { |
1339 ++stats_.packets_discarded; | 1338 ++stats_.packets_discarded; |
1340 return true; | 1339 return true; |
1341 } | 1340 } |
1342 | 1341 |
1343 // If the packet is CONNECTION_CLOSE, we need to try to send it immediately | |
1344 // and encrypt it to hand it off to TimeWaitListManager. | |
1345 // If the packet is QUEUED, we don't re-consult the congestion control. | |
1346 // This ensures packets are sent in sequence number order. | |
1347 // TODO(ianswett): The congestion control should have been consulted before | |
1348 // serializing the packet, so this could be turned into a LOG_IF(DFATAL). | |
1349 if (packet.type == NORMAL && !CanWrite(packet.retransmittable)) { | |
1350 return false; | |
1351 } | |
1352 | |
1353 // Some encryption algorithms require the packet sequence numbers not be | 1342 // Some encryption algorithms require the packet sequence numbers not be |
1354 // repeated. | 1343 // repeated. |
1355 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); | 1344 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); |
1356 sequence_number_of_last_sent_packet_ = sequence_number; | 1345 sequence_number_of_last_sent_packet_ = sequence_number; |
1357 | 1346 |
1358 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( | 1347 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( |
1359 packet.encryption_level, sequence_number, *packet.packet); | 1348 packet.encryption_level, sequence_number, *packet.packet); |
1360 if (encrypted == NULL) { | 1349 if (encrypted == NULL) { |
1361 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " | 1350 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " |
1362 << sequence_number; | 1351 << sequence_number; |
1363 // CloseConnection does not send close packet, so no infinite loop here. | 1352 // CloseConnection does not send close packet, so no infinite loop here. |
1364 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | 1353 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); |
1365 return false; | 1354 return false; |
1366 } | 1355 } |
1367 | 1356 |
1368 // Connection close packets are eventually owned by TimeWaitListManager. | 1357 // Connection close packets are eventually owned by TimeWaitListManager. |
1369 // Others are deleted at the end of this call. | 1358 // Others are deleted at the end of this call. |
1370 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; | 1359 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; |
1371 if (packet.type == CONNECTION_CLOSE) { | 1360 if (packet.is_connection_close) { |
1372 DCHECK(connection_close_packet_.get() == NULL); | 1361 DCHECK(connection_close_packet_.get() == NULL); |
1373 connection_close_packet_.reset(encrypted); | 1362 connection_close_packet_.reset(encrypted); |
1374 // This assures we won't try to write *forced* packets when blocked. | 1363 // This assures we won't try to write *forced* packets when blocked. |
1375 // Return true to stop processing. | 1364 // Return true to stop processing. |
1376 if (writer_->IsWriteBlocked()) { | 1365 if (writer_->IsWriteBlocked()) { |
1377 visitor_->OnWriteBlocked(); | 1366 visitor_->OnWriteBlocked(); |
1378 return true; | 1367 return true; |
1379 } | 1368 } |
1380 } else { | 1369 } else { |
1381 encrypted_deleter.reset(encrypted); | 1370 encrypted_deleter.reset(encrypted); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1531 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); | 1520 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); |
1532 } | 1521 } |
1533 | 1522 |
1534 void QuicConnection::OnHandshakeComplete() { | 1523 void QuicConnection::OnHandshakeComplete() { |
1535 sent_packet_manager_.SetHandshakeConfirmed(); | 1524 sent_packet_manager_.SetHandshakeConfirmed(); |
1536 } | 1525 } |
1537 | 1526 |
1538 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, | 1527 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, |
1539 const SerializedPacket& packet, | 1528 const SerializedPacket& packet, |
1540 TransmissionType transmission_type) { | 1529 TransmissionType transmission_type) { |
| 1530 // The caller of this function is responsible for checking CanWrite(). |
1541 if (packet.packet == NULL) { | 1531 if (packet.packet == NULL) { |
1542 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; | 1532 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; |
1543 return true; | 1533 return true; |
1544 } | 1534 } |
1545 | 1535 |
1546 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, | 1536 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, |
1547 packet.entropy_hash); | 1537 packet.entropy_hash); |
1548 QueuedPacket queued_packet(packet, level, transmission_type); | 1538 QueuedPacket queued_packet(packet, level, transmission_type); |
1549 // If there are already queued packets, put this at the end, | 1539 // If there are already queued packets, put this at the end, |
1550 // unless it's ConnectionClose, in which case it is written immediately. | 1540 // unless it's ConnectionClose, in which case it is written immediately. |
1551 if ((queued_packet.type == CONNECTION_CLOSE || queued_packets_.empty()) && | 1541 if ((queued_packet.is_connection_close || queued_packets_.empty()) && |
1552 WritePacket(queued_packet)) { | 1542 WritePacket(queued_packet)) { |
1553 delete packet.packet; | 1543 delete packet.packet; |
1554 return true; | 1544 return true; |
1555 } | 1545 } |
1556 queued_packet.type = QUEUED; | |
1557 queued_packets_.push_back(queued_packet); | 1546 queued_packets_.push_back(queued_packet); |
1558 return false; | 1547 return false; |
1559 } | 1548 } |
1560 | 1549 |
1561 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { | 1550 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { |
1562 stop_waiting->least_unacked = GetLeastUnacked(); | 1551 stop_waiting->least_unacked = GetLeastUnacked(); |
1563 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( | 1552 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( |
1564 stop_waiting->least_unacked - 1); | 1553 stop_waiting->least_unacked - 1); |
1565 } | 1554 } |
1566 | 1555 |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2000 // If we changed the generator's batch state, restore original batch state. | 1989 // If we changed the generator's batch state, restore original batch state. |
2001 if (!already_in_batch_mode_) { | 1990 if (!already_in_batch_mode_) { |
2002 DVLOG(1) << "Leaving Batch Mode."; | 1991 DVLOG(1) << "Leaving Batch Mode."; |
2003 connection_->packet_generator_.FinishBatchOperations(); | 1992 connection_->packet_generator_.FinishBatchOperations(); |
2004 } | 1993 } |
2005 DCHECK_EQ(already_in_batch_mode_, | 1994 DCHECK_EQ(already_in_batch_mode_, |
2006 connection_->packet_generator_.InBatchMode()); | 1995 connection_->packet_generator_.InBatchMode()); |
2007 } | 1996 } |
2008 | 1997 |
2009 } // namespace net | 1998 } // namespace net |
OLD | NEW |