Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: net/quic/quic_connection.cc

Issue 559373003: Landing Recent QUIC Changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compiler errors Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/quic/quic_connection.h ('k') | net/quic/quic_connection_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 153
154 private: 154 private:
155 QuicConnection* connection_; 155 QuicConnection* connection_;
156 156
157 DISALLOW_COPY_AND_ASSIGN(PingAlarm); 157 DISALLOW_COPY_AND_ASSIGN(PingAlarm);
158 }; 158 };
159 159
160 } // namespace 160 } // namespace
161 161
162 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet, 162 QuicConnection::QueuedPacket::QueuedPacket(SerializedPacket packet,
163 EncryptionLevel level, 163 EncryptionLevel level)
164 TransmissionType transmission_type)
165 : serialized_packet(packet), 164 : serialized_packet(packet),
166 encryption_level(level), 165 encryption_level(level),
167 transmission_type(transmission_type) { 166 transmission_type(NOT_RETRANSMISSION),
167 original_sequence_number(0) {
168 }
169
170 QuicConnection::QueuedPacket::QueuedPacket(
171 SerializedPacket packet,
172 EncryptionLevel level,
173 TransmissionType transmission_type,
174 QuicPacketSequenceNumber original_sequence_number)
175 : serialized_packet(packet),
176 encryption_level(level),
177 transmission_type(transmission_type),
178 original_sequence_number(original_sequence_number) {
168 } 179 }
169 180
170 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") 181 #define ENDPOINT (is_server_ ? "Server: " : " Client: ")
171 182
172 QuicConnection::QuicConnection(QuicConnectionId connection_id, 183 QuicConnection::QuicConnection(QuicConnectionId connection_id,
173 IPEndPoint address, 184 IPEndPoint address,
174 QuicConnectionHelperInterface* helper, 185 QuicConnectionHelperInterface* helper,
175 const PacketWriterFactory& writer_factory, 186 const PacketWriterFactory& writer_factory,
176 bool owns_writer, 187 bool owns_writer,
177 bool is_server, 188 bool is_server,
(...skipping 22 matching lines...) Expand all
200 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))), 211 ack_alarm_(helper->CreateAlarm(new AckAlarm(this))),
201 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))), 212 retransmission_alarm_(helper->CreateAlarm(new RetransmissionAlarm(this))),
202 send_alarm_(helper->CreateAlarm(new SendAlarm(this))), 213 send_alarm_(helper->CreateAlarm(new SendAlarm(this))),
203 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))), 214 resume_writes_alarm_(helper->CreateAlarm(new SendAlarm(this))),
204 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))), 215 timeout_alarm_(helper->CreateAlarm(new TimeoutAlarm(this))),
205 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))), 216 ping_alarm_(helper->CreateAlarm(new PingAlarm(this))),
206 packet_generator_(connection_id_, &framer_, random_generator_, this), 217 packet_generator_(connection_id_, &framer_, random_generator_, this),
207 idle_network_timeout_( 218 idle_network_timeout_(
208 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)), 219 QuicTime::Delta::FromSeconds(kDefaultInitialTimeoutSecs)),
209 overall_connection_timeout_(QuicTime::Delta::Infinite()), 220 overall_connection_timeout_(QuicTime::Delta::Infinite()),
210 time_of_last_received_packet_(clock_->ApproximateNow()), 221 time_of_last_received_packet_(
211 time_of_last_sent_new_packet_(clock_->ApproximateNow()), 222 FLAGS_quic_timeouts_require_activity
223 ? QuicTime::Zero() : clock_->ApproximateNow()),
224 time_of_last_sent_new_packet_(
225 FLAGS_quic_timeouts_require_activity
226 ? QuicTime::Zero() : clock_->ApproximateNow()),
212 sequence_number_of_last_sent_packet_(0), 227 sequence_number_of_last_sent_packet_(0),
213 sent_packet_manager_( 228 sent_packet_manager_(
214 is_server, clock_, &stats_, 229 is_server, clock_, &stats_,
215 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic, 230 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic,
216 FLAGS_quic_use_time_loss_detection ? kTime : kNack), 231 FLAGS_quic_use_time_loss_detection ? kTime : kNack),
217 version_negotiation_state_(START_NEGOTIATION), 232 version_negotiation_state_(START_NEGOTIATION),
218 is_server_(is_server), 233 is_server_(is_server),
219 connected_(true), 234 connected_(true),
220 peer_ip_changed_(false), 235 peer_ip_changed_(false),
221 peer_port_changed_(false), 236 peer_port_changed_(false),
(...skipping 16 matching lines...) Expand all
238 } 253 }
239 254
240 QuicConnection::~QuicConnection() { 255 QuicConnection::~QuicConnection() {
241 if (owns_writer_) { 256 if (owns_writer_) {
242 delete writer_; 257 delete writer_;
243 } 258 }
244 STLDeleteElements(&undecryptable_packets_); 259 STLDeleteElements(&undecryptable_packets_);
245 STLDeleteValues(&group_map_); 260 STLDeleteValues(&group_map_);
246 for (QueuedPacketList::iterator it = queued_packets_.begin(); 261 for (QueuedPacketList::iterator it = queued_packets_.begin();
247 it != queued_packets_.end(); ++it) { 262 it != queued_packets_.end(); ++it) {
263 delete it->serialized_packet.retransmittable_frames;
248 delete it->serialized_packet.packet; 264 delete it->serialized_packet.packet;
249 } 265 }
250 } 266 }
251 267
252 void QuicConnection::SetFromConfig(const QuicConfig& config) { 268 void QuicConnection::SetFromConfig(const QuicConfig& config) {
253 SetIdleNetworkTimeout(config.idle_connection_state_lifetime()); 269 SetIdleNetworkTimeout(config.idle_connection_state_lifetime());
254 sent_packet_manager_.SetFromConfig(config); 270 sent_packet_manager_.SetFromConfig(config);
255 } 271 }
256 272
257 bool QuicConnection::SelectMutualVersion( 273 bool QuicConnection::SelectMutualVersion(
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 if (!SelectMutualVersion(packet.versions)) { 409 if (!SelectMutualVersion(packet.versions)) {
394 SendConnectionCloseWithDetails(QUIC_INVALID_VERSION, 410 SendConnectionCloseWithDetails(QUIC_INVALID_VERSION,
395 "no common version found"); 411 "no common version found");
396 return; 412 return;
397 } 413 }
398 414
399 DVLOG(1) << ENDPOINT 415 DVLOG(1) << ENDPOINT
400 << "Negotiated version: " << QuicVersionToString(version()); 416 << "Negotiated version: " << QuicVersionToString(version());
401 server_supported_versions_ = packet.versions; 417 server_supported_versions_ = packet.versions;
402 version_negotiation_state_ = NEGOTIATION_IN_PROGRESS; 418 version_negotiation_state_ = NEGOTIATION_IN_PROGRESS;
403 RetransmitUnackedPackets(ALL_PACKETS); 419 RetransmitUnackedPackets(ALL_UNACKED_RETRANSMISSION);
404 } 420 }
405 421
406 void QuicConnection::OnRevivedPacket() { 422 void QuicConnection::OnRevivedPacket() {
407 } 423 }
408 424
409 bool QuicConnection::OnUnauthenticatedPublicHeader( 425 bool QuicConnection::OnUnauthenticatedPublicHeader(
410 const QuicPacketPublicHeader& header) { 426 const QuicPacketPublicHeader& header) {
411 return true; 427 return true;
412 } 428 }
413 429
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after
1210 1226
1211 void QuicConnection::WriteQueuedPackets() { 1227 void QuicConnection::WriteQueuedPackets() {
1212 DCHECK(!writer_->IsWriteBlocked()); 1228 DCHECK(!writer_->IsWriteBlocked());
1213 1229
1214 if (pending_version_negotiation_packet_) { 1230 if (pending_version_negotiation_packet_) {
1215 SendVersionNegotiationPacket(); 1231 SendVersionNegotiationPacket();
1216 } 1232 }
1217 1233
1218 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); 1234 QueuedPacketList::iterator packet_iterator = queued_packets_.begin();
1219 while (packet_iterator != queued_packets_.end() && 1235 while (packet_iterator != queued_packets_.end() &&
1220 WritePacket(*packet_iterator)) { 1236 WritePacket(&(*packet_iterator))) {
1221 delete packet_iterator->serialized_packet.packet;
1222 packet_iterator = queued_packets_.erase(packet_iterator); 1237 packet_iterator = queued_packets_.erase(packet_iterator);
1223 } 1238 }
1224 } 1239 }
1225 1240
1226 void QuicConnection::WritePendingRetransmissions() { 1241 void QuicConnection::WritePendingRetransmissions() {
1227 // Keep writing as long as there's a pending retransmission which can be 1242 // Keep writing as long as there's a pending retransmission which can be
1228 // written. 1243 // written.
1229 while (sent_packet_manager_.HasPendingRetransmissions()) { 1244 while (sent_packet_manager_.HasPendingRetransmissions()) {
1230 const QuicSentPacketManager::PendingRetransmission pending = 1245 const QuicSentPacketManager::PendingRetransmission pending =
1231 sent_packet_manager_.NextPendingRetransmission(); 1246 sent_packet_manager_.NextPendingRetransmission();
1232 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { 1247 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) {
1233 break; 1248 break;
1234 } 1249 }
1235 1250
1236 // Re-packetize the frames with a new sequence number for retransmission. 1251 // Re-packetize the frames with a new sequence number for retransmission.
1237 // Retransmitted data packets do not use FEC, even when it's enabled. 1252 // Retransmitted data packets do not use FEC, even when it's enabled.
1238 // Retransmitted packets use the same sequence number length as the 1253 // Retransmitted packets use the same sequence number length as the
1239 // original. 1254 // original.
1240 // Flush the packet generator before making a new packet. 1255 // Flush the packet generator before making a new packet.
1241 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that 1256 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that
1242 // does not require the creator to be flushed. 1257 // does not require the creator to be flushed.
1243 packet_generator_.FlushAllQueuedFrames(); 1258 packet_generator_.FlushAllQueuedFrames();
1244 SerializedPacket serialized_packet = packet_generator_.ReserializeAllFrames( 1259 SerializedPacket serialized_packet = packet_generator_.ReserializeAllFrames(
1245 pending.retransmittable_frames.frames(), 1260 pending.retransmittable_frames.frames(),
1246 pending.sequence_number_length); 1261 pending.sequence_number_length);
1247 1262
1248 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number 1263 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.sequence_number
1249 << " as " << serialized_packet.sequence_number; 1264 << " as " << serialized_packet.sequence_number;
1250 if (debug_visitor_.get() != NULL) { 1265 SendOrQueuePacket(
1251 debug_visitor_->OnPacketRetransmitted( 1266 QueuedPacket(serialized_packet,
1252 pending.sequence_number, serialized_packet.sequence_number); 1267 pending.retransmittable_frames.encryption_level(),
1253 } 1268 pending.transmission_type,
1254 sent_packet_manager_.OnRetransmittedPacket( 1269 pending.sequence_number));
1255 pending.sequence_number,
1256 serialized_packet.sequence_number);
1257
1258 SendOrQueuePacket(pending.retransmittable_frames.encryption_level(),
1259 serialized_packet,
1260 pending.transmission_type);
1261 } 1270 }
1262 } 1271 }
1263 1272
1264 void QuicConnection::RetransmitUnackedPackets( 1273 void QuicConnection::RetransmitUnackedPackets(
1265 RetransmissionType retransmission_type) { 1274 TransmissionType retransmission_type) {
1266 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type); 1275 sent_packet_manager_.RetransmitUnackedPackets(retransmission_type);
1267 1276
1268 WriteIfNotBlocked(); 1277 WriteIfNotBlocked();
1269 } 1278 }
1270 1279
1271 void QuicConnection::NeuterUnencryptedPackets() { 1280 void QuicConnection::NeuterUnencryptedPackets() {
1272 sent_packet_manager_.NeuterUnencryptedPackets(); 1281 sent_packet_manager_.NeuterUnencryptedPackets();
1273 // This may have changed the retransmission timer, so re-arm it. 1282 // This may have changed the retransmission timer, so re-arm it.
1274 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime(); 1283 QuicTime retransmission_time = sent_packet_manager_.GetRetransmissionTime();
1275 retransmission_alarm_->Update(retransmission_time, 1284 retransmission_alarm_->Update(retransmission_time,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 // If the scheduler requires a delay, then we can not send this packet now. 1319 // If the scheduler requires a delay, then we can not send this packet now.
1311 if (!delay.IsZero()) { 1320 if (!delay.IsZero()) {
1312 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1)); 1321 send_alarm_->Update(now.Add(delay), QuicTime::Delta::FromMilliseconds(1));
1313 DVLOG(1) << "Delaying sending."; 1322 DVLOG(1) << "Delaying sending.";
1314 return false; 1323 return false;
1315 } 1324 }
1316 send_alarm_->Cancel(); 1325 send_alarm_->Cancel();
1317 return true; 1326 return true;
1318 } 1327 }
1319 1328
1320 bool QuicConnection::WritePacket(const QueuedPacket& packet) { 1329 bool QuicConnection::WritePacket(QueuedPacket* packet) {
1321 if (ShouldDiscardPacket(packet)) { 1330 if (!WritePacketInner(packet)) {
1331 return false;
1332 }
1333 delete packet->serialized_packet.retransmittable_frames;
1334 delete packet->serialized_packet.packet;
1335 packet->serialized_packet.retransmittable_frames = NULL;
1336 packet->serialized_packet.packet = NULL;
1337 return true;
1338 }
1339
1340 bool QuicConnection::WritePacketInner(QueuedPacket* packet) {
1341 if (ShouldDiscardPacket(*packet)) {
1322 ++stats_.packets_discarded; 1342 ++stats_.packets_discarded;
1323 return true; 1343 return true;
1324 } 1344 }
1325 // Connection close packets are encypted and saved, so don't exit early. 1345 // Connection close packets are encrypted and saved, so don't exit early.
1326 if (writer_->IsWriteBlocked() && !IsConnectionClose(packet)) { 1346 if (writer_->IsWriteBlocked() && !IsConnectionClose(*packet)) {
1327 return false; 1347 return false;
1328 } 1348 }
1329 1349
1330 QuicPacketSequenceNumber sequence_number = 1350 QuicPacketSequenceNumber sequence_number =
1331 packet.serialized_packet.sequence_number; 1351 packet->serialized_packet.sequence_number;
1332 // Some encryption algorithms require the packet sequence numbers not be 1352 // Some encryption algorithms require the packet sequence numbers not be
1333 // repeated. 1353 // repeated.
1334 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number); 1354 DCHECK_LE(sequence_number_of_last_sent_packet_, sequence_number);
1335 sequence_number_of_last_sent_packet_ = sequence_number; 1355 sequence_number_of_last_sent_packet_ = sequence_number;
1336 1356
1337 QuicEncryptedPacket* encrypted = framer_.EncryptPacket( 1357 QuicEncryptedPacket* encrypted = framer_.EncryptPacket(
1338 packet.encryption_level, 1358 packet->encryption_level,
1339 sequence_number, 1359 sequence_number,
1340 *packet.serialized_packet.packet); 1360 *packet->serialized_packet.packet);
1341 if (encrypted == NULL) { 1361 if (encrypted == NULL) {
1342 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number " 1362 LOG(DFATAL) << ENDPOINT << "Failed to encrypt packet number "
1343 << sequence_number; 1363 << sequence_number;
1344 // CloseConnection does not send close packet, so no infinite loop here. 1364 // CloseConnection does not send close packet, so no infinite loop here.
1345 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); 1365 CloseConnection(QUIC_ENCRYPTION_FAILURE, false);
1346 return false; 1366 return false;
1347 } 1367 }
1348 1368
1349 // Connection close packets are eventually owned by TimeWaitListManager. 1369 // Connection close packets are eventually owned by TimeWaitListManager.
1350 // Others are deleted at the end of this call. 1370 // Others are deleted at the end of this call.
1351 scoped_ptr<QuicEncryptedPacket> encrypted_deleter; 1371 scoped_ptr<QuicEncryptedPacket> encrypted_deleter;
1352 if (IsConnectionClose(packet)) { 1372 if (IsConnectionClose(*packet)) {
1353 DCHECK(connection_close_packet_.get() == NULL); 1373 DCHECK(connection_close_packet_.get() == NULL);
1354 connection_close_packet_.reset(encrypted); 1374 connection_close_packet_.reset(encrypted);
1355 // This assures we won't try to write *forced* packets when blocked. 1375 // This assures we won't try to write *forced* packets when blocked.
1356 // Return true to stop processing. 1376 // Return true to stop processing.
1357 if (writer_->IsWriteBlocked()) { 1377 if (writer_->IsWriteBlocked()) {
1358 visitor_->OnWriteBlocked(); 1378 visitor_->OnWriteBlocked();
1359 return true; 1379 return true;
1360 } 1380 }
1361 } else { 1381 } else {
1362 encrypted_deleter.reset(encrypted); 1382 encrypted_deleter.reset(encrypted);
1363 } 1383 }
1364 1384
1365 if (!FLAGS_quic_allow_oversized_packets_for_test) { 1385 if (!FLAGS_quic_allow_oversized_packets_for_test) {
1366 DCHECK_LE(encrypted->length(), kMaxPacketSize); 1386 DCHECK_LE(encrypted->length(), kMaxPacketSize);
1367 } 1387 }
1368 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length()); 1388 DCHECK_LE(encrypted->length(), packet_generator_.max_packet_length());
1369 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : " 1389 DVLOG(1) << ENDPOINT << "Sending packet " << sequence_number << " : "
1370 << (packet.serialized_packet.packet->is_fec_packet() ? "FEC " : 1390 << (packet->serialized_packet.packet->is_fec_packet() ? "FEC " :
1371 (IsRetransmittable(packet) == HAS_RETRANSMITTABLE_DATA 1391 (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA
1372 ? "data bearing " : " ack only ")) 1392 ? "data bearing " : " ack only "))
1373 << ", encryption level: " 1393 << ", encryption level: "
1374 << QuicUtils::EncryptionLevelToString(packet.encryption_level) 1394 << QuicUtils::EncryptionLevelToString(packet->encryption_level)
1375 << ", length:" 1395 << ", length:"
1376 << packet.serialized_packet.packet->length() 1396 << packet->serialized_packet.packet->length()
1377 << ", encrypted length:" 1397 << ", encrypted length:"
1378 << encrypted->length(); 1398 << encrypted->length();
1379 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl 1399 DVLOG(2) << ENDPOINT << "packet(" << sequence_number << "): " << std::endl
1380 << QuicUtils::StringToHexASCIIDump( 1400 << QuicUtils::StringToHexASCIIDump(
1381 packet.serialized_packet.packet->AsStringPiece()); 1401 packet->serialized_packet.packet->AsStringPiece());
1382 1402
1383 WriteResult result = writer_->WritePacket(encrypted->data(), 1403 WriteResult result = writer_->WritePacket(encrypted->data(),
1384 encrypted->length(), 1404 encrypted->length(),
1385 self_address().address(), 1405 self_address().address(),
1386 peer_address()); 1406 peer_address());
1387 if (result.error_code == ERR_IO_PENDING) { 1407 if (result.error_code == ERR_IO_PENDING) {
1388 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); 1408 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status);
1389 } 1409 }
1390 if (debug_visitor_.get() != NULL) { 1410 if (debug_visitor_.get() != NULL) {
1391 // Pass the write result to the visitor. 1411 // Pass the write result to the visitor.
1392 debug_visitor_->OnPacketSent(sequence_number, 1412 debug_visitor_->OnPacketSent(sequence_number,
1393 packet.encryption_level, 1413 packet->encryption_level,
1394 packet.transmission_type, 1414 packet->transmission_type,
1395 *encrypted, 1415 *encrypted,
1396 result); 1416 result);
1397 } 1417 }
1398 1418
1399 if (result.status == WRITE_STATUS_BLOCKED) { 1419 if (result.status == WRITE_STATUS_BLOCKED) {
1400 visitor_->OnWriteBlocked(); 1420 visitor_->OnWriteBlocked();
1401 // If the socket buffers the the data, then the packet should not 1421 // If the socket buffers the the data, then the packet should not
1402 // be queued and sent again, which would result in an unnecessary 1422 // be queued and sent again, which would result in an unnecessary
1403 // duplicate packet being sent. The helper must call OnCanWrite 1423 // duplicate packet being sent. The helper must call OnCanWrite
1404 // when the write completes, and OnWriteError if an error occurs. 1424 // when the write completes, and OnWriteError if an error occurs.
1405 if (!writer_->IsWriteBlockedDataBuffered()) { 1425 if (!writer_->IsWriteBlockedDataBuffered()) {
1406 return false; 1426 return false;
1407 } 1427 }
1408 } 1428 }
1409 QuicTime now = clock_->Now(); 1429 QuicTime now = clock_->Now();
1410 if (packet.transmission_type == NOT_RETRANSMISSION) { 1430 if (packet->transmission_type == NOT_RETRANSMISSION) {
1411 time_of_last_sent_new_packet_ = now; 1431 time_of_last_sent_new_packet_ = now;
1412 } 1432 }
1413 SetPingAlarm(); 1433 SetPingAlarm();
1414 DVLOG(1) << ENDPOINT << "time of last sent packet: " 1434 DVLOG(1) << ENDPOINT << "time of last sent packet: "
1415 << now.ToDebuggingValue(); 1435 << now.ToDebuggingValue();
1416 1436
1417 // TODO(ianswett): Change the sequence number length and other packet creator 1437 // TODO(ianswett): Change the sequence number length and other packet creator
1418 // options by a more explicit API than setting a struct value directly, 1438 // options by a more explicit API than setting a struct value directly,
1419 // perhaps via the NetworkChangeVisitor. 1439 // perhaps via the NetworkChangeVisitor.
1420 packet_generator_.UpdateSequenceNumberLength( 1440 packet_generator_.UpdateSequenceNumberLength(
1421 sent_packet_manager_.least_packet_awaited_by_peer(), 1441 sent_packet_manager_.least_packet_awaited_by_peer(),
1422 sent_packet_manager_.GetCongestionWindow()); 1442 sent_packet_manager_.GetCongestionWindow());
1423 1443
1444 if (packet->original_sequence_number == 0) {
1445 sent_packet_manager_.OnSerializedPacket(packet->serialized_packet);
1446 } else {
1447 if (debug_visitor_.get() != NULL) {
1448 debug_visitor_->OnPacketRetransmitted(
1449 packet->original_sequence_number, sequence_number);
1450 }
1451 sent_packet_manager_.OnRetransmittedPacket(packet->original_sequence_number,
1452 sequence_number);
1453 }
1424 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( 1454 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent(
1425 sequence_number, 1455 sequence_number,
1426 now, 1456 now,
1427 encrypted->length(), 1457 encrypted->length(),
1428 packet.transmission_type, 1458 packet->transmission_type,
1429 IsRetransmittable(packet)); 1459 IsRetransmittable(*packet));
1460 // The SentPacketManager now owns the retransmittable frames.
1461 packet->serialized_packet.retransmittable_frames = NULL;
1430 1462
1431 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { 1463 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) {
1432 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(), 1464 retransmission_alarm_->Update(sent_packet_manager_.GetRetransmissionTime(),
1433 QuicTime::Delta::FromMilliseconds(1)); 1465 QuicTime::Delta::FromMilliseconds(1));
1434 } 1466 }
1435 1467
1436 stats_.bytes_sent += result.bytes_written; 1468 stats_.bytes_sent += result.bytes_written;
1437 ++stats_.packets_sent; 1469 ++stats_.packets_sent;
1438 if (packet.transmission_type != NOT_RETRANSMISSION) { 1470 if (packet->transmission_type != NOT_RETRANSMISSION) {
1439 stats_.bytes_retransmitted += result.bytes_written; 1471 stats_.bytes_retransmitted += result.bytes_written;
1440 ++stats_.packets_retransmitted; 1472 ++stats_.packets_retransmitted;
1441 } 1473 }
1442 1474
1443 if (result.status == WRITE_STATUS_ERROR) { 1475 if (result.status == WRITE_STATUS_ERROR) {
1444 OnWriteError(result.error_code); 1476 OnWriteError(result.error_code);
1445 return false; 1477 return false;
1446 } 1478 }
1447 1479
1448 return true; 1480 return true;
1449 } 1481 }
1450 1482
1451 bool QuicConnection::ShouldDiscardPacket(const QueuedPacket& packet) { 1483 bool QuicConnection::ShouldDiscardPacket(const QueuedPacket& packet) {
1452 if (!connected_) { 1484 if (!connected_) {
1453 DVLOG(1) << ENDPOINT 1485 DVLOG(1) << ENDPOINT
1454 << "Not sending packet as connection is disconnected."; 1486 << "Not sending packet as connection is disconnected.";
1455 return true; 1487 return true;
1456 } 1488 }
1457 1489
1458 QuicPacketSequenceNumber sequence_number = 1490 QuicPacketSequenceNumber sequence_number =
1459 packet.serialized_packet.sequence_number; 1491 packet.serialized_packet.sequence_number;
1460 // If the packet has been discarded before sending, don't send it.
1461 // This occurs if a packet gets serialized, queued, then discarded.
1462 if (!sent_packet_manager_.IsUnacked(sequence_number)) {
1463 DVLOG(1) << ENDPOINT << "Dropping packet before sending: "
1464 << sequence_number << " since it has already been discarded.";
1465 return true;
1466 }
1467
1468 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE && 1492 if (encryption_level_ == ENCRYPTION_FORWARD_SECURE &&
1469 packet.encryption_level == ENCRYPTION_NONE) { 1493 packet.encryption_level == ENCRYPTION_NONE) {
1470 // Drop packets that are NULL encrypted since the peer won't accept them 1494 // Drop packets that are NULL encrypted since the peer won't accept them
1471 // anymore. 1495 // anymore.
1472 DVLOG(1) << ENDPOINT << "Dropping NULL encrypted packet: " 1496 DVLOG(1) << ENDPOINT << "Dropping NULL encrypted packet: "
1473 << sequence_number << " since the connection is forward secure."; 1497 << sequence_number << " since the connection is forward secure.";
1474 LOG_IF(DFATAL,
1475 sent_packet_manager_.HasRetransmittableFrames(sequence_number))
1476 << "Once forward secure, all NULL encrypted packets should be "
1477 << "neutered.";
1478 return true; 1498 return true;
1479 } 1499 }
1480 1500
1501 // If a retransmission has been acked before sending, don't send it.
1502 // This occurs if a packet gets serialized, queued, then discarded.
1481 if (packet.transmission_type != NOT_RETRANSMISSION && 1503 if (packet.transmission_type != NOT_RETRANSMISSION &&
1482 !sent_packet_manager_.HasRetransmittableFrames(sequence_number)) { 1504 (!sent_packet_manager_.IsUnacked(packet.original_sequence_number) ||
1505 !sent_packet_manager_.HasRetransmittableFrames(
1506 packet.original_sequence_number))) {
1483 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number 1507 DVLOG(1) << ENDPOINT << "Dropping unacked packet: " << sequence_number
1484 << " A previous transmission was acked while write blocked."; 1508 << " A previous transmission was acked while write blocked.";
1485 return true; 1509 return true;
1486 } 1510 }
1487 1511
1488 return false; 1512 return false;
1489 } 1513 }
1490 1514
1491 void QuicConnection::OnWriteError(int error_code) { 1515 void QuicConnection::OnWriteError(int error_code) {
1492 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code 1516 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code
1493 << " (" << ErrorToString(error_code) << ")"; 1517 << " (" << ErrorToString(error_code) << ")";
1494 // We can't send an error as the socket is presumably borked. 1518 // We can't send an error as the socket is presumably borked.
1495 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); 1519 CloseConnection(QUIC_PACKET_WRITE_ERROR, false);
1496 } 1520 }
1497 1521
1498 bool QuicConnection::OnSerializedPacket( 1522 void QuicConnection::OnSerializedPacket(
1499 const SerializedPacket& serialized_packet) { 1523 const SerializedPacket& serialized_packet) {
1500 if (serialized_packet.retransmittable_frames) { 1524 if (serialized_packet.retransmittable_frames) {
1501 serialized_packet.retransmittable_frames-> 1525 serialized_packet.retransmittable_frames->
1502 set_encryption_level(encryption_level_); 1526 set_encryption_level(encryption_level_);
1503 } 1527 }
1504 sent_packet_manager_.OnSerializedPacket(serialized_packet); 1528 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_));
1505 // The TransmissionType is NOT_RETRANSMISSION because all retransmissions
1506 // serialize packets and invoke SendOrQueuePacket directly.
1507 return SendOrQueuePacket(encryption_level_,
1508 serialized_packet,
1509 NOT_RETRANSMISSION);
1510 } 1529 }
1511 1530
1512 void QuicConnection::OnCongestionWindowChange(QuicByteCount congestion_window) { 1531 void QuicConnection::OnCongestionWindowChange(QuicByteCount congestion_window) {
1513 packet_generator_.OnCongestionWindowChange(congestion_window); 1532 packet_generator_.OnCongestionWindowChange(congestion_window);
1514 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); 1533 visitor_->OnCongestionWindowChange(clock_->ApproximateNow());
1515 } 1534 }
1516 1535
1517 void QuicConnection::OnHandshakeComplete() { 1536 void QuicConnection::OnHandshakeComplete() {
1518 sent_packet_manager_.SetHandshakeConfirmed(); 1537 sent_packet_manager_.SetHandshakeConfirmed();
1519 } 1538 }
1520 1539
1521 bool QuicConnection::SendOrQueuePacket(EncryptionLevel level, 1540 void QuicConnection::SendOrQueuePacket(QueuedPacket packet) {
1522 const SerializedPacket& packet,
1523 TransmissionType transmission_type) {
1524 // The caller of this function is responsible for checking CanWrite(). 1541 // The caller of this function is responsible for checking CanWrite().
1525 if (packet.packet == NULL) { 1542 if (packet.serialized_packet.packet == NULL) {
1526 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket"; 1543 LOG(DFATAL) << "NULL packet passed in to SendOrQueuePacket";
1527 return true; 1544 return;
1528 } 1545 }
1529 1546
1530 sent_entropy_manager_.RecordPacketEntropyHash(packet.sequence_number, 1547 sent_entropy_manager_.RecordPacketEntropyHash(
1531 packet.entropy_hash); 1548 packet.serialized_packet.sequence_number,
1532 QueuedPacket queued_packet(packet, level, transmission_type); 1549 packet.serialized_packet.entropy_hash);
1533 LOG_IF(DFATAL, !queued_packets_.empty() && !writer_->IsWriteBlocked()) 1550 LOG_IF(DFATAL, !queued_packets_.empty() && !writer_->IsWriteBlocked())
1534 << "Packets should only be left queued if we're write blocked."; 1551 << "Packets should only be left queued if we're write blocked.";
1535 if (WritePacket(queued_packet)) { 1552 if (!WritePacket(&packet)) {
1536 delete packet.packet; 1553 queued_packets_.push_back(packet);
1537 return true;
1538 } 1554 }
1539 queued_packets_.push_back(queued_packet);
1540 return false;
1541 } 1555 }
1542 1556
1543 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) { 1557 void QuicConnection::UpdateStopWaiting(QuicStopWaitingFrame* stop_waiting) {
1544 stop_waiting->least_unacked = GetLeastUnacked(); 1558 stop_waiting->least_unacked = GetLeastUnacked();
1545 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy( 1559 stop_waiting->entropy_hash = sent_entropy_manager_.GetCumulativeEntropy(
1546 stop_waiting->least_unacked - 1); 1560 stop_waiting->least_unacked - 1);
1547 } 1561 }
1548 1562
1549 void QuicConnection::SendPing() { 1563 void QuicConnection::SendPing() {
1550 if (retransmission_alarm_->IsSet()) { 1564 if (retransmission_alarm_->IsSet()) {
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1885 } else { 1899 } else {
1886 overall_connection_timeout_ = timeout; 1900 overall_connection_timeout_ = timeout;
1887 } 1901 }
1888 } 1902 }
1889 1903
1890 bool QuicConnection::CheckForTimeout() { 1904 bool QuicConnection::CheckForTimeout() {
1891 QuicTime now = clock_->ApproximateNow(); 1905 QuicTime now = clock_->ApproximateNow();
1892 QuicTime time_of_last_packet = max(time_of_last_received_packet_, 1906 QuicTime time_of_last_packet = max(time_of_last_received_packet_,
1893 time_of_last_sent_new_packet_); 1907 time_of_last_sent_new_packet_);
1894 1908
1909 // If no packets have been sent or received, then don't timeout.
1910 if (FLAGS_quic_timeouts_require_activity &&
1911 !time_of_last_packet.IsInitialized()) {
1912 timeout_alarm_->Cancel();
1913 timeout_alarm_->Set(now.Add(idle_network_timeout_));
1914 return false;
1915 }
1916
1895 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| 1917 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet|
1896 // is accurate time. However, this should not change the behavior of 1918 // is accurate time. However, this should not change the behavior of
1897 // timeout handling. 1919 // timeout handling.
1898 QuicTime::Delta delta = now.Subtract(time_of_last_packet); 1920 QuicTime::Delta delta = now.Subtract(time_of_last_packet);
1899 DVLOG(1) << ENDPOINT << "last packet " 1921 DVLOG(1) << ENDPOINT << "last packet "
1900 << time_of_last_packet.ToDebuggingValue() 1922 << time_of_last_packet.ToDebuggingValue()
1901 << " now:" << now.ToDebuggingValue() 1923 << " now:" << now.ToDebuggingValue()
1902 << " delta:" << delta.ToMicroseconds() 1924 << " delta:" << delta.ToMicroseconds()
1903 << " network_timeout: " << idle_network_timeout_.ToMicroseconds(); 1925 << " network_timeout: " << idle_network_timeout_.ToMicroseconds();
1904 if (delta >= idle_network_timeout_) { 1926 if (delta >= idle_network_timeout_) {
1905 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; 1927 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity.";
1906 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); 1928 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT);
1907 return true; 1929 return true;
1908 } 1930 }
1909 1931
1910 // Next timeout delta. 1932 // Next timeout delta.
1911 QuicTime::Delta timeout = idle_network_timeout_.Subtract(delta); 1933 QuicTime::Delta timeout = idle_network_timeout_.Subtract(delta);
1912 1934
1913 if (!overall_connection_timeout_.IsInfinite()) { 1935 if (!overall_connection_timeout_.IsInfinite()) {
1914 QuicTime::Delta connected_time = 1936 QuicTime::Delta connected_time =
1915 now.Subtract(stats_.connection_creation_time); 1937 now.Subtract(stats_.connection_creation_time);
1916 DVLOG(1) << ENDPOINT << "connection time: " 1938 DVLOG(1) << ENDPOINT << "connection time: "
1917 << connected_time.ToMilliseconds() << " overall timeout: " 1939 << connected_time.ToMilliseconds() << " overall timeout: "
1918 << overall_connection_timeout_.ToMilliseconds(); 1940 << overall_connection_timeout_.ToMilliseconds();
1919 if (connected_time >= overall_connection_timeout_) { 1941 if (connected_time >= overall_connection_timeout_) {
1920 DVLOG(1) << ENDPOINT << 1942 DVLOG(1) << ENDPOINT <<
1921 "Connection timedout due to overall connection timeout."; 1943 "Connection timedout due to overall connection timeout.";
1922 SendConnectionClose(QUIC_CONNECTION_TIMED_OUT); 1944 SendConnectionClose(QUIC_CONNECTION_OVERALL_TIMED_OUT);
1923 return true; 1945 return true;
1924 } 1946 }
1925 1947
1926 // Take the min timeout. 1948 // Take the min timeout.
1927 QuicTime::Delta connection_timeout = 1949 QuicTime::Delta connection_timeout =
1928 overall_connection_timeout_.Subtract(connected_time); 1950 overall_connection_timeout_.Subtract(connected_time);
1929 if (connection_timeout < timeout) { 1951 if (connection_timeout < timeout) {
1930 timeout = connection_timeout; 1952 timeout = connection_timeout;
1931 } 1953 }
1932 } 1954 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
2010 } 2032 }
2011 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { 2033 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) {
2012 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { 2034 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) {
2013 return true; 2035 return true;
2014 } 2036 }
2015 } 2037 }
2016 return false; 2038 return false;
2017 } 2039 }
2018 2040
2019 } // namespace net 2041 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_connection.h ('k') | net/quic/quic_connection_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698