blob: f8c595692964a094601073a60d3952c7fba5f766 [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"
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070029#include "ns3/names.h"
Ilya Moiseenko00b30482011-11-15 17:58:00 -080030#include "ns3/log.h"
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070031
32#define NDN_RTO_ALPHA 0.125
33#define NDN_RTO_BETA 0.25
34#define NDN_RTO_K 4
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070035
36//#define NDN_DEBUG_OSPF 0
37//#define NDN_DEBUG_OSPF_NODES 0
38
39//#define NDN_DUMP_FIB 0
40namespace ns3 {
41
42
43//////////////////////////////////////////////////////////////////////
44// Helpers
45//////////////////////////////////////////////////////////////////////
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070046namespace __ccnx_private {
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070047
48struct CcnxFibFaceMetricByFace
49{
50 typedef CcnxFibFaceMetricContainer::type::index<i_face>::type
51 type;
52};
53
54struct ChangeStatus
55{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070056 ChangeStatus (CcnxFibFaceMetric::Status status) : m_status (status) { }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070057 void operator() (CcnxFibFaceMetric &entry)
58 {
59 entry.m_status = m_status;
60 }
61private:
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070062 CcnxFibFaceMetric::Status m_status;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070063};
64
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070065struct ChangeMetric
66{
67 ChangeMetric (int32_t metric) : m_metric (metric) { }
68 void operator() (CcnxFibFaceMetric &entry)
69 {
70 entry.m_routingCost = m_metric;
71 }
72private:
73 int32_t m_metric;
74};
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070075
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070076// struct SearchByFace {
77// /**
78// * \brief To perform effective searches by CcnxFace
79// */
80// bool
81// operator() (const CcnxFibFaceMetric &m, const Ptr<CcnxFace> &face) const
82// {
83// return *(m.m_face) < *face;
84// }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070085
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070086// /**
87// * \brief To perform effective searches by CcnxFace
88// */
89// bool
90// operator() (const Ptr<CcnxFace> &face, const CcnxFibFaceMetric &m) const
91// {
92// return *face < *(m.m_face);
93// }
94// };
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070095
96}
97//////////////////////////////////////////////////////////////////////
98//////////////////////////////////////////////////////////////////////
Ilya Moiseenko00b30482011-11-15 17:58:00 -080099NS_LOG_COMPONENT_DEFINE ("CcnxFib");
100
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700101using namespace __ccnx_private;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700102
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700103TypeId
104CcnxFib::GetTypeId (void)
105{
106 static TypeId tid = TypeId ("ns3::CcnxFib")
107 .SetParent<Object> ()
108 .SetGroupName ("Ccnx")
109 .AddConstructor<CcnxFib> ()
110
111 ;
112 return tid;
113}
114
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700115void
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700116CcnxFibFaceMetric::UpdateRtt::operator() (CcnxFibFaceMetric &entry)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700117{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700118 // const Time & this->m_rttSample
119
120 //update srtt and rttvar (RFC 2988)
121 if (entry.m_sRtt.IsZero ())
122 {
123 //first RTT measurement
124 NS_ASSERT_MSG (entry.m_rttVar.IsZero (), "SRTT is zero, but variation is not");
125
126 entry.m_sRtt = m_rttSample;
127 entry.m_rttVar = Time (entry.m_sRtt / 2.0);
128 }
129 else
130 {
131 entry.m_rttVar = Time ((1 - NDN_RTO_BETA) * entry.m_rttVar + NDN_RTO_BETA * Abs(entry.m_sRtt - m_rttSample));
132 entry.m_sRtt = Time ((1 - NDN_RTO_ALPHA) * entry.m_sRtt + NDN_RTO_ALPHA * m_rttSample);
133 }
134}
135
136void
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700137CcnxFibEntry::UpdateStatus::operator () (CcnxFibEntry &entry)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700138{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700139 CcnxFibFaceMetricByFace::type::iterator record = entry.m_faces.get<i_face> ().find (m_face);
140 NS_ASSERT_MSG (record != entry.m_faces.get<i_face> ().end (),
141 "Update status can be performed only on existing faces of CcxnFibEntry");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700142
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700143 entry.m_faces.modify (record, ChangeStatus (m_status));
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700144
145 // reordering random access index same way as by metric index
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700146 entry.m_faces.get<i_nth> ().rearrange (entry.m_faces.get<i_metric> ().begin ());
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700147}
148
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700149void
150CcnxFibEntry::AddOrUpdateRoutingMetric::operator () (CcnxFibEntry &entry)
151{
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800152 NS_LOG_FUNCTION(this);
153 NS_ASSERT_MSG (m_face != NULL, "Trying to Add or Update NULL face");
154
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700155 CcnxFibFaceMetricByFace::type::iterator record = entry.m_faces.get<i_face> ().find (m_face);
156 if (record == entry.m_faces.get<i_face> ().end ())
157 {
158 entry.m_faces.insert (CcnxFibFaceMetric (m_face, m_metric));
159 }
160 else
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800161 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700162 entry.m_faces.modify (record, ChangeMetric (m_metric));
163 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700164 // reordering random access index same way as by metric index
165 entry.m_faces.get<i_nth> ().rearrange (entry.m_faces.get<i_metric> ().begin ());
166}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700167
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700168void
169CcnxFibEntry::UpdateFaceRtt::operator() (CcnxFibEntry &entry)
170{
171 CcnxFibFaceMetricContainer::type::iterator metric = entry.m_faces.find (m_face);
172 NS_ASSERT_MSG (metric != entry.m_faces.end (),
173 "Something wrong. Cannot find entry for the face in FIB");
174
175 entry.m_faces.modify (metric, CcnxFibFaceMetric::UpdateRtt (m_rttSample));
176}
177
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700178Ptr<CcnxFace>
Ilya Moiseenko4e473482011-10-31 17:58:14 -0700179CcnxFibEntry::FindBestCandidate (int skip/* = 0*/) const
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700180{
181 skip = skip % m_faces.size();
182 return m_faces.get<i_nth> () [skip].GetFace ();
183}
184
185
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700186CcnxFib::CcnxFib ()
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700187{
188}
189
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700190void
191CcnxFib::NotifyNewAggregate ()
192{
193 if (m_node == 0)
194 m_node = this->GetObject<Node>();
195 Object::NotifyNewAggregate ();
196}
197
198void
199CcnxFib::DoDispose (void)
200{
201 m_node = 0;
202 Object::DoDispose ();
203}
204
205
206CcnxFibEntryContainer::type::iterator
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700207CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const
208{
209 const CcnxNameComponents &name = interest.GetName ();
210 for (size_t componentsCount = name.GetComponents ().size ();
211 componentsCount > 0;
212 componentsCount--)
213 {
214 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700215 CcnxFibEntryContainer::type::iterator match = find (subPrefix);
216 if (match != end())
217 return match;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700218 }
219
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700220 return end ();
221}
222
223
224CcnxFibEntryContainer::type::iterator
225CcnxFib::Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric)
226{
227// CcnxFibFaceMetric
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800228 NS_LOG_FUNCTION(this << prefix << face << metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700229 CcnxFibEntryContainer::type::iterator entry = find (prefix);
230 if (entry == end ())
231 {
232 entry = insert (end (), CcnxFibEntry (prefix));
233 // insert new
234 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700235
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800236 NS_ASSERT_MSG (face != NULL, "Trying to modify NULL face");
237 modify (entry, CcnxFibEntry::AddOrUpdateRoutingMetric (face, metric));
238
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700239 return entry;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700240}
Ilya Moiseenko4e473482011-10-31 17:58:14 -0700241
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700242
243std::ostream& operator<< (std::ostream& os, const CcnxFib &fib)
244{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700245 os << "Node " << Names::FindName (fib.m_node) << "\n";
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700246 os << " Dest prefix Interfaces(Costs) \n";
247 os << "+-------------+--------------------------------------+\n";
248
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249 for (CcnxFibEntryContainer::type::iterator entry = fib.begin ();
250 entry != fib.end ();
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700251 entry++)
252 {
253 os << *entry << "\n";
254 }
255 return os;
256}
257
258std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry)
259{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700260 os << *entry.m_prefix << "\t";
261
262 for (CcnxFibFaceMetricContainer::type::index<i_nth>::type::iterator metric =
263 entry.m_faces.get<i_nth> ().begin ();
264 metric != entry.m_faces.get<i_nth> ().end ();
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700265 metric++)
266 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700267 if (metric != entry.m_faces.get<i_nth> ().begin ())
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700268 os << ", ";
269
270 os << *metric;
271 }
272 return os;
273}
274
275std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric)
276{
277 static const std::string statusString[] = {"","g","y","r"};
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700278
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700279 os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")";
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700280 return os;
281}
282
283// void CcnxFib::resetProbing()
284// {
285// for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++)
286// VALUE(fib).needsProbing = true;
287// }
288
289// void CcnxFib::updateInterfaceStatus( int interface, int status )
290// {
291// for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ )
292// {
293// VALUE(fib).updateStatus( interface, status );
294// }
295// }
296
297} // namespace ns3