1 // Copyright (c) 2012 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.
5 #include "net/quic/congestion_control/cubic.h"
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "base/time/time.h"
13 #include "net/quic/quic_flags.h"
14 #include "net/quic/quic_protocol.h"
22 // Constants based on TCP defaults.
23 // The following constants are in 2^10 fractions of a second instead of ms to
24 // allow a 10 shift right to divide.
25 const int kCubeScale
= 40; // 1024*1024^3 (first 1024 is from 0.100^3)
26 // where 0.100 is 100 ms which is the scaling
28 const int kCubeCongestionWindowScale
= 410;
29 const uint64 kCubeFactor
= (GG_UINT64_C(1) << kCubeScale
) /
30 kCubeCongestionWindowScale
;
32 const uint32 kDefaultNumConnections
= 2;
33 const float kBeta
= 0.7f
; // Default Cubic backoff factor.
34 // Additional backoff factor when loss occurs in the concave part of the Cubic
35 // curve. This additional backoff factor is expected to give up bandwidth to
36 // new concurrent flows and speed up convergence.
37 const float kBetaLastMax
= 0.85f
;
41 Cubic::Cubic(const QuicClock
* clock
)
43 num_connections_(kDefaultNumConnections
),
44 epoch_(QuicTime::Zero()),
45 last_update_time_(QuicTime::Zero()) {
49 void Cubic::SetNumConnections(int num_connections
) {
50 num_connections_
= num_connections
;
53 float Cubic::Alpha() const {
54 // TCPFriendly alpha is described in Section 3.3 of the CUBIC paper. Note that
55 // beta here is a cwnd multiplier, and is equal to 1-beta from the paper.
56 // We derive the equivalent alpha for an N-connection emulation as:
57 const float beta
= Beta();
58 return 3 * num_connections_
* num_connections_
* (1 - beta
) / (1 + beta
);
61 float Cubic::Beta() const {
62 // kNConnectionBeta is the backoff factor after loss for our N-connection
63 // emulation, which emulates the effective backoff of an ensemble of N
64 // TCP-Reno connections on a single loss event. The effective multiplier is
66 return (num_connections_
- 1 + kBeta
) / num_connections_
;
70 epoch_
= QuicTime::Zero(); // Reset time.
71 last_update_time_
= QuicTime::Zero(); // Reset time.
72 last_congestion_window_
= 0;
73 last_max_congestion_window_
= 0;
74 acked_packets_count_
= 0;
75 estimated_tcp_congestion_window_
= 0;
76 origin_point_congestion_window_
= 0;
77 time_to_origin_point_
= 0;
78 last_target_congestion_window_
= 0;
81 QuicPacketCount
Cubic::CongestionWindowAfterPacketLoss(
82 QuicPacketCount current_congestion_window
) {
83 if (current_congestion_window
< last_max_congestion_window_
) {
84 // We never reached the old max, so assume we are competing with another
85 // flow. Use our extra back off factor to allow the other flow to go up.
86 last_max_congestion_window_
=
87 static_cast<int>(kBetaLastMax
* current_congestion_window
);
89 last_max_congestion_window_
= current_congestion_window
;
91 epoch_
= QuicTime::Zero(); // Reset time.
92 return static_cast<int>(current_congestion_window
* Beta());
95 QuicPacketCount
Cubic::CongestionWindowAfterAck(
96 QuicPacketCount current_congestion_window
,
97 QuicTime::Delta delay_min
) {
98 acked_packets_count_
+= 1; // Packets acked.
99 QuicTime current_time
= clock_
->ApproximateNow();
101 // Cubic is "independent" of RTT, the update is limited by the time elapsed.
102 if (last_congestion_window_
== current_congestion_window
&&
103 (current_time
.Subtract(last_update_time_
) <= MaxCubicTimeInterval())) {
104 return max(last_target_congestion_window_
,
105 estimated_tcp_congestion_window_
);
107 last_congestion_window_
= current_congestion_window
;
108 last_update_time_
= current_time
;
110 if (!epoch_
.IsInitialized()) {
111 // First ACK after a loss event.
112 DVLOG(1) << "Start of epoch";
113 epoch_
= current_time
; // Start of epoch.
114 acked_packets_count_
= 1; // Reset count.
115 // Reset estimated_tcp_congestion_window_ to be in sync with cubic.
116 estimated_tcp_congestion_window_
= current_congestion_window
;
117 if (last_max_congestion_window_
<= current_congestion_window
) {
118 time_to_origin_point_
= 0;
119 origin_point_congestion_window_
= current_congestion_window
;
121 time_to_origin_point_
=
122 static_cast<uint32
>(cbrt(kCubeFactor
* (last_max_congestion_window_
-
123 current_congestion_window
)));
124 origin_point_congestion_window_
=
125 last_max_congestion_window_
;
128 // Change the time unit from microseconds to 2^10 fractions per second. Take
129 // the round trip time in account. This is done to allow us to use shift as a
132 (current_time
.Add(delay_min
).Subtract(epoch_
).ToMicroseconds() << 10) /
133 base::Time::kMicrosecondsPerSecond
;
135 int64 offset
= time_to_origin_point_
- elapsed_time
;
136 QuicPacketCount delta_congestion_window
= (kCubeCongestionWindowScale
137 * offset
* offset
* offset
) >> kCubeScale
;
139 QuicPacketCount target_congestion_window
=
140 origin_point_congestion_window_
- delta_congestion_window
;
142 DCHECK_LT(0u, estimated_tcp_congestion_window_
);
143 // With dynamic beta/alpha based on number of active streams, it is possible
144 // for the required_ack_count to become much lower than acked_packets_count_
145 // suddenly, leading to more than one iteration through the following loop.
147 // Update estimated TCP congestion_window.
148 QuicPacketCount required_ack_count
= static_cast<QuicPacketCount
>(
149 estimated_tcp_congestion_window_
/ Alpha());
150 if (acked_packets_count_
< required_ack_count
) {
153 acked_packets_count_
-= required_ack_count
;
154 estimated_tcp_congestion_window_
++;
157 // We have a new cubic congestion window.
158 last_target_congestion_window_
= target_congestion_window
;
160 // Compute target congestion_window based on cubic target and estimated TCP
161 // congestion_window, use highest (fastest).
162 if (target_congestion_window
< estimated_tcp_congestion_window_
) {
163 target_congestion_window
= estimated_tcp_congestion_window_
;
166 DVLOG(1) << "Target congestion_window: " << target_congestion_window
;
167 return target_congestion_window
;