29 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
35 using ndn::nfd::ControlParameters;
42 , m_controller(controller)
55 m_inheritedRoutes.clear();
58 m_updatesForBatchFaceId.clear();
59 m_updatesForNonBatchFaceId.clear();
61 computeUpdates(batch);
63 sendUpdatesForBatchFaceId(onSuccess, onFailure);
73 switch (update.getAction()) {
75 computeUpdatesForRegistration(update);
78 computeUpdatesForUnregistration(update);
81 computeUpdatesForUnregistration(update);
85 m_updatesForBatchFaceId.clear();
92 FibUpdater::computeUpdatesForRegistration(
const RibUpdate& update)
94 const Name& prefix = update.getName();
95 const Route& route = update.getRoute();
97 auto it = m_rib.
find(prefix);
100 if (it != m_rib.
end()) {
101 shared_ptr<const RibEntry> entry(it->second);
103 auto existingRoute = entry->findRoute(route);
106 if (existingRoute == entry->end()) {
108 bool willCaptureBeTurnedOn = (entry->hasCapture() ==
false && route.isRibCapture());
110 createFibUpdatesForNewRoute(*entry, route, willCaptureBeTurnedOn);
114 RibEntry entryCopy = *entry;
116 Route& routeToUpdate = *entryCopy.findRoute(route);
117 routeToUpdate.flags = route.flags;
118 routeToUpdate.cost = route.cost;
119 routeToUpdate.expires = route.expires;
121 createFibUpdatesForUpdatedRoute(entryCopy, route, *existingRoute);
127 shared_ptr<RibEntry> parent = m_rib.
findParent(prefix);
132 for (
const auto& descendant : descendants) {
135 if (descendant->getParent() == parent) {
136 children.push_back(descendant);
140 createFibUpdatesForNewRibEntry(prefix, route, children);
145 FibUpdater::computeUpdatesForUnregistration(
const RibUpdate& update)
147 const Name& prefix = update.getName();
148 const Route& route = update.getRoute();
150 auto ribIt = m_rib.
find(prefix);
153 if (ribIt != m_rib.
end()) {
154 shared_ptr<const RibEntry> entry(ribIt->second);
155 const bool hadCapture = entry->hasCapture();
157 auto existing = entry->findRoute(route);
158 if (existing != entry->end()) {
159 RibEntry temp = *entry;
162 temp.eraseRoute(route);
164 const bool captureWasTurnedOff = (hadCapture && !temp.hasCapture());
166 createFibUpdatesForErasedRoute(temp, *existing, captureWasTurnedOff);
170 const Route* next = entry->getRouteWithSecondLowestCostByFaceId(route.faceId);
172 if (next !=
nullptr) {
173 createFibUpdatesForNewRoute(temp, *next,
false);
177 if (entry->getNRoutes() == 1) {
178 createFibUpdatesForErasedRibEntry(*entry);
185 FibUpdater::sendUpdates(
const FibUpdateList& updates,
186 const FibUpdateSuccessCallback& onSuccess,
187 const FibUpdateFailureCallback& onFailure)
189 NFD_LOG_DEBUG(
"Applying " << updates.size() <<
" FIB update(s)");
191 for (
const FibUpdate& update : updates) {
195 sendAddNextHopUpdate(update, onSuccess, onFailure);
198 sendRemoveNextHopUpdate(update, onSuccess, onFailure);
204 FibUpdater::sendUpdatesForBatchFaceId(
const FibUpdateSuccessCallback& onSuccess,
205 const FibUpdateFailureCallback& onFailure)
207 if (m_updatesForBatchFaceId.size() > 0) {
208 sendUpdates(m_updatesForBatchFaceId, onSuccess, onFailure);
211 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
216 FibUpdater::sendUpdatesForNonBatchFaceId(
const FibUpdateSuccessCallback& onSuccess,
217 const FibUpdateFailureCallback& onFailure)
219 if (m_updatesForNonBatchFaceId.size() > 0) {
220 sendUpdates(m_updatesForNonBatchFaceId, onSuccess, onFailure);
223 onSuccess(m_inheritedRoutes);
228 FibUpdater::sendAddNextHopUpdate(
const FibUpdate& update,
229 const FibUpdateSuccessCallback& onSuccess,
230 const FibUpdateFailureCallback& onFailure,
233 m_controller.start<ndn::nfd::FibAddNextHopCommand>(
235 .setName(update.name)
236 .setFaceId(update.faceId)
237 .setCost(update.cost),
238 [=] (
const auto&) { onUpdateSuccess(update, onSuccess, onFailure); },
239 [=] (
const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); });
243 FibUpdater::sendRemoveNextHopUpdate(
const FibUpdate& update,
244 const FibUpdateSuccessCallback& onSuccess,
245 const FibUpdateFailureCallback& onFailure,
248 m_controller.start<ndn::nfd::FibRemoveNextHopCommand>(
250 .setName(update.name)
251 .setFaceId(update.faceId),
252 [=] (
const auto&) { onUpdateSuccess(update, onSuccess, onFailure); },
253 [=] (
const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); });
257 FibUpdater::onUpdateSuccess(
const FibUpdate& update,
258 const FibUpdateSuccessCallback& onSuccess,
259 const FibUpdateFailureCallback& onFailure)
261 if (update.faceId == m_batchFaceId) {
262 m_updatesForBatchFaceId.remove(update);
264 if (m_updatesForBatchFaceId.size() == 0) {
265 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
269 m_updatesForNonBatchFaceId.remove(update);
271 if (m_updatesForNonBatchFaceId.size() == 0) {
272 onSuccess(m_inheritedRoutes);
278 FibUpdater::onUpdateError(
const FibUpdate& update,
279 const FibUpdateSuccessCallback& onSuccess,
280 const FibUpdateFailureCallback& onFailure,
281 const ndn::nfd::ControlResponse& response, uint32_t nTimeouts)
283 uint32_t code = response.getCode();
285 " [code: " << code <<
", error: " << response.getText() <<
"]");
287 if (code == ndn::nfd::Controller::ERROR_TIMEOUT && nTimeouts <
MAX_NUM_TIMEOUTS) {
288 sendAddNextHopUpdate(update, onSuccess, onFailure, ++nTimeouts);
291 if (update.faceId == m_batchFaceId) {
292 onFailure(code, response.getText());
295 m_updatesForNonBatchFaceId.remove(update);
297 if (m_updatesForNonBatchFaceId.size() == 0) {
298 onSuccess(m_inheritedRoutes);
303 NDN_THROW(Error(
"Non-recoverable error " + std::to_string(code) +
": " + response.getText()));
308 FibUpdater::addFibUpdate(
const FibUpdate& update)
310 FibUpdateList& updates = (update.faceId == m_batchFaceId) ? m_updatesForBatchFaceId :
311 m_updatesForNonBatchFaceId;
314 auto it = std::find_if(updates.begin(), updates.end(),
315 [&update] (
const FibUpdate& other) {
316 return update.name == other.name && update.faceId == other.faceId;
319 if (it != updates.end()) {
320 FibUpdate& existingUpdate = *it;
321 existingUpdate.action = update.action;
322 existingUpdate.cost = update.cost;
325 updates.push_back(update);
330 FibUpdater::addInheritedRoutes(
const RibEntry& entry,
const Rib::RouteSet& routesToAdd)
332 for (
const Route& route : routesToAdd) {
334 if (!entry.hasFaceId(route.faceId)) {
336 addInheritedRoute(entry.getName(), route);
344 FibUpdater::addInheritedRoutes(
const Name& name,
const Rib::RouteSet& routesToAdd,
347 for (
const Route& route : routesToAdd) {
348 if (route.faceId != ignore.faceId) {
350 addInheritedRoute(name, route);
358 FibUpdater::removeInheritedRoutes(
const RibEntry& entry,
const Rib::Rib::RouteSet& routesToRemove)
360 for (
const Route& route : routesToRemove) {
362 if (entry.hasInheritedRoute(route)) {
363 removeInheritedRoute(entry.getName(), route);
370 FibUpdater::createFibUpdatesForNewRibEntry(
const Name& name,
const Route& route,
377 if (!route.isChildInherit() && !route.isRibCapture()) {
379 addInheritedRoutes(name, m_rib.getAncestorRoutes(name), route);
381 else if (route.isChildInherit() && route.isRibCapture()) {
383 Rib::RouteSet routesToAdd;
384 routesToAdd.insert(route);
387 modifyChildrensInheritedRoutes(children, routesToAdd, m_rib.getAncestorRoutes(name));
389 else if (route.isChildInherit()) {
390 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(name);
393 addInheritedRoutes(name, ancestorRoutes, route);
397 auto it = ancestorRoutes.find(route);
400 if (it != ancestorRoutes.end()) {
401 ancestorRoutes.erase(it);
405 ancestorRoutes.insert(route);
408 modifyChildrensInheritedRoutes(children, ancestorRoutes, Rib::RouteSet());
410 else if (route.isRibCapture()) {
412 modifyChildrensInheritedRoutes(children, Rib::RouteSet(), m_rib.getAncestorRoutes(name));
417 FibUpdater::createFibUpdatesForNewRoute(
const RibEntry& entry,
const Route& route,
418 bool captureWasTurnedOn)
421 const Route* prevRoute = entry.getRouteWithLowestCostAndChildInheritByFaceId(route.faceId);
423 Rib::RouteSet routesToAdd;
424 if (route.isChildInherit()) {
428 if (prevRoute ==
nullptr || route.cost <= prevRoute->cost) {
430 routesToAdd.insert(route);
434 Rib::RouteSet routesToRemove;
435 if (captureWasTurnedOn) {
437 routesToRemove = m_rib.getAncestorRoutes(entry);
440 removeInheritedRoutes(entry, routesToRemove);
443 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove);
448 const Route* other = entry.getRouteWithLowestCostByFaceId(route.faceId);
450 if (other ==
nullptr || route.cost <= other->cost) {
456 FibUpdater::createFibUpdatesForUpdatedRoute(
const RibEntry& entry,
const Route& route,
457 const Route& existingRoute)
459 const bool costDidChange = (route.cost != existingRoute.cost);
462 const Route* prevRoute = entry.getRouteWithLowestCostAndChildInheritByFaceId(route.faceId);
465 if (route.flags == existingRoute.flags && !costDidChange) {
472 if (route.cost <= entry.getRouteWithLowestCostByFaceId(route.faceId)->cost) {
476 else if (existingRoute.cost < entry.getRouteWithLowestCostByFaceId(route.faceId)->cost) {
484 if (prevRoute ==
nullptr || route.cost <= prevRoute->cost) {
487 if ((route.flags == existingRoute.flags) && route.isChildInherit()) {
489 Rib::RouteSet routesToAdd;
490 routesToAdd.insert(route);
491 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, Rib::RouteSet());
499 if (!existingRoute.isChildInherit() && route.isChildInherit()) {
502 if (prevRoute ==
nullptr || route.cost <= prevRoute->cost) {
504 Rib::RouteSet routesToAdd;
505 routesToAdd.insert(route);
506 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, Rib::RouteSet());
509 else if (existingRoute.isChildInherit() && !route.isChildInherit()) {
511 Rib::RouteSet routesToRemove;
512 routesToRemove.insert(route);
514 Rib::RouteSet routesToAdd;
516 if (prevRoute !=
nullptr) {
517 routesToAdd.insert(*prevRoute);
521 const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
522 auto it = ancestorRoutes.find(route);
525 if (it != ancestorRoutes.end()) {
526 routesToAdd.insert(*it);
530 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove);
534 if (!existingRoute.isRibCapture() && route.isRibCapture()) {
535 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
538 removeInheritedRoutes(entry, ancestorRoutes);
541 modifyChildrensInheritedRoutes(entry.getChildren(), Rib::RouteSet(), ancestorRoutes);
543 else if (existingRoute.isRibCapture() && !route.isRibCapture()) {
544 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
547 addInheritedRoutes(entry, ancestorRoutes);
550 modifyChildrensInheritedRoutes(entry.getChildren(), ancestorRoutes, Rib::RouteSet());
555 FibUpdater::createFibUpdatesForErasedRoute(
const RibEntry& entry,
const Route& route,
556 bool captureWasTurnedOff)
560 if (route.isChildInherit() && route.isRibCapture()) {
562 Rib::RouteSet routesToRemove;
563 routesToRemove.insert(route);
567 Rib::RouteSet routesToAdd;
568 if (captureWasTurnedOff && entry.getNRoutes() != 0) {
570 routesToAdd = m_rib.getAncestorRoutes(entry);
573 addInheritedRoutes(entry, routesToAdd);
576 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove);
578 else if (route.isChildInherit()) {
580 Rib::RouteSet routesToAdd;
581 if (!entry.hasCapture()) {
582 routesToAdd = m_rib.getAncestorRoutes(entry);
585 Rib::RouteSet routesToRemove;
586 routesToRemove.insert(route);
589 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove);
591 else if (route.isRibCapture()) {
594 Rib::RouteSet routesToAdd;
595 if (captureWasTurnedOff && entry.getNRoutes() != 0) {
597 routesToAdd = m_rib.getAncestorRoutes(entry);
600 addInheritedRoutes(entry, routesToAdd);
603 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, Rib::RouteSet());
607 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
610 if (!entry.hasCapture() && entry.getNRoutes() != 0) {
613 auto it = ancestorRoutes.find(route);
615 if (it != ancestorRoutes.end()) {
616 addInheritedRoute(entry.getName(), *it);
623 FibUpdater::createFibUpdatesForErasedRibEntry(
const RibEntry& entry)
625 for (
const Route& route : entry.getInheritedRoutes()) {
632 const Rib::RouteSet& routesToAdd,
633 const Rib::RouteSet& routesToRemove)
635 for (
const auto& child : children) {
636 traverseSubTree(*child, routesToAdd, routesToRemove);
641 FibUpdater::traverseSubTree(
const RibEntry& entry, Rib::Rib::RouteSet routesToAdd,
642 Rib::Rib::RouteSet routesToRemove)
645 if (entry.hasCapture()) {
650 for (
auto removeIt = routesToRemove.begin(); removeIt != routesToRemove.end(); ) {
653 if (entry.hasChildInheritOnFaceId(removeIt->faceId)) {
654 removeIt = routesToRemove.erase(removeIt);
659 if (entry.hasInheritedRoute(*removeIt)) {
660 removeInheritedRoute(entry.getName(), *removeIt);
668 for (
auto addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
670 if (entry.hasChildInheritOnFaceId(addIt->faceId)) {
671 addIt = routesToAdd.erase(addIt);
676 if (!entry.hasFaceId(addIt->faceId)) {
677 addInheritedRoute(entry.getName(), *addIt);
684 modifyChildrensInheritedRoutes(entry.getChildren(), routesToAdd, routesToRemove);
688 FibUpdater::addInheritedRoute(
const Name& name,
const Route& route)
695 m_inheritedRoutes.push_back(update);
699 FibUpdater::removeInheritedRoute(
const Name& name,
const Route& route)
706 m_inheritedRoutes.push_back(update);
static FibUpdate createRemoveUpdate(const Name &name, uint64_t faceId)
static FibUpdate createAddUpdate(const Name &name, uint64_t faceId, uint64_t cost)
std::function< void(RibUpdateList inheritedRoutes)> FibUpdateSuccessCallback
std::list< FibUpdate > FibUpdateList
FibUpdater(Rib &rib, ndn::nfd::Controller &controller)
std::function< void(uint32_t code, const std::string &error)> FibUpdateFailureCallback
void computeAndSendFibUpdates(const RibUpdateBatch &batch, const FibUpdateSuccessCallback &onSuccess, const FibUpdateFailureCallback &onFailure)
Computes FibUpdates using the provided RibUpdateBatch and then sends the updates to NFD's FIB.
Represents the Routing Information Base.
void setFibUpdater(FibUpdater *updater)
const_iterator end() const
std::list< shared_ptr< RibEntry > > RibEntryList
const_iterator find(const Name &prefix) const
shared_ptr< RibEntry > findParent(const Name &prefix) const
Represents a collection of RibUpdates to be applied to a single FaceId.
uint64_t getFaceId() const
Represents a route that will be added to or removed from a namespace.
@ REMOVE_FACE
An update triggered by a face destruction notification.
#define NFD_LOG_INIT(name)
constexpr uint32_t ERROR_FACE_NOT_FOUND
constexpr int MAX_NUM_TIMEOUTS