blob: d47d880d934fd1d15924943bca8fc206b70708e1 [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/*
Junxiao Shi6593a432023-08-21 10:50:28 +00003 * Copyright (c) 2014-2023, 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
laqinfancd7ca052017-06-02 04:57:18 -050027#include <algorithm>
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -040028#include <cmath>
29#include <map>
akmhoquec8a10f72014-04-25 18:42:55 -050030
akmhoque53353462014-04-22 08:43:45 -050031namespace nlsr {
32
dmcoomescf8d0ed2017-02-21 11:39:01 -060033INIT_LOGGER(route.Fib);
akmhoque674b0b12014-05-20 14:33:28 -050034
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060035Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040036 ConfParameter& conf, ndn::security::KeyChain& keyChain)
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060037 : m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060038 , m_refreshTime(2 * conf.getLsaRefreshTime())
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060039 , m_controller(face, keyChain)
40 , m_adjacencyList(adjacencyList)
41 , m_confParameter(conf)
42{
43}
44
akmhoque53353462014-04-22 08:43:45 -050045void
akmhoque31d1d4b2014-05-05 22:08:14 -050046Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050047{
dmcoomes5bcb39e2017-10-31 15:07:55 -050048 NLSR_LOG_DEBUG("Fib::remove called");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040049 auto it = m_table.find(name);
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060050
51 // Only unregister the prefix if it ISN'T a neighbor.
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070052 if (it != m_table.end() && isNotNeighbor((it->second).name)) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070053 for (const auto& nexthop : (it->second).nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070054 unregisterPrefix((it->second).name, nexthop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -050055 }
akmhoque53353462014-04-22 08:43:45 -050056 m_table.erase(it);
57 }
58}
59
Vince Lehman18841082014-08-19 17:15:24 -050060void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070061Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NextHopsUriSortedSet& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050062{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070063 const ndn::Name& name = entry.name;
Vince Lehman18841082014-08-19 17:15:24 -050064
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060065 bool shouldRegister = isNotNeighbor(name);
66
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070067 for (const auto& hop : hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050068 {
69 // Add nexthop to FIB entry
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070070 NLSR_LOG_DEBUG("Adding " << hop.getConnectingFaceUri() << " to " << entry.name);
71 entry.nexthopSet.addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050072
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060073 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050074 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060075 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
76 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050077 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
78 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
79 }
80 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050081}
82
83void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050084Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050085{
dmcoomes5bcb39e2017-10-31 15:07:55 -050086 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050087
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060088 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050089 // the length of the list of all next hops.
90 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050091
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070092 NextHopsUriSortedSet hopsToAdd;
Vince Lehman942eb7b2014-10-02 10:09:27 -050093 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050094
Vince Lehman942eb7b2014-10-02 10:09:27 -050095 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040096 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 hopsToAdd.addNextHop(*it);
98 }
Vince Lehman18841082014-08-19 17:15:24 -050099
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400100 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500101
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500102 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500103 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500104 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500105
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700106 FibEntry entry;
107 entry.name = name;
Vince Lehman942eb7b2014-10-02 10:09:27 -0500108 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500109
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400110 entryIt = m_table.try_emplace(name, std::move(entry)).first;
akmhoque53353462014-04-22 08:43:45 -0500111 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500112 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500113 else {
Vince Lehman18841082014-08-19 17:15:24 -0500114 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500115 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500116
117 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500118 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500119 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500120 return;
akmhoque53353462014-04-22 08:43:45 -0500121 }
Vince Lehman18841082014-08-19 17:15:24 -0500122
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400123 FibEntry& entry = entryIt->second;
Vince Lehman942eb7b2014-10-02 10:09:27 -0500124 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500125
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700126 std::set<NextHop, NextHopUriSortedComparator> hopsToRemove;
127 std::set_difference(entry.nexthopSet.begin(), entry.nexthopSet.end(),
laqinfancd7ca052017-06-02 04:57:18 -0500128 hopsToAdd.begin(), hopsToAdd.end(),
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700129 std::inserter(hopsToRemove, hopsToRemove.begin()),
130 NextHopUriSortedComparator());
laqinfancd7ca052017-06-02 04:57:18 -0500131
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700132 bool isUpdatable = isNotNeighbor(entry.name);
laqinfancd7ca052017-06-02 04:57:18 -0500133 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600134 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500135 if (isUpdatable) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700136 unregisterPrefix(entry.name, hop.getConnectingFaceUri());
laqinfancd7ca052017-06-02 04:57:18 -0500137 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700138 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.name);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700139 entry.nexthopSet.removeNextHop(hop);
laqinfancd7ca052017-06-02 04:57:18 -0500140 }
Vince Lehman18841082014-08-19 17:15:24 -0500141
Vince Lehman18841082014-08-19 17:15:24 -0500142 // Increment sequence number
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700143 entry.seqNo += 1;
Nick Gordon5867b522017-09-06 17:41:37 -0500144 entryIt = m_table.find(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500145 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700146
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600147 if (entryIt != m_table.end() &&
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700148 !entryIt->second.refreshEventId &&
149 isNotNeighbor(entryIt->second.name)) {
150 scheduleEntryRefresh(entryIt->second, [this] (FibEntry& entry) { scheduleLoop(entry); });
akmhoque53353462014-04-22 08:43:45 -0500151 }
152}
153
akmhoque53353462014-04-22 08:43:45 -0500154void
akmhoque31d1d4b2014-05-05 22:08:14 -0500155Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500156{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700157 NLSR_LOG_DEBUG("Clean called");
158 for (const auto& it : m_table) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700159 for (const auto& hop : it.second.nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700160 unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500161 }
162 }
akmhoque53353462014-04-22 08:43:45 -0500163}
164
Vince Lehman942eb7b2014-10-02 10:09:27 -0500165unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500166Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500167{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500168 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
169 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
170
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700171 // 0 == all faces
172 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500173}
174
akmhoque393d4ff2014-07-16 14:27:03 -0500175bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600176Fib::isNotNeighbor(const ndn::Name& name)
177{
178 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500179}
180
akmhoque53353462014-04-22 08:43:45 -0500181void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500182Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande41878572019-09-29 00:16:02 -0500183 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500184 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500185{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500186 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500187
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500188 if (faceId > 0) {
akmhoque060d3022014-08-12 13:35:06 -0500189 ndn::nfd::ControlParameters faceParameters;
190 faceParameters
191 .setName(namePrefix)
192 .setFaceId(faceId)
193 .setFlags(flags)
194 .setCost(faceCost)
195 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500196 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500197
dmcoomes5bcb39e2017-10-31 15:07:55 -0500198 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500199 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500200 std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
201 std::bind(&Fib::onRegistrationFailure, this, _1,
202 faceParameters, faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500203 }
204 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500205 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500206 }
207}
208
209void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500210Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
211 const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500212{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500213 NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
214 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500215
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400216 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500217 if (adjacent != m_adjacencyList.end()) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500218 adjacent->setFaceId(param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500219 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500220 onPrefixRegistrationSuccess(param.getName());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500221}
222
223void
224Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500225 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500226 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500227 uint8_t times)
228{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500229 NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
230 " (code: " << response.getCode() << ")");
231 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500232 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500233 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500234 registerPrefix(parameters.getName(), faceUri,
235 parameters.getCost(),
236 parameters.getExpirationPeriod(),
237 parameters.getFlags(), times+1);
238 }
239 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500240 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500241 }
akmhoquefdbddb12014-05-02 18:35:19 -0500242}
akmhoque31d1d4b2014-05-05 22:08:14 -0500243
akmhoquefdbddb12014-05-02 18:35:19 -0500244void
Junxiao Shi6593a432023-08-21 10:50:28 +0000245Fib::unregisterPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500246{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500247 uint64_t faceId = 0;
Junxiao Shi6593a432023-08-21 10:50:28 +0000248 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500249 if (adjacent != m_adjacencyList.end()) {
250 faceId = adjacent->getFaceId();
251 }
252
dmcoomes5bcb39e2017-10-31 15:07:55 -0500253 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500254 if (faceId > 0) {
255 ndn::nfd::ControlParameters controlParameters;
256 controlParameters
257 .setName(namePrefix)
258 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500259 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500260
akmhoque157b0a42014-05-13 00:26:37 -0500261 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500262 [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
263 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
264 " Face Id: " << commandSuccessResult.getFaceId());
265 },
266 [] (const ndn::nfd::ControlResponse& response) {
267 NLSR_LOG_DEBUG("Failed in unregistering name" << ": " << response.getText() <<
268 " (code: " << response.getCode() << ")");
269 });
akmhoque157b0a42014-05-13 00:26:37 -0500270 }
akmhoquefdbddb12014-05-02 18:35:19 -0500271}
272
273void
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400274Fib::setStrategy(const ndn::Name& name, const ndn::Name& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500275{
276 ndn::nfd::ControlParameters parameters;
277 parameters
278 .setName(name)
279 .setStrategy(strategy);
280
281 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400282 std::bind(&Fib::onSetStrategySuccess, this, _1),
dmcoomes9f936662017-03-02 10:33:09 -0600283 std::bind(&Fib::onSetStrategyFailure, this, _1,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400284 parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500285}
286
287void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400288Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500289{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400290 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600291 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500292}
293
294void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000295Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
296 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400297 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500298{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400299 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600300 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500301 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600302 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500303 }
304}
305
306void
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400307Fib::scheduleEntryRefresh(FibEntry& entry, const AfterRefreshCallback& refreshCallback)
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500308{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700309 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.name <<
310 " Seq Num: " << entry.seqNo <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600311 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500312
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700313 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
314 std::bind(&Fib::refreshEntry, this,
315 entry.name, refreshCallback));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500316}
317
318void
319Fib::scheduleLoop(FibEntry& entry)
320{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600321 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500322}
323
324void
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -0400325Fib::refreshEntry(const ndn::Name& name, AfterRefreshCallback refreshCb)
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500326{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400327 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500328 if (it == m_table.end()) {
329 return;
330 }
331
332 FibEntry& entry = it->second;
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700333 NLSR_LOG_DEBUG("Refreshing " << entry.name << " Seq Num: " << entry.seqNo);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500334
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700335 entry.seqNo += 1;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500336
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700337 for (const NextHop& hop : entry.nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700338 registerPrefix(entry.name,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500339 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500340 hop.getRouteCostAsAdjustedInteger(),
341 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
342 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500343 }
344
345 refreshCb(entry);
346}
347
348void
akmhoque674b0b12014-05-20 14:33:28 -0500349Fib::writeLog()
350{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500351 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600352 for (const auto& entry : m_table) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700353 NLSR_LOG_DEBUG("Name prefix: " << entry.first);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700354 NLSR_LOG_DEBUG("Seq No: " << entry.second.seqNo);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700355 NLSR_LOG_DEBUG("Nexthop List: \n" << entry.second.nexthopSet);
akmhoque674b0b12014-05-20 14:33:28 -0500356 }
357}
akmhoquefdbddb12014-05-02 18:35:19 -0500358
Nick Gordonfad8e252016-08-11 14:21:38 -0500359} // namespace nlsr