blob: ee820f69f174acebd62b9d79d996b242418a123b [file] [log] [blame]
Vince Lehman76c751c2014-11-18 17:36:38 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Vince Lehman76c751c2014-11-18 17:36:38 -06004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Davide Pesavento1b077f62019-02-19 19:19:44 -050026#ifndef NFD_DAEMON_RIB_FIB_UPDATER_HPP
27#define NFD_DAEMON_RIB_FIB_UPDATER_HPP
Vince Lehman76c751c2014-11-18 17:36:38 -060028
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Vince Lehman76c751c2014-11-18 17:36:38 -060030#include "fib-update.hpp"
31#include "rib.hpp"
32#include "rib-update-batch.hpp"
33
Junxiao Shi25c6ce42016-09-09 13:49:59 +000034#include <ndn-cxx/mgmt/nfd/controller.hpp>
Vince Lehman76c751c2014-11-18 17:36:38 -060035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::rib {
Vince Lehman76c751c2014-11-18 17:36:38 -060037
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040038/**
39 * \brief Computes FibUpdates based on updates to the RIB and sends them to NFD.
Vince Lehman76c751c2014-11-18 17:36:38 -060040 */
41class FibUpdater : noncopyable
42{
43public:
44 class Error : public std::runtime_error
45 {
46 public:
Davide Pesavento1b077f62019-02-19 19:19:44 -050047 using std::runtime_error::runtime_error;
Vince Lehman76c751c2014-11-18 17:36:38 -060048 };
49
Junxiao Shidf1dc652019-08-30 19:03:19 +000050 using FibUpdateList = std::list<FibUpdate>;
51 using FibUpdateSuccessCallback = std::function<void(RibUpdateList inheritedRoutes)>;
52 using FibUpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>;
Vince Lehman76c751c2014-11-18 17:36:38 -060053
54 FibUpdater(Rib& rib, ndn::nfd::Controller& controller);
55
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040056#ifdef NFD_WITH_TESTS
57 virtual
Junxiao Shidf1dc652019-08-30 19:03:19 +000058 ~FibUpdater() = default;
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040059#endif
Junxiao Shidf1dc652019-08-30 19:03:19 +000060
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040061 /** \brief Computes FibUpdates using the provided RibUpdateBatch and then sends the
62 * updates to NFD's FIB.
Vince Lehman76c751c2014-11-18 17:36:38 -060063 *
64 * \note Caller must guarantee that the previous batch has either succeeded or failed
65 * before calling this method
66 */
67 void
68 computeAndSendFibUpdates(const RibUpdateBatch& batch,
69 const FibUpdateSuccessCallback& onSuccess,
70 const FibUpdateFailureCallback& onFailure);
71
Junxiao Shidf1dc652019-08-30 19:03:19 +000072private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040073 /**
74 * \brief Determines the type of action that will be performed on the RIB and calls the
75 * corresponding computation method.
76 */
Vince Lehman76c751c2014-11-18 17:36:38 -060077 void
78 computeUpdates(const RibUpdateBatch& batch);
79
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040080 /**
81 * \brief Sends the passed updates to NFD.
82 *
83 * onSuccess or onFailure will be called based on the results in
84 * onUpdateSuccess or onUpdateFailure.
85 *
86 * \see FibUpdater::onUpdateSuccess
87 * \see FibUpdater::onUpdateFailure
88 */
Vince Lehman76c751c2014-11-18 17:36:38 -060089 void
90 sendUpdates(const FibUpdateList& updates,
91 const FibUpdateSuccessCallback& onSuccess,
92 const FibUpdateFailureCallback& onFailure);
93
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040094 /**
95 * \brief Sends the updates in m_updatesForBatchFaceId to NFD if any exist,
96 * otherwise calls FibUpdater::sendUpdatesForNonBatchFaceId.
97 */
Vince Lehman76c751c2014-11-18 17:36:38 -060098 void
99 sendUpdatesForBatchFaceId(const FibUpdateSuccessCallback& onSuccess,
100 const FibUpdateFailureCallback& onFailure);
101
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400102 /**
103 * \brief Sends the updates in m_updatesForNonBatchFaceId to NFD if any exist,
104 * otherwise calls onSuccess.
105 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600106 void
107 sendUpdatesForNonBatchFaceId(const FibUpdateSuccessCallback& onSuccess,
108 const FibUpdateFailureCallback& onFailure);
109
Davide Pesavento264af772021-02-09 21:48:24 -0500110NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400111 /**
112 * \brief Sends a FibAddNextHopCommand to NFD using the parameters supplied by
113 * the passed update.
114 * \param nTimeouts the number of times this FibUpdate has failed due to timeout
115 */
Davide Pesavento264af772021-02-09 21:48:24 -0500116 NFD_VIRTUAL_WITH_TESTS void
Vince Lehman76c751c2014-11-18 17:36:38 -0600117 sendAddNextHopUpdate(const FibUpdate& update,
118 const FibUpdateSuccessCallback& onSuccess,
119 const FibUpdateFailureCallback& onFailure,
120 uint32_t nTimeouts = 0);
121
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400122 /**
123 * \brief Sends a FibRemoveNextHopCommand to NFD using the parameters supplied by
124 * the passed update.
125 * \param nTimeouts the number of times this FibUpdate has failed due to timeout
126 */
Davide Pesavento264af772021-02-09 21:48:24 -0500127 NFD_VIRTUAL_WITH_TESTS void
Vince Lehman76c751c2014-11-18 17:36:38 -0600128 sendRemoveNextHopUpdate(const FibUpdate& update,
129 const FibUpdateSuccessCallback& onSuccess,
130 const FibUpdateFailureCallback& onFailure,
131 uint32_t nTimeouts = 0);
132
133private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400134 /**
135 * \brief Calculates the FibUpdates generated by a RIB registration.
136 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600137 void
138 computeUpdatesForRegistration(const RibUpdate& update);
139
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400140 /**
141 * \brief Calculates the FibUpdates generated by a RIB unregistration.
142 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600143 void
144 computeUpdatesForUnregistration(const RibUpdate& update);
145
Davide Pesavento264af772021-02-09 21:48:24 -0500146NFD_PROTECTED_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400147 /**
148 * \brief Callback used by NfdController when a FibAddNextHopCommand or FibRemoveNextHopCommand
149 * is successful.
150 *
151 * If the update has the same Face ID as the batch being processed, the update is
152 * removed from m_updatesForBatchFaceId. If m_updatesForBatchFaceId becomes empty,
153 * the updates with a different Face ID than the batch are sent to NFD.
154 *
155 * If the update has a different Face ID than the batch being processed, the update is
156 * removed from m_updatesForBatchNonFaceId. If m_updatesForBatchNonFaceId becomes empty,
157 * the FIB update process is considered a success.
158 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600159 void
Davide Pesavento412c9822021-07-02 00:21:05 -0400160 onUpdateSuccess(const FibUpdate& update,
Vince Lehman76c751c2014-11-18 17:36:38 -0600161 const FibUpdateSuccessCallback& onSuccess,
162 const FibUpdateFailureCallback& onFailure);
163
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400164 /**
165 * \brief Callback used by NfdController when a FibAddNextHopCommand or FibRemoveNextHopCommand
166 * is successful.
167 *
168 * If the update has not reached the max number of timeouts allowed, the update
169 * is retried.
170 *
171 * If the update failed due to a non-existent face and the update has the same Face ID
172 * as the update batch, the FIB update process fails.
173 *
174 * If the update failed due to a non-existent face and the update has a different
175 * face than the update batch, the update is not retried and the error is
176 * ignored.
177 *
178 * Otherwise, a non-recoverable error has occurred and an exception is thrown.
179 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600180 void
Davide Pesavento412c9822021-07-02 00:21:05 -0400181 onUpdateError(const FibUpdate& update,
Vince Lehman76c751c2014-11-18 17:36:38 -0600182 const FibUpdateSuccessCallback& onSuccess,
183 const FibUpdateFailureCallback& onFailure,
Junxiao Shi29b41282016-08-22 03:47:02 +0000184 const ndn::nfd::ControlResponse& response, uint32_t nTimeouts);
Vince Lehman76c751c2014-11-18 17:36:38 -0600185
186private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400187 /**
188 * \brief Adds the update to an update list based on its Face ID.
189 *
190 * If the update has the same Face ID as the update batch, the update is added
191 * to m_updatesForBatchFaceId.
192 *
193 * Otherwise, the update is added to m_updatesForBatchNonFaceId.
194 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600195 void
Davide Pesavento412c9822021-07-02 00:21:05 -0400196 addFibUpdate(const FibUpdate& update);
Vince Lehman76c751c2014-11-18 17:36:38 -0600197
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400198 /**
199 * \brief Creates records of the passed routes added to the entry and creates FIB updates.
200 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600201 void
202 addInheritedRoutes(const RibEntry& entry, const Rib::RouteSet& routesToAdd);
203
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400204 /**
205 * \brief Creates records of the passed routes added to the name and creates FIB updates.
206 * Routes in \p routesToAdd with the same Face ID as ignore will be not be considered.
207 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600208 void
209 addInheritedRoutes(const Name& name, const Rib::RouteSet& routesToAdd, const Route& ignore);
210
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400211 /**
212 * \brief Creates records of the passed routes removed from the entry and creates FIB updates.
213 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600214 void
215 removeInheritedRoutes(const RibEntry& entry, const Rib::RouteSet& routesToRemove);
216
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400217 /**
218 * \brief Calculates updates for a name that will create a new RIB entry.
219 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600220 void
221 createFibUpdatesForNewRibEntry(const Name& name, const Route& route,
222 const Rib::RibEntryList& children);
223
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400224 /**
225 * \brief Calculates updates for a new route added to a RIB entry.
226 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600227 void
228 createFibUpdatesForNewRoute(const RibEntry& entry, const Route& route,
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400229 bool captureWasTurnedOn);
Vince Lehman76c751c2014-11-18 17:36:38 -0600230
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400231 /**
232 * \brief Calculates updates for changes to an existing route for a RIB entry.
233 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600234 void
235 createFibUpdatesForUpdatedRoute(const RibEntry& entry, const Route& route,
236 const Route& existingRoute);
237
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400238 /**
239 * \brief Calculates updates for a an existing route removed from a RIB entry.
240 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600241 void
242 createFibUpdatesForErasedRoute(const RibEntry& entry, const Route& route,
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400243 bool captureWasTurnedOff);
Vince Lehman76c751c2014-11-18 17:36:38 -0600244
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400245 /**
246 * \brief Calculates updates for an entry that will be removed from the RIB.
247 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600248 void
249 createFibUpdatesForErasedRibEntry(const RibEntry& entry);
250
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400251 /**
252 * \brief Adds and removes passed routes to children's inherited routes.
253 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600254 void
255 modifyChildrensInheritedRoutes(const Rib::RibEntryList& children,
256 const Rib::RouteSet& routesToAdd,
257 const Rib::RouteSet& routesToRemove);
258
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400259 /**
260 * \brief Traverses the entry's children adding and removing the passed routes.
261 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600262 void
263 traverseSubTree(const RibEntry& entry, Rib::RouteSet routesToAdd, Rib::RouteSet routesToRemove);
264
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400265 /**
266 * \brief Creates a record of a calculated inherited route that should be added to the entry.
267 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600268 void
269 addInheritedRoute(const Name& name, const Route& route);
270
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400271 /**
272 * \brief Creates a record of an existing inherited route that should be removed from the entry.
273 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600274 void
275 removeInheritedRoute(const Name& name, const Route& route);
276
277private:
278 const Rib& m_rib;
279 ndn::nfd::Controller& m_controller;
280 uint64_t m_batchFaceId;
281
Davide Pesavento264af772021-02-09 21:48:24 -0500282NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman76c751c2014-11-18 17:36:38 -0600283 FibUpdateList m_updatesForBatchFaceId;
284 FibUpdateList m_updatesForNonBatchFaceId;
285
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400286 /**
287 * \brief List of inherited routes generated during FIB update calculation;
288 * passed to the RIB when updates are completed successfully.
Vince Lehman76c751c2014-11-18 17:36:38 -0600289 */
290 RibUpdateList m_inheritedRoutes;
Vince Lehman76c751c2014-11-18 17:36:38 -0600291};
292
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400293} // namespace nfd::rib
Vince Lehman76c751c2014-11-18 17:36:38 -0600294
Davide Pesavento1b077f62019-02-19 19:19:44 -0500295#endif // NFD_DAEMON_RIB_FIB_UPDATER_HPP