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 | 7fd74f9 | 2011-08-25 19:40:17 -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> |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 35 | #include <boost/multi_index/random_access_index.hpp> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 36 | #include <boost/multi_index/member.hpp> |
| 37 | #include <boost/multi_index/mem_fun.hpp> |
| 38 | |
| 39 | #include <iostream> |
| 40 | |
| 41 | namespace ns3 { |
| 42 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 43 | class CcnxInterestHeader; |
| 44 | |
| 45 | /** |
| 46 | * \ingroup ccnx |
| 47 | * \brief Structure holding various parameters associated with a (FibEntry, Face) tuple |
| 48 | */ |
| 49 | class CcnxFibFaceMetric |
| 50 | { |
| 51 | public: |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 52 | enum Status { NDN_FIB_GREEN = 1, |
| 53 | NDN_FIB_YELLOW = 2, |
| 54 | NDN_FIB_RED = 3 }; |
| 55 | public: |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 56 | /** |
| 57 | * \brief Metric constructor |
| 58 | * |
| 59 | * \param face Face for which metric |
| 60 | * \param cost Initial value for routing cost |
| 61 | */ |
| 62 | CcnxFibFaceMetric (Ptr<CcnxFace> face, int cost) |
| 63 | : m_face (face) |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 64 | , m_status (NDN_FIB_YELLOW) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 65 | , m_routingCost (cost) |
| 66 | , m_sRtt (Seconds (0)) |
| 67 | , m_rttVar (Seconds (0)) |
| 68 | { } |
| 69 | |
| 70 | /** |
| 71 | * \brief Comparison operator used by boost::multi_index::identity<> |
| 72 | */ |
| 73 | bool |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 74 | operator< (const CcnxFibFaceMetric &fm) const { return *m_face < *fm.m_face; } // return identity of the face |
| 75 | |
| 76 | bool |
| 77 | operator< (const Ptr<CcnxFace> &face) const { return *m_face < *face; } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 79 | Ptr<CcnxFace> |
| 80 | GetFace () const { return m_face; } |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * \brief Unary function to recalculate smoothed RTT and RTT variation |
| 84 | * \param rttSample RTT sample |
| 85 | */ |
| 86 | struct UpdateRtt |
| 87 | { |
| 88 | UpdateRtt (const Time &rttSample) : m_rttSample (rttSample) {}; |
| 89 | void operator() (CcnxFibFaceMetric &entry); |
| 90 | private: |
| 91 | const Time &m_rttSample; |
| 92 | }; |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 94 | private: |
| 95 | friend std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric); |
| 96 | public: |
| 97 | Ptr<CcnxFace> m_face; ///< Face |
| 98 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 99 | Status m_status; ///< \brief Status of the next hop: |
| 100 | ///< - NDN_FIB_GREEN |
| 101 | ///< - NDN_FIB_YELLOW |
| 102 | ///< - NDN_FIB_RED |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 103 | |
| 104 | uint32_t m_routingCost; ///< \brief routing protocol cost (interpretation of the value depends on the underlying routing protocol) |
| 105 | |
| 106 | Time m_sRtt; ///< \brief smoothed round-trip time |
| 107 | Time m_rttVar; ///< \brief round-trip time variation |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * \ingroup ccnx |
| 112 | * \brief Typedef for indexed face container of CcnxFibEntry |
| 113 | * |
| 114 | * Currently, there are 2 indexes: |
| 115 | * - by face (used to find record and update metric) |
| 116 | * - by metric (face ranking) |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 117 | * - random access index (for fast lookup on nth face). Order is |
| 118 | * maintained manually to be equal to the 'by metric' order |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 119 | */ |
| 120 | struct CcnxFibFaceMetricContainer |
| 121 | { |
| 122 | typedef boost::multi_index::multi_index_container< |
| 123 | CcnxFibFaceMetric, |
| 124 | boost::multi_index::indexed_by< |
| 125 | // For fast access to elements using CcnxFace |
| 126 | boost::multi_index::ordered_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 127 | boost::multi_index::tag<__ccnx_private::i_face>, |
| 128 | boost::multi_index::member<CcnxFibFaceMetric,Ptr<CcnxFace>,&CcnxFibFaceMetric::m_face> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 129 | >, |
| 130 | |
| 131 | // List of available faces ordered by (status, m_routingCost) |
| 132 | boost::multi_index::ordered_non_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 133 | boost::multi_index::tag<__ccnx_private::i_metric>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 134 | boost::multi_index::composite_key< |
| 135 | CcnxFibFaceMetric, |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 136 | boost::multi_index::member<CcnxFibFaceMetric,CcnxFibFaceMetric::Status,&CcnxFibFaceMetric::m_status>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 137 | boost::multi_index::member<CcnxFibFaceMetric,uint32_t,&CcnxFibFaceMetric::m_routingCost> |
| 138 | > |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 139 | >, |
| 140 | |
| 141 | // To optimize nth candidate selection (sacrifice a little bit space to gain speed) |
| 142 | boost::multi_index::random_access< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 143 | boost::multi_index::tag<__ccnx_private::i_nth> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 144 | > |
| 145 | > |
| 146 | > type; |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * \ingroup ccnx |
| 151 | * \brief Structure for FIB table entry, holding indexed list of |
| 152 | * available faces and their respective metrics |
| 153 | */ |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 154 | class CcnxFibEntry : public SimpleRefCount<CcnxFibEntry> |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 155 | { |
| 156 | public: |
| 157 | /** |
| 158 | * \brief Constructor |
| 159 | * \param prefix Prefix for the FIB entry |
| 160 | */ |
| 161 | CcnxFibEntry (Ptr<CcnxNameComponents> prefix) |
| 162 | : m_prefix (prefix) |
| 163 | , m_needsProbing (false) |
| 164 | { } |
| 165 | |
| 166 | /** |
| 167 | * \brief Update status of FIB next hop |
| 168 | */ |
| 169 | void |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 170 | UpdateStatus (Ptr<CcnxFace> face, CcnxFibFaceMetric::Status status); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * \brief Get prefix for the FIB entry |
| 174 | */ |
| 175 | const CcnxNameComponents& |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 176 | GetPrefix () const { return *m_prefix; } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 178 | /** |
| 179 | * \brief Find "best route" candidate, skipping `skip' first candidates (modulo # of faces) |
| 180 | */ |
| 181 | Ptr<CcnxFace> |
| 182 | FindBestCandidate (int skip = 0); |
| 183 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 184 | private: |
| 185 | friend std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry); |
| 186 | |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 187 | public: |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 188 | Ptr<CcnxNameComponents> m_prefix; ///< \brief Prefix of the FIB entry |
| 189 | CcnxFibFaceMetricContainer::type m_faces; ///< \brief Indexed list of faces |
| 190 | |
| 191 | bool m_needsProbing; ///< \brief flag indicating that probing should be performed |
| 192 | }; |
| 193 | |
| 194 | /////////////////////////////////////////////////////////////////////////////// |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | /** |
| 198 | * \ingroup ccnx |
| 199 | * \brief Typedef for indexed container for FIB entries |
| 200 | * |
| 201 | * Currently, there is only one index |
| 202 | * - by prefix hash, which is used to perform prefix match |
| 203 | */ |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 204 | struct CcnxFibEntryContainer |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 205 | { |
| 206 | typedef boost::multi_index::multi_index_container< |
| 207 | CcnxFibEntry, |
| 208 | boost::multi_index::indexed_by< |
| 209 | // For fast access to elements using CcnxFace |
| 210 | boost::multi_index::hashed_unique< |
Alexander Afanasyev | 78cf0c9 | 2011-09-01 19:57:14 -0700 | [diff] [blame] | 211 | boost::multi_index::tag<__ccnx_private::i_prefix>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 212 | boost::multi_index::const_mem_fun<CcnxFibEntry, |
| 213 | const CcnxNameComponents&, |
Alexander Afanasyev | a98cdd2 | 2011-08-29 17:32:37 -0700 | [diff] [blame] | 214 | &CcnxFibEntry::GetPrefix>, |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 215 | CcnxPrefixHash> |
| 216 | |
| 217 | // other indexes? |
| 218 | > |
| 219 | > type; |
| 220 | }; |
| 221 | |
| 222 | /** |
| 223 | * \ingroup ccnx |
| 224 | * \brief Class implementing FIB functionality |
| 225 | */ |
| 226 | class CcnxFib |
| 227 | { |
| 228 | public: |
| 229 | /** |
| 230 | * \brief Constructor |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 231 | */ |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 232 | CcnxFib (); |
| 233 | // * \param node smart pointer to Ccnx stack associated with particular node |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 234 | |
| 235 | // // Invalidate entries in FIB |
| 236 | // // Will leave FIB records in hash, but assign metric=NETWORK_UNREACHABLE |
| 237 | // void invalidate( ); |
| 238 | |
| 239 | // //Find corresponding FIB entry for the given content name |
| 240 | // //Longest match is performed |
| 241 | // FibIterator lookup( const string &name ); |
| 242 | // bool isValid( const FibIterator &it ) { return it!=_fib.end(); } |
| 243 | |
| 244 | /** |
| 245 | * \brief Perform longest prefix match |
| 246 | * |
| 247 | * \todo Implement exclude filters |
| 248 | * |
| 249 | * \param interest Interest packet header |
| 250 | * \returns If entry found a pair <valid_iterator, true> will be returned, otherwise <invalid_iterator, false> |
| 251 | */ |
| 252 | std::pair<CcnxFibEntryContainer::type::iterator, bool> |
| 253 | LongestPrefixMatch (const CcnxInterestHeader &interest) const; |
| 254 | |
| 255 | /** |
| 256 | * Update FIB entry |
| 257 | * If the entry exists, metric will be updated. Otherwise, new entry will be created |
| 258 | * |
| 259 | * Entries in FIB never deleted. They can be invalidated with metric==NETWORK_UNREACHABLE |
| 260 | * |
| 261 | * @param name Prefix |
| 262 | * @param interfaceIndex Forwarding interface |
| 263 | * @param metric Routing metric |
| 264 | * @param nextHop Nexthop node address (IPv4) |
| 265 | * @return true if a new entry created, false otherwise |
| 266 | */ |
| 267 | // bool update( const string &name, int interfaceIndex, int metric, NodeAddress nextHop ); |
| 268 | // bool update( NodeAddress nodeId, int interfaceIndex, int metric, NodeAddress nextHop ); |
| 269 | // Bool update( NodeAddress nodeId, int metric, NodeAddress nextHop ); |
| 270 | |
| 271 | // // Update Fib from OSPF routing table (through a hack in OSPF algorithm) |
| 272 | // void updateFibFromOSPFv2( int interface ); |
| 273 | |
| 274 | // // Update Fib from BGP routing table (using info from RibIn) |
| 275 | // void updateFibFromBGP( ); |
| 276 | |
| 277 | // // Update Fib from IP routing table |
| 278 | // void updateFibFromIpRouting( ); |
| 279 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 280 | // void dump( ); |
| 281 | // void dump( const FibIterator &fib ); |
| 282 | |
| 283 | // void resetProbing(); //reset needsProbing field for every FibEntry |
| 284 | private: |
| 285 | friend std::ostream& operator<< (std::ostream& os, const CcnxFib &fib); |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 286 | CcnxFib(const CcnxFib&) {} ; ///< \brief copy constructor is disabled |
| 287 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 288 | private: |
Alexander Afanasyev | a67e28c | 2011-08-31 21:16:25 -0700 | [diff] [blame] | 289 | // Ptr<Ccnx> m_node; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 290 | |
| 291 | CcnxFibEntryContainer::type m_fib; |
| 292 | }; |
| 293 | |
| 294 | /////////////////////////////////////////////////////////////////////////////// |
| 295 | /////////////////////////////////////////////////////////////////////////////// |
| 296 | |
| 297 | std::ostream& operator<< (std::ostream& os, const CcnxFib &fib); |
| 298 | std::ostream& operator<< (std::ostream& os, const CcnxFibEntry &entry); |
| 299 | std::ostream& operator<< (std::ostream& os, const CcnxFibFaceMetric &metric); |
| 300 | |
| 301 | } // namespace ns3 |
| 302 | |
| 303 | #endif /* NDN_FIB_H */ |