• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 "quiche/quic/core/congestion_control/pacing_sender.h"
6 
7 #include "quiche/quic/core/quic_bandwidth.h"
8 #include "quiche/quic/platform/api/quic_flag_utils.h"
9 #include "quiche/quic/platform/api/quic_flags.h"
10 #include "quiche/quic/platform/api/quic_logging.h"
11 
12 namespace quic {
13 namespace {
14 
15 // Configured maximum size of the burst coming out of quiescence.  The burst
16 // is never larger than the current CWND in packets.
17 static const uint32_t kInitialUnpacedBurst = 10;
18 
19 }  // namespace
20 
PacingSender()21 PacingSender::PacingSender()
22     : sender_(nullptr),
23       max_pacing_rate_(QuicBandwidth::Zero()),
24       burst_tokens_(kInitialUnpacedBurst),
25       ideal_next_packet_send_time_(QuicTime::Zero()),
26       initial_burst_size_(kInitialUnpacedBurst),
27       lumpy_tokens_(0),
28       alarm_granularity_(kAlarmGranularity),
29       pacing_limited_(false) {}
30 
~PacingSender()31 PacingSender::~PacingSender() {}
32 
set_sender(SendAlgorithmInterface * sender)33 void PacingSender::set_sender(SendAlgorithmInterface* sender) {
34   QUICHE_DCHECK(sender != nullptr);
35   sender_ = sender;
36 }
37 
OnCongestionEvent(bool rtt_updated,QuicByteCount bytes_in_flight,QuicTime event_time,const AckedPacketVector & acked_packets,const LostPacketVector & lost_packets,QuicPacketCount num_ect,QuicPacketCount num_ce)38 void PacingSender::OnCongestionEvent(bool rtt_updated,
39                                      QuicByteCount bytes_in_flight,
40                                      QuicTime event_time,
41                                      const AckedPacketVector& acked_packets,
42                                      const LostPacketVector& lost_packets,
43                                      QuicPacketCount num_ect,
44                                      QuicPacketCount num_ce) {
45   QUICHE_DCHECK(sender_ != nullptr);
46   if (!lost_packets.empty()) {
47     // Clear any burst tokens when entering recovery.
48     burst_tokens_ = 0;
49   }
50   sender_->OnCongestionEvent(rtt_updated, bytes_in_flight, event_time,
51                              acked_packets, lost_packets, num_ect, num_ce);
52 }
53 
OnPacketSent(QuicTime sent_time,QuicByteCount bytes_in_flight,QuicPacketNumber packet_number,QuicByteCount bytes,HasRetransmittableData has_retransmittable_data)54 void PacingSender::OnPacketSent(
55     QuicTime sent_time, QuicByteCount bytes_in_flight,
56     QuicPacketNumber packet_number, QuicByteCount bytes,
57     HasRetransmittableData has_retransmittable_data) {
58   QUICHE_DCHECK(sender_ != nullptr);
59   sender_->OnPacketSent(sent_time, bytes_in_flight, packet_number, bytes,
60                         has_retransmittable_data);
61   if (has_retransmittable_data != HAS_RETRANSMITTABLE_DATA) {
62     return;
63   }
64   // If in recovery, the connection is not coming out of quiescence.
65   if (bytes_in_flight == 0 && !sender_->InRecovery()) {
66     // Add more burst tokens anytime the connection is leaving quiescence, but
67     // limit it to the equivalent of a single bulk write, not exceeding the
68     // current CWND in packets.
69     burst_tokens_ = std::min(
70         initial_burst_size_,
71         static_cast<uint32_t>(sender_->GetCongestionWindow() / kDefaultTCPMSS));
72   }
73   if (burst_tokens_ > 0) {
74     --burst_tokens_;
75     ideal_next_packet_send_time_ = QuicTime::Zero();
76     pacing_limited_ = false;
77     return;
78   }
79   // The next packet should be sent as soon as the current packet has been
80   // transferred.  PacingRate is based on bytes in flight including this packet.
81   QuicTime::Delta delay =
82       PacingRate(bytes_in_flight + bytes).TransferTime(bytes);
83   if (!pacing_limited_ || lumpy_tokens_ == 0) {
84     // Reset lumpy_tokens_ if either application or cwnd throttles sending or
85     // token runs out.
86     lumpy_tokens_ = std::max(
87         1u, std::min(static_cast<uint32_t>(GetQuicFlag(quic_lumpy_pacing_size)),
88                      static_cast<uint32_t>(
89                          (sender_->GetCongestionWindow() *
90                           GetQuicFlag(quic_lumpy_pacing_cwnd_fraction)) /
91                          kDefaultTCPMSS)));
92     if (sender_->BandwidthEstimate() <
93         QuicBandwidth::FromKBitsPerSecond(
94             GetQuicFlag(quic_lumpy_pacing_min_bandwidth_kbps))) {
95       // Below 1.2Mbps, send 1 packet at once, because one full-sized packet
96       // is about 10ms of queueing.
97       lumpy_tokens_ = 1u;
98     }
99     if ((bytes_in_flight + bytes) >= sender_->GetCongestionWindow()) {
100       // Don't add lumpy_tokens if the congestion controller is CWND limited.
101       lumpy_tokens_ = 1u;
102     }
103   }
104   --lumpy_tokens_;
105   if (pacing_limited_) {
106     // Make up for lost time since pacing throttles the sending.
107     ideal_next_packet_send_time_ = ideal_next_packet_send_time_ + delay;
108   } else {
109     ideal_next_packet_send_time_ =
110         std::max(ideal_next_packet_send_time_ + delay, sent_time + delay);
111   }
112   // Stop making up for lost time if underlying sender prevents sending.
113   pacing_limited_ = sender_->CanSend(bytes_in_flight + bytes);
114 }
115 
OnApplicationLimited()116 void PacingSender::OnApplicationLimited() {
117   // The send is application limited, stop making up for lost time.
118   pacing_limited_ = false;
119 }
120 
SetBurstTokens(uint32_t burst_tokens)121 void PacingSender::SetBurstTokens(uint32_t burst_tokens) {
122   initial_burst_size_ = burst_tokens;
123   burst_tokens_ = std::min(
124       initial_burst_size_,
125       static_cast<uint32_t>(sender_->GetCongestionWindow() / kDefaultTCPMSS));
126 }
127 
TimeUntilSend(QuicTime now,QuicByteCount bytes_in_flight) const128 QuicTime::Delta PacingSender::TimeUntilSend(
129     QuicTime now, QuicByteCount bytes_in_flight) const {
130   QUICHE_DCHECK(sender_ != nullptr);
131 
132   if (!sender_->CanSend(bytes_in_flight)) {
133     // The underlying sender prevents sending.
134     return QuicTime::Delta::Infinite();
135   }
136 
137   if (burst_tokens_ > 0 || bytes_in_flight == 0 || lumpy_tokens_ > 0) {
138     // Don't pace if we have burst tokens available or leaving quiescence.
139     QUIC_DVLOG(1) << "Sending packet now. burst_tokens:" << burst_tokens_
140                   << ", bytes_in_flight:" << bytes_in_flight
141                   << ", lumpy_tokens:" << lumpy_tokens_;
142     return QuicTime::Delta::Zero();
143   }
144 
145   // If the next send time is within the alarm granularity, send immediately.
146   if (ideal_next_packet_send_time_ > now + alarm_granularity_) {
147     QUIC_DVLOG(1) << "Delaying packet: "
148                   << (ideal_next_packet_send_time_ - now).ToMicroseconds();
149     return ideal_next_packet_send_time_ - now;
150   }
151 
152   QUIC_DVLOG(1) << "Sending packet now. ideal_next_packet_send_time: "
153                 << ideal_next_packet_send_time_ << ", now: " << now;
154   return QuicTime::Delta::Zero();
155 }
156 
PacingRate(QuicByteCount bytes_in_flight) const157 QuicBandwidth PacingSender::PacingRate(QuicByteCount bytes_in_flight) const {
158   QUICHE_DCHECK(sender_ != nullptr);
159   if (!max_pacing_rate_.IsZero()) {
160     return QuicBandwidth::FromBitsPerSecond(
161         std::min(max_pacing_rate_.ToBitsPerSecond(),
162                  sender_->PacingRate(bytes_in_flight).ToBitsPerSecond()));
163   }
164   return sender_->PacingRate(bytes_in_flight);
165 }
166 
167 }  // namespace quic
168