author | Tommaso Pecorella <tommaso.pecorella@unifi.it> |
Wed, 20 Nov 2013 20:15:02 +0100 | |
changeset 10440 | 1e48ff9185f1 |
parent 10410 | 4d4eb8097fa3 |
child 10652 | dc18deba4502 |
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 |
||
10410
4d4eb8097fa3
doxygen] Suppress "warning: Member NS_OBJECT_ENSURE_REGISTERED is not documented"
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7609
diff
changeset
|
35 |
NS_OBJECT_ENSURE_REGISTERED (TcpReno) |
4d4eb8097fa3
doxygen] Suppress "warning: Member NS_OBJECT_ENSURE_REGISTERED is not documented"
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
7609
diff
changeset
|
36 |
; |
6694 | 37 |
|
38 |
TypeId |
|
39 |
TcpReno::GetTypeId (void) |
|
40 |
{ |
|
41 |
static TypeId tid = TypeId ("ns3::TcpReno") |
|
42 |
.SetParent<TcpSocketBase> () |
|
43 |
.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
|
44 |
.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
|
45 |
UintegerValue (3), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
46 |
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
|
47 |
MakeUintegerChecker<uint32_t> ()) |
6694 | 48 |
.AddTraceSource ("CongestionWindow", |
49 |
"The TCP connection's congestion window", |
|
50 |
MakeTraceSourceAccessor (&TcpReno::m_cWnd)) |
|
51 |
; |
|
52 |
return tid; |
|
53 |
} |
|
54 |
||
7609
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
55 |
TcpReno::TcpReno (void) : m_retxThresh (3), m_inFastRec (false) |
6694 | 56 |
{ |
57 |
NS_LOG_FUNCTION (this); |
|
58 |
} |
|
59 |
||
60 |
TcpReno::TcpReno (const TcpReno& sock) |
|
61 |
: TcpSocketBase (sock), |
|
62 |
m_cWnd (sock.m_cWnd), |
|
63 |
m_ssThresh (sock.m_ssThresh), |
|
64 |
m_initialCWnd (sock.m_initialCWnd), |
|
7609
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
65 |
m_retxThresh (sock.m_retxThresh), |
6694 | 66 |
m_inFastRec (false) |
67 |
{ |
|
68 |
NS_LOG_FUNCTION (this); |
|
69 |
NS_LOG_LOGIC ("Invoked the copy constructor"); |
|
70 |
} |
|
71 |
||
72 |
TcpReno::~TcpReno (void) |
|
73 |
{ |
|
74 |
} |
|
75 |
||
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
76 |
/* 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
|
77 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
78 |
TcpReno::Listen (void) |
6694 | 79 |
{ |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
80 |
NS_LOG_FUNCTION (this); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
81 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
82 |
return TcpSocketBase::Listen (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
83 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
84 |
|
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
85 |
/* 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
|
86 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
87 |
TcpReno::Connect (const Address & address) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
88 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
89 |
NS_LOG_FUNCTION (this << address); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
90 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
91 |
return TcpSocketBase::Connect (address); |
6694 | 92 |
} |
93 |
||
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
94 |
/* Limit the size of in-flight data by cwnd and receiver's rxwin */ |
6694 | 95 |
uint32_t |
96 |
TcpReno::Window (void) |
|
97 |
{ |
|
98 |
NS_LOG_FUNCTION (this); |
|
99 |
return std::min (m_rWnd.Get (), m_cWnd.Get ()); |
|
100 |
} |
|
101 |
||
102 |
Ptr<TcpSocketBase> |
|
103 |
TcpReno::Fork (void) |
|
104 |
{ |
|
105 |
return CopyObject<TcpReno> (this); |
|
106 |
} |
|
107 |
||
10440
1e48ff9185f1
Bug 938 - missing Doxygen in ns-3 (internet model + helper)
Tommaso Pecorella <tommaso.pecorella@unifi.it>
parents:
10410
diff
changeset
|
108 |
/* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */ |
6694 | 109 |
void |
110 |
TcpReno::NewAck (const SequenceNumber32& seq) |
|
111 |
{ |
|
112 |
NS_LOG_FUNCTION (this << seq); |
|
113 |
NS_LOG_LOGIC ("TcpReno receieved ACK for seq " << seq << |
|
114 |
" cwnd " << m_cWnd << |
|
115 |
" ssthresh " << m_ssThresh); |
|
116 |
||
117 |
// Check for exit condition of fast recovery |
|
118 |
if (m_inFastRec) |
|
119 |
{ // RFC2001, sec.4; RFC2581, sec.3.2 |
|
120 |
// First new ACK after fast recovery: reset cwnd |
|
121 |
m_cWnd = m_ssThresh; |
|
122 |
m_inFastRec = false; |
|
123 |
NS_LOG_INFO ("Reset cwnd to " << m_cWnd); |
|
124 |
}; |
|
125 |
||
126 |
// Increase of cwnd based on current phase (slow start or congestion avoidance) |
|
127 |
if (m_cWnd < m_ssThresh) |
|
128 |
{ // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1) |
|
129 |
m_cWnd += m_segmentSize; |
|
130 |
NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
131 |
} |
|
132 |
else |
|
133 |
{ // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1) |
|
134 |
// To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd |
|
135 |
double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get (); |
|
136 |
adder = std::max (1.0, adder); |
|
137 |
m_cWnd += static_cast<uint32_t> (adder); |
|
138 |
NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
139 |
} |
|
140 |
||
141 |
// Complete newAck processing |
|
142 |
TcpSocketBase::NewAck (seq); |
|
143 |
} |
|
144 |
||
145 |
// Fast recovery and fast retransmit |
|
146 |
void |
|
147 |
TcpReno::DupAck (const TcpHeader& t, uint32_t count) |
|
148 |
{ |
|
149 |
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
|
150 |
if (count == m_retxThresh && !m_inFastRec) |
6694 | 151 |
{ // triple duplicate ack triggers fast retransmit (RFC2581, sec.3.2) |
152 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
153 |
m_cWnd = m_ssThresh + 3 * m_segmentSize; |
|
154 |
m_inFastRec = true; |
|
155 |
NS_LOG_INFO ("Triple dupack. Reset cwnd to " << m_cWnd << ", ssthresh to " << m_ssThresh); |
|
156 |
DoRetransmit (); |
|
157 |
} |
|
158 |
else if (m_inFastRec) |
|
159 |
{ // In fast recovery, inc cwnd for every additional dupack (RFC2581, sec.3.2) |
|
160 |
m_cWnd += m_segmentSize; |
|
161 |
NS_LOG_INFO ("Increased cwnd to " << m_cWnd); |
|
162 |
SendPendingData (m_connected); |
|
163 |
}; |
|
164 |
} |
|
165 |
||
166 |
// Retransmit timeout |
|
167 |
void TcpReno::Retransmit (void) |
|
168 |
{ |
|
169 |
NS_LOG_FUNCTION (this); |
|
170 |
NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ()); |
|
171 |
m_inFastRec = false; |
|
172 |
||
173 |
// If erroneous timeout in closed/timed-wait state, just return |
|
174 |
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
|
175 |
// 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
|
176 |
if (m_state <= ESTABLISHED && m_txBuffer.HeadSequence () >= m_highTxMark) return; |
6694 | 177 |
|
178 |
// According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight |
|
179 |
// size and cwnd is set to 1*MSS, then the lost packet is retransmitted and |
|
180 |
// TCP back to slow start |
|
181 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
182 |
m_cWnd = m_segmentSize; |
|
183 |
m_nextTxSequence = m_txBuffer.HeadSequence (); // Restart from highest Ack |
|
184 |
NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd << |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
185 |
", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence); |
6694 | 186 |
m_rtt->IncreaseMultiplier (); // Double the next RTO |
187 |
DoRetransmit (); // Retransmit the packet |
|
188 |
} |
|
189 |
||
190 |
void |
|
191 |
TcpReno::SetSegSize (uint32_t size) |
|
192 |
{ |
|
193 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetSegSize() cannot change segment size after connection started."); |
|
194 |
m_segmentSize = size; |
|
195 |
} |
|
196 |
||
197 |
void |
|
198 |
TcpReno::SetSSThresh (uint32_t threshold) |
|
199 |
{ |
|
200 |
m_ssThresh = threshold; |
|
201 |
} |
|
202 |
||
203 |
uint32_t |
|
204 |
TcpReno::GetSSThresh (void) const |
|
205 |
{ |
|
206 |
return m_ssThresh; |
|
207 |
} |
|
208 |
||
209 |
void |
|
210 |
TcpReno::SetInitialCwnd (uint32_t cwnd) |
|
211 |
{ |
|
212 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetInitialCwnd() cannot change initial cwnd after connection started."); |
|
213 |
m_initialCWnd = cwnd; |
|
214 |
} |
|
215 |
||
216 |
uint32_t |
|
217 |
TcpReno::GetInitialCwnd (void) const |
|
218 |
{ |
|
219 |
return m_initialCWnd; |
|
220 |
} |
|
221 |
||
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
222 |
void |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
223 |
TcpReno::InitializeCwnd (void) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
224 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
225 |
/* |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
226 |
* 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
|
227 |
* 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
|
228 |
* 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
|
229 |
*/ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
230 |
m_cWnd = m_initialCWnd * m_segmentSize; |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
231 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
232 |
|
6694 | 233 |
} // namespace ns3 |