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 | /* |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_RIB_RIB_HPP |
| 27 | #define NFD_DAEMON_RIB_RIB_HPP |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 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: |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 62 | using RibEntryList = std::list<shared_ptr<RibEntry>>; |
| 63 | using RibTable = std::map<Name, shared_ptr<RibEntry>>; |
| 64 | using const_iterator = RibTable::const_iterator; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 65 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 66 | void |
| 67 | setFibUpdater(FibUpdater* updater); |
| 68 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 69 | const_iterator |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 70 | find(const Name& prefix) const; |
| 71 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 72 | Route* |
| 73 | find(const Name& prefix, const Route& route) const; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 74 | |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 75 | Route* |
| 76 | findLongestPrefix(const Name& prefix, const Route& route) const; |
| 77 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 78 | const_iterator |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 79 | begin() const |
| 80 | { |
| 81 | return m_rib.begin(); |
| 82 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 83 | |
| 84 | const_iterator |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 85 | end() const |
| 86 | { |
| 87 | return m_rib.end(); |
| 88 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 89 | |
| 90 | size_t |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 91 | size() const |
| 92 | { |
| 93 | return m_nItems; |
| 94 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 95 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 96 | bool |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 97 | empty() const |
| 98 | { |
| 99 | return m_rib.empty(); |
| 100 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 101 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 102 | shared_ptr<RibEntry> |
| 103 | findParent(const Name& prefix) const; |
| 104 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 105 | public: |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 106 | using UpdateSuccessCallback = std::function<void()>; |
| 107 | using UpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 108 | |
| 109 | /** \brief passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates. |
| 110 | * |
| 111 | * If the FIB is updated successfully, onFibUpdateSuccess() will be called, and the |
| 112 | * RIB will be updated |
| 113 | * |
| 114 | * If the FIB update fails, onFibUpdateFailure() will be called, and the RIB will not |
| 115 | * be updated. |
| 116 | */ |
| 117 | void |
| 118 | beginApplyUpdate(const RibUpdate& update, |
| 119 | const UpdateSuccessCallback& onSuccess, |
| 120 | const UpdateFailureCallback& onFailure); |
| 121 | |
| 122 | /** \brief starts the FIB update process when a face has been destroyed |
| 123 | */ |
| 124 | void |
| 125 | beginRemoveFace(uint64_t faceId); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 126 | |
| 127 | void |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 128 | beginRemoveFailedFaces(const std::set<uint64_t>& activeFaceIds); |
| 129 | |
| 130 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 131 | onRouteExpiration(const Name& prefix, const Route& route); |
| 132 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 133 | void |
| 134 | insert(const Name& prefix, const Route& route); |
| 135 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 136 | private: |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 137 | void |
| 138 | enqueueRemoveFace(const RibEntry& entry, uint64_t faceId); |
| 139 | |
Junxiao Shi | 5153338 | 2019-07-19 13:50:26 +0000 | [diff] [blame] | 140 | /** \brief Append the RIB update to the update queue. |
| 141 | * |
| 142 | * To start updates, invoke sendBatchFromQueue() . |
| 143 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 144 | void |
| 145 | addUpdateToQueue(const RibUpdate& update, |
| 146 | const Rib::UpdateSuccessCallback& onSuccess, |
| 147 | const Rib::UpdateFailureCallback& onFailure); |
| 148 | |
Junxiao Shi | 5153338 | 2019-07-19 13:50:26 +0000 | [diff] [blame] | 149 | /** \brief Send the first update batch in the queue, if no other update is in progress. |
| 150 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 151 | void |
| 152 | sendBatchFromQueue(); |
| 153 | |
Junxiao Shi | 5153338 | 2019-07-19 13:50:26 +0000 | [diff] [blame] | 154 | void |
| 155 | onFibUpdateSuccess(const RibUpdateBatch& batch, |
| 156 | const RibUpdateList& inheritedRoutes, |
| 157 | const Rib::UpdateSuccessCallback& onSuccess); |
| 158 | |
| 159 | void |
| 160 | onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure, |
| 161 | uint32_t code, const std::string& error); |
| 162 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 163 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 164 | void |
| 165 | erase(const Name& prefix, const Route& route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 166 | |
| 167 | private: |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 168 | using RouteComparePredicate = bool (*)(const Route&, const Route&); |
| 169 | using RouteSet = std::set<Route, RouteComparePredicate>; |
| 170 | |
Junxiao Shi | 5153338 | 2019-07-19 13:50:26 +0000 | [diff] [blame] | 171 | /** \brief find entries under \p prefix |
| 172 | * \pre a RIB entry exists at \p prefix |
| 173 | */ |
| 174 | std::list<shared_ptr<RibEntry>> |
| 175 | findDescendants(const Name& prefix) const; |
| 176 | |
| 177 | /** \brief find entries under \p prefix |
| 178 | * \pre a RIB entry does not exist at \p prefix |
| 179 | */ |
| 180 | std::list<shared_ptr<RibEntry>> |
| 181 | findDescendantsForNonInsertedName(const Name& prefix) const; |
| 182 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 183 | RibTable::iterator |
| 184 | eraseEntry(RibTable::iterator it); |
| 185 | |
| 186 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 187 | updateRib(const RibUpdateBatch& batch); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 188 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 189 | /** \brief returns routes inherited from the entry's ancestors |
| 190 | * |
| 191 | * \return{ a list of inherited routes } |
| 192 | */ |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 193 | RouteSet |
| 194 | getAncestorRoutes(const RibEntry& entry) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 195 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 196 | /** \brief returns routes inherited from the parent of the name and the parent's ancestors |
| 197 | * |
| 198 | * \note A parent is first found for the passed name before inherited routes are collected |
| 199 | * |
| 200 | * \return{ a list of inherited routes } |
| 201 | */ |
| 202 | RouteSet |
| 203 | getAncestorRoutes(const Name& name) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 204 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 205 | /** \brief applies the passed inheritedRoutes and their actions to the corresponding RibEntries' |
| 206 | * inheritedRoutes lists |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 207 | */ |
| 208 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 209 | modifyInheritedRoutes(const RibUpdateList& inheritedRoutes); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 210 | |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 211 | public: |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 212 | /** \brief signals after a RIB entry is inserted |
| 213 | * |
| 214 | * A RIB entry is inserted when the first route associated with a |
| 215 | * certain namespace is added. |
| 216 | */ |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 217 | signal::Signal<Rib, Name> afterInsertEntry; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 218 | |
| 219 | /** \brief signals after a RIB entry is erased |
| 220 | * |
| 221 | * A RIB entry is erased when the last route associated with a |
| 222 | * certain namespace is removed. |
| 223 | */ |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 224 | signal::Signal<Rib, Name> afterEraseEntry; |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 225 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 226 | /** \brief signals after a Route is added |
| 227 | */ |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 228 | signal::Signal<Rib, RibRouteRef> afterAddRoute; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 229 | |
| 230 | /** \brief signals before a route is removed |
| 231 | */ |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 232 | signal::Signal<Rib, RibRouteRef> beforeRemoveRoute; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 233 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 234 | private: |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 235 | RibTable m_rib; |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 236 | std::multimap<uint64_t, shared_ptr<RibEntry>> m_faceEntries; ///< FaceId => Entry with Route on this face |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 237 | size_t m_nItems = 0; |
| 238 | FibUpdater* m_fibUpdater = nullptr; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 239 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 240 | struct UpdateQueueItem |
| 241 | { |
| 242 | RibUpdateBatch batch; |
| 243 | const Rib::UpdateSuccessCallback managerSuccessCallback; |
| 244 | const Rib::UpdateFailureCallback managerFailureCallback; |
| 245 | }; |
| 246 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 247 | using UpdateQueue = std::list<UpdateQueueItem>; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 248 | UpdateQueue m_updateBatches; |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 249 | bool m_isUpdateInProgress = false; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 250 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame^] | 251 | friend class FibUpdater; |
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 | std::ostream& |
| 255 | operator<<(std::ostream& os, const Rib& rib); |
| 256 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 257 | } // namespace rib |
| 258 | } // namespace nfd |
| 259 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 260 | #endif // NFD_DAEMON_RIB_RIB_HPP |