blob: 1004889e5e1046cab285e232f5e4828939bc41ca [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 Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, 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)) {
57 for (const auto& nexthop : (it->second).nexthopList.getNextHops()) {
58 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 Gawandee8d8bd52018-08-09 17:18:51 -050065Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& 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
71 for (const auto& hop : hopsToAdd.getNextHops())
Vince Lehman18841082014-08-19 17:15:24 -050072 {
73 // Add nexthop to FIB entry
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070074 entry.nexthopList.addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050075
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060076 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050077 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060078 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
79 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050080 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
81 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
82 }
83 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050084}
85
86void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050087Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050088{
dmcoomes5bcb39e2017-10-31 15:07:55 -050089 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050090
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060091 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050092 // the length of the list of all next hops.
93 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050094
Vince Lehman942eb7b2014-10-02 10:09:27 -050095 NexthopList hopsToAdd;
96 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050097
Vince Lehman942eb7b2014-10-02 10:09:27 -050098 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040099 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100 hopsToAdd.addNextHop(*it);
101 }
Vince Lehman18841082014-08-19 17:15:24 -0500102
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400103 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500104
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500105 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500106 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500107 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500108
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700109 FibEntry entry;
110 entry.name = name;
Vince Lehman18841082014-08-19 17:15:24 -0500111
Vince Lehman942eb7b2014-10-02 10:09:27 -0500112 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500113
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700114 m_table.emplace(name, std::move(entry));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500115
Nick Gordon5867b522017-09-06 17:41:37 -0500116 entryIt = m_table.find(name);
akmhoque53353462014-04-22 08:43:45 -0500117 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500118 // Existing FIB entry that may or may not have nextHops
akmhoque157b0a42014-05-13 00:26:37 -0500119 else {
Vince Lehman18841082014-08-19 17:15:24 -0500120 // Existing FIB entry
dmcoomes5bcb39e2017-10-31 15:07:55 -0500121 NLSR_LOG_DEBUG("Existing FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500122
123 // Remove empty FIB entry
Nick Gordonff9a6272017-10-12 13:38:29 -0500124 if (hopsToAdd.size() == 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500125 remove(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500126 return;
akmhoque53353462014-04-22 08:43:45 -0500127 }
Vince Lehman18841082014-08-19 17:15:24 -0500128
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500129 FibEntry& entry = (entryIt->second);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500130 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500131
laqinfancd7ca052017-06-02 04:57:18 -0500132 std::set<NextHop, NextHopComparator> hopsToRemove;
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700133 std::set_difference(entry.nexthopList.begin(), entry.nexthopList.end(),
laqinfancd7ca052017-06-02 04:57:18 -0500134 hopsToAdd.begin(), hopsToAdd.end(),
135 std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
136
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700137 bool isUpdatable = isNotNeighbor(entry.name);
laqinfancd7ca052017-06-02 04:57:18 -0500138 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600139 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500140 if (isUpdatable) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700141 unregisterPrefix(entry.name, hop.getConnectingFaceUri());
laqinfancd7ca052017-06-02 04:57:18 -0500142 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700143 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.name);
144 entry.nexthopList.removeNextHop(hop);
laqinfancd7ca052017-06-02 04:57:18 -0500145 }
Vince Lehman18841082014-08-19 17:15:24 -0500146
Vince Lehman18841082014-08-19 17:15:24 -0500147 // Increment sequence number
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700148 entry.seqNo += 1;
Nick Gordon5867b522017-09-06 17:41:37 -0500149 entryIt = m_table.find(name);
Nick Gordon5867b522017-09-06 17:41:37 -0500150 }
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700151
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600152 if (entryIt != m_table.end() &&
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700153 !entryIt->second.refreshEventId &&
154 isNotNeighbor(entryIt->second.name)) {
155 scheduleEntryRefresh(entryIt->second, [this] (FibEntry& entry) { scheduleLoop(entry); });
akmhoque53353462014-04-22 08:43:45 -0500156 }
157}
158
akmhoque53353462014-04-22 08:43:45 -0500159void
akmhoque31d1d4b2014-05-05 22:08:14 -0500160Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500161{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700162 NLSR_LOG_DEBUG("Clean called");
163 for (const auto& it : m_table) {
164 for (const auto& hop : it.second.nexthopList.getNextHops()) {
165 unregisterPrefix(it.second.name, hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500166 }
167 }
akmhoque53353462014-04-22 08:43:45 -0500168}
169
Vince Lehman942eb7b2014-10-02 10:09:27 -0500170unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500171Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500172{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500173 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
174 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
175
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700176 // 0 == all faces
177 return nMaxFaces == 0 ? nNextHops : std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500178}
179
akmhoque393d4ff2014-07-16 14:27:03 -0500180bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600181Fib::isNotNeighbor(const ndn::Name& name)
182{
183 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500184}
185
akmhoque53353462014-04-22 08:43:45 -0500186void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500187Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande41878572019-09-29 00:16:02 -0500188 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500189 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500190{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500191 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500192
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500193 if (faceId > 0) {
akmhoque060d3022014-08-12 13:35:06 -0500194 ndn::nfd::ControlParameters faceParameters;
195 faceParameters
196 .setName(namePrefix)
197 .setFaceId(faceId)
198 .setFlags(flags)
199 .setCost(faceCost)
200 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500201 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500202
dmcoomes5bcb39e2017-10-31 15:07:55 -0500203 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500204 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500205 std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
206 std::bind(&Fib::onRegistrationFailure, this, _1,
207 faceParameters, faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500208 }
209 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500210 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500211 }
212}
213
214void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500215Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
216 const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500217{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500218 NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
219 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500220
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400221 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500222 if (adjacent != m_adjacencyList.end()) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500223 adjacent->setFaceId(param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500224 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500225 onPrefixRegistrationSuccess(param.getName());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500226}
227
228void
229Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500230 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500231 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500232 uint8_t times)
233{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500234 NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
235 " (code: " << response.getCode() << ")");
236 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500237 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500238 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500239 registerPrefix(parameters.getName(), faceUri,
240 parameters.getCost(),
241 parameters.getExpirationPeriod(),
242 parameters.getFlags(), times+1);
243 }
244 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500245 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500246 }
akmhoquefdbddb12014-05-02 18:35:19 -0500247}
akmhoque31d1d4b2014-05-05 22:08:14 -0500248
akmhoquefdbddb12014-05-02 18:35:19 -0500249void
akmhoque157b0a42014-05-13 00:26:37 -0500250Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500251{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500252 uint64_t faceId = 0;
253 auto adjacent = m_adjacencyList.findAdjacent(ndn::FaceUri(faceUri));
254 if (adjacent != m_adjacencyList.end()) {
255 faceId = adjacent->getFaceId();
256 }
257
dmcoomes5bcb39e2017-10-31 15:07:55 -0500258 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500259 if (faceId > 0) {
260 ndn::nfd::ControlParameters controlParameters;
261 controlParameters
262 .setName(namePrefix)
263 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500264 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500265
akmhoque157b0a42014-05-13 00:26:37 -0500266 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500267 [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
268 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
269 " Face Id: " << commandSuccessResult.getFaceId());
270 },
271 [] (const ndn::nfd::ControlResponse& response) {
272 NLSR_LOG_DEBUG("Failed in unregistering name" << ": " << response.getText() <<
273 " (code: " << response.getCode() << ")");
274 });
akmhoque157b0a42014-05-13 00:26:37 -0500275 }
akmhoquefdbddb12014-05-02 18:35:19 -0500276}
277
278void
akmhoque393d4ff2014-07-16 14:27:03 -0500279Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500280{
281 ndn::nfd::ControlParameters parameters;
282 parameters
283 .setName(name)
284 .setStrategy(strategy);
285
286 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400287 std::bind(&Fib::onSetStrategySuccess, this, _1),
dmcoomes9f936662017-03-02 10:33:09 -0600288 std::bind(&Fib::onSetStrategyFailure, this, _1,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400289 parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500290}
291
292void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400293Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500294{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400295 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600296 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500297}
298
299void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000300Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
301 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400302 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500303{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400304 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600305 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500306 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600307 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500308 }
309}
310
311void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500312Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
313{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700314 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.name <<
315 " Seq Num: " << entry.seqNo <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600316 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500317
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700318 entry.refreshEventId = m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
319 std::bind(&Fib::refreshEntry, this,
320 entry.name, refreshCallback));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500321}
322
323void
324Fib::scheduleLoop(FibEntry& entry)
325{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600326 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500327}
328
329void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500330Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
331{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400332 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500333 if (it == m_table.end()) {
334 return;
335 }
336
337 FibEntry& entry = it->second;
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700338 NLSR_LOG_DEBUG("Refreshing " << entry.name << " Seq Num: " << entry.seqNo);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500339
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700340 entry.seqNo += 1;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500341
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700342 for (const NextHop& hop : entry.nexthopList) {
343 registerPrefix(entry.name,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500344 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500345 hop.getRouteCostAsAdjustedInteger(),
346 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
347 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500348 }
349
350 refreshCb(entry);
351}
352
353void
akmhoque674b0b12014-05-20 14:33:28 -0500354Fib::writeLog()
355{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500356 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600357 for (const auto& entry : m_table) {
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700358 NLSR_LOG_DEBUG("Name Prefix: " << entry.second.name);
359 NLSR_LOG_DEBUG("Seq No: " << entry.second.seqNo);
360 NLSR_LOG_DEBUG(entry.second.nexthopList);
akmhoque674b0b12014-05-20 14:33:28 -0500361 }
362}
akmhoquefdbddb12014-05-02 18:35:19 -0500363
Nick Gordonfad8e252016-08-11 14:21:38 -0500364} // namespace nlsr