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