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