tcp_cong.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 tcp_sock *tp)
  66. {
  67. struct tcp_congestion_ops *ca;
  68. if (tp->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. tp->ca_ops = ca;
  74. break;
  75. }
  76. }
  77. rcu_read_unlock();
  78. if (tp->ca_ops->init)
  79. tp->ca_ops->init(tp);
  80. }
  81. /* Manage refcounts on socket close. */
  82. void tcp_cleanup_congestion_control(struct tcp_sock *tp)
  83. {
  84. if (tp->ca_ops->release)
  85. tp->ca_ops->release(tp);
  86. module_put(tp->ca_ops->owner);
  87. }
  88. /* Used by sysctl to change default congestion control */
  89. int tcp_set_default_congestion_control(const char *name)
  90. {
  91. struct tcp_congestion_ops *ca;
  92. int ret = -ENOENT;
  93. spin_lock(&tcp_cong_list_lock);
  94. ca = tcp_ca_find(name);
  95. #ifdef CONFIG_KMOD
  96. if (!ca) {
  97. spin_unlock(&tcp_cong_list_lock);
  98. request_module("tcp_%s", name);
  99. spin_lock(&tcp_cong_list_lock);
  100. ca = tcp_ca_find(name);
  101. }
  102. #endif
  103. if (ca) {
  104. list_move(&ca->list, &tcp_cong_list);
  105. ret = 0;
  106. }
  107. spin_unlock(&tcp_cong_list_lock);
  108. return ret;
  109. }
  110. /* Get current default congestion control */
  111. void tcp_get_default_congestion_control(char *name)
  112. {
  113. struct tcp_congestion_ops *ca;
  114. /* We will always have reno... */
  115. BUG_ON(list_empty(&tcp_cong_list));
  116. rcu_read_lock();
  117. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  118. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  119. rcu_read_unlock();
  120. }
  121. /* Change congestion control for socket */
  122. int tcp_set_congestion_control(struct tcp_sock *tp, const char *name)
  123. {
  124. struct tcp_congestion_ops *ca;
  125. int err = 0;
  126. rcu_read_lock();
  127. ca = tcp_ca_find(name);
  128. if (ca == tp->ca_ops)
  129. goto out;
  130. if (!ca)
  131. err = -ENOENT;
  132. else if (!try_module_get(ca->owner))
  133. err = -EBUSY;
  134. else {
  135. tcp_cleanup_congestion_control(tp);
  136. tp->ca_ops = ca;
  137. if (tp->ca_ops->init)
  138. tp->ca_ops->init(tp);
  139. }
  140. out:
  141. rcu_read_unlock();
  142. return err;
  143. }
  144. /*
  145. * TCP Reno congestion control
  146. * This is special case used for fallback as well.
  147. */
  148. /* This is Jacobson's slow start and congestion avoidance.
  149. * SIGCOMM '88, p. 328.
  150. */
  151. void tcp_reno_cong_avoid(struct tcp_sock *tp, u32 ack, u32 rtt, u32 in_flight,
  152. int flag)
  153. {
  154. if (in_flight < tp->snd_cwnd)
  155. return;
  156. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  157. /* In "safe" area, increase. */
  158. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  159. tp->snd_cwnd++;
  160. } else {
  161. /* In dangerous area, increase slowly.
  162. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  163. */
  164. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  165. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  166. tp->snd_cwnd++;
  167. tp->snd_cwnd_cnt = 0;
  168. } else
  169. tp->snd_cwnd_cnt++;
  170. }
  171. }
  172. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  173. /* Slow start threshold is half the congestion window (min 2) */
  174. u32 tcp_reno_ssthresh(struct tcp_sock *tp)
  175. {
  176. return max(tp->snd_cwnd >> 1U, 2U);
  177. }
  178. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  179. /* Lower bound on congestion window. */
  180. u32 tcp_reno_min_cwnd(struct tcp_sock *tp)
  181. {
  182. return tp->snd_ssthresh/2;
  183. }
  184. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  185. struct tcp_congestion_ops tcp_reno = {
  186. .name = "reno",
  187. .owner = THIS_MODULE,
  188. .ssthresh = tcp_reno_ssthresh,
  189. .cong_avoid = tcp_reno_cong_avoid,
  190. .min_cwnd = tcp_reno_min_cwnd,
  191. };
  192. /* Initial congestion control used (until SYN)
  193. * really reno under another name so we can tell difference
  194. * during tcp_set_default_congestion_control
  195. */
  196. struct tcp_congestion_ops tcp_init_congestion_ops = {
  197. .name = "",
  198. .owner = THIS_MODULE,
  199. .ssthresh = tcp_reno_ssthresh,
  200. .cong_avoid = tcp_reno_cong_avoid,
  201. .min_cwnd = tcp_reno_min_cwnd,
  202. };
  203. EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);