38 return std::tie(lhs.
entry->getName(), lhs.
route->faceId, lhs.
route->origin) <
51 m_fibUpdater = updater;
57 return m_rib.find(prefix);
63 auto ribIt = m_rib.find(prefix);
66 if (ribIt != m_rib.end()) {
67 shared_ptr<RibEntry> entry = ribIt->second;
68 auto routeIt = entry->findRoute(route);
69 if (routeIt != entry->end()) {
80 Route* existingRoute =
find(prefix, route);
81 if (existingRoute ==
nullptr) {
84 existingRoute =
find(parent->getName(), route);
94 auto ribIt = m_rib.find(prefix);
97 if (ribIt != m_rib.end()) {
98 shared_ptr<RibEntry> entry(ribIt->second);
101 bool didInsert =
false;
102 std::tie(entryIt, didInsert) = entry->insertRoute(route);
111 m_faceEntries.emplace(route.
faceId, entry);
116 if (entryIt->getExpirationEvent()) {
117 NFD_LOG_TRACE(
"Cancelling expiration event for " << entry->getName() <<
" " << *entryIt);
118 entryIt->cancelExpirationEvent();
126 auto entry = make_shared<RibEntry>();
128 m_rib[prefix] = entry;
131 entry->setName(prefix);
132 auto routeIt = entry->insertRoute(route).first;
135 shared_ptr<RibEntry> parent =
findParent(prefix);
138 if (parent !=
nullptr) {
139 parent->addChild(entry);
144 for (
const auto& child : children) {
145 if (child->getParent() == parent) {
147 if (parent !=
nullptr) {
148 parent->removeChild(child);
151 entry->addChild(child);
156 m_faceEntries.emplace(route.
faceId, entry);
165 Rib::erase(
const Name& prefix,
const Route& route)
167 auto ribIt = m_rib.find(prefix);
168 if (ribIt == m_rib.end()) {
173 shared_ptr<RibEntry> entry = ribIt->second;
174 auto routeIt = entry->findRoute(route);
176 if (routeIt != entry->end()) {
179 auto faceId = route.
faceId;
180 entry->eraseRoute(routeIt);
184 if (!entry->hasFaceId(faceId)) {
185 auto range = m_faceEntries.equal_range(faceId);
186 for (
auto it = range.first; it != range.second; ++it) {
187 if (it->second == entry) {
188 m_faceEntries.erase(it);
195 if (entry->getRoutes().empty()) {
217 for (
int i = prefix.
size() - 1; i >= 0; i--) {
218 auto it = m_rib.find(prefix.
getPrefix(i));
219 if (it != m_rib.end()) {
227 std::list<shared_ptr<RibEntry>>
228 Rib::findDescendants(
const Name& prefix)
const
230 std::list<shared_ptr<RibEntry>> children;
232 RibTable::const_iterator it = m_rib.find(prefix);
233 if (it != m_rib.end()) {
235 for (; it != m_rib.end(); ++it) {
237 children.push_back((it->second));
248 std::list<shared_ptr<RibEntry>>
249 Rib::findDescendantsForNonInsertedName(
const Name& prefix)
const
251 std::list<shared_ptr<RibEntry>> children;
253 for (
const auto& pair : m_rib) {
254 if (prefix.isPrefixOf(pair.first)) {
255 children.push_back(pair.second);
262 Rib::RibTable::iterator
263 Rib::eraseEntry(RibTable::iterator it)
266 if (it == m_rib.end()) {
270 shared_ptr<RibEntry> entry(it->second);
272 shared_ptr<RibEntry> parent = entry->getParent();
275 if (parent !=
nullptr) {
276 parent->removeChild(entry);
279 for (
auto childIt = entry->getChildren().begin(); childIt != entry->getChildren().end(); ) {
280 shared_ptr<RibEntry> child = *childIt;
286 entry->removeChild(child);
289 if (parent !=
nullptr) {
290 parent->addChild(child);
294 auto nextIt = m_rib.erase(it);
303 Rib::getAncestorRoutes(
const RibEntry& entry)
const
307 shared_ptr<RibEntry> parent = entry.getParent();
309 while (parent !=
nullptr) {
310 for (
const Route& route : parent->getRoutes()) {
312 ancestorRoutes.insert(route);
316 if (parent->hasCapture()) {
320 parent = parent->getParent();
323 return ancestorRoutes;
327 Rib::getAncestorRoutes(
const Name&
name)
const
333 while (parent !=
nullptr) {
334 for (
const Route& route : parent->getRoutes()) {
336 ancestorRoutes.insert(route);
340 if (parent->hasCapture()) {
344 parent = parent->getParent();
347 return ancestorRoutes;
355 BOOST_ASSERT(m_fibUpdater !=
nullptr);
357 addUpdateToQueue(update, onSuccess, onFailure);
359 sendBatchFromQueue();
365 auto range = m_faceEntries.equal_range(faceId);
366 for (
auto it = range.first; it != range.second; ++it) {
367 enqueueRemoveFace(*it->second, faceId);
369 sendBatchFromQueue();
375 for (
auto it = m_faceEntries.begin(); it != m_faceEntries.end(); ++it) {
376 if (activeFaceIds.count(it->first) > 0) {
379 enqueueRemoveFace(*it->second, it->first);
381 sendBatchFromQueue();
385 Rib::enqueueRemoveFace(
const RibEntry& entry, uint64_t faceId)
387 for (
const Route& route : entry) {
388 if (route.
faceId != faceId) {
396 addUpdateToQueue(update,
nullptr,
nullptr);
401 Rib::addUpdateToQueue(
const RibUpdate& update,
405 RibUpdateBatch batch(update.getRoute().faceId);
408 UpdateQueueItem item{batch, onSuccess, onFailure};
409 m_updateBatches.push_back(
std::move(item));
413 Rib::sendBatchFromQueue()
415 if (m_updateBatches.empty() || m_isUpdateInProgress) {
419 m_isUpdateInProgress =
true;
421 UpdateQueueItem item =
std::move(m_updateBatches.front());
422 m_updateBatches.pop_front();
424 RibUpdateBatch& batch = item.batch;
427 BOOST_ASSERT(batch.size() == 1);
429 auto fibSuccessCb = bind(&Rib::onFibUpdateSuccess,
this, batch, _1, item.managerSuccessCallback);
430 auto fibFailureCb = bind(&Rib::onFibUpdateFailure,
this, item.managerFailureCallback, _1, _2);
436 Rib::onFibUpdateSuccess(
const RibUpdateBatch& batch,
440 for (
const RibUpdate& update : batch) {
441 switch (update.getAction()) {
443 insert(update.getName(), update.getRoute());
447 erase(update.getName(), update.getRoute());
453 modifyInheritedRoutes(inheritedRoutes);
455 m_isUpdateInProgress =
false;
457 if (onSuccess !=
nullptr) {
462 sendBatchFromQueue();
467 uint32_t code,
const std::string& error)
469 m_isUpdateInProgress =
false;
471 if (onFailure !=
nullptr) {
472 onFailure(code, error);
476 sendBatchFromQueue();
480 Rib::modifyInheritedRoutes(
const RibUpdateList& inheritedRoutes)
482 for (
const RibUpdate& update : inheritedRoutes) {
483 auto ribIt = m_rib.find(update.getName());
484 BOOST_ASSERT(ribIt != m_rib.end());
485 shared_ptr<RibEntry> entry(ribIt->second);
487 switch (update.getAction()) {
489 entry->addInheritedRoute(update.getRoute());
492 entry->removeInheritedRoute(update.getRoute());
503 for (
const auto& item : rib) {
504 os << *item.second <<
"\n";