1 // Copyright 2013 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/quic_received_packet_manager.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "net/base/linked_hash_map.h"
13 #include "net/quic/crypto/crypto_protocol.h"
14 #include "net/quic/quic_connection_stats.h"
18 using std::numeric_limits
;
24 // The maximum number of packets to ack immediately after a missing packet for
25 // fast retransmission to kick in at the sender. This limit is created to
26 // reduce the number of acks sent that have no benefit for fast retransmission.
27 // Set to the number of nacks needed for fast retransmit plus one for protection
28 // against an ack loss
29 const size_t kMaxPacketsAfterNewMissing
= 4;
33 QuicReceivedPacketManager::EntropyTracker::EntropyTracker()
34 : packets_entropy_hash_(0),
36 largest_observed_(0) {
39 QuicReceivedPacketManager::EntropyTracker::~EntropyTracker() {}
41 QuicPacketEntropyHash
QuicReceivedPacketManager::EntropyTracker::EntropyHash(
42 QuicPacketNumber packet_number
) const {
43 DCHECK_LE(packet_number
, largest_observed_
);
44 if (packet_number
== largest_observed_
) {
45 return packets_entropy_hash_
;
48 DCHECK_GE(packet_number
, first_gap_
);
49 DCHECK_EQ(first_gap_
+ packets_entropy_
.size() - 1, largest_observed_
);
50 QuicPacketEntropyHash hash
= packets_entropy_hash_
;
51 ReceivedEntropyHashes::const_reverse_iterator it
= packets_entropy_
.rbegin();
52 for (QuicPacketNumber i
= 0; i
< (largest_observed_
- packet_number
);
59 void QuicReceivedPacketManager::EntropyTracker::RecordPacketEntropyHash(
60 QuicPacketNumber packet_number
,
61 QuicPacketEntropyHash entropy_hash
) {
62 if (packet_number
< first_gap_
) {
63 DVLOG(1) << "Ignoring received packet entropy for packet_number:"
65 << " less than largest_peer_packet_number:" << first_gap_
;
68 // RecordPacketEntropyHash is only intended to be called once per packet.
69 DCHECK(packet_number
> largest_observed_
||
70 !packets_entropy_
[packet_number
- first_gap_
].second
);
72 packets_entropy_hash_
^= entropy_hash
;
74 // Optimize the typical case of no gaps.
75 if (packet_number
== largest_observed_
+ 1 && packets_entropy_
.empty()) {
77 largest_observed_
= packet_number
;
80 if (packet_number
> largest_observed_
) {
81 for (QuicPacketNumber i
= 0; i
< (packet_number
- largest_observed_
- 1);
83 packets_entropy_
.push_back(std::make_pair(0, false));
85 packets_entropy_
.push_back(std::make_pair(entropy_hash
, true));
86 largest_observed_
= packet_number
;
88 packets_entropy_
[packet_number
- first_gap_
] =
89 std::make_pair(entropy_hash
, true);
90 AdvanceFirstGapAndGarbageCollectEntropyMap();
93 DVLOG(2) << "setting cumulative received entropy hash to: "
94 << static_cast<int>(packets_entropy_hash_
)
95 << " updated with packet number " << packet_number
96 << " entropy hash: " << static_cast<int>(entropy_hash
);
99 void QuicReceivedPacketManager::EntropyTracker::SetCumulativeEntropyUpTo(
100 QuicPacketNumber packet_number
,
101 QuicPacketEntropyHash entropy_hash
) {
102 DCHECK_LE(packet_number
, largest_observed_
);
103 if (packet_number
< first_gap_
) {
104 DVLOG(1) << "Ignoring set entropy at:" << packet_number
105 << " less than first_gap_:" << first_gap_
;
108 while (first_gap_
< packet_number
) {
110 if (!packets_entropy_
.empty()) {
111 packets_entropy_
.pop_front();
114 // Compute the current entropy by XORing in all entropies received including
115 // and since packet_number.
116 packets_entropy_hash_
= entropy_hash
;
117 for (ReceivedEntropyHashes::const_iterator it
= packets_entropy_
.begin();
118 it
!= packets_entropy_
.end(); ++it
) {
119 packets_entropy_hash_
^= it
->first
;
122 // Garbage collect entries from the beginning of the map.
123 AdvanceFirstGapAndGarbageCollectEntropyMap();
126 void QuicReceivedPacketManager::EntropyTracker::
127 AdvanceFirstGapAndGarbageCollectEntropyMap() {
128 while (!packets_entropy_
.empty() && packets_entropy_
.front().second
) {
130 packets_entropy_
.pop_front();
134 QuicReceivedPacketManager::QuicReceivedPacketManager(QuicConnectionStats
* stats
)
135 : peer_least_packet_awaiting_ack_(0),
136 time_largest_observed_(QuicTime::Zero()),
138 ack_frame_
.largest_observed
= 0;
139 ack_frame_
.entropy_hash
= 0;
142 QuicReceivedPacketManager::~QuicReceivedPacketManager() {}
144 void QuicReceivedPacketManager::RecordPacketReceived(
146 const QuicPacketHeader
& header
,
147 QuicTime receipt_time
) {
148 QuicPacketNumber packet_number
= header
.packet_packet_number
;
149 DCHECK(IsAwaitingPacket(packet_number
));
151 // Adds the range of packet numbers from max(largest observed + 1, least
152 // awaiting ack) up to packet_number not including packet_number.
153 ack_frame_
.missing_packets
.Add(
154 max(ack_frame_
.largest_observed
+ 1, peer_least_packet_awaiting_ack_
),
157 if (ack_frame_
.largest_observed
> packet_number
) {
158 // We've gotten one of the out of order packets - remove it from our
159 // "missing packets" list.
160 DVLOG(1) << "Removing " << packet_number
<< " from missing list";
161 ack_frame_
.missing_packets
.Remove(packet_number
);
163 // Record how out of order stats.
164 ++stats_
->packets_reordered
;
165 stats_
->max_sequence_reordering
=
166 max(stats_
->max_sequence_reordering
,
167 ack_frame_
.largest_observed
- packet_number
);
168 int64 reordering_time_us
=
169 receipt_time
.Subtract(time_largest_observed_
).ToMicroseconds();
170 stats_
->max_time_reordering_us
= max(stats_
->max_time_reordering_us
,
173 if (packet_number
> ack_frame_
.largest_observed
) {
174 ack_frame_
.largest_observed
= packet_number
;
175 time_largest_observed_
= receipt_time
;
177 entropy_tracker_
.RecordPacketEntropyHash(packet_number
, header
.entropy_hash
);
179 received_packet_times_
.push_back(std::make_pair(packet_number
, receipt_time
));
181 ack_frame_
.revived_packets
.erase(packet_number
);
184 void QuicReceivedPacketManager::RecordPacketRevived(
185 QuicPacketNumber packet_number
) {
186 LOG_IF(DFATAL
, !IsAwaitingPacket(packet_number
));
187 ack_frame_
.revived_packets
.insert(packet_number
);
190 bool QuicReceivedPacketManager::IsMissing(QuicPacketNumber packet_number
) {
191 return ack_frame_
.missing_packets
.Contains(packet_number
);
194 bool QuicReceivedPacketManager::IsAwaitingPacket(
195 QuicPacketNumber packet_number
) {
196 return ::net::IsAwaitingPacket(ack_frame_
, packet_number
);
201 explicit isTooLarge(QuicPacketNumber n
) : largest_observed_(n
) {}
202 QuicPacketNumber largest_observed_
;
204 // Return true if the packet in p is too different from largest_observed_
206 bool operator()(const std::pair
<QuicPacketNumber
, QuicTime
>& p
) const {
207 return largest_observed_
- p
.first
>= numeric_limits
<uint8
>::max();
212 void QuicReceivedPacketManager::UpdateReceivedPacketInfo(
213 QuicAckFrame
* ack_frame
, QuicTime approximate_now
) {
214 *ack_frame
= ack_frame_
;
215 ack_frame
->entropy_hash
= EntropyHash(ack_frame_
.largest_observed
);
217 if (time_largest_observed_
== QuicTime::Zero()) {
218 // We have received no packets.
219 ack_frame
->delta_time_largest_observed
= QuicTime::Delta::Infinite();
223 // Ensure the delta is zero if approximate now is "in the past".
224 ack_frame
->delta_time_largest_observed
=
225 approximate_now
< time_largest_observed_
?
226 QuicTime::Delta::Zero() :
227 approximate_now
.Subtract(time_largest_observed_
);
229 // Remove all packets that are too far from largest_observed to express.
230 received_packet_times_
.remove_if(isTooLarge(ack_frame_
.largest_observed
));
232 ack_frame
->received_packet_times
.clear();
233 ack_frame
->received_packet_times
.swap(received_packet_times_
);
236 QuicPacketEntropyHash
QuicReceivedPacketManager::EntropyHash(
237 QuicPacketNumber packet_number
) const {
238 return entropy_tracker_
.EntropyHash(packet_number
);
241 bool QuicReceivedPacketManager::DontWaitForPacketsBefore(
242 QuicPacketNumber least_unacked
) {
243 ack_frame_
.revived_packets
.erase(
244 ack_frame_
.revived_packets
.begin(),
245 ack_frame_
.revived_packets
.lower_bound(least_unacked
));
246 return ack_frame_
.missing_packets
.RemoveUpTo(least_unacked
);
249 void QuicReceivedPacketManager::UpdatePacketInformationSentByPeer(
250 const QuicStopWaitingFrame
& stop_waiting
) {
251 // ValidateAck() should fail if peer_least_packet_awaiting_ack_ shrinks.
252 DCHECK_LE(peer_least_packet_awaiting_ack_
, stop_waiting
.least_unacked
);
253 if (stop_waiting
.least_unacked
> peer_least_packet_awaiting_ack_
) {
254 bool missed_packets
= DontWaitForPacketsBefore(stop_waiting
.least_unacked
);
255 if (missed_packets
) {
256 DVLOG(1) << "Updating entropy hashed since we missed packets";
257 // There were some missing packets that we won't ever get now. Recalculate
258 // the received entropy hash.
259 entropy_tracker_
.SetCumulativeEntropyUpTo(stop_waiting
.least_unacked
,
260 stop_waiting
.entropy_hash
);
262 peer_least_packet_awaiting_ack_
= stop_waiting
.least_unacked
;
264 DCHECK(ack_frame_
.missing_packets
.Empty() ||
265 ack_frame_
.missing_packets
.Min() >= peer_least_packet_awaiting_ack_
);
268 bool QuicReceivedPacketManager::HasNewMissingPackets() const {
269 return !ack_frame_
.missing_packets
.Empty() &&
270 (ack_frame_
.largest_observed
- ack_frame_
.missing_packets
.Max()) <=
271 kMaxPacketsAfterNewMissing
;
274 size_t QuicReceivedPacketManager::NumTrackedPackets() const {
275 return entropy_tracker_
.size();