tcp_cong.c 13 KB

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