blob: d98d72a78e57e23c6048fa8b130e1d962935644d [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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::rib {
Vince12e49462014-06-09 13:29:32 -050034
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040035/**
36 * \brief Represents a RIB entry, which contains one or more Routes with the same prefix.
Vince12e49462014-06-09 13:29:32 -050037 */
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040038class RibEntry : public std::enable_shared_from_this<RibEntry>
Vince12e49462014-06-09 13:29:32 -050039{
40public:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040041 using RouteList = std::list<Route>;
42 using iterator = RouteList::iterator;
43 using const_iterator = RouteList::const_iterator;
Vince12e49462014-06-09 13:29:32 -050044
45 void
46 setName(const Name& prefix);
47
48 const Name&
49 getName() const;
50
51 shared_ptr<RibEntry>
52 getParent() const;
53
54 bool
55 hasParent() const;
56
57 void
58 addChild(shared_ptr<RibEntry> child);
59
60 void
61 removeChild(shared_ptr<RibEntry> child);
62
Vince Lehman76c751c2014-11-18 17:36:38 -060063 const std::list<shared_ptr<RibEntry>>&
64 getChildren() const;
Vince12e49462014-06-09 13:29:32 -050065
66 bool
67 hasChildren() const;
68
Vince Lehman218be0a2015-01-15 17:25:20 -060069 /** \brief inserts a new route into the entry's route list
70 * If another route already exists with the same faceId and origin,
71 * the new route is not inserted.
Nick Gordon89c4cca2016-11-02 15:42:32 +000072 * \return a pair, whose first element is the iterator to the newly
73 * inserted element if the insert succeeds and to the
74 * previously-existing element otherwise, and whose second element
75 * is true if the insert succeeds and false otherwise.
Vince12e49462014-06-09 13:29:32 -050076 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000077 std::pair<RibEntry::iterator, bool>
Vince Lehman218be0a2015-01-15 17:25:20 -060078 insertRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050079
Vince Lehman218be0a2015-01-15 17:25:20 -060080 /** \brief erases a Route with the same faceId and origin
Vince12e49462014-06-09 13:29:32 -050081 */
Vince Lehman281ded72014-08-21 12:17:08 -050082 void
Vince Lehman218be0a2015-01-15 17:25:20 -060083 eraseRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050084
Vince Lehman218be0a2015-01-15 17:25:20 -060085 /** \brief erases a Route with the passed iterator
Vince12e49462014-06-09 13:29:32 -050086 * \return{ an iterator to the element that followed the erased iterator }
87 */
88 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -060089 eraseRoute(RouteList::iterator route);
Vince12e49462014-06-09 13:29:32 -050090
91 bool
Davide Pesavento412c9822021-07-02 00:21:05 -040092 hasFaceId(uint64_t faceId) const;
Vince12e49462014-06-09 13:29:32 -050093
Davide Pesaventod396b612017-02-20 22:11:50 -050094 const RouteList&
95 getRoutes() const;
Vince12e49462014-06-09 13:29:32 -050096
Vince Lehman76c751c2014-11-18 17:36:38 -060097 size_t
98 getNRoutes() const;
99
Vince12e49462014-06-09 13:29:32 -0500100 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600101 findRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -0500102
Vince Lehman76c751c2014-11-18 17:36:38 -0600103 const_iterator
104 findRoute(const Route& route) const;
105
Vince Lehman4387e782014-06-19 16:57:45 -0500106 bool
Vince Lehman218be0a2015-01-15 17:25:20 -0600107 hasRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500108
109 void
Vince Lehman218be0a2015-01-15 17:25:20 -0600110 addInheritedRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500111
112 void
Vince Lehman218be0a2015-01-15 17:25:20 -0600113 removeInheritedRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500114
Vince Lehman218be0a2015-01-15 17:25:20 -0600115 /** \brief Returns the routes this namespace has inherited.
116 * The inherited routes returned represent inherited routes this namespace has in the FIB.
117 * \return{ routes inherited by this namespace }
Vince Lehman4387e782014-06-19 16:57:45 -0500118 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600119 const RouteList&
120 getInheritedRoutes() const;
Vince Lehman4387e782014-06-19 16:57:45 -0500121
Vince Lehman218be0a2015-01-15 17:25:20 -0600122 /** \brief Finds an inherited route with a matching face ID.
123 * \return{ An iterator to the matching route if one is found;
Vince Lehman4387e782014-06-19 16:57:45 -0500124 * otherwise, an iterator to the end of the entry's
Vince Lehman218be0a2015-01-15 17:25:20 -0600125 * inherited route list }
Vince Lehman4387e782014-06-19 16:57:45 -0500126 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600127 RouteList::const_iterator
128 findInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500129
Vince Lehman218be0a2015-01-15 17:25:20 -0600130 /** \brief Determines if the entry has an inherited route with a matching face ID.
131 * \return{ True, if a matching inherited route is found; otherwise, false. }
Vince Lehman4387e782014-06-19 16:57:45 -0500132 */
133 bool
Vince Lehman76c751c2014-11-18 17:36:38 -0600134 hasInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500135
136 bool
137 hasCapture() const;
138
Vince Lehman218be0a2015-01-15 17:25:20 -0600139 /** \brief Determines if the entry has an inherited route with the passed
Vince Lehman4387e782014-06-19 16:57:45 -0500140 * face ID and its child inherit flag set.
Vince Lehman218be0a2015-01-15 17:25:20 -0600141 * \return{ True, if a matching inherited route is found; otherwise, false. }
Vince Lehman4387e782014-06-19 16:57:45 -0500142 */
143 bool
144 hasChildInheritOnFaceId(uint64_t faceId) const;
145
Vince Lehman218be0a2015-01-15 17:25:20 -0600146 /** \brief Returns the route with the lowest cost that has the passed face ID.
147 * \return{ The route with the lowest cost that has the passed face ID}
Vince Lehman4387e782014-06-19 16:57:45 -0500148 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500149 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600150 getRouteWithLowestCostByFaceId(uint64_t faceId) const;
151
Vince Lehman9dcfc402015-03-26 03:18:54 -0500152 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600153 getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500154
Vince Lehman218be0a2015-01-15 17:25:20 -0600155 /** \brief Returns the route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500156 * and its child inherit flag set.
Vince Lehman218be0a2015-01-15 17:25:20 -0600157 * \return{ The route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500158 * and its child inherit flag set }
159 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500160 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600161 getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500162
Junxiao Shid47cd632018-09-11 03:10:00 +0000163 /** \brief Retrieve a prefix announcement suitable for readvertising this route.
164 *
165 * If one or more routes in this RIB entry contains a prefix announcement, this method returns
166 * the announcement from the route that expires last.
167 *
168 * If this RIB entry does not have a route containing a prefix announcement, this method creates
169 * a new announcement. Its expiration period reflects the remaining lifetime of this RIB entry,
170 * confined within [\p minExpiration, \p maxExpiration] range. The caller is expected to sign
171 * this announcement.
172 *
173 * \warning (minExpiration > maxExpiration) triggers undefined behavior.
174 */
175 ndn::PrefixAnnouncement
176 getPrefixAnnouncement(time::milliseconds minExpiration = 15_s,
177 time::milliseconds maxExpiration = 1_h) const;
178
Vince12e49462014-06-09 13:29:32 -0500179 const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600180 begin() const;
Vince12e49462014-06-09 13:29:32 -0500181
182 const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600183 end() const;
Vince12e49462014-06-09 13:29:32 -0500184
185 iterator
186 begin();
187
188 iterator
189 end();
190
191private:
192 void
193 setParent(shared_ptr<RibEntry> parent);
194
195private:
196 Name m_name;
Vince Lehman218be0a2015-01-15 17:25:20 -0600197 std::list<shared_ptr<RibEntry>> m_children;
Vince12e49462014-06-09 13:29:32 -0500198 shared_ptr<RibEntry> m_parent;
Vince Lehman218be0a2015-01-15 17:25:20 -0600199 RouteList m_routes;
200 RouteList m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500201
Vince Lehman218be0a2015-01-15 17:25:20 -0600202 /** \brief The number of routes on this namespace with the capture flag set.
Vince Lehman4387e782014-06-19 16:57:45 -0500203 *
Vince Lehman218be0a2015-01-15 17:25:20 -0600204 * This count is used to check if the namespace will block inherited routes.
Junxiao Shi2e526d72016-08-22 16:02:13 +0000205 * If the number is greater than zero, a route on the namespace has its capture
Vince Lehman218be0a2015-01-15 17:25:20 -0600206 * flag set which means the namespace should not inherit any routes.
Vince Lehman4387e782014-06-19 16:57:45 -0500207 */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400208 uint64_t m_nRoutesWithCaptureSet = 0;
Vince12e49462014-06-09 13:29:32 -0500209};
210
211inline void
212RibEntry::setName(const Name& prefix)
213{
214 m_name = prefix;
215}
216
217inline const Name&
218RibEntry::getName() const
219{
220 return m_name;
221}
222
223inline void
224RibEntry::setParent(shared_ptr<RibEntry> parent)
225{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400226 m_parent = std::move(parent);
Vince12e49462014-06-09 13:29:32 -0500227}
228
229inline shared_ptr<RibEntry>
230RibEntry::getParent() const
231{
232 return m_parent;
233}
234
Davide Pesaventod396b612017-02-20 22:11:50 -0500235inline const std::list<shared_ptr<RibEntry>>&
Vince Lehman76c751c2014-11-18 17:36:38 -0600236RibEntry::getChildren() const
Vince12e49462014-06-09 13:29:32 -0500237{
238 return m_children;
239}
240
Davide Pesaventod396b612017-02-20 22:11:50 -0500241inline const RibEntry::RouteList&
242RibEntry::getRoutes() const
Vince12e49462014-06-09 13:29:32 -0500243{
Vince Lehman218be0a2015-01-15 17:25:20 -0600244 return m_routes;
Vince12e49462014-06-09 13:29:32 -0500245}
246
Vince Lehman76c751c2014-11-18 17:36:38 -0600247inline const RibEntry::RouteList&
248RibEntry::getInheritedRoutes() const
Vince Lehman4387e782014-06-19 16:57:45 -0500249{
Vince Lehman218be0a2015-01-15 17:25:20 -0600250 return m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500251}
252
Vince12e49462014-06-09 13:29:32 -0500253inline RibEntry::const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600254RibEntry::begin() const
Vince12e49462014-06-09 13:29:32 -0500255{
Vince Lehman218be0a2015-01-15 17:25:20 -0600256 return m_routes.begin();
Vince12e49462014-06-09 13:29:32 -0500257}
258
259inline RibEntry::const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600260RibEntry::end() const
Vince12e49462014-06-09 13:29:32 -0500261{
Vince Lehman218be0a2015-01-15 17:25:20 -0600262 return m_routes.end();
Vince12e49462014-06-09 13:29:32 -0500263}
264
265inline RibEntry::iterator
266RibEntry::begin()
267{
Vince Lehman218be0a2015-01-15 17:25:20 -0600268 return m_routes.begin();
Vince12e49462014-06-09 13:29:32 -0500269}
270
271inline RibEntry::iterator
272RibEntry::end()
273{
Vince Lehman218be0a2015-01-15 17:25:20 -0600274 return m_routes.end();
Vince12e49462014-06-09 13:29:32 -0500275}
276
Vince Lehman4387e782014-06-19 16:57:45 -0500277std::ostream&
278operator<<(std::ostream& os, const RibEntry& entry);
279
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400280} // namespace nfd::rib
Vince12e49462014-06-09 13:29:32 -0500281
Davide Pesavento1b077f62019-02-19 19:19:44 -0500282#endif // NFD_DAEMON_RIB_RIB_ENTRY_HPP