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