blob: bc559c690928bef99738c6977e39cce9dd8b2f73 [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"
26#include "ns3/nstime.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070027#include "ns3/simple-ref-count.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070028
29#include <boost/multi_index_container.hpp>
30#include <boost/multi_index/tag.hpp>
31#include <boost/multi_index/ordered_index.hpp>
32#include <boost/multi_index/composite_key.hpp>
33#include <boost/multi_index/hashed_index.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070034#include <boost/multi_index/random_access_index.hpp>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070035#include <boost/multi_index/member.hpp>
36#include <boost/multi_index/mem_fun.hpp>
37
38#include <iostream>
39
40namespace ns3 {
41
42namespace __ccnx_private_fib
43{
44const uint8_t NDN_FIB_GREEN = 1; ///< \brief
45const uint8_t NDN_FIB_YELLOW = 2;
46const uint8_t NDN_FIB_RED = 3;
47
48class i_face {};
49class i_metric {};
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070050class i_nth {};
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070051class i_prefix {};
52}
53
54class Ccnx;
55class CcnxInterestHeader;
56
57/**
58 * \ingroup ccnx
59 * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple
60 */
61class CcnxFibFaceMetric
62{
63public:
64 /**
65 * \brief Metric constructor
66 *
67 * \param face Face for which metric
68 * \param cost Initial value for routing cost
69 */
70 CcnxFibFaceMetric (Ptr<CcnxFace> face, int cost)
71 : m_face (face)
72 , m_status (__ccnx_private_fib::NDN_FIB_YELLOW)
73 , m_routingCost (cost)
74 , m_sRtt (Seconds (0))
75 , m_rttVar (Seconds (0))
76 { }
77
78 /**
79 * \brief Comparison operator used by boost::multi_index::identity<>
80 */
81 bool
82 operator< (const CcnxFibFaceMetric &m) const { return *m_face < *(m.m_face); } // return identity of the face
83
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070084 Ptr<CcnxFace>
85 GetFace () const { return m_face; }
86
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070087private:
88 friend std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
89public:
90 Ptr<CcnxFace> m_face; ///< Face
91
92 uint8_t m_status; ///< \brief Status of the next hop:
93 ///< - #__ccnx_private_fib::NDN_FIB_GREEN
94 ///< - #__ccnx_private_fib::NDN_FIB_YELLOW
95 ///< - #__ccnx_private_fib::NDN_FIB_RED
96
97 uint32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol)
98
99 Time m_sRtt; ///< \brief smoothed round-trip time
100 Time m_rttVar; ///< \brief round-trip time variation
101};
102
103/**
104 * \ingroup ccnx
105 * \brief Typedef for indexed face container of CcnxFibEntry
106 *
107 * Currently, there are 2 indexes:
108 * - by face (used to find record and update metric)
109 * - by metric (face ranking)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700110 * - random access index (for fast lookup on nth face). Order is
111 * maintained manually to be equal to the 'by metric' order
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700112 */
113struct CcnxFibFaceMetricContainer
114{
115 typedef boost::multi_index::multi_index_container<
116 CcnxFibFaceMetric,
117 boost::multi_index::indexed_by<
118 // For fast access to elements using CcnxFace
119 boost::multi_index::ordered_unique<
120 boost::multi_index::tag<__ccnx_private_fib::i_face>,
121 boost::multi_index::identity<CcnxFibFaceMetric>
122 >,
123
124 // List of available faces ordered by (status, m_routingCost)
125 boost::multi_index::ordered_non_unique<
126 boost::multi_index::tag<__ccnx_private_fib::i_metric>,
127 boost::multi_index::composite_key<
128 CcnxFibFaceMetric,
129 boost::multi_index::member<CcnxFibFaceMetric,uint8_t,&CcnxFibFaceMetric::m_status>,
130 boost::multi_index::member<CcnxFibFaceMetric,uint32_t,&CcnxFibFaceMetric::m_routingCost>
131 >
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700132 >,
133
134 // To optimize nth candidate selection (sacrifice a little bit space to gain speed)
135 boost::multi_index::random_access<
136 boost::multi_index::tag<__ccnx_private_fib::i_nth>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700137 >
138 >
139 > type;
140};
141
142/**
143 * \ingroup ccnx
144 * \brief Structure for FIB table entry, holding indexed list of
145 * available faces and their respective metrics
146 */
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700147class CcnxFibEntry : public SimpleRefCount<CcnxFibEntry>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700148{
149public:
150 /**
151 * \brief Constructor
152 * \param prefix Prefix for the FIB entry
153 */
154 CcnxFibEntry (Ptr<CcnxNameComponents> prefix)
155 : m_prefix (prefix)
156 , m_needsProbing (false)
157 { }
158
159 /**
160 * \brief Update status of FIB next hop
161 */
162 void
163 UpdateStatus (Ptr<CcnxFace> face, uint8_t status);
164
165 /**
166 * \brief Get prefix for the FIB entry
167 */
168 const CcnxNameComponents&
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700169 GetPrefix () const { return *m_prefix; }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700170
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700171 /**
172 * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces)
173 */
174 Ptr<CcnxFace>
175 FindBestCandidate (int skip = 0);
176
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700177private:
178 friend std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
179
180private:
181 Ptr<CcnxNameComponents> m_prefix; ///< \brief Prefix of the FIB entry
182 CcnxFibFaceMetricContainer::type m_faces; ///< \brief Indexed list of faces
183
184 bool m_needsProbing; ///< \brief flag indicating that probing should be performed
185};
186
187///////////////////////////////////////////////////////////////////////////////
188///////////////////////////////////////////////////////////////////////////////
189
190/**
191 * \ingroup ccnx
192 * \brief Typedef for indexed container for FIB entries
193 *
194 * Currently, there is only one index
195 * - by prefix hash, which is used to perform prefix match
196 */
197struct CcnxFibEntryContainer
198{
199 typedef boost::multi_index::multi_index_container<
200 CcnxFibEntry,
201 boost::multi_index::indexed_by<
202 // For fast access to elements using CcnxFace
203 boost::multi_index::hashed_unique<
204 boost::multi_index::tag<__ccnx_private_fib::i_prefix>,
205 boost::multi_index::const_mem_fun<CcnxFibEntry,
206 const CcnxNameComponents&,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700207 &CcnxFibEntry::GetPrefix>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700208 CcnxPrefixHash>
209
210 // other indexes?
211 >
212 > type;
213};
214
215/**
216 * \ingroup ccnx
217 * \brief Class implementing FIB functionality
218 */
219class CcnxFib
220{
221public:
222 /**
223 * \brief Constructor
224 * \param node smart pointer to Ccnx stack associated with particular node
225 */
226 CcnxFib (Ptr<Ccnx> node);
227
228 // // Invalidate entries in FIB
229 // // Will leave FIB records in hash, but assign metric=NETWORK_UNREACHABLE
230 // void invalidate( );
231
232 // //Find corresponding FIB entry for the given content name
233 // //Longest match is performed
234 // FibIterator lookup( const string &name );
235 // bool isValid( const FibIterator &it ) { return it!=_fib.end(); }
236
237 /**
238 * \brief Perform longest prefix match
239 *
240 * \todo Implement exclude filters
241 *
242 * \param interest Interest packet header
243 * \returns If entry found a pair <valid_iterator, true> will be returned, otherwise <invalid_iterator, false>
244 */
245 std::pair<CcnxFibEntryContainer::type::iterator, bool>
246 LongestPrefixMatch (const CcnxInterestHeader &interest) const;
247
248 /**
249 * Update FIB entry
250 * If the entry exists, metric will be updated. Otherwise, new entry will be created
251 *
252 * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE
253 *
254 * @param name Prefix
255 * @param interfaceIndex Forwarding interface
256 * @param metric Routing metric
257 * @param nextHop Nexthop node address (IPv4)
258 * @return true if a new entry created, false otherwise
259 */
260 // bool update( const string &name, int interfaceIndex, int metric, NodeAddress nextHop );
261 // bool update( NodeAddress nodeId, int interfaceIndex, int metric, NodeAddress nextHop );
262 // Bool update( NodeAddress nodeId, int metric, NodeAddress nextHop );
263
264 // // Update Fib from OSPF routing table (through a hack in OSPF algorithm)
265 // void updateFibFromOSPFv2( int interface );
266
267 // // Update Fib from BGP routing table (using info from RibIn)
268 // void updateFibFromBGP( );
269
270 // // Update Fib from IP routing table
271 // void updateFibFromIpRouting( );
272
273 // // Update the status for all FIB records for the specified interface
274 // void updateInterfaceStatus( int interface, int status );
275
276 // void dump( );
277 // void dump( const FibIterator &fib );
278
279 // void resetProbing(); //reset needsProbing field for every FibEntry
280private:
281 friend std::ostream& operator<< (std::ostream& os, const CcnxFib &fib);
282
283private:
284 Ptr<Ccnx> m_node;
285
286 CcnxFibEntryContainer::type m_fib;
287};
288
289///////////////////////////////////////////////////////////////////////////////
290///////////////////////////////////////////////////////////////////////////////
291
292std::ostream& operator<< (std::ostream& os, const CcnxFib &fib);
293std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
294std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
295
296} // namespace ns3
297
298#endif /* NDN_FIB_H */