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