blob: a2539b8852b5909ceb4c9f2746e8411af138acc2 [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/*
3 * Copyright (c) 2014-2018, 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
26#ifndef NFD_RIB_RIB_HPP
27#define NFD_RIB_RIB_HPP
28
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
34namespace nfd {
35namespace rib {
36
Vince Lehman76c751c2014-11-18 17:36:38 -060037using ndn::nfd::ControlParameters;
38
39class FibUpdater;
40
Nick Gordon89c4cca2016-11-02 15:42:32 +000041 /** \brief references a route
42 */
43struct RibRouteRef
44{
45 shared_ptr<RibEntry> entry;
46 RibEntry::const_iterator route;
47};
48
Junxiao Shi89c0ea02017-03-06 19:52:05 +000049bool
50operator<(const RibRouteRef& lhs, const RibRouteRef& rhs);
51
Nick Gordon89c4cca2016-11-02 15:42:32 +000052/** \brief represents the Routing Information Base
53
54 The Routing Information Base contains a collection of Routes, each
55 represents a piece of static or dynamic routing information
56 registered by applications, operators, or NFD itself. Routes
57 associated with the same namespace are collected into a RIB entry.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070058 */
Alexander Afanasyev20d31442014-04-19 17:00:53 -070059class Rib : noncopyable
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070060{
61public:
Vince Lehman218be0a2015-01-15 17:25:20 -060062 typedef std::list<shared_ptr<RibEntry>> RibEntryList;
63 typedef std::map<Name, shared_ptr<RibEntry>> RibTable;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070064 typedef RibTable::const_iterator const_iterator;
Vince Lehman218be0a2015-01-15 17:25:20 -060065 typedef std::map<uint64_t, std::list<shared_ptr<RibEntry>>> FaceLookupTable;
66 typedef bool (*RouteComparePredicate)(const Route&, const Route&);
67 typedef std::set<Route, RouteComparePredicate> RouteSet;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070068
69 Rib();
70
Vince Lehman76c751c2014-11-18 17:36:38 -060071 ~Rib();
72
73 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
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070082 const_iterator
83 begin() const;
84
85 const_iterator
86 end() const;
87
88 size_t
89 size() const;
90
Vince12e49462014-06-09 13:29:32 -050091 bool
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070092 empty() const;
93
Vince12e49462014-06-09 13:29:32 -050094 shared_ptr<RibEntry>
95 findParent(const Name& prefix) const;
96
97 /** \brief finds namespaces under the passed prefix
98 * \return{ a list of entries which are under the passed prefix }
99 */
Vince Lehman218be0a2015-01-15 17:25:20 -0600100 std::list<shared_ptr<RibEntry>>
Vince12e49462014-06-09 13:29:32 -0500101 findDescendants(const Name& prefix) const;
102
Vince Lehman76c751c2014-11-18 17:36:38 -0600103 /** \brief finds namespaces under the passed prefix
104 *
105 * \note Unlike findDescendants, needs to find where prefix would fit in tree
106 * before collecting list of descendant prefixes
107 *
108 * \return{ a list of entries which would be under the passed prefix if the prefix
109 * existed in the RIB }
110 */
111 std::list<shared_ptr<RibEntry>>
112 findDescendantsForNonInsertedName(const Name& prefix) const;
113
114public:
Davide Pesavento87fc0f82018-04-11 23:43:51 -0400115 using UpdateSuccessCallback = std::function<void()>;
116 using UpdateFailureCallback = std::function<void(uint32_t code, const std::string& error)>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600117
118 /** \brief passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
119 *
120 * If the FIB is updated successfully, onFibUpdateSuccess() will be called, and the
121 * RIB will be updated
122 *
123 * If the FIB update fails, onFibUpdateFailure() will be called, and the RIB will not
124 * be updated.
125 */
126 void
127 beginApplyUpdate(const RibUpdate& update,
128 const UpdateSuccessCallback& onSuccess,
129 const UpdateFailureCallback& onFailure);
130
131 /** \brief starts the FIB update process when a face has been destroyed
132 */
133 void
134 beginRemoveFace(uint64_t faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500135
136 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600137 onFibUpdateSuccess(const RibUpdateBatch& batch,
138 const RibUpdateList& inheritedRoutes,
139 const Rib::UpdateSuccessCallback& onSuccess);
140
141 void
142 onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure,
143 uint32_t code, const std::string& error);
144
145 void
146 onRouteExpiration(const Name& prefix, const Route& route);
147
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000148 void
149 insert(const Name& prefix, const Route& route);
150
Vince Lehman76c751c2014-11-18 17:36:38 -0600151private:
152 /** \brief adds the passed update to a RibUpdateBatch and adds the batch to
153 * the end of the update queue.
154 *
155 * If an update is not in progress, the front update batch in the queue will be
156 * processed by the RIB.
157 *
158 * If an update is in progress, the added update will eventually be processed
159 * when it reaches the front of the queue; after other update batches are
160 * processed, the queue is advanced.
161 */
162 void
163 addUpdateToQueue(const RibUpdate& update,
164 const Rib::UpdateSuccessCallback& onSuccess,
165 const Rib::UpdateFailureCallback& onFailure);
166
167 /** \brief Attempts to send the front update batch in the queue.
168 *
169 * If an update is not in progress, the front update batch in the queue will be
170 * sent to the RIB for processing.
171 *
172 * If an update is in progress, nothing will be done.
173 */
174 void
175 sendBatchFromQueue();
176
177PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Junxiao Shi52009042018-09-10 12:33:56 +0000178#ifdef WITH_TESTS
179 /** \brief In unit tests, mock FIB update result.
180 *
181 * If the callback is not nullptr, sendBatchFromQueue() immediately succeeds or fails according
182 * to the return value of callback function.
183 */
184 std::function<bool(const RibUpdateBatch&)> mockFibResponse;
185
186 bool wantMockFibResponseOnce = false; ///< if true, mockFibResponse is cleared after every use.
187#endif
Vince Lehman76c751c2014-11-18 17:36:38 -0600188
Vince Lehman76c751c2014-11-18 17:36:38 -0600189 void
190 erase(const Name& prefix, const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500191
192private:
Vince12e49462014-06-09 13:29:32 -0500193 RibTable::iterator
194 eraseEntry(RibTable::iterator it);
195
196 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600197 updateRib(const RibUpdateBatch& batch);
Vince Lehman4387e782014-06-19 16:57:45 -0500198
Vince Lehman76c751c2014-11-18 17:36:38 -0600199 /** \brief returns routes inherited from the entry's ancestors
200 *
201 * \return{ a list of inherited routes }
202 */
Vince Lehman218be0a2015-01-15 17:25:20 -0600203 RouteSet
204 getAncestorRoutes(const RibEntry& entry) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500205
Vince Lehman76c751c2014-11-18 17:36:38 -0600206 /** \brief returns routes inherited from the parent of the name and the parent's ancestors
207 *
208 * \note A parent is first found for the passed name before inherited routes are collected
209 *
210 * \return{ a list of inherited routes }
211 */
212 RouteSet
213 getAncestorRoutes(const Name& name) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500214
Vince Lehman76c751c2014-11-18 17:36:38 -0600215 /** \brief applies the passed inheritedRoutes and their actions to the corresponding RibEntries'
216 * inheritedRoutes lists
Vince Lehman4387e782014-06-19 16:57:45 -0500217 */
218 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600219 modifyInheritedRoutes(const RibUpdateList& inheritedRoutes);
Vince Lehman4387e782014-06-19 16:57:45 -0500220
Vince Lehman76c751c2014-11-18 17:36:38 -0600221PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400222 using NameAndRoute = std::pair<const Name&, const Route&>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600223
224 std::list<NameAndRoute>
225 findRoutesWithFaceId(uint64_t faceId);
Vince12e49462014-06-09 13:29:32 -0500226
Yanbiao Lic17de832014-11-21 17:51:45 -0800227public:
Nick Gordon89c4cca2016-11-02 15:42:32 +0000228 /** \brief signals after a RIB entry is inserted
229 *
230 * A RIB entry is inserted when the first route associated with a
231 * certain namespace is added.
232 */
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800233 ndn::util::signal::Signal<Rib, Name> afterInsertEntry;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000234
235 /** \brief signals after a RIB entry is erased
236 *
237 * A RIB entry is erased when the last route associated with a
238 * certain namespace is removed.
239 */
240
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800241 ndn::util::signal::Signal<Rib, Name> afterEraseEntry;
Yanbiao Lic17de832014-11-21 17:51:45 -0800242
Nick Gordon89c4cca2016-11-02 15:42:32 +0000243 /** \brief signals after a Route is added
244 */
245 ndn::util::signal::Signal<Rib, RibRouteRef> afterAddRoute;
246
247 /** \brief signals before a route is removed
248 */
249 ndn::util::signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
250
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700251private:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252 RibTable m_rib;
Vince12e49462014-06-09 13:29:32 -0500253 FaceLookupTable m_faceMap;
Vince Lehman76c751c2014-11-18 17:36:38 -0600254 FibUpdater* m_fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500255
Vince12e49462014-06-09 13:29:32 -0500256 size_t m_nItems;
Vince Lehman76c751c2014-11-18 17:36:38 -0600257
258 friend class FibUpdater;
259
260private:
261 struct UpdateQueueItem
262 {
263 RibUpdateBatch batch;
264 const Rib::UpdateSuccessCallback managerSuccessCallback;
265 const Rib::UpdateFailureCallback managerFailureCallback;
266 };
267
268PUBLIC_WITH_TESTS_ELSE_PRIVATE:
269 typedef std::list<UpdateQueueItem> UpdateQueue;
270 UpdateQueue m_updateBatches;
271
272private:
273 bool m_isUpdateInProgress;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700274};
275
276inline Rib::const_iterator
277Rib::begin() const
278{
279 return m_rib.begin();
280}
281
282inline Rib::const_iterator
283Rib::end() const
284{
285 return m_rib.end();
286}
287
288inline size_t
289Rib::size() const
290{
Vince12e49462014-06-09 13:29:32 -0500291 return m_nItems;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700292}
293
Vince12e49462014-06-09 13:29:32 -0500294inline bool
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700295Rib::empty() const
296{
297 return m_rib.empty();
298}
299
Vince12e49462014-06-09 13:29:32 -0500300std::ostream&
301operator<<(std::ostream& os, const Rib& rib);
302
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700303} // namespace rib
304} // namespace nfd
305
306#endif // NFD_RIB_RIB_HPP