/* DataCenter TCP (DCTCP) congestion control. * * http://simula.stanford.edu/~alizade/Site/DCTCP.html * * This is an implementation of DCTCP over Reno, an enhancement to the * TCP congestion control algorithm designed for data centers. DCTCP * leverages Explicit Congestion Notification (ECN) in the network to * provide multi-bit feedback to the end hosts. DCTCP's goal is to meet * the following three data center transport requirements: * * - High burst tolerance (incast due to partition/aggregate) * - Low latency (short flows, queries) * - High throughput (continuous data updates, large file transfers) * with commodity shallow buffered switches * * The algorithm is described in detail in the following two papers: * * 1) Mohammad Alizadeh, Albert Greenberg, David A. Maltz, Jitendra Padhye, * Parveen Patel, Balaji Prabhakar, Sudipta Sengupta, and Murari Sridharan: * "Data Center TCP (DCTCP)", Data Center Networks session * Proc. ACM SIGCOMM, New Delhi, 2010. * http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp-final.pdf * * 2) Mohammad Alizadeh, Adel Javanmard, and Balaji Prabhakar: * "Analysis of DCTCP: Stability, Convergence, and Fairness" * Proc. ACM SIGMETRICS, San Jose, 2011. * http://simula.stanford.edu/~alizade/Site/DCTCP_files/dctcp_analysis-full.pdf * * Initial prototype from Abdul Kabbani, Masato Yasuda and Mohammad Alizadeh. * * Authors: * * Daniel Borkmann * Florian Westphal * Glenn Judd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. */ #include #include #include #include #define DCTCP_MAX_ALPHA 1024U struct dctcp { u32 acked_bytes_ecn; u32 acked_bytes_total; u32 prior_snd_una; u32 prior_rcv_nxt; u32 dctcp_alpha; u32 next_seq; u32 ce_state; u32 delayed_ack_reserved; }; static unsigned int dctcp_shift_g __read_mostly = 4; /* g = 1/2^4 */ module_param(dctcp_shift_g, uint, 0644); MODULE_PARM_DESC(dctcp_shift_g, "parameter g for updating dctcp_alpha"); static unsigned int dctcp_alpha_on_init __read_mostly = DCTCP_MAX_ALPHA; module_param(dctcp_alpha_on_init, uint, 0644); MODULE_PARM_DESC(dctcp_alpha_on_init, "parameter for initial alpha value"); static unsigned int dctcp_clamp_alpha_on_loss __read_mostly; module_param(dctcp_clamp_alpha_on_loss, uint, 0644); MODULE_PARM_DESC(dctcp_clamp_alpha_on_loss, "parameter for clamping alpha on loss"); static struct tcp_congestion_ops dctcp_reno; static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca) { ca->next_seq = tp->snd_nxt; ca->acked_bytes_ecn = 0; ca->acked_bytes_total = 0; } static void dctcp_init(struct sock *sk) { const struct tcp_sock *tp = tcp_sk(sk); if ((tp->ecn_flags & TCP_ECN_OK) || (sk->sk_state == TCP_LISTEN || sk->sk_state == TCP_CLOSE)) { struct dctcp *ca = inet_csk_ca(sk); ca->prior_snd_una = tp->snd_una; ca->prior_rcv_nxt = tp->rcv_nxt; ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA); ca->delayed_ack_reserved = 0; ca->ce_state = 0; dctcp_reset(tp, ca); return; } /* No ECN support? Fall back to Reno. Also need to clear * ECT from sk since it is set during 3WHS for DCTCP. */ inet_csk(sk)->icsk_ca_ops = &dctcp_reno; INET_ECN_dontxmit(sk); } static u32 dctcp_ssthresh(struct sock *sk) { const struct dctcp *ca = inet_csk_ca(sk); struct tcp_sock *tp = tcp_sk(sk); return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->dctcp_alpha) >> 11U), 2U); } /* Minimal DCTP CE state machine: * * S: 0 <- last pkt was non-CE * 1 <- last pkt was CE */ static void dctcp_ce_state_0_to_1(struct sock *sk) { struct dctcp *ca = inet_csk_ca(sk); struct tcp_sock *tp = tcp_sk(sk); /* State has changed from CE=0 to CE=1 and delayed * ACK has not sent yet. */ if (!ca->ce_state && ca->delayed_ack_reserved) { u32 tmp_rcv_nxt; /* Save current rcv_nxt. */ tmp_rcv_nxt = tp->rcv_nxt; /* Generate previous ack with CE=0. */ tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; tp->rcv_nxt = ca->prior_rcv_nxt; tcp_send_ack(sk); /* Recover current rcv_nxt. */ tp->rcv_nxt = tmp_rcv_nxt; } ca->prior_rcv_nxt = tp->rcv_nxt; ca->ce_state = 1; tp->ecn_flags |= TCP_ECN_DEMAND_CWR; } static void dctcp_ce_state_1_to_0(struct sock *sk) { struct dctcp *ca = inet_csk_ca(sk); struct tcp_sock *tp = tcp_sk(sk); /* State has changed from CE=1 to CE=0 and delayed * ACK has not sent yet. */ if (ca->ce_state && ca->delayed_ack_reserved) { u32 tmp_rcv_nxt; /* Save current rcv_nxt. */ tmp_rcv_nxt = tp->rcv_nxt; /* Generate previous ack with CE=1. */ tp->ecn_flags |= TCP_ECN_DEMAND_CWR; tp->rcv_nxt = ca->prior_rcv_nxt; tcp_send_ack(sk); /* Recover current rcv_nxt. */ tp->rcv_nxt = tmp_rcv_nxt; } ca->prior_rcv_nxt = tp->rcv_nxt; ca->ce_state = 0; tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; } static void dctcp_update_alpha(struct sock *sk, u32 flags) { const struct tcp_sock *tp = tcp_sk(sk); struct dctcp *ca = inet_csk_ca(sk); u32 acked_bytes = tp->snd_una - ca->prior_snd_una; /* If ack did not advance snd_una, count dupack as MSS size. * If ack did update window, do not count it at all. */ if (acked_bytes == 0 && !(flags & CA_ACK_WIN_UPDATE)) acked_bytes = inet_csk(sk)->icsk_ack.rcv_mss; if (acked_bytes) { ca->acked_bytes_total += acked_bytes; ca->prior_snd_una = tp->snd_una; if (flags & CA_ACK_ECE) ca->acked_bytes_ecn += acked_bytes; } /* Expired RTT */ if (!before(tp->snd_una, ca->next_seq)) { /* For avoiding denominator == 1. */ if (ca->acked_bytes_total == 0) ca->acked_bytes_total = 1; /* alpha = (1 - g) * alpha + g * F */ ca->dctcp_alpha = ca->dctcp_alpha - (ca->dctcp_alpha >> dctcp_shift_g) + (ca->acked_bytes_ecn <