123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- #include <linux/module.h>
- #include <linux/mm.h>
- #include <net/tcp.h>
- #include <linux/inet_diag.h>
- #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;
- u32 loss_cwnd;
- };
- static unsigned int dctcp_shift_g __read_mostly = 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->loss_cwnd = 0;
- ca->ce_state = 0;
- dctcp_reset(tp, ca);
- return;
- }
-
- inet_csk(sk)->icsk_ca_ops = &dctcp_reno;
- INET_ECN_dontxmit(sk);
- }
- static u32 dctcp_ssthresh(struct sock *sk)
- {
- struct dctcp *ca = inet_csk_ca(sk);
- struct tcp_sock *tp = tcp_sk(sk);
- ca->loss_cwnd = tp->snd_cwnd;
- return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->dctcp_alpha) >> 11U), 2U);
- }
- 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);
- if (!ca->ce_state) {
-
- if (inet_csk(sk)->icsk_ack.pending & ICSK_ACK_TIMER)
- __tcp_send_ack(sk, ca->prior_rcv_nxt);
- tcp_enter_quickack_mode(sk);
- }
- 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);
- if (ca->ce_state) {
-
- if (inet_csk(sk)->icsk_ack.pending & ICSK_ACK_TIMER)
- __tcp_send_ack(sk, ca->prior_rcv_nxt);
- tcp_enter_quickack_mode(sk);
- }
- 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 (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;
- }
-
- if (!before(tp->snd_una, ca->next_seq)) {
- u64 bytes_ecn = ca->acked_bytes_ecn;
- u32 alpha = ca->dctcp_alpha;
-
- alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g);
- if (bytes_ecn) {
-
- bytes_ecn <<= (10 - dctcp_shift_g);
- do_div(bytes_ecn, max(1U, ca->acked_bytes_total));
- alpha = min(alpha + (u32)bytes_ecn, DCTCP_MAX_ALPHA);
- }
-
- WRITE_ONCE(ca->dctcp_alpha, alpha);
- dctcp_reset(tp, ca);
- }
- }
- static void dctcp_state(struct sock *sk, u8 new_state)
- {
- if (dctcp_clamp_alpha_on_loss && new_state == TCP_CA_Loss) {
- struct dctcp *ca = inet_csk_ca(sk);
-
- ca->dctcp_alpha = DCTCP_MAX_ALPHA;
- }
- }
- static void dctcp_update_ack_reserved(struct sock *sk, enum tcp_ca_event ev)
- {
- struct dctcp *ca = inet_csk_ca(sk);
- switch (ev) {
- case CA_EVENT_DELAYED_ACK:
- if (!ca->delayed_ack_reserved)
- ca->delayed_ack_reserved = 1;
- break;
- case CA_EVENT_NON_DELAYED_ACK:
- if (ca->delayed_ack_reserved)
- ca->delayed_ack_reserved = 0;
- break;
- default:
-
- break;
- }
- }
- static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
- {
- switch (ev) {
- case CA_EVENT_ECN_IS_CE:
- dctcp_ce_state_0_to_1(sk);
- break;
- case CA_EVENT_ECN_NO_CE:
- dctcp_ce_state_1_to_0(sk);
- break;
- case CA_EVENT_DELAYED_ACK:
- case CA_EVENT_NON_DELAYED_ACK:
- dctcp_update_ack_reserved(sk, ev);
- break;
- default:
-
- break;
- }
- }
- static size_t dctcp_get_info(struct sock *sk, u32 ext, int *attr,
- union tcp_cc_info *info)
- {
- const struct dctcp *ca = inet_csk_ca(sk);
-
- if (ext & (1 << (INET_DIAG_DCTCPINFO - 1)) ||
- ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
- memset(&info->dctcp, 0, sizeof(info->dctcp));
- if (inet_csk(sk)->icsk_ca_ops != &dctcp_reno) {
- info->dctcp.dctcp_enabled = 1;
- info->dctcp.dctcp_ce_state = (u16) ca->ce_state;
- info->dctcp.dctcp_alpha = ca->dctcp_alpha;
- info->dctcp.dctcp_ab_ecn = ca->acked_bytes_ecn;
- info->dctcp.dctcp_ab_tot = ca->acked_bytes_total;
- }
- *attr = INET_DIAG_DCTCPINFO;
- return sizeof(info->dctcp);
- }
- return 0;
- }
- static u32 dctcp_cwnd_undo(struct sock *sk)
- {
- const struct dctcp *ca = inet_csk_ca(sk);
- return max(tcp_sk(sk)->snd_cwnd, ca->loss_cwnd);
- }
- static struct tcp_congestion_ops dctcp __read_mostly = {
- .init = dctcp_init,
- .in_ack_event = dctcp_update_alpha,
- .cwnd_event = dctcp_cwnd_event,
- .ssthresh = dctcp_ssthresh,
- .cong_avoid = tcp_reno_cong_avoid,
- .undo_cwnd = dctcp_cwnd_undo,
- .set_state = dctcp_state,
- .get_info = dctcp_get_info,
- .flags = TCP_CONG_NEEDS_ECN,
- .owner = THIS_MODULE,
- .name = "dctcp",
- };
- static struct tcp_congestion_ops dctcp_reno __read_mostly = {
- .ssthresh = tcp_reno_ssthresh,
- .cong_avoid = tcp_reno_cong_avoid,
- .get_info = dctcp_get_info,
- .owner = THIS_MODULE,
- .name = "dctcp-reno",
- };
- static int __init dctcp_register(void)
- {
- BUILD_BUG_ON(sizeof(struct dctcp) > ICSK_CA_PRIV_SIZE);
- return tcp_register_congestion_control(&dctcp);
- }
- static void __exit dctcp_unregister(void)
- {
- tcp_unregister_congestion_control(&dctcp);
- }
- module_init(dctcp_register);
- module_exit(dctcp_unregister);
- MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
- MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
- MODULE_AUTHOR("Glenn Judd <glenn.judd@morganstanley.com>");
- MODULE_LICENSE("GPL v2");
- MODULE_DESCRIPTION("DataCenter TCP (DCTCP)");
|