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 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 visitor_(nullptr), | 297 visitor_(nullptr), |
298 debug_visitor_(nullptr), | 298 debug_visitor_(nullptr), |
299 packet_generator_(connection_id_, | 299 packet_generator_(connection_id_, |
300 &framer_, | 300 &framer_, |
301 random_generator_, | 301 random_generator_, |
302 helper->GetBufferAllocator(), | 302 helper->GetBufferAllocator(), |
303 this), | 303 this), |
304 fec_alarm_(helper->CreateAlarm(arena_.New<FecAlarm>(&packet_generator_), | 304 fec_alarm_(helper->CreateAlarm(arena_.New<FecAlarm>(&packet_generator_), |
305 &arena_)), | 305 &arena_)), |
306 idle_network_timeout_(QuicTime::Delta::Infinite()), | 306 idle_network_timeout_(QuicTime::Delta::Infinite()), |
307 overall_connection_timeout_(QuicTime::Delta::Infinite()), | 307 handshake_timeout_(QuicTime::Delta::Infinite()), |
308 time_of_last_received_packet_(clock_->ApproximateNow()), | 308 time_of_last_received_packet_(clock_->ApproximateNow()), |
309 time_of_last_sent_new_packet_(clock_->ApproximateNow()), | 309 time_of_last_sent_new_packet_(clock_->ApproximateNow()), |
310 last_send_for_timeout_(clock_->ApproximateNow()), | 310 last_send_for_timeout_(clock_->ApproximateNow()), |
311 packet_number_of_last_sent_packet_(0), | 311 packet_number_of_last_sent_packet_(0), |
312 sent_packet_manager_( | 312 sent_packet_manager_( |
313 perspective, | 313 perspective, |
314 kDefaultPathId, | 314 kDefaultPathId, |
315 clock_, | 315 clock_, |
316 &stats_, | 316 &stats_, |
317 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic, | 317 FLAGS_quic_use_bbr_congestion_control ? kBBR : kCubic, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 if (termination_packets_.get() != nullptr) { | 355 if (termination_packets_.get() != nullptr) { |
356 STLDeleteElements(termination_packets_.get()); | 356 STLDeleteElements(termination_packets_.get()); |
357 } | 357 } |
358 STLDeleteValues(&group_map_); | 358 STLDeleteValues(&group_map_); |
359 ClearQueuedPackets(); | 359 ClearQueuedPackets(); |
360 } | 360 } |
361 | 361 |
362 void QuicConnection::ClearQueuedPackets() { | 362 void QuicConnection::ClearQueuedPackets() { |
363 for (QueuedPacketList::iterator it = queued_packets_.begin(); | 363 for (QueuedPacketList::iterator it = queued_packets_.begin(); |
364 it != queued_packets_.end(); ++it) { | 364 it != queued_packets_.end(); ++it) { |
| 365 // Delete the buffer before calling ClearSerializedPacket, which sets |
| 366 // encrypted_buffer to nullptr. |
| 367 delete[] it->encrypted_buffer; |
365 QuicUtils::ClearSerializedPacket(&(*it)); | 368 QuicUtils::ClearSerializedPacket(&(*it)); |
366 } | 369 } |
367 queued_packets_.clear(); | 370 queued_packets_.clear(); |
368 } | 371 } |
369 | 372 |
370 void QuicConnection::SetFromConfig(const QuicConfig& config) { | 373 void QuicConnection::SetFromConfig(const QuicConfig& config) { |
371 if (config.negotiated()) { | 374 if (config.negotiated()) { |
| 375 // Handshake complete, set handshake timeout to Infinite. |
372 SetNetworkTimeouts(QuicTime::Delta::Infinite(), | 376 SetNetworkTimeouts(QuicTime::Delta::Infinite(), |
373 config.IdleConnectionStateLifetime()); | 377 config.IdleConnectionStateLifetime()); |
374 if (config.SilentClose()) { | 378 if (config.SilentClose()) { |
375 silent_close_enabled_ = true; | 379 silent_close_enabled_ = true; |
376 } | 380 } |
377 if (FLAGS_quic_enable_multipath && config.MultipathEnabled()) { | 381 if (FLAGS_quic_enable_multipath && config.MultipathEnabled()) { |
378 multipath_enabled_ = true; | 382 multipath_enabled_ = true; |
379 } | 383 } |
380 } else { | 384 } else { |
381 SetNetworkTimeouts(config.max_time_before_crypto_handshake(), | 385 SetNetworkTimeouts(config.max_time_before_crypto_handshake(), |
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1199 &packet_iterator->retransmittable_frames; | 1203 &packet_iterator->retransmittable_frames; |
1200 if (retransmittable_frames->empty()) { | 1204 if (retransmittable_frames->empty()) { |
1201 ++packet_iterator; | 1205 ++packet_iterator; |
1202 continue; | 1206 continue; |
1203 } | 1207 } |
1204 QuicUtils::RemoveFramesForStream(retransmittable_frames, id); | 1208 QuicUtils::RemoveFramesForStream(retransmittable_frames, id); |
1205 if (!retransmittable_frames->empty()) { | 1209 if (!retransmittable_frames->empty()) { |
1206 ++packet_iterator; | 1210 ++packet_iterator; |
1207 continue; | 1211 continue; |
1208 } | 1212 } |
| 1213 delete[] packet_iterator->encrypted_buffer; |
1209 QuicUtils::ClearSerializedPacket(&(*packet_iterator)); | 1214 QuicUtils::ClearSerializedPacket(&(*packet_iterator)); |
1210 packet_iterator = queued_packets_.erase(packet_iterator); | 1215 packet_iterator = queued_packets_.erase(packet_iterator); |
1211 } | 1216 } |
1212 } | 1217 } |
1213 | 1218 |
1214 void QuicConnection::SendWindowUpdate(QuicStreamId id, | 1219 void QuicConnection::SendWindowUpdate(QuicStreamId id, |
1215 QuicStreamOffset byte_offset) { | 1220 QuicStreamOffset byte_offset) { |
1216 // Opportunistically bundle an ack with this outgoing packet. | 1221 // Opportunistically bundle an ack with this outgoing packet. |
1217 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); | 1222 ScopedPacketBundler ack_bundler(this, BUNDLE_PENDING_ACK); |
1218 packet_generator_.AddControlFrame( | 1223 packet_generator_.AddControlFrame( |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1463 void QuicConnection::WriteQueuedPackets() { | 1468 void QuicConnection::WriteQueuedPackets() { |
1464 DCHECK(!writer_->IsWriteBlocked()); | 1469 DCHECK(!writer_->IsWriteBlocked()); |
1465 | 1470 |
1466 if (pending_version_negotiation_packet_) { | 1471 if (pending_version_negotiation_packet_) { |
1467 SendVersionNegotiationPacket(); | 1472 SendVersionNegotiationPacket(); |
1468 } | 1473 } |
1469 | 1474 |
1470 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); | 1475 QueuedPacketList::iterator packet_iterator = queued_packets_.begin(); |
1471 while (packet_iterator != queued_packets_.end() && | 1476 while (packet_iterator != queued_packets_.end() && |
1472 WritePacket(&(*packet_iterator))) { | 1477 WritePacket(&(*packet_iterator))) { |
| 1478 delete[] packet_iterator->encrypted_buffer; |
| 1479 QuicUtils::ClearSerializedPacket(&(*packet_iterator)); |
1473 packet_iterator = queued_packets_.erase(packet_iterator); | 1480 packet_iterator = queued_packets_.erase(packet_iterator); |
1474 } | 1481 } |
1475 } | 1482 } |
1476 | 1483 |
1477 void QuicConnection::WritePendingRetransmissions() { | 1484 void QuicConnection::WritePendingRetransmissions() { |
1478 // Keep writing as long as there's a pending retransmission which can be | 1485 // Keep writing as long as there's a pending retransmission which can be |
1479 // written. | 1486 // written. |
1480 while (sent_packet_manager_.HasPendingRetransmissions()) { | 1487 while (sent_packet_manager_.HasPendingRetransmissions()) { |
1481 const PendingRetransmission pending = | 1488 const PendingRetransmission pending = |
1482 sent_packet_manager_.NextPendingRetransmission(); | 1489 sent_packet_manager_.NextPendingRetransmission(); |
1483 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { | 1490 if (!CanWrite(HAS_RETRANSMITTABLE_DATA)) { |
1484 break; | 1491 break; |
1485 } | 1492 } |
1486 | 1493 |
1487 // Re-packetize the frames with a new packet number for retransmission. | 1494 // Re-packetize the frames with a new packet number for retransmission. |
1488 // Retransmitted data packets do not use FEC, even when it's enabled. | 1495 // Retransmitted data packets do not use FEC, even when it's enabled. |
1489 // Retransmitted packets use the same packet number length as the | 1496 // Retransmitted packets use the same packet number length as the |
1490 // original. | 1497 // original. |
1491 // Flush the packet generator before making a new packet. | 1498 // Flush the packet generator before making a new packet. |
1492 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that | 1499 // TODO(ianswett): Implement ReserializeAllFrames as a separate path that |
1493 // does not require the creator to be flushed. | 1500 // does not require the creator to be flushed. |
1494 packet_generator_.FlushAllQueuedFrames(); | 1501 packet_generator_.FlushAllQueuedFrames(); |
1495 char buffer[kMaxPacketSize]; | 1502 char buffer[kMaxPacketSize]; |
1496 SerializedPacket serialized_packet = | 1503 SerializedPacket serialized_packet = |
1497 packet_generator_.ReserializeAllFrames(pending, buffer, kMaxPacketSize); | 1504 packet_generator_.ReserializeAllFrames(pending, buffer, kMaxPacketSize); |
1498 if (FLAGS_quic_retransmit_via_onserializedpacket) { | 1505 if (FLAGS_quic_retransmit_via_onserializedpacket) { |
1499 DCHECK(serialized_packet.packet == nullptr); | 1506 DCHECK(serialized_packet.encrypted_buffer == nullptr); |
1500 continue; | 1507 continue; |
1501 } | 1508 } |
1502 if (serialized_packet.packet == nullptr) { | 1509 if (serialized_packet.encrypted_buffer == nullptr) { |
1503 // We failed to serialize the packet, so close the connection. | 1510 // We failed to serialize the packet, so close the connection. |
1504 // CloseConnection does not send close packet, so no infinite loop here. | 1511 // CloseConnection does not send close packet, so no infinite loop here. |
1505 // TODO(ianswett): This is actually an internal error, not an encryption | 1512 // TODO(ianswett): This is actually an internal error, not an encryption |
1506 // failure. | 1513 // failure. |
1507 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | 1514 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); |
1508 return; | 1515 return; |
1509 } | 1516 } |
1510 | 1517 |
1511 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.packet_number << " as " | 1518 DVLOG(1) << ENDPOINT << "Retransmitting " << pending.packet_number << " as " |
1512 << serialized_packet.packet_number; | 1519 << serialized_packet.packet_number; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1579 << "ms"; | 1586 << "ms"; |
1580 return false; | 1587 return false; |
1581 } | 1588 } |
1582 if (!FLAGS_quic_respect_send_alarm2) { | 1589 if (!FLAGS_quic_respect_send_alarm2) { |
1583 send_alarm_->Cancel(); | 1590 send_alarm_->Cancel(); |
1584 } | 1591 } |
1585 return true; | 1592 return true; |
1586 } | 1593 } |
1587 | 1594 |
1588 bool QuicConnection::WritePacket(SerializedPacket* packet) { | 1595 bool QuicConnection::WritePacket(SerializedPacket* packet) { |
1589 if (!WritePacketInner(packet)) { | |
1590 return false; | |
1591 } | |
1592 QuicUtils::ClearSerializedPacket(packet); | |
1593 return true; | |
1594 } | |
1595 | |
1596 bool QuicConnection::WritePacketInner(SerializedPacket* packet) { | |
1597 if (packet->packet_number < sent_packet_manager_.largest_sent_packet()) { | 1596 if (packet->packet_number < sent_packet_manager_.largest_sent_packet()) { |
1598 QUIC_BUG << "Attempt to write packet:" << packet->packet_number | 1597 QUIC_BUG << "Attempt to write packet:" << packet->packet_number |
1599 << " after:" << sent_packet_manager_.largest_sent_packet(); | 1598 << " after:" << sent_packet_manager_.largest_sent_packet(); |
1600 SendConnectionCloseWithDetails(QUIC_INTERNAL_ERROR, | 1599 SendConnectionCloseWithDetails(QUIC_INTERNAL_ERROR, |
1601 "Packet written out of order."); | 1600 "Packet written out of order."); |
1602 return true; | 1601 return true; |
1603 } | 1602 } |
1604 if (ShouldDiscardPacket(*packet)) { | 1603 if (ShouldDiscardPacket(*packet)) { |
1605 ++stats_.packets_discarded; | 1604 ++stats_.packets_discarded; |
1606 return true; | 1605 return true; |
1607 } | 1606 } |
1608 // Termination packets are encrypted and saved, so don't exit early. | 1607 // Termination packets are encrypted and saved, so don't exit early. |
1609 const bool is_termination_packet = IsTerminationPacket(*packet); | 1608 const bool is_termination_packet = IsTerminationPacket(*packet); |
1610 if (writer_->IsWriteBlocked() && !is_termination_packet) { | 1609 if (writer_->IsWriteBlocked() && !is_termination_packet) { |
1611 return false; | 1610 return false; |
1612 } | 1611 } |
1613 | 1612 |
1614 QuicPacketNumber packet_number = packet->packet_number; | 1613 QuicPacketNumber packet_number = packet->packet_number; |
1615 DCHECK_LE(packet_number_of_last_sent_packet_, packet_number); | 1614 DCHECK_LE(packet_number_of_last_sent_packet_, packet_number); |
1616 packet_number_of_last_sent_packet_ = packet_number; | 1615 packet_number_of_last_sent_packet_ = packet_number; |
1617 | 1616 |
1618 QuicEncryptedPacket* encrypted = packet->packet; | 1617 QuicPacketLength encrypted_length = packet->encrypted_length; |
1619 // Termination packets are eventually owned by TimeWaitListManager. | 1618 // Termination packets are eventually owned by TimeWaitListManager. |
1620 // Others are deleted at the end of this call. | 1619 // Others are deleted at the end of this call. |
1621 if (is_termination_packet) { | 1620 if (is_termination_packet) { |
1622 if (termination_packets_.get() == nullptr) { | 1621 if (termination_packets_.get() == nullptr) { |
1623 termination_packets_.reset(new std::vector<QuicEncryptedPacket*>); | 1622 termination_packets_.reset(new std::vector<QuicEncryptedPacket*>); |
1624 } | 1623 } |
1625 // Clone the packet so it's owned in the future. | 1624 // Copy the buffer so it's owned in the future. |
1626 termination_packets_->push_back(encrypted->Clone()); | 1625 char* buffer_copy = QuicUtils::CopyBuffer(*packet); |
| 1626 termination_packets_->push_back( |
| 1627 new QuicEncryptedPacket(buffer_copy, encrypted_length, true)); |
1627 // This assures we won't try to write *forced* packets when blocked. | 1628 // This assures we won't try to write *forced* packets when blocked. |
1628 // Return true to stop processing. | 1629 // Return true to stop processing. |
1629 if (writer_->IsWriteBlocked()) { | 1630 if (writer_->IsWriteBlocked()) { |
1630 visitor_->OnWriteBlocked(); | 1631 visitor_->OnWriteBlocked(); |
1631 return true; | 1632 return true; |
1632 } | 1633 } |
1633 } | 1634 } |
1634 | 1635 |
1635 DCHECK_LE(encrypted->length(), kMaxPacketSize); | 1636 DCHECK_LE(encrypted_length, kMaxPacketSize); |
1636 DCHECK_LE(encrypted->length(), packet_generator_.GetMaxPacketLength()); | 1637 DCHECK_LE(encrypted_length, packet_generator_.GetMaxPacketLength()); |
1637 DVLOG(1) << ENDPOINT << "Sending packet " << packet_number << " : " | 1638 DVLOG(1) << ENDPOINT << "Sending packet " << packet_number << " : " |
1638 << (packet->is_fec_packet | 1639 << (packet->is_fec_packet |
1639 ? "FEC " | 1640 ? "FEC " |
1640 : (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA | 1641 : (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA |
1641 ? "data bearing " | 1642 ? "data bearing " |
1642 : " ack only ")) | 1643 : " ack only ")) |
1643 << ", encryption level: " | 1644 << ", encryption level: " |
1644 << QuicUtils::EncryptionLevelToString(packet->encryption_level) | 1645 << QuicUtils::EncryptionLevelToString(packet->encryption_level) |
1645 << ", encrypted length:" << encrypted->length(); | 1646 << ", encrypted length:" << encrypted_length; |
1646 DVLOG(2) << ENDPOINT << "packet(" << packet_number << "): " << std::endl | 1647 DVLOG(2) << ENDPOINT << "packet(" << packet_number << "): " << std::endl |
1647 << QuicUtils::StringToHexASCIIDump(encrypted->AsStringPiece()); | 1648 << QuicUtils::StringToHexASCIIDump( |
| 1649 StringPiece(packet->encrypted_buffer, encrypted_length)); |
1648 | 1650 |
1649 // Measure the RTT from before the write begins to avoid underestimating the | 1651 // Measure the RTT from before the write begins to avoid underestimating the |
1650 // min_rtt_, especially in cases where the thread blocks or gets swapped out | 1652 // min_rtt_, especially in cases where the thread blocks or gets swapped out |
1651 // during the WritePacket below. | 1653 // during the WritePacket below. |
1652 QuicTime packet_send_time = clock_->Now(); | 1654 QuicTime packet_send_time = clock_->Now(); |
1653 WriteResult result = writer_->WritePacket( | 1655 WriteResult result = writer_->WritePacket( |
1654 encrypted->data(), encrypted->length(), self_address().address().bytes(), | 1656 packet->encrypted_buffer, encrypted_length, |
1655 peer_address(), per_packet_options_); | 1657 self_address().address().bytes(), peer_address(), per_packet_options_); |
1656 if (result.error_code == ERR_IO_PENDING) { | 1658 if (result.error_code == ERR_IO_PENDING) { |
1657 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); | 1659 DCHECK_EQ(WRITE_STATUS_BLOCKED, result.status); |
1658 } | 1660 } |
1659 | 1661 |
1660 if (result.status == WRITE_STATUS_BLOCKED) { | 1662 if (result.status == WRITE_STATUS_BLOCKED) { |
1661 visitor_->OnWriteBlocked(); | 1663 visitor_->OnWriteBlocked(); |
1662 // If the socket buffers the the data, then the packet should not | 1664 // If the socket buffers the the data, then the packet should not |
1663 // be queued and sent again, which would result in an unnecessary | 1665 // be queued and sent again, which would result in an unnecessary |
1664 // duplicate packet being sent. The helper must call OnCanWrite | 1666 // duplicate packet being sent. The helper must call OnCanWrite |
1665 // when the write completes, and OnWriteError if an error occurs. | 1667 // when the write completes, and OnWriteError if an error occurs. |
1666 if (!writer_->IsWriteBlockedDataBuffered()) { | 1668 if (!writer_->IsWriteBlockedDataBuffered()) { |
1667 return false; | 1669 return false; |
1668 } | 1670 } |
1669 } | 1671 } |
1670 if (result.status != WRITE_STATUS_ERROR && debug_visitor_ != nullptr) { | 1672 if (result.status != WRITE_STATUS_ERROR && debug_visitor_ != nullptr) { |
1671 // Pass the write result to the visitor. | 1673 // Pass the write result to the visitor. |
1672 debug_visitor_->OnPacketSent(*packet, packet->original_packet_number, | 1674 debug_visitor_->OnPacketSent(*packet, packet->original_packet_number, |
1673 packet->transmission_type, encrypted->length(), | 1675 packet->transmission_type, encrypted_length, |
1674 packet_send_time); | 1676 packet_send_time); |
1675 } | 1677 } |
1676 if (packet->transmission_type == NOT_RETRANSMISSION) { | 1678 if (packet->transmission_type == NOT_RETRANSMISSION) { |
1677 time_of_last_sent_new_packet_ = packet_send_time; | 1679 time_of_last_sent_new_packet_ = packet_send_time; |
1678 if (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA && | 1680 if (IsRetransmittable(*packet) == HAS_RETRANSMITTABLE_DATA && |
1679 last_send_for_timeout_ <= time_of_last_received_packet_) { | 1681 last_send_for_timeout_ <= time_of_last_received_packet_) { |
1680 last_send_for_timeout_ = packet_send_time; | 1682 last_send_for_timeout_ = packet_send_time; |
1681 } | 1683 } |
1682 } | 1684 } |
1683 SetPingAlarm(); | 1685 SetPingAlarm(); |
1684 MaybeSetFecAlarm(packet_number); | 1686 MaybeSetFecAlarm(packet_number); |
1685 MaybeSetMtuAlarm(); | 1687 MaybeSetMtuAlarm(); |
1686 DVLOG(1) << ENDPOINT << "time we began writing last sent packet: " | 1688 DVLOG(1) << ENDPOINT << "time we began writing last sent packet: " |
1687 << packet_send_time.ToDebuggingValue(); | 1689 << packet_send_time.ToDebuggingValue(); |
1688 | 1690 |
1689 // TODO(ianswett): Change the packet number length and other packet creator | 1691 // TODO(ianswett): Change the packet number length and other packet creator |
1690 // options by a more explicit API than setting a struct value directly, | 1692 // options by a more explicit API than setting a struct value directly, |
1691 // perhaps via the NetworkChangeVisitor. | 1693 // perhaps via the NetworkChangeVisitor. |
1692 packet_generator_.UpdateSequenceNumberLength( | 1694 packet_generator_.UpdateSequenceNumberLength( |
1693 sent_packet_manager_.least_packet_awaited_by_peer(), | 1695 sent_packet_manager_.least_packet_awaited_by_peer(), |
1694 sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); | 1696 sent_packet_manager_.EstimateMaxPacketsInFlight(max_packet_length())); |
1695 | 1697 |
1696 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( | 1698 bool reset_retransmission_alarm = sent_packet_manager_.OnPacketSent( |
1697 packet, packet->original_packet_number, packet_send_time, | 1699 packet, packet->original_packet_number, packet_send_time, |
1698 encrypted->length(), packet->transmission_type, | 1700 encrypted_length, packet->transmission_type, IsRetransmittable(*packet)); |
1699 IsRetransmittable(*packet)); | |
1700 | 1701 |
1701 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { | 1702 if (reset_retransmission_alarm || !retransmission_alarm_->IsSet()) { |
1702 SetRetransmissionAlarm(); | 1703 SetRetransmissionAlarm(); |
1703 } | 1704 } |
1704 | 1705 |
1705 stats_.bytes_sent += result.bytes_written; | 1706 stats_.bytes_sent += result.bytes_written; |
1706 ++stats_.packets_sent; | 1707 ++stats_.packets_sent; |
1707 if (packet->transmission_type != NOT_RETRANSMISSION) { | 1708 if (packet->transmission_type != NOT_RETRANSMISSION) { |
1708 stats_.bytes_retransmitted += result.bytes_written; | 1709 stats_.bytes_retransmitted += result.bytes_written; |
1709 ++stats_.packets_retransmitted; | 1710 ++stats_.packets_retransmitted; |
1710 } | 1711 } |
1711 | 1712 |
1712 if (result.status == WRITE_STATUS_ERROR) { | 1713 if (result.status == WRITE_STATUS_ERROR) { |
1713 OnWriteError(result.error_code); | 1714 OnWriteError(result.error_code); |
1714 DLOG(ERROR) << ENDPOINT << "failed writing " << encrypted->length() | 1715 DLOG(ERROR) << ENDPOINT << "failed writing " << encrypted_length |
1715 << " bytes " | 1716 << " bytes " |
1716 << " from host " << (self_address().address().empty() | 1717 << " from host " << (self_address().address().empty() |
1717 ? " empty address " | 1718 ? " empty address " |
1718 : self_address().ToStringWithoutPort()) | 1719 : self_address().ToStringWithoutPort()) |
1719 << " to address " << peer_address().ToString(); | 1720 << " to address " << peer_address().ToString(); |
1720 return false; | 1721 return false; |
1721 } | 1722 } |
1722 | 1723 |
1723 return true; | 1724 return true; |
1724 } | 1725 } |
(...skipping 30 matching lines...) Expand all Loading... |
1755 | 1756 |
1756 void QuicConnection::OnWriteError(int error_code) { | 1757 void QuicConnection::OnWriteError(int error_code) { |
1757 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code << " (" | 1758 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code << " (" |
1758 << ErrorToString(error_code) << ")"; | 1759 << ErrorToString(error_code) << ")"; |
1759 // We can't send an error as the socket is presumably borked. | 1760 // We can't send an error as the socket is presumably borked. |
1760 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 1761 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
1761 } | 1762 } |
1762 | 1763 |
1763 void QuicConnection::OnSerializedPacket(SerializedPacket* serialized_packet) { | 1764 void QuicConnection::OnSerializedPacket(SerializedPacket* serialized_packet) { |
1764 DCHECK_NE(kInvalidPathId, serialized_packet->path_id); | 1765 DCHECK_NE(kInvalidPathId, serialized_packet->path_id); |
1765 if (serialized_packet->packet == nullptr) { | 1766 if (serialized_packet->encrypted_buffer == nullptr) { |
1766 // We failed to serialize the packet, so close the connection. | 1767 // We failed to serialize the packet, so close the connection. |
1767 // CloseConnection does not send close packet, so no infinite loop here. | 1768 // CloseConnection does not send close packet, so no infinite loop here. |
1768 // TODO(ianswett): This is actually an internal error, not an encryption | 1769 // TODO(ianswett): This is actually an internal error, not an encryption |
1769 // failure. | 1770 // failure. |
1770 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); | 1771 CloseConnection(QUIC_ENCRYPTION_FAILURE, false); |
1771 return; | 1772 return; |
1772 } | 1773 } |
1773 if (serialized_packet->is_fec_packet && fec_alarm_->IsSet()) { | 1774 if (serialized_packet->is_fec_packet && fec_alarm_->IsSet()) { |
1774 // If an FEC packet is serialized with the FEC alarm set, cancel the alarm. | 1775 // If an FEC packet is serialized with the FEC alarm set, cancel the alarm. |
1775 fec_alarm_->Cancel(); | 1776 fec_alarm_->Cancel(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1809 // complete with the server. | 1810 // complete with the server. |
1810 if (perspective_ == Perspective::IS_CLIENT && !ack_queued_ && | 1811 if (perspective_ == Perspective::IS_CLIENT && !ack_queued_ && |
1811 ack_frame_updated()) { | 1812 ack_frame_updated()) { |
1812 ack_alarm_->Cancel(); | 1813 ack_alarm_->Cancel(); |
1813 ack_alarm_->Set(clock_->ApproximateNow()); | 1814 ack_alarm_->Set(clock_->ApproximateNow()); |
1814 } | 1815 } |
1815 } | 1816 } |
1816 | 1817 |
1817 void QuicConnection::SendOrQueuePacket(SerializedPacket* packet) { | 1818 void QuicConnection::SendOrQueuePacket(SerializedPacket* packet) { |
1818 // The caller of this function is responsible for checking CanWrite(). | 1819 // The caller of this function is responsible for checking CanWrite(). |
1819 if (packet->packet == nullptr) { | 1820 if (packet->encrypted_buffer == nullptr) { |
1820 QUIC_BUG << "packet.packet == nullptr in to SendOrQueuePacket"; | 1821 QUIC_BUG << "packet.encrypted_buffer == nullptr in to SendOrQueuePacket"; |
1821 return; | 1822 return; |
1822 } | 1823 } |
1823 | 1824 |
1824 sent_entropy_manager_.RecordPacketEntropyHash(packet->packet_number, | 1825 sent_entropy_manager_.RecordPacketEntropyHash(packet->packet_number, |
1825 packet->entropy_hash); | 1826 packet->entropy_hash); |
1826 // If there are already queued packets, queue this one immediately to ensure | 1827 // If there are already queued packets, queue this one immediately to ensure |
1827 // it's written in sequence number order. | 1828 // it's written in sequence number order. |
1828 if (!queued_packets_.empty() || !WritePacket(packet)) { | 1829 if (!queued_packets_.empty() || !WritePacket(packet)) { |
1829 // Take ownership of the underlying encrypted packet. | 1830 // Take ownership of the underlying encrypted packet. |
1830 if (!packet->packet->owns_buffer()) { | 1831 packet->encrypted_buffer = QuicUtils::CopyBuffer(*packet); |
1831 scoped_ptr<QuicEncryptedPacket> encrypted_deleter(packet->packet); | |
1832 packet->packet = packet->packet->Clone(); | |
1833 } | |
1834 queued_packets_.push_back(*packet); | 1832 queued_packets_.push_back(*packet); |
1835 packet->retransmittable_frames.clear(); | 1833 packet->retransmittable_frames.clear(); |
1836 } | 1834 } |
1837 | 1835 |
| 1836 QuicUtils::ClearSerializedPacket(packet); |
1838 // If a forward-secure encrypter is available but is not being used and the | 1837 // If a forward-secure encrypter is available but is not being used and the |
1839 // next packet number is the first packet which requires | 1838 // next packet number is the first packet which requires |
1840 // forward security, start using the forward-secure encrypter. | 1839 // forward security, start using the forward-secure encrypter. |
1841 if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && | 1840 if (encryption_level_ != ENCRYPTION_FORWARD_SECURE && |
1842 has_forward_secure_encrypter_ && | 1841 has_forward_secure_encrypter_ && |
1843 packet->packet_number >= first_required_forward_secure_packet_ - 1) { | 1842 packet->packet_number >= first_required_forward_secure_packet_ - 1) { |
1844 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); | 1843 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
1845 } | 1844 } |
1846 } | 1845 } |
1847 | 1846 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2048 // serialized packet. | 2047 // serialized packet. |
2049 SendConnectionClosePacket(error, details); | 2048 SendConnectionClosePacket(error, details); |
2050 CloseConnection(error, false); | 2049 CloseConnection(error, false); |
2051 } | 2050 } |
2052 | 2051 |
2053 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, | 2052 void QuicConnection::SendConnectionClosePacket(QuicErrorCode error, |
2054 const string& details) { | 2053 const string& details) { |
2055 DVLOG(1) << ENDPOINT << "Force closing " << connection_id() << " with error " | 2054 DVLOG(1) << ENDPOINT << "Force closing " << connection_id() << " with error " |
2056 << QuicUtils::ErrorToString(error) << " (" << error << ") " | 2055 << QuicUtils::ErrorToString(error) << " (" << error << ") " |
2057 << details; | 2056 << details; |
2058 // Don't send explicit connection close packets for timeouts. | |
2059 // This is particularly important on mobile, where connections are short. | |
2060 if (silent_close_enabled_ && | |
2061 error == QuicErrorCode::QUIC_CONNECTION_TIMED_OUT) { | |
2062 return; | |
2063 } | |
2064 ClearQueuedPackets(); | 2057 ClearQueuedPackets(); |
2065 ScopedPacketBundler ack_bundler(this, SEND_ACK); | 2058 ScopedPacketBundler ack_bundler(this, SEND_ACK); |
2066 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); | 2059 QuicConnectionCloseFrame* frame = new QuicConnectionCloseFrame(); |
2067 frame->error_code = error; | 2060 frame->error_code = error; |
2068 frame->error_details = details; | 2061 frame->error_details = details; |
2069 packet_generator_.AddControlFrame(QuicFrame(frame)); | 2062 packet_generator_.AddControlFrame(QuicFrame(frame)); |
2070 packet_generator_.FlushAllQueuedFrames(); | 2063 packet_generator_.FlushAllQueuedFrames(); |
2071 } | 2064 } |
2072 | 2065 |
2073 void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) { | 2066 void QuicConnection::CloseConnection(QuicErrorCode error, bool from_peer) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2164 | 2157 |
2165 IsHandshake pending_handshake = | 2158 IsHandshake pending_handshake = |
2166 visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE; | 2159 visitor_->HasPendingHandshake() ? IS_HANDSHAKE : NOT_HANDSHAKE; |
2167 // Sending queued packets may have caused the socket to become write blocked, | 2160 // Sending queued packets may have caused the socket to become write blocked, |
2168 // or the congestion manager to prohibit sending. If we've sent everything | 2161 // or the congestion manager to prohibit sending. If we've sent everything |
2169 // we had queued and we're still not blocked, let the visitor know it can | 2162 // we had queued and we're still not blocked, let the visitor know it can |
2170 // write more. | 2163 // write more. |
2171 return ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, pending_handshake); | 2164 return ShouldGeneratePacket(HAS_RETRANSMITTABLE_DATA, pending_handshake); |
2172 } | 2165 } |
2173 | 2166 |
2174 void QuicConnection::SetNetworkTimeouts(QuicTime::Delta overall_timeout, | 2167 void QuicConnection::SetNetworkTimeouts(QuicTime::Delta handshake_timeout, |
2175 QuicTime::Delta idle_timeout) { | 2168 QuicTime::Delta idle_timeout) { |
2176 QUIC_BUG_IF(idle_timeout > overall_timeout) | 2169 QUIC_BUG_IF(idle_timeout > handshake_timeout) |
2177 << "idle_timeout:" << idle_timeout.ToMilliseconds() | 2170 << "idle_timeout:" << idle_timeout.ToMilliseconds() |
2178 << " overall_timeout:" << overall_timeout.ToMilliseconds(); | 2171 << " handshake_timeout:" << handshake_timeout.ToMilliseconds(); |
2179 // Adjust the idle timeout on client and server to prevent clients from | 2172 // Adjust the idle timeout on client and server to prevent clients from |
2180 // sending requests to servers which have already closed the connection. | 2173 // sending requests to servers which have already closed the connection. |
2181 if (perspective_ == Perspective::IS_SERVER) { | 2174 if (perspective_ == Perspective::IS_SERVER) { |
2182 idle_timeout = idle_timeout.Add(QuicTime::Delta::FromSeconds(3)); | 2175 idle_timeout = idle_timeout.Add(QuicTime::Delta::FromSeconds(3)); |
2183 } else if (idle_timeout > QuicTime::Delta::FromSeconds(1)) { | 2176 } else if (idle_timeout > QuicTime::Delta::FromSeconds(1)) { |
2184 idle_timeout = idle_timeout.Subtract(QuicTime::Delta::FromSeconds(1)); | 2177 idle_timeout = idle_timeout.Subtract(QuicTime::Delta::FromSeconds(1)); |
2185 } | 2178 } |
2186 overall_connection_timeout_ = overall_timeout; | 2179 handshake_timeout_ = handshake_timeout; |
2187 idle_network_timeout_ = idle_timeout; | 2180 idle_network_timeout_ = idle_timeout; |
2188 | 2181 |
2189 SetTimeoutAlarm(); | 2182 SetTimeoutAlarm(); |
2190 } | 2183 } |
2191 | 2184 |
2192 void QuicConnection::CheckForTimeout() { | 2185 void QuicConnection::CheckForTimeout() { |
2193 QuicTime now = clock_->ApproximateNow(); | 2186 QuicTime now = clock_->ApproximateNow(); |
2194 QuicTime time_of_last_packet = QuicTime::Zero(); | 2187 QuicTime time_of_last_packet = QuicTime::Zero(); |
2195 if (!FLAGS_quic_use_new_idle_timeout) { | 2188 if (!FLAGS_quic_use_new_idle_timeout) { |
2196 time_of_last_packet = | 2189 time_of_last_packet = |
2197 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); | 2190 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); |
2198 } else { | 2191 } else { |
2199 time_of_last_packet = | 2192 time_of_last_packet = |
2200 max(time_of_last_received_packet_, last_send_for_timeout_); | 2193 max(time_of_last_received_packet_, last_send_for_timeout_); |
2201 } | 2194 } |
2202 | 2195 |
2203 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| | 2196 // |delta| can be < 0 as |now| is approximate time but |time_of_last_packet| |
2204 // is accurate time. However, this should not change the behavior of | 2197 // is accurate time. However, this should not change the behavior of |
2205 // timeout handling. | 2198 // timeout handling. |
2206 QuicTime::Delta idle_duration = now.Subtract(time_of_last_packet); | 2199 QuicTime::Delta idle_duration = now.Subtract(time_of_last_packet); |
2207 DVLOG(1) << ENDPOINT << "last packet " | 2200 DVLOG(1) << ENDPOINT << "last packet " |
2208 << time_of_last_packet.ToDebuggingValue() | 2201 << time_of_last_packet.ToDebuggingValue() |
2209 << " now:" << now.ToDebuggingValue() | 2202 << " now:" << now.ToDebuggingValue() |
2210 << " idle_duration:" << idle_duration.ToMicroseconds() | 2203 << " idle_duration:" << idle_duration.ToMicroseconds() |
2211 << " idle_network_timeout: " | 2204 << " idle_network_timeout: " |
2212 << idle_network_timeout_.ToMicroseconds(); | 2205 << idle_network_timeout_.ToMicroseconds(); |
2213 if (idle_duration >= idle_network_timeout_) { | 2206 if (idle_duration >= idle_network_timeout_) { |
2214 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; | 2207 DVLOG(1) << ENDPOINT << "Connection timedout due to no network activity."; |
2215 SendConnectionCloseWithDetails(QUIC_CONNECTION_TIMED_OUT, | 2208 if (silent_close_enabled_) { |
2216 "No recent network activity"); | 2209 // Just clean up local state, don't send a connection close packet. |
| 2210 CloseConnection(QUIC_NETWORK_IDLE_TIMEOUT, /*from_peer=*/false); |
| 2211 } else { |
| 2212 SendConnectionCloseWithDetails(QUIC_NETWORK_IDLE_TIMEOUT, |
| 2213 "No recent network activity"); |
| 2214 } |
2217 return; | 2215 return; |
2218 } | 2216 } |
2219 | 2217 |
2220 if (!overall_connection_timeout_.IsInfinite()) { | 2218 if (!handshake_timeout_.IsInfinite()) { |
2221 QuicTime::Delta connected_duration = | 2219 QuicTime::Delta connected_duration = |
2222 now.Subtract(stats_.connection_creation_time); | 2220 now.Subtract(stats_.connection_creation_time); |
2223 DVLOG(1) << ENDPOINT | 2221 DVLOG(1) << ENDPOINT |
2224 << "connection time: " << connected_duration.ToMicroseconds() | 2222 << "connection time: " << connected_duration.ToMicroseconds() |
2225 << " overall timeout: " | 2223 << " handshake timeout: " << handshake_timeout_.ToMicroseconds(); |
2226 << overall_connection_timeout_.ToMicroseconds(); | 2224 if (connected_duration >= handshake_timeout_) { |
2227 if (connected_duration >= overall_connection_timeout_) { | 2225 DVLOG(1) << ENDPOINT << "Connection timedout due to handshake timeout."; |
2228 DVLOG(1) << ENDPOINT | 2226 SendConnectionCloseWithDetails(QUIC_HANDSHAKE_TIMEOUT, |
2229 << "Connection timedout due to overall connection timeout."; | 2227 "Handshake timeout expired"); |
2230 SendConnectionCloseWithDetails(QUIC_CONNECTION_OVERALL_TIMED_OUT, | |
2231 "Overall timeout expired"); | |
2232 return; | 2228 return; |
2233 } | 2229 } |
2234 } | 2230 } |
2235 | 2231 |
2236 SetTimeoutAlarm(); | 2232 SetTimeoutAlarm(); |
2237 } | 2233 } |
2238 | 2234 |
2239 void QuicConnection::SetTimeoutAlarm() { | 2235 void QuicConnection::SetTimeoutAlarm() { |
2240 QuicTime time_of_last_packet = | 2236 QuicTime time_of_last_packet = |
2241 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); | 2237 max(time_of_last_received_packet_, time_of_last_sent_new_packet_); |
2242 | 2238 |
2243 QuicTime deadline = time_of_last_packet.Add(idle_network_timeout_); | 2239 QuicTime deadline = time_of_last_packet.Add(idle_network_timeout_); |
2244 if (!overall_connection_timeout_.IsInfinite()) { | 2240 if (!handshake_timeout_.IsInfinite()) { |
2245 deadline = | 2241 deadline = |
2246 min(deadline, | 2242 min(deadline, stats_.connection_creation_time.Add(handshake_timeout_)); |
2247 stats_.connection_creation_time.Add(overall_connection_timeout_)); | |
2248 } | 2243 } |
2249 | 2244 |
2250 timeout_alarm_->Cancel(); | 2245 timeout_alarm_->Cancel(); |
2251 timeout_alarm_->Set(deadline); | 2246 timeout_alarm_->Set(deadline); |
2252 } | 2247 } |
2253 | 2248 |
2254 void QuicConnection::SetPingAlarm() { | 2249 void QuicConnection::SetPingAlarm() { |
2255 if (perspective_ == Perspective::IS_SERVER) { | 2250 if (perspective_ == Perspective::IS_SERVER) { |
2256 // Only clients send pings. | 2251 // Only clients send pings. |
2257 return; | 2252 return; |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2536 void QuicConnection::OnPathClosed(QuicPathId path_id) { | 2531 void QuicConnection::OnPathClosed(QuicPathId path_id) { |
2537 // Stop receiving packets on this path. | 2532 // Stop receiving packets on this path. |
2538 framer_.OnPathClosed(path_id); | 2533 framer_.OnPathClosed(path_id); |
2539 } | 2534 } |
2540 | 2535 |
2541 bool QuicConnection::ack_frame_updated() const { | 2536 bool QuicConnection::ack_frame_updated() const { |
2542 return received_packet_manager_.ack_frame_updated(); | 2537 return received_packet_manager_.ack_frame_updated(); |
2543 } | 2538 } |
2544 | 2539 |
2545 } // namespace net | 2540 } // namespace net |
OLD | NEW |