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