tcp: make newreno byte instead of acks dependent
[vpp.git] / src / vnet / tcp / tcp_newreno.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vnet/tcp/tcp.h>
17
18 void
19 newreno_congestion (tcp_connection_t * tc)
20 {
21   tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss);
22 }
23
24 void
25 newreno_recovered (tcp_connection_t * tc)
26 {
27   tc->cwnd = tc->ssthresh;
28 }
29
30 void
31 newreno_rcv_ack (tcp_connection_t * tc)
32 {
33   if (tcp_in_slowstart (tc))
34     {
35       tc->cwnd += clib_min (tc->snd_mss, tc->bytes_acked);
36     }
37   else
38     {
39       /* tc->cwnd += clib_max ((tc->snd_mss * tc->snd_mss) / tc->cwnd, 1); */
40       tc->cwnd_acc_bytes += tc->bytes_acked;
41       if (tc->cwnd_acc_bytes >= tc->cwnd)
42         {
43           u32 inc = tc->cwnd_acc_bytes / tc->cwnd;
44           tc->cwnd += inc * tc->snd_mss;
45           tc->cwnd_acc_bytes -= inc * tc->cwnd;
46         }
47     }
48 }
49
50 void
51 newreno_rcv_cong_ack (tcp_connection_t * tc, tcp_cc_ack_t ack_type)
52 {
53   if (ack_type == TCP_CC_DUPACK)
54     {
55       if (!tcp_opts_sack_permitted (tc))
56         tc->cwnd += tc->snd_mss;
57     }
58   else if (ack_type == TCP_CC_PARTIALACK)
59     {
60       /* RFC 6582 Sec. 3.2 */
61       if (!tcp_opts_sack_permitted (&tc->rcv_opts))
62         {
63           /* Deflate the congestion window by the amount of new data
64            * acknowledged by the Cumulative Acknowledgment field.
65            * If the partial ACK acknowledges at least one SMSS of new data,
66            * then add back SMSS bytes to the congestion window. This
67            * artificially inflates the congestion window in order to reflect
68            * the additional segment that has left the network. This "partial
69            * window deflation" attempts to ensure that, when fast recovery
70            * eventually ends, approximately ssthresh amount of data will be
71            * outstanding in the network.*/
72           tc->cwnd = (tc->cwnd > tc->bytes_acked + tc->snd_mss) ?
73             tc->cwnd - tc->bytes_acked : tc->snd_mss;
74           if (tc->bytes_acked > tc->snd_mss)
75             tc->cwnd += tc->snd_mss;
76         }
77     }
78 }
79
80 void
81 newreno_conn_init (tcp_connection_t * tc)
82 {
83   tc->ssthresh = tc->snd_wnd;
84   tc->cwnd = tcp_initial_cwnd (tc);
85 }
86
87 const static tcp_cc_algorithm_t tcp_newreno = {
88   .congestion = newreno_congestion,
89   .recovered = newreno_recovered,
90   .rcv_ack = newreno_rcv_ack,
91   .rcv_cong_ack = newreno_rcv_cong_ack,
92   .init = newreno_conn_init
93 };
94
95 clib_error_t *
96 newreno_init (vlib_main_t * vm)
97 {
98   clib_error_t *error = 0;
99
100   tcp_cc_algo_register (TCP_CC_NEWRENO, &tcp_newreno);
101
102   return error;
103 }
104
105 VLIB_INIT_FUNCTION (newreno_init);
106
107 /*
108  * fd.io coding-style-patch-verification: ON
109  *
110  * Local Variables:
111  * eval: (c-set-style "gnu")
112  * End:
113  */