30 #include <ndn-cxx/management/nfd-control-parameters.hpp> 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);
76 switch(update.getAction()) {
78 computeUpdatesForRegistration(update);
81 computeUpdatesForUnregistration(update);
84 computeUpdatesForUnregistration(update);
88 m_updatesForBatchFaceId.clear();
95 FibUpdater::computeUpdatesForRegistration(
const RibUpdate& update)
103 if (it != m_rib.
end()) {
104 shared_ptr<const RibEntry> entry(it->second);
109 if (existingRoute == entry->end()) {
111 bool willCaptureBeTurnedOn = (entry->hasCapture() ==
false && route.
isCapture());
113 createFibUpdatesForNewRoute(*entry, route, willCaptureBeTurnedOn);
125 createFibUpdatesForUpdatedRoute(entryCopy, route, *existingRoute);
131 shared_ptr<RibEntry> parent = m_rib.
findParent(prefix);
136 for (
const auto& descendant : descendants) {
139 if (descendant->getParent() == parent) {
140 children.push_back(descendant);
144 createFibUpdatesForNewRibEntry(prefix, route, children);
149 FibUpdater::computeUpdatesForUnregistration(
const RibUpdate& update)
157 if (ribIt != m_rib.
end()) {
158 shared_ptr<const RibEntry> entry(ribIt->second);
160 const bool hadCapture = entry->hasCapture();
164 if (existing != entry->end()) {
170 const bool captureWasTurnedOff = (hadCapture && !temp.
hasCapture());
172 createFibUpdatesForErasedRoute(temp, *existing, captureWasTurnedOff);
176 const Route* next = entry->getRouteWithSecondLowestCostByFaceId(route.
faceId);
178 if (next !=
nullptr) {
179 createFibUpdatesForNewRoute(temp, *next,
false);
183 if (entry->getNRoutes() == 1) {
184 createFibUpdatesForErasedRibEntry(*entry);
195 std::string updateString = (updates.size() == 1) ?
" update" :
" updates";
196 NFD_LOG_DEBUG(
"Applying " << updates.size() << updateString <<
" to FIB");
198 for (
const FibUpdate& update : updates) {
202 sendAddNextHopUpdate(update, onSuccess, onFailure);
205 sendRemoveNextHopUpdate(update, onSuccess, onFailure);
214 if (m_updatesForBatchFaceId.size() > 0) {
215 sendUpdates(m_updatesForBatchFaceId, onSuccess, onFailure);
218 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
226 if (m_updatesForNonBatchFaceId.size() > 0) {
227 sendUpdates(m_updatesForNonBatchFaceId, onSuccess, onFailure);
230 onSuccess(m_inheritedRoutes);
235 FibUpdater::sendAddNextHopUpdate(
const FibUpdate& update,
242 .setName(update.
name)
244 .setCost(update.
cost),
245 bind(&FibUpdater::onUpdateSuccess,
this, update, onSuccess, onFailure),
246 bind(&FibUpdater::onUpdateError,
this, update, onSuccess, onFailure, _1, _2, nTimeouts));
250 FibUpdater::sendRemoveNextHopUpdate(
const FibUpdate& update,
257 .setName(update.
name)
258 .setFaceId(update.
faceId),
259 bind(&FibUpdater::onUpdateSuccess,
this, update, onSuccess, onFailure),
260 bind(&FibUpdater::onUpdateError,
this, update, onSuccess, onFailure, _1, _2, nTimeouts));
264 FibUpdater::onUpdateSuccess(
const FibUpdate update,
268 if (update.
faceId == m_batchFaceId) {
269 m_updatesForBatchFaceId.remove(update);
271 if (m_updatesForBatchFaceId.size() == 0) {
272 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
276 m_updatesForNonBatchFaceId.remove(update);
278 if (m_updatesForNonBatchFaceId.size() == 0) {
279 onSuccess(m_inheritedRoutes);
285 FibUpdater::onUpdateError(
const FibUpdate update,
288 uint32_t code,
const std::string& error, uint32_t nTimeouts)
290 NFD_LOG_DEBUG(
"Failed to apply " << update <<
" (code: " << code <<
", error: " << error <<
")");
293 sendAddNextHopUpdate(update, onSuccess, onFailure, ++nTimeouts);
295 else if (code == ERROR_FACE_NOT_FOUND) {
296 if (update.
faceId == m_batchFaceId) {
297 onFailure(code, error);
300 m_updatesForNonBatchFaceId.remove(update);
302 if (m_updatesForNonBatchFaceId.size() == 0) {
303 onSuccess(m_inheritedRoutes);
308 BOOST_THROW_EXCEPTION(
Error(
"Non-recoverable error: " + error +
" code: " +
to_string(code)));
313 FibUpdater::addFibUpdate(
FibUpdate update)
316 m_updatesForNonBatchFaceId;
322 return update.
name == other.name && update.
faceId == other.faceId;
325 if (it != updates.end()) {
331 updates.push_back(update);
338 for (
const Route& route : routesToAdd) {
342 addInheritedRoute(entry.
getName(), route);
353 for (
const Route& route : routesToAdd) {
354 if (route.faceId != ignore.
faceId) {
356 addInheritedRoute(name, route);
364 FibUpdater::removeInheritedRoutes(
const RibEntry& entry,
const Rib::Rib::RouteSet& routesToRemove)
366 for (
const Route& route : routesToRemove) {
369 removeInheritedRoute(entry.
getName(), route);
376 FibUpdater::createFibUpdatesForNewRibEntry(
const Name& name,
const Route& route,
385 addInheritedRoutes(name, m_rib.getAncestorRoutes(name), route);
390 routesToAdd.insert(route);
393 modifyChildrensInheritedRoutes(children, routesToAdd, m_rib.getAncestorRoutes(name));
396 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(name);
399 addInheritedRoutes(name, ancestorRoutes, route);
406 if (it != ancestorRoutes.end()) {
407 ancestorRoutes.erase(it);
411 ancestorRoutes.insert(route);
414 modifyChildrensInheritedRoutes(children, ancestorRoutes,
Rib::RouteSet());
418 modifyChildrensInheritedRoutes(children,
Rib::RouteSet(), m_rib.getAncestorRoutes(name));
423 FibUpdater::createFibUpdatesForNewRoute(
const RibEntry& entry,
const Route& route,
424 bool captureWasTurnedOn)
434 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
436 routesToAdd.insert(route);
441 if (captureWasTurnedOn) {
443 routesToRemove = m_rib.getAncestorRoutes(entry);
446 removeInheritedRoutes(entry, routesToRemove);
449 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
456 if (other ==
nullptr || route.
cost <= other->cost) {
462 FibUpdater::createFibUpdatesForUpdatedRoute(
const RibEntry& entry,
const Route& route,
463 const Route& existingRoute)
465 const bool costDidChange = (route.
cost != existingRoute.
cost);
471 if (route.
flags == existingRoute.
flags && !costDidChange) {
490 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
496 routesToAdd.insert(route);
508 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
511 routesToAdd.insert(route);
518 routesToRemove.insert(route);
522 if (prevRoute !=
nullptr) {
523 routesToAdd.insert(*prevRoute);
527 const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
531 if (it != ancestorRoutes.end()) {
532 routesToAdd.insert(*it);
536 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
541 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
544 removeInheritedRoutes(entry, ancestorRoutes);
550 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
553 addInheritedRoutes(entry, ancestorRoutes);
561 FibUpdater::createFibUpdatesForErasedRoute(
const RibEntry& entry,
const Route& route,
562 const bool captureWasTurnedOff)
569 routesToRemove.insert(route);
574 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
576 routesToAdd = m_rib.getAncestorRoutes(entry);
579 addInheritedRoutes(entry, routesToAdd);
582 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
588 routesToAdd = m_rib.getAncestorRoutes(entry);
592 routesToRemove.insert(route);
595 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
601 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
603 routesToAdd = m_rib.getAncestorRoutes(entry);
606 addInheritedRoutes(entry, routesToAdd);
613 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
621 if (it != ancestorRoutes.end()) {
622 addInheritedRoute(entry.
getName(), *it);
629 FibUpdater::createFibUpdatesForErasedRibEntry(
const RibEntry& entry)
641 for (
const auto& child : children) {
642 traverseSubTree(*child, routesToAdd, routesToRemove);
647 FibUpdater::traverseSubTree(
const RibEntry& entry, Rib::Rib::RouteSet routesToAdd,
648 Rib::Rib::RouteSet routesToRemove)
656 for (Rib::RouteSet::const_iterator removeIt = routesToRemove.begin();
657 removeIt != routesToRemove.end(); )
662 routesToRemove.erase(removeIt++);
668 removeInheritedRoute(entry.
getName(), *removeIt);
676 for (Rib::RouteSet::const_iterator addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
680 routesToAdd.erase(addIt++);
686 addInheritedRoute(entry.
getName(), *addIt);
693 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
697 FibUpdater::addInheritedRoute(
const Name& name,
const Route& route)
704 m_inheritedRoutes.push_back(update);
708 FibUpdater::removeInheritedRoute(
const Name& name,
const Route& route)
715 m_inheritedRoutes.push_back(update);
RibUpdate & setRoute(const Route &route)
void start(const ControlParameters ¶meters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start command execution
const_iterator end() const
FibUpdater(Rib &rib, ndn::nfd::Controller &controller)
#define NFD_LOG_DEBUG(expression)
represents a fib/add-nexthop command
const Route & getRoute() const
function< void(RibUpdateList inheritedRoutes)> FibUpdateSuccessCallback
time::steady_clock::TimePoint expires
iterator findRoute(const Route &route)
bool hasFaceId(const uint64_t faceId) const
represents a collection of RibUpdates to be applied to a single FaceId
size_t getNRoutes() const
const_iterator find(const Name &prefix) const
bool isChildInherit() const
std::list< FibUpdate > FibUpdateList
function< void(uint32_t code, const std::string &error)> FibUpdateFailureCallback
represents a fib/remove-nexthop command
const Name & getName() const
static const uint32_t ERROR_TIMEOUT
error code for timeout
std::set< Route, RouteComparePredicate > RouteSet
Table::const_iterator iterator
std::list< shared_ptr< RibEntry > > RibEntryList
const Name & getName() const
static FibUpdate createRemoveUpdate(const Name &name, const uint64_t faceId)
Copyright (c) 2011-2015 Regents of the University of California.
static FibUpdate createAddUpdate(const Name &name, const uint64_t faceId, const uint64_t cost)
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...
RibUpdate & setAction(Action action)
represents a RIB entry, which contains one or more Routes with the same prefix
represents a route for a name prefix
shared_ptr< RibEntry > findParent(const Name &prefix) const
NFD Management protocol - ControlCommand client.
bool hasInheritedRoute(const Route &route) const
Determines if the entry has an inherited route with a matching face ID.
Name abstraction to represent an absolute name.
RibTable::const_iterator const_iterator
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...
#define NFD_LOG_INIT(name)
RibUpdate & setName(const Name &name)
RouteList::const_iterator const_iterator
const RouteList & getInheritedRoutes() const
Returns the routes this namespace has inherited.
void eraseRoute(const Route &route)
erases a Route with the same faceId and origin
std::list< shared_ptr< RibEntry > > findDescendantsForNonInsertedName(const Name &prefix) const
finds namespaces under the passed prefix
std::string to_string(const V &v)
An update triggered by a face destruction notification.
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 ...
const Route * getRouteWithLowestCostByFaceId(uint64_t faceId) const
Returns the route with the lowest cost that has the passed face ID.
uint64_t getFaceId() const
void setFibUpdater(FibUpdater *updater)
const std::list< shared_ptr< RibEntry > > & getChildren() const