1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/module.h>
37#include <net/tcp.h>
38
39
40#define LP_RESOL TCP_TS_HZ
41
42
43
44
45
46
47
48
49
50
51
52enum tcp_lp_state {
53 LP_VALID_RHZ = (1 << 0),
54 LP_VALID_OWD = (1 << 1),
55 LP_WITHIN_THR = (1 << 3),
56 LP_WITHIN_INF = (1 << 4),
57};
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76struct lp {
77 u32 flag;
78 u32 sowd;
79 u32 owd_min;
80 u32 owd_max;
81 u32 owd_max_rsv;
82 u32 remote_hz;
83 u32 remote_ref_time;
84 u32 local_ref_time;
85 u32 last_drop;
86 u32 inference;
87};
88
89
90
91
92
93
94
95static void tcp_lp_init(struct sock *sk)
96{
97 struct lp *lp = inet_csk_ca(sk);
98
99 lp->flag = 0;
100 lp->sowd = 0;
101 lp->owd_min = 0xffffffff;
102 lp->owd_max = 0;
103 lp->owd_max_rsv = 0;
104 lp->remote_hz = 0;
105 lp->remote_ref_time = 0;
106 lp->local_ref_time = 0;
107 lp->last_drop = 0;
108 lp->inference = 0;
109}
110
111
112
113
114
115
116
117
118static void tcp_lp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
119{
120 struct lp *lp = inet_csk_ca(sk);
121
122 if (!(lp->flag & LP_WITHIN_INF))
123 tcp_reno_cong_avoid(sk, ack, acked);
124}
125
126
127
128
129
130
131
132
133static u32 tcp_lp_remote_hz_estimator(struct sock *sk)
134{
135 struct tcp_sock *tp = tcp_sk(sk);
136 struct lp *lp = inet_csk_ca(sk);
137 s64 rhz = lp->remote_hz << 6;
138 s64 m = 0;
139
140
141
142 if (lp->remote_ref_time == 0 || lp->local_ref_time == 0)
143 goto out;
144
145
146 if (tp->rx_opt.rcv_tsval == lp->remote_ref_time ||
147 tp->rx_opt.rcv_tsecr == lp->local_ref_time)
148 goto out;
149
150 m = TCP_TS_HZ *
151 (tp->rx_opt.rcv_tsval - lp->remote_ref_time) /
152 (tp->rx_opt.rcv_tsecr - lp->local_ref_time);
153 if (m < 0)
154 m = -m;
155
156 if (rhz > 0) {
157 m -= rhz >> 6;
158 rhz += m;
159 } else
160 rhz = m << 6;
161
162 out:
163
164 if ((rhz >> 6) > 0)
165 lp->flag |= LP_VALID_RHZ;
166 else
167 lp->flag &= ~LP_VALID_RHZ;
168
169
170 lp->remote_ref_time = tp->rx_opt.rcv_tsval;
171 lp->local_ref_time = tp->rx_opt.rcv_tsecr;
172
173 return rhz >> 6;
174}
175
176
177
178
179
180
181
182
183
184
185
186static u32 tcp_lp_owd_calculator(struct sock *sk)
187{
188 struct tcp_sock *tp = tcp_sk(sk);
189 struct lp *lp = inet_csk_ca(sk);
190 s64 owd = 0;
191
192 lp->remote_hz = tcp_lp_remote_hz_estimator(sk);
193
194 if (lp->flag & LP_VALID_RHZ) {
195 owd =
196 tp->rx_opt.rcv_tsval * (LP_RESOL / lp->remote_hz) -
197 tp->rx_opt.rcv_tsecr * (LP_RESOL / TCP_TS_HZ);
198 if (owd < 0)
199 owd = -owd;
200 }
201
202 if (owd > 0)
203 lp->flag |= LP_VALID_OWD;
204 else
205 lp->flag &= ~LP_VALID_OWD;
206
207 return owd;
208}
209
210
211
212
213
214
215
216
217
218
219
220static void tcp_lp_rtt_sample(struct sock *sk, u32 rtt)
221{
222 struct lp *lp = inet_csk_ca(sk);
223 s64 mowd = tcp_lp_owd_calculator(sk);
224
225
226 if (!(lp->flag & LP_VALID_RHZ) || !(lp->flag & LP_VALID_OWD))
227 return;
228
229
230 if (mowd < lp->owd_min)
231 lp->owd_min = mowd;
232
233
234
235 if (mowd > lp->owd_max) {
236 if (mowd > lp->owd_max_rsv) {
237 if (lp->owd_max_rsv == 0)
238 lp->owd_max = mowd;
239 else
240 lp->owd_max = lp->owd_max_rsv;
241 lp->owd_max_rsv = mowd;
242 } else
243 lp->owd_max = mowd;
244 }
245
246
247 if (lp->sowd != 0) {
248 mowd -= lp->sowd >> 3;
249 lp->sowd += mowd;
250 } else
251 lp->sowd = mowd << 3;
252}
253
254
255
256
257
258
259
260
261
262
263static void tcp_lp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
264{
265 struct tcp_sock *tp = tcp_sk(sk);
266 struct lp *lp = inet_csk_ca(sk);
267 u32 now = tcp_time_stamp(tp);
268 u32 delta;
269
270 if (sample->rtt_us > 0)
271 tcp_lp_rtt_sample(sk, sample->rtt_us);
272
273
274 delta = now - tp->rx_opt.rcv_tsecr;
275 if ((s32)delta > 0)
276 lp->inference = 3 * delta;
277
278
279 if (lp->last_drop && (now - lp->last_drop < lp->inference))
280 lp->flag |= LP_WITHIN_INF;
281 else
282 lp->flag &= ~LP_WITHIN_INF;
283
284
285 if (lp->sowd >> 3 <
286 lp->owd_min + 15 * (lp->owd_max - lp->owd_min) / 100)
287 lp->flag |= LP_WITHIN_THR;
288 else
289 lp->flag &= ~LP_WITHIN_THR;
290
291 pr_debug("TCP-LP: %05o|%5u|%5u|%15u|%15u|%15u\n", lp->flag,
292 tp->snd_cwnd, lp->remote_hz, lp->owd_min, lp->owd_max,
293 lp->sowd >> 3);
294
295 if (lp->flag & LP_WITHIN_THR)
296 return;
297
298
299
300
301 lp->owd_min = lp->sowd >> 3;
302 lp->owd_max = lp->sowd >> 2;
303 lp->owd_max_rsv = lp->sowd >> 2;
304
305
306
307 if (lp->flag & LP_WITHIN_INF)
308 tp->snd_cwnd = 1U;
309
310
311
312 else
313 tp->snd_cwnd = max(tp->snd_cwnd >> 1U, 1U);
314
315
316 lp->last_drop = now;
317}
318
319static struct tcp_congestion_ops tcp_lp __read_mostly = {
320 .init = tcp_lp_init,
321 .ssthresh = tcp_reno_ssthresh,
322 .undo_cwnd = tcp_reno_undo_cwnd,
323 .cong_avoid = tcp_lp_cong_avoid,
324 .pkts_acked = tcp_lp_pkts_acked,
325
326 .owner = THIS_MODULE,
327 .name = "lp"
328};
329
330static int __init tcp_lp_register(void)
331{
332 BUILD_BUG_ON(sizeof(struct lp) > ICSK_CA_PRIV_SIZE);
333 return tcp_register_congestion_control(&tcp_lp);
334}
335
336static void __exit tcp_lp_unregister(void)
337{
338 tcp_unregister_congestion_control(&tcp_lp);
339}
340
341module_init(tcp_lp_register);
342module_exit(tcp_lp_unregister);
343
344MODULE_AUTHOR("Wong Hoi Sing Edison, Hung Hing Lun Mike");
345MODULE_LICENSE("GPL");
346MODULE_DESCRIPTION("TCP Low Priority");
347