xref: /openbmc/linux/net/ipv4/tcp_cong.c (revision 69f397e6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Pluggable TCP congestion control support and newReno
4  * congestion control.
5  * Based on ideas from I/O scheduler support and Web100.
6  *
7  * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
8  */
9 
10 #define pr_fmt(fmt) "TCP: " fmt
11 
12 #include <linux/module.h>
13 #include <linux/mm.h>
14 #include <linux/types.h>
15 #include <linux/list.h>
16 #include <linux/gfp.h>
17 #include <linux/jhash.h>
18 #include <net/tcp.h>
19 #include <trace/events/tcp.h>
20 
21 static DEFINE_SPINLOCK(tcp_cong_list_lock);
22 static LIST_HEAD(tcp_cong_list);
23 
24 /* Simple linear search, don't expect many entries! */
tcp_ca_find(const char * name)25 struct tcp_congestion_ops *tcp_ca_find(const char *name)
26 {
27 	struct tcp_congestion_ops *e;
28 
29 	list_for_each_entry_rcu(e, &tcp_cong_list, list) {
30 		if (strcmp(e->name, name) == 0)
31 			return e;
32 	}
33 
34 	return NULL;
35 }
36 
tcp_set_ca_state(struct sock * sk,const u8 ca_state)37 void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
38 {
39 	struct inet_connection_sock *icsk = inet_csk(sk);
40 
41 	trace_tcp_cong_state_set(sk, ca_state);
42 
43 	if (icsk->icsk_ca_ops->set_state)
44 		icsk->icsk_ca_ops->set_state(sk, ca_state);
45 	icsk->icsk_ca_state = ca_state;
46 }
47 
48 /* Must be called with rcu lock held */
tcp_ca_find_autoload(const char * name)49 static struct tcp_congestion_ops *tcp_ca_find_autoload(const char *name)
50 {
51 	struct tcp_congestion_ops *ca = tcp_ca_find(name);
52 
53 #ifdef CONFIG_MODULES
54 	if (!ca && capable(CAP_NET_ADMIN)) {
55 		rcu_read_unlock();
56 		request_module("tcp_%s", name);
57 		rcu_read_lock();
58 		ca = tcp_ca_find(name);
59 	}
60 #endif
61 	return ca;
62 }
63 
64 /* Simple linear search, not much in here. */
tcp_ca_find_key(u32 key)65 struct tcp_congestion_ops *tcp_ca_find_key(u32 key)
66 {
67 	struct tcp_congestion_ops *e;
68 
69 	list_for_each_entry_rcu(e, &tcp_cong_list, list) {
70 		if (e->key == key)
71 			return e;
72 	}
73 
74 	return NULL;
75 }
76 
tcp_validate_congestion_control(struct tcp_congestion_ops * ca)77 int tcp_validate_congestion_control(struct tcp_congestion_ops *ca)
78 {
79 	/* all algorithms must implement these */
80 	if (!ca->ssthresh || !ca->undo_cwnd ||
81 	    !(ca->cong_avoid || ca->cong_control)) {
82 		pr_err("%s does not implement required ops\n", ca->name);
83 		return -EINVAL;
84 	}
85 
86 	return 0;
87 }
88 
89 /* Attach new congestion control algorithm to the list
90  * of available options.
91  */
tcp_register_congestion_control(struct tcp_congestion_ops * ca)92 int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
93 {
94 	int ret;
95 
96 	ret = tcp_validate_congestion_control(ca);
97 	if (ret)
98 		return ret;
99 
100 	ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
101 
102 	spin_lock(&tcp_cong_list_lock);
103 	if (ca->key == TCP_CA_UNSPEC || tcp_ca_find_key(ca->key)) {
104 		pr_notice("%s already registered or non-unique key\n",
105 			  ca->name);
106 		ret = -EEXIST;
107 	} else {
108 		list_add_tail_rcu(&ca->list, &tcp_cong_list);
109 		pr_debug("%s registered\n", ca->name);
110 	}
111 	spin_unlock(&tcp_cong_list_lock);
112 
113 	return ret;
114 }
115 EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
116 
117 /*
118  * Remove congestion control algorithm, called from
119  * the module's remove function.  Module ref counts are used
120  * to ensure that this can't be done till all sockets using
121  * that method are closed.
122  */
tcp_unregister_congestion_control(struct tcp_congestion_ops * ca)123 void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
124 {
125 	spin_lock(&tcp_cong_list_lock);
126 	list_del_rcu(&ca->list);
127 	spin_unlock(&tcp_cong_list_lock);
128 
129 	/* Wait for outstanding readers to complete before the
130 	 * module gets removed entirely.
131 	 *
132 	 * A try_module_get() should fail by now as our module is
133 	 * in "going" state since no refs are held anymore and
134 	 * module_exit() handler being called.
135 	 */
136 	synchronize_rcu();
137 }
138 EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
139 
140 /* Replace a registered old ca with a new one.
141  *
142  * The new ca must have the same name as the old one, that has been
143  * registered.
144  */
tcp_update_congestion_control(struct tcp_congestion_ops * ca,struct tcp_congestion_ops * old_ca)145 int tcp_update_congestion_control(struct tcp_congestion_ops *ca, struct tcp_congestion_ops *old_ca)
146 {
147 	struct tcp_congestion_ops *existing;
148 	int ret;
149 
150 	ret = tcp_validate_congestion_control(ca);
151 	if (ret)
152 		return ret;
153 
154 	ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
155 
156 	spin_lock(&tcp_cong_list_lock);
157 	existing = tcp_ca_find_key(old_ca->key);
158 	if (ca->key == TCP_CA_UNSPEC || !existing || strcmp(existing->name, ca->name)) {
159 		pr_notice("%s not registered or non-unique key\n",
160 			  ca->name);
161 		ret = -EINVAL;
162 	} else if (existing != old_ca) {
163 		pr_notice("invalid old congestion control algorithm to replace\n");
164 		ret = -EINVAL;
165 	} else {
166 		/* Add the new one before removing the old one to keep
167 		 * one implementation available all the time.
168 		 */
169 		list_add_tail_rcu(&ca->list, &tcp_cong_list);
170 		list_del_rcu(&existing->list);
171 		pr_debug("%s updated\n", ca->name);
172 	}
173 	spin_unlock(&tcp_cong_list_lock);
174 
175 	/* Wait for outstanding readers to complete before the
176 	 * module or struct_ops gets removed entirely.
177 	 */
178 	if (!ret)
179 		synchronize_rcu();
180 
181 	return ret;
182 }
183 
tcp_ca_get_key_by_name(const char * name,bool * ecn_ca)184 u32 tcp_ca_get_key_by_name(const char *name, bool *ecn_ca)
185 {
186 	const struct tcp_congestion_ops *ca;
187 	u32 key = TCP_CA_UNSPEC;
188 
189 	might_sleep();
190 
191 	rcu_read_lock();
192 	ca = tcp_ca_find_autoload(name);
193 	if (ca) {
194 		key = ca->key;
195 		*ecn_ca = ca->flags & TCP_CONG_NEEDS_ECN;
196 	}
197 	rcu_read_unlock();
198 
199 	return key;
200 }
201 
tcp_ca_get_name_by_key(u32 key,char * buffer)202 char *tcp_ca_get_name_by_key(u32 key, char *buffer)
203 {
204 	const struct tcp_congestion_ops *ca;
205 	char *ret = NULL;
206 
207 	rcu_read_lock();
208 	ca = tcp_ca_find_key(key);
209 	if (ca)
210 		ret = strncpy(buffer, ca->name,
211 			      TCP_CA_NAME_MAX);
212 	rcu_read_unlock();
213 
214 	return ret;
215 }
216 
217 /* Assign choice of congestion control. */
tcp_assign_congestion_control(struct sock * sk)218 void tcp_assign_congestion_control(struct sock *sk)
219 {
220 	struct net *net = sock_net(sk);
221 	struct inet_connection_sock *icsk = inet_csk(sk);
222 	const struct tcp_congestion_ops *ca;
223 
224 	rcu_read_lock();
225 	ca = rcu_dereference(net->ipv4.tcp_congestion_control);
226 	if (unlikely(!bpf_try_module_get(ca, ca->owner)))
227 		ca = &tcp_reno;
228 	icsk->icsk_ca_ops = ca;
229 	rcu_read_unlock();
230 
231 	memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
232 	if (ca->flags & TCP_CONG_NEEDS_ECN)
233 		INET_ECN_xmit(sk);
234 	else
235 		INET_ECN_dontxmit(sk);
236 }
237 
tcp_init_congestion_control(struct sock * sk)238 void tcp_init_congestion_control(struct sock *sk)
239 {
240 	struct inet_connection_sock *icsk = inet_csk(sk);
241 
242 	tcp_sk(sk)->prior_ssthresh = 0;
243 	if (icsk->icsk_ca_ops->init)
244 		icsk->icsk_ca_ops->init(sk);
245 	if (tcp_ca_needs_ecn(sk))
246 		INET_ECN_xmit(sk);
247 	else
248 		INET_ECN_dontxmit(sk);
249 	icsk->icsk_ca_initialized = 1;
250 }
251 
tcp_reinit_congestion_control(struct sock * sk,const struct tcp_congestion_ops * ca)252 static void tcp_reinit_congestion_control(struct sock *sk,
253 					  const struct tcp_congestion_ops *ca)
254 {
255 	struct inet_connection_sock *icsk = inet_csk(sk);
256 
257 	tcp_cleanup_congestion_control(sk);
258 	icsk->icsk_ca_ops = ca;
259 	icsk->icsk_ca_setsockopt = 1;
260 	memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
261 
262 	if (ca->flags & TCP_CONG_NEEDS_ECN)
263 		INET_ECN_xmit(sk);
264 	else
265 		INET_ECN_dontxmit(sk);
266 
267 	if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
268 		tcp_init_congestion_control(sk);
269 }
270 
271 /* Manage refcounts on socket close. */
tcp_cleanup_congestion_control(struct sock * sk)272 void tcp_cleanup_congestion_control(struct sock *sk)
273 {
274 	struct inet_connection_sock *icsk = inet_csk(sk);
275 
276 	if (icsk->icsk_ca_ops->release)
277 		icsk->icsk_ca_ops->release(sk);
278 	bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
279 }
280 
281 /* Used by sysctl to change default congestion control */
tcp_set_default_congestion_control(struct net * net,const char * name)282 int tcp_set_default_congestion_control(struct net *net, const char *name)
283 {
284 	struct tcp_congestion_ops *ca;
285 	const struct tcp_congestion_ops *prev;
286 	int ret;
287 
288 	rcu_read_lock();
289 	ca = tcp_ca_find_autoload(name);
290 	if (!ca) {
291 		ret = -ENOENT;
292 	} else if (!bpf_try_module_get(ca, ca->owner)) {
293 		ret = -EBUSY;
294 	} else if (!net_eq(net, &init_net) &&
295 			!(ca->flags & TCP_CONG_NON_RESTRICTED)) {
296 		/* Only init netns can set default to a restricted algorithm */
297 		ret = -EPERM;
298 	} else {
299 		prev = xchg(&net->ipv4.tcp_congestion_control, ca);
300 		if (prev)
301 			bpf_module_put(prev, prev->owner);
302 
303 		ca->flags |= TCP_CONG_NON_RESTRICTED;
304 		ret = 0;
305 	}
306 	rcu_read_unlock();
307 
308 	return ret;
309 }
310 
311 /* Set default value from kernel configuration at bootup */
tcp_congestion_default(void)312 static int __init tcp_congestion_default(void)
313 {
314 	return tcp_set_default_congestion_control(&init_net,
315 						  CONFIG_DEFAULT_TCP_CONG);
316 }
317 late_initcall(tcp_congestion_default);
318 
319 /* Build string with list of available congestion control values */
tcp_get_available_congestion_control(char * buf,size_t maxlen)320 void tcp_get_available_congestion_control(char *buf, size_t maxlen)
321 {
322 	struct tcp_congestion_ops *ca;
323 	size_t offs = 0;
324 
325 	rcu_read_lock();
326 	list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
327 		offs += snprintf(buf + offs, maxlen - offs,
328 				 "%s%s",
329 				 offs == 0 ? "" : " ", ca->name);
330 
331 		if (WARN_ON_ONCE(offs >= maxlen))
332 			break;
333 	}
334 	rcu_read_unlock();
335 }
336 
337 /* Get current default congestion control */
tcp_get_default_congestion_control(struct net * net,char * name)338 void tcp_get_default_congestion_control(struct net *net, char *name)
339 {
340 	const struct tcp_congestion_ops *ca;
341 
342 	rcu_read_lock();
343 	ca = rcu_dereference(net->ipv4.tcp_congestion_control);
344 	strncpy(name, ca->name, TCP_CA_NAME_MAX);
345 	rcu_read_unlock();
346 }
347 
348 /* Built list of non-restricted congestion control values */
tcp_get_allowed_congestion_control(char * buf,size_t maxlen)349 void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
350 {
351 	struct tcp_congestion_ops *ca;
352 	size_t offs = 0;
353 
354 	*buf = '\0';
355 	rcu_read_lock();
356 	list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
357 		if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
358 			continue;
359 		offs += snprintf(buf + offs, maxlen - offs,
360 				 "%s%s",
361 				 offs == 0 ? "" : " ", ca->name);
362 
363 		if (WARN_ON_ONCE(offs >= maxlen))
364 			break;
365 	}
366 	rcu_read_unlock();
367 }
368 
369 /* Change list of non-restricted congestion control */
tcp_set_allowed_congestion_control(char * val)370 int tcp_set_allowed_congestion_control(char *val)
371 {
372 	struct tcp_congestion_ops *ca;
373 	char *saved_clone, *clone, *name;
374 	int ret = 0;
375 
376 	saved_clone = clone = kstrdup(val, GFP_USER);
377 	if (!clone)
378 		return -ENOMEM;
379 
380 	spin_lock(&tcp_cong_list_lock);
381 	/* pass 1 check for bad entries */
382 	while ((name = strsep(&clone, " ")) && *name) {
383 		ca = tcp_ca_find(name);
384 		if (!ca) {
385 			ret = -ENOENT;
386 			goto out;
387 		}
388 	}
389 
390 	/* pass 2 clear old values */
391 	list_for_each_entry_rcu(ca, &tcp_cong_list, list)
392 		ca->flags &= ~TCP_CONG_NON_RESTRICTED;
393 
394 	/* pass 3 mark as allowed */
395 	while ((name = strsep(&val, " ")) && *name) {
396 		ca = tcp_ca_find(name);
397 		WARN_ON(!ca);
398 		if (ca)
399 			ca->flags |= TCP_CONG_NON_RESTRICTED;
400 	}
401 out:
402 	spin_unlock(&tcp_cong_list_lock);
403 	kfree(saved_clone);
404 
405 	return ret;
406 }
407 
408 /* Change congestion control for socket. If load is false, then it is the
409  * responsibility of the caller to call tcp_init_congestion_control or
410  * tcp_reinit_congestion_control (if the current congestion control was
411  * already initialized.
412  */
tcp_set_congestion_control(struct sock * sk,const char * name,bool load,bool cap_net_admin)413 int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
414 			       bool cap_net_admin)
415 {
416 	struct inet_connection_sock *icsk = inet_csk(sk);
417 	const struct tcp_congestion_ops *ca;
418 	int err = 0;
419 
420 	if (icsk->icsk_ca_dst_locked)
421 		return -EPERM;
422 
423 	rcu_read_lock();
424 	if (!load)
425 		ca = tcp_ca_find(name);
426 	else
427 		ca = tcp_ca_find_autoload(name);
428 
429 	/* No change asking for existing value */
430 	if (ca == icsk->icsk_ca_ops) {
431 		icsk->icsk_ca_setsockopt = 1;
432 		goto out;
433 	}
434 
435 	if (!ca)
436 		err = -ENOENT;
437 	else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) || cap_net_admin))
438 		err = -EPERM;
439 	else if (!bpf_try_module_get(ca, ca->owner))
440 		err = -EBUSY;
441 	else
442 		tcp_reinit_congestion_control(sk, ca);
443  out:
444 	rcu_read_unlock();
445 	return err;
446 }
447 
448 /* Slow start is used when congestion window is no greater than the slow start
449  * threshold. We base on RFC2581 and also handle stretch ACKs properly.
450  * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
451  * something better;) a packet is only considered (s)acked in its entirety to
452  * defend the ACK attacks described in the RFC. Slow start processes a stretch
453  * ACK of degree N as if N acks of degree 1 are received back to back except
454  * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
455  * returns the leftover acks to adjust cwnd in congestion avoidance mode.
456  */
tcp_slow_start(struct tcp_sock * tp,u32 acked)457 __bpf_kfunc u32 tcp_slow_start(struct tcp_sock *tp, u32 acked)
458 {
459 	u32 cwnd = min(tcp_snd_cwnd(tp) + acked, tp->snd_ssthresh);
460 
461 	acked -= cwnd - tcp_snd_cwnd(tp);
462 	tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp));
463 
464 	return acked;
465 }
466 EXPORT_SYMBOL_GPL(tcp_slow_start);
467 
468 /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w),
469  * for every packet that was ACKed.
470  */
tcp_cong_avoid_ai(struct tcp_sock * tp,u32 w,u32 acked)471 __bpf_kfunc void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked)
472 {
473 	/* If credits accumulated at a higher w, apply them gently now. */
474 	if (tp->snd_cwnd_cnt >= w) {
475 		tp->snd_cwnd_cnt = 0;
476 		tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
477 	}
478 
479 	tp->snd_cwnd_cnt += acked;
480 	if (tp->snd_cwnd_cnt >= w) {
481 		u32 delta = tp->snd_cwnd_cnt / w;
482 
483 		tp->snd_cwnd_cnt -= delta * w;
484 		tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + delta);
485 	}
486 	tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp), tp->snd_cwnd_clamp));
487 }
488 EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
489 
490 /*
491  * TCP Reno congestion control
492  * This is special case used for fallback as well.
493  */
494 /* This is Jacobson's slow start and congestion avoidance.
495  * SIGCOMM '88, p. 328.
496  */
tcp_reno_cong_avoid(struct sock * sk,u32 ack,u32 acked)497 __bpf_kfunc void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
498 {
499 	struct tcp_sock *tp = tcp_sk(sk);
500 
501 	if (!tcp_is_cwnd_limited(sk))
502 		return;
503 
504 	/* In "safe" area, increase. */
505 	if (tcp_in_slow_start(tp)) {
506 		acked = tcp_slow_start(tp, acked);
507 		if (!acked)
508 			return;
509 	}
510 	/* In dangerous area, increase slowly. */
511 	tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked);
512 }
513 EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
514 
515 /* Slow start threshold is half the congestion window (min 2) */
tcp_reno_ssthresh(struct sock * sk)516 __bpf_kfunc u32 tcp_reno_ssthresh(struct sock *sk)
517 {
518 	const struct tcp_sock *tp = tcp_sk(sk);
519 
520 	return max(tcp_snd_cwnd(tp) >> 1U, 2U);
521 }
522 EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
523 
tcp_reno_undo_cwnd(struct sock * sk)524 __bpf_kfunc u32 tcp_reno_undo_cwnd(struct sock *sk)
525 {
526 	const struct tcp_sock *tp = tcp_sk(sk);
527 
528 	return max(tcp_snd_cwnd(tp), tp->prior_cwnd);
529 }
530 EXPORT_SYMBOL_GPL(tcp_reno_undo_cwnd);
531 
532 struct tcp_congestion_ops tcp_reno = {
533 	.flags		= TCP_CONG_NON_RESTRICTED,
534 	.name		= "reno",
535 	.owner		= THIS_MODULE,
536 	.ssthresh	= tcp_reno_ssthresh,
537 	.cong_avoid	= tcp_reno_cong_avoid,
538 	.undo_cwnd	= tcp_reno_undo_cwnd,
539 };
540