tcp_cong.c 9.6 KB

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