blob: 795222c46b420d560c8aa05a3877feefff19ef1b [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesavento8f0b8b62023-08-29 21:04:08 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070010 *
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/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
Davide Pesavento1b077f62019-02-19 19:19:44 -050026#ifndef NFD_DAEMON_RIB_RIB_HPP
27#define NFD_DAEMON_RIB_RIB_HPP
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070028
Vince Lehman76c751c2014-11-18 17:36:38 -060029#include "rib-entry.hpp"
30#include "rib-update-batch.hpp"
31
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070033
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034namespace nfd::rib {
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070035
Vince Lehman76c751c2014-11-18 17:36:38 -060036using ndn::nfd::ControlParameters;
37
38class FibUpdater;
39
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040040/**
41 * \brief References a route.
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040042 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000043struct RibRouteRef
44{
45 shared_ptr<RibEntry> entry;
46 RibEntry::const_iterator route;
Nick Gordon89c4cca2016-11-02 15:42:32 +000047
Davide Pesavento8f0b8b62023-08-29 21:04:08 -040048 friend bool
49 operator<(const RibRouteRef& lhs, const RibRouteRef& rhs) noexcept
50 {
51 return std::tie(lhs.entry->getName(), lhs.route->faceId, lhs.route->origin) <
52 std::tie(rhs.entry->getName(), rhs.route->faceId, rhs.route->origin);
53 }
54};
Junxiao Shi89c0ea02017-03-06 19:52:05 +000055
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040056/**
57 * \brief Represents the Routing Information Base.
58 *
59 * The Routing Information Base (RIB) contains a collection of Route objects,
60 * each representing a piece of static or dynamic routing information registered
61 * by applications, operators, or NFD itself. Routes associated with the same
62 * namespace are collected into a RIB entry.
63 *
64 * \sa RibEntry
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070065 */
Alexander Afanasyev20d31442014-04-19 17:00:53 -070066class Rib : noncopyable
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067{
68public:
Junxiao Shidf1dc652019-08-30 19:03:19 +000069 using RibEntryList = std::list<shared_ptr<RibEntry>>;
70 using RibTable = std::map<Name, shared_ptr<RibEntry>>;
71 using const_iterator = RibTable::const_iterator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070072
Vince Lehman76c751c2014-11-18 17:36:38 -060073 void
74 setFibUpdater(FibUpdater* updater);
75
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070076 const_iterator
Vince12e49462014-06-09 13:29:32 -050077 find(const Name& prefix) const;
78
Vince Lehman218be0a2015-01-15 17:25:20 -060079 Route*
80 find(const Name& prefix, const Route& route) const;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070081
Teng Lianga4e6ec32018-10-21 09:25:00 -070082 Route*
83 findLongestPrefix(const Name& prefix, const Route& route) const;
84
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070085 const_iterator
Junxiao Shidf1dc652019-08-30 19:03:19 +000086 begin() const
87 {
88 return m_rib.begin();
89 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070090
91 const_iterator
Junxiao Shidf1dc652019-08-30 19:03:19 +000092 end() const
93 {
94 return m_rib.end();
95 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070096
97 size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040098 size() const noexcept
Junxiao Shidf1dc652019-08-30 19:03:19 +000099 {
100 return m_nItems;
101 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700102
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400103 [[nodiscard]] bool
104 empty() const noexcept
Junxiao Shidf1dc652019-08-30 19:03:19 +0000105 {
106 return m_rib.empty();
107 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700108
Vince12e49462014-06-09 13:29:32 -0500109 shared_ptr<RibEntry>
110 findParent(const Name& prefix) const;
111
Vince Lehman76c751c2014-11-18 17:36:38 -0600112public:
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400113 using UpdateSuccessCallback = std::function<void()>;
114 using UpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600115
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400116 /** \brief Passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
Vince Lehman76c751c2014-11-18 17:36:38 -0600117 *
118 * If the FIB is updated successfully, onFibUpdateSuccess() will be called, and the
119 * RIB will be updated
120 *
121 * If the FIB update fails, onFibUpdateFailure() will be called, and the RIB will not
122 * be updated.
123 */
124 void
125 beginApplyUpdate(const RibUpdate& update,
126 const UpdateSuccessCallback& onSuccess,
127 const UpdateFailureCallback& onFailure);
128
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400129 /** \brief Starts the FIB update process when a face has been destroyed.
Vince Lehman76c751c2014-11-18 17:36:38 -0600130 */
131 void
132 beginRemoveFace(uint64_t faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500133
134 void
Junxiao Shi17a70012019-06-25 10:50:32 +0000135 beginRemoveFailedFaces(const std::set<uint64_t>& activeFaceIds);
136
137 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600138 onRouteExpiration(const Name& prefix, const Route& route);
139
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000140 void
141 insert(const Name& prefix, const Route& route);
142
Vince Lehman76c751c2014-11-18 17:36:38 -0600143private:
Junxiao Shi17a70012019-06-25 10:50:32 +0000144 void
145 enqueueRemoveFace(const RibEntry& entry, uint64_t faceId);
146
Junxiao Shi51533382019-07-19 13:50:26 +0000147 /** \brief Append the RIB update to the update queue.
148 *
149 * To start updates, invoke sendBatchFromQueue() .
150 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600151 void
152 addUpdateToQueue(const RibUpdate& update,
153 const Rib::UpdateSuccessCallback& onSuccess,
154 const Rib::UpdateFailureCallback& onFailure);
155
Junxiao Shi51533382019-07-19 13:50:26 +0000156 /** \brief Send the first update batch in the queue, if no other update is in progress.
157 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600158 void
159 sendBatchFromQueue();
160
Junxiao Shi51533382019-07-19 13:50:26 +0000161 void
162 onFibUpdateSuccess(const RibUpdateBatch& batch,
163 const RibUpdateList& inheritedRoutes,
164 const Rib::UpdateSuccessCallback& onSuccess);
165
166 void
167 onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure,
168 uint32_t code, const std::string& error);
169
Davide Pesavento264af772021-02-09 21:48:24 -0500170NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman76c751c2014-11-18 17:36:38 -0600171 void
172 erase(const Name& prefix, const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500173
174private:
Junxiao Shidf1dc652019-08-30 19:03:19 +0000175 using RouteComparePredicate = bool (*)(const Route&, const Route&);
176 using RouteSet = std::set<Route, RouteComparePredicate>;
177
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400178 /** \brief Find entries under \p prefix.
Junxiao Shi51533382019-07-19 13:50:26 +0000179 * \pre a RIB entry exists at \p prefix
180 */
181 std::list<shared_ptr<RibEntry>>
182 findDescendants(const Name& prefix) const;
183
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400184 /** \brief Find entries under \p prefix.
Junxiao Shi51533382019-07-19 13:50:26 +0000185 * \pre a RIB entry does not exist at \p prefix
186 */
187 std::list<shared_ptr<RibEntry>>
188 findDescendantsForNonInsertedName(const Name& prefix) const;
189
Vince12e49462014-06-09 13:29:32 -0500190 RibTable::iterator
191 eraseEntry(RibTable::iterator it);
192
193 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600194 updateRib(const RibUpdateBatch& batch);
Vince Lehman4387e782014-06-19 16:57:45 -0500195
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400196 /** \brief Returns routes inherited from the entry's ancestors.
197 * \return a list of inherited routes
Vince Lehman76c751c2014-11-18 17:36:38 -0600198 */
Vince Lehman218be0a2015-01-15 17:25:20 -0600199 RouteSet
200 getAncestorRoutes(const RibEntry& entry) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500201
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400202 /** \brief Returns routes inherited from the parent of the name and the parent's ancestors.
Vince Lehman76c751c2014-11-18 17:36:38 -0600203 * \note A parent is first found for the passed name before inherited routes are collected
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400204 * \return a list of inherited routes
Vince Lehman76c751c2014-11-18 17:36:38 -0600205 */
206 RouteSet
207 getAncestorRoutes(const Name& name) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500208
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400209 /** \brief Applies the passed \p inheritedRoutes and their actions to the corresponding
210 * RibEntries' inheritedRoutes lists.
Vince Lehman4387e782014-06-19 16:57:45 -0500211 */
212 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600213 modifyInheritedRoutes(const RibUpdateList& inheritedRoutes);
Vince Lehman4387e782014-06-19 16:57:45 -0500214
Yanbiao Lic17de832014-11-21 17:51:45 -0800215public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400216 /** \brief Signals after a RIB entry is inserted.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000217 *
218 * A RIB entry is inserted when the first route associated with a
219 * certain namespace is added.
220 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000221 signal::Signal<Rib, Name> afterInsertEntry;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000222
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400223 /** \brief Signals after a RIB entry is erased.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000224 *
225 * A RIB entry is erased when the last route associated with a
226 * certain namespace is removed.
227 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000228 signal::Signal<Rib, Name> afterEraseEntry;
Yanbiao Lic17de832014-11-21 17:51:45 -0800229
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400230 /** \brief Signals after a Route is added.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000231 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000232 signal::Signal<Rib, RibRouteRef> afterAddRoute;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000233
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400234 /** \brief Signals before a route is removed.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000235 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000236 signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000237
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700238private:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700239 RibTable m_rib;
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400240 // FaceId => Entry with Route on this face
241 std::multimap<uint64_t, shared_ptr<RibEntry>> m_faceEntries;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000242 size_t m_nItems = 0;
243 FibUpdater* m_fibUpdater = nullptr;
Vince Lehman4387e782014-06-19 16:57:45 -0500244
Vince Lehman76c751c2014-11-18 17:36:38 -0600245 struct UpdateQueueItem
246 {
247 RibUpdateBatch batch;
248 const Rib::UpdateSuccessCallback managerSuccessCallback;
249 const Rib::UpdateFailureCallback managerFailureCallback;
250 };
251
Junxiao Shidf1dc652019-08-30 19:03:19 +0000252 using UpdateQueue = std::list<UpdateQueueItem>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600253 UpdateQueue m_updateBatches;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000254 bool m_isUpdateInProgress = false;
Vince Lehman76c751c2014-11-18 17:36:38 -0600255
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400256 friend FibUpdater;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700257};
258
Vince12e49462014-06-09 13:29:32 -0500259std::ostream&
260operator<<(std::ostream& os, const Rib& rib);
261
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400262} // namespace nfd::rib
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700263
Davide Pesavento1b077f62019-02-19 19:19:44 -0500264#endif // NFD_DAEMON_RIB_RIB_HPP