tcp_cong.c 11 KB

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