tcp_cong.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 algorithm 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 no choice made yet assign the current value set as default */
  69. if (icsk->icsk_ca_ops == &tcp_init_congestion_ops) {
  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. /* fallback to next available */
  77. }
  78. rcu_read_unlock();
  79. }
  80. if (icsk->icsk_ca_ops->init)
  81. icsk->icsk_ca_ops->init(sk);
  82. }
  83. /* Manage refcounts on socket close. */
  84. void tcp_cleanup_congestion_control(struct sock *sk)
  85. {
  86. struct inet_connection_sock *icsk = inet_csk(sk);
  87. if (icsk->icsk_ca_ops->release)
  88. icsk->icsk_ca_ops->release(sk);
  89. module_put(icsk->icsk_ca_ops->owner);
  90. }
  91. /* Used by sysctl to change default congestion control */
  92. int tcp_set_default_congestion_control(const char *name)
  93. {
  94. struct tcp_congestion_ops *ca;
  95. int ret = -ENOENT;
  96. spin_lock(&tcp_cong_list_lock);
  97. ca = tcp_ca_find(name);
  98. #ifdef CONFIG_KMOD
  99. if (!ca && capable(CAP_SYS_MODULE)) {
  100. spin_unlock(&tcp_cong_list_lock);
  101. request_module("tcp_%s", name);
  102. spin_lock(&tcp_cong_list_lock);
  103. ca = tcp_ca_find(name);
  104. }
  105. #endif
  106. if (ca) {
  107. ca->non_restricted = 1; /* default is always allowed */
  108. list_move(&ca->list, &tcp_cong_list);
  109. ret = 0;
  110. }
  111. spin_unlock(&tcp_cong_list_lock);
  112. return ret;
  113. }
  114. /* Set default value from kernel configuration at bootup */
  115. static int __init tcp_congestion_default(void)
  116. {
  117. return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
  118. }
  119. late_initcall(tcp_congestion_default);
  120. /* Build string with list of available congestion control values */
  121. void tcp_get_available_congestion_control(char *buf, size_t maxlen)
  122. {
  123. struct tcp_congestion_ops *ca;
  124. size_t offs = 0;
  125. rcu_read_lock();
  126. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  127. offs += snprintf(buf + offs, maxlen - offs,
  128. "%s%s",
  129. offs == 0 ? "" : " ", ca->name);
  130. }
  131. rcu_read_unlock();
  132. }
  133. /* Get current default congestion control */
  134. void tcp_get_default_congestion_control(char *name)
  135. {
  136. struct tcp_congestion_ops *ca;
  137. /* We will always have reno... */
  138. BUG_ON(list_empty(&tcp_cong_list));
  139. rcu_read_lock();
  140. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  141. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  142. rcu_read_unlock();
  143. }
  144. /* Built list of non-restricted congestion control values */
  145. void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
  146. {
  147. struct tcp_congestion_ops *ca;
  148. size_t offs = 0;
  149. *buf = '\0';
  150. rcu_read_lock();
  151. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  152. if (!ca->non_restricted)
  153. continue;
  154. offs += snprintf(buf + offs, maxlen - offs,
  155. "%s%s",
  156. offs == 0 ? "" : " ", ca->name);
  157. }
  158. rcu_read_unlock();
  159. }
  160. /* Change list of non-restricted congestion control */
  161. int tcp_set_allowed_congestion_control(char *val)
  162. {
  163. struct tcp_congestion_ops *ca;
  164. char *clone, *name;
  165. int ret = 0;
  166. clone = kstrdup(val, GFP_USER);
  167. if (!clone)
  168. return -ENOMEM;
  169. spin_lock(&tcp_cong_list_lock);
  170. /* pass 1 check for bad entries */
  171. while ((name = strsep(&clone, " ")) && *name) {
  172. ca = tcp_ca_find(name);
  173. if (!ca) {
  174. ret = -ENOENT;
  175. goto out;
  176. }
  177. }
  178. /* pass 2 clear */
  179. list_for_each_entry_rcu(ca, &tcp_cong_list, list)
  180. ca->non_restricted = 0;
  181. /* pass 3 mark as allowed */
  182. while ((name = strsep(&val, " ")) && *name) {
  183. ca = tcp_ca_find(name);
  184. WARN_ON(!ca);
  185. if (ca)
  186. ca->non_restricted = 1;
  187. }
  188. out:
  189. spin_unlock(&tcp_cong_list_lock);
  190. return ret;
  191. }
  192. /* Change congestion control for socket */
  193. int tcp_set_congestion_control(struct sock *sk, const char *name)
  194. {
  195. struct inet_connection_sock *icsk = inet_csk(sk);
  196. struct tcp_congestion_ops *ca;
  197. int err = 0;
  198. rcu_read_lock();
  199. ca = tcp_ca_find(name);
  200. /* no change asking for existing value */
  201. if (ca == icsk->icsk_ca_ops)
  202. goto out;
  203. #ifdef CONFIG_KMOD
  204. /* not found attempt to autoload module */
  205. if (!ca && capable(CAP_SYS_MODULE)) {
  206. rcu_read_unlock();
  207. request_module("tcp_%s", name);
  208. rcu_read_lock();
  209. ca = tcp_ca_find(name);
  210. }
  211. #endif
  212. if (!ca)
  213. err = -ENOENT;
  214. else if (!(ca->non_restricted || capable(CAP_NET_ADMIN)))
  215. err = -EPERM;
  216. else if (!try_module_get(ca->owner))
  217. err = -EBUSY;
  218. else {
  219. tcp_cleanup_congestion_control(sk);
  220. icsk->icsk_ca_ops = ca;
  221. if (sk->sk_state != TCP_CLOSE && icsk->icsk_ca_ops->init)
  222. icsk->icsk_ca_ops->init(sk);
  223. }
  224. out:
  225. rcu_read_unlock();
  226. return err;
  227. }
  228. /*
  229. * Linear increase during slow start
  230. */
  231. void tcp_slow_start(struct tcp_sock *tp)
  232. {
  233. if (sysctl_tcp_abc) {
  234. /* RFC3465: Slow Start
  235. * TCP sender SHOULD increase cwnd by the number of
  236. * previously unacknowledged bytes ACKed by each incoming
  237. * acknowledgment, provided the increase is not more than L
  238. */
  239. if (tp->bytes_acked < tp->mss_cache)
  240. return;
  241. /* We MAY increase by 2 if discovered delayed ack */
  242. if (sysctl_tcp_abc > 1 && tp->bytes_acked >= 2*tp->mss_cache) {
  243. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  244. tp->snd_cwnd++;
  245. }
  246. }
  247. tp->bytes_acked = 0;
  248. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  249. tp->snd_cwnd++;
  250. }
  251. EXPORT_SYMBOL_GPL(tcp_slow_start);
  252. /*
  253. * TCP Reno congestion control
  254. * This is special case used for fallback as well.
  255. */
  256. /* This is Jacobson's slow start and congestion avoidance.
  257. * SIGCOMM '88, p. 328.
  258. */
  259. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 rtt, u32 in_flight,
  260. int flag)
  261. {
  262. struct tcp_sock *tp = tcp_sk(sk);
  263. if (!tcp_is_cwnd_limited(sk, in_flight))
  264. return;
  265. /* In "safe" area, increase. */
  266. if (tp->snd_cwnd <= tp->snd_ssthresh)
  267. tcp_slow_start(tp);
  268. /* In dangerous area, increase slowly. */
  269. else if (sysctl_tcp_abc) {
  270. /* RFC3465: Appropriate Byte Count
  271. * increase once for each full cwnd acked
  272. */
  273. if (tp->bytes_acked >= tp->snd_cwnd*tp->mss_cache) {
  274. tp->bytes_acked -= tp->snd_cwnd*tp->mss_cache;
  275. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  276. tp->snd_cwnd++;
  277. }
  278. } else {
  279. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd */
  280. if (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  281. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  282. tp->snd_cwnd++;
  283. tp->snd_cwnd_cnt = 0;
  284. } else
  285. tp->snd_cwnd_cnt++;
  286. }
  287. }
  288. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  289. /* Slow start threshold is half the congestion window (min 2) */
  290. u32 tcp_reno_ssthresh(struct sock *sk)
  291. {
  292. const struct tcp_sock *tp = tcp_sk(sk);
  293. return max(tp->snd_cwnd >> 1U, 2U);
  294. }
  295. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  296. /* Lower bound on congestion window with halving. */
  297. u32 tcp_reno_min_cwnd(const struct sock *sk)
  298. {
  299. const struct tcp_sock *tp = tcp_sk(sk);
  300. return tp->snd_ssthresh/2;
  301. }
  302. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  303. struct tcp_congestion_ops tcp_reno = {
  304. .name = "reno",
  305. .non_restricted = 1,
  306. .owner = THIS_MODULE,
  307. .ssthresh = tcp_reno_ssthresh,
  308. .cong_avoid = tcp_reno_cong_avoid,
  309. .min_cwnd = tcp_reno_min_cwnd,
  310. };
  311. /* Initial congestion control used (until SYN)
  312. * really reno under another name so we can tell difference
  313. * during tcp_set_default_congestion_control
  314. */
  315. struct tcp_congestion_ops tcp_init_congestion_ops = {
  316. .name = "",
  317. .owner = THIS_MODULE,
  318. .ssthresh = tcp_reno_ssthresh,
  319. .cong_avoid = tcp_reno_cong_avoid,
  320. .min_cwnd = tcp_reno_min_cwnd,
  321. };
  322. EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);