Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c0822fa | 2018-05-10 21:54:10 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Alexander Afanasyev | 7c10b3b | 2015-01-20 12:24:27 -0800 | [diff] [blame] | 4 | * 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. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 10 | * |
| 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 Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_RIB_RIB_ENTRY_HPP |
| 27 | #define NFD_DAEMON_RIB_RIB_ENTRY_HPP |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 28 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 29 | #include "route.hpp" |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 30 | |
Davide Pesavento | c0822fa | 2018-05-10 21:54:10 -0400 | [diff] [blame] | 31 | #include <list> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::rib { |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 34 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 35 | /** |
| 36 | * \brief Represents a RIB entry, which contains one or more Routes with the same prefix. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 37 | */ |
Davide Pesavento | c0822fa | 2018-05-10 21:54:10 -0400 | [diff] [blame] | 38 | class RibEntry : public std::enable_shared_from_this<RibEntry> |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 39 | { |
| 40 | public: |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 41 | using RouteList = std::list<Route>; |
| 42 | using iterator = RouteList::iterator; |
| 43 | using const_iterator = RouteList::const_iterator; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 44 | |
| 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 Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 63 | const std::list<shared_ptr<RibEntry>>& |
| 64 | getChildren() const; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 65 | |
| 66 | bool |
| 67 | hasChildren() const; |
| 68 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 69 | /** \brief Inserts a new route into the entry's route list. |
| 70 | * |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 71 | * If another route already exists with the same faceId and origin, |
| 72 | * the new route is not inserted. |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 73 | * |
| 74 | * \return A pair, whose first element is the iterator to the newly |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 75 | * inserted element if the insert succeeds and to the |
| 76 | * previously-existing element otherwise, and whose second element |
| 77 | * is true if the insert succeeds and false otherwise. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 78 | */ |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 79 | std::pair<RibEntry::iterator, bool> |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 80 | insertRoute(const Route& route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 81 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 82 | /** |
| 83 | * \brief Erases a Route with the same FaceId and origin. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 84 | */ |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 85 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 86 | eraseRoute(const Route& route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 87 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 88 | /** |
| 89 | * \brief Erases a Route with the passed iterator. |
| 90 | * \return An iterator to the element that followed the erased iterator |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 91 | */ |
| 92 | iterator |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 93 | eraseRoute(RouteList::iterator route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 94 | |
| 95 | bool |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 96 | hasFaceId(uint64_t faceId) const; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 97 | |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 98 | const RouteList& |
| 99 | getRoutes() const; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 100 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 101 | size_t |
| 102 | getNRoutes() const; |
| 103 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 104 | iterator |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 105 | findRoute(const Route& route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 106 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 107 | const_iterator |
| 108 | findRoute(const Route& route) const; |
| 109 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 110 | bool |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 111 | hasRoute(const Route& route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 112 | |
| 113 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 114 | addInheritedRoute(const Route& route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 115 | |
| 116 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 117 | removeInheritedRoute(const Route& route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 118 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 119 | /** |
| 120 | * \brief Returns the routes this namespace has inherited. |
| 121 | * |
| 122 | * The inherited routes returned represent inherited routes this namespace has in the FIB. |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 123 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 124 | const RouteList& |
| 125 | getInheritedRoutes() const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 126 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 127 | /** |
| 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 Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 132 | */ |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 133 | RouteList::const_iterator |
| 134 | findInheritedRoute(const Route& route) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 135 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 136 | /** \brief Determines if the entry has an inherited route with a matching face ID. |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 137 | * \return True, if a matching inherited route is found; otherwise, false. |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 138 | */ |
| 139 | bool |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 140 | hasInheritedRoute(const Route& route) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 141 | |
| 142 | bool |
| 143 | hasCapture() const; |
| 144 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 145 | /** \brief Determines if the entry has an inherited route with the passed |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 146 | * face ID and its child inherit flag set. |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 147 | * \return True, if a matching inherited route is found; otherwise, false. |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 148 | */ |
| 149 | bool |
| 150 | hasChildInheritOnFaceId(uint64_t faceId) const; |
| 151 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 152 | /** \brief Returns the route with the lowest cost that has the passed face ID. |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 153 | * \return The route with the lowest cost that has the passed face ID |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 154 | */ |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 155 | const Route* |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 156 | getRouteWithLowestCostByFaceId(uint64_t faceId) const; |
| 157 | |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 158 | const Route* |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 159 | getRouteWithSecondLowestCostByFaceId(uint64_t faceId) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 160 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 161 | /** \brief Returns the route with the lowest cost that has the passed face ID |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 162 | * and its child inherit flag set. |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 163 | */ |
Vince Lehman | 9dcfc40 | 2015-03-26 03:18:54 -0500 | [diff] [blame] | 164 | const Route* |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 165 | getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 166 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 167 | /** \brief Retrieve a prefix announcement suitable for readvertising this route. |
| 168 | * |
| 169 | * If one or more routes in this RIB entry contains a prefix announcement, this method returns |
| 170 | * the announcement from the route that expires last. |
| 171 | * |
| 172 | * If this RIB entry does not have a route containing a prefix announcement, this method creates |
| 173 | * a new announcement. Its expiration period reflects the remaining lifetime of this RIB entry, |
| 174 | * confined within [\p minExpiration, \p maxExpiration] range. The caller is expected to sign |
| 175 | * this announcement. |
| 176 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 177 | * \warning `minExpiration > maxExpiration` triggers undefined behavior. |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 178 | */ |
| 179 | ndn::PrefixAnnouncement |
| 180 | getPrefixAnnouncement(time::milliseconds minExpiration = 15_s, |
| 181 | time::milliseconds maxExpiration = 1_h) const; |
| 182 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 183 | const_iterator |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 184 | begin() const; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 185 | |
| 186 | const_iterator |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 187 | end() const; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 188 | |
| 189 | iterator |
| 190 | begin(); |
| 191 | |
| 192 | iterator |
| 193 | end(); |
| 194 | |
| 195 | private: |
| 196 | void |
| 197 | setParent(shared_ptr<RibEntry> parent); |
| 198 | |
| 199 | private: |
| 200 | Name m_name; |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 201 | std::list<shared_ptr<RibEntry>> m_children; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 202 | shared_ptr<RibEntry> m_parent; |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 203 | RouteList m_routes; |
| 204 | RouteList m_inheritedRoutes; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 205 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 206 | /** \brief The number of routes on this namespace with the capture flag set. |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 207 | * |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 208 | * This count is used to check if the namespace will block inherited routes. |
Junxiao Shi | 2e526d7 | 2016-08-22 16:02:13 +0000 | [diff] [blame] | 209 | * If the number is greater than zero, a route on the namespace has its capture |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 210 | * flag set which means the namespace should not inherit any routes. |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 211 | */ |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 212 | uint64_t m_nRoutesWithCaptureSet = 0; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | inline void |
| 216 | RibEntry::setName(const Name& prefix) |
| 217 | { |
| 218 | m_name = prefix; |
| 219 | } |
| 220 | |
| 221 | inline const Name& |
| 222 | RibEntry::getName() const |
| 223 | { |
| 224 | return m_name; |
| 225 | } |
| 226 | |
| 227 | inline void |
| 228 | RibEntry::setParent(shared_ptr<RibEntry> parent) |
| 229 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 230 | m_parent = std::move(parent); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | inline shared_ptr<RibEntry> |
| 234 | RibEntry::getParent() const |
| 235 | { |
| 236 | return m_parent; |
| 237 | } |
| 238 | |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 239 | inline const std::list<shared_ptr<RibEntry>>& |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 240 | RibEntry::getChildren() const |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 241 | { |
| 242 | return m_children; |
| 243 | } |
| 244 | |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 245 | inline const RibEntry::RouteList& |
| 246 | RibEntry::getRoutes() const |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 247 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 248 | return m_routes; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 251 | inline const RibEntry::RouteList& |
| 252 | RibEntry::getInheritedRoutes() const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 253 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 254 | return m_inheritedRoutes; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 257 | inline RibEntry::const_iterator |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 258 | RibEntry::begin() const |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 259 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 260 | return m_routes.begin(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | inline RibEntry::const_iterator |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 264 | RibEntry::end() const |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 265 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 266 | return m_routes.end(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | inline RibEntry::iterator |
| 270 | RibEntry::begin() |
| 271 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 272 | return m_routes.begin(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | inline RibEntry::iterator |
| 276 | RibEntry::end() |
| 277 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 278 | return m_routes.end(); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 281 | std::ostream& |
| 282 | operator<<(std::ostream& os, const RibEntry& entry); |
| 283 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 284 | } // namespace nfd::rib |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 285 | |
Davide Pesavento | 1b077f6 | 2019-02-19 19:19:44 -0500 | [diff] [blame] | 286 | #endif // NFD_DAEMON_RIB_RIB_ENTRY_HPP |