1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/congestion_control/tcp_loss_algorithm.h"
7 #include "net/quic/congestion_control/rtt_stats.h"
8 #include "net/quic/quic_protocol.h"
14 // The minimum delay before a packet will be considered lost,
15 // regardless of SRTT. Half of the minimum TLP, since the loss algorithm only
16 // triggers when a nack has been receieved for the packet.
17 static const size_t kMinLossDelayMs
= 5;
19 // How many RTTs the algorithm waits before determining a packet is lost due
20 // to early retransmission.
21 static const double kEarlyRetransmitLossDelayMultiplier
= 1.25;
25 TCPLossAlgorithm::TCPLossAlgorithm()
26 : loss_detection_timeout_(QuicTime::Zero()) { }
28 LossDetectionType
TCPLossAlgorithm::GetLossDetectionType() const {
32 // Uses nack counts to decide when packets are lost.
33 SequenceNumberSet
TCPLossAlgorithm::DetectLostPackets(
34 const QuicUnackedPacketMap
& unacked_packets
,
36 QuicPacketSequenceNumber largest_observed
,
37 const RttStats
& rtt_stats
) {
38 SequenceNumberSet lost_packets
;
39 loss_detection_timeout_
= QuicTime::Zero();
40 QuicTime::Delta early_retransmit_delay
= QuicTime::Delta::Max(
41 QuicTime::Delta::FromMilliseconds(kMinLossDelayMs
),
42 rtt_stats
.smoothed_rtt().Multiply(kEarlyRetransmitLossDelayMultiplier
));
44 QuicPacketSequenceNumber sequence_number
= unacked_packets
.GetLeastUnacked();
45 for (QuicUnackedPacketMap::const_iterator it
= unacked_packets
.begin();
46 it
!= unacked_packets
.end() && sequence_number
<= largest_observed
;
47 ++it
, ++sequence_number
) {
52 LOG_IF(DFATAL
, it
->nack_count
== 0 && it
->sent_time
.IsInitialized())
53 << "All packets less than largest observed should have been nacked."
54 << "sequence_number:" << sequence_number
55 << " largest_observed:" << largest_observed
;
56 if (it
->nack_count
>= kNumberOfNacksBeforeRetransmission
) {
57 lost_packets
.insert(sequence_number
);
61 // Immediately lose the packet if it's been an srtt between the sent time
62 // of it and the largest observed. This speeds recovery from timer based
63 // retransmissions, such as TLP and RTO, when there may be fewer than
64 // kNumberOfNacksBeforeRetransmission nacks.
65 if (it
->sent_time
.Add(rtt_stats
.smoothed_rtt()) <
66 unacked_packets
.GetTransmissionInfo(largest_observed
).sent_time
) {
67 lost_packets
.insert(sequence_number
);
71 // Only early retransmit(RFC5827) when the last packet gets acked and
72 // there are retransmittable packets in flight.
73 // This also implements a timer-protected variant of FACK.
74 if (it
->retransmittable_frames
&&
75 unacked_packets
.largest_sent_packet() == largest_observed
) {
76 // Early retransmit marks the packet as lost once 1.25RTTs have passed
77 // since the packet was sent and otherwise sets an alarm.
78 if (time
>= it
->sent_time
.Add(early_retransmit_delay
)) {
79 lost_packets
.insert(sequence_number
);
81 // Set the timeout for the earliest retransmittable packet where early
82 // retransmit applies.
83 loss_detection_timeout_
= it
->sent_time
.Add(early_retransmit_delay
);
92 QuicTime
TCPLossAlgorithm::GetLossTimeout() const {
93 return loss_detection_timeout_
;