blob: 6ec8b6501b828ddda3444a46a2c1278f5534f427 [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 Pesaventoc2442be2025-01-11 17:25:40 -05003 * Copyright (c) 2014-2025, 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
Vince12e49462014-06-09 13:29:32 -050045 const Name&
Davide Pesaventoc2442be2025-01-11 17:25:40 -050046 getName() const noexcept
47 {
48 return m_name;
49 }
50
51 void
52 setName(const Name& prefix)
53 {
54 m_name = prefix;
55 }
Vince12e49462014-06-09 13:29:32 -050056
57 shared_ptr<RibEntry>
Davide Pesaventoc2442be2025-01-11 17:25:40 -050058 getParent() const
59 {
60 return m_parent;
61 }
Vince12e49462014-06-09 13:29:32 -050062
Davide Pesaventoc2442be2025-01-11 17:25:40 -050063 const std::list<shared_ptr<RibEntry>>&
64 getChildren() const noexcept
65 {
66 return m_children;
67 }
Vince12e49462014-06-09 13:29:32 -050068
69 void
70 addChild(shared_ptr<RibEntry> child);
71
72 void
73 removeChild(shared_ptr<RibEntry> child);
74
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040075 /** \brief Inserts a new route into the entry's route list.
76 *
Vince Lehman218be0a2015-01-15 17:25:20 -060077 * If another route already exists with the same faceId and origin,
78 * the new route is not inserted.
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040079 *
80 * \return A pair, whose first element is the iterator to the newly
Nick Gordon89c4cca2016-11-02 15:42:32 +000081 * inserted element if the insert succeeds and to the
82 * previously-existing element otherwise, and whose second element
83 * is true if the insert succeeds and false otherwise.
Vince12e49462014-06-09 13:29:32 -050084 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000085 std::pair<RibEntry::iterator, bool>
Vince Lehman218be0a2015-01-15 17:25:20 -060086 insertRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050087
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040088 /**
89 * \brief Erases a Route with the same FaceId and origin.
Vince12e49462014-06-09 13:29:32 -050090 */
Vince Lehman281ded72014-08-21 12:17:08 -050091 void
Vince Lehman218be0a2015-01-15 17:25:20 -060092 eraseRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050093
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040094 /**
95 * \brief Erases a Route with the passed iterator.
96 * \return An iterator to the element that followed the erased iterator
Vince12e49462014-06-09 13:29:32 -050097 */
98 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -060099 eraseRoute(RouteList::iterator route);
Vince12e49462014-06-09 13:29:32 -0500100
101 bool
Davide Pesavento412c9822021-07-02 00:21:05 -0400102 hasFaceId(uint64_t faceId) const;
Vince12e49462014-06-09 13:29:32 -0500103
Vince12e49462014-06-09 13:29:32 -0500104 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600105 findRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -0500106
Vince Lehman76c751c2014-11-18 17:36:38 -0600107 const_iterator
108 findRoute(const Route& route) const;
109
Vince Lehman4387e782014-06-19 16:57:45 -0500110 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
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400116 /**
117 * \brief Returns the routes this namespace has inherited.
118 *
119 * The inherited routes returned represent inherited routes this namespace has in the FIB.
Vince Lehman4387e782014-06-19 16:57:45 -0500120 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600121 const RouteList&
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500122 getInheritedRoutes() const noexcept
123 {
124 return m_inheritedRoutes;
125 }
Vince Lehman4387e782014-06-19 16:57:45 -0500126
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400127 /**
128 * \brief Finds an inherited route with a matching face ID.
129 * \return An iterator to the matching route if one is found;
130 * otherwise, an iterator to the end of the entry's
131 * inherited route list
Vince Lehman4387e782014-06-19 16:57:45 -0500132 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600133 RouteList::const_iterator
134 findInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500135
Vince Lehman218be0a2015-01-15 17:25:20 -0600136 /** \brief Determines if the entry has an inherited route with a matching face ID.
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400137 * \return True, if a matching inherited route is found; otherwise, false.
Vince Lehman4387e782014-06-19 16:57:45 -0500138 */
139 bool
Vince Lehman76c751c2014-11-18 17:36:38 -0600140 hasInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500141
142 bool
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500143 hasCapture() const noexcept
144 {
145 return m_nRoutesWithCaptureSet > 0;
146 }
Vince Lehman4387e782014-06-19 16:57:45 -0500147
Vince Lehman218be0a2015-01-15 17:25:20 -0600148 /** \brief Determines if the entry has an inherited route with the passed
Vince Lehman4387e782014-06-19 16:57:45 -0500149 * face ID and its child inherit flag set.
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400150 * \return True, if a matching inherited route is found; otherwise, false.
Vince Lehman4387e782014-06-19 16:57:45 -0500151 */
152 bool
153 hasChildInheritOnFaceId(uint64_t faceId) const;
154
Vince Lehman218be0a2015-01-15 17:25:20 -0600155 /** \brief Returns the route with the lowest cost that has the passed face ID.
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400156 * \return The route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500157 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500158 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600159 getRouteWithLowestCostByFaceId(uint64_t faceId) const;
160
Vince Lehman9dcfc402015-03-26 03:18:54 -0500161 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600162 getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500163
Vince Lehman218be0a2015-01-15 17:25:20 -0600164 /** \brief Returns the route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500165 * and its child inherit flag set.
Vince Lehman4387e782014-06-19 16:57:45 -0500166 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500167 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600168 getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500169
Junxiao Shid47cd632018-09-11 03:10:00 +0000170 /** \brief Retrieve a prefix announcement suitable for readvertising this route.
171 *
172 * If one or more routes in this RIB entry contains a prefix announcement, this method returns
173 * the announcement from the route that expires last.
174 *
175 * If this RIB entry does not have a route containing a prefix announcement, this method creates
176 * a new announcement. Its expiration period reflects the remaining lifetime of this RIB entry,
177 * confined within [\p minExpiration, \p maxExpiration] range. The caller is expected to sign
178 * this announcement.
179 *
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400180 * \warning `minExpiration > maxExpiration` triggers undefined behavior.
Junxiao Shid47cd632018-09-11 03:10:00 +0000181 */
182 ndn::PrefixAnnouncement
183 getPrefixAnnouncement(time::milliseconds minExpiration = 15_s,
184 time::milliseconds maxExpiration = 1_h) const;
185
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500186 const RouteList&
187 getRoutes() const noexcept
188 {
189 return m_routes;
190 }
191
192 bool
193 empty() const noexcept
194 {
195 return m_routes.empty();
196 }
Vince12e49462014-06-09 13:29:32 -0500197
198 const_iterator
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500199 begin() const noexcept
200 {
201 return m_routes.begin();
202 }
203
204 const_iterator
205 end() const noexcept
206 {
207 return m_routes.end();
208 }
Vince12e49462014-06-09 13:29:32 -0500209
210 iterator
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500211 begin() noexcept
212 {
213 return m_routes.begin();
214 }
Vince12e49462014-06-09 13:29:32 -0500215
216 iterator
Davide Pesaventoc2442be2025-01-11 17:25:40 -0500217 end() noexcept
218 {
219 return m_routes.end();
220 }
Vince12e49462014-06-09 13:29:32 -0500221
222private:
223 Name m_name;
Vince Lehman218be0a2015-01-15 17:25:20 -0600224 std::list<shared_ptr<RibEntry>> m_children;
Vince12e49462014-06-09 13:29:32 -0500225 shared_ptr<RibEntry> m_parent;
Vince Lehman218be0a2015-01-15 17:25:20 -0600226 RouteList m_routes;
227 RouteList m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500228
Vince Lehman218be0a2015-01-15 17:25:20 -0600229 /** \brief The number of routes on this namespace with the capture flag set.
Vince Lehman4387e782014-06-19 16:57:45 -0500230 *
Vince Lehman218be0a2015-01-15 17:25:20 -0600231 * This count is used to check if the namespace will block inherited routes.
Junxiao Shi2e526d72016-08-22 16:02:13 +0000232 * If the number is greater than zero, a route on the namespace has its capture
Vince Lehman218be0a2015-01-15 17:25:20 -0600233 * flag set which means the namespace should not inherit any routes.
Vince Lehman4387e782014-06-19 16:57:45 -0500234 */
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400235 uint64_t m_nRoutesWithCaptureSet = 0;
Vince12e49462014-06-09 13:29:32 -0500236};
237
Vince Lehman4387e782014-06-19 16:57:45 -0500238std::ostream&
239operator<<(std::ostream& os, const RibEntry& entry);
240
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400241} // namespace nfd::rib
Vince12e49462014-06-09 13:29:32 -0500242
Davide Pesavento1b077f62019-02-19 19:19:44 -0500243#endif // NFD_DAEMON_RIB_RIB_ENTRY_HPP