12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2e3118e83SDaniel Borkmann /* DataCenter TCP (DCTCP) congestion control. 3e3118e83SDaniel Borkmann * 4e3118e83SDaniel Borkmann * http://simula.stanford.edu/~alizade/Site/DCTCP.html 5e3118e83SDaniel Borkmann * 6e3118e83SDaniel Borkmann * This is an implementation of DCTCP over Reno, an enhancement to the 7e3118e83SDaniel Borkmann * TCP congestion control algorithm designed for data centers. DCTCP 8e3118e83SDaniel Borkmann * leverages Explicit Congestion Notification (ECN) in the network to 9e3118e83SDaniel Borkmann * provide multi-bit feedback to the end hosts. DCTCP's goal is to meet 10e3118e83SDaniel Borkmann * the following three data center transport requirements: 11e3118e83SDaniel Borkmann * 12e3118e83SDaniel Borkmann * - High burst tolerance (incast due to partition/aggregate) 13e3118e83SDaniel Borkmann * - Low latency (short flows, queries) 14e3118e83SDaniel Borkmann * - High throughput (continuous data updates, large file transfers) 15e3118e83SDaniel Borkmann * with commodity shallow buffered switches 16e3118e83SDaniel Borkmann * 17e3118e83SDaniel Borkmann * The algorithm is described in detail in the following two papers: 18e3118e83SDaniel Borkmann * 19e3118e83SDaniel Borkmann * 1) Mohammad Alizadeh, Albert Greenberg, David A. Maltz, Jitendra Padhye, 20e3118e83SDaniel Borkmann * Parveen Patel, Balaji Prabhakar, Sudipta Sengupta, and Murari Sridharan: 21e3118e83SDaniel Borkmann * "Data Center TCP (DCTCP)", Data Center Networks session 22e3118e83SDaniel Borkmann * Proc. ACM SIGCOMM, New Delhi, 2010. 23e3118e83SDaniel Borkmann * http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp-final.pdf 24e3118e83SDaniel Borkmann * 25e3118e83SDaniel Borkmann * 2) Mohammad Alizadeh, Adel Javanmard, and Balaji Prabhakar: 26e3118e83SDaniel Borkmann * "Analysis of DCTCP: Stability, Convergence, and Fairness" 27e3118e83SDaniel Borkmann * Proc. ACM SIGMETRICS, San Jose, 2011. 28e3118e83SDaniel Borkmann * http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp_analysis-full.pdf 29e3118e83SDaniel Borkmann * 30e3118e83SDaniel Borkmann * Initial prototype from Abdul Kabbani, Masato Yasuda and Mohammad Alizadeh. 31e3118e83SDaniel Borkmann * 32e3118e83SDaniel Borkmann * Authors: 33e3118e83SDaniel Borkmann * 34e3118e83SDaniel Borkmann * Daniel Borkmann <dborkman@redhat.com> 35e3118e83SDaniel Borkmann * Florian Westphal <fw@strlen.de> 36e3118e83SDaniel Borkmann * Glenn Judd <glenn.judd@morganstanley.com> 37e3118e83SDaniel Borkmann */ 38e3118e83SDaniel Borkmann 390e32dfc8SKumar Kartikeya Dwivedi #include <linux/btf.h> 400e32dfc8SKumar Kartikeya Dwivedi #include <linux/btf_ids.h> 41e3118e83SDaniel Borkmann #include <linux/module.h> 42e3118e83SDaniel Borkmann #include <linux/mm.h> 43e3118e83SDaniel Borkmann #include <net/tcp.h> 44e3118e83SDaniel Borkmann #include <linux/inet_diag.h> 45ffd177deSYuchung Cheng #include "tcp_dctcp.h" 46e3118e83SDaniel Borkmann 47e3118e83SDaniel Borkmann #define DCTCP_MAX_ALPHA 1024U 48e3118e83SDaniel Borkmann 49e3118e83SDaniel Borkmann struct dctcp { 50e3058450SEric Dumazet u32 old_delivered; 51e3058450SEric Dumazet u32 old_delivered_ce; 52e3118e83SDaniel Borkmann u32 prior_rcv_nxt; 53e3118e83SDaniel Borkmann u32 dctcp_alpha; 54e3118e83SDaniel Borkmann u32 next_seq; 55e3118e83SDaniel Borkmann u32 ce_state; 56ce6dd233SFlorian Westphal u32 loss_cwnd; 57c30f8e0bSMubashir Adnan Qureshi struct tcp_plb_state plb; 58e3118e83SDaniel Borkmann }; 59e3118e83SDaniel Borkmann 60e3118e83SDaniel Borkmann static unsigned int dctcp_shift_g __read_mostly = 4; /* g = 1/2^4 */ 61*3ebc46caSKuniyuki Iwashima 62*3ebc46caSKuniyuki Iwashima static int dctcp_shift_g_set(const char *val, const struct kernel_param *kp) 63*3ebc46caSKuniyuki Iwashima { 64*3ebc46caSKuniyuki Iwashima return param_set_uint_minmax(val, kp, 0, 10); 65*3ebc46caSKuniyuki Iwashima } 66*3ebc46caSKuniyuki Iwashima 67*3ebc46caSKuniyuki Iwashima static const struct kernel_param_ops dctcp_shift_g_ops = { 68*3ebc46caSKuniyuki Iwashima .set = dctcp_shift_g_set, 69*3ebc46caSKuniyuki Iwashima .get = param_get_uint, 70*3ebc46caSKuniyuki Iwashima }; 71*3ebc46caSKuniyuki Iwashima 72*3ebc46caSKuniyuki Iwashima module_param_cb(dctcp_shift_g, &dctcp_shift_g_ops, &dctcp_shift_g, 0644); 73e3118e83SDaniel Borkmann MODULE_PARM_DESC(dctcp_shift_g, "parameter g for updating dctcp_alpha"); 74e3118e83SDaniel Borkmann 75e3118e83SDaniel Borkmann static unsigned int dctcp_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA; 76e3118e83SDaniel Borkmann module_param(dctcp_alpha_on_init, uint, 0644); 77e3118e83SDaniel Borkmann MODULE_PARM_DESC(dctcp_alpha_on_init, "parameter for initial alpha value"); 78e3118e83SDaniel Borkmann 79e3118e83SDaniel Borkmann static struct tcp_congestion_ops dctcp_reno; 80e3118e83SDaniel Borkmann 81e3118e83SDaniel Borkmann static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca) 82e3118e83SDaniel Borkmann { 83e3118e83SDaniel Borkmann ca->next_seq = tp->snd_nxt; 84e3118e83SDaniel Borkmann 85e3058450SEric Dumazet ca->old_delivered = tp->delivered; 86e3058450SEric Dumazet ca->old_delivered_ce = tp->delivered_ce; 87e3118e83SDaniel Borkmann } 88e3118e83SDaniel Borkmann 89400031e0SDavid Vernet __bpf_kfunc static void dctcp_init(struct sock *sk) 90e3118e83SDaniel Borkmann { 91e3118e83SDaniel Borkmann const struct tcp_sock *tp = tcp_sk(sk); 92e3118e83SDaniel Borkmann 93e3118e83SDaniel Borkmann if ((tp->ecn_flags & TCP_ECN_OK) || 94e3118e83SDaniel Borkmann (sk->sk_state == TCP_LISTEN || 95e3118e83SDaniel Borkmann sk->sk_state == TCP_CLOSE)) { 96e3118e83SDaniel Borkmann struct dctcp *ca = inet_csk_ca(sk); 97e3118e83SDaniel Borkmann 98e3118e83SDaniel Borkmann ca->prior_rcv_nxt = tp->rcv_nxt; 99e3118e83SDaniel Borkmann 100e3118e83SDaniel Borkmann ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA); 101e3118e83SDaniel Borkmann 102ce6dd233SFlorian Westphal ca->loss_cwnd = 0; 103e3118e83SDaniel Borkmann ca->ce_state = 0; 104e3118e83SDaniel Borkmann 105e3118e83SDaniel Borkmann dctcp_reset(tp, ca); 106c30f8e0bSMubashir Adnan Qureshi tcp_plb_init(sk, &ca->plb); 107c30f8e0bSMubashir Adnan Qureshi 108e3118e83SDaniel Borkmann return; 109e3118e83SDaniel Borkmann } 110e3118e83SDaniel Borkmann 111e3118e83SDaniel Borkmann /* No ECN support? Fall back to Reno. Also need to clear 112e3118e83SDaniel Borkmann * ECT from sk since it is set during 3WHS for DCTCP. 113e3118e83SDaniel Borkmann */ 114e3118e83SDaniel Borkmann inet_csk(sk)->icsk_ca_ops = &dctcp_reno; 115e3118e83SDaniel Borkmann INET_ECN_dontxmit(sk); 116e3118e83SDaniel Borkmann } 117e3118e83SDaniel Borkmann 118400031e0SDavid Vernet __bpf_kfunc static u32 dctcp_ssthresh(struct sock *sk) 119e3118e83SDaniel Borkmann { 120ce6dd233SFlorian Westphal struct dctcp *ca = inet_csk_ca(sk); 121e3118e83SDaniel Borkmann struct tcp_sock *tp = tcp_sk(sk); 122e3118e83SDaniel Borkmann 12340570375SEric Dumazet ca->loss_cwnd = tcp_snd_cwnd(tp); 12440570375SEric Dumazet return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * ca->dctcp_alpha) >> 11U), 2U); 125e3118e83SDaniel Borkmann } 126e3118e83SDaniel Borkmann 127400031e0SDavid Vernet __bpf_kfunc static void dctcp_update_alpha(struct sock *sk, u32 flags) 128e3118e83SDaniel Borkmann { 129343dfaa1SFlorian Westphal const struct tcp_sock *tp = tcp_sk(sk); 130e3118e83SDaniel Borkmann struct dctcp *ca = inet_csk_ca(sk); 131e3118e83SDaniel Borkmann 132e3118e83SDaniel Borkmann /* Expired RTT */ 133e3118e83SDaniel Borkmann if (!before(tp->snd_una, ca->next_seq)) { 134c30f8e0bSMubashir Adnan Qureshi u32 delivered = tp->delivered - ca->old_delivered; 135e3058450SEric Dumazet u32 delivered_ce = tp->delivered_ce - ca->old_delivered_ce; 136f9c2ff22SEric Dumazet u32 alpha = ca->dctcp_alpha; 137c30f8e0bSMubashir Adnan Qureshi u32 ce_ratio = 0; 138c30f8e0bSMubashir Adnan Qureshi 139c30f8e0bSMubashir Adnan Qureshi if (delivered > 0) { 140c30f8e0bSMubashir Adnan Qureshi /* dctcp_alpha keeps EWMA of fraction of ECN marked 141c30f8e0bSMubashir Adnan Qureshi * packets. Because of EWMA smoothing, PLB reaction can 142c30f8e0bSMubashir Adnan Qureshi * be slow so we use ce_ratio which is an instantaneous 143c30f8e0bSMubashir Adnan Qureshi * measure of congestion. ce_ratio is the fraction of 144c30f8e0bSMubashir Adnan Qureshi * ECN marked packets in the previous RTT. 145c30f8e0bSMubashir Adnan Qureshi */ 146c30f8e0bSMubashir Adnan Qureshi if (delivered_ce > 0) 147c30f8e0bSMubashir Adnan Qureshi ce_ratio = (delivered_ce << TCP_PLB_SCALE) / delivered; 148c30f8e0bSMubashir Adnan Qureshi tcp_plb_update_state(sk, &ca->plb, (int)ce_ratio); 149c30f8e0bSMubashir Adnan Qureshi tcp_plb_check_rehash(sk, &ca->plb); 150c30f8e0bSMubashir Adnan Qureshi } 151e3118e83SDaniel Borkmann 152e3118e83SDaniel Borkmann /* alpha = (1 - g) * alpha + g * F */ 153e3118e83SDaniel Borkmann 154c80dbe04SAndrew Shewmaker alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g); 155e3058450SEric Dumazet if (delivered_ce) { 156e3118e83SDaniel Borkmann 157e3058450SEric Dumazet /* If dctcp_shift_g == 1, a 32bit value would overflow 158e3058450SEric Dumazet * after 8 M packets. 159e3058450SEric Dumazet */ 160e3058450SEric Dumazet delivered_ce <<= (10 - dctcp_shift_g); 161e3058450SEric Dumazet delivered_ce /= max(1U, delivered); 162e3058450SEric Dumazet 163e3058450SEric Dumazet alpha = min(alpha + delivered_ce, DCTCP_MAX_ALPHA); 164f9c2ff22SEric Dumazet } 165f9c2ff22SEric Dumazet /* dctcp_alpha can be read from dctcp_get_info() without 166f9c2ff22SEric Dumazet * synchro, so we ask compiler to not use dctcp_alpha 167f9c2ff22SEric Dumazet * as a temporary variable in prior operations. 168f9c2ff22SEric Dumazet */ 169f9c2ff22SEric Dumazet WRITE_ONCE(ca->dctcp_alpha, alpha); 170e3118e83SDaniel Borkmann dctcp_reset(tp, ca); 171e3118e83SDaniel Borkmann } 172e3118e83SDaniel Borkmann } 173e3118e83SDaniel Borkmann 174aecfde23SKoen De Schepper static void dctcp_react_to_loss(struct sock *sk) 175aecfde23SKoen De Schepper { 176aecfde23SKoen De Schepper struct dctcp *ca = inet_csk_ca(sk); 177aecfde23SKoen De Schepper struct tcp_sock *tp = tcp_sk(sk); 178aecfde23SKoen De Schepper 17940570375SEric Dumazet ca->loss_cwnd = tcp_snd_cwnd(tp); 18040570375SEric Dumazet tp->snd_ssthresh = max(tcp_snd_cwnd(tp) >> 1U, 2U); 181aecfde23SKoen De Schepper } 182aecfde23SKoen De Schepper 183400031e0SDavid Vernet __bpf_kfunc static void dctcp_state(struct sock *sk, u8 new_state) 184e3118e83SDaniel Borkmann { 185aecfde23SKoen De Schepper if (new_state == TCP_CA_Recovery && 186aecfde23SKoen De Schepper new_state != inet_csk(sk)->icsk_ca_state) 187aecfde23SKoen De Schepper dctcp_react_to_loss(sk); 188aecfde23SKoen De Schepper /* We handle RTO in dctcp_cwnd_event to ensure that we perform only 189aecfde23SKoen De Schepper * one loss-adjustment per RTT. 190e3118e83SDaniel Borkmann */ 191e3118e83SDaniel Borkmann } 192e3118e83SDaniel Borkmann 193400031e0SDavid Vernet __bpf_kfunc static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev) 194e3118e83SDaniel Borkmann { 195ffd177deSYuchung Cheng struct dctcp *ca = inet_csk_ca(sk); 196ffd177deSYuchung Cheng 197e3118e83SDaniel Borkmann switch (ev) { 198e3118e83SDaniel Borkmann case CA_EVENT_ECN_IS_CE: 199e3118e83SDaniel Borkmann case CA_EVENT_ECN_NO_CE: 200ffd177deSYuchung Cheng dctcp_ece_ack_update(sk, ev, &ca->prior_rcv_nxt, &ca->ce_state); 201e3118e83SDaniel Borkmann break; 202aecfde23SKoen De Schepper case CA_EVENT_LOSS: 203c30f8e0bSMubashir Adnan Qureshi tcp_plb_update_state_upon_rto(sk, &ca->plb); 204aecfde23SKoen De Schepper dctcp_react_to_loss(sk); 205aecfde23SKoen De Schepper break; 206c30f8e0bSMubashir Adnan Qureshi case CA_EVENT_TX_START: 207c30f8e0bSMubashir Adnan Qureshi tcp_plb_check_rehash(sk, &ca->plb); /* Maybe rehash when inflight is 0 */ 208c30f8e0bSMubashir Adnan Qureshi break; 209e3118e83SDaniel Borkmann default: 210e3118e83SDaniel Borkmann /* Don't care for the rest. */ 211e3118e83SDaniel Borkmann break; 212e3118e83SDaniel Borkmann } 213e3118e83SDaniel Borkmann } 214e3118e83SDaniel Borkmann 21564f40ff5SEric Dumazet static size_t dctcp_get_info(struct sock *sk, u32 ext, int *attr, 21664f40ff5SEric Dumazet union tcp_cc_info *info) 217e3118e83SDaniel Borkmann { 218e3118e83SDaniel Borkmann const struct dctcp *ca = inet_csk_ca(sk); 219e3058450SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 220e3118e83SDaniel Borkmann 221e3118e83SDaniel Borkmann /* Fill it also in case of VEGASINFO due to req struct limits. 222e3118e83SDaniel Borkmann * We can still correctly retrieve it later. 223e3118e83SDaniel Borkmann */ 224e3118e83SDaniel Borkmann if (ext & (1 << (INET_DIAG_DCTCPINFO - 1)) || 225e3118e83SDaniel Borkmann ext & (1 << (INET_DIAG_VEGASINFO - 1))) { 226dcf1158bSNeal Cardwell memset(&info->dctcp, 0, sizeof(info->dctcp)); 227e3118e83SDaniel Borkmann if (inet_csk(sk)->icsk_ca_ops != &dctcp_reno) { 22864f40ff5SEric Dumazet info->dctcp.dctcp_enabled = 1; 22964f40ff5SEric Dumazet info->dctcp.dctcp_ce_state = (u16) ca->ce_state; 23064f40ff5SEric Dumazet info->dctcp.dctcp_alpha = ca->dctcp_alpha; 231e3058450SEric Dumazet info->dctcp.dctcp_ab_ecn = tp->mss_cache * 232e3058450SEric Dumazet (tp->delivered_ce - ca->old_delivered_ce); 233e3058450SEric Dumazet info->dctcp.dctcp_ab_tot = tp->mss_cache * 234e3058450SEric Dumazet (tp->delivered - ca->old_delivered); 235e3118e83SDaniel Borkmann } 236e3118e83SDaniel Borkmann 23764f40ff5SEric Dumazet *attr = INET_DIAG_DCTCPINFO; 238dcf1158bSNeal Cardwell return sizeof(info->dctcp); 239e3118e83SDaniel Borkmann } 240521f1cf1SEric Dumazet return 0; 241e3118e83SDaniel Borkmann } 242e3118e83SDaniel Borkmann 243400031e0SDavid Vernet __bpf_kfunc static u32 dctcp_cwnd_undo(struct sock *sk) 244ce6dd233SFlorian Westphal { 245ce6dd233SFlorian Westphal const struct dctcp *ca = inet_csk_ca(sk); 24640570375SEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 247ce6dd233SFlorian Westphal 24840570375SEric Dumazet return max(tcp_snd_cwnd(tp), ca->loss_cwnd); 249ce6dd233SFlorian Westphal } 250ce6dd233SFlorian Westphal 251e3118e83SDaniel Borkmann static struct tcp_congestion_ops dctcp __read_mostly = { 252e3118e83SDaniel Borkmann .init = dctcp_init, 253e3118e83SDaniel Borkmann .in_ack_event = dctcp_update_alpha, 254e3118e83SDaniel Borkmann .cwnd_event = dctcp_cwnd_event, 255e3118e83SDaniel Borkmann .ssthresh = dctcp_ssthresh, 256e3118e83SDaniel Borkmann .cong_avoid = tcp_reno_cong_avoid, 257ce6dd233SFlorian Westphal .undo_cwnd = dctcp_cwnd_undo, 258e3118e83SDaniel Borkmann .set_state = dctcp_state, 259e3118e83SDaniel Borkmann .get_info = dctcp_get_info, 260e3118e83SDaniel Borkmann .flags = TCP_CONG_NEEDS_ECN, 261e3118e83SDaniel Borkmann .owner = THIS_MODULE, 262e3118e83SDaniel Borkmann .name = "dctcp", 263e3118e83SDaniel Borkmann }; 264e3118e83SDaniel Borkmann 265e3118e83SDaniel Borkmann static struct tcp_congestion_ops dctcp_reno __read_mostly = { 266e3118e83SDaniel Borkmann .ssthresh = tcp_reno_ssthresh, 267e3118e83SDaniel Borkmann .cong_avoid = tcp_reno_cong_avoid, 268e9799183SFlorian Westphal .undo_cwnd = tcp_reno_undo_cwnd, 269e3118e83SDaniel Borkmann .get_info = dctcp_get_info, 270e3118e83SDaniel Borkmann .owner = THIS_MODULE, 271e3118e83SDaniel Borkmann .name = "dctcp-reno", 272e3118e83SDaniel Borkmann }; 273e3118e83SDaniel Borkmann 2746f3189f3SDaniel Xu BTF_KFUNCS_START(tcp_dctcp_check_kfunc_ids) 275a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, dctcp_init) 276a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, dctcp_update_alpha) 277a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, dctcp_cwnd_event) 278a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, dctcp_ssthresh) 279a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, dctcp_cwnd_undo) 280a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, dctcp_state) 2816f3189f3SDaniel Xu BTF_KFUNCS_END(tcp_dctcp_check_kfunc_ids) 2820e32dfc8SKumar Kartikeya Dwivedi 283b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set tcp_dctcp_kfunc_set = { 284b202d844SKumar Kartikeya Dwivedi .owner = THIS_MODULE, 285a4703e31SKumar Kartikeya Dwivedi .set = &tcp_dctcp_check_kfunc_ids, 286b202d844SKumar Kartikeya Dwivedi }; 2870e32dfc8SKumar Kartikeya Dwivedi 288e3118e83SDaniel Borkmann static int __init dctcp_register(void) 289e3118e83SDaniel Borkmann { 2900e32dfc8SKumar Kartikeya Dwivedi int ret; 2910e32dfc8SKumar Kartikeya Dwivedi 292e3118e83SDaniel Borkmann BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE); 293b202d844SKumar Kartikeya Dwivedi 294b202d844SKumar Kartikeya Dwivedi ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &tcp_dctcp_kfunc_set); 295b202d844SKumar Kartikeya Dwivedi if (ret < 0) 2960e32dfc8SKumar Kartikeya Dwivedi return ret; 297b202d844SKumar Kartikeya Dwivedi return tcp_register_congestion_control(&dctcp); 298e3118e83SDaniel Borkmann } 299e3118e83SDaniel Borkmann 300e3118e83SDaniel Borkmann static void __exit dctcp_unregister(void) 301e3118e83SDaniel Borkmann { 302e3118e83SDaniel Borkmann tcp_unregister_congestion_control(&dctcp); 303e3118e83SDaniel Borkmann } 304e3118e83SDaniel Borkmann 305e3118e83SDaniel Borkmann module_init(dctcp_register); 306e3118e83SDaniel Borkmann module_exit(dctcp_unregister); 307e3118e83SDaniel Borkmann 308e3118e83SDaniel Borkmann MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); 309e3118e83SDaniel Borkmann MODULE_AUTHOR("Florian Westphal <fw@strlen.de>"); 310e3118e83SDaniel Borkmann MODULE_AUTHOR("Glenn Judd <glenn.judd@morganstanley.com>"); 311e3118e83SDaniel Borkmann 312e3118e83SDaniel Borkmann MODULE_LICENSE("GPL v2"); 313e3118e83SDaniel Borkmann MODULE_DESCRIPTION("DataCenter TCP (DCTCP)"); 314