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