1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/quic/quic_received_packet_manager.h"
#include <limits>
#include <utility>
#include "base/logging.h"
#include "base/stl_util.h"
#include "net/base/linked_hash_map.h"
#include "net/quic/crypto/crypto_protocol.h"
#include "net/quic/quic_connection_stats.h"
using std::make_pair;
using std::max;
using std::min;
using std::numeric_limits;
namespace net {
namespace {
// The maximum number of packets to ack immediately after a missing packet for
// fast retransmission to kick in at the sender. This limit is created to
// reduce the number of acks sent that have no benefit for fast retransmission.
// Set to the number of nacks needed for fast retransmit plus one for protection
// against an ack loss
const size_t kMaxPacketsAfterNewMissing = 4;
}
QuicReceivedPacketManager::EntropyTracker::EntropyTracker()
: packets_entropy_hash_(0),
first_gap_(1),
largest_observed_(0) {
}
QuicReceivedPacketManager::EntropyTracker::~EntropyTracker() {}
QuicPacketEntropyHash QuicReceivedPacketManager::EntropyTracker::EntropyHash(
QuicPacketSequenceNumber sequence_number) const {
DCHECK_LE(sequence_number, largest_observed_);
if (sequence_number == largest_observed_) {
return packets_entropy_hash_;
}
DCHECK_GE(sequence_number, first_gap_);
DCHECK_EQ(first_gap_ + packets_entropy_.size() - 1, largest_observed_);
QuicPacketEntropyHash hash = packets_entropy_hash_;
ReceivedEntropyHashes::const_reverse_iterator it = packets_entropy_.rbegin();
for (QuicPacketSequenceNumber i = 0;
i < (largest_observed_ - sequence_number); ++i, ++it) {
hash ^= it->first;
}
return hash;
}
void QuicReceivedPacketManager::EntropyTracker::RecordPacketEntropyHash(
QuicPacketSequenceNumber sequence_number,
QuicPacketEntropyHash entropy_hash) {
if (sequence_number < first_gap_) {
DVLOG(1) << "Ignoring received packet entropy for sequence_number:"
<< sequence_number << " less than largest_peer_sequence_number:"
<< first_gap_;
return;
}
// RecordPacketEntropyHash is only intended to be called once per packet.
DCHECK(sequence_number > largest_observed_ ||
!packets_entropy_[sequence_number - first_gap_].second);
packets_entropy_hash_ ^= entropy_hash;
// Optimize the typical case of no gaps.
if (sequence_number == largest_observed_ + 1 && packets_entropy_.empty()) {
++first_gap_;
largest_observed_ = sequence_number;
return;
}
if (sequence_number > largest_observed_) {
for (QuicPacketSequenceNumber i = 0;
i < (sequence_number - largest_observed_ - 1); ++i) {
packets_entropy_.push_back(make_pair(0, false));
}
packets_entropy_.push_back(make_pair(entropy_hash, true));
largest_observed_ = sequence_number;
} else {
packets_entropy_[sequence_number - first_gap_] =
make_pair(entropy_hash, true);
AdvanceFirstGapAndGarbageCollectEntropyMap();
}
DVLOG(2) << "setting cumulative received entropy hash to: "
<< static_cast<int>(packets_entropy_hash_)
<< " updated with sequence number " << sequence_number
<< " entropy hash: " << static_cast<int>(entropy_hash);
}
void QuicReceivedPacketManager::EntropyTracker::SetCumulativeEntropyUpTo(
QuicPacketSequenceNumber sequence_number,
QuicPacketEntropyHash entropy_hash) {
DCHECK_LE(sequence_number, largest_observed_);
if (sequence_number < first_gap_) {
DVLOG(1) << "Ignoring set entropy at:" << sequence_number
<< " less than first_gap_:" << first_gap_;
return;
}
while (first_gap_ < sequence_number) {
++first_gap_;
if (!packets_entropy_.empty()) {
packets_entropy_.pop_front();
}
}
// Compute the current entropy by XORing in all entropies received including
// and since sequence_number.
packets_entropy_hash_ = entropy_hash;
for (ReceivedEntropyHashes::const_iterator it = packets_entropy_.begin();
it != packets_entropy_.end(); ++it) {
packets_entropy_hash_ ^= it->first;
}
// Garbage collect entries from the beginning of the map.
AdvanceFirstGapAndGarbageCollectEntropyMap();
}
void QuicReceivedPacketManager::EntropyTracker::
AdvanceFirstGapAndGarbageCollectEntropyMap() {
while (!packets_entropy_.empty() && packets_entropy_.front().second) {
++first_gap_;
packets_entropy_.pop_front();
}
}
QuicReceivedPacketManager::QuicReceivedPacketManager(QuicConnectionStats* stats)
: peer_least_packet_awaiting_ack_(0),
time_largest_observed_(QuicTime::Zero()),
stats_(stats) {
ack_frame_.largest_observed = 0;
ack_frame_.entropy_hash = 0;
}
QuicReceivedPacketManager::~QuicReceivedPacketManager() {}
void QuicReceivedPacketManager::RecordPacketReceived(
QuicByteCount bytes,
const QuicPacketHeader& header,
QuicTime receipt_time) {
QuicPacketSequenceNumber sequence_number = header.packet_sequence_number;
DCHECK(IsAwaitingPacket(sequence_number));
InsertMissingPacketsBetween(
&ack_frame_,
max(ack_frame_.largest_observed + 1, peer_least_packet_awaiting_ack_),
sequence_number);
if (ack_frame_.largest_observed > sequence_number) {
// We've gotten one of the out of order packets - remove it from our
// "missing packets" list.
DVLOG(1) << "Removing " << sequence_number << " from missing list";
ack_frame_.missing_packets.erase(sequence_number);
// Record how out of order stats.
++stats_->packets_reordered;
stats_->max_sequence_reordering =
max(stats_->max_sequence_reordering,
ack_frame_.largest_observed - sequence_number);
int64 reordering_time_us =
receipt_time.Subtract(time_largest_observed_).ToMicroseconds();
stats_->max_time_reordering_us = max(stats_->max_time_reordering_us,
reordering_time_us);
}
if (sequence_number > ack_frame_.largest_observed) {
ack_frame_.largest_observed = sequence_number;
time_largest_observed_ = receipt_time;
}
entropy_tracker_.RecordPacketEntropyHash(sequence_number,
header.entropy_hash);
received_packet_times_.push_back(
std::make_pair(sequence_number, receipt_time));
ack_frame_.revived_packets.erase(sequence_number);
}
void QuicReceivedPacketManager::RecordPacketRevived(
QuicPacketSequenceNumber sequence_number) {
LOG_IF(DFATAL, !IsAwaitingPacket(sequence_number));
ack_frame_.revived_packets.insert(sequence_number);
}
bool QuicReceivedPacketManager::IsMissing(
QuicPacketSequenceNumber sequence_number) {
return ContainsKey(ack_frame_.missing_packets, sequence_number);
}
bool QuicReceivedPacketManager::IsAwaitingPacket(
QuicPacketSequenceNumber sequence_number) {
return ::net::IsAwaitingPacket(ack_frame_, sequence_number);
}
namespace {
struct isTooLarge {
explicit isTooLarge(QuicPacketSequenceNumber n) : largest_observed_(n) {}
QuicPacketSequenceNumber largest_observed_;
// Return true if the packet in p is too different from largest_observed_
// to express.
bool operator() (
const std::pair<QuicPacketSequenceNumber, QuicTime>& p) const {
return largest_observed_ - p.first >= numeric_limits<uint8>::max();
}
};
} // namespace
void QuicReceivedPacketManager::UpdateReceivedPacketInfo(
QuicAckFrame* ack_frame, QuicTime approximate_now) {
*ack_frame = ack_frame_;
ack_frame->entropy_hash = EntropyHash(ack_frame_.largest_observed);
if (time_largest_observed_ == QuicTime::Zero()) {
// We have received no packets.
ack_frame->delta_time_largest_observed = QuicTime::Delta::Infinite();
return;
}
// Ensure the delta is zero if approximate now is "in the past".
ack_frame->delta_time_largest_observed =
approximate_now < time_largest_observed_ ?
QuicTime::Delta::Zero() :
approximate_now.Subtract(time_largest_observed_);
// Remove all packets that are too far from largest_observed to express.
received_packet_times_.remove_if(isTooLarge(ack_frame_.largest_observed));
ack_frame->received_packet_times.clear();
ack_frame->received_packet_times.swap(received_packet_times_);
}
QuicPacketEntropyHash QuicReceivedPacketManager::EntropyHash(
QuicPacketSequenceNumber sequence_number) const {
return entropy_tracker_.EntropyHash(sequence_number);
}
bool QuicReceivedPacketManager::DontWaitForPacketsBefore(
QuicPacketSequenceNumber least_unacked) {
ack_frame_.revived_packets.erase(
ack_frame_.revived_packets.begin(),
ack_frame_.revived_packets.lower_bound(least_unacked));
size_t missing_packets_count = ack_frame_.missing_packets.size();
ack_frame_.missing_packets.erase(
ack_frame_.missing_packets.begin(),
ack_frame_.missing_packets.lower_bound(least_unacked));
return missing_packets_count != ack_frame_.missing_packets.size();
}
void QuicReceivedPacketManager::UpdatePacketInformationSentByPeer(
const QuicStopWaitingFrame& stop_waiting) {
// ValidateAck() should fail if peer_least_packet_awaiting_ack_ shrinks.
DCHECK_LE(peer_least_packet_awaiting_ack_, stop_waiting.least_unacked);
if (stop_waiting.least_unacked > peer_least_packet_awaiting_ack_) {
bool missed_packets = DontWaitForPacketsBefore(stop_waiting.least_unacked);
if (missed_packets) {
DVLOG(1) << "Updating entropy hashed since we missed packets";
// There were some missing packets that we won't ever get now. Recalculate
// the received entropy hash.
entropy_tracker_.SetCumulativeEntropyUpTo(stop_waiting.least_unacked,
stop_waiting.entropy_hash);
}
peer_least_packet_awaiting_ack_ = stop_waiting.least_unacked;
}
DCHECK(ack_frame_.missing_packets.empty() ||
*ack_frame_.missing_packets.begin() >=
peer_least_packet_awaiting_ack_);
}
bool QuicReceivedPacketManager::HasNewMissingPackets() const {
return !ack_frame_.missing_packets.empty() &&
(ack_frame_.largest_observed -
*ack_frame_.missing_packets.rbegin()) <= kMaxPacketsAfterNewMissing;
}
size_t QuicReceivedPacketManager::NumTrackedPackets() const {
return entropy_tracker_.size();
}
} // namespace net
|