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-2020, 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 std::string Fib::MULTICAST_STRATEGY("ndn:/localhost/nfd/strategy/multicast");
37 const std::string Fib::BEST_ROUTE_V2_STRATEGY("ndn:/localhost/nfd/strategy/best-route");
38 
39 Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
40  ConfParameter& conf, ndn::security::KeyChain& keyChain)
41  : m_scheduler(scheduler)
42  , m_refreshTime(2 * conf.getLsaRefreshTime())
43  , m_controller(face, keyChain)
44  , m_adjacencyList(adjacencyList)
45  , m_confParameter(conf)
46 {
47 }
48 
49 void
50 Fib::remove(const ndn::Name& name)
51 {
52  NLSR_LOG_DEBUG("Fib::remove called");
53  auto it = m_table.find(name);
54 
55  // Only unregister the prefix if it ISN'T a neighbor.
56  if (it != m_table.end() && isNotNeighbor((it->second).name)) {
57  for (const auto& nexthop : (it->second).nexthopList.getNextHops()) {
58  unregisterPrefix((it->second).name, nexthop.getConnectingFaceUri());
59  }
60  m_table.erase(it);
61  }
62 }
63 
64 void
65 Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd)
66 {
67  const ndn::Name& name = entry.name;
68 
69  bool shouldRegister = isNotNeighbor(name);
70 
71  for (const auto& hop : hopsToAdd.getNextHops())
72  {
73  // Add nexthop to FIB entry
74  entry.nexthopList.addNextHop(hop);
75 
76  if (shouldRegister) {
77  // Add nexthop to NDN-FIB
78  registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
79  hop.getRouteCostAsAdjustedInteger(),
80  ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
81  ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
82  }
83  }
84 }
85 
86 void
87 Fib::update(const ndn::Name& name, const NexthopList& allHops)
88 {
89  NLSR_LOG_DEBUG("Fib::update called");
90 
91  // Get the max possible faces which is the minimum of the configuration setting and
92  // the length of the list of all next hops.
93  unsigned int maxFaces = getNumberOfFacesForName(allHops);
94 
95  NexthopList hopsToAdd;
96  unsigned int nFaces = 0;
97 
98  // Create a list of next hops to be installed with length == maxFaces
99  for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
100  hopsToAdd.addNextHop(*it);
101  }
102 
103  auto entryIt = m_table.find(name);
104 
105  // New FIB entry that has nextHops
106  if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
107  NLSR_LOG_DEBUG("New FIB Entry");
108 
109  FibEntry entry;
110  entry.name = name;
111 
112  addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
113 
114  m_table.emplace(name, std::move(entry));
115 
116  entryIt = m_table.find(name);
117  }
118  // Existing FIB entry that may or may not have nextHops
119  else {
120  // Existing FIB entry
121  NLSR_LOG_DEBUG("Existing FIB Entry");
122 
123  // Remove empty FIB entry
124  if (hopsToAdd.size() == 0) {
125  remove(name);
126  return;
127  }
128 
129  FibEntry& entry = (entryIt->second);
130  addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
131 
132  std::set<NextHop, NextHopComparator> hopsToRemove;
133  std::set_difference(entry.nexthopList.begin(), entry.nexthopList.end(),
134  hopsToAdd.begin(), hopsToAdd.end(),
135  std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
136 
137  bool isUpdatable = isNotNeighbor(entry.name);
138  // Remove the uninstalled next hops from NFD and FIB entry
139  for (const auto& hop : hopsToRemove){
140  if (isUpdatable) {
141  unregisterPrefix(entry.name, hop.getConnectingFaceUri());
142  }
143  NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.name);
144  entry.nexthopList.removeNextHop(hop);
145  }
146 
147  // Increment sequence number
148  entry.seqNo += 1;
149  entryIt = m_table.find(name);
150  }
151 
152  if (entryIt != m_table.end() &&
153  !entryIt->second.refreshEventId &&
154  isNotNeighbor(entryIt->second.name)) {
155  scheduleEntryRefresh(entryIt->second, [this] (FibEntry& entry) { scheduleLoop(entry); });
156  }
157 }
158 
159 void
161 {
162  NLSR_LOG_DEBUG("Clean called");
163  for (const auto& it : m_table) {
164  for (const auto& hop : it.second.nexthopList.getNextHops()) {
165  unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
166  }
167  }
168 }
169 
170 unsigned int
171 Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
172 {
173  uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
174  uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
175 
176  // 0 == all faces
177  return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
178 }
179 
180 bool
181 Fib::isNotNeighbor(const ndn::Name& name)
182 {
183  return !m_adjacencyList.isNeighbor(name);
184 }
185 
186 void
187 Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
188  uint64_t faceCost, const ndn::time::milliseconds& timeout,
189  uint64_t flags, uint8_t times)
190 {
191  uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
192 
193  if (faceId > 0) {
194  ndn::nfd::ControlParameters faceParameters;
195  faceParameters
196  .setName(namePrefix)
197  .setFaceId(faceId)
198  .setFlags(flags)
199  .setCost(faceCost)
200  .setExpirationPeriod(timeout)
201  .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
202 
203  NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
204  m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
205  std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
206  std::bind(&Fib::onRegistrationFailure, this, _1,
207  faceParameters, faceUri, times));
208  }
209  else {
210  NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
211  }
212 }
213 
214 void
215 Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
216  const ndn::FaceUri& faceUri)
217 {
218  NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
219  " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
220 
221  auto adjacent = m_adjacencyList.findAdjacent(faceUri);
222  if (adjacent != m_adjacencyList.end()) {
223  adjacent->setFaceId(param.getFaceId());
224  }
225  onPrefixRegistrationSuccess(param.getName());
226 }
227 
228 void
229 Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
230  const ndn::nfd::ControlParameters& parameters,
231  const ndn::FaceUri& faceUri,
232  uint8_t times)
233 {
234  NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
235  " (code: " << response.getCode() << ")");
236  NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
237  if (times < 3) {
238  NLSR_LOG_DEBUG("Trying to register again...");
239  registerPrefix(parameters.getName(), faceUri,
240  parameters.getCost(),
241  parameters.getExpirationPeriod(),
242  parameters.getFlags(), times+1);
243  }
244  else {
245  NLSR_LOG_DEBUG("Registration trial given up");
246  }
247 }
248 
249 void
250 Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
251 {
252  uint64_t faceId = 0;
253  auto adjacent = m_adjacencyList.findAdjacent(ndn::FaceUri(faceUri));
254  if (adjacent != m_adjacencyList.end()) {
255  faceId = adjacent->getFaceId();
256  }
257 
258  NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
259  if (faceId > 0) {
260  ndn::nfd::ControlParameters controlParameters;
261  controlParameters
262  .setName(namePrefix)
263  .setFaceId(faceId)
264  .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
265 
266  m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
267  [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
268  NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
269  " Face Id: " << commandSuccessResult.getFaceId());
270  },
271  [] (const ndn::nfd::ControlResponse& response) {
272  NLSR_LOG_DEBUG("Failed in unregistering name" << ": " << response.getText() <<
273  " (code: " << response.getCode() << ")");
274  });
275  }
276 }
277 
278 void
279 Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
280 {
281  ndn::nfd::ControlParameters parameters;
282  parameters
283  .setName(name)
284  .setStrategy(strategy);
285 
286  m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
287  std::bind(&Fib::onSetStrategySuccess, this, _1),
288  std::bind(&Fib::onSetStrategyFailure, this, _1,
289  parameters, count));
290 }
291 
292 void
293 Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
294 {
295  NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
296  " for name: " << commandSuccessResult.getName());
297 }
298 
299 void
300 Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
301  const ndn::nfd::ControlParameters& parameters,
302  uint32_t count)
303 {
304  NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
305  " for name: " << parameters.getName());
306  if (count < 3) {
307  setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
308  }
309 }
310 
311 void
312 Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
313 {
314  NLSR_LOG_DEBUG("Scheduling refresh for " << entry.name <<
315  " Seq Num: " << entry.seqNo <<
316  " in " << m_refreshTime << " seconds");
317 
318  entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
319  std::bind(&Fib::refreshEntry, this,
320  entry.name, refreshCallback));
321 }
322 
323 void
324 Fib::scheduleLoop(FibEntry& entry)
325 {
326  scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
327 }
328 
329 void
330 Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
331 {
332  auto it = m_table.find(name);
333  if (it == m_table.end()) {
334  return;
335  }
336 
337  FibEntry& entry = it->second;
338  NLSR_LOG_DEBUG("Refreshing " << entry.name << " Seq Num: " << entry.seqNo);
339 
340  entry.seqNo += 1;
341 
342  for (const NextHop& hop : entry.nexthopList) {
343  registerPrefix(entry.name,
344  ndn::FaceUri(hop.getConnectingFaceUri()),
345  hop.getRouteCostAsAdjustedInteger(),
346  ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
347  ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
348  }
349 
350  refreshCb(entry);
351 }
352 
353 void
355 {
356  NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
357  for (const auto& entry : m_table) {
358  NLSR_LOG_DEBUG("Name Prefix: " << entry.second.name);
359  NLSR_LOG_DEBUG("Seq No: " << entry.second.seqNo);
360  NLSR_LOG_DEBUG(entry.second.nexthopList);
361  }
362 }
363 
364 } // namespace nlsr
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 writeLog()
Definition: fib.cpp:354
static const std::string MULTICAST_STRATEGY
Definition: fib.hpp:219
void clean()
Remove all entries from the FIB.
Definition: fib.cpp:160
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
Definition: fib.cpp:50
void update(const ndn::Name &name, const NexthopList &allHops)
Set the nexthop list of a name.
Definition: fib.cpp:87
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::KeyChain &keyChain)
Definition: fib.cpp:39
static const std::string BEST_ROUTE_V2_STRATEGY
Definition: fib.hpp:220
void setStrategy(const ndn::Name &name, const std::string &strategy, uint32_t count)
Definition: fib.cpp:279
ndn::util::Signal< Fib, const ndn::Name & > onPrefixRegistrationSuccess
Definition: fib.hpp:221
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:187
size_t size() const
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
const_iterator cend() const
const_iterator cbegin() const
void removeNextHop(const NextHop &nh)
Remove a next hop from the Next Hop list.
const std::set< NextHop, NextHopComparator > & getNextHops() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_WARN(x)
Definition: logger.hpp:40
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...
std::function< void(FibEntry &)> afterRefreshCallback
Definition: fib.hpp:41
Definition: fib.hpp:34
ndn::Name name
Definition: fib.hpp:35
NexthopList nexthopList
Definition: fib.hpp:38
int32_t seqNo
Definition: fib.hpp:37