blob: 93193d2bdcd47dab846235188277ff0d4410f043 [file] [log] [blame]
Alexander Afanasyevd9a7f192013-03-07 13:58:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2//
3// Copyright (c) 2006 Georgia Tech Research Corporation
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: Rajib Bhattacharjea<raj.b@gatech.edu>
19//
20
21// THIS IS A COPY OF rtt-estimator.cc from internet module with minor modifications
22
23// Ported from:
24// Georgia Tech Network Simulator - Round Trip Time Estimation Class
25// George F. Riley. Georgia Tech, Spring 2002
26
27// Implements several variations of round trip time estimators
28
29#include <iostream>
30
31#include "ndn-rtt-estimator.h"
32#include "ns3/simulator.h"
33#include "ns3/double.h"
34#include "ns3/integer.h"
35#include "ns3/uinteger.h"
36#include "ns3/log.h"
37
38NS_LOG_COMPONENT_DEFINE ("ndn.RttEstimator");
39
40namespace ns3 {
41
42namespace ndn {
43
44NS_OBJECT_ENSURE_REGISTERED (RttEstimator);
45
46TypeId
47RttEstimator::GetTypeId (void)
48{
49 static TypeId tid = TypeId ("ns3::ndn::RttEstimator")
50 .SetParent<Object> ()
51 .AddAttribute ("MaxMultiplier",
52 "Maximum RTO Multiplier",
53 UintegerValue (64),
54 MakeUintegerAccessor (&RttEstimator::m_maxMultiplier),
55 MakeUintegerChecker<uint16_t> ())
56 .AddAttribute ("InitialEstimation",
57 "Initial RTT estimation",
58 TimeValue (Seconds (1.0)),
59 MakeTimeAccessor (&RttEstimator::m_initialEstimatedRtt),
60 MakeTimeChecker ())
61 .AddAttribute ("MinRTO",
62 "Minimum retransmit timeout value",
63 TimeValue (Seconds (0.2)), // RFC2988 says min RTO=1 sec, but Linux uses 200ms. See http://www.postel.org/pipermail/end2end-interest/2004-November/004402.html
64 MakeTimeAccessor (&RttEstimator::SetMinRto,
65 &RttEstimator::GetMinRto),
66 MakeTimeChecker ())
67 .AddAttribute ("MaxRTO",
68 "Maximum retransmit timeout value",
69 TimeValue (Seconds (200.0)),
70 MakeTimeAccessor (&RttEstimator::SetMaxRto,
71 &RttEstimator::GetMaxRto),
72 MakeTimeChecker ())
73 ;
74 return tid;
75}
76
77void
78RttEstimator::SetMinRto (Time minRto)
79{
80 NS_LOG_FUNCTION (this << minRto);
81 m_minRto = minRto;
82}
83Time
84RttEstimator::GetMinRto (void) const
85{
86 return m_minRto;
87}
88
89void
90RttEstimator::SetMaxRto (Time maxRto)
91{
92 NS_LOG_FUNCTION (this << maxRto);
93 m_maxRto = maxRto;
94}
95Time
96RttEstimator::GetMaxRto (void) const
97{
98 return m_maxRto;
99}
100
101void
102RttEstimator::SetCurrentEstimate (Time estimate)
103{
104 NS_LOG_FUNCTION (this << estimate);
105 m_currentEstimatedRtt = estimate;
106}
107Time
108RttEstimator::GetCurrentEstimate (void) const
109{
110 return m_currentEstimatedRtt;
111}
112
113
114//RttHistory methods
115RttHistory::RttHistory (SequenceNumber32 s, uint32_t c, Time t)
116 : seq (s), count (c), time (t), retx (false)
117{
118 NS_LOG_FUNCTION (this);
119}
120
121RttHistory::RttHistory (const RttHistory& h)
122 : seq (h.seq), count (h.count), time (h.time), retx (h.retx)
123{
124 NS_LOG_FUNCTION (this);
125}
126
127// Base class methods
128
129RttEstimator::RttEstimator ()
130 : m_next (1),
131 m_nSamples (0),
132 m_multiplier (1),
133 m_history ()
134{
135 NS_LOG_FUNCTION (this);
136 //note next=1 everywhere since first segment will have sequence 1
137
138 // We need attributes initialized here, not later, so use the
139 // ConstructSelf() technique documented in the manual
140 ObjectBase::ConstructSelf (AttributeConstructionList ());
141 m_currentEstimatedRtt = m_initialEstimatedRtt;
142 NS_LOG_DEBUG ("Initialize m_currentEstimatedRtt to " << m_currentEstimatedRtt.GetSeconds () << " sec.");
143}
144
145RttEstimator::RttEstimator (const RttEstimator& c)
146 : Object (c), m_next (c.m_next),
147 m_maxMultiplier (c.m_maxMultiplier),
148 m_initialEstimatedRtt (c.m_initialEstimatedRtt),
149 m_currentEstimatedRtt (c.m_currentEstimatedRtt), m_minRto (c.m_minRto), m_maxRto (c.m_maxRto),
150 m_nSamples (c.m_nSamples), m_multiplier (c.m_multiplier),
151 m_history (c.m_history)
152{
153 NS_LOG_FUNCTION (this);
154}
155
156RttEstimator::~RttEstimator ()
157{
158 NS_LOG_FUNCTION (this);
159}
160
161void RttEstimator::SentSeq (SequenceNumber32 seq, uint32_t size)
162{
163 NS_LOG_FUNCTION (this << seq << size);
164 // Note that a particular sequence has been sent
165 if (seq == m_next)
166 { // This is the next expected one, just log at end
167 m_history.push_back (RttHistory (seq, size, Simulator::Now () ));
168 m_next = seq + SequenceNumber32 (size); // Update next expected
169 }
170 else
171 { // This is a retransmit, find in list and mark as re-tx
172 for (RttHistory_t::iterator i = m_history.begin (); i != m_history.end (); ++i)
173 {
174 if ((seq >= i->seq) && (seq < (i->seq + SequenceNumber32 (i->count))))
175 { // Found it
176 i->retx = true;
177 // One final test..be sure this re-tx does not extend "next"
178 if ((seq + SequenceNumber32 (size)) > m_next)
179 {
180 m_next = seq + SequenceNumber32 (size);
181 i->count = ((seq + SequenceNumber32 (size)) - i->seq); // And update count in hist
182 }
183 break;
184 }
185 }
186 }
187}
188
189Time RttEstimator::AckSeq (SequenceNumber32 ackSeq)
190{
191 NS_LOG_FUNCTION (this << ackSeq);
192 // An ack has been received, calculate rtt and log this measurement
193 // Note we use a linear search (O(n)) for this since for the common
194 // case the ack'ed packet will be at the head of the list
195 Time m = Seconds (0.0);
196 if (m_history.size () == 0) return (m); // No pending history, just exit
197 RttHistory& h = m_history.front ();
198 if (!h.retx && ackSeq >= (h.seq + SequenceNumber32 (h.count)))
199 { // Ok to use this sample
200 m = Simulator::Now () - h.time; // Elapsed time
201 Measurement (m); // Log the measurement
202 ResetMultiplier (); // Reset multiplier on valid measurement
203 }
204 // Now delete all ack history with seq <= ack
205 while(m_history.size () > 0)
206 {
207 RttHistory& h = m_history.front ();
208 if ((h.seq + SequenceNumber32 (h.count)) > ackSeq) break; // Done removing
209 m_history.pop_front (); // Remove
210 }
211 return m;
212}
213
214void RttEstimator::ClearSent ()
215{
216 NS_LOG_FUNCTION (this);
217 // Clear all history entries
218 m_next = 1;
219 m_history.clear ();
220}
221
222void RttEstimator::IncreaseMultiplier ()
223{
224 NS_LOG_FUNCTION (this);
225 m_multiplier = (m_multiplier*2 < m_maxMultiplier) ? m_multiplier*2 : m_maxMultiplier;
226 NS_LOG_DEBUG ("Multiplier increased to " << m_multiplier);
227}
228
229void RttEstimator::ResetMultiplier ()
230{
231 NS_LOG_FUNCTION (this);
232 m_multiplier = 1;
233}
234
235void RttEstimator::Reset ()
236{
237 NS_LOG_FUNCTION (this);
238 // Reset to initial state
239 m_next = 1;
240 m_currentEstimatedRtt = m_initialEstimatedRtt;
241 m_history.clear (); // Remove all info from the history
242 m_nSamples = 0;
243 ResetMultiplier ();
244}
245
246
247} // namespace ndn
248} // namespace ns3