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