Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
| 26 | #ifndef NFD_RIB_RIB_HPP |
| 27 | #define NFD_RIB_RIB_HPP |
| 28 | |
| 29 | #include "common.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 30 | |
| 31 | #include "rib-entry.hpp" |
| 32 | #include "rib-update-batch.hpp" |
| 33 | |
| 34 | #include <ndn-cxx/management/nfd-control-parameters.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
| 37 | namespace rib { |
| 38 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 39 | using ndn::nfd::ControlParameters; |
| 40 | |
| 41 | class FibUpdater; |
| 42 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 43 | /** \brief represents the RIB |
| 44 | */ |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 45 | class Rib : noncopyable |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 46 | { |
| 47 | public: |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 48 | typedef std::list<shared_ptr<RibEntry>> RibEntryList; |
| 49 | typedef std::map<Name, shared_ptr<RibEntry>> RibTable; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 50 | typedef RibTable::const_iterator const_iterator; |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 51 | typedef std::map<uint64_t, std::list<shared_ptr<RibEntry>>> FaceLookupTable; |
| 52 | typedef bool (*RouteComparePredicate)(const Route&, const Route&); |
| 53 | typedef std::set<Route, RouteComparePredicate> RouteSet; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 54 | |
| 55 | Rib(); |
| 56 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 57 | ~Rib(); |
| 58 | |
| 59 | void |
| 60 | setFibUpdater(FibUpdater* updater); |
| 61 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 62 | const_iterator |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 63 | find(const Name& prefix) const; |
| 64 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 65 | Route* |
| 66 | find(const Name& prefix, const Route& route) const; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 68 | const_iterator |
| 69 | begin() const; |
| 70 | |
| 71 | const_iterator |
| 72 | end() const; |
| 73 | |
| 74 | size_t |
| 75 | size() const; |
| 76 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 77 | bool |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 78 | empty() const; |
| 79 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 80 | shared_ptr<RibEntry> |
| 81 | findParent(const Name& prefix) const; |
| 82 | |
| 83 | /** \brief finds namespaces under the passed prefix |
| 84 | * \return{ a list of entries which are under the passed prefix } |
| 85 | */ |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 86 | std::list<shared_ptr<RibEntry>> |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 87 | findDescendants(const Name& prefix) const; |
| 88 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 89 | /** \brief finds namespaces under the passed prefix |
| 90 | * |
| 91 | * \note Unlike findDescendants, needs to find where prefix would fit in tree |
| 92 | * before collecting list of descendant prefixes |
| 93 | * |
| 94 | * \return{ a list of entries which would be under the passed prefix if the prefix |
| 95 | * existed in the RIB } |
| 96 | */ |
| 97 | std::list<shared_ptr<RibEntry>> |
| 98 | findDescendantsForNonInsertedName(const Name& prefix) const; |
| 99 | |
| 100 | public: |
| 101 | typedef function<void()> UpdateSuccessCallback; |
| 102 | typedef function<void(uint32_t code, const std::string& error)> UpdateFailureCallback; |
| 103 | |
| 104 | /** \brief passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates. |
| 105 | * |
| 106 | * If the FIB is updated successfully, onFibUpdateSuccess() will be called, and the |
| 107 | * RIB will be updated |
| 108 | * |
| 109 | * If the FIB update fails, onFibUpdateFailure() will be called, and the RIB will not |
| 110 | * be updated. |
| 111 | */ |
| 112 | void |
| 113 | beginApplyUpdate(const RibUpdate& update, |
| 114 | const UpdateSuccessCallback& onSuccess, |
| 115 | const UpdateFailureCallback& onFailure); |
| 116 | |
| 117 | /** \brief starts the FIB update process when a face has been destroyed |
| 118 | */ |
| 119 | void |
| 120 | beginRemoveFace(uint64_t faceId); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 121 | |
| 122 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 123 | onFibUpdateSuccess(const RibUpdateBatch& batch, |
| 124 | const RibUpdateList& inheritedRoutes, |
| 125 | const Rib::UpdateSuccessCallback& onSuccess); |
| 126 | |
| 127 | void |
| 128 | onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure, |
| 129 | uint32_t code, const std::string& error); |
| 130 | |
| 131 | void |
| 132 | onRouteExpiration(const Name& prefix, const Route& route); |
| 133 | |
| 134 | private: |
| 135 | /** \brief adds the passed update to a RibUpdateBatch and adds the batch to |
| 136 | * the end of the update queue. |
| 137 | * |
| 138 | * If an update is not in progress, the front update batch in the queue will be |
| 139 | * processed by the RIB. |
| 140 | * |
| 141 | * If an update is in progress, the added update will eventually be processed |
| 142 | * when it reaches the front of the queue; after other update batches are |
| 143 | * processed, the queue is advanced. |
| 144 | */ |
| 145 | void |
| 146 | addUpdateToQueue(const RibUpdate& update, |
| 147 | const Rib::UpdateSuccessCallback& onSuccess, |
| 148 | const Rib::UpdateFailureCallback& onFailure); |
| 149 | |
| 150 | /** \brief Attempts to send the front update batch in the queue. |
| 151 | * |
| 152 | * If an update is not in progress, the front update batch in the queue will be |
| 153 | * sent to the RIB for processing. |
| 154 | * |
| 155 | * If an update is in progress, nothing will be done. |
| 156 | */ |
| 157 | void |
| 158 | sendBatchFromQueue(); |
| 159 | |
| 160 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 161 | // Used by RibManager unit-tests to get sent batch to simulate successful FIB update |
| 162 | function<void(RibUpdateBatch)> m_onSendBatchFromQueue; |
| 163 | |
| 164 | public: |
| 165 | void |
| 166 | insert(const Name& prefix, const Route& route); |
| 167 | |
| 168 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 169 | void |
| 170 | erase(const Name& prefix, const Route& route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 171 | |
| 172 | private: |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 173 | RibTable::iterator |
| 174 | eraseEntry(RibTable::iterator it); |
| 175 | |
| 176 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 177 | updateRib(const RibUpdateBatch& batch); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 178 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 179 | /** \brief returns routes inherited from the entry's ancestors |
| 180 | * |
| 181 | * \return{ a list of inherited routes } |
| 182 | */ |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 183 | RouteSet |
| 184 | getAncestorRoutes(const RibEntry& entry) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 185 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 186 | /** \brief returns routes inherited from the parent of the name and the parent's ancestors |
| 187 | * |
| 188 | * \note A parent is first found for the passed name before inherited routes are collected |
| 189 | * |
| 190 | * \return{ a list of inherited routes } |
| 191 | */ |
| 192 | RouteSet |
| 193 | getAncestorRoutes(const Name& name) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 194 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 195 | /** \brief applies the passed inheritedRoutes and their actions to the corresponding RibEntries' |
| 196 | * inheritedRoutes lists |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 197 | */ |
| 198 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 199 | modifyInheritedRoutes(const RibUpdateList& inheritedRoutes); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 200 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 201 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 202 | typedef std::pair<const Name&,const Route&> NameAndRoute; |
| 203 | |
| 204 | std::list<NameAndRoute> |
| 205 | findRoutesWithFaceId(uint64_t faceId); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 206 | |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 207 | public: |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 208 | ndn::util::signal::Signal<Rib, Name> afterInsertEntry; |
| 209 | ndn::util::signal::Signal<Rib, Name> afterEraseEntry; |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 210 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 211 | private: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 212 | RibTable m_rib; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 213 | FaceLookupTable m_faceMap; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 214 | FibUpdater* m_fibUpdater; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 215 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 216 | size_t m_nItems; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 217 | |
| 218 | friend class FibUpdater; |
| 219 | |
| 220 | private: |
| 221 | struct UpdateQueueItem |
| 222 | { |
| 223 | RibUpdateBatch batch; |
| 224 | const Rib::UpdateSuccessCallback managerSuccessCallback; |
| 225 | const Rib::UpdateFailureCallback managerFailureCallback; |
| 226 | }; |
| 227 | |
| 228 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 229 | typedef std::list<UpdateQueueItem> UpdateQueue; |
| 230 | UpdateQueue m_updateBatches; |
| 231 | |
| 232 | private: |
| 233 | bool m_isUpdateInProgress; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | inline Rib::const_iterator |
| 237 | Rib::begin() const |
| 238 | { |
| 239 | return m_rib.begin(); |
| 240 | } |
| 241 | |
| 242 | inline Rib::const_iterator |
| 243 | Rib::end() const |
| 244 | { |
| 245 | return m_rib.end(); |
| 246 | } |
| 247 | |
| 248 | inline size_t |
| 249 | Rib::size() const |
| 250 | { |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 251 | return m_nItems; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 254 | inline bool |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 255 | Rib::empty() const |
| 256 | { |
| 257 | return m_rib.empty(); |
| 258 | } |
| 259 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 260 | std::ostream& |
| 261 | operator<<(std::ostream& os, const Rib& rib); |
| 262 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 263 | } // namespace rib |
| 264 | } // namespace nfd |
| 265 | |
| 266 | #endif // NFD_RIB_RIB_HPP |