37 return std::tie(lhs.
entry->getName(), lhs.
route->faceId, lhs.
route->origin) <
50 m_fibUpdater = updater;
56 return m_rib.find(prefix);
62 auto ribIt = m_rib.find(prefix);
65 if (ribIt != m_rib.end()) {
66 shared_ptr<RibEntry> entry = ribIt->second;
67 auto routeIt = entry->findRoute(route);
68 if (routeIt != entry->end()) {
79 Route* existingRoute =
find(prefix, route);
80 if (existingRoute ==
nullptr) {
83 existingRoute =
find(parent->getName(), route);
93 auto ribIt = m_rib.find(prefix);
96 if (ribIt != m_rib.end()) {
97 shared_ptr<RibEntry> entry(ribIt->second);
98 auto [entryIt, didInsert] = entry->insertRoute(route);
107 m_faceEntries.emplace(route.
faceId, entry);
112 if (entryIt->getExpirationEvent()) {
113 NFD_LOG_TRACE(
"Cancelling expiration event for " << entry->getName() <<
" " << *entryIt);
114 entryIt->cancelExpirationEvent();
122 auto entry = make_shared<RibEntry>();
124 m_rib[prefix] = entry;
127 entry->setName(prefix);
128 auto routeIt = entry->insertRoute(route).first;
131 shared_ptr<RibEntry> parent =
findParent(prefix);
134 if (parent !=
nullptr) {
135 parent->addChild(entry);
138 auto children = findDescendants(prefix);
139 for (
const auto& child : children) {
140 if (child->getParent() == parent) {
142 if (parent !=
nullptr) {
143 parent->removeChild(child);
145 entry->addChild(child);
150 m_faceEntries.emplace(route.
faceId, entry);
159 Rib::erase(
const Name& prefix,
const Route& route)
161 auto ribIt = m_rib.find(prefix);
162 if (ribIt == m_rib.end()) {
167 shared_ptr<RibEntry> entry = ribIt->second;
168 auto routeIt = entry->findRoute(route);
170 if (routeIt != entry->end()) {
173 auto faceId = route.
faceId;
174 entry->eraseRoute(routeIt);
178 if (!entry->hasFaceId(faceId)) {
179 auto range = m_faceEntries.equal_range(faceId);
180 for (
auto it = range.first; it != range.second; ++it) {
181 if (it->second == entry) {
182 m_faceEntries.erase(it);
189 if (entry->getRoutes().empty()) {
211 for (
int i = prefix.size() - 1; i >= 0; i--) {
212 auto it = m_rib.find(prefix.getPrefix(i));
213 if (it != m_rib.end()) {
221 std::list<shared_ptr<RibEntry>>
222 Rib::findDescendants(
const Name& prefix)
const
224 std::list<shared_ptr<RibEntry>> children;
226 auto it = m_rib.find(prefix);
227 if (it != m_rib.end()) {
229 for (; it != m_rib.end(); ++it) {
230 if (prefix.isPrefixOf(it->first)) {
231 children.push_back(it->second);
242 std::list<shared_ptr<RibEntry>>
243 Rib::findDescendantsForNonInsertedName(
const Name& prefix)
const
245 std::list<shared_ptr<RibEntry>> children;
247 for (
const auto& [name, ribEntry] : m_rib) {
248 if (prefix.isPrefixOf(name)) {
249 children.push_back(ribEntry);
256 Rib::RibTable::iterator
257 Rib::eraseEntry(RibTable::iterator it)
260 if (it == m_rib.end()) {
264 shared_ptr<RibEntry> entry(it->second);
265 shared_ptr<RibEntry> parent = entry->getParent();
268 if (parent !=
nullptr) {
269 parent->removeChild(entry);
272 for (
auto childIt = entry->getChildren().begin(); childIt != entry->getChildren().end(); ) {
273 shared_ptr<RibEntry> child = *childIt;
279 entry->removeChild(child);
282 if (parent !=
nullptr) {
283 parent->addChild(child);
287 auto nextIt = m_rib.erase(it);
296 Rib::getAncestorRoutes(
const RibEntry& entry)
const
300 auto parent = entry.getParent();
301 while (parent !=
nullptr) {
302 for (
const auto& route : parent->getRoutes()) {
303 if (route.isChildInherit()) {
304 ancestorRoutes.insert(route);
308 if (parent->hasCapture()) {
312 parent = parent->getParent();
315 return ancestorRoutes;
319 Rib::getAncestorRoutes(
const Name& name)
const
324 while (parent !=
nullptr) {
325 for (
const auto& route : parent->getRoutes()) {
326 if (route.isChildInherit()) {
327 ancestorRoutes.insert(route);
331 if (parent->hasCapture()) {
335 parent = parent->getParent();
338 return ancestorRoutes;
346 BOOST_ASSERT(m_fibUpdater !=
nullptr);
347 addUpdateToQueue(update, onSuccess, onFailure);
348 sendBatchFromQueue();
354 auto range = m_faceEntries.equal_range(faceId);
355 for (
auto it = range.first; it != range.second; ++it) {
356 enqueueRemoveFace(*it->second, faceId);
358 sendBatchFromQueue();
364 for (
const auto& [faceId, ribEntry] : m_faceEntries) {
365 if (activeFaceIds.count(faceId) > 0) {
368 enqueueRemoveFace(*ribEntry, faceId);
370 sendBatchFromQueue();
374 Rib::enqueueRemoveFace(
const RibEntry& entry, uint64_t faceId)
376 for (
const Route& route : entry) {
377 if (route.
faceId != faceId) {
385 addUpdateToQueue(update,
nullptr,
nullptr);
390 Rib::addUpdateToQueue(
const RibUpdate& update,
394 RibUpdateBatch batch(update.getRoute().faceId);
397 UpdateQueueItem item{batch, onSuccess, onFailure};
398 m_updateBatches.push_back(std::move(item));
402 Rib::sendBatchFromQueue()
404 if (m_updateBatches.empty() || m_isUpdateInProgress) {
408 m_isUpdateInProgress =
true;
410 UpdateQueueItem item = std::move(m_updateBatches.front());
411 m_updateBatches.pop_front();
413 RibUpdateBatch& batch = item.batch;
416 BOOST_ASSERT(batch.size() == 1);
418 auto fibSuccessCb = std::bind(&Rib::onFibUpdateSuccess,
this, batch, _1, item.managerSuccessCallback);
419 auto fibFailureCb = std::bind(&Rib::onFibUpdateFailure,
this, item.managerFailureCallback, _1, _2);
424 Rib::onFibUpdateSuccess(
const RibUpdateBatch& batch,
428 for (
const RibUpdate& update : batch) {
429 switch (update.getAction()) {
431 insert(update.getName(), update.getRoute());
435 erase(update.getName(), update.getRoute());
441 modifyInheritedRoutes(inheritedRoutes);
443 m_isUpdateInProgress =
false;
445 if (onSuccess !=
nullptr) {
450 sendBatchFromQueue();
455 uint32_t code,
const std::string& error)
457 m_isUpdateInProgress =
false;
459 if (onFailure !=
nullptr) {
460 onFailure(code, error);
464 sendBatchFromQueue();
468 Rib::modifyInheritedRoutes(
const RibUpdateList& inheritedRoutes)
470 for (
const RibUpdate& update : inheritedRoutes) {
471 auto ribIt = m_rib.find(update.getName());
472 BOOST_ASSERT(ribIt != m_rib.end());
473 shared_ptr<RibEntry> entry(ribIt->second);
475 switch (update.getAction()) {
477 entry->addInheritedRoute(update.getRoute());
480 entry->removeInheritedRoute(update.getRoute());
491 for (
const auto& item : rib) {
492 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::list< RibUpdate > RibUpdateList
bool operator<(const ReadvertisedRoute &lhs, const ReadvertisedRoute &rhs)
static bool sortRoutes(const Route &lhs, const Route &rhs)
std::ostream & operator<<(std::ostream &os, const FibUpdate &update)
shared_ptr< RibEntry > entry
RibEntry::const_iterator route