blob: 76affde9937e8e7214d01a8bcc19a802c00daacb [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoaf7a2112019-03-19 14:55:20 -04002/*
Davide Pesaventod2610dc2025-01-03 13:20:06 -05003 * Copyright (c) 2014-2025, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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/>.
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040019 */
Nick Gordonb7168472016-09-21 13:57:17 -050020
akmhoquefdbddb12014-05-02 18:35:19 -050021#include "fib.hpp"
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050022#include "adjacency-list.hpp"
23#include "conf-parameter.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050024#include "logger.hpp"
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050025#include "nexthop-list.hpp"
26
Davide Pesaventod2610dc2025-01-03 13:20:06 -050027#include <ndn-cxx/mgmt/nfd/control-command.hpp>
28
laqinfancd7ca052017-06-02 04:57:18 -050029#include <algorithm>
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040030#include <cmath>
31#include <map>
akmhoquec8a10f72014-04-25 18:42:55 -050032
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
dmcoomescf8d0ed2017-02-21 11:39:01 -060035INIT_LOGGER(route.Fib);
akmhoque674b0b12014-05-20 14:33:28 -050036
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060037Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040038 ConfParameter& conf, ndn::security::KeyChain& keyChain)
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060039 : m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060040 , m_refreshTime(2 * conf.getLsaRefreshTime())
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060041 , m_controller(face, keyChain)
42 , m_adjacencyList(adjacencyList)
43 , m_confParameter(conf)
44{
45}
46
akmhoque53353462014-04-22 08:43:45 -050047void
akmhoque31d1d4b2014-05-05 22:08:14 -050048Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050049{
dmcoomes5bcb39e2017-10-31 15:07:55 -050050 NLSR_LOG_DEBUG("Fib::remove called");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040051 auto it = m_table.find(name);
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060052
53 // Only unregister the prefix if it ISN'T a neighbor.
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070054 if (it != m_table.end() && isNotNeighbor((it->second).name)) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070055 for (const auto& nexthop : (it->second).nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070056 unregisterPrefix((it->second).name, nexthop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -050057 }
akmhoque53353462014-04-22 08:43:45 -050058 m_table.erase(it);
59 }
60}
61
Vince Lehman18841082014-08-19 17:15:24 -050062void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070063Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NextHopsUriSortedSet& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050064{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070065 const ndn::Name& name = entry.name;
Vince Lehman18841082014-08-19 17:15:24 -050066
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060067 bool shouldRegister = isNotNeighbor(name);
68
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070069 for (const auto& hop : hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050070 {
71 // Add nexthop to FIB entry
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070072 NLSR_LOG_DEBUG("Adding " << hop.getConnectingFaceUri() << " to " << entry.name);
73 entry.nexthopSet.addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050074
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060075 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050076 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060077 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
78 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050079 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
80 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
81 }
82 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050083}
84
85void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050086Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050087{
dmcoomes5bcb39e2017-10-31 15:07:55 -050088 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050089
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060090 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050091 // the length of the list of all next hops.
92 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050093
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070094 NextHopsUriSortedSet hopsToAdd;
Vince Lehman942eb7b2014-10-02 10:09:27 -050095 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050096
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040098 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -050099 hopsToAdd.addNextHop(*it);
100 }
Vince Lehman18841082014-08-19 17:15:24 -0500101
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400102 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500103
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500104 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500105 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500106 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500107
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700108 FibEntry entry;
109 entry.name = name;
Vince Lehman942eb7b2014-10-02 10:09:27 -0500110 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500111
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400112 entryIt = m_table.try_emplace(name, std::move(entry)).first;
akmhoque53353462014-04-22 08:43:45 -0500113 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500114 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500115 else {
Vince Lehman18841082014-08-19 17:15:24 -0500116 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500117 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500118
119 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500120 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500121 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500122 return;
akmhoque53353462014-04-22 08:43:45 -0500123 }
Vince Lehman18841082014-08-19 17:15:24 -0500124
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400125 FibEntry& entry = entryIt->second;
Vince Lehman942eb7b2014-10-02 10:09:27 -0500126 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500127
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700128 std::set<NextHop, NextHopUriSortedComparator> hopsToRemove;
129 std::set_difference(entry.nexthopSet.begin(), entry.nexthopSet.end(),
laqinfancd7ca052017-06-02 04:57:18 -0500130 hopsToAdd.begin(), hopsToAdd.end(),
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700131 std::inserter(hopsToRemove, hopsToRemove.begin()),
132 NextHopUriSortedComparator());
laqinfancd7ca052017-06-02 04:57:18 -0500133
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700134 bool isUpdatable = isNotNeighbor(entry.name);
laqinfancd7ca052017-06-02 04:57:18 -0500135 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600136 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500137 if (isUpdatable) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700138 unregisterPrefix(entry.name, hop.getConnectingFaceUri());
laqinfancd7ca052017-06-02 04:57:18 -0500139 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700140 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.name);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700141 entry.nexthopSet.removeNextHop(hop);
laqinfancd7ca052017-06-02 04:57:18 -0500142 }
Vince Lehman18841082014-08-19 17:15:24 -0500143
Vince Lehman18841082014-08-19 17:15:24 -0500144 // Increment sequence number
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700145 entry.seqNo += 1;
Nick Gordon5867b522017-09-06 17:41:37 -0500146 entryIt = m_table.find(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500147 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700148
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600149 if (entryIt != m_table.end() &&
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700150 !entryIt->second.refreshEventId &&
151 isNotNeighbor(entryIt->second.name)) {
152 scheduleEntryRefresh(entryIt->second, [this] (FibEntry& entry) { scheduleLoop(entry); });
akmhoque53353462014-04-22 08:43:45 -0500153 }
154}
155
Vince Lehman942eb7b2014-10-02 10:09:27 -0500156unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500157Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500158{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500159 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
160 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
161
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700162 // 0 == all faces
163 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500164}
165
akmhoque393d4ff2014-07-16 14:27:03 -0500166bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600167Fib::isNotNeighbor(const ndn::Name& name)
168{
169 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500170}
171
akmhoque53353462014-04-22 08:43:45 -0500172void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500173Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande41878572019-09-29 00:16:02 -0500174 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500175 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500176{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500177 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500178
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500179 if (faceId > 0) {
akmhoque060d3022014-08-12 13:35:06 -0500180 ndn::nfd::ControlParameters faceParameters;
181 faceParameters
182 .setName(namePrefix)
183 .setFaceId(faceId)
184 .setFlags(flags)
185 .setCost(faceCost)
186 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500187 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500188
dmcoomes5bcb39e2017-10-31 15:07:55 -0500189 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500190 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500191 std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
Davide Pesaventod2610dc2025-01-03 13:20:06 -0500192 std::bind(&Fib::onRegistrationFailure, this, _1, faceParameters, faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500193 }
194 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500195 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500196 }
197}
198
199void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500200Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
201 const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500202{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500203 NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
204 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500205
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400206 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500207 if (adjacent != m_adjacencyList.end()) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500208 adjacent->setFaceId(param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500209 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500210 onPrefixRegistrationSuccess(param.getName());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500211}
212
213void
214Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500215 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500216 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500217 uint8_t times)
218{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500219 NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
220 " (code: " << response.getCode() << ")");
221 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500222 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500223 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500224 registerPrefix(parameters.getName(), faceUri,
225 parameters.getCost(),
226 parameters.getExpirationPeriod(),
227 parameters.getFlags(), times+1);
228 }
229 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500230 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500231 }
akmhoquefdbddb12014-05-02 18:35:19 -0500232}
akmhoque31d1d4b2014-05-05 22:08:14 -0500233
akmhoquefdbddb12014-05-02 18:35:19 -0500234void
Junxiao Shi6593a432023-08-21 10:50:28 +0000235Fib::unregisterPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500236{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500237 uint64_t faceId = 0;
Junxiao Shi6593a432023-08-21 10:50:28 +0000238 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500239 if (adjacent != m_adjacencyList.end()) {
240 faceId = adjacent->getFaceId();
241 }
242
dmcoomes5bcb39e2017-10-31 15:07:55 -0500243 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500244 if (faceId > 0) {
245 ndn::nfd::ControlParameters controlParameters;
246 controlParameters
247 .setName(namePrefix)
248 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500249 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500250
akmhoque157b0a42014-05-13 00:26:37 -0500251 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500252 [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
253 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
254 " Face Id: " << commandSuccessResult.getFaceId());
255 },
256 [] (const ndn::nfd::ControlResponse& response) {
Davide Pesaventod2610dc2025-01-03 13:20:06 -0500257 NLSR_LOG_DEBUG("Failed in unregistering name: " << response.getText() <<
258 " (code " << response.getCode() << ")");
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500259 });
akmhoque157b0a42014-05-13 00:26:37 -0500260 }
akmhoquefdbddb12014-05-02 18:35:19 -0500261}
262
263void
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400264Fib::setStrategy(const ndn::Name& name, const ndn::Name& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500265{
266 ndn::nfd::ControlParameters parameters;
267 parameters
268 .setName(name)
269 .setStrategy(strategy);
270
271 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventod2610dc2025-01-03 13:20:06 -0500272 std::bind(&Fib::onSetStrategySuccess, this, _1),
273 std::bind(&Fib::onSetStrategyFailure, this, _1, parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500274}
275
276void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400277Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500278{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400279 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600280 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500281}
282
283void
Davide Pesaventod2610dc2025-01-03 13:20:06 -0500284Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse&,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000285 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400286 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500287{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400288 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600289 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500290 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600291 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500292 }
293}
294
295void
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400296Fib::scheduleEntryRefresh(FibEntry& entry, const AfterRefreshCallback& refreshCallback)
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500297{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700298 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.name <<
299 " Seq Num: " << entry.seqNo <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600300 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500301
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700302 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
303 std::bind(&Fib::refreshEntry, this,
304 entry.name, refreshCallback));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500305}
306
307void
308Fib::scheduleLoop(FibEntry& entry)
309{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600310 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500311}
312
313void
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400314Fib::refreshEntry(const ndn::Name& name, AfterRefreshCallback refreshCb)
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500315{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400316 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500317 if (it == m_table.end()) {
318 return;
319 }
320
321 FibEntry& entry = it->second;
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700322 NLSR_LOG_DEBUG("Refreshing " << entry.name << " Seq Num: " << entry.seqNo);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500323
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700324 entry.seqNo += 1;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500325
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700326 for (const NextHop& hop : entry.nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700327 registerPrefix(entry.name,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500328 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500329 hop.getRouteCostAsAdjustedInteger(),
330 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
331 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500332 }
333
334 refreshCb(entry);
335}
336
337void
akmhoque674b0b12014-05-20 14:33:28 -0500338Fib::writeLog()
339{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500340 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600341 for (const auto& entry : m_table) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700342 NLSR_LOG_DEBUG("Name prefix: " << entry.first);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700343 NLSR_LOG_DEBUG("Seq No: " << entry.second.seqNo);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700344 NLSR_LOG_DEBUG("Nexthop List: \n" << entry.second.nexthopSet);
akmhoque674b0b12014-05-20 14:33:28 -0500345 }
346}
akmhoquefdbddb12014-05-02 18:35:19 -0500347
Nick Gordonfad8e252016-08-11 14:21:38 -0500348} // namespace nlsr