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 | |
| 116 | // 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] | 117 | entryIt->setExpirationEvent(route.getExpirationEvent()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 118 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 119 | entryIt->flags = route.flags; |
| 120 | entryIt->cost = route.cost; |
| 121 | entryIt->expires = route.expires; |
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 |
| 156 | m_faceMap[route.faceId].push_back(entry); |
| 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); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 168 | |
| 169 | // Name prefix exists |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 170 | if (ribIt != m_rib.end()) { |
Davide Pesavento | e94804b | 2016-09-19 17:23:21 +0000 | [diff] [blame] | 171 | shared_ptr<RibEntry> entry = ribIt->second; |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 172 | auto routeIt = entry->findRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 173 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 174 | if (routeIt != entry->end()) { |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 175 | beforeRemoveRoute(RibRouteRef{entry, routeIt}); |
| 176 | |
Davide Pesavento | e94804b | 2016-09-19 17:23:21 +0000 | [diff] [blame] | 177 | auto faceId = route.faceId; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 178 | entry->eraseRoute(routeIt); |
| 179 | m_nItems--; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 180 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 181 | // 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] | 182 | if (!entry->hasFaceId(faceId)) { |
| 183 | m_faceMap[faceId].remove(entry); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 184 | } |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 185 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 186 | // 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] | 187 | if (entry->getRoutes().empty()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 188 | eraseEntry(ribIt); |
| 189 | } |
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 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 195 | Rib::onRouteExpiration(const Name& prefix, const Route& route) |
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 | NFD_LOG_DEBUG(route << " for " << prefix << " has expired"); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 198 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 199 | RibUpdate update; |
| 200 | update.setAction(RibUpdate::UNREGISTER) |
| 201 | .setName(prefix) |
| 202 | .setRoute(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 | beginApplyUpdate(update, nullptr, nullptr); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | shared_ptr<RibEntry> |
| 208 | Rib::findParent(const Name& prefix) const |
| 209 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 210 | for (int i = prefix.size() - 1; i >= 0; i--) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 211 | auto it = m_rib.find(prefix.getPrefix(i)); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 212 | if (it != m_rib.end()) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 213 | return it->second; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 214 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 215 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 216 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 217 | return nullptr; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 220 | std::list<shared_ptr<RibEntry>> |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 221 | Rib::findDescendants(const Name& prefix) const |
| 222 | { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 223 | std::list<shared_ptr<RibEntry>> children; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 224 | |
| 225 | RibTable::const_iterator it = m_rib.find(prefix); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 226 | if (it != m_rib.end()) { |
| 227 | ++it; |
| 228 | for (; it != m_rib.end(); ++it) { |
| 229 | if (prefix.isPrefixOf(it->first)) { |
| 230 | children.push_back((it->second)); |
| 231 | } |
| 232 | else { |
| 233 | break; |
| 234 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 235 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | return children; |
| 239 | } |
| 240 | |
| 241 | std::list<shared_ptr<RibEntry>> |
| 242 | Rib::findDescendantsForNonInsertedName(const Name& prefix) const |
| 243 | { |
| 244 | std::list<shared_ptr<RibEntry>> children; |
| 245 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 246 | for (const auto& pair : m_rib) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 247 | if (prefix.isPrefixOf(pair.first)) { |
| 248 | children.push_back(pair.second); |
| 249 | } |
| 250 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 251 | |
| 252 | return children; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 255 | Rib::RibTable::iterator |
| 256 | Rib::eraseEntry(RibTable::iterator it) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 257 | { |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 258 | // Entry does not exist |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 259 | if (it == m_rib.end()) { |
| 260 | return m_rib.end(); |
| 261 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 262 | |
| 263 | shared_ptr<RibEntry> entry(it->second); |
| 264 | |
| 265 | shared_ptr<RibEntry> parent = entry->getParent(); |
| 266 | |
| 267 | // Remove self from parent's children |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 268 | if (parent != nullptr) { |
| 269 | parent->removeChild(entry); |
| 270 | } |
| 271 | |
| 272 | for (auto childIt = entry->getChildren().begin(); childIt != entry->getChildren().end(); ) { |
| 273 | shared_ptr<RibEntry> child = *childIt; |
| 274 | |
| 275 | // Advance iterator so it is not invalidated by removal |
| 276 | ++childIt; |
| 277 | |
| 278 | // Remove children from self |
| 279 | entry->removeChild(child); |
| 280 | |
| 281 | // Update parent's children |
| 282 | if (parent != nullptr) { |
| 283 | parent->addChild(child); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 284 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 285 | } |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 286 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 287 | auto nextIt = m_rib.erase(it); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 288 | |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 289 | // do something after erasing an entry. |
| 290 | afterEraseEntry(entry->getName()); |
| 291 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 292 | return nextIt; |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 295 | Rib::RouteSet |
| 296 | Rib::getAncestorRoutes(const RibEntry& entry) const |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 297 | { |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 298 | RouteSet ancestorRoutes(&sortRoutes); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 299 | |
| 300 | shared_ptr<RibEntry> parent = entry.getParent(); |
| 301 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 302 | while (parent != nullptr) { |
| 303 | for (const Route& route : parent->getRoutes()) { |
| 304 | if (route.isChildInherit()) { |
| 305 | ancestorRoutes.insert(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 306 | } |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 307 | } |
| 308 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 309 | if (parent->hasCapture()) { |
| 310 | break; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 311 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 312 | |
| 313 | parent = parent->getParent(); |
| 314 | } |
| 315 | |
| 316 | return ancestorRoutes; |
| 317 | } |
| 318 | |
| 319 | Rib::RouteSet |
| 320 | Rib::getAncestorRoutes(const Name& name) const |
| 321 | { |
| 322 | RouteSet ancestorRoutes(&sortRoutes); |
| 323 | |
| 324 | shared_ptr<RibEntry> parent = findParent(name); |
| 325 | |
| 326 | while (parent != nullptr) { |
| 327 | for (const Route& route : parent->getRoutes()) { |
| 328 | if (route.isChildInherit()) { |
| 329 | ancestorRoutes.insert(route); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (parent->hasCapture()) { |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | parent = parent->getParent(); |
| 338 | } |
| 339 | |
| 340 | return ancestorRoutes; |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | Rib::beginApplyUpdate(const RibUpdate& update, |
| 345 | const Rib::UpdateSuccessCallback& onSuccess, |
| 346 | const Rib::UpdateFailureCallback& onFailure) |
| 347 | { |
| 348 | BOOST_ASSERT(m_fibUpdater != nullptr); |
| 349 | |
| 350 | addUpdateToQueue(update, onSuccess, onFailure); |
| 351 | |
| 352 | sendBatchFromQueue(); |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | Rib::beginRemoveFace(uint64_t faceId) |
| 357 | { |
| 358 | for (const auto& nameAndRoute : findRoutesWithFaceId(faceId)) { |
| 359 | RibUpdate update; |
| 360 | update.setAction(RibUpdate::REMOVE_FACE) |
| 361 | .setName(nameAndRoute.first) |
| 362 | .setRoute(nameAndRoute.second); |
| 363 | |
| 364 | addUpdateToQueue(update, nullptr, nullptr); |
| 365 | } |
| 366 | |
| 367 | sendBatchFromQueue(); |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | Rib::addUpdateToQueue(const RibUpdate& update, |
| 372 | const Rib::UpdateSuccessCallback& onSuccess, |
| 373 | const Rib::UpdateFailureCallback& onFailure) |
| 374 | { |
| 375 | RibUpdateBatch batch(update.getRoute().faceId); |
| 376 | batch.add(update); |
| 377 | |
| 378 | UpdateQueueItem item{batch, onSuccess, onFailure}; |
| 379 | m_updateBatches.push_back(std::move(item)); |
| 380 | } |
| 381 | |
| 382 | void |
| 383 | Rib::sendBatchFromQueue() |
| 384 | { |
| 385 | if (m_updateBatches.empty() || m_isUpdateInProgress) { |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | m_isUpdateInProgress = true; |
| 390 | |
| 391 | UpdateQueueItem item = std::move(m_updateBatches.front()); |
| 392 | m_updateBatches.pop_front(); |
| 393 | |
| 394 | RibUpdateBatch& batch = item.batch; |
| 395 | |
| 396 | // Until task #1698, each RibUpdateBatch contains exactly one RIB update |
| 397 | BOOST_ASSERT(batch.size() == 1); |
| 398 | |
| 399 | const Rib::UpdateSuccessCallback& managerSuccessCallback = item.managerSuccessCallback; |
| 400 | const Rib::UpdateFailureCallback& managerFailureCallback = item.managerFailureCallback; |
| 401 | |
| 402 | m_fibUpdater->computeAndSendFibUpdates(batch, |
| 403 | bind(&Rib::onFibUpdateSuccess, this, |
| 404 | batch, _1, managerSuccessCallback), |
| 405 | bind(&Rib::onFibUpdateFailure, this, |
| 406 | managerFailureCallback, _1, _2)); |
| 407 | |
| 408 | if (m_onSendBatchFromQueue != nullptr) { |
| 409 | m_onSendBatchFromQueue(batch); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void |
| 414 | Rib::onFibUpdateSuccess(const RibUpdateBatch& batch, |
| 415 | const RibUpdateList& inheritedRoutes, |
| 416 | const Rib::UpdateSuccessCallback& onSuccess) |
| 417 | { |
| 418 | for (const RibUpdate& update : batch) { |
| 419 | switch (update.getAction()) { |
| 420 | case RibUpdate::REGISTER: |
| 421 | insert(update.getName(), update.getRoute()); |
| 422 | break; |
| 423 | case RibUpdate::UNREGISTER: |
| 424 | case RibUpdate::REMOVE_FACE: |
| 425 | erase(update.getName(), update.getRoute()); |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Add and remove precalculated inherited routes to RibEntries |
| 431 | modifyInheritedRoutes(inheritedRoutes); |
| 432 | |
| 433 | m_isUpdateInProgress = false; |
| 434 | |
| 435 | if (onSuccess != nullptr) { |
| 436 | onSuccess(); |
| 437 | } |
| 438 | |
| 439 | // Try to advance the batch queue |
| 440 | sendBatchFromQueue(); |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | Rib::onFibUpdateFailure(const Rib::UpdateFailureCallback& onFailure, |
| 445 | uint32_t code, const std::string& error) |
| 446 | { |
| 447 | m_isUpdateInProgress = false; |
| 448 | |
| 449 | if (onFailure != nullptr) { |
| 450 | onFailure(code, error); |
| 451 | } |
| 452 | |
| 453 | // Try to advance the batch queue |
| 454 | sendBatchFromQueue(); |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | Rib::modifyInheritedRoutes(const RibUpdateList& inheritedRoutes) |
| 459 | { |
| 460 | for (const RibUpdate& update : inheritedRoutes) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 461 | auto ribIt = m_rib.find(update.getName()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 462 | BOOST_ASSERT(ribIt != m_rib.end()); |
| 463 | shared_ptr<RibEntry> entry(ribIt->second); |
| 464 | |
| 465 | switch (update.getAction()) { |
| 466 | case RibUpdate::REGISTER: |
| 467 | entry->addInheritedRoute(update.getRoute()); |
| 468 | break; |
| 469 | case RibUpdate::UNREGISTER: |
| 470 | entry->removeInheritedRoute(update.getRoute()); |
| 471 | break; |
| 472 | case RibUpdate::REMOVE_FACE: |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | std::list<Rib::NameAndRoute> |
| 479 | Rib::findRoutesWithFaceId(uint64_t faceId) |
| 480 | { |
| 481 | std::list<NameAndRoute> routes; |
| 482 | |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 483 | auto lookupIt = m_faceMap.find(faceId); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 484 | if (lookupIt == m_faceMap.end()) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 485 | // No RIB entries have this face |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 486 | return routes; |
| 487 | } |
| 488 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 489 | // For each RIB entry that has faceId |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 490 | for (const auto& entry : lookupIt->second) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 491 | // Find the routes in the entry |
| 492 | for (const Route& route : *entry) { |
| 493 | if (route.faceId == faceId) { |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 494 | routes.emplace_back(entry->getName(), route); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return routes; |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 502 | std::ostream& |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 503 | operator<<(std::ostream& os, const Rib& rib) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 504 | { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 505 | for (const auto& item : rib) { |
Weiwei Liu | aaa58a6 | 2016-11-28 23:15:15 -0700 | [diff] [blame] | 506 | os << *item.second << "\n"; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 507 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 508 | |
| 509 | return os; |
| 510 | } |
| 511 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 512 | } // namespace rib |
| 513 | } // namespace nfd |