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