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