Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame^] | 41 | /** \brief references a route |
| 42 | */ |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 43 | struct RibRouteRef |
| 44 | { |
| 45 | shared_ptr<RibEntry> entry; |
| 46 | RibEntry::const_iterator route; |
| 47 | }; |
| 48 | |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 49 | bool |
| 50 | operator<(const RibRouteRef& lhs, const RibRouteRef& rhs); |
| 51 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 52 | /** \brief represents the Routing Information Base |
| 53 | |
| 54 | The Routing Information Base contains a collection of Routes, each |
| 55 | represents a piece of static or dynamic routing information |
| 56 | registered by applications, operators, or NFD itself. Routes |
| 57 | associated with the same namespace are collected into a RIB entry. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 58 | */ |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 59 | class Rib : noncopyable |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 60 | { |
| 61 | public: |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 62 | typedef std::list<shared_ptr<RibEntry>> RibEntryList; |
| 63 | typedef std::map<Name, shared_ptr<RibEntry>> RibTable; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 64 | typedef RibTable::const_iterator const_iterator; |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 65 | typedef std::map<uint64_t, std::list<shared_ptr<RibEntry>>> FaceLookupTable; |
| 66 | typedef bool (*RouteComparePredicate)(const Route&, const Route&); |
| 67 | typedef std::set<Route, RouteComparePredicate> RouteSet; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 68 | |
| 69 | Rib(); |
| 70 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 71 | void |
| 72 | setFibUpdater(FibUpdater* updater); |
| 73 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 74 | const_iterator |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 75 | find(const Name& prefix) const; |
| 76 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 77 | Route* |
| 78 | find(const Name& prefix, const Route& route) const; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 79 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 80 | const_iterator |
| 81 | begin() const; |
| 82 | |
| 83 | const_iterator |
| 84 | end() const; |
| 85 | |
| 86 | size_t |
| 87 | size() const; |
| 88 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 89 | bool |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 90 | empty() const; |
| 91 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 92 | shared_ptr<RibEntry> |
| 93 | findParent(const Name& prefix) const; |
| 94 | |
| 95 | /** \brief finds namespaces under the passed prefix |
| 96 | * \return{ a list of entries which are under the passed prefix } |
| 97 | */ |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 98 | std::list<shared_ptr<RibEntry>> |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 99 | findDescendants(const Name& prefix) const; |
| 100 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 101 | /** \brief finds namespaces under the passed prefix |
| 102 | * |
| 103 | * \note Unlike findDescendants, needs to find where prefix would fit in tree |
| 104 | * before collecting list of descendant prefixes |
| 105 | * |
| 106 | * \return{ a list of entries which would be under the passed prefix if the prefix |
| 107 | * existed in the RIB } |
| 108 | */ |
| 109 | std::list<shared_ptr<RibEntry>> |
| 110 | findDescendantsForNonInsertedName(const Name& prefix) const; |
| 111 | |
| 112 | public: |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 113 | using UpdateSuccessCallback = std::function<void()>; |
| 114 | using UpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 115 | |
| 116 | /** \brief passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates. |
| 117 | * |
| 118 | * If the FIB is updated successfully, onFibUpdateSuccess() will be called, and the |
| 119 | * RIB will be updated |
| 120 | * |
| 121 | * If the FIB update fails, onFibUpdateFailure() will be called, and the RIB will not |
| 122 | * be updated. |
| 123 | */ |
| 124 | void |
| 125 | beginApplyUpdate(const RibUpdate& update, |
| 126 | const UpdateSuccessCallback& onSuccess, |
| 127 | const UpdateFailureCallback& onFailure); |
| 128 | |
| 129 | /** \brief starts the FIB update process when a face has been destroyed |
| 130 | */ |
| 131 | void |
| 132 | beginRemoveFace(uint64_t faceId); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 133 | |
| 134 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 135 | onFibUpdateSuccess(const RibUpdateBatch& batch, |
| 136 | const RibUpdateList& inheritedRoutes, |
| 137 | const Rib::UpdateSuccessCallback& onSuccess); |
| 138 | |
| 139 | void |
| 140 | onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure, |
| 141 | uint32_t code, const std::string& error); |
| 142 | |
| 143 | void |
| 144 | onRouteExpiration(const Name& prefix, const Route& route); |
| 145 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 146 | void |
| 147 | insert(const Name& prefix, const Route& route); |
| 148 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 149 | private: |
| 150 | /** \brief adds the passed update to a RibUpdateBatch and adds the batch to |
| 151 | * the end of the update queue. |
| 152 | * |
| 153 | * If an update is not in progress, the front update batch in the queue will be |
| 154 | * processed by the RIB. |
| 155 | * |
| 156 | * If an update is in progress, the added update will eventually be processed |
| 157 | * when it reaches the front of the queue; after other update batches are |
| 158 | * processed, the queue is advanced. |
| 159 | */ |
| 160 | void |
| 161 | addUpdateToQueue(const RibUpdate& update, |
| 162 | const Rib::UpdateSuccessCallback& onSuccess, |
| 163 | const Rib::UpdateFailureCallback& onFailure); |
| 164 | |
| 165 | /** \brief Attempts to send the front update batch in the queue. |
| 166 | * |
| 167 | * If an update is not in progress, the front update batch in the queue will be |
| 168 | * sent to the RIB for processing. |
| 169 | * |
| 170 | * If an update is in progress, nothing will be done. |
| 171 | */ |
| 172 | void |
| 173 | sendBatchFromQueue(); |
| 174 | |
| 175 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 176 | #ifdef WITH_TESTS |
| 177 | /** \brief In unit tests, mock FIB update result. |
| 178 | * |
| 179 | * If the callback is not nullptr, sendBatchFromQueue() immediately succeeds or fails according |
| 180 | * to the return value of callback function. |
| 181 | */ |
| 182 | std::function<bool(const RibUpdateBatch&)> mockFibResponse; |
| 183 | |
| 184 | bool wantMockFibResponseOnce = false; ///< if true, mockFibResponse is cleared after every use. |
| 185 | #endif |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 186 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 187 | void |
| 188 | erase(const Name& prefix, const Route& route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 189 | |
| 190 | private: |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 191 | RibTable::iterator |
| 192 | eraseEntry(RibTable::iterator it); |
| 193 | |
| 194 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 195 | updateRib(const RibUpdateBatch& batch); |
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 | /** \brief returns routes inherited from the entry's ancestors |
| 198 | * |
| 199 | * \return{ a list of inherited routes } |
| 200 | */ |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 201 | RouteSet |
| 202 | getAncestorRoutes(const RibEntry& entry) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 203 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 204 | /** \brief returns routes inherited from the parent of the name and the parent's ancestors |
| 205 | * |
| 206 | * \note A parent is first found for the passed name before inherited routes are collected |
| 207 | * |
| 208 | * \return{ a list of inherited routes } |
| 209 | */ |
| 210 | RouteSet |
| 211 | getAncestorRoutes(const Name& name) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 212 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 213 | /** \brief applies the passed inheritedRoutes and their actions to the corresponding RibEntries' |
| 214 | * inheritedRoutes lists |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 215 | */ |
| 216 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 217 | modifyInheritedRoutes(const RibUpdateList& inheritedRoutes); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 218 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 219 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 220 | using NameAndRoute = std::pair<const Name&, const Route&>; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 221 | |
| 222 | std::list<NameAndRoute> |
| 223 | findRoutesWithFaceId(uint64_t faceId); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 224 | |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 225 | public: |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 226 | /** \brief signals after a RIB entry is inserted |
| 227 | * |
| 228 | * A RIB entry is inserted when the first route associated with a |
| 229 | * certain namespace is added. |
| 230 | */ |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 231 | ndn::util::signal::Signal<Rib, Name> afterInsertEntry; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 232 | |
| 233 | /** \brief signals after a RIB entry is erased |
| 234 | * |
| 235 | * A RIB entry is erased when the last route associated with a |
| 236 | * certain namespace is removed. |
| 237 | */ |
| 238 | |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 239 | ndn::util::signal::Signal<Rib, Name> afterEraseEntry; |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 240 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 241 | /** \brief signals after a Route is added |
| 242 | */ |
| 243 | ndn::util::signal::Signal<Rib, RibRouteRef> afterAddRoute; |
| 244 | |
| 245 | /** \brief signals before a route is removed |
| 246 | */ |
| 247 | ndn::util::signal::Signal<Rib, RibRouteRef> beforeRemoveRoute; |
| 248 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 249 | private: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 250 | RibTable m_rib; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 251 | FaceLookupTable m_faceMap; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 252 | FibUpdater* m_fibUpdater; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 253 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 254 | size_t m_nItems; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 255 | |
| 256 | friend class FibUpdater; |
| 257 | |
| 258 | private: |
| 259 | struct UpdateQueueItem |
| 260 | { |
| 261 | RibUpdateBatch batch; |
| 262 | const Rib::UpdateSuccessCallback managerSuccessCallback; |
| 263 | const Rib::UpdateFailureCallback managerFailureCallback; |
| 264 | }; |
| 265 | |
| 266 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 267 | typedef std::list<UpdateQueueItem> UpdateQueue; |
| 268 | UpdateQueue m_updateBatches; |
| 269 | |
| 270 | private: |
| 271 | bool m_isUpdateInProgress; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | inline Rib::const_iterator |
| 275 | Rib::begin() const |
| 276 | { |
| 277 | return m_rib.begin(); |
| 278 | } |
| 279 | |
| 280 | inline Rib::const_iterator |
| 281 | Rib::end() const |
| 282 | { |
| 283 | return m_rib.end(); |
| 284 | } |
| 285 | |
| 286 | inline size_t |
| 287 | Rib::size() const |
| 288 | { |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 289 | return m_nItems; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 292 | inline bool |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 293 | Rib::empty() const |
| 294 | { |
| 295 | return m_rib.empty(); |
| 296 | } |
| 297 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 298 | std::ostream& |
| 299 | operator<<(std::ostream& os, const Rib& rib); |
| 300 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 301 | } // namespace rib |
| 302 | } // namespace nfd |
| 303 | |
| 304 | #endif // NFD_RIB_RIB_HPP |