tcp_cong.c 6.6 KB

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