blob: 45d5a877ee2a06233582822cfca824b85506fd65 [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 Pesavento1b077f62019-02-19 19:19:44 -05003 * Copyright (c) 2014-2019, 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 Pesaventoc0822fa2018-05-10 21:54:10 -040036/** \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:
Vince Lehman218be0a2015-01-15 17:25:20 -060041 typedef std::list<Route> RouteList;
42 typedef RouteList::iterator iterator;
43 typedef RouteList::const_iterator const_iterator;
Vince12e49462014-06-09 13:29:32 -050044
45 RibEntry()
Vince Lehman218be0a2015-01-15 17:25:20 -060046 : m_nRoutesWithCaptureSet(0)
Vince12e49462014-06-09 13:29:32 -050047 {
48 }
49
50 void
51 setName(const Name& prefix);
52
53 const Name&
54 getName() const;
55
56 shared_ptr<RibEntry>
57 getParent() const;
58
59 bool
60 hasParent() const;
61
62 void
63 addChild(shared_ptr<RibEntry> child);
64
65 void
66 removeChild(shared_ptr<RibEntry> child);
67
Vince Lehman76c751c2014-11-18 17:36:38 -060068 const std::list<shared_ptr<RibEntry>>&
69 getChildren() const;
Vince12e49462014-06-09 13:29:32 -050070
71 bool
72 hasChildren() const;
73
Vince Lehman218be0a2015-01-15 17:25:20 -060074 /** \brief inserts a new route into the entry's route list
75 * If another route already exists with the same faceId and origin,
76 * the new route is not inserted.
Nick Gordon89c4cca2016-11-02 15:42:32 +000077 * \return a pair, whose first element is the iterator to the newly
78 * inserted element if the insert succeeds and to the
79 * previously-existing element otherwise, and whose second element
80 * is true if the insert succeeds and false otherwise.
Vince12e49462014-06-09 13:29:32 -050081 */
Nick Gordon89c4cca2016-11-02 15:42:32 +000082 std::pair<RibEntry::iterator, bool>
Vince Lehman218be0a2015-01-15 17:25:20 -060083 insertRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050084
Vince Lehman218be0a2015-01-15 17:25:20 -060085 /** \brief erases a Route with the same faceId and origin
Vince12e49462014-06-09 13:29:32 -050086 */
Vince Lehman281ded72014-08-21 12:17:08 -050087 void
Vince Lehman218be0a2015-01-15 17:25:20 -060088 eraseRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -050089
Vince Lehman218be0a2015-01-15 17:25:20 -060090 /** \brief erases a Route with the passed iterator
Vince12e49462014-06-09 13:29:32 -050091 * \return{ an iterator to the element that followed the erased iterator }
92 */
93 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -060094 eraseRoute(RouteList::iterator route);
Vince12e49462014-06-09 13:29:32 -050095
96 bool
97 hasFaceId(const uint64_t faceId) const;
98
Davide Pesaventod396b612017-02-20 22:11:50 -050099 const RouteList&
100 getRoutes() const;
Vince12e49462014-06-09 13:29:32 -0500101
Vince Lehman76c751c2014-11-18 17:36:38 -0600102 size_t
103 getNRoutes() const;
104
Vince12e49462014-06-09 13:29:32 -0500105 iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600106 findRoute(const Route& route);
Vince12e49462014-06-09 13:29:32 -0500107
Vince Lehman76c751c2014-11-18 17:36:38 -0600108 const_iterator
109 findRoute(const Route& route) const;
110
Vince Lehman4387e782014-06-19 16:57:45 -0500111 bool
Vince Lehman218be0a2015-01-15 17:25:20 -0600112 hasRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500113
114 void
Vince Lehman218be0a2015-01-15 17:25:20 -0600115 addInheritedRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500116
117 void
Vince Lehman218be0a2015-01-15 17:25:20 -0600118 removeInheritedRoute(const Route& route);
Vince Lehman4387e782014-06-19 16:57:45 -0500119
Vince Lehman218be0a2015-01-15 17:25:20 -0600120 /** \brief Returns the routes this namespace has inherited.
121 * The inherited routes returned represent inherited routes this namespace has in the FIB.
122 * \return{ routes inherited by this namespace }
Vince Lehman4387e782014-06-19 16:57:45 -0500123 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600124 const RouteList&
125 getInheritedRoutes() const;
Vince Lehman4387e782014-06-19 16:57:45 -0500126
Vince Lehman218be0a2015-01-15 17:25:20 -0600127 /** \brief Finds an inherited route with a matching face ID.
128 * \return{ An iterator to the matching route if one is found;
Vince Lehman4387e782014-06-19 16:57:45 -0500129 * otherwise, an iterator to the end of the entry's
Vince Lehman218be0a2015-01-15 17:25:20 -0600130 * inherited route list }
Vince Lehman4387e782014-06-19 16:57:45 -0500131 */
Vince Lehman76c751c2014-11-18 17:36:38 -0600132 RouteList::const_iterator
133 findInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500134
Vince Lehman218be0a2015-01-15 17:25:20 -0600135 /** \brief Determines if the entry has an inherited route with a matching face ID.
136 * \return{ True, if a matching inherited route is found; otherwise, false. }
Vince Lehman4387e782014-06-19 16:57:45 -0500137 */
138 bool
Vince Lehman76c751c2014-11-18 17:36:38 -0600139 hasInheritedRoute(const Route& route) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500140
141 bool
142 hasCapture() const;
143
Vince Lehman218be0a2015-01-15 17:25:20 -0600144 /** \brief Determines if the entry has an inherited route with the passed
Vince Lehman4387e782014-06-19 16:57:45 -0500145 * face ID and its child inherit flag set.
Vince Lehman218be0a2015-01-15 17:25:20 -0600146 * \return{ True, if a matching inherited route is found; otherwise, false. }
Vince Lehman4387e782014-06-19 16:57:45 -0500147 */
148 bool
149 hasChildInheritOnFaceId(uint64_t faceId) const;
150
Vince Lehman218be0a2015-01-15 17:25:20 -0600151 /** \brief Returns the route with the lowest cost that has the passed face ID.
152 * \return{ The route with the lowest cost that has the passed face ID}
Vince Lehman4387e782014-06-19 16:57:45 -0500153 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500154 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600155 getRouteWithLowestCostByFaceId(uint64_t faceId) const;
156
Vince Lehman9dcfc402015-03-26 03:18:54 -0500157 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600158 getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500159
Vince Lehman218be0a2015-01-15 17:25:20 -0600160 /** \brief Returns the route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500161 * and its child inherit flag set.
Vince Lehman218be0a2015-01-15 17:25:20 -0600162 * \return{ The route with the lowest cost that has the passed face ID
Vince Lehman4387e782014-06-19 16:57:45 -0500163 * and its child inherit flag set }
164 */
Vince Lehman9dcfc402015-03-26 03:18:54 -0500165 const Route*
Vince Lehman76c751c2014-11-18 17:36:38 -0600166 getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const;
Vince Lehman4387e782014-06-19 16:57:45 -0500167
Junxiao Shid47cd632018-09-11 03:10:00 +0000168 /** \brief Retrieve a prefix announcement suitable for readvertising this route.
169 *
170 * If one or more routes in this RIB entry contains a prefix announcement, this method returns
171 * the announcement from the route that expires last.
172 *
173 * If this RIB entry does not have a route containing a prefix announcement, this method creates
174 * a new announcement. Its expiration period reflects the remaining lifetime of this RIB entry,
175 * confined within [\p minExpiration, \p maxExpiration] range. The caller is expected to sign
176 * this announcement.
177 *
178 * \warning (minExpiration > maxExpiration) triggers undefined behavior.
179 */
180 ndn::PrefixAnnouncement
181 getPrefixAnnouncement(time::milliseconds minExpiration = 15_s,
182 time::milliseconds maxExpiration = 1_h) const;
183
Vince12e49462014-06-09 13:29:32 -0500184 const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600185 begin() const;
Vince12e49462014-06-09 13:29:32 -0500186
187 const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600188 end() const;
Vince12e49462014-06-09 13:29:32 -0500189
190 iterator
191 begin();
192
193 iterator
194 end();
195
196private:
197 void
198 setParent(shared_ptr<RibEntry> parent);
199
200private:
201 Name m_name;
Vince Lehman218be0a2015-01-15 17:25:20 -0600202 std::list<shared_ptr<RibEntry>> m_children;
Vince12e49462014-06-09 13:29:32 -0500203 shared_ptr<RibEntry> m_parent;
Vince Lehman218be0a2015-01-15 17:25:20 -0600204 RouteList m_routes;
205 RouteList m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500206
Vince Lehman218be0a2015-01-15 17:25:20 -0600207 /** \brief The number of routes on this namespace with the capture flag set.
Vince Lehman4387e782014-06-19 16:57:45 -0500208 *
Vince Lehman218be0a2015-01-15 17:25:20 -0600209 * This count is used to check if the namespace will block inherited routes.
Junxiao Shi2e526d72016-08-22 16:02:13 +0000210 * If the number is greater than zero, a route on the namespace has its capture
Vince Lehman218be0a2015-01-15 17:25:20 -0600211 * flag set which means the namespace should not inherit any routes.
Vince Lehman4387e782014-06-19 16:57:45 -0500212 */
Vince Lehman218be0a2015-01-15 17:25:20 -0600213 uint64_t m_nRoutesWithCaptureSet;
Vince12e49462014-06-09 13:29:32 -0500214};
215
216inline void
217RibEntry::setName(const Name& prefix)
218{
219 m_name = prefix;
220}
221
222inline const Name&
223RibEntry::getName() const
224{
225 return m_name;
226}
227
228inline void
229RibEntry::setParent(shared_ptr<RibEntry> parent)
230{
231 m_parent = parent;
232}
233
234inline shared_ptr<RibEntry>
235RibEntry::getParent() const
236{
237 return m_parent;
238}
239
Davide Pesaventod396b612017-02-20 22:11:50 -0500240inline const std::list<shared_ptr<RibEntry>>&
Vince Lehman76c751c2014-11-18 17:36:38 -0600241RibEntry::getChildren() const
Vince12e49462014-06-09 13:29:32 -0500242{
243 return m_children;
244}
245
Davide Pesaventod396b612017-02-20 22:11:50 -0500246inline const RibEntry::RouteList&
247RibEntry::getRoutes() const
Vince12e49462014-06-09 13:29:32 -0500248{
Vince Lehman218be0a2015-01-15 17:25:20 -0600249 return m_routes;
Vince12e49462014-06-09 13:29:32 -0500250}
251
Vince Lehman76c751c2014-11-18 17:36:38 -0600252inline const RibEntry::RouteList&
253RibEntry::getInheritedRoutes() const
Vince Lehman4387e782014-06-19 16:57:45 -0500254{
Vince Lehman218be0a2015-01-15 17:25:20 -0600255 return m_inheritedRoutes;
Vince Lehman4387e782014-06-19 16:57:45 -0500256}
257
Vince12e49462014-06-09 13:29:32 -0500258inline RibEntry::const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600259RibEntry::begin() const
Vince12e49462014-06-09 13:29:32 -0500260{
Vince Lehman218be0a2015-01-15 17:25:20 -0600261 return m_routes.begin();
Vince12e49462014-06-09 13:29:32 -0500262}
263
264inline RibEntry::const_iterator
Vince Lehman218be0a2015-01-15 17:25:20 -0600265RibEntry::end() const
Vince12e49462014-06-09 13:29:32 -0500266{
Vince Lehman218be0a2015-01-15 17:25:20 -0600267 return m_routes.end();
Vince12e49462014-06-09 13:29:32 -0500268}
269
270inline RibEntry::iterator
271RibEntry::begin()
272{
Vince Lehman218be0a2015-01-15 17:25:20 -0600273 return m_routes.begin();
Vince12e49462014-06-09 13:29:32 -0500274}
275
276inline RibEntry::iterator
277RibEntry::end()
278{
Vince Lehman218be0a2015-01-15 17:25:20 -0600279 return m_routes.end();
Vince12e49462014-06-09 13:29:32 -0500280}
281
Vince Lehman4387e782014-06-19 16:57:45 -0500282std::ostream&
283operator<<(std::ostream& os, const RibEntry& entry);
284
Vince12e49462014-06-09 13:29:32 -0500285} // namespace rib
286} // namespace nfd
287
Davide Pesavento1b077f62019-02-19 19:19:44 -0500288#endif // NFD_DAEMON_RIB_RIB_ENTRY_HPP