blob: 3ed881a140db900d5546ede5ec88c31d51975816 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2013 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/congestion_control/pacing_sender.h"
6
7#include <memory>
8
9#include "net/third_party/quiche/src/quic/core/quic_packets.h"
10#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
11#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
15#include "net/third_party/quiche/src/quic/test_tools/mock_clock.h"
16#include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h"
17
18using testing::_;
19using testing::AtMost;
20using testing::IsEmpty;
21using testing::Return;
22using testing::StrictMock;
23
24namespace quic {
25namespace test {
26
27const QuicByteCount kBytesInFlight = 1024;
28const int kInitialBurstPackets = 10;
29
30class PacingSenderTest : public QuicTest {
31 protected:
32 PacingSenderTest()
33 : zero_time_(QuicTime::Delta::Zero()),
34 infinite_time_(QuicTime::Delta::Infinite()),
35 packet_number_(1),
36 mock_sender_(new StrictMock<MockSendAlgorithm>()),
37 pacing_sender_(new PacingSender) {
38 pacing_sender_->set_sender(mock_sender_.get());
39 // Pick arbitrary time.
40 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(9));
41 }
42
43 ~PacingSenderTest() override {}
44
45 void InitPacingRate(QuicPacketCount burst_size, QuicBandwidth bandwidth) {
46 mock_sender_ = QuicMakeUnique<StrictMock<MockSendAlgorithm>>();
47 pacing_sender_ = QuicMakeUnique<PacingSender>();
48 pacing_sender_->set_sender(mock_sender_.get());
49 EXPECT_CALL(*mock_sender_, PacingRate(_)).WillRepeatedly(Return(bandwidth));
wub04a83592019-04-05 06:30:59 -070050 EXPECT_CALL(*mock_sender_, BandwidthEstimate())
51 .WillRepeatedly(Return(bandwidth));
QUICHE teama6ef0a62019-03-07 20:34:33 -050052 if (burst_size == 0) {
53 EXPECT_CALL(*mock_sender_, OnCongestionEvent(_, _, _, _, _));
54 LostPacketVector lost_packets;
55 lost_packets.push_back(LostPacket(QuicPacketNumber(1), kMaxPacketSize));
56 AckedPacketVector empty;
57 pacing_sender_->OnCongestionEvent(true, 1234, clock_.Now(), empty,
58 lost_packets);
59 } else if (burst_size != kInitialBurstPackets) {
60 QUIC_LOG(FATAL) << "Unsupported burst_size " << burst_size
61 << " specificied, only 0 and " << kInitialBurstPackets
62 << " are supported.";
63 }
64 }
65
66 void CheckPacketIsSentImmediately(HasRetransmittableData retransmittable_data,
67 QuicByteCount bytes_in_flight,
68 bool in_recovery,
69 bool cwnd_limited,
70 QuicPacketCount cwnd) {
71 // In order for the packet to be sendable, the underlying sender must
72 // permit it to be sent immediately.
73 for (int i = 0; i < 2; ++i) {
74 EXPECT_CALL(*mock_sender_, CanSend(bytes_in_flight))
75 .WillOnce(Return(true));
76 // Verify that the packet can be sent immediately.
77 EXPECT_EQ(zero_time_,
78 pacing_sender_->TimeUntilSend(clock_.Now(), bytes_in_flight));
79 }
80
81 // Actually send the packet.
82 if (bytes_in_flight == 0) {
83 EXPECT_CALL(*mock_sender_, InRecovery()).WillOnce(Return(in_recovery));
84 }
85 EXPECT_CALL(*mock_sender_,
86 OnPacketSent(clock_.Now(), bytes_in_flight, packet_number_,
87 kMaxPacketSize, retransmittable_data));
88 EXPECT_CALL(*mock_sender_, GetCongestionWindow())
89 .Times(AtMost(1))
90 .WillRepeatedly(Return(cwnd * kDefaultTCPMSS));
91 EXPECT_CALL(*mock_sender_, CanSend(bytes_in_flight + kMaxPacketSize))
92 .Times(AtMost(1))
93 .WillRepeatedly(Return(!cwnd_limited));
94 pacing_sender_->OnPacketSent(clock_.Now(), bytes_in_flight,
95 packet_number_++, kMaxPacketSize,
96 retransmittable_data);
97 }
98
99 void CheckPacketIsSentImmediately() {
100 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight,
101 false, false, 10);
102 }
103
104 void CheckPacketIsDelayed(QuicTime::Delta delay) {
105 // In order for the packet to be sendable, the underlying sender must
106 // permit it to be sent immediately.
107 for (int i = 0; i < 2; ++i) {
108 EXPECT_CALL(*mock_sender_, CanSend(kBytesInFlight))
109 .WillOnce(Return(true));
110 // Verify that the packet is delayed.
111 EXPECT_EQ(delay.ToMicroseconds(),
112 pacing_sender_->TimeUntilSend(clock_.Now(), kBytesInFlight)
113 .ToMicroseconds());
114 }
115 }
116
117 void UpdateRtt() {
118 EXPECT_CALL(*mock_sender_,
119 OnCongestionEvent(true, kBytesInFlight, _, _, _));
120 AckedPacketVector empty_acked;
121 LostPacketVector empty_lost;
122 pacing_sender_->OnCongestionEvent(true, kBytesInFlight, clock_.Now(),
123 empty_acked, empty_lost);
124 }
125
126 void OnApplicationLimited() { pacing_sender_->OnApplicationLimited(); }
127
128 const QuicTime::Delta zero_time_;
129 const QuicTime::Delta infinite_time_;
130 MockClock clock_;
131 QuicPacketNumber packet_number_;
132 std::unique_ptr<StrictMock<MockSendAlgorithm>> mock_sender_;
133 std::unique_ptr<PacingSender> pacing_sender_;
134};
135
136TEST_F(PacingSenderTest, NoSend) {
137 for (int i = 0; i < 2; ++i) {
138 EXPECT_CALL(*mock_sender_, CanSend(kBytesInFlight)).WillOnce(Return(false));
139 EXPECT_EQ(infinite_time_,
140 pacing_sender_->TimeUntilSend(clock_.Now(), kBytesInFlight));
141 }
142}
143
144TEST_F(PacingSenderTest, SendNow) {
145 for (int i = 0; i < 2; ++i) {
146 EXPECT_CALL(*mock_sender_, CanSend(kBytesInFlight)).WillOnce(Return(true));
147 EXPECT_EQ(zero_time_,
148 pacing_sender_->TimeUntilSend(clock_.Now(), kBytesInFlight));
149 }
150}
151
152TEST_F(PacingSenderTest, VariousSending) {
153 // Configure pacing rate of 1 packet per 1 ms, no initial burst.
154 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
155 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
156
157 // Now update the RTT and verify that packets are actually paced.
158 UpdateRtt();
159
160 CheckPacketIsSentImmediately();
161 CheckPacketIsSentImmediately();
162
163 // The first packet was a "make up", then we sent two packets "into the
164 // future", so the delay should be 2.
165 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
166
167 // Wake up on time.
168 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
169 CheckPacketIsSentImmediately();
170 CheckPacketIsSentImmediately();
171 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
172
173 // Wake up late.
174 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(4));
175 CheckPacketIsSentImmediately();
176 CheckPacketIsSentImmediately();
177 CheckPacketIsSentImmediately();
178 CheckPacketIsSentImmediately();
179 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
180
181 // Wake up really late.
182 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
183 CheckPacketIsSentImmediately();
184 CheckPacketIsSentImmediately();
185 CheckPacketIsSentImmediately();
186 CheckPacketIsSentImmediately();
187 CheckPacketIsSentImmediately();
188 CheckPacketIsSentImmediately();
189 CheckPacketIsSentImmediately();
190 CheckPacketIsSentImmediately();
191 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
192
193 // Wake up really late again, but application pause partway through.
194 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(8));
195 CheckPacketIsSentImmediately();
196 CheckPacketIsSentImmediately();
197 OnApplicationLimited();
198 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
199 CheckPacketIsSentImmediately();
200 CheckPacketIsSentImmediately();
201 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
202 // Wake up early, but after enough time has passed to permit a send.
203 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1));
204 CheckPacketIsSentImmediately();
205}
206
207TEST_F(PacingSenderTest, InitialBurst) {
208 // Configure pacing rate of 1 packet per 1 ms.
209 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
210 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
211
212 // Update the RTT and verify that the first 10 packets aren't paced.
213 UpdateRtt();
214
215 // Send 10 packets, and verify that they are not paced.
216 for (int i = 0; i < kInitialBurstPackets; ++i) {
217 CheckPacketIsSentImmediately();
218 }
219
220 // The first packet was a "make up", then we sent two packets "into the
221 // future", so the delay should be 2ms.
222 CheckPacketIsSentImmediately();
223 CheckPacketIsSentImmediately();
224 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
225
226 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
227 CheckPacketIsSentImmediately();
228
229 // Next time TimeUntilSend is called with no bytes in flight, pacing should
230 // allow a packet to be sent, and when it's sent, the tokens are refilled.
231 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false, false, 10);
232 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
233 CheckPacketIsSentImmediately();
234 }
235
236 // The first packet was a "make up", then we sent two packets "into the
237 // future", so the delay should be 2ms.
238 CheckPacketIsSentImmediately();
239 CheckPacketIsSentImmediately();
240 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
241}
242
243TEST_F(PacingSenderTest, InitialBurstNoRttMeasurement) {
244 // Configure pacing rate of 1 packet per 1 ms.
245 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
246 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
247
248 // Send 10 packets, and verify that they are not paced.
249 for (int i = 0; i < kInitialBurstPackets; ++i) {
250 CheckPacketIsSentImmediately();
251 }
252
253 // The first packet was a "make up", then we sent two packets "into the
254 // future", so the delay should be 2ms.
255 CheckPacketIsSentImmediately();
256 CheckPacketIsSentImmediately();
257 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
258
259 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
260 CheckPacketIsSentImmediately();
261
262 // Next time TimeUntilSend is called with no bytes in flight, the tokens
263 // should be refilled and there should be no delay.
264 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false, false, 10);
265 // Send 10 packets, and verify that they are not paced.
266 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
267 CheckPacketIsSentImmediately();
268 }
269
270 // The first packet was a "make up", then we sent two packets "into the
271 // future", so the delay should be 2ms.
272 CheckPacketIsSentImmediately();
273 CheckPacketIsSentImmediately();
274 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
275}
276
277TEST_F(PacingSenderTest, FastSending) {
278 // Ensure the pacing sender paces, even when the inter-packet spacing is less
279 // than the pacing granularity.
280 InitPacingRate(10,
281 QuicBandwidth::FromBytesAndTimeDelta(
282 2 * kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
283 // Update the RTT and verify that the first 10 packets aren't paced.
284 UpdateRtt();
285
286 // Send 10 packets, and verify that they are not paced.
287 for (int i = 0; i < kInitialBurstPackets; ++i) {
288 CheckPacketIsSentImmediately();
289 }
290
291 // The first packet was a "make up", then we sent two packets "into the
292 // future", since it's 2 packets/ms, so the delay should be 1.5ms.
293 CheckPacketIsSentImmediately();
294 CheckPacketIsSentImmediately();
295 CheckPacketIsSentImmediately();
296 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
297
298 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
299 CheckPacketIsSentImmediately();
300
301 // Next time TimeUntilSend is called with no bytes in flight, the tokens
302 // should be refilled and there should be no delay.
303 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, false, false, 10);
304 for (int i = 0; i < kInitialBurstPackets - 1; ++i) {
305 CheckPacketIsSentImmediately();
306 }
307
308 // The first packet was a "make up", then we sent two packets "into the
309 // future", so the delay should be 1.5ms.
310 CheckPacketIsSentImmediately();
311 CheckPacketIsSentImmediately();
312 CheckPacketIsSentImmediately();
313 CheckPacketIsDelayed(QuicTime::Delta::FromMicroseconds(1500));
314}
315
316TEST_F(PacingSenderTest, NoBurstEnteringRecovery) {
317 // Configure pacing rate of 1 packet per 1 ms with no burst tokens.
318 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
319 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
320 // Sending a packet will set burst tokens.
321 CheckPacketIsSentImmediately();
322
323 // Losing a packet will set clear burst tokens.
324 LostPacketVector lost_packets;
325 lost_packets.push_back(LostPacket(QuicPacketNumber(1), kMaxPacketSize));
326 AckedPacketVector empty_acked;
327 EXPECT_CALL(*mock_sender_,
328 OnCongestionEvent(true, kMaxPacketSize, _, IsEmpty(), _));
329 pacing_sender_->OnCongestionEvent(true, kMaxPacketSize, clock_.Now(),
330 empty_acked, lost_packets);
331 // One packet is sent immediately, because of 1ms pacing granularity.
332 CheckPacketIsSentImmediately();
333 // Ensure packets are immediately paced.
334 EXPECT_CALL(*mock_sender_, CanSend(kDefaultTCPMSS)).WillOnce(Return(true));
335 // Verify the next packet is paced and delayed 2ms due to granularity.
336 EXPECT_EQ(QuicTime::Delta::FromMilliseconds(2),
337 pacing_sender_->TimeUntilSend(clock_.Now(), kDefaultTCPMSS));
338 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
339}
340
341TEST_F(PacingSenderTest, NoBurstInRecovery) {
342 // Configure pacing rate of 1 packet per 1 ms with no burst tokens.
343 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
344 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
345
346 UpdateRtt();
347
348 // Ensure only one packet is sent immediately and the rest are paced.
349 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, 0, true, false, 10);
350 CheckPacketIsSentImmediately();
351 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
352}
353
354TEST_F(PacingSenderTest, CwndLimited) {
355 // Configure pacing rate of 1 packet per 1 ms, no initial burst.
356 InitPacingRate(0, QuicBandwidth::FromBytesAndTimeDelta(
357 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
358
359 UpdateRtt();
360
361 CheckPacketIsSentImmediately();
362 CheckPacketIsSentImmediately();
363 // Packet 3 will be delayed 2ms.
364 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
365
366 // Wake up on time.
367 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(2));
368 // After sending packet 3, cwnd is limited.
369 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight, false,
370 true, 10);
371
372 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
373 // Verify pacing sender stops making up for lost time after sending packet 3.
374 // Packet 6 will be delayed 2ms.
375 CheckPacketIsSentImmediately();
376 CheckPacketIsSentImmediately();
377 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
378}
379
380TEST_F(PacingSenderTest, LumpyPacingWithInitialBurstToken) {
381 // Set lumpy size to be 3, and cwnd faction to 0.5
382 SetQuicFlag(&FLAGS_quic_lumpy_pacing_size, 3);
383 SetQuicFlag(&FLAGS_quic_lumpy_pacing_cwnd_fraction, 0.5f);
384 // Configure pacing rate of 1 packet per 1 ms.
385 InitPacingRate(10, QuicBandwidth::FromBytesAndTimeDelta(
386 kMaxPacketSize, QuicTime::Delta::FromMilliseconds(1)));
387 UpdateRtt();
388
389 // Send 10 packets, and verify that they are not paced.
390 for (int i = 0; i < kInitialBurstPackets; ++i) {
391 CheckPacketIsSentImmediately();
392 }
393
394 CheckPacketIsSentImmediately();
395 CheckPacketIsSentImmediately();
396 CheckPacketIsSentImmediately();
397 // Packet 14 will be delayed 3ms.
398 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
399
400 // Wake up on time.
401 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3));
402 CheckPacketIsSentImmediately();
403 CheckPacketIsSentImmediately();
404 CheckPacketIsSentImmediately();
405 // Packet 17 will be delayed 3ms.
406 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
407
408 // Application throttles sending.
409 OnApplicationLimited();
410 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
411 CheckPacketIsSentImmediately();
412 CheckPacketIsSentImmediately();
413 CheckPacketIsSentImmediately();
414 // Packet 20 will be delayed 3ms.
415 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(3));
416
417 // Wake up on time.
418 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(3));
419 CheckPacketIsSentImmediately();
420 // After sending packet 21, cwnd is limited.
421 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight, false,
422 true, 10);
423
424 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(100));
425 // Suppose cwnd size is 5, so that lumpy size becomes 2.
426 CheckPacketIsSentImmediately(HAS_RETRANSMITTABLE_DATA, kBytesInFlight, false,
427 false, 5);
428 CheckPacketIsSentImmediately();
429 // Packet 24 will be delayed 2ms.
430 CheckPacketIsDelayed(QuicTime::Delta::FromMilliseconds(2));
431}
432
wub04a83592019-04-05 06:30:59 -0700433TEST_F(PacingSenderTest, NoLumpyPacingForLowBandwidthFlows) {
434 // Set lumpy size to be 3, and cwnd faction to 0.5
435 SetQuicFlag(&FLAGS_quic_lumpy_pacing_size, 3);
436 SetQuicFlag(&FLAGS_quic_lumpy_pacing_cwnd_fraction, 0.5f);
437 SetQuicReloadableFlag(quic_no_lumpy_pacing_at_low_bw, true);
438
439 // Configure pacing rate of 1 packet per 100 ms.
440 QuicTime::Delta inter_packet_delay = QuicTime::Delta::FromMilliseconds(100);
441 InitPacingRate(kInitialBurstPackets, QuicBandwidth::FromBytesAndTimeDelta(
442 kMaxPacketSize, inter_packet_delay));
443 UpdateRtt();
444
445 // Send kInitialBurstPackets packets, and verify that they are not paced.
446 for (int i = 0; i < kInitialBurstPackets; ++i) {
447 CheckPacketIsSentImmediately();
448 }
449
450 // The first packet after burst token exhausted is also sent immediately,
451 // because ideal_next_packet_send_time has not been set yet.
452 CheckPacketIsSentImmediately();
453
454 for (int i = 0; i < 200; ++i) {
455 CheckPacketIsDelayed(inter_packet_delay);
456 }
457}
458
QUICHE teama6ef0a62019-03-07 20:34:33 -0500459} // namespace test
460} // namespace quic