QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 1 | // 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" |
wub | e4df93b | 2020-03-02 17:55:23 -0800 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 17 | |
| 18 | namespace quic { |
| 19 | |
| 20 | namespace { |
| 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. |
| 24 | const QuicByteCount kDefaultMinimumCongestionWindow = 4 * kMaxSegmentSize; |
| 25 | |
| 26 | const float kInitialPacingGain = 2.885f; |
| 27 | |
| 28 | const 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 | |
| 55 | Bbr2Sender::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, |
wub | 0abcd8f | 2020-03-30 16:24:50 -0700 | [diff] [blame] | 61 | QuicConnectionStats* stats, |
| 62 | BbrSender* old_sender) |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 63 | : mode_(Bbr2Mode::STARTUP), |
| 64 | rtt_stats_(rtt_stats), |
| 65 | unacked_packets_(unacked_packets), |
| 66 | random_(random), |
wub | be29b9e | 2019-11-24 06:56:04 -0800 | [diff] [blame] | 67 | connection_stats_(stats), |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 68 | params_(kDefaultMinimumCongestionWindow, |
| 69 | max_cwnd_in_packets * kDefaultTCPMSS), |
| 70 | model_(¶ms_, |
| 71 | rtt_stats->SmoothedOrInitialRtt(), |
| 72 | rtt_stats->last_update_time(), |
| 73 | /*cwnd_gain=*/1.0, |
wub | 0abcd8f | 2020-03-30 16:24:50 -0700 | [diff] [blame] | 74 | /*pacing_gain=*/kInitialPacingGain, |
| 75 | old_sender ? &old_sender->sampler_ : nullptr), |
wub | 7652d41 | 2019-08-13 15:11:40 -0700 | [diff] [blame] | 76 | initial_cwnd_( |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 77 | cwnd_limits().ApplyLimits(initial_cwnd_in_packets * kDefaultTCPMSS)), |
wub | 7652d41 | 2019-08-13 15:11:40 -0700 | [diff] [blame] | 78 | cwnd_(initial_cwnd_), |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 79 | pacing_rate_(kInitialPacingGain * QuicBandwidth::FromBytesAndTimeDelta( |
| 80 | cwnd_, |
| 81 | rtt_stats->SmoothedOrInitialRtt())), |
wub | be29b9e | 2019-11-24 06:56:04 -0800 | [diff] [blame] | 82 | startup_(this, &model_, now), |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 83 | drain_(this, &model_), |
| 84 | probe_bw_(this, &model_), |
| 85 | probe_rtt_(this, &model_), |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 86 | last_sample_is_app_limited_(false) { |
| 87 | QUIC_DVLOG(2) << this << " Initializing Bbr2Sender. mode:" << mode_ |
| 88 | << ", PacingRate:" << pacing_rate_ << ", Cwnd:" << cwnd_ |
| 89 | << ", CwndLimits:" << cwnd_limits() << " @ " << now; |
| 90 | DCHECK_EQ(mode_, Bbr2Mode::STARTUP); |
| 91 | } |
| 92 | |
| 93 | void Bbr2Sender::SetFromConfig(const QuicConfig& config, |
| 94 | Perspective perspective) { |
| 95 | if (config.HasClientRequestedIndependentOption(kBBR9, perspective)) { |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 96 | params_.flexible_app_limited = true; |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 97 | } |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 98 | if (config.HasClientRequestedIndependentOption(kB2NA, perspective)) { |
| 99 | params_.add_ack_height_to_queueing_threshold = false; |
| 100 | } |
| 101 | if (config.HasClientRequestedIndependentOption(kB2RP, perspective)) { |
| 102 | params_.avoid_unnecessary_probe_rtt = false; |
| 103 | } |
wub | cc13f59 | 2020-04-16 12:40:30 -0700 | [diff] [blame] | 104 | if (GetQuicReloadableFlag(quic_bbr2_avoid_too_low_probe_bw_cwnd) && |
| 105 | config.HasClientRequestedIndependentOption(kB2CL, perspective)) { |
| 106 | params_.avoid_too_low_probe_bw_cwnd = false; |
| 107 | } |
ianswett | 1e9525d | 2020-04-16 14:42:51 -0700 | [diff] [blame] | 108 | if (GetQuicReloadableFlag(quic_bbr2_fewer_startup_round_trips) && |
| 109 | config.HasClientRequestedIndependentOption(k1RTT, perspective)) { |
| 110 | QUIC_RELOADABLE_FLAG_COUNT_N(quic_bbr2_fewer_startup_round_trips, 1, 2); |
| 111 | params_.startup_full_bw_rounds = 1; |
| 112 | } |
| 113 | if (GetQuicReloadableFlag(quic_bbr2_fewer_startup_round_trips) && |
| 114 | config.HasClientRequestedIndependentOption(k2RTT, perspective)) { |
| 115 | QUIC_RELOADABLE_FLAG_COUNT_N(quic_bbr2_fewer_startup_round_trips, 2, 2); |
| 116 | params_.startup_full_bw_rounds = 2; |
| 117 | } |
ianswett | 64634b2 | 2020-08-31 09:30:23 -0700 | [diff] [blame] | 118 | if (config.HasClientRequestedIndependentOption(kB2LO, perspective)) { |
ianswett | 9b94db4 | 2020-04-27 12:21:32 -0700 | [diff] [blame] | 119 | params_.ignore_inflight_lo = true; |
| 120 | } |
ianswett | 88554d6 | 2020-05-25 11:33:07 -0700 | [diff] [blame] | 121 | if (GetQuicReloadableFlag(quic_bbr2_limit_inflight_hi) && |
| 122 | config.HasClientRequestedIndependentOption(kB2HI, perspective)) { |
| 123 | QUIC_RELOADABLE_FLAG_COUNT(quic_bbr2_limit_inflight_hi); |
| 124 | params_.limit_inflight_hi_by_cwnd = true; |
| 125 | } |
wub | d09f1a6 | 2020-05-04 06:57:40 -0700 | [diff] [blame] | 126 | |
| 127 | ApplyConnectionOptions(config.ClientRequestedIndependentOptions(perspective)); |
| 128 | } |
| 129 | |
| 130 | void Bbr2Sender::ApplyConnectionOptions( |
| 131 | const QuicTagVector& connection_options) { |
ianswett | 0c54e87 | 2020-05-18 12:24:05 -0700 | [diff] [blame] | 132 | if (ContainsQuicTag(connection_options, kBBQ2)) { |
wub | d09f1a6 | 2020-05-04 06:57:40 -0700 | [diff] [blame] | 133 | // 2 is the lower, derived gain for CWND. |
| 134 | params_.startup_cwnd_gain = 2; |
| 135 | params_.drain_cwnd_gain = 2; |
| 136 | } |
wub | a0d2a57 | 2020-07-16 14:30:57 -0700 | [diff] [blame] | 137 | if (ContainsQuicTag(connection_options, kBSAO)) { |
| 138 | model_.EnableOverestimateAvoidance(); |
| 139 | } |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | Limits<QuicByteCount> Bbr2Sender::GetCwndLimitsByMode() const { |
| 143 | switch (mode_) { |
| 144 | case Bbr2Mode::STARTUP: |
| 145 | return startup_.GetCwndLimits(); |
| 146 | case Bbr2Mode::PROBE_BW: |
| 147 | return probe_bw_.GetCwndLimits(); |
| 148 | case Bbr2Mode::DRAIN: |
| 149 | return drain_.GetCwndLimits(); |
| 150 | case Bbr2Mode::PROBE_RTT: |
| 151 | return probe_rtt_.GetCwndLimits(); |
| 152 | default: |
| 153 | QUIC_NOTREACHED(); |
| 154 | return Unlimited<QuicByteCount>(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | const Limits<QuicByteCount>& Bbr2Sender::cwnd_limits() const { |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 159 | return params().cwnd_limits; |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 160 | } |
| 161 | |
QUICHE team | fdcfe3b | 2019-11-06 10:54:25 -0800 | [diff] [blame] | 162 | void Bbr2Sender::AdjustNetworkParameters(const NetworkParams& params) { |
wub | 5b8985a | 2020-09-08 07:45:36 -0700 | [diff] [blame] | 163 | model_.UpdateNetworkParameters(params.rtt); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 164 | |
| 165 | if (mode_ == Bbr2Mode::STARTUP) { |
| 166 | const QuicByteCount prior_cwnd = cwnd_; |
| 167 | |
wub | 5b8985a | 2020-09-08 07:45:36 -0700 | [diff] [blame] | 168 | QuicBandwidth effective_bandwidth = |
| 169 | std::max(params.bandwidth, model_.BandwidthEstimate()); |
| 170 | cwnd_ = cwnd_limits().ApplyLimits(model_.BDP(effective_bandwidth)); |
wub | e4949fb | 2020-07-14 11:28:01 -0700 | [diff] [blame] | 171 | |
QUICHE team | b4e187c | 2019-11-14 06:22:50 -0800 | [diff] [blame] | 172 | if (!params.allow_cwnd_to_decrease) { |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 173 | cwnd_ = std::max(cwnd_, prior_cwnd); |
| 174 | } |
wub | e4949fb | 2020-07-14 11:28:01 -0700 | [diff] [blame] | 175 | |
wub | 5b8985a | 2020-09-08 07:45:36 -0700 | [diff] [blame] | 176 | pacing_rate_ = std::max(pacing_rate_, QuicBandwidth::FromBytesAndTimeDelta( |
| 177 | cwnd_, model_.MinRtt())); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | void Bbr2Sender::SetInitialCongestionWindowInPackets( |
| 182 | QuicPacketCount congestion_window) { |
| 183 | if (mode_ == Bbr2Mode::STARTUP) { |
| 184 | // The cwnd limits is unchanged and still applies to the new cwnd. |
| 185 | cwnd_ = cwnd_limits().ApplyLimits(congestion_window * kDefaultTCPMSS); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void Bbr2Sender::OnCongestionEvent(bool /*rtt_updated*/, |
| 190 | QuicByteCount prior_in_flight, |
| 191 | QuicTime event_time, |
| 192 | const AckedPacketVector& acked_packets, |
| 193 | const LostPacketVector& lost_packets) { |
| 194 | QUIC_DVLOG(3) << this |
| 195 | << " OnCongestionEvent. prior_in_flight:" << prior_in_flight |
| 196 | << " prior_cwnd:" << cwnd_ << " @ " << event_time; |
| 197 | Bbr2CongestionEvent congestion_event; |
| 198 | congestion_event.prior_cwnd = cwnd_; |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 199 | congestion_event.prior_bytes_in_flight = prior_in_flight; |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 200 | congestion_event.is_probing_for_bandwidth = |
| 201 | BBR2_MODE_DISPATCH(IsProbingForBandwidth()); |
| 202 | |
| 203 | model_.OnCongestionEventStart(event_time, acked_packets, lost_packets, |
| 204 | &congestion_event); |
| 205 | |
| 206 | // Number of mode changes allowed for this congestion event. |
| 207 | int mode_changes_allowed = kMaxModeChangesPerCongestionEvent; |
| 208 | while (true) { |
| 209 | Bbr2Mode next_mode = BBR2_MODE_DISPATCH( |
| 210 | OnCongestionEvent(prior_in_flight, event_time, acked_packets, |
| 211 | lost_packets, congestion_event)); |
| 212 | |
| 213 | if (next_mode == mode_) { |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | QUIC_DVLOG(2) << this << " Mode change: " << mode_ << " ==> " << next_mode |
| 218 | << " @ " << event_time; |
wub | 81f488f | 2020-03-02 10:49:35 -0800 | [diff] [blame] | 219 | BBR2_MODE_DISPATCH(Leave(event_time, &congestion_event)); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 220 | mode_ = next_mode; |
wub | 81f488f | 2020-03-02 10:49:35 -0800 | [diff] [blame] | 221 | BBR2_MODE_DISPATCH(Enter(event_time, &congestion_event)); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 222 | --mode_changes_allowed; |
| 223 | if (mode_changes_allowed < 0) { |
| 224 | QUIC_BUG << "Exceeded max number of mode changes per congestion event."; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | UpdatePacingRate(congestion_event.bytes_acked); |
| 230 | QUIC_BUG_IF(pacing_rate_.IsZero()) << "Pacing rate must not be zero!"; |
| 231 | |
| 232 | UpdateCongestionWindow(congestion_event.bytes_acked); |
| 233 | QUIC_BUG_IF(cwnd_ == 0u) << "Congestion window must not be zero!"; |
| 234 | |
| 235 | model_.OnCongestionEventFinish(unacked_packets_->GetLeastUnacked(), |
| 236 | congestion_event); |
| 237 | last_sample_is_app_limited_ = congestion_event.last_sample_is_app_limited; |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 238 | if (congestion_event.bytes_in_flight == 0 && |
| 239 | params().avoid_unnecessary_probe_rtt) { |
wub | e4df93b | 2020-03-02 17:55:23 -0800 | [diff] [blame] | 240 | OnEnterQuiescence(event_time); |
| 241 | } |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 242 | |
| 243 | QUIC_DVLOG(3) |
| 244 | << this << " END CongestionEvent(acked:" << acked_packets |
| 245 | << ", lost:" << lost_packets.size() << ") " |
| 246 | << ", Mode:" << mode_ << ", RttCount:" << model_.RoundTripCount() |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 247 | << ", BytesInFlight:" << congestion_event.bytes_in_flight |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 248 | << ", PacingRate:" << PacingRate(0) << ", CWND:" << GetCongestionWindow() |
| 249 | << ", PacingGain:" << model_.pacing_gain() |
| 250 | << ", CwndGain:" << model_.cwnd_gain() |
| 251 | << ", BandwidthEstimate(kbps):" << BandwidthEstimate().ToKBitsPerSecond() |
| 252 | << ", MinRTT(us):" << model_.MinRtt().ToMicroseconds() |
| 253 | << ", BDP:" << model_.BDP(BandwidthEstimate()) |
| 254 | << ", BandwidthLatest(kbps):" |
| 255 | << model_.bandwidth_latest().ToKBitsPerSecond() |
| 256 | << ", BandwidthLow(kbps):" << model_.bandwidth_lo().ToKBitsPerSecond() |
| 257 | << ", BandwidthHigh(kbps):" << model_.MaxBandwidth().ToKBitsPerSecond() |
| 258 | << ", InflightLatest:" << model_.inflight_latest() |
| 259 | << ", InflightLow:" << model_.inflight_lo() |
| 260 | << ", InflightHigh:" << model_.inflight_hi() |
| 261 | << ", TotalAcked:" << model_.total_bytes_acked() |
| 262 | << ", TotalLost:" << model_.total_bytes_lost() |
| 263 | << ", TotalSent:" << model_.total_bytes_sent() << " @ " << event_time; |
| 264 | } |
| 265 | |
| 266 | void Bbr2Sender::UpdatePacingRate(QuicByteCount bytes_acked) { |
| 267 | if (BandwidthEstimate().IsZero()) { |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | if (model_.total_bytes_acked() == bytes_acked) { |
| 272 | // After the first ACK, cwnd_ is still the initial congestion window. |
| 273 | pacing_rate_ = QuicBandwidth::FromBytesAndTimeDelta(cwnd_, model_.MinRtt()); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | QuicBandwidth target_rate = model_.pacing_gain() * model_.BandwidthEstimate(); |
| 278 | if (startup_.FullBandwidthReached()) { |
| 279 | pacing_rate_ = target_rate; |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (target_rate > pacing_rate_) { |
| 284 | pacing_rate_ = target_rate; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void Bbr2Sender::UpdateCongestionWindow(QuicByteCount bytes_acked) { |
| 289 | QuicByteCount target_cwnd = GetTargetCongestionWindow(model_.cwnd_gain()); |
| 290 | |
| 291 | const QuicByteCount prior_cwnd = cwnd_; |
| 292 | if (startup_.FullBandwidthReached()) { |
| 293 | target_cwnd += model_.MaxAckHeight(); |
| 294 | cwnd_ = std::min(prior_cwnd + bytes_acked, target_cwnd); |
wub | 7652d41 | 2019-08-13 15:11:40 -0700 | [diff] [blame] | 295 | } else if (prior_cwnd < target_cwnd || prior_cwnd < 2 * initial_cwnd_) { |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 296 | cwnd_ = prior_cwnd + bytes_acked; |
| 297 | } |
| 298 | const QuicByteCount desired_cwnd = cwnd_; |
| 299 | |
| 300 | cwnd_ = GetCwndLimitsByMode().ApplyLimits(cwnd_); |
| 301 | const QuicByteCount model_limited_cwnd = cwnd_; |
| 302 | |
| 303 | cwnd_ = cwnd_limits().ApplyLimits(cwnd_); |
| 304 | |
| 305 | QUIC_DVLOG(3) << this << " Updating CWND. target_cwnd:" << target_cwnd |
| 306 | << ", max_ack_height:" << model_.MaxAckHeight() |
| 307 | << ", full_bw:" << startup_.FullBandwidthReached() |
| 308 | << ", bytes_acked:" << bytes_acked |
| 309 | << ", inflight_lo:" << model_.inflight_lo() |
| 310 | << ", inflight_hi:" << model_.inflight_hi() << ". (prior_cwnd) " |
| 311 | << prior_cwnd << " => (desired_cwnd) " << desired_cwnd |
| 312 | << " => (model_limited_cwnd) " << model_limited_cwnd |
| 313 | << " => (final_cwnd) " << cwnd_; |
| 314 | } |
| 315 | |
| 316 | QuicByteCount Bbr2Sender::GetTargetCongestionWindow(float gain) const { |
| 317 | return std::max(model_.BDP(model_.BandwidthEstimate(), gain), |
| 318 | cwnd_limits().Min()); |
| 319 | } |
| 320 | |
| 321 | void Bbr2Sender::OnPacketSent(QuicTime sent_time, |
| 322 | QuicByteCount bytes_in_flight, |
| 323 | QuicPacketNumber packet_number, |
| 324 | QuicByteCount bytes, |
| 325 | HasRetransmittableData is_retransmittable) { |
| 326 | QUIC_DVLOG(3) << this << " OnPacketSent: pkn:" << packet_number |
| 327 | << ", bytes:" << bytes << ", cwnd:" << cwnd_ |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 328 | << ", inflight:" << bytes_in_flight + bytes |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 329 | << ", total_sent:" << model_.total_bytes_sent() + bytes |
| 330 | << ", total_acked:" << model_.total_bytes_acked() |
| 331 | << ", total_lost:" << model_.total_bytes_lost() << " @ " |
| 332 | << sent_time; |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 333 | if (bytes_in_flight == 0 && params().avoid_unnecessary_probe_rtt) { |
wub | e4df93b | 2020-03-02 17:55:23 -0800 | [diff] [blame] | 334 | OnExitQuiescence(sent_time); |
| 335 | } |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 336 | model_.OnPacketSent(sent_time, bytes_in_flight, packet_number, bytes, |
| 337 | is_retransmittable); |
| 338 | } |
| 339 | |
wub | f4ab965 | 2020-02-20 14:45:43 -0800 | [diff] [blame] | 340 | void Bbr2Sender::OnPacketNeutered(QuicPacketNumber packet_number) { |
| 341 | model_.OnPacketNeutered(packet_number); |
| 342 | } |
| 343 | |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 344 | bool Bbr2Sender::CanSend(QuicByteCount bytes_in_flight) { |
| 345 | const bool result = bytes_in_flight < GetCongestionWindow(); |
| 346 | return result; |
| 347 | } |
| 348 | |
| 349 | QuicByteCount Bbr2Sender::GetCongestionWindow() const { |
| 350 | // TODO(wub): Implement Recovery? |
| 351 | return cwnd_; |
| 352 | } |
| 353 | |
| 354 | QuicBandwidth Bbr2Sender::PacingRate(QuicByteCount /*bytes_in_flight*/) const { |
| 355 | return pacing_rate_; |
| 356 | } |
| 357 | |
| 358 | void Bbr2Sender::OnApplicationLimited(QuicByteCount bytes_in_flight) { |
| 359 | if (bytes_in_flight >= GetCongestionWindow()) { |
| 360 | return; |
| 361 | } |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 362 | if (params().flexible_app_limited && IsPipeSufficientlyFull()) { |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
| 366 | model_.OnApplicationLimited(); |
| 367 | QUIC_DVLOG(2) << this << " Becoming application limited. Last sent packet: " |
| 368 | << model_.last_sent_packet() |
| 369 | << ", CWND: " << GetCongestionWindow(); |
| 370 | } |
| 371 | |
wub | 2957990 | 2019-12-18 10:16:17 -0800 | [diff] [blame] | 372 | QuicByteCount Bbr2Sender::GetTargetBytesInflight() const { |
| 373 | QuicByteCount bdp = model_.BDP(model_.BandwidthEstimate()); |
| 374 | return std::min(bdp, GetCongestionWindow()); |
| 375 | } |
| 376 | |
wub | 5cd4959 | 2019-11-25 15:17:13 -0800 | [diff] [blame] | 377 | void Bbr2Sender::PopulateConnectionStats(QuicConnectionStats* stats) const { |
| 378 | stats->num_ack_aggregation_epochs = model_.num_ack_aggregation_epochs(); |
| 379 | } |
| 380 | |
wub | e4df93b | 2020-03-02 17:55:23 -0800 | [diff] [blame] | 381 | void Bbr2Sender::OnEnterQuiescence(QuicTime now) { |
| 382 | last_quiescence_start_ = now; |
| 383 | } |
| 384 | |
| 385 | void Bbr2Sender::OnExitQuiescence(QuicTime now) { |
| 386 | if (last_quiescence_start_ != QuicTime::Zero()) { |
| 387 | Bbr2Mode next_mode = BBR2_MODE_DISPATCH( |
| 388 | OnExitQuiescence(now, std::min(now, last_quiescence_start_))); |
| 389 | if (next_mode != mode_) { |
| 390 | BBR2_MODE_DISPATCH(Leave(now, nullptr)); |
| 391 | mode_ = next_mode; |
| 392 | BBR2_MODE_DISPATCH(Enter(now, nullptr)); |
| 393 | } |
| 394 | last_quiescence_start_ = QuicTime::Zero(); |
| 395 | } |
| 396 | } |
| 397 | |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 398 | bool Bbr2Sender::ShouldSendProbingPacket() const { |
| 399 | // TODO(wub): Implement ShouldSendProbingPacket properly. |
| 400 | if (!BBR2_MODE_DISPATCH(IsProbingForBandwidth())) { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | // TODO(b/77975811): If the pipe is highly under-utilized, consider not |
| 405 | // sending a probing transmission, because the extra bandwidth is not needed. |
| 406 | // If flexible_app_limited is enabled, check if the pipe is sufficiently full. |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 407 | if (params().flexible_app_limited) { |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 408 | const bool is_pipe_sufficiently_full = IsPipeSufficientlyFull(); |
| 409 | QUIC_DVLOG(3) << this << " CWND: " << GetCongestionWindow() |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 410 | << ", inflight: " << unacked_packets_->bytes_in_flight() |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 411 | << ", pacing_rate: " << PacingRate(0) |
wub | f929323 | 2020-03-16 09:43:00 -0700 | [diff] [blame] | 412 | << ", flexible_app_limited: true, ShouldSendProbingPacket: " |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 413 | << !is_pipe_sufficiently_full; |
| 414 | return !is_pipe_sufficiently_full; |
| 415 | } else { |
| 416 | return true; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | bool Bbr2Sender::IsPipeSufficientlyFull() const { |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 421 | QuicByteCount bytes_in_flight = unacked_packets_->bytes_in_flight(); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 422 | // See if we need more bytes in flight to see more bandwidth. |
| 423 | if (mode_ == Bbr2Mode::STARTUP) { |
| 424 | // STARTUP exits if it doesn't observe a 25% bandwidth increase, so the CWND |
| 425 | // must be more than 25% above the target. |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 426 | return bytes_in_flight >= GetTargetCongestionWindow(1.5); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 427 | } |
| 428 | if (model_.pacing_gain() > 1) { |
| 429 | // Super-unity PROBE_BW doesn't exit until 1.25 * BDP is achieved. |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 430 | return bytes_in_flight >= GetTargetCongestionWindow(model_.pacing_gain()); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 431 | } |
| 432 | // If bytes_in_flight are above the target congestion window, it should be |
| 433 | // possible to observe the same or more bandwidth if it's available. |
wub | a545ca1 | 2019-12-11 19:27:43 -0800 | [diff] [blame] | 434 | return bytes_in_flight >= GetTargetCongestionWindow(1.1); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | std::string Bbr2Sender::GetDebugState() const { |
| 438 | std::ostringstream stream; |
| 439 | stream << ExportDebugState(); |
| 440 | return stream.str(); |
| 441 | } |
| 442 | |
| 443 | Bbr2Sender::DebugState Bbr2Sender::ExportDebugState() const { |
| 444 | DebugState s; |
| 445 | s.mode = mode_; |
| 446 | s.round_trip_count = model_.RoundTripCount(); |
| 447 | s.bandwidth_hi = model_.MaxBandwidth(); |
| 448 | s.bandwidth_lo = model_.bandwidth_lo(); |
| 449 | s.bandwidth_est = BandwidthEstimate(); |
wub | b5a8b9e | 2019-10-18 03:07:25 -0700 | [diff] [blame] | 450 | s.inflight_hi = model_.inflight_hi(); |
| 451 | s.inflight_lo = model_.inflight_lo(); |
| 452 | s.max_ack_height = model_.MaxAckHeight(); |
QUICHE team | 7872c77 | 2019-07-23 10:19:37 -0400 | [diff] [blame] | 453 | s.min_rtt = model_.MinRtt(); |
| 454 | s.min_rtt_timestamp = model_.MinRttTimestamp(); |
| 455 | s.congestion_window = cwnd_; |
| 456 | s.pacing_rate = pacing_rate_; |
| 457 | s.last_sample_is_app_limited = last_sample_is_app_limited_; |
| 458 | s.end_of_app_limited_phase = model_.end_of_app_limited_phase(); |
| 459 | |
| 460 | s.startup = startup_.ExportDebugState(); |
| 461 | s.drain = drain_.ExportDebugState(); |
| 462 | s.probe_bw = probe_bw_.ExportDebugState(); |
| 463 | s.probe_rtt = probe_rtt_.ExportDebugState(); |
| 464 | |
| 465 | return s; |
| 466 | } |
| 467 | |
| 468 | std::ostream& operator<<(std::ostream& os, const Bbr2Sender::DebugState& s) { |
| 469 | os << "mode: " << s.mode << "\n"; |
| 470 | os << "round_trip_count: " << s.round_trip_count << "\n"; |
| 471 | os << "bandwidth_hi ~ lo ~ est: " << s.bandwidth_hi << " ~ " << s.bandwidth_lo |
| 472 | << " ~ " << s.bandwidth_est << "\n"; |
| 473 | os << "min_rtt: " << s.min_rtt << "\n"; |
| 474 | os << "min_rtt_timestamp: " << s.min_rtt_timestamp << "\n"; |
| 475 | os << "congestion_window: " << s.congestion_window << "\n"; |
| 476 | os << "pacing_rate: " << s.pacing_rate << "\n"; |
| 477 | os << "last_sample_is_app_limited: " << s.last_sample_is_app_limited << "\n"; |
| 478 | |
| 479 | if (s.mode == Bbr2Mode::STARTUP) { |
| 480 | os << s.startup; |
| 481 | } |
| 482 | |
| 483 | if (s.mode == Bbr2Mode::DRAIN) { |
| 484 | os << s.drain; |
| 485 | } |
| 486 | |
| 487 | if (s.mode == Bbr2Mode::PROBE_BW) { |
| 488 | os << s.probe_bw; |
| 489 | } |
| 490 | |
| 491 | if (s.mode == Bbr2Mode::PROBE_RTT) { |
| 492 | os << s.probe_rtt; |
| 493 | } |
| 494 | |
| 495 | return os; |
| 496 | } |
| 497 | |
| 498 | } // namespace quic |