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