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