author | Tom Henderson <tomh@tomh.org> |
Sun, 22 May 2011 23:18:47 -0700 | |
changeset 7256 | b04ba6772f8c |
parent 7176 | 9f2663992e99 |
child 7382 | 200fa0ae38c5 |
permissions | -rw-r--r-- |
6694 | 1 |
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
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> () |
|
43 |
.AddTraceSource ("CongestionWindow", |
|
44 |
"The TCP connection's congestion window", |
|
45 |
MakeTraceSourceAccessor (&TcpNewReno::m_cWnd)) |
|
46 |
; |
|
47 |
return tid; |
|
48 |
} |
|
49 |
||
50 |
TcpNewReno::TcpNewReno (void) : m_inFastRec (false) |
|
51 |
{ |
|
52 |
NS_LOG_FUNCTION (this); |
|
53 |
} |
|
54 |
||
55 |
TcpNewReno::TcpNewReno (const TcpNewReno& sock) |
|
56 |
: TcpSocketBase (sock), |
|
57 |
m_cWnd (sock.m_cWnd), |
|
58 |
m_ssThresh (sock.m_ssThresh), |
|
59 |
m_initialCWnd (sock.m_initialCWnd), |
|
60 |
m_inFastRec (false) |
|
61 |
{ |
|
62 |
NS_LOG_FUNCTION (this); |
|
63 |
NS_LOG_LOGIC ("Invoked the copy constructor"); |
|
64 |
} |
|
65 |
||
66 |
TcpNewReno::~TcpNewReno (void) |
|
67 |
{ |
|
68 |
} |
|
69 |
||
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
70 |
/** 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
|
71 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
72 |
TcpNewReno::Listen (void) |
6694 | 73 |
{ |
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
74 |
NS_LOG_FUNCTION (this); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
75 |
InitializeCwnd (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
76 |
return TcpSocketBase::Listen (); |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
77 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
78 |
|
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
79 |
/** 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
|
80 |
int |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
81 |
TcpNewReno::Connect (const Address & address) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
82 |
{ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
83 |
NS_LOG_FUNCTION (this << address); |
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::Connect (address); |
6694 | 86 |
} |
87 |
||
88 |
/** Limit the size of in-flight data by cwnd and receiver's rxwin */ |
|
89 |
uint32_t |
|
90 |
TcpNewReno::Window (void) |
|
91 |
{ |
|
92 |
NS_LOG_FUNCTION (this); |
|
93 |
return std::min (m_rWnd.Get (), m_cWnd.Get ()); |
|
94 |
} |
|
95 |
||
96 |
Ptr<TcpSocketBase> |
|
97 |
TcpNewReno::Fork (void) |
|
98 |
{ |
|
99 |
return CopyObject<TcpNewReno> (this); |
|
100 |
} |
|
101 |
||
102 |
/** New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */ |
|
103 |
void |
|
104 |
TcpNewReno::NewAck (const SequenceNumber32& seq) |
|
105 |
{ |
|
106 |
NS_LOG_FUNCTION (this << seq); |
|
107 |
NS_LOG_LOGIC ("TcpNewReno receieved ACK for seq " << seq << |
|
108 |
" cwnd " << m_cWnd << |
|
109 |
" ssthresh " << m_ssThresh); |
|
110 |
||
111 |
// Check for exit condition of fast recovery |
|
112 |
if (m_inFastRec && seq < m_recover) |
|
113 |
{ // Partial ACK, partial window deflation (RFC2582 sec.3 bullet #5 paragraph 3) |
|
114 |
m_cWnd += m_segmentSize; // increase cwnd |
|
115 |
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
|
116 |
TcpSocketBase::NewAck (seq); // update m_nextTxSequence and send new data if allowed by window |
6694 | 117 |
DoRetransmit (); // Assume the next seq is lost. Retransmit lost packet |
118 |
return; |
|
119 |
} |
|
120 |
else if (m_inFastRec && seq >= m_recover) |
|
121 |
{ // Full ACK (RFC2582 sec.3 bullet #5 paragraph 2, option 1) |
|
122 |
m_cWnd = std::min (m_ssThresh, BytesInFlight () + m_segmentSize); |
|
123 |
m_inFastRec = false; |
|
124 |
NS_LOG_INFO ("Received full ACK. Leaving fast recovery with cwnd set to " << m_cWnd); |
|
125 |
} |
|
126 |
||
127 |
// Increase of cwnd based on current phase (slow start or congestion avoidance) |
|
128 |
if (m_cWnd < m_ssThresh) |
|
129 |
{ // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1) |
|
130 |
m_cWnd += m_segmentSize; |
|
131 |
NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
132 |
} |
|
133 |
else |
|
134 |
{ // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1) |
|
135 |
// To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd |
|
136 |
double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get (); |
|
137 |
adder = std::max (1.0, adder); |
|
138 |
m_cWnd += static_cast<uint32_t> (adder); |
|
139 |
NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh); |
|
140 |
} |
|
141 |
||
142 |
// Complete newAck processing |
|
143 |
TcpSocketBase::NewAck (seq); |
|
144 |
} |
|
145 |
||
146 |
/** Cut cwnd and enter fast recovery mode upon triple dupack */ |
|
147 |
void |
|
148 |
TcpNewReno::DupAck (const TcpHeader& t, uint32_t count) |
|
149 |
{ |
|
7176
9f2663992e99
internet coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents:
6834
diff
changeset
|
150 |
if (count == 3 && !m_inFastRec) |
6694 | 151 |
{ // triple duplicate ack triggers fast retransmit (RFC2582 sec.3 bullet #1) |
152 |
m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2); |
|
153 |
m_cWnd = m_ssThresh + 3 * m_segmentSize; |
|
154 |
m_recover = m_highTxMark; |
|
155 |
m_inFastRec = true; |
|
156 |
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
|
157 |
", ssthresh to " << m_ssThresh << " at fast recovery seqnum " << m_recover); |
6694 | 158 |
DoRetransmit (); |
159 |
} |
|
160 |
else if (m_inFastRec) |
|
161 |
{ // Increase cwnd for every additional dupack (RFC2582, sec.3 bullet #3) |
|
162 |
m_cWnd += m_segmentSize; |
|
163 |
NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd); |
|
164 |
SendPendingData (m_connected); |
|
165 |
}; |
|
166 |
} |
|
167 |
||
168 |
/** Retransmit timeout */ |
|
169 |
void |
|
170 |
TcpNewReno::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; |
|
178 |
// If all data are received, just return |
|
179 |
if (m_txBuffer.HeadSequence () >= m_nextTxSequence) return; |
|
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 |
TcpNewReno::SetSegSize (uint32_t size) |
|
195 |
{ |
|
196 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetSegSize() cannot change segment size after connection started."); |
|
197 |
m_segmentSize = size; |
|
198 |
} |
|
199 |
||
200 |
void |
|
201 |
TcpNewReno::SetSSThresh (uint32_t threshold) |
|
202 |
{ |
|
203 |
m_ssThresh = threshold; |
|
204 |
} |
|
205 |
||
206 |
uint32_t |
|
207 |
TcpNewReno::GetSSThresh (void) const |
|
208 |
{ |
|
209 |
return m_ssThresh; |
|
210 |
} |
|
211 |
||
212 |
void |
|
213 |
TcpNewReno::SetInitialCwnd (uint32_t cwnd) |
|
214 |
{ |
|
215 |
NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetInitialCwnd() cannot change initial cwnd after connection started."); |
|
216 |
m_initialCWnd = cwnd; |
|
217 |
} |
|
218 |
||
219 |
uint32_t |
|
220 |
TcpNewReno::GetInitialCwnd (void) const |
|
221 |
{ |
|
222 |
return m_initialCWnd; |
|
223 |
} |
|
224 |
||
6697
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
225 |
void |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
226 |
TcpNewReno::InitializeCwnd (void) |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
227 |
{ |
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 |
* 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
|
230 |
* 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
|
231 |
* 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
|
232 |
*/ |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
233 |
m_cWnd = m_initialCWnd * m_segmentSize; |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
234 |
} |
6f1114f669ff
fix valgrind warnings for new TCP code
Tom Henderson <tomh@tomh.org>
parents:
6694
diff
changeset
|
235 |
|
6694 | 236 |
} // namespace ns3 |