tcp_cong.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 <linux/gfp.h>
  13. #include <net/tcp.h>
  14. int sysctl_tcp_max_ssthresh = 0;
  15. static DEFINE_SPINLOCK(tcp_cong_list_lock);
  16. static LIST_HEAD(tcp_cong_list);
  17. /* Simple linear search, don't expect many entries! */
  18. static struct tcp_congestion_ops *tcp_ca_find(const char *name)
  19. {
  20. struct tcp_congestion_ops *e;
  21. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  22. if (strcmp(e->name, name) == 0)
  23. return e;
  24. }
  25. return NULL;
  26. }
  27. /*
  28. * Attach new congestion control algorithm to the list
  29. * of available options.
  30. */
  31. int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
  32. {
  33. int ret = 0;
  34. /* all algorithms must implement ssthresh and cong_avoid ops */
  35. if (!ca->ssthresh || !ca->cong_avoid) {
  36. printk(KERN_ERR "TCP %s does not implement required ops\n",
  37. ca->name);
  38. return -EINVAL;
  39. }
  40. spin_lock(&tcp_cong_list_lock);
  41. if (tcp_ca_find(ca->name)) {
  42. printk(KERN_NOTICE "TCP %s already registered\n", ca->name);
  43. ret = -EEXIST;
  44. } else {
  45. list_add_tail_rcu(&ca->list, &tcp_cong_list);
  46. printk(KERN_INFO "TCP %s registered\n", ca->name);
  47. }
  48. spin_unlock(&tcp_cong_list_lock);
  49. return ret;
  50. }
  51. EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
  52. /*
  53. * Remove congestion control algorithm, called from
  54. * the module's remove function. Module ref counts are used
  55. * to ensure that this can't be done till all sockets using
  56. * that method are closed.
  57. */
  58. void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
  59. {
  60. spin_lock(&tcp_cong_list_lock);
  61. list_del_rcu(&ca->list);
  62. spin_unlock(&tcp_cong_list_lock);
  63. }
  64. EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
  65. /* Assign choice of congestion control. */
  66. void tcp_init_congestion_control(struct sock *sk)
  67. {
  68. struct inet_connection_sock *icsk = inet_csk(sk);
  69. struct tcp_congestion_ops *ca;
  70. /* if no choice made yet assign the current value set as default */
  71. if (icsk->icsk_ca_ops == &tcp_init_congestion_ops) {
  72. rcu_read_lock();
  73. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  74. if (try_module_get(ca->owner)) {
  75. icsk->icsk_ca_ops = ca;
  76. break;
  77. }
  78. /* fallback to next available */
  79. }
  80. rcu_read_unlock();
  81. }
  82. if (icsk->icsk_ca_ops->init)
  83. icsk->icsk_ca_ops->init(sk);
  84. }
  85. /* Manage refcounts on socket close. */
  86. void tcp_cleanup_congestion_control(struct sock *sk)
  87. {
  88. struct inet_connection_sock *icsk = inet_csk(sk);
  89. if (icsk->icsk_ca_ops->release)
  90. icsk->icsk_ca_ops->release(sk);
  91. module_put(icsk->icsk_ca_ops->owner);
  92. }
  93. /* Used by sysctl to change default congestion control */
  94. int tcp_set_default_congestion_control(const char *name)
  95. {
  96. struct tcp_congestion_ops *ca;
  97. int ret = -ENOENT;
  98. spin_lock(&tcp_cong_list_lock);
  99. ca = tcp_ca_find(name);
  100. #ifdef CONFIG_MODULES
  101. if (!ca && capable(CAP_NET_ADMIN)) {
  102. spin_unlock(&tcp_cong_list_lock);
  103. request_module("tcp_%s", name);
  104. spin_lock(&tcp_cong_list_lock);
  105. ca = tcp_ca_find(name);
  106. }
  107. #endif
  108. if (ca) {
  109. ca->flags |= TCP_CONG_NON_RESTRICTED; /* default is always allowed */
  110. list_move(&ca->list, &tcp_cong_list);
  111. ret = 0;
  112. }
  113. spin_unlock(&tcp_cong_list_lock);
  114. return ret;
  115. }
  116. /* Set default value from kernel configuration at bootup */
  117. static int __init tcp_congestion_default(void)
  118. {
  119. return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
  120. }
  121. late_initcall(tcp_congestion_default);
  122. /* Build string with list of available congestion control values */
  123. void tcp_get_available_congestion_control(char *buf, size_t maxlen)
  124. {
  125. struct tcp_congestion_ops *ca;
  126. size_t offs = 0;
  127. rcu_read_lock();
  128. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  129. offs += snprintf(buf + offs, maxlen - offs,
  130. "%s%s",
  131. offs == 0 ? "" : " ", ca->name);
  132. }
  133. rcu_read_unlock();
  134. }
  135. /* Get current default congestion control */
  136. void tcp_get_default_congestion_control(char *name)
  137. {
  138. struct tcp_congestion_ops *ca;
  139. /* We will always have reno... */
  140. BUG_ON(list_empty(&tcp_cong_list));
  141. rcu_read_lock();
  142. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  143. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  144. rcu_read_unlock();
  145. }
  146. /* Built list of non-restricted congestion control values */
  147. void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
  148. {
  149. struct tcp_congestion_ops *ca;
  150. size_t offs = 0;
  151. *buf = '\0';
  152. rcu_read_lock();
  153. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  154. if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
  155. continue;
  156. offs += snprintf(buf + offs, maxlen - offs,
  157. "%s%s",
  158. offs == 0 ? "" : " ", ca->name);
  159. }
  160. rcu_read_unlock();
  161. }
  162. /* Change list of non-restricted congestion control */
  163. int tcp_set_allowed_congestion_control(char *val)
  164. {
  165. struct tcp_congestion_ops *ca;
  166. char *saved_clone, *clone, *name;
  167. int ret = 0;
  168. saved_clone = clone = kstrdup(val, GFP_USER);
  169. if (!clone)
  170. return -ENOMEM;
  171. spin_lock(&tcp_cong_list_lock);
  172. /* pass 1 check for bad entries */
  173. while ((name = strsep(&clone, " ")) && *name) {
  174. ca = tcp_ca_find(name);
  175. if (!ca) {
  176. ret = -ENOENT;
  177. goto out;
  178. }
  179. }
  180. /* pass 2 clear old values */
  181. list_for_each_entry_rcu(ca, &tcp_cong_list, list)
  182. ca->flags &= ~TCP_CONG_NON_RESTRICTED;
  183. /* pass 3 mark as allowed */
  184. while ((name = strsep(&val, " ")) && *name) {
  185. ca = tcp_ca_find(name);
  186. WARN_ON(!ca);
  187. if (ca)
  188. ca->flags |= TCP_CONG_NON_RESTRICTED;
  189. }
  190. out:
  191. spin_unlock(&tcp_cong_list_lock);
  192. kfree(saved_clone);
  193. return ret;
  194. }
  195. /* Change congestion control for socket */
  196. int tcp_set_congestion_control(struct sock *sk, const char *name)
  197. {
  198. struct inet_connection_sock *icsk = inet_csk(sk);
  199. struct tcp_congestion_ops *ca;
  200. int err = 0;
  201. rcu_read_lock();
  202. ca = tcp_ca_find(name);
  203. /* no change asking for existing value */
  204. if (ca == icsk->icsk_ca_ops)
  205. goto out;
  206. #ifdef CONFIG_MODULES
  207. /* not found attempt to autoload module */
  208. if (!ca && capable(CAP_NET_ADMIN)) {
  209. rcu_read_unlock();
  210. request_module("tcp_%s", name);
  211. rcu_read_lock();
  212. ca = tcp_ca_find(name);
  213. }
  214. #endif
  215. if (!ca)
  216. err = -ENOENT;
  217. else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) || capable(CAP_NET_ADMIN)))
  218. err = -EPERM;
  219. else if (!try_module_get(ca->owner))
  220. err = -EBUSY;
  221. else {
  222. tcp_cleanup_congestion_control(sk);
  223. icsk->icsk_ca_ops = ca;
  224. if (sk->sk_state != TCP_CLOSE && icsk->icsk_ca_ops->init)
  225. icsk->icsk_ca_ops->init(sk);
  226. }
  227. out:
  228. rcu_read_unlock();
  229. return err;
  230. }
  231. /* RFC2861 Check whether we are limited by application or congestion window
  232. * This is the inverse of cwnd check in tcp_tso_should_defer
  233. */
  234. int tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight)
  235. {
  236. const struct tcp_sock *tp = tcp_sk(sk);
  237. u32 left;
  238. if (in_flight >= tp->snd_cwnd)
  239. return 1;
  240. left = tp->snd_cwnd - in_flight;
  241. if (sk_can_gso(sk) &&
  242. left * sysctl_tcp_tso_win_divisor < tp->snd_cwnd &&
  243. left * tp->mss_cache < sk->sk_gso_max_size)
  244. return 1;
  245. return left <= tcp_max_burst(tp);
  246. }
  247. EXPORT_SYMBOL_GPL(tcp_is_cwnd_limited);
  248. /*
  249. * Slow start is used when congestion window is less than slow start
  250. * threshold. This version implements the basic RFC2581 version
  251. * and optionally supports:
  252. * RFC3742 Limited Slow Start - growth limited to max_ssthresh
  253. * RFC3465 Appropriate Byte Counting - growth limited by bytes acknowledged
  254. */
  255. void tcp_slow_start(struct tcp_sock *tp)
  256. {
  257. int cnt; /* increase in packets */
  258. /* RFC3465: ABC Slow start
  259. * Increase only after a full MSS of bytes is acked
  260. *
  261. * TCP sender SHOULD increase cwnd by the number of
  262. * previously unacknowledged bytes ACKed by each incoming
  263. * acknowledgment, provided the increase is not more than L
  264. */
  265. if (sysctl_tcp_abc && tp->bytes_acked < tp->mss_cache)
  266. return;
  267. if (sysctl_tcp_max_ssthresh > 0 && tp->snd_cwnd > sysctl_tcp_max_ssthresh)
  268. cnt = sysctl_tcp_max_ssthresh >> 1; /* limited slow start */
  269. else
  270. cnt = tp->snd_cwnd; /* exponential increase */
  271. /* RFC3465: ABC
  272. * We MAY increase by 2 if discovered delayed ack
  273. */
  274. if (sysctl_tcp_abc > 1 && tp->bytes_acked >= 2*tp->mss_cache)
  275. cnt <<= 1;
  276. tp->bytes_acked = 0;
  277. tp->snd_cwnd_cnt += cnt;
  278. while (tp->snd_cwnd_cnt >= tp->snd_cwnd) {
  279. tp->snd_cwnd_cnt -= tp->snd_cwnd;
  280. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  281. tp->snd_cwnd++;
  282. }
  283. }
  284. EXPORT_SYMBOL_GPL(tcp_slow_start);
  285. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w) */
  286. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w)
  287. {
  288. if (tp->snd_cwnd_cnt >= w) {
  289. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  290. tp->snd_cwnd++;
  291. tp->snd_cwnd_cnt = 0;
  292. } else {
  293. tp->snd_cwnd_cnt++;
  294. }
  295. }
  296. EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
  297. /*
  298. * TCP Reno congestion control
  299. * This is special case used for fallback as well.
  300. */
  301. /* This is Jacobson's slow start and congestion avoidance.
  302. * SIGCOMM '88, p. 328.
  303. */
  304. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
  305. {
  306. struct tcp_sock *tp = tcp_sk(sk);
  307. if (!tcp_is_cwnd_limited(sk, in_flight))
  308. return;
  309. /* In "safe" area, increase. */
  310. if (tp->snd_cwnd <= tp->snd_ssthresh)
  311. tcp_slow_start(tp);
  312. /* In dangerous area, increase slowly. */
  313. else if (sysctl_tcp_abc) {
  314. /* RFC3465: Appropriate Byte Count
  315. * increase once for each full cwnd acked
  316. */
  317. if (tp->bytes_acked >= tp->snd_cwnd*tp->mss_cache) {
  318. tp->bytes_acked -= tp->snd_cwnd*tp->mss_cache;
  319. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  320. tp->snd_cwnd++;
  321. }
  322. } else {
  323. tcp_cong_avoid_ai(tp, tp->snd_cwnd);
  324. }
  325. }
  326. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  327. /* Slow start threshold is half the congestion window (min 2) */
  328. u32 tcp_reno_ssthresh(struct sock *sk)
  329. {
  330. const struct tcp_sock *tp = tcp_sk(sk);
  331. return max(tp->snd_cwnd >> 1U, 2U);
  332. }
  333. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  334. /* Lower bound on congestion window with halving. */
  335. u32 tcp_reno_min_cwnd(const struct sock *sk)
  336. {
  337. const struct tcp_sock *tp = tcp_sk(sk);
  338. return tp->snd_ssthresh/2;
  339. }
  340. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  341. struct tcp_congestion_ops tcp_reno = {
  342. .flags = TCP_CONG_NON_RESTRICTED,
  343. .name = "reno",
  344. .owner = THIS_MODULE,
  345. .ssthresh = tcp_reno_ssthresh,
  346. .cong_avoid = tcp_reno_cong_avoid,
  347. .min_cwnd = tcp_reno_min_cwnd,
  348. };
  349. /* Initial congestion control used (until SYN)
  350. * really reno under another name so we can tell difference
  351. * during tcp_set_default_congestion_control
  352. */
  353. struct tcp_congestion_ops tcp_init_congestion_ops = {
  354. .name = "",
  355. .owner = THIS_MODULE,
  356. .ssthresh = tcp_reno_ssthresh,
  357. .cong_avoid = tcp_reno_cong_avoid,
  358. .min_cwnd = tcp_reno_min_cwnd,
  359. };
  360. EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);