A Discrete-Event Network Simulator
API
tcp-tahoe.cc
Go to the documentation of this file.
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-tahoe.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 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("TcpTahoe");
34 
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::TcpTahoe")
42  .AddConstructor<TcpTahoe> ()
43  .AddAttribute ("ReTxThreshold", "Threshold for fast retransmit",
44  UintegerValue (3),
46  MakeUintegerChecker<uint32_t> ())
47  .AddTraceSource ("CongestionWindow",
48  "The TCP connection's congestion window",
50  "ns3::TracedValue::Uint32Callback")
51  .AddTraceSource ("SlowStartThreshold",
52  "TCP slow start threshold (bytes)",
54  "ns3::TracedValue::Uint32Callback")
55  ;
56  return tid;
57 }
58 
59 TcpTahoe::TcpTahoe (void) : m_initialCWnd (1), m_retxThresh (3)
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
65  : TcpSocketBase (sock),
66  m_cWnd (sock.m_cWnd),
67  m_ssThresh (sock.m_ssThresh),
68  m_initialCWnd (sock.m_initialCWnd),
69  m_initialSsThresh (sock.m_initialSsThresh),
70  m_retxThresh (sock.m_retxThresh)
71 {
72  NS_LOG_FUNCTION (this);
73  NS_LOG_LOGIC ("Invoked the copy constructor");
74 }
75 
77 {
78 }
79 
80 /* We initialize m_cWnd from this function, after attributes initialized */
81 int
83 {
84  NS_LOG_FUNCTION (this);
85  InitializeCwnd ();
86  return TcpSocketBase::Listen ();
87 }
88 
89 /* We initialize m_cWnd from this function, after attributes initialized */
90 int
92 {
93  NS_LOG_FUNCTION (this << address);
94  InitializeCwnd ();
95  return TcpSocketBase::Connect (address);
96 }
97 
98 /* Limit the size of in-flight data by cwnd and receiver's rxwin */
99 uint32_t
101 {
102  NS_LOG_FUNCTION (this);
103  return std::min (m_rWnd.Get (), m_cWnd.Get ());
104 }
105 
108 {
109  return CopyObject<TcpTahoe> (this);
110 }
111 
112 /* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */
113 void
115 {
116  NS_LOG_FUNCTION (this << seq);
117  NS_LOG_LOGIC ("TcpTahoe received ACK for seq " << seq <<
118  " cwnd " << m_cWnd <<
119  " ssthresh " << m_ssThresh);
120  if (m_cWnd < m_ssThresh)
121  { // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1)
123  NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
124  }
125  else
126  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1)
127  // To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd
128  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get ();
129  adder = std::max (1.0, adder);
130  m_cWnd += static_cast<uint32_t> (adder);
131  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
132  }
133  TcpSocketBase::NewAck (seq); // Complete newAck processing
134 }
135 
136 /* Cut down ssthresh upon triple dupack */
137 void
138 TcpTahoe::DupAck (const TcpHeader& t, uint32_t count)
139 {
140  NS_LOG_FUNCTION (this << "t " << count);
141  if (count == m_retxThresh)
142  { // triple duplicate ack triggers fast retransmit (RFC2001, sec.3)
143  NS_LOG_INFO ("Triple Dup Ack: old ssthresh " << m_ssThresh << " cwnd " << m_cWnd);
144  // fast retransmit in Tahoe means triggering RTO earlier. Tx is restarted
145  // from the highest ack and run slow start again.
146  // (Fall & Floyd 1996, sec.1)
147  m_ssThresh = std::max (static_cast<unsigned> (m_cWnd / 2), m_segmentSize * 2); // Half ssthresh
148  m_cWnd = m_segmentSize; // Run slow start again
149  m_nextTxSequence = m_txBuffer->HeadSequence (); // Restart from highest Ack
150  NS_LOG_INFO ("Triple Dup Ack: new ssthresh " << m_ssThresh << " cwnd " << m_cWnd);
151  NS_LOG_LOGIC ("Triple Dup Ack: retransmit missing segment at " << Simulator::Now ().GetSeconds ());
152  DoRetransmit ();
153  }
154 }
155 
156 /* Retransmit timeout */
158 {
159  NS_LOG_FUNCTION (this);
160  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
161  // If erroneous timeout in closed/timed-wait state, just return
162  if (m_state == CLOSED || m_state == TIME_WAIT) return;
163  // If all data are received (non-closing socket and nothing to send), just return
164  if (m_state <= ESTABLISHED && m_txBuffer->HeadSequence () >= m_highTxMark) return;
165 
166  m_ssThresh = std::max (static_cast<unsigned> (m_cWnd / 2), m_segmentSize * 2); // Half ssthresh
167  m_cWnd = m_segmentSize; // Set cwnd to 1 segSize (RFC2001, sec.2)
168  m_nextTxSequence = m_txBuffer->HeadSequence (); // Restart from highest Ack
169  DoRetransmit (); // Retransmit the packet
170 }
171 
172 void
173 TcpTahoe::SetSegSize (uint32_t size)
174 {
175  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpTahoe::SetSegSize() cannot change segment size after connection started.");
176  m_segmentSize = size;
177 }
178 
179 void
180 TcpTahoe::SetInitialSSThresh (uint32_t threshold)
181 {
182  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpTahoe::SetSSThresh() cannot change initial ssThresh after connection started.");
183  m_initialSsThresh = threshold;
184 }
185 
186 uint32_t
188 {
189  return m_initialSsThresh;
190 }
191 
192 void
194 {
195  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpTahoe::SetInitialCwnd() cannot change initial cwnd after connection started.");
196  m_initialCWnd = cwnd;
197 }
198 
199 uint32_t
201 {
202  return m_initialCWnd;
203 }
204 
205 void
207 {
208  /*
209  * Initialize congestion window, default to 1 MSS (RFC2001, sec.1) and must
210  * not be larger than 2 MSS (RFC2581, sec.3.1). Both m_initiaCWnd and
211  * m_segmentSize are set by the attribute system in ns3::TcpSocket.
212  */
215 }
216 
217 } // namespace ns3
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
Definition: tcp-tahoe.cc:107
TracedValue< uint32_t > m_cWnd
Congestion window.
Definition: tcp-tahoe.h:85
virtual int Listen(void)
Listen for incoming connections.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void SetSegSize(uint32_t size)
Set the segment size.
Definition: tcp-tahoe.cc:173
uint32_t m_retxThresh
Fast Retransmit threshold.
Definition: tcp-tahoe.h:89
virtual uint32_t Window(void)
Return the max possible number of unacked bytes.
Definition: tcp-tahoe.cc:100
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual uint32_t GetInitialSSThresh(void) const
Get the initial Slow Start Threshold.
Definition: tcp-tahoe.cc:187
virtual uint32_t GetInitialCwnd(void) const
Get the initial Congestion Window.
Definition: tcp-tahoe.cc:200
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
uint32_t m_segmentSize
Segment size.
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
T Get(void) const
Get the underlying value.
Definition: traced-value.h:186
TracedValue< TcpStates_t > m_state
TCP state.
An implementation of a stream socket using TCP.
Definition: tcp-tahoe.h:42
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual void SetInitialCwnd(uint32_t cwnd)
Set the initial Congestion Window.
Definition: tcp-tahoe.cc:193
virtual void SetInitialSSThresh(uint32_t threshold)
Set the initial Slow Start Threshold.
Definition: tcp-tahoe.cc:180
TracedValue< uint32_t > m_ssThresh
Slow Start Threshold.
Definition: tcp-tahoe.h:86
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
virtual int Listen(void)
Listen for incoming connections.
Definition: tcp-tahoe.cc:82
Hold an unsigned integer type.
Definition: uinteger.h:44
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-tahoe.cc:38
A base class for implementation of a stream socket using TCP.
virtual ~TcpTahoe(void)
Definition: tcp-tahoe.cc:76
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
uint32_t m_initialCWnd
Initial cWnd value.
Definition: tcp-tahoe.h:87
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
Definition: tcp-tahoe.cc:157
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
Definition: tcp-tahoe.cc:138
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
uint32_t m_initialSsThresh
Initial Slow Start Threshold value.
Definition: tcp-tahoe.h:88
TracedValue< SequenceNumber32 > m_highTxMark
Highest seqno ever sent, regardless of ReTx.
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
Definition: tcp-tahoe.cc:114
TcpTahoe(void)
Create an unbound tcp socket.
Definition: tcp-tahoe.cc:59
void InitializeCwnd(void)
Set the congestion window when connection starts.
Definition: tcp-tahoe.cc:206
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
Definition: tcp-tahoe.cc:91
tuple address
Definition: first.py:37
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
TracedValue< uint32_t > m_rWnd
Flow control window at remote side.
virtual void DoRetransmit(void)
Retransmit the oldest packet.