blob: b7ed57fd63b394e5144a7d9c348edb584a383732 [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
90void
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070091CcnxFibFaceMetric::UpdateRtt::operator() (CcnxFibFaceMetric &entry)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070092{
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070093 // const Time & this->m_rttSample
94
95 //update srtt and rttvar (RFC 2988)
96 if (entry.m_sRtt.IsZero ())
97 {
98 //first RTT measurement
99 NS_ASSERT_MSG (entry.m_rttVar.IsZero (), "SRTT is zero, but variation is not");
100
101 entry.m_sRtt = m_rttSample;
102 entry.m_rttVar = Time (entry.m_sRtt / 2.0);
103 }
104 else
105 {
106 entry.m_rttVar = Time ((1 - NDN_RTO_BETA) * entry.m_rttVar + NDN_RTO_BETA * Abs(entry.m_sRtt - m_rttSample));
107 entry.m_sRtt = Time ((1 - NDN_RTO_ALPHA) * entry.m_sRtt + NDN_RTO_ALPHA * m_rttSample);
108 }
109}
110
111void
112CcnxFibEntry::UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status)
113{
114 CcnxFibFaceMetricByFace::type::iterator record = m_faces.get<i_face> ().find (face);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700115 NS_ASSERT_MSG (record != m_faces.get<i_face> ().end (), "Update status can be performed only on existing faces of CcxnFibEntry");
116
117 m_faces.modify (record, ChangeStatus (status));
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700118
119 // reordering random access index same way as by metric index
120 m_faces.get<i_nth> ().rearrange (m_faces.get<i_metric> ().begin ());
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700121}
122
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700123
124Ptr<CcnxFace>
125CcnxFibEntry::FindBestCandidate (int skip/* = 0*/)
126{
127 skip = skip % m_faces.size();
128 return m_faces.get<i_nth> () [skip].GetFace ();
129}
130
131
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700132CcnxFib::CcnxFib ()
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700133{
134}
135
136std::pair<CcnxFibEntryContainer::type::iterator, bool>
137CcnxFib::LongestPrefixMatch (const CcnxInterestHeader &interest) const
138{
139 const CcnxNameComponents &name = interest.GetName ();
140 for (size_t componentsCount = name.GetComponents ().size ();
141 componentsCount > 0;
142 componentsCount--)
143 {
144 CcnxNameComponents subPrefix (name.GetSubComponents (componentsCount));
145 CcnxFibEntryContainer::type::iterator match = m_fib.find (subPrefix);
146 if (match != m_fib.end())
147 return std::pair<CcnxFibEntryContainer::type::iterator, bool> (match,true);
148 }
149
150 return std::pair<CcnxFibEntryContainer::type::iterator, bool> (m_fib.end(),false);
151}
152
153std::ostream& operator<< (std::ostream& os, const CcnxFib &fib)
154{
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700155 // os << "Node " << fib.m_node->GetObject<Node> ()->GetId () << "\n";
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700156 os << " Dest prefix Interfaces(Costs) \n";
157 os << "+-------------+--------------------------------------+\n";
158
159 for (CcnxFibEntryContainer::type::iterator entry = fib.m_fib.begin ();
160 entry != fib.m_fib.end ();
161 entry++)
162 {
163 os << *entry << "\n";
164 }
165 return os;
166}
167
168std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry)
169{
170 os << entry.m_prefix << "\t";
171 for (CcnxFibFaceMetricByFace::type::iterator metric = entry.m_faces.get<i_face> ().begin ();
172 metric != entry.m_faces.get<i_face> ().end ();
173 metric++)
174 {
175 if (metric != entry.m_faces.get<i_face> ().begin ())
176 os << ", ";
177
178 os << *metric;
179 }
180 return os;
181}
182
183std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric)
184{
185 static const std::string statusString[] = {"","g","y","r"};
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700186
187 os << metric.m_face << "(" << metric.m_routingCost << ","<< statusString [metric.m_status] << ")";
188 return os;
189}
190
191// void CcnxFib::resetProbing()
192// {
193// for(FibRangeIterator fib = _fib.begin(); fib != _fib.end(); fib++)
194// VALUE(fib).needsProbing = true;
195// }
196
197// void CcnxFib::updateInterfaceStatus( int interface, int status )
198// {
199// for( FibRangeIterator fib = _fib.begin(); fib!=_fib.end(); fib++ )
200// {
201// VALUE(fib).updateStatus( interface, status );
202// }
203// }
204
205} // namespace ns3