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