0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/mm.h>
0018 #include <linux/module.h>
0019 #include <net/tcp.h>
0020
0021 #define BICTCP_BETA_SCALE 1024
0022
0023
0024 #define BICTCP_B 4
0025
0026
0027
0028
0029 static int fast_convergence = 1;
0030 static int max_increment = 16;
0031 static int low_window = 14;
0032 static int beta = 819;
0033 static int initial_ssthresh;
0034 static int smooth_part = 20;
0035
0036 module_param(fast_convergence, int, 0644);
0037 MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
0038 module_param(max_increment, int, 0644);
0039 MODULE_PARM_DESC(max_increment, "Limit on increment allowed during binary search");
0040 module_param(low_window, int, 0644);
0041 MODULE_PARM_DESC(low_window, "lower bound on congestion window (for TCP friendliness)");
0042 module_param(beta, int, 0644);
0043 MODULE_PARM_DESC(beta, "beta for multiplicative increase");
0044 module_param(initial_ssthresh, int, 0644);
0045 MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
0046 module_param(smooth_part, int, 0644);
0047 MODULE_PARM_DESC(smooth_part, "log(B/(B*Smin))/log(B/(B-1))+B, # of RTT from Wmax-B to Wmax");
0048
0049
0050 struct bictcp {
0051 u32 cnt;
0052 u32 last_max_cwnd;
0053 u32 last_cwnd;
0054 u32 last_time;
0055 u32 epoch_start;
0056 #define ACK_RATIO_SHIFT 4
0057 u32 delayed_ack;
0058 };
0059
0060 static inline void bictcp_reset(struct bictcp *ca)
0061 {
0062 ca->cnt = 0;
0063 ca->last_max_cwnd = 0;
0064 ca->last_cwnd = 0;
0065 ca->last_time = 0;
0066 ca->epoch_start = 0;
0067 ca->delayed_ack = 2 << ACK_RATIO_SHIFT;
0068 }
0069
0070 static void bictcp_init(struct sock *sk)
0071 {
0072 struct bictcp *ca = inet_csk_ca(sk);
0073
0074 bictcp_reset(ca);
0075
0076 if (initial_ssthresh)
0077 tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
0078 }
0079
0080
0081
0082
0083 static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
0084 {
0085 if (ca->last_cwnd == cwnd &&
0086 (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
0087 return;
0088
0089 ca->last_cwnd = cwnd;
0090 ca->last_time = tcp_jiffies32;
0091
0092 if (ca->epoch_start == 0)
0093 ca->epoch_start = tcp_jiffies32;
0094
0095
0096 if (cwnd <= low_window) {
0097 ca->cnt = cwnd;
0098 return;
0099 }
0100
0101
0102 if (cwnd < ca->last_max_cwnd) {
0103 __u32 dist = (ca->last_max_cwnd - cwnd)
0104 / BICTCP_B;
0105
0106 if (dist > max_increment)
0107
0108 ca->cnt = cwnd / max_increment;
0109 else if (dist <= 1U)
0110
0111 ca->cnt = (cwnd * smooth_part) / BICTCP_B;
0112 else
0113
0114 ca->cnt = cwnd / dist;
0115 } else {
0116
0117 if (cwnd < ca->last_max_cwnd + BICTCP_B)
0118
0119 ca->cnt = (cwnd * smooth_part) / BICTCP_B;
0120 else if (cwnd < ca->last_max_cwnd + max_increment*(BICTCP_B-1))
0121
0122 ca->cnt = (cwnd * (BICTCP_B-1))
0123 / (cwnd - ca->last_max_cwnd);
0124 else
0125
0126 ca->cnt = cwnd / max_increment;
0127 }
0128
0129
0130 if (ca->last_max_cwnd == 0) {
0131 if (ca->cnt > 20)
0132 ca->cnt = 20;
0133 }
0134
0135 ca->cnt = (ca->cnt << ACK_RATIO_SHIFT) / ca->delayed_ack;
0136 if (ca->cnt == 0)
0137 ca->cnt = 1;
0138 }
0139
0140 static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
0141 {
0142 struct tcp_sock *tp = tcp_sk(sk);
0143 struct bictcp *ca = inet_csk_ca(sk);
0144
0145 if (!tcp_is_cwnd_limited(sk))
0146 return;
0147
0148 if (tcp_in_slow_start(tp)) {
0149 acked = tcp_slow_start(tp, acked);
0150 if (!acked)
0151 return;
0152 }
0153 bictcp_update(ca, tcp_snd_cwnd(tp));
0154 tcp_cong_avoid_ai(tp, ca->cnt, acked);
0155 }
0156
0157
0158
0159
0160
0161 static u32 bictcp_recalc_ssthresh(struct sock *sk)
0162 {
0163 const struct tcp_sock *tp = tcp_sk(sk);
0164 struct bictcp *ca = inet_csk_ca(sk);
0165
0166 ca->epoch_start = 0;
0167
0168
0169 if (tcp_snd_cwnd(tp) < ca->last_max_cwnd && fast_convergence)
0170 ca->last_max_cwnd = (tcp_snd_cwnd(tp) * (BICTCP_BETA_SCALE + beta))
0171 / (2 * BICTCP_BETA_SCALE);
0172 else
0173 ca->last_max_cwnd = tcp_snd_cwnd(tp);
0174
0175 if (tcp_snd_cwnd(tp) <= low_window)
0176 return max(tcp_snd_cwnd(tp) >> 1U, 2U);
0177 else
0178 return max((tcp_snd_cwnd(tp) * beta) / BICTCP_BETA_SCALE, 2U);
0179 }
0180
0181 static void bictcp_state(struct sock *sk, u8 new_state)
0182 {
0183 if (new_state == TCP_CA_Loss)
0184 bictcp_reset(inet_csk_ca(sk));
0185 }
0186
0187
0188
0189
0190 static void bictcp_acked(struct sock *sk, const struct ack_sample *sample)
0191 {
0192 const struct inet_connection_sock *icsk = inet_csk(sk);
0193
0194 if (icsk->icsk_ca_state == TCP_CA_Open) {
0195 struct bictcp *ca = inet_csk_ca(sk);
0196
0197 ca->delayed_ack += sample->pkts_acked -
0198 (ca->delayed_ack >> ACK_RATIO_SHIFT);
0199 }
0200 }
0201
0202 static struct tcp_congestion_ops bictcp __read_mostly = {
0203 .init = bictcp_init,
0204 .ssthresh = bictcp_recalc_ssthresh,
0205 .cong_avoid = bictcp_cong_avoid,
0206 .set_state = bictcp_state,
0207 .undo_cwnd = tcp_reno_undo_cwnd,
0208 .pkts_acked = bictcp_acked,
0209 .owner = THIS_MODULE,
0210 .name = "bic",
0211 };
0212
0213 static int __init bictcp_register(void)
0214 {
0215 BUILD_BUG_ON(sizeof(struct bictcp) > ICSK_CA_PRIV_SIZE);
0216 return tcp_register_congestion_control(&bictcp);
0217 }
0218
0219 static void __exit bictcp_unregister(void)
0220 {
0221 tcp_unregister_congestion_control(&bictcp);
0222 }
0223
0224 module_init(bictcp_register);
0225 module_exit(bictcp_unregister);
0226
0227 MODULE_AUTHOR("Stephen Hemminger");
0228 MODULE_LICENSE("GPL");
0229 MODULE_DESCRIPTION("BIC TCP");