Skip to content

Commit

Permalink
rib: turn RibUpdate into an aggregate
Browse files Browse the repository at this point in the history
And move it together with RibUpdateList and RibUpdateBatch.
Also remove a few unused functions from RibEntry.

Change-Id: Id4f79fda27d3bffb8411e2a95c24154e2cb80c4f
  • Loading branch information
Pesa committed Jan 11, 2025
1 parent 21e24f9 commit c2442be
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 454 deletions.
15 changes: 3 additions & 12 deletions daemon/mgmt/rib-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

namespace nfd {

using rib::RibUpdate;
using rib::Route;

NFD_LOG_INIT(RibManager);
Expand Down Expand Up @@ -156,11 +155,7 @@ RibManager::beginAddRoute(const Name& name, Route route, std::optional<time::nan
NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
}

RibUpdate update;
update.setAction(RibUpdate::REGISTER)
.setName(name)
.setRoute(route);
beginRibUpdate(update, done);
beginRibUpdate({rib::RibUpdate::REGISTER, name, route}, done);
}

void
Expand All @@ -170,15 +165,11 @@ RibManager::beginRemoveRoute(const Name& name, const Route& route,
NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
" origin=" << route.origin);

RibUpdate update;
update.setAction(RibUpdate::UNREGISTER)
.setName(name)
.setRoute(route);
beginRibUpdate(update, done);
beginRibUpdate({rib::RibUpdate::UNREGISTER, name, route}, done);
}

void
RibManager::beginRibUpdate(const RibUpdate& update,
RibManager::beginRibUpdate(const rib::RibUpdate& update,
const std::function<void(RibUpdateResult)>& done)
{
m_rib.beginApplyUpdate(update,
Expand Down
4 changes: 2 additions & 2 deletions daemon/mgmt/rib-manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#define NFD_DAEMON_MGMT_RIB_MANAGER_HPP

#include "manager-base.hpp"
#include "rib/route.hpp"

#include <ndn-cxx/mgmt/nfd/controller.hpp>
#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Expand All @@ -39,8 +38,9 @@
namespace nfd {

namespace rib {
class Route;
class Rib;
class RibUpdate;
struct RibUpdate;
} // namespace rib

/**
Expand Down
67 changes: 24 additions & 43 deletions daemon/rib/fib-updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ FibUpdater::computeUpdates(const RibUpdateBatch& batch)

// Compute updates and add to m_fibUpdates
for (const RibUpdate& update : batch) {
switch (update.getAction()) {
switch (update.action) {
case RibUpdate::REGISTER:
computeUpdatesForRegistration(update);
break;
Expand All @@ -88,42 +88,38 @@ FibUpdater::computeUpdates(const RibUpdateBatch& batch)
void
FibUpdater::computeUpdatesForRegistration(const RibUpdate& update)
{
const Name& prefix = update.getName();
const Route& route = update.getRoute();

auto it = m_rib.find(prefix);
auto it = m_rib.find(update.name);

// Name prefix exists
if (it != m_rib.end()) {
shared_ptr<const RibEntry> entry(it->second);

auto existingRoute = entry->findRoute(route);
auto existingRoute = entry->findRoute(update.route);

// Route will be new
if (existingRoute == entry->end()) {
// Will the new route change the namespace's capture flag?
bool willCaptureBeTurnedOn = (entry->hasCapture() == false && route.isRibCapture());
bool willCaptureBeTurnedOn = (!entry->hasCapture() && update.route.isRibCapture());

createFibUpdatesForNewRoute(*entry, route, willCaptureBeTurnedOn);
createFibUpdatesForNewRoute(*entry, update.route, willCaptureBeTurnedOn);
}
else {
// Route already exists
RibEntry entryCopy = *entry;

Route& routeToUpdate = *entryCopy.findRoute(route);
routeToUpdate.flags = route.flags;
routeToUpdate.cost = route.cost;
routeToUpdate.expires = route.expires;
Route& routeToUpdate = *entryCopy.findRoute(update.route);
routeToUpdate.flags = update.route.flags;
routeToUpdate.cost = update.route.cost;
routeToUpdate.expires = update.route.expires;

createFibUpdatesForUpdatedRoute(entryCopy, route, *existingRoute);
createFibUpdatesForUpdatedRoute(entryCopy, update.route, *existingRoute);
}
}
else {
// New name in RIB
// Find prefix's parent
shared_ptr<RibEntry> parent = m_rib.findParent(prefix);
shared_ptr<RibEntry> parent = m_rib.findParent(update.name);

Rib::RibEntryList descendants = m_rib.findDescendantsForNonInsertedName(prefix);
Rib::RibEntryList descendants = m_rib.findDescendantsForNonInsertedName(update.name);
Rib::RibEntryList children;

for (const auto& descendant : descendants) {
Expand All @@ -134,44 +130,40 @@ FibUpdater::computeUpdatesForRegistration(const RibUpdate& update)
}
}

createFibUpdatesForNewRibEntry(prefix, route, children);
createFibUpdatesForNewRibEntry(update.name, update.route, children);
}
}

void
FibUpdater::computeUpdatesForUnregistration(const RibUpdate& update)
{
const Name& prefix = update.getName();
const Route& route = update.getRoute();

auto ribIt = m_rib.find(prefix);
auto ribIt = m_rib.find(update.name);

// Name prefix exists
if (ribIt != m_rib.end()) {
shared_ptr<const RibEntry> entry(ribIt->second);
const bool hadCapture = entry->hasCapture();

auto existing = entry->findRoute(route);
auto existing = entry->findRoute(update.route);
if (existing != entry->end()) {
RibEntry temp = *entry;

// Erase route in temp entry
temp.eraseRoute(route);

const bool captureWasTurnedOff = (hadCapture && !temp.hasCapture());
temp.eraseRoute(update.route);

bool captureWasTurnedOff = (hadCapture && !temp.hasCapture());
createFibUpdatesForErasedRoute(temp, *existing, captureWasTurnedOff);

// The RibEntry still has the face ID; need to update FIB
// with lowest cost for the same face instead of removing the face from the FIB
const Route* next = entry->getRouteWithSecondLowestCostByFaceId(route.faceId);
const Route* next = entry->getRouteWithSecondLowestCostByFaceId(update.route.faceId);

if (next != nullptr) {
createFibUpdatesForNewRoute(temp, *next, false);
}

// The RibEntry will be empty after this removal
if (entry->getNRoutes() == 1) {
if (entry->getRoutes().size() == 1) {
createFibUpdatesForErasedRibEntry(*entry);
}
}
Expand Down Expand Up @@ -562,7 +554,7 @@ FibUpdater::createFibUpdatesForErasedRoute(const RibEntry& entry, const Route& r
// If capture is turned off for the route and another route is installed in the RibEntry,
// add ancestors to self
Rib::RouteSet routesToAdd;
if (captureWasTurnedOff && entry.getNRoutes() != 0) {
if (captureWasTurnedOff && !entry.empty()) {
// Look for an ancestors that were blocked previously
routesToAdd = m_rib.getAncestorRoutes(entry);

Expand All @@ -589,7 +581,7 @@ FibUpdater::createFibUpdatesForErasedRoute(const RibEntry& entry, const Route& r
// If capture is turned off for the route and another route is installed in the RibEntry,
// add ancestors to self
Rib::RouteSet routesToAdd;
if (captureWasTurnedOff && entry.getNRoutes() != 0) {
if (captureWasTurnedOff && !entry.empty()) {
// Look for an ancestors that were blocked previously
routesToAdd = m_rib.getAncestorRoutes(entry);

Expand All @@ -604,11 +596,10 @@ FibUpdater::createFibUpdatesForErasedRoute(const RibEntry& entry, const Route& r
Rib::RouteSet ancestorRoutes = m_rib.getAncestorRoutes(entry);

// If the current entry has capture set or is pending removal, don't add inherited route
if (!entry.hasCapture() && entry.getNRoutes() != 0) {
if (!entry.hasCapture() && !entry.empty()) {
// If there is an ancestor route which is the same as the erased route, add that route
// to the current entry
auto it = ancestorRoutes.find(route);

if (it != ancestorRoutes.end()) {
addInheritedRoute(entry.getName(), *it);
addFibUpdate(FibUpdate::createAddUpdate(entry.getName(), it->faceId, it->cost));
Expand Down Expand Up @@ -684,23 +675,13 @@ FibUpdater::traverseSubTree(const RibEntry& entry, Rib::Rib::RouteSet routesToAd
void
FibUpdater::addInheritedRoute(const Name& name, const Route& route)
{
RibUpdate update;
update.setAction(RibUpdate::REGISTER)
.setName(name)
.setRoute(route);

m_inheritedRoutes.push_back(update);
m_inheritedRoutes.push_back({RibUpdate::REGISTER, name, route});
}

void
FibUpdater::removeInheritedRoute(const Name& name, const Route& route)
{
RibUpdate update;
update.setAction(RibUpdate::UNREGISTER)
.setName(name)
.setRoute(route);

m_inheritedRoutes.push_back(update);
m_inheritedRoutes.push_back({RibUpdate::UNREGISTER, name, route});
}

} // namespace nfd::rib
31 changes: 5 additions & 26 deletions daemon/rib/rib-entry.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2022, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -72,43 +72,28 @@ RibEntry::insertRoute(const Route& route)
void
RibEntry::eraseRoute(const Route& route)
{
auto it = findRoute(route);
eraseRoute(it);
}

bool
RibEntry::hasRoute(const Route& route)
{
auto it = findRoute(route);
return it != end();
eraseRoute(findRoute(route));
}

bool
RibEntry::hasFaceId(uint64_t faceId) const
{
auto it = std::find_if(begin(), end(), [faceId] (const auto& r) { return r.faceId == faceId; });
return it != end();
}

size_t
RibEntry::getNRoutes() const
{
return m_routes.size();
return std::find_if(begin(), end(), [=] (const auto& r) { return r.faceId == faceId; }) != end();
}

void
RibEntry::addChild(shared_ptr<RibEntry> child)
{
BOOST_ASSERT(!child->getParent());
child->setParent(this->shared_from_this());
child->m_parent = shared_from_this();
m_children.push_back(std::move(child));
}

void
RibEntry::removeChild(shared_ptr<RibEntry> child)
{
BOOST_ASSERT(child->getParent().get() == this);
child->setParent(nullptr);
child->m_parent = nullptr;
m_children.remove(child);
}

Expand Down Expand Up @@ -155,12 +140,6 @@ RibEntry::hasInheritedRoute(const Route& route) const
return findInheritedRoute(route) != m_inheritedRoutes.end();
}

bool
RibEntry::hasCapture() const
{
return m_nRoutesWithCaptureSet > 0;
}

bool
RibEntry::hasChildInheritOnFaceId(uint64_t faceId) const
{
Expand Down
Loading

0 comments on commit c2442be

Please sign in to comment.