tcp_cong.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. * TCP Reno congestion control
  149. * This is special case used for fallback as well.
  150. */
  151. /* This is Jacobson's slow start and congestion avoidance.
  152. * SIGCOMM '88, p. 328.
  153. */
  154. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 rtt, u32 in_flight,
  155. int flag)
  156. {
  157. struct tcp_sock *tp = tcp_sk(sk);
  158. if (in_flight < tp->snd_cwnd)
  159. return;
  160. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  161. /* In "safe" area, increase. */
  162. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  163. tp->snd_cwnd++;
  164. } else {
  165. /* In dangerous area, increase slowly.
  166. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  167. */
  168. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  169. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  170. tp->snd_cwnd++;
  171. tp->snd_cwnd_cnt = 0;
  172. } else
  173. tp->snd_cwnd_cnt++;
  174. }
  175. }
  176. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  177. /* Slow start threshold is half the congestion window (min 2) */
  178. u32 tcp_reno_ssthresh(struct sock *sk)
  179. {
  180. const struct tcp_sock *tp = tcp_sk(sk);
  181. return max(tp->snd_cwnd >> 1U, 2U);
  182. }
  183. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  184. /* Lower bound on congestion window. */
  185. u32 tcp_reno_min_cwnd(struct sock *sk)
  186. {
  187. const struct tcp_sock *tp = tcp_sk(sk);
  188. return tp->snd_ssthresh/2;
  189. }
  190. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  191. struct tcp_congestion_ops tcp_reno = {
  192. .name = "reno",
  193. .owner = THIS_MODULE,
  194. .ssthresh = tcp_reno_ssthresh,
  195. .cong_avoid = tcp_reno_cong_avoid,
  196. .min_cwnd = tcp_reno_min_cwnd,
  197. };
  198. /* Initial congestion control used (until SYN)
  199. * really reno under another name so we can tell difference
  200. * during tcp_set_default_congestion_control
  201. */
  202. struct tcp_congestion_ops tcp_init_congestion_ops = {
  203. .name = "",
  204. .owner = THIS_MODULE,
  205. .ssthresh = tcp_reno_ssthresh,
  206. .cong_avoid = tcp_reno_cong_avoid,
  207. .min_cwnd = tcp_reno_min_cwnd,
  208. };
  209. EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);