tcp_dctcp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* DataCenter TCP (DCTCP) congestion control.
  3. *
  4. * http://simula.stanford.edu/~alizade/Site/DCTCP.html
  5. *
  6. * This is an implementation of DCTCP over Reno, an enhancement to the
  7. * TCP congestion control algorithm designed for data centers. DCTCP
  8. * leverages Explicit Congestion Notification (ECN) in the network to
  9. * provide multi-bit feedback to the end hosts. DCTCP's goal is to meet
  10. * the following three data center transport requirements:
  11. *
  12. * - High burst tolerance (incast due to partition/aggregate)
  13. * - Low latency (short flows, queries)
  14. * - High throughput (continuous data updates, large file transfers)
  15. * with commodity shallow buffered switches
  16. *
  17. * The algorithm is described in detail in the following two papers:
  18. *
  19. * 1) Mohammad Alizadeh, Albert Greenberg, David A. Maltz, Jitendra Padhye,
  20. * Parveen Patel, Balaji Prabhakar, Sudipta Sengupta, and Murari Sridharan:
  21. * "Data Center TCP (DCTCP)", Data Center Networks session
  22. * Proc. ACM SIGCOMM, New Delhi, 2010.
  23. * http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp-final.pdf
  24. *
  25. * 2) Mohammad Alizadeh, Adel Javanmard, and Balaji Prabhakar:
  26. * "Analysis of DCTCP: Stability, Convergence, and Fairness"
  27. * Proc. ACM SIGMETRICS, San Jose, 2011.
  28. * http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp_analysis-full.pdf
  29. *
  30. * Initial prototype from Abdul Kabbani, Masato Yasuda and Mohammad Alizadeh.
  31. *
  32. * Authors:
  33. *
  34. * Daniel Borkmann <[email protected]>
  35. * Florian Westphal <[email protected]>
  36. * Glenn Judd <[email protected]>
  37. */
  38. #include <linux/btf.h>
  39. #include <linux/btf_ids.h>
  40. #include <linux/module.h>
  41. #include <linux/mm.h>
  42. #include <net/tcp.h>
  43. #include <linux/inet_diag.h>
  44. #include "tcp_dctcp.h"
  45. #define DCTCP_MAX_ALPHA 1024U
  46. struct dctcp {
  47. u32 old_delivered;
  48. u32 old_delivered_ce;
  49. u32 prior_rcv_nxt;
  50. u32 dctcp_alpha;
  51. u32 next_seq;
  52. u32 ce_state;
  53. u32 loss_cwnd;
  54. };
  55. static unsigned int dctcp_shift_g __read_mostly = 4; /* g = 1/2^4 */
  56. module_param(dctcp_shift_g, uint, 0644);
  57. MODULE_PARM_DESC(dctcp_shift_g, "parameter g for updating dctcp_alpha");
  58. static unsigned int dctcp_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA;
  59. module_param(dctcp_alpha_on_init, uint, 0644);
  60. MODULE_PARM_DESC(dctcp_alpha_on_init, "parameter for initial alpha value");
  61. static struct tcp_congestion_ops dctcp_reno;
  62. static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca)
  63. {
  64. ca->next_seq = tp->snd_nxt;
  65. ca->old_delivered = tp->delivered;
  66. ca->old_delivered_ce = tp->delivered_ce;
  67. }
  68. static void dctcp_init(struct sock *sk)
  69. {
  70. const struct tcp_sock *tp = tcp_sk(sk);
  71. if ((tp->ecn_flags & TCP_ECN_OK) ||
  72. (sk->sk_state == TCP_LISTEN ||
  73. sk->sk_state == TCP_CLOSE)) {
  74. struct dctcp *ca = inet_csk_ca(sk);
  75. ca->prior_rcv_nxt = tp->rcv_nxt;
  76. ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
  77. ca->loss_cwnd = 0;
  78. ca->ce_state = 0;
  79. dctcp_reset(tp, ca);
  80. return;
  81. }
  82. /* No ECN support? Fall back to Reno. Also need to clear
  83. * ECT from sk since it is set during 3WHS for DCTCP.
  84. */
  85. inet_csk(sk)->icsk_ca_ops = &dctcp_reno;
  86. INET_ECN_dontxmit(sk);
  87. }
  88. static u32 dctcp_ssthresh(struct sock *sk)
  89. {
  90. struct dctcp *ca = inet_csk_ca(sk);
  91. struct tcp_sock *tp = tcp_sk(sk);
  92. ca->loss_cwnd = tcp_snd_cwnd(tp);
  93. return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * ca->dctcp_alpha) >> 11U), 2U);
  94. }
  95. static void dctcp_update_alpha(struct sock *sk, u32 flags)
  96. {
  97. const struct tcp_sock *tp = tcp_sk(sk);
  98. struct dctcp *ca = inet_csk_ca(sk);
  99. /* Expired RTT */
  100. if (!before(tp->snd_una, ca->next_seq)) {
  101. u32 delivered_ce = tp->delivered_ce - ca->old_delivered_ce;
  102. u32 alpha = ca->dctcp_alpha;
  103. /* alpha = (1 - g) * alpha + g * F */
  104. alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g);
  105. if (delivered_ce) {
  106. u32 delivered = tp->delivered - ca->old_delivered;
  107. /* If dctcp_shift_g == 1, a 32bit value would overflow
  108. * after 8 M packets.
  109. */
  110. delivered_ce <<= (10 - dctcp_shift_g);
  111. delivered_ce /= max(1U, delivered);
  112. alpha = min(alpha + delivered_ce, DCTCP_MAX_ALPHA);
  113. }
  114. /* dctcp_alpha can be read from dctcp_get_info() without
  115. * synchro, so we ask compiler to not use dctcp_alpha
  116. * as a temporary variable in prior operations.
  117. */
  118. WRITE_ONCE(ca->dctcp_alpha, alpha);
  119. dctcp_reset(tp, ca);
  120. }
  121. }
  122. static void dctcp_react_to_loss(struct sock *sk)
  123. {
  124. struct dctcp *ca = inet_csk_ca(sk);
  125. struct tcp_sock *tp = tcp_sk(sk);
  126. ca->loss_cwnd = tcp_snd_cwnd(tp);
  127. tp->snd_ssthresh = max(tcp_snd_cwnd(tp) >> 1U, 2U);
  128. }
  129. static void dctcp_state(struct sock *sk, u8 new_state)
  130. {
  131. if (new_state == TCP_CA_Recovery &&
  132. new_state != inet_csk(sk)->icsk_ca_state)
  133. dctcp_react_to_loss(sk);
  134. /* We handle RTO in dctcp_cwnd_event to ensure that we perform only
  135. * one loss-adjustment per RTT.
  136. */
  137. }
  138. static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
  139. {
  140. struct dctcp *ca = inet_csk_ca(sk);
  141. switch (ev) {
  142. case CA_EVENT_ECN_IS_CE:
  143. case CA_EVENT_ECN_NO_CE:
  144. dctcp_ece_ack_update(sk, ev, &ca->prior_rcv_nxt, &ca->ce_state);
  145. break;
  146. case CA_EVENT_LOSS:
  147. dctcp_react_to_loss(sk);
  148. break;
  149. default:
  150. /* Don't care for the rest. */
  151. break;
  152. }
  153. }
  154. static size_t dctcp_get_info(struct sock *sk, u32 ext, int *attr,
  155. union tcp_cc_info *info)
  156. {
  157. const struct dctcp *ca = inet_csk_ca(sk);
  158. const struct tcp_sock *tp = tcp_sk(sk);
  159. /* Fill it also in case of VEGASINFO due to req struct limits.
  160. * We can still correctly retrieve it later.
  161. */
  162. if (ext & (1 << (INET_DIAG_DCTCPINFO - 1)) ||
  163. ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  164. memset(&info->dctcp, 0, sizeof(info->dctcp));
  165. if (inet_csk(sk)->icsk_ca_ops != &dctcp_reno) {
  166. info->dctcp.dctcp_enabled = 1;
  167. info->dctcp.dctcp_ce_state = (u16) ca->ce_state;
  168. info->dctcp.dctcp_alpha = ca->dctcp_alpha;
  169. info->dctcp.dctcp_ab_ecn = tp->mss_cache *
  170. (tp->delivered_ce - ca->old_delivered_ce);
  171. info->dctcp.dctcp_ab_tot = tp->mss_cache *
  172. (tp->delivered - ca->old_delivered);
  173. }
  174. *attr = INET_DIAG_DCTCPINFO;
  175. return sizeof(info->dctcp);
  176. }
  177. return 0;
  178. }
  179. static u32 dctcp_cwnd_undo(struct sock *sk)
  180. {
  181. const struct dctcp *ca = inet_csk_ca(sk);
  182. struct tcp_sock *tp = tcp_sk(sk);
  183. return max(tcp_snd_cwnd(tp), ca->loss_cwnd);
  184. }
  185. static struct tcp_congestion_ops dctcp __read_mostly = {
  186. .init = dctcp_init,
  187. .in_ack_event = dctcp_update_alpha,
  188. .cwnd_event = dctcp_cwnd_event,
  189. .ssthresh = dctcp_ssthresh,
  190. .cong_avoid = tcp_reno_cong_avoid,
  191. .undo_cwnd = dctcp_cwnd_undo,
  192. .set_state = dctcp_state,
  193. .get_info = dctcp_get_info,
  194. .flags = TCP_CONG_NEEDS_ECN,
  195. .owner = THIS_MODULE,
  196. .name = "dctcp",
  197. };
  198. static struct tcp_congestion_ops dctcp_reno __read_mostly = {
  199. .ssthresh = tcp_reno_ssthresh,
  200. .cong_avoid = tcp_reno_cong_avoid,
  201. .undo_cwnd = tcp_reno_undo_cwnd,
  202. .get_info = dctcp_get_info,
  203. .owner = THIS_MODULE,
  204. .name = "dctcp-reno",
  205. };
  206. BTF_SET8_START(tcp_dctcp_check_kfunc_ids)
  207. #ifdef CONFIG_X86
  208. #ifdef CONFIG_DYNAMIC_FTRACE
  209. BTF_ID_FLAGS(func, dctcp_init)
  210. BTF_ID_FLAGS(func, dctcp_update_alpha)
  211. BTF_ID_FLAGS(func, dctcp_cwnd_event)
  212. BTF_ID_FLAGS(func, dctcp_ssthresh)
  213. BTF_ID_FLAGS(func, dctcp_cwnd_undo)
  214. BTF_ID_FLAGS(func, dctcp_state)
  215. #endif
  216. #endif
  217. BTF_SET8_END(tcp_dctcp_check_kfunc_ids)
  218. static const struct btf_kfunc_id_set tcp_dctcp_kfunc_set = {
  219. .owner = THIS_MODULE,
  220. .set = &tcp_dctcp_check_kfunc_ids,
  221. };
  222. static int __init dctcp_register(void)
  223. {
  224. int ret;
  225. BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE);
  226. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &tcp_dctcp_kfunc_set);
  227. if (ret < 0)
  228. return ret;
  229. return tcp_register_congestion_control(&dctcp);
  230. }
  231. static void __exit dctcp_unregister(void)
  232. {
  233. tcp_unregister_congestion_control(&dctcp);
  234. }
  235. module_init(dctcp_register);
  236. module_exit(dctcp_unregister);
  237. MODULE_AUTHOR("Daniel Borkmann <[email protected]>");
  238. MODULE_AUTHOR("Florian Westphal <[email protected]>");
  239. MODULE_AUTHOR("Glenn Judd <[email protected]>");
  240. MODULE_LICENSE("GPL v2");
  241. MODULE_DESCRIPTION("DataCenter TCP (DCTCP)");