43 , m_controller(controller)
56 m_inheritedRoutes.clear();
59 m_updatesForBatchFaceId.clear();
60 m_updatesForNonBatchFaceId.clear();
62 computeUpdates(batch);
64 sendUpdatesForBatchFaceId(onSuccess, onFailure);
74 switch (update.getAction()) {
76 computeUpdatesForRegistration(update);
79 computeUpdatesForUnregistration(update);
82 computeUpdatesForUnregistration(update);
86 m_updatesForBatchFaceId.clear();
93 FibUpdater::computeUpdatesForRegistration(
const RibUpdate& update)
98 auto it = m_rib.
find(prefix);
101 if (it != m_rib.
end()) {
102 shared_ptr<const RibEntry> entry(it->second);
104 auto existingRoute = entry->findRoute(route);
107 if (existingRoute == entry->end()) {
109 bool willCaptureBeTurnedOn = (entry->hasCapture() ==
false && route.
isRibCapture());
111 createFibUpdatesForNewRoute(*entry, route, willCaptureBeTurnedOn);
122 createFibUpdatesForUpdatedRoute(entryCopy, route, *existingRoute);
128 shared_ptr<RibEntry> parent = m_rib.
findParent(prefix);
133 for (
const auto& descendant : descendants) {
136 if (descendant->getParent() == parent) {
137 children.push_back(descendant);
141 createFibUpdatesForNewRibEntry(prefix, route, children);
146 FibUpdater::computeUpdatesForUnregistration(
const RibUpdate& update)
151 auto ribIt = m_rib.
find(prefix);
154 if (ribIt != m_rib.
end()) {
155 shared_ptr<const RibEntry> entry(ribIt->second);
156 const bool hadCapture = entry->hasCapture();
158 auto existing = entry->findRoute(route);
159 if (existing != entry->end()) {
165 const bool captureWasTurnedOff = (hadCapture && !temp.
hasCapture());
167 createFibUpdatesForErasedRoute(temp, *existing, captureWasTurnedOff);
171 const Route* next = entry->getRouteWithSecondLowestCostByFaceId(route.
faceId);
173 if (next !=
nullptr) {
174 createFibUpdatesForNewRoute(temp, *next,
false);
178 if (entry->getNRoutes() == 1) {
179 createFibUpdatesForErasedRibEntry(*entry);
190 std::string updateString = (updates.size() == 1) ?
" update" :
" updates";
191 NFD_LOG_DEBUG(
"Applying " << updates.size() << updateString <<
" to FIB");
193 for (
const FibUpdate& update : updates) {
197 sendAddNextHopUpdate(update, onSuccess, onFailure);
200 sendRemoveNextHopUpdate(update, onSuccess, onFailure);
209 if (m_updatesForBatchFaceId.size() > 0) {
210 sendUpdates(m_updatesForBatchFaceId, onSuccess, onFailure);
213 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
221 if (m_updatesForNonBatchFaceId.size() > 0) {
222 sendUpdates(m_updatesForNonBatchFaceId, onSuccess, onFailure);
225 onSuccess(m_inheritedRoutes);
230 FibUpdater::sendAddNextHopUpdate(
const FibUpdate& update,
237 .setName(update.
name)
239 .setCost(update.
cost),
240 [=] (
const auto&) { onUpdateSuccess(update, onSuccess, onFailure); },
241 [=] (
const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); });
245 FibUpdater::sendRemoveNextHopUpdate(
const FibUpdate& update,
252 .setName(update.
name)
253 .setFaceId(update.
faceId),
254 [=] (
const auto&) { onUpdateSuccess(update, onSuccess, onFailure); },
255 [=] (
const auto& resp) { onUpdateError(update, onSuccess, onFailure, resp, nTimeouts); });
259 FibUpdater::onUpdateSuccess(
const FibUpdate& update,
263 if (update.
faceId == m_batchFaceId) {
264 m_updatesForBatchFaceId.remove(update);
266 if (m_updatesForBatchFaceId.size() == 0) {
267 sendUpdatesForNonBatchFaceId(onSuccess, onFailure);
271 m_updatesForNonBatchFaceId.remove(update);
273 if (m_updatesForNonBatchFaceId.size() == 0) {
274 onSuccess(m_inheritedRoutes);
280 FibUpdater::onUpdateError(
const FibUpdate& update,
285 uint32_t code = response.
getCode();
287 " (code: " << code <<
", error: " << response.
getText() <<
")");
290 sendAddNextHopUpdate(update, onSuccess, onFailure, ++nTimeouts);
292 else if (code == ERROR_FACE_NOT_FOUND) {
293 if (update.
faceId == m_batchFaceId) {
294 onFailure(code, response.
getText());
297 m_updatesForNonBatchFaceId.remove(update);
299 if (m_updatesForNonBatchFaceId.size() == 0) {
300 onSuccess(m_inheritedRoutes);
310 FibUpdater::addFibUpdate(
const FibUpdate& update)
313 m_updatesForNonBatchFaceId;
316 auto it = std::find_if(updates.begin(), updates.end(),
318 return update.
name == other.name && update.
faceId == other.faceId;
321 if (it != updates.end()) {
327 updates.push_back(update);
332 FibUpdater::addInheritedRoutes(
const RibEntry& entry,
const Rib::RouteSet& routesToAdd)
334 for (
const Route& route : routesToAdd) {
338 addInheritedRoute(entry.
getName(), route);
346 FibUpdater::addInheritedRoutes(
const Name&
name,
const Rib::RouteSet& routesToAdd,
349 for (
const Route& route : routesToAdd) {
350 if (route.faceId != ignore.
faceId) {
352 addInheritedRoute(name, route);
360 FibUpdater::removeInheritedRoutes(
const RibEntry& entry,
const Rib::Rib::RouteSet& routesToRemove)
362 for (
const Route& route : routesToRemove) {
365 removeInheritedRoute(entry.
getName(), route);
372 FibUpdater::createFibUpdatesForNewRibEntry(
const Name& name,
const Route& route,
381 addInheritedRoutes(name, m_rib.getAncestorRoutes(name), route);
385 Rib::RouteSet routesToAdd;
386 routesToAdd.insert(route);
389 modifyChildrensInheritedRoutes(children, routesToAdd, m_rib.getAncestorRoutes(name));
392 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(name);
395 addInheritedRoutes(name, ancestorRoutes, route);
399 auto it = ancestorRoutes.find(route);
402 if (it != ancestorRoutes.end()) {
403 ancestorRoutes.erase(it);
407 ancestorRoutes.insert(route);
410 modifyChildrensInheritedRoutes(children, ancestorRoutes, Rib::RouteSet());
414 modifyChildrensInheritedRoutes(children, Rib::RouteSet(), m_rib.getAncestorRoutes(name));
419 FibUpdater::createFibUpdatesForNewRoute(
const RibEntry& entry,
const Route& route,
420 bool captureWasTurnedOn)
425 Rib::RouteSet routesToAdd;
430 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
432 routesToAdd.insert(route);
436 Rib::RouteSet routesToRemove;
437 if (captureWasTurnedOn) {
439 routesToRemove = m_rib.getAncestorRoutes(entry);
442 removeInheritedRoutes(entry, routesToRemove);
445 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
452 if (other ==
nullptr || route.
cost <= other->cost) {
458 FibUpdater::createFibUpdatesForUpdatedRoute(
const RibEntry& entry,
const Route& route,
459 const Route& existingRoute)
461 const bool costDidChange = (route.
cost != existingRoute.
cost);
467 if (route.
flags == existingRoute.
flags && !costDidChange) {
486 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
491 Rib::RouteSet routesToAdd;
492 routesToAdd.insert(route);
493 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, Rib::RouteSet());
504 if (prevRoute ==
nullptr || route.
cost <= prevRoute->
cost) {
506 Rib::RouteSet routesToAdd;
507 routesToAdd.insert(route);
508 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, Rib::RouteSet());
513 Rib::RouteSet routesToRemove;
514 routesToRemove.insert(route);
516 Rib::RouteSet routesToAdd;
518 if (prevRoute !=
nullptr) {
519 routesToAdd.insert(*prevRoute);
523 const Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
524 auto it = ancestorRoutes.find(route);
527 if (it != ancestorRoutes.end()) {
528 routesToAdd.insert(*it);
532 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
537 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
540 removeInheritedRoutes(entry, ancestorRoutes);
543 modifyChildrensInheritedRoutes(entry.
getChildren(), Rib::RouteSet(), ancestorRoutes);
546 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
549 addInheritedRoutes(entry, ancestorRoutes);
552 modifyChildrensInheritedRoutes(entry.
getChildren(), ancestorRoutes, Rib::RouteSet());
557 FibUpdater::createFibUpdatesForErasedRoute(
const RibEntry& entry,
const Route& route,
558 const bool captureWasTurnedOff)
564 Rib::RouteSet routesToRemove;
565 routesToRemove.insert(route);
569 Rib::RouteSet routesToAdd;
570 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
572 routesToAdd = m_rib.getAncestorRoutes(entry);
575 addInheritedRoutes(entry, routesToAdd);
578 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
582 Rib::RouteSet routesToAdd;
584 routesToAdd = m_rib.getAncestorRoutes(entry);
587 Rib::RouteSet routesToRemove;
588 routesToRemove.insert(route);
591 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
596 Rib::RouteSet routesToAdd;
597 if (captureWasTurnedOff && entry.
getNRoutes() != 0) {
599 routesToAdd = m_rib.getAncestorRoutes(entry);
602 addInheritedRoutes(entry, routesToAdd);
605 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, Rib::RouteSet());
609 Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);
615 auto it = ancestorRoutes.find(route);
617 if (it != ancestorRoutes.end()) {
618 addInheritedRoute(entry.
getName(), *it);
625 FibUpdater::createFibUpdatesForErasedRibEntry(
const RibEntry& entry)
634 const Rib::RouteSet& routesToAdd,
635 const Rib::RouteSet& routesToRemove)
637 for (
const auto& child : children) {
638 traverseSubTree(*child, routesToAdd, routesToRemove);
643 FibUpdater::traverseSubTree(
const RibEntry& entry, Rib::Rib::RouteSet routesToAdd,
644 Rib::Rib::RouteSet routesToRemove)
652 for (
auto removeIt = routesToRemove.begin(); removeIt != routesToRemove.end(); ) {
656 removeIt = routesToRemove.erase(removeIt);
662 removeInheritedRoute(entry.
getName(), *removeIt);
670 for (
auto addIt = routesToAdd.begin(); addIt != routesToAdd.end(); ) {
673 addIt = routesToAdd.erase(addIt);
679 addInheritedRoute(entry.
getName(), *addIt);
686 modifyChildrensInheritedRoutes(entry.
getChildren(), routesToAdd, routesToRemove);
690 FibUpdater::addInheritedRoute(
const Name& name,
const Route& route)
697 m_inheritedRoutes.push_back(update);
701 FibUpdater::removeInheritedRoute(
const Name& name,
const Route& route)
708 m_inheritedRoutes.push_back(update);
bool hasFaceId(uint64_t faceId) const
RibUpdate & setRoute(const Route &route)
void start(const ControlParameters ¶meters, const CommandSucceedCallback &onSuccess, const CommandFailCallback &onFailure, const CommandOptions &options=CommandOptions())
start command execution
FibUpdater(Rib &rib, ndn::nfd::Controller &controller)
represents the Routing Information Base
represents a fib/add-nexthop command
std::underlying_type_t< ndn::nfd::RouteFlags > flags
const Route & getRoute() const
#define NFD_LOG_INIT(name)
std::string to_string(const T &val)
iterator findRoute(const Route &route)
Represents a collection of RibUpdates to be applied to a single FaceId.
size_t getNRoutes() const
const_iterator find(const Name &prefix) const
computes FibUpdates based on updates to the RIB and sends them to NFD
constexpr int MAX_NUM_TIMEOUTS
bool isRibCapture() const
represents a fib/remove-nexthop command
const Name & getName() const
static const uint32_t ERROR_TIMEOUT
error code for timeout
constexpr uint32_t ERROR_FACE_NOT_FOUND
std::list< shared_ptr< RibEntry > > RibEntryList
optional< time::steady_clock::time_point > expires
std::list< FibUpdate > FibUpdateList
const Name & getName() const
const_iterator end() 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.
Represents an absolute name.
std::function< void(uint32_t code, const std::string &error)> FibUpdateFailureCallback
bool isChildInherit() const
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)
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
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
std::function< void(RibUpdateList inheritedRoutes)> FibUpdateSuccessCallback
void setFibUpdater(FibUpdater *updater)
const std::list< shared_ptr< RibEntry > > & getChildren() const