tcp: fix cwnd limited checking to improve congestion control
[deliverable/linux.git] / net / ipv4 / tcp_hybla.c
CommitLineData
835b3f0c
DL
1/*
2 * TCP HYBLA
3 *
4 * TCP-HYBLA Congestion control algorithm, based on:
5 * C.Caini, R.Firrincieli, "TCP-Hybla: A TCP Enhancement
6 * for Heterogeneous Networks",
7 * International Journal on satellite Communications,
8 * September 2004
9 * Daniele Lacamera
10 * root at danielinux.net
11 */
12
835b3f0c
DL
13#include <linux/module.h>
14#include <net/tcp.h>
15
16/* Tcp Hybla structure. */
17struct hybla {
a2a385d6 18 bool hybla_en;
835b3f0c
DL
19 u32 snd_cwnd_cents; /* Keeps increment values when it is <1, <<7 */
20 u32 rho; /* Rho parameter, integer part */
21 u32 rho2; /* Rho * Rho, integer part */
22 u32 rho_3ls; /* Rho parameter, <<3 */
23 u32 rho2_7ls; /* Rho^2, <<7 */
740b0f18 24 u32 minrtt_us; /* Minimum smoothed round trip time value seen */
835b3f0c
DL
25};
26
a2a385d6 27/* Hybla reference round trip time (default= 1/40 sec = 25 ms), in ms */
835b3f0c
DL
28static int rtt0 = 25;
29module_param(rtt0, int, 0644);
30MODULE_PARM_DESC(rtt0, "reference rout trip time (ms)");
31
32
33/* This is called to refresh values for hybla parameters */
6687e988 34static inline void hybla_recalc_param (struct sock *sk)
835b3f0c 35{
6687e988 36 struct hybla *ca = inet_csk_ca(sk);
835b3f0c 37
740b0f18
ED
38 ca->rho_3ls = max_t(u32,
39 tcp_sk(sk)->srtt_us / (rtt0 * USEC_PER_MSEC),
40 8U);
835b3f0c
DL
41 ca->rho = ca->rho_3ls >> 3;
42 ca->rho2_7ls = (ca->rho_3ls * ca->rho_3ls) << 1;
a2a385d6 43 ca->rho2 = ca->rho2_7ls >> 7;
835b3f0c
DL
44}
45
6687e988 46static void hybla_init(struct sock *sk)
835b3f0c 47{
6687e988
ACM
48 struct tcp_sock *tp = tcp_sk(sk);
49 struct hybla *ca = inet_csk_ca(sk);
835b3f0c
DL
50
51 ca->rho = 0;
52 ca->rho2 = 0;
53 ca->rho_3ls = 0;
54 ca->rho2_7ls = 0;
55 ca->snd_cwnd_cents = 0;
a2a385d6 56 ca->hybla_en = true;
835b3f0c
DL
57 tp->snd_cwnd = 2;
58 tp->snd_cwnd_clamp = 65535;
59
60 /* 1st Rho measurement based on initial srtt */
6687e988 61 hybla_recalc_param(sk);
835b3f0c
DL
62
63 /* set minimum rtt as this is the 1st ever seen */
740b0f18 64 ca->minrtt_us = tp->srtt_us;
835b3f0c
DL
65 tp->snd_cwnd = ca->rho;
66}
67
6687e988 68static void hybla_state(struct sock *sk, u8 ca_state)
835b3f0c 69{
6687e988 70 struct hybla *ca = inet_csk_ca(sk);
a2a385d6 71
835b3f0c
DL
72 ca->hybla_en = (ca_state == TCP_CA_Open);
73}
74
75static inline u32 hybla_fraction(u32 odds)
76{
77 static const u32 fractions[] = {
78 128, 139, 152, 165, 181, 197, 215, 234,
79 };
80
81 return (odds < ARRAY_SIZE(fractions)) ? fractions[odds] : 128;
82}
83
84/* TCP Hybla main routine.
85 * This is the algorithm behavior:
86 * o Recalc Hybla parameters if min_rtt has changed
87 * o Give cwnd a new value based on the model proposed
88 * o remember increments <1
89 */
9f9843a7
YC
90static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked,
91 u32 in_flight)
835b3f0c 92{
6687e988
ACM
93 struct tcp_sock *tp = tcp_sk(sk);
94 struct hybla *ca = inet_csk_ca(sk);
835b3f0c
DL
95 u32 increment, odd, rho_fractions;
96 int is_slowstart = 0;
97
98 /* Recalculate rho only if this srtt is the lowest */
740b0f18 99 if (tp->srtt_us < ca->minrtt_us) {
6687e988 100 hybla_recalc_param(sk);
740b0f18 101 ca->minrtt_us = tp->srtt_us;
835b3f0c
DL
102 }
103
f4805ede
SH
104 if (!tcp_is_cwnd_limited(sk, in_flight))
105 return;
106
ab59859d 107 if (!ca->hybla_en) {
9f9843a7 108 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
ab59859d
HH
109 return;
110 }
835b3f0c 111
835b3f0c 112 if (ca->rho == 0)
6687e988 113 hybla_recalc_param(sk);
835b3f0c
DL
114
115 rho_fractions = ca->rho_3ls - (ca->rho << 3);
116
117 if (tp->snd_cwnd < tp->snd_ssthresh) {
118 /*
119 * slow start
120 * INC = 2^RHO - 1
121 * This is done by splitting the rho parameter
122 * into 2 parts: an integer part and a fraction part.
123 * Inrement<<7 is estimated by doing:
124 * [2^(int+fract)]<<7
125 * that is equal to:
126 * (2^int) * [(2^fract) <<7]
127 * 2^int is straightly computed as 1<<int,
128 * while we will use hybla_slowstart_fraction_increment() to
129 * calculate 2^fract in a <<7 value.
130 */
131 is_slowstart = 1;
edafe502
DL
132 increment = ((1 << min(ca->rho, 16U)) *
133 hybla_fraction(rho_fractions)) - 128;
835b3f0c
DL
134 } else {
135 /*
136 * congestion avoidance
137 * INC = RHO^2 / W
138 * as long as increment is estimated as (rho<<7)/window
139 * it already is <<7 and we can easily count its fractions.
140 */
141 increment = ca->rho2_7ls / tp->snd_cwnd;
142 if (increment < 128)
143 tp->snd_cwnd_cnt++;
144 }
145
146 odd = increment % 128;
147 tp->snd_cwnd += increment >> 7;
148 ca->snd_cwnd_cents += odd;
149
150 /* check when fractions goes >=128 and increase cwnd by 1. */
2de979bd 151 while (ca->snd_cwnd_cents >= 128) {
835b3f0c
DL
152 tp->snd_cwnd++;
153 ca->snd_cwnd_cents -= 128;
154 tp->snd_cwnd_cnt = 0;
155 }
9d2c27e1
DL
156 /* check when cwnd has not been incremented for a while */
157 if (increment == 0 && odd == 0 && tp->snd_cwnd_cnt >= tp->snd_cwnd) {
158 tp->snd_cwnd++;
159 tp->snd_cwnd_cnt = 0;
160 }
835b3f0c
DL
161 /* clamp down slowstart cwnd to ssthresh value. */
162 if (is_slowstart)
163 tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh);
164
165 tp->snd_cwnd = min_t(u32, tp->snd_cwnd, tp->snd_cwnd_clamp);
166}
167
a252bebe 168static struct tcp_congestion_ops tcp_hybla __read_mostly = {
835b3f0c
DL
169 .init = hybla_init,
170 .ssthresh = tcp_reno_ssthresh,
835b3f0c
DL
171 .cong_avoid = hybla_cong_avoid,
172 .set_state = hybla_state,
173
174 .owner = THIS_MODULE,
175 .name = "hybla"
176};
177
178static int __init hybla_register(void)
179{
74975d40 180 BUILD_BUG_ON(sizeof(struct hybla) > ICSK_CA_PRIV_SIZE);
835b3f0c
DL
181 return tcp_register_congestion_control(&tcp_hybla);
182}
183
184static void __exit hybla_unregister(void)
185{
186 tcp_unregister_congestion_control(&tcp_hybla);
187}
188
189module_init(hybla_register);
190module_exit(hybla_unregister);
191
192MODULE_AUTHOR("Daniele Lacamera");
193MODULE_LICENSE("GPL");
194MODULE_DESCRIPTION("TCP Hybla");
This page took 0.686093 seconds and 5 git commands to generate.