blob: 9aa75cbbddadd45ffc9675cd5097912a10e681f2 [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
21#ifndef _CCNX_FIB_ENTRY_H_
22#define _CCNX_FIB_ENTRY_H_
23
24#include "ns3/ptr.h"
25#include "ns3/nstime.h"
26#include "ns3/ccnx.h"
27#include "ns3/ccnx-face.h"
28
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>
34#include <boost/multi_index/random_access_index.hpp>
35#include <boost/multi_index/member.hpp>
36#include <boost/multi_index/mem_fun.hpp>
37
38namespace ns3
39{
40
41class CcnxNameComponents;
42
43/**
44 * \ingroup ccnx
45 * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple
46 */
47class CcnxFibFaceMetric
48{
49public:
50 /**
51 * @brief Color codes for FIB face status
52 */
53 enum Status { NDN_FIB_GREEN = 1,
54 NDN_FIB_YELLOW = 2,
55 NDN_FIB_RED = 3 };
56public:
57 /**
58 * \brief Metric constructor
59 *
60 * \param face Face for which metric
61 * \param cost Initial value for routing cost
62 */
63 CcnxFibFaceMetric (Ptr<CcnxFace> face, int32_t cost)
64 : m_face (face)
65 , m_status (NDN_FIB_YELLOW)
66 , 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
75 operator< (const CcnxFibFaceMetric &fm) const { return *m_face < *fm.m_face; } // return identity of the face
76
77 /**
78 * @brief Comparison between CcnxFibFaceMetric and CcnxFace
79 */
80 bool
81 operator< (const Ptr<CcnxFace> &face) const { return *m_face < *face; }
82
83 /**
84 * @brief Return CcnxFace associated with CcnxFibFaceMetric
85 */
86 Ptr<CcnxFace>
87 GetFace () const { return m_face; }
88
89 /**
90 * \brief Recalculate smoothed RTT and RTT variation
91 * \param rttSample RTT sample
92 */
93 void
94 UpdateRtt (const Time &rttSample);
95
96private:
97 friend std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
98public:
99 Ptr<CcnxFace> m_face; ///< Face
100
101 Status m_status; ///< \brief Status of the next hop:
102 ///< - NDN_FIB_GREEN
103 ///< - NDN_FIB_YELLOW
104 ///< - NDN_FIB_RED
105
106 int32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol)
107
108 Time m_sRtt; ///< \brief smoothed round-trip time
109 Time m_rttVar; ///< \brief round-trip time variation
110};
111
112/**
113 * \ingroup ccnx
114 * \brief Typedef for indexed face container of CcnxFibEntry
115 *
116 * Currently, there are 2 indexes:
117 * - by face (used to find record and update metric)
118 * - by metric (face ranking)
119 * - random access index (for fast lookup on nth face). Order is
120 * maintained manually to be equal to the 'by metric' order
121 */
122struct CcnxFibFaceMetricContainer
123{
124 /// @cond include_hidden
125 typedef boost::multi_index::multi_index_container<
126 CcnxFibFaceMetric,
127 boost::multi_index::indexed_by<
128 // For fast access to elements using CcnxFace
129 boost::multi_index::ordered_unique<
130 boost::multi_index::tag<__ccnx_private::i_face>,
131 boost::multi_index::member<CcnxFibFaceMetric,Ptr<CcnxFace>,&CcnxFibFaceMetric::m_face>
132 >,
133
134 // List of available faces ordered by (status, m_routingCost)
135 boost::multi_index::ordered_non_unique<
136 boost::multi_index::tag<__ccnx_private::i_metric>,
137 boost::multi_index::composite_key<
138 CcnxFibFaceMetric,
139 boost::multi_index::member<CcnxFibFaceMetric,CcnxFibFaceMetric::Status,&CcnxFibFaceMetric::m_status>,
140 boost::multi_index::member<CcnxFibFaceMetric,int32_t,&CcnxFibFaceMetric::m_routingCost>
141 >
142 >,
143
144 // To optimize nth candidate selection (sacrifice a little bit space to gain speed)
145 boost::multi_index::random_access<
146 boost::multi_index::tag<__ccnx_private::i_nth>
147 >
148 >
149 > type;
150 /// @endcond
151};
152
153/**
154 * \ingroup ccnx
155 * \brief Structure for FIB table entry, holding indexed list of
156 * available faces and their respective metrics
157 */
158class CcnxFibEntry : public SimpleRefCount<CcnxFibEntry>
159{
160public:
161 class NoFaces {}; ///< @brief Exception class for the case when FIB entry is not found
162
163 /**
164 * \brief Constructor
165 * \param prefix smart pointer to the prefix for the FIB entry
166 */
167 CcnxFibEntry (const Ptr<const CcnxNameComponents> &prefix)
168 : m_prefix (prefix)
169 , m_needsProbing (false)
170 { }
171
172 /**
173 * \brief Update status of FIB next hop
174 * \param status Status to set on the FIB entry
175 */
176 void UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status);
177
178 /**
179 * \brief Add or update routing metric of FIB next hop
180 *
181 * Initial status of the next hop is set to YELLOW
182 */
183 void AddOrUpdateRoutingMetric (Ptr<CcnxFace> face, int32_t metric);
184
185 /**
186 * @brief Invalidate face
187 *
188 * Set routing metric on all faces to max and status to RED
189 */
190 void
191 Invalidate ();
192
193 /**
194 * @brief Update RTT averages for the face
195 */
196 void
197 UpdateFaceRtt (Ptr<CcnxFace> face, const Time &sample);
198
199 /**
200 * \brief Get prefix for the FIB entry
201 */
202 const CcnxNameComponents&
203 GetPrefix () const { return *m_prefix; }
204
205 /**
206 * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces)
207 *
208 * throws CcnxFibEntry::NoFaces if m_faces.size()==0
209 */
210 const CcnxFibFaceMetric &
211 FindBestCandidate (uint32_t skip = 0) const;
212
213 /**
214 * @brief Remove record associated with `face`
215 */
216 void
217 RemoveFace (const Ptr<CcnxFace> &face)
218 {
219 m_faces.erase (face);
220 }
221
222private:
223 friend std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
224
225public:
226 Ptr<const CcnxNameComponents> m_prefix; ///< \brief Prefix of the FIB entry
227 CcnxFibFaceMetricContainer::type m_faces; ///< \brief Indexed list of faces
228
229 bool m_needsProbing; ///< \brief flag indicating that probing should be performed
230};
231
232std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry);
233std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric);
234
235} // ns3
236
237#endif // _CCNX_FIB_ENTRY_H_