blob: 0dee4385cc26cd4c24624e24965e487dbc758089 [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#ifndef QUICHE_QUIC_CORE_CONGESTION_CONTROL_BBR2_MISC_H_
6#define QUICHE_QUIC_CORE_CONGESTION_CONTROL_BBR2_MISC_H_
7
8#include <algorithm>
9#include <limits>
10
11#include "net/third_party/quiche/src/quic/core/congestion_control/bandwidth_sampler.h"
12#include "net/third_party/quiche/src/quic/core/congestion_control/windowed_filter.h"
13#include "net/third_party/quiche/src/quic/core/quic_bandwidth.h"
14#include "net/third_party/quiche/src/quic/core/quic_packet_number.h"
15#include "net/third_party/quiche/src/quic/core/quic_time.h"
16#include "net/third_party/quiche/src/quic/core/quic_types.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
wub174cb5c2019-08-06 08:54:26 -070018#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
QUICHE team7872c772019-07-23 10:19:37 -040019#include "net/quic/platform/impl/quic_export_impl.h"
20
21namespace quic {
22
QUICHE team7872c772019-07-23 10:19:37 -040023template <typename T>
24class QUIC_EXPORT_PRIVATE Limits {
25 public:
26 Limits(T min, T max) : min_(min), max_(max) {}
27
28 // If [min, max] is an empty range, i.e. min > max, this function returns max,
29 // because typically a value larger than max means "risky".
30 T ApplyLimits(T raw_value) const {
31 return std::min(max_, std::max(min_, raw_value));
32 }
33
34 T Min() const { return min_; }
35 T Max() const { return max_; }
36
37 private:
38 T min_;
39 T max_;
40};
41
42template <typename T>
43QUIC_EXPORT_PRIVATE inline Limits<T> MinMax(T min, T max) {
44 return Limits<T>(min, max);
45}
46
47template <typename T>
48QUIC_EXPORT_PRIVATE inline Limits<T> NoLessThan(T min) {
49 return Limits<T>(min, std::numeric_limits<T>::max());
50}
51
52template <typename T>
53QUIC_EXPORT_PRIVATE inline Limits<T> NoGreaterThan(T max) {
54 return Limits<T>(std::numeric_limits<T>::min(), max);
55}
56
57template <typename T>
58QUIC_EXPORT_PRIVATE inline Limits<T> Unlimited() {
59 return Limits<T>(std::numeric_limits<T>::min(),
60 std::numeric_limits<T>::max());
61}
62
63template <typename T>
64QUIC_EXPORT_PRIVATE inline std::ostream& operator<<(std::ostream& os,
65 const Limits<T>& limits) {
66 return os << "[" << limits.Min() << ", " << limits.Max() << "]";
67}
68
69// Bbr2Params contains all parameters of a Bbr2Sender.
70struct QUIC_EXPORT_PRIVATE Bbr2Params {
71 Bbr2Params(QuicByteCount cwnd_min, QuicByteCount cwnd_max)
72 : cwnd_limits(cwnd_min, cwnd_max) {}
73
74 /*
75 * STARTUP parameters.
76 */
77
78 // The gain for both CWND and PacingRate at startup.
79 // TODO(wub): Maybe change to the newly derived value of 2.773 (4 * ln(2)).
80 float startup_gain = 2.885;
81
82 // Full bandwidth is declared if the total bandwidth growth is less than
83 // |startup_full_bw_threshold| times in the last |startup_full_bw_rounds|
84 // round trips.
85 float startup_full_bw_threshold = 1.25;
86
87 QuicRoundTripCount startup_full_bw_rounds = 3;
88
89 // The minimum number of loss marking events to exit STARTUP.
90 int64_t startup_full_loss_count = 8;
91
92 /*
93 * DRAIN parameters.
94 */
95 float drain_cwnd_gain = 2.885;
96 float drain_pacing_gain = 1.0 / 2.885;
97
98 /*
99 * PROBE_BW parameters.
100 */
101 // Max amount of randomness to inject in round counting for Reno-coexistence.
102 QuicRoundTripCount probe_bw_max_probe_rand_rounds = 2;
103
104 // Max number of rounds before probing for Reno-coexistence.
105 uint32_t probe_bw_probe_max_rounds = 63;
106
107 // Multiplier to get Reno-style probe epoch duration as: k * BDP round trips.
108 // If zero, disables Reno-style BDP-scaled coexistence mechanism.
109 float probe_bw_probe_reno_gain = 1.0;
110
111 // Minimum duration for BBR-native probes.
112 QuicTime::Delta probe_bw_probe_base_duration =
wub174cb5c2019-08-06 08:54:26 -0700113 QuicTime::Delta::FromMilliseconds(
114 GetQuicFlag(FLAGS_quic_bbr2_default_probe_bw_base_duration_ms));
QUICHE team7872c772019-07-23 10:19:37 -0400115
wub174cb5c2019-08-06 08:54:26 -0700116 // The upper bound of the random amount of BBR-native probes.
QUICHE team7872c772019-07-23 10:19:37 -0400117 QuicTime::Delta probe_bw_probe_max_rand_duration =
wub174cb5c2019-08-06 08:54:26 -0700118 QuicTime::Delta::FromMilliseconds(
119 GetQuicFlag(FLAGS_quic_bbr2_default_probe_bw_max_rand_duration_ms));
QUICHE team7872c772019-07-23 10:19:37 -0400120
121 // Multiplier to get target inflight (as multiple of BDP) for PROBE_UP phase.
122 float probe_bw_probe_inflight_gain = 1.25;
123
124 // Pacing gains.
125 float probe_bw_probe_up_pacing_gain = 1.25;
126 float probe_bw_probe_down_pacing_gain = 0.75;
127 float probe_bw_default_pacing_gain = 1.0;
128
129 float probe_bw_cwnd_gain = 2.0;
130
131 /*
132 * PROBE_RTT parameters.
133 */
134 float probe_rtt_inflight_target_bdp_fraction = 0.5;
wub174cb5c2019-08-06 08:54:26 -0700135 QuicTime::Delta probe_rtt_period = QuicTime::Delta::FromMilliseconds(
136 GetQuicFlag(FLAGS_quic_bbr2_default_probe_rtt_period_ms));
QUICHE team7872c772019-07-23 10:19:37 -0400137 QuicTime::Delta probe_rtt_duration = QuicTime::Delta::FromMilliseconds(200);
138
139 /*
140 * Parameters used by multiple modes.
141 */
142
143 // The initial value of the max ack height filter's window length.
144 QuicRoundTripCount initial_max_ack_height_filter_window = 10;
145
146 // Fraction of unutilized headroom to try to leave in path upon high loss.
147 float inflight_hi_headroom = 0.15;
148
149 // Estimate startup/bw probing has gone too far if loss rate exceeds this.
wub5a6ea9a2019-08-09 13:10:49 -0700150 float loss_threshold = GetQuicFlag(FLAGS_quic_bbr2_default_loss_threshold);
QUICHE team7872c772019-07-23 10:19:37 -0400151
152 Limits<QuicByteCount> cwnd_limits;
153};
154
155class QUIC_EXPORT_PRIVATE RoundTripCounter {
156 public:
157 RoundTripCounter();
158
159 QuicRoundTripCount Count() const { return round_trip_count_; }
160
161 QuicPacketNumber last_sent_packet() const { return last_sent_packet_; }
162
163 // Must be called in ascending packet number order.
164 void OnPacketSent(QuicPacketNumber packet_number);
165
166 // Return whether a round trip has just completed.
167 bool OnPacketsAcked(QuicPacketNumber last_acked_packet);
168
169 void RestartRound();
170
171 private:
172 QuicRoundTripCount round_trip_count_;
173 QuicPacketNumber last_sent_packet_;
174 // The last sent packet number of the current round trip.
175 QuicPacketNumber end_of_round_trip_;
176};
177
178class QUIC_EXPORT_PRIVATE MinRttFilter {
179 public:
180 MinRttFilter(QuicTime::Delta initial_min_rtt,
181 QuicTime initial_min_rtt_timestamp);
182
183 void Update(QuicTime::Delta sample_rtt, QuicTime now);
184
185 void ForceUpdate(QuicTime::Delta sample_rtt, QuicTime now);
186
187 QuicTime::Delta Get() const { return min_rtt_; }
188
189 QuicTime GetTimestamp() const { return min_rtt_timestamp_; }
190
191 private:
192 QuicTime::Delta min_rtt_;
193 // Time when the current value of |min_rtt_| was assigned.
194 QuicTime min_rtt_timestamp_;
195};
196
197class QUIC_EXPORT_PRIVATE Bbr2MaxBandwidthFilter {
198 public:
199 void Update(QuicBandwidth sample) {
200 max_bandwidth_[1] = std::max(sample, max_bandwidth_[1]);
201 }
202
203 void Advance() {
204 if (max_bandwidth_[1].IsZero()) {
205 return;
206 }
207
208 max_bandwidth_[0] = max_bandwidth_[1];
209 max_bandwidth_[1] = QuicBandwidth::Zero();
210 }
211
212 QuicBandwidth Get() const {
213 return std::max(max_bandwidth_[0], max_bandwidth_[1]);
214 }
215
216 private:
217 QuicBandwidth max_bandwidth_[2] = {QuicBandwidth::Zero(),
218 QuicBandwidth::Zero()};
219};
220
221// Information that are meaningful only when Bbr2Sender::OnCongestionEvent is
222// running.
223struct QUIC_EXPORT_PRIVATE Bbr2CongestionEvent {
224 QuicTime event_time = QuicTime::Zero();
225
226 // The congestion window prior to the processing of the ack/loss events.
227 QuicByteCount prior_cwnd;
228
229 // Total bytes inflight after the processing of the ack/loss events.
230 QuicByteCount bytes_in_flight = 0;
231
232 // Total bytes acked from acks in this event.
233 QuicByteCount bytes_acked = 0;
234
235 // Total bytes lost from losses in this event.
236 QuicByteCount bytes_lost = 0;
237
238 // Whether acked_packets indicates the end of a round trip.
239 bool end_of_round_trip = false;
240
241 // Whether the last bandwidth sample from acked_packets is app limited.
242 // false if acked_packets is empty.
243 bool last_sample_is_app_limited = false;
244
245 // When the event happened, whether the sender is probing for bandwidth.
246 bool is_probing_for_bandwidth = false;
247
248 // Minimum rtt of all bandwidth samples from acked_packets.
249 // QuicTime::Delta::Infinite() if acked_packets is empty.
250 QuicTime::Delta sample_min_rtt = QuicTime::Delta::Infinite();
251
wubeedb91b2019-07-23 14:31:57 -0700252 // Maximum bandwidth of all bandwidth samples from acked_packets.
253 QuicBandwidth sample_max_bandwidth = QuicBandwidth::Zero();
254
QUICHE team7872c772019-07-23 10:19:37 -0400255 // Send time state of the largest-numbered packet in this event.
256 // SendTimeState send_time_state;
257 struct {
258 QuicPacketNumber packet_number;
259 BandwidthSample bandwidth_sample;
260 // Total bytes acked while |packet| is inflight.
261 QuicByteCount inflight_sample;
262 } last_acked_sample;
263
264 struct {
265 QuicPacketNumber packet_number;
266 SendTimeState send_time_state;
267 } last_lost_sample;
268};
269
270QUIC_EXPORT_PRIVATE const SendTimeState& SendStateOfLargestPacket(
271 const Bbr2CongestionEvent& congestion_event);
272
QUICHE team7872c772019-07-23 10:19:37 -0400273// Bbr2NetworkModel takes low level congestion signals(packets sent/acked/lost)
274// as input and produces BBRv2 model parameters like inflight_(hi|lo),
275// bandwidth_(hi|lo), bandwidth and rtt estimates, etc.
276class QUIC_EXPORT_PRIVATE Bbr2NetworkModel {
277 public:
278 Bbr2NetworkModel(const Bbr2Params* params,
279 QuicTime::Delta initial_rtt,
280 QuicTime initial_rtt_timestamp,
281 float cwnd_gain,
282 float pacing_gain);
283
284 void OnPacketSent(QuicTime sent_time,
285 QuicByteCount bytes_in_flight,
286 QuicPacketNumber packet_number,
287 QuicByteCount bytes,
288 HasRetransmittableData is_retransmittable);
289
290 void OnCongestionEventStart(QuicTime event_time,
291 const AckedPacketVector& acked_packets,
292 const LostPacketVector& lost_packets,
293 Bbr2CongestionEvent* congestion_event);
294
295 void OnCongestionEventFinish(QuicPacketNumber least_unacked_packet,
296 const Bbr2CongestionEvent& congestion_event);
297
298 // Update the model without a congestion event.
299 // Max bandwidth is updated if |bandwidth| is larger than existing max
300 // bandwidth. Min rtt is updated if |rtt| is non-zero and smaller than
301 // existing min rtt.
302 void UpdateNetworkParameters(QuicBandwidth bandwidth, QuicTime::Delta rtt);
303
304 // Update inflight/bandwidth short-term lower bounds.
305 void AdaptLowerBounds(const Bbr2CongestionEvent& congestion_event);
306
307 // Restart the current round trip as if it is starting now.
308 void RestartRound();
309
310 void AdvanceMaxBandwidthFilter() { max_bandwidth_filter_.Advance(); }
311
312 void OnApplicationLimited() { bandwidth_sampler_.OnAppLimited(); }
313
314 QuicByteCount BDP(QuicBandwidth bandwidth) const {
315 return bandwidth * MinRtt();
316 }
317
318 QuicByteCount BDP(QuicBandwidth bandwidth, float gain) const {
319 return bandwidth * MinRtt() * gain;
320 }
321
322 QuicTime::Delta MinRtt() const { return min_rtt_filter_.Get(); }
323
324 QuicTime MinRttTimestamp() const { return min_rtt_filter_.GetTimestamp(); }
325
326 QuicBandwidth MaxBandwidth() const { return max_bandwidth_filter_.Get(); }
327
wubf35ea982019-08-06 16:19:38 -0700328 QuicByteCount MaxAckHeight() const {
329 return bandwidth_sampler_.max_ack_height();
330 }
QUICHE team7872c772019-07-23 10:19:37 -0400331
332 bool MaybeExpireMinRtt(const Bbr2CongestionEvent& congestion_event);
333
334 QuicBandwidth BandwidthEstimate() const {
335 return std::min(MaxBandwidth(), bandwidth_lo_);
336 }
337
338 QuicRoundTripCount RoundTripCount() const {
339 return round_trip_counter_.Count();
340 }
341
342 bool IsCongestionWindowLimited(
343 const Bbr2CongestionEvent& congestion_event) const;
344
345 bool IsInflightTooHigh(const Bbr2CongestionEvent& congestion_event) const;
346
347 QuicPacketNumber last_sent_packet() const {
348 return round_trip_counter_.last_sent_packet();
349 }
350
351 QuicByteCount total_bytes_acked() const {
352 return bandwidth_sampler_.total_bytes_acked();
353 }
354
355 QuicByteCount total_bytes_lost() const {
356 return bandwidth_sampler_.total_bytes_lost();
357 }
358
359 QuicByteCount total_bytes_sent() const {
360 return bandwidth_sampler_.total_bytes_sent();
361 }
362
363 QuicByteCount bytes_in_flight() const {
364 return total_bytes_sent() - total_bytes_acked() - total_bytes_lost();
365 }
366
367 QuicPacketNumber end_of_app_limited_phase() const {
368 return bandwidth_sampler_.end_of_app_limited_phase();
369 }
370
371 QuicBandwidth bandwidth_latest() const { return bandwidth_latest_; }
372 QuicBandwidth bandwidth_lo() const { return bandwidth_lo_; }
373 void clear_bandwidth_lo() { bandwidth_lo_ = QuicBandwidth::Infinite(); }
374
375 QuicByteCount inflight_latest() const { return inflight_latest_; }
376 QuicByteCount inflight_lo() const { return inflight_lo_; }
377 static QuicByteCount inflight_lo_default() {
378 return std::numeric_limits<QuicByteCount>::max();
379 }
380 void clear_inflight_lo() { inflight_lo_ = inflight_lo_default(); }
381
382 QuicByteCount inflight_hi_with_headroom() const;
383 QuicByteCount inflight_hi() const { return inflight_hi_; }
384 static QuicByteCount inflight_hi_default() {
385 return std::numeric_limits<QuicByteCount>::max();
386 }
387 void set_inflight_hi(QuicByteCount inflight_hi) {
388 inflight_hi_ = inflight_hi;
389 }
390
391 float cwnd_gain() const { return cwnd_gain_; }
392 void set_cwnd_gain(float cwnd_gain) { cwnd_gain_ = cwnd_gain; }
393
394 float pacing_gain() const { return pacing_gain_; }
395 void set_pacing_gain(float pacing_gain) { pacing_gain_ = pacing_gain; }
396
397 private:
398 const Bbr2Params& Params() const { return *params_; }
399 const Bbr2Params* const params_;
400 RoundTripCounter round_trip_counter_;
401
402 // Bandwidth sampler provides BBR with the bandwidth measurements at
403 // individual points.
404 BandwidthSampler bandwidth_sampler_;
405 // The filter that tracks the maximum bandwidth over multiple recent round
406 // trips.
407 Bbr2MaxBandwidthFilter max_bandwidth_filter_;
408 MinRttFilter min_rtt_filter_;
409
QUICHE team7872c772019-07-23 10:19:37 -0400410 // Bytes lost in the current round. Updated once per congestion event.
411 QuicByteCount bytes_lost_in_round_ = 0;
412
413 // Max bandwidth in the current round. Updated once per congestion event.
414 QuicBandwidth bandwidth_latest_ = QuicBandwidth::Zero();
415 // Max bandwidth of recent rounds. Updated once per round.
416 QuicBandwidth bandwidth_lo_ = QuicBandwidth::Infinite();
417
418 // Max inflight in the current round. Updated once per congestion event.
419 QuicByteCount inflight_latest_ = 0;
420 // Max inflight of recent rounds. Updated once per round.
421 QuicByteCount inflight_lo_ = inflight_lo_default();
422 QuicByteCount inflight_hi_ = inflight_hi_default();
423
424 float cwnd_gain_;
425 float pacing_gain_;
426};
427
428enum class Bbr2Mode : uint8_t {
429 // Startup phase of the connection.
430 STARTUP,
431 // After achieving the highest possible bandwidth during the startup, lower
432 // the pacing rate in order to drain the queue.
433 DRAIN,
434 // Cruising mode.
435 PROBE_BW,
436 // Temporarily slow down sending in order to empty the buffer and measure
437 // the real minimum RTT.
438 PROBE_RTT,
439};
440
441QUIC_EXPORT_PRIVATE inline std::ostream& operator<<(std::ostream& os,
442 const Bbr2Mode& mode) {
443 switch (mode) {
444 case Bbr2Mode::STARTUP:
445 return os << "STARTUP";
446 case Bbr2Mode::DRAIN:
447 return os << "DRAIN";
448 case Bbr2Mode::PROBE_BW:
449 return os << "PROBE_BW";
450 case Bbr2Mode::PROBE_RTT:
451 return os << "PROBE_RTT";
452 }
453 return os << "<Invalid Mode>";
454}
455
456// The base class for all BBRv2 modes. A Bbr2Sender is in one mode at a time,
457// this interface is used to implement mode-specific behaviors.
458class Bbr2Sender;
459class QUIC_EXPORT_PRIVATE Bbr2ModeBase {
460 public:
461 Bbr2ModeBase(const Bbr2Sender* sender, Bbr2NetworkModel* model)
462 : sender_(sender), model_(model) {}
463
464 virtual ~Bbr2ModeBase() = default;
465
466 virtual void Enter(const Bbr2CongestionEvent& congestion_event) = 0;
467
468 virtual Bbr2Mode OnCongestionEvent(
469 QuicByteCount prior_in_flight,
470 QuicTime event_time,
471 const AckedPacketVector& acked_packets,
472 const LostPacketVector& lost_packets,
473 const Bbr2CongestionEvent& congestion_event) = 0;
474
475 virtual Limits<QuicByteCount> GetCwndLimits() const = 0;
476
477 virtual bool IsProbingForBandwidth() const = 0;
478
479 protected:
480 const Bbr2Sender* const sender_;
481 Bbr2NetworkModel* model_;
482};
483
484QUIC_EXPORT_PRIVATE inline QuicByteCount BytesInFlight(
485 const SendTimeState& send_state) {
486 DCHECK(send_state.is_valid);
487 return send_state.total_bytes_sent - send_state.total_bytes_acked -
488 send_state.total_bytes_lost;
489}
490
491} // namespace quic
492
493#endif // QUICHE_QUIC_CORE_CONGESTION_CONTROL_BBR2_MISC_H_