blob: 108cb342e052b0adb79741995fc78e70a1271f54 [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"
15#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
16
17namespace quic {
18
19namespace {
20// Constants based on TCP defaults.
21// The minimum CWND to ensure delayed acks don't reduce bandwidth measurements.
22// Does not inflate the pacing rate.
23const QuicByteCount kDefaultMinimumCongestionWindow = 4 * kMaxSegmentSize;
24
25const float kInitialPacingGain = 2.885f;
26
27const int kMaxModeChangesPerCongestionEvent = 4;
28} // namespace
29
30// Call |member_function_call| based on the current Bbr2Mode we are in. e.g.
31//
32// auto result = BBR2_MODE_DISPATCH(Foo());
33//
34// is equivalent to:
35//
36// Bbr2ModeBase& Bbr2Sender::GetCurrentMode() {
37// if (mode_ == Bbr2Mode::STARTUP) { return startup_; }
38// if (mode_ == Bbr2Mode::DRAIN) { return drain_; }
39// ...
40// }
41// auto result = GetCurrentMode().Foo();
42//
43// Except that BBR2_MODE_DISPATCH guarantees the call to Foo() is non-virtual.
44//
45#define BBR2_MODE_DISPATCH(member_function_call) \
46 (mode_ == Bbr2Mode::STARTUP \
47 ? (startup_.member_function_call) \
48 : (mode_ == Bbr2Mode::PROBE_BW \
49 ? (probe_bw_.member_function_call) \
50 : (mode_ == Bbr2Mode::DRAIN \
51 ? (drain_.member_function_call) \
52 : (probe_rtt_or_die().member_function_call))))
53
54Bbr2Sender::Bbr2Sender(QuicTime now,
55 const RttStats* rtt_stats,
56 const QuicUnackedPacketMap* unacked_packets,
57 QuicPacketCount initial_cwnd_in_packets,
58 QuicPacketCount max_cwnd_in_packets,
59 QuicRandom* random,
wubbe29b9e2019-11-24 06:56:04 -080060 QuicConnectionStats* stats)
QUICHE team7872c772019-07-23 10:19:37 -040061 : mode_(Bbr2Mode::STARTUP),
62 rtt_stats_(rtt_stats),
63 unacked_packets_(unacked_packets),
64 random_(random),
wubbe29b9e2019-11-24 06:56:04 -080065 connection_stats_(stats),
QUICHE team7872c772019-07-23 10:19:37 -040066 params_(kDefaultMinimumCongestionWindow,
67 max_cwnd_in_packets * kDefaultTCPMSS),
68 model_(&params_,
69 rtt_stats->SmoothedOrInitialRtt(),
70 rtt_stats->last_update_time(),
71 /*cwnd_gain=*/1.0,
72 /*pacing_gain=*/kInitialPacingGain),
wub7652d412019-08-13 15:11:40 -070073 initial_cwnd_(
QUICHE team7872c772019-07-23 10:19:37 -040074 cwnd_limits().ApplyLimits(initial_cwnd_in_packets * kDefaultTCPMSS)),
wub7652d412019-08-13 15:11:40 -070075 cwnd_(initial_cwnd_),
QUICHE team7872c772019-07-23 10:19:37 -040076 pacing_rate_(kInitialPacingGain * QuicBandwidth::FromBytesAndTimeDelta(
77 cwnd_,
78 rtt_stats->SmoothedOrInitialRtt())),
wubbe29b9e2019-11-24 06:56:04 -080079 startup_(this, &model_, now),
QUICHE team7872c772019-07-23 10:19:37 -040080 drain_(this, &model_),
81 probe_bw_(this, &model_),
82 probe_rtt_(this, &model_),
83 flexible_app_limited_(false),
84 last_sample_is_app_limited_(false) {
85 QUIC_DVLOG(2) << this << " Initializing Bbr2Sender. mode:" << mode_
86 << ", PacingRate:" << pacing_rate_ << ", Cwnd:" << cwnd_
87 << ", CwndLimits:" << cwnd_limits() << " @ " << now;
88 DCHECK_EQ(mode_, Bbr2Mode::STARTUP);
89}
90
91void Bbr2Sender::SetFromConfig(const QuicConfig& config,
92 Perspective perspective) {
93 if (config.HasClientRequestedIndependentOption(kBBR9, perspective)) {
94 flexible_app_limited_ = true;
95 }
96}
97
98Limits<QuicByteCount> Bbr2Sender::GetCwndLimitsByMode() const {
99 switch (mode_) {
100 case Bbr2Mode::STARTUP:
101 return startup_.GetCwndLimits();
102 case Bbr2Mode::PROBE_BW:
103 return probe_bw_.GetCwndLimits();
104 case Bbr2Mode::DRAIN:
105 return drain_.GetCwndLimits();
106 case Bbr2Mode::PROBE_RTT:
107 return probe_rtt_.GetCwndLimits();
108 default:
109 QUIC_NOTREACHED();
110 return Unlimited<QuicByteCount>();
111 }
112}
113
114const Limits<QuicByteCount>& Bbr2Sender::cwnd_limits() const {
115 return params_.cwnd_limits;
116}
117
QUICHE teamfdcfe3b2019-11-06 10:54:25 -0800118void Bbr2Sender::AdjustNetworkParameters(const NetworkParams& params) {
QUICHE teamb4e187c2019-11-14 06:22:50 -0800119 model_.UpdateNetworkParameters(params.bandwidth, params.rtt);
QUICHE team7872c772019-07-23 10:19:37 -0400120
121 if (mode_ == Bbr2Mode::STARTUP) {
122 const QuicByteCount prior_cwnd = cwnd_;
123
124 // Normally UpdateCongestionWindow updates |cwnd_| towards the target by a
125 // small step per congestion event, by changing |cwnd_| to the bdp at here
126 // we are reducing the number of updates needed to arrive at the target.
127 cwnd_ = model_.BDP(model_.BandwidthEstimate());
128 UpdateCongestionWindow(0);
QUICHE teamb4e187c2019-11-14 06:22:50 -0800129 if (!params.allow_cwnd_to_decrease) {
QUICHE team7872c772019-07-23 10:19:37 -0400130 cwnd_ = std::max(cwnd_, prior_cwnd);
131 }
132 }
133}
134
135void Bbr2Sender::SetInitialCongestionWindowInPackets(
136 QuicPacketCount congestion_window) {
137 if (mode_ == Bbr2Mode::STARTUP) {
138 // The cwnd limits is unchanged and still applies to the new cwnd.
139 cwnd_ = cwnd_limits().ApplyLimits(congestion_window * kDefaultTCPMSS);
140 }
141}
142
143void Bbr2Sender::OnCongestionEvent(bool /*rtt_updated*/,
144 QuicByteCount prior_in_flight,
145 QuicTime event_time,
146 const AckedPacketVector& acked_packets,
147 const LostPacketVector& lost_packets) {
148 QUIC_DVLOG(3) << this
149 << " OnCongestionEvent. prior_in_flight:" << prior_in_flight
150 << " prior_cwnd:" << cwnd_ << " @ " << event_time;
151 Bbr2CongestionEvent congestion_event;
152 congestion_event.prior_cwnd = cwnd_;
wuba545ca12019-12-11 19:27:43 -0800153 congestion_event.prior_bytes_in_flight = prior_in_flight;
QUICHE team7872c772019-07-23 10:19:37 -0400154 congestion_event.is_probing_for_bandwidth =
155 BBR2_MODE_DISPATCH(IsProbingForBandwidth());
156
157 model_.OnCongestionEventStart(event_time, acked_packets, lost_packets,
158 &congestion_event);
159
160 // Number of mode changes allowed for this congestion event.
161 int mode_changes_allowed = kMaxModeChangesPerCongestionEvent;
162 while (true) {
163 Bbr2Mode next_mode = BBR2_MODE_DISPATCH(
164 OnCongestionEvent(prior_in_flight, event_time, acked_packets,
165 lost_packets, congestion_event));
166
167 if (next_mode == mode_) {
168 break;
169 }
170
171 QUIC_DVLOG(2) << this << " Mode change: " << mode_ << " ==> " << next_mode
172 << " @ " << event_time;
wubbe29b9e2019-11-24 06:56:04 -0800173 BBR2_MODE_DISPATCH(Leave(congestion_event));
QUICHE team7872c772019-07-23 10:19:37 -0400174 mode_ = next_mode;
175 BBR2_MODE_DISPATCH(Enter(congestion_event));
176 --mode_changes_allowed;
177 if (mode_changes_allowed < 0) {
178 QUIC_BUG << "Exceeded max number of mode changes per congestion event.";
179 break;
180 }
181 }
182
183 UpdatePacingRate(congestion_event.bytes_acked);
184 QUIC_BUG_IF(pacing_rate_.IsZero()) << "Pacing rate must not be zero!";
185
186 UpdateCongestionWindow(congestion_event.bytes_acked);
187 QUIC_BUG_IF(cwnd_ == 0u) << "Congestion window must not be zero!";
188
189 model_.OnCongestionEventFinish(unacked_packets_->GetLeastUnacked(),
190 congestion_event);
191 last_sample_is_app_limited_ = congestion_event.last_sample_is_app_limited;
192
193 QUIC_DVLOG(3)
194 << this << " END CongestionEvent(acked:" << acked_packets
195 << ", lost:" << lost_packets.size() << ") "
196 << ", Mode:" << mode_ << ", RttCount:" << model_.RoundTripCount()
wuba545ca12019-12-11 19:27:43 -0800197 << ", BytesInFlight:" << congestion_event.bytes_in_flight
QUICHE team7872c772019-07-23 10:19:37 -0400198 << ", PacingRate:" << PacingRate(0) << ", CWND:" << GetCongestionWindow()
199 << ", PacingGain:" << model_.pacing_gain()
200 << ", CwndGain:" << model_.cwnd_gain()
201 << ", BandwidthEstimate(kbps):" << BandwidthEstimate().ToKBitsPerSecond()
202 << ", MinRTT(us):" << model_.MinRtt().ToMicroseconds()
203 << ", BDP:" << model_.BDP(BandwidthEstimate())
204 << ", BandwidthLatest(kbps):"
205 << model_.bandwidth_latest().ToKBitsPerSecond()
206 << ", BandwidthLow(kbps):" << model_.bandwidth_lo().ToKBitsPerSecond()
207 << ", BandwidthHigh(kbps):" << model_.MaxBandwidth().ToKBitsPerSecond()
208 << ", InflightLatest:" << model_.inflight_latest()
209 << ", InflightLow:" << model_.inflight_lo()
210 << ", InflightHigh:" << model_.inflight_hi()
211 << ", TotalAcked:" << model_.total_bytes_acked()
212 << ", TotalLost:" << model_.total_bytes_lost()
213 << ", TotalSent:" << model_.total_bytes_sent() << " @ " << event_time;
214}
215
216void Bbr2Sender::UpdatePacingRate(QuicByteCount bytes_acked) {
217 if (BandwidthEstimate().IsZero()) {
218 return;
219 }
220
221 if (model_.total_bytes_acked() == bytes_acked) {
222 // After the first ACK, cwnd_ is still the initial congestion window.
223 pacing_rate_ = QuicBandwidth::FromBytesAndTimeDelta(cwnd_, model_.MinRtt());
224 return;
225 }
226
227 QuicBandwidth target_rate = model_.pacing_gain() * model_.BandwidthEstimate();
228 if (startup_.FullBandwidthReached()) {
229 pacing_rate_ = target_rate;
230 return;
231 }
232
233 if (target_rate > pacing_rate_) {
234 pacing_rate_ = target_rate;
235 }
236}
237
238void Bbr2Sender::UpdateCongestionWindow(QuicByteCount bytes_acked) {
239 QuicByteCount target_cwnd = GetTargetCongestionWindow(model_.cwnd_gain());
240
241 const QuicByteCount prior_cwnd = cwnd_;
242 if (startup_.FullBandwidthReached()) {
243 target_cwnd += model_.MaxAckHeight();
244 cwnd_ = std::min(prior_cwnd + bytes_acked, target_cwnd);
wub7652d412019-08-13 15:11:40 -0700245 } else if (prior_cwnd < target_cwnd || prior_cwnd < 2 * initial_cwnd_) {
QUICHE team7872c772019-07-23 10:19:37 -0400246 cwnd_ = prior_cwnd + bytes_acked;
247 }
248 const QuicByteCount desired_cwnd = cwnd_;
249
250 cwnd_ = GetCwndLimitsByMode().ApplyLimits(cwnd_);
251 const QuicByteCount model_limited_cwnd = cwnd_;
252
253 cwnd_ = cwnd_limits().ApplyLimits(cwnd_);
254
255 QUIC_DVLOG(3) << this << " Updating CWND. target_cwnd:" << target_cwnd
256 << ", max_ack_height:" << model_.MaxAckHeight()
257 << ", full_bw:" << startup_.FullBandwidthReached()
258 << ", bytes_acked:" << bytes_acked
259 << ", inflight_lo:" << model_.inflight_lo()
260 << ", inflight_hi:" << model_.inflight_hi() << ". (prior_cwnd) "
261 << prior_cwnd << " => (desired_cwnd) " << desired_cwnd
262 << " => (model_limited_cwnd) " << model_limited_cwnd
263 << " => (final_cwnd) " << cwnd_;
264}
265
266QuicByteCount Bbr2Sender::GetTargetCongestionWindow(float gain) const {
267 return std::max(model_.BDP(model_.BandwidthEstimate(), gain),
268 cwnd_limits().Min());
269}
270
271void Bbr2Sender::OnPacketSent(QuicTime sent_time,
272 QuicByteCount bytes_in_flight,
273 QuicPacketNumber packet_number,
274 QuicByteCount bytes,
275 HasRetransmittableData is_retransmittable) {
276 QUIC_DVLOG(3) << this << " OnPacketSent: pkn:" << packet_number
277 << ", bytes:" << bytes << ", cwnd:" << cwnd_
wuba545ca12019-12-11 19:27:43 -0800278 << ", inflight:" << bytes_in_flight + bytes
QUICHE team7872c772019-07-23 10:19:37 -0400279 << ", total_sent:" << model_.total_bytes_sent() + bytes
280 << ", total_acked:" << model_.total_bytes_acked()
281 << ", total_lost:" << model_.total_bytes_lost() << " @ "
282 << sent_time;
283 model_.OnPacketSent(sent_time, bytes_in_flight, packet_number, bytes,
284 is_retransmittable);
285}
286
wubf4ab9652020-02-20 14:45:43 -0800287void Bbr2Sender::OnPacketNeutered(QuicPacketNumber packet_number) {
288 model_.OnPacketNeutered(packet_number);
289}
290
QUICHE team7872c772019-07-23 10:19:37 -0400291bool Bbr2Sender::CanSend(QuicByteCount bytes_in_flight) {
292 const bool result = bytes_in_flight < GetCongestionWindow();
293 return result;
294}
295
296QuicByteCount Bbr2Sender::GetCongestionWindow() const {
297 // TODO(wub): Implement Recovery?
298 return cwnd_;
299}
300
301QuicBandwidth Bbr2Sender::PacingRate(QuicByteCount /*bytes_in_flight*/) const {
302 return pacing_rate_;
303}
304
305void Bbr2Sender::OnApplicationLimited(QuicByteCount bytes_in_flight) {
306 if (bytes_in_flight >= GetCongestionWindow()) {
307 return;
308 }
309 if (flexible_app_limited_ && IsPipeSufficientlyFull()) {
310 return;
311 }
312
313 model_.OnApplicationLimited();
314 QUIC_DVLOG(2) << this << " Becoming application limited. Last sent packet: "
315 << model_.last_sent_packet()
316 << ", CWND: " << GetCongestionWindow();
317}
318
wub29579902019-12-18 10:16:17 -0800319QuicByteCount Bbr2Sender::GetTargetBytesInflight() const {
320 QuicByteCount bdp = model_.BDP(model_.BandwidthEstimate());
321 return std::min(bdp, GetCongestionWindow());
322}
323
wub5cd49592019-11-25 15:17:13 -0800324void Bbr2Sender::PopulateConnectionStats(QuicConnectionStats* stats) const {
325 stats->num_ack_aggregation_epochs = model_.num_ack_aggregation_epochs();
326}
327
QUICHE team7872c772019-07-23 10:19:37 -0400328bool Bbr2Sender::ShouldSendProbingPacket() const {
329 // TODO(wub): Implement ShouldSendProbingPacket properly.
330 if (!BBR2_MODE_DISPATCH(IsProbingForBandwidth())) {
331 return false;
332 }
333
334 // TODO(b/77975811): If the pipe is highly under-utilized, consider not
335 // sending a probing transmission, because the extra bandwidth is not needed.
336 // If flexible_app_limited is enabled, check if the pipe is sufficiently full.
337 if (flexible_app_limited_) {
338 const bool is_pipe_sufficiently_full = IsPipeSufficientlyFull();
339 QUIC_DVLOG(3) << this << " CWND: " << GetCongestionWindow()
wuba545ca12019-12-11 19:27:43 -0800340 << ", inflight: " << unacked_packets_->bytes_in_flight()
QUICHE team7872c772019-07-23 10:19:37 -0400341 << ", pacing_rate: " << PacingRate(0)
342 << ", flexible_app_limited_: true, ShouldSendProbingPacket: "
343 << !is_pipe_sufficiently_full;
344 return !is_pipe_sufficiently_full;
345 } else {
346 return true;
347 }
348}
349
350bool Bbr2Sender::IsPipeSufficientlyFull() const {
wuba545ca12019-12-11 19:27:43 -0800351 QuicByteCount bytes_in_flight = unacked_packets_->bytes_in_flight();
QUICHE team7872c772019-07-23 10:19:37 -0400352 // See if we need more bytes in flight to see more bandwidth.
353 if (mode_ == Bbr2Mode::STARTUP) {
354 // STARTUP exits if it doesn't observe a 25% bandwidth increase, so the CWND
355 // must be more than 25% above the target.
wuba545ca12019-12-11 19:27:43 -0800356 return bytes_in_flight >= GetTargetCongestionWindow(1.5);
QUICHE team7872c772019-07-23 10:19:37 -0400357 }
358 if (model_.pacing_gain() > 1) {
359 // Super-unity PROBE_BW doesn't exit until 1.25 * BDP is achieved.
wuba545ca12019-12-11 19:27:43 -0800360 return bytes_in_flight >= GetTargetCongestionWindow(model_.pacing_gain());
QUICHE team7872c772019-07-23 10:19:37 -0400361 }
362 // If bytes_in_flight are above the target congestion window, it should be
363 // possible to observe the same or more bandwidth if it's available.
wuba545ca12019-12-11 19:27:43 -0800364 return bytes_in_flight >= GetTargetCongestionWindow(1.1);
QUICHE team7872c772019-07-23 10:19:37 -0400365}
366
367std::string Bbr2Sender::GetDebugState() const {
368 std::ostringstream stream;
369 stream << ExportDebugState();
370 return stream.str();
371}
372
373Bbr2Sender::DebugState Bbr2Sender::ExportDebugState() const {
374 DebugState s;
375 s.mode = mode_;
376 s.round_trip_count = model_.RoundTripCount();
377 s.bandwidth_hi = model_.MaxBandwidth();
378 s.bandwidth_lo = model_.bandwidth_lo();
379 s.bandwidth_est = BandwidthEstimate();
wubb5a8b9e2019-10-18 03:07:25 -0700380 s.inflight_hi = model_.inflight_hi();
381 s.inflight_lo = model_.inflight_lo();
382 s.max_ack_height = model_.MaxAckHeight();
QUICHE team7872c772019-07-23 10:19:37 -0400383 s.min_rtt = model_.MinRtt();
384 s.min_rtt_timestamp = model_.MinRttTimestamp();
385 s.congestion_window = cwnd_;
386 s.pacing_rate = pacing_rate_;
387 s.last_sample_is_app_limited = last_sample_is_app_limited_;
388 s.end_of_app_limited_phase = model_.end_of_app_limited_phase();
389
390 s.startup = startup_.ExportDebugState();
391 s.drain = drain_.ExportDebugState();
392 s.probe_bw = probe_bw_.ExportDebugState();
393 s.probe_rtt = probe_rtt_.ExportDebugState();
394
395 return s;
396}
397
398std::ostream& operator<<(std::ostream& os, const Bbr2Sender::DebugState& s) {
399 os << "mode: " << s.mode << "\n";
400 os << "round_trip_count: " << s.round_trip_count << "\n";
401 os << "bandwidth_hi ~ lo ~ est: " << s.bandwidth_hi << " ~ " << s.bandwidth_lo
402 << " ~ " << s.bandwidth_est << "\n";
403 os << "min_rtt: " << s.min_rtt << "\n";
404 os << "min_rtt_timestamp: " << s.min_rtt_timestamp << "\n";
405 os << "congestion_window: " << s.congestion_window << "\n";
406 os << "pacing_rate: " << s.pacing_rate << "\n";
407 os << "last_sample_is_app_limited: " << s.last_sample_is_app_limited << "\n";
408
409 if (s.mode == Bbr2Mode::STARTUP) {
410 os << s.startup;
411 }
412
413 if (s.mode == Bbr2Mode::DRAIN) {
414 os << s.drain;
415 }
416
417 if (s.mode == Bbr2Mode::PROBE_BW) {
418 os << s.probe_bw;
419 }
420
421 if (s.mode == Bbr2Mode::PROBE_RTT) {
422 os << s.probe_rtt;
423 }
424
425 return os;
426}
427
428} // namespace quic