1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * TCP NV: TCP with Congestion Avoidance
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * TCP-NV is a successor of TCP-Vegas that has been developed to
6*4882a593Smuzhiyun * deal with the issues that occur in modern networks.
7*4882a593Smuzhiyun * Like TCP-Vegas, TCP-NV supports true congestion avoidance,
8*4882a593Smuzhiyun * the ability to detect congestion before packet losses occur.
9*4882a593Smuzhiyun * When congestion (queue buildup) starts to occur, TCP-NV
10*4882a593Smuzhiyun * predicts what the cwnd size should be for the current
11*4882a593Smuzhiyun * throughput and it reduces the cwnd proportionally to
12*4882a593Smuzhiyun * the difference between the current cwnd and the predicted cwnd.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * NV is only recommeneded for traffic within a data center, and when
15*4882a593Smuzhiyun * all the flows are NV (at least those within the data center). This
16*4882a593Smuzhiyun * is due to the inherent unfairness between flows using losses to
17*4882a593Smuzhiyun * detect congestion (congestion control) and those that use queue
18*4882a593Smuzhiyun * buildup to detect congestion (congestion avoidance).
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Note: High NIC coalescence values may lower the performance of NV
21*4882a593Smuzhiyun * due to the increased noise in RTT values. In particular, we have
22*4882a593Smuzhiyun * seen issues with rx-frames values greater than 8.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * TODO:
25*4882a593Smuzhiyun * 1) Add mechanism to deal with reverse congestion.
26*4882a593Smuzhiyun */
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include <linux/mm.h>
29*4882a593Smuzhiyun #include <linux/module.h>
30*4882a593Smuzhiyun #include <linux/math64.h>
31*4882a593Smuzhiyun #include <net/tcp.h>
32*4882a593Smuzhiyun #include <linux/inet_diag.h>
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun /* TCP NV parameters
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * nv_pad Max number of queued packets allowed in network
37*4882a593Smuzhiyun * nv_pad_buffer Do not grow cwnd if this closed to nv_pad
38*4882a593Smuzhiyun * nv_reset_period How often (in) seconds)to reset min_rtt
39*4882a593Smuzhiyun * nv_min_cwnd Don't decrease cwnd below this if there are no losses
40*4882a593Smuzhiyun * nv_cong_dec_mult Decrease cwnd by X% (30%) of congestion when detected
41*4882a593Smuzhiyun * nv_ssthresh_factor On congestion set ssthresh to this * <desired cwnd> / 8
42*4882a593Smuzhiyun * nv_rtt_factor RTT averaging factor
43*4882a593Smuzhiyun * nv_loss_dec_factor Decrease cwnd to this (80%) when losses occur
44*4882a593Smuzhiyun * nv_dec_eval_min_calls Wait this many RTT measurements before dec cwnd
45*4882a593Smuzhiyun * nv_inc_eval_min_calls Wait this many RTT measurements before inc cwnd
46*4882a593Smuzhiyun * nv_ssthresh_eval_min_calls Wait this many RTT measurements before stopping
47*4882a593Smuzhiyun * slow-start due to congestion
48*4882a593Smuzhiyun * nv_stop_rtt_cnt Only grow cwnd for this many RTTs after non-congestion
49*4882a593Smuzhiyun * nv_rtt_min_cnt Wait these many RTTs before making congesion decision
50*4882a593Smuzhiyun * nv_cwnd_growth_rate_neg
51*4882a593Smuzhiyun * nv_cwnd_growth_rate_pos
52*4882a593Smuzhiyun * How quickly to double growth rate (not rate) of cwnd when not
53*4882a593Smuzhiyun * congested. One value (nv_cwnd_growth_rate_neg) for when
54*4882a593Smuzhiyun * rate < 1 pkt/RTT (after losses). The other (nv_cwnd_growth_rate_pos)
55*4882a593Smuzhiyun * otherwise.
56*4882a593Smuzhiyun */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun static int nv_pad __read_mostly = 10;
59*4882a593Smuzhiyun static int nv_pad_buffer __read_mostly = 2;
60*4882a593Smuzhiyun static int nv_reset_period __read_mostly = 5; /* in seconds */
61*4882a593Smuzhiyun static int nv_min_cwnd __read_mostly = 2;
62*4882a593Smuzhiyun static int nv_cong_dec_mult __read_mostly = 30 * 128 / 100; /* = 30% */
63*4882a593Smuzhiyun static int nv_ssthresh_factor __read_mostly = 8; /* = 1 */
64*4882a593Smuzhiyun static int nv_rtt_factor __read_mostly = 128; /* = 1/2*old + 1/2*new */
65*4882a593Smuzhiyun static int nv_loss_dec_factor __read_mostly = 819; /* => 80% */
66*4882a593Smuzhiyun static int nv_cwnd_growth_rate_neg __read_mostly = 8;
67*4882a593Smuzhiyun static int nv_cwnd_growth_rate_pos __read_mostly; /* 0 => fixed like Reno */
68*4882a593Smuzhiyun static int nv_dec_eval_min_calls __read_mostly = 60;
69*4882a593Smuzhiyun static int nv_inc_eval_min_calls __read_mostly = 20;
70*4882a593Smuzhiyun static int nv_ssthresh_eval_min_calls __read_mostly = 30;
71*4882a593Smuzhiyun static int nv_stop_rtt_cnt __read_mostly = 10;
72*4882a593Smuzhiyun static int nv_rtt_min_cnt __read_mostly = 2;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun module_param(nv_pad, int, 0644);
75*4882a593Smuzhiyun MODULE_PARM_DESC(nv_pad, "max queued packets allowed in network");
76*4882a593Smuzhiyun module_param(nv_reset_period, int, 0644);
77*4882a593Smuzhiyun MODULE_PARM_DESC(nv_reset_period, "nv_min_rtt reset period (secs)");
78*4882a593Smuzhiyun module_param(nv_min_cwnd, int, 0644);
79*4882a593Smuzhiyun MODULE_PARM_DESC(nv_min_cwnd, "NV will not decrease cwnd below this value"
80*4882a593Smuzhiyun " without losses");
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /* TCP NV Parameters */
83*4882a593Smuzhiyun struct tcpnv {
84*4882a593Smuzhiyun unsigned long nv_min_rtt_reset_jiffies; /* when to switch to
85*4882a593Smuzhiyun * nv_min_rtt_new */
86*4882a593Smuzhiyun s8 cwnd_growth_factor; /* Current cwnd growth factor,
87*4882a593Smuzhiyun * < 0 => less than 1 packet/RTT */
88*4882a593Smuzhiyun u8 available8;
89*4882a593Smuzhiyun u16 available16;
90*4882a593Smuzhiyun u8 nv_allow_cwnd_growth:1, /* whether cwnd can grow */
91*4882a593Smuzhiyun nv_reset:1, /* whether to reset values */
92*4882a593Smuzhiyun nv_catchup:1; /* whether we are growing because
93*4882a593Smuzhiyun * of temporary cwnd decrease */
94*4882a593Smuzhiyun u8 nv_eval_call_cnt; /* call count since last eval */
95*4882a593Smuzhiyun u8 nv_min_cwnd; /* nv won't make a ca decision if cwnd is
96*4882a593Smuzhiyun * smaller than this. It may grow to handle
97*4882a593Smuzhiyun * TSO, LRO and interrupt coalescence because
98*4882a593Smuzhiyun * with these a small cwnd cannot saturate
99*4882a593Smuzhiyun * the link. Note that this is different from
100*4882a593Smuzhiyun * the file local nv_min_cwnd */
101*4882a593Smuzhiyun u8 nv_rtt_cnt; /* RTTs without making ca decision */;
102*4882a593Smuzhiyun u32 nv_last_rtt; /* last rtt */
103*4882a593Smuzhiyun u32 nv_min_rtt; /* active min rtt. Used to determine slope */
104*4882a593Smuzhiyun u32 nv_min_rtt_new; /* min rtt for future use */
105*4882a593Smuzhiyun u32 nv_base_rtt; /* If non-zero it represents the threshold for
106*4882a593Smuzhiyun * congestion */
107*4882a593Smuzhiyun u32 nv_lower_bound_rtt; /* Used in conjunction with nv_base_rtt. It is
108*4882a593Smuzhiyun * set to 80% of nv_base_rtt. It helps reduce
109*4882a593Smuzhiyun * unfairness between flows */
110*4882a593Smuzhiyun u32 nv_rtt_max_rate; /* max rate seen during current RTT */
111*4882a593Smuzhiyun u32 nv_rtt_start_seq; /* current RTT ends when packet arrives
112*4882a593Smuzhiyun * acking beyond nv_rtt_start_seq */
113*4882a593Smuzhiyun u32 nv_last_snd_una; /* Previous value of tp->snd_una. It is
114*4882a593Smuzhiyun * used to determine bytes acked since last
115*4882a593Smuzhiyun * call to bictcp_acked */
116*4882a593Smuzhiyun u32 nv_no_cong_cnt; /* Consecutive no congestion decisions */
117*4882a593Smuzhiyun };
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun #define NV_INIT_RTT U32_MAX
120*4882a593Smuzhiyun #define NV_MIN_CWND 4
121*4882a593Smuzhiyun #define NV_MIN_CWND_GROW 2
122*4882a593Smuzhiyun #define NV_TSO_CWND_BOUND 80
123*4882a593Smuzhiyun
tcpnv_reset(struct tcpnv * ca,struct sock * sk)124*4882a593Smuzhiyun static inline void tcpnv_reset(struct tcpnv *ca, struct sock *sk)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun ca->nv_reset = 0;
129*4882a593Smuzhiyun ca->nv_no_cong_cnt = 0;
130*4882a593Smuzhiyun ca->nv_rtt_cnt = 0;
131*4882a593Smuzhiyun ca->nv_last_rtt = 0;
132*4882a593Smuzhiyun ca->nv_rtt_max_rate = 0;
133*4882a593Smuzhiyun ca->nv_rtt_start_seq = tp->snd_una;
134*4882a593Smuzhiyun ca->nv_eval_call_cnt = 0;
135*4882a593Smuzhiyun ca->nv_last_snd_una = tp->snd_una;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
tcpnv_init(struct sock * sk)138*4882a593Smuzhiyun static void tcpnv_init(struct sock *sk)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun struct tcpnv *ca = inet_csk_ca(sk);
141*4882a593Smuzhiyun int base_rtt;
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun tcpnv_reset(ca, sk);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /* See if base_rtt is available from socket_ops bpf program.
146*4882a593Smuzhiyun * It is meant to be used in environments, such as communication
147*4882a593Smuzhiyun * within a datacenter, where we have reasonable estimates of
148*4882a593Smuzhiyun * RTTs
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun base_rtt = tcp_call_bpf(sk, BPF_SOCK_OPS_BASE_RTT, 0, NULL);
151*4882a593Smuzhiyun if (base_rtt > 0) {
152*4882a593Smuzhiyun ca->nv_base_rtt = base_rtt;
153*4882a593Smuzhiyun ca->nv_lower_bound_rtt = (base_rtt * 205) >> 8; /* 80% */
154*4882a593Smuzhiyun } else {
155*4882a593Smuzhiyun ca->nv_base_rtt = 0;
156*4882a593Smuzhiyun ca->nv_lower_bound_rtt = 0;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 1;
160*4882a593Smuzhiyun ca->nv_min_rtt_reset_jiffies = jiffies + 2 * HZ;
161*4882a593Smuzhiyun ca->nv_min_rtt = NV_INIT_RTT;
162*4882a593Smuzhiyun ca->nv_min_rtt_new = NV_INIT_RTT;
163*4882a593Smuzhiyun ca->nv_min_cwnd = NV_MIN_CWND;
164*4882a593Smuzhiyun ca->nv_catchup = 0;
165*4882a593Smuzhiyun ca->cwnd_growth_factor = 0;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* If provided, apply upper (base_rtt) and lower (lower_bound_rtt)
169*4882a593Smuzhiyun * bounds to RTT.
170*4882a593Smuzhiyun */
nv_get_bounded_rtt(struct tcpnv * ca,u32 val)171*4882a593Smuzhiyun inline u32 nv_get_bounded_rtt(struct tcpnv *ca, u32 val)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun if (ca->nv_lower_bound_rtt > 0 && val < ca->nv_lower_bound_rtt)
174*4882a593Smuzhiyun return ca->nv_lower_bound_rtt;
175*4882a593Smuzhiyun else if (ca->nv_base_rtt > 0 && val > ca->nv_base_rtt)
176*4882a593Smuzhiyun return ca->nv_base_rtt;
177*4882a593Smuzhiyun else
178*4882a593Smuzhiyun return val;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
tcpnv_cong_avoid(struct sock * sk,u32 ack,u32 acked)181*4882a593Smuzhiyun static void tcpnv_cong_avoid(struct sock *sk, u32 ack, u32 acked)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
184*4882a593Smuzhiyun struct tcpnv *ca = inet_csk_ca(sk);
185*4882a593Smuzhiyun u32 cnt;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (!tcp_is_cwnd_limited(sk))
188*4882a593Smuzhiyun return;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /* Only grow cwnd if NV has not detected congestion */
191*4882a593Smuzhiyun if (!ca->nv_allow_cwnd_growth)
192*4882a593Smuzhiyun return;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (tcp_in_slow_start(tp)) {
195*4882a593Smuzhiyun acked = tcp_slow_start(tp, acked);
196*4882a593Smuzhiyun if (!acked)
197*4882a593Smuzhiyun return;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun if (ca->cwnd_growth_factor < 0) {
201*4882a593Smuzhiyun cnt = tp->snd_cwnd << -ca->cwnd_growth_factor;
202*4882a593Smuzhiyun tcp_cong_avoid_ai(tp, cnt, acked);
203*4882a593Smuzhiyun } else {
204*4882a593Smuzhiyun cnt = max(4U, tp->snd_cwnd >> ca->cwnd_growth_factor);
205*4882a593Smuzhiyun tcp_cong_avoid_ai(tp, cnt, acked);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
tcpnv_recalc_ssthresh(struct sock * sk)209*4882a593Smuzhiyun static u32 tcpnv_recalc_ssthresh(struct sock *sk)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun const struct tcp_sock *tp = tcp_sk(sk);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun return max((tp->snd_cwnd * nv_loss_dec_factor) >> 10, 2U);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun
tcpnv_state(struct sock * sk,u8 new_state)216*4882a593Smuzhiyun static void tcpnv_state(struct sock *sk, u8 new_state)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun struct tcpnv *ca = inet_csk_ca(sk);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (new_state == TCP_CA_Open && ca->nv_reset) {
221*4882a593Smuzhiyun tcpnv_reset(ca, sk);
222*4882a593Smuzhiyun } else if (new_state == TCP_CA_Loss || new_state == TCP_CA_CWR ||
223*4882a593Smuzhiyun new_state == TCP_CA_Recovery) {
224*4882a593Smuzhiyun ca->nv_reset = 1;
225*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 0;
226*4882a593Smuzhiyun if (new_state == TCP_CA_Loss) {
227*4882a593Smuzhiyun /* Reset cwnd growth factor to Reno value */
228*4882a593Smuzhiyun if (ca->cwnd_growth_factor > 0)
229*4882a593Smuzhiyun ca->cwnd_growth_factor = 0;
230*4882a593Smuzhiyun /* Decrease growth rate if allowed */
231*4882a593Smuzhiyun if (nv_cwnd_growth_rate_neg > 0 &&
232*4882a593Smuzhiyun ca->cwnd_growth_factor > -8)
233*4882a593Smuzhiyun ca->cwnd_growth_factor--;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* Do congestion avoidance calculations for TCP-NV
239*4882a593Smuzhiyun */
tcpnv_acked(struct sock * sk,const struct ack_sample * sample)240*4882a593Smuzhiyun static void tcpnv_acked(struct sock *sk, const struct ack_sample *sample)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun const struct inet_connection_sock *icsk = inet_csk(sk);
243*4882a593Smuzhiyun struct tcp_sock *tp = tcp_sk(sk);
244*4882a593Smuzhiyun struct tcpnv *ca = inet_csk_ca(sk);
245*4882a593Smuzhiyun unsigned long now = jiffies;
246*4882a593Smuzhiyun u64 rate64;
247*4882a593Smuzhiyun u32 rate, max_win, cwnd_by_slope;
248*4882a593Smuzhiyun u32 avg_rtt;
249*4882a593Smuzhiyun u32 bytes_acked = 0;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* Some calls are for duplicates without timetamps */
252*4882a593Smuzhiyun if (sample->rtt_us < 0)
253*4882a593Smuzhiyun return;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /* If not in TCP_CA_Open or TCP_CA_Disorder states, skip. */
256*4882a593Smuzhiyun if (icsk->icsk_ca_state != TCP_CA_Open &&
257*4882a593Smuzhiyun icsk->icsk_ca_state != TCP_CA_Disorder)
258*4882a593Smuzhiyun return;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /* Stop cwnd growth if we were in catch up mode */
261*4882a593Smuzhiyun if (ca->nv_catchup && tp->snd_cwnd >= nv_min_cwnd) {
262*4882a593Smuzhiyun ca->nv_catchup = 0;
263*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 0;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun bytes_acked = tp->snd_una - ca->nv_last_snd_una;
267*4882a593Smuzhiyun ca->nv_last_snd_una = tp->snd_una;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun if (sample->in_flight == 0)
270*4882a593Smuzhiyun return;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun /* Calculate moving average of RTT */
273*4882a593Smuzhiyun if (nv_rtt_factor > 0) {
274*4882a593Smuzhiyun if (ca->nv_last_rtt > 0) {
275*4882a593Smuzhiyun avg_rtt = (((u64)sample->rtt_us) * nv_rtt_factor +
276*4882a593Smuzhiyun ((u64)ca->nv_last_rtt)
277*4882a593Smuzhiyun * (256 - nv_rtt_factor)) >> 8;
278*4882a593Smuzhiyun } else {
279*4882a593Smuzhiyun avg_rtt = sample->rtt_us;
280*4882a593Smuzhiyun ca->nv_min_rtt = avg_rtt << 1;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun ca->nv_last_rtt = avg_rtt;
283*4882a593Smuzhiyun } else {
284*4882a593Smuzhiyun avg_rtt = sample->rtt_us;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* rate in 100's bits per second */
288*4882a593Smuzhiyun rate64 = ((u64)sample->in_flight) * 80000;
289*4882a593Smuzhiyun do_div(rate64, avg_rtt ?: 1);
290*4882a593Smuzhiyun rate = (u32)rate64;
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /* Remember the maximum rate seen during this RTT
293*4882a593Smuzhiyun * Note: It may be more than one RTT. This function should be
294*4882a593Smuzhiyun * called at least nv_dec_eval_min_calls times.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun if (ca->nv_rtt_max_rate < rate)
297*4882a593Smuzhiyun ca->nv_rtt_max_rate = rate;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /* We have valid information, increment counter */
300*4882a593Smuzhiyun if (ca->nv_eval_call_cnt < 255)
301*4882a593Smuzhiyun ca->nv_eval_call_cnt++;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /* Apply bounds to rtt. Only used to update min_rtt */
304*4882a593Smuzhiyun avg_rtt = nv_get_bounded_rtt(ca, avg_rtt);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* update min rtt if necessary */
307*4882a593Smuzhiyun if (avg_rtt < ca->nv_min_rtt)
308*4882a593Smuzhiyun ca->nv_min_rtt = avg_rtt;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /* update future min_rtt if necessary */
311*4882a593Smuzhiyun if (avg_rtt < ca->nv_min_rtt_new)
312*4882a593Smuzhiyun ca->nv_min_rtt_new = avg_rtt;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* nv_min_rtt is updated with the minimum (possibley averaged) rtt
315*4882a593Smuzhiyun * seen in the last sysctl_tcp_nv_reset_period seconds (i.e. a
316*4882a593Smuzhiyun * warm reset). This new nv_min_rtt will be continued to be updated
317*4882a593Smuzhiyun * and be used for another sysctl_tcp_nv_reset_period seconds,
318*4882a593Smuzhiyun * when it will be updated again.
319*4882a593Smuzhiyun * In practice we introduce some randomness, so the actual period used
320*4882a593Smuzhiyun * is chosen randomly from the range:
321*4882a593Smuzhiyun * [sysctl_tcp_nv_reset_period*3/4, sysctl_tcp_nv_reset_period*5/4)
322*4882a593Smuzhiyun */
323*4882a593Smuzhiyun if (time_after_eq(now, ca->nv_min_rtt_reset_jiffies)) {
324*4882a593Smuzhiyun unsigned char rand;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun ca->nv_min_rtt = ca->nv_min_rtt_new;
327*4882a593Smuzhiyun ca->nv_min_rtt_new = NV_INIT_RTT;
328*4882a593Smuzhiyun get_random_bytes(&rand, 1);
329*4882a593Smuzhiyun ca->nv_min_rtt_reset_jiffies =
330*4882a593Smuzhiyun now + ((nv_reset_period * (384 + rand) * HZ) >> 9);
331*4882a593Smuzhiyun /* Every so often we decrease ca->nv_min_cwnd in case previous
332*4882a593Smuzhiyun * value is no longer accurate.
333*4882a593Smuzhiyun */
334*4882a593Smuzhiyun ca->nv_min_cwnd = max(ca->nv_min_cwnd / 2, NV_MIN_CWND);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* Once per RTT check if we need to do congestion avoidance */
338*4882a593Smuzhiyun if (before(ca->nv_rtt_start_seq, tp->snd_una)) {
339*4882a593Smuzhiyun ca->nv_rtt_start_seq = tp->snd_nxt;
340*4882a593Smuzhiyun if (ca->nv_rtt_cnt < 0xff)
341*4882a593Smuzhiyun /* Increase counter for RTTs without CA decision */
342*4882a593Smuzhiyun ca->nv_rtt_cnt++;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* If this function is only called once within an RTT
345*4882a593Smuzhiyun * the cwnd is probably too small (in some cases due to
346*4882a593Smuzhiyun * tso, lro or interrupt coalescence), so we increase
347*4882a593Smuzhiyun * ca->nv_min_cwnd.
348*4882a593Smuzhiyun */
349*4882a593Smuzhiyun if (ca->nv_eval_call_cnt == 1 &&
350*4882a593Smuzhiyun bytes_acked >= (ca->nv_min_cwnd - 1) * tp->mss_cache &&
351*4882a593Smuzhiyun ca->nv_min_cwnd < (NV_TSO_CWND_BOUND + 1)) {
352*4882a593Smuzhiyun ca->nv_min_cwnd = min(ca->nv_min_cwnd
353*4882a593Smuzhiyun + NV_MIN_CWND_GROW,
354*4882a593Smuzhiyun NV_TSO_CWND_BOUND + 1);
355*4882a593Smuzhiyun ca->nv_rtt_start_seq = tp->snd_nxt +
356*4882a593Smuzhiyun ca->nv_min_cwnd * tp->mss_cache;
357*4882a593Smuzhiyun ca->nv_eval_call_cnt = 0;
358*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 1;
359*4882a593Smuzhiyun return;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun /* Find the ideal cwnd for current rate from slope
363*4882a593Smuzhiyun * slope = 80000.0 * mss / nv_min_rtt
364*4882a593Smuzhiyun * cwnd_by_slope = nv_rtt_max_rate / slope
365*4882a593Smuzhiyun */
366*4882a593Smuzhiyun cwnd_by_slope = (u32)
367*4882a593Smuzhiyun div64_u64(((u64)ca->nv_rtt_max_rate) * ca->nv_min_rtt,
368*4882a593Smuzhiyun 80000ULL * tp->mss_cache);
369*4882a593Smuzhiyun max_win = cwnd_by_slope + nv_pad;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun /* If cwnd > max_win, decrease cwnd
372*4882a593Smuzhiyun * if cwnd < max_win, grow cwnd
373*4882a593Smuzhiyun * else leave the same
374*4882a593Smuzhiyun */
375*4882a593Smuzhiyun if (tp->snd_cwnd > max_win) {
376*4882a593Smuzhiyun /* there is congestion, check that it is ok
377*4882a593Smuzhiyun * to make a CA decision
378*4882a593Smuzhiyun * 1. We should have at least nv_dec_eval_min_calls
379*4882a593Smuzhiyun * data points before making a CA decision
380*4882a593Smuzhiyun * 2. We only make a congesion decision after
381*4882a593Smuzhiyun * nv_rtt_min_cnt RTTs
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun if (ca->nv_rtt_cnt < nv_rtt_min_cnt) {
384*4882a593Smuzhiyun return;
385*4882a593Smuzhiyun } else if (tp->snd_ssthresh == TCP_INFINITE_SSTHRESH) {
386*4882a593Smuzhiyun if (ca->nv_eval_call_cnt <
387*4882a593Smuzhiyun nv_ssthresh_eval_min_calls)
388*4882a593Smuzhiyun return;
389*4882a593Smuzhiyun /* otherwise we will decrease cwnd */
390*4882a593Smuzhiyun } else if (ca->nv_eval_call_cnt <
391*4882a593Smuzhiyun nv_dec_eval_min_calls) {
392*4882a593Smuzhiyun if (ca->nv_allow_cwnd_growth &&
393*4882a593Smuzhiyun ca->nv_rtt_cnt > nv_stop_rtt_cnt)
394*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 0;
395*4882a593Smuzhiyun return;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun /* We have enough data to determine we are congested */
399*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 0;
400*4882a593Smuzhiyun tp->snd_ssthresh =
401*4882a593Smuzhiyun (nv_ssthresh_factor * max_win) >> 3;
402*4882a593Smuzhiyun if (tp->snd_cwnd - max_win > 2) {
403*4882a593Smuzhiyun /* gap > 2, we do exponential cwnd decrease */
404*4882a593Smuzhiyun int dec;
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun dec = max(2U, ((tp->snd_cwnd - max_win) *
407*4882a593Smuzhiyun nv_cong_dec_mult) >> 7);
408*4882a593Smuzhiyun tp->snd_cwnd -= dec;
409*4882a593Smuzhiyun } else if (nv_cong_dec_mult > 0) {
410*4882a593Smuzhiyun tp->snd_cwnd = max_win;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun if (ca->cwnd_growth_factor > 0)
413*4882a593Smuzhiyun ca->cwnd_growth_factor = 0;
414*4882a593Smuzhiyun ca->nv_no_cong_cnt = 0;
415*4882a593Smuzhiyun } else if (tp->snd_cwnd <= max_win - nv_pad_buffer) {
416*4882a593Smuzhiyun /* There is no congestion, grow cwnd if allowed*/
417*4882a593Smuzhiyun if (ca->nv_eval_call_cnt < nv_inc_eval_min_calls)
418*4882a593Smuzhiyun return;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun ca->nv_allow_cwnd_growth = 1;
421*4882a593Smuzhiyun ca->nv_no_cong_cnt++;
422*4882a593Smuzhiyun if (ca->cwnd_growth_factor < 0 &&
423*4882a593Smuzhiyun nv_cwnd_growth_rate_neg > 0 &&
424*4882a593Smuzhiyun ca->nv_no_cong_cnt > nv_cwnd_growth_rate_neg) {
425*4882a593Smuzhiyun ca->cwnd_growth_factor++;
426*4882a593Smuzhiyun ca->nv_no_cong_cnt = 0;
427*4882a593Smuzhiyun } else if (ca->cwnd_growth_factor >= 0 &&
428*4882a593Smuzhiyun nv_cwnd_growth_rate_pos > 0 &&
429*4882a593Smuzhiyun ca->nv_no_cong_cnt >
430*4882a593Smuzhiyun nv_cwnd_growth_rate_pos) {
431*4882a593Smuzhiyun ca->cwnd_growth_factor++;
432*4882a593Smuzhiyun ca->nv_no_cong_cnt = 0;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun } else {
435*4882a593Smuzhiyun /* cwnd is in-between, so do nothing */
436*4882a593Smuzhiyun return;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /* update state */
440*4882a593Smuzhiyun ca->nv_eval_call_cnt = 0;
441*4882a593Smuzhiyun ca->nv_rtt_cnt = 0;
442*4882a593Smuzhiyun ca->nv_rtt_max_rate = 0;
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /* Don't want to make cwnd < nv_min_cwnd
445*4882a593Smuzhiyun * (it wasn't before, if it is now is because nv
446*4882a593Smuzhiyun * decreased it).
447*4882a593Smuzhiyun */
448*4882a593Smuzhiyun if (tp->snd_cwnd < nv_min_cwnd)
449*4882a593Smuzhiyun tp->snd_cwnd = nv_min_cwnd;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /* Extract info for Tcp socket info provided via netlink */
tcpnv_get_info(struct sock * sk,u32 ext,int * attr,union tcp_cc_info * info)454*4882a593Smuzhiyun static size_t tcpnv_get_info(struct sock *sk, u32 ext, int *attr,
455*4882a593Smuzhiyun union tcp_cc_info *info)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun const struct tcpnv *ca = inet_csk_ca(sk);
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
460*4882a593Smuzhiyun info->vegas.tcpv_enabled = 1;
461*4882a593Smuzhiyun info->vegas.tcpv_rttcnt = ca->nv_rtt_cnt;
462*4882a593Smuzhiyun info->vegas.tcpv_rtt = ca->nv_last_rtt;
463*4882a593Smuzhiyun info->vegas.tcpv_minrtt = ca->nv_min_rtt;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun *attr = INET_DIAG_VEGASINFO;
466*4882a593Smuzhiyun return sizeof(struct tcpvegas_info);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun return 0;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun static struct tcp_congestion_ops tcpnv __read_mostly = {
472*4882a593Smuzhiyun .init = tcpnv_init,
473*4882a593Smuzhiyun .ssthresh = tcpnv_recalc_ssthresh,
474*4882a593Smuzhiyun .cong_avoid = tcpnv_cong_avoid,
475*4882a593Smuzhiyun .set_state = tcpnv_state,
476*4882a593Smuzhiyun .undo_cwnd = tcp_reno_undo_cwnd,
477*4882a593Smuzhiyun .pkts_acked = tcpnv_acked,
478*4882a593Smuzhiyun .get_info = tcpnv_get_info,
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun .owner = THIS_MODULE,
481*4882a593Smuzhiyun .name = "nv",
482*4882a593Smuzhiyun };
483*4882a593Smuzhiyun
tcpnv_register(void)484*4882a593Smuzhiyun static int __init tcpnv_register(void)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct tcpnv) > ICSK_CA_PRIV_SIZE);
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun return tcp_register_congestion_control(&tcpnv);
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun
tcpnv_unregister(void)491*4882a593Smuzhiyun static void __exit tcpnv_unregister(void)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun tcp_unregister_congestion_control(&tcpnv);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun module_init(tcpnv_register);
497*4882a593Smuzhiyun module_exit(tcpnv_unregister);
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun MODULE_AUTHOR("Lawrence Brakmo");
500*4882a593Smuzhiyun MODULE_LICENSE("GPL");
501*4882a593Smuzhiyun MODULE_DESCRIPTION("TCP NV");
502*4882a593Smuzhiyun MODULE_VERSION("1.0");
503