blob: 0306d257fa6461e1b5e0c3edf0a1173c7d819b4b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Stephen Hemminger317a76f2005-06-23 12:19:55 -07002/*
Fabian Frederickb92022f2014-11-04 20:25:38 +01003 * Pluggable TCP congestion control support and newReno
Stephen Hemminger317a76f2005-06-23 12:19:55 -07004 * congestion control.
Masanari Iida02582e92012-08-22 19:11:26 +09005 * Based on ideas from I/O scheduler support and Web100.
Stephen Hemminger317a76f2005-06-23 12:19:55 -07006 *
7 * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
8 */
9
Joe Perchesafd465032012-03-12 07:03:32 +000010#define pr_fmt(fmt) "TCP: " fmt
11
Stephen Hemminger317a76f2005-06-23 12:19:55 -070012#include <linux/module.h>
13#include <linux/mm.h>
14#include <linux/types.h>
15#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +010017#include <linux/jhash.h>
Stephen Hemminger317a76f2005-06-23 12:19:55 -070018#include <net/tcp.h>
Ping Gan15fcdf62022-04-06 09:09:56 +080019#include <trace/events/tcp.h>
Stephen Hemminger317a76f2005-06-23 12:19:55 -070020
21static DEFINE_SPINLOCK(tcp_cong_list_lock);
22static LIST_HEAD(tcp_cong_list);
23
24/* Simple linear search, don't expect many entries! */
Martin KaFai Lau0baf26b2020-01-08 16:35:08 -080025struct tcp_congestion_ops *tcp_ca_find(const char *name)
Stephen Hemminger317a76f2005-06-23 12:19:55 -070026{
27 struct tcp_congestion_ops *e;
28
Stephen Hemminger5f8ef482005-06-23 20:37:36 -070029 list_for_each_entry_rcu(e, &tcp_cong_list, list) {
Stephen Hemminger317a76f2005-06-23 12:19:55 -070030 if (strcmp(e->name, name) == 0)
31 return e;
32 }
33
34 return NULL;
35}
36
Ping Gan15fcdf62022-04-06 09:09:56 +080037void 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
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +010048/* Must be called with rcu lock held */
Jason Xing61e2bba2024-05-31 23:46:34 +080049static struct tcp_congestion_ops *tcp_ca_find_autoload(const char *name)
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +010050{
Stephen Hemminger6670e152017-11-14 08:25:49 -080051 struct tcp_congestion_ops *ca = tcp_ca_find(name);
52
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +010053#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. */
65struct 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
Kui-Feng Lee8fb1a762023-03-22 20:23:59 -070077int tcp_validate_congestion_control(struct tcp_congestion_ops *ca)
Stephen Hemminger317a76f2005-06-23 12:19:55 -070078{
Florian Westphale9799182016-11-21 14:18:38 +010079 /* all algorithms must implement these */
80 if (!ca->ssthresh || !ca->undo_cwnd ||
81 !(ca->cong_avoid || ca->cong_control)) {
Joe Perchesafd465032012-03-12 07:03:32 +000082 pr_err("%s does not implement required ops\n", ca->name);
Stephen Hemminger317a76f2005-06-23 12:19:55 -070083 return -EINVAL;
84 }
85
Kui-Feng Lee8fb1a762023-03-22 20:23:59 -070086 return 0;
87}
88
89/* Attach new congestion control algorithm to the list
90 * of available options.
91 */
92int 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
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100100 ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
101
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700102 spin_lock(&tcp_cong_list_lock);
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100103 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);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700106 ret = -EEXIST;
107 } else {
Stephen Hemminger3d2573f2006-09-24 20:11:58 -0700108 list_add_tail_rcu(&ca->list, &tcp_cong_list);
stephen hemmingerdb2855a2015-02-16 09:38:13 -0500109 pr_debug("%s registered\n", ca->name);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700110 }
111 spin_unlock(&tcp_cong_list_lock);
112
113 return ret;
114}
115EXPORT_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 */
123void 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);
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100128
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();
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700137}
138EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
139
Kui-Feng Lee8fb1a762023-03-22 20:23:59 -0700140/* 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 */
145int tcp_update_congestion_control(struct tcp_congestion_ops *ca, struct tcp_congestion_ops *old_ca)
146{
147 struct tcp_congestion_ops *existing;
Kui-Feng Lee73e4f9e2024-02-24 14:34:16 -0800148 int ret = 0;
Kui-Feng Lee8fb1a762023-03-22 20:23:59 -0700149
150 ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
151
152 spin_lock(&tcp_cong_list_lock);
153 existing = tcp_ca_find_key(old_ca->key);
154 if (ca->key == TCP_CA_UNSPEC || !existing || strcmp(existing->name, ca->name)) {
155 pr_notice("%s not registered or non-unique key\n",
156 ca->name);
157 ret = -EINVAL;
158 } else if (existing != old_ca) {
159 pr_notice("invalid old congestion control algorithm to replace\n");
160 ret = -EINVAL;
161 } else {
162 /* Add the new one before removing the old one to keep
163 * one implementation available all the time.
164 */
165 list_add_tail_rcu(&ca->list, &tcp_cong_list);
166 list_del_rcu(&existing->list);
167 pr_debug("%s updated\n", ca->name);
168 }
169 spin_unlock(&tcp_cong_list_lock);
170
171 /* Wait for outstanding readers to complete before the
172 * module or struct_ops gets removed entirely.
173 */
174 if (!ret)
175 synchronize_rcu();
176
177 return ret;
178}
179
Jason Xing61e2bba2024-05-31 23:46:34 +0800180u32 tcp_ca_get_key_by_name(const char *name, bool *ecn_ca)
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100181{
182 const struct tcp_congestion_ops *ca;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200183 u32 key = TCP_CA_UNSPEC;
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100184
185 might_sleep();
186
187 rcu_read_lock();
Jason Xing61e2bba2024-05-31 23:46:34 +0800188 ca = tcp_ca_find_autoload(name);
Daniel Borkmannc3a8d942015-08-31 15:58:47 +0200189 if (ca) {
190 key = ca->key;
191 *ecn_ca = ca->flags & TCP_CONG_NEEDS_ECN;
192 }
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100193 rcu_read_unlock();
194
195 return key;
196}
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100197
198char *tcp_ca_get_name_by_key(u32 key, char *buffer)
199{
200 const struct tcp_congestion_ops *ca;
201 char *ret = NULL;
202
203 rcu_read_lock();
204 ca = tcp_ca_find_key(key);
Kees Cooka3bfc092024-07-13 21:11:15 -0700205 if (ca) {
206 strscpy(buffer, ca->name, TCP_CA_NAME_MAX);
207 ret = buffer;
208 }
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100209 rcu_read_unlock();
210
211 return ret;
212}
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100213
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700214/* Assign choice of congestion control. */
Florian Westphal55d86942014-09-26 22:37:32 +0200215void tcp_assign_congestion_control(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700216{
Stephen Hemminger6670e152017-11-14 08:25:49 -0800217 struct net *net = sock_net(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300218 struct inet_connection_sock *icsk = inet_csk(sk);
Stephen Hemminger6670e152017-11-14 08:25:49 -0800219 const struct tcp_congestion_ops *ca;
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700220
Florian Westphal55d86942014-09-26 22:37:32 +0200221 rcu_read_lock();
Stephen Hemminger6670e152017-11-14 08:25:49 -0800222 ca = rcu_dereference(net->ipv4.tcp_congestion_control);
Martin KaFai Lau0baf26b2020-01-08 16:35:08 -0800223 if (unlikely(!bpf_try_module_get(ca, ca->owner)))
Stephen Hemminger6670e152017-11-14 08:25:49 -0800224 ca = &tcp_reno;
225 icsk->icsk_ca_ops = ca;
Florian Westphal55d86942014-09-26 22:37:32 +0200226 rcu_read_unlock();
227
Stephen Hemminger6670e152017-11-14 08:25:49 -0800228 memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
Eric Dumazet6ac705b2015-09-25 07:39:18 -0700229 if (ca->flags & TCP_CONG_NEEDS_ECN)
230 INET_ECN_xmit(sk);
231 else
232 INET_ECN_dontxmit(sk);
Florian Westphal55d86942014-09-26 22:37:32 +0200233}
234
235void tcp_init_congestion_control(struct sock *sk)
236{
Neal Cardwell8919a9b2020-09-10 15:35:32 -0400237 struct inet_connection_sock *icsk = inet_csk(sk);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700238
Yuchung Cheng44abafc2017-05-31 11:21:27 -0700239 tcp_sk(sk)->prior_ssthresh = 0;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300240 if (icsk->icsk_ca_ops->init)
241 icsk->icsk_ca_ops->init(sk);
Eric Dumazet6ac705b2015-09-25 07:39:18 -0700242 if (tcp_ca_needs_ecn(sk))
243 INET_ECN_xmit(sk);
244 else
245 INET_ECN_dontxmit(sk);
Neal Cardwell8919a9b2020-09-10 15:35:32 -0400246 icsk->icsk_ca_initialized = 1;
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700247}
248
Sabrina Dubrocaebfa00c2017-08-25 13:10:12 +0200249static void tcp_reinit_congestion_control(struct sock *sk,
250 const struct tcp_congestion_ops *ca)
Daniel Borkmann29ba4fff2015-01-05 23:57:45 +0100251{
252 struct inet_connection_sock *icsk = inet_csk(sk);
253
254 tcp_cleanup_congestion_control(sk);
255 icsk->icsk_ca_ops = ca;
Neal Cardwell9f950412015-05-29 13:47:07 -0400256 icsk->icsk_ca_setsockopt = 1;
Wei Wangc1201442017-04-25 17:38:02 -0700257 memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
Daniel Borkmann29ba4fff2015-01-05 23:57:45 +0100258
Alexander Duyck55472012020-11-19 13:23:58 -0800259 if (ca->flags & TCP_CONG_NEEDS_ECN)
260 INET_ECN_xmit(sk);
261 else
262 INET_ECN_dontxmit(sk);
263
Christoph Paaschce69e562020-07-08 16:18:34 -0700264 if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
Eric Dumazet6ac705b2015-09-25 07:39:18 -0700265 tcp_init_congestion_control(sk);
Daniel Borkmann29ba4fff2015-01-05 23:57:45 +0100266}
267
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700268/* Manage refcounts on socket close. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300269void tcp_cleanup_congestion_control(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700270{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300271 struct inet_connection_sock *icsk = inet_csk(sk);
272
273 if (icsk->icsk_ca_ops->release)
274 icsk->icsk_ca_ops->release(sk);
Martin KaFai Lau0baf26b2020-01-08 16:35:08 -0800275 bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700276}
277
278/* Used by sysctl to change default congestion control */
Stephen Hemminger6670e152017-11-14 08:25:49 -0800279int tcp_set_default_congestion_control(struct net *net, const char *name)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700280{
281 struct tcp_congestion_ops *ca;
Stephen Hemminger6670e152017-11-14 08:25:49 -0800282 const struct tcp_congestion_ops *prev;
283 int ret;
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700284
Stephen Hemminger6670e152017-11-14 08:25:49 -0800285 rcu_read_lock();
Jason Xing61e2bba2024-05-31 23:46:34 +0800286 ca = tcp_ca_find_autoload(name);
Stephen Hemminger6670e152017-11-14 08:25:49 -0800287 if (!ca) {
288 ret = -ENOENT;
Martin KaFai Lau0baf26b2020-01-08 16:35:08 -0800289 } else if (!bpf_try_module_get(ca, ca->owner)) {
Stephen Hemminger6670e152017-11-14 08:25:49 -0800290 ret = -EBUSY;
Jonathon Reinhart8d432592021-05-01 04:28:22 -0400291 } else if (!net_eq(net, &init_net) &&
292 !(ca->flags & TCP_CONG_NON_RESTRICTED)) {
293 /* Only init netns can set default to a restricted algorithm */
294 ret = -EPERM;
Stephen Hemminger6670e152017-11-14 08:25:49 -0800295 } else {
296 prev = xchg(&net->ipv4.tcp_congestion_control, ca);
297 if (prev)
Martin KaFai Lau0baf26b2020-01-08 16:35:08 -0800298 bpf_module_put(prev, prev->owner);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700299
Stephen Hemminger6670e152017-11-14 08:25:49 -0800300 ca->flags |= TCP_CONG_NON_RESTRICTED;
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700301 ret = 0;
302 }
Stephen Hemminger6670e152017-11-14 08:25:49 -0800303 rcu_read_unlock();
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700304
305 return ret;
306}
307
Stephen Hemmingerb1736a72006-10-31 17:31:33 -0800308/* Set default value from kernel configuration at bootup */
309static int __init tcp_congestion_default(void)
310{
Stephen Hemminger6670e152017-11-14 08:25:49 -0800311 return tcp_set_default_congestion_control(&init_net,
312 CONFIG_DEFAULT_TCP_CONG);
Stephen Hemmingerb1736a72006-10-31 17:31:33 -0800313}
314late_initcall(tcp_congestion_default);
315
Stephen Hemminger3ff825b2006-11-09 16:32:06 -0800316/* Build string with list of available congestion control values */
317void tcp_get_available_congestion_control(char *buf, size_t maxlen)
318{
319 struct tcp_congestion_ops *ca;
320 size_t offs = 0;
321
322 rcu_read_lock();
323 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
324 offs += snprintf(buf + offs, maxlen - offs,
325 "%s%s",
326 offs == 0 ? "" : " ", ca->name);
Hangbin Liu9bb59a22019-11-20 16:38:08 +0800327
328 if (WARN_ON_ONCE(offs >= maxlen))
329 break;
Stephen Hemminger3ff825b2006-11-09 16:32:06 -0800330 }
331 rcu_read_unlock();
332}
333
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700334/* Get current default congestion control */
Stephen Hemminger6670e152017-11-14 08:25:49 -0800335void tcp_get_default_congestion_control(struct net *net, char *name)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700336{
Stephen Hemminger6670e152017-11-14 08:25:49 -0800337 const struct tcp_congestion_ops *ca;
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700338
339 rcu_read_lock();
Stephen Hemminger6670e152017-11-14 08:25:49 -0800340 ca = rcu_dereference(net->ipv4.tcp_congestion_control);
Kees Cooka3bfc092024-07-13 21:11:15 -0700341 strscpy(name, ca->name, TCP_CA_NAME_MAX);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700342 rcu_read_unlock();
343}
344
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800345/* Built list of non-restricted congestion control values */
346void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
347{
348 struct tcp_congestion_ops *ca;
349 size_t offs = 0;
350
351 *buf = '\0';
352 rcu_read_lock();
353 list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
Stephen Hemminger164891a2007-04-23 22:26:16 -0700354 if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800355 continue;
356 offs += snprintf(buf + offs, maxlen - offs,
357 "%s%s",
358 offs == 0 ? "" : " ", ca->name);
Hangbin Liu9bb59a22019-11-20 16:38:08 +0800359
360 if (WARN_ON_ONCE(offs >= maxlen))
361 break;
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800362 }
363 rcu_read_unlock();
364}
365
366/* Change list of non-restricted congestion control */
367int tcp_set_allowed_congestion_control(char *val)
368{
369 struct tcp_congestion_ops *ca;
Julia Lawallc34186e2010-08-27 19:31:56 -0700370 char *saved_clone, *clone, *name;
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800371 int ret = 0;
372
Julia Lawallc34186e2010-08-27 19:31:56 -0700373 saved_clone = clone = kstrdup(val, GFP_USER);
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800374 if (!clone)
375 return -ENOMEM;
376
377 spin_lock(&tcp_cong_list_lock);
378 /* pass 1 check for bad entries */
379 while ((name = strsep(&clone, " ")) && *name) {
380 ca = tcp_ca_find(name);
381 if (!ca) {
382 ret = -ENOENT;
383 goto out;
384 }
385 }
386
Stephen Hemminger164891a2007-04-23 22:26:16 -0700387 /* pass 2 clear old values */
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800388 list_for_each_entry_rcu(ca, &tcp_cong_list, list)
Stephen Hemminger164891a2007-04-23 22:26:16 -0700389 ca->flags &= ~TCP_CONG_NON_RESTRICTED;
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800390
391 /* pass 3 mark as allowed */
392 while ((name = strsep(&val, " ")) && *name) {
393 ca = tcp_ca_find(name);
394 WARN_ON(!ca);
395 if (ca)
Stephen Hemminger164891a2007-04-23 22:26:16 -0700396 ca->flags |= TCP_CONG_NON_RESTRICTED;
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800397 }
398out:
399 spin_unlock(&tcp_cong_list_lock);
Julia Lawallc34186e2010-08-27 19:31:56 -0700400 kfree(saved_clone);
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800401
402 return ret;
403}
404
Lawrence Brakmo91b5b212017-06-30 20:02:49 -0700405/* Change congestion control for socket. If load is false, then it is the
406 * responsibility of the caller to call tcp_init_congestion_control or
407 * tcp_reinit_congestion_control (if the current congestion control was
408 * already initialized.
409 */
Eric Dumazet8d650cd2019-07-18 19:28:14 -0700410int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
Neal Cardwell29a94932020-09-10 15:35:34 -0400411 bool cap_net_admin)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700412{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300413 struct inet_connection_sock *icsk = inet_csk(sk);
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100414 const struct tcp_congestion_ops *ca;
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700415 int err = 0;
416
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100417 if (icsk->icsk_ca_dst_locked)
418 return -EPERM;
Stephen Hemminger4d4d3d12007-04-23 22:32:11 -0700419
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100420 rcu_read_lock();
Lawrence Brakmo91b5b212017-06-30 20:02:49 -0700421 if (!load)
422 ca = tcp_ca_find(name);
423 else
Jason Xing61e2bba2024-05-31 23:46:34 +0800424 ca = tcp_ca_find_autoload(name);
Stephen Hemminger6670e152017-11-14 08:25:49 -0800425
Daniel Borkmannc5c6a8a2015-01-05 23:57:46 +0100426 /* No change asking for existing value */
Neal Cardwell9f950412015-05-29 13:47:07 -0400427 if (ca == icsk->icsk_ca_ops) {
428 icsk->icsk_ca_setsockopt = 1;
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700429 goto out;
Neal Cardwell9f950412015-05-29 13:47:07 -0400430 }
Stephen Hemminger6670e152017-11-14 08:25:49 -0800431
Neal Cardwell5050bef2020-09-10 15:35:36 -0400432 if (!ca)
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700433 err = -ENOENT;
Neal Cardwell5050bef2020-09-10 15:35:36 -0400434 else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) || cap_net_admin))
Stephen Hemmingerce7bc3b2006-11-09 16:35:15 -0800435 err = -EPERM;
Neal Cardwell5050bef2020-09-10 15:35:36 -0400436 else if (!bpf_try_module_get(ca, ca->owner))
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700437 err = -EBUSY;
Neal Cardwell5050bef2020-09-10 15:35:36 -0400438 else
Daniel Borkmann29ba4fff2015-01-05 23:57:45 +0100439 tcp_reinit_congestion_control(sk, ca);
Stephen Hemminger5f8ef482005-06-23 20:37:36 -0700440 out:
441 rcu_read_unlock();
442 return err;
443}
444
Yuchung Cheng9f9843a72013-10-31 11:07:31 -0700445/* Slow start is used when congestion window is no greater than the slow start
446 * threshold. We base on RFC2581 and also handle stretch ACKs properly.
447 * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
448 * something better;) a packet is only considered (s)acked in its entirety to
449 * defend the ACK attacks described in the RFC. Slow start processes a stretch
450 * ACK of degree N as if N acks of degree 1 are received back to back except
451 * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
452 * returns the leftover acks to adjust cwnd in congestion avoidance mode.
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800453 */
David Vernet400031e2023-02-01 11:30:15 -0600454__bpf_kfunc u32 tcp_slow_start(struct tcp_sock *tp, u32 acked)
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800455{
Eric Dumazet40570372022-04-05 16:35:38 -0700456 u32 cwnd = min(tcp_snd_cwnd(tp) + acked, tp->snd_ssthresh);
Eric Dumazet973ec442013-02-02 05:23:16 +0000457
Eric Dumazet40570372022-04-05 16:35:38 -0700458 acked -= cwnd - tcp_snd_cwnd(tp);
459 tcp_snd_cwnd_set(tp, min(cwnd, tp->snd_cwnd_clamp));
Neal Cardwelle73ebb02015-01-28 20:01:35 -0500460
461 return acked;
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800462}
463EXPORT_SYMBOL_GPL(tcp_slow_start);
464
Neal Cardwell814d4882015-01-28 20:01:36 -0500465/* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w),
466 * for every packet that was ACKed.
467 */
David Vernet400031e2023-02-01 11:30:15 -0600468__bpf_kfunc void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked)
Ilpo Järvinen758ce5c2009-02-28 04:44:37 +0000469{
Neal Cardwell9949afa2015-03-10 17:17:03 -0400470 /* If credits accumulated at a higher w, apply them gently now. */
471 if (tp->snd_cwnd_cnt >= w) {
472 tp->snd_cwnd_cnt = 0;
Eric Dumazet40570372022-04-05 16:35:38 -0700473 tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
Neal Cardwell9949afa2015-03-10 17:17:03 -0400474 }
475
Neal Cardwell814d4882015-01-28 20:01:36 -0500476 tp->snd_cwnd_cnt += acked;
Ilpo Järvinen758ce5c2009-02-28 04:44:37 +0000477 if (tp->snd_cwnd_cnt >= w) {
Neal Cardwell814d4882015-01-28 20:01:36 -0500478 u32 delta = tp->snd_cwnd_cnt / w;
479
480 tp->snd_cwnd_cnt -= delta * w;
Eric Dumazet40570372022-04-05 16:35:38 -0700481 tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + delta);
Ilpo Järvinen758ce5c2009-02-28 04:44:37 +0000482 }
Eric Dumazet40570372022-04-05 16:35:38 -0700483 tcp_snd_cwnd_set(tp, min(tcp_snd_cwnd(tp), tp->snd_cwnd_clamp));
Ilpo Järvinen758ce5c2009-02-28 04:44:37 +0000484}
485EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
486
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700487/*
488 * TCP Reno congestion control
489 * This is special case used for fallback as well.
490 */
491/* This is Jacobson's slow start and congestion avoidance.
492 * SIGCOMM '88, p. 328.
493 */
David Vernet400031e2023-02-01 11:30:15 -0600494__bpf_kfunc void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700495{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300496 struct tcp_sock *tp = tcp_sk(sk);
497
Eric Dumazet24901552014-05-02 21:18:05 -0700498 if (!tcp_is_cwnd_limited(sk))
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700499 return;
500
Stephen Hemminger7faffa1c2005-11-10 17:07:24 -0800501 /* In "safe" area, increase. */
Yuchung Cheng071d5082015-07-09 13:16:29 -0700502 if (tcp_in_slow_start(tp)) {
Neal Cardwellc22bdca2015-01-28 20:01:37 -0500503 acked = tcp_slow_start(tp, acked);
504 if (!acked)
505 return;
506 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900507 /* In dangerous area, increase slowly. */
Eric Dumazet40570372022-04-05 16:35:38 -0700508 tcp_cong_avoid_ai(tp, tcp_snd_cwnd(tp), acked);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700509}
510EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
511
512/* Slow start threshold is half the congestion window (min 2) */
David Vernet400031e2023-02-01 11:30:15 -0600513__bpf_kfunc u32 tcp_reno_ssthresh(struct sock *sk)
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700514{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300515 const struct tcp_sock *tp = tcp_sk(sk);
stephen hemminger688d1942014-08-29 23:32:05 -0700516
Eric Dumazet40570372022-04-05 16:35:38 -0700517 return max(tcp_snd_cwnd(tp) >> 1U, 2U);
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700518}
519EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
520
David Vernet400031e2023-02-01 11:30:15 -0600521__bpf_kfunc u32 tcp_reno_undo_cwnd(struct sock *sk)
Florian Westphale9799182016-11-21 14:18:38 +0100522{
523 const struct tcp_sock *tp = tcp_sk(sk);
524
Eric Dumazet40570372022-04-05 16:35:38 -0700525 return max(tcp_snd_cwnd(tp), tp->prior_cwnd);
Florian Westphale9799182016-11-21 14:18:38 +0100526}
527EXPORT_SYMBOL_GPL(tcp_reno_undo_cwnd);
528
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700529struct tcp_congestion_ops tcp_reno = {
Stephen Hemminger164891a2007-04-23 22:26:16 -0700530 .flags = TCP_CONG_NON_RESTRICTED,
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700531 .name = "reno",
532 .owner = THIS_MODULE,
533 .ssthresh = tcp_reno_ssthresh,
534 .cong_avoid = tcp_reno_cong_avoid,
Florian Westphale9799182016-11-21 14:18:38 +0100535 .undo_cwnd = tcp_reno_undo_cwnd,
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700536};