tcp_dctcp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <dborkman@redhat.com>
  35. * Florian Westphal <fw@strlen.de>
  36. * Glenn Judd <glenn.judd@morganstanley.com>
  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. struct tcp_plb_state plb;
  55. };
  56. static unsigned int dctcp_shift_g __read_mostly = 4; /* g = 1/2^4 */
  57. static int dctcp_shift_g_set(const char *val, const struct kernel_param *kp)
  58. {
  59. return param_set_uint_minmax(val, kp, 0, 10);
  60. }
  61. static const struct kernel_param_ops dctcp_shift_g_ops = {
  62. .set = dctcp_shift_g_set,
  63. .get = param_get_uint,
  64. };
  65. module_param_cb(dctcp_shift_g, &dctcp_shift_g_ops, &dctcp_shift_g, 0644);
  66. MODULE_PARM_DESC(dctcp_shift_g, "parameter g for updating dctcp_alpha");
  67. static unsigned int dctcp_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA;
  68. module_param(dctcp_alpha_on_init, uint, 0644);
  69. MODULE_PARM_DESC(dctcp_alpha_on_init, "parameter for initial alpha value");
  70. static struct tcp_congestion_ops dctcp_reno;
  71. static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca)
  72. {
  73. ca->next_seq = tp->snd_nxt;
  74. ca->old_delivered = tp->delivered;
  75. ca->old_delivered_ce = tp->delivered_ce;
  76. }
  77. __bpf_kfunc static void dctcp_init(struct sock *sk)
  78. {
  79. const struct tcp_sock *tp = tcp_sk(sk);
  80. if (tcp_ecn_mode_any(tp) ||
  81. (sk->sk_state == TCP_LISTEN ||
  82. sk->sk_state == TCP_CLOSE)) {
  83. struct dctcp *ca = inet_csk_ca(sk);
  84. ca->prior_rcv_nxt = tp->rcv_nxt;
  85. ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
  86. ca->loss_cwnd = 0;
  87. ca->ce_state = 0;
  88. dctcp_reset(tp, ca);
  89. tcp_plb_init(sk, &ca->plb);
  90. return;
  91. }
  92. /* No ECN support? Fall back to Reno. Also need to clear
  93. * ECT from sk since it is set during 3WHS for DCTCP.
  94. */
  95. inet_csk(sk)->icsk_ca_ops = &dctcp_reno;
  96. INET_ECN_dontxmit(sk);
  97. }
  98. __bpf_kfunc static u32 dctcp_ssthresh(struct sock *sk)
  99. {
  100. struct dctcp *ca = inet_csk_ca(sk);
  101. struct tcp_sock *tp = tcp_sk(sk);
  102. ca->loss_cwnd = tcp_snd_cwnd(tp);
  103. return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * ca->dctcp_alpha) >> 11U), 2U);
  104. }
  105. __bpf_kfunc static void dctcp_update_alpha(struct sock *sk, u32 flags)
  106. {
  107. const struct tcp_sock *tp = tcp_sk(sk);
  108. struct dctcp *ca = inet_csk_ca(sk);
  109. /* Expired RTT */
  110. if (!before(tp->snd_una, ca->next_seq)) {
  111. u32 delivered = tp->delivered - ca->old_delivered;
  112. u32 delivered_ce = tp->delivered_ce - ca->old_delivered_ce;
  113. u32 alpha = ca->dctcp_alpha;
  114. u32 ce_ratio = 0;
  115. if (delivered > 0) {
  116. /* dctcp_alpha keeps EWMA of fraction of ECN marked
  117. * packets. Because of EWMA smoothing, PLB reaction can
  118. * be slow so we use ce_ratio which is an instantaneous
  119. * measure of congestion. ce_ratio is the fraction of
  120. * ECN marked packets in the previous RTT.
  121. */
  122. if (delivered_ce > 0)
  123. ce_ratio = (delivered_ce << TCP_PLB_SCALE) / delivered;
  124. tcp_plb_update_state(sk, &ca->plb, (int)ce_ratio);
  125. tcp_plb_check_rehash(sk, &ca->plb);
  126. }
  127. /* alpha = (1 - g) * alpha + g * F */
  128. alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g);
  129. if (delivered_ce) {
  130. /* If dctcp_shift_g == 1, a 32bit value would overflow
  131. * after 8 M packets.
  132. */
  133. delivered_ce <<= (10 - dctcp_shift_g);
  134. delivered_ce /= max(1U, delivered);
  135. alpha = min(alpha + delivered_ce, DCTCP_MAX_ALPHA);
  136. }
  137. /* dctcp_alpha can be read from dctcp_get_info() without
  138. * synchro, so we ask compiler to not use dctcp_alpha
  139. * as a temporary variable in prior operations.
  140. */
  141. WRITE_ONCE(ca->dctcp_alpha, alpha);
  142. dctcp_reset(tp, ca);
  143. }
  144. }
  145. static void dctcp_react_to_loss(struct sock *sk)
  146. {
  147. struct dctcp *ca = inet_csk_ca(sk);
  148. struct tcp_sock *tp = tcp_sk(sk);
  149. ca->loss_cwnd = tcp_snd_cwnd(tp);
  150. tp->snd_ssthresh = max(tcp_snd_cwnd(tp) >> 1U, 2U);
  151. }
  152. __bpf_kfunc static void dctcp_state(struct sock *sk, u8 new_state)
  153. {
  154. if (new_state == TCP_CA_Recovery &&
  155. new_state != inet_csk(sk)->icsk_ca_state)
  156. dctcp_react_to_loss(sk);
  157. /* We handle RTO in dctcp_cwnd_event to ensure that we perform only
  158. * one loss-adjustment per RTT.
  159. */
  160. }
  161. __bpf_kfunc static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
  162. {
  163. struct dctcp *ca = inet_csk_ca(sk);
  164. switch (ev) {
  165. case CA_EVENT_ECN_IS_CE:
  166. case CA_EVENT_ECN_NO_CE:
  167. dctcp_ece_ack_update(sk, ev, &ca->prior_rcv_nxt, &ca->ce_state);
  168. break;
  169. case CA_EVENT_LOSS:
  170. tcp_plb_update_state_upon_rto(sk, &ca->plb);
  171. dctcp_react_to_loss(sk);
  172. break;
  173. case CA_EVENT_TX_START:
  174. tcp_plb_check_rehash(sk, &ca->plb); /* Maybe rehash when inflight is 0 */
  175. break;
  176. default:
  177. /* Don't care for the rest. */
  178. break;
  179. }
  180. }
  181. static size_t dctcp_get_info(struct sock *sk, u32 ext, int *attr,
  182. union tcp_cc_info *info)
  183. {
  184. const struct dctcp *ca = inet_csk_ca(sk);
  185. const struct tcp_sock *tp = tcp_sk(sk);
  186. /* Fill it also in case of VEGASINFO due to req struct limits.
  187. * We can still correctly retrieve it later.
  188. */
  189. if (ext & (1 << (INET_DIAG_DCTCPINFO - 1)) ||
  190. ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
  191. memset(&info->dctcp, 0, sizeof(info->dctcp));
  192. if (inet_csk(sk)->icsk_ca_ops != &dctcp_reno) {
  193. info->dctcp.dctcp_enabled = 1;
  194. info->dctcp.dctcp_ce_state = (u16) ca->ce_state;
  195. info->dctcp.dctcp_alpha = ca->dctcp_alpha;
  196. info->dctcp.dctcp_ab_ecn = tp->mss_cache *
  197. (tp->delivered_ce - ca->old_delivered_ce);
  198. info->dctcp.dctcp_ab_tot = tp->mss_cache *
  199. (tp->delivered - ca->old_delivered);
  200. }
  201. *attr = INET_DIAG_DCTCPINFO;
  202. return sizeof(info->dctcp);
  203. }
  204. return 0;
  205. }
  206. __bpf_kfunc static u32 dctcp_cwnd_undo(struct sock *sk)
  207. {
  208. const struct dctcp *ca = inet_csk_ca(sk);
  209. struct tcp_sock *tp = tcp_sk(sk);
  210. return max(tcp_snd_cwnd(tp), ca->loss_cwnd);
  211. }
  212. static struct tcp_congestion_ops dctcp __read_mostly = {
  213. .init = dctcp_init,
  214. .in_ack_event = dctcp_update_alpha,
  215. .cwnd_event = dctcp_cwnd_event,
  216. .ssthresh = dctcp_ssthresh,
  217. .cong_avoid = tcp_reno_cong_avoid,
  218. .undo_cwnd = dctcp_cwnd_undo,
  219. .set_state = dctcp_state,
  220. .get_info = dctcp_get_info,
  221. .flags = TCP_CONG_NEEDS_ECN,
  222. .owner = THIS_MODULE,
  223. .name = "dctcp",
  224. };
  225. static struct tcp_congestion_ops dctcp_reno __read_mostly = {
  226. .ssthresh = tcp_reno_ssthresh,
  227. .cong_avoid = tcp_reno_cong_avoid,
  228. .undo_cwnd = tcp_reno_undo_cwnd,
  229. .get_info = dctcp_get_info,
  230. .owner = THIS_MODULE,
  231. .name = "dctcp-reno",
  232. };
  233. BTF_KFUNCS_START(tcp_dctcp_check_kfunc_ids)
  234. BTF_ID_FLAGS(func, dctcp_init)
  235. BTF_ID_FLAGS(func, dctcp_update_alpha)
  236. BTF_ID_FLAGS(func, dctcp_cwnd_event)
  237. BTF_ID_FLAGS(func, dctcp_ssthresh)
  238. BTF_ID_FLAGS(func, dctcp_cwnd_undo)
  239. BTF_ID_FLAGS(func, dctcp_state)
  240. BTF_KFUNCS_END(tcp_dctcp_check_kfunc_ids)
  241. static const struct btf_kfunc_id_set tcp_dctcp_kfunc_set = {
  242. .owner = THIS_MODULE,
  243. .set = &tcp_dctcp_check_kfunc_ids,
  244. };
  245. static int __init dctcp_register(void)
  246. {
  247. int ret;
  248. BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE);
  249. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &tcp_dctcp_kfunc_set);
  250. if (ret < 0)
  251. return ret;
  252. return tcp_register_congestion_control(&dctcp);
  253. }
  254. static void __exit dctcp_unregister(void)
  255. {
  256. tcp_unregister_congestion_control(&dctcp);
  257. }
  258. module_init(dctcp_register);
  259. module_exit(dctcp_unregister);
  260. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  261. MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
  262. MODULE_AUTHOR("Glenn Judd <glenn.judd@morganstanley.com>");
  263. MODULE_LICENSE("GPL v2");
  264. MODULE_DESCRIPTION("DataCenter TCP (DCTCP)");