tcp_cong.c 12 KB

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