blob: 5beea2df754b182dbb68822b2ffdd93dcd85f49a [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
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040041/** \brief references a route
42 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000043struct 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 void
72 setFibUpdater(FibUpdater* updater);
73
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070074 const_iterator
Vince12e49462014-06-09 13:29:32 -050075 find(const Name& prefix) const;
76
Vince Lehman218be0a2015-01-15 17:25:20 -060077 Route*
78 find(const Name& prefix, const Route& route) const;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070079
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070080 const_iterator
81 begin() const;
82
83 const_iterator
84 end() const;
85
86 size_t
87 size() const;
88
Vince12e49462014-06-09 13:29:32 -050089 bool
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070090 empty() const;
91
Vince12e49462014-06-09 13:29:32 -050092 shared_ptr<RibEntry>
93 findParent(const Name& prefix) const;
94
95 /** \brief finds namespaces under the passed prefix
96 * \return{ a list of entries which are under the passed prefix }
97 */
Vince Lehman218be0a2015-01-15 17:25:20 -060098 std::list<shared_ptr<RibEntry>>
Vince12e49462014-06-09 13:29:32 -050099 findDescendants(const Name& prefix) const;
100
Vince Lehman76c751c2014-11-18 17:36:38 -0600101 /** \brief finds namespaces under the passed prefix
102 *
103 * \note Unlike findDescendants, needs to find where prefix would fit in tree
104 * before collecting list of descendant prefixes
105 *
106 * \return{ a list of entries which would be under the passed prefix if the prefix
107 * existed in the RIB }
108 */
109 std::list<shared_ptr<RibEntry>>
110 findDescendantsForNonInsertedName(const Name& prefix) const;
111
112public:
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
116 /** \brief passes the provided RibUpdateBatch to FibUpdater to calculate and send FibUpdates.
117 *
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
129 /** \brief starts the FIB update process when a face has been destroyed
130 */
131 void
132 beginRemoveFace(uint64_t faceId);
Vince Lehman4387e782014-06-19 16:57:45 -0500133
134 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600135 onFibUpdateSuccess(const RibUpdateBatch& batch,
136 const RibUpdateList& inheritedRoutes,
137 const Rib::UpdateSuccessCallback& onSuccess);
138
139 void
140 onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure,
141 uint32_t code, const std::string& error);
142
143 void
144 onRouteExpiration(const Name& prefix, const Route& route);
145
Junxiao Shi9f5b01d2016-08-05 03:54:28 +0000146 void
147 insert(const Name& prefix, const Route& route);
148
Vince Lehman76c751c2014-11-18 17:36:38 -0600149private:
150 /** \brief adds the passed update to a RibUpdateBatch and adds the batch to
151 * the end of the update queue.
152 *
153 * If an update is not in progress, the front update batch in the queue will be
154 * processed by the RIB.
155 *
156 * If an update is in progress, the added update will eventually be processed
157 * when it reaches the front of the queue; after other update batches are
158 * processed, the queue is advanced.
159 */
160 void
161 addUpdateToQueue(const RibUpdate& update,
162 const Rib::UpdateSuccessCallback& onSuccess,
163 const Rib::UpdateFailureCallback& onFailure);
164
165 /** \brief Attempts to send the front update batch in the queue.
166 *
167 * If an update is not in progress, the front update batch in the queue will be
168 * sent to the RIB for processing.
169 *
170 * If an update is in progress, nothing will be done.
171 */
172 void
173 sendBatchFromQueue();
174
175PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Junxiao Shi52009042018-09-10 12:33:56 +0000176#ifdef WITH_TESTS
177 /** \brief In unit tests, mock FIB update result.
178 *
179 * If the callback is not nullptr, sendBatchFromQueue() immediately succeeds or fails according
180 * to the return value of callback function.
181 */
182 std::function<bool(const RibUpdateBatch&)> mockFibResponse;
183
184 bool wantMockFibResponseOnce = false; ///< if true, mockFibResponse is cleared after every use.
185#endif
Vince Lehman76c751c2014-11-18 17:36:38 -0600186
Vince Lehman76c751c2014-11-18 17:36:38 -0600187 void
188 erase(const Name& prefix, const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500189
190private:
Vince12e49462014-06-09 13:29:32 -0500191 RibTable::iterator
192 eraseEntry(RibTable::iterator it);
193
194 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600195 updateRib(const RibUpdateBatch& batch);
Vince Lehman4387e782014-06-19 16:57:45 -0500196
Vince Lehman76c751c2014-11-18 17:36:38 -0600197 /** \brief returns routes inherited from the entry's ancestors
198 *
199 * \return{ a list of inherited routes }
200 */
Vince Lehman218be0a2015-01-15 17:25:20 -0600201 RouteSet
202 getAncestorRoutes(const RibEntry& entry) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500203
Vince Lehman76c751c2014-11-18 17:36:38 -0600204 /** \brief returns routes inherited from the parent of the name and the parent's ancestors
205 *
206 * \note A parent is first found for the passed name before inherited routes are collected
207 *
208 * \return{ a list of inherited routes }
209 */
210 RouteSet
211 getAncestorRoutes(const Name& name) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500212
Vince Lehman76c751c2014-11-18 17:36:38 -0600213 /** \brief applies the passed inheritedRoutes and their actions to the corresponding RibEntries'
214 * inheritedRoutes lists
Vince Lehman4387e782014-06-19 16:57:45 -0500215 */
216 void
Vince Lehman76c751c2014-11-18 17:36:38 -0600217 modifyInheritedRoutes(const RibUpdateList& inheritedRoutes);
Vince Lehman4387e782014-06-19 16:57:45 -0500218
Vince Lehman76c751c2014-11-18 17:36:38 -0600219PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400220 using NameAndRoute = std::pair<const Name&, const Route&>;
Vince Lehman76c751c2014-11-18 17:36:38 -0600221
222 std::list<NameAndRoute>
223 findRoutesWithFaceId(uint64_t faceId);
Vince12e49462014-06-09 13:29:32 -0500224
Yanbiao Lic17de832014-11-21 17:51:45 -0800225public:
Nick Gordon89c4cca2016-11-02 15:42:32 +0000226 /** \brief signals after a RIB entry is inserted
227 *
228 * A RIB entry is inserted when the first route associated with a
229 * certain namespace is added.
230 */
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800231 ndn::util::signal::Signal<Rib, Name> afterInsertEntry;
Nick Gordon89c4cca2016-11-02 15:42:32 +0000232
233 /** \brief signals after a RIB entry is erased
234 *
235 * A RIB entry is erased when the last route associated with a
236 * certain namespace is removed.
237 */
238
Yanbiao Lib9d439d2014-12-11 16:12:32 -0800239 ndn::util::signal::Signal<Rib, Name> afterEraseEntry;
Yanbiao Lic17de832014-11-21 17:51:45 -0800240
Nick Gordon89c4cca2016-11-02 15:42:32 +0000241 /** \brief signals after a Route is added
242 */
243 ndn::util::signal::Signal<Rib, RibRouteRef> afterAddRoute;
244
245 /** \brief signals before a route is removed
246 */
247 ndn::util::signal::Signal<Rib, RibRouteRef> beforeRemoveRoute;
248
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700249private:
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700250 RibTable m_rib;
Vince12e49462014-06-09 13:29:32 -0500251 FaceLookupTable m_faceMap;
Vince Lehman76c751c2014-11-18 17:36:38 -0600252 FibUpdater* m_fibUpdater;
Vince Lehman4387e782014-06-19 16:57:45 -0500253
Vince12e49462014-06-09 13:29:32 -0500254 size_t m_nItems;
Vince Lehman76c751c2014-11-18 17:36:38 -0600255
256 friend class FibUpdater;
257
258private:
259 struct UpdateQueueItem
260 {
261 RibUpdateBatch batch;
262 const Rib::UpdateSuccessCallback managerSuccessCallback;
263 const Rib::UpdateFailureCallback managerFailureCallback;
264 };
265
266PUBLIC_WITH_TESTS_ELSE_PRIVATE:
267 typedef std::list<UpdateQueueItem> UpdateQueue;
268 UpdateQueue m_updateBatches;
269
270private:
271 bool m_isUpdateInProgress;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700272};
273
274inline Rib::const_iterator
275Rib::begin() const
276{
277 return m_rib.begin();
278}
279
280inline Rib::const_iterator
281Rib::end() const
282{
283 return m_rib.end();
284}
285
286inline size_t
287Rib::size() const
288{
Vince12e49462014-06-09 13:29:32 -0500289 return m_nItems;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700290}
291
Vince12e49462014-06-09 13:29:32 -0500292inline bool
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700293Rib::empty() const
294{
295 return m_rib.empty();
296}
297
Vince12e49462014-06-09 13:29:32 -0500298std::ostream&
299operator<<(std::ostream& os, const Rib& rib);
300
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700301} // namespace rib
302} // namespace nfd
303
304#endif // NFD_RIB_RIB_HPP