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