Loading...
Searching...
No Matches
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
33namespace nlsr {
34
35INIT_LOGGER(route.Fib);
36
37Fib::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
47void
48Fib::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
62void
63Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NextHopsUriSortedSet& hopsToAdd, uint64_t routeFlags)
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 routeFlags, 0);
81 }
82 }
83}
84
85void
86Fib::update(const ndn::Name& name, const NexthopList& allHops, uint64_t routeFlags)
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, routeFlags);
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, routeFlags);
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, routeFlags,
153 [this] (FibEntry& entry, uint64_t routeFlags) { scheduleLoop(entry, routeFlags); });
154 }
155}
156
157unsigned int
158Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
159{
160 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
161 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
162
163 // 0 == all faces
164 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
165}
166
167bool
168Fib::isNotNeighbor(const ndn::Name& name)
169{
170 return !m_adjacencyList.isNeighbor(name);
171}
172
173void
174Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
175 uint64_t faceCost, const ndn::time::milliseconds& timeout,
176 uint64_t flags, uint8_t times)
177{
178 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
179
180 if (faceId > 0) {
181 ndn::nfd::ControlParameters faceParameters;
182 faceParameters
183 .setName(namePrefix)
184 .setFaceId(faceId)
185 .setFlags(flags)
186 .setCost(faceCost)
187 .setExpirationPeriod(timeout)
188 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
189
190 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
191 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
192 std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
193 std::bind(&Fib::onRegistrationFailure, this, _1, faceParameters, faceUri, times));
194 }
195 else {
196 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
197 }
198}
199
200void
201Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
202 const ndn::FaceUri& faceUri)
203{
204 NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
205 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
206
207 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
208 if (adjacent != m_adjacencyList.end()) {
209 adjacent->setFaceId(param.getFaceId());
210 }
211 onPrefixRegistrationSuccess(param.getName());
212}
213
214void
215Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
216 const ndn::nfd::ControlParameters& parameters,
217 const ndn::FaceUri& faceUri,
218 uint8_t times)
219{
220 NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
221 " (code: " << response.getCode() << ")");
222 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
223 if (times < 3) {
224 NLSR_LOG_DEBUG("Trying to register again...");
225 registerPrefix(parameters.getName(), faceUri,
226 parameters.getCost(),
227 parameters.getExpirationPeriod(),
228 parameters.getFlags(), times+1);
229 }
230 else {
231 NLSR_LOG_DEBUG("Registration trial given up");
232 }
233}
234
235void
236Fib::unregisterPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri)
237{
238 uint64_t faceId = 0;
239 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
240 if (adjacent != m_adjacencyList.end()) {
241 faceId = adjacent->getFaceId();
242 }
243
244 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
245 if (faceId > 0) {
246 ndn::nfd::ControlParameters controlParameters;
247 controlParameters
248 .setName(namePrefix)
249 .setFaceId(faceId)
250 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
251
252 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
253 [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
254 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
255 " Face Id: " << commandSuccessResult.getFaceId());
256 },
257 [] (const ndn::nfd::ControlResponse& response) {
258 NLSR_LOG_DEBUG("Failed in unregistering name: " << response.getText() <<
259 " (code " << response.getCode() << ")");
260 });
261 }
262}
263
264void
265Fib::setStrategy(const ndn::Name& name, const ndn::Name& strategy, uint32_t count)
266{
267 ndn::nfd::ControlParameters parameters;
268 parameters
269 .setName(name)
270 .setStrategy(strategy);
271
272 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
273 std::bind(&Fib::onSetStrategySuccess, this, _1),
274 std::bind(&Fib::onSetStrategyFailure, this, _1, parameters, count));
275}
276
277void
278Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
279{
280 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
281 " for name: " << commandSuccessResult.getName());
282}
283
284void
285Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse&,
286 const ndn::nfd::ControlParameters& parameters,
287 uint32_t count)
288{
289 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
290 " for name: " << parameters.getName());
291 if (count < 3) {
292 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
293 }
294}
295
296void
297Fib::scheduleEntryRefresh(FibEntry& entry, uint64_t routeFlags, const AfterRefreshCallback& refreshCallback)
298{
299 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.name <<
300 " Seq Num: " << entry.seqNo <<
301 " in " << m_refreshTime << " seconds");
302
303 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
304 std::bind(&Fib::refreshEntry, this,
305 entry.name, routeFlags, refreshCallback));
306}
307
308void
309Fib::scheduleLoop(FibEntry& entry, uint64_t routeFlags)
310{
311 scheduleEntryRefresh(entry, routeFlags, std::bind(&Fib::scheduleLoop, this, _1, _2));
312}
313
314void
315Fib::refreshEntry(const ndn::Name& name, uint64_t routeFlags, AfterRefreshCallback refreshCb)
316{
317 auto it = m_table.find(name);
318 if (it == m_table.end()) {
319 return;
320 }
321 FibEntry& entry = it->second;
322 NLSR_LOG_DEBUG("Refreshing " << entry.name << " Seq Num: " << entry.seqNo);
323
324 entry.seqNo += 1;
325
326 for (const NextHop& hop : entry.nexthopSet) {
327 registerPrefix(entry.name,
328 ndn::FaceUri(hop.getConnectingFaceUri()),
329 hop.getRouteCostAsAdjustedInteger(),
330 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
331 routeFlags, 0);
332 }
333
334 refreshCb(entry, routeFlags);
335}
336
337void
339{
340 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
341 for (const auto& entry : m_table) {
342 NLSR_LOG_DEBUG("Name prefix: " << entry.first);
343 NLSR_LOG_DEBUG("Seq No: " << entry.second.seqNo);
344 NLSR_LOG_DEBUG("Nexthop List: \n" << entry.second.nexthopSet);
345 }
346}
347
348} // 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:265
void writeLog()
Definition fib.cpp:338
void remove(const ndn::Name &name)
Completely remove a name prefix from the FIB.
Definition fib.cpp:48
Fib(ndn::Face &face, ndn::Scheduler &scheduler, AdjacencyList &adjacencyList, ConfParameter &conf, ndn::security::KeyChain &keyChain)
Definition fib.cpp:37
void update(const ndn::Name &name, const NexthopList &allHops, uint64_t routeFlags)
Set the nexthop list of a name.
Definition fib.cpp:86
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:174
ndn::signal::Signal< Fib, ndn::Name > onPrefixRegistrationSuccess
Definition fib.hpp:215
const std::set< NextHop, T > & getNextHops() const
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
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 &, uint64_t)> 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