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