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 | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2025, 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 | |
| 26 | #include "fib-updater.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 27 | #include "common/logger.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 28 | |
Davide Pesavento | 45c1f6a | 2025-01-01 19:30:30 -0500 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::rib { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 32 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 33 | NFD_LOG_INIT(FibUpdater); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 34 | |
Junxiao Shi | df1dc65 | 2019-08-30 19:03:19 +0000 | [diff] [blame] | 35 | constexpr int MAX_NUM_TIMEOUTS = 10; |
| 36 | constexpr uint32_t ERROR_FACE_NOT_FOUND = 410; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 37 | |
| 38 | FibUpdater::FibUpdater(Rib& rib, ndn::nfd::Controller& controller) |
| 39 | : m_rib(rib) |
| 40 | , m_controller(controller) |
| 41 | { |
| 42 | rib.setFibUpdater(this); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | FibUpdater::computeAndSendFibUpdates(const RibUpdateBatch& batch, |
| 47 | const FibUpdateSuccessCallback& onSuccess, |
| 48 | const FibUpdateFailureCallback& onFailure) |
| 49 | { |
| 50 | m_batchFaceId = batch.getFaceId(); |
| 51 | |
| 52 | // Erase previously calculated inherited routes |
| 53 | m_inheritedRoutes.clear(); |
| 54 | |
| 55 | // Erase previously calculated FIB updates |
| 56 | m_updatesForBatchFaceId.clear(); |
| 57 | m_updatesForNonBatchFaceId.clear(); |
| 58 | |
| 59 | computeUpdates(batch); |
| 60 | |
| 61 | sendUpdatesForBatchFaceId(onSuccess, onFailure); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | FibUpdater::computeUpdates(const RibUpdateBatch& batch) |
| 66 | { |
| 67 | NFD_LOG_DEBUG("Computing updates for batch with faceID: " << batch.getFaceId()); |
| 68 | |
| 69 | // Compute updates and add to m_fibUpdates |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 70 | for (const RibUpdate& update : batch) { |
| 71 | switch (update.getAction()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 72 | case RibUpdate::REGISTER: |
| 73 | computeUpdatesForRegistration(update); |
| 74 | break; |
| 75 | case RibUpdate::UNREGISTER: |
| 76 | computeUpdatesForUnregistration(update); |
| 77 | break; |
| 78 | case RibUpdate::REMOVE_FACE: |
| 79 | computeUpdatesForUnregistration(update); |
| 80 | |
| 81 | // Do not apply updates with the same face ID as the destroyed face |
| 82 | // since they will be rejected by the FIB |
| 83 | m_updatesForBatchFaceId.clear(); |
| 84 | break; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 85 | } |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 86 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
| 90 | FibUpdater::computeUpdatesForRegistration(const RibUpdate& update) |
| 91 | { |
| 92 | const Name& prefix = update.getName(); |
| 93 | const Route& route = update.getRoute(); |
| 94 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 95 | auto it = m_rib.find(prefix); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 96 | |
| 97 | // Name prefix exists |
| 98 | if (it != m_rib.end()) { |
| 99 | shared_ptr<const RibEntry> entry(it->second); |
| 100 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 101 | auto existingRoute = entry->findRoute(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 102 | |
| 103 | // Route will be new |
| 104 | if (existingRoute == entry->end()) { |
| 105 | // Will the new route change the namespace's capture flag? |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 106 | bool willCaptureBeTurnedOn = (entry->hasCapture() == false && route.isRibCapture()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 107 | |
| 108 | createFibUpdatesForNewRoute(*entry, route, willCaptureBeTurnedOn); |
| 109 | } |
| 110 | else { |
| 111 | // Route already exists |
| 112 | RibEntry entryCopy = *entry; |
| 113 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 114 | Route& routeToUpdate = *entryCopy.findRoute(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 115 | routeToUpdate.flags = route.flags; |
| 116 | routeToUpdate.cost = route.cost; |
| 117 | routeToUpdate.expires = route.expires; |
| 118 | |
| 119 | createFibUpdatesForUpdatedRoute(entryCopy, route, *existingRoute); |
| 120 | } |
| 121 | } |
| 122 | else { |
| 123 | // New name in RIB |
| 124 | // Find prefix's parent |
| 125 | shared_ptr<RibEntry> parent = m_rib.findParent(prefix); |
| 126 | |
| 127 | Rib::RibEntryList descendants = m_rib.findDescendantsForNonInsertedName(prefix); |
| 128 | Rib::RibEntryList children; |
| 129 | |
| 130 | for (const auto& descendant : descendants) { |
| 131 | // If the child has the same parent as the new entry, |
| 132 | // the new entry must be the child's new parent |
| 133 | if (descendant->getParent() == parent) { |
| 134 | children.push_back(descendant); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | createFibUpdatesForNewRibEntry(prefix, route, children); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | FibUpdater::computeUpdatesForUnregistration(const RibUpdate& update) |
| 144 | { |
| 145 | const Name& prefix = update.getName(); |
| 146 | const Route& route = update.getRoute(); |
| 147 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 148 | auto ribIt = m_rib.find(prefix); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 149 | |
| 150 | // Name prefix exists |
| 151 | if (ribIt != m_rib.end()) { |
| 152 | shared_ptr<const RibEntry> entry(ribIt->second); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 153 | const bool hadCapture = entry->hasCapture(); |
| 154 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 155 | auto existing = entry->findRoute(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 156 | if (existing != entry->end()) { |
| 157 | RibEntry temp = *entry; |
| 158 | |
| 159 | // Erase route in temp entry |
| 160 | temp.eraseRoute(route); |
| 161 | |
| 162 | const bool captureWasTurnedOff = (hadCapture && !temp.hasCapture()); |
| 163 | |
| 164 | createFibUpdatesForErasedRoute(temp, *existing, captureWasTurnedOff); |
| 165 | |
| 166 | // The RibEntry still has the face ID; need to update FIB |
| 167 | // with lowest cost for the same face instead of removing the face from the FIB |
| 168 | const Route* next = entry->getRouteWithSecondLowestCostByFaceId(route.faceId); |
| 169 | |
| 170 | if (next != nullptr) { |
| 171 | createFibUpdatesForNewRoute(temp, *next, false); |
| 172 | } |
| 173 | |
| 174 | // The RibEntry will be empty after this removal |
| 175 | if (entry->getNRoutes() == 1) { |
| 176 | createFibUpdatesForErasedRibEntry(*entry); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void |
| 183 | FibUpdater::sendUpdates(const FibUpdateList& updates, |
| 184 | const FibUpdateSuccessCallback& onSuccess, |
| 185 | const FibUpdateFailureCallback& onFailure) |
| 186 | { |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 187 | NFD_LOG_DEBUG("Applying " << updates.size() << " FIB update(s)"); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 188 | |
| 189 | for (const FibUpdate& update : updates) { |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 190 | NFD_LOG_DEBUG("Sending " << update); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 191 | |
| 192 | if (update.action == FibUpdate::ADD_NEXTHOP) { |
| 193 | sendAddNextHopUpdate(update, onSuccess, onFailure); |
| 194 | } |
| 195 | else if (update.action == FibUpdate::REMOVE_NEXTHOP) { |
| 196 | sendRemoveNextHopUpdate(update, onSuccess, onFailure); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | FibUpdater::sendUpdatesForBatchFaceId(const FibUpdateSuccessCallback& onSuccess, |
| 203 | const FibUpdateFailureCallback& onFailure) |
| 204 | { |
| 205 | if (m_updatesForBatchFaceId.size() > 0) { |
| 206 | sendUpdates(m_updatesForBatchFaceId, onSuccess, onFailure); |
| 207 | } |
| 208 | else { |
| 209 | sendUpdatesForNonBatchFaceId(onSuccess, onFailure); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | FibUpdater::sendUpdatesForNonBatchFaceId(const FibUpdateSuccessCallback& onSuccess, |
| 215 | const FibUpdateFailureCallback& onFailure) |
| 216 | { |
| 217 | if (m_updatesForNonBatchFaceId.size() > 0) { |
| 218 | sendUpdates(m_updatesForNonBatchFaceId, onSuccess, onFailure); |
| 219 | } |
| 220 | else { |
| 221 | onSuccess(m_inheritedRoutes); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void |
| 226 | FibUpdater::sendAddNextHopUpdate(const FibUpdate& update, |
| 227 | const FibUpdateSuccessCallback& onSuccess, |
| 228 | const FibUpdateFailureCallback& onFailure, |
| 229 | uint32_t nTimeouts) |
| 230 | { |
| 231 | m_controller.start<ndn::nfd::FibAddNextHopCommand>( |
| 232 | ControlParameters() |
| 233 | .setName(update.name) |
| 234 | .setFaceId(update.faceId) |
| 235 | .setCost(update.cost), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 236 | [=] (const auto&) { onUpdateSuccess(update, onSuccess, onFailure); }, |
| 237 | [=] (const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); }); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void |
| 241 | FibUpdater::sendRemoveNextHopUpdate(const FibUpdate& update, |
| 242 | const FibUpdateSuccessCallback& onSuccess, |
| 243 | const FibUpdateFailureCallback& onFailure, |
| 244 | uint32_t nTimeouts) |
| 245 | { |
| 246 | m_controller.start<ndn::nfd::FibRemoveNextHopCommand>( |
| 247 | ControlParameters() |
| 248 | .setName(update.name) |
| 249 | .setFaceId(update.faceId), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 250 | [=] (const auto&) { onUpdateSuccess(update, onSuccess, onFailure); }, |
| 251 | [=] (const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); }); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 255 | FibUpdater::onUpdateSuccess(const FibUpdate& update, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 256 | const FibUpdateSuccessCallback& onSuccess, |
| 257 | const FibUpdateFailureCallback& onFailure) |
| 258 | { |
| 259 | if (update.faceId == m_batchFaceId) { |
| 260 | m_updatesForBatchFaceId.remove(update); |
| 261 | |
| 262 | if (m_updatesForBatchFaceId.size() == 0) { |
| 263 | sendUpdatesForNonBatchFaceId(onSuccess, onFailure); |
| 264 | } |
| 265 | } |
| 266 | else { |
| 267 | m_updatesForNonBatchFaceId.remove(update); |
| 268 | |
| 269 | if (m_updatesForNonBatchFaceId.size() == 0) { |
| 270 | onSuccess(m_inheritedRoutes); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 276 | FibUpdater::onUpdateError(const FibUpdate& update, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 277 | const FibUpdateSuccessCallback& onSuccess, |
| 278 | const FibUpdateFailureCallback& onFailure, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 279 | const ndn::nfd::ControlResponse& response, uint32_t nTimeouts) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 280 | { |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 281 | uint32_t code = response.getCode(); |
| 282 | NFD_LOG_DEBUG("Failed to apply " << update << |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 283 | " [code: " << code << ", error: " << response.getText() << "]"); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 284 | |
| 285 | if (code == ndn::nfd::Controller::ERROR_TIMEOUT && nTimeouts < MAX_NUM_TIMEOUTS) { |
| 286 | sendAddNextHopUpdate(update, onSuccess, onFailure, ++nTimeouts); |
| 287 | } |
| 288 | else if (code == ERROR_FACE_NOT_FOUND) { |
| 289 | if (update.faceId == m_batchFaceId) { |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 290 | onFailure(code, response.getText()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 291 | } |
| 292 | else { |
| 293 | m_updatesForNonBatchFaceId.remove(update); |
| 294 | |
| 295 | if (m_updatesForNonBatchFaceId.size() == 0) { |
| 296 | onSuccess(m_inheritedRoutes); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | else { |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 301 | NDN_THROW(Error("Non-recoverable error " + std::to_string(code) + ": " + response.getText())); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | void |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 306 | FibUpdater::addFibUpdate(const FibUpdate& update) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 307 | { |
| 308 | FibUpdateList& updates = (update.faceId == m_batchFaceId) ? m_updatesForBatchFaceId : |
| 309 | m_updatesForNonBatchFaceId; |
| 310 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 311 | // If an update with the same name and route already exists, replace it |
| 312 | auto it = std::find_if(updates.begin(), updates.end(), |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 313 | [&update] (const FibUpdate& other) { |
| 314 | return update.name == other.name && update.faceId == other.faceId; |
| 315 | }); |
| 316 | |
| 317 | if (it != updates.end()) { |
| 318 | FibUpdate& existingUpdate = *it; |
| 319 | existingUpdate.action = update.action; |
| 320 | existingUpdate.cost = update.cost; |
| 321 | } |
| 322 | else { |
| 323 | updates.push_back(update); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 328 | FibUpdater::addInheritedRoutes(const RibEntry& entry, const Rib::RouteSet& routesToAdd) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 329 | { |
| 330 | for (const Route& route : routesToAdd) { |
| 331 | // Don't add an ancestor faceId if the namespace has an entry for that faceId |
| 332 | if (!entry.hasFaceId(route.faceId)) { |
| 333 | // Create a record of the inherited route so it can be added to the RIB later |
| 334 | addInheritedRoute(entry.getName(), route); |
| 335 | |
| 336 | addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), route.faceId, route.cost)); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 342 | FibUpdater::addInheritedRoutes(const Name& name, const Rib::RouteSet& routesToAdd, |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 343 | const Route& ignore) |
| 344 | { |
| 345 | for (const Route& route : routesToAdd) { |
| 346 | if (route.faceId != ignore.faceId) { |
| 347 | // Create a record of the inherited route so it can be added to the RIB later |
| 348 | addInheritedRoute(name, route); |
| 349 | |
| 350 | addFibUpdate(FibUpdate::createAddUpdate(name, route.faceId, route.cost)); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | FibUpdater::removeInheritedRoutes(const RibEntry& entry, const Rib::Rib::RouteSet& routesToRemove) |
| 357 | { |
| 358 | for (const Route& route : routesToRemove) { |
| 359 | // Only remove if the route has been inherited |
| 360 | if (entry.hasInheritedRoute(route)) { |
| 361 | removeInheritedRoute(entry.getName(), route); |
| 362 | addFibUpdate(FibUpdate::createRemoveUpdate(entry.getName(), route.faceId)); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | FibUpdater::createFibUpdatesForNewRibEntry(const Name& name, const Route& route, |
| 369 | const Rib::RibEntryList& children) |
| 370 | { |
| 371 | // Create FIB update for new entry |
| 372 | addFibUpdate(FibUpdate::createAddUpdate(name, route.faceId, route.cost)); |
| 373 | |
| 374 | // No flags are set |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 375 | if (!route.isChildInherit() && !route.isRibCapture()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 376 | // Add ancestor routes to self |
| 377 | addInheritedRoutes(name, m_rib.getAncestorRoutes(name), route); |
| 378 | } |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 379 | else if (route.isChildInherit() && route.isRibCapture()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 380 | // Add route to children |
| 381 | Rib::RouteSet routesToAdd; |
| 382 | routesToAdd.insert(route); |
| 383 | |
| 384 | // Remove routes blocked by capture and add self to children |
| 385 | modifyChildrensInheritedRoutes(children, routesToAdd, m_rib.getAncestorRoutes(name)); |
| 386 | } |
| 387 | else if (route.isChildInherit()) { |
| 388 | Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(name); |
| 389 | |
| 390 | // Add ancestor routes to self |
| 391 | addInheritedRoutes(name, ancestorRoutes, route); |
| 392 | |
| 393 | // If there is an ancestor route which is the same as the new route, replace it |
| 394 | // with the new route |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 395 | auto it = ancestorRoutes.find(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 396 | |
| 397 | // There is a route that needs to be overwritten, erase and then replace |
| 398 | if (it != ancestorRoutes.end()) { |
| 399 | ancestorRoutes.erase(it); |
| 400 | } |
| 401 | |
| 402 | // Add new route to ancestor list so it can be added to children |
| 403 | ancestorRoutes.insert(route); |
| 404 | |
| 405 | // Add ancestor routes to children |
| 406 | modifyChildrensInheritedRoutes(children, ancestorRoutes, Rib::RouteSet()); |
| 407 | } |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 408 | else if (route.isRibCapture()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 409 | // Remove routes blocked by capture |
| 410 | modifyChildrensInheritedRoutes(children, Rib::RouteSet(), m_rib.getAncestorRoutes(name)); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | FibUpdater::createFibUpdatesForNewRoute(const RibEntry& entry, const Route& route, |
| 416 | bool captureWasTurnedOn) |
| 417 | { |
| 418 | // Only update if the new route has a lower cost than a previously installed route |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 419 | const Route* prevRoute = entry.getRouteWithLowestCostAndChildInheritByFaceId(route.faceId); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 420 | |
| 421 | Rib::RouteSet routesToAdd; |
| 422 | if (route.isChildInherit()) { |
| 423 | // Add to children if this new route doesn't override a previous lower cost, or |
| 424 | // add to children if this new route is lower cost than a previous route. |
| 425 | // Less than equal, since entry may find this route |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 426 | if (prevRoute == nullptr || route.cost <= prevRoute->cost) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 427 | // Add self to children |
| 428 | routesToAdd.insert(route); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | Rib::RouteSet routesToRemove; |
| 433 | if (captureWasTurnedOn) { |
| 434 | // Capture flag on |
| 435 | routesToRemove = m_rib.getAncestorRoutes(entry); |
| 436 | |
| 437 | // Remove ancestor routes from self |
| 438 | removeInheritedRoutes(entry, routesToRemove); |
| 439 | } |
| 440 | |
| 441 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove); |
| 442 | |
| 443 | // If another route with same faceId and lower cost exists, don't update. |
| 444 | // Must be done last so that add updates replace removal updates |
| 445 | // Create FIB update for new entry |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 446 | const Route* other = entry.getRouteWithLowestCostByFaceId(route.faceId); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 447 | |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 448 | if (other == nullptr || route.cost <= other->cost) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 449 | addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), route.faceId, route.cost)); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | FibUpdater::createFibUpdatesForUpdatedRoute(const RibEntry& entry, const Route& route, |
| 455 | const Route& existingRoute) |
| 456 | { |
| 457 | const bool costDidChange = (route.cost != existingRoute.cost); |
| 458 | |
| 459 | // Look for an installed route with the lowest cost and child inherit set |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 460 | const Route* prevRoute = entry.getRouteWithLowestCostAndChildInheritByFaceId(route.faceId); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 461 | |
| 462 | // No flags changed and cost didn't change, no change in FIB |
| 463 | if (route.flags == existingRoute.flags && !costDidChange) { |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | // Cost changed so create update for the entry itself |
| 468 | if (costDidChange) { |
| 469 | // Create update if this route's cost is lower than other routes |
| 470 | if (route.cost <= entry.getRouteWithLowestCostByFaceId(route.faceId)->cost) { |
| 471 | // Create FIB update for the updated entry |
| 472 | addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), route.faceId, route.cost)); |
| 473 | } |
| 474 | else if (existingRoute.cost < entry.getRouteWithLowestCostByFaceId(route.faceId)->cost) { |
| 475 | // Create update if this route used to be the lowest route but is no longer |
| 476 | // the lowest cost route. |
| 477 | addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), prevRoute->faceId, prevRoute->cost)); |
| 478 | } |
| 479 | |
| 480 | // If another route with same faceId and lower cost and ChildInherit exists, |
| 481 | // don't update children. |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 482 | if (prevRoute == nullptr || route.cost <= prevRoute->cost) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 483 | // If no flags changed but child inheritance is set, need to update children |
| 484 | // with new cost |
| 485 | if ((route.flags == existingRoute.flags) && route.isChildInherit()) { |
| 486 | // Add self to children |
| 487 | Rib::RouteSet routesToAdd; |
| 488 | routesToAdd.insert(route); |
| 489 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, Rib::RouteSet()); |
| 490 | |
| 491 | return; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // Child inherit was turned on |
| 497 | if (!existingRoute.isChildInherit() && route.isChildInherit()) { |
| 498 | // If another route with same faceId and lower cost and ChildInherit exists, |
| 499 | // don't update children. |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 500 | if (prevRoute == nullptr || route.cost <= prevRoute->cost) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 501 | // Add self to children |
| 502 | Rib::RouteSet routesToAdd; |
| 503 | routesToAdd.insert(route); |
| 504 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, Rib::RouteSet()); |
| 505 | } |
| 506 | } // Child inherit was turned off |
| 507 | else if (existingRoute.isChildInherit() && !route.isChildInherit()) { |
| 508 | // Remove self from children |
| 509 | Rib::RouteSet routesToRemove; |
| 510 | routesToRemove.insert(route); |
| 511 | |
| 512 | Rib::RouteSet routesToAdd; |
| 513 | // If another route with same faceId and ChildInherit exists, update children with this route. |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 514 | if (prevRoute != nullptr) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 515 | routesToAdd.insert(*prevRoute); |
| 516 | } |
| 517 | else { |
| 518 | // Look for an ancestor that was blocked previously |
| 519 | const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry); |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 520 | auto it = ancestorRoutes.find(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 521 | |
| 522 | // If an ancestor is found, add it to children |
| 523 | if (it != ancestorRoutes.end()) { |
| 524 | routesToAdd.insert(*it); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove); |
| 529 | } |
| 530 | |
| 531 | // Capture was turned on |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 532 | if (!existingRoute.isRibCapture() && route.isRibCapture()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 533 | Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry); |
| 534 | |
| 535 | // Remove ancestor routes from self |
| 536 | removeInheritedRoutes(entry, ancestorRoutes); |
| 537 | |
| 538 | // Remove ancestor routes from children |
| 539 | modifyChildrensInheritedRoutes(entry.getChildren(), Rib::RouteSet(), ancestorRoutes); |
| 540 | } // Capture was turned off |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 541 | else if (existingRoute.isRibCapture() && !route.isRibCapture()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 542 | Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry); |
| 543 | |
| 544 | // Add ancestor routes to self |
| 545 | addInheritedRoutes(entry, ancestorRoutes); |
| 546 | |
| 547 | // Add ancestor routes to children |
| 548 | modifyChildrensInheritedRoutes(entry.getChildren(), ancestorRoutes, Rib::RouteSet()); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void |
| 553 | FibUpdater::createFibUpdatesForErasedRoute(const RibEntry& entry, const Route& route, |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 554 | bool captureWasTurnedOff) |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 555 | { |
| 556 | addFibUpdate(FibUpdate::createRemoveUpdate(entry.getName(), route.faceId)); |
| 557 | |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 558 | if (route.isChildInherit() && route.isRibCapture()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 559 | // Remove self from children |
| 560 | Rib::RouteSet routesToRemove; |
| 561 | routesToRemove.insert(route); |
| 562 | |
Vince Lehman | 9aac873 | 2016-01-11 15:49:23 -0600 | [diff] [blame] | 563 | // If capture is turned off for the route and another route is installed in the RibEntry, |
| 564 | // add ancestors to self |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 565 | Rib::RouteSet routesToAdd; |
Vince Lehman | 9aac873 | 2016-01-11 15:49:23 -0600 | [diff] [blame] | 566 | if (captureWasTurnedOff && entry.getNRoutes() != 0) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 567 | // Look for an ancestors that were blocked previously |
| 568 | routesToAdd = m_rib.getAncestorRoutes(entry); |
| 569 | |
| 570 | // Add ancestor routes to self |
| 571 | addInheritedRoutes(entry, routesToAdd); |
| 572 | } |
| 573 | |
| 574 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove); |
| 575 | } |
| 576 | else if (route.isChildInherit()) { |
| 577 | // If not blocked by capture, add inherited routes to children |
| 578 | Rib::RouteSet routesToAdd; |
| 579 | if (!entry.hasCapture()) { |
| 580 | routesToAdd = m_rib.getAncestorRoutes(entry); |
| 581 | } |
| 582 | |
| 583 | Rib::RouteSet routesToRemove; |
| 584 | routesToRemove.insert(route); |
| 585 | |
| 586 | // Add ancestor routes to children |
| 587 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove); |
| 588 | } |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 589 | else if (route.isRibCapture()) { |
Vince Lehman | 9aac873 | 2016-01-11 15:49:23 -0600 | [diff] [blame] | 590 | // If capture is turned off for the route and another route is installed in the RibEntry, |
| 591 | // add ancestors to self |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 592 | Rib::RouteSet routesToAdd; |
Vince Lehman | 9aac873 | 2016-01-11 15:49:23 -0600 | [diff] [blame] | 593 | if (captureWasTurnedOff && entry.getNRoutes() != 0) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 594 | // Look for an ancestors that were blocked previously |
| 595 | routesToAdd = m_rib.getAncestorRoutes(entry); |
| 596 | |
| 597 | // Add ancestor routes to self |
| 598 | addInheritedRoutes(entry, routesToAdd); |
| 599 | } |
| 600 | |
| 601 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, Rib::RouteSet()); |
| 602 | } |
| 603 | |
| 604 | // Need to check if the removed route was blocking an inherited route |
| 605 | Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry); |
| 606 | |
Vince Lehman | f91ab74 | 2015-04-23 15:26:55 -0500 | [diff] [blame] | 607 | // If the current entry has capture set or is pending removal, don't add inherited route |
| 608 | if (!entry.hasCapture() && entry.getNRoutes() != 0) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 609 | // If there is an ancestor route which is the same as the erased route, add that route |
| 610 | // to the current entry |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 611 | auto it = ancestorRoutes.find(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 612 | |
| 613 | if (it != ancestorRoutes.end()) { |
| 614 | addInheritedRoute(entry.getName(), *it); |
| 615 | addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), it->faceId, it->cost)); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void |
| 621 | FibUpdater::createFibUpdatesForErasedRibEntry(const RibEntry& entry) |
| 622 | { |
| 623 | for (const Route& route : entry.getInheritedRoutes()) { |
| 624 | addFibUpdate(FibUpdate::createRemoveUpdate(entry.getName(), route.faceId)); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | void |
| 629 | FibUpdater::modifyChildrensInheritedRoutes(const Rib::RibEntryList& children, |
| 630 | const Rib::RouteSet& routesToAdd, |
| 631 | const Rib::RouteSet& routesToRemove) |
| 632 | { |
| 633 | for (const auto& child : children) { |
| 634 | traverseSubTree(*child, routesToAdd, routesToRemove); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | void |
| 639 | FibUpdater::traverseSubTree(const RibEntry& entry, Rib::Rib::RouteSet routesToAdd, |
| 640 | Rib::Rib::RouteSet routesToRemove) |
| 641 | { |
| 642 | // If a route on the namespace has the capture flag set, ignore self and children |
| 643 | if (entry.hasCapture()) { |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | // Remove inherited routes from current namespace |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 648 | for (auto removeIt = routesToRemove.begin(); removeIt != routesToRemove.end(); ) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 649 | // If a route on the namespace has the same face ID and child inheritance set, |
| 650 | // ignore this route |
| 651 | if (entry.hasChildInheritOnFaceId(removeIt->faceId)) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 652 | removeIt = routesToRemove.erase(removeIt); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 653 | continue; |
| 654 | } |
| 655 | |
| 656 | // Only remove route if it removes an existing inherited route |
| 657 | if (entry.hasInheritedRoute(*removeIt)) { |
| 658 | removeInheritedRoute(entry.getName(), *removeIt); |
| 659 | addFibUpdate(FibUpdate::createRemoveUpdate(entry.getName(), removeIt->faceId)); |
| 660 | } |
| 661 | |
| 662 | ++removeIt; |
| 663 | } |
| 664 | |
| 665 | // Add inherited routes to current namespace |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 666 | for (auto addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 667 | // If a route on the namespace has the same face ID and child inherit set, ignore this face |
| 668 | if (entry.hasChildInheritOnFaceId(addIt->faceId)) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 669 | addIt = routesToAdd.erase(addIt); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 670 | continue; |
| 671 | } |
| 672 | |
| 673 | // Only add route if it does not override an existing route |
| 674 | if (!entry.hasFaceId(addIt->faceId)) { |
| 675 | addInheritedRoute(entry.getName(), *addIt); |
| 676 | addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), addIt->faceId, addIt->cost)); |
| 677 | } |
| 678 | |
| 679 | ++addIt; |
| 680 | } |
| 681 | |
| 682 | modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove); |
| 683 | } |
| 684 | |
| 685 | void |
| 686 | FibUpdater::addInheritedRoute(const Name& name, const Route& route) |
| 687 | { |
| 688 | RibUpdate update; |
| 689 | update.setAction(RibUpdate::REGISTER) |
| 690 | .setName(name) |
| 691 | .setRoute(route); |
| 692 | |
| 693 | m_inheritedRoutes.push_back(update); |
| 694 | } |
| 695 | |
| 696 | void |
| 697 | FibUpdater::removeInheritedRoute(const Name& name, const Route& route) |
| 698 | { |
| 699 | RibUpdate update; |
| 700 | update.setAction(RibUpdate::UNREGISTER) |
| 701 | .setName(name) |
| 702 | .setRoute(route); |
| 703 | |
| 704 | m_inheritedRoutes.push_back(update); |
| 705 | } |
| 706 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 707 | } // namespace nfd::rib |