VPP-598: tcp stack initial commit
[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->prev_ssthresh = tc->ssthresh;
22   tc->ssthresh = clib_max (tcp_flight_size (tc) / 2, 2 * tc->snd_mss);
23 }
24
25 void
26 newreno_recovered (tcp_connection_t * tc)
27 {
28   tc->cwnd = tc->ssthresh;
29 }
30
31 void
32 newreno_rcv_ack (tcp_connection_t * tc)
33 {
34   if (tcp_in_slowstart (tc))
35     {
36       tc->cwnd += clib_min (tc->snd_mss, tc->bytes_acked);
37     }
38   else
39     {
40       /* Round up to 1 if needed */
41       tc->cwnd += clib_max (tc->snd_mss * tc->snd_mss / tc->cwnd, 1);
42     }
43 }
44
45 void
46 newreno_rcv_cong_ack (tcp_connection_t * tc, tcp_cc_ack_t ack_type)
47 {
48   if (ack_type == TCP_CC_DUPACK)
49     {
50       tc->cwnd += tc->snd_mss;
51     }
52   else if (ack_type == TCP_CC_PARTIALACK)
53     {
54       tc->cwnd -= tc->bytes_acked;
55       if (tc->bytes_acked > tc->snd_mss)
56         tc->bytes_acked += tc->snd_mss;
57     }
58 }
59
60 void
61 newreno_conn_init (tcp_connection_t * tc)
62 {
63   tc->ssthresh = tc->snd_wnd;
64   tc->cwnd = tcp_initial_cwnd (tc);
65 }
66
67 const static tcp_cc_algorithm_t tcp_newreno = {
68   .congestion = newreno_congestion,
69   .recovered = newreno_recovered,
70   .rcv_ack = newreno_rcv_ack,
71   .rcv_cong_ack = newreno_rcv_cong_ack,
72   .init = newreno_conn_init
73 };
74
75 clib_error_t *
76 newreno_init (vlib_main_t * vm)
77 {
78   clib_error_t *error = 0;
79
80   tcp_cc_algo_register (TCP_CC_NEWRENO, &tcp_newreno);
81
82   return error;
83 }
84
85 VLIB_INIT_FUNCTION (newreno_init);
86
87 /*
88  * fd.io coding-style-patch-verification: ON
89  *
90  * Local Variables:
91  * eval: (c-set-style "gnu")
92  * End:
93  */