author | Adrian S Tam <adrian.sw.tam@gmail.com> |
Thu, 01 Dec 2011 21:41:16 -0500 | |
changeset 7609 | 7193df6ebc5d |
parent 7608 | de67936e4017 |
child 10410 | 4d4eb8097fa3 |
permissions | -rw-r--r-- |
7385
10beb0e53130
standardize emacs c++ mode comments
Vedran Miletić <rivanvx@gmail.com>
parents:
7382
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-newreno.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 ("TcpNewReno"); |
|
32 |
||
33 |
namespace ns3 { |
|
34 |
||
35 |
NS_OBJECT_ENSURE_REGISTERED (TcpNewReno); |
|
36 |
||
37 |
TypeId |
|
38 |
TcpNewReno::GetTypeId (void) |
|
39 |
{ |
|
40 |
static TypeId tid = TypeId ("ns3::TcpNewReno") |
|
41 |
.SetParent<TcpSocketBase> () |
|
42 |
.AddConstructor<TcpNewReno> () |
|
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 (&TcpNewReno::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> ()) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
47 |
.AddAttribute ("LimitedTransmit", "Enable limited transmit", |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
48 |
BooleanValue (false), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
49 |
MakeBooleanAccessor (&TcpNewReno::m_limitedTx), |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
50 |
MakeBooleanChecker ()) |
6694 | 51 |
.AddTraceSource ("CongestionWindow", |
52 |
"The TCP connection's congestion window", |
|
53 |
MakeTraceSourceAccessor (&TcpNewReno::m_cWnd)) |
|
54 |
; |
|
55 |
return tid; |
|
56 |
} |
|
57 |
||
7609
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
58 |
TcpNewReno::TcpNewReno (void) |
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
59 |
: m_retxThresh (3), // mute valgrind, actual value set by the attribute system |
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
60 |
m_inFastRec (false), |
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
61 |
m_limitedTx (false) // mute valgrind, actual value set by the attribute system |
6694 | 62 |
{ |
63 |
NS_LOG_FUNCTION (this); |
|
64 |
} |
|
65 |
||
66 |
TcpNewReno::TcpNewReno (const TcpNewReno& sock) |
|
67 |
: TcpSocketBase (sock), |
|
68 |
m_cWnd (sock.m_cWnd), |
|
69 |
m_ssThresh (sock.m_ssThresh), |
|
70 |
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
|
71 |
m_retxThresh (sock.m_retxThresh), |
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
72 |
m_inFastRec (false), |
7193df6ebc5d
TCP's attribute variables are initialized in constructor (mutes valgrind)
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7608
diff
changeset
|
73 |
m_limitedTx (sock.m_limitedTx) |
6694 | 74 |
{ |
75 |
NS_LOG_FUNCTION (this); |
|
76 |
NS_LOG_LOGIC ("Invoked the copy constructor"); |
|
77 |
} |
|
78 |
||
79 |
TcpNewReno::~TcpNewReno (void) |
|
80 |
{ |
|
81 |
} |
|
82 |
||
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
83 |
/** We initialize m_cWnd from this function, after attributes initialized */ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
84 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
85 |
TcpNewReno::Listen (void) |
6694 | 86 |
{ |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
87 |
NS_LOG_FUNCTION (this); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
88 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
89 |
return TcpSocketBase::Listen (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
90 |
} |
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 |
/** We initialize m_cWnd from this function, after attributes initialized */ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
93 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
94 |
TcpNewReno::Connect (const Address & address) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
95 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
96 |
NS_LOG_FUNCTION (this << address); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
97 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
98 |
return TcpSocketBase::Connect (address); |
6694 | 99 |
} |
100 |
||
101 |
/** Limit the size of in-flight data by cwnd and receiver's rxwin */ |
|
102 |
uint32_t |
|
103 |
TcpNewReno::Window (void) |
|
104 |
{ |
|
105 |
NS_LOG_FUNCTION (this); |
|
106 |
return std::min (m_rWnd.Get (), m_cWnd.Get ()); |
|
107 |
} |
|
108 |
||
109 |
Ptr<TcpSocketBase> |
|
110 |
TcpNewReno::Fork (void) |
|
111 |
{ |
|
112 |
return CopyObject<TcpNewReno> (this); |
|
113 |
} |
|
114 |
||
115 |
/** New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */ |
|
116 |
void |
|
117 |
TcpNewReno::NewAck (const SequenceNumber32& seq) |
|
118 |
{ |
|
119 |
NS_LOG_FUNCTION (this << seq); |
|
120 |
NS_LOG_LOGIC ("TcpNewReno receieved ACK for seq " << seq << |
|
121 |
" cwnd " << m_cWnd << |
|
122 |
" ssthresh " << m_ssThresh); |
|
123 |
||
124 |
// Check for exit condition of fast recovery |
|
125 |
if (m_inFastRec && seq < m_recover) |
|
126 |
{ // Partial ACK, partial window deflation (RFC2582 sec.3 bullet #5 paragraph 3) |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
127 |
m_cWnd -= seq - m_txBuffer.HeadSequence (); |
6694 | 128 |
m_cWnd += m_segmentSize; // increase cwnd |
129 |
NS_LOG_INFO ("Partial ACK in fast recovery: cwnd set to " << m_cWnd); |
|
7256
b04ba6772f8c
rerun check-style.py at default level to enforce space after function name
Tom Henderson <tomh@tomh.org>
parents:
7176
diff
changeset
|
130 |
TcpSocketBase::NewAck (seq); // update m_nextTxSequence and send new data if allowed by window |
6694 | 131 |
DoRetransmit (); // Assume the next seq is lost. Retransmit lost packet |
132 |
return; |
|
133 |
} |
|
134 |
else if (m_inFastRec && seq >= m_recover) |
|
135 |
{ // Full ACK (RFC2582 sec.3 bullet #5 paragraph 2, option 1) |
|
136 |
m_cWnd = std::min (m_ssThresh, BytesInFlight () + m_segmentSize); |
|
137 |
m_inFastRec = false; |
|
138 |
NS_LOG_INFO ("Received full ACK. Leaving fast recovery with cwnd set to " << m_cWnd); |
|
139 |
} |
|
140 |
||
141 |
// Increase of cwnd based on current phase (slow start or congestion avoidance) |
|
142 |
if (m_cWnd < m_ssThresh) |
|
143 |
{ // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1) |
|
144 |
m_cWnd += m_segmentSize; |
|
145 |
NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
146 |
} |
|
147 |
else |
|
148 |
{ // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1) |
|
149 |
// To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd |
|
150 |
double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get (); |
|
151 |
adder = std::max (1.0, adder); |
|
152 |
m_cWnd += static_cast<uint32_t> (adder); |
|
153 |
NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
154 |
} |
|
155 |
||
156 |
// Complete newAck processing |
|
157 |
TcpSocketBase::NewAck (seq); |
|
158 |
} |
|
159 |
||
160 |
/** Cut cwnd and enter fast recovery mode upon triple dupack */ |
|
161 |
void |
|
162 |
TcpNewReno::DupAck (const TcpHeader& t, uint32_t count) |
|
163 |
{ |
|
7382
200fa0ae38c5
Function log the TcpNewReno::DupAck()
Tom Henderson <tomh@tomh.org>
parents:
7256
diff
changeset
|
164 |
NS_LOG_FUNCTION (this << count); |
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
165 |
if (count == m_retxThresh && !m_inFastRec) |
6694 | 166 |
{ // triple duplicate ack triggers fast retransmit (RFC2582 sec.3 bullet #1) |
167 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
168 |
m_cWnd = m_ssThresh + 3 * m_segmentSize; |
|
169 |
m_recover = m_highTxMark; |
|
170 |
m_inFastRec = true; |
|
171 |
NS_LOG_INFO ("Triple dupack. Enter fast recovery mode. Reset cwnd to " << m_cWnd << |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
172 |
", ssthresh to " << m_ssThresh << " at fast recovery seqnum " << m_recover); |
6694 | 173 |
DoRetransmit (); |
174 |
} |
|
175 |
else if (m_inFastRec) |
|
176 |
{ // Increase cwnd for every additional dupack (RFC2582, sec.3 bullet #3) |
|
177 |
m_cWnd += m_segmentSize; |
|
178 |
NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd); |
|
179 |
SendPendingData (m_connected); |
|
7608
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
180 |
} |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
181 |
else if (!m_inFastRec && m_limitedTx && m_txBuffer.SizeFromSequence (m_nextTxSequence) > 0) |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
182 |
{ // RFC3042 Limited transmit: Send a new packet for each duplicated ACK before fast retransmit |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
183 |
NS_LOG_INFO ("Limited transmit"); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
184 |
uint32_t sz = SendDataPacket (m_nextTxSequence, m_segmentSize, true); |
de67936e4017
Mixed bugfixes on TCP, closes bug 1166, 1227, 1242.
Adrian S Tam <adrian.sw.tam@gmail.com>
parents:
7385
diff
changeset
|
185 |
m_nextTxSequence += sz; // Advance next tx sequence |
6694 | 186 |
}; |
187 |
} |
|
188 |
||
189 |
/** Retransmit timeout */ |
|
190 |
void |
|
191 |
TcpNewReno::Retransmit (void) |
|
192 |
{ |
|
193 |
NS_LOG_FUNCTION (this); |
|
194 |
NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ()); |
|
195 |
m_inFastRec = false; |
|
196 |
||
197 |
// If erroneous timeout in closed/timed-wait state, just return |
|
198 |
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
|
199 |
// 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
|
200 |
if (m_state <= ESTABLISHED && m_txBuffer.HeadSequence () >= m_highTxMark) return; |
6694 | 201 |
|
202 |
// According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight |
|
203 |
// size and cwnd is set to 1*MSS, then the lost packet is retransmitted and |
|
204 |
// TCP back to slow start |
|
205 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
206 |
m_cWnd = m_segmentSize; |
|
207 |
m_nextTxSequence = m_txBuffer.HeadSequence (); // Restart from highest Ack |
|
208 |
NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd << |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
209 |
", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence); |
6694 | 210 |
m_rtt->IncreaseMultiplier (); // Double the next RTO |
211 |
DoRetransmit (); // Retransmit the packet |
|
212 |
} |
|
213 |
||
214 |
void |
|
215 |
TcpNewReno::SetSegSize (uint32_t size) |
|
216 |
{ |
|
217 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetSegSize() cannot change segment size after connection started."); |
|
218 |
m_segmentSize = size; |
|
219 |
} |
|
220 |
||
221 |
void |
|
222 |
TcpNewReno::SetSSThresh (uint32_t threshold) |
|
223 |
{ |
|
224 |
m_ssThresh = threshold; |
|
225 |
} |
|
226 |
||
227 |
uint32_t |
|
228 |
TcpNewReno::GetSSThresh (void) const |
|
229 |
{ |
|
230 |
return m_ssThresh; |
|
231 |
} |
|
232 |
||
233 |
void |
|
234 |
TcpNewReno::SetInitialCwnd (uint32_t cwnd) |
|
235 |
{ |
|
236 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetInitialCwnd() cannot change initial cwnd after connection started."); |
|
237 |
m_initialCWnd = cwnd; |
|
238 |
} |
|
239 |
||
240 |
uint32_t |
|
241 |
TcpNewReno::GetInitialCwnd (void) const |
|
242 |
{ |
|
243 |
return m_initialCWnd; |
|
244 |
} |
|
245 |
||
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
246 |
void |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
247 |
TcpNewReno::InitializeCwnd (void) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
248 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
249 |
/* |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
250 |
* 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
|
251 |
* 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
|
252 |
* 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
|
253 |
*/ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
254 |
m_cWnd = m_initialCWnd * m_segmentSize; |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
255 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
256 |
|
6694 | 257 |
} // namespace ns3 |