Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -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 | |
| 21 | #ifndef _CCNX_FIB_H_ |
| 22 | #define _CCNX_FIB_H_ |
| 23 | |
| 24 | #include "hash-helper.h" |
| 25 | #include "ccnx-face.h" |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 26 | #include "ccnx.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 27 | #include "ns3/nstime.h" |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 28 | #include "ns3/simple-ref-count.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 29 | #include "ns3/node.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 30 | |
| 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 Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 36 | #include <boost/multi_index/random_access_index.hpp> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 37 | #include <boost/multi_index/member.hpp> |
| 38 | #include <boost/multi_index/mem_fun.hpp> |
| 39 | |
| 40 | #include <iostream> |
| 41 | |
| 42 | namespace ns3 { |
| 43 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 44 | class CcnxInterestHeader; |
| 45 | |
| 46 | /** |
| 47 | * \ingroup ccnx |
| 48 | * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple |
| 49 | */ |
| 50 | class CcnxFibFaceMetric |
| 51 | { |
| 52 | public: |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 53 | enum Status { NDN_FIB_GREEN = 1, |
| 54 | NDN_FIB_YELLOW = 2, |
| 55 | NDN_FIB_RED = 3 }; |
| 56 | public: |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 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, int cost) |
| 64 | : m_face (face) |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 65 | , m_status (NDN_FIB_YELLOW) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 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 |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 75 | 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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 80 | Ptr<CcnxFace> |
| 81 | GetFace () const { return m_face; } |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 84 | * \brief Recalculate smoothed RTT and RTT variation |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 85 | * \param rttSample RTT sample |
| 86 | */ |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 87 | void |
| 88 | UpdateRtt (const Time &rttSample); |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 90 | private: |
| 91 | friend std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric); |
| 92 | public: |
| 93 | Ptr<CcnxFace> m_face; ///< Face |
| 94 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 95 | Status m_status; ///< \brief Status of the next hop: |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 96 | ///< - NDN_FIB_GREEN |
| 97 | ///< - NDN_FIB_YELLOW |
| 98 | ///< - NDN_FIB_RED |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 100 | int32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 101 | |
| 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 Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 113 | * - random access index (for fast lookup on nth face). Order is |
| 114 | * maintained manually to be equal to the 'by metric' order |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 115 | */ |
| 116 | struct 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 Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 123 | boost::multi_index::tag<__ccnx_private::i_face>, |
| 124 | boost::multi_index::member<CcnxFibFaceMetric,Ptr<CcnxFace>,&CcnxFibFaceMetric::m_face> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 125 | >, |
| 126 | |
| 127 | // List of available faces ordered by (status, m_routingCost) |
| 128 | boost::multi_index::ordered_non_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 129 | boost::multi_index::tag<__ccnx_private::i_metric>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 130 | boost::multi_index::composite_key< |
| 131 | CcnxFibFaceMetric, |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 132 | boost::multi_index::member<CcnxFibFaceMetric,CcnxFibFaceMetric::Status,&CcnxFibFaceMetric::m_status>, |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 133 | boost::multi_index::member<CcnxFibFaceMetric,int32_t,&CcnxFibFaceMetric::m_routingCost> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 134 | > |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 135 | >, |
| 136 | |
| 137 | // To optimize nth candidate selection (sacrifice a little bit space to gain speed) |
| 138 | boost::multi_index::random_access< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 139 | boost::multi_index::tag<__ccnx_private::i_nth> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 140 | > |
| 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 Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 150 | class CcnxFibEntry : public SimpleRefCount<CcnxFibEntry> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 151 | { |
| 152 | public: |
| 153 | /** |
| 154 | * \brief Constructor |
| 155 | * \param prefix Prefix for the FIB entry |
| 156 | */ |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 157 | CcnxFibEntry (const CcnxNameComponents &prefix) |
| 158 | : m_prefix (Create<CcnxNameComponents> (prefix)) |
| 159 | , m_needsProbing (false) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 160 | { } |
| 161 | |
| 162 | /** |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 163 | * \brief Update status of FIB next hop |
| 164 | * \param status Status to set on the FIB entry |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 165 | */ |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 166 | void UpdateStatus (const CcnxFace &face, CcnxFibFaceMetric::Status status); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 167 | |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame^] | 168 | // /** |
| 169 | // * \brief Add or update routing metric of FIB next hop |
| 170 | // * |
| 171 | // * Initial status of the next hop is set to YELLOW |
| 172 | // */ |
| 173 | // void AddOrUpdateRoutingMetric (Ptr<CcnxFace> face, int32_t metric); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * \brief Get prefix for the FIB entry |
| 177 | */ |
| 178 | const CcnxNameComponents& |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 179 | GetPrefix () const { return *m_prefix; } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 180 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 181 | /** |
| 182 | * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces) |
| 183 | */ |
| 184 | Ptr<CcnxFace> |
Ilya Moiseenko | 4e47348 | 2011-10-31 17:58:14 -0700 | [diff] [blame] | 185 | FindBestCandidate (int skip = 0) const; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 187 | private: |
| 188 | friend std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry); |
| 189 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 190 | public: |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 191 | Ptr<CcnxNameComponents> m_prefix; ///< \brief Prefix of the FIB entry |
| 192 | CcnxFibFaceMetricContainer::type m_faces; ///< \brief Indexed list of faces |
| 193 | |
| 194 | bool m_needsProbing; ///< \brief flag indicating that probing should be performed |
| 195 | }; |
| 196 | |
| 197 | /////////////////////////////////////////////////////////////////////////////// |
| 198 | /////////////////////////////////////////////////////////////////////////////// |
| 199 | |
| 200 | /** |
| 201 | * \ingroup ccnx |
| 202 | * \brief Typedef for indexed container for FIB entries |
| 203 | * |
| 204 | * Currently, there is only one index |
| 205 | * - by prefix hash, which is used to perform prefix match |
| 206 | */ |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 207 | struct CcnxFibEntryContainer |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 208 | { |
| 209 | typedef boost::multi_index::multi_index_container< |
| 210 | CcnxFibEntry, |
| 211 | boost::multi_index::indexed_by< |
| 212 | // For fast access to elements using CcnxFace |
| 213 | boost::multi_index::hashed_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 214 | boost::multi_index::tag<__ccnx_private::i_prefix>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 215 | boost::multi_index::const_mem_fun<CcnxFibEntry, |
| 216 | const CcnxNameComponents&, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 217 | &CcnxFibEntry::GetPrefix>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 218 | CcnxPrefixHash> |
| 219 | |
| 220 | // other indexes? |
| 221 | > |
| 222 | > type; |
| 223 | }; |
| 224 | |
| 225 | /** |
| 226 | * \ingroup ccnx |
| 227 | * \brief Class implementing FIB functionality |
| 228 | */ |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 229 | class CcnxFib : public Object, public CcnxFibEntryContainer::type |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 230 | { |
| 231 | public: |
| 232 | /** |
Alexander Afanasyev | cf133f0 | 2011-09-06 12:13:48 -0700 | [diff] [blame] | 233 | * \brief Interface ID |
| 234 | * |
| 235 | * \return interface ID |
| 236 | */ |
| 237 | static TypeId GetTypeId (); |
| 238 | |
| 239 | /** |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 240 | * \brief Constructor |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 241 | */ |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 242 | CcnxFib (); |
| 243 | // * \param node smart pointer to Ccnx stack associated with particular node |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 244 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 245 | /** |
| 246 | * \brief Perform longest prefix match |
| 247 | * |
| 248 | * \todo Implement exclude filters |
| 249 | * |
| 250 | * \param interest Interest packet header |
Alexander Afanasyev | 8accdf6 | 2011-09-20 11:33:59 -0700 | [diff] [blame] | 251 | * \returns If entry found a valid iterator will be returned, otherwise end () |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 252 | */ |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 253 | CcnxFibEntryContainer::type::iterator |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 254 | LongestPrefixMatch (const CcnxInterestHeader &interest) const; |
| 255 | |
| 256 | /** |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 257 | * \brief Add or update FIB entry |
| 258 | * |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 259 | * If the entry exists, metric will be updated. Otherwise, new entry will be created |
| 260 | * |
| 261 | * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE |
| 262 | * |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 263 | * @param name Prefix |
| 264 | * @param face Forwarding face |
| 265 | * @param metric Routing metric |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 266 | */ |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 267 | CcnxFibEntryContainer::type::iterator |
| 268 | Add (const CcnxNameComponents &prefix, Ptr<CcnxFace> face, int32_t metric); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 269 | |
| 270 | // void resetProbing(); //reset needsProbing field for every FibEntry |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 271 | |
| 272 | protected: |
| 273 | // inherited from Object class |
| 274 | virtual void NotifyNewAggregate (); |
| 275 | virtual void DoDispose (); |
| 276 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 277 | private: |
| 278 | friend std::ostream& operator<< (std::ostream& os, const CcnxFib &fib); |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 279 | CcnxFib(const CcnxFib&) {} ; ///< \brief copy constructor is disabled |
| 280 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 281 | private: |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 282 | Ptr<Node> m_node; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | /////////////////////////////////////////////////////////////////////////////// |
| 286 | /////////////////////////////////////////////////////////////////////////////// |
| 287 | |
| 288 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib); |
| 289 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry); |
| 290 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric); |
| 291 | |
| 292 | } // namespace ns3 |
| 293 | |
| 294 | #endif /* NDN_FIB_H */ |