blob: ddfa59196416bf458b61aa5d7bad66395efa8c36 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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 Pesaventoaa9e3b22022-10-21 17:00:07 -040040/** \brief References a route.
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040041 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000042struct RibRouteRef
43{
44 shared_ptr<RibEntry> entry;
45 RibEntry::const_iterator route;
46};
47
Junxiao Shi89c0ea02017-03-06 19:52:05 +000048bool
49operator<(const RibRouteRef& lhs, const RibRouteRef& rhs);
50
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040051/**
52 * \brief Represents the Routing Information Base.
53 *
54 * The Routing Information Base (RIB) contains a collection of Route objects,
55 * each representing a piece of static or dynamic routing information registered
56 * by applications, operators, or NFD itself. Routes associated with the same
57 * namespace are collected into a RIB entry.
58 *
59 * \sa RibEntry
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070060 */
Alexander Afanasyev20d31442014-04-19 17:00:53 -070061class Rib : noncopyable
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070062{
63public:
Junxiao Shidf1dc652019-08-30 19:03:19 +000064 using RibEntryList = std::list<shared_ptr<RibEntry>>;
65 using RibTable = std::map<Name, shared_ptr<RibEntry>>;
66 using const_iterator = RibTable::const_iterator;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070067
Vince Lehman76c751c2014-11-18 17:36:38 -060068 void
69 setFibUpdater(FibUpdater* updater);
70
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070071 const_iterator
Vince12e49462014-06-09 13:29:32 -050072 find(const Name& prefix) const;
73
Vince Lehman218be0a2015-01-15 17:25:20 -060074 Route*
75 find(const Name& prefix, const Route& route) const;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070076
Teng Lianga4e6ec32018-10-21 09:25:00 -070077 Route*
78 findLongestPrefix(const Name& prefix, const Route& route) const;
79
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070080 const_iterator
Junxiao Shidf1dc652019-08-30 19:03:19 +000081 begin() const
82 {
83 return m_rib.begin();
84 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070085
86 const_iterator
Junxiao Shidf1dc652019-08-30 19:03:19 +000087 end() const
88 {
89 return m_rib.end();
90 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070091
92 size_t
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040093 size() const noexcept
Junxiao Shidf1dc652019-08-30 19:03:19 +000094 {
95 return m_nItems;
96 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070097
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040098 [[nodiscard]] bool
99 empty() const noexcept
Junxiao Shidf1dc652019-08-30 19:03:19 +0000100 {
101 return m_rib.empty();
102 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700103
Vince12e49462014-06-09 13:29:32 -0500104 shared_ptr<RibEntry>
105 findParent(const Name& prefix) const;
106
Vince Lehman76c751c2014-11-18 17:36:38 -0600107public:
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400108 using UpdateSuccessCallback = std::function<void()>;
109 using UpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600110
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400111 /** \brief Passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
Vince Lehman76c751c2014-11-18 17:36:38 -0600112 *
113 * If the FIB is updated successfully, onFibUpdateSuccess() will be called, and the
114 * RIB will be updated
115 *
116 * If the FIB update fails, onFibUpdateFailure() will be called, and the RIB will not
117 * be updated.
118 */
119 void
120 beginApplyUpdate(const RibUpdate& update,
121 const UpdateSuccessCallback& onSuccess,
122 const UpdateFailureCallback& onFailure);
123
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400124 /** \brief Starts the FIB update process when a face has been destroyed.
Vince Lehman76c751c2014-11-18 17:36:38 -0600125 */
126 void
127 beginRemoveFace(uint64_t faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500128
129 void
Junxiao Shi17a70012019-06-25 10:50:32 +0000130 beginRemoveFailedFaces(const std::set<uint64_t>& activeFaceIds);
131
132 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600133 onRouteExpiration(const Name& prefix, const Route& route);
134
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000135 void
136 insert(const Name& prefix, const Route& route);
137
Vince Lehman76c751c2014-11-18 17:36:38 -0600138private:
Junxiao Shi17a70012019-06-25 10:50:32 +0000139 void
140 enqueueRemoveFace(const RibEntry& entry, uint64_t faceId);
141
Junxiao Shi51533382019-07-19 13:50:26 +0000142 /** \brief Append the RIB update to the update queue.
143 *
144 * To start updates, invoke sendBatchFromQueue() .
145 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600146 void
147 addUpdateToQueue(const RibUpdate& update,
148 const Rib::UpdateSuccessCallback& onSuccess,
149 const Rib::UpdateFailureCallback& onFailure);
150
Junxiao Shi51533382019-07-19 13:50:26 +0000151 /** \brief Send the first update batch in the queue, if no other update is in progress.
152 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600153 void
154 sendBatchFromQueue();
155
Junxiao Shi51533382019-07-19 13:50:26 +0000156 void
157 onFibUpdateSuccess(const RibUpdateBatch& batch,
158 const RibUpdateList& inheritedRoutes,
159 const Rib::UpdateSuccessCallback& onSuccess);
160
161 void
162 onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure,
163 uint32_t code, const std::string& error);
164
Davide Pesavento264af772021-02-09 21:48:24 -0500165NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Vince Lehman76c751c2014-11-18 17:36:38 -0600166 void
167 erase(const Name& prefix, const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500168
169private:
Junxiao Shidf1dc652019-08-30 19:03:19 +0000170 using RouteComparePredicate = bool (*)(const Route&, const Route&);
171 using RouteSet = std::set<Route, RouteComparePredicate>;
172
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400173 /** \brief Find entries under \p prefix.
Junxiao Shi51533382019-07-19 13:50:26 +0000174 * \pre a RIB entry exists at \p prefix
175 */
176 std::list<shared_ptr<RibEntry>>
177 findDescendants(const Name& prefix) const;
178
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400179 /** \brief Find entries under \p prefix.
Junxiao Shi51533382019-07-19 13:50:26 +0000180 * \pre a RIB entry does not exist at \p prefix
181 */
182 std::list<shared_ptr<RibEntry>>
183 findDescendantsForNonInsertedName(const Name& prefix) const;
184
Vince12e49462014-06-09 13:29:32 -0500185 RibTable::iterator
186 eraseEntry(RibTable::iterator it);
187
188 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600189 updateRib(const RibUpdateBatch& batch);
Vince Lehman4387e782014-06-19 16:57:45 -0500190
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400191 /** \brief Returns routes inherited from the entry's ancestors.
192 * \return a list of inherited routes
Vince Lehman76c751c2014-11-18 17:36:38 -0600193 */
Vince Lehman218be0a2015-01-15 17:25:20 -0600194 RouteSet
195 getAncestorRoutes(const RibEntry& entry) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500196
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400197 /** \brief Returns routes inherited from the parent of the name and the parent's ancestors.
Vince Lehman76c751c2014-11-18 17:36:38 -0600198 * \note A parent is first found for the passed name before inherited routes are collected
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400199 * \return a list of inherited routes
Vince Lehman76c751c2014-11-18 17:36:38 -0600200 */
201 RouteSet
202 getAncestorRoutes(const Name& name) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500203
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400204 /** \brief Applies the passed \p inheritedRoutes and their actions to the corresponding
205 * RibEntries' inheritedRoutes lists.
Vince Lehman4387e782014-06-19 16:57:45 -0500206 */
207 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600208 modifyInheritedRoutes(const RibUpdateList& inheritedRoutes);
Vince Lehman4387e782014-06-19 16:57:45 -0500209
Yanbiao Lic17de832014-11-21 17:51:45 -0800210public:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400211 /** \brief Signals after a RIB entry is inserted.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000212 *
213 * A RIB entry is inserted when the first route associated with a
214 * certain namespace is added.
215 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000216 signal::Signal<Rib, Name> afterInsertEntry;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000217
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400218 /** \brief Signals after a RIB entry is erased.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000219 *
220 * A RIB entry is erased when the last route associated with a
221 * certain namespace is removed.
222 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000223 signal::Signal<Rib, Name> afterEraseEntry;
Yanbiao Lic17de832014-11-21 17:51:45 -0800224
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400225 /** \brief Signals after a Route is added.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000226 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000227 signal::Signal<Rib, RibRouteRef> afterAddRoute;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000228
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400229 /** \brief Signals before a route is removed.
Nick Gordon89c4cca2016-11-02 15:42:32 +0000230 */
Junxiao Shidf1dc652019-08-30 19:03:19 +0000231 signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000232
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700233private:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700234 RibTable m_rib;
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400235 // FaceId => Entry with Route on this face
236 std::multimap<uint64_t, shared_ptr<RibEntry>> m_faceEntries;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000237 size_t m_nItems = 0;
238 FibUpdater* m_fibUpdater = nullptr;
Vince Lehman4387e782014-06-19 16:57:45 -0500239
Vince Lehman76c751c2014-11-18 17:36:38 -0600240 struct UpdateQueueItem
241 {
242 RibUpdateBatch batch;
243 const Rib::UpdateSuccessCallback managerSuccessCallback;
244 const Rib::UpdateFailureCallback managerFailureCallback;
245 };
246
Junxiao Shidf1dc652019-08-30 19:03:19 +0000247 using UpdateQueue = std::list<UpdateQueueItem>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600248 UpdateQueue m_updateBatches;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000249 bool m_isUpdateInProgress = false;
Vince Lehman76c751c2014-11-18 17:36:38 -0600250
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400251 friend FibUpdater;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252};
253
Vince12e49462014-06-09 13:29:32 -0500254std::ostream&
255operator<<(std::ostream& os, const Rib& rib);
256
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400257} // namespace nfd::rib
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700258
Davide Pesavento1b077f62019-02-19 19:19:44 -0500259#endif // NFD_DAEMON_RIB_RIB_HPP