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