blob: 90cddee1868c672842293251ad3cc104c9665f8c [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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
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
akmhoque393d4ff2014-07-16 14:27:03 -050036const uint64_t Fib::GRACE_PERIOD = 10;
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050037const std::string Fib::MULTICAST_STRATEGY("ndn:/localhost/nfd/strategy/multicast");
38const std::string Fib::BEST_ROUTE_V2_STRATEGY("ndn:/localhost/nfd/strategy/best-route");
akmhoque393d4ff2014-07-16 14:27:03 -050039
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060040Fib::Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
41 ConfParameter& conf, ndn::security::v2::KeyChain& keyChain)
42 : m_scheduler(scheduler)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060043 , m_refreshTime(2 * conf.getLsaRefreshTime())
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060044 , m_controller(face, keyChain)
45 , m_adjacencyList(adjacencyList)
46 , m_confParameter(conf)
47{
48}
49
akmhoque53353462014-04-22 08:43:45 -050050void
akmhoque31d1d4b2014-05-05 22:08:14 -050051Fib::remove(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050052{
dmcoomes5bcb39e2017-10-31 15:07:55 -050053 NLSR_LOG_DEBUG("Fib::remove called");
Davide Pesaventoaf7a2112019-03-19 14:55:20 -040054 auto it = m_table.find(name);
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060055
56 // Only unregister the prefix if it ISN'T a neighbor.
57 if (it != m_table.end() && isNotNeighbor((it->second).getName())) {
58 for (const auto& nexthop : (it->second).getNexthopList().getNextHops()) {
59 unregisterPrefix((it->second).getName(), nexthop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -050060 }
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050061 cancelEntryRefresh(it->second);
akmhoque53353462014-04-22 08:43:45 -050062 m_table.erase(it);
63 }
64}
65
Vince Lehman18841082014-08-19 17:15:24 -050066void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050067Fib::addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd)
Vince Lehman18841082014-08-19 17:15:24 -050068{
69 const ndn::Name& name = entry.getName();
70
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060071 bool shouldRegister = isNotNeighbor(name);
72
73 for (const auto& hop : hopsToAdd.getNextHops())
Vince Lehman18841082014-08-19 17:15:24 -050074 {
75 // Add nexthop to FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060076 entry.getNexthopList().addNextHop(hop);
Vince Lehman18841082014-08-19 17:15:24 -050077
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060078 if (shouldRegister) {
Vince Lehman18841082014-08-19 17:15:24 -050079 // Add nexthop to NDN-FIB
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060080 registerPrefix(name, ndn::FaceUri(hop.getConnectingFaceUri()),
81 hop.getRouteCostAsAdjustedInteger(),
Vince Lehman18841082014-08-19 17:15:24 -050082 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
83 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
84 }
85 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050086}
87
88void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050089Fib::update(const ndn::Name& name, const NexthopList& allHops)
Vince Lehman18841082014-08-19 17:15:24 -050090{
dmcoomes5bcb39e2017-10-31 15:07:55 -050091 NLSR_LOG_DEBUG("Fib::update called");
Vince Lehman18841082014-08-19 17:15:24 -050092
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060093 // Get the max possible faces which is the minimum of the configuration setting and
Vince Lehman942eb7b2014-10-02 10:09:27 -050094 // the length of the list of all next hops.
95 unsigned int maxFaces = getNumberOfFacesForName(allHops);
Vince Lehman18841082014-08-19 17:15:24 -050096
Vince Lehman942eb7b2014-10-02 10:09:27 -050097 NexthopList hopsToAdd;
98 unsigned int nFaces = 0;
Vince Lehman18841082014-08-19 17:15:24 -050099
Vince Lehman942eb7b2014-10-02 10:09:27 -0500100 // Create a list of next hops to be installed with length == maxFaces
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400101 for (auto it = allHops.cbegin(); it != allHops.cend() && nFaces < maxFaces; ++it, ++nFaces) {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500102 hopsToAdd.addNextHop(*it);
103 }
Vince Lehman18841082014-08-19 17:15:24 -0500104
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400105 auto entryIt = m_table.find(name);
Vince Lehman942eb7b2014-10-02 10:09:27 -0500106
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500107 // New FIB entry that has nextHops
Nick Gordonff9a6272017-10-12 13:38:29 -0500108 if (entryIt == m_table.end() && hopsToAdd.size() != 0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500109 NLSR_LOG_DEBUG("New FIB Entry");
Vince Lehman18841082014-08-19 17:15:24 -0500110
Vince Lehman18841082014-08-19 17:15:24 -0500111 FibEntry entry(name);
112
Vince Lehman942eb7b2014-10-02 10:09:27 -0500113 addNextHopsToFibEntryAndNfd(entry, hopsToAdd);
Vince Lehman18841082014-08-19 17:15:24 -0500114
Nick Gordonb7168472016-09-21 13:57:17 -0500115 m_table.emplace(name, 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
laqinfancd7ca052017-06-02 04:57:18 -0500133 std::set<NextHop, NextHopComparator> hopsToRemove;
134 std::set_difference(entry.getNexthopList().begin(), entry.getNexthopList().end(),
135 hopsToAdd.begin(), hopsToAdd.end(),
136 std::inserter(hopsToRemove, hopsToRemove.end()), NextHopComparator());
137
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600138 bool isUpdatable = isNotNeighbor(entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500139 // Remove the uninstalled next hops from NFD and FIB entry
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600140 for (const auto& hop : hopsToRemove){
laqinfancd7ca052017-06-02 04:57:18 -0500141 if (isUpdatable) {
142 unregisterPrefix(entry.getName(), hop.getConnectingFaceUri());
143 }
dmcoomes5bcb39e2017-10-31 15:07:55 -0500144 NLSR_LOG_DEBUG("Removing " << hop.getConnectingFaceUri() << " from " << entry.getName());
laqinfancd7ca052017-06-02 04:57:18 -0500145 entry.getNexthopList().removeNextHop(hop);
146 }
Vince Lehman18841082014-08-19 17:15:24 -0500147
Vince Lehman18841082014-08-19 17:15:24 -0500148 // Increment sequence number
149 entry.setSeqNo(entry.getSeqNo() + 1);
150
Nick Gordon5867b522017-09-06 17:41:37 -0500151 entryIt = m_table.find(name);
152
153 }
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600154 if (entryIt != m_table.end() &&
155 !entryIt->second.getRefreshEventId() &&
156 isNotNeighbor(entryIt->second.getName())) {
Nick Gordon5867b522017-09-06 17:41:37 -0500157 scheduleEntryRefresh(entryIt->second,
158 [this] (FibEntry& entry) {
159 scheduleLoop(entry);
160 });
akmhoque53353462014-04-22 08:43:45 -0500161 }
162}
163
akmhoque53353462014-04-22 08:43:45 -0500164void
akmhoque31d1d4b2014-05-05 22:08:14 -0500165Fib::clean()
akmhoque53353462014-04-22 08:43:45 -0500166{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500167 NLSR_LOG_DEBUG("Fib::clean called");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600168 // can't use const ref here as getNexthopList can't be marked const
169 for (auto&& it : m_table) {
170 NLSR_LOG_DEBUG("Canceling Scheduled event. Name: " << it.second.getName());
171 cancelEntryRefresh(it.second);
172
173 for (const auto& hop : it.second.getNexthopList().getNextHops()) {
174 unregisterPrefix(it.second.getName(), hop.getConnectingFaceUri());
akmhoque53353462014-04-22 08:43:45 -0500175 }
176 }
akmhoque53353462014-04-22 08:43:45 -0500177}
178
Vince Lehman942eb7b2014-10-02 10:09:27 -0500179unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500180Fib::getNumberOfFacesForName(const NexthopList& nextHopList)
akmhoque53353462014-04-22 08:43:45 -0500181{
Vince Lehman942eb7b2014-10-02 10:09:27 -0500182 uint32_t nNextHops = static_cast<uint32_t>(nextHopList.getNextHops().size());
183 uint32_t nMaxFaces = m_confParameter.getMaxFacesPerPrefix();
184
185 // Allow all faces
186 if (nMaxFaces == 0) {
187 return nNextHops;
akmhoque53353462014-04-22 08:43:45 -0500188 }
akmhoque157b0a42014-05-13 00:26:37 -0500189 else {
Vince Lehman942eb7b2014-10-02 10:09:27 -0500190 return std::min(nNextHops, nMaxFaces);
akmhoque53353462014-04-22 08:43:45 -0500191 }
akmhoque53353462014-04-22 08:43:45 -0500192}
193
akmhoque393d4ff2014-07-16 14:27:03 -0500194bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600195Fib::isNotNeighbor(const ndn::Name& name)
196{
197 return !m_adjacencyList.isNeighbor(name);
akmhoque393d4ff2014-07-16 14:27:03 -0500198}
199
akmhoque53353462014-04-22 08:43:45 -0500200void
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500201Fib::registerPrefix(const ndn::Name& namePrefix, const ndn::FaceUri& faceUri,
Ashlesh Gawande41878572019-09-29 00:16:02 -0500202 uint64_t faceCost, const ndn::time::milliseconds& timeout,
akmhoque060d3022014-08-12 13:35:06 -0500203 uint64_t flags, uint8_t times)
akmhoquec04e7272014-07-02 11:00:14 -0500204{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500205 uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500206
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500207 if (faceId > 0) {
akmhoque060d3022014-08-12 13:35:06 -0500208 ndn::nfd::ControlParameters faceParameters;
209 faceParameters
210 .setName(namePrefix)
211 .setFaceId(faceId)
212 .setFlags(flags)
213 .setCost(faceCost)
214 .setExpirationPeriod(timeout)
Davide Pesavento87911da2017-02-21 22:10:55 -0500215 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
akmhoque060d3022014-08-12 13:35:06 -0500216
dmcoomes5bcb39e2017-10-31 15:07:55 -0500217 NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500218 m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500219 std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
220 std::bind(&Fib::onRegistrationFailure, this, _1,
221 faceParameters, faceUri, times));
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500222 }
223 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500224 NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500225 }
226}
227
228void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500229Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
230 const ndn::FaceUri& faceUri)
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500231{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500232 NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
233 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500234
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400235 auto adjacent = m_adjacencyList.findAdjacent(faceUri);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500236 if (adjacent != m_adjacencyList.end()) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500237 adjacent->setFaceId(param.getFaceId());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500238 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500239 onPrefixRegistrationSuccess(param.getName());
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500240}
241
242void
243Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500244 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500245 const ndn::FaceUri& faceUri,
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500246 uint8_t times)
247{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500248 NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
249 " (code: " << response.getCode() << ")");
250 NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500251 if (times < 3) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500252 NLSR_LOG_DEBUG("Trying to register again...");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500253 registerPrefix(parameters.getName(), faceUri,
254 parameters.getCost(),
255 parameters.getExpirationPeriod(),
256 parameters.getFlags(), times+1);
257 }
258 else {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500259 NLSR_LOG_DEBUG("Registration trial given up");
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500260 }
akmhoquefdbddb12014-05-02 18:35:19 -0500261}
akmhoque31d1d4b2014-05-05 22:08:14 -0500262
akmhoquefdbddb12014-05-02 18:35:19 -0500263void
akmhoque157b0a42014-05-13 00:26:37 -0500264Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
akmhoquefdbddb12014-05-02 18:35:19 -0500265{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500266 uint64_t faceId = 0;
267 auto adjacent = m_adjacencyList.findAdjacent(ndn::FaceUri(faceUri));
268 if (adjacent != m_adjacencyList.end()) {
269 faceId = adjacent->getFaceId();
270 }
271
dmcoomes5bcb39e2017-10-31 15:07:55 -0500272 NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
akmhoque157b0a42014-05-13 00:26:37 -0500273 if (faceId > 0) {
274 ndn::nfd::ControlParameters controlParameters;
275 controlParameters
276 .setName(namePrefix)
277 .setFaceId(faceId)
Davide Pesavento87911da2017-02-21 22:10:55 -0500278 .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500279
akmhoque157b0a42014-05-13 00:26:37 -0500280 m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500281 [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
282 NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
283 " Face Id: " << commandSuccessResult.getFaceId());
284 },
285 [] (const ndn::nfd::ControlResponse& response) {
286 NLSR_LOG_DEBUG("Failed in unregistering name" << ": " << response.getText() <<
287 " (code: " << response.getCode() << ")");
288 });
akmhoque157b0a42014-05-13 00:26:37 -0500289 }
akmhoquefdbddb12014-05-02 18:35:19 -0500290}
291
292void
akmhoque393d4ff2014-07-16 14:27:03 -0500293Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
akmhoque157b0a42014-05-13 00:26:37 -0500294{
295 ndn::nfd::ControlParameters parameters;
296 parameters
297 .setName(name)
298 .setStrategy(strategy);
299
300 m_controller.start<ndn::nfd::StrategyChoiceSetCommand>(parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400301 std::bind(&Fib::onSetStrategySuccess, this, _1),
dmcoomes9f936662017-03-02 10:33:09 -0600302 std::bind(&Fib::onSetStrategyFailure, this, _1,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400303 parameters, count));
akmhoque157b0a42014-05-13 00:26:37 -0500304}
305
306void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400307Fib::onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult)
akmhoque393d4ff2014-07-16 14:27:03 -0500308{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400309 NLSR_LOG_DEBUG("Successfully set strategy choice: " << commandSuccessResult.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600310 " for name: " << commandSuccessResult.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500311}
312
313void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000314Fib::onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
315 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400316 uint32_t count)
akmhoque393d4ff2014-07-16 14:27:03 -0500317{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400318 NLSR_LOG_DEBUG("Failed to set strategy choice: " << parameters.getStrategy() <<
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600319 " for name: " << parameters.getName());
akmhoque393d4ff2014-07-16 14:27:03 -0500320 if (count < 3) {
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600321 setStrategy(parameters.getName(), parameters.getStrategy().toUri(), count + 1);
akmhoque393d4ff2014-07-16 14:27:03 -0500322 }
323}
324
325void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500326Fib::scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCallback)
327{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600328 NLSR_LOG_DEBUG("Scheduling refresh for " << entry.getName() <<
329 " Seq Num: " << entry.getSeqNo() <<
330 " in " << m_refreshTime << " seconds");
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500331
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400332 entry.setRefreshEventId(m_scheduler.schedule(ndn::time::seconds(m_refreshTime),
333 std::bind(&Fib::refreshEntry, this,
334 entry.getName(), refreshCallback)));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500335}
336
337void
338Fib::scheduleLoop(FibEntry& entry)
339{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600340 scheduleEntryRefresh(entry, std::bind(&Fib::scheduleLoop, this, _1));
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500341}
342
343void
344Fib::cancelEntryRefresh(const FibEntry& entry)
345{
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600346 NLSR_LOG_DEBUG("Canceling refresh for " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400347 entry.getRefreshEventId().cancel();
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500348}
349
350void
351Fib::refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb)
352{
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400353 auto it = m_table.find(name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500354 if (it == m_table.end()) {
355 return;
356 }
357
358 FibEntry& entry = it->second;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500359 NLSR_LOG_DEBUG("Refreshing " << entry.getName() << " Seq Num: " << entry.getSeqNo());
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500360
361 // Increment sequence number
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600362 entry.setSeqNo(entry.getSeqNo() + 1);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500363
364 for (const NextHop& hop : entry) {
365 registerPrefix(entry.getName(),
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500366 ndn::FaceUri(hop.getConnectingFaceUri()),
Ashlesh Gawande793e8702017-08-01 15:59:26 -0500367 hop.getRouteCostAsAdjustedInteger(),
368 ndn::time::seconds(m_refreshTime + GRACE_PERIOD),
369 ndn::nfd::ROUTE_FLAG_CAPTURE, 0);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500370 }
371
372 refreshCb(entry);
373}
374
375void
akmhoque674b0b12014-05-20 14:33:28 -0500376Fib::writeLog()
377{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500378 NLSR_LOG_DEBUG("-------------------FIB-----------------------------");
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600379 for (const auto& entry : m_table) {
380 entry.second.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500381 }
382}
akmhoquefdbddb12014-05-02 18:35:19 -0500383
Nick Gordonfad8e252016-08-11 14:21:38 -0500384} // namespace nlsr