-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp-congestion-ops.cc
220 lines (189 loc) · 5.68 KB
/
tcp-congestion-ops.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2015 Natale Patriciello <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "tcp-congestion-ops.h"
#include "tcp-socket-base.h"
#include "ns3/log.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("TcpCongestionOps");
NS_OBJECT_ENSURE_REGISTERED (TcpCongestionOps);
TypeId
TcpCongestionOps::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpCongestionOps")
.SetParent<Object> ()
.SetGroupName ("Internet")
;
return tid;
}
TcpCongestionOps::TcpCongestionOps () : Object ()
{
}
TcpCongestionOps::TcpCongestionOps (const TcpCongestionOps &other) : Object (other)
{
}
TcpCongestionOps::~TcpCongestionOps ()
{
}
// RENO
NS_OBJECT_ENSURE_REGISTERED (TcpNewReno);
TypeId
TcpNewReno::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::TcpNewReno")
.SetParent<TcpCongestionOps> ()
.SetGroupName ("Internet")
.AddConstructor<TcpNewReno> ()
;
return tid;
}
TcpNewReno::TcpNewReno (void) : TcpCongestionOps ()
{
NS_LOG_FUNCTION (this);
}
TcpNewReno::TcpNewReno (const TcpNewReno& sock)
: TcpCongestionOps (sock)
{
NS_LOG_FUNCTION (this);
}
TcpNewReno::~TcpNewReno (void)
{
}
/**
* \brief Tcp NewReno slow start algorithm
*
* Defined in RFC 5681 as
*
* > During slow start, a TCP increments cwnd by at most SMSS bytes for
* > each ACK received that cumulatively acknowledges new data. Slow
* > start ends when cwnd exceeds ssthresh (or, optionally, when it
* > reaches it, as noted above) or when congestion is observed. While
* > traditionally TCP implementations have increased cwnd by precisely
* > SMSS bytes upon receipt of an ACK covering new data, we RECOMMEND
* > that TCP implementations increase cwnd, per:
* >
* > cwnd += min (N, SMSS) (2)
* >
* > where N is the number of previously unacknowledged bytes acknowledged
* > in the incoming ACK.
*
* The ns-3 implementation respect the RFC definition. Linux does something
* different:
* \verbatim
u32 tcp_slow_start(struct tcp_sock *tp, u32 acked)
{
u32 cwnd = tp->snd_cwnd + acked;
if (cwnd > tp->snd_ssthresh)
cwnd = tp->snd_ssthresh + 1;
acked -= cwnd - tp->snd_cwnd;
tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
return acked;
}
\endverbatim
*
* As stated, we want to avoid the case when a cumulative ACK increases cWnd more
* than a segment size, but we keep count of how many segments we have ignored,
* and return them.
*
* \param tcb internal congestion state
* \param segmentsAcked count of segments acked
* \return the number of segments not considered for increasing the cWnd
*/
uint32_t
TcpNewReno::SlowStart (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
NS_LOG_FUNCTION (this << tcb << segmentsAcked);
if (segmentsAcked >= 1)
{
tcb->m_cWnd += tcb->m_segmentSize;
NS_LOG_INFO ("In SlowStart, updated to cwnd " << tcb->m_cWnd << " ssthresh " << tcb->m_ssThresh);
return segmentsAcked - 1;
}
return 0;
}
/**
* \brief NewReno congestion avoidance
*
* During congestion avoidance, cwnd is incremented by roughly 1 full-sized
* segment per round-trip time (RTT).
*
* \param tcb internal congestion state
* \param segmentsAcked count of segments acked
*/
void
TcpNewReno::CongestionAvoidance (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
NS_LOG_FUNCTION (this << tcb << segmentsAcked);
if (segmentsAcked > 0)
{
double adder = static_cast<double> (tcb->m_segmentSize * tcb->m_segmentSize) / tcb->m_cWnd.Get ();
adder = std::max (1.0, adder);
tcb->m_cWnd += static_cast<uint32_t> (adder);
NS_LOG_INFO ("In CongAvoid, updated to cwnd " << tcb->m_cWnd <<
" ssthresh " << tcb->m_ssThresh);
}
}
/**
* \brief Try to increase the cWnd following the NewReno specification
*
* \see SlowStart
* \see CongestionAvoidance
*
* \param tcb internal congestion state
* \param segmentsAcked count of segments acked
*/
void
TcpNewReno::IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
NS_LOG_FUNCTION (this << tcb << segmentsAcked);
if (tcb->m_cWnd < tcb->m_ssThresh)
{
//segmentsAcked = SlowStart (tcb, segmentsAcked); //Disabling Slow Start
CongestionAvoidance (tcb, segmentsAcked);
}
if (tcb->m_cWnd >= tcb->m_ssThresh)
{
CongestionAvoidance (tcb, segmentsAcked);
}
/* At this point, we could have segmentsAcked != 0. This because RFC says
* that in slow start, we should increase cWnd by min (N, SMSS); if in
* slow start we receive a cumulative ACK, it counts only for 1 SMSS of
* increase, wasting the others.
*
* // Incorrect assert, I am sorry
* NS_ASSERT (segmentsAcked == 0);
*/
}
std::string
TcpNewReno::GetName () const
{
return "TcpNewReno";
}
uint32_t
TcpNewReno::GetSsThresh (Ptr<const TcpSocketState> state,
uint32_t bytesInFlight)
{
NS_LOG_FUNCTION (this << state << bytesInFlight);
return std::max (2 * state->m_segmentSize, bytesInFlight / 2);
}
Ptr<TcpCongestionOps>
TcpNewReno::Fork ()
{
return CopyObject<TcpNewReno> (this);
}
} // namespace ns3