blob: 35b3a992dd297130709514cf4cc234e718f0ee82 [file] [log] [blame]
Alexander Afanasyev78057c32012-07-06 15:18:46 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#include "ndn-fib-entry.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070022
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070023#include "ns3/ndn-name-components.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070024#include "ns3/log.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070025#include "ns3/simulator.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070026
27#define NDN_RTO_ALPHA 0.125
28#define NDN_RTO_BETA 0.25
29#define NDN_RTO_K 4
30
31#include <boost/ref.hpp>
32#include <boost/lambda/lambda.hpp>
33#include <boost/lambda/bind.hpp>
34namespace ll = boost::lambda;
35
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036NS_LOG_COMPONENT_DEFINE ("ndn.fib.Entry");
Alexander Afanasyev78057c32012-07-06 15:18:46 -070037
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ns3 {
39namespace ndn {
40namespace fib {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070041
42//////////////////////////////////////////////////////////////////////
43// Helpers
44//////////////////////////////////////////////////////////////////////
Alexander Afanasyev78057c32012-07-06 15:18:46 -070045
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046struct FaceMetricByFace
Alexander Afanasyev78057c32012-07-06 15:18:46 -070047{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 typedef FaceMetricContainer::type::index<i_face>::type
Alexander Afanasyev78057c32012-07-06 15:18:46 -070049 type;
50};
51
Alexander Afanasyev78057c32012-07-06 15:18:46 -070052
53void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054FaceMetric::UpdateRtt (const Time &rttSample)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070055{
56 // const Time & this->m_rttSample
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080057
Alexander Afanasyev78057c32012-07-06 15:18:46 -070058 //update srtt and rttvar (RFC 2988)
59 if (m_sRtt.IsZero ())
60 {
61 //first RTT measurement
62 NS_ASSERT_MSG (m_rttVar.IsZero (), "SRTT is zero, but variation is not");
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080063
Alexander Afanasyev78057c32012-07-06 15:18:46 -070064 m_sRtt = rttSample;
65 m_rttVar = Time (m_sRtt / 2.0);
66 }
67 else
68 {
69 m_rttVar = Time ((1 - NDN_RTO_BETA) * m_rttVar + NDN_RTO_BETA * Abs(m_sRtt - rttSample));
70 m_sRtt = Time ((1 - NDN_RTO_ALPHA) * m_sRtt + NDN_RTO_ALPHA * rttSample);
71 }
72}
73
74/////////////////////////////////////////////////////////////////////
75
76void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070077Entry::UpdateFaceRtt (Ptr<Face> face, const Time &sample)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070078{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070080 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (),
81 "Update status can be performed only on existing faces of CcxnFibEntry");
82
83 m_faces.modify (record,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070084 ll::bind (&FaceMetric::UpdateRtt, ll::_1, sample));
Alexander Afanasyev78057c32012-07-06 15:18:46 -070085
86 // reordering random access index same way as by metric index
87 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
88}
89
90void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091Entry::UpdateStatus (Ptr<Face> face, FaceMetric::Status status)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070092{
93 NS_LOG_FUNCTION (this << boost::cref(*face) << status);
94
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070096 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (),
97 "Update status can be performed only on existing faces of CcxnFibEntry");
98
99 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800100 ll::bind (&FaceMetric::SetStatus, ll::_1, status));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700101
102 // reordering random access index same way as by metric index
103 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
104}
105
106void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700107Entry::AddOrUpdateRoutingMetric (Ptr<Face> face, int32_t metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700108{
109 NS_LOG_FUNCTION (this);
110 NS_ASSERT_MSG (face != NULL, "Trying to Add or Update NULL face");
111
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700113 if (record == m_faces.get<i_face> ().end ())
114 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700115 m_faces.insert (FaceMetric (face, metric));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700116 }
117 else
118 {
119 // don't update metric to higher value
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800120 if (record->GetRoutingCost () > metric || record->GetStatus () == FaceMetric::NDN_FIB_RED)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700121 {
122 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800123 ll::bind (&FaceMetric::SetRoutingCost, ll::_1, metric));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700124
125 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800126 ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700127 }
128 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800129
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700130 // reordering random access index same way as by metric index
131 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
132}
133
134void
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800135Entry::SetRealDelayToProducer (Ptr<Face> face, Time delay)
136{
137 NS_LOG_FUNCTION (this);
138 NS_ASSERT_MSG (face != NULL, "Trying to Update NULL face");
139
140 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
141 if (record != m_faces.get<i_face> ().end ())
142 {
143 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800144 ll::bind (&FaceMetric::SetRealDelay, ll::_1, delay));
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800145 }
146}
147
148
149void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700150Entry::Invalidate ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700151{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 for (FaceMetricByFace::type::iterator face = m_faces.begin ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700153 face != m_faces.end ();
154 face++)
155 {
156 m_faces.modify (face,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800157 ll::bind (&FaceMetric::SetRoutingCost, ll::_1, std::numeric_limits<uint16_t>::max ()));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700158
159 m_faces.modify (face,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800160 ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_RED));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700161 }
162}
163
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164const FaceMetric &
165Entry::FindBestCandidate (uint32_t skip/* = 0*/) const
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700166{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167 if (m_faces.size () == 0) throw Entry::NoFaces ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700168 skip = skip % m_faces.size();
169 return m_faces.get<i_nth> () [skip];
170}
171
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700172std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700173{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700174 for (FaceMetricContainer::type::index<i_nth>::type::iterator metric =
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700175 entry.m_faces.get<i_nth> ().begin ();
176 metric != entry.m_faces.get<i_nth> ().end ();
177 metric++)
178 {
179 if (metric != entry.m_faces.get<i_nth> ().begin ())
180 os << ", ";
181
182 os << *metric;
183 }
184 return os;
185}
186
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700187std::ostream& operator<< (std::ostream& os, const FaceMetric &metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700188{
189 static const std::string statusString[] = {"","g","y","r"};
190
191 os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << "," << metric.m_face->GetMetric () << ")";
192 return os;
193}
194
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195} // namespace fib
196} // namespace ndn
197} // namespace ns3