Blame view

net/ipv4/tcp_bic.c 5.95 KB
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
1
2
  /*
   * Binary Increase Congestion control for TCP
0bc8c7bf9   Sangtae Ha   [TCP]: BIC web pa...
3
4
   * Home page:
   *      http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
5
6
7
8
9
   * This is from the implementation of BICTCP in
   * Lison-Xu, Kahaled Harfoush, and Injong Rhee.
   *  "Binary Increase Congestion Control for Fast, Long Distance
   *  Networks" in InfoComm 2004
   * Available from:
0bc8c7bf9   Sangtae Ha   [TCP]: BIC web pa...
10
   *  http://netsrv.csc.ncsu.edu/export/bitcp.pdf
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
11
12
13
14
   *
   * Unless BIC is enabled and congestion window is large
   * this behaves the same as the original Reno.
   */
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
15
16
17
  #include <linux/mm.h>
  #include <linux/module.h>
  #include <net/tcp.h>
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
18
19
20
21
22
23
24
25
26
  #define BICTCP_BETA_SCALE    1024	/* Scale factor beta calculation
  					 * max_cwnd = snd_cwnd * beta
  					 */
  #define BICTCP_B		4	 /*
  					  * In binary search,
  					  * go to point (max+min)/N
  					  */
  
  static int fast_convergence = 1;
450b5b189   Stephen Hemminger   [TCP]: BIC max in...
27
  static int max_increment = 16;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
28
29
  static int low_window = 14;
  static int beta = 819;		/* = 819/1024 (BICTCP_BETA_SCALE) */
66e1e3b20   David S. Miller   [TCP]: Set initia...
30
  static int initial_ssthresh;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
31
32
33
34
35
36
37
38
39
40
  static int smooth_part = 20;
  
  module_param(fast_convergence, int, 0644);
  MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
  module_param(max_increment, int, 0644);
  MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
  module_param(low_window, int, 0644);
  MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
  module_param(beta, int, 0644);
  MODULE_PARM_DESC(beta, "beta for multiplicative increase");
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
41
42
43
44
  module_param(initial_ssthresh, int, 0644);
  MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
  module_param(smooth_part, int, 0644);
  MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
45
46
47
  /* BIC TCP Parameters */
  struct bictcp {
  	u32	cnt;		/* increase cwnd by 1 after ACKs */
688d1945b   stephen hemminger   tcp: whitespace f...
48
  	u32	last_max_cwnd;	/* last maximum snd_cwnd */
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
49
50
  	u32	last_cwnd;	/* the last snd_cwnd */
  	u32	last_time;	/* time when updated last_cwnd */
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
51
52
53
54
55
56
57
58
59
  	u32	epoch_start;	/* beginning of an epoch */
  #define ACK_RATIO_SHIFT	4
  	u32	delayed_ack;	/* estimate the ratio of Packets/ACKs << 4 */
  };
  
  static inline void bictcp_reset(struct bictcp *ca)
  {
  	ca->cnt = 0;
  	ca->last_max_cwnd = 0;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
60
61
  	ca->last_cwnd = 0;
  	ca->last_time = 0;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
62
63
64
  	ca->epoch_start = 0;
  	ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
  }
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
65
  static void bictcp_init(struct sock *sk)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
66
  {
fc16dcd8c   Neal Cardwell   tcp: fix undo aft...
67
68
69
  	struct bictcp *ca = inet_csk_ca(sk);
  
  	bictcp_reset(ca);
fc16dcd8c   Neal Cardwell   tcp: fix undo aft...
70

83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
71
  	if (initial_ssthresh)
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
72
  		tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
73
74
75
76
77
78
79
80
  }
  
  /*
   * Compute congestion window to use.
   */
  static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
  {
  	if (ca->last_cwnd == cwnd &&
ac35f5622   Eric Dumazet   tcp: bic, cubic: ...
81
  	    (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
82
83
84
  		return;
  
  	ca->last_cwnd = cwnd;
ac35f5622   Eric Dumazet   tcp: bic, cubic: ...
85
  	ca->last_time = tcp_jiffies32;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
86
87
  
  	if (ca->epoch_start == 0) /* record the beginning of an epoch */
ac35f5622   Eric Dumazet   tcp: bic, cubic: ...
88
  		ca->epoch_start = tcp_jiffies32;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
89
90
91
92
93
94
95
96
97
  
  	/* start off normal */
  	if (cwnd <= low_window) {
  		ca->cnt = cwnd;
  		return;
  	}
  
  	/* binary increase */
  	if (cwnd < ca->last_max_cwnd) {
688d1945b   stephen hemminger   tcp: whitespace f...
98
  		__u32	dist = (ca->last_max_cwnd - cwnd)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  			/ BICTCP_B;
  
  		if (dist > max_increment)
  			/* linear increase */
  			ca->cnt = cwnd / max_increment;
  		else if (dist <= 1U)
  			/* binary search increase */
  			ca->cnt = (cwnd * smooth_part) / BICTCP_B;
  		else
  			/* binary search increase */
  			ca->cnt = cwnd / dist;
  	} else {
  		/* slow start AMD linear increase */
  		if (cwnd < ca->last_max_cwnd + BICTCP_B)
  			/* slow start */
  			ca->cnt = (cwnd * smooth_part) / BICTCP_B;
  		else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1))
  			/* slow start */
  			ca->cnt = (cwnd * (BICTCP_B-1))
42a39450f   Stephen Hemminger   [TCP]: BIC coding...
118
  				/ (cwnd - ca->last_max_cwnd);
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
119
120
121
122
123
124
  		else
  			/* linear increase */
  			ca->cnt = cwnd / max_increment;
  	}
  
  	/* if in slow start or link utilization is very low */
fc16dcd8c   Neal Cardwell   tcp: fix undo aft...
125
  	if (ca->last_max_cwnd == 0) {
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
126
127
128
129
130
131
132
133
  		if (ca->cnt > 20) /* increase cwnd 5% per RTT */
  			ca->cnt = 20;
  	}
  
  	ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
  	if (ca->cnt == 0)			/* cannot be zero */
  		ca->cnt = 1;
  }
249015515   Eric Dumazet   tcp: remove in_fl...
134
  static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
135
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
136
137
  	struct tcp_sock *tp = tcp_sk(sk);
  	struct bictcp *ca = inet_csk_ca(sk);
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
138

249015515   Eric Dumazet   tcp: remove in_fl...
139
  	if (!tcp_is_cwnd_limited(sk))
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
140
  		return;
071d5080e   Yuchung Cheng   tcp: add tcp_in_s...
141
  	if (tcp_in_slow_start(tp))
9f9843a75   Yuchung Cheng   tcp: properly han...
142
  		tcp_slow_start(tp, acked);
7faffa1c7   Stephen Hemminger   [TCP]: add tcp_sl...
143
  	else {
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
144
  		bictcp_update(ca, tp->snd_cwnd);
e73ebb088   Neal Cardwell   tcp: stretch ACK ...
145
  		tcp_cong_avoid_ai(tp, ca->cnt, 1);
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
146
  	}
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
147
148
149
150
151
152
  }
  
  /*
   *	behave like Reno until low_window is reached,
   *	then increase congestion window slowly
   */
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
153
  static u32 bictcp_recalc_ssthresh(struct sock *sk)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
154
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
155
156
  	const struct tcp_sock *tp = tcp_sk(sk);
  	struct bictcp *ca = inet_csk_ca(sk);
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
157
158
  
  	ca->epoch_start = 0;	/* end of epoch */
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
159
160
161
162
163
164
  	/* Wmax and fast convergence */
  	if (tp->snd_cwnd < ca->last_max_cwnd && fast_convergence)
  		ca->last_max_cwnd = (tp->snd_cwnd * (BICTCP_BETA_SCALE + beta))
  			/ (2 * BICTCP_BETA_SCALE);
  	else
  		ca->last_max_cwnd = tp->snd_cwnd;
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
165
166
167
168
169
  	if (tp->snd_cwnd <= low_window)
  		return max(tp->snd_cwnd >> 1U, 2U);
  	else
  		return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
  }
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
170
  static void bictcp_state(struct sock *sk, u8 new_state)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
171
172
  {
  	if (new_state == TCP_CA_Loss)
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
173
  		bictcp_reset(inet_csk_ca(sk));
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
174
  }
05d054503   Stephen Hemminger   [TCP] BIC: spelli...
175
  /* Track delayed acknowledgment ratio using sliding window
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
176
177
   * ratio = (15*ratio + sample) / 16
   */
756ee1729   Lawrence Brakmo   tcp: replace cnt ...
178
  static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
179
  {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
180
  	const struct inet_connection_sock *icsk = inet_csk(sk);
35e869419   Ilpo Järvinen   [TCP]: Remove num...
181
  	if (icsk->icsk_ca_state == TCP_CA_Open) {
6687e988d   Arnaldo Carvalho de Melo   [ICSK]: Move TCP ...
182
  		struct bictcp *ca = inet_csk_ca(sk);
688d1945b   stephen hemminger   tcp: whitespace f...
183

756ee1729   Lawrence Brakmo   tcp: replace cnt ...
184
185
  		ca->delayed_ack += sample->pkts_acked -
  			(ca->delayed_ack >> ACK_RATIO_SHIFT);
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
186
187
  	}
  }
a252bebe2   Stephen Hemminger   tcp: mark tcp_con...
188
  static struct tcp_congestion_ops bictcp __read_mostly = {
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
189
190
191
192
  	.init		= bictcp_init,
  	.ssthresh	= bictcp_recalc_ssthresh,
  	.cong_avoid	= bictcp_cong_avoid,
  	.set_state	= bictcp_state,
f1722a1be   Yuchung Cheng   tcp: consolidate ...
193
  	.undo_cwnd	= tcp_reno_undo_cwnd,
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
194
195
196
197
198
199
200
  	.pkts_acked     = bictcp_acked,
  	.owner		= THIS_MODULE,
  	.name		= "bic",
  };
  
  static int __init bictcp_register(void)
  {
65e3d7265   Alexey Dobriyan   [TCP] tcp_bic: us...
201
  	BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
83803034f   Stephen Hemminger   [TCP]: Add TCP BI...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  	return tcp_register_congestion_control(&bictcp);
  }
  
  static void __exit bictcp_unregister(void)
  {
  	tcp_unregister_congestion_control(&bictcp);
  }
  
  module_init(bictcp_register);
  module_exit(bictcp_unregister);
  
  MODULE_AUTHOR("Stephen Hemminger");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("BIC TCP");