30 #include <ndn-cxx/mgmt/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, 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, 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,
290 uint32_t code = response.
getCode();
292 " (code: " << code <<
", error: " << response.
getText() <<
")");
295 sendAddNextHopUpdate(update, onSuccess, onFailure, ++nTimeouts);
297 else if (code == ERROR_FACE_NOT_FOUND) {
298 if (update.
faceId == m_batchFaceId) {
299 onFailure(code, response.
getText());
302 m_updatesForNonBatchFaceId.remove(update);
304 if (m_updatesForNonBatchFaceId.size() == 0) {
305 onSuccess(m_inheritedRoutes);
310 BOOST_THROW_EXCEPTION(
Error(
"Non-recoverable error: " + response.
getText() +
316 FibUpdater::addFibUpdate(
FibUpdate update)
319 m_updatesForNonBatchFaceId;
325 return update.
name == other.name && update.
faceId == other.faceId;
328 if (it != updates.end()) {
334 updates.push_back(update);
341 for (
const Route& route : routesToAdd) {
345 addInheritedRoute(entry.
getName(), route);
356 for (
const Route& route : routesToAdd) {
357 if (route.faceId != ignore.
faceId) {
359 addInheritedRoute(name, route);
367 FibUpdater::removeInheritedRoutes(
const RibEntry& entry,
const Rib::Rib::RouteSet& routesToRemove)
369 for (
const Route& route : routesToRemove) {
372 removeInheritedRoute(entry.
getName(), route);
379 FibUpdater::createFibUpdatesForNewRibEntry(
const Name& name,
const Route& route,
388 addInheritedRoutes(name, m_rib.getAncestorRoutes(name), route);
393 routesToAdd.insert(route);
396 modifyChildrensInheritedRoutes(children, routesToAdd, m_rib.getAncestorRoutes(name));
399 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(name);
402 addInheritedRoutes(name, ancestorRoutes, route);
409 if (it != ancestorRoutes.end()) {
410 ancestorRoutes.erase(it);
414 ancestorRoutes.insert(route);
417 modifyChildrensInheritedRoutes(children, ancestorRoutes,
Rib::RouteSet());
421 modifyChildrensInheritedRoutes(children,
Rib::RouteSet(), m_rib.getAncestorRoutes(name));
426 FibUpdater::createFibUpdatesForNewRoute(
const RibEntry& entry,
const Route& route,
427 bool captureWasTurnedOn)
437 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
439 routesToAdd.insert(route);
444 if (captureWasTurnedOn) {
446 routesToRemove = m_rib.getAncestorRoutes(entry);
449 removeInheritedRoutes(entry, routesToRemove);
452 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
459 if (other ==
nullptr || route.
cost <= other->cost) {
465 FibUpdater::createFibUpdatesForUpdatedRoute(
const RibEntry& entry,
const Route& route,
466 const Route& existingRoute)
468 const bool costDidChange = (route.
cost != existingRoute.
cost);
474 if (route.
flags == existingRoute.
flags && !costDidChange) {
493 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
499 routesToAdd.insert(route);
511 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
514 routesToAdd.insert(route);
521 routesToRemove.insert(route);
525 if (prevRoute !=
nullptr) {
526 routesToAdd.insert(*prevRoute);
530 const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
534 if (it != ancestorRoutes.end()) {
535 routesToAdd.insert(*it);
539 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
544 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
547 removeInheritedRoutes(entry, ancestorRoutes);
553 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
556 addInheritedRoutes(entry, ancestorRoutes);
564 FibUpdater::createFibUpdatesForErasedRoute(
const RibEntry& entry,
const Route& route,
565 const bool captureWasTurnedOff)
572 routesToRemove.insert(route);
577 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
579 routesToAdd = m_rib.getAncestorRoutes(entry);
582 addInheritedRoutes(entry, routesToAdd);
585 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
591 routesToAdd = m_rib.getAncestorRoutes(entry);
595 routesToRemove.insert(route);
598 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
604 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
606 routesToAdd = m_rib.getAncestorRoutes(entry);
609 addInheritedRoutes(entry, routesToAdd);
616 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
624 if (it != ancestorRoutes.end()) {
625 addInheritedRoute(entry.
getName(), *it);
632 FibUpdater::createFibUpdatesForErasedRibEntry(
const RibEntry& entry)
644 for (
const auto& child : children) {
645 traverseSubTree(*child, routesToAdd, routesToRemove);
650 FibUpdater::traverseSubTree(
const RibEntry& entry, Rib::Rib::RouteSet routesToAdd,
651 Rib::Rib::RouteSet routesToRemove)
659 for (Rib::RouteSet::const_iterator removeIt = routesToRemove.begin();
660 removeIt != routesToRemove.end(); )
665 routesToRemove.erase(removeIt++);
671 removeInheritedRoute(entry.
getName(), *removeIt);
679 for (Rib::RouteSet::const_iterator addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
683 routesToAdd.erase(addIt++);
689 addInheritedRoute(entry.
getName(), *addIt);
696 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
700 FibUpdater::addInheritedRoute(
const Name& name,
const Route& route)
707 m_inheritedRoutes.push_back(update);
711 FibUpdater::removeInheritedRoute(
const Name& name,
const Route& route)
718 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)
represents the Routing Information Base
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
#define NFD_LOG_DEBUG(expression)
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 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...
RibUpdate & setName(const Name &name)
RouteList::const_iterator const_iterator
const RouteList & getInheritedRoutes() const
Returns the routes this namespace has inherited.
const std::string & getText() const
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 ...
#define NFD_LOG_INIT(name)
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