blob: eee3381b1669c89ee59bc592b7755dba8486f264 [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 {
70 m_rttVar = Time ((1 - NDN_RTO_BETA) * m_rttVar + NDN_RTO_BETA * Abs(m_sRtt - rttSample));
71 m_sRtt = Time ((1 - NDN_RTO_ALPHA) * m_sRtt + NDN_RTO_ALPHA * rttSample);
72 }
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 Afanasyev78057c32012-07-06 15:18:46 -070081 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (),
82 "Update status can be performed only on existing faces of CcxnFibEntry");
83
84 m_faces.modify (record,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070085 ll::bind (&FaceMetric::UpdateRtt, ll::_1, sample));
Alexander Afanasyev78057c32012-07-06 15:18:46 -070086
87 // reordering random access index same way as by metric index
88 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
89}
90
91void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070092Entry::UpdateStatus (Ptr<Face> face, FaceMetric::Status status)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070093{
94 NS_LOG_FUNCTION (this << boost::cref(*face) << status);
95
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev78057c32012-07-06 15:18:46 -070097 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (),
98 "Update status can be performed only on existing faces of CcxnFibEntry");
99
100 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800101 ll::bind (&FaceMetric::SetStatus, ll::_1, status));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700102
103 // reordering random access index same way as by metric index
104 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
105}
106
107void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700108Entry::AddOrUpdateRoutingMetric (Ptr<Face> face, int32_t metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700109{
110 NS_LOG_FUNCTION (this);
111 NS_ASSERT_MSG (face != NULL, "Trying to Add or Update NULL face");
112
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700113 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700114 if (record == m_faces.get<i_face> ().end ())
115 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700116 m_faces.insert (FaceMetric (face, metric));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700117 }
118 else
119 {
120 // don't update metric to higher value
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800121 if (record->GetRoutingCost () > metric || record->GetStatus () == FaceMetric::NDN_FIB_RED)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700122 {
123 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800124 ll::bind (&FaceMetric::SetRoutingCost, ll::_1, metric));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700125
126 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800127 ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_YELLOW));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700128 }
129 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800130
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700131 // reordering random access index same way as by metric index
132 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
133}
134
135void
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800136Entry::SetRealDelayToProducer (Ptr<Face> face, Time delay)
137{
138 NS_LOG_FUNCTION (this);
139 NS_ASSERT_MSG (face != NULL, "Trying to Update NULL face");
140
141 FaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
142 if (record != m_faces.get<i_face> ().end ())
143 {
144 m_faces.modify (record,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800145 ll::bind (&FaceMetric::SetRealDelay, ll::_1, delay));
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800146 }
147}
148
149
150void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700151Entry::Invalidate ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700152{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700153 for (FaceMetricByFace::type::iterator face = m_faces.begin ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700154 face != m_faces.end ();
155 face++)
156 {
157 m_faces.modify (face,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800158 ll::bind (&FaceMetric::SetRoutingCost, ll::_1, std::numeric_limits<uint16_t>::max ()));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700159
160 m_faces.modify (face,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800161 ll::bind (&FaceMetric::SetStatus, ll::_1, FaceMetric::NDN_FIB_RED));
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700162 }
163}
164
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700165const FaceMetric &
166Entry::FindBestCandidate (uint32_t skip/* = 0*/) const
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700167{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700168 if (m_faces.size () == 0) throw Entry::NoFaces ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700169 skip = skip % m_faces.size();
170 return m_faces.get<i_nth> () [skip];
171}
172
Alexander Afanasyevff0d9ca2013-04-14 23:13:46 -0700173Ptr<Fib>
174Entry::GetFib ()
175{
176 return m_fib;
177}
178
179
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700181{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700182 for (FaceMetricContainer::type::index<i_nth>::type::iterator metric =
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700183 entry.m_faces.get<i_nth> ().begin ();
184 metric != entry.m_faces.get<i_nth> ().end ();
185 metric++)
186 {
187 if (metric != entry.m_faces.get<i_nth> ().begin ())
188 os << ", ";
189
190 os << *metric;
191 }
192 return os;
193}
194
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195std::ostream& operator<< (std::ostream& os, const FaceMetric &metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700196{
197 static const std::string statusString[] = {"","g","y","r"};
198
199 os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << "," << metric.m_face->GetMetric () << ")";
200 return os;
201}
202
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203} // namespace fib
204} // namespace ndn
205} // namespace ns3