blob: 750f9da70b9a7f64667b47d1b233c5ffaf9f61a9 [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
57
58 //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");
63
64 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700100 (&ll::_1)->*&FaceMetric::m_status = 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700120 if (record->m_routingCost > metric || record->m_status == FaceMetric::NDN_FIB_RED)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700121 {
122 m_faces.modify (record,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700123 (&ll::_1)->*&FaceMetric::m_routingCost = metric);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700124
125 m_faces.modify (record,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700126 (&ll::_1)->*&FaceMetric::m_status = FaceMetric::NDN_FIB_YELLOW);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700127 }
128 }
129
130 // 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 Afanasyev2b4c9472012-08-09 15:00:38 -0700135Entry::Invalidate ()
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700136{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700137 for (FaceMetricByFace::type::iterator face = m_faces.begin ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700138 face != m_faces.end ();
139 face++)
140 {
141 m_faces.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142 (&ll::_1)->*&FaceMetric::m_routingCost = std::numeric_limits<uint16_t>::max ());
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700143
144 m_faces.modify (face,
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700145 (&ll::_1)->*&FaceMetric::m_status = FaceMetric::NDN_FIB_RED);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700146 }
147}
148
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700149const FaceMetric &
150Entry::FindBestCandidate (uint32_t skip/* = 0*/) const
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700151{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 if (m_faces.size () == 0) throw Entry::NoFaces ();
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700153 skip = skip % m_faces.size();
154 return m_faces.get<i_nth> () [skip];
155}
156
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700157std::ostream& operator<< (std::ostream& os, const Entry &entry)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700158{
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700159 for (FaceMetricContainer::type::index<i_nth>::type::iterator metric =
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700160 entry.m_faces.get<i_nth> ().begin ();
161 metric != entry.m_faces.get<i_nth> ().end ();
162 metric++)
163 {
164 if (metric != entry.m_faces.get<i_nth> ().begin ())
165 os << ", ";
166
167 os << *metric;
168 }
169 return os;
170}
171
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700172std::ostream& operator<< (std::ostream& os, const FaceMetric &metric)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700173{
174 static const std::string statusString[] = {"","g","y","r"};
175
176 os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << "," << metric.m_face->GetMetric () << ")";
177 return os;
178}
179
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180} // namespace fib
181} // namespace ndn
182} // namespace ns3