xref: /linux/net/ipv4/tcp_lp.c (revision 6dfafbd0299a60bfb5d5e277fdf100037c7ded07)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TCP Low Priority (TCP-LP)
4  *
5  * TCP Low Priority is a distributed algorithm whose goal is to utilize only
6  *   the excess network bandwidth as compared to the ``fair share`` of
7  *   bandwidth as targeted by TCP.
8  *
9  * As of 2.6.13, Linux supports pluggable congestion control algorithms.
10  * Due to the limitation of the API, we take the following changes from
11  * the original TCP-LP implementation:
12  *   o We use newReno in most core CA handling. Only add some checking
13  *     within cong_avoid.
14  *   o Error correcting in remote HZ, therefore remote HZ will be keeped
15  *     on checking and updating.
16  *   o Handling calculation of One-Way-Delay (OWD) within rtt_sample, since
17  *     OWD have a similar meaning as RTT. Also correct the buggy formular.
18  *   o Handle reaction for Early Congestion Indication (ECI) within
19  *     pkts_acked, as mentioned within pseudo code.
20  *   o OWD is handled in relative format, where local time stamp will in
21  *     tcp_time_stamp format.
22  *
23  * Original Author:
24  *   Aleksandar Kuzmanovic <akuzma@northwestern.edu>
25  * Available from:
26  *   https://users.cs.northwestern.edu/~akuzma/doc/TCP-LP-ToN.pdf
27  * Original implementation for 2.4.19:
28  *   https://users.cs.northwestern.edu/~akuzma/rice/TCP-LP/linux/tcp-lp-linux.htm
29  *
30  * 2.6.x module Authors:
31  *   Wong Hoi Sing, Edison <hswong3i@gmail.com>
32  *   Hung Hing Lun, Mike <hlhung3i@gmail.com>
33  * SourceForge project page:
34  *   http://tcp-lp-mod.sourceforge.net/
35  */
36 
37 #include <linux/module.h>
38 #include <net/tcp.h>
39 
40 /* resolution of owd */
41 #define LP_RESOL       TCP_TS_HZ
42 
43 /**
44  * enum tcp_lp_state
45  * @LP_VALID_RHZ: is remote HZ valid?
46  * @LP_VALID_OWD: is OWD valid?
47  * @LP_WITHIN_THR: are we within threshold?
48  * @LP_WITHIN_INF: are we within inference?
49  *
50  * TCP-LP's state flags.
51  * We create this set of state flag mainly for debugging.
52  */
53 enum 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  * struct lp
62  * @flag: TCP-LP state flag
63  * @sowd: smoothed OWD << 3
64  * @owd_min: min OWD
65  * @owd_max: max OWD
66  * @owd_max_rsv: reserved max owd
67  * @remote_hz: estimated remote HZ
68  * @remote_ref_time: remote reference time
69  * @local_ref_time: local reference time
70  * @last_drop: time for last active drop
71  * @inference: current inference
72  *
73  * TCP-LP's private struct.
74  * We get the idea from original TCP-LP implementation where only left those we
75  * found are really useful.
76  */
77 struct 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  * tcp_lp_init
92  * @sk: socket to initialize congestion control algorithm for
93  *
94  * Init all required variables.
95  * Clone the handling from Vegas module implementation.
96  */
97 static void tcp_lp_init(struct sock *sk)
98 {
99 	struct lp *lp = inet_csk_ca(sk);
100 
101 	lp->flag = 0;
102 	lp->sowd = 0;
103 	lp->owd_min = 0xffffffff;
104 	lp->owd_max = 0;
105 	lp->owd_max_rsv = 0;
106 	lp->remote_hz = 0;
107 	lp->remote_ref_time = 0;
108 	lp->local_ref_time = 0;
109 	lp->last_drop = 0;
110 	lp->inference = 0;
111 }
112 
113 /**
114  * tcp_lp_cong_avoid
115  * @sk: socket to avoid congesting
116  * @ack: current ack sequence number
117  * @acked: number of ACKed packets
118  *
119  * Implementation of cong_avoid.
120  * Will only call newReno CA when away from inference.
121  * From TCP-LP's paper, this will be handled in additive increasement.
122  */
123 static void tcp_lp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
124 {
125 	struct lp *lp = inet_csk_ca(sk);
126 
127 	if (!(lp->flag & LP_WITHIN_INF))
128 		tcp_reno_cong_avoid(sk, ack, acked);
129 }
130 
131 /**
132  * tcp_lp_remote_hz_estimator
133  * @sk: socket which needs an estimate for the remote HZs
134  *
135  * Estimate remote HZ.
136  * We keep on updating the estimated value, where original TCP-LP
137  * implementation only guest it for once and use forever.
138  */
139 static u32 tcp_lp_remote_hz_estimator(struct sock *sk)
140 {
141 	struct tcp_sock *tp = tcp_sk(sk);
142 	struct lp *lp = inet_csk_ca(sk);
143 	s64 rhz = lp->remote_hz << 6;	/* remote HZ << 6 */
144 	s64 m = 0;
145 
146 	/* not yet record reference time
147 	 * go away!! record it before come back!! */
148 	if (lp->remote_ref_time == 0 || lp->local_ref_time == 0)
149 		goto out;
150 
151 	/* we can't calc remote HZ with no different!! */
152 	if (tp->rx_opt.rcv_tsval == lp->remote_ref_time ||
153 	    tp->rx_opt.rcv_tsecr == lp->local_ref_time)
154 		goto out;
155 
156 	m = TCP_TS_HZ *
157 	    (tp->rx_opt.rcv_tsval - lp->remote_ref_time) /
158 	    (tp->rx_opt.rcv_tsecr - lp->local_ref_time);
159 	if (m < 0)
160 		m = -m;
161 
162 	if (rhz > 0) {
163 		m -= rhz >> 6;	/* m is now error in remote HZ est */
164 		rhz += m;	/* 63/64 old + 1/64 new */
165 	} else
166 		rhz = m << 6;
167 
168  out:
169 	/* record time for successful remote HZ calc */
170 	if ((rhz >> 6) > 0)
171 		lp->flag |= LP_VALID_RHZ;
172 	else
173 		lp->flag &= ~LP_VALID_RHZ;
174 
175 	/* record reference time stamp */
176 	lp->remote_ref_time = tp->rx_opt.rcv_tsval;
177 	lp->local_ref_time = tp->rx_opt.rcv_tsecr;
178 
179 	return rhz >> 6;
180 }
181 
182 /**
183  * tcp_lp_owd_calculator
184  * @sk: socket to calculate one way delay for
185  *
186  * Calculate one way delay (in relative format).
187  * Original implement OWD as minus of remote time difference to local time
188  * difference directly. As this time difference just simply equal to RTT, when
189  * the network status is stable, remote RTT will equal to local RTT, and result
190  * OWD into zero.
191  * It seems to be a bug and so we fixed it.
192  */
193 static u32 tcp_lp_owd_calculator(struct sock *sk)
194 {
195 	struct tcp_sock *tp = tcp_sk(sk);
196 	struct lp *lp = inet_csk_ca(sk);
197 	s64 owd = 0;
198 
199 	lp->remote_hz = tcp_lp_remote_hz_estimator(sk);
200 
201 	if (lp->flag & LP_VALID_RHZ) {
202 		owd =
203 		    tp->rx_opt.rcv_tsval * (LP_RESOL / lp->remote_hz) -
204 		    tp->rx_opt.rcv_tsecr * (LP_RESOL / TCP_TS_HZ);
205 		if (owd < 0)
206 			owd = -owd;
207 	}
208 
209 	if (owd > 0)
210 		lp->flag |= LP_VALID_OWD;
211 	else
212 		lp->flag &= ~LP_VALID_OWD;
213 
214 	return owd;
215 }
216 
217 /**
218  * tcp_lp_rtt_sample
219  * @sk: socket to add a rtt sample to
220  * @rtt: round trip time, which is ignored!
221  *
222  * Implementation or rtt_sample.
223  * Will take the following action,
224  *   1. calc OWD,
225  *   2. record the min/max OWD,
226  *   3. calc smoothed OWD (SOWD).
227  * Most ideas come from the original TCP-LP implementation.
228  */
229 static void tcp_lp_rtt_sample(struct sock *sk, u32 rtt)
230 {
231 	struct lp *lp = inet_csk_ca(sk);
232 	s64 mowd = tcp_lp_owd_calculator(sk);
233 
234 	/* sorry that we don't have valid data */
235 	if (!(lp->flag & LP_VALID_RHZ) || !(lp->flag & LP_VALID_OWD))
236 		return;
237 
238 	/* record the next min owd */
239 	if (mowd < lp->owd_min)
240 		lp->owd_min = mowd;
241 
242 	/* always forget the max of the max
243 	 * we just set owd_max as one below it */
244 	if (mowd > lp->owd_max) {
245 		if (mowd > lp->owd_max_rsv) {
246 			if (lp->owd_max_rsv == 0)
247 				lp->owd_max = mowd;
248 			else
249 				lp->owd_max = lp->owd_max_rsv;
250 			lp->owd_max_rsv = mowd;
251 		} else
252 			lp->owd_max = mowd;
253 	}
254 
255 	/* calc for smoothed owd */
256 	if (lp->sowd != 0) {
257 		mowd -= lp->sowd >> 3;	/* m is now error in owd est */
258 		lp->sowd += mowd;	/* owd = 7/8 owd + 1/8 new */
259 	} else
260 		lp->sowd = mowd << 3;	/* take the measured time be owd */
261 }
262 
263 /**
264  * tcp_lp_pkts_acked
265  * @sk: socket requiring congestion avoidance calculations
266  * @sample: ACK sample containing timing and rate information
267  *
268  * Implementation of pkts_acked.
269  * Deal with active drop under Early Congestion Indication.
270  * Only drop to half and 1 will be handle, because we hope to use back
271  * newReno in increase case.
272  * We work it out by following the idea from TCP-LP's paper directly
273  */
274 static void tcp_lp_pkts_acked(struct sock *sk, const struct ack_sample *sample)
275 {
276 	struct tcp_sock *tp = tcp_sk(sk);
277 	struct lp *lp = inet_csk_ca(sk);
278 	u32 now = tcp_time_stamp_ts(tp);
279 	u32 delta;
280 
281 	if (sample->rtt_us > 0)
282 		tcp_lp_rtt_sample(sk, sample->rtt_us);
283 
284 	/* calc inference */
285 	delta = now - tp->rx_opt.rcv_tsecr;
286 	if ((s32)delta > 0)
287 		lp->inference = 3 * delta;
288 
289 	/* test if within inference */
290 	if (lp->last_drop && (now - lp->last_drop < lp->inference))
291 		lp->flag |= LP_WITHIN_INF;
292 	else
293 		lp->flag &= ~LP_WITHIN_INF;
294 
295 	/* test if within threshold */
296 	if (lp->sowd >> 3 <
297 	    lp->owd_min + 15 * (lp->owd_max - lp->owd_min) / 100)
298 		lp->flag |= LP_WITHIN_THR;
299 	else
300 		lp->flag &= ~LP_WITHIN_THR;
301 
302 	pr_debug("TCP-LP: %05o|%5u|%5u|%15u|%15u|%15u\n", lp->flag,
303 		 tcp_snd_cwnd(tp), lp->remote_hz, lp->owd_min, lp->owd_max,
304 		 lp->sowd >> 3);
305 
306 	if (lp->flag & LP_WITHIN_THR)
307 		return;
308 
309 	/* FIXME: try to reset owd_min and owd_max here
310 	 * so decrease the chance the min/max is no longer suitable
311 	 * and will usually within threshold when within inference */
312 	lp->owd_min = lp->sowd >> 3;
313 	lp->owd_max = lp->sowd >> 2;
314 	lp->owd_max_rsv = lp->sowd >> 2;
315 
316 	/* happened within inference
317 	 * drop snd_cwnd into 1 */
318 	if (lp->flag & LP_WITHIN_INF)
319 		tcp_snd_cwnd_set(tp, 1U);
320 
321 	/* happened after inference
322 	 * cut snd_cwnd into half */
323 	else
324 		tcp_snd_cwnd_set(tp, max(tcp_snd_cwnd(tp) >> 1U, 1U));
325 
326 	/* record this drop time */
327 	lp->last_drop = now;
328 }
329 
330 static struct tcp_congestion_ops tcp_lp __read_mostly = {
331 	.init = tcp_lp_init,
332 	.ssthresh = tcp_reno_ssthresh,
333 	.undo_cwnd = tcp_reno_undo_cwnd,
334 	.cong_avoid = tcp_lp_cong_avoid,
335 	.pkts_acked = tcp_lp_pkts_acked,
336 
337 	.owner = THIS_MODULE,
338 	.name = "lp"
339 };
340 
341 static int __init tcp_lp_register(void)
342 {
343 	BUILD_BUG_ON(sizeof(struct lp) > ICSK_CA_PRIV_SIZE);
344 	return tcp_register_congestion_control(&tcp_lp);
345 }
346 
347 static void __exit tcp_lp_unregister(void)
348 {
349 	tcp_unregister_congestion_control(&tcp_lp);
350 }
351 
352 module_init(tcp_lp_register);
353 module_exit(tcp_lp_unregister);
354 
355 MODULE_AUTHOR("Wong Hoi Sing Edison, Hung Hing Lun Mike");
356 MODULE_LICENSE("GPL");
357 MODULE_DESCRIPTION("TCP Low Priority");
358