blob: 32c656d0e72672614e69bbf25bf8587bdb76939e [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/*
3 * Copyright (c) 2014-2018, 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
akmhoque157b0a42014-05-13 00:26:37 -050025#include "face-map.hpp"
akmhoque53353462014-04-22 08:43:45 -050026#include "fib-entry.hpp"
Vince Lehman942eb7b2014-10-02 10:09:27 -050027#include "test-access-control.hpp"
akmhoque53353462014-04-22 08:43:45 -050028
Nick Gordon21088082017-05-24 10:57:06 -050029#include <ndn-cxx/mgmt/nfd/controller.hpp>
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050030#include <ndn-cxx/util/time.hpp>
31
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050034typedef std::function<void(FibEntry&)> afterRefreshCallback;
akmhoquec04e7272014-07-02 11:00:14 -050035
Vince Lehman942eb7b2014-10-02 10:09:27 -050036class AdjacencyList;
37class ConfParameter;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -050038class FibEntry;
akmhoque53353462014-04-22 08:43:45 -050039
Nick Gordond0a7df32017-05-30 16:44:34 -050040/*! \brief Maps names to lists of next hops, and exports this information to NFD.
41 *
42 * The FIB (Forwarding Information Base) is the "authoritative" source
43 * of how to route Interests on this router to other nodes running
44 * NLSR. In essence, the FIB is a map that takes name prefixes to a
45 * list of next-hops out of this router. This class also contains
46 * methods to inform NFD about these relationships. The FIB has its
47 * entries populated by the NamePrefixTable
48 *
49 * \sa nlsr::NamePrefixTable
50 * \sa nlsr::NamePrefixTable::addEntry
51 * \sa nlsr::NamePrefixTable::updateWithNewRoute
52 */
akmhoque53353462014-04-22 08:43:45 -050053class Fib
54{
55public:
Vince Lehmanb7079a12014-11-04 12:45:50 -060056 Fib(ndn::Face& face, ndn::Scheduler& scheduler, AdjacencyList& adjacencyList, ConfParameter& conf,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050057 ndn::security::v2::KeyChain& keyChain)
Vince Lehman942eb7b2014-10-02 10:09:27 -050058 : m_scheduler(scheduler)
akmhoque53353462014-04-22 08:43:45 -050059 , m_refreshTime(0)
Vince Lehmanb7079a12014-11-04 12:45:50 -060060 , m_controller(face, keyChain)
Vince Lehman942eb7b2014-10-02 10:09:27 -050061 , m_adjacencyList(adjacencyList)
62 , m_confParameter(conf)
akmhoquefdbddb12014-05-02 18:35:19 -050063 {
64 }
Vince Lehman942eb7b2014-10-02 10:09:27 -050065
Nick Gordond0a7df32017-05-30 16:44:34 -050066 /*! \brief Completely remove a name prefix from the FIB.
67 *
68 * If a name prefix is found to no longer be reachable from this
69 * router, it will be removed from the FIB and all of its next-hops
70 * will be unregistered from NFD.
71 *
72 * \sa nlsr::NamePrefixTable::removeEntry
73 */
Ashlesh Gawande744e4812018-08-22 16:26:24 -050074 void
akmhoque31d1d4b2014-05-05 22:08:14 -050075 remove(const ndn::Name& name);
akmhoque53353462014-04-22 08:43:45 -050076
Nick Gordond0a7df32017-05-30 16:44:34 -050077 /*! \brief Set the nexthop list of a name.
78 *
79 * This method is the entry for others to add next-hop information
80 * to the FIB. Formally put, this method registers in NFD all
81 * next-hops in allHops, and unregisters the set difference of
82 * newHops - oldHops. This method also schedules the regular refresh
83 * of those next hops.
84 *
85 * \param name The name prefix that the next-hops apply to
86 * \param allHops A complete list of next-hops to associate with name.
87 */
Ashlesh Gawande744e4812018-08-22 16:26:24 -050088 void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -050089 update(const ndn::Name& name, const NexthopList& allHops);
Nick Gordon4d2c6c02017-01-20 13:18:46 -060090
Nick Gordond0a7df32017-05-30 16:44:34 -050091 /*! \brief Remove all entries from the FIB.
92 *
93 * This method is called before terminating NLSR to minimize the
94 * time NFD spends routing on now-invalid information. This is not
95 * strictly necessary, because eventually those prefix registrations
96 * will expire, but cleaning up after ourselves improves
97 * performance.
98 *
99 * \sa NlsrRunner::run
100 *
101 */
akmhoque53353462014-04-22 08:43:45 -0500102 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 clean();
akmhoque53353462014-04-22 08:43:45 -0500104
105 void
akmhoquefdbddb12014-05-02 18:35:19 -0500106 setEntryRefreshTime(int32_t fert)
akmhoque53353462014-04-22 08:43:45 -0500107 {
108 m_refreshTime = fert;
109 }
110
Nick Gordond0a7df32017-05-30 16:44:34 -0500111 /*! \brief Inform NFD of a next-hop
112 *
113 * This method informs NFD of a next-hop for some name prefix. This
114 * method actually submits the information to NFD's RIB, which then
115 * aggregates its own best hops and updates NFD's (the actual)
116 * FIB. Typically, NLSR's FIB and NFD's FIB will be almost the
117 * same. However, this is not necessarily the case and there may be
118 * cases when other sources of information provide better next-hops
119 * to NFD that NLSR doesn't know about. For example, an operator
120 * could set up a direct link to a node that isn't running NLSR.
121 *
122 * \param namePrefix The name prefix to register a next-hop for
123 * \param faceUri The faceUri of the adjacent that this prefix can be reached through
124 * \param faceCost The cost to reach namePrefix through faceUri
125 * \param timeout How long this registration should last
126 * \param flags Route inheritance flags (CAPTURE, CHILD_INHERIT)
127 * \param times How many times we have failed to register this prefix since the last success.
128 *
129 * \sa Fib::registerPrefixInNfd
130 */
akmhoque53353462014-04-22 08:43:45 -0500131 void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500132 registerPrefix(const ndn::Name& namePrefix,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500133 const ndn::FaceUri& faceUri,
akmhoquebf11c5f2014-07-21 14:49:47 -0500134 uint64_t faceCost,
akmhoque060d3022014-08-12 13:35:06 -0500135 const ndn::time::milliseconds& timeout,
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500136 uint64_t flags,
137 uint8_t times);
akmhoquefdbddb12014-05-02 18:35:19 -0500138
139 void
akmhoque393d4ff2014-07-16 14:27:03 -0500140 setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count);
akmhoque157b0a42014-05-13 00:26:37 -0500141
akmhoque674b0b12014-05-20 14:33:28 -0500142 void
143 writeLog();
144
akmhoque157b0a42014-05-13 00:26:37 -0500145private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500146 /*! \brief Indicates whether a prefix is a direct neighbor or not.
147 *
148 * \return Whether the name is NOT associated with a direct neighbor
149 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500150 bool
151 isPrefixUpdatable(const ndn::Name& name);
152
Nick Gordond0a7df32017-05-30 16:44:34 -0500153 /*! \brief Does one half of the updating of a FibEntry with new next-hops.
154 *
155 * Adds nexthops to a FibEntry and registers them in NFD.
156 * \sa Fib::update
157 * \sa Fib::removeOldNextHopsFromFibEntryAndNfd
158 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500159 void
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500160 addNextHopsToFibEntryAndNfd(FibEntry& entry, const NexthopList& hopsToAdd);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500161
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500162 unsigned int
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500163 getNumberOfFacesForName(const NexthopList& nextHopList);
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500164
Nick Gordond0a7df32017-05-30 16:44:34 -0500165 /*! \brief Unregisters a prefix from NFD's RIB.
166 *
167 */
akmhoque157b0a42014-05-13 00:26:37 -0500168 void
169 unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri);
170
Nick Gordond0a7df32017-05-30 16:44:34 -0500171 /*! \brief Log registration success, and update the Face ID associated with a URI.
172 */
akmhoque157b0a42014-05-13 00:26:37 -0500173 void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500174 onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500175 const std::string& message, const ndn::FaceUri& faceUri);
akmhoquefdbddb12014-05-02 18:35:19 -0500176
Nick Gordond0a7df32017-05-30 16:44:34 -0500177 /*! \brief Retry a prefix (next-hop) registration up to three (3) times.
178 */
akmhoquefdbddb12014-05-02 18:35:19 -0500179 void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000180 onRegistrationFailure(const ndn::nfd::ControlResponse& response,
akmhoque102aea42014-08-04 10:22:12 -0500181 const std::string& message,
akmhoque060d3022014-08-12 13:35:06 -0500182 const ndn::nfd::ControlParameters& parameters,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500183 const ndn::FaceUri& faceUri,
akmhoque102aea42014-08-04 10:22:12 -0500184 uint8_t times);
185
Nick Gordond0a7df32017-05-30 16:44:34 -0500186 /*! \brief Log a successful unregistration.
187 */
akmhoque102aea42014-08-04 10:22:12 -0500188 void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500189 onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
Junxiao Shi63bd0342016-08-17 16:57:14 +0000190 const std::string& message);
akmhoque53353462014-04-22 08:43:45 -0500191
Nick Gordond0a7df32017-05-30 16:44:34 -0500192 /*! \brief Log an unregistration failure. Does not retry.
193 */
akmhoque393d4ff2014-07-16 14:27:03 -0500194 void
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500195 onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
196 const std::string& message);
197
Nick Gordond0a7df32017-05-30 16:44:34 -0500198 /*! \brief Log a successful strategy setting.
199 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500200 void
akmhoque393d4ff2014-07-16 14:27:03 -0500201 onSetStrategySuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
202 const std::string& message);
203
Nick Gordond0a7df32017-05-30 16:44:34 -0500204 /*! \brief Retry a strategy setting up to three (3) times.
205 */
akmhoque393d4ff2014-07-16 14:27:03 -0500206 void
Junxiao Shi63bd0342016-08-17 16:57:14 +0000207 onSetStrategyFailure(const ndn::nfd::ControlResponse& response,
akmhoque393d4ff2014-07-16 14:27:03 -0500208 const ndn::nfd::ControlParameters& parameters,
209 uint32_t count,
210 const std::string& message);
211
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500212PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Nick Gordond0a7df32017-05-30 16:44:34 -0500213 /*! \brief Schedule a refresh event for an entry.
214 *
215 * Schedules a refresh event for an entry. In order to form a
216 * perpetual loop, refreshCallback needs to call
217 * Fib::scheduleEntryRefresh in some way, with refreshCallback being
218 * the same each time. In the current implementation, this is
219 * accomplished by having a separate function, Fib::scheduleLoop,
220 * that does this work.
221 * \sa Fib::scheduleLoop
222 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500223 void
224 scheduleEntryRefresh(FibEntry& entry, const afterRefreshCallback& refreshCb);
225
226private:
Nick Gordond0a7df32017-05-30 16:44:34 -0500227 /*! \brief Continue the entry refresh cycle.
228 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500229 void
230 scheduleLoop(FibEntry& entry);
231
Nick Gordond0a7df32017-05-30 16:44:34 -0500232 /*! \brief Cancel an entry's refresh event.
233 *
234 * Cancel an entry's refresh event. This only needs to be done when
235 * an entry is removed. Typically this happens when NLSR is
236 * terminated or crashes, and we don't want the scheduler to crash
237 * because it's referencing memory that has no valid function.
238 *
239 * \sa NlsrRunner::run
240 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500241 void
242 cancelEntryRefresh(const FibEntry& entry);
243
Nick Gordond0a7df32017-05-30 16:44:34 -0500244 /*! \brief Refreshes an entry in NFD.
245 */
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500246 void
247 refreshEntry(const ndn::Name& name, afterRefreshCallback refreshCb);
248
akmhoque53353462014-04-22 08:43:45 -0500249private:
Vince Lehman7c603292014-09-11 17:48:16 -0500250 ndn::Scheduler& m_scheduler;
akmhoquefdbddb12014-05-02 18:35:19 -0500251 int32_t m_refreshTime;
252 ndn::nfd::Controller m_controller;
Vince Lehman942eb7b2014-10-02 10:09:27 -0500253
254PUBLIC_WITH_TESTS_ELSE_PRIVATE:
akmhoque157b0a42014-05-13 00:26:37 -0500255 FaceMap m_faceMap;
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500256 std::map<ndn::Name, FibEntry> m_table;
akmhoque393d4ff2014-07-16 14:27:03 -0500257
Vince Lehman942eb7b2014-10-02 10:09:27 -0500258private:
259 AdjacencyList& m_adjacencyList;
260 ConfParameter& m_confParameter;
261
Nick Gordond0a7df32017-05-30 16:44:34 -0500262 /*! GRACE_PERIOD A "window" we append to the timeout time to
263 * allow for things like stuttering prefix registrations and
264 * processing time when refreshing events.
265 */
akmhoque393d4ff2014-07-16 14:27:03 -0500266 static const uint64_t GRACE_PERIOD;
akmhoque53353462014-04-22 08:43:45 -0500267};
268
Nick Gordonfad8e252016-08-11 14:21:38 -0500269} // namespace nlsr
Muktadir Chowdhury3be64662015-05-01 14:50:53 -0500270
271#endif // NLSR_ROUTE_FIB_HPP