tcp_cong.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(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. rcu_read_lock();
  69. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  70. if (try_module_get(ca->owner)) {
  71. tp->ca_ops = ca;
  72. break;
  73. }
  74. }
  75. rcu_read_unlock();
  76. if (tp->ca_ops->init)
  77. tp->ca_ops->init(tp);
  78. }
  79. /* Manage refcounts on socket close. */
  80. void tcp_cleanup_congestion_control(struct tcp_sock *tp)
  81. {
  82. if (tp->ca_ops->release)
  83. tp->ca_ops->release(tp);
  84. module_put(tp->ca_ops->owner);
  85. }
  86. /* Used by sysctl to change default congestion control */
  87. int tcp_set_default_congestion_control(const char *name)
  88. {
  89. struct tcp_congestion_ops *ca;
  90. int ret = -ENOENT;
  91. spin_lock(&tcp_cong_list_lock);
  92. ca = tcp_ca_find(name);
  93. #ifdef CONFIG_KMOD
  94. if (!ca) {
  95. spin_unlock(&tcp_cong_list_lock);
  96. request_module("tcp_%s", name);
  97. spin_lock(&tcp_cong_list_lock);
  98. ca = tcp_ca_find(name);
  99. }
  100. #endif
  101. if (ca) {
  102. list_move(&ca->list, &tcp_cong_list);
  103. ret = 0;
  104. }
  105. spin_unlock(&tcp_cong_list_lock);
  106. return ret;
  107. }
  108. /* Get current default congestion control */
  109. void tcp_get_default_congestion_control(char *name)
  110. {
  111. struct tcp_congestion_ops *ca;
  112. /* We will always have reno... */
  113. BUG_ON(list_empty(&tcp_cong_list));
  114. rcu_read_lock();
  115. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  116. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  117. rcu_read_unlock();
  118. }
  119. /*
  120. * TCP Reno congestion control
  121. * This is special case used for fallback as well.
  122. */
  123. /* This is Jacobson's slow start and congestion avoidance.
  124. * SIGCOMM '88, p. 328.
  125. */
  126. void tcp_reno_cong_avoid(struct tcp_sock *tp, u32 ack, u32 rtt, u32 in_flight,
  127. int flag)
  128. {
  129. if (in_flight < tp->snd_cwnd)
  130. return;
  131. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  132. /* In "safe" area, increase. */
  133. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  134. tp->snd_cwnd++;
  135. } else {
  136. /* In dangerous area, increase slowly.
  137. * In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd
  138. */
  139. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  140. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  141. tp->snd_cwnd++;
  142. tp->snd_cwnd_cnt = 0;
  143. } else
  144. tp->snd_cwnd_cnt++;
  145. }
  146. }
  147. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  148. /* Slow start threshold is half the congestion window (min 2) */
  149. u32 tcp_reno_ssthresh(struct tcp_sock *tp)
  150. {
  151. return max(tp->snd_cwnd >> 1U, 2U);
  152. }
  153. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  154. /* Lower bound on congestion window. */
  155. u32 tcp_reno_min_cwnd(struct tcp_sock *tp)
  156. {
  157. return tp->snd_ssthresh/2;
  158. }
  159. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  160. struct tcp_congestion_ops tcp_reno = {
  161. .name = "reno",
  162. .owner = THIS_MODULE,
  163. .ssthresh = tcp_reno_ssthresh,
  164. .cong_avoid = tcp_reno_cong_avoid,
  165. .min_cwnd = tcp_reno_min_cwnd,
  166. };
  167. EXPORT_SYMBOL_GPL(tcp_reno);