blob: a2fb64014e875f1d73f8b983c1d619e545d3b173 [file] [log] [blame]
QUICHE teamb23daa72019-03-21 08:37:48 -07001// Copyright 2019 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.
4
5#include "net/third_party/quiche/src/quic/core/uber_received_packet_manager.h"
6
bnc463f2352019-10-10 04:49:34 -07007#include <utility>
8
QUICHE teamb23daa72019-03-21 08:37:48 -07009#include "net/third_party/quiche/src/quic/core/congestion_control/rtt_stats.h"
10#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
11#include "net/third_party/quiche/src/quic/core/quic_connection_stats.h"
QUICHE team1dfa46b2019-03-22 10:39:10 -070012#include "net/third_party/quiche/src/quic/core/quic_utils.h"
QUICHE teamb23daa72019-03-21 08:37:48 -070013#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
14#include "net/third_party/quiche/src/quic/test_tools/mock_clock.h"
15
16namespace quic {
17namespace test {
18
19class UberReceivedPacketManagerPeer {
20 public:
21 static void SetAckMode(UberReceivedPacketManager* manager, AckMode ack_mode) {
QUICHE team1dfa46b2019-03-22 10:39:10 -070022 for (auto& received_packet_manager : manager->received_packet_managers_) {
23 received_packet_manager.ack_mode_ = ack_mode;
24 }
QUICHE teamb23daa72019-03-21 08:37:48 -070025 }
26
27 static void SetFastAckAfterQuiescence(UberReceivedPacketManager* manager,
28 bool fast_ack_after_quiescence) {
QUICHE team1dfa46b2019-03-22 10:39:10 -070029 for (auto& received_packet_manager : manager->received_packet_managers_) {
30 received_packet_manager.fast_ack_after_quiescence_ =
31 fast_ack_after_quiescence;
32 }
QUICHE teamb23daa72019-03-21 08:37:48 -070033 }
34
35 static void SetAckDecimationDelay(UberReceivedPacketManager* manager,
36 float ack_decimation_delay) {
QUICHE team1dfa46b2019-03-22 10:39:10 -070037 for (auto& received_packet_manager : manager->received_packet_managers_) {
38 received_packet_manager.ack_decimation_delay_ = ack_decimation_delay;
39 }
QUICHE teamb23daa72019-03-21 08:37:48 -070040 }
41};
42
43namespace {
44
45const bool kInstigateAck = true;
46const QuicTime::Delta kMinRttMs = QuicTime::Delta::FromMilliseconds(40);
47const QuicTime::Delta kDelayedAckTime =
48 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs);
49
50class UberReceivedPacketManagerTest : public QuicTest {
51 protected:
52 UberReceivedPacketManagerTest() {
vasilvv0fc587f2019-09-06 13:33:08 -070053 manager_ = std::make_unique<UberReceivedPacketManager>(&stats_);
QUICHE teamb23daa72019-03-21 08:37:48 -070054 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1));
55 rtt_stats_.UpdateRtt(kMinRttMs, QuicTime::Delta::Zero(), QuicTime::Zero());
56 manager_->set_save_timestamps(true);
57 }
58
59 void RecordPacketReceipt(uint64_t packet_number) {
QUICHE team1dfa46b2019-03-22 10:39:10 -070060 RecordPacketReceipt(ENCRYPTION_FORWARD_SECURE, packet_number);
QUICHE teamb23daa72019-03-21 08:37:48 -070061 }
62
63 void RecordPacketReceipt(uint64_t packet_number, QuicTime receipt_time) {
QUICHE team1dfa46b2019-03-22 10:39:10 -070064 RecordPacketReceipt(ENCRYPTION_FORWARD_SECURE, packet_number, receipt_time);
QUICHE teamb23daa72019-03-21 08:37:48 -070065 }
66
QUICHE team1dfa46b2019-03-22 10:39:10 -070067 void RecordPacketReceipt(EncryptionLevel decrypted_packet_level,
68 uint64_t packet_number) {
69 RecordPacketReceipt(decrypted_packet_level, packet_number,
70 QuicTime::Zero());
71 }
72
73 void RecordPacketReceipt(EncryptionLevel decrypted_packet_level,
74 uint64_t packet_number,
75 QuicTime receipt_time) {
76 QuicPacketHeader header;
77 header.packet_number = QuicPacketNumber(packet_number);
78 manager_->RecordPacketReceived(decrypted_packet_level, header,
79 receipt_time);
80 }
81
82 bool HasPendingAck() {
83 if (!manager_->supports_multiple_packet_number_spaces()) {
84 return manager_->GetAckTimeout(APPLICATION_DATA).IsInitialized();
85 }
86 return manager_->GetEarliestAckTimeout().IsInitialized();
87 }
QUICHE teamb23daa72019-03-21 08:37:48 -070088
89 void MaybeUpdateAckTimeout(bool should_last_packet_instigate_acks,
90 uint64_t last_received_packet_number) {
QUICHE team1dfa46b2019-03-22 10:39:10 -070091 MaybeUpdateAckTimeout(should_last_packet_instigate_acks,
92 ENCRYPTION_FORWARD_SECURE,
93 last_received_packet_number);
94 }
95
96 void MaybeUpdateAckTimeout(bool should_last_packet_instigate_acks,
97 EncryptionLevel decrypted_packet_level,
98 uint64_t last_received_packet_number) {
QUICHE teamb23daa72019-03-21 08:37:48 -070099 manager_->MaybeUpdateAckTimeout(
QUICHE team1dfa46b2019-03-22 10:39:10 -0700100 should_last_packet_instigate_acks, decrypted_packet_level,
QUICHE teamb23daa72019-03-21 08:37:48 -0700101 QuicPacketNumber(last_received_packet_number), clock_.ApproximateNow(),
ianswett309987e2019-08-02 13:16:26 -0700102 clock_.ApproximateNow(), &rtt_stats_);
QUICHE teamb23daa72019-03-21 08:37:48 -0700103 }
104
105 void CheckAckTimeout(QuicTime time) {
QUICHE team1dfa46b2019-03-22 10:39:10 -0700106 DCHECK(HasPendingAck());
107 if (!manager_->supports_multiple_packet_number_spaces()) {
108 DCHECK(manager_->GetAckTimeout(APPLICATION_DATA) == time);
109 if (time <= clock_.ApproximateNow()) {
110 // ACK timeout expires, send an ACK.
111 manager_->ResetAckStates(ENCRYPTION_FORWARD_SECURE);
112 DCHECK(!HasPendingAck());
113 }
114 return;
115 }
116 DCHECK(manager_->GetEarliestAckTimeout() == time);
117 // Send all expired ACKs.
118 for (int8_t i = INITIAL_DATA; i < NUM_PACKET_NUMBER_SPACES; ++i) {
119 const QuicTime ack_timeout =
120 manager_->GetAckTimeout(static_cast<PacketNumberSpace>(i));
121 if (!ack_timeout.IsInitialized() ||
122 ack_timeout > clock_.ApproximateNow()) {
123 continue;
124 }
125 manager_->ResetAckStates(
126 QuicUtils::GetEncryptionLevel(static_cast<PacketNumberSpace>(i)));
QUICHE teamb23daa72019-03-21 08:37:48 -0700127 }
128 }
129
130 MockClock clock_;
131 RttStats rtt_stats_;
132 QuicConnectionStats stats_;
133 std::unique_ptr<UberReceivedPacketManager> manager_;
134};
135
136TEST_F(UberReceivedPacketManagerTest, DontWaitForPacketsBefore) {
fayang5d92c412020-02-19 12:10:31 -0800137 EXPECT_TRUE(manager_->IsAckFrameEmpty(APPLICATION_DATA));
QUICHE team1dfa46b2019-03-22 10:39:10 -0700138 RecordPacketReceipt(2);
fayang5d92c412020-02-19 12:10:31 -0800139 EXPECT_FALSE(manager_->IsAckFrameEmpty(APPLICATION_DATA));
QUICHE team1dfa46b2019-03-22 10:39:10 -0700140 RecordPacketReceipt(7);
141 EXPECT_TRUE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
142 QuicPacketNumber(3u)));
143 EXPECT_TRUE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
144 QuicPacketNumber(6u)));
145 manager_->DontWaitForPacketsBefore(ENCRYPTION_FORWARD_SECURE,
146 QuicPacketNumber(4));
147 EXPECT_FALSE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
148 QuicPacketNumber(3u)));
149 EXPECT_TRUE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
150 QuicPacketNumber(6u)));
QUICHE teamb23daa72019-03-21 08:37:48 -0700151}
152
153TEST_F(UberReceivedPacketManagerTest, GetUpdatedAckFrame) {
QUICHE teamb23daa72019-03-21 08:37:48 -0700154 QuicTime two_ms = QuicTime::Zero() + QuicTime::Delta::FromMilliseconds(2);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700155 EXPECT_FALSE(manager_->IsAckFrameUpdated());
156 RecordPacketReceipt(2, two_ms);
157 EXPECT_TRUE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700158
QUICHE team1dfa46b2019-03-22 10:39:10 -0700159 QuicFrame ack =
160 manager_->GetUpdatedAckFrame(APPLICATION_DATA, QuicTime::Zero());
161 manager_->ResetAckStates(ENCRYPTION_FORWARD_SECURE);
162 EXPECT_FALSE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700163 // When UpdateReceivedPacketInfo with a time earlier than the time of the
164 // largest observed packet, make sure that the delta is 0, not negative.
165 EXPECT_EQ(QuicTime::Delta::Zero(), ack.ack_frame->ack_delay_time);
166 EXPECT_EQ(1u, ack.ack_frame->received_packet_times.size());
167
168 QuicTime four_ms = QuicTime::Zero() + QuicTime::Delta::FromMilliseconds(4);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700169 ack = manager_->GetUpdatedAckFrame(APPLICATION_DATA, four_ms);
170 manager_->ResetAckStates(ENCRYPTION_FORWARD_SECURE);
171 EXPECT_FALSE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700172 // When UpdateReceivedPacketInfo after not having received a new packet,
173 // the delta should still be accurate.
174 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(2),
175 ack.ack_frame->ack_delay_time);
176 // And received packet times won't have change.
177 EXPECT_EQ(1u, ack.ack_frame->received_packet_times.size());
178
QUICHE team1dfa46b2019-03-22 10:39:10 -0700179 RecordPacketReceipt(999, two_ms);
180 RecordPacketReceipt(4, two_ms);
181 RecordPacketReceipt(1000, two_ms);
182 EXPECT_TRUE(manager_->IsAckFrameUpdated());
183 ack = manager_->GetUpdatedAckFrame(APPLICATION_DATA, two_ms);
184 manager_->ResetAckStates(ENCRYPTION_FORWARD_SECURE);
185 EXPECT_FALSE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700186 // UpdateReceivedPacketInfo should discard any times which can't be
187 // expressed on the wire.
188 EXPECT_EQ(2u, ack.ack_frame->received_packet_times.size());
189}
190
191TEST_F(UberReceivedPacketManagerTest, UpdateReceivedConnectionStats) {
QUICHE team1dfa46b2019-03-22 10:39:10 -0700192 EXPECT_FALSE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700193 RecordPacketReceipt(1);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700194 EXPECT_TRUE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700195 RecordPacketReceipt(6);
196 RecordPacketReceipt(2,
197 QuicTime::Zero() + QuicTime::Delta::FromMilliseconds(1));
198
199 EXPECT_EQ(4u, stats_.max_sequence_reordering);
200 EXPECT_EQ(1000, stats_.max_time_reordering_us);
201 EXPECT_EQ(1u, stats_.packets_reordered);
202}
203
204TEST_F(UberReceivedPacketManagerTest, LimitAckRanges) {
205 manager_->set_max_ack_ranges(10);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700206 EXPECT_FALSE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700207 for (int i = 0; i < 100; ++i) {
208 RecordPacketReceipt(1 + 2 * i);
QUICHE team1dfa46b2019-03-22 10:39:10 -0700209 EXPECT_TRUE(manager_->IsAckFrameUpdated());
210 manager_->GetUpdatedAckFrame(APPLICATION_DATA, QuicTime::Zero());
QUICHE teamb23daa72019-03-21 08:37:48 -0700211 EXPECT_GE(10u, manager_->ack_frame().packets.NumIntervals());
212 EXPECT_EQ(QuicPacketNumber(1u + 2 * i),
213 manager_->ack_frame().packets.Max());
214 for (int j = 0; j < std::min(10, i + 1); ++j) {
215 ASSERT_GE(i, j);
216 EXPECT_TRUE(manager_->ack_frame().packets.Contains(
217 QuicPacketNumber(1 + (i - j) * 2)));
218 if (i > j) {
219 EXPECT_FALSE(manager_->ack_frame().packets.Contains(
220 QuicPacketNumber((i - j) * 2)));
221 }
222 }
223 }
224}
225
226TEST_F(UberReceivedPacketManagerTest, IgnoreOutOfOrderTimestamps) {
QUICHE team1dfa46b2019-03-22 10:39:10 -0700227 EXPECT_FALSE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700228 RecordPacketReceipt(1, QuicTime::Zero());
QUICHE team1dfa46b2019-03-22 10:39:10 -0700229 EXPECT_TRUE(manager_->IsAckFrameUpdated());
QUICHE teamb23daa72019-03-21 08:37:48 -0700230 EXPECT_EQ(1u, manager_->ack_frame().received_packet_times.size());
231 RecordPacketReceipt(2,
232 QuicTime::Zero() + QuicTime::Delta::FromMilliseconds(1));
233 EXPECT_EQ(2u, manager_->ack_frame().received_packet_times.size());
234 RecordPacketReceipt(3, QuicTime::Zero());
235 EXPECT_EQ(2u, manager_->ack_frame().received_packet_times.size());
236}
237
238TEST_F(UberReceivedPacketManagerTest, OutOfOrderReceiptCausesAckSent) {
239 EXPECT_FALSE(HasPendingAck());
240
241 RecordPacketReceipt(3, clock_.ApproximateNow());
242 MaybeUpdateAckTimeout(kInstigateAck, 3);
fayang6dba4902019-06-17 10:04:23 -0700243 // Delayed ack is scheduled.
244 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
QUICHE teamb23daa72019-03-21 08:37:48 -0700245
246 RecordPacketReceipt(2, clock_.ApproximateNow());
247 MaybeUpdateAckTimeout(kInstigateAck, 2);
248 CheckAckTimeout(clock_.ApproximateNow());
249
250 RecordPacketReceipt(1, clock_.ApproximateNow());
251 MaybeUpdateAckTimeout(kInstigateAck, 1);
252 // Should ack immediately, since this fills the last hole.
253 CheckAckTimeout(clock_.ApproximateNow());
254
255 RecordPacketReceipt(4, clock_.ApproximateNow());
256 MaybeUpdateAckTimeout(kInstigateAck, 4);
257 // Delayed ack is scheduled.
258 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
259}
260
261TEST_F(UberReceivedPacketManagerTest, OutOfOrderAckReceiptCausesNoAck) {
262 EXPECT_FALSE(HasPendingAck());
263
264 RecordPacketReceipt(2, clock_.ApproximateNow());
265 MaybeUpdateAckTimeout(!kInstigateAck, 2);
266 EXPECT_FALSE(HasPendingAck());
267
268 RecordPacketReceipt(1, clock_.ApproximateNow());
269 MaybeUpdateAckTimeout(!kInstigateAck, 1);
270 EXPECT_FALSE(HasPendingAck());
271}
272
273TEST_F(UberReceivedPacketManagerTest, AckReceiptCausesAckSend) {
274 EXPECT_FALSE(HasPendingAck());
275
276 RecordPacketReceipt(1, clock_.ApproximateNow());
277 MaybeUpdateAckTimeout(!kInstigateAck, 1);
278 EXPECT_FALSE(HasPendingAck());
279
280 RecordPacketReceipt(2, clock_.ApproximateNow());
281 MaybeUpdateAckTimeout(!kInstigateAck, 2);
282 EXPECT_FALSE(HasPendingAck());
283
284 RecordPacketReceipt(3, clock_.ApproximateNow());
285 MaybeUpdateAckTimeout(kInstigateAck, 3);
286 // Delayed ack is scheduled.
287 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
288 clock_.AdvanceTime(kDelayedAckTime);
289 CheckAckTimeout(clock_.ApproximateNow());
290
291 RecordPacketReceipt(4, clock_.ApproximateNow());
292 MaybeUpdateAckTimeout(!kInstigateAck, 4);
293 EXPECT_FALSE(HasPendingAck());
294
295 RecordPacketReceipt(5, clock_.ApproximateNow());
296 MaybeUpdateAckTimeout(!kInstigateAck, 5);
297 EXPECT_FALSE(HasPendingAck());
298}
299
300TEST_F(UberReceivedPacketManagerTest, AckSentEveryNthPacket) {
301 EXPECT_FALSE(HasPendingAck());
302 manager_->set_ack_frequency_before_ack_decimation(3);
303
304 // Receives packets 1 - 39.
305 for (size_t i = 1; i <= 39; ++i) {
306 RecordPacketReceipt(i, clock_.ApproximateNow());
307 MaybeUpdateAckTimeout(kInstigateAck, i);
308 if (i % 3 == 0) {
309 CheckAckTimeout(clock_.ApproximateNow());
310 } else {
311 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
312 }
313 }
314}
315
316TEST_F(UberReceivedPacketManagerTest, AckDecimationReducesAcks) {
317 EXPECT_FALSE(HasPendingAck());
318 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(),
319 ACK_DECIMATION_WITH_REORDERING);
320
321 // Start ack decimation from 10th packet.
322 manager_->set_min_received_before_ack_decimation(10);
323
324 // Receives packets 1 - 29.
325 for (size_t i = 1; i <= 29; ++i) {
326 RecordPacketReceipt(i, clock_.ApproximateNow());
327 MaybeUpdateAckTimeout(kInstigateAck, i);
328 if (i <= 10) {
329 // For packets 1-10, ack every 2 packets.
330 if (i % 2 == 0) {
331 CheckAckTimeout(clock_.ApproximateNow());
332 } else {
333 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
334 }
335 continue;
336 }
337 // ack at 20.
338 if (i == 20) {
339 CheckAckTimeout(clock_.ApproximateNow());
340 } else {
341 CheckAckTimeout(clock_.ApproximateNow() + kMinRttMs * 0.25);
342 }
343 }
344
345 // We now receive the 30th packet, and so we send an ack.
346 RecordPacketReceipt(30, clock_.ApproximateNow());
347 MaybeUpdateAckTimeout(kInstigateAck, 30);
348 CheckAckTimeout(clock_.ApproximateNow());
349}
350
351TEST_F(UberReceivedPacketManagerTest, SendDelayedAfterQuiescence) {
352 EXPECT_FALSE(HasPendingAck());
353 UberReceivedPacketManagerPeer::SetFastAckAfterQuiescence(manager_.get(),
354 true);
355 // The beginning of the connection counts as quiescence.
356 QuicTime ack_time =
357 clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1);
358
359 RecordPacketReceipt(1, clock_.ApproximateNow());
360 MaybeUpdateAckTimeout(kInstigateAck, 1);
361 CheckAckTimeout(ack_time);
362 // Simulate delayed ack alarm firing.
363 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
364 CheckAckTimeout(clock_.ApproximateNow());
365
366 // Process another packet immediately after sending the ack and expect the
367 // ack timeout to be set delayed ack time in the future.
368 ack_time = clock_.ApproximateNow() + kDelayedAckTime;
369 RecordPacketReceipt(2, clock_.ApproximateNow());
370 MaybeUpdateAckTimeout(kInstigateAck, 2);
371 CheckAckTimeout(ack_time);
372 // Simulate delayed ack alarm firing.
373 clock_.AdvanceTime(kDelayedAckTime);
374 CheckAckTimeout(clock_.ApproximateNow());
375
376 // Wait 1 second and enesure the ack timeout is set to 1ms in the future.
377 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1));
378 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1);
379 RecordPacketReceipt(3, clock_.ApproximateNow());
380 MaybeUpdateAckTimeout(kInstigateAck, 3);
381 CheckAckTimeout(ack_time);
382}
383
ianswett309987e2019-08-02 13:16:26 -0700384TEST_F(UberReceivedPacketManagerTest, SendDelayedMaxAckDelay) {
385 EXPECT_FALSE(HasPendingAck());
386 QuicTime::Delta max_ack_delay = QuicTime::Delta::FromMilliseconds(100);
387 manager_->set_max_ack_delay(max_ack_delay);
388 QuicTime ack_time = clock_.ApproximateNow() + max_ack_delay;
389
390 RecordPacketReceipt(1, clock_.ApproximateNow());
391 MaybeUpdateAckTimeout(kInstigateAck, 1);
392 CheckAckTimeout(ack_time);
393 // Simulate delayed ack alarm firing.
394 clock_.AdvanceTime(max_ack_delay);
395 CheckAckTimeout(clock_.ApproximateNow());
396}
397
QUICHE teamb23daa72019-03-21 08:37:48 -0700398TEST_F(UberReceivedPacketManagerTest, SendDelayedAckDecimation) {
399 EXPECT_FALSE(HasPendingAck());
400 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(), ACK_DECIMATION);
401 // The ack time should be based on min_rtt * 1/4, since it's less than the
402 // default delayed ack time.
403 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.25;
404
405 // Process all the packets in order so there aren't missing packets.
406 uint64_t kFirstDecimatedPacket = 101;
407 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
408 RecordPacketReceipt(i, clock_.ApproximateNow());
409 MaybeUpdateAckTimeout(kInstigateAck, i);
410 if (i % 2 == 0) {
411 // Ack every 2 packets by default.
412 CheckAckTimeout(clock_.ApproximateNow());
413 } else {
414 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
415 }
416 }
417
418 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
419 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
420 CheckAckTimeout(ack_time);
421
422 // The 10th received packet causes an ack to be sent.
423 for (uint64_t i = 1; i < 10; ++i) {
424 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
425 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + i);
426 }
427 CheckAckTimeout(clock_.ApproximateNow());
428}
429
430TEST_F(UberReceivedPacketManagerTest,
431 SendDelayedAckAckDecimationAfterQuiescence) {
432 EXPECT_FALSE(HasPendingAck());
433 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(), ACK_DECIMATION);
434 UberReceivedPacketManagerPeer::SetFastAckAfterQuiescence(manager_.get(),
435 true);
436 // The beginning of the connection counts as quiescence.
437 QuicTime ack_time =
438 clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1);
439 RecordPacketReceipt(1, clock_.ApproximateNow());
440 MaybeUpdateAckTimeout(kInstigateAck, 1);
441 CheckAckTimeout(ack_time);
442 // Simulate delayed ack alarm firing.
443 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
444 CheckAckTimeout(clock_.ApproximateNow());
445
446 // Process another packet immedately after sending the ack and expect the
447 // ack timeout to be set delayed ack time in the future.
448 ack_time = clock_.ApproximateNow() + kDelayedAckTime;
449 RecordPacketReceipt(2, clock_.ApproximateNow());
450 MaybeUpdateAckTimeout(kInstigateAck, 2);
451 CheckAckTimeout(ack_time);
452 // Simulate delayed ack alarm firing.
453 clock_.AdvanceTime(kDelayedAckTime);
454 CheckAckTimeout(clock_.ApproximateNow());
455
456 // Wait 1 second and enesure the ack timeout is set to 1ms in the future.
457 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1));
458 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1);
459 RecordPacketReceipt(3, clock_.ApproximateNow());
460 MaybeUpdateAckTimeout(kInstigateAck, 3);
461 CheckAckTimeout(ack_time);
462 // Process enough packets to get into ack decimation behavior.
463 // The ack time should be based on min_rtt/4, since it's less than the
464 // default delayed ack time.
465 ack_time = clock_.ApproximateNow() + kMinRttMs * 0.25;
466 uint64_t kFirstDecimatedPacket = 101;
467 for (uint64_t i = 4; i < kFirstDecimatedPacket; ++i) {
468 RecordPacketReceipt(i, clock_.ApproximateNow());
469 MaybeUpdateAckTimeout(kInstigateAck, i);
470 if (i % 2 == 0) {
471 // Ack every 2 packets by default.
472 CheckAckTimeout(clock_.ApproximateNow());
473 } else {
474 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
475 }
476 }
477 EXPECT_FALSE(HasPendingAck());
478 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
479 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
480 CheckAckTimeout(ack_time);
481
482 // The 10th received packet causes an ack to be sent.
483 for (uint64_t i = 1; i < 10; ++i) {
484 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
485 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + i);
486 }
487 CheckAckTimeout(clock_.ApproximateNow());
488
489 // Wait 1 second and enesure the ack timeout is set to 1ms in the future.
490 clock_.AdvanceTime(QuicTime::Delta::FromSeconds(1));
491 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(1);
492 RecordPacketReceipt(kFirstDecimatedPacket + 10, clock_.ApproximateNow());
493 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 10);
494 CheckAckTimeout(ack_time);
495}
496
497TEST_F(UberReceivedPacketManagerTest,
498 SendDelayedAckDecimationUnlimitedAggregation) {
499 EXPECT_FALSE(HasPendingAck());
500 QuicConfig config;
501 QuicTagVector connection_options;
502 connection_options.push_back(kACKD);
503 // No limit on the number of packets received before sending an ack.
504 connection_options.push_back(kAKDU);
505 config.SetConnectionOptionsToSend(connection_options);
506 manager_->SetFromConfig(config, Perspective::IS_CLIENT);
507
508 // The ack time should be based on min_rtt/4, since it's less than the
509 // default delayed ack time.
510 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.25;
511
512 // Process all the initial packets in order so there aren't missing packets.
513 uint64_t kFirstDecimatedPacket = 101;
514 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
515 RecordPacketReceipt(i, clock_.ApproximateNow());
516 MaybeUpdateAckTimeout(kInstigateAck, i);
517 if (i % 2 == 0) {
518 // Ack every 2 packets by default.
519 CheckAckTimeout(clock_.ApproximateNow());
520 } else {
521 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
522 }
523 }
524
525 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
526 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
527 CheckAckTimeout(ack_time);
528
529 // 18 packets will not cause an ack to be sent. 19 will because when
530 // stop waiting frames are in use, we ack every 20 packets no matter what.
531 for (int i = 1; i <= 18; ++i) {
532 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
533 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + i);
534 }
535 CheckAckTimeout(ack_time);
536}
537
538TEST_F(UberReceivedPacketManagerTest, SendDelayedAckDecimationEighthRtt) {
539 EXPECT_FALSE(HasPendingAck());
540 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(), ACK_DECIMATION);
541 UberReceivedPacketManagerPeer::SetAckDecimationDelay(manager_.get(), 0.125);
542
543 // The ack time should be based on min_rtt/8, since it's less than the
544 // default delayed ack time.
545 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.125;
546
547 // Process all the packets in order so there aren't missing packets.
548 uint64_t kFirstDecimatedPacket = 101;
549 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
550 RecordPacketReceipt(i, clock_.ApproximateNow());
551 MaybeUpdateAckTimeout(kInstigateAck, i);
552 if (i % 2 == 0) {
553 // Ack every 2 packets by default.
554 CheckAckTimeout(clock_.ApproximateNow());
555 } else {
556 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
557 }
558 }
559
560 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
561 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
562 CheckAckTimeout(ack_time);
563
564 // The 10th received packet causes an ack to be sent.
565 for (uint64_t i = 1; i < 10; ++i) {
566 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
567 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + i);
568 }
569 CheckAckTimeout(clock_.ApproximateNow());
570}
571
572TEST_F(UberReceivedPacketManagerTest, SendDelayedAckDecimationWithReordering) {
573 EXPECT_FALSE(HasPendingAck());
574 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(),
575 ACK_DECIMATION_WITH_REORDERING);
576
577 // The ack time should be based on min_rtt/4, since it's less than the
578 // default delayed ack time.
579 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.25;
580 // Process all the packets in order so there aren't missing packets.
581 uint64_t kFirstDecimatedPacket = 101;
582 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
583 RecordPacketReceipt(i, clock_.ApproximateNow());
584 MaybeUpdateAckTimeout(kInstigateAck, i);
585 if (i % 2 == 0) {
586 // Ack every 2 packets by default.
587 CheckAckTimeout(clock_.ApproximateNow());
588 } else {
589 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
590 }
591 }
592
593 // Receive one packet out of order and then the rest in order.
594 // The loop leaves a one packet gap between acks sent to simulate some loss.
595 for (int j = 0; j < 3; ++j) {
596 // Process packet 10 first and ensure the timeout is one eighth min_rtt.
597 RecordPacketReceipt(kFirstDecimatedPacket + 9 + (j * 11),
598 clock_.ApproximateNow());
599 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 9 + (j * 11));
600 ack_time = clock_.ApproximateNow() + QuicTime::Delta::FromMilliseconds(5);
601 CheckAckTimeout(ack_time);
602
603 // The 10th received packet causes an ack to be sent.
604 for (int i = 0; i < 9; ++i) {
605 RecordPacketReceipt(kFirstDecimatedPacket + i + (j * 11),
606 clock_.ApproximateNow());
607 MaybeUpdateAckTimeout(kInstigateAck,
608 kFirstDecimatedPacket + i + (j * 11));
609 }
610 CheckAckTimeout(clock_.ApproximateNow());
611 }
612}
613
614TEST_F(UberReceivedPacketManagerTest,
615 SendDelayedAckDecimationWithLargeReordering) {
616 EXPECT_FALSE(HasPendingAck());
617 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(),
618 ACK_DECIMATION_WITH_REORDERING);
619 // The ack time should be based on min_rtt/4, since it's less than the
620 // default delayed ack time.
621 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.25;
622
623 // Process all the packets in order so there aren't missing packets.
624 uint64_t kFirstDecimatedPacket = 101;
625 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
626 RecordPacketReceipt(i, clock_.ApproximateNow());
627 MaybeUpdateAckTimeout(kInstigateAck, i);
628 if (i % 2 == 0) {
629 // Ack every 2 packets by default.
630 CheckAckTimeout(clock_.ApproximateNow());
631 } else {
632 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
633 }
634 }
635
636 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
637 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
638 CheckAckTimeout(ack_time);
639
640 RecordPacketReceipt(kFirstDecimatedPacket + 19, clock_.ApproximateNow());
641 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 19);
642 ack_time = clock_.ApproximateNow() + kMinRttMs * 0.125;
643 CheckAckTimeout(ack_time);
644
645 // The 10th received packet causes an ack to be sent.
646 for (int i = 1; i < 9; ++i) {
647 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
648 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + i);
649 }
650 CheckAckTimeout(clock_.ApproximateNow());
651
652 // The next packet received in order will cause an immediate ack, because it
653 // fills a hole.
654 RecordPacketReceipt(kFirstDecimatedPacket + 10, clock_.ApproximateNow());
655 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 10);
656 CheckAckTimeout(clock_.ApproximateNow());
657}
658
659TEST_F(UberReceivedPacketManagerTest,
660 SendDelayedAckDecimationWithReorderingEighthRtt) {
661 EXPECT_FALSE(HasPendingAck());
662 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(),
663 ACK_DECIMATION_WITH_REORDERING);
664 UberReceivedPacketManagerPeer::SetAckDecimationDelay(manager_.get(), 0.125);
665 // The ack time should be based on min_rtt/8, since it's less than the
666 // default delayed ack time.
667 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.125;
668
669 // Process all the packets in order so there aren't missing packets.
670 uint64_t kFirstDecimatedPacket = 101;
671 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
672 RecordPacketReceipt(i, clock_.ApproximateNow());
673 MaybeUpdateAckTimeout(kInstigateAck, i);
674 if (i % 2 == 0) {
675 // Ack every 2 packets by default.
676 CheckAckTimeout(clock_.ApproximateNow());
677 } else {
678 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
679 }
680 }
681
682 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
683 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
684 CheckAckTimeout(ack_time);
685
686 // Process packet 10 first and ensure the timeout is one eighth min_rtt.
687 RecordPacketReceipt(kFirstDecimatedPacket + 9, clock_.ApproximateNow());
688 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 9);
689 CheckAckTimeout(ack_time);
690
691 // The 10th received packet causes an ack to be sent.
692 for (int i = 1; i < 9; ++i) {
693 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
694 MaybeUpdateAckTimeout(kInstigateAck + i, kFirstDecimatedPacket);
695 }
696 CheckAckTimeout(clock_.ApproximateNow());
697}
698
699TEST_F(UberReceivedPacketManagerTest,
700 SendDelayedAckDecimationWithLargeReorderingEighthRtt) {
701 EXPECT_FALSE(HasPendingAck());
702 UberReceivedPacketManagerPeer::SetAckMode(manager_.get(),
703 ACK_DECIMATION_WITH_REORDERING);
704 UberReceivedPacketManagerPeer::SetAckDecimationDelay(manager_.get(), 0.125);
705
706 // The ack time should be based on min_rtt/8, since it's less than the
707 // default delayed ack time.
708 QuicTime ack_time = clock_.ApproximateNow() + kMinRttMs * 0.125;
709 // Process all the packets in order so there aren't missing packets.
710 uint64_t kFirstDecimatedPacket = 101;
711 for (uint64_t i = 1; i < kFirstDecimatedPacket; ++i) {
712 RecordPacketReceipt(i, clock_.ApproximateNow());
713 MaybeUpdateAckTimeout(kInstigateAck, i);
714 if (i % 2 == 0) {
715 // Ack every 2 packets by default.
716 CheckAckTimeout(clock_.ApproximateNow());
717 } else {
718 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
719 }
720 }
721
722 RecordPacketReceipt(kFirstDecimatedPacket, clock_.ApproximateNow());
723 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket);
724 CheckAckTimeout(ack_time);
725
726 RecordPacketReceipt(kFirstDecimatedPacket + 19, clock_.ApproximateNow());
727 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 19);
728 CheckAckTimeout(ack_time);
729
730 // The 10th received packet causes an ack to be sent.
731 for (int i = 1; i < 9; ++i) {
732 RecordPacketReceipt(kFirstDecimatedPacket + i, clock_.ApproximateNow());
733 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + i);
734 }
735 CheckAckTimeout(clock_.ApproximateNow());
736
737 // The next packet received in order will cause an immediate ack, because it
738 // fills a hole.
739 RecordPacketReceipt(kFirstDecimatedPacket + 10, clock_.ApproximateNow());
740 MaybeUpdateAckTimeout(kInstigateAck, kFirstDecimatedPacket + 10);
741 CheckAckTimeout(clock_.ApproximateNow());
742}
743
QUICHE team1dfa46b2019-03-22 10:39:10 -0700744TEST_F(UberReceivedPacketManagerTest,
745 DontWaitForPacketsBeforeMultiplePacketNumberSpaces) {
746 manager_->EnableMultiplePacketNumberSpacesSupport();
747 EXPECT_FALSE(
748 manager_->GetLargestObserved(ENCRYPTION_HANDSHAKE).IsInitialized());
749 EXPECT_FALSE(
750 manager_->GetLargestObserved(ENCRYPTION_FORWARD_SECURE).IsInitialized());
751 RecordPacketReceipt(ENCRYPTION_HANDSHAKE, 2);
752 RecordPacketReceipt(ENCRYPTION_HANDSHAKE, 4);
753 RecordPacketReceipt(ENCRYPTION_FORWARD_SECURE, 3);
754 RecordPacketReceipt(ENCRYPTION_FORWARD_SECURE, 7);
755 EXPECT_EQ(QuicPacketNumber(4),
756 manager_->GetLargestObserved(ENCRYPTION_HANDSHAKE));
757 EXPECT_EQ(QuicPacketNumber(7),
758 manager_->GetLargestObserved(ENCRYPTION_FORWARD_SECURE));
759
760 EXPECT_TRUE(
761 manager_->IsAwaitingPacket(ENCRYPTION_HANDSHAKE, QuicPacketNumber(3)));
762 EXPECT_FALSE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
763 QuicPacketNumber(3)));
764 EXPECT_TRUE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
765 QuicPacketNumber(4)));
766
767 manager_->DontWaitForPacketsBefore(ENCRYPTION_FORWARD_SECURE,
768 QuicPacketNumber(5));
769 EXPECT_TRUE(
770 manager_->IsAwaitingPacket(ENCRYPTION_HANDSHAKE, QuicPacketNumber(3)));
771 EXPECT_FALSE(manager_->IsAwaitingPacket(ENCRYPTION_FORWARD_SECURE,
772 QuicPacketNumber(4)));
773}
774
775TEST_F(UberReceivedPacketManagerTest, AckSendingDifferentPacketNumberSpaces) {
776 manager_->EnableMultiplePacketNumberSpacesSupport();
QUICHE team1dfa46b2019-03-22 10:39:10 -0700777 EXPECT_FALSE(HasPendingAck());
778 EXPECT_FALSE(manager_->IsAckFrameUpdated());
779
780 RecordPacketReceipt(ENCRYPTION_HANDSHAKE, 3);
781 EXPECT_TRUE(manager_->IsAckFrameUpdated());
782 MaybeUpdateAckTimeout(kInstigateAck, ENCRYPTION_HANDSHAKE, 3);
783 EXPECT_TRUE(HasPendingAck());
784 // Delayed ack is scheduled.
ianswett309987e2019-08-02 13:16:26 -0700785 CheckAckTimeout(clock_.ApproximateNow() +
786 QuicTime::Delta::FromMilliseconds(1));
787 // Send delayed handshake data ACK.
788 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
789 CheckAckTimeout(clock_.ApproximateNow());
790 EXPECT_FALSE(HasPendingAck());
QUICHE team1dfa46b2019-03-22 10:39:10 -0700791
792 RecordPacketReceipt(ENCRYPTION_FORWARD_SECURE, 3);
793 MaybeUpdateAckTimeout(kInstigateAck, ENCRYPTION_FORWARD_SECURE, 3);
794 EXPECT_TRUE(HasPendingAck());
795 // Delayed ack is scheduled.
796 CheckAckTimeout(clock_.ApproximateNow() + kDelayedAckTime);
797
798 RecordPacketReceipt(ENCRYPTION_FORWARD_SECURE, 2);
799 MaybeUpdateAckTimeout(kInstigateAck, ENCRYPTION_FORWARD_SECURE, 2);
800 // Application data ACK should be sent immediately.
801 CheckAckTimeout(clock_.ApproximateNow());
QUICHE team1dfa46b2019-03-22 10:39:10 -0700802 EXPECT_FALSE(HasPendingAck());
803}
804
QUICHE teamb23daa72019-03-21 08:37:48 -0700805} // namespace
806} // namespace test
807} // namespace quic