tcp_cong.c 12 KB

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