blob: 2f42259a9c24c77994191c80259f99e12521e6b9 [file] [log] [blame]
Alexander Afanasyev78057c32012-07-06 15:18:46 -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
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#ifndef _NDN_FIB_ENTRY_H_
22#define _NDN_FIB_ENTRY_H_
Alexander Afanasyev78057c32012-07-06 15:18:46 -070023
24#include "ns3/ptr.h"
25#include "ns3/nstime.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070026#include "ns3/ndn-face.h"
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070027#include "ns3/ndn-name.h"
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070028#include "ns3/ndn-limits.h"
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -080029#include "ns3/traced-value.h"
Alexander Afanasyev78057c32012-07-06 15:18:46 -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>
36#include <boost/multi_index/random_access_index.hpp>
37#include <boost/multi_index/member.hpp>
38#include <boost/multi_index/mem_fun.hpp>
39
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ns3 {
41namespace ndn {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070042
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070043class Name;
Alexander Afanasyev73f06f62013-03-15 15:41:38 -070044typedef Name NameComponents;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045
Alexander Afanasyevff0d9ca2013-04-14 23:13:46 -070046class Fib;
47
Alexander Afanasyev79206512013-07-27 16:49:12 -070048/**
49 * @ingroup ndn-fib
50 * @brief Namespace for FIB operations
51 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052namespace fib {
Alexander Afanasyev78057c32012-07-06 15:18:46 -070053
54/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070055 * @ingroup ndn-fib
Alexander Afanasyev78057c32012-07-06 15:18:46 -070056 * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple
57 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058class FaceMetric
Alexander Afanasyev78057c32012-07-06 15:18:46 -070059{
60public:
61 /**
62 * @brief Color codes for FIB face status
63 */
64 enum Status { NDN_FIB_GREEN = 1,
65 NDN_FIB_YELLOW = 2,
66 NDN_FIB_RED = 3 };
67public:
68 /**
69 * \brief Metric constructor
70 *
71 * \param face Face for which metric
72 * \param cost Initial value for routing cost
73 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070074 FaceMetric (Ptr<Face> face, int32_t cost)
Alexander Afanasyev78057c32012-07-06 15:18:46 -070075 : m_face (face)
76 , m_status (NDN_FIB_YELLOW)
77 , m_routingCost (cost)
78 , m_sRtt (Seconds (0))
79 , m_rttVar (Seconds (0))
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -080080 , m_realDelay (Seconds (0))
Alexander Afanasyev78057c32012-07-06 15:18:46 -070081 { }
82
83 /**
84 * \brief Comparison operator used by boost::multi_index::identity<>
85 */
86 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070087 operator< (const FaceMetric &fm) const { return *m_face < *fm.m_face; } // return identity of the face
Alexander Afanasyev78057c32012-07-06 15:18:46 -070088
89 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070090 * @brief Comparison between FaceMetric and Face
Alexander Afanasyev78057c32012-07-06 15:18:46 -070091 */
92 bool
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -080093 operator< (const Ptr<Face> &face) const { return *m_face < *face; }
Alexander Afanasyev78057c32012-07-06 15:18:46 -070094
95 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 * @brief Return Face associated with FaceMetric
Alexander Afanasyev78057c32012-07-06 15:18:46 -070097 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070098 Ptr<Face>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070099 GetFace () const { return m_face; }
100
101 /**
102 * \brief Recalculate smoothed RTT and RTT variation
103 * \param rttSample RTT sample
104 */
105 void
106 UpdateRtt (const Time &rttSample);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800107
108 /**
109 * @brief Get current status of FIB entry
110 */
111 Status
112 GetStatus () const
113 {
114 return m_status;
115 }
116
117 /**
118 * @brief Set current status of FIB entry
119 */
120 void
121 SetStatus (Status status)
122 {
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -0800123 m_status.Set (status);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800124 }
125
126 /**
127 * @brief Get current routing cost
128 */
129 int32_t
130 GetRoutingCost () const
131 {
132 return m_routingCost;
133 }
134
135 /**
136 * @brief Set routing cost
137 */
138 void
139 SetRoutingCost (int32_t routingCost)
140 {
141 m_routingCost = routingCost;
142 }
143
144 /**
145 * @brief Get real propagation delay to the producer, calculated based on NS-3 p2p link delays
146 */
147 Time
148 GetRealDelay () const
149 {
150 return m_realDelay;
151 }
152
153 /**
154 * @brief Set real propagation delay to the producer, calculated based on NS-3 p2p link delays
155 */
156 void
157 SetRealDelay (Time realDelay)
158 {
159 m_realDelay = realDelay;
160 }
161
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -0800162 /**
163 * @brief Get direct access to status trace
164 */
165 TracedValue<Status> &
166 GetStatusTrace ()
167 {
168 return m_status;
169 }
170
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700171private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700172 friend std::ostream& operator<< (std::ostream& os, const FaceMetric &metric);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800173
174private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175 Ptr<Face> m_face; ///< Face
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800176
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -0800177 TracedValue<Status> m_status; ///< \brief Status of the next hop:
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700178 ///< - NDN_FIB_GREEN
179 ///< - NDN_FIB_YELLOW
180 ///< - NDN_FIB_RED
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800181
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700182 int32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol)
183
184 Time m_sRtt; ///< \brief smoothed round-trip time
185 Time m_rttVar; ///< \brief round-trip time variation
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800186
187 Time m_realDelay; ///< \brief real propagation delay to the producer, calculated based on NS-3 p2p link delays
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700188};
189
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700190/// @cond include_hidden
191class i_face {};
192class i_metric {};
193class i_nth {};
194/// @endcond
195
196
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700197/**
Alexander Afanasyev79206512013-07-27 16:49:12 -0700198 * @ingroup ndn-fib
199 * @brief Typedef for indexed face container of Entry
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700200 *
201 * Currently, there are 2 indexes:
202 * - by face (used to find record and update metric)
203 * - by metric (face ranking)
204 * - random access index (for fast lookup on nth face). Order is
205 * maintained manually to be equal to the 'by metric' order
206 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700207struct FaceMetricContainer
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700208{
209 /// @cond include_hidden
210 typedef boost::multi_index::multi_index_container<
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700211 FaceMetric,
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700212 boost::multi_index::indexed_by<
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213 // For fast access to elements using Face
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700214 boost::multi_index::ordered_unique<
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700215 boost::multi_index::tag<i_face>,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800216 boost::multi_index::const_mem_fun<FaceMetric,Ptr<Face>,&FaceMetric::GetFace>
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700217 >,
218
219 // List of available faces ordered by (status, m_routingCost)
220 boost::multi_index::ordered_non_unique<
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700221 boost::multi_index::tag<i_metric>,
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700222 boost::multi_index::composite_key<
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700223 FaceMetric,
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800224 boost::multi_index::const_mem_fun<FaceMetric,FaceMetric::Status,&FaceMetric::GetStatus>,
225 boost::multi_index::const_mem_fun<FaceMetric,int32_t,&FaceMetric::GetRoutingCost>
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700226 >
227 >,
228
229 // To optimize nth candidate selection (sacrifice a little bit space to gain speed)
230 boost::multi_index::random_access<
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700231 boost::multi_index::tag<i_nth>
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700232 >
233 >
234 > type;
235 /// @endcond
236};
237
238/**
Alexander Afanasyev79206512013-07-27 16:49:12 -0700239 * @ingroup ndn-fib
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700240 * \brief Structure for FIB table entry, holding indexed list of
241 * available faces and their respective metrics
242 */
Alexander Afanasyevf5c07742012-10-31 13:13:05 -0700243class Entry : public Object
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700244{
245public:
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800246 typedef Entry base_type;
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800247
Alexander Afanasyev8566f452012-12-10 15:21:51 -0800248public:
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700249 class NoFaces {}; ///< @brief Exception class for the case when FIB entry is not found
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800250
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700251 /**
252 * \brief Constructor
253 * \param prefix smart pointer to the prefix for the FIB entry
254 */
Alexander Afanasyevff0d9ca2013-04-14 23:13:46 -0700255 Entry (Ptr<Fib> fib, const Ptr<const Name> &prefix)
256 : m_fib (fib)
257 , m_prefix (prefix)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700258 , m_needsProbing (false)
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700259 {
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700260 }
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800261
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700262 /**
263 * \brief Update status of FIB next hop
264 * \param status Status to set on the FIB entry
265 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700266 void UpdateStatus (Ptr<Face> face, FaceMetric::Status status);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700267
268 /**
269 * \brief Add or update routing metric of FIB next hop
270 *
271 * Initial status of the next hop is set to YELLOW
272 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700273 void AddOrUpdateRoutingMetric (Ptr<Face> face, int32_t metric);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700274
275 /**
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800276 * \brief Set real delay to the producer
277 */
278 void
279 SetRealDelayToProducer (Ptr<Face> face, Time delay);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800280
Alexander Afanasyev6b0c88f2012-12-01 12:24:27 -0800281 /**
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700282 * @brief Invalidate face
283 *
284 * Set routing metric on all faces to max and status to RED
285 */
286 void
287 Invalidate ();
288
289 /**
290 * @brief Update RTT averages for the face
291 */
292 void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700293 UpdateFaceRtt (Ptr<Face> face, const Time &sample);
Alexander Afanasyev06dba7c2013-02-21 11:36:26 -0800294
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700295 /**
296 * \brief Get prefix for the FIB entry
297 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700298 const Name&
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700299 GetPrefix () const { return *m_prefix; }
300
301 /**
302 * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces)
303 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700304 * throws Entry::NoFaces if m_faces.size()==0
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700305 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700306 const FaceMetric &
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700307 FindBestCandidate (uint32_t skip = 0) const;
308
309 /**
310 * @brief Remove record associated with `face`
311 */
312 void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700313 RemoveFace (const Ptr<Face> &face)
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700314 {
315 m_faces.erase (face);
316 }
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700317
Alexander Afanasyevff0d9ca2013-04-14 23:13:46 -0700318 /**
319 * @brief Get pointer to access FIB, to which this entry is added
320 */
321 Ptr<Fib>
322 GetFib ();
323
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700324private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700325 friend std::ostream& operator<< (std::ostream& os, const Entry &entry);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700326
327public:
Alexander Afanasyevff0d9ca2013-04-14 23:13:46 -0700328 Ptr<Fib> m_fib; ///< \brief FIB to which entry is added
329
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700330 Ptr<const Name> m_prefix; ///< \brief Prefix of the FIB entry
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700331 FaceMetricContainer::type m_faces; ///< \brief Indexed list of faces
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700332
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700333 bool m_needsProbing; ///< \brief flag indicating that probing should be performed
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700334};
335
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700336std::ostream& operator<< (std::ostream& os, const Entry &entry);
337std::ostream& operator<< (std::ostream& os, const FaceMetric &metric);
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700338
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700339} // namespace fib
340} // namespace ndn
341} // namespace ns3
Alexander Afanasyev78057c32012-07-06 15:18:46 -0700342
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700343#endif // _NDN_FIB_ENTRY_H_