blob: 331a0e8842ee4afefc5ebce99a42a8b45fa7cc85 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright 2014 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/quic_unacked_packet_map.h"
6#include <limits>
7
8#include "net/third_party/quiche/src/quic/core/frames/quic_stream_frame.h"
9#include "net/third_party/quiche/src/quic/core/quic_transmission_info.h"
10#include "net/third_party/quiche/src/quic/core/quic_utils.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
13#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
14#include "net/third_party/quiche/src/quic/test_tools/quic_unacked_packet_map_peer.h"
15
16using testing::_;
17using testing::Return;
18using testing::StrictMock;
19
20namespace quic {
21namespace test {
22namespace {
23
24// Default packet length.
25const uint32_t kDefaultLength = 1000;
26
27struct TestParams {
28 TestParams(Perspective perspective, bool session_decides_what_to_write)
29 : perspective(perspective),
30 session_decides_what_to_write(session_decides_what_to_write) {}
31
32 friend std::ostream& operator<<(std::ostream& os, const TestParams& p) {
33 os << "{ Perspective: " << p.perspective
34 << " session_decides_what_to_write: " << p.session_decides_what_to_write
35 << " }";
36 return os;
37 }
38
39 Perspective perspective;
40 bool session_decides_what_to_write;
41};
42
43std::vector<TestParams> GetTestParams() {
44 std::vector<TestParams> params;
45 for (Perspective perspective :
46 {Perspective::IS_CLIENT, Perspective::IS_SERVER}) {
47 for (bool session_decides_what_to_write : {true, false}) {
48 params.push_back(TestParams(perspective, session_decides_what_to_write));
49 }
50 }
51 return params;
52}
53
54class QuicUnackedPacketMapTest : public QuicTestWithParam<TestParams> {
55 protected:
56 QuicUnackedPacketMapTest()
57 : unacked_packets_(GetParam().perspective),
58 now_(QuicTime::Zero() + QuicTime::Delta::FromMilliseconds(1000)) {
59 unacked_packets_.SetSessionNotifier(&notifier_);
60 unacked_packets_.SetSessionDecideWhatToWrite(
61 GetParam().session_decides_what_to_write);
62 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(true));
63 EXPECT_CALL(notifier_, OnStreamFrameRetransmitted(_))
64 .Times(testing::AnyNumber());
65 }
66
67 ~QuicUnackedPacketMapTest() override {}
68
69 SerializedPacket CreateRetransmittablePacket(uint64_t packet_number) {
70 return CreateRetransmittablePacketForStream(
71 packet_number, QuicUtils::GetHeadersStreamId(
72 CurrentSupportedVersions()[0].transport_version));
73 }
74
75 SerializedPacket CreateRetransmittablePacketForStream(
76 uint64_t packet_number,
77 QuicStreamId stream_id) {
78 SerializedPacket packet(QuicPacketNumber(packet_number),
79 PACKET_1BYTE_PACKET_NUMBER, nullptr, kDefaultLength,
80 false, false);
81 QuicStreamFrame frame;
82 frame.stream_id = stream_id;
83 packet.retransmittable_frames.push_back(QuicFrame(frame));
84 return packet;
85 }
86
87 SerializedPacket CreateNonRetransmittablePacket(uint64_t packet_number) {
88 return SerializedPacket(QuicPacketNumber(packet_number),
89 PACKET_1BYTE_PACKET_NUMBER, nullptr, kDefaultLength,
90 false, false);
91 }
92
93 void VerifyInFlightPackets(uint64_t* packets, size_t num_packets) {
94 unacked_packets_.RemoveObsoletePackets();
95 if (num_packets == 0) {
96 EXPECT_FALSE(unacked_packets_.HasInFlightPackets());
97 EXPECT_FALSE(unacked_packets_.HasMultipleInFlightPackets());
98 return;
99 }
100 if (num_packets == 1) {
101 EXPECT_TRUE(unacked_packets_.HasInFlightPackets());
102 EXPECT_FALSE(unacked_packets_.HasMultipleInFlightPackets());
103 ASSERT_TRUE(unacked_packets_.IsUnacked(QuicPacketNumber(packets[0])));
104 EXPECT_TRUE(
105 unacked_packets_.GetTransmissionInfo(QuicPacketNumber(packets[0]))
106 .in_flight);
107 }
108 for (size_t i = 0; i < num_packets; ++i) {
109 ASSERT_TRUE(unacked_packets_.IsUnacked(QuicPacketNumber(packets[i])));
110 EXPECT_TRUE(
111 unacked_packets_.GetTransmissionInfo(QuicPacketNumber(packets[i]))
112 .in_flight);
113 }
114 size_t in_flight_count = 0;
115 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
116 it != unacked_packets_.end(); ++it) {
117 if (it->in_flight) {
118 ++in_flight_count;
119 }
120 }
121 EXPECT_EQ(num_packets, in_flight_count);
122 }
123
124 void VerifyUnackedPackets(uint64_t* packets, size_t num_packets) {
125 unacked_packets_.RemoveObsoletePackets();
126 if (num_packets == 0) {
127 EXPECT_TRUE(unacked_packets_.empty());
128 if (!GetQuicReloadableFlag(quic_optimize_inflight_check)) {
129 EXPECT_FALSE(unacked_packets_.HasUnackedRetransmittableFrames());
130 }
131 return;
132 }
133 EXPECT_FALSE(unacked_packets_.empty());
134 for (size_t i = 0; i < num_packets; ++i) {
135 EXPECT_TRUE(unacked_packets_.IsUnacked(QuicPacketNumber(packets[i])))
136 << packets[i];
137 }
138 EXPECT_EQ(num_packets, unacked_packets_.GetNumUnackedPacketsDebugOnly());
139 }
140
141 void VerifyRetransmittablePackets(uint64_t* packets, size_t num_packets) {
142 unacked_packets_.RemoveObsoletePackets();
143 size_t num_retransmittable_packets = 0;
144 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
145 it != unacked_packets_.end(); ++it) {
146 if (unacked_packets_.HasRetransmittableFrames(*it)) {
147 ++num_retransmittable_packets;
148 }
149 }
150 EXPECT_EQ(num_packets, num_retransmittable_packets);
151 for (size_t i = 0; i < num_packets; ++i) {
152 EXPECT_TRUE(unacked_packets_.HasRetransmittableFrames(
153 QuicPacketNumber(packets[i])))
154 << " packets[" << i << "]:" << packets[i];
155 }
156 }
157
158 void UpdatePacketState(uint64_t packet_number, SentPacketState state) {
159 unacked_packets_
160 .GetMutableTransmissionInfo(QuicPacketNumber(packet_number))
161 ->state = state;
162 }
163
164 void RetransmitAndSendPacket(uint64_t old_packet_number,
165 uint64_t new_packet_number,
166 TransmissionType transmission_type) {
167 DCHECK(unacked_packets_.HasRetransmittableFrames(
168 QuicPacketNumber(old_packet_number)));
169 if (!unacked_packets_.session_decides_what_to_write()) {
170 SerializedPacket packet(
171 CreateNonRetransmittablePacket(new_packet_number));
172 unacked_packets_.AddSentPacket(&packet,
173 QuicPacketNumber(old_packet_number),
174 transmission_type, now_, true);
175 return;
176 }
177 QuicTransmissionInfo* info = unacked_packets_.GetMutableTransmissionInfo(
178 QuicPacketNumber(old_packet_number));
179 QuicStreamId stream_id = QuicUtils::GetHeadersStreamId(
180 CurrentSupportedVersions()[0].transport_version);
181 for (const auto& frame : info->retransmittable_frames) {
182 if (frame.type == STREAM_FRAME) {
183 stream_id = frame.stream_frame.stream_id;
184 break;
185 }
186 }
187 UpdatePacketState(
188 old_packet_number,
189 QuicUtils::RetransmissionTypeToPacketState(transmission_type));
190 info->retransmission = QuicPacketNumber(new_packet_number);
191 SerializedPacket packet(
192 CreateRetransmittablePacketForStream(new_packet_number, stream_id));
193 unacked_packets_.AddSentPacket(&packet, QuicPacketNumber(),
194 transmission_type, now_, true);
195 }
196 QuicUnackedPacketMap unacked_packets_;
197 QuicTime now_;
198 StrictMock<MockSessionNotifier> notifier_;
199};
200
201INSTANTIATE_TEST_SUITE_P(Tests,
202 QuicUnackedPacketMapTest,
203 ::testing::ValuesIn(GetTestParams()));
204
205TEST_P(QuicUnackedPacketMapTest, RttOnly) {
206 // Acks are only tracked for RTT measurement purposes.
207 SerializedPacket packet(CreateNonRetransmittablePacket(1));
208 unacked_packets_.AddSentPacket(&packet, QuicPacketNumber(),
209 NOT_RETRANSMISSION, now_, false);
210
211 uint64_t unacked[] = {1};
212 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
213 VerifyInFlightPackets(nullptr, 0);
214 VerifyRetransmittablePackets(nullptr, 0);
215
216 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(1));
217 VerifyUnackedPackets(nullptr, 0);
218 VerifyInFlightPackets(nullptr, 0);
219 VerifyRetransmittablePackets(nullptr, 0);
220}
221
222TEST_P(QuicUnackedPacketMapTest, RetransmittableInflightAndRtt) {
223 // Simulate a retransmittable packet being sent and acked.
224 SerializedPacket packet(CreateRetransmittablePacket(1));
225 unacked_packets_.AddSentPacket(&packet, QuicPacketNumber(),
226 NOT_RETRANSMISSION, now_, true);
227
228 uint64_t unacked[] = {1};
229 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
230 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
231 VerifyRetransmittablePackets(unacked, QUIC_ARRAYSIZE(unacked));
232
233 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(1));
234 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
235 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
236 VerifyRetransmittablePackets(nullptr, 0);
237
238 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(1));
239 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
240 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
241 VerifyRetransmittablePackets(nullptr, 0);
242
243 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(1));
244 VerifyUnackedPackets(nullptr, 0);
245 VerifyInFlightPackets(nullptr, 0);
246 VerifyRetransmittablePackets(nullptr, 0);
247}
248
249TEST_P(QuicUnackedPacketMapTest, StopRetransmission) {
250 const QuicStreamId stream_id = 2;
251 SerializedPacket packet(CreateRetransmittablePacketForStream(1, stream_id));
252 unacked_packets_.AddSentPacket(&packet, QuicPacketNumber(),
253 NOT_RETRANSMISSION, now_, true);
254
255 uint64_t unacked[] = {1};
256 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
257 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
258 uint64_t retransmittable[] = {1};
259 VerifyRetransmittablePackets(retransmittable,
260 QUIC_ARRAYSIZE(retransmittable));
261
262 if (unacked_packets_.session_decides_what_to_write()) {
263 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
264 } else {
265 unacked_packets_.CancelRetransmissionsForStream(stream_id);
266 }
267 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
268 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
269 VerifyRetransmittablePackets(nullptr, 0);
270}
271
272TEST_P(QuicUnackedPacketMapTest, StopRetransmissionOnOtherStream) {
273 const QuicStreamId stream_id = 2;
274 SerializedPacket packet(CreateRetransmittablePacketForStream(1, stream_id));
275 unacked_packets_.AddSentPacket(&packet, QuicPacketNumber(),
276 NOT_RETRANSMISSION, now_, true);
277
278 uint64_t unacked[] = {1};
279 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
280 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
281 uint64_t retransmittable[] = {1};
282 VerifyRetransmittablePackets(retransmittable,
283 QUIC_ARRAYSIZE(retransmittable));
284
285 // Stop retransmissions on another stream and verify the packet is unchanged.
286 if (!unacked_packets_.session_decides_what_to_write()) {
287 unacked_packets_.CancelRetransmissionsForStream(stream_id + 2);
288 }
289 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
290 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
291 VerifyRetransmittablePackets(retransmittable,
292 QUIC_ARRAYSIZE(retransmittable));
293}
294
295TEST_P(QuicUnackedPacketMapTest, StopRetransmissionAfterRetransmission) {
296 const QuicStreamId stream_id = 2;
297 SerializedPacket packet1(CreateRetransmittablePacketForStream(1, stream_id));
298 unacked_packets_.AddSentPacket(&packet1, QuicPacketNumber(),
299 NOT_RETRANSMISSION, now_, true);
300 RetransmitAndSendPacket(1, 2, LOSS_RETRANSMISSION);
301
302 uint64_t unacked[] = {1, 2};
303 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
304 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
305 std::vector<uint64_t> retransmittable;
306 if (unacked_packets_.session_decides_what_to_write()) {
307 retransmittable = {1, 2};
308 } else {
309 retransmittable = {2};
310 }
311 VerifyRetransmittablePackets(&retransmittable[0], retransmittable.size());
312
313 if (unacked_packets_.session_decides_what_to_write()) {
314 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
315 } else {
316 unacked_packets_.CancelRetransmissionsForStream(stream_id);
317 }
318 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
319 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
320 VerifyRetransmittablePackets(nullptr, 0);
321}
322
323TEST_P(QuicUnackedPacketMapTest, RetransmittedPacket) {
324 // Simulate a retransmittable packet being sent, retransmitted, and the first
325 // transmission being acked.
326 SerializedPacket packet1(CreateRetransmittablePacket(1));
327 unacked_packets_.AddSentPacket(&packet1, QuicPacketNumber(),
328 NOT_RETRANSMISSION, now_, true);
329 RetransmitAndSendPacket(1, 2, LOSS_RETRANSMISSION);
330
331 uint64_t unacked[] = {1, 2};
332 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
333 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
334 std::vector<uint64_t> retransmittable;
335 if (unacked_packets_.session_decides_what_to_write()) {
336 retransmittable = {1, 2};
337 } else {
338 retransmittable = {2};
339 }
340 VerifyRetransmittablePackets(&retransmittable[0], retransmittable.size());
341
342 EXPECT_CALL(notifier_, IsFrameOutstanding(_)).WillRepeatedly(Return(false));
343 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(1));
344 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
345 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
346 VerifyRetransmittablePackets(nullptr, 0);
347
348 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(2));
349 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
350 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
351 VerifyRetransmittablePackets(nullptr, 0);
352
353 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(2));
354 uint64_t unacked2[] = {1};
355 VerifyUnackedPackets(unacked2, QUIC_ARRAYSIZE(unacked2));
356 VerifyInFlightPackets(unacked2, QUIC_ARRAYSIZE(unacked2));
357 VerifyRetransmittablePackets(nullptr, 0);
358
359 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(1));
360 VerifyUnackedPackets(nullptr, 0);
361 VerifyInFlightPackets(nullptr, 0);
362 VerifyRetransmittablePackets(nullptr, 0);
363}
364
365TEST_P(QuicUnackedPacketMapTest, RetransmitThreeTimes) {
366 // Simulate a retransmittable packet being sent and retransmitted twice.
367 SerializedPacket packet1(CreateRetransmittablePacket(1));
368 unacked_packets_.AddSentPacket(&packet1, QuicPacketNumber(),
369 NOT_RETRANSMISSION, now_, true);
370 SerializedPacket packet2(CreateRetransmittablePacket(2));
371 unacked_packets_.AddSentPacket(&packet2, QuicPacketNumber(),
372 NOT_RETRANSMISSION, now_, true);
373
374 uint64_t unacked[] = {1, 2};
375 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
376 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
377 uint64_t retransmittable[] = {1, 2};
378 VerifyRetransmittablePackets(retransmittable,
379 QUIC_ARRAYSIZE(retransmittable));
380
381 // Early retransmit 1 as 3 and send new data as 4.
382 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(2));
383 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(2));
384 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(2));
385 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(1));
386 RetransmitAndSendPacket(1, 3, LOSS_RETRANSMISSION);
387 SerializedPacket packet4(CreateRetransmittablePacket(4));
388 unacked_packets_.AddSentPacket(&packet4, QuicPacketNumber(),
389 NOT_RETRANSMISSION, now_, true);
390
391 uint64_t unacked2[] = {1, 3, 4};
392 VerifyUnackedPackets(unacked2, QUIC_ARRAYSIZE(unacked2));
393 uint64_t pending2[] = {3, 4};
394 VerifyInFlightPackets(pending2, QUIC_ARRAYSIZE(pending2));
395 std::vector<uint64_t> retransmittable2;
396 if (unacked_packets_.session_decides_what_to_write()) {
397 retransmittable2 = {1, 3, 4};
398 } else {
399 retransmittable2 = {3, 4};
400 }
401 VerifyRetransmittablePackets(&retransmittable2[0], retransmittable2.size());
402
403 // Early retransmit 3 (formerly 1) as 5, and remove 1 from unacked.
404 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(4));
405 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(4));
406 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(4));
407 RetransmitAndSendPacket(3, 5, LOSS_RETRANSMISSION);
408 SerializedPacket packet6(CreateRetransmittablePacket(6));
409 unacked_packets_.AddSentPacket(&packet6, QuicPacketNumber(),
410 NOT_RETRANSMISSION, now_, true);
411
412 std::vector<uint64_t> unacked3;
413 std::vector<uint64_t> retransmittable3;
414 if (unacked_packets_.session_decides_what_to_write()) {
415 unacked3 = {3, 5, 6};
416 retransmittable3 = {3, 5, 6};
417 } else {
418 unacked3 = {3, 5, 6};
419 retransmittable3 = {5, 6};
420 }
421 VerifyUnackedPackets(&unacked3[0], unacked3.size());
422 VerifyRetransmittablePackets(&retransmittable3[0], retransmittable3.size());
423 uint64_t pending3[] = {3, 5, 6};
424 VerifyInFlightPackets(pending3, QUIC_ARRAYSIZE(pending3));
425
426 // Early retransmit 5 as 7 and ensure in flight packet 3 is not removed.
427 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(6));
428 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(6));
429 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(6));
430 RetransmitAndSendPacket(5, 7, LOSS_RETRANSMISSION);
431
432 std::vector<uint64_t> unacked4;
433 std::vector<uint64_t> retransmittable4;
434 if (unacked_packets_.session_decides_what_to_write()) {
435 unacked4 = {3, 5, 7};
436 retransmittable4 = {3, 5, 7};
437 } else {
438 unacked4 = {3, 5, 7};
439 retransmittable4 = {7};
440 }
441 VerifyUnackedPackets(&unacked4[0], unacked4.size());
442 VerifyRetransmittablePackets(&retransmittable4[0], retransmittable4.size());
443 uint64_t pending4[] = {3, 5, 7};
444 VerifyInFlightPackets(pending4, QUIC_ARRAYSIZE(pending4));
445
446 // Remove the older two transmissions from in flight.
447 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(3));
448 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(5));
449 uint64_t pending5[] = {7};
450 VerifyInFlightPackets(pending5, QUIC_ARRAYSIZE(pending5));
451}
452
453TEST_P(QuicUnackedPacketMapTest, RetransmitFourTimes) {
454 // Simulate a retransmittable packet being sent and retransmitted twice.
455 SerializedPacket packet1(CreateRetransmittablePacket(1));
456 unacked_packets_.AddSentPacket(&packet1, QuicPacketNumber(),
457 NOT_RETRANSMISSION, now_, true);
458 SerializedPacket packet2(CreateRetransmittablePacket(2));
459 unacked_packets_.AddSentPacket(&packet2, QuicPacketNumber(),
460 NOT_RETRANSMISSION, now_, true);
461
462 uint64_t unacked[] = {1, 2};
463 VerifyUnackedPackets(unacked, QUIC_ARRAYSIZE(unacked));
464 VerifyInFlightPackets(unacked, QUIC_ARRAYSIZE(unacked));
465 uint64_t retransmittable[] = {1, 2};
466 VerifyRetransmittablePackets(retransmittable,
467 QUIC_ARRAYSIZE(retransmittable));
468
469 // Early retransmit 1 as 3.
470 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(2));
471 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(2));
472 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(2));
473 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(1));
474 RetransmitAndSendPacket(1, 3, LOSS_RETRANSMISSION);
475
476 uint64_t unacked2[] = {1, 3};
477 VerifyUnackedPackets(unacked2, QUIC_ARRAYSIZE(unacked2));
478 uint64_t pending2[] = {3};
479 VerifyInFlightPackets(pending2, QUIC_ARRAYSIZE(pending2));
480 std::vector<uint64_t> retransmittable2;
481 if (unacked_packets_.session_decides_what_to_write()) {
482 retransmittable2 = {1, 3};
483 } else {
484 retransmittable2 = {3};
485 }
486 VerifyRetransmittablePackets(&retransmittable2[0], retransmittable2.size());
487
488 // TLP 3 (formerly 1) as 4, and don't remove 1 from unacked.
489 RetransmitAndSendPacket(3, 4, TLP_RETRANSMISSION);
490 SerializedPacket packet5(CreateRetransmittablePacket(5));
491 unacked_packets_.AddSentPacket(&packet5, QuicPacketNumber(),
492 NOT_RETRANSMISSION, now_, true);
493
494 uint64_t unacked3[] = {1, 3, 4, 5};
495 VerifyUnackedPackets(unacked3, QUIC_ARRAYSIZE(unacked3));
496 uint64_t pending3[] = {3, 4, 5};
497 VerifyInFlightPackets(pending3, QUIC_ARRAYSIZE(pending3));
498 std::vector<uint64_t> retransmittable3;
499 if (unacked_packets_.session_decides_what_to_write()) {
500 retransmittable3 = {1, 3, 4, 5};
501 } else {
502 retransmittable3 = {4, 5};
503 }
504 VerifyRetransmittablePackets(&retransmittable3[0], retransmittable3.size());
505
506 // Early retransmit 4 as 6 and ensure in flight packet 3 is removed.
507 unacked_packets_.IncreaseLargestAcked(QuicPacketNumber(5));
508 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(5));
509 unacked_packets_.RemoveRetransmittability(QuicPacketNumber(5));
510 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(3));
511 unacked_packets_.RemoveFromInFlight(QuicPacketNumber(4));
512 RetransmitAndSendPacket(4, 6, LOSS_RETRANSMISSION);
513
514 std::vector<uint64_t> unacked4;
515 if (unacked_packets_.session_decides_what_to_write()) {
516 unacked4 = {4, 6};
517 } else {
518 unacked4 = {4, 6};
519 }
520 VerifyUnackedPackets(&unacked4[0], unacked4.size());
521 uint64_t pending4[] = {6};
522 VerifyInFlightPackets(pending4, QUIC_ARRAYSIZE(pending4));
523 std::vector<uint64_t> retransmittable4;
524 if (unacked_packets_.session_decides_what_to_write()) {
525 retransmittable4 = {4, 6};
526 } else {
527 retransmittable4 = {6};
528 }
529 VerifyRetransmittablePackets(&retransmittable4[0], retransmittable4.size());
530}
531
532TEST_P(QuicUnackedPacketMapTest, SendWithGap) {
533 // Simulate a retransmittable packet being sent, retransmitted, and the first
534 // transmission being acked.
535 SerializedPacket packet1(CreateRetransmittablePacket(1));
536 unacked_packets_.AddSentPacket(&packet1, QuicPacketNumber(),
537 NOT_RETRANSMISSION, now_, true);
538 SerializedPacket packet3(CreateRetransmittablePacket(3));
539 unacked_packets_.AddSentPacket(&packet3, QuicPacketNumber(),
540 NOT_RETRANSMISSION, now_, true);
541 RetransmitAndSendPacket(3, 5, LOSS_RETRANSMISSION);
542
543 EXPECT_EQ(QuicPacketNumber(1u), unacked_packets_.GetLeastUnacked());
544 EXPECT_TRUE(unacked_packets_.IsUnacked(QuicPacketNumber(1)));
545 EXPECT_FALSE(unacked_packets_.IsUnacked(QuicPacketNumber(2)));
546 EXPECT_TRUE(unacked_packets_.IsUnacked(QuicPacketNumber(3)));
547 EXPECT_FALSE(unacked_packets_.IsUnacked(QuicPacketNumber(4)));
548 EXPECT_TRUE(unacked_packets_.IsUnacked(QuicPacketNumber(5)));
549 EXPECT_EQ(QuicPacketNumber(5u), unacked_packets_.largest_sent_packet());
550}
551
552TEST_P(QuicUnackedPacketMapTest, AggregateContiguousAckedStreamFrames) {
553 testing::InSequence s;
554 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(0);
555 unacked_packets_.NotifyAggregatedStreamFrameAcked(QuicTime::Delta::Zero());
556
557 QuicTransmissionInfo info1;
558 QuicStreamFrame stream_frame1(3, false, 0, 100);
559 info1.retransmittable_frames.push_back(QuicFrame(stream_frame1));
560
561 QuicTransmissionInfo info2;
562 QuicStreamFrame stream_frame2(3, false, 100, 100);
563 info2.retransmittable_frames.push_back(QuicFrame(stream_frame2));
564
565 QuicTransmissionInfo info3;
566 QuicStreamFrame stream_frame3(3, false, 200, 100);
567 info3.retransmittable_frames.push_back(QuicFrame(stream_frame3));
568
569 QuicTransmissionInfo info4;
570 QuicStreamFrame stream_frame4(3, true, 300, 0);
571 info4.retransmittable_frames.push_back(QuicFrame(stream_frame4));
572
573 // Verify stream frames are aggregated.
574 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(0);
575 unacked_packets_.MaybeAggregateAckedStreamFrame(info1,
576 QuicTime::Delta::Zero());
577 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(0);
578 unacked_packets_.MaybeAggregateAckedStreamFrame(info2,
579 QuicTime::Delta::Zero());
580 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(0);
581 unacked_packets_.MaybeAggregateAckedStreamFrame(info3,
582 QuicTime::Delta::Zero());
583
584 // Verify aggregated stream frame gets acked since fin is acked.
585 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(1);
586 unacked_packets_.MaybeAggregateAckedStreamFrame(info4,
587 QuicTime::Delta::Zero());
588}
589
590// Regression test for b/112930090.
591TEST_P(QuicUnackedPacketMapTest, CannotAggregateIfDataLengthOverflow) {
592 QuicByteCount kMaxAggregatedDataLength =
593 std::numeric_limits<decltype(QuicStreamFrame().data_length)>::max();
594 QuicStreamId stream_id = 2;
595
596 // acked_stream_length=512 covers the case where a frame will cause the
597 // aggregated frame length to be exactly 64K.
598 // acked_stream_length=1300 covers the case where a frame will cause the
599 // aggregated frame length to exceed 64K.
600 for (const QuicPacketLength acked_stream_length : {512, 1300}) {
601 ++stream_id;
602 QuicStreamOffset offset = 0;
603 // Expected length of the aggregated stream frame.
604 QuicByteCount aggregated_data_length = 0;
605
606 while (offset < 1e6) {
607 QuicTransmissionInfo info;
608 QuicStreamFrame stream_frame(stream_id, false, offset,
609 acked_stream_length);
610 info.retransmittable_frames.push_back(QuicFrame(stream_frame));
611
612 const QuicStreamFrame& aggregated_stream_frame =
613 QuicUnackedPacketMapPeer::GetAggregatedStreamFrame(unacked_packets_);
614 if (aggregated_stream_frame.data_length + acked_stream_length <=
615 kMaxAggregatedDataLength) {
616 // Verify the acked stream frame can be aggregated.
617 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(0);
618 unacked_packets_.MaybeAggregateAckedStreamFrame(
619 info, QuicTime::Delta::Zero());
620 aggregated_data_length += acked_stream_length;
621 testing::Mock::VerifyAndClearExpectations(&notifier_);
622 } else {
623 // Verify the acked stream frame cannot be aggregated because
624 // data_length is overflow.
625 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(1);
626 unacked_packets_.MaybeAggregateAckedStreamFrame(
627 info, QuicTime::Delta::Zero());
628 aggregated_data_length = acked_stream_length;
629 testing::Mock::VerifyAndClearExpectations(&notifier_);
630 }
631
632 EXPECT_EQ(aggregated_data_length, aggregated_stream_frame.data_length);
633 offset += acked_stream_length;
634 }
635
636 // Ack the last frame of the stream.
637 QuicTransmissionInfo info;
638 QuicStreamFrame stream_frame(stream_id, true, offset, acked_stream_length);
639 info.retransmittable_frames.push_back(QuicFrame(stream_frame));
640 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(1);
641 unacked_packets_.MaybeAggregateAckedStreamFrame(info,
642 QuicTime::Delta::Zero());
643 testing::Mock::VerifyAndClearExpectations(&notifier_);
644 }
645}
646
647TEST_P(QuicUnackedPacketMapTest, CannotAggregateAckedControlFrames) {
648 testing::InSequence s;
649 QuicWindowUpdateFrame window_update(1, 5, 100);
650 QuicStreamFrame stream_frame1(3, false, 0, 100);
651 QuicStreamFrame stream_frame2(3, false, 100, 100);
652 QuicBlockedFrame blocked(2, 5);
653 QuicGoAwayFrame go_away(3, QUIC_PEER_GOING_AWAY, 5, "Going away.");
654
655 QuicTransmissionInfo info1;
656 info1.retransmittable_frames.push_back(QuicFrame(&window_update));
657 info1.retransmittable_frames.push_back(QuicFrame(stream_frame1));
658 info1.retransmittable_frames.push_back(QuicFrame(stream_frame2));
659
660 QuicTransmissionInfo info2;
661 info2.retransmittable_frames.push_back(QuicFrame(&blocked));
662 info2.retransmittable_frames.push_back(QuicFrame(&go_away));
663
664 // Verify 2 contiguous stream frames are aggregated.
665 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(1);
666 unacked_packets_.MaybeAggregateAckedStreamFrame(info1,
667 QuicTime::Delta::Zero());
668 // Verify aggregated stream frame gets acked.
669 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(3);
670 unacked_packets_.MaybeAggregateAckedStreamFrame(info2,
671 QuicTime::Delta::Zero());
672
673 EXPECT_CALL(notifier_, OnFrameAcked(_, _)).Times(0);
674 unacked_packets_.NotifyAggregatedStreamFrameAcked(QuicTime::Delta::Zero());
675}
676
QUICHE teamc279cec2019-03-22 06:51:48 -0700677TEST_P(QuicUnackedPacketMapTest, LargestSentPacketMultiplePacketNumberSpaces) {
678 if (!GetQuicReloadableFlag(quic_use_uber_loss_algorithm)) {
679 return;
680 }
681 unacked_packets_.EnableMultiplePacketNumberSpacesSupport();
682 EXPECT_FALSE(unacked_packets_
683 .GetLargestSentPacketOfPacketNumberSpace(ENCRYPTION_INITIAL)
684 .IsInitialized());
685 // Send packet 1.
686 SerializedPacket packet1(CreateRetransmittablePacket(1));
687 packet1.encryption_level = ENCRYPTION_INITIAL;
688 unacked_packets_.AddSentPacket(&packet1, QuicPacketNumber(),
689 NOT_RETRANSMISSION, now_, true);
690 EXPECT_EQ(QuicPacketNumber(1u), unacked_packets_.largest_sent_packet());
691 EXPECT_EQ(QuicPacketNumber(1),
692 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
693 ENCRYPTION_INITIAL));
694 EXPECT_FALSE(
695 unacked_packets_
696 .GetLargestSentPacketOfPacketNumberSpace(ENCRYPTION_HANDSHAKE)
697 .IsInitialized());
698 // Send packet 2.
699 SerializedPacket packet2(CreateRetransmittablePacket(2));
700 packet2.encryption_level = ENCRYPTION_HANDSHAKE;
701 unacked_packets_.AddSentPacket(&packet2, QuicPacketNumber(),
702 NOT_RETRANSMISSION, now_, true);
703 EXPECT_EQ(QuicPacketNumber(2u), unacked_packets_.largest_sent_packet());
704 EXPECT_EQ(QuicPacketNumber(1),
705 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
706 ENCRYPTION_INITIAL));
707 EXPECT_EQ(QuicPacketNumber(2),
708 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
709 ENCRYPTION_HANDSHAKE));
710 EXPECT_FALSE(unacked_packets_
711 .GetLargestSentPacketOfPacketNumberSpace(ENCRYPTION_ZERO_RTT)
712 .IsInitialized());
713 // Send packet 3.
714 SerializedPacket packet3(CreateRetransmittablePacket(3));
715 packet3.encryption_level = ENCRYPTION_ZERO_RTT;
716 unacked_packets_.AddSentPacket(&packet3, QuicPacketNumber(),
717 NOT_RETRANSMISSION, now_, true);
718 EXPECT_EQ(QuicPacketNumber(3u), unacked_packets_.largest_sent_packet());
719 EXPECT_EQ(QuicPacketNumber(1),
720 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
721 ENCRYPTION_INITIAL));
722 EXPECT_EQ(QuicPacketNumber(2),
723 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
724 ENCRYPTION_HANDSHAKE));
725 EXPECT_EQ(QuicPacketNumber(3),
726 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
727 ENCRYPTION_ZERO_RTT));
728 // Verify forward secure belongs to the same packet number space as encryption
729 // zero rtt.
730 EXPECT_EQ(QuicPacketNumber(3),
731 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
732 ENCRYPTION_FORWARD_SECURE));
733
734 // Send packet 4.
735 SerializedPacket packet4(CreateRetransmittablePacket(4));
736 packet4.encryption_level = ENCRYPTION_FORWARD_SECURE;
737 unacked_packets_.AddSentPacket(&packet4, QuicPacketNumber(),
738 NOT_RETRANSMISSION, now_, true);
739 EXPECT_EQ(QuicPacketNumber(4u), unacked_packets_.largest_sent_packet());
740 EXPECT_EQ(QuicPacketNumber(1),
741 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
742 ENCRYPTION_INITIAL));
743 EXPECT_EQ(QuicPacketNumber(2),
744 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
745 ENCRYPTION_HANDSHAKE));
746 EXPECT_EQ(QuicPacketNumber(4),
747 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
748 ENCRYPTION_ZERO_RTT));
749 // Verify forward secure belongs to the same packet number space as encryption
750 // zero rtt.
751 EXPECT_EQ(QuicPacketNumber(4),
752 unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
753 ENCRYPTION_FORWARD_SECURE));
754}
755
QUICHE teama6ef0a62019-03-07 20:34:33 -0500756} // namespace
757} // namespace test
758} // namespace quic