30 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> 35 using ndn::nfd::ControlParameters;
39 const unsigned int FibUpdater::MAX_NUM_TIMEOUTS = 10;
40 const uint32_t FibUpdater::ERROR_FACE_NOT_FOUND = 410;
44 , m_controller(controller)
57 m_inheritedRoutes.clear();
60 m_updatesForBatchFaceId.clear();
61 m_updatesForNonBatchFaceId.clear();
63 computeUpdates(batch);
65 sendUpdatesForBatchFaceId(onSuccess, onFailure);
75 switch (update.getAction()) {
77 computeUpdatesForRegistration(update);
80 computeUpdatesForUnregistration(update);
83 computeUpdatesForUnregistration(update);
87 m_updatesForBatchFaceId.clear();
94 FibUpdater::computeUpdatesForRegistration(
const RibUpdate& update)
96 const Name& prefix = update.
getName();
102 if (it != m_rib.
end()) {
103 shared_ptr<const RibEntry> entry(it->second);
108 if (existingRoute == entry->end()) {
110 bool willCaptureBeTurnedOn = (entry->hasCapture() ==
false && route.isRibCapture());
112 createFibUpdatesForNewRoute(*entry, route, willCaptureBeTurnedOn);
124 createFibUpdatesForUpdatedRoute(entryCopy, route, *existingRoute);
130 shared_ptr<RibEntry> parent = m_rib.
findParent(prefix);
135 for (
const auto& descendant : descendants) {
138 if (descendant->getParent() == parent) {
139 children.push_back(descendant);
143 createFibUpdatesForNewRibEntry(prefix, route, children);
148 FibUpdater::computeUpdatesForUnregistration(
const RibUpdate& update)
150 const Name& prefix = update.
getName();
156 if (ribIt != m_rib.
end()) {
157 shared_ptr<const RibEntry> entry(ribIt->second);
159 const bool hadCapture = entry->hasCapture();
163 if (existing != entry->end()) {
169 const bool captureWasTurnedOff = (hadCapture && !temp.
hasCapture());
171 createFibUpdatesForErasedRoute(temp, *existing, captureWasTurnedOff);
175 const Route* next = entry->getRouteWithSecondLowestCostByFaceId(route.
faceId);
177 if (next !=
nullptr) {
178 createFibUpdatesForNewRoute(temp, *next,
false);
182 if (entry->getNRoutes() == 1) {
183 createFibUpdatesForErasedRibEntry(*entry);
194 std::string updateString = (updates.size() == 1) ?
" update" :
" updates";
195 NFD_LOG_DEBUG(
"Applying " << updates.size() << updateString <<
" to FIB");
197 for (
const FibUpdate& update : updates) {
201 sendAddNextHopUpdate(update, onSuccess, onFailure);
204 sendRemoveNextHopUpdate(update, onSuccess, onFailure);
213 if (m_updatesForBatchFaceId.size() > 0) {
214 sendUpdates(m_updatesForBatchFaceId, onSuccess, onFailure);
217 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
225 if (m_updatesForNonBatchFaceId.size() > 0) {
226 sendUpdates(m_updatesForNonBatchFaceId, onSuccess, onFailure);
229 onSuccess(m_inheritedRoutes);
234 FibUpdater::sendAddNextHopUpdate(
const FibUpdate& update,
239 m_controller.start<ndn::nfd::FibAddNextHopCommand>(
241 .setName(update.
name)
243 .setCost(update.
cost),
244 bind(&FibUpdater::onUpdateSuccess,
this, update, onSuccess, onFailure),
245 bind(&FibUpdater::onUpdateError,
this, update, onSuccess, onFailure, _1, nTimeouts));
249 FibUpdater::sendRemoveNextHopUpdate(
const FibUpdate& update,
254 m_controller.start<ndn::nfd::FibRemoveNextHopCommand>(
256 .setName(update.
name)
257 .setFaceId(update.
faceId),
258 bind(&FibUpdater::onUpdateSuccess,
this, update, onSuccess, onFailure),
259 bind(&FibUpdater::onUpdateError,
this, update, onSuccess, onFailure, _1, nTimeouts));
263 FibUpdater::onUpdateSuccess(
const FibUpdate update,
267 if (update.
faceId == m_batchFaceId) {
268 m_updatesForBatchFaceId.remove(update);
270 if (m_updatesForBatchFaceId.size() == 0) {
271 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
275 m_updatesForNonBatchFaceId.remove(update);
277 if (m_updatesForNonBatchFaceId.size() == 0) {
278 onSuccess(m_inheritedRoutes);
284 FibUpdater::onUpdateError(
const FibUpdate update,
287 const ndn::nfd::ControlResponse& response, uint32_t nTimeouts)
289 uint32_t code = response.getCode();
291 " (code: " << code <<
", error: " << response.getText() <<
")");
293 if (code == ndn::nfd::Controller::ERROR_TIMEOUT && nTimeouts < MAX_NUM_TIMEOUTS) {
294 sendAddNextHopUpdate(update, onSuccess, onFailure, ++nTimeouts);
296 else if (code == ERROR_FACE_NOT_FOUND) {
297 if (update.
faceId == m_batchFaceId) {
298 onFailure(code, response.getText());
301 m_updatesForNonBatchFaceId.remove(update);
303 if (m_updatesForNonBatchFaceId.size() == 0) {
304 onSuccess(m_inheritedRoutes);
309 BOOST_THROW_EXCEPTION(
Error(
"Non-recoverable error: " + response.getText() +
310 " code: " + to_string(code)));
315 FibUpdater::addFibUpdate(
FibUpdate update)
318 m_updatesForNonBatchFaceId;
324 return update.
name == other.name && update.
faceId == other.faceId;
327 if (it != updates.end()) {
333 updates.push_back(update);
340 for (
const Route& route : routesToAdd) {
344 addInheritedRoute(entry.
getName(), route);
352 FibUpdater::addInheritedRoutes(
const Name& name,
const Rib::RouteSet& routesToAdd,
355 for (
const Route& route : routesToAdd) {
356 if (route.faceId != ignore.
faceId) {
358 addInheritedRoute(name, route);
366 FibUpdater::removeInheritedRoutes(
const RibEntry& entry,
const Rib::Rib::RouteSet& routesToRemove)
368 for (
const Route& route : routesToRemove) {
371 removeInheritedRoute(entry.
getName(), route);
378 FibUpdater::createFibUpdatesForNewRibEntry(
const Name& name,
const Route& route,
385 if (!route.isChildInherit() && !route.isRibCapture()) {
387 addInheritedRoutes(name, m_rib.getAncestorRoutes(name), route);
389 else if (route.isChildInherit() && route.isRibCapture()) {
392 routesToAdd.insert(route);
395 modifyChildrensInheritedRoutes(children, routesToAdd, m_rib.getAncestorRoutes(name));
397 else if (route.isChildInherit()) {
398 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(name);
401 addInheritedRoutes(name, ancestorRoutes, route);
408 if (it != ancestorRoutes.end()) {
409 ancestorRoutes.erase(it);
413 ancestorRoutes.insert(route);
416 modifyChildrensInheritedRoutes(children, ancestorRoutes,
Rib::RouteSet());
418 else if (route.isRibCapture()) {
420 modifyChildrensInheritedRoutes(children,
Rib::RouteSet(), m_rib.getAncestorRoutes(name));
425 FibUpdater::createFibUpdatesForNewRoute(
const RibEntry& entry,
const Route& route,
426 bool captureWasTurnedOn)
432 if (route.isChildInherit()) {
436 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
438 routesToAdd.insert(route);
443 if (captureWasTurnedOn) {
445 routesToRemove = m_rib.getAncestorRoutes(entry);
448 removeInheritedRoutes(entry, routesToRemove);
451 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
458 if (other ==
nullptr || route.
cost <= other->cost) {
464 FibUpdater::createFibUpdatesForUpdatedRoute(
const RibEntry& entry,
const Route& route,
465 const Route& existingRoute)
467 const bool costDidChange = (route.
cost != existingRoute.
cost);
473 if (route.
flags == existingRoute.
flags && !costDidChange) {
492 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
495 if ((route.
flags == existingRoute.
flags) && route.isChildInherit()) {
498 routesToAdd.insert(route);
507 if (!existingRoute.isChildInherit() && route.isChildInherit()) {
510 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
513 routesToAdd.insert(route);
517 else if (existingRoute.isChildInherit() && !route.isChildInherit()) {
520 routesToRemove.insert(route);
524 if (prevRoute !=
nullptr) {
525 routesToAdd.insert(*prevRoute);
529 const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
533 if (it != ancestorRoutes.end()) {
534 routesToAdd.insert(*it);
538 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
542 if (!existingRoute.isRibCapture() && route.isRibCapture()) {
543 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
546 removeInheritedRoutes(entry, ancestorRoutes);
551 else if (existingRoute.isRibCapture() && !route.isRibCapture()) {
552 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
555 addInheritedRoutes(entry, ancestorRoutes);
563 FibUpdater::createFibUpdatesForErasedRoute(
const RibEntry& entry,
const Route& route,
564 const bool captureWasTurnedOff)
568 if (route.isChildInherit() && route.isRibCapture()) {
571 routesToRemove.insert(route);
576 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
578 routesToAdd = m_rib.getAncestorRoutes(entry);
581 addInheritedRoutes(entry, routesToAdd);
584 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
586 else if (route.isChildInherit()) {
590 routesToAdd = m_rib.getAncestorRoutes(entry);
594 routesToRemove.insert(route);
597 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
599 else if (route.isRibCapture()) {
603 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
605 routesToAdd = m_rib.getAncestorRoutes(entry);
608 addInheritedRoutes(entry, routesToAdd);
615 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
623 if (it != ancestorRoutes.end()) {
624 addInheritedRoute(entry.
getName(), *it);
631 FibUpdater::createFibUpdatesForErasedRibEntry(
const RibEntry& entry)
643 for (
const auto& child : children) {
644 traverseSubTree(*child, routesToAdd, routesToRemove);
649 FibUpdater::traverseSubTree(
const RibEntry& entry, Rib::Rib::RouteSet routesToAdd,
650 Rib::Rib::RouteSet routesToRemove)
658 for (Rib::RouteSet::const_iterator removeIt = routesToRemove.begin();
659 removeIt != routesToRemove.end(); )
664 routesToRemove.erase(removeIt++);
670 removeInheritedRoute(entry.
getName(), *removeIt);
678 for (Rib::RouteSet::const_iterator addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
682 routesToAdd.erase(addIt++);
688 addInheritedRoute(entry.
getName(), *addIt);
695 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
699 FibUpdater::addInheritedRoute(
const Name& name,
const Route& route)
706 m_inheritedRoutes.push_back(update);
710 FibUpdater::removeInheritedRoute(
const Name& name,
const Route& route)
717 m_inheritedRoutes.push_back(update);
std::list< FibUpdate > FibUpdateList
void eraseRoute(const Route &route)
erases a Route with the same faceId and origin
RibUpdate & setRoute(const Route &route)
represents the Routing Information Base
#define NFD_LOG_DEBUG(expression)
std::set< Route, RouteComparePredicate > RouteSet
RibUpdate & setAction(Action action)
An update triggered by a face destruction notification.
const_iterator find(const Name &prefix) const
function< void(RibUpdateList inheritedRoutes)> FibUpdateSuccessCallback
const Name & getName() const
std::list< shared_ptr< RibEntry > > findDescendantsForNonInsertedName(const Name &prefix) const
finds namespaces under the passed prefix
represents a collection of RibUpdates to be applied to a single FaceId
void setFibUpdater(FibUpdater *updater)
const_iterator end() const
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 ...
Table::const_iterator iterator
RibUpdate & setName(const Name &name)
static FibUpdate createAddUpdate(const Name &name, const uint64_t faceId, const uint64_t cost)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
const Name & getName() const
const std::list< shared_ptr< RibEntry > > & getChildren() const
std::list< shared_ptr< RibEntry > > RibEntryList
represents a route for a name prefix
RouteList::const_iterator const_iterator
bool hasInheritedRoute(const Route &route) const
Determines if the entry has an inherited route with a matching face ID.
FibUpdater(Rib &rib, ndn::nfd::Controller &controller)
uint64_t getFaceId() const
#define NFD_LOG_INIT(name)
ndn::optional< time::steady_clock::TimePoint > expires
bool hasChildInheritOnFaceId(uint64_t faceId) const
Determines if the entry has an inherited route with the passed face ID and its child inherit flag set...
const Route & getRoute() const
RibTable::const_iterator const_iterator
represents a RIB entry, which contains one or more Routes with the same prefix
function< void(uint32_t code, const std::string &error)> FibUpdateFailureCallback
size_t getNRoutes() const
const Route * getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const
Returns the route with the lowest cost that has the passed face ID and its child inherit flag set...
iterator findRoute(const Route &route)
bool hasFaceId(const uint64_t faceId) const
static FibUpdate createRemoveUpdate(const Name &name, const uint64_t faceId)
const Route * getRouteWithLowestCostByFaceId(uint64_t faceId) const
Returns the route with the lowest cost that has the passed face ID.
const RouteList & getInheritedRoutes() const
Returns the routes this namespace has inherited.
shared_ptr< RibEntry > findParent(const Name &prefix) const
std::underlying_type< ndn::nfd::RouteFlags >::type flags