blob: 2d34a36fd3415fdc9fb6729d6d9413d6b0a69da5 [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/*
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -07003 * Copyright (c) 2014-2021, 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
27#include <map>
28#include <cmath>
laqinfancd7ca052017-06-02 04:57:18 -050029#include <algorithm>
30#include <iterator>
akmhoquec8a10f72014-04-25 18:42:55 -050031
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
dmcoomescf8d0ed2017-02-21 11:39:01 -060034INIT_LOGGER(route.Fib);
akmhoque674b0b12014-05-20 14:33:28 -050035
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050036const std::string Fib::MULTICAST_STRATEGY("ndn:/localhost/nfd/strategy/multicast");
37const std::string Fib::BEST_ROUTE_V2_STRATEGY("ndn:/localhost/nfd/strategy/best-route");
akmhoque393d4ff2014-07-16 14:27:03 -050038
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060039Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040040 ConfParameter& conf, ndn::security::KeyChain& keyChain)
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060041 : m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , m_refreshTime(2 * conf.getLsaRefreshTime())
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060043 , m_controller(face, keyChain)
44 , m_adjacencyList(adjacencyList)
45 , m_confParameter(conf)
46{
47}
48
akmhoque53353462014-04-22 08:43:45 -050049void
akmhoque31d1d4b2014-05-05 22:08:14 -050050Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050051{
dmcoomes5bcb39e2017-10-31 15:07:55 -050052 NLSR_LOG_DEBUG("Fib::remove called");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040053 auto it = m_table.find(name);
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060054
55 // Only unregister the prefix if it ISN'T a neighbor.
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070056 if (it != m_table.end() && isNotNeighbor((it->second).name)) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070057 for (const auto& nexthop : (it->second).nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070058 unregisterPrefix((it->second).name, nexthop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -050059 }
akmhoque53353462014-04-22 08:43:45 -050060 m_table.erase(it);
61 }
62}
63
Vince Lehman18841082014-08-19 17:15:24 -050064void
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070065Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NextHopsUriSortedSet& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050066{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070067 const ndn::Name& name = entry.name;
Vince Lehman18841082014-08-19 17:15:24 -050068
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060069 bool shouldRegister = isNotNeighbor(name);
70
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070071 for (const auto& hop : hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050072 {
73 // Add nexthop to FIB entry
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070074 NLSR_LOG_DEBUG("Adding " << hop.getConnectingFaceUri() << " to " << entry.name);
75 entry.nexthopSet.addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050076
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060077 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050078 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060079 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
80 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050081 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
82 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
83 }
84 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050085}
86
87void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050088Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050089{
dmcoomes5bcb39e2017-10-31 15:07:55 -050090 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050091
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060092 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050093 // the length of the list of all next hops.
94 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050095
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -070096 NextHopsUriSortedSet hopsToAdd;
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050098
Vince Lehman942eb7b2014-10-02 10:09:27 -050099 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400100 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500101 hopsToAdd.addNextHop(*it);
102 }
Vince Lehman18841082014-08-19 17:15:24 -0500103
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400104 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500105
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500106 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500107 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500108 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500109
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700110 FibEntry entry;
111 entry.name = name;
Vince Lehman18841082014-08-19 17:15:24 -0500112
Vince Lehman942eb7b2014-10-02 10:09:27 -0500113 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500114
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700115 m_table.emplace(name, std::move(entry));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500116
Nick Gordon5867b522017-09-06 17:41:37 -0500117 entryIt = m_table.find(name);
akmhoque53353462014-04-22 08:43:45 -0500118 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500119 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500120 else {
Vince Lehman18841082014-08-19 17:15:24 -0500121 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500122 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500123
124 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500125 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500127 return;
akmhoque53353462014-04-22 08:43:45 -0500128 }
Vince Lehman18841082014-08-19 17:15:24 -0500129
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500130 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500131 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500132
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700133 std::set<NextHop, NextHopUriSortedComparator> hopsToRemove;
134 std::set_difference(entry.nexthopSet.begin(), entry.nexthopSet.end(),
laqinfancd7ca052017-06-02 04:57:18 -0500135 hopsToAdd.begin(), hopsToAdd.end(),
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700136 std::inserter(hopsToRemove, hopsToRemove.begin()),
137 NextHopUriSortedComparator());
laqinfancd7ca052017-06-02 04:57:18 -0500138
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700139 bool isUpdatable = isNotNeighbor(entry.name);
laqinfancd7ca052017-06-02 04:57:18 -0500140 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600141 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500142 if (isUpdatable) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700143 unregisterPrefix(entry.name, hop.getConnectingFaceUri());
laqinfancd7ca052017-06-02 04:57:18 -0500144 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700145 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.name);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700146 entry.nexthopSet.removeNextHop(hop);
laqinfancd7ca052017-06-02 04:57:18 -0500147 }
Vince Lehman18841082014-08-19 17:15:24 -0500148
Vince Lehman18841082014-08-19 17:15:24 -0500149 // Increment sequence number
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700150 entry.seqNo += 1;
Nick Gordon5867b522017-09-06 17:41:37 -0500151 entryIt = m_table.find(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500152 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700153
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600154 if (entryIt != m_table.end() &&
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700155 !entryIt->second.refreshEventId &&
156 isNotNeighbor(entryIt->second.name)) {
157 scheduleEntryRefresh(entryIt->second, [this] (FibEntry& entry) { scheduleLoop(entry); });
akmhoque53353462014-04-22 08:43:45 -0500158 }
159}
160
akmhoque53353462014-04-22 08:43:45 -0500161void
akmhoque31d1d4b2014-05-05 22:08:14 -0500162Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500163{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700164 NLSR_LOG_DEBUG("Clean called");
165 for (const auto& it : m_table) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700166 for (const auto& hop : it.second.nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700167 unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500168 }
169 }
akmhoque53353462014-04-22 08:43:45 -0500170}
171
Vince Lehman942eb7b2014-10-02 10:09:27 -0500172unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500173Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500174{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500175 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
176 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
177
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700178 // 0 == all faces
179 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500180}
181
akmhoque393d4ff2014-07-16 14:27:03 -0500182bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600183Fib::isNotNeighbor(const ndn::Name& name)
184{
185 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500186}
187
akmhoque53353462014-04-22 08:43:45 -0500188void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500189Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande41878572019-09-29 00:16:02 -0500190 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500191 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500192{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500193 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500194
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500195 if (faceId > 0) {
akmhoque060d3022014-08-12 13:35:06 -0500196 ndn::nfd::ControlParameters faceParameters;
197 faceParameters
198 .setName(namePrefix)
199 .setFaceId(faceId)
200 .setFlags(flags)
201 .setCost(faceCost)
202 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500203 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500204
dmcoomes5bcb39e2017-10-31 15:07:55 -0500205 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500206 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500207 std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
208 std::bind(&Fib::onRegistrationFailure, this, _1,
209 faceParameters, faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500210 }
211 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500212 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500213 }
214}
215
216void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500217Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
218 const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500219{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500220 NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
221 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500222
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400223 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500224 if (adjacent != m_adjacencyList.end()) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500225 adjacent->setFaceId(param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500226 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500227 onPrefixRegistrationSuccess(param.getName());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500228}
229
230void
231Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500232 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500233 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500234 uint8_t times)
235{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500236 NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
237 " (code: " << response.getCode() << ")");
238 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500239 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500240 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500241 registerPrefix(parameters.getName(), faceUri,
242 parameters.getCost(),
243 parameters.getExpirationPeriod(),
244 parameters.getFlags(), times+1);
245 }
246 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500247 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500248 }
akmhoquefdbddb12014-05-02 18:35:19 -0500249}
akmhoque31d1d4b2014-05-05 22:08:14 -0500250
akmhoquefdbddb12014-05-02 18:35:19 -0500251void
akmhoque157b0a42014-05-13 00:26:37 -0500252Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500253{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500254 uint64_t faceId = 0;
255 auto adjacent = m_adjacencyList.findAdjacent(ndn::FaceUri(faceUri));
256 if (adjacent != m_adjacencyList.end()) {
257 faceId = adjacent->getFaceId();
258 }
259
dmcoomes5bcb39e2017-10-31 15:07:55 -0500260 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500261 if (faceId > 0) {
262 ndn::nfd::ControlParameters controlParameters;
263 controlParameters
264 .setName(namePrefix)
265 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500266 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500267
akmhoque157b0a42014-05-13 00:26:37 -0500268 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500269 [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
270 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
271 " Face Id: " << commandSuccessResult.getFaceId());
272 },
273 [] (const ndn::nfd::ControlResponse& response) {
274 NLSR_LOG_DEBUG("Failed in unregistering name" << ": " << response.getText() <<
275 " (code: " << response.getCode() << ")");
276 });
akmhoque157b0a42014-05-13 00:26:37 -0500277 }
akmhoquefdbddb12014-05-02 18:35:19 -0500278}
279
280void
akmhoque393d4ff2014-07-16 14:27:03 -0500281Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500282{
283 ndn::nfd::ControlParameters parameters;
284 parameters
285 .setName(name)
286 .setStrategy(strategy);
287
288 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400289 std::bind(&Fib::onSetStrategySuccess, this, _1),
dmcoomes9f936662017-03-02 10:33:09 -0600290 std::bind(&Fib::onSetStrategyFailure, this, _1,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400291 parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500292}
293
294void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400295Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500296{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400297 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600298 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500299}
300
301void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000302Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
303 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400304 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500305{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400306 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600307 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500308 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600309 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500310 }
311}
312
313void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500314Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
315{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700316 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.name <<
317 " Seq Num: " << entry.seqNo <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600318 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500319
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700320 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
321 std::bind(&Fib::refreshEntry, this,
322 entry.name, refreshCallback));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500323}
324
325void
326Fib::scheduleLoop(FibEntry& entry)
327{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600328 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500329}
330
331void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500332Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
333{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400334 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500335 if (it == m_table.end()) {
336 return;
337 }
338
339 FibEntry& entry = it->second;
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700340 NLSR_LOG_DEBUG("Refreshing " << entry.name << " Seq Num: " << entry.seqNo);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500341
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700342 entry.seqNo += 1;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500343
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700344 for (const NextHop& hop : entry.nexthopSet) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700345 registerPrefix(entry.name,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500346 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500347 hop.getRouteCostAsAdjustedInteger(),
348 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
349 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500350 }
351
352 refreshCb(entry);
353}
354
355void
akmhoque674b0b12014-05-20 14:33:28 -0500356Fib::writeLog()
357{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500358 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600359 for (const auto& entry : m_table) {
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700360 NLSR_LOG_DEBUG("Name prefix: " << entry.first);
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700361 NLSR_LOG_DEBUG("Seq No: " << entry.second.seqNo);
Ashlesh Gawande6f0f35d2021-08-21 23:52:14 -0700362 NLSR_LOG_DEBUG("Nexthop List: \n" << entry.second.nexthopSet);
akmhoque674b0b12014-05-20 14:33:28 -0500363 }
364}
akmhoquefdbddb12014-05-02 18:35:19 -0500365
Nick Gordonfad8e252016-08-11 14:21:38 -0500366} // namespace nlsr