blob: 79f16276822ead6fb9ba1350bcdf20fee0b3670e [file] [log] [blame]
Vince12e49462014-06-09 13:29:32 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc0822fa2018-05-10 21:54:10 -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.
Vince12e49462014-06-09 13:29:32 -050010 *
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_RIB_ENTRY_HPP
27#define NFD_DAEMON_RIB_RIB_ENTRY_HPP
Vince12e49462014-06-09 13:29:32 -050028
Vince Lehman218be0a2015-01-15 17:25:20 -060029#include "route.hpp"
Vince12e49462014-06-09 13:29:32 -050030
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040031#include <list>
32
Vince12e49462014-06-09 13:29:32 -050033namespace nfd {
34namespace rib {
35
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040036/**
37 * \brief Represents a RIB entry, which contains one or more Routes with the same prefix.
Vince12e49462014-06-09 13:29:32 -050038 */
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040039class RibEntry : public std::enable_shared_from_this<RibEntry>
Vince12e49462014-06-09 13:29:32 -050040{
41public:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040042 using RouteList = std::list<Route>;
43 using iterator = RouteList::iterator;
44 using const_iterator = RouteList::const_iterator;
Vince12e49462014-06-09 13:29:32 -050045
46 void
47 setName(const Name& prefix);
48
49 const Name&
50 getName() const;
51
52 shared_ptr<RibEntry>
53 getParent() const;
54
55 bool
56 hasParent() const;
57
58 void
59 addChild(shared_ptr<RibEntry> child);
60
61 void
62 removeChild(shared_ptr<RibEntry> child);
63
Vince Lehman76c751c2014-11-18 17:36:38 -060064 const std::list<shared_ptr<RibEntry>>&
65 getChildren() const;
Vince12e49462014-06-09 13:29:32 -050066
67 bool
68 hasChildren() const;
69
Vince Lehman218be0a2015-01-15 17:25:20 -060070 /** \brief inserts a new route into the entry's route list
71 * If another route already exists with the same faceId and origin,
72 * the new route is not inserted.
Nick Gordon89c4cca2016-11-02 15:42:32 +000073 * \return a pair, whose first element is the iterator to the newly
74 * inserted element if the insert succeeds and to the
75 * previously-existing element otherwise, and whose second element
76 * is true if the insert succeeds and false otherwise.
Vince12e49462014-06-09 13:29:32 -050077 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000078 std::pair<RibEntry::iterator, bool>
Vince Lehman218be0a2015-01-15 17:25:20 -060079 insertRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050080
Vince Lehman218be0a2015-01-15 17:25:20 -060081 /** \brief erases a Route with the same faceId and origin
Vince12e49462014-06-09 13:29:32 -050082 */
Vince Lehman281ded72014-08-21 12:17:08 -050083 void
Vince Lehman218be0a2015-01-15 17:25:20 -060084 eraseRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050085
Vince Lehman218be0a2015-01-15 17:25:20 -060086 /** \brief erases a Route with the passed iterator
Vince12e49462014-06-09 13:29:32 -050087 * \return{ an iterator to the element that followed the erased iterator }
88 */
89 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -060090 eraseRoute(RouteList::iterator route);
Vince12e49462014-06-09 13:29:32 -050091
92 bool
Davide Pesavento412c9822021-07-02 00:21:05 -040093 hasFaceId(uint64_t faceId) const;
Vince12e49462014-06-09 13:29:32 -050094
Davide Pesaventod396b612017-02-20 22:11:50 -050095 const RouteList&
96 getRoutes() const;
Vince12e49462014-06-09 13:29:32 -050097
Vince Lehman76c751c2014-11-18 17:36:38 -060098 size_t
99 getNRoutes() const;
100
Vince12e49462014-06-09 13:29:32 -0500101 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600102 findRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -0500103
Vince Lehman76c751c2014-11-18 17:36:38 -0600104 const_iterator
105 findRoute(const Route& route) const;
106
Vince Lehman4387e782014-06-19 16:57:45 -0500107 bool
Vince Lehman218be0a2015-01-15 17:25:20 -0600108 hasRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500109
110 void
Vince Lehman218be0a2015-01-15 17:25:20 -0600111 addInheritedRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500112
113 void
Vince Lehman218be0a2015-01-15 17:25:20 -0600114 removeInheritedRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500115
Vince Lehman218be0a2015-01-15 17:25:20 -0600116 /** \brief Returns the routes this namespace has inherited.
117 * The inherited routes returned represent inherited routes this namespace has in the FIB.
118 * \return{ routes inherited by this namespace }
Vince Lehman4387e782014-06-19 16:57:45 -0500119 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600120 const RouteList&
121 getInheritedRoutes() const;
Vince Lehman4387e782014-06-19 16:57:45 -0500122
Vince Lehman218be0a2015-01-15 17:25:20 -0600123 /** \brief Finds an inherited route with a matching face ID.
124 * \return{ An iterator to the matching route if one is found;
Vince Lehman4387e782014-06-19 16:57:45 -0500125 * otherwise, an iterator to the end of the entry's
Vince Lehman218be0a2015-01-15 17:25:20 -0600126 * inherited route list }
Vince Lehman4387e782014-06-19 16:57:45 -0500127 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600128 RouteList::const_iterator
129 findInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500130
Vince Lehman218be0a2015-01-15 17:25:20 -0600131 /** \brief Determines if the entry has an inherited route with a matching face ID.
132 * \return{ True, if a matching inherited route is found; otherwise, false. }
Vince Lehman4387e782014-06-19 16:57:45 -0500133 */
134 bool
Vince Lehman76c751c2014-11-18 17:36:38 -0600135 hasInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500136
137 bool
138 hasCapture() const;
139
Vince Lehman218be0a2015-01-15 17:25:20 -0600140 /** \brief Determines if the entry has an inherited route with the passed
Vince Lehman4387e782014-06-19 16:57:45 -0500141 * face ID and its child inherit flag set.
Vince Lehman218be0a2015-01-15 17:25:20 -0600142 * \return{ True, if a matching inherited route is found; otherwise, false. }
Vince Lehman4387e782014-06-19 16:57:45 -0500143 */
144 bool
145 hasChildInheritOnFaceId(uint64_t faceId) const;
146
Vince Lehman218be0a2015-01-15 17:25:20 -0600147 /** \brief Returns the route with the lowest cost that has the passed face ID.
148 * \return{ The route with the lowest cost that has the passed face ID}
Vince Lehman4387e782014-06-19 16:57:45 -0500149 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500150 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600151 getRouteWithLowestCostByFaceId(uint64_t faceId) const;
152
Vince Lehman9dcfc402015-03-26 03:18:54 -0500153 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600154 getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500155
Vince Lehman218be0a2015-01-15 17:25:20 -0600156 /** \brief Returns the route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500157 * and its child inherit flag set.
Vince Lehman218be0a2015-01-15 17:25:20 -0600158 * \return{ The route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500159 * and its child inherit flag set }
160 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500161 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600162 getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500163
Junxiao Shid47cd632018-09-11 03:10:00 +0000164 /** \brief Retrieve a prefix announcement suitable for readvertising this route.
165 *
166 * If one or more routes in this RIB entry contains a prefix announcement, this method returns
167 * the announcement from the route that expires last.
168 *
169 * If this RIB entry does not have a route containing a prefix announcement, this method creates
170 * a new announcement. Its expiration period reflects the remaining lifetime of this RIB entry,
171 * confined within [\p minExpiration, \p maxExpiration] range. The caller is expected to sign
172 * this announcement.
173 *
174 * \warning (minExpiration > maxExpiration) triggers undefined behavior.
175 */
176 ndn::PrefixAnnouncement
177 getPrefixAnnouncement(time::milliseconds minExpiration = 15_s,
178 time::milliseconds maxExpiration = 1_h) const;
179
Vince12e49462014-06-09 13:29:32 -0500180 const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600181 begin() const;
Vince12e49462014-06-09 13:29:32 -0500182
183 const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600184 end() const;
Vince12e49462014-06-09 13:29:32 -0500185
186 iterator
187 begin();
188
189 iterator
190 end();
191
192private:
193 void
194 setParent(shared_ptr<RibEntry> parent);
195
196private:
197 Name m_name;
Vince Lehman218be0a2015-01-15 17:25:20 -0600198 std::list<shared_ptr<RibEntry>> m_children;
Vince12e49462014-06-09 13:29:32 -0500199 shared_ptr<RibEntry> m_parent;
Vince Lehman218be0a2015-01-15 17:25:20 -0600200 RouteList m_routes;
201 RouteList m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500202
Vince Lehman218be0a2015-01-15 17:25:20 -0600203 /** \brief The number of routes on this namespace with the capture flag set.
Vince Lehman4387e782014-06-19 16:57:45 -0500204 *
Vince Lehman218be0a2015-01-15 17:25:20 -0600205 * This count is used to check if the namespace will block inherited routes.
Junxiao Shi2e526d72016-08-22 16:02:13 +0000206 * If the number is greater than zero, a route on the namespace has its capture
Vince Lehman218be0a2015-01-15 17:25:20 -0600207 * flag set which means the namespace should not inherit any routes.
Vince Lehman4387e782014-06-19 16:57:45 -0500208 */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400209 uint64_t m_nRoutesWithCaptureSet = 0;
Vince12e49462014-06-09 13:29:32 -0500210};
211
212inline void
213RibEntry::setName(const Name& prefix)
214{
215 m_name = prefix;
216}
217
218inline const Name&
219RibEntry::getName() const
220{
221 return m_name;
222}
223
224inline void
225RibEntry::setParent(shared_ptr<RibEntry> parent)
226{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400227 m_parent = std::move(parent);
Vince12e49462014-06-09 13:29:32 -0500228}
229
230inline shared_ptr<RibEntry>
231RibEntry::getParent() const
232{
233 return m_parent;
234}
235
Davide Pesaventod396b612017-02-20 22:11:50 -0500236inline const std::list<shared_ptr<RibEntry>>&
Vince Lehman76c751c2014-11-18 17:36:38 -0600237RibEntry::getChildren() const
Vince12e49462014-06-09 13:29:32 -0500238{
239 return m_children;
240}
241
Davide Pesaventod396b612017-02-20 22:11:50 -0500242inline const RibEntry::RouteList&
243RibEntry::getRoutes() const
Vince12e49462014-06-09 13:29:32 -0500244{
Vince Lehman218be0a2015-01-15 17:25:20 -0600245 return m_routes;
Vince12e49462014-06-09 13:29:32 -0500246}
247
Vince Lehman76c751c2014-11-18 17:36:38 -0600248inline const RibEntry::RouteList&
249RibEntry::getInheritedRoutes() const
Vince Lehman4387e782014-06-19 16:57:45 -0500250{
Vince Lehman218be0a2015-01-15 17:25:20 -0600251 return m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500252}
253
Vince12e49462014-06-09 13:29:32 -0500254inline RibEntry::const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600255RibEntry::begin() const
Vince12e49462014-06-09 13:29:32 -0500256{
Vince Lehman218be0a2015-01-15 17:25:20 -0600257 return m_routes.begin();
Vince12e49462014-06-09 13:29:32 -0500258}
259
260inline RibEntry::const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600261RibEntry::end() const
Vince12e49462014-06-09 13:29:32 -0500262{
Vince Lehman218be0a2015-01-15 17:25:20 -0600263 return m_routes.end();
Vince12e49462014-06-09 13:29:32 -0500264}
265
266inline RibEntry::iterator
267RibEntry::begin()
268{
Vince Lehman218be0a2015-01-15 17:25:20 -0600269 return m_routes.begin();
Vince12e49462014-06-09 13:29:32 -0500270}
271
272inline RibEntry::iterator
273RibEntry::end()
274{
Vince Lehman218be0a2015-01-15 17:25:20 -0600275 return m_routes.end();
Vince12e49462014-06-09 13:29:32 -0500276}
277
Vince Lehman4387e782014-06-19 16:57:45 -0500278std::ostream&
279operator<<(std::ostream& os, const RibEntry& entry);
280
Vince12e49462014-06-09 13:29:32 -0500281} // namespace rib
282} // namespace nfd
283
Davide Pesavento1b077f62019-02-19 19:19:44 -0500284#endif // NFD_DAEMON_RIB_RIB_ENTRY_HPP