blob: f05140e142b98583ae49d43fb99447dd3092b4da [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 Afanasyevff0d9ca2013-04-14 23:13:46 -070022#include "ndn-fib.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070023
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070024#include "ns3/ndn-name.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070025#include "ns3/log.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070026#include "ns3/simulator.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -070027
28#define NDN_RTO_ALPHA 0.125
29#define NDN_RTO_BETA 0.25
30#define NDN_RTO_K 4
31
32#include <boost/ref.hpp>
33#include <boost/lambda/lambda.hpp>
34#include <boost/lambda/bind.hpp>
35namespace ll = boost::lambda;
36
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037NS_LOG_COMPONENT_DEFINE ("ndn.fib.Entry");
Alexander Afanasyev78057c32012-07-06 15:18:46 -070038
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070039namespace ns3 {
40namespace ndn {
41namespace fib {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070042
43//////////////////////////////////////////////////////////////////////
44// Helpers
45//////////////////////////////////////////////////////////////////////
Alexander Afanasyev78057c32012-07-06 15:18:46 -070046
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047struct FaceMetricByFace
Alexander Afanasyev78057c32012-07-06 15:18:46 -070048{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 typedef FaceMetricContainer::type::index<i_face>::type
Alexander Afanasyev78057c32012-07-06 15:18:46 -070050 type;
51};
52
Alexander Afanasyev78057c32012-07-06 15:18:46 -070053
54void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070055FaceMetric::UpdateRtt (const Time &rttSample)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070056{
57 // const Time & this->m_rttSample
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080058
Alexander Afanasyev78057c32012-07-06 15:18:46 -070059 //update srtt and rttvar (RFC 2988)
60 if (m_sRtt.IsZero ())
61 {
62 //first RTT measurement
63 NS_ASSERT_MSG (m_rttVar.IsZero (), "SRTT is zero, but variation is not");
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080064
Alexander Afanasyev78057c32012-07-06 15:18:46 -070065 m_sRtt = rttSample;
66 m_rttVar = Time (m_sRtt / 2.0);
67 }
68 else
69 {
Alexander Afanasyevdb31e7c2014-07-09 21:48:59 -070070 m_rttVar = Time ((1 - NDN_RTO_BETA) * m_rttVar + 1.0 * NDN_RTO_BETA * Abs(m_sRtt - rttSample));
71 m_sRtt = Time ((1 - NDN_RTO_ALPHA) * m_sRtt + 1.0 * NDN_RTO_ALPHA * rttSample);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070072 }
73}
74
75/////////////////////////////////////////////////////////////////////
76
77void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070078Entry::UpdateFaceRtt (Ptr<Face> face, const Time &sample)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070079{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyevdc794a32013-09-26 14:34:18 -070081 if (record == m_faces.get<i_face> ().end ())
82 {
83 return;
84 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -070085
86 m_faces.modify (record,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087 ll::bind (&FaceMetric::UpdateRtt, ll::_1, sample));
Alexander Afanasyev78057c32012-07-06 15:18:46 -070088
89 // reordering random access index same way as by metric index
90 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
91}
92
93void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094Entry::UpdateStatus (Ptr<Face> face, FaceMetric::Status status)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070095{
96 NS_LOG_FUNCTION (this << boost::cref(*face) << status);
97
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyevdc794a32013-09-26 14:34:18 -070099 if (record == m_faces.get<i_face> ().end ())
100 {
101 return;
102 }
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700103
104 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800105 ll::bind (&FaceMetric::SetStatus, ll::_1, status));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700106
107 // reordering random access index same way as by metric index
108 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
109}
110
111void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700112Entry::AddOrUpdateRoutingMetric (Ptr<Face> face, int32_t metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700113{
114 NS_LOG_FUNCTION (this);
115 NS_ASSERT_MSG (face != NULL, "Trying to Add or Update NULL face");
116
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700117 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700118 if (record == m_faces.get<i_face> ().end ())
119 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700120 m_faces.insert (FaceMetric (face, metric));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700121 }
122 else
123 {
124 // don't update metric to higher value
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800125 if (record->GetRoutingCost () > metric || record->GetStatus () == FaceMetric::NDN_FIB_RED)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700126 {
127 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800128 ll::bind (&FaceMetric::SetRoutingCost, ll::_1, metric));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700129
130 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800131 ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700132 }
133 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800134
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700135 // reordering random access index same way as by metric index
136 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
137}
138
139void
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800140Entry::SetRealDelayToProducer (Ptr<Face> face, Time delay)
141{
142 NS_LOG_FUNCTION (this);
143 NS_ASSERT_MSG (face != NULL, "Trying to Update NULL face");
144
145 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
146 if (record != m_faces.get<i_face> ().end ())
147 {
148 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800149 ll::bind (&FaceMetric::SetRealDelay, ll::_1, delay));
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800150 }
151}
152
153
154void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700155Entry::Invalidate ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700156{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157 for (FaceMetricByFace::type::iterator face = m_faces.begin ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700158 face != m_faces.end ();
159 face++)
160 {
161 m_faces.modify (face,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800162 ll::bind (&FaceMetric::SetRoutingCost, ll::_1, std::numeric_limits<uint16_t>::max ()));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700163
164 m_faces.modify (face,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800165 ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_RED));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700166 }
167}
168
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700169const FaceMetric &
170Entry::FindBestCandidate (uint32_t skip/* = 0*/) const
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700171{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700172 if (m_faces.size () == 0) throw Entry::NoFaces ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700173 skip = skip % m_faces.size();
174 return m_faces.get<i_nth> () [skip];
175}
176
Alexander Afanasyevff0d9ca2013-04-14 23:13:46 -0700177Ptr<Fib>
178Entry::GetFib ()
179{
180 return m_fib;
181}
182
183
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700184std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700185{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700186 for (FaceMetricContainer::type::index<i_nth>::type::iterator metric =
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700187 entry.m_faces.get<i_nth> ().begin ();
188 metric != entry.m_faces.get<i_nth> ().end ();
189 metric++)
190 {
191 if (metric != entry.m_faces.get<i_nth> ().begin ())
192 os << ", ";
193
194 os << *metric;
195 }
196 return os;
197}
198
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700199std::ostream& operator<< (std::ostream& os, const FaceMetric &metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700200{
201 static const std::string statusString[] = {"","g","y","r"};
202
203 os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << "," << metric.m_face->GetMetric () << ")";
204 return os;
205}
206
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207} // namespace fib
208} // namespace ndn
209} // namespace ns3