tcp_cong.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Pluggable 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 <linux/jhash.h>
  15. #include <net/tcp.h>
  16. static DEFINE_SPINLOCK(tcp_cong_list_lock);
  17. static LIST_HEAD(tcp_cong_list);
  18. /* Simple linear search, don't expect many entries! */
  19. static struct tcp_congestion_ops *tcp_ca_find(const char *name)
  20. {
  21. struct tcp_congestion_ops *e;
  22. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  23. if (strcmp(e->name, name) == 0)
  24. return e;
  25. }
  26. return NULL;
  27. }
  28. /* Must be called with rcu lock held */
  29. static const struct tcp_congestion_ops *__tcp_ca_find_autoload(const char *name)
  30. {
  31. const struct tcp_congestion_ops *ca = tcp_ca_find(name);
  32. #ifdef CONFIG_MODULES
  33. if (!ca && capable(CAP_NET_ADMIN)) {
  34. rcu_read_unlock();
  35. request_module("tcp_%s", name);
  36. rcu_read_lock();
  37. ca = tcp_ca_find(name);
  38. }
  39. #endif
  40. return ca;
  41. }
  42. /* Simple linear search, not much in here. */
  43. struct tcp_congestion_ops *tcp_ca_find_key(u32 key)
  44. {
  45. struct tcp_congestion_ops *e;
  46. list_for_each_entry_rcu(e, &tcp_cong_list, list) {
  47. if (e->key == key)
  48. return e;
  49. }
  50. return NULL;
  51. }
  52. /*
  53. * Attach new congestion control algorithm to the list
  54. * of available options.
  55. */
  56. int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
  57. {
  58. int ret = 0;
  59. /* all algorithms must implement ssthresh and cong_avoid ops */
  60. if (!ca->ssthresh || !ca->cong_avoid) {
  61. pr_err("%s does not implement required ops\n", ca->name);
  62. return -EINVAL;
  63. }
  64. ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
  65. spin_lock(&tcp_cong_list_lock);
  66. if (ca->key == TCP_CA_UNSPEC || tcp_ca_find_key(ca->key)) {
  67. pr_notice("%s already registered or non-unique key\n",
  68. ca->name);
  69. ret = -EEXIST;
  70. } else {
  71. list_add_tail_rcu(&ca->list, &tcp_cong_list);
  72. pr_info("%s registered\n", ca->name);
  73. }
  74. spin_unlock(&tcp_cong_list_lock);
  75. return ret;
  76. }
  77. EXPORT_SYMBOL_GPL(tcp_register_congestion_control);
  78. /*
  79. * Remove congestion control algorithm, called from
  80. * the module's remove function. Module ref counts are used
  81. * to ensure that this can't be done till all sockets using
  82. * that method are closed.
  83. */
  84. void tcp_unregister_congestion_control(struct tcp_congestion_ops *ca)
  85. {
  86. spin_lock(&tcp_cong_list_lock);
  87. list_del_rcu(&ca->list);
  88. spin_unlock(&tcp_cong_list_lock);
  89. /* Wait for outstanding readers to complete before the
  90. * module gets removed entirely.
  91. *
  92. * A try_module_get() should fail by now as our module is
  93. * in "going" state since no refs are held anymore and
  94. * module_exit() handler being called.
  95. */
  96. synchronize_rcu();
  97. }
  98. EXPORT_SYMBOL_GPL(tcp_unregister_congestion_control);
  99. u32 tcp_ca_get_key_by_name(const char *name)
  100. {
  101. const struct tcp_congestion_ops *ca;
  102. u32 key;
  103. might_sleep();
  104. rcu_read_lock();
  105. ca = __tcp_ca_find_autoload(name);
  106. key = ca ? ca->key : TCP_CA_UNSPEC;
  107. rcu_read_unlock();
  108. return key;
  109. }
  110. EXPORT_SYMBOL_GPL(tcp_ca_get_key_by_name);
  111. char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  112. {
  113. const struct tcp_congestion_ops *ca;
  114. char *ret = NULL;
  115. rcu_read_lock();
  116. ca = tcp_ca_find_key(key);
  117. if (ca)
  118. ret = strncpy(buffer, ca->name,
  119. TCP_CA_NAME_MAX);
  120. rcu_read_unlock();
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(tcp_ca_get_name_by_key);
  124. /* Assign choice of congestion control. */
  125. void tcp_assign_congestion_control(struct sock *sk)
  126. {
  127. struct inet_connection_sock *icsk = inet_csk(sk);
  128. struct tcp_congestion_ops *ca;
  129. rcu_read_lock();
  130. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  131. if (likely(try_module_get(ca->owner))) {
  132. icsk->icsk_ca_ops = ca;
  133. goto out;
  134. }
  135. /* Fallback to next available. The last really
  136. * guaranteed fallback is Reno from this list.
  137. */
  138. }
  139. out:
  140. rcu_read_unlock();
  141. /* Clear out private data before diag gets it and
  142. * the ca has not been initialized.
  143. */
  144. if (ca->get_info)
  145. memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
  146. }
  147. void tcp_init_congestion_control(struct sock *sk)
  148. {
  149. const struct inet_connection_sock *icsk = inet_csk(sk);
  150. if (icsk->icsk_ca_ops->init)
  151. icsk->icsk_ca_ops->init(sk);
  152. }
  153. static void tcp_reinit_congestion_control(struct sock *sk,
  154. const struct tcp_congestion_ops *ca)
  155. {
  156. struct inet_connection_sock *icsk = inet_csk(sk);
  157. tcp_cleanup_congestion_control(sk);
  158. icsk->icsk_ca_ops = ca;
  159. if (sk->sk_state != TCP_CLOSE && icsk->icsk_ca_ops->init)
  160. icsk->icsk_ca_ops->init(sk);
  161. }
  162. /* Manage refcounts on socket close. */
  163. void tcp_cleanup_congestion_control(struct sock *sk)
  164. {
  165. struct inet_connection_sock *icsk = inet_csk(sk);
  166. if (icsk->icsk_ca_ops->release)
  167. icsk->icsk_ca_ops->release(sk);
  168. module_put(icsk->icsk_ca_ops->owner);
  169. }
  170. /* Used by sysctl to change default congestion control */
  171. int tcp_set_default_congestion_control(const char *name)
  172. {
  173. struct tcp_congestion_ops *ca;
  174. int ret = -ENOENT;
  175. spin_lock(&tcp_cong_list_lock);
  176. ca = tcp_ca_find(name);
  177. #ifdef CONFIG_MODULES
  178. if (!ca && capable(CAP_NET_ADMIN)) {
  179. spin_unlock(&tcp_cong_list_lock);
  180. request_module("tcp_%s", name);
  181. spin_lock(&tcp_cong_list_lock);
  182. ca = tcp_ca_find(name);
  183. }
  184. #endif
  185. if (ca) {
  186. ca->flags |= TCP_CONG_NON_RESTRICTED; /* default is always allowed */
  187. list_move(&ca->list, &tcp_cong_list);
  188. ret = 0;
  189. }
  190. spin_unlock(&tcp_cong_list_lock);
  191. return ret;
  192. }
  193. /* Set default value from kernel configuration at bootup */
  194. static int __init tcp_congestion_default(void)
  195. {
  196. return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
  197. }
  198. late_initcall(tcp_congestion_default);
  199. /* Build string with list of available congestion control values */
  200. void tcp_get_available_congestion_control(char *buf, size_t maxlen)
  201. {
  202. struct tcp_congestion_ops *ca;
  203. size_t offs = 0;
  204. rcu_read_lock();
  205. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  206. offs += snprintf(buf + offs, maxlen - offs,
  207. "%s%s",
  208. offs == 0 ? "" : " ", ca->name);
  209. }
  210. rcu_read_unlock();
  211. }
  212. /* Get current default congestion control */
  213. void tcp_get_default_congestion_control(char *name)
  214. {
  215. struct tcp_congestion_ops *ca;
  216. /* We will always have reno... */
  217. BUG_ON(list_empty(&tcp_cong_list));
  218. rcu_read_lock();
  219. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  220. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  221. rcu_read_unlock();
  222. }
  223. /* Built list of non-restricted congestion control values */
  224. void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
  225. {
  226. struct tcp_congestion_ops *ca;
  227. size_t offs = 0;
  228. *buf = '\0';
  229. rcu_read_lock();
  230. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  231. if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
  232. continue;
  233. offs += snprintf(buf + offs, maxlen - offs,
  234. "%s%s",
  235. offs == 0 ? "" : " ", ca->name);
  236. }
  237. rcu_read_unlock();
  238. }
  239. /* Change list of non-restricted congestion control */
  240. int tcp_set_allowed_congestion_control(char *val)
  241. {
  242. struct tcp_congestion_ops *ca;
  243. char *saved_clone, *clone, *name;
  244. int ret = 0;
  245. saved_clone = clone = kstrdup(val, GFP_USER);
  246. if (!clone)
  247. return -ENOMEM;
  248. spin_lock(&tcp_cong_list_lock);
  249. /* pass 1 check for bad entries */
  250. while ((name = strsep(&clone, " ")) && *name) {
  251. ca = tcp_ca_find(name);
  252. if (!ca) {
  253. ret = -ENOENT;
  254. goto out;
  255. }
  256. }
  257. /* pass 2 clear old values */
  258. list_for_each_entry_rcu(ca, &tcp_cong_list, list)
  259. ca->flags &= ~TCP_CONG_NON_RESTRICTED;
  260. /* pass 3 mark as allowed */
  261. while ((name = strsep(&val, " ")) && *name) {
  262. ca = tcp_ca_find(name);
  263. WARN_ON(!ca);
  264. if (ca)
  265. ca->flags |= TCP_CONG_NON_RESTRICTED;
  266. }
  267. out:
  268. spin_unlock(&tcp_cong_list_lock);
  269. kfree(saved_clone);
  270. return ret;
  271. }
  272. /* Change congestion control for socket */
  273. int tcp_set_congestion_control(struct sock *sk, const char *name)
  274. {
  275. struct inet_connection_sock *icsk = inet_csk(sk);
  276. const struct tcp_congestion_ops *ca;
  277. int err = 0;
  278. if (icsk->icsk_ca_dst_locked)
  279. return -EPERM;
  280. rcu_read_lock();
  281. ca = __tcp_ca_find_autoload(name);
  282. /* No change asking for existing value */
  283. if (ca == icsk->icsk_ca_ops)
  284. goto out;
  285. if (!ca)
  286. err = -ENOENT;
  287. else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) ||
  288. ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)))
  289. err = -EPERM;
  290. else if (!try_module_get(ca->owner))
  291. err = -EBUSY;
  292. else
  293. tcp_reinit_congestion_control(sk, ca);
  294. out:
  295. rcu_read_unlock();
  296. return err;
  297. }
  298. /* Slow start is used when congestion window is no greater than the slow start
  299. * threshold. We base on RFC2581 and also handle stretch ACKs properly.
  300. * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
  301. * something better;) a packet is only considered (s)acked in its entirety to
  302. * defend the ACK attacks described in the RFC. Slow start processes a stretch
  303. * ACK of degree N as if N acks of degree 1 are received back to back except
  304. * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
  305. * returns the leftover acks to adjust cwnd in congestion avoidance mode.
  306. */
  307. u32 tcp_slow_start(struct tcp_sock *tp, u32 acked)
  308. {
  309. u32 cwnd = tp->snd_cwnd + acked;
  310. if (cwnd > tp->snd_ssthresh)
  311. cwnd = tp->snd_ssthresh + 1;
  312. acked -= cwnd - tp->snd_cwnd;
  313. tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
  314. return acked;
  315. }
  316. EXPORT_SYMBOL_GPL(tcp_slow_start);
  317. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w),
  318. * for every packet that was ACKed.
  319. */
  320. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked)
  321. {
  322. /* If credits accumulated at a higher w, apply them gently now. */
  323. if (tp->snd_cwnd_cnt >= w) {
  324. tp->snd_cwnd_cnt = 0;
  325. tp->snd_cwnd++;
  326. }
  327. tp->snd_cwnd_cnt += acked;
  328. if (tp->snd_cwnd_cnt >= w) {
  329. u32 delta = tp->snd_cwnd_cnt / w;
  330. tp->snd_cwnd_cnt -= delta * w;
  331. tp->snd_cwnd += delta;
  332. }
  333. tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_cwnd_clamp);
  334. }
  335. EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
  336. /*
  337. * TCP Reno congestion control
  338. * This is special case used for fallback as well.
  339. */
  340. /* This is Jacobson's slow start and congestion avoidance.
  341. * SIGCOMM '88, p. 328.
  342. */
  343. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  344. {
  345. struct tcp_sock *tp = tcp_sk(sk);
  346. if (!tcp_is_cwnd_limited(sk))
  347. return;
  348. /* In "safe" area, increase. */
  349. if (tp->snd_cwnd <= tp->snd_ssthresh) {
  350. acked = tcp_slow_start(tp, acked);
  351. if (!acked)
  352. return;
  353. }
  354. /* In dangerous area, increase slowly. */
  355. tcp_cong_avoid_ai(tp, tp->snd_cwnd, acked);
  356. }
  357. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  358. /* Slow start threshold is half the congestion window (min 2) */
  359. u32 tcp_reno_ssthresh(struct sock *sk)
  360. {
  361. const struct tcp_sock *tp = tcp_sk(sk);
  362. return max(tp->snd_cwnd >> 1U, 2U);
  363. }
  364. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  365. struct tcp_congestion_ops tcp_reno = {
  366. .flags = TCP_CONG_NON_RESTRICTED,
  367. .name = "reno",
  368. .owner = THIS_MODULE,
  369. .ssthresh = tcp_reno_ssthresh,
  370. .cong_avoid = tcp_reno_cong_avoid,
  371. };