blob: 1080d00bda84f1fd242106f26cf76e2736fa1d3f [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#ifndef _CCNX_FIB_H_
22#define _CCNX_FIB_H_
23
24#include "hash-helper.h"
25#include "ccnx-face.h"
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070026#include "ccnx.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070027#include "ns3/nstime.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070028#include "ns3/simple-ref-count.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070029
30#include <boost/multi_index_container.hpp>
31#include <boost/multi_index/tag.hpp>
32#include <boost/multi_index/ordered_index.hpp>
33#include <boost/multi_index/composite_key.hpp>
34#include <boost/multi_index/hashed_index.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070035#include <boost/multi_index/random_access_index.hpp>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070036#include <boost/multi_index/member.hpp>
37#include <boost/multi_index/mem_fun.hpp>
38
39#include <iostream>
40
41namespace ns3 {
42
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070043class CcnxInterestHeader;
44
45/**
46 * \ingroup ccnx
47 * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple
48 */
49class CcnxFibFaceMetric
50{
51public:
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070052 enum Status { NDN_FIB_GREEN = 1,
53 NDN_FIB_YELLOW = 2,
54 NDN_FIB_RED = 3 };
55public:
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070056 /**
57 * \brief Metric constructor
58 *
59 * \param face Face for which metric
60 * \param cost Initial value for routing cost
61 */
62 CcnxFibFaceMetric (Ptr<CcnxFace> face, int cost)
63 : m_face (face)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070064 , m_status (NDN_FIB_YELLOW)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070065 , m_routingCost (cost)
66 , m_sRtt (Seconds (0))
67 , m_rttVar (Seconds (0))
68 { }
69
70 /**
71 * \brief Comparison operator used by boost::multi_index::identity<>
72 */
73 bool
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070074 operator< (const CcnxFibFaceMetric &fm) const { return *m_face < *fm.m_face; } // return identity of the face
75
76 bool
77 operator< (const Ptr<CcnxFace> &face) const { return *m_face < *face; }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070078
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070079 Ptr<CcnxFace>
80 GetFace () const { return m_face; }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070081
82 /**
83 * \brief Unary function to recalculate smoothed RTT and RTT variation
84 * \param rttSample RTT sample
85 */
86 struct UpdateRtt
87 {
88 UpdateRtt (const Time &rttSample) : m_rttSample (rttSample) {};
89 void operator() (CcnxFibFaceMetric &entry);
90 private:
91 const Time &m_rttSample;
92 };
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070093
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070094private:
95 friend std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
96public:
97 Ptr<CcnxFace> m_face; ///< Face
98
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070099 Status m_status; ///< \brief Status of the next hop:
100 ///< - NDN_FIB_GREEN
101 ///< - NDN_FIB_YELLOW
102 ///< - NDN_FIB_RED
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700103
104 uint32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol)
105
106 Time m_sRtt; ///< \brief smoothed round-trip time
107 Time m_rttVar; ///< \brief round-trip time variation
108};
109
110/**
111 * \ingroup ccnx
112 * \brief Typedef for indexed face container of CcnxFibEntry
113 *
114 * Currently, there are 2 indexes:
115 * - by face (used to find record and update metric)
116 * - by metric (face ranking)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700117 * - random access index (for fast lookup on nth face). Order is
118 * maintained manually to be equal to the 'by metric' order
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700119 */
120struct CcnxFibFaceMetricContainer
121{
122 typedef boost::multi_index::multi_index_container<
123 CcnxFibFaceMetric,
124 boost::multi_index::indexed_by<
125 // For fast access to elements using CcnxFace
126 boost::multi_index::ordered_unique<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700127 boost::multi_index::tag<__ccnx_private::i_face>,
128 boost::multi_index::member<CcnxFibFaceMetric,Ptr<CcnxFace>,&CcnxFibFaceMetric::m_face>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700129 >,
130
131 // List of available faces ordered by (status, m_routingCost)
132 boost::multi_index::ordered_non_unique<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700133 boost::multi_index::tag<__ccnx_private::i_metric>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700134 boost::multi_index::composite_key<
135 CcnxFibFaceMetric,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700136 boost::multi_index::member<CcnxFibFaceMetric,CcnxFibFaceMetric::Status,&CcnxFibFaceMetric::m_status>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700137 boost::multi_index::member<CcnxFibFaceMetric,uint32_t,&CcnxFibFaceMetric::m_routingCost>
138 >
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700139 >,
140
141 // To optimize nth candidate selection (sacrifice a little bit space to gain speed)
142 boost::multi_index::random_access<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700143 boost::multi_index::tag<__ccnx_private::i_nth>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700144 >
145 >
146 > type;
147};
148
149/**
150 * \ingroup ccnx
151 * \brief Structure for FIB table entry, holding indexed list of
152 * available faces and their respective metrics
153 */
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700154class CcnxFibEntry : public SimpleRefCount<CcnxFibEntry>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700155{
156public:
157 /**
158 * \brief Constructor
159 * \param prefix Prefix for the FIB entry
160 */
161 CcnxFibEntry (Ptr<CcnxNameComponents> prefix)
162 : m_prefix (prefix)
163 , m_needsProbing (false)
164 { }
165
166 /**
167 * \brief Update status of FIB next hop
168 */
169 void
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700170 UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700171
172 /**
173 * \brief Get prefix for the FIB entry
174 */
175 const CcnxNameComponents&
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700176 GetPrefix () const { return *m_prefix; }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700177
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700178 /**
179 * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces)
180 */
181 Ptr<CcnxFace>
182 FindBestCandidate (int skip = 0);
183
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700184private:
185 friend std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
186
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700187public:
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700188 Ptr<CcnxNameComponents> m_prefix; ///< \brief Prefix of the FIB entry
189 CcnxFibFaceMetricContainer::type m_faces; ///< \brief Indexed list of faces
190
191 bool m_needsProbing; ///< \brief flag indicating that probing should be performed
192};
193
194///////////////////////////////////////////////////////////////////////////////
195///////////////////////////////////////////////////////////////////////////////
196
197/**
198 * \ingroup ccnx
199 * \brief Typedef for indexed container for FIB entries
200 *
201 * Currently, there is only one index
202 * - by prefix hash, which is used to perform prefix match
203 */
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700204struct CcnxFibEntryContainer
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700205{
206 typedef boost::multi_index::multi_index_container<
207 CcnxFibEntry,
208 boost::multi_index::indexed_by<
209 // For fast access to elements using CcnxFace
210 boost::multi_index::hashed_unique<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700211 boost::multi_index::tag<__ccnx_private::i_prefix>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700212 boost::multi_index::const_mem_fun<CcnxFibEntry,
213 const CcnxNameComponents&,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700214 &CcnxFibEntry::GetPrefix>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700215 CcnxPrefixHash>
216
217 // other indexes?
218 >
219 > type;
220};
221
222/**
223 * \ingroup ccnx
224 * \brief Class implementing FIB functionality
225 */
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700226class CcnxFib : public Object
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700227{
228public:
229 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700230 * \brief Interface ID
231 *
232 * \return interface ID
233 */
234 static TypeId GetTypeId ();
235
236 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700237 * \brief Constructor
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700238 */
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700239 CcnxFib ();
240 // * \param node smart pointer to Ccnx stack associated with particular node
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700241
242 // // Invalidate entries in FIB
243 // // Will leave FIB records in hash, but assign metric=NETWORK_UNREACHABLE
244 // void invalidate( );
245
246 // //Find corresponding FIB entry for the given content name
247 // //Longest match is performed
248 // FibIterator lookup( const string &name );
249 // bool isValid( const FibIterator &it ) { return it!=_fib.end(); }
250
251 /**
252 * \brief Perform longest prefix match
253 *
254 * \todo Implement exclude filters
255 *
256 * \param interest Interest packet header
257 * \returns If entry found a pair <valid_iterator, true> will be returned, otherwise <invalid_iterator, false>
258 */
259 std::pair<CcnxFibEntryContainer::type::iterator, bool>
260 LongestPrefixMatch (const CcnxInterestHeader &interest) const;
261
262 /**
263 * Update FIB entry
264 * If the entry exists, metric will be updated. Otherwise, new entry will be created
265 *
266 * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE
267 *
268 * @param name Prefix
269 * @param interfaceIndex Forwarding interface
270 * @param metric Routing metric
271 * @param nextHop Nexthop node address (IPv4)
272 * @return true if a new entry created, false otherwise
273 */
274 // bool update( const string &name, int interfaceIndex, int metric, NodeAddress nextHop );
275 // bool update( NodeAddress nodeId, int interfaceIndex, int metric, NodeAddress nextHop );
276 // Bool update( NodeAddress nodeId, int metric, NodeAddress nextHop );
277
278 // // Update Fib from OSPF routing table (through a hack in OSPF algorithm)
279 // void updateFibFromOSPFv2( int interface );
280
281 // // Update Fib from BGP routing table (using info from RibIn)
282 // void updateFibFromBGP( );
283
284 // // Update Fib from IP routing table
285 // void updateFibFromIpRouting( );
286
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700287 // void dump( );
288 // void dump( const FibIterator &fib );
289
290 // void resetProbing(); //reset needsProbing field for every FibEntry
291private:
292 friend std::ostream& operator<< (std::ostream& os, const CcnxFib &fib);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700293 CcnxFib(const CcnxFib&) {} ; ///< \brief copy constructor is disabled
294
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700295private:
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700296 // Ptr<Ccnx> m_node;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700297
298 CcnxFibEntryContainer::type m_fib;
299};
300
301///////////////////////////////////////////////////////////////////////////////
302///////////////////////////////////////////////////////////////////////////////
303
304std::ostream& operator<< (std::ostream& os, const CcnxFib &fib);
305std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
306std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
307
308} // namespace ns3
309
310#endif /* NDN_FIB_H */