blob: 5b6f6e8964c5dfcf2f9cc0bba52f95515c32d646 [file] [log] [blame]
QUICHE team7872c772019-07-23 10:19:37 -04001// 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/congestion_control/bbr2_sender.h"
6
7#include <cstddef>
8
9#include "net/third_party/quiche/src/quic/core/congestion_control/bandwidth_sampler.h"
10#include "net/third_party/quiche/src/quic/core/congestion_control/bbr2_drain.h"
11#include "net/third_party/quiche/src/quic/core/congestion_control/bbr2_misc.h"
12#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
13#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
14#include "net/third_party/quiche/src/quic/core/quic_types.h"
wube4df93b2020-03-02 17:55:23 -080015#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
QUICHE team7872c772019-07-23 10:19:37 -040016#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
17
18namespace quic {
19
20namespace {
21// Constants based on TCP defaults.
22// The minimum CWND to ensure delayed acks don't reduce bandwidth measurements.
23// Does not inflate the pacing rate.
24const QuicByteCount kDefaultMinimumCongestionWindow = 4 * kMaxSegmentSize;
25
26const float kInitialPacingGain = 2.885f;
27
28const int kMaxModeChangesPerCongestionEvent = 4;
29} // namespace
30
31// Call |member_function_call| based on the current Bbr2Mode we are in. e.g.
32//
33// auto result = BBR2_MODE_DISPATCH(Foo());
34//
35// is equivalent to:
36//
37// Bbr2ModeBase& Bbr2Sender::GetCurrentMode() {
38// if (mode_ == Bbr2Mode::STARTUP) { return startup_; }
39// if (mode_ == Bbr2Mode::DRAIN) { return drain_; }
40// ...
41// }
42// auto result = GetCurrentMode().Foo();
43//
44// Except that BBR2_MODE_DISPATCH guarantees the call to Foo() is non-virtual.
45//
46#define BBR2_MODE_DISPATCH(member_function_call) \
47 (mode_ == Bbr2Mode::STARTUP \
48 ? (startup_.member_function_call) \
49 : (mode_ == Bbr2Mode::PROBE_BW \
50 ? (probe_bw_.member_function_call) \
51 : (mode_ == Bbr2Mode::DRAIN \
52 ? (drain_.member_function_call) \
53 : (probe_rtt_or_die().member_function_call))))
54
55Bbr2Sender::Bbr2Sender(QuicTime now,
56 const RttStats* rtt_stats,
57 const QuicUnackedPacketMap* unacked_packets,
58 QuicPacketCount initial_cwnd_in_packets,
59 QuicPacketCount max_cwnd_in_packets,
60 QuicRandom* random,
wubbe29b9e2019-11-24 06:56:04 -080061 QuicConnectionStats* stats)
QUICHE team7872c772019-07-23 10:19:37 -040062 : mode_(Bbr2Mode::STARTUP),
63 rtt_stats_(rtt_stats),
64 unacked_packets_(unacked_packets),
65 random_(random),
wubbe29b9e2019-11-24 06:56:04 -080066 connection_stats_(stats),
QUICHE team7872c772019-07-23 10:19:37 -040067 params_(kDefaultMinimumCongestionWindow,
68 max_cwnd_in_packets * kDefaultTCPMSS),
69 model_(&params_,
70 rtt_stats->SmoothedOrInitialRtt(),
71 rtt_stats->last_update_time(),
72 /*cwnd_gain=*/1.0,
73 /*pacing_gain=*/kInitialPacingGain),
wub7652d412019-08-13 15:11:40 -070074 initial_cwnd_(
QUICHE team7872c772019-07-23 10:19:37 -040075 cwnd_limits().ApplyLimits(initial_cwnd_in_packets * kDefaultTCPMSS)),
wub7652d412019-08-13 15:11:40 -070076 cwnd_(initial_cwnd_),
QUICHE team7872c772019-07-23 10:19:37 -040077 pacing_rate_(kInitialPacingGain * QuicBandwidth::FromBytesAndTimeDelta(
78 cwnd_,
79 rtt_stats->SmoothedOrInitialRtt())),
wubbe29b9e2019-11-24 06:56:04 -080080 startup_(this, &model_, now),
QUICHE team7872c772019-07-23 10:19:37 -040081 drain_(this, &model_),
82 probe_bw_(this, &model_),
83 probe_rtt_(this, &model_),
84 flexible_app_limited_(false),
85 last_sample_is_app_limited_(false) {
86 QUIC_DVLOG(2) << this << " Initializing Bbr2Sender. mode:" << mode_
87 << ", PacingRate:" << pacing_rate_ << ", Cwnd:" << cwnd_
88 << ", CwndLimits:" << cwnd_limits() << " @ " << now;
89 DCHECK_EQ(mode_, Bbr2Mode::STARTUP);
90}
91
92void Bbr2Sender::SetFromConfig(const QuicConfig& config,
93 Perspective perspective) {
94 if (config.HasClientRequestedIndependentOption(kBBR9, perspective)) {
95 flexible_app_limited_ = true;
96 }
wub44cc4612020-02-21 15:20:04 -080097 if (GetQuicReloadableFlag(
98 quic_avoid_overestimate_bandwidth_with_aggregation) &&
99 config.HasClientRequestedIndependentOption(kBSAO, perspective)) {
100 QUIC_RELOADABLE_FLAG_COUNT_N(
101 quic_avoid_overestimate_bandwidth_with_aggregation, 4, 4);
102 model_.EnableOverestimateAvoidance();
103 }
QUICHE team7872c772019-07-23 10:19:37 -0400104}
105
106Limits<QuicByteCount> Bbr2Sender::GetCwndLimitsByMode() const {
107 switch (mode_) {
108 case Bbr2Mode::STARTUP:
109 return startup_.GetCwndLimits();
110 case Bbr2Mode::PROBE_BW:
111 return probe_bw_.GetCwndLimits();
112 case Bbr2Mode::DRAIN:
113 return drain_.GetCwndLimits();
114 case Bbr2Mode::PROBE_RTT:
115 return probe_rtt_.GetCwndLimits();
116 default:
117 QUIC_NOTREACHED();
118 return Unlimited<QuicByteCount>();
119 }
120}
121
122const Limits<QuicByteCount>& Bbr2Sender::cwnd_limits() const {
123 return params_.cwnd_limits;
124}
125
QUICHE teamfdcfe3b2019-11-06 10:54:25 -0800126void Bbr2Sender::AdjustNetworkParameters(const NetworkParams& params) {
QUICHE teamb4e187c2019-11-14 06:22:50 -0800127 model_.UpdateNetworkParameters(params.bandwidth, params.rtt);
QUICHE team7872c772019-07-23 10:19:37 -0400128
129 if (mode_ == Bbr2Mode::STARTUP) {
130 const QuicByteCount prior_cwnd = cwnd_;
131
132 // Normally UpdateCongestionWindow updates |cwnd_| towards the target by a
133 // small step per congestion event, by changing |cwnd_| to the bdp at here
134 // we are reducing the number of updates needed to arrive at the target.
135 cwnd_ = model_.BDP(model_.BandwidthEstimate());
136 UpdateCongestionWindow(0);
QUICHE teamb4e187c2019-11-14 06:22:50 -0800137 if (!params.allow_cwnd_to_decrease) {
QUICHE team7872c772019-07-23 10:19:37 -0400138 cwnd_ = std::max(cwnd_, prior_cwnd);
139 }
140 }
141}
142
143void Bbr2Sender::SetInitialCongestionWindowInPackets(
144 QuicPacketCount congestion_window) {
145 if (mode_ == Bbr2Mode::STARTUP) {
146 // The cwnd limits is unchanged and still applies to the new cwnd.
147 cwnd_ = cwnd_limits().ApplyLimits(congestion_window * kDefaultTCPMSS);
148 }
149}
150
151void Bbr2Sender::OnCongestionEvent(bool /*rtt_updated*/,
152 QuicByteCount prior_in_flight,
153 QuicTime event_time,
154 const AckedPacketVector& acked_packets,
155 const LostPacketVector& lost_packets) {
156 QUIC_DVLOG(3) << this
157 << " OnCongestionEvent. prior_in_flight:" << prior_in_flight
158 << " prior_cwnd:" << cwnd_ << " @ " << event_time;
159 Bbr2CongestionEvent congestion_event;
160 congestion_event.prior_cwnd = cwnd_;
wuba545ca12019-12-11 19:27:43 -0800161 congestion_event.prior_bytes_in_flight = prior_in_flight;
QUICHE team7872c772019-07-23 10:19:37 -0400162 congestion_event.is_probing_for_bandwidth =
163 BBR2_MODE_DISPATCH(IsProbingForBandwidth());
164
165 model_.OnCongestionEventStart(event_time, acked_packets, lost_packets,
166 &congestion_event);
167
168 // Number of mode changes allowed for this congestion event.
169 int mode_changes_allowed = kMaxModeChangesPerCongestionEvent;
170 while (true) {
171 Bbr2Mode next_mode = BBR2_MODE_DISPATCH(
172 OnCongestionEvent(prior_in_flight, event_time, acked_packets,
173 lost_packets, congestion_event));
174
175 if (next_mode == mode_) {
176 break;
177 }
178
179 QUIC_DVLOG(2) << this << " Mode change: " << mode_ << " ==> " << next_mode
180 << " @ " << event_time;
wub81f488f2020-03-02 10:49:35 -0800181 BBR2_MODE_DISPATCH(Leave(event_time, &congestion_event));
QUICHE team7872c772019-07-23 10:19:37 -0400182 mode_ = next_mode;
wub81f488f2020-03-02 10:49:35 -0800183 BBR2_MODE_DISPATCH(Enter(event_time, &congestion_event));
QUICHE team7872c772019-07-23 10:19:37 -0400184 --mode_changes_allowed;
185 if (mode_changes_allowed < 0) {
186 QUIC_BUG << "Exceeded max number of mode changes per congestion event.";
187 break;
188 }
189 }
190
191 UpdatePacingRate(congestion_event.bytes_acked);
192 QUIC_BUG_IF(pacing_rate_.IsZero()) << "Pacing rate must not be zero!";
193
194 UpdateCongestionWindow(congestion_event.bytes_acked);
195 QUIC_BUG_IF(cwnd_ == 0u) << "Congestion window must not be zero!";
196
197 model_.OnCongestionEventFinish(unacked_packets_->GetLeastUnacked(),
198 congestion_event);
199 last_sample_is_app_limited_ = congestion_event.last_sample_is_app_limited;
wube4df93b2020-03-02 17:55:23 -0800200 if (avoid_unnecessary_probe_rtt_ && congestion_event.bytes_in_flight == 0) {
201 QUIC_RELOADABLE_FLAG_COUNT_N(quic_bbr2_avoid_unnecessary_probe_rtt, 2, 2);
202 OnEnterQuiescence(event_time);
203 }
QUICHE team7872c772019-07-23 10:19:37 -0400204
205 QUIC_DVLOG(3)
206 << this << " END CongestionEvent(acked:" << acked_packets
207 << ", lost:" << lost_packets.size() << ") "
208 << ", Mode:" << mode_ << ", RttCount:" << model_.RoundTripCount()
wuba545ca12019-12-11 19:27:43 -0800209 << ", BytesInFlight:" << congestion_event.bytes_in_flight
QUICHE team7872c772019-07-23 10:19:37 -0400210 << ", PacingRate:" << PacingRate(0) << ", CWND:" << GetCongestionWindow()
211 << ", PacingGain:" << model_.pacing_gain()
212 << ", CwndGain:" << model_.cwnd_gain()
213 << ", BandwidthEstimate(kbps):" << BandwidthEstimate().ToKBitsPerSecond()
214 << ", MinRTT(us):" << model_.MinRtt().ToMicroseconds()
215 << ", BDP:" << model_.BDP(BandwidthEstimate())
216 << ", BandwidthLatest(kbps):"
217 << model_.bandwidth_latest().ToKBitsPerSecond()
218 << ", BandwidthLow(kbps):" << model_.bandwidth_lo().ToKBitsPerSecond()
219 << ", BandwidthHigh(kbps):" << model_.MaxBandwidth().ToKBitsPerSecond()
220 << ", InflightLatest:" << model_.inflight_latest()
221 << ", InflightLow:" << model_.inflight_lo()
222 << ", InflightHigh:" << model_.inflight_hi()
223 << ", TotalAcked:" << model_.total_bytes_acked()
224 << ", TotalLost:" << model_.total_bytes_lost()
225 << ", TotalSent:" << model_.total_bytes_sent() << " @ " << event_time;
226}
227
228void Bbr2Sender::UpdatePacingRate(QuicByteCount bytes_acked) {
229 if (BandwidthEstimate().IsZero()) {
230 return;
231 }
232
233 if (model_.total_bytes_acked() == bytes_acked) {
234 // After the first ACK, cwnd_ is still the initial congestion window.
235 pacing_rate_ = QuicBandwidth::FromBytesAndTimeDelta(cwnd_, model_.MinRtt());
236 return;
237 }
238
239 QuicBandwidth target_rate = model_.pacing_gain() * model_.BandwidthEstimate();
240 if (startup_.FullBandwidthReached()) {
241 pacing_rate_ = target_rate;
242 return;
243 }
244
245 if (target_rate > pacing_rate_) {
246 pacing_rate_ = target_rate;
247 }
248}
249
250void Bbr2Sender::UpdateCongestionWindow(QuicByteCount bytes_acked) {
251 QuicByteCount target_cwnd = GetTargetCongestionWindow(model_.cwnd_gain());
252
253 const QuicByteCount prior_cwnd = cwnd_;
254 if (startup_.FullBandwidthReached()) {
255 target_cwnd += model_.MaxAckHeight();
256 cwnd_ = std::min(prior_cwnd + bytes_acked, target_cwnd);
wub7652d412019-08-13 15:11:40 -0700257 } else if (prior_cwnd < target_cwnd || prior_cwnd < 2 * initial_cwnd_) {
QUICHE team7872c772019-07-23 10:19:37 -0400258 cwnd_ = prior_cwnd + bytes_acked;
259 }
260 const QuicByteCount desired_cwnd = cwnd_;
261
262 cwnd_ = GetCwndLimitsByMode().ApplyLimits(cwnd_);
263 const QuicByteCount model_limited_cwnd = cwnd_;
264
265 cwnd_ = cwnd_limits().ApplyLimits(cwnd_);
266
267 QUIC_DVLOG(3) << this << " Updating CWND. target_cwnd:" << target_cwnd
268 << ", max_ack_height:" << model_.MaxAckHeight()
269 << ", full_bw:" << startup_.FullBandwidthReached()
270 << ", bytes_acked:" << bytes_acked
271 << ", inflight_lo:" << model_.inflight_lo()
272 << ", inflight_hi:" << model_.inflight_hi() << ". (prior_cwnd) "
273 << prior_cwnd << " => (desired_cwnd) " << desired_cwnd
274 << " => (model_limited_cwnd) " << model_limited_cwnd
275 << " => (final_cwnd) " << cwnd_;
276}
277
278QuicByteCount Bbr2Sender::GetTargetCongestionWindow(float gain) const {
279 return std::max(model_.BDP(model_.BandwidthEstimate(), gain),
280 cwnd_limits().Min());
281}
282
283void Bbr2Sender::OnPacketSent(QuicTime sent_time,
284 QuicByteCount bytes_in_flight,
285 QuicPacketNumber packet_number,
286 QuicByteCount bytes,
287 HasRetransmittableData is_retransmittable) {
288 QUIC_DVLOG(3) << this << " OnPacketSent: pkn:" << packet_number
289 << ", bytes:" << bytes << ", cwnd:" << cwnd_
wuba545ca12019-12-11 19:27:43 -0800290 << ", inflight:" << bytes_in_flight + bytes
QUICHE team7872c772019-07-23 10:19:37 -0400291 << ", total_sent:" << model_.total_bytes_sent() + bytes
292 << ", total_acked:" << model_.total_bytes_acked()
293 << ", total_lost:" << model_.total_bytes_lost() << " @ "
294 << sent_time;
wube4df93b2020-03-02 17:55:23 -0800295 if (avoid_unnecessary_probe_rtt_ && bytes_in_flight == 0) {
296 QUIC_RELOADABLE_FLAG_COUNT_N(quic_bbr2_avoid_unnecessary_probe_rtt, 1, 2);
297 OnExitQuiescence(sent_time);
298 }
QUICHE team7872c772019-07-23 10:19:37 -0400299 model_.OnPacketSent(sent_time, bytes_in_flight, packet_number, bytes,
300 is_retransmittable);
301}
302
wubf4ab9652020-02-20 14:45:43 -0800303void Bbr2Sender::OnPacketNeutered(QuicPacketNumber packet_number) {
304 model_.OnPacketNeutered(packet_number);
305}
306
QUICHE team7872c772019-07-23 10:19:37 -0400307bool Bbr2Sender::CanSend(QuicByteCount bytes_in_flight) {
308 const bool result = bytes_in_flight < GetCongestionWindow();
309 return result;
310}
311
312QuicByteCount Bbr2Sender::GetCongestionWindow() const {
313 // TODO(wub): Implement Recovery?
314 return cwnd_;
315}
316
317QuicBandwidth Bbr2Sender::PacingRate(QuicByteCount /*bytes_in_flight*/) const {
318 return pacing_rate_;
319}
320
321void Bbr2Sender::OnApplicationLimited(QuicByteCount bytes_in_flight) {
322 if (bytes_in_flight >= GetCongestionWindow()) {
323 return;
324 }
325 if (flexible_app_limited_ && IsPipeSufficientlyFull()) {
326 return;
327 }
328
329 model_.OnApplicationLimited();
330 QUIC_DVLOG(2) << this << " Becoming application limited. Last sent packet: "
331 << model_.last_sent_packet()
332 << ", CWND: " << GetCongestionWindow();
333}
334
wub29579902019-12-18 10:16:17 -0800335QuicByteCount Bbr2Sender::GetTargetBytesInflight() const {
336 QuicByteCount bdp = model_.BDP(model_.BandwidthEstimate());
337 return std::min(bdp, GetCongestionWindow());
338}
339
wub5cd49592019-11-25 15:17:13 -0800340void Bbr2Sender::PopulateConnectionStats(QuicConnectionStats* stats) const {
341 stats->num_ack_aggregation_epochs = model_.num_ack_aggregation_epochs();
342}
343
wube4df93b2020-03-02 17:55:23 -0800344void Bbr2Sender::OnEnterQuiescence(QuicTime now) {
345 last_quiescence_start_ = now;
346}
347
348void Bbr2Sender::OnExitQuiescence(QuicTime now) {
349 if (last_quiescence_start_ != QuicTime::Zero()) {
350 Bbr2Mode next_mode = BBR2_MODE_DISPATCH(
351 OnExitQuiescence(now, std::min(now, last_quiescence_start_)));
352 if (next_mode != mode_) {
353 BBR2_MODE_DISPATCH(Leave(now, nullptr));
354 mode_ = next_mode;
355 BBR2_MODE_DISPATCH(Enter(now, nullptr));
356 }
357 last_quiescence_start_ = QuicTime::Zero();
358 }
359}
360
QUICHE team7872c772019-07-23 10:19:37 -0400361bool Bbr2Sender::ShouldSendProbingPacket() const {
362 // TODO(wub): Implement ShouldSendProbingPacket properly.
363 if (!BBR2_MODE_DISPATCH(IsProbingForBandwidth())) {
364 return false;
365 }
366
367 // TODO(b/77975811): If the pipe is highly under-utilized, consider not
368 // sending a probing transmission, because the extra bandwidth is not needed.
369 // If flexible_app_limited is enabled, check if the pipe is sufficiently full.
370 if (flexible_app_limited_) {
371 const bool is_pipe_sufficiently_full = IsPipeSufficientlyFull();
372 QUIC_DVLOG(3) << this << " CWND: " << GetCongestionWindow()
wuba545ca12019-12-11 19:27:43 -0800373 << ", inflight: " << unacked_packets_->bytes_in_flight()
QUICHE team7872c772019-07-23 10:19:37 -0400374 << ", pacing_rate: " << PacingRate(0)
375 << ", flexible_app_limited_: true, ShouldSendProbingPacket: "
376 << !is_pipe_sufficiently_full;
377 return !is_pipe_sufficiently_full;
378 } else {
379 return true;
380 }
381}
382
383bool Bbr2Sender::IsPipeSufficientlyFull() const {
wuba545ca12019-12-11 19:27:43 -0800384 QuicByteCount bytes_in_flight = unacked_packets_->bytes_in_flight();
QUICHE team7872c772019-07-23 10:19:37 -0400385 // See if we need more bytes in flight to see more bandwidth.
386 if (mode_ == Bbr2Mode::STARTUP) {
387 // STARTUP exits if it doesn't observe a 25% bandwidth increase, so the CWND
388 // must be more than 25% above the target.
wuba545ca12019-12-11 19:27:43 -0800389 return bytes_in_flight >= GetTargetCongestionWindow(1.5);
QUICHE team7872c772019-07-23 10:19:37 -0400390 }
391 if (model_.pacing_gain() > 1) {
392 // Super-unity PROBE_BW doesn't exit until 1.25 * BDP is achieved.
wuba545ca12019-12-11 19:27:43 -0800393 return bytes_in_flight >= GetTargetCongestionWindow(model_.pacing_gain());
QUICHE team7872c772019-07-23 10:19:37 -0400394 }
395 // If bytes_in_flight are above the target congestion window, it should be
396 // possible to observe the same or more bandwidth if it's available.
wuba545ca12019-12-11 19:27:43 -0800397 return bytes_in_flight >= GetTargetCongestionWindow(1.1);
QUICHE team7872c772019-07-23 10:19:37 -0400398}
399
400std::string Bbr2Sender::GetDebugState() const {
401 std::ostringstream stream;
402 stream << ExportDebugState();
403 return stream.str();
404}
405
406Bbr2Sender::DebugState Bbr2Sender::ExportDebugState() const {
407 DebugState s;
408 s.mode = mode_;
409 s.round_trip_count = model_.RoundTripCount();
410 s.bandwidth_hi = model_.MaxBandwidth();
411 s.bandwidth_lo = model_.bandwidth_lo();
412 s.bandwidth_est = BandwidthEstimate();
wubb5a8b9e2019-10-18 03:07:25 -0700413 s.inflight_hi = model_.inflight_hi();
414 s.inflight_lo = model_.inflight_lo();
415 s.max_ack_height = model_.MaxAckHeight();
QUICHE team7872c772019-07-23 10:19:37 -0400416 s.min_rtt = model_.MinRtt();
417 s.min_rtt_timestamp = model_.MinRttTimestamp();
418 s.congestion_window = cwnd_;
419 s.pacing_rate = pacing_rate_;
420 s.last_sample_is_app_limited = last_sample_is_app_limited_;
421 s.end_of_app_limited_phase = model_.end_of_app_limited_phase();
422
423 s.startup = startup_.ExportDebugState();
424 s.drain = drain_.ExportDebugState();
425 s.probe_bw = probe_bw_.ExportDebugState();
426 s.probe_rtt = probe_rtt_.ExportDebugState();
427
428 return s;
429}
430
431std::ostream& operator<<(std::ostream& os, const Bbr2Sender::DebugState& s) {
432 os << "mode: " << s.mode << "\n";
433 os << "round_trip_count: " << s.round_trip_count << "\n";
434 os << "bandwidth_hi ~ lo ~ est: " << s.bandwidth_hi << " ~ " << s.bandwidth_lo
435 << " ~ " << s.bandwidth_est << "\n";
436 os << "min_rtt: " << s.min_rtt << "\n";
437 os << "min_rtt_timestamp: " << s.min_rtt_timestamp << "\n";
438 os << "congestion_window: " << s.congestion_window << "\n";
439 os << "pacing_rate: " << s.pacing_rate << "\n";
440 os << "last_sample_is_app_limited: " << s.last_sample_is_app_limited << "\n";
441
442 if (s.mode == Bbr2Mode::STARTUP) {
443 os << s.startup;
444 }
445
446 if (s.mode == Bbr2Mode::DRAIN) {
447 os << s.drain;
448 }
449
450 if (s.mode == Bbr2Mode::PROBE_BW) {
451 os << s.probe_bw;
452 }
453
454 if (s.mode == Bbr2Mode::PROBE_RTT) {
455 os << s.probe_rtt;
456 }
457
458 return os;
459}
460
461} // namespace quic