tcp_cong.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Plugable TCP congestion control support and newReno
  3. * congestion control.
  4. * Based on ideas from I/O scheduler support and Web100.
  5. *
  6. * Copyright (C) 2005 Stephen Hemminger <shemminger@osdl.org>
  7. */
  8. #define pr_fmt(fmt) "TCP: " fmt
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/types.h>
  12. #include <linux/list.h>
  13. #include <linux/gfp.h>
  14. #include <net/tcp.h>
  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. pr_err("%s does not implement required ops\n", ca->name);
  37. return -EINVAL;
  38. }
  39. spin_lock(&tcp_cong_list_lock);
  40. if (tcp_ca_find(ca->name)) {
  41. pr_notice("%s already registered\n", ca->name);
  42. ret = -EEXIST;
  43. } else {
  44. list_add_tail_rcu(&ca->list, &tcp_cong_list);
  45. pr_info("%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_MODULES
  100. if (!ca && capable(CAP_NET_ADMIN)) {
  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 *saved_clone, *clone, *name;
  166. int ret = 0;
  167. saved_clone = 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. kfree(saved_clone);
  192. return ret;
  193. }
  194. /* Change congestion control for socket */
  195. int tcp_set_congestion_control(struct sock *sk, const char *name)
  196. {
  197. struct inet_connection_sock *icsk = inet_csk(sk);
  198. struct tcp_congestion_ops *ca;
  199. int err = 0;
  200. rcu_read_lock();
  201. ca = tcp_ca_find(name);
  202. /* no change asking for existing value */
  203. if (ca == icsk->icsk_ca_ops)
  204. goto out;
  205. #ifdef CONFIG_MODULES
  206. /* not found attempt to autoload module */
  207. if (!ca && capable(CAP_NET_ADMIN)) {
  208. rcu_read_unlock();
  209. request_module("tcp_%s", name);
  210. rcu_read_lock();
  211. ca = tcp_ca_find(name);
  212. }
  213. #endif
  214. if (!ca)
  215. err = -ENOENT;
  216. else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) ||
  217. ns_capable(sock_net(sk)->user_ns, 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. bool 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 true;
  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. left < sk->sk_gso_max_segs)
  245. return true;
  246. return left <= tcp_max_tso_deferred_mss(tp);
  247. }
  248. EXPORT_SYMBOL_GPL(tcp_is_cwnd_limited);
  249. /* Slow start is used when congestion window is no greater than the slow start
  250. * threshold. We base on RFC2581 and also handle stretch ACKs properly.
  251. * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
  252. * something better;) a packet is only considered (s)acked in its entirety to
  253. * defend the ACK attacks described in the RFC. Slow start processes a stretch
  254. * ACK of degree N as if N acks of degree 1 are received back to back except
  255. * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
  256. * returns the leftover acks to adjust cwnd in congestion avoidance mode.
  257. */
  258. int tcp_slow_start(struct tcp_sock *tp, u32 acked)
  259. {
  260. u32 cwnd = tp->snd_cwnd + acked;
  261. if (cwnd > tp->snd_ssthresh)
  262. cwnd = tp->snd_ssthresh + 1;
  263. acked -= cwnd - tp->snd_cwnd;
  264. tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
  265. return acked;
  266. }
  267. EXPORT_SYMBOL_GPL(tcp_slow_start);
  268. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w) */
  269. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w)
  270. {
  271. if (tp->snd_cwnd_cnt >= w) {
  272. if (tp->snd_cwnd < tp->snd_cwnd_clamp)
  273. tp->snd_cwnd++;
  274. tp->snd_cwnd_cnt = 0;
  275. } else {
  276. tp->snd_cwnd_cnt++;
  277. }
  278. }
  279. EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
  280. /*
  281. * TCP Reno congestion control
  282. * This is special case used for fallback as well.
  283. */
  284. /* This is Jacobson's slow start and congestion avoidance.
  285. * SIGCOMM '88, p. 328.
  286. */
  287. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked, u32 in_flight)
  288. {
  289. struct tcp_sock *tp = tcp_sk(sk);
  290. if (!tcp_is_cwnd_limited(sk, in_flight))
  291. return;
  292. /* In "safe" area, increase. */
  293. if (tp->snd_cwnd <= tp->snd_ssthresh)
  294. tcp_slow_start(tp, acked);
  295. /* In dangerous area, increase slowly. */
  296. else
  297. tcp_cong_avoid_ai(tp, tp->snd_cwnd);
  298. }
  299. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  300. /* Slow start threshold is half the congestion window (min 2) */
  301. u32 tcp_reno_ssthresh(struct sock *sk)
  302. {
  303. const struct tcp_sock *tp = tcp_sk(sk);
  304. return max(tp->snd_cwnd >> 1U, 2U);
  305. }
  306. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  307. /* Lower bound on congestion window with halving. */
  308. u32 tcp_reno_min_cwnd(const struct sock *sk)
  309. {
  310. const struct tcp_sock *tp = tcp_sk(sk);
  311. return tp->snd_ssthresh/2;
  312. }
  313. EXPORT_SYMBOL_GPL(tcp_reno_min_cwnd);
  314. struct tcp_congestion_ops tcp_reno = {
  315. .flags = TCP_CONG_NON_RESTRICTED,
  316. .name = "reno",
  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. /* Initial congestion control used (until SYN)
  323. * really reno under another name so we can tell difference
  324. * during tcp_set_default_congestion_control
  325. */
  326. struct tcp_congestion_ops tcp_init_congestion_ops = {
  327. .name = "",
  328. .owner = THIS_MODULE,
  329. .ssthresh = tcp_reno_ssthresh,
  330. .cong_avoid = tcp_reno_cong_avoid,
  331. .min_cwnd = tcp_reno_min_cwnd,
  332. };
  333. EXPORT_SYMBOL_GPL(tcp_init_congestion_ops);