A Discrete-Event Network Simulator
API
tcp-reno.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-reno.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 ("TcpReno");
34 
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::TcpReno")
42  .SetGroupName ("Internet")
43  .AddConstructor<TcpReno> ()
44  .AddAttribute ("ReTxThreshold", "Threshold for fast retransmit",
45  UintegerValue (3),
47  MakeUintegerChecker<uint32_t> ())
48  ;
49  return tid;
50 }
51 
52 TcpReno::TcpReno (void) : m_retxThresh (3), m_inFastRec (false)
53 {
54  NS_LOG_FUNCTION (this);
55 }
56 
58  : TcpSocketBase (sock),
59  m_retxThresh (sock.m_retxThresh),
60  m_inFastRec (false)
61 {
62  NS_LOG_FUNCTION (this);
63  NS_LOG_LOGIC ("Invoked the copy constructor");
64 }
65 
67 {
68 }
69 
72 {
73  return CopyObject<TcpReno> (this);
74 }
75 
76 /* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */
77 void
79 {
80  NS_LOG_FUNCTION (this << seq);
81  NS_LOG_LOGIC ("TcpReno receieved ACK for seq " << seq <<
82  " cwnd " << m_cWnd <<
83  " ssthresh " << m_ssThresh);
84 
85  // Check for exit condition of fast recovery
86  if (m_inFastRec)
87  { // RFC2001, sec.4; RFC2581, sec.3.2
88  // First new ACK after fast recovery: reset cwnd
90  m_inFastRec = false;
91  NS_LOG_INFO ("Reset cwnd to " << m_cWnd);
92  };
93 
94  // Increase of cwnd based on current phase (slow start or congestion avoidance)
95  if (m_cWnd < m_ssThresh)
96  { // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1)
98  NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
99  }
100  else
101  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1)
102  // To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd
103  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get ();
104  adder = std::max (1.0, adder);
105  m_cWnd += static_cast<uint32_t> (adder);
106  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
107  }
108 
109  // Complete newAck processing
110  TcpSocketBase::NewAck (seq);
111 }
112 
113 // Fast recovery and fast retransmit
114 void
115 TcpReno::DupAck (const TcpHeader& t, uint32_t count)
116 {
117  NS_LOG_FUNCTION (this << "t " << count);
118  if (count == m_retxThresh && !m_inFastRec)
119  { // triple duplicate ack triggers fast retransmit (RFC2581, sec.3.2)
120  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
122  m_inFastRec = true;
123  NS_LOG_INFO ("Triple dupack. Reset cwnd to " << m_cWnd << ", ssthresh to " << m_ssThresh);
124  DoRetransmit ();
125  }
126  else if (m_inFastRec)
127  { // In fast recovery, inc cwnd for every additional dupack (RFC2581, sec.3.2)
129  NS_LOG_INFO ("Increased cwnd to " << m_cWnd);
131  {
133  }
134  };
135 }
136 
137 // Retransmit timeout
139 {
140  NS_LOG_FUNCTION (this);
141  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
142  m_inFastRec = false;
143 
144  // If erroneous timeout in closed/timed-wait state, just return
145  if (m_state == CLOSED || m_state == TIME_WAIT) return;
146  // If all data are received (non-closing socket and nothing to send), just return
147  if (m_state <= ESTABLISHED && m_txBuffer->HeadSequence () >= m_highTxMark) return;
148 
149  // According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight
150  // size and cwnd is set to 1*MSS, then the lost packet is retransmitted and
151  // TCP back to slow start
152  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
154  m_nextTxSequence = m_txBuffer->HeadSequence (); // Restart from highest Ack
155  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
156  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
157  DoRetransmit (); // Retransmit the packet
158 }
159 
160 } // namespace ns3
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 Retransmit(void)
Halving cwnd and call DoRetransmit()
Definition: tcp-reno.cc:138
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Socket is finished.
Definition: tcp-socket.h:65
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
Definition: tcp-reno.cc:71
TracedValue< uint32_t > m_cWnd
Congestion window.
#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
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
uint32_t m_segmentSize
Segment size.
virtual void NewAck(const SequenceNumber32 &seq)
Update buffers w.r.t.
Definition: tcp-reno.cc:78
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:217
TracedValue< TcpStates_t > m_state
TCP state.
An implementation of a stream socket using TCP.
Definition: tcp-reno.h:38
bool m_inFastRec
currently in fast recovery
Definition: tcp-reno.h:65
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
Definition: tcp-reno.cc:115
Hold an unsigned integer type.
Definition: uinteger.h:44
A base class for implementation of a stream socket using TCP.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
virtual ~TcpReno(void)
Definition: tcp-reno.cc:66
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
uint32_t m_retxThresh
Fast Retransmit threshold.
Definition: tcp-reno.h:64
virtual uint32_t BytesInFlight(void)
Return total bytes in flight.
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-reno.cc:38
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
TracedValue< SequenceNumber32 > m_highTxMark
Highest seqno ever sent, regardless of ReTx.
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
bool m_connected
Connection established.
Timeout to catch resent junk before entering closed, can only be entered from FIN_WAIT2 or CLOSING...
Definition: tcp-socket.h:82
TracedValue< uint32_t > m_ssThresh
Slow start threshold.
EventId m_sendPendingDataEvent
micro-delay event to send pending data
TcpReno(void)
Create an unbound tcp socket.
Definition: tcp-reno.cc:52
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:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:826
bool SendPendingData(bool withAck=false)
Send as much pending data as possible according to the Tx window.
virtual void DoRetransmit(void)
Retransmit the oldest packet.