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 QuicPacketSequenceNumber sequence_number
) const {
43 DCHECK_LE(sequence_number
, largest_observed_
);
44 if (sequence_number
== largest_observed_
) {
45 return packets_entropy_hash_
;
48 DCHECK_GE(sequence_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 (QuicPacketSequenceNumber i
= 0;
53 i
< (largest_observed_
- sequence_number
); ++i
, ++it
) {
59 void QuicReceivedPacketManager::EntropyTracker::RecordPacketEntropyHash(
60 QuicPacketSequenceNumber sequence_number
,
61 QuicPacketEntropyHash entropy_hash
) {
62 if (sequence_number
< first_gap_
) {
63 DVLOG(1) << "Ignoring received packet entropy for sequence_number:"
64 << sequence_number
<< " less than largest_peer_sequence_number:"
68 // RecordPacketEntropyHash is only intended to be called once per packet.
69 DCHECK(sequence_number
> largest_observed_
||
70 !packets_entropy_
[sequence_number
- first_gap_
].second
);
72 packets_entropy_hash_
^= entropy_hash
;
74 // Optimize the typical case of no gaps.
75 if (sequence_number
== largest_observed_
+ 1 && packets_entropy_
.empty()) {
77 largest_observed_
= sequence_number
;
80 if (sequence_number
> largest_observed_
) {
81 for (QuicPacketSequenceNumber i
= 0;
82 i
< (sequence_number
- largest_observed_
- 1); ++i
) {
83 packets_entropy_
.push_back(std::make_pair(0, false));
85 packets_entropy_
.push_back(std::make_pair(entropy_hash
, true));
86 largest_observed_
= sequence_number
;
88 packets_entropy_
[sequence_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 sequence number " << sequence_number
96 << " entropy hash: " << static_cast<int>(entropy_hash
);
99 void QuicReceivedPacketManager::EntropyTracker::SetCumulativeEntropyUpTo(
100 QuicPacketSequenceNumber sequence_number
,
101 QuicPacketEntropyHash entropy_hash
) {
102 DCHECK_LE(sequence_number
, largest_observed_
);
103 if (sequence_number
< first_gap_
) {
104 DVLOG(1) << "Ignoring set entropy at:" << sequence_number
105 << " less than first_gap_:" << first_gap_
;
108 while (first_gap_
< sequence_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 sequence_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 QuicPacketSequenceNumber sequence_number
= header
.packet_sequence_number
;
149 DCHECK(IsAwaitingPacket(sequence_number
));
151 InsertMissingPacketsBetween(
153 max(ack_frame_
.largest_observed
+ 1, peer_least_packet_awaiting_ack_
),
156 if (ack_frame_
.largest_observed
> sequence_number
) {
157 // We've gotten one of the out of order packets - remove it from our
158 // "missing packets" list.
159 DVLOG(1) << "Removing " << sequence_number
<< " from missing list";
160 ack_frame_
.missing_packets
.erase(sequence_number
);
162 // Record how out of order stats.
163 ++stats_
->packets_reordered
;
164 stats_
->max_sequence_reordering
=
165 max(stats_
->max_sequence_reordering
,
166 ack_frame_
.largest_observed
- sequence_number
);
167 int64 reordering_time_us
=
168 receipt_time
.Subtract(time_largest_observed_
).ToMicroseconds();
169 stats_
->max_time_reordering_us
= max(stats_
->max_time_reordering_us
,
172 if (sequence_number
> ack_frame_
.largest_observed
) {
173 ack_frame_
.largest_observed
= sequence_number
;
174 time_largest_observed_
= receipt_time
;
176 entropy_tracker_
.RecordPacketEntropyHash(sequence_number
,
177 header
.entropy_hash
);
179 received_packet_times_
.push_back(
180 std::make_pair(sequence_number
, receipt_time
));
182 ack_frame_
.revived_packets
.erase(sequence_number
);
185 void QuicReceivedPacketManager::RecordPacketRevived(
186 QuicPacketSequenceNumber sequence_number
) {
187 LOG_IF(DFATAL
, !IsAwaitingPacket(sequence_number
));
188 ack_frame_
.revived_packets
.insert(sequence_number
);
191 bool QuicReceivedPacketManager::IsMissing(
192 QuicPacketSequenceNumber sequence_number
) {
193 return ContainsKey(ack_frame_
.missing_packets
, sequence_number
);
196 bool QuicReceivedPacketManager::IsAwaitingPacket(
197 QuicPacketSequenceNumber sequence_number
) {
198 return ::net::IsAwaitingPacket(ack_frame_
, sequence_number
);
203 explicit isTooLarge(QuicPacketSequenceNumber n
) : largest_observed_(n
) {}
204 QuicPacketSequenceNumber largest_observed_
;
206 // Return true if the packet in p is too different from largest_observed_
209 const std::pair
<QuicPacketSequenceNumber
, QuicTime
>& p
) const {
210 return largest_observed_
- p
.first
>= numeric_limits
<uint8
>::max();
215 void QuicReceivedPacketManager::UpdateReceivedPacketInfo(
216 QuicAckFrame
* ack_frame
, QuicTime approximate_now
) {
217 *ack_frame
= ack_frame_
;
218 ack_frame
->entropy_hash
= EntropyHash(ack_frame_
.largest_observed
);
220 if (time_largest_observed_
== QuicTime::Zero()) {
221 // We have received no packets.
222 ack_frame
->delta_time_largest_observed
= QuicTime::Delta::Infinite();
226 // Ensure the delta is zero if approximate now is "in the past".
227 ack_frame
->delta_time_largest_observed
=
228 approximate_now
< time_largest_observed_
?
229 QuicTime::Delta::Zero() :
230 approximate_now
.Subtract(time_largest_observed_
);
232 // Remove all packets that are too far from largest_observed to express.
233 received_packet_times_
.remove_if(isTooLarge(ack_frame_
.largest_observed
));
235 ack_frame
->received_packet_times
.clear();
236 ack_frame
->received_packet_times
.swap(received_packet_times_
);
239 QuicPacketEntropyHash
QuicReceivedPacketManager::EntropyHash(
240 QuicPacketSequenceNumber sequence_number
) const {
241 return entropy_tracker_
.EntropyHash(sequence_number
);
244 bool QuicReceivedPacketManager::DontWaitForPacketsBefore(
245 QuicPacketSequenceNumber least_unacked
) {
246 ack_frame_
.revived_packets
.erase(
247 ack_frame_
.revived_packets
.begin(),
248 ack_frame_
.revived_packets
.lower_bound(least_unacked
));
249 size_t missing_packets_count
= ack_frame_
.missing_packets
.size();
250 ack_frame_
.missing_packets
.erase(
251 ack_frame_
.missing_packets
.begin(),
252 ack_frame_
.missing_packets
.lower_bound(least_unacked
));
253 return missing_packets_count
!= ack_frame_
.missing_packets
.size();
256 void QuicReceivedPacketManager::UpdatePacketInformationSentByPeer(
257 const QuicStopWaitingFrame
& stop_waiting
) {
258 // ValidateAck() should fail if peer_least_packet_awaiting_ack_ shrinks.
259 DCHECK_LE(peer_least_packet_awaiting_ack_
, stop_waiting
.least_unacked
);
260 if (stop_waiting
.least_unacked
> peer_least_packet_awaiting_ack_
) {
261 bool missed_packets
= DontWaitForPacketsBefore(stop_waiting
.least_unacked
);
262 if (missed_packets
) {
263 DVLOG(1) << "Updating entropy hashed since we missed packets";
264 // There were some missing packets that we won't ever get now. Recalculate
265 // the received entropy hash.
266 entropy_tracker_
.SetCumulativeEntropyUpTo(stop_waiting
.least_unacked
,
267 stop_waiting
.entropy_hash
);
269 peer_least_packet_awaiting_ack_
= stop_waiting
.least_unacked
;
271 DCHECK(ack_frame_
.missing_packets
.empty() ||
272 *ack_frame_
.missing_packets
.begin() >=
273 peer_least_packet_awaiting_ack_
);
276 bool QuicReceivedPacketManager::HasNewMissingPackets() const {
277 return !ack_frame_
.missing_packets
.empty() &&
278 (ack_frame_
.largest_observed
-
279 *ack_frame_
.missing_packets
.rbegin()) <= kMaxPacketsAfterNewMissing
;
282 size_t QuicReceivedPacketManager::NumTrackedPackets() const {
283 return entropy_tracker_
.size();