blob: e4e36d5dcfd87e43fc99b5fb4d79066c7138066b [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
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080036#include <boost/lambda/lambda.hpp>
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080037#include <boost/lambda/bind.hpp>
38namespace ll = boost::lambda;
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080039
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070040namespace 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
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070054}
55//////////////////////////////////////////////////////////////////////
56//////////////////////////////////////////////////////////////////////
Ilya Moiseenko00b30482011-11-15 17:58:00 -080057NS_LOG_COMPONENT_DEFINE ("CcnxFib");
58
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070059using namespace __ccnx_private;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070060
Alexander Afanasyevcf133f02011-09-06 12:13:48 -070061TypeId
62CcnxFib::GetTypeId (void)
63{
64 static TypeId tid = TypeId ("ns3::CcnxFib")
65 .SetParent<Object> ()
66 .SetGroupName ("Ccnx")
67 .AddConstructor<CcnxFib> ()
68
69 ;
70 return tid;
71}
72
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080073/////////////////////////////////////////////////////////////////////
74
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070075void
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080076CcnxFibFaceMetric::UpdateRtt (const Time &rttSample)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070077{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070078 // const Time & this->m_rttSample
79
80 //update srtt and rttvar (RFC 2988)
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080081 if (m_sRtt.IsZero ())
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070082 {
83 //first RTT measurement
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080084 NS_ASSERT_MSG (m_rttVar.IsZero (), "SRTT is zero, but variation is not");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070085
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080086 m_sRtt = rttSample;
87 m_rttVar = Time (m_sRtt / 2.0);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070088 }
89 else
90 {
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080091 m_rttVar = Time ((1 - NDN_RTO_BETA) * m_rttVar + NDN_RTO_BETA * Abs(m_sRtt - rttSample));
92 m_sRtt = Time ((1 - NDN_RTO_ALPHA) * m_sRtt + NDN_RTO_ALPHA * rttSample);
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070093 }
94}
95
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080096/////////////////////////////////////////////////////////////////////
97
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070098void
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080099CcnxFibEntry::UpdateFaceRtt (Ptr<CcnxFace> face, const Time &sample)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700100{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800101 CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
102 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (),
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700103 "Update status can be performed only on existing faces of CcxnFibEntry");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700104
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800105 m_faces.modify (record,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 ll::bind (&CcnxFibFaceMetric::UpdateRtt, ll::_1, sample));
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700107
108 // reordering random access index same way as by metric index
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800109 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700110}
111
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800112void
113CcnxFibEntry::UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700114{
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800115 CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
116 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (),
117 "Update status can be performed only on existing faces of CcxnFibEntry");
118
119 m_faces.modify (record,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800120 (&ll::_1)->*&CcnxFibFaceMetric::m_status = status);
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800121
122 // reordering random access index same way as by metric index
123 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
124}
125
126void
127CcnxFibEntry::AddOrUpdateRoutingMetric (Ptr<CcnxFace> face, int32_t metric)
128{
129 NS_LOG_FUNCTION (this);
130 NS_ASSERT_MSG (face != NULL, "Trying to Add or Update NULL face");
131
132 CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
133 if (record == m_faces.get<i_face> ().end ())
134 {
135 m_faces.insert (CcnxFibFaceMetric (face, metric));
136 }
137 else
138 {
139 m_faces.modify (record,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800140 (&ll::_1)->*&CcnxFibFaceMetric::m_routingCost = metric);
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800141 }
142
143 // reordering random access index same way as by metric index
144 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
145}
146
147const CcnxFibFaceMetric &
148CcnxFibEntry::FindBestCandidate (uint32_t skip/* = 0*/) const
149{
150 if (m_faces.size () == 0) throw CcnxFibEntry::NoFaces ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700151 skip = skip % m_faces.size();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800152 return m_faces.get<i_nth> () [skip];
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700153}
154
155
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700156CcnxFib::CcnxFib ()
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700157{
158}
159
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700160void
161CcnxFib::NotifyNewAggregate ()
162{
163 if (m_node == 0)
164 m_node = this->GetObject<Node>();
165 Object::NotifyNewAggregate ();
166}
167
168void
169CcnxFib::DoDispose (void)
170{
Alexander Afanasyev4fa5e842011-11-21 13:38:39 -0800171 clear ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172 m_node = 0;
Alexander Afanasyev18252852011-11-21 13:35:31 -0800173 clear ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174 Object::DoDispose ();
175}
176
177
178CcnxFibEntryContainer::type::iterator
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700179CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const
180{
181 const CcnxNameComponents &name = interest.GetName ();
182 for (size_t componentsCount = name.GetComponents ().size ();
183 componentsCount > 0;
184 componentsCount--)
185 {
186 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount));
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700187 CcnxFibEntryContainer::type::iterator match = find (subPrefix);
188 if (match != end())
189 return match;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700190 }
191
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192 return end ();
193}
194
195
196CcnxFibEntryContainer::type::iterator
197CcnxFib::Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric)
198{
199// CcnxFibFaceMetric
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800200 NS_LOG_FUNCTION(this << prefix << face << metric);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201 CcnxFibEntryContainer::type::iterator entry = find (prefix);
202 if (entry == end ())
203 {
204 entry = insert (end (), CcnxFibEntry (prefix));
205 // insert new
206 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700207
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800208 NS_ASSERT_MSG (face != NULL, "Trying to modify NULL face");
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800209 modify (entry,
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800210 ll::bind (&CcnxFibEntry::AddOrUpdateRoutingMetric, ll::_1, face, metric));
Ilya Moiseenko00b30482011-11-15 17:58:00 -0800211
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700212 return entry;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700213}
Ilya Moiseenko4e473482011-10-31 17:58:14 -0700214
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800215void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800216CcnxFib::Remove (const CcnxFibEntry &entry, Ptr<CcnxFace> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800217{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800218 NS_LOG_FUNCTION (this);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800219
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800220 modify (iterator_to (entry),
221 ll::bind (&CcnxFibEntry::RemoveFace, ll::_1, face));
222 if (entry.m_faces.size () == 0)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800223 {
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800224 erase (iterator_to (entry));
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800225 }
226}
227
228void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800229CcnxFib::RemoveFromAll (Ptr<CcnxFace> face)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800230{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800231 NS_LOG_FUNCTION (this);
232
233 for_each (begin (), end (), ll::bind (&CcnxFib::Remove, this, ll::_1, face));
234 // BOOST_FOREACH (const CcnxFibEntry &entry, *this)
235 // {
236 // Remove (entry, face);
237 // }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800238}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700239
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800240
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700241std::ostream& operator<< (std::ostream& os, const CcnxFib &fib)
242{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700243 os << "Node " << Names::FindName (fib.m_node) << "\n";
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700244 os << " Dest prefix Interfaces(Costs) \n";
245 os << "+-------------+--------------------------------------+\n";
246
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700247 for (CcnxFibEntryContainer::type::iterator entry = fib.begin ();
248 entry != fib.end ();
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700249 entry++)
250 {
251 os << *entry << "\n";
252 }
253 return os;
254}
255
256std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry)
257{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700258 os << *entry.m_prefix << "\t";
259
260 for (CcnxFibFaceMetricContainer::type::index<i_nth>::type::iterator metric =
261 entry.m_faces.get<i_nth> ().begin ();
262 metric != entry.m_faces.get<i_nth> ().end ();
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700263 metric++)
264 {
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700265 if (metric != entry.m_faces.get<i_nth> ().begin ())
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700266 os << ", ";
267
268 os << *metric;
269 }
270 return os;
271}
272
273std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric)
274{
275 static const std::string statusString[] = {"","g","y","r"};
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700276
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700277 os << *metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")";
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700278 return os;
279}
280
281// void CcnxFib::resetProbing()
282// {
283// for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++)
284// VALUE(fib).needsProbing = true;
285// }
286
287// void CcnxFib::updateInterfaceStatus( int interface, int status )
288// {
289// for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ )
290// {
291// VALUE(fib).updateStatus( interface, status );
292// }
293// }
294
295} // namespace ns3