author | Tommaso Pecorella <tommaso.pecorella@unifi.it> |
Wed, 13 Aug 2014 23:46:16 +0200 | |
changeset 10855 | 7ef081ddfc7f |
parent 10652 | dc18deba4502 |
child 10968 | 2d29fee2b7b8 |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7176
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
6694 | 2 |
/* |
3 |
* Copyright (c) 2010 Adrian Sai-wah Tam |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License version 2 as |
|
7 |
* published by the Free Software Foundation; |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
* |
|
18 |
* Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com> |
|
19 |
*/ |
|
20 |
||
21 |
#define NS_LOG_APPEND_CONTEXT \ |
|
22 |
if (m_node) { std::clog << Simulator::Now ().GetSeconds () << " [node " << m_node->GetId () << "] "; } |
|
23 |
||
24 |
#include "tcp-reno.h" |
|
25 |
#include "ns3/log.h" |
|
26 |
#include "ns3/trace-source-accessor.h" |
|
27 |
#include "ns3/simulator.h" |
|
28 |
#include "ns3/abort.h" |
|
29 |
#include "ns3/node.h" |
|
30 |
||
31 |
NS_LOG_COMPONENT_DEFINE ("TcpReno"); |
|
32 |
||
33 |
namespace ns3 { |
|
34 |
||
10652
dc18deba4502
[doxygen] Revert r10410, r10411, r10412
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
10440
diff
changeset
|
35 |
NS_OBJECT_ENSURE_REGISTERED (TcpReno); |
6694 | 36 |
|
37 |
TypeId |
|
38 |
TcpReno::GetTypeId (void) |
|
39 |
{ |
|
40 |
static TypeId tid = TypeId ("ns3::TcpReno") |
|
41 |
.SetParent<TcpSocketBase> () |
|
42 |
.AddConstructor<TcpReno> () |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
43 |
.AddAttribute ("ReTxThreshold", "Threshold for fast retransmit", |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
44 |
UintegerValue (3), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
45 |
MakeUintegerAccessor (&TcpReno::m_retxThresh), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
46 |
MakeUintegerChecker<uint32_t> ()) |
6694 | 47 |
.AddTraceSource ("CongestionWindow", |
48 |
"The TCP connection's congestion window", |
|
49 |
MakeTraceSourceAccessor (&TcpReno::m_cWnd)) |
|
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
50 |
.AddTraceSource ("SlowStartThreshold", |
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
51 |
"TCP slow start threshold (bytes)", |
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
52 |
MakeTraceSourceAccessor (&TcpReno::m_ssThresh)) |
6694 | 53 |
; |
54 |
return tid; |
|
55 |
} |
|
56 |
||
7609
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
57 |
TcpReno::TcpReno (void) : m_retxThresh (3), m_inFastRec (false) |
6694 | 58 |
{ |
59 |
NS_LOG_FUNCTION (this); |
|
60 |
} |
|
61 |
||
62 |
TcpReno::TcpReno (const TcpReno& sock) |
|
63 |
: TcpSocketBase (sock), |
|
64 |
m_cWnd (sock.m_cWnd), |
|
65 |
m_ssThresh (sock.m_ssThresh), |
|
66 |
m_initialCWnd (sock.m_initialCWnd), |
|
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
67 |
m_initialSsThresh (sock.m_initialSsThresh), |
7609
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
68 |
m_retxThresh (sock.m_retxThresh), |
6694 | 69 |
m_inFastRec (false) |
70 |
{ |
|
71 |
NS_LOG_FUNCTION (this); |
|
72 |
NS_LOG_LOGIC ("Invoked the copy constructor"); |
|
73 |
} |
|
74 |
||
75 |
TcpReno::~TcpReno (void) |
|
76 |
{ |
|
77 |
} |
|
78 |
||
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
79 |
/* We initialize m_cWnd from this function, after attributes initialized */ |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
80 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
81 |
TcpReno::Listen (void) |
6694 | 82 |
{ |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
83 |
NS_LOG_FUNCTION (this); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
84 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
85 |
return TcpSocketBase::Listen (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
86 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
87 |
|
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
88 |
/* We initialize m_cWnd from this function, after attributes initialized */ |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
89 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
90 |
TcpReno::Connect (const Address & address) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
91 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
92 |
NS_LOG_FUNCTION (this << address); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
93 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
94 |
return TcpSocketBase::Connect (address); |
6694 | 95 |
} |
96 |
||
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
97 |
/* Limit the size of in-flight data by cwnd and receiver's rxwin */ |
6694 | 98 |
uint32_t |
99 |
TcpReno::Window (void) |
|
100 |
{ |
|
101 |
NS_LOG_FUNCTION (this); |
|
102 |
return std::min (m_rWnd.Get (), m_cWnd.Get ()); |
|
103 |
} |
|
104 |
||
105 |
Ptr<TcpSocketBase> |
|
106 |
TcpReno::Fork (void) |
|
107 |
{ |
|
108 |
return CopyObject<TcpReno> (this); |
|
109 |
} |
|
110 |
||
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
111 |
/* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */ |
6694 | 112 |
void |
113 |
TcpReno::NewAck (const SequenceNumber32& seq) |
|
114 |
{ |
|
115 |
NS_LOG_FUNCTION (this << seq); |
|
116 |
NS_LOG_LOGIC ("TcpReno receieved ACK for seq " << seq << |
|
117 |
" cwnd " << m_cWnd << |
|
118 |
" ssthresh " << m_ssThresh); |
|
119 |
||
120 |
// Check for exit condition of fast recovery |
|
121 |
if (m_inFastRec) |
|
122 |
{ // RFC2001, sec.4; RFC2581, sec.3.2 |
|
123 |
// First new ACK after fast recovery: reset cwnd |
|
124 |
m_cWnd = m_ssThresh; |
|
125 |
m_inFastRec = false; |
|
126 |
NS_LOG_INFO ("Reset cwnd to " << m_cWnd); |
|
127 |
}; |
|
128 |
||
129 |
// Increase of cwnd based on current phase (slow start or congestion avoidance) |
|
130 |
if (m_cWnd < m_ssThresh) |
|
131 |
{ // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1) |
|
132 |
m_cWnd += m_segmentSize; |
|
133 |
NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
134 |
} |
|
135 |
else |
|
136 |
{ // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1) |
|
137 |
// To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd |
|
138 |
double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get (); |
|
139 |
adder = std::max (1.0, adder); |
|
140 |
m_cWnd += static_cast<uint32_t> (adder); |
|
141 |
NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
142 |
} |
|
143 |
||
144 |
// Complete newAck processing |
|
145 |
TcpSocketBase::NewAck (seq); |
|
146 |
} |
|
147 |
||
148 |
// Fast recovery and fast retransmit |
|
149 |
void |
|
150 |
TcpReno::DupAck (const TcpHeader& t, uint32_t count) |
|
151 |
{ |
|
152 |
NS_LOG_FUNCTION (this << "t " << count); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
153 |
if (count == m_retxThresh && !m_inFastRec) |
6694 | 154 |
{ // triple duplicate ack triggers fast retransmit (RFC2581, sec.3.2) |
155 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
156 |
m_cWnd = m_ssThresh + 3 * m_segmentSize; |
|
157 |
m_inFastRec = true; |
|
158 |
NS_LOG_INFO ("Triple dupack. Reset cwnd to " << m_cWnd << ", ssthresh to " << m_ssThresh); |
|
159 |
DoRetransmit (); |
|
160 |
} |
|
161 |
else if (m_inFastRec) |
|
162 |
{ // In fast recovery, inc cwnd for every additional dupack (RFC2581, sec.3.2) |
|
163 |
m_cWnd += m_segmentSize; |
|
164 |
NS_LOG_INFO ("Increased cwnd to " << m_cWnd); |
|
165 |
SendPendingData (m_connected); |
|
166 |
}; |
|
167 |
} |
|
168 |
||
169 |
// Retransmit timeout |
|
170 |
void TcpReno::Retransmit (void) |
|
171 |
{ |
|
172 |
NS_LOG_FUNCTION (this); |
|
173 |
NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ()); |
|
174 |
m_inFastRec = false; |
|
175 |
||
176 |
// If erroneous timeout in closed/timed-wait state, just return |
|
177 |
if (m_state == CLOSED || m_state == TIME_WAIT) return; |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
178 |
// If all data are received (non-closing socket and nothing to send), just return |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
179 |
if (m_state <= ESTABLISHED && m_txBuffer.HeadSequence () >= m_highTxMark) return; |
6694 | 180 |
|
181 |
// According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight |
|
182 |
// size and cwnd is set to 1*MSS, then the lost packet is retransmitted and |
|
183 |
// TCP back to slow start |
|
184 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
185 |
m_cWnd = m_segmentSize; |
|
186 |
m_nextTxSequence = m_txBuffer.HeadSequence (); // Restart from highest Ack |
|
187 |
NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd << |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
188 |
", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence); |
6694 | 189 |
m_rtt->IncreaseMultiplier (); // Double the next RTO |
190 |
DoRetransmit (); // Retransmit the packet |
|
191 |
} |
|
192 |
||
193 |
void |
|
194 |
TcpReno::SetSegSize (uint32_t size) |
|
195 |
{ |
|
196 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetSegSize() cannot change segment size after connection started."); |
|
197 |
m_segmentSize = size; |
|
198 |
} |
|
199 |
||
200 |
void |
|
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
201 |
TcpReno::SetInitialSSThresh (uint32_t threshold) |
6694 | 202 |
{ |
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
203 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetSSThresh() cannot change initial ssThresh after connection started."); |
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
204 |
m_initialSsThresh = threshold; |
6694 | 205 |
} |
206 |
||
207 |
uint32_t |
|
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
208 |
TcpReno::GetInitialSSThresh (void) const |
6694 | 209 |
{ |
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
210 |
return m_initialSsThresh; |
6694 | 211 |
} |
212 |
||
213 |
void |
|
214 |
TcpReno::SetInitialCwnd (uint32_t cwnd) |
|
215 |
{ |
|
216 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetInitialCwnd() cannot change initial cwnd after connection started."); |
|
217 |
m_initialCWnd = cwnd; |
|
218 |
} |
|
219 |
||
220 |
uint32_t |
|
221 |
TcpReno::GetInitialCwnd (void) const |
|
222 |
{ |
|
223 |
return m_initialCWnd; |
|
224 |
} |
|
225 |
||
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
226 |
void |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
227 |
TcpReno::InitializeCwnd (void) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
228 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
229 |
/* |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
230 |
* Initialize congestion window, default to 1 MSS (RFC2001, sec.1) and must |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
231 |
* not be larger than 2 MSS (RFC2581, sec.3.1). Both m_initiaCWnd and |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
232 |
* m_segmentSize are set by the attribute system in ns3::TcpSocket. |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
233 |
*/ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
234 |
m_cWnd = m_initialCWnd * m_segmentSize; |
10855
7ef081ddfc7f
Bug 1831 - TcpSocket SlowStartThreshold is not a TraceSource
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10652
diff
changeset
|
235 |
m_ssThresh = m_initialSsThresh; |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
236 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
237 |
|
6694 | 238 |
} // namespace ns3 |