tcp_cong.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Pluggable TCP congestion control support and newReno
  3. * congestion control.
  4. * Based on ideas from I/O scheduler support and Web100.
  5. *
  6. * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
  7. */
  8. #define pr_fmt(fmt) "TCP: " fmt
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/types.h>
  12. #include <linux/list.h>
  13. #include <linux/gfp.h>
  14. #include <linux/jhash.h>
  15. #include <net/tcp.h>
  16. static DEFINE_SPINLOCK(tcp_cong_list_lock);
  17. static LIST_HEAD(tcp_cong_list);
  18. /* Simple linear search, don't expect many entries! */
  19. static struct tcp_congestion_ops *tcp_ca_find(const char *name)
  20. {
  21. struct tcp_congestion_ops *e;
  22. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  23. if (strcmp(e->name, name) == 0)
  24. return e;
  25. }
  26. return NULL;
  27. }
  28. /* Must be called with rcu lock held */
  29. static struct tcp_congestion_ops *tcp_ca_find_autoload(struct net *net,
  30. const char *name)
  31. {
  32. struct tcp_congestion_ops *ca = tcp_ca_find(name);
  33. #ifdef CONFIG_MODULES
  34. if (!ca && capable(CAP_NET_ADMIN)) {
  35. rcu_read_unlock();
  36. request_module("tcp_%s", name);
  37. rcu_read_lock();
  38. ca = tcp_ca_find(name);
  39. }
  40. #endif
  41. return ca;
  42. }
  43. /* Simple linear search, not much in here. */
  44. struct tcp_congestion_ops *tcp_ca_find_key(u32 key)
  45. {
  46. struct tcp_congestion_ops *e;
  47. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  48. if (e->key == key)
  49. return e;
  50. }
  51. return NULL;
  52. }
  53. /*
  54. * Attach new congestion control algorithm to the list
  55. * of available options.
  56. */
  57. int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
  58. {
  59. int ret = 0;
  60. /* all algorithms must implement these */
  61. if (!ca->ssthresh || !ca->undo_cwnd ||
  62. !(ca->cong_avoid || ca->cong_control)) {
  63. pr_err("%s does not implement required ops\n", ca->name);
  64. return -EINVAL;
  65. }
  66. ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
  67. spin_lock(&tcp_cong_list_lock);
  68. if (ca->key == TCP_CA_UNSPEC || tcp_ca_find_key(ca->key)) {
  69. pr_notice("%s already registered or non-unique key\n",
  70. ca->name);
  71. ret = -EEXIST;
  72. } else {
  73. list_add_tail_rcu(&ca->list, &tcp_cong_list);
  74. pr_debug("%s registered\n", ca->name);
  75. }
  76. spin_unlock(&tcp_cong_list_lock);
  77. return ret;
  78. }
  79. EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
  80. /*
  81. * Remove congestion control algorithm, called from
  82. * the module's remove function. Module ref counts are used
  83. * to ensure that this can't be done till all sockets using
  84. * that method are closed.
  85. */
  86. void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
  87. {
  88. spin_lock(&tcp_cong_list_lock);
  89. list_del_rcu(&ca->list);
  90. spin_unlock(&tcp_cong_list_lock);
  91. /* Wait for outstanding readers to complete before the
  92. * module gets removed entirely.
  93. *
  94. * A try_module_get() should fail by now as our module is
  95. * in "going" state since no refs are held anymore and
  96. * module_exit() handler being called.
  97. */
  98. synchronize_rcu();
  99. }
  100. EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
  101. u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca)
  102. {
  103. const struct tcp_congestion_ops *ca;
  104. u32 key = TCP_CA_UNSPEC;
  105. might_sleep();
  106. rcu_read_lock();
  107. ca = tcp_ca_find_autoload(net, name);
  108. if (ca) {
  109. key = ca->key;
  110. *ecn_ca = ca->flags & TCP_CONG_NEEDS_ECN;
  111. }
  112. rcu_read_unlock();
  113. return key;
  114. }
  115. EXPORT_SYMBOL_GPL(tcp_ca_get_key_by_name);
  116. char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  117. {
  118. const struct tcp_congestion_ops *ca;
  119. char *ret = NULL;
  120. rcu_read_lock();
  121. ca = tcp_ca_find_key(key);
  122. if (ca)
  123. ret = strncpy(buffer, ca->name,
  124. TCP_CA_NAME_MAX);
  125. rcu_read_unlock();
  126. return ret;
  127. }
  128. EXPORT_SYMBOL_GPL(tcp_ca_get_name_by_key);
  129. /* Assign choice of congestion control. */
  130. void tcp_assign_congestion_control(struct sock *sk)
  131. {
  132. struct net *net = sock_net(sk);
  133. struct inet_connection_sock *icsk = inet_csk(sk);
  134. const struct tcp_congestion_ops *ca;
  135. rcu_read_lock();
  136. ca = rcu_dereference(net->ipv4.tcp_congestion_control);
  137. if (unlikely(!try_module_get(ca->owner)))
  138. ca = &tcp_reno;
  139. icsk->icsk_ca_ops = ca;
  140. rcu_read_unlock();
  141. memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
  142. if (ca->flags & TCP_CONG_NEEDS_ECN)
  143. INET_ECN_xmit(sk);
  144. else
  145. INET_ECN_dontxmit(sk);
  146. }
  147. void tcp_init_congestion_control(struct sock *sk)
  148. {
  149. const struct inet_connection_sock *icsk = inet_csk(sk);
  150. tcp_sk(sk)->prior_ssthresh = 0;
  151. if (icsk->icsk_ca_ops->init)
  152. icsk->icsk_ca_ops->init(sk);
  153. if (tcp_ca_needs_ecn(sk))
  154. INET_ECN_xmit(sk);
  155. else
  156. INET_ECN_dontxmit(sk);
  157. }
  158. static void tcp_reinit_congestion_control(struct sock *sk,
  159. const struct tcp_congestion_ops *ca)
  160. {
  161. struct inet_connection_sock *icsk = inet_csk(sk);
  162. tcp_cleanup_congestion_control(sk);
  163. icsk->icsk_ca_ops = ca;
  164. icsk->icsk_ca_setsockopt = 1;
  165. memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
  166. if (ca->flags & TCP_CONG_NEEDS_ECN)
  167. INET_ECN_xmit(sk);
  168. else
  169. INET_ECN_dontxmit(sk);
  170. if (!((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  171. tcp_init_congestion_control(sk);
  172. }
  173. /* Manage refcounts on socket close. */
  174. void tcp_cleanup_congestion_control(struct sock *sk)
  175. {
  176. struct inet_connection_sock *icsk = inet_csk(sk);
  177. if (icsk->icsk_ca_ops->release)
  178. icsk->icsk_ca_ops->release(sk);
  179. module_put(icsk->icsk_ca_ops->owner);
  180. }
  181. /* Used by sysctl to change default congestion control */
  182. int tcp_set_default_congestion_control(struct net *net, const char *name)
  183. {
  184. struct tcp_congestion_ops *ca;
  185. const struct tcp_congestion_ops *prev;
  186. int ret;
  187. rcu_read_lock();
  188. ca = tcp_ca_find_autoload(net, name);
  189. if (!ca) {
  190. ret = -ENOENT;
  191. } else if (!try_module_get(ca->owner)) {
  192. ret = -EBUSY;
  193. } else if (!net_eq(net, &init_net) &&
  194. !(ca->flags & TCP_CONG_NON_RESTRICTED)) {
  195. /* Only init netns can set default to a restricted algorithm */
  196. ret = -EPERM;
  197. } else {
  198. prev = xchg(&net->ipv4.tcp_congestion_control, ca);
  199. if (prev)
  200. module_put(prev->owner);
  201. ca->flags |= TCP_CONG_NON_RESTRICTED;
  202. ret = 0;
  203. }
  204. rcu_read_unlock();
  205. return ret;
  206. }
  207. /* Set default value from kernel configuration at bootup */
  208. static int __init tcp_congestion_default(void)
  209. {
  210. return tcp_set_default_congestion_control(&init_net,
  211. CONFIG_DEFAULT_TCP_CONG);
  212. }
  213. late_initcall(tcp_congestion_default);
  214. /* Build string with list of available congestion control values */
  215. void tcp_get_available_congestion_control(char *buf, size_t maxlen)
  216. {
  217. struct tcp_congestion_ops *ca;
  218. size_t offs = 0;
  219. rcu_read_lock();
  220. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  221. offs += snprintf(buf + offs, maxlen - offs,
  222. "%s%s",
  223. offs == 0 ? "" : " ", ca->name);
  224. }
  225. rcu_read_unlock();
  226. }
  227. /* Get current default congestion control */
  228. void tcp_get_default_congestion_control(struct net *net, char *name)
  229. {
  230. const struct tcp_congestion_ops *ca;
  231. rcu_read_lock();
  232. ca = rcu_dereference(net->ipv4.tcp_congestion_control);
  233. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  234. rcu_read_unlock();
  235. }
  236. /* Built list of non-restricted congestion control values */
  237. void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
  238. {
  239. struct tcp_congestion_ops *ca;
  240. size_t offs = 0;
  241. *buf = '\0';
  242. rcu_read_lock();
  243. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  244. if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
  245. continue;
  246. offs += snprintf(buf + offs, maxlen - offs,
  247. "%s%s",
  248. offs == 0 ? "" : " ", ca->name);
  249. }
  250. rcu_read_unlock();
  251. }
  252. /* Change list of non-restricted congestion control */
  253. int tcp_set_allowed_congestion_control(char *val)
  254. {
  255. struct tcp_congestion_ops *ca;
  256. char *saved_clone, *clone, *name;
  257. int ret = 0;
  258. saved_clone = clone = kstrdup(val, GFP_USER);
  259. if (!clone)
  260. return -ENOMEM;
  261. spin_lock(&tcp_cong_list_lock);
  262. /* pass 1 check for bad entries */
  263. while ((name = strsep(&clone, " ")) && *name) {
  264. ca = tcp_ca_find(name);
  265. if (!ca) {
  266. ret = -ENOENT;
  267. goto out;
  268. }
  269. }
  270. /* pass 2 clear old values */
  271. list_for_each_entry_rcu(ca, &tcp_cong_list, list)
  272. ca->flags &= ~TCP_CONG_NON_RESTRICTED;
  273. /* pass 3 mark as allowed */
  274. while ((name = strsep(&val, " ")) && *name) {
  275. ca = tcp_ca_find(name);
  276. WARN_ON(!ca);
  277. if (ca)
  278. ca->flags |= TCP_CONG_NON_RESTRICTED;
  279. }
  280. out:
  281. spin_unlock(&tcp_cong_list_lock);
  282. kfree(saved_clone);
  283. return ret;
  284. }
  285. /* Change congestion control for socket. If load is false, then it is the
  286. * responsibility of the caller to call tcp_init_congestion_control or
  287. * tcp_reinit_congestion_control (if the current congestion control was
  288. * already initialized.
  289. */
  290. int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
  291. bool reinit, bool cap_net_admin)
  292. {
  293. struct inet_connection_sock *icsk = inet_csk(sk);
  294. const struct tcp_congestion_ops *ca;
  295. int err = 0;
  296. if (icsk->icsk_ca_dst_locked)
  297. return -EPERM;
  298. rcu_read_lock();
  299. if (!load)
  300. ca = tcp_ca_find(name);
  301. else
  302. ca = tcp_ca_find_autoload(sock_net(sk), name);
  303. /* No change asking for existing value */
  304. if (ca == icsk->icsk_ca_ops) {
  305. icsk->icsk_ca_setsockopt = 1;
  306. goto out;
  307. }
  308. if (!ca) {
  309. err = -ENOENT;
  310. } else if (!load) {
  311. const struct tcp_congestion_ops *old_ca = icsk->icsk_ca_ops;
  312. if (try_module_get(ca->owner)) {
  313. if (reinit) {
  314. tcp_reinit_congestion_control(sk, ca);
  315. } else {
  316. icsk->icsk_ca_ops = ca;
  317. module_put(old_ca->owner);
  318. }
  319. } else {
  320. err = -EBUSY;
  321. }
  322. } else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) || cap_net_admin)) {
  323. err = -EPERM;
  324. } else if (!try_module_get(ca->owner)) {
  325. err = -EBUSY;
  326. } else {
  327. tcp_reinit_congestion_control(sk, ca);
  328. }
  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(tp->snd_cwnd + acked, tp->snd_ssthresh);
  345. acked -= cwnd - tp->snd_cwnd;
  346. tp->snd_cwnd = 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. tp->snd_cwnd++;
  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. tp->snd_cwnd += delta;
  365. }
  366. tp->snd_cwnd = min(tp->snd_cwnd, 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, tp->snd_cwnd, 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(tp->snd_cwnd >> 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(tp->snd_cwnd, 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. };