tcp_cong.c 8.8 KB

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