27#include <ndn-cxx/mgmt/nfd/control-command.hpp>
39 : m_scheduler(scheduler)
40 , m_refreshTime(2 * conf.getLsaRefreshTime())
41 , m_controller(face, keyChain)
42 , m_adjacencyList(adjacencyList)
43 , m_confParameter(conf)
51 auto it = m_table.find(name);
54 if (it != m_table.end() && isNotNeighbor((it->second).name)) {
55 for (
const auto& nexthop : (it->second).nexthopSet) {
56 unregisterPrefix((it->second).name, nexthop.getConnectingFaceUri());
65 const ndn::Name& name = entry.
name;
67 bool shouldRegister = isNotNeighbor(name);
69 for (
const auto& hop : hopsToAdd)
78 hop.getRouteCostAsAdjustedInteger(),
79 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
92 unsigned int maxFaces = getNumberOfFacesForName(allHops);
95 unsigned int nFaces = 0;
98 for (
auto it = allHops.
cbegin(); it != allHops.
cend() && nFaces < maxFaces; ++it, ++nFaces) {
102 auto entryIt = m_table.find(name);
105 if (entryIt == m_table.end() && hopsToAdd.
size() != 0) {
110 addNextHopsToFibEntryAndNfd(entry, hopsToAdd, routeFlags);
112 entryIt = m_table.try_emplace(name, std::move(entry)).first;
120 if (hopsToAdd.
size() == 0) {
126 addNextHopsToFibEntryAndNfd(entry, hopsToAdd, routeFlags);
128 std::set<NextHop, NextHopUriSortedComparator> hopsToRemove;
131 std::inserter(hopsToRemove, hopsToRemove.begin()),
134 bool isUpdatable = isNotNeighbor(entry.
name);
136 for (
const auto& hop : hopsToRemove){
138 unregisterPrefix(entry.
name, hop.getConnectingFaceUri());
146 entryIt = m_table.find(name);
149 if (entryIt != m_table.end() &&
150 !entryIt->second.refreshEventId &&
151 isNotNeighbor(entryIt->second.name)) {
152 scheduleEntryRefresh(entryIt->second, routeFlags,
153 [
this] (
FibEntry& entry, uint64_t routeFlags) { scheduleLoop(entry, routeFlags); });
158Fib::getNumberOfFacesForName(
const NexthopList& nextHopList)
160 uint32_t nNextHops =
static_cast<uint32_t
>(nextHopList.
getNextHops().size());
164 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
168Fib::isNotNeighbor(
const ndn::Name& name)
175 uint64_t faceCost,
const ndn::time::milliseconds& timeout,
176 uint64_t flags, uint8_t times)
178 uint64_t faceId = m_adjacencyList.
getFaceId(faceUri);
181 ndn::nfd::ControlParameters faceParameters;
187 .setExpirationPeriod(timeout)
188 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
190 NLSR_LOG_DEBUG(
"Registering prefix: " << faceParameters.getName() <<
" faceUri: " << faceUri);
191 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
192 std::bind(&Fib::onRegistrationSuccess,
this, _1, faceUri),
193 std::bind(&Fib::onRegistrationFailure,
this, _1, faceParameters, faceUri, times));
196 NLSR_LOG_WARN(
"Error: No Face Id for face uri: " << faceUri);
201Fib::onRegistrationSuccess(
const ndn::nfd::ControlParameters& param,
202 const ndn::FaceUri& faceUri)
204 NLSR_LOG_DEBUG(
"Successful in name registration: " << param.getName() <<
205 " Face Uri: " << faceUri <<
" faceId: " << param.getFaceId());
208 if (adjacent != m_adjacencyList.
end()) {
209 adjacent->setFaceId(param.getFaceId());
215Fib::onRegistrationFailure(
const ndn::nfd::ControlResponse& response,
216 const ndn::nfd::ControlParameters& parameters,
217 const ndn::FaceUri& faceUri,
220 NLSR_LOG_DEBUG(
"Failed in name registration: " << response.getText() <<
221 " (code: " << response.getCode() <<
")");
222 NLSR_LOG_DEBUG(
"Prefix: " << parameters.getName() <<
" failed for: " << +times);
226 parameters.getCost(),
227 parameters.getExpirationPeriod(),
228 parameters.getFlags(), times+1);
236Fib::unregisterPrefix(
const ndn::Name& namePrefix,
const ndn::FaceUri& faceUri)
240 if (adjacent != m_adjacencyList.
end()) {
241 faceId = adjacent->getFaceId();
244 NLSR_LOG_DEBUG(
"Unregister prefix: " << namePrefix <<
" Face Uri: " << faceUri);
246 ndn::nfd::ControlParameters controlParameters;
250 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
252 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
253 [] (
const ndn::nfd::ControlParameters& commandSuccessResult) {
254 NLSR_LOG_DEBUG(
"Unregister successful Prefix: " << commandSuccessResult.getName() <<
255 " Face Id: " << commandSuccessResult.getFaceId());
257 [] (
const ndn::nfd::ControlResponse& response) {
258 NLSR_LOG_DEBUG(
"Failed in unregistering name: " << response.getText() <<
259 " (code " << response.getCode() <<
")");
267 ndn::nfd::ControlParameters parameters;
270 .setStrategy(strategy);
272 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
273 std::bind(&Fib::onSetStrategySuccess,
this, _1),
274 std::bind(&Fib::onSetStrategyFailure,
this, _1, parameters, count));
278Fib::onSetStrategySuccess(
const ndn::nfd::ControlParameters& commandSuccessResult)
280 NLSR_LOG_DEBUG(
"Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
281 " for name: " << commandSuccessResult.getName());
285Fib::onSetStrategyFailure(
const ndn::nfd::ControlResponse&,
286 const ndn::nfd::ControlParameters& parameters,
289 NLSR_LOG_DEBUG(
"Failed to set strategy choice: " << parameters.getStrategy() <<
290 " for name: " << parameters.getName());
292 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
297Fib::scheduleEntryRefresh(FibEntry& entry, uint64_t routeFlags,
const AfterRefreshCallback& refreshCallback)
300 " Seq Num: " << entry.seqNo <<
301 " in " << m_refreshTime <<
" seconds");
303 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
304 std::bind(&Fib::refreshEntry,
this,
305 entry.name, routeFlags, refreshCallback));
309Fib::scheduleLoop(FibEntry& entry, uint64_t routeFlags)
311 scheduleEntryRefresh(entry, routeFlags, std::bind(&Fib::scheduleLoop,
this, _1, _2));
317 auto it = m_table.find(name);
318 if (it == m_table.end()) {
321 FibEntry& entry = it->second;
322 NLSR_LOG_DEBUG(
"Refreshing " << entry.name <<
" Seq Num: " << entry.seqNo);
326 for (
const NextHop& hop : entry.nexthopSet) {
328 ndn::FaceUri(hop.getConnectingFaceUri()),
329 hop.getRouteCostAsAdjustedInteger(),
330 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
334 refreshCb(entry, routeFlags);
340 NLSR_LOG_DEBUG(
"-------------------FIB-----------------------------");
341 for (
const auto& entry : m_table) {
uint64_t getFaceId(const ndn::FaceUri &faceUri)
const_iterator end() const
bool isNeighbor(const ndn::Name &adjName) const
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
A class to house all the configuration parameters for NLSR.
uint32_t getMaxFacesPerPrefix() const
void setStrategy(const ndn::Name &name, const ndn::Name &strategy, uint32_t count)
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::KeyChain &keyChain)
void update(const ndn::Name &name, const NexthopList &allHops, uint64_t routeFlags)
Set the nexthop list of a name.
void registerPrefix(const ndn::Name &namePrefix, const ndn::FaceUri &faceUri, uint64_t faceCost, const ndn::time::milliseconds &timeout, uint64_t flags, uint8_t times)
Inform NFD of a next-hop.
ndn::signal::Signal< Fib, ndn::Name > onPrefixRegistrationSuccess
const std::set< NextHop, T > & getNextHops() const
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
const_iterator cend() const
const_iterator cbegin() const
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
#define INIT_LOGGER(name)
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::function< void(FibEntry &, uint64_t)> AfterRefreshCallback
NextHopsUriSortedSet nexthopSet