fib.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, The University of Memphis,
4  * Regents of the University of California
5  *
6  * This file is part of NLSR (Named-data Link State Routing).
7  * See AUTHORS.md for complete list of NLSR authors and contributors.
8  *
9  * NLSR is free software: you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  *
13  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19  **/
20 
21 #include "fib.hpp"
22 #include "adjacency-list.hpp"
23 #include "conf-parameter.hpp"
24 #include "logger.hpp"
25 #include "nexthop-list.hpp"
26 
27 #include <map>
28 #include <cmath>
29 #include <algorithm>
30 #include <iterator>
31 
32 namespace nlsr {
33 
34 INIT_LOGGER(route.Fib);
35 
36 const uint64_t Fib::GRACE_PERIOD = 10;
37 const std::string Fib::MULTICAST_STRATEGY("ndn:/localhost/nfd/strategy/multicast");
38 const std::string Fib::BEST_ROUTE_V2_STRATEGY("ndn:/localhost/nfd/strategy/best-route");
39 
40 Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
41  ConfParameter& conf, ndn::security::v2::KeyChain& keyChain)
42  : m_scheduler(scheduler)
43  , m_refreshTime(2 * conf.getLsaRefreshTime())
44  , m_controller(face, keyChain)
45  , m_adjacencyList(adjacencyList)
46  , m_confParameter(conf)
47 {
48 }
49 
50 void
51 Fib::remove(const ndn::Name& name)
52 {
53  NLSR_LOG_DEBUG("Fib::remove called");
54  auto it = m_table.find(name);
55 
56  // Only unregister the prefix if it ISN'T a neighbor.
57  if (it != m_table.end() && isNotNeighbor((it->second).getName())) {
58  for (const auto& nexthop : (it->second).getNexthopList().getNextHops()) {
59  unregisterPrefix((it->second).getName(), nexthop.getConnectingFaceUri());
60  }
61  cancelEntryRefresh(it->second);
62  m_table.erase(it);
63  }
64 }
65 
66 void
67 Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd)
68 {
69  const ndn::Name& name = entry.getName();
70 
71  bool shouldRegister = isNotNeighbor(name);
72 
73  for (const auto& hop : hopsToAdd.getNextHops())
74  {
75  // Add nexthop to FIB entry
76  entry.getNexthopList().addNextHop(hop);
77 
78  if (shouldRegister) {
79  // Add nexthop to NDN-FIB
80  registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
81  hop.getRouteCostAsAdjustedInteger(),
82  ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
83  ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
84  }
85  }
86 }
87 
88 void
89 Fib::update(const ndn::Name& name, const NexthopList& allHops)
90 {
91  NLSR_LOG_DEBUG("Fib::update called");
92 
93  // Get the max possible faces which is the minimum of the configuration setting and
94  // the length of the list of all next hops.
95  unsigned int maxFaces = getNumberOfFacesForName(allHops);
96 
97  NexthopList hopsToAdd;
98  unsigned int nFaces = 0;
99 
100  // Create a list of next hops to be installed with length == maxFaces
101  for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
102  hopsToAdd.addNextHop(*it);
103  }
104 
105  auto entryIt = m_table.find(name);
106 
107  // New FIB entry that has nextHops
108  if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
109  NLSR_LOG_DEBUG("New FIB Entry");
110 
111  FibEntry entry(name);
112 
113  addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
114 
115  m_table.emplace(name, entry);
116 
117  entryIt = m_table.find(name);
118  }
119  // Existing FIB entry that may or may not have nextHops
120  else {
121  // Existing FIB entry
122  NLSR_LOG_DEBUG("Existing FIB Entry");
123 
124  // Remove empty FIB entry
125  if (hopsToAdd.size() == 0) {
126  remove(name);
127  return;
128  }
129 
130  FibEntry& entry = (entryIt->second);
131  addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
132 
133  std::set<NextHop, NextHopComparator> hopsToRemove;
134  std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
135  hopsToAdd.begin(), hopsToAdd.end(),
136  std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
137 
138  bool isUpdatable = isNotNeighbor(entry.getName());
139  // Remove the uninstalled next hops from NFD and FIB entry
140  for (const auto& hop : hopsToRemove){
141  if (isUpdatable) {
142  unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
143  }
144  NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
145  entry.getNexthopList().removeNextHop(hop);
146  }
147 
148  // Increment sequence number
149  entry.setSeqNo(entry.getSeqNo() + 1);
150 
151  entryIt = m_table.find(name);
152 
153  }
154  if (entryIt != m_table.end() &&
155  !entryIt->second.getRefreshEventId() &&
156  isNotNeighbor(entryIt->second.getName())) {
157  scheduleEntryRefresh(entryIt->second,
158  [this] (FibEntry& entry) {
159  scheduleLoop(entry);
160  });
161  }
162 }
163 
164 void
166 {
167  NLSR_LOG_DEBUG("Fib::clean called");
168  // can't use const ref here as getNexthopList can't be marked const
169  for (auto&& it : m_table) {
170  NLSR_LOG_DEBUG("Canceling Scheduled event. Name: " << it.second.getName());
171  cancelEntryRefresh(it.second);
172 
173  for (const auto& hop : it.second.getNexthopList().getNextHops()) {
174  unregisterPrefix(it.second.getName(), hop.getConnectingFaceUri());
175  }
176  }
177 }
178 
179 unsigned int
180 Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
181 {
182  uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
183  uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
184 
185  // Allow all faces
186  if (nMaxFaces == 0) {
187  return nNextHops;
188  }
189  else {
190  return std::min(nNextHops, nMaxFaces);
191  }
192 }
193 
194 bool
195 Fib::isNotNeighbor(const ndn::Name& name)
196 {
197  return !m_adjacencyList.isNeighbor(name);
198 }
199 
200 void
201 Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
202  uint64_t faceCost,
203  const ndn::time::milliseconds& timeout,
204  uint64_t flags, uint8_t times)
205 {
206  uint64_t faceId = m_adjacencyList.getFaceId(ndn::FaceUri(faceUri));
207 
208  if (faceId != 0) {
209  ndn::nfd::ControlParameters faceParameters;
210  faceParameters
211  .setName(namePrefix)
212  .setFaceId(faceId)
213  .setFlags(flags)
214  .setCost(faceCost)
215  .setExpirationPeriod(timeout)
216  .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
217 
218  NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
219  m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
220  std::bind(&Fib::onRegistrationSuccess, this, _1,
221  "Successful in name registration",
222  faceUri),
223  std::bind(&Fib::onRegistrationFailure, this, _1,
224  "Failed in name registration",
225  faceParameters,
226  faceUri, times));
227  }
228  else {
229  NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
230  }
231 }
232 
233 void
234 Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
235  const std::string& message, const ndn::FaceUri& faceUri)
236 {
237  NLSR_LOG_DEBUG(message << ": " << commandSuccessResult.getName() <<
238  " Face Uri: " << faceUri << " faceId: " << commandSuccessResult.getFaceId());
239 
240  auto adjacent = m_adjacencyList.findAdjacent(faceUri);
241  if (adjacent != m_adjacencyList.end()) {
242  adjacent->setFaceId(commandSuccessResult.getFaceId());
243  }
244 
245  // Update the fast-access FaceMap with the new Face ID, too
246  m_faceMap.update(faceUri.toString(), commandSuccessResult.getFaceId());
247  m_faceMap.writeLog();
248 }
249 
250 void
251 Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
252  const std::string& message,
253  const ndn::nfd::ControlParameters& parameters,
254  const ndn::FaceUri& faceUri,
255  uint8_t times)
256 {
257  NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
258  NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
259  if (times < 3) {
260  NLSR_LOG_DEBUG("Trying to register again...");
261  registerPrefix(parameters.getName(), faceUri,
262  parameters.getCost(),
263  parameters.getExpirationPeriod(),
264  parameters.getFlags(), times+1);
265  }
266  else {
267  NLSR_LOG_DEBUG("Registration trial given up");
268  }
269 }
270 
271 void
272 Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
273 {
274  uint32_t faceId = m_faceMap.getFaceId(faceUri);
275  NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
276  if (faceId > 0) {
277  ndn::nfd::ControlParameters controlParameters;
278  controlParameters
279  .setName(namePrefix)
280  .setFaceId(faceId)
281  .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
282  m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
283  std::bind(&Fib::onUnregistrationSuccess, this, _1,
284  "Successful in unregistering name"),
285  std::bind(&Fib::onUnregistrationFailure,
286  this, _1,
287  "Failed in unregistering name"));
288  }
289 }
290 
291 void
292 Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
293  const std::string& message)
294 {
295  NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
296  " Face Id: " << commandSuccessResult.getFaceId());
297 }
298 
299 void
300 Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
301  const std::string& message)
302 {
303  NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
304 }
305 
306 void
307 Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
308 {
309  ndn::nfd::ControlParameters parameters;
310  parameters
311  .setName(name)
312  .setStrategy(strategy);
313 
314  m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
315  std::bind(&Fib::onSetStrategySuccess, this, _1),
316  std::bind(&Fib::onSetStrategyFailure, this, _1,
317  parameters, count));
318 }
319 
320 void
321 Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
322 {
323  NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
324  " for name: " << commandSuccessResult.getName());
325 }
326 
327 void
328 Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
329  const ndn::nfd::ControlParameters& parameters,
330  uint32_t count)
331 {
332  NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
333  " for name: " << parameters.getName());
334  if (count < 3) {
335  setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
336  }
337 }
338 
339 void
340 Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
341 {
342  NLSR_LOG_DEBUG("Scheduling refresh for " << entry.getName() <<
343  " Seq Num: " << entry.getSeqNo() <<
344  " in " << m_refreshTime << " seconds");
345 
346  entry.setRefreshEventId(m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
347  std::bind(&Fib::refreshEntry, this,
348  entry.getName(), refreshCallback)));
349 }
350 
351 void
352 Fib::scheduleLoop(FibEntry& entry)
353 {
354  scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
355 }
356 
357 void
358 Fib::cancelEntryRefresh(const FibEntry& entry)
359 {
360  NLSR_LOG_DEBUG("Canceling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
361  entry.getRefreshEventId().cancel();
362 }
363 
364 void
365 Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
366 {
367  auto it = m_table.find(name);
368  if (it == m_table.end()) {
369  return;
370  }
371 
372  FibEntry& entry = it->second;
373  NLSR_LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
374 
375  // Increment sequence number
376  entry.setSeqNo(entry.getSeqNo() + 1);
377 
378  for (const NextHop& hop : entry) {
379  registerPrefix(entry.getName(),
380  ndn::FaceUri(hop.getConnectingFaceUri()),
381  hop.getRouteCostAsAdjustedInteger(),
382  ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
383  ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
384  }
385 
386  refreshCb(entry);
387 }
388 
389 void
391 {
392  NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
393  for (const auto& entry : m_table) {
394  entry.second.writeLog();
395  }
396 }
397 
398 } // namespace nlsr
void writeLog()
Definition: fib.cpp:390
void setRefreshEventId(ndn::scheduler::EventId id)
Definition: fib-entry.hpp:53
#define NLSR_LOG_WARN(x)
Definition: logger.hpp:40
A class to house all the configuration parameters for NLSR.
const std::set< NextHop, NextHopComparator > & getNextHops() const
void update(const ndn::Name &name, const NexthopList &allHops)
Set the nexthop list of a name.
Definition: fib.cpp:89
static const std::string MULTICAST_STRATEGY
Definition: fib.hpp:241
size_t size() const
const_iterator cend() const
bool isNeighbor(const ndn::Name &adjName) const
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
void setStrategy(const ndn::Name &name, const std::string &strategy, uint32_t count)
Definition: fib.cpp:307
static const std::string BEST_ROUTE_V2_STRATEGY
Definition: fib.hpp:242
const_iterator cbegin() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
uint32_t getMaxFacesPerPrefix() const
NexthopList & getNexthopList()
Definition: fib-entry.hpp:47
void setSeqNo(int32_t fsn)
Definition: fib-entry.hpp:65
ndn::scheduler::EventId getRefreshEventId() const
Definition: fib-entry.hpp:59
void clean()
Remove all entries from the FIB.
Definition: fib.cpp:165
int32_t getSeqNo() const
Definition: fib-entry.hpp:71
void writeLog()
Definition: face-map.cpp:35
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::v2::KeyChain &keyChain)
Definition: fib.cpp:40
void writeLog() const
Definition: fib-entry.cpp:29
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
const ndn::Name & getName() const
Definition: fib-entry.hpp:41
AdjacencyList::iterator findAdjacent(const ndn::Name &adjName)
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
Definition: fib.cpp:51
uint64_t getFaceId(const ndn::FaceUri &faceUri)
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
void update(const std::string &faceUri, uint32_t faceId)
Definition: face-map.hpp:89
std::function< void(FibEntry &)> afterRefreshCallback
Definition: fib.hpp:34
const_iterator end() const
uint32_t getFaceId(const std::string &faceUri)
Definition: face-map.hpp:102
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.
Definition: fib.cpp:201