Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "auto-prefix-propagator.hpp" |
| 27 | #include "core/logger.hpp" |
| 28 | #include "core/scheduler.hpp" |
| 29 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace rib { |
| 34 | |
| 35 | NFD_LOG_INIT("AutoPrefixPropagator"); |
| 36 | |
| 37 | using ndn::nfd::ControlParameters; |
| 38 | using ndn::nfd::CommandOptions; |
| 39 | |
| 40 | const Name LOCAL_REGISTRATION_PREFIX("/localhost"); |
| 41 | const Name LINK_LOCAL_NFD_PREFIX("/localhop/nfd"); |
| 42 | const name::Component IGNORE_COMMPONENT("nrd"); |
| 43 | const time::seconds PREFIX_PROPAGATION_DEFAULT_REFRESH_INTERVAL = time::seconds(25); |
| 44 | const time::seconds PREFIX_PROPAGATION_MAX_REFRESH_INTERVAL = time::seconds(600); |
| 45 | const time::seconds PREFIX_PROPAGATION_DEFAULT_BASE_RETRY_WAIT = time::seconds(50); |
| 46 | const time::seconds PREFIX_PROPAGATION_DEFAULT_MAX_RETRY_WAIT = time::seconds(3600); |
| 47 | const uint64_t PREFIX_PROPAGATION_DEFAULT_COST = 15; |
| 48 | const time::milliseconds PREFIX_PROPAGATION_DEFAULT_TIMEOUT = time::milliseconds(10000); |
| 49 | |
| 50 | AutoPrefixPropagator::AutoPrefixPropagator(ndn::nfd::Controller& controller, |
| 51 | ndn::KeyChain& keyChain, |
| 52 | Rib& rib) |
| 53 | : m_nfdController(controller) |
| 54 | , m_keyChain(keyChain) |
| 55 | , m_rib(rib) |
| 56 | , m_refreshInterval(PREFIX_PROPAGATION_DEFAULT_REFRESH_INTERVAL) |
| 57 | , m_baseRetryWait(PREFIX_PROPAGATION_DEFAULT_BASE_RETRY_WAIT) |
| 58 | , m_maxRetryWait(PREFIX_PROPAGATION_DEFAULT_MAX_RETRY_WAIT) |
| 59 | , m_hasConnectedHub(false) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | AutoPrefixPropagator::loadConfig(const ConfigSection& configSection) |
| 65 | { |
| 66 | m_refreshInterval = PREFIX_PROPAGATION_DEFAULT_REFRESH_INTERVAL; |
| 67 | m_baseRetryWait = PREFIX_PROPAGATION_DEFAULT_BASE_RETRY_WAIT; |
| 68 | m_maxRetryWait = PREFIX_PROPAGATION_DEFAULT_MAX_RETRY_WAIT; |
| 69 | |
| 70 | m_controlParameters |
| 71 | .setCost(PREFIX_PROPAGATION_DEFAULT_COST) |
| 72 | .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client. |
| 73 | .setFaceId(0);// the remote hub will take the input face as the faceId. |
| 74 | |
| 75 | m_commandOptions |
| 76 | .setPrefix(LINK_LOCAL_NFD_PREFIX) |
| 77 | .setTimeout(PREFIX_PROPAGATION_DEFAULT_TIMEOUT); |
| 78 | |
| 79 | NFD_LOG_INFO("Load auto_prefix_propagate section in rib section"); |
| 80 | |
| 81 | for (auto&& i : configSection) { |
| 82 | if (i.first == "cost") { |
| 83 | m_controlParameters.setCost(i.second.get_value<uint64_t>()); |
| 84 | } |
| 85 | else if (i.first == "timeout") { |
| 86 | m_commandOptions.setTimeout(time::milliseconds(i.second.get_value<size_t>())); |
| 87 | } |
| 88 | else if (i.first == "refresh_interval") { |
| 89 | m_refreshInterval = std::min(PREFIX_PROPAGATION_MAX_REFRESH_INTERVAL, |
| 90 | time::seconds(i.second.get_value<size_t>())); |
| 91 | } |
| 92 | else if (i.first == "base_retry_wait") { |
| 93 | m_baseRetryWait = time::seconds(i.second.get_value<size_t>()); |
| 94 | } |
| 95 | else if (i.first == "max_retry_wait") { |
| 96 | m_maxRetryWait = time::seconds(i.second.get_value<size_t>()); |
| 97 | } |
| 98 | else { |
| 99 | BOOST_THROW_EXCEPTION(ConfigFile::Error("Unrecognized option \"" + i.first + |
| 100 | "\" in \"auto_prefix_propagate\" section")); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | AutoPrefixPropagator::enable() |
| 107 | { |
| 108 | m_afterInsertConnection = |
| 109 | m_rib.afterInsertEntry.connect(bind(&AutoPrefixPropagator::afterInsertRibEntry, this, _1)); |
| 110 | m_afterEraseConnection = |
| 111 | m_rib.afterEraseEntry.connect(bind(&AutoPrefixPropagator::afterEraseRibEntry, this, _1)); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | AutoPrefixPropagator::disable() |
| 116 | { |
| 117 | m_afterInsertConnection.disconnect(); |
| 118 | m_afterEraseConnection.disconnect(); |
| 119 | } |
| 120 | |
| 121 | AutoPrefixPropagator::PrefixPropagationParameters |
| 122 | AutoPrefixPropagator::getPrefixPropagationParameters(const Name& localRibPrefix) |
| 123 | { |
| 124 | // get all identities from the KeyChain |
| 125 | std::vector<Name> identities; |
| 126 | m_keyChain.getAllIdentities(identities, false); // get all except the default |
| 127 | identities.push_back(m_keyChain.getDefaultIdentity()); // get the default |
| 128 | |
| 129 | // shortest prefix matching to all identies. |
| 130 | Name propagatedPrefix, signingIdentity; |
| 131 | bool isFound = false; |
| 132 | for (auto&& i : identities) { |
| 133 | Name prefix = !i.empty() && IGNORE_COMMPONENT == i.at(-1) ? i.getPrefix(-1) : i; |
| 134 | if (prefix.isPrefixOf(localRibPrefix) && (!isFound || i.size() < signingIdentity.size())) { |
| 135 | isFound = true; |
| 136 | propagatedPrefix = prefix; |
| 137 | signingIdentity = i; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | PrefixPropagationParameters propagateParameters; |
| 142 | if (!isFound) { |
| 143 | propagateParameters.isValid = false; |
| 144 | } |
| 145 | else { |
| 146 | propagateParameters.isValid = true; |
| 147 | propagateParameters.parameters = m_controlParameters; |
| 148 | propagateParameters.options = m_commandOptions; |
| 149 | propagateParameters.parameters.setName(propagatedPrefix); |
| 150 | propagateParameters.options.setSigningInfo(signingByIdentity(signingIdentity)); |
| 151 | } |
| 152 | |
| 153 | return propagateParameters; |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | AutoPrefixPropagator::afterInsertRibEntry(const Name& prefix) |
| 158 | { |
| 159 | if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix)) { |
| 160 | NFD_LOG_INFO("local registration only for " << prefix); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if (prefix == LINK_LOCAL_NFD_PREFIX) { |
| 165 | NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix); |
| 166 | |
| 167 | m_hasConnectedHub = true; |
| 168 | return afterHubConnect(); |
| 169 | } |
| 170 | |
| 171 | auto propagateParameters = getPrefixPropagationParameters(prefix); |
| 172 | if (!propagateParameters.isValid) { |
| 173 | NFD_LOG_INFO("no signing identity available for: " << prefix); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | auto entryIt = m_propagatedEntries.find(propagateParameters.parameters.getName()); |
| 178 | if (entryIt != m_propagatedEntries.end()) { |
| 179 | BOOST_ASSERT(!entryIt->second.isNew()); |
| 180 | NFD_LOG_INFO("prefix has already been propagated: " |
| 181 | << propagateParameters.parameters.getName()); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | afterRibInsert(propagateParameters.parameters, propagateParameters.options); |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | AutoPrefixPropagator::afterEraseRibEntry(const Name& prefix) |
| 190 | { |
| 191 | if (LOCAL_REGISTRATION_PREFIX.isPrefixOf(prefix)) { |
| 192 | NFD_LOG_INFO("local unregistration only for " << prefix); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (prefix == LINK_LOCAL_NFD_PREFIX) { |
| 197 | NFD_LOG_INFO("disconnected to hub with prefix: " << prefix); |
| 198 | |
| 199 | m_hasConnectedHub = false; |
| 200 | return afterHubDisconnect(); |
| 201 | } |
| 202 | |
| 203 | auto propagateParameters = getPrefixPropagationParameters(prefix); |
| 204 | if (!propagateParameters.isValid) { |
| 205 | NFD_LOG_INFO("no signing identity available for: " << prefix); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | auto entryIt = m_propagatedEntries.find(propagateParameters.parameters.getName()); |
| 210 | if (entryIt == m_propagatedEntries.end()) { |
| 211 | NFD_LOG_INFO("prefix has not been propagated yet: " |
| 212 | << propagateParameters.parameters.getName()); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | for (auto&& ribTableEntry : m_rib) { |
| 217 | if (propagateParameters.parameters.getName().isPrefixOf(ribTableEntry.first) && |
| 218 | propagateParameters.options.getSigningInfo().getSignerName() == |
| 219 | getPrefixPropagationParameters(ribTableEntry.first) |
| 220 | .options.getSigningInfo().getSignerName()) { |
| 221 | NFD_LOG_INFO("should be kept for another RIB entry: " << ribTableEntry.first); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | afterRibErase(propagateParameters.parameters.unsetCost(), propagateParameters.options); |
| 227 | } |
| 228 | |
| 229 | bool |
| 230 | AutoPrefixPropagator::doesCurrentPropagatedPrefixWork(const Name& prefix) |
| 231 | { |
| 232 | auto propagateParameters = getPrefixPropagationParameters(prefix); |
| 233 | if (!propagateParameters.isValid) { |
| 234 | // no identity can sign the input prefix |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // there is at least one identity can sign the input prefix, so the prefix selected for |
| 239 | // propagation (i.e., propagateParameters.parameters.getName()) must be a prefix of the input |
| 240 | // prefix. Namely it's either equal to the input prefix or a better choice. |
| 241 | return propagateParameters.parameters.getName().size() == prefix.size(); |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | AutoPrefixPropagator::redoPropagation(PropagatedEntryIt entryIt, |
| 246 | const ControlParameters& parameters, |
| 247 | const CommandOptions& options, |
| 248 | time::seconds retryWaitTime) |
| 249 | { |
| 250 | if (doesCurrentPropagatedPrefixWork(parameters.getName())) { |
| 251 | // PROPAGATED / PROPAGATE_FAIL --> PROPAGATING |
| 252 | entryIt->second.startPropagation(); |
| 253 | return startPropagation(parameters, options, retryWaitTime); |
| 254 | } |
| 255 | |
| 256 | NFD_LOG_INFO("current propagated prefix does not work any more"); |
| 257 | m_propagatedEntries.erase(entryIt); |
| 258 | |
| 259 | // re-handle all locally RIB entries that can be covered by this propagated prefix |
| 260 | for (auto&& ribTableEntry : m_rib) { |
| 261 | if (parameters.getName().isPrefixOf(ribTableEntry.first)) { |
| 262 | afterInsertRibEntry(ribTableEntry.first); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void |
| 268 | AutoPrefixPropagator::startPropagation(const ControlParameters& parameters, |
| 269 | const CommandOptions& options, |
| 270 | time::seconds retryWaitTime) |
| 271 | { |
| 272 | NFD_LOG_TRACE("start propagate " << parameters.getName()); |
| 273 | |
| 274 | ndn::Scheduler::Event refreshEvent = |
| 275 | bind(&AutoPrefixPropagator::onRefreshTimer, this, parameters, options); |
| 276 | ndn::Scheduler::Event retryEvent = |
| 277 | bind(&AutoPrefixPropagator::onRetryTimer, this, parameters, options, |
| 278 | std::min(m_maxRetryWait, retryWaitTime * 2)); |
| 279 | |
| 280 | m_nfdController.start<ndn::nfd::RibRegisterCommand>( |
| 281 | parameters, |
| 282 | bind(&AutoPrefixPropagator::afterPropagateSucceed, this, parameters, options, refreshEvent), |
| 283 | bind(&AutoPrefixPropagator::afterPropagateFail, |
| 284 | this, _1, _2, parameters, options, retryWaitTime, retryEvent), |
| 285 | options); |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | AutoPrefixPropagator::startRevocation(const ControlParameters& parameters, |
| 290 | const CommandOptions& options, |
| 291 | time::seconds retryWaitTime) |
| 292 | { |
| 293 | NFD_LOG_INFO("start revoke propagation of " << parameters.getName()); |
| 294 | |
| 295 | m_nfdController.start<ndn::nfd::RibUnregisterCommand>( |
| 296 | parameters, |
| 297 | bind(&AutoPrefixPropagator::afterRevokeSucceed, this, parameters, options, retryWaitTime), |
| 298 | bind(&AutoPrefixPropagator::afterRevokeFail, this, _1, _2, parameters, options), |
| 299 | options); |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | AutoPrefixPropagator::afterRibInsert(const ControlParameters& parameters, |
| 304 | const CommandOptions& options) |
| 305 | { |
| 306 | BOOST_ASSERT(m_propagatedEntries.find(parameters.getName()) == m_propagatedEntries.end()); |
| 307 | |
| 308 | // keep valid entries although there is no connectivity to hub |
| 309 | auto& entry = m_propagatedEntries[parameters.getName()] |
| 310 | .setSigningIdentity(options.getSigningInfo().getSignerName()); |
| 311 | |
| 312 | if (!m_hasConnectedHub) { |
| 313 | NFD_LOG_INFO("no hub connected to propagate " << parameters.getName()); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | // NEW --> PROPAGATING |
| 318 | entry.startPropagation(); |
| 319 | startPropagation(parameters, options, m_baseRetryWait); |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | AutoPrefixPropagator::afterRibErase(const ControlParameters& parameters, |
| 324 | const CommandOptions& options) |
| 325 | { |
| 326 | auto entryIt = m_propagatedEntries.find(parameters.getName()); |
| 327 | BOOST_ASSERT(entryIt != m_propagatedEntries.end()); |
| 328 | |
| 329 | bool hasPropagationSucceeded = entryIt->second.isPropagated(); |
| 330 | |
| 331 | // --> "RELEASED" |
| 332 | m_propagatedEntries.erase(entryIt); |
| 333 | |
| 334 | if (!m_hasConnectedHub) { |
| 335 | NFD_LOG_INFO("no hub connected to revoke propagation of " << parameters.getName()); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | if (!hasPropagationSucceeded) { |
| 340 | NFD_LOG_INFO("propagation has not succeeded: " << parameters.getName()); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | startRevocation(parameters, options, m_baseRetryWait); |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | AutoPrefixPropagator::afterHubConnect() |
| 349 | { |
| 350 | NFD_LOG_INFO("redo " << m_propagatedEntries.size() |
| 351 | << " propagations when new Hub connectivity is built."); |
| 352 | |
| 353 | std::vector<PropagatedEntryIt> regEntryIterators; |
| 354 | for (auto it = m_propagatedEntries.begin() ; it != m_propagatedEntries.end() ; it ++) { |
| 355 | BOOST_ASSERT(it->second.isNew()); |
| 356 | regEntryIterators.push_back(it); |
| 357 | } |
| 358 | |
| 359 | for (auto&& it : regEntryIterators) { |
| 360 | auto parameters = m_controlParameters; |
| 361 | auto options = m_commandOptions; |
| 362 | |
| 363 | redoPropagation(it, |
| 364 | parameters.setName(it->first), |
| 365 | options.setSigningInfo(signingByIdentity(it->second.getSigningIdentity())), |
| 366 | m_baseRetryWait); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | AutoPrefixPropagator::afterHubDisconnect() |
| 372 | { |
| 373 | for (auto&& entry : m_propagatedEntries) { |
| 374 | // --> NEW |
| 375 | BOOST_ASSERT(!entry.second.isNew()); |
| 376 | entry.second.initialize(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void |
| 381 | AutoPrefixPropagator::afterPropagateSucceed(const ControlParameters& parameters, |
| 382 | const CommandOptions& options, |
| 383 | const ndn::Scheduler::Event& refreshEvent) |
| 384 | { |
| 385 | NFD_LOG_TRACE("success to propagate " << parameters.getName()); |
| 386 | |
| 387 | auto entryIt = m_propagatedEntries.find(parameters.getName()); |
| 388 | if (entryIt == m_propagatedEntries.end()) { |
| 389 | // propagation should be revoked if this entry has been erased (i.e., be in RELEASED state) |
| 390 | NFD_LOG_DEBUG("Already erased!"); |
| 391 | ControlParameters newParameters = parameters; |
| 392 | return startRevocation(newParameters.unsetCost(), options, m_baseRetryWait); |
| 393 | } |
| 394 | |
| 395 | // PROPAGATING --> PROPAGATED |
| 396 | BOOST_ASSERT(entryIt->second.isPropagating()); |
| 397 | entryIt->second.succeed(scheduler::schedule(m_refreshInterval, refreshEvent)); |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | AutoPrefixPropagator::afterPropagateFail(uint32_t code, const std::string& reason, |
| 402 | const ControlParameters& parameters, |
| 403 | const CommandOptions& options, |
| 404 | time::seconds retryWaitTime, |
| 405 | const ndn::Scheduler::Event& retryEvent) |
| 406 | { |
| 407 | NFD_LOG_TRACE("fail to propagate " << parameters.getName() |
| 408 | << "\n\t reason:" << reason |
| 409 | << "\n\t retry wait time: " << retryWaitTime); |
| 410 | |
| 411 | auto entryIt = m_propagatedEntries.find(parameters.getName()); |
| 412 | if (entryIt == m_propagatedEntries.end()) { |
| 413 | // current state is RELEASED |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | // PROPAGATING --> PROPAGATE_FAIL |
| 418 | BOOST_ASSERT(entryIt->second.isPropagating()); |
| 419 | entryIt->second.fail(scheduler::schedule(retryWaitTime, retryEvent)); |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | AutoPrefixPropagator::afterRevokeSucceed(const ControlParameters& parameters, |
| 424 | const CommandOptions& options, |
| 425 | time::seconds retryWaitTime) |
| 426 | { |
| 427 | NFD_LOG_TRACE("success to revoke propagation of " << parameters.getName()); |
| 428 | |
| 429 | auto entryIt = m_propagatedEntries.find(parameters.getName()); |
| 430 | if (m_propagatedEntries.end() != entryIt && !entryIt->second.isPropagateFail()) { |
| 431 | // if is not RELEASED or PROPAGATE_FAIL |
| 432 | NFD_LOG_DEBUG("propagated entry still exists"); |
| 433 | |
| 434 | // PROPAGATING / PROPAGATED --> PROPAGATING |
| 435 | BOOST_ASSERT(!entryIt->second.isNew()); |
| 436 | entryIt->second.startPropagation(); |
| 437 | |
| 438 | ControlParameters newParameters = parameters; |
| 439 | startPropagation(newParameters.setCost(m_controlParameters.getCost()), options, retryWaitTime); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | void |
| 444 | AutoPrefixPropagator::afterRevokeFail(uint32_t code, const std::string& reason, |
| 445 | const ControlParameters& parameters, |
| 446 | const CommandOptions& options) |
| 447 | { |
| 448 | NFD_LOG_INFO("fail to revoke the propagation of " << parameters.getName() |
| 449 | << "\n\t reason:" << reason); |
| 450 | } |
| 451 | |
| 452 | void |
| 453 | AutoPrefixPropagator::onRefreshTimer(const ControlParameters& parameters, |
| 454 | const CommandOptions& options) |
| 455 | { |
| 456 | auto entryIt = m_propagatedEntries.find(parameters.getName()); |
| 457 | BOOST_ASSERT(entryIt != m_propagatedEntries.end() && entryIt->second.isPropagated()); |
| 458 | redoPropagation(entryIt, parameters, options, m_baseRetryWait); |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | AutoPrefixPropagator::onRetryTimer(const ControlParameters& parameters, |
| 463 | const CommandOptions& options, |
| 464 | time::seconds retryWaitTime) |
| 465 | { |
| 466 | auto entryIt = m_propagatedEntries.find(parameters.getName()); |
| 467 | BOOST_ASSERT(entryIt != m_propagatedEntries.end() && entryIt->second.isPropagateFail()); |
| 468 | redoPropagation(entryIt, parameters, options, retryWaitTime); |
| 469 | } |
| 470 | |
| 471 | } // namespace rib |
| 472 | } // namespace nfd |