blob: e9474d064c32bd3d894c49a836c72ca23da0095c [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
Alexander Afanasyev161a5c42012-04-17 15:14:04 -070024#include "ccnx-name-components-hash-helper.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070025#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 Afanasyevc5a23e22011-09-07 00:37:36 -070029#include "ns3/node.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070030
31#include <boost/multi_index_container.hpp>
32#include <boost/multi_index/tag.hpp>
33#include <boost/multi_index/ordered_index.hpp>
34#include <boost/multi_index/composite_key.hpp>
35#include <boost/multi_index/hashed_index.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070036#include <boost/multi_index/random_access_index.hpp>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070037#include <boost/multi_index/member.hpp>
38#include <boost/multi_index/mem_fun.hpp>
39
40#include <iostream>
41
42namespace ns3 {
43
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070044class CcnxInterestHeader;
45
46/**
47 * \ingroup ccnx
48 * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple
49 */
50class CcnxFibFaceMetric
51{
52public:
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070053 enum Status { NDN_FIB_GREEN = 1,
54 NDN_FIB_YELLOW = 2,
55 NDN_FIB_RED = 3 };
56public:
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070057 /**
58 * \brief Metric constructor
59 *
60 * \param face Face for which metric
61 * \param cost Initial value for routing cost
62 */
Alexander Afanasyevef056552012-01-20 16:19:12 -080063 CcnxFibFaceMetric (Ptr<CcnxFace> face, int32_t cost)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070064 : m_face (face)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070065 , m_status (NDN_FIB_YELLOW)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070066 , m_routingCost (cost)
67 , m_sRtt (Seconds (0))
68 , m_rttVar (Seconds (0))
69 { }
70
71 /**
72 * \brief Comparison operator used by boost::multi_index::identity<>
73 */
74 bool
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070075 operator< (const CcnxFibFaceMetric &fm) const { return *m_face < *fm.m_face; } // return identity of the face
76
77 bool
78 operator< (const Ptr<CcnxFace> &face) const { return *m_face < *face; }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070079
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070080 Ptr<CcnxFace>
81 GetFace () const { return m_face; }
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070082
83 /**
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080084 * \brief Recalculate smoothed RTT and RTT variation
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070085 * \param rttSample RTT sample
86 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080087 void
88 UpdateRtt (const Time &rttSample);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070089
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070090private:
91 friend std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
92public:
93 Ptr<CcnxFace> m_face; ///< Face
94
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070095 Status m_status; ///< \brief Status of the next hop:
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080096 ///< - NDN_FIB_GREEN
97 ///< - NDN_FIB_YELLOW
98 ///< - NDN_FIB_RED
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070099
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700100 int32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700101
102 Time m_sRtt; ///< \brief smoothed round-trip time
103 Time m_rttVar; ///< \brief round-trip time variation
104};
105
106/**
107 * \ingroup ccnx
108 * \brief Typedef for indexed face container of CcnxFibEntry
109 *
110 * Currently, there are 2 indexes:
111 * - by face (used to find record and update metric)
112 * - by metric (face ranking)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700113 * - random access index (for fast lookup on nth face). Order is
114 * maintained manually to be equal to the 'by metric' order
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700115 */
116struct CcnxFibFaceMetricContainer
117{
118 typedef boost::multi_index::multi_index_container<
119 CcnxFibFaceMetric,
120 boost::multi_index::indexed_by<
121 // For fast access to elements using CcnxFace
122 boost::multi_index::ordered_unique<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700123 boost::multi_index::tag<__ccnx_private::i_face>,
124 boost::multi_index::member<CcnxFibFaceMetric,Ptr<CcnxFace>,&CcnxFibFaceMetric::m_face>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700125 >,
126
127 // List of available faces ordered by (status, m_routingCost)
128 boost::multi_index::ordered_non_unique<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700129 boost::multi_index::tag<__ccnx_private::i_metric>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700130 boost::multi_index::composite_key<
131 CcnxFibFaceMetric,
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700132 boost::multi_index::member<CcnxFibFaceMetric,CcnxFibFaceMetric::Status,&CcnxFibFaceMetric::m_status>,
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700133 boost::multi_index::member<CcnxFibFaceMetric,int32_t,&CcnxFibFaceMetric::m_routingCost>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700134 >
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700135 >,
136
137 // To optimize nth candidate selection (sacrifice a little bit space to gain speed)
138 boost::multi_index::random_access<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700139 boost::multi_index::tag<__ccnx_private::i_nth>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700140 >
141 >
142 > type;
143};
144
145/**
146 * \ingroup ccnx
147 * \brief Structure for FIB table entry, holding indexed list of
148 * available faces and their respective metrics
149 */
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700150class CcnxFibEntry // : public SimpleRefCount<CcnxFibEntry>
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700151{
152public:
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800153 class NoFaces {};
154
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700155 /**
156 * \brief Constructor
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700157 * \param prefix smart pointer to the prefix for the FIB entry
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700158 */
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700159 CcnxFibEntry (const Ptr<const CcnxNameComponents> &prefix)
160 : m_prefix (prefix)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700161 , m_needsProbing (false)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700162 { }
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700163
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700164 /**
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800165 * \brief Update status of FIB next hop
166 * \param status Status to set on the FIB entry
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700167 */
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800168 void UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700169
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800170 /**
171 * \brief Add or update routing metric of FIB next hop
172 *
173 * Initial status of the next hop is set to YELLOW
174 */
175 void AddOrUpdateRoutingMetric (Ptr<CcnxFace> face, int32_t metric);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700176
177 /**
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700178 * @brief Invalidate face
179 *
180 * Set routing metric on all faces to max and status to RED
181 */
182 void
183 Invalidate ();
184
185 /**
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800186 * @brief Update RTT averages for the face
187 */
188 void
189 UpdateFaceRtt (Ptr<CcnxFace> face, const Time &sample);
190
191 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700192 * \brief Get prefix for the FIB entry
193 */
194 const CcnxNameComponents&
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700195 GetPrefix () const { return *m_prefix; }
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700196
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700197 /**
198 * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces)
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800199 *
200 * throws CcnxFibEntry::NoFaces if m_faces.size()==0
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700201 */
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800202 const CcnxFibFaceMetric &
203 FindBestCandidate (uint32_t skip = 0) const;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800204
205 /**
206 * @brief Remove record associated with `face`
207 */
208 void
209 RemoveFace (const Ptr<CcnxFace> &face)
210 {
211 m_faces.erase (face);
212 }
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700213
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700214private:
215 friend std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
216
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700217public:
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700218 Ptr<const CcnxNameComponents> m_prefix; ///< \brief Prefix of the FIB entry
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700219 CcnxFibFaceMetricContainer::type m_faces; ///< \brief Indexed list of faces
220
221 bool m_needsProbing; ///< \brief flag indicating that probing should be performed
222};
223
224///////////////////////////////////////////////////////////////////////////////
225///////////////////////////////////////////////////////////////////////////////
226
227/**
228 * \ingroup ccnx
229 * \brief Typedef for indexed container for FIB entries
230 *
231 * Currently, there is only one index
232 * - by prefix hash, which is used to perform prefix match
233 */
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700234struct CcnxFibEntryContainer
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700235{
236 typedef boost::multi_index::multi_index_container<
237 CcnxFibEntry,
238 boost::multi_index::indexed_by<
239 // For fast access to elements using CcnxFace
240 boost::multi_index::hashed_unique<
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700241 boost::multi_index::tag<__ccnx_private::i_prefix>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700242 boost::multi_index::const_mem_fun<CcnxFibEntry,
243 const CcnxNameComponents&,
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700244 &CcnxFibEntry::GetPrefix>,
Alexander Afanasyev07827182011-12-13 01:07:32 -0800245 CcnxPrefixHash>,
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700246
Alexander Afanasyev07827182011-12-13 01:07:32 -0800247 boost::multi_index::random_access<
248 boost::multi_index::tag<__ccnx_private::i_nth>
249 >
250 >
251 > type;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700252};
253
254/**
255 * \ingroup ccnx
256 * \brief Class implementing FIB functionality
257 */
Alexander Afanasyev07827182011-12-13 01:07:32 -0800258class CcnxFib : public Object
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700259{
260public:
261 /**
Alexander Afanasyevcf133f02011-09-06 12:13:48 -0700262 * \brief Interface ID
263 *
264 * \return interface ID
265 */
266 static TypeId GetTypeId ();
267
268 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700269 * \brief Constructor
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700270 */
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700271 CcnxFib ();
272 // * \param node smart pointer to Ccnx stack associated with particular node
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700273
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700274 /**
275 * \brief Perform longest prefix match
276 *
277 * \todo Implement exclude filters
278 *
279 * \param interest Interest packet header
Alexander Afanasyev8accdf62011-09-20 11:33:59 -0700280 * \returns If entry found a valid iterator will be returned, otherwise end ()
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700281 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700282 CcnxFibEntryContainer::type::iterator
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700283 LongestPrefixMatch (const CcnxInterestHeader &interest) const;
284
285 /**
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700286 * \brief Add or update FIB entry
287 *
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700288 * If the entry exists, metric will be updated. Otherwise, new entry will be created
289 *
290 * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE
291 *
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700292 * @param name Prefix
293 * @param face Forwarding face
294 * @param metric Routing metric
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700295 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700296 CcnxFibEntryContainer::type::iterator
297 Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700298
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800299 /**
Alexander Afanasyev8e2f1122012-04-17 15:01:11 -0700300 * \brief Add or update FIB entry using smart pointer to prefix
301 *
302 * If the entry exists, metric will be updated. Otherwise, new entry will be created
303 *
304 * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE
305 *
306 * @param name Smart pointer to prefix
307 * @param face Forwarding face
308 * @param metric Routing metric
309 */
310 CcnxFibEntryContainer::type::iterator
311 Add (const Ptr<const CcnxNameComponents> &prefix, Ptr<CcnxFace> face, int32_t metric);
312
313 /**
314 * @brief Remove FIB entry
315 *
316 * ! ATTENTION ! Use with caution. All PIT entries referencing the corresponding FIB entry will become invalid.
317 * So, simulation may crash.
318 *
319 * @param name Smart pointer to prefix
320 */
321 void
322 Remove (const Ptr<const CcnxNameComponents> &prefix);
323
324 /**
325 * @brief Invalidate FIB entry ("Safe" version of Remove)
326 *
327 * All faces for the entry will be assigned maximum routing metric and NDN_FIB_RED status
328 * @param name Smart pointer to prefix
329 */
330 void
331 Invalidate (const Ptr<const CcnxNameComponents> &prefix);
332
333 /**
334 * @brief Invalidate all FIB entries
335 */
336 void
337 InvalidateAll ();
338
339 /**
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800340 * @brief Remove reference to a face from the entry. If entry had only this face, the whole
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800341 * entry will be removed
342 */
343 void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800344 Remove (const CcnxFibEntry &entry, Ptr<CcnxFace> face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800345
346 /**
347 * @brief Remove all references to a face from FIB. If for some enty that face was the only element,
348 * this FIB entry will be removed.
349 */
350 void
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800351 RemoveFromAll (Ptr<CcnxFace> face);
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700352
Alexander Afanasyev07827182011-12-13 01:07:32 -0800353 /**
354 * \brief Get number of FIB entry (for python bindings)
355 */
356 uint32_t
357 GetCcnxFibEntryCount () const;
358
359 /**
360 * \brief Get FIB entry by index (for python bindings)
361 */
362 const CcnxFibEntry &
363 GetCcnxFibEntry (uint32_t index);
364
365public:
366 CcnxFibEntryContainer::type m_fib;
367
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700368protected:
369 // inherited from Object class
370 virtual void NotifyNewAggregate ();
371 virtual void DoDispose ();
372
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700373private:
374 friend std::ostream& operator<< (std::ostream& os, const CcnxFib &fib);
Alexander Afanasyeva67e28c2011-08-31 21:16:25 -0700375 CcnxFib(const CcnxFib&) {} ; ///< \brief copy constructor is disabled
376
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700377private:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700378 Ptr<Node> m_node;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700379};
380
381///////////////////////////////////////////////////////////////////////////////
382///////////////////////////////////////////////////////////////////////////////
383
384std::ostream& operator<< (std::ostream& os, const CcnxFib &fib);
385std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
386std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
387
388} // namespace ns3
389
390#endif /* NDN_FIB_H */