Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [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 | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_RIB_FIB_UPDATER_HPP |
| 27 | #define NFD_DAEMON_RIB_FIB_UPDATER_HPP |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 28 | |
Junxiao Shi | 9f5b01d | 2016-08-05 03:54:28 +0000 | [diff] [blame] | 29 | #include "core/common.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 30 | #include "fib-update.hpp" |
| 31 | #include "rib.hpp" |
| 32 | #include "rib-update-batch.hpp" |
| 33 | |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/mgmt/nfd/controller.hpp> |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::rib { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 37 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 38 | /** |
| 39 | * \brief Computes FibUpdates based on updates to the RIB and sends them to NFD. |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 40 | */ |
| 41 | class FibUpdater : noncopyable |
| 42 | { |
| 43 | public: |
| 44 | class Error : public std::runtime_error |
| 45 | { |
| 46 | public: |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 47 | using std::runtime_error::runtime_error; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 48 | }; |
| 49 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 50 | using FibUpdateList = std::list<FibUpdate>; |
| 51 | using FibUpdateSuccessCallback = std::function<void(RibUpdateList inheritedRoutes)>; |
| 52 | using FibUpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 53 | |
| 54 | FibUpdater(Rib& rib, ndn::nfd::Controller& controller); |
| 55 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 56 | #ifdef NFD_WITH_TESTS |
| 57 | virtual |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 58 | ~FibUpdater() = default; |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 59 | #endif |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 60 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 61 | /** \brief Computes FibUpdates using the provided RibUpdateBatch and then sends the |
| 62 | * updates to NFD's FIB. |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 63 | * |
| 64 | * \note Caller must guarantee that the previous batch has either succeeded or failed |
| 65 | * before calling this method |
| 66 | */ |
| 67 | void |
| 68 | computeAndSendFibUpdates(const RibUpdateBatch& batch, |
| 69 | const FibUpdateSuccessCallback& onSuccess, |
| 70 | const FibUpdateFailureCallback& onFailure); |
| 71 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 72 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 73 | /** |
| 74 | * \brief Determines the type of action that will be performed on the RIB and calls the |
| 75 | * corresponding computation method. |
| 76 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 77 | void |
| 78 | computeUpdates(const RibUpdateBatch& batch); |
| 79 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 80 | /** |
| 81 | * \brief Sends the passed updates to NFD. |
| 82 | * |
| 83 | * onSuccess or onFailure will be called based on the results in |
| 84 | * onUpdateSuccess or onUpdateFailure. |
| 85 | * |
| 86 | * \see FibUpdater::onUpdateSuccess |
| 87 | * \see FibUpdater::onUpdateFailure |
| 88 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 89 | void |
| 90 | sendUpdates(const FibUpdateList& updates, |
| 91 | const FibUpdateSuccessCallback& onSuccess, |
| 92 | const FibUpdateFailureCallback& onFailure); |
| 93 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 94 | /** |
| 95 | * \brief Sends the updates in m_updatesForBatchFaceId to NFD if any exist, |
| 96 | * otherwise calls FibUpdater::sendUpdatesForNonBatchFaceId. |
| 97 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 98 | void |
| 99 | sendUpdatesForBatchFaceId(const FibUpdateSuccessCallback& onSuccess, |
| 100 | const FibUpdateFailureCallback& onFailure); |
| 101 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 102 | /** |
| 103 | * \brief Sends the updates in m_updatesForNonBatchFaceId to NFD if any exist, |
| 104 | * otherwise calls onSuccess. |
| 105 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 106 | void |
| 107 | sendUpdatesForNonBatchFaceId(const FibUpdateSuccessCallback& onSuccess, |
| 108 | const FibUpdateFailureCallback& onFailure); |
| 109 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 110 | NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 111 | /** |
| 112 | * \brief Sends a FibAddNextHopCommand to NFD using the parameters supplied by |
| 113 | * the passed update. |
| 114 | * \param nTimeouts the number of times this FibUpdate has failed due to timeout |
| 115 | */ |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 116 | NFD_VIRTUAL_WITH_TESTS void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 117 | sendAddNextHopUpdate(const FibUpdate& update, |
| 118 | const FibUpdateSuccessCallback& onSuccess, |
| 119 | const FibUpdateFailureCallback& onFailure, |
| 120 | uint32_t nTimeouts = 0); |
| 121 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 122 | /** |
| 123 | * \brief Sends a FibRemoveNextHopCommand to NFD using the parameters supplied by |
| 124 | * the passed update. |
| 125 | * \param nTimeouts the number of times this FibUpdate has failed due to timeout |
| 126 | */ |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 127 | NFD_VIRTUAL_WITH_TESTS void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 128 | sendRemoveNextHopUpdate(const FibUpdate& update, |
| 129 | const FibUpdateSuccessCallback& onSuccess, |
| 130 | const FibUpdateFailureCallback& onFailure, |
| 131 | uint32_t nTimeouts = 0); |
| 132 | |
| 133 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 134 | /** |
| 135 | * \brief Calculates the FibUpdates generated by a RIB registration. |
| 136 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 137 | void |
| 138 | computeUpdatesForRegistration(const RibUpdate& update); |
| 139 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 140 | /** |
| 141 | * \brief Calculates the FibUpdates generated by a RIB unregistration. |
| 142 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 143 | void |
| 144 | computeUpdatesForUnregistration(const RibUpdate& update); |
| 145 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 146 | NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 147 | /** |
| 148 | * \brief Callback used by NfdController when a FibAddNextHopCommand or FibRemoveNextHopCommand |
| 149 | * is successful. |
| 150 | * |
| 151 | * If the update has the same Face ID as the batch being processed, the update is |
| 152 | * removed from m_updatesForBatchFaceId. If m_updatesForBatchFaceId becomes empty, |
| 153 | * the updates with a different Face ID than the batch are sent to NFD. |
| 154 | * |
| 155 | * If the update has a different Face ID than the batch being processed, the update is |
| 156 | * removed from m_updatesForBatchNonFaceId. If m_updatesForBatchNonFaceId becomes empty, |
| 157 | * the FIB update process is considered a success. |
| 158 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 159 | void |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 160 | onUpdateSuccess(const FibUpdate& update, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 161 | const FibUpdateSuccessCallback& onSuccess, |
| 162 | const FibUpdateFailureCallback& onFailure); |
| 163 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 164 | /** |
| 165 | * \brief Callback used by NfdController when a FibAddNextHopCommand or FibRemoveNextHopCommand |
| 166 | * is successful. |
| 167 | * |
| 168 | * If the update has not reached the max number of timeouts allowed, the update |
| 169 | * is retried. |
| 170 | * |
| 171 | * If the update failed due to a non-existent face and the update has the same Face ID |
| 172 | * as the update batch, the FIB update process fails. |
| 173 | * |
| 174 | * If the update failed due to a non-existent face and the update has a different |
| 175 | * face than the update batch, the update is not retried and the error is |
| 176 | * ignored. |
| 177 | * |
| 178 | * Otherwise, a non-recoverable error has occurred and an exception is thrown. |
| 179 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 180 | void |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 181 | onUpdateError(const FibUpdate& update, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 182 | const FibUpdateSuccessCallback& onSuccess, |
| 183 | const FibUpdateFailureCallback& onFailure, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 184 | const ndn::nfd::ControlResponse& response, uint32_t nTimeouts); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 185 | |
| 186 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 187 | /** |
| 188 | * \brief Adds the update to an update list based on its Face ID. |
| 189 | * |
| 190 | * If the update has the same Face ID as the update batch, the update is added |
| 191 | * to m_updatesForBatchFaceId. |
| 192 | * |
| 193 | * Otherwise, the update is added to m_updatesForBatchNonFaceId. |
| 194 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 195 | void |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 196 | addFibUpdate(const FibUpdate& update); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 197 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 198 | /** |
| 199 | * \brief Creates records of the passed routes added to the entry and creates FIB updates. |
| 200 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 201 | void |
| 202 | addInheritedRoutes(const RibEntry& entry, const Rib::RouteSet& routesToAdd); |
| 203 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 204 | /** |
| 205 | * \brief Creates records of the passed routes added to the name and creates FIB updates. |
| 206 | * Routes in \p routesToAdd with the same Face ID as ignore will be not be considered. |
| 207 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 208 | void |
| 209 | addInheritedRoutes(const Name& name, const Rib::RouteSet& routesToAdd, const Route& ignore); |
| 210 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 211 | /** |
| 212 | * \brief Creates records of the passed routes removed from the entry and creates FIB updates. |
| 213 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 214 | void |
| 215 | removeInheritedRoutes(const RibEntry& entry, const Rib::RouteSet& routesToRemove); |
| 216 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 217 | /** |
| 218 | * \brief Calculates updates for a name that will create a new RIB entry. |
| 219 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 220 | void |
| 221 | createFibUpdatesForNewRibEntry(const Name& name, const Route& route, |
| 222 | const Rib::RibEntryList& children); |
| 223 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 224 | /** |
| 225 | * \brief Calculates updates for a new route added to a RIB entry. |
| 226 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 227 | void |
| 228 | createFibUpdatesForNewRoute(const RibEntry& entry, const Route& route, |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 229 | bool captureWasTurnedOn); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 230 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 231 | /** |
| 232 | * \brief Calculates updates for changes to an existing route for a RIB entry. |
| 233 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 234 | void |
| 235 | createFibUpdatesForUpdatedRoute(const RibEntry& entry, const Route& route, |
| 236 | const Route& existingRoute); |
| 237 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 238 | /** |
| 239 | * \brief Calculates updates for a an existing route removed from a RIB entry. |
| 240 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 241 | void |
| 242 | createFibUpdatesForErasedRoute(const RibEntry& entry, const Route& route, |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 243 | bool captureWasTurnedOff); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 244 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 245 | /** |
| 246 | * \brief Calculates updates for an entry that will be removed from the RIB. |
| 247 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 248 | void |
| 249 | createFibUpdatesForErasedRibEntry(const RibEntry& entry); |
| 250 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 251 | /** |
| 252 | * \brief Adds and removes passed routes to children's inherited routes. |
| 253 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 254 | void |
| 255 | modifyChildrensInheritedRoutes(const Rib::RibEntryList& children, |
| 256 | const Rib::RouteSet& routesToAdd, |
| 257 | const Rib::RouteSet& routesToRemove); |
| 258 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 259 | /** |
| 260 | * \brief Traverses the entry's children adding and removing the passed routes. |
| 261 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 262 | void |
| 263 | traverseSubTree(const RibEntry& entry, Rib::RouteSet routesToAdd, Rib::RouteSet routesToRemove); |
| 264 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 265 | /** |
| 266 | * \brief Creates a record of a calculated inherited route that should be added to the entry. |
| 267 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 268 | void |
| 269 | addInheritedRoute(const Name& name, const Route& route); |
| 270 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 271 | /** |
| 272 | * \brief Creates a record of an existing inherited route that should be removed from the entry. |
| 273 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 274 | void |
| 275 | removeInheritedRoute(const Name& name, const Route& route); |
| 276 | |
| 277 | private: |
| 278 | const Rib& m_rib; |
| 279 | ndn::nfd::Controller& m_controller; |
| 280 | uint64_t m_batchFaceId; |
| 281 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 282 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 283 | FibUpdateList m_updatesForBatchFaceId; |
| 284 | FibUpdateList m_updatesForNonBatchFaceId; |
| 285 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 286 | /** |
| 287 | * \brief List of inherited routes generated during FIB update calculation; |
| 288 | * passed to the RIB when updates are completed successfully. |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 289 | */ |
| 290 | RibUpdateList m_inheritedRoutes; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 291 | }; |
| 292 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 293 | } // namespace nfd::rib |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 294 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 295 | #endif // NFD_DAEMON_RIB_FIB_UPDATER_HPP |