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