tcp_cong.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Plugable 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 <net/tcp.h>
  15. static DEFINE_SPINLOCK(tcp_cong_list_lock);
  16. static LIST_HEAD(tcp_cong_list);
  17. /* Simple linear search, don't expect many entries! */
  18. static struct tcp_congestion_ops *tcp_ca_find(const char *name)
  19. {
  20. struct tcp_congestion_ops *e;
  21. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  22. if (strcmp(e->name, name) == 0)
  23. return e;
  24. }
  25. return NULL;
  26. }
  27. /*
  28. * Attach new congestion control algorithm to the list
  29. * of available options.
  30. */
  31. int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
  32. {
  33. int ret = 0;
  34. /* all algorithms must implement ssthresh and cong_avoid ops */
  35. if (!ca->ssthresh || !ca->cong_avoid) {
  36. pr_err("%s does not implement required ops\n", ca->name);
  37. return -EINVAL;
  38. }
  39. spin_lock(&tcp_cong_list_lock);
  40. if (tcp_ca_find(ca->name)) {
  41. pr_notice("%s already registered\n", ca->name);
  42. ret = -EEXIST;
  43. } else {
  44. list_add_tail_rcu(&ca->list, &tcp_cong_list);
  45. pr_info("%s registered\n", ca->name);
  46. }
  47. spin_unlock(&tcp_cong_list_lock);
  48. return ret;
  49. }
  50. EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
  51. /*
  52. * Remove congestion control algorithm, called from
  53. * the module's remove function. Module ref counts are used
  54. * to ensure that this can't be done till all sockets using
  55. * that method are closed.
  56. */
  57. void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
  58. {
  59. spin_lock(&tcp_cong_list_lock);
  60. list_del_rcu(&ca->list);
  61. spin_unlock(&tcp_cong_list_lock);
  62. }
  63. EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
  64. /* Assign choice of congestion control. */
  65. void tcp_assign_congestion_control(struct sock *sk)
  66. {
  67. struct inet_connection_sock *icsk = inet_csk(sk);
  68. struct tcp_congestion_ops *ca;
  69. rcu_read_lock();
  70. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  71. if (likely(try_module_get(ca->owner))) {
  72. icsk->icsk_ca_ops = ca;
  73. goto out;
  74. }
  75. /* Fallback to next available. The last really
  76. * guaranteed fallback is Reno from this list.
  77. */
  78. }
  79. out:
  80. rcu_read_unlock();
  81. /* Clear out private data before diag gets it and
  82. * the ca has not been initialized.
  83. */
  84. if (ca->get_info)
  85. memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
  86. }
  87. void tcp_init_congestion_control(struct sock *sk)
  88. {
  89. const struct inet_connection_sock *icsk = inet_csk(sk);
  90. if (icsk->icsk_ca_ops->init)
  91. icsk->icsk_ca_ops->init(sk);
  92. }
  93. /* Manage refcounts on socket close. */
  94. void tcp_cleanup_congestion_control(struct sock *sk)
  95. {
  96. struct inet_connection_sock *icsk = inet_csk(sk);
  97. if (icsk->icsk_ca_ops->release)
  98. icsk->icsk_ca_ops->release(sk);
  99. module_put(icsk->icsk_ca_ops->owner);
  100. }
  101. /* Used by sysctl to change default congestion control */
  102. int tcp_set_default_congestion_control(const char *name)
  103. {
  104. struct tcp_congestion_ops *ca;
  105. int ret = -ENOENT;
  106. spin_lock(&tcp_cong_list_lock);
  107. ca = tcp_ca_find(name);
  108. #ifdef CONFIG_MODULES
  109. if (!ca && capable(CAP_NET_ADMIN)) {
  110. spin_unlock(&tcp_cong_list_lock);
  111. request_module("tcp_%s", name);
  112. spin_lock(&tcp_cong_list_lock);
  113. ca = tcp_ca_find(name);
  114. }
  115. #endif
  116. if (ca) {
  117. ca->flags |= TCP_CONG_NON_RESTRICTED; /* default is always allowed */
  118. list_move(&ca->list, &tcp_cong_list);
  119. ret = 0;
  120. }
  121. spin_unlock(&tcp_cong_list_lock);
  122. return ret;
  123. }
  124. /* Set default value from kernel configuration at bootup */
  125. static int __init tcp_congestion_default(void)
  126. {
  127. return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
  128. }
  129. late_initcall(tcp_congestion_default);
  130. /* Build string with list of available congestion control values */
  131. void tcp_get_available_congestion_control(char *buf, size_t maxlen)
  132. {
  133. struct tcp_congestion_ops *ca;
  134. size_t offs = 0;
  135. rcu_read_lock();
  136. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  137. offs += snprintf(buf + offs, maxlen - offs,
  138. "%s%s",
  139. offs == 0 ? "" : " ", ca->name);
  140. }
  141. rcu_read_unlock();
  142. }
  143. /* Get current default congestion control */
  144. void tcp_get_default_congestion_control(char *name)
  145. {
  146. struct tcp_congestion_ops *ca;
  147. /* We will always have reno... */
  148. BUG_ON(list_empty(&tcp_cong_list));
  149. rcu_read_lock();
  150. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  151. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  152. rcu_read_unlock();
  153. }
  154. /* Built list of non-restricted congestion control values */
  155. void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
  156. {
  157. struct tcp_congestion_ops *ca;
  158. size_t offs = 0;
  159. *buf = '\0';
  160. rcu_read_lock();
  161. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  162. if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
  163. continue;
  164. offs += snprintf(buf + offs, maxlen - offs,
  165. "%s%s",
  166. offs == 0 ? "" : " ", ca->name);
  167. }
  168. rcu_read_unlock();
  169. }
  170. /* Change list of non-restricted congestion control */
  171. int tcp_set_allowed_congestion_control(char *val)
  172. {
  173. struct tcp_congestion_ops *ca;
  174. char *saved_clone, *clone, *name;
  175. int ret = 0;
  176. saved_clone = clone = kstrdup(val, GFP_USER);
  177. if (!clone)
  178. return -ENOMEM;
  179. spin_lock(&tcp_cong_list_lock);
  180. /* pass 1 check for bad entries */
  181. while ((name = strsep(&clone, " ")) && *name) {
  182. ca = tcp_ca_find(name);
  183. if (!ca) {
  184. ret = -ENOENT;
  185. goto out;
  186. }
  187. }
  188. /* pass 2 clear old values */
  189. list_for_each_entry_rcu(ca, &tcp_cong_list, list)
  190. ca->flags &= ~TCP_CONG_NON_RESTRICTED;
  191. /* pass 3 mark as allowed */
  192. while ((name = strsep(&val, " ")) && *name) {
  193. ca = tcp_ca_find(name);
  194. WARN_ON(!ca);
  195. if (ca)
  196. ca->flags |= TCP_CONG_NON_RESTRICTED;
  197. }
  198. out:
  199. spin_unlock(&tcp_cong_list_lock);
  200. kfree(saved_clone);
  201. return ret;
  202. }
  203. /* Change congestion control for socket */
  204. int tcp_set_congestion_control(struct sock *sk, const char *name)
  205. {
  206. struct inet_connection_sock *icsk = inet_csk(sk);
  207. struct tcp_congestion_ops *ca;
  208. int err = 0;
  209. rcu_read_lock();
  210. ca = tcp_ca_find(name);
  211. /* no change asking for existing value */
  212. if (ca == icsk->icsk_ca_ops)
  213. goto out;
  214. #ifdef CONFIG_MODULES
  215. /* not found attempt to autoload module */
  216. if (!ca && capable(CAP_NET_ADMIN)) {
  217. rcu_read_unlock();
  218. request_module("tcp_%s", name);
  219. rcu_read_lock();
  220. ca = tcp_ca_find(name);
  221. }
  222. #endif
  223. if (!ca)
  224. err = -ENOENT;
  225. else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) ||
  226. ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)))
  227. err = -EPERM;
  228. else if (!try_module_get(ca->owner))
  229. err = -EBUSY;
  230. else {
  231. tcp_cleanup_congestion_control(sk);
  232. icsk->icsk_ca_ops = ca;
  233. if (sk->sk_state != TCP_CLOSE && icsk->icsk_ca_ops->init)
  234. icsk->icsk_ca_ops->init(sk);
  235. }
  236. out:
  237. rcu_read_unlock();
  238. return err;
  239. }
  240. /* Slow start is used when congestion window is no greater than the slow start
  241. * threshold. We base on RFC2581 and also handle stretch ACKs properly.
  242. * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
  243. * something better;) a packet is only considered (s)acked in its entirety to
  244. * defend the ACK attacks described in the RFC. Slow start processes a stretch
  245. * ACK of degree N as if N acks of degree 1 are received back to back except
  246. * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
  247. * returns the leftover acks to adjust cwnd in congestion avoidance mode.
  248. */
  249. void tcp_slow_start(struct tcp_sock *tp, u32 acked)
  250. {
  251. u32 cwnd = tp->snd_cwnd + acked;
  252. if (cwnd > tp->snd_ssthresh)
  253. cwnd = tp->snd_ssthresh + 1;
  254. tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
  255. }
  256. EXPORT_SYMBOL_GPL(tcp_slow_start);
  257. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w) */
  258. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w)
  259. {
  260. if (tp->snd_cwnd_cnt >= w) {
  261. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  262. tp->snd_cwnd++;
  263. tp->snd_cwnd_cnt = 0;
  264. } else {
  265. tp->snd_cwnd_cnt++;
  266. }
  267. }
  268. EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
  269. /*
  270. * TCP Reno congestion control
  271. * This is special case used for fallback as well.
  272. */
  273. /* This is Jacobson's slow start and congestion avoidance.
  274. * SIGCOMM '88, p. 328.
  275. */
  276. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  277. {
  278. struct tcp_sock *tp = tcp_sk(sk);
  279. if (!tcp_is_cwnd_limited(sk))
  280. return;
  281. /* In "safe" area, increase. */
  282. if (tp->snd_cwnd <= tp->snd_ssthresh)
  283. tcp_slow_start(tp, acked);
  284. /* In dangerous area, increase slowly. */
  285. else
  286. tcp_cong_avoid_ai(tp, tp->snd_cwnd);
  287. }
  288. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  289. /* Slow start threshold is half the congestion window (min 2) */
  290. u32 tcp_reno_ssthresh(struct sock *sk)
  291. {
  292. const struct tcp_sock *tp = tcp_sk(sk);
  293. return max(tp->snd_cwnd >> 1U, 2U);
  294. }
  295. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  296. struct tcp_congestion_ops tcp_reno = {
  297. .flags = TCP_CONG_NON_RESTRICTED,
  298. .name = "reno",
  299. .owner = THIS_MODULE,
  300. .ssthresh = tcp_reno_ssthresh,
  301. .cong_avoid = tcp_reno_cong_avoid,
  302. };