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