blob: 7e2991c47f620b64064ceaeb8a88b3922428745a [file] [log] [blame]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -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
21#include "ccnx-fib.h"
22
23#include "ccnx.h"
24#include "ccnx-face.h"
25#include "ccnx-interest-header.h"
26
27#include "ns3/node.h"
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070028#include "ns3/assert.h"
29
30#define NDN_RTO_ALPHA 0.125
31#define NDN_RTO_BETA 0.25
32#define NDN_RTO_K 4
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070033
34//#define NDN_DEBUG_OSPF 0
35//#define NDN_DEBUG_OSPF_NODES 0
36
37//#define NDN_DUMP_FIB 0
38namespace ns3 {
39
40
41//////////////////////////////////////////////////////////////////////
42// Helpers
43//////////////////////////////////////////////////////////////////////
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070044namespace __ccnx_private {
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070045
46struct CcnxFibFaceMetricByFace
47{
48 typedef CcnxFibFaceMetricContainer::type::index<i_face>::type
49 type;
50};
51
52struct ChangeStatus
53{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070054 ChangeStatus (CcnxFibFaceMetric::Status status) : m_status (status) { }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070055 void operator() (CcnxFibFaceMetric &entry)
56 {
57 entry.m_status = m_status;
58 }
59private:
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070060 CcnxFibFaceMetric::Status m_status;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070061};
62
63
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070064// struct SearchByFace {
65// /**
66// * \brief To perform effective searches by CcnxFace
67// */
68// bool
69// operator() (const CcnxFibFaceMetric &m, const Ptr<CcnxFace> &face) const
70// {
71// return *(m.m_face) < *face;
72// }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070073
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070074// /**
75// * \brief To perform effective searches by CcnxFace
76// */
77// bool
78// operator() (const Ptr<CcnxFace> &face, const CcnxFibFaceMetric &m) const
79// {
80// return *face < *(m.m_face);
81// }
82// };
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070083
84}
85//////////////////////////////////////////////////////////////////////
86//////////////////////////////////////////////////////////////////////
87
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070088using namespace __ccnx_private;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070089
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070090TypeId
91CcnxFib::GetTypeId (void)
92{
93 static TypeId tid = TypeId ("ns3::CcnxFib")
94 .SetParent<Object> ()
95 .SetGroupName ("Ccnx")
96 .AddConstructor<CcnxFib> ()
97
98 ;
99 return tid;
100}
101
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700102void
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700103CcnxFibFaceMetric::UpdateRtt::operator() (CcnxFibFaceMetric &entry)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700104{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700105 // const Time & this->m_rttSample
106
107 //update srtt and rttvar (RFC 2988)
108 if (entry.m_sRtt.IsZero ())
109 {
110 //first RTT measurement
111 NS_ASSERT_MSG (entry.m_rttVar.IsZero (), "SRTT is zero, but variation is not");
112
113 entry.m_sRtt = m_rttSample;
114 entry.m_rttVar = Time (entry.m_sRtt / 2.0);
115 }
116 else
117 {
118 entry.m_rttVar = Time ((1 - NDN_RTO_BETA) * entry.m_rttVar + NDN_RTO_BETA * Abs(entry.m_sRtt - m_rttSample));
119 entry.m_sRtt = Time ((1 - NDN_RTO_ALPHA) * entry.m_sRtt + NDN_RTO_ALPHA * m_rttSample);
120 }
121}
122
123void
124CcnxFibEntry::UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status)
125{
126 CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700127 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (), "Update status can be performed only on existing faces of CcxnFibEntry");
128
129 m_faces.modify (record, ChangeStatus (status));
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700130
131 // reordering random access index same way as by metric index
132 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700133}
134
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700135
136Ptr<CcnxFace>
137CcnxFibEntry::FindBestCandidate (int skip/* = 0*/)
138{
139 skip = skip % m_faces.size();
140 return m_faces.get<i_nth> () [skip].GetFace ();
141}
142
143
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700144CcnxFib::CcnxFib ()
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700145{
146}
147
148std::pair<CcnxFibEntryContainer::type::iterator, bool>
149CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const
150{
151 const CcnxNameComponents &name = interest.GetName ();
152 for (size_t componentsCount = name.GetComponents ().size ();
153 componentsCount > 0;
154 componentsCount--)
155 {
156 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount));
157 CcnxFibEntryContainer::type::iterator match = m_fib.find (subPrefix);
158 if (match != m_fib.end())
159 return std::pair<CcnxFibEntryContainer::type::iterator, bool> (match,true);
160 }
161
162 return std::pair<CcnxFibEntryContainer::type::iterator, bool> (m_fib.end(),false);
163}
164
165std::ostream& operator<< (std::ostream& os, const CcnxFib &fib)
166{
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700167 // os << "Node " << fib.m_node->GetObject<Node> ()->GetId () << "\n";
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700168 os << " Dest prefix Interfaces(Costs) \n";
169 os << "+-------------+--------------------------------------+\n";
170
171 for (CcnxFibEntryContainer::type::iterator entry = fib.m_fib.begin ();
172 entry != fib.m_fib.end ();
173 entry++)
174 {
175 os << *entry << "\n";
176 }
177 return os;
178}
179
180std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry)
181{
182 os << entry.m_prefix << "\t";
183 for (CcnxFibFaceMetricByFace::type::iterator metric = entry.m_faces.get<i_face> ().begin ();
184 metric != entry.m_faces.get<i_face> ().end ();
185 metric++)
186 {
187 if (metric != entry.m_faces.get<i_face> ().begin ())
188 os << ", ";
189
190 os << *metric;
191 }
192 return os;
193}
194
195std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric)
196{
197 static const std::string statusString[] = {"","g","y","r"};
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700198
199 os << metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")";
200 return os;
201}
202
203// void CcnxFib::resetProbing()
204// {
205// for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++)
206// VALUE(fib).needsProbing = true;
207// }
208
209// void CcnxFib::updateInterfaceStatus( int interface, int status )
210// {
211// for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ )
212// {
213// VALUE(fib).updateStatus( interface, status );
214// }
215// }
216
217} // namespace ns3