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),
80 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
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);
112 entryIt = m_table.try_emplace(name, std::move(entry)).first;
120 if (hopsToAdd.
size() == 0) {
126 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
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, [
this] (
FibEntry& entry) { scheduleLoop(entry); });
160 for (
const auto& it : m_table) {
161 for (
const auto& hop : it.second.nexthopSet) {
162 unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
168 Fib::getNumberOfFacesForName(
const NexthopList& nextHopList)
170 uint32_t nNextHops =
static_cast<uint32_t
>(nextHopList.
getNextHops().size());
174 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
178 Fib::isNotNeighbor(
const ndn::Name& name)
185 uint64_t faceCost,
const ndn::time::milliseconds& timeout,
186 uint64_t flags, uint8_t times)
188 uint64_t faceId = m_adjacencyList.
getFaceId(faceUri);
191 ndn::nfd::ControlParameters faceParameters;
197 .setExpirationPeriod(timeout)
198 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
200 NLSR_LOG_DEBUG(
"Registering prefix: " << faceParameters.getName() <<
" faceUri: " << faceUri);
201 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
202 std::bind(&Fib::onRegistrationSuccess,
this, _1, faceUri),
203 std::bind(&Fib::onRegistrationFailure,
this, _1, faceParameters, faceUri, times));
206 NLSR_LOG_WARN(
"Error: No Face Id for face uri: " << faceUri);
211 Fib::onRegistrationSuccess(
const ndn::nfd::ControlParameters& param,
212 const ndn::FaceUri& faceUri)
214 NLSR_LOG_DEBUG(
"Successful in name registration: " << param.getName() <<
215 " Face Uri: " << faceUri <<
" faceId: " << param.getFaceId());
218 if (adjacent != m_adjacencyList.
end()) {
219 adjacent->setFaceId(param.getFaceId());
225 Fib::onRegistrationFailure(
const ndn::nfd::ControlResponse& response,
226 const ndn::nfd::ControlParameters& parameters,
227 const ndn::FaceUri& faceUri,
230 NLSR_LOG_DEBUG(
"Failed in name registration: " << response.getText() <<
231 " (code: " << response.getCode() <<
")");
232 NLSR_LOG_DEBUG(
"Prefix: " << parameters.getName() <<
" failed for: " << +times);
236 parameters.getCost(),
237 parameters.getExpirationPeriod(),
238 parameters.getFlags(), times+1);
246 Fib::unregisterPrefix(
const ndn::Name& namePrefix,
const ndn::FaceUri& faceUri)
250 if (adjacent != m_adjacencyList.
end()) {
251 faceId = adjacent->getFaceId();
254 NLSR_LOG_DEBUG(
"Unregister prefix: " << namePrefix <<
" Face Uri: " << faceUri);
256 ndn::nfd::ControlParameters controlParameters;
260 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
262 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
263 [] (
const ndn::nfd::ControlParameters& commandSuccessResult) {
264 NLSR_LOG_DEBUG(
"Unregister successful Prefix: " << commandSuccessResult.getName() <<
265 " Face Id: " << commandSuccessResult.getFaceId());
267 [] (
const ndn::nfd::ControlResponse& response) {
268 NLSR_LOG_DEBUG(
"Failed in unregistering name: " << response.getText() <<
269 " (code " << response.getCode() <<
")");
277 ndn::nfd::ControlParameters parameters;
280 .setStrategy(strategy);
282 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
283 std::bind(&Fib::onSetStrategySuccess,
this, _1),
284 std::bind(&Fib::onSetStrategyFailure,
this, _1, parameters, count));
288 Fib::onSetStrategySuccess(
const ndn::nfd::ControlParameters& commandSuccessResult)
290 NLSR_LOG_DEBUG(
"Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
291 " for name: " << commandSuccessResult.getName());
295 Fib::onSetStrategyFailure(
const ndn::nfd::ControlResponse&,
296 const ndn::nfd::ControlParameters& parameters,
299 NLSR_LOG_DEBUG(
"Failed to set strategy choice: " << parameters.getStrategy() <<
300 " for name: " << parameters.getName());
302 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
310 " Seq Num: " << entry.seqNo <<
311 " in " << m_refreshTime <<
" seconds");
313 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
314 std::bind(&Fib::refreshEntry,
this,
315 entry.name, refreshCallback));
319 Fib::scheduleLoop(FibEntry& entry)
321 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop,
this, _1));
327 auto it = m_table.find(name);
328 if (it == m_table.end()) {
332 FibEntry& entry = it->second;
333 NLSR_LOG_DEBUG(
"Refreshing " << entry.name <<
" Seq Num: " << entry.seqNo);
337 for (
const NextHop& hop : entry.nexthopSet) {
339 ndn::FaceUri(hop.getConnectingFaceUri()),
340 hop.getRouteCostAsAdjustedInteger(),
341 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
342 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
351 NLSR_LOG_DEBUG(
"-------------------FIB-----------------------------");
352 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 clean()
Remove all entries from the FIB.
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
void update(const ndn::Name &name, const NexthopList &allHops)
Set the nexthop list of a name.
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::KeyChain &keyChain)
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
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.
const std::set< NextHop, T > & getNextHops() const
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 &)> AfterRefreshCallback
NextHopsUriSortedSet nexthopSet