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