2 * Binary Increase Congestion control for TCP
4 * This is from the implementation of BICTCP in
5 * Lison-Xu, Kahaled Harfoush, and Injong Rhee.
6 * "Binary Increase Congestion Control for Fast, Long Distance
7 * Networks" in InfoComm 2004
9 * http://www.csc.ncsu.edu/faculty/rhee/export/bitcp.pdf
11 * Unless BIC is enabled and congestion window is large
12 * this behaves the same as the original Reno.
15 #include <linux/config.h>
17 #include <linux/module.h>
21 #define BICTCP_BETA_SCALE 1024 /* Scale factor beta calculation
22 * max_cwnd = snd_cwnd * beta
26 * go to point (max+min)/N
29 static int fast_convergence
= 1;
30 static int max_increment
= 32;
31 static int low_window
= 14;
32 static int beta
= 819; /* = 819/1024 (BICTCP_BETA_SCALE) */
33 static int low_utilization_threshold
= 153;
34 static int low_utilization_period
= 2;
35 static int initial_ssthresh
= 100;
36 static int smooth_part
= 20;
38 module_param(fast_convergence
, int, 0644);
39 MODULE_PARM_DESC(fast_convergence
, "turn on/off fast convergence");
40 module_param(max_increment
, int, 0644);
41 MODULE_PARM_DESC(max_increment
, "Limit on increment allowed during binary search");
42 module_param(low_window
, int, 0644);
43 MODULE_PARM_DESC(low_window
, "lower bound on congestion window (for TCP friendliness)");
44 module_param(beta
, int, 0644);
45 MODULE_PARM_DESC(beta
, "beta for multiplicative increase");
46 module_param(low_utilization_threshold
, int, 0644);
47 MODULE_PARM_DESC(low_utilization_threshold
, "percent (scaled by 1024) for low utilization mode");
48 module_param(low_utilization_period
, int, 0644);
49 MODULE_PARM_DESC(low_utilization_period
, "if average delay exceeds then goto to low utilization mode (seconds)");
50 module_param(initial_ssthresh
, int, 0644);
51 MODULE_PARM_DESC(initial_ssthresh
, "initial value of slow start threshold");
52 module_param(smooth_part
, int, 0644);
53 MODULE_PARM_DESC(smooth_part
, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
56 /* BIC TCP Parameters */
58 u32 cnt
; /* increase cwnd by 1 after ACKs */
59 u32 last_max_cwnd
; /* last maximum snd_cwnd */
60 u32 loss_cwnd
; /* congestion window at last loss */
61 u32 last_cwnd
; /* the last snd_cwnd */
62 u32 last_time
; /* time when updated last_cwnd */
63 u32 delay_min
; /* min delay */
64 u32 delay_max
; /* max delay */
66 u8 low_utilization
;/* 0: high; 1: low */
67 u32 low_utilization_start
; /* starting time of low utilization detection*/
68 u32 epoch_start
; /* beginning of an epoch */
69 #define ACK_RATIO_SHIFT 4
70 u32 delayed_ack
; /* estimate the ratio of Packets/ACKs << 4 */
73 static inline void bictcp_reset(struct bictcp
*ca
)
76 ca
->last_max_cwnd
= 0;
83 ca
->low_utilization
= 0;
84 ca
->low_utilization_start
= 0;
86 ca
->delayed_ack
= 2 << ACK_RATIO_SHIFT
;
89 static void bictcp_init(struct tcp_sock
*tp
)
91 bictcp_reset(tcp_ca(tp
));
93 tp
->snd_ssthresh
= initial_ssthresh
;
97 * Compute congestion window to use.
99 static inline void bictcp_update(struct bictcp
*ca
, u32 cwnd
)
101 if (ca
->last_cwnd
== cwnd
&&
102 (s32
)(tcp_time_stamp
- ca
->last_time
) <= HZ
/ 32)
105 ca
->last_cwnd
= cwnd
;
106 ca
->last_time
= tcp_time_stamp
;
108 if (ca
->epoch_start
== 0) /* record the beginning of an epoch */
109 ca
->epoch_start
= tcp_time_stamp
;
111 /* start off normal */
112 if (cwnd
<= low_window
) {
117 /* binary increase */
118 if (cwnd
< ca
->last_max_cwnd
) {
119 __u32 dist
= (ca
->last_max_cwnd
- cwnd
)
122 if (dist
> max_increment
)
123 /* linear increase */
124 ca
->cnt
= cwnd
/ max_increment
;
126 /* binary search increase */
127 ca
->cnt
= (cwnd
* smooth_part
) / BICTCP_B
;
129 /* binary search increase */
130 ca
->cnt
= cwnd
/ dist
;
132 /* slow start AMD linear increase */
133 if (cwnd
< ca
->last_max_cwnd
+ BICTCP_B
)
135 ca
->cnt
= (cwnd
* smooth_part
) / BICTCP_B
;
136 else if (cwnd
< ca
->last_max_cwnd
+ max_increment
*(BICTCP_B
-1))
138 ca
->cnt
= (cwnd
* (BICTCP_B
-1))
139 / cwnd
-ca
->last_max_cwnd
;
141 /* linear increase */
142 ca
->cnt
= cwnd
/ max_increment
;
145 /* if in slow start or link utilization is very low */
146 if ( ca
->loss_cwnd
== 0 ||
147 (cwnd
> ca
->loss_cwnd
&& ca
->low_utilization
)) {
148 if (ca
->cnt
> 20) /* increase cwnd 5% per RTT */
152 ca
->cnt
= (ca
->cnt
<< ACK_RATIO_SHIFT
) / ca
->delayed_ack
;
153 if (ca
->cnt
== 0) /* cannot be zero */
158 /* Detect low utilization in congestion avoidance */
159 static inline void bictcp_low_utilization(struct tcp_sock
*tp
, int flag
)
161 struct bictcp
*ca
= tcp_ca(tp
);
165 if (!(tp
->rx_opt
.saw_tstamp
&& tp
->rx_opt
.rcv_tsecr
) ||
166 /* Discard delay samples right after fast recovery */
167 tcp_time_stamp
< ca
->epoch_start
+ HZ
||
168 /* this delay samples may not be accurate */
174 delay
= ca
->last_delay
<<3; /* use the same scale as tp->srtt*/
175 ca
->last_delay
= tcp_time_stamp
- tp
->rx_opt
.rcv_tsecr
;
176 if (delay
== 0) /* no previous delay sample */
179 /* first time call or link delay decreases */
180 if (ca
->delay_min
== 0 || ca
->delay_min
> delay
) {
181 ca
->delay_min
= ca
->delay_max
= delay
;
185 if (ca
->delay_max
< delay
)
186 ca
->delay_max
= delay
;
188 /* utilization is low, if avg delay < dist*threshold
189 for checking_period time */
190 dist
= ca
->delay_max
- ca
->delay_min
;
191 if (dist
<= ca
->delay_min
>>6 ||
192 tp
->srtt
- ca
->delay_min
>= (dist
*low_utilization_threshold
)>>10)
195 if (ca
->low_utilization_start
== 0) {
196 ca
->low_utilization
= 0;
197 ca
->low_utilization_start
= tcp_time_stamp
;
198 } else if ((s32
)(tcp_time_stamp
- ca
->low_utilization_start
)
199 > low_utilization_period
*HZ
) {
200 ca
->low_utilization
= 1;
206 ca
->low_utilization
= 0;
207 ca
->low_utilization_start
= 0;
211 static void bictcp_cong_avoid(struct tcp_sock
*tp
, u32 ack
,
212 u32 seq_rtt
, u32 in_flight
, int data_acked
)
214 struct bictcp
*ca
= tcp_ca(tp
);
216 bictcp_low_utilization(tp
, data_acked
);
218 if (in_flight
< tp
->snd_cwnd
)
221 if (tp
->snd_cwnd
<= tp
->snd_ssthresh
) {
222 /* In "safe" area, increase. */
223 if (tp
->snd_cwnd
< tp
->snd_cwnd_clamp
)
226 bictcp_update(ca
, tp
->snd_cwnd
);
228 /* In dangerous area, increase slowly.
229 * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
231 if (tp
->snd_cwnd_cnt
>= ca
->cnt
) {
232 if (tp
->snd_cwnd
< tp
->snd_cwnd_clamp
)
234 tp
->snd_cwnd_cnt
= 0;
242 * behave like Reno until low_window is reached,
243 * then increase congestion window slowly
245 static u32
bictcp_recalc_ssthresh(struct tcp_sock
*tp
)
247 struct bictcp
*ca
= tcp_ca(tp
);
249 ca
->epoch_start
= 0; /* end of epoch */
251 /* in case of wrong delay_max*/
252 if (ca
->delay_min
> 0 && ca
->delay_max
> ca
->delay_min
)
253 ca
->delay_max
= ca
->delay_min
254 + ((ca
->delay_max
- ca
->delay_min
)* 90) / 100;
256 /* Wmax and fast convergence */
257 if (tp
->snd_cwnd
< ca
->last_max_cwnd
&& fast_convergence
)
258 ca
->last_max_cwnd
= (tp
->snd_cwnd
* (BICTCP_BETA_SCALE
+ beta
))
259 / (2 * BICTCP_BETA_SCALE
);
261 ca
->last_max_cwnd
= tp
->snd_cwnd
;
263 ca
->loss_cwnd
= tp
->snd_cwnd
;
266 if (tp
->snd_cwnd
<= low_window
)
267 return max(tp
->snd_cwnd
>> 1U, 2U);
269 return max((tp
->snd_cwnd
* beta
) / BICTCP_BETA_SCALE
, 2U);
272 static u32
bictcp_undo_cwnd(struct tcp_sock
*tp
)
274 struct bictcp
*ca
= tcp_ca(tp
);
276 return max(tp
->snd_cwnd
, ca
->last_max_cwnd
);
279 static u32
bictcp_min_cwnd(struct tcp_sock
*tp
)
281 return tp
->snd_ssthresh
;
284 static void bictcp_state(struct tcp_sock
*tp
, u8 new_state
)
286 if (new_state
== TCP_CA_Loss
)
287 bictcp_reset(tcp_ca(tp
));
290 /* Track delayed acknowledgement ratio using sliding window
291 * ratio = (15*ratio + sample) / 16
293 static void bictcp_acked(struct tcp_sock
*tp
, u32 cnt
)
295 if (cnt
> 0 && tp
->ca_state
== TCP_CA_Open
) {
296 struct bictcp
*ca
= tcp_ca(tp
);
297 cnt
-= ca
->delayed_ack
>> ACK_RATIO_SHIFT
;
298 ca
->delayed_ack
+= cnt
;
303 static struct tcp_congestion_ops bictcp
= {
305 .ssthresh
= bictcp_recalc_ssthresh
,
306 .cong_avoid
= bictcp_cong_avoid
,
307 .set_state
= bictcp_state
,
308 .undo_cwnd
= bictcp_undo_cwnd
,
309 .min_cwnd
= bictcp_min_cwnd
,
310 .pkts_acked
= bictcp_acked
,
311 .owner
= THIS_MODULE
,
315 static int __init
bictcp_register(void)
317 BUG_ON(sizeof(struct bictcp
) > TCP_CA_PRIV_SIZE
);
318 return tcp_register_congestion_control(&bictcp
);
321 static void __exit
bictcp_unregister(void)
323 tcp_unregister_congestion_control(&bictcp
);
326 module_init(bictcp_register
);
327 module_exit(bictcp_unregister
);
329 MODULE_AUTHOR("Stephen Hemminger");
330 MODULE_LICENSE("GPL");
331 MODULE_DESCRIPTION("BIC TCP");