blob: 27891eb12c299d59571f3071a07688ab4a35e5b2 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Junxiao Shi3e5120c2016-09-10 16:58:34 +00004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Alexander Afanasyev67758b12018-03-06 18:36:44 -050020 */
akmhoque53353462014-04-22 08:43:45 -050021
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050022#ifndef NLSR_ROUTE_FIB_HPP
23#define NLSR_ROUTE_FIB_HPP
Nick Gordonb7168472016-09-21 13:57:17 -050024
akmhoque53353462014-04-22 08:43:45 -050025#include "fib-entry.hpp"
Vince Lehman942eb7b2014-10-02 10:09:27 -050026#include "test-access-control.hpp"
akmhoque53353462014-04-22 08:43:45 -050027
Nick Gordon21088082017-05-24 10:57:06 -050028#include <ndn-cxx/mgmt/nfd/controller.hpp>
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050029#include <ndn-cxx/util/time.hpp>
30
akmhoque53353462014-04-22 08:43:45 -050031namespace nlsr {
32
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050033typedef std::function<void(FibEntry&)> afterRefreshCallback;
akmhoquec04e7272014-07-02 11:00:14 -050034
Vince Lehman942eb7b2014-10-02 10:09:27 -050035class AdjacencyList;
36class ConfParameter;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050037class FibEntry;
akmhoque53353462014-04-22 08:43:45 -050038
Nick Gordond0a7df32017-05-30 16:44:34 -050039/*! \brief Maps names to lists of next hops, and exports this information to NFD.
40 *
41 * The FIB (Forwarding Information Base) is the "authoritative" source
42 * of how to route Interests on this router to other nodes running
43 * NLSR. In essence, the FIB is a map that takes name prefixes to a
44 * list of next-hops out of this router. This class also contains
45 * methods to inform NFD about these relationships. The FIB has its
46 * entries populated by the NamePrefixTable
47 *
48 * \sa nlsr::NamePrefixTable
49 * \sa nlsr::NamePrefixTable::addEntry
50 * \sa nlsr::NamePrefixTable::updateWithNewRoute
51 */
akmhoque53353462014-04-22 08:43:45 -050052class Fib
53{
54public:
Ashlesh Gawandee5002b32018-12-20 21:07:31 -060055 Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList,
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040056 ConfParameter& conf, ndn::security::KeyChain& keyChain);
Vince Lehman942eb7b2014-10-02 10:09:27 -050057
Nick Gordond0a7df32017-05-30 16:44:34 -050058 /*! \brief Completely remove a name prefix from the FIB.
59 *
60 * If a name prefix is found to no longer be reachable from this
61 * router, it will be removed from the FIB and all of its next-hops
62 * will be unregistered from NFD.
63 *
64 * \sa nlsr::NamePrefixTable::removeEntry
65 */
Ashlesh Gawande744e4812018-08-22 16:26:24 -050066 void
akmhoque31d1d4b2014-05-05 22:08:14 -050067 remove(const ndn::Name& name);
akmhoque53353462014-04-22 08:43:45 -050068
Nick Gordond0a7df32017-05-30 16:44:34 -050069 /*! \brief Set the nexthop list of a name.
70 *
71 * This method is the entry for others to add next-hop information
72 * to the FIB. Formally put, this method registers in NFD all
73 * next-hops in allHops, and unregisters the set difference of
74 * newHops - oldHops. This method also schedules the regular refresh
75 * of those next hops.
76 *
77 * \param name The name prefix that the next-hops apply to
78 * \param allHops A complete list of next-hops to associate with name.
79 */
Ashlesh Gawande744e4812018-08-22 16:26:24 -050080 void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050081 update(const ndn::Name& name, const NexthopList& allHops);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060082
Nick Gordond0a7df32017-05-30 16:44:34 -050083 /*! \brief Remove all entries from the FIB.
84 *
85 * This method is called before terminating NLSR to minimize the
86 * time NFD spends routing on now-invalid information. This is not
87 * strictly necessary, because eventually those prefix registrations
88 * will expire, but cleaning up after ourselves improves
89 * performance.
Nick Gordond0a7df32017-05-30 16:44:34 -050090 */
akmhoque53353462014-04-22 08:43:45 -050091 void
akmhoque31d1d4b2014-05-05 22:08:14 -050092 clean();
akmhoque53353462014-04-22 08:43:45 -050093
94 void
akmhoquefdbddb12014-05-02 18:35:19 -050095 setEntryRefreshTime(int32_t fert)
akmhoque53353462014-04-22 08:43:45 -050096 {
97 m_refreshTime = fert;
98 }
99
Nick Gordond0a7df32017-05-30 16:44:34 -0500100 /*! \brief Inform NFD of a next-hop
101 *
102 * This method informs NFD of a next-hop for some name prefix. This
103 * method actually submits the information to NFD's RIB, which then
104 * aggregates its own best hops and updates NFD's (the actual)
105 * FIB. Typically, NLSR's FIB and NFD's FIB will be almost the
106 * same. However, this is not necessarily the case and there may be
107 * cases when other sources of information provide better next-hops
108 * to NFD that NLSR doesn't know about. For example, an operator
109 * could set up a direct link to a node that isn't running NLSR.
110 *
111 * \param namePrefix The name prefix to register a next-hop for
112 * \param faceUri The faceUri of the adjacent that this prefix can be reached through
113 * \param faceCost The cost to reach namePrefix through faceUri
114 * \param timeout How long this registration should last
115 * \param flags Route inheritance flags (CAPTURE, CHILD_INHERIT)
116 * \param times How many times we have failed to register this prefix since the last success.
117 *
118 * \sa Fib::registerPrefixInNfd
119 */
akmhoque53353462014-04-22 08:43:45 -0500120 void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500121 registerPrefix(const ndn::Name& namePrefix,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500122 const ndn::FaceUri& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500123 uint64_t faceCost,
akmhoque060d3022014-08-12 13:35:06 -0500124 const ndn::time::milliseconds& timeout,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500125 uint64_t flags,
126 uint8_t times);
akmhoquefdbddb12014-05-02 18:35:19 -0500127
128 void
akmhoque393d4ff2014-07-16 14:27:03 -0500129 setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count);
akmhoque157b0a42014-05-13 00:26:37 -0500130
akmhoque674b0b12014-05-20 14:33:28 -0500131 void
132 writeLog();
133
akmhoque157b0a42014-05-13 00:26:37 -0500134private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500135 /*! \brief Indicates whether a prefix is a direct neighbor or not.
136 *
137 * \return Whether the name is NOT associated with a direct neighbor
138 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500139 bool
Ashlesh Gawandee5002b32018-12-20 21:07:31 -0600140 isNotNeighbor(const ndn::Name& name);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500141
Nick Gordond0a7df32017-05-30 16:44:34 -0500142 /*! \brief Does one half of the updating of a FibEntry with new next-hops.
143 *
144 * Adds nexthops to a FibEntry and registers them in NFD.
145 * \sa Fib::update
146 * \sa Fib::removeOldNextHopsFromFibEntryAndNfd
147 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500148 void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500149 addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500150
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500151 unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500152 getNumberOfFacesForName(const NexthopList& nextHopList);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500153
Nick Gordond0a7df32017-05-30 16:44:34 -0500154 /*! \brief Unregisters a prefix from NFD's RIB.
155 *
156 */
akmhoque157b0a42014-05-13 00:26:37 -0500157 void
158 unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri);
159
Nick Gordond0a7df32017-05-30 16:44:34 -0500160 /*! \brief Log registration success, and update the Face ID associated with a URI.
161 */
akmhoque157b0a42014-05-13 00:26:37 -0500162 void
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500163 onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
164 const ndn::FaceUri& faceUri);
akmhoquefdbddb12014-05-02 18:35:19 -0500165
Nick Gordond0a7df32017-05-30 16:44:34 -0500166 /*! \brief Retry a prefix (next-hop) registration up to three (3) times.
167 */
akmhoquefdbddb12014-05-02 18:35:19 -0500168 void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000169 onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque060d3022014-08-12 13:35:06 -0500170 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500171 const ndn::FaceUri& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500172 uint8_t times);
173
Nick Gordond0a7df32017-05-30 16:44:34 -0500174 /*! \brief Log a successful strategy setting.
175 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500176 void
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400177 onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult);
akmhoque393d4ff2014-07-16 14:27:03 -0500178
Nick Gordond0a7df32017-05-30 16:44:34 -0500179 /*! \brief Retry a strategy setting up to three (3) times.
180 */
akmhoque393d4ff2014-07-16 14:27:03 -0500181 void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000182 onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
akmhoque393d4ff2014-07-16 14:27:03 -0500183 const ndn::nfd::ControlParameters& parameters,
Davide Pesaventoaf7a2112019-03-19 14:55:20 -0400184 uint32_t count);
akmhoque393d4ff2014-07-16 14:27:03 -0500185
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500186PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick Gordond0a7df32017-05-30 16:44:34 -0500187 /*! \brief Schedule a refresh event for an entry.
188 *
189 * Schedules a refresh event for an entry. In order to form a
190 * perpetual loop, refreshCallback needs to call
191 * Fib::scheduleEntryRefresh in some way, with refreshCallback being
192 * the same each time. In the current implementation, this is
193 * accomplished by having a separate function, Fib::scheduleLoop,
194 * that does this work.
195 * \sa Fib::scheduleLoop
196 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500197 void
198 scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCb);
199
200private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500201 /*! \brief Continue the entry refresh cycle.
202 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500203 void
204 scheduleLoop(FibEntry& entry);
205
Nick Gordond0a7df32017-05-30 16:44:34 -0500206 /*! \brief Cancel an entry's refresh event.
207 *
208 * Cancel an entry's refresh event. This only needs to be done when
209 * an entry is removed. Typically this happens when NLSR is
210 * terminated or crashes, and we don't want the scheduler to crash
211 * because it's referencing memory that has no valid function.
Nick Gordond0a7df32017-05-30 16:44:34 -0500212 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500213 void
214 cancelEntryRefresh(const FibEntry& entry);
215
Nick Gordond0a7df32017-05-30 16:44:34 -0500216 /*! \brief Refreshes an entry in NFD.
217 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500218 void
219 refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb);
220
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500221public:
222 static const std::string MULTICAST_STRATEGY;
223 static const std::string BEST_ROUTE_V2_STRATEGY;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500224 ndn::util::Signal<Fib, const ndn::Name&> onPrefixRegistrationSuccess;
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500225
akmhoque53353462014-04-22 08:43:45 -0500226private:
Vince Lehman7c603292014-09-11 17:48:16 -0500227 ndn::Scheduler& m_scheduler;
akmhoquefdbddb12014-05-02 18:35:19 -0500228 int32_t m_refreshTime;
229 ndn::nfd::Controller m_controller;
Vince Lehman942eb7b2014-10-02 10:09:27 -0500230
231PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500232 std::map<ndn::Name, FibEntry> m_table;
akmhoque393d4ff2014-07-16 14:27:03 -0500233
Vince Lehman942eb7b2014-10-02 10:09:27 -0500234private:
235 AdjacencyList& m_adjacencyList;
236 ConfParameter& m_confParameter;
237
Nick Gordond0a7df32017-05-30 16:44:34 -0500238 /*! GRACE_PERIOD A "window" we append to the timeout time to
239 * allow for things like stuttering prefix registrations and
240 * processing time when refreshing events.
241 */
akmhoque393d4ff2014-07-16 14:27:03 -0500242 static const uint64_t GRACE_PERIOD;
akmhoque53353462014-04-22 08:43:45 -0500243};
244
Nick Gordonfad8e252016-08-11 14:21:38 -0500245} // namespace nlsr
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500246
247#endif // NLSR_ROUTE_FIB_HPP