Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | 1e46be3 | 2015-01-08 20:18:05 -0700 | [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-manager.hpp" |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 27 | #include "readvertise/readvertise.hpp" |
| 28 | #include "readvertise/client-to-nlsr-readvertise-policy.hpp" |
| 29 | #include "readvertise/nfd-rib-readvertise-destination.hpp" |
| 30 | |
Alexander Afanasyev | 03ea3eb | 2014-04-17 18:19:06 -0700 | [diff] [blame] | 31 | #include "core/global-io.hpp" |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 32 | #include "core/logger.hpp" |
Alexander Afanasyev | 63108c4 | 2014-07-07 19:10:47 -0700 | [diff] [blame] | 33 | #include "core/scheduler.hpp" |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 34 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/lp/tags.hpp> |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 36 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
| 37 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
| 38 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 39 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
| 40 | #include <ndn-cxx/mgmt/nfd/rib-entry.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 41 | |
| 42 | namespace nfd { |
| 43 | namespace rib { |
| 44 | |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 45 | using ndn::nfd::ControlCommand; |
| 46 | using ndn::nfd::ControlResponse; |
| 47 | using ndn::nfd::ControlParameters; |
| 48 | using ndn::nfd::FaceEventNotification; |
| 49 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 50 | NFD_LOG_INIT("RibManager"); |
| 51 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 52 | const Name RibManager::LOCAL_HOST_TOP_PREFIX = "/localhost/nfd"; |
| 53 | const Name RibManager::LOCAL_HOP_TOP_PREFIX = "/localhop/nfd"; |
Junxiao Shi | fde3f54 | 2016-07-10 19:54:53 +0000 | [diff] [blame] | 54 | const std::string RibManager::MGMT_MODULE_NAME = "rib"; |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 55 | const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list"; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 56 | const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300); |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 57 | const Name RibManager::READVERTISE_NLSR_PREFIX = "/localhost/nlsr"; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 58 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 59 | RibManager::RibManager(Dispatcher& dispatcher, |
| 60 | ndn::Face& face, |
| 61 | ndn::KeyChain& keyChain) |
Junxiao Shi | fde3f54 | 2016-07-10 19:54:53 +0000 | [diff] [blame] | 62 | : ManagerBase(dispatcher, MGMT_MODULE_NAME) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 63 | , m_face(face) |
Vince Lehman | c1dfdb4 | 2015-07-16 12:17:36 -0500 | [diff] [blame] | 64 | , m_keyChain(keyChain) |
Junxiao Shi | 8e273ca | 2014-11-12 00:42:29 -0700 | [diff] [blame] | 65 | , m_nfdController(m_face, m_keyChain) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 66 | , m_faceMonitor(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 67 | , m_localhostValidator(m_face) |
| 68 | , m_localhopValidator(m_face) |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 69 | , m_isLocalhopEnabled(false) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 70 | , m_prefixPropagator(m_nfdController, m_keyChain, m_rib) |
| 71 | , m_fibUpdater(m_rib, m_nfdController) |
| 72 | , m_addTopPrefix([&dispatcher] (const Name& topPrefix) { |
| 73 | dispatcher.addTopPrefix(topPrefix, false); |
| 74 | }) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 75 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 76 | registerCommandHandler<ndn::nfd::RibRegisterCommand>("register", |
| 77 | bind(&RibManager::registerEntry, this, _2, _3, _4, _5)); |
| 78 | registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister", |
| 79 | bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5)); |
| 80 | |
| 81 | registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 84 | RibManager::~RibManager() |
| 85 | { |
| 86 | scheduler::cancel(m_activeFaceFetchEvent); |
| 87 | } |
| 88 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 89 | void |
| 90 | RibManager::registerWithNfd() |
| 91 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 92 | registerTopPrefix(LOCAL_HOST_TOP_PREFIX); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 94 | if (m_isLocalhopEnabled) { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 95 | registerTopPrefix(LOCAL_HOP_TOP_PREFIX); |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 96 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 98 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Junxiao Shi | fbf7834 | 2015-01-23 14:46:41 -0700 | [diff] [blame] | 99 | m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1)); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 100 | m_faceMonitor.start(); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 101 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 102 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 106 | RibManager::enableLocalFields() |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 107 | { |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 108 | m_nfdController.start<ndn::nfd::FaceUpdateCommand>( |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 109 | ControlParameters() |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 110 | .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true), |
| 111 | bind(&RibManager::onEnableLocalFieldsSuccess, this), |
| 112 | bind(&RibManager::onEnableLocalFieldsError, this, _1)); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 116 | RibManager::setConfigFile(ConfigFile& configFile) |
| 117 | { |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 118 | configFile.addSectionHandler("rib", |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 119 | bind(&RibManager::onConfig, this, _1, _2, _3)); |
| 120 | } |
| 121 | |
| 122 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 123 | RibManager::onRibUpdateSuccess(const RibUpdate& update) |
| 124 | { |
| 125 | NFD_LOG_DEBUG("RIB update succeeded for " << update); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | RibManager::onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error) |
| 130 | { |
| 131 | NFD_LOG_DEBUG("RIB update failed for " << update << " (code: " << code |
| 132 | << ", error: " << error << ")"); |
| 133 | |
| 134 | // Since the FIB rejected the update, clean up invalid routes |
| 135 | scheduleActiveFaceFetch(time::seconds(1)); |
| 136 | } |
| 137 | |
| 138 | void |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 139 | RibManager::onConfig(const ConfigSection& configSection, |
Yingdi Yu | f4db0b5 | 2014-04-17 13:17:39 -0700 | [diff] [blame] | 140 | bool isDryRun, |
| 141 | const std::string& filename) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 142 | { |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 143 | bool isAutoPrefixPropagatorEnabled = false; |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 144 | bool wantReadvertiseToNlsr = false; |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 145 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 146 | for (const auto& item : configSection) { |
| 147 | if (item.first == "localhost_security") { |
| 148 | m_localhostValidator.load(item.second, filename); |
Yingdi Yu | e5224e9 | 2014-04-29 18:04:02 -0700 | [diff] [blame] | 149 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 150 | else if (item.first == "localhop_security") { |
| 151 | m_localhopValidator.load(item.second, filename); |
| 152 | m_isLocalhopEnabled = true; |
Yanbiao Li | b9d439d | 2014-12-11 16:12:32 -0800 | [diff] [blame] | 153 | } |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 154 | else if (item.first == "auto_prefix_propagate") { |
| 155 | m_prefixPropagator.loadConfig(item.second); |
| 156 | isAutoPrefixPropagatorEnabled = true; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 157 | |
| 158 | // Avoid other actions when isDryRun == true |
| 159 | if (isDryRun) { |
| 160 | continue; |
| 161 | } |
| 162 | |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 163 | m_prefixPropagator.enable(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 164 | } |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 165 | else if (item.first == "readvertise_nlsr") { |
| 166 | wantReadvertiseToNlsr = ConfigFile::parseYesNo(item, "rib.readvertise_nlsr"); |
| 167 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 168 | else { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first)); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Yanbiao Li | d7c9636 | 2015-01-30 23:58:24 -0800 | [diff] [blame] | 173 | if (!isAutoPrefixPropagatorEnabled) { |
| 174 | m_prefixPropagator.disable(); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 175 | } |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 176 | |
| 177 | if (wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) { |
| 178 | NFD_LOG_DEBUG("Enabling readvertise-to-nlsr."); |
| 179 | m_readvertiseNlsr.reset(new Readvertise( |
| 180 | m_rib, |
| 181 | make_unique<ClientToNlsrReadvertisePolicy>(), |
| 182 | make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib))); |
| 183 | } |
| 184 | else if (!wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) { |
| 185 | NFD_LOG_DEBUG("Disabling readvertise-to-nlsr."); |
| 186 | m_readvertiseNlsr.reset(); |
| 187 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 191 | RibManager::registerTopPrefix(const Name& topPrefix) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 192 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 193 | // register entry to the FIB |
| 194 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
| 195 | ControlParameters() |
Junxiao Shi | fde3f54 | 2016-07-10 19:54:53 +0000 | [diff] [blame] | 196 | .setName(Name(topPrefix).append(MGMT_MODULE_NAME)) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 197 | .setFaceId(0), |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 198 | bind(&RibManager::onCommandPrefixAddNextHopSuccess, this, cref(topPrefix), _1), |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 199 | bind(&RibManager::onCommandPrefixAddNextHopError, this, cref(topPrefix), _1)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 200 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 201 | // add top prefix to the dispatcher |
| 202 | m_addTopPrefix(topPrefix); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 206 | RibManager::registerEntry(const Name& topPrefix, const Interest& interest, |
| 207 | ControlParameters parameters, |
| 208 | const ndn::mgmt::CommandContinuation& done) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 209 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 210 | setFaceForSelfRegistration(interest, parameters); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 211 | |
| 212 | // Respond since command is valid and authorized |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 213 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 214 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 215 | Route route; |
| 216 | route.faceId = parameters.getFaceId(); |
| 217 | route.origin = parameters.getOrigin(); |
| 218 | route.cost = parameters.getCost(); |
| 219 | route.flags = parameters.getFlags(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 220 | |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 221 | if (parameters.hasExpirationPeriod() && |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame^] | 222 | parameters.getExpirationPeriod() != time::milliseconds::max()) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 223 | route.expires = time::steady_clock::now() + parameters.getExpirationPeriod(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 224 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 225 | // Schedule a new event, the old one will be cancelled during rib insertion. |
| 226 | scheduler::EventId eventId = scheduler::schedule(parameters.getExpirationPeriod(), |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 227 | bind(&Rib::onRouteExpiration, &m_rib, parameters.getName(), route)); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 228 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 229 | NFD_LOG_TRACE("Scheduled unregistration at: " << route.expires << |
| 230 | " with EventId: " << eventId); |
| 231 | |
| 232 | // Set the NewEventId of this entry |
| 233 | route.setExpirationEvent(eventId); |
| 234 | } |
| 235 | else { |
| 236 | route.expires = time::steady_clock::TimePoint::max(); |
| 237 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 238 | |
Vince Lehman | ff8b397 | 2015-02-20 16:51:21 -0600 | [diff] [blame] | 239 | NFD_LOG_INFO("Adding route " << parameters.getName() << " nexthop=" << route.faceId |
| 240 | << " origin=" << route.origin |
| 241 | << " cost=" << route.cost); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 242 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 243 | RibUpdate update; |
| 244 | update.setAction(RibUpdate::REGISTER) |
| 245 | .setName(parameters.getName()) |
| 246 | .setRoute(route); |
| 247 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 248 | m_rib.beginApplyUpdate(update, |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 249 | bind(&RibManager::onRibUpdateSuccess, this, update), |
| 250 | bind(&RibManager::onRibUpdateFailure, this, update, _1, _2)); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 251 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 252 | m_registeredFaces.insert(route.faceId); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 256 | RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest, |
| 257 | ControlParameters parameters, |
| 258 | const ndn::mgmt::CommandContinuation& done) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 259 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 260 | setFaceForSelfRegistration(interest, parameters); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 261 | |
| 262 | // Respond since command is valid and authorized |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 263 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 264 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 265 | Route route; |
| 266 | route.faceId = parameters.getFaceId(); |
| 267 | route.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 268 | |
Vince Lehman | ff8b397 | 2015-02-20 16:51:21 -0600 | [diff] [blame] | 269 | NFD_LOG_INFO("Removing route " << parameters.getName() << " nexthop=" << route.faceId |
| 270 | << " origin=" << route.origin); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 271 | |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 272 | RibUpdate update; |
| 273 | update.setAction(RibUpdate::UNREGISTER) |
| 274 | .setName(parameters.getName()) |
| 275 | .setRoute(route); |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 276 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 277 | m_rib.beginApplyUpdate(update, |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 278 | bind(&RibManager::onRibUpdateSuccess, this, update), |
| 279 | bind(&RibManager::onRibUpdateFailure, this, update, _1, _2)); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 283 | RibManager::listEntries(const Name& topPrefix, const Interest& interest, |
| 284 | ndn::mgmt::StatusDatasetContext& context) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 285 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 286 | for (auto&& ribTableEntry : m_rib) { |
| 287 | const auto& ribEntry = *ribTableEntry.second; |
| 288 | ndn::nfd::RibEntry record; |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 289 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 290 | for (auto&& route : ribEntry) { |
| 291 | ndn::nfd::Route routeElement; |
| 292 | routeElement.setFaceId(route.faceId) |
| 293 | .setOrigin(route.origin) |
| 294 | .setCost(route.cost) |
| 295 | .setFlags(route.flags); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 296 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 297 | if (route.expires < time::steady_clock::TimePoint::max()) { |
| 298 | routeElement.setExpirationPeriod(time::duration_cast<time::milliseconds>( |
| 299 | route.expires - time::steady_clock::now())); |
| 300 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 301 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 302 | record.addRoute(routeElement); |
| 303 | } |
| 304 | |
| 305 | record.setName(ribEntry.getName()); |
| 306 | context.append(record.wireEncode()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 307 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 308 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 309 | context.end(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 313 | RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 314 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 315 | bool isSelfRegistration = (parameters.getFaceId() == 0); |
| 316 | if (isSelfRegistration) { |
| 317 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 318 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 319 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 320 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 321 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 322 | parameters.setFaceId(*incomingFaceIdTag); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 323 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 326 | ndn::mgmt::Authorization |
| 327 | RibManager::makeAuthorization(const std::string& verb) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 328 | { |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 329 | return [this] (const Name& prefix, const Interest& interest, |
| 330 | const ndn::mgmt::ControlParameters* params, |
| 331 | const ndn::mgmt::AcceptContinuation& accept, |
| 332 | const ndn::mgmt::RejectContinuation& reject) { |
| 333 | BOOST_ASSERT(params != nullptr); |
| 334 | BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters)); |
| 335 | BOOST_ASSERT(prefix == LOCAL_HOST_TOP_PREFIX || prefix == LOCAL_HOP_TOP_PREFIX); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 336 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 337 | ndn::ValidatorConfig& validator = prefix == LOCAL_HOST_TOP_PREFIX ? |
| 338 | m_localhostValidator : m_localhopValidator; |
| 339 | validator.validate(interest, |
| 340 | bind([&interest, this, accept] { extractRequester(interest, accept); }), |
| 341 | bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); })); |
| 342 | }; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 346 | RibManager::fetchActiveFaces() |
| 347 | { |
| 348 | NFD_LOG_DEBUG("Fetching active faces"); |
| 349 | |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 350 | m_nfdController.fetch<ndn::nfd::FaceDataset>( |
| 351 | bind(&RibManager::removeInvalidFaces, this, _1), |
| 352 | bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2), |
| 353 | ndn::nfd::CommandOptions()); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 357 | RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 358 | { |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 359 | NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 360 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | RibManager::onFaceDestroyedEvent(uint64_t faceId) |
| 365 | { |
| 366 | m_rib.beginRemoveFace(faceId); |
| 367 | m_registeredFaces.erase(faceId); |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait) |
| 372 | { |
| 373 | scheduler::cancel(m_activeFaceFetchEvent); |
| 374 | |
| 375 | m_activeFaceFetchEvent = scheduler::schedule(timeToWait, |
| 376 | bind(&RibManager::fetchActiveFaces, this)); |
| 377 | } |
| 378 | |
| 379 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 380 | RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 381 | { |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 382 | NFD_LOG_DEBUG("Checking for invalid face registrations"); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 383 | |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 384 | FaceIdSet activeFaceIds; |
| 385 | for (const ndn::nfd::FaceStatus& item : activeFaces) { |
| 386 | activeFaceIds.insert(item.getFaceId()); |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 389 | // Look for face IDs that were registered but not active to find missed |
| 390 | // face destroyed events |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 391 | for (auto&& faceId : m_registeredFaces) { |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 392 | if (activeFaceIds.count(faceId) == 0) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 393 | NFD_LOG_DEBUG("Removing invalid face ID: " << faceId); |
| 394 | |
| 395 | scheduler::schedule(time::seconds(0), |
| 396 | bind(&RibManager::onFaceDestroyedEvent, this, faceId)); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 397 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 398 | } |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 399 | |
| 400 | // Reschedule the check for future clean up |
| 401 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 405 | RibManager::onNotification(const FaceEventNotification& notification) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 406 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 407 | NFD_LOG_TRACE("onNotification: " << notification); |
| 408 | |
| 409 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { |
| 410 | NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId()); |
| 411 | |
| 412 | scheduler::schedule(time::seconds(0), |
| 413 | bind(&RibManager::onFaceDestroyedEvent, this, notification.getFaceId())); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void |
Junxiao Shi | b260017 | 2016-07-11 08:53:53 +0000 | [diff] [blame] | 418 | RibManager::onCommandPrefixAddNextHopSuccess(const Name& prefix, |
| 419 | const ndn::nfd::ControlParameters& result) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 420 | { |
| 421 | NFD_LOG_DEBUG("Successfully registered " + prefix.toUri() + " with NFD"); |
| 422 | |
| 423 | // Routes must be inserted into the RIB so route flags can be applied |
| 424 | Route route; |
| 425 | route.faceId = result.getFaceId(); |
| 426 | route.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
| 427 | route.expires = time::steady_clock::TimePoint::max(); |
| 428 | route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 429 | |
| 430 | m_rib.insert(prefix, route); |
| 431 | |
| 432 | m_registeredFaces.insert(route.faceId); |
| 433 | } |
| 434 | |
| 435 | void |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 436 | RibManager::onCommandPrefixAddNextHopError(const Name& name, |
| 437 | const ndn::nfd::ControlResponse& response) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 438 | { |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 439 | BOOST_THROW_EXCEPTION(Error("Error in setting interest filter (" + name.toUri() + |
| 440 | "): " + response.getText())); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 444 | RibManager::onEnableLocalFieldsSuccess() |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 445 | { |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 446 | NFD_LOG_DEBUG("Local fields enabled"); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 450 | RibManager::onEnableLocalFieldsError(const ndn::nfd::ControlResponse& response) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 451 | { |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 452 | BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (code: " + |
| 453 | to_string(response.getCode()) + ", info: " + response.getText() + |
| 454 | ")")); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 457 | } // namespace rib |
| 458 | } // namespace nfd |