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); });
157 Fib::getNumberOfFacesForName(
const NexthopList& nextHopList)
159 uint32_t nNextHops =
static_cast<uint32_t
>(nextHopList.
getNextHops().size());
163 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
167 Fib::isNotNeighbor(
const ndn::Name& name)
174 uint64_t faceCost,
const ndn::time::milliseconds& timeout,
175 uint64_t flags, uint8_t times)
177 uint64_t faceId = m_adjacencyList.
getFaceId(faceUri);
180 ndn::nfd::ControlParameters faceParameters;
186 .setExpirationPeriod(timeout)
187 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
189 NLSR_LOG_DEBUG(
"Registering prefix: " << faceParameters.getName() <<
" faceUri: " << faceUri);
190 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
191 std::bind(&Fib::onRegistrationSuccess,
this, _1, faceUri),
192 std::bind(&Fib::onRegistrationFailure,
this, _1, faceParameters, faceUri, times));
195 NLSR_LOG_WARN(
"Error: No Face Id for face uri: " << faceUri);
200 Fib::onRegistrationSuccess(
const ndn::nfd::ControlParameters& param,
201 const ndn::FaceUri& faceUri)
203 NLSR_LOG_DEBUG(
"Successful in name registration: " << param.getName() <<
204 " Face Uri: " << faceUri <<
" faceId: " << param.getFaceId());
207 if (adjacent != m_adjacencyList.
end()) {
208 adjacent->setFaceId(param.getFaceId());
214 Fib::onRegistrationFailure(
const ndn::nfd::ControlResponse& response,
215 const ndn::nfd::ControlParameters& parameters,
216 const ndn::FaceUri& faceUri,
219 NLSR_LOG_DEBUG(
"Failed in name registration: " << response.getText() <<
220 " (code: " << response.getCode() <<
")");
221 NLSR_LOG_DEBUG(
"Prefix: " << parameters.getName() <<
" failed for: " << +times);
225 parameters.getCost(),
226 parameters.getExpirationPeriod(),
227 parameters.getFlags(), times+1);
235 Fib::unregisterPrefix(
const ndn::Name& namePrefix,
const ndn::FaceUri& faceUri)
239 if (adjacent != m_adjacencyList.
end()) {
240 faceId = adjacent->getFaceId();
243 NLSR_LOG_DEBUG(
"Unregister prefix: " << namePrefix <<
" Face Uri: " << faceUri);
245 ndn::nfd::ControlParameters controlParameters;
249 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
251 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
252 [] (
const ndn::nfd::ControlParameters& commandSuccessResult) {
253 NLSR_LOG_DEBUG(
"Unregister successful Prefix: " << commandSuccessResult.getName() <<
254 " Face Id: " << commandSuccessResult.getFaceId());
256 [] (
const ndn::nfd::ControlResponse& response) {
257 NLSR_LOG_DEBUG(
"Failed in unregistering name: " << response.getText() <<
258 " (code " << response.getCode() <<
")");
266 ndn::nfd::ControlParameters parameters;
269 .setStrategy(strategy);
271 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
272 std::bind(&Fib::onSetStrategySuccess,
this, _1),
273 std::bind(&Fib::onSetStrategyFailure,
this, _1, parameters, count));
277 Fib::onSetStrategySuccess(
const ndn::nfd::ControlParameters& commandSuccessResult)
279 NLSR_LOG_DEBUG(
"Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
280 " for name: " << commandSuccessResult.getName());
284 Fib::onSetStrategyFailure(
const ndn::nfd::ControlResponse&,
285 const ndn::nfd::ControlParameters& parameters,
288 NLSR_LOG_DEBUG(
"Failed to set strategy choice: " << parameters.getStrategy() <<
289 " for name: " << parameters.getName());
291 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
299 " Seq Num: " << entry.seqNo <<
300 " in " << m_refreshTime <<
" seconds");
302 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
303 std::bind(&Fib::refreshEntry,
this,
304 entry.name, refreshCallback));
308 Fib::scheduleLoop(FibEntry& entry)
310 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop,
this, _1));
316 auto it = m_table.find(name);
317 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),
331 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
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.
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