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