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