A Discrete-Event Network Simulator
API
tcp-newreno.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-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 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("TcpNewReno");
34 
35 NS_OBJECT_ENSURE_REGISTERED (TcpNewReno);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::TcpNewReno")
42  .SetGroupName ("Internet")
43  .AddConstructor<TcpNewReno> ()
44  .AddAttribute ("ReTxThreshold", "Threshold for fast retransmit",
45  UintegerValue (3),
47  MakeUintegerChecker<uint32_t> ())
48  .AddAttribute ("LimitedTransmit", "Enable limited transmit",
49  BooleanValue (false),
52  ;
53  return tid;
54 }
55 
57  : m_retxThresh (3), // mute valgrind, actual value set by the attribute system
58  m_inFastRec (false),
59  m_limitedTx (false) // mute valgrind, actual value set by the attribute system
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
65  : TcpSocketBase (sock),
66  m_retxThresh (sock.m_retxThresh),
67  m_inFastRec (false),
68  m_limitedTx (sock.m_limitedTx)
69 {
70  NS_LOG_FUNCTION (this);
71  NS_LOG_LOGIC ("Invoked the copy constructor");
72 }
73 
75 {
76 }
77 
80 {
81  return CopyObject<TcpNewReno> (this);
82 }
83 
84 /* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */
85 void
87 {
88  NS_LOG_FUNCTION (this << seq);
89  NS_LOG_LOGIC ("TcpNewReno received ACK for seq " << seq <<
90  " cwnd " << m_cWnd <<
91  " ssthresh " << m_ssThresh);
92 
93  // Check for exit condition of fast recovery
94  if (m_inFastRec && seq < m_recover)
95  { // Partial ACK, partial window deflation (RFC2582 sec.3 bullet #5 paragraph 3)
96  m_cWnd += m_segmentSize - (seq - m_txBuffer->HeadSequence ());
97  NS_LOG_INFO ("Partial ACK for seq " << seq << " in fast recovery: cwnd set to " << m_cWnd);
98  m_txBuffer->DiscardUpTo(seq); //Bug 1850: retransmit before newack
99  DoRetransmit (); // Assume the next seq is lost. Retransmit lost packet
100  TcpSocketBase::NewAck (seq); // update m_nextTxSequence and send new data if allowed by window
101  return;
102  }
103  else if (m_inFastRec && seq >= m_recover)
104  { // Full ACK (RFC2582 sec.3 bullet #5 paragraph 2, option 1)
105  m_cWnd = std::min (m_ssThresh.Get (), BytesInFlight () + m_segmentSize);
106  m_inFastRec = false;
107  NS_LOG_INFO ("Received full ACK for seq " << seq <<". Leaving fast recovery with cwnd set to " << m_cWnd);
108  }
109 
110  // Increase of cwnd based on current phase (slow start or congestion avoidance)
111  if (m_cWnd < m_ssThresh)
112  { // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1)
114  NS_LOG_INFO ("In SlowStart, ACK of seq " << seq << "; update cwnd to " << m_cWnd << "; ssthresh " << m_ssThresh);
115  }
116  else
117  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1)
118  // To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd
119  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get ();
120  adder = std::max (1.0, adder);
121  m_cWnd += static_cast<uint32_t> (adder);
122  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
123  }
124 
125  // Complete newAck processing
126  TcpSocketBase::NewAck (seq);
127 }
128 
129 /* Cut cwnd and enter fast recovery mode upon triple dupack */
130 void
131 TcpNewReno::DupAck (const TcpHeader& t, uint32_t count)
132 {
133  NS_LOG_FUNCTION (this << count);
134  if (count == m_retxThresh && !m_inFastRec)
135  { // triple duplicate ack triggers fast retransmit (RFC2582 sec.3 bullet #1)
136  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
139  m_inFastRec = true;
140  NS_LOG_INFO ("Triple dupack. Enter fast recovery mode. Reset cwnd to " << m_cWnd <<
141  ", ssthresh to " << m_ssThresh << " at fast recovery seqnum " << m_recover);
142  DoRetransmit ();
143  }
144  else if (m_inFastRec)
145  { // Increase cwnd for every additional dupack (RFC2582, sec.3 bullet #3)
147  NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd);
149  {
151  }
152  }
153  else if (!m_inFastRec && m_limitedTx && m_txBuffer->SizeFromSequence (m_nextTxSequence) > 0)
154  { // RFC3042 Limited transmit: Send a new packet for each duplicated ACK before fast retransmit
155  NS_LOG_INFO ("Limited transmit");
156  uint32_t sz = SendDataPacket (m_nextTxSequence, m_segmentSize, true);
157  m_nextTxSequence += sz; // Advance next tx sequence
158  };
159 }
160 
161 /* Retransmit timeout */
162 void
164 {
165  NS_LOG_FUNCTION (this);
166  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
167  m_inFastRec = false;
168 
169  // If erroneous timeout in closed/timed-wait state, just return
170  if (m_state == CLOSED || m_state == TIME_WAIT) return;
171  // If all data are received (non-closing socket and nothing to send), just return
172  if (m_state <= ESTABLISHED && m_txBuffer->HeadSequence () >= m_highTxMark) return;
173 
174  // According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight
175  // size and cwnd is set to 1*MSS, then the lost packet is retransmitted and
176  // TCP back to slow start
177  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
179  m_nextTxSequence = m_txBuffer->HeadSequence (); // Restart from highest Ack
180  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
181  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
182  DoRetransmit (); // Retransmit the packet
183 }
184 
185 } // 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 NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
Definition: tcp-newreno.cc:86
AttributeValue implementation for Boolean.
Definition: boolean.h:34
bool m_limitedTx
perform limited transmit
Definition: tcp-newreno.h:65
uint32_t SendDataPacket(SequenceNumber32 seq, uint32_t maxSize, bool withAck)
Extract at most maxSize bytes from the TxBuffer at sequence seq, add the TCP header, and send to TcpL4Protocol.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:81
Socket is finished.
Definition: tcp-socket.h:65
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.
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
An implementation of a stream socket using TCP.
Definition: tcp-newreno.h:36
T Get(void) const
Get the underlying value.
Definition: traced-value.h:217
TracedValue< TcpStates_t > m_state
TCP state.
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-newreno.cc:38
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
Definition: tcp-newreno.cc:131
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
bool m_inFastRec
currently in fast recovery
Definition: tcp-newreno.h:64
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t m_retxThresh
Fast Retransmit threshold.
Definition: tcp-newreno.h:63
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
virtual uint32_t BytesInFlight(void)
Return total bytes in flight.
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
Definition: tcp-newreno.cc:163
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.
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
Definition: tcp-newreno.cc:79
EventId m_sendPendingDataEvent
micro-delay event to send pending data
TcpNewReno(void)
Create an unbound tcp socket.
Definition: tcp-newreno.cc:56
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.
SequenceNumber32 m_recover
Previous highest Tx seqnum for fast recovery.
Definition: tcp-newreno.h:62
virtual ~TcpNewReno(void)
Definition: tcp-newreno.cc:74