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