tcp_cong.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Plugable TCP congestion control support and newReno
  3. * congestion control.
  4. * Based on ideas from I/O scheduler suport and Web100.
  5. *
  6. * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
  7. */
  8. #include <linux/config.h>
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/types.h>
  12. #include <linux/list.h>
  13. #include <net/tcp.h>
  14. static DEFINE_SPINLOCK(tcp_cong_list_lock);
  15. static LIST_HEAD(tcp_cong_list);
  16. /* Simple linear search, don't expect many entries! */
  17. static struct tcp_congestion_ops *tcp_ca_find(const char *name)
  18. {
  19. struct tcp_congestion_ops *e;
  20. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  21. if (strcmp(e->name, name) == 0)
  22. return e;
  23. }
  24. return NULL;
  25. }
  26. /*
  27. * Attach new congestion control algorthim to the list
  28. * of available options.
  29. */
  30. int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
  31. {
  32. int ret = 0;
  33. /* all algorithms must implement ssthresh and cong_avoid ops */
  34. if (!ca->ssthresh || !ca->cong_avoid || !ca->min_cwnd) {
  35. printk(KERN_ERR "TCP %s does not implement required ops\n",
  36. ca->name);
  37. return -EINVAL;
  38. }
  39. spin_lock(&tcp_cong_list_lock);
  40. if (tcp_ca_find(ca->name)) {
  41. printk(KERN_NOTICE "TCP %s already registered\n", ca->name);
  42. ret = -EEXIST;
  43. } else {
  44. list_add_rcu(&ca->list, &tcp_cong_list);
  45. printk(KERN_INFO "TCP %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_init_congestion_control(struct sock *sk)
  66. {
  67. struct inet_connection_sock *icsk = inet_csk(sk);
  68. struct tcp_congestion_ops *ca;
  69. if (icsk->icsk_ca_ops != &tcp_init_congestion_ops)
  70. return;
  71. rcu_read_lock();
  72. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  73. if (try_module_get(ca->owner)) {
  74. icsk->icsk_ca_ops = ca;
  75. break;
  76. }
  77. }
  78. rcu_read_unlock();
  79. if (icsk->icsk_ca_ops->init)
  80. icsk->icsk_ca_ops->init(sk);
  81. }
  82. /* Manage refcounts on socket close. */
  83. void tcp_cleanup_congestion_control(struct sock *sk)
  84. {
  85. struct inet_connection_sock *icsk = inet_csk(sk);
  86. if (icsk->icsk_ca_ops->release)
  87. icsk->icsk_ca_ops->release(sk);
  88. module_put(icsk->icsk_ca_ops->owner);
  89. }
  90. /* Used by sysctl to change default congestion control */
  91. int tcp_set_default_congestion_control(const char *name)
  92. {
  93. struct tcp_congestion_ops *ca;
  94. int ret = -ENOENT;
  95. spin_lock(&tcp_cong_list_lock);
  96. ca = tcp_ca_find(name);
  97. #ifdef CONFIG_KMOD
  98. if (!ca) {
  99. spin_unlock(&tcp_cong_list_lock);
  100. request_module("tcp_%s", name);
  101. spin_lock(&tcp_cong_list_lock);
  102. ca = tcp_ca_find(name);
  103. }
  104. #endif
  105. if (ca) {
  106. list_move(&ca->list, &tcp_cong_list);
  107. ret = 0;
  108. }
  109. spin_unlock(&tcp_cong_list_lock);
  110. return ret;
  111. }
  112. /* Get current default congestion control */
  113. void tcp_get_default_congestion_control(char *name)
  114. {
  115. struct tcp_congestion_ops *ca;
  116. /* We will always have reno... */
  117. BUG_ON(list_empty(&tcp_cong_list));
  118. rcu_read_lock();
  119. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  120. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  121. rcu_read_unlock();
  122. }
  123. /* Change congestion control for socket */
  124. int tcp_set_congestion_control(struct sock *sk, const char *name)
  125. {
  126. struct inet_connection_sock *icsk = inet_csk(sk);
  127. struct tcp_congestion_ops *ca;
  128. int err = 0;
  129. rcu_read_lock();
  130. ca = tcp_ca_find(name);
  131. if (ca == icsk->icsk_ca_ops)
  132. goto out;
  133. if (!ca)
  134. err = -ENOENT;
  135. else if (!try_module_get(ca->owner))
  136. err = -EBUSY;
  137. else {
  138. tcp_cleanup_congestion_control(sk);
  139. icsk->icsk_ca_ops = ca;
  140. if (icsk->icsk_ca_ops->init)
  141. icsk->icsk_ca_ops->init(sk);
  142. }
  143. out:
  144. rcu_read_unlock();
  145. return err;
  146. }
  147. /*
  148. * Linear increase during slow start
  149. */
  150. void tcp_slow_start(struct tcp_sock *tp)
  151. {
  152. if (sysctl_tcp_abc) {
  153. /* RFC3465: Slow Start
  154. * TCP sender SHOULD increase cwnd by the number of
  155. * previously unacknowledged bytes ACKed by each incoming
  156. * acknowledgment, provided the increase is not more than L
  157. */
  158. if (tp->bytes_acked < tp->mss_cache)
  159. return;
  160. /* We MAY increase by 2 if discovered delayed ack */
  161. if (sysctl_tcp_abc > 1 && tp->bytes_acked > 2*tp->mss_cache) {
  162. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  163. tp->snd_cwnd++;
  164. }
  165. }
  166. tp->bytes_acked = 0;
  167. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  168. tp->snd_cwnd++;
  169. }
  170. EXPORT_SYMBOL_GPL(tcp_slow_start);
  171. /*
  172. * TCP Reno congestion control
  173. * This is special case used for fallback as well.
  174. */
  175. /* This is Jacobson's slow start and congestion avoidance.
  176. * SIGCOMM '88, p. 328.
  177. */
  178. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 rtt, u32 in_flight,
  179. int flag)
  180. {
  181. struct tcp_sock *tp = tcp_sk(sk);
  182. if (!tcp_is_cwnd_limited(sk, in_flight))
  183. return;
  184. /* In "safe" area, increase. */
  185. if (tp->snd_cwnd <= tp->snd_ssthresh)
  186. tcp_slow_start(tp);
  187. /* In dangerous area, increase slowly. */
  188. else if (sysctl_tcp_abc) {
  189. /* RFC3465: Apppriate Byte Count
  190. * increase once for each full cwnd acked
  191. */
  192. if (tp->bytes_acked >= tp->snd_cwnd*tp->mss_cache) {
  193. tp->bytes_acked -= tp->snd_cwnd*tp->mss_cache;
  194. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  195. tp->snd_cwnd++;
  196. }
  197. } else {
  198. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd */
  199. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  200. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  201. tp->snd_cwnd++;
  202. tp->snd_cwnd_cnt = 0;
  203. } else
  204. tp->snd_cwnd_cnt++;
  205. }
  206. }
  207. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  208. /* Slow start threshold is half the congestion window (min 2) */
  209. u32 tcp_reno_ssthresh(struct sock *sk)
  210. {
  211. const struct tcp_sock *tp = tcp_sk(sk);
  212. return max(tp->snd_cwnd >> 1U, 2U);
  213. }
  214. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  215. /* Lower bound on congestion window. */
  216. u32 tcp_reno_min_cwnd(struct sock *sk)
  217. {
  218. const struct tcp_sock *tp = tcp_sk(sk);
  219. return tp->snd_ssthresh/2;
  220. }
  221. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  222. struct tcp_congestion_ops tcp_reno = {
  223. .name = "reno",
  224. .owner = THIS_MODULE,
  225. .ssthresh = tcp_reno_ssthresh,
  226. .cong_avoid = tcp_reno_cong_avoid,
  227. .min_cwnd = tcp_reno_min_cwnd,
  228. };
  229. /* Initial congestion control used (until SYN)
  230. * really reno under another name so we can tell difference
  231. * during tcp_set_default_congestion_control
  232. */
  233. struct tcp_congestion_ops tcp_init_congestion_ops = {
  234. .name = "",
  235. .owner = THIS_MODULE,
  236. .ssthresh = tcp_reno_ssthresh,
  237. .cong_avoid = tcp_reno_cong_avoid,
  238. .min_cwnd = tcp_reno_min_cwnd,
  239. };
  240. EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);