Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, 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. |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [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/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
| 26 | #include "rib.hpp" |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 27 | #include "fib-updater.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::rib { |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 32 | NFD_LOG_INIT(Rib); |
| 33 | |
Junxiao Shi | 89c0ea0 | 2017-03-06 19:52:05 +0000 | [diff] [blame] | 34 | bool |
| 35 | operator<(const RibRouteRef& lhs, const RibRouteRef& rhs) |
| 36 | { |
| 37 | return std::tie(lhs.entry->getName(), lhs.route->faceId, lhs.route->origin) < |
| 38 | std::tie(rhs.entry->getName(), rhs.route->faceId, rhs.route->origin); |
| 39 | } |
| 40 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 41 | static inline bool |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 42 | sortRoutes(const Route& lhs, const Route& rhs) |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 43 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 44 | return lhs.faceId < rhs.faceId; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 47 | void |
| 48 | Rib::setFibUpdater(FibUpdater* updater) |
| 49 | { |
| 50 | m_fibUpdater = updater; |
| 51 | } |
| 52 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 53 | Rib::const_iterator |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 54 | Rib::find(const Name& prefix) const |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 55 | { |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 56 | return m_rib.find(prefix); |
| 57 | } |
| 58 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 59 | Route* |
| 60 | Rib::find(const Name& prefix, const Route& route) const |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 61 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 62 | auto ribIt = m_rib.find(prefix); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 63 | |
| 64 | // Name prefix exists |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 65 | if (ribIt != m_rib.end()) { |
| 66 | shared_ptr<RibEntry> entry = ribIt->second; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 67 | auto routeIt = entry->findRoute(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 68 | if (routeIt != entry->end()) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 69 | return &*routeIt; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 70 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 71 | } |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 72 | |
| 73 | return nullptr; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 76 | Route* |
| 77 | Rib::findLongestPrefix(const Name& prefix, const Route& route) const |
| 78 | { |
| 79 | Route* existingRoute = find(prefix, route); |
| 80 | if (existingRoute == nullptr) { |
| 81 | auto parent = findParent(prefix); |
| 82 | if (parent) { |
| 83 | existingRoute = find(parent->getName(), route); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return existingRoute; |
| 88 | } |
| 89 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 90 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 91 | Rib::insert(const Name& prefix, const Route& route) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 92 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 93 | auto ribIt = m_rib.find(prefix); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 94 | |
| 95 | // Name prefix exists |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 96 | if (ribIt != m_rib.end()) { |
| 97 | shared_ptr<RibEntry> entry(ribIt->second); |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 98 | auto [entryIt, didInsert] = entry->insertRoute(route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 99 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 100 | if (didInsert) { |
| 101 | // The route was new and we successfully inserted it. |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 102 | m_nItems++; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 103 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 104 | afterAddRoute(RibRouteRef{entry, entryIt}); |
| 105 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 106 | // Register with face lookup table |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 107 | m_faceEntries.emplace(route.faceId, entry); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 108 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 109 | else { |
| 110 | // Route exists, update fields |
| 111 | // First cancel old scheduled event, if any, then set the EventId to new one |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 112 | if (entryIt->getExpirationEvent()) { |
| 113 | NFD_LOG_TRACE("Cancelling expiration event for " << entry->getName() << " " << *entryIt); |
| 114 | entryIt->cancelExpirationEvent(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 117 | *entryIt = route; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | else { |
| 121 | // New name prefix |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 122 | auto entry = make_shared<RibEntry>(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 123 | |
| 124 | m_rib[prefix] = entry; |
| 125 | m_nItems++; |
| 126 | |
| 127 | entry->setName(prefix); |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 128 | auto routeIt = entry->insertRoute(route).first; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 129 | |
| 130 | // Find prefix's parent |
| 131 | shared_ptr<RibEntry> parent = findParent(prefix); |
| 132 | |
| 133 | // Add self to parent's children |
| 134 | if (parent != nullptr) { |
| 135 | parent->addChild(entry); |
| 136 | } |
| 137 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 138 | auto children = findDescendants(prefix); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 139 | for (const auto& child : children) { |
| 140 | if (child->getParent() == parent) { |
| 141 | // Remove child from parent and inherit parent's child |
| 142 | if (parent != nullptr) { |
| 143 | parent->removeChild(child); |
| 144 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 145 | entry->addChild(child); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Register with face lookup table |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 150 | m_faceEntries.emplace(route.faceId, entry); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 151 | |
| 152 | // do something after inserting an entry |
| 153 | afterInsertEntry(prefix); |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 154 | afterAddRoute(RibRouteRef{entry, routeIt}); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 155 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 158 | void |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 159 | Rib::erase(const Name& prefix, const Route& route) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 160 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 161 | auto ribIt = m_rib.find(prefix); |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 162 | if (ribIt == m_rib.end()) { |
| 163 | // Name prefix does not exist |
| 164 | return; |
| 165 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 166 | |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 167 | shared_ptr<RibEntry> entry = ribIt->second; |
| 168 | auto routeIt = entry->findRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 169 | |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 170 | if (routeIt != entry->end()) { |
| 171 | beforeRemoveRoute(RibRouteRef{entry, routeIt}); |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 172 | |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 173 | auto faceId = route.faceId; |
| 174 | entry->eraseRoute(routeIt); |
| 175 | m_nItems--; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 176 | |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 177 | // If this RibEntry no longer has this faceId, unregister from face lookup table |
| 178 | if (!entry->hasFaceId(faceId)) { |
| 179 | auto range = m_faceEntries.equal_range(faceId); |
| 180 | for (auto it = range.first; it != range.second; ++it) { |
| 181 | if (it->second == entry) { |
| 182 | m_faceEntries.erase(it); |
| 183 | break; |
| 184 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 185 | } |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 186 | } |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 187 | |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 188 | // If a RibEntry's route list is empty, remove it from the tree |
| 189 | if (entry->getRoutes().empty()) { |
| 190 | eraseEntry(ribIt); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 191 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 192 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 196 | Rib::onRouteExpiration(const Name& prefix, const Route& route) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 197 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 198 | NFD_LOG_DEBUG(route << " for " << prefix << " has expired"); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 199 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 200 | RibUpdate update; |
| 201 | update.setAction(RibUpdate::UNREGISTER) |
| 202 | .setName(prefix) |
| 203 | .setRoute(route); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 204 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 205 | beginApplyUpdate(update, nullptr, nullptr); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | shared_ptr<RibEntry> |
| 209 | Rib::findParent(const Name& prefix) const |
| 210 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 211 | for (int i = prefix.size() - 1; i >= 0; i--) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 212 | auto it = m_rib.find(prefix.getPrefix(i)); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 213 | if (it != m_rib.end()) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 214 | return it->second; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 215 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 216 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 217 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 218 | return nullptr; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 221 | std::list<shared_ptr<RibEntry>> |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 222 | Rib::findDescendants(const Name& prefix) const |
| 223 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 224 | std::list<shared_ptr<RibEntry>> children; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 225 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 226 | auto it = m_rib.find(prefix); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 227 | if (it != m_rib.end()) { |
| 228 | ++it; |
| 229 | for (; it != m_rib.end(); ++it) { |
| 230 | if (prefix.isPrefixOf(it->first)) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 231 | children.push_back(it->second); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 232 | } |
| 233 | else { |
| 234 | break; |
| 235 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 236 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return children; |
| 240 | } |
| 241 | |
| 242 | std::list<shared_ptr<RibEntry>> |
| 243 | Rib::findDescendantsForNonInsertedName(const Name& prefix) const |
| 244 | { |
| 245 | std::list<shared_ptr<RibEntry>> children; |
| 246 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 247 | for (const auto& [name, ribEntry] : m_rib) { |
| 248 | if (prefix.isPrefixOf(name)) { |
| 249 | children.push_back(ribEntry); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 250 | } |
| 251 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 252 | |
| 253 | return children; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 256 | Rib::RibTable::iterator |
| 257 | Rib::eraseEntry(RibTable::iterator it) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 258 | { |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 259 | // Entry does not exist |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 260 | if (it == m_rib.end()) { |
| 261 | return m_rib.end(); |
| 262 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 263 | |
| 264 | shared_ptr<RibEntry> entry(it->second); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 265 | shared_ptr<RibEntry> parent = entry->getParent(); |
| 266 | |
| 267 | // Remove self from parent's children |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 268 | if (parent != nullptr) { |
| 269 | parent->removeChild(entry); |
| 270 | } |
| 271 | |
| 272 | for (auto childIt = entry->getChildren().begin(); childIt != entry->getChildren().end(); ) { |
| 273 | shared_ptr<RibEntry> child = *childIt; |
| 274 | |
| 275 | // Advance iterator so it is not invalidated by removal |
| 276 | ++childIt; |
| 277 | |
| 278 | // Remove children from self |
| 279 | entry->removeChild(child); |
| 280 | |
| 281 | // Update parent's children |
| 282 | if (parent != nullptr) { |
| 283 | parent->addChild(child); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 284 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 285 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 286 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 287 | auto nextIt = m_rib.erase(it); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 288 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 289 | // do something after erasing an entry |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 290 | afterEraseEntry(entry->getName()); |
| 291 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 292 | return nextIt; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 295 | Rib::RouteSet |
| 296 | Rib::getAncestorRoutes(const RibEntry& entry) const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 297 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 298 | RouteSet ancestorRoutes(&sortRoutes); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 299 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 300 | auto parent = entry.getParent(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 301 | while (parent != nullptr) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 302 | for (const auto& route : parent->getRoutes()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 303 | if (route.isChildInherit()) { |
| 304 | ancestorRoutes.insert(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 305 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 308 | if (parent->hasCapture()) { |
| 309 | break; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 310 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 311 | |
| 312 | parent = parent->getParent(); |
| 313 | } |
| 314 | |
| 315 | return ancestorRoutes; |
| 316 | } |
| 317 | |
| 318 | Rib::RouteSet |
| 319 | Rib::getAncestorRoutes(const Name& name) const |
| 320 | { |
| 321 | RouteSet ancestorRoutes(&sortRoutes); |
| 322 | |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 323 | auto parent = findParent(name); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 324 | while (parent != nullptr) { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 325 | for (const auto& route : parent->getRoutes()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 326 | if (route.isChildInherit()) { |
| 327 | ancestorRoutes.insert(route); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (parent->hasCapture()) { |
| 332 | break; |
| 333 | } |
| 334 | |
| 335 | parent = parent->getParent(); |
| 336 | } |
| 337 | |
| 338 | return ancestorRoutes; |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | Rib::beginApplyUpdate(const RibUpdate& update, |
| 343 | const Rib::UpdateSuccessCallback& onSuccess, |
| 344 | const Rib::UpdateFailureCallback& onFailure) |
| 345 | { |
| 346 | BOOST_ASSERT(m_fibUpdater != nullptr); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 347 | addUpdateToQueue(update, onSuccess, onFailure); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 348 | sendBatchFromQueue(); |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | Rib::beginRemoveFace(uint64_t faceId) |
| 353 | { |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 354 | auto range = m_faceEntries.equal_range(faceId); |
| 355 | for (auto it = range.first; it != range.second; ++it) { |
| 356 | enqueueRemoveFace(*it->second, faceId); |
| 357 | } |
| 358 | sendBatchFromQueue(); |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | Rib::beginRemoveFailedFaces(const std::set<uint64_t>& activeFaceIds) |
| 363 | { |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 364 | for (const auto& [faceId, ribEntry] : m_faceEntries) { |
| 365 | if (activeFaceIds.count(faceId) > 0) { |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 366 | continue; |
| 367 | } |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 368 | enqueueRemoveFace(*ribEntry, faceId); |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 369 | } |
| 370 | sendBatchFromQueue(); |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | Rib::enqueueRemoveFace(const RibEntry& entry, uint64_t faceId) |
| 375 | { |
| 376 | for (const Route& route : entry) { |
| 377 | if (route.faceId != faceId) { |
| 378 | continue; |
| 379 | } |
| 380 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 381 | RibUpdate update; |
| 382 | update.setAction(RibUpdate::REMOVE_FACE) |
Junxiao Shi | 17a7001 | 2019-06-25 10:50:32 +0000 | [diff] [blame] | 383 | .setName(entry.getName()) |
| 384 | .setRoute(route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 385 | addUpdateToQueue(update, nullptr, nullptr); |
| 386 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void |
| 390 | Rib::addUpdateToQueue(const RibUpdate& update, |
| 391 | const Rib::UpdateSuccessCallback& onSuccess, |
| 392 | const Rib::UpdateFailureCallback& onFailure) |
| 393 | { |
| 394 | RibUpdateBatch batch(update.getRoute().faceId); |
| 395 | batch.add(update); |
| 396 | |
| 397 | UpdateQueueItem item{batch, onSuccess, onFailure}; |
| 398 | m_updateBatches.push_back(std::move(item)); |
| 399 | } |
| 400 | |
| 401 | void |
| 402 | Rib::sendBatchFromQueue() |
| 403 | { |
| 404 | if (m_updateBatches.empty() || m_isUpdateInProgress) { |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | m_isUpdateInProgress = true; |
| 409 | |
| 410 | UpdateQueueItem item = std::move(m_updateBatches.front()); |
| 411 | m_updateBatches.pop_front(); |
| 412 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 413 | // Until task #1698, each RibUpdateBatch contains exactly one RIB update |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 414 | BOOST_ASSERT(item.batch.size() == 1); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 415 | |
Davide Pesavento | ae43030 | 2023-05-11 01:42:46 -0400 | [diff] [blame^] | 416 | m_fibUpdater->computeAndSendFibUpdates(item.batch, |
| 417 | [this, batch = item.batch, successCb = item.managerSuccessCallback] (const auto& routes) { |
| 418 | onFibUpdateSuccess(batch, routes, successCb); |
| 419 | }, |
| 420 | [this, failureCb = item.managerFailureCallback] (const auto& code, const auto& error) { |
| 421 | onFibUpdateFailure(failureCb, code, error); |
| 422 | }); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | void |
| 426 | Rib::onFibUpdateSuccess(const RibUpdateBatch& batch, |
| 427 | const RibUpdateList& inheritedRoutes, |
| 428 | const Rib::UpdateSuccessCallback& onSuccess) |
| 429 | { |
| 430 | for (const RibUpdate& update : batch) { |
| 431 | switch (update.getAction()) { |
| 432 | case RibUpdate::REGISTER: |
| 433 | insert(update.getName(), update.getRoute()); |
| 434 | break; |
| 435 | case RibUpdate::UNREGISTER: |
| 436 | case RibUpdate::REMOVE_FACE: |
| 437 | erase(update.getName(), update.getRoute()); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | // Add and remove precalculated inherited routes to RibEntries |
| 443 | modifyInheritedRoutes(inheritedRoutes); |
| 444 | |
| 445 | m_isUpdateInProgress = false; |
| 446 | |
| 447 | if (onSuccess != nullptr) { |
| 448 | onSuccess(); |
| 449 | } |
| 450 | |
| 451 | // Try to advance the batch queue |
| 452 | sendBatchFromQueue(); |
| 453 | } |
| 454 | |
| 455 | void |
| 456 | Rib::onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure, |
| 457 | uint32_t code, const std::string& error) |
| 458 | { |
| 459 | m_isUpdateInProgress = false; |
| 460 | |
| 461 | if (onFailure != nullptr) { |
| 462 | onFailure(code, error); |
| 463 | } |
| 464 | |
| 465 | // Try to advance the batch queue |
| 466 | sendBatchFromQueue(); |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | Rib::modifyInheritedRoutes(const RibUpdateList& inheritedRoutes) |
| 471 | { |
| 472 | for (const RibUpdate& update : inheritedRoutes) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 473 | auto ribIt = m_rib.find(update.getName()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 474 | BOOST_ASSERT(ribIt != m_rib.end()); |
| 475 | shared_ptr<RibEntry> entry(ribIt->second); |
| 476 | |
| 477 | switch (update.getAction()) { |
| 478 | case RibUpdate::REGISTER: |
| 479 | entry->addInheritedRoute(update.getRoute()); |
| 480 | break; |
| 481 | case RibUpdate::UNREGISTER: |
| 482 | entry->removeInheritedRoute(update.getRoute()); |
| 483 | break; |
| 484 | case RibUpdate::REMOVE_FACE: |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 490 | std::ostream& |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 491 | operator<<(std::ostream& os, const Rib& rib) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 492 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 493 | for (const auto& item : rib) { |
Weiwei Liu | aaa58a6 | 2016-11-28 23:15:15 -0700 | [diff] [blame] | 494 | os << *item.second << "\n"; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 495 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 496 | |
| 497 | return os; |
| 498 | } |
| 499 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 500 | } // namespace nfd::rib |