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