Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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