tcp_cong.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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_debug("%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, bool *ecn_ca)
  100. {
  101. const struct tcp_congestion_ops *ca;
  102. u32 key = TCP_CA_UNSPEC;
  103. might_sleep();
  104. rcu_read_lock();
  105. ca = __tcp_ca_find_autoload(name);
  106. if (ca) {
  107. key = ca->key;
  108. *ecn_ca = ca->flags & TCP_CONG_NEEDS_ECN;
  109. }
  110. rcu_read_unlock();
  111. return key;
  112. }
  113. EXPORT_SYMBOL_GPL(tcp_ca_get_key_by_name);
  114. char *tcp_ca_get_name_by_key(u32 key, char *buffer)
  115. {
  116. const struct tcp_congestion_ops *ca;
  117. char *ret = NULL;
  118. rcu_read_lock();
  119. ca = tcp_ca_find_key(key);
  120. if (ca)
  121. ret = strncpy(buffer, ca->name,
  122. TCP_CA_NAME_MAX);
  123. rcu_read_unlock();
  124. return ret;
  125. }
  126. EXPORT_SYMBOL_GPL(tcp_ca_get_name_by_key);
  127. /* Assign choice of congestion control. */
  128. void tcp_assign_congestion_control(struct sock *sk)
  129. {
  130. struct inet_connection_sock *icsk = inet_csk(sk);
  131. struct tcp_congestion_ops *ca;
  132. rcu_read_lock();
  133. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  134. if (likely(try_module_get(ca->owner))) {
  135. icsk->icsk_ca_ops = ca;
  136. goto out;
  137. }
  138. /* Fallback to next available. The last really
  139. * guaranteed fallback is Reno from this list.
  140. */
  141. }
  142. out:
  143. rcu_read_unlock();
  144. /* Clear out private data before diag gets it and
  145. * the ca has not been initialized.
  146. */
  147. if (ca->get_info)
  148. memset(icsk->icsk_ca_priv, 0, sizeof(icsk->icsk_ca_priv));
  149. if (ca->flags & TCP_CONG_NEEDS_ECN)
  150. INET_ECN_xmit(sk);
  151. else
  152. INET_ECN_dontxmit(sk);
  153. }
  154. void tcp_init_congestion_control(struct sock *sk)
  155. {
  156. const struct inet_connection_sock *icsk = inet_csk(sk);
  157. tcp_sk(sk)->prior_ssthresh = 0;
  158. if (icsk->icsk_ca_ops->init)
  159. icsk->icsk_ca_ops->init(sk);
  160. if (tcp_ca_needs_ecn(sk))
  161. INET_ECN_xmit(sk);
  162. else
  163. INET_ECN_dontxmit(sk);
  164. }
  165. static void tcp_reinit_congestion_control(struct sock *sk,
  166. const struct tcp_congestion_ops *ca)
  167. {
  168. struct inet_connection_sock *icsk = inet_csk(sk);
  169. tcp_cleanup_congestion_control(sk);
  170. icsk->icsk_ca_ops = ca;
  171. icsk->icsk_ca_setsockopt = 1;
  172. if (sk->sk_state != TCP_CLOSE)
  173. tcp_init_congestion_control(sk);
  174. }
  175. /* Manage refcounts on socket close. */
  176. void tcp_cleanup_congestion_control(struct sock *sk)
  177. {
  178. struct inet_connection_sock *icsk = inet_csk(sk);
  179. if (icsk->icsk_ca_ops->release)
  180. icsk->icsk_ca_ops->release(sk);
  181. module_put(icsk->icsk_ca_ops->owner);
  182. }
  183. /* Used by sysctl to change default congestion control */
  184. int tcp_set_default_congestion_control(const char *name)
  185. {
  186. struct tcp_congestion_ops *ca;
  187. int ret = -ENOENT;
  188. spin_lock(&tcp_cong_list_lock);
  189. ca = tcp_ca_find(name);
  190. #ifdef CONFIG_MODULES
  191. if (!ca && capable(CAP_NET_ADMIN)) {
  192. spin_unlock(&tcp_cong_list_lock);
  193. request_module("tcp_%s", name);
  194. spin_lock(&tcp_cong_list_lock);
  195. ca = tcp_ca_find(name);
  196. }
  197. #endif
  198. if (ca) {
  199. ca->flags |= TCP_CONG_NON_RESTRICTED; /* default is always allowed */
  200. list_move(&ca->list, &tcp_cong_list);
  201. ret = 0;
  202. }
  203. spin_unlock(&tcp_cong_list_lock);
  204. return ret;
  205. }
  206. /* Set default value from kernel configuration at bootup */
  207. static int __init tcp_congestion_default(void)
  208. {
  209. return tcp_set_default_congestion_control(CONFIG_DEFAULT_TCP_CONG);
  210. }
  211. late_initcall(tcp_congestion_default);
  212. /* Build string with list of available congestion control values */
  213. void tcp_get_available_congestion_control(char *buf, size_t maxlen)
  214. {
  215. struct tcp_congestion_ops *ca;
  216. size_t offs = 0;
  217. rcu_read_lock();
  218. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  219. offs += snprintf(buf + offs, maxlen - offs,
  220. "%s%s",
  221. offs == 0 ? "" : " ", ca->name);
  222. }
  223. rcu_read_unlock();
  224. }
  225. /* Get current default congestion control */
  226. void tcp_get_default_congestion_control(char *name)
  227. {
  228. struct tcp_congestion_ops *ca;
  229. /* We will always have reno... */
  230. BUG_ON(list_empty(&tcp_cong_list));
  231. rcu_read_lock();
  232. ca = list_entry(tcp_cong_list.next, struct tcp_congestion_ops, list);
  233. strncpy(name, ca->name, TCP_CA_NAME_MAX);
  234. rcu_read_unlock();
  235. }
  236. /* Built list of non-restricted congestion control values */
  237. void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
  238. {
  239. struct tcp_congestion_ops *ca;
  240. size_t offs = 0;
  241. *buf = '\0';
  242. rcu_read_lock();
  243. list_for_each_entry_rcu(ca, &tcp_cong_list, list) {
  244. if (!(ca->flags & TCP_CONG_NON_RESTRICTED))
  245. continue;
  246. offs += snprintf(buf + offs, maxlen - offs,
  247. "%s%s",
  248. offs == 0 ? "" : " ", ca->name);
  249. }
  250. rcu_read_unlock();
  251. }
  252. /* Change list of non-restricted congestion control */
  253. int tcp_set_allowed_congestion_control(char *val)
  254. {
  255. struct tcp_congestion_ops *ca;
  256. char *saved_clone, *clone, *name;
  257. int ret = 0;
  258. saved_clone = clone = kstrdup(val, GFP_USER);
  259. if (!clone)
  260. return -ENOMEM;
  261. spin_lock(&tcp_cong_list_lock);
  262. /* pass 1 check for bad entries */
  263. while ((name = strsep(&clone, " ")) && *name) {
  264. ca = tcp_ca_find(name);
  265. if (!ca) {
  266. ret = -ENOENT;
  267. goto out;
  268. }
  269. }
  270. /* pass 2 clear old values */
  271. list_for_each_entry_rcu(ca, &tcp_cong_list, list)
  272. ca->flags &= ~TCP_CONG_NON_RESTRICTED;
  273. /* pass 3 mark as allowed */
  274. while ((name = strsep(&val, " ")) && *name) {
  275. ca = tcp_ca_find(name);
  276. WARN_ON(!ca);
  277. if (ca)
  278. ca->flags |= TCP_CONG_NON_RESTRICTED;
  279. }
  280. out:
  281. spin_unlock(&tcp_cong_list_lock);
  282. kfree(saved_clone);
  283. return ret;
  284. }
  285. /* Change congestion control for socket */
  286. int tcp_set_congestion_control(struct sock *sk, const char *name)
  287. {
  288. struct inet_connection_sock *icsk = inet_csk(sk);
  289. const struct tcp_congestion_ops *ca;
  290. int err = 0;
  291. if (icsk->icsk_ca_dst_locked)
  292. return -EPERM;
  293. rcu_read_lock();
  294. ca = __tcp_ca_find_autoload(name);
  295. /* No change asking for existing value */
  296. if (ca == icsk->icsk_ca_ops) {
  297. icsk->icsk_ca_setsockopt = 1;
  298. goto out;
  299. }
  300. if (!ca)
  301. err = -ENOENT;
  302. else if (!((ca->flags & TCP_CONG_NON_RESTRICTED) ||
  303. ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)))
  304. err = -EPERM;
  305. else if (!try_module_get(ca->owner))
  306. err = -EBUSY;
  307. else
  308. tcp_reinit_congestion_control(sk, ca);
  309. out:
  310. rcu_read_unlock();
  311. return err;
  312. }
  313. /* Slow start is used when congestion window is no greater than the slow start
  314. * threshold. We base on RFC2581 and also handle stretch ACKs properly.
  315. * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
  316. * something better;) a packet is only considered (s)acked in its entirety to
  317. * defend the ACK attacks described in the RFC. Slow start processes a stretch
  318. * ACK of degree N as if N acks of degree 1 are received back to back except
  319. * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
  320. * returns the leftover acks to adjust cwnd in congestion avoidance mode.
  321. */
  322. u32 tcp_slow_start(struct tcp_sock *tp, u32 acked)
  323. {
  324. u32 cwnd = min(tp->snd_cwnd + acked, tp->snd_ssthresh);
  325. acked -= cwnd - tp->snd_cwnd;
  326. tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
  327. return acked;
  328. }
  329. EXPORT_SYMBOL_GPL(tcp_slow_start);
  330. /* In theory this is tp->snd_cwnd += 1 / tp->snd_cwnd (or alternative w),
  331. * for every packet that was ACKed.
  332. */
  333. void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked)
  334. {
  335. /* If credits accumulated at a higher w, apply them gently now. */
  336. if (tp->snd_cwnd_cnt >= w) {
  337. tp->snd_cwnd_cnt = 0;
  338. tp->snd_cwnd++;
  339. }
  340. tp->snd_cwnd_cnt += acked;
  341. if (tp->snd_cwnd_cnt >= w) {
  342. u32 delta = tp->snd_cwnd_cnt / w;
  343. tp->snd_cwnd_cnt -= delta * w;
  344. tp->snd_cwnd += delta;
  345. }
  346. tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_cwnd_clamp);
  347. }
  348. EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
  349. /*
  350. * TCP Reno congestion control
  351. * This is special case used for fallback as well.
  352. */
  353. /* This is Jacobson's slow start and congestion avoidance.
  354. * SIGCOMM '88, p. 328.
  355. */
  356. void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked)
  357. {
  358. struct tcp_sock *tp = tcp_sk(sk);
  359. if (!tcp_is_cwnd_limited(sk))
  360. return;
  361. /* In "safe" area, increase. */
  362. if (tcp_in_slow_start(tp)) {
  363. acked = tcp_slow_start(tp, acked);
  364. if (!acked)
  365. return;
  366. }
  367. /* In dangerous area, increase slowly. */
  368. tcp_cong_avoid_ai(tp, tp->snd_cwnd, acked);
  369. }
  370. EXPORT_SYMBOL_GPL(tcp_reno_cong_avoid);
  371. /* Slow start threshold is half the congestion window (min 2) */
  372. u32 tcp_reno_ssthresh(struct sock *sk)
  373. {
  374. const struct tcp_sock *tp = tcp_sk(sk);
  375. return max(tp->snd_cwnd >> 1U, 2U);
  376. }
  377. EXPORT_SYMBOL_GPL(tcp_reno_ssthresh);
  378. struct tcp_congestion_ops tcp_reno = {
  379. .flags = TCP_CONG_NON_RESTRICTED,
  380. .name = "reno",
  381. .owner = THIS_MODULE,
  382. .ssthresh = tcp_reno_ssthresh,
  383. .cong_avoid = tcp_reno_cong_avoid,
  384. };