43 m_fibUpdater = updater;
49 return m_rib.find(prefix);
55 auto ribIt = m_rib.find(prefix);
58 if (ribIt != m_rib.end()) {
59 shared_ptr<RibEntry> entry = ribIt->second;
60 auto routeIt = entry->findRoute(route);
61 if (routeIt != entry->end()) {
72 Route* existingRoute =
find(prefix, route);
73 if (existingRoute ==
nullptr) {
76 existingRoute =
find(parent->getName(), route);
86 auto ribIt = m_rib.find(prefix);
89 if (ribIt != m_rib.end()) {
90 shared_ptr<RibEntry> entry(ribIt->second);
91 auto [entryIt, didInsert] = entry->insertRoute(route);
100 m_faceEntries.emplace(route.
faceId, entry);
105 if (entryIt->getExpirationEvent()) {
106 NFD_LOG_TRACE(
"Cancelling expiration event for " << entry->getName() <<
" " << *entryIt);
107 entryIt->cancelExpirationEvent();
115 auto entry = make_shared<RibEntry>();
117 m_rib[prefix] = entry;
120 entry->setName(prefix);
121 auto routeIt = entry->insertRoute(route).first;
124 shared_ptr<RibEntry> parent =
findParent(prefix);
127 if (parent !=
nullptr) {
128 parent->addChild(entry);
131 auto children = findDescendants(prefix);
132 for (
const auto& child : children) {
133 if (child->getParent() == parent) {
135 if (parent !=
nullptr) {
136 parent->removeChild(child);
138 entry->addChild(child);
143 m_faceEntries.emplace(route.
faceId, entry);
152 Rib::erase(
const Name& prefix,
const Route& route)
154 auto ribIt = m_rib.find(prefix);
155 if (ribIt == m_rib.end()) {
160 shared_ptr<RibEntry> entry = ribIt->second;
161 auto routeIt = entry->findRoute(route);
163 if (routeIt != entry->end()) {
166 auto faceId = route.
faceId;
167 entry->eraseRoute(routeIt);
171 if (!entry->hasFaceId(faceId)) {
172 auto range = m_faceEntries.equal_range(faceId);
173 for (
auto it = range.first; it != range.second; ++it) {
174 if (it->second == entry) {
175 m_faceEntries.erase(it);
182 if (entry->getRoutes().empty()) {
204 for (
int i = prefix.size() - 1; i >= 0; i--) {
205 auto it = m_rib.find(prefix.getPrefix(i));
206 if (it != m_rib.end()) {
214 std::list<shared_ptr<RibEntry>>
215 Rib::findDescendants(
const Name& prefix)
const
217 std::list<shared_ptr<RibEntry>> children;
219 auto it = m_rib.find(prefix);
220 if (it != m_rib.end()) {
222 for (; it != m_rib.end(); ++it) {
223 if (prefix.isPrefixOf(it->first)) {
224 children.push_back(it->second);
235 std::list<shared_ptr<RibEntry>>
236 Rib::findDescendantsForNonInsertedName(
const Name& prefix)
const
238 std::list<shared_ptr<RibEntry>> children;
240 for (
const auto& [name, ribEntry] : m_rib) {
241 if (prefix.isPrefixOf(name)) {
242 children.push_back(ribEntry);
249 Rib::RibTable::iterator
250 Rib::eraseEntry(RibTable::iterator it)
253 if (it == m_rib.end()) {
257 shared_ptr<RibEntry> entry(it->second);
258 shared_ptr<RibEntry> parent = entry->getParent();
261 if (parent !=
nullptr) {
262 parent->removeChild(entry);
265 for (
auto childIt = entry->getChildren().begin(); childIt != entry->getChildren().end(); ) {
266 shared_ptr<RibEntry> child = *childIt;
272 entry->removeChild(child);
275 if (parent !=
nullptr) {
276 parent->addChild(child);
280 auto nextIt = m_rib.erase(it);
289 Rib::getAncestorRoutes(
const RibEntry& entry)
const
293 auto parent = entry.getParent();
294 while (parent !=
nullptr) {
295 for (
const auto& route : parent->getRoutes()) {
296 if (route.isChildInherit()) {
297 ancestorRoutes.insert(route);
301 if (parent->hasCapture()) {
305 parent = parent->getParent();
308 return ancestorRoutes;
312 Rib::getAncestorRoutes(
const Name& name)
const
317 while (parent !=
nullptr) {
318 for (
const auto& route : parent->getRoutes()) {
319 if (route.isChildInherit()) {
320 ancestorRoutes.insert(route);
324 if (parent->hasCapture()) {
328 parent = parent->getParent();
331 return ancestorRoutes;
339 BOOST_ASSERT(m_fibUpdater !=
nullptr);
340 addUpdateToQueue(update, onSuccess, onFailure);
341 sendBatchFromQueue();
347 auto range = m_faceEntries.equal_range(faceId);
348 for (
auto it = range.first; it != range.second; ++it) {
349 enqueueRemoveFace(*it->second, faceId);
351 sendBatchFromQueue();
357 for (
const auto& [faceId, ribEntry] : m_faceEntries) {
358 if (activeFaceIds.count(faceId) > 0) {
361 enqueueRemoveFace(*ribEntry, faceId);
363 sendBatchFromQueue();
367 Rib::enqueueRemoveFace(
const RibEntry& entry, uint64_t faceId)
369 for (
const Route& route : entry) {
370 if (route.
faceId != faceId) {
378 addUpdateToQueue(update,
nullptr,
nullptr);
383 Rib::addUpdateToQueue(
const RibUpdate& update,
387 RibUpdateBatch batch(update.getRoute().faceId);
390 UpdateQueueItem item{batch, onSuccess, onFailure};
391 m_updateBatches.push_back(std::move(item));
395 Rib::sendBatchFromQueue()
397 if (m_updateBatches.empty() || m_isUpdateInProgress) {
401 m_isUpdateInProgress =
true;
403 UpdateQueueItem item = std::move(m_updateBatches.front());
404 m_updateBatches.pop_front();
407 BOOST_ASSERT(item.batch.size() == 1);
410 [
this, batch = item.batch, successCb = item.managerSuccessCallback] (
const auto& routes) {
411 onFibUpdateSuccess(batch, routes, successCb);
413 [
this, failureCb = item.managerFailureCallback] (
const auto& code,
const auto& error) {
414 onFibUpdateFailure(failureCb, code, error);
419 Rib::onFibUpdateSuccess(
const RibUpdateBatch& batch,
423 for (
const RibUpdate& update : batch) {
424 switch (update.getAction()) {
426 insert(update.getName(), update.getRoute());
430 erase(update.getName(), update.getRoute());
436 modifyInheritedRoutes(inheritedRoutes);
438 m_isUpdateInProgress =
false;
440 if (onSuccess !=
nullptr) {
445 sendBatchFromQueue();
450 uint32_t code,
const std::string& error)
452 m_isUpdateInProgress =
false;
454 if (onFailure !=
nullptr) {
455 onFailure(code, error);
459 sendBatchFromQueue();
463 Rib::modifyInheritedRoutes(
const RibUpdateList& inheritedRoutes)
465 for (
const RibUpdate& update : inheritedRoutes) {
466 auto ribIt = m_rib.find(update.getName());
467 BOOST_ASSERT(ribIt != m_rib.end());
468 shared_ptr<RibEntry> entry(ribIt->second);
470 switch (update.getAction()) {
472 entry->addInheritedRoute(update.getRoute());
475 entry->removeInheritedRoute(update.getRoute());
486 for (
const auto& item : rib) {
487 os << *item.second <<
"\n";
Computes FibUpdates based on updates to the RIB and sends them to NFD.
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 a RIB entry, which contains one or more Routes with the same prefix.
Represents the Routing Information Base.
signal::Signal< Rib, Name > afterInsertEntry
Signals after a RIB entry is inserted.
signal::Signal< Rib, RibRouteRef > afterAddRoute
Signals after a Route is added.
void beginRemoveFailedFaces(const std::set< uint64_t > &activeFaceIds)
signal::Signal< Rib, RibRouteRef > beforeRemoveRoute
Signals before a route is removed.
std::function< void()> UpdateSuccessCallback
void setFibUpdater(FibUpdater *updater)
void beginRemoveFace(uint64_t faceId)
Starts the FIB update process when a face has been destroyed.
RibTable::const_iterator const_iterator
Route * findLongestPrefix(const Name &prefix, const Route &route) const
void insert(const Name &prefix, const Route &route)
signal::Signal< Rib, Name > afterEraseEntry
Signals after a RIB entry is erased.
void onRouteExpiration(const Name &prefix, const Route &route)
const_iterator find(const Name &prefix) const
void beginApplyUpdate(const RibUpdate &update, const UpdateSuccessCallback &onSuccess, const UpdateFailureCallback &onFailure)
Passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
std::function< void(uint32_t code, const std::string &error)> UpdateFailureCallback
shared_ptr< RibEntry > findParent(const Name &prefix) const
Represents a route that will be added to or removed from a namespace.
RibUpdate & setAction(Action action)
@ REMOVE_FACE
An update triggered by a face destruction notification.
RibUpdate & setName(const Name &name)
RibUpdate & setRoute(const Route &route)
Represents a route for a name prefix.
#define NFD_LOG_INIT(name)
std::ostream & operator<<(std::ostream &os, const FibUpdate &update)
std::list< RibUpdate > RibUpdateList
static bool sortRoutes(const Route &lhs, const Route &rhs)