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" |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 30 | |
Junxiao Shi | cbc8e94 | 2016-09-06 03:17:45 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/lp/tags.hpp> |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 34 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/mgmt/nfd/face-status.hpp> |
| 36 | #include <ndn-cxx/mgmt/nfd/rib-entry.hpp> |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 37 | |
| 38 | namespace nfd { |
| 39 | namespace rib { |
| 40 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 41 | NFD_LOG_INIT(RibManager); |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 43 | static const std::string MGMT_MODULE_NAME = "rib"; |
| 44 | static const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd"; |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 45 | static const time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 46 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 47 | const Name RibManager::LOCALHOP_TOP_PREFIX = "/localhop/nfd"; |
| 48 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 49 | RibManager::RibManager(Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain, |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 50 | ndn::nfd::Controller& nfdController, Dispatcher& dispatcher, |
| 51 | ndn::util::Scheduler& scheduler) |
Junxiao Shi | fde3f54 | 2016-07-10 19:54:53 +0000 | [diff] [blame] | 52 | : ManagerBase(dispatcher, MGMT_MODULE_NAME) |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 53 | , m_rib(rib) |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 54 | , m_keyChain(keyChain) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 55 | , m_nfdController(nfdController) |
| 56 | , m_dispatcher(dispatcher) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 57 | , m_scheduler(scheduler) |
Davide Pesavento | 9f8b10e | 2018-08-22 08:45:37 +0000 | [diff] [blame] | 58 | , m_faceMonitor(face) |
| 59 | , m_localhostValidator(face) |
| 60 | , m_localhopValidator(face) |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 61 | , m_isLocalhopEnabled(false) |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 62 | , m_activeFaceFetchEvent(m_scheduler) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 63 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 64 | registerCommandHandler<ndn::nfd::RibRegisterCommand>("register", |
| 65 | bind(&RibManager::registerEntry, this, _2, _3, _4, _5)); |
| 66 | registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister", |
| 67 | bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5)); |
| 68 | |
| 69 | registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3)); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 72 | void |
| 73 | RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename) |
| 74 | { |
| 75 | m_localhostValidator.load(section, filename); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename) |
| 80 | { |
| 81 | m_localhopValidator.load(section, filename); |
| 82 | m_isLocalhopEnabled = true; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | RibManager::disableLocalhop() |
| 87 | { |
| 88 | m_isLocalhopEnabled = false; |
| 89 | } |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 90 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 91 | void |
| 92 | RibManager::registerWithNfd() |
| 93 | { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 94 | registerTopPrefix(LOCALHOST_TOP_PREFIX); |
Alexander Afanasyev | b3893c9 | 2014-05-15 01:49:54 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 96 | if (m_isLocalhopEnabled) { |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 97 | registerTopPrefix(LOCALHOP_TOP_PREFIX); |
Junxiao Shi | a329574 | 2014-05-16 22:40:10 -0700 | [diff] [blame] | 98 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | 89cf5e0 | 2014-04-17 12:08:57 -0700 | [diff] [blame] | 100 | NFD_LOG_INFO("Start monitoring face create/destroy events"); |
Junxiao Shi | fbf7834 | 2015-01-23 14:46:41 -0700 | [diff] [blame] | 101 | m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1)); |
Junxiao Shi | 15b12e7 | 2014-08-09 19:56:24 -0700 | [diff] [blame] | 102 | m_faceMonitor.start(); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 103 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 104 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 108 | RibManager::enableLocalFields() |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 109 | { |
Eric Newberry | ecc45cb | 2016-11-08 19:57:12 +0000 | [diff] [blame] | 110 | m_nfdController.start<ndn::nfd::FaceUpdateCommand>( |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 111 | ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true), |
| 112 | [] (const ControlParameters& res) { |
| 113 | NFD_LOG_DEBUG("Local fields enabled"); |
| 114 | }, |
| 115 | [] (const ControlResponse& res) { |
| 116 | BOOST_THROW_EXCEPTION(Error("Couldn't enable local fields (" + to_string(res.getCode()) + |
| 117 | " " + res.getText() + ")")); |
| 118 | }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 122 | RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires, |
| 123 | const std::function<void(RibUpdateResult)>& done) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 124 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 125 | if (expires) { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 126 | route.expires = time::steady_clock::now() + *expires; |
| 127 | } |
| 128 | else if (route.expires) { |
| 129 | expires = *route.expires - time::steady_clock::now(); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (expires && *expires <= 0_s) { |
| 133 | m_rib.onRouteExpiration(name, route); |
| 134 | return done(RibUpdateResult::EXPIRED); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId << |
| 138 | " origin=" << route.origin << " cost=" << route.cost); |
| 139 | |
| 140 | if (expires) { |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 141 | auto event = m_scheduler.scheduleEvent(*expires, [=] { m_rib.onRouteExpiration(name, route); }); |
| 142 | route.setExpirationEvent(event, m_scheduler); |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 143 | NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires); |
| 144 | } |
| 145 | |
| 146 | m_registeredFaces.insert(route.faceId); |
| 147 | |
| 148 | RibUpdate update; |
| 149 | update.setAction(RibUpdate::REGISTER) |
| 150 | .setName(name) |
| 151 | .setRoute(route); |
| 152 | beginRibUpdate(update, done); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 156 | RibManager::beginRemoveRoute(const Name& name, const Route& route, |
| 157 | const std::function<void(RibUpdateResult)>& done) |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 158 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 159 | NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId << |
| 160 | " origin=" << route.origin); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 161 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 162 | RibUpdate update; |
| 163 | update.setAction(RibUpdate::UNREGISTER) |
| 164 | .setName(name) |
| 165 | .setRoute(route); |
| 166 | beginRibUpdate(update, done); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | RibManager::beginRibUpdate(const RibUpdate& update, |
| 171 | const std::function<void(RibUpdateResult)>& done) |
| 172 | { |
| 173 | m_rib.beginApplyUpdate(update, |
| 174 | [=] { |
| 175 | NFD_LOG_DEBUG("RIB update succeeded for " << update); |
| 176 | done(RibUpdateResult::OK); |
| 177 | }, |
| 178 | [=] (uint32_t code, const std::string& error) { |
| 179 | NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")"); |
| 180 | |
| 181 | // Since the FIB rejected the update, clean up invalid routes |
| 182 | scheduleActiveFaceFetch(1_s); |
| 183 | |
| 184 | done(RibUpdateResult::ERROR); |
| 185 | }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 189 | RibManager::registerTopPrefix(const Name& topPrefix) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 190 | { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 191 | // add FIB nexthop |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 192 | m_nfdController.start<ndn::nfd::FibAddNextHopCommand>( |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 193 | ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME)) |
| 194 | .setFaceId(0), |
| 195 | [=] (const ControlParameters& res) { |
| 196 | NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD"); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 197 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 198 | // Routes must be inserted into the RIB so route flags can be applied |
| 199 | Route route; |
| 200 | route.faceId = res.getFaceId(); |
| 201 | route.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
| 202 | route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 203 | |
| 204 | m_rib.insert(topPrefix, route); |
| 205 | |
| 206 | m_registeredFaces.insert(route.faceId); |
| 207 | }, |
| 208 | [=] (const ControlResponse& res) { |
| 209 | BOOST_THROW_EXCEPTION(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" + |
| 210 | to_string(res.getCode()) + " " + res.getText() + ")")); |
| 211 | }); |
| 212 | |
| 213 | // add top prefix to the dispatcher without prefix registration |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 214 | m_dispatcher.addTopPrefix(topPrefix, false); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 218 | RibManager::registerEntry(const Name& topPrefix, const Interest& interest, |
| 219 | ControlParameters parameters, |
| 220 | const ndn::mgmt::CommandContinuation& done) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 221 | { |
Junxiao Shi | 7530635 | 2018-02-01 21:59:44 +0000 | [diff] [blame] | 222 | if (parameters.getName().size() > FIB_MAX_DEPTH) { |
| 223 | done(ControlResponse(414, "Route prefix cannot exceed " + ndn::to_string(FIB_MAX_DEPTH) + |
| 224 | " components")); |
| 225 | return; |
| 226 | } |
| 227 | |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 228 | setFaceForSelfRegistration(interest, parameters); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 229 | |
| 230 | // Respond since command is valid and authorized |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 231 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 232 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 233 | Route route; |
| 234 | route.faceId = parameters.getFaceId(); |
| 235 | route.origin = parameters.getOrigin(); |
| 236 | route.cost = parameters.getCost(); |
| 237 | route.flags = parameters.getFlags(); |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 238 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 239 | optional<time::nanoseconds> expires; |
Alexander Afanasyev | f67cf08 | 2014-07-18 16:47:29 -0700 | [diff] [blame] | 240 | if (parameters.hasExpirationPeriod() && |
Nick Gordon | 9fcf123 | 2017-03-10 22:30:20 +0000 | [diff] [blame] | 241 | parameters.getExpirationPeriod() != time::milliseconds::max()) { |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 242 | expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 243 | } |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 244 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 245 | beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {}); |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 249 | RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest, |
| 250 | ControlParameters parameters, |
| 251 | const ndn::mgmt::CommandContinuation& done) |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 252 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 253 | setFaceForSelfRegistration(interest, parameters); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 254 | |
| 255 | // Respond since command is valid and authorized |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 256 | done(ControlResponse(200, "Success").setBody(parameters.wireEncode())); |
Alexander Afanasyev | fb1c808 | 2014-07-17 15:16:15 -0700 | [diff] [blame] | 257 | |
Vince Lehman | 218be0a | 2015-01-15 17:25:20 -0600 | [diff] [blame] | 258 | Route route; |
| 259 | route.faceId = parameters.getFaceId(); |
| 260 | route.origin = parameters.getOrigin(); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 261 | |
Junxiao Shi | 5200904 | 2018-09-10 12:33:56 +0000 | [diff] [blame] | 262 | beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {}); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 266 | RibManager::listEntries(const Name& topPrefix, const Interest& interest, |
| 267 | ndn::mgmt::StatusDatasetContext& context) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 268 | { |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 269 | auto now = time::steady_clock::now(); |
| 270 | for (const auto& kv : m_rib) { |
| 271 | const RibEntry& entry = *kv.second; |
| 272 | ndn::nfd::RibEntry item; |
| 273 | item.setName(entry.getName()); |
| 274 | for (const Route& route : entry.getRoutes()) { |
| 275 | ndn::nfd::Route r; |
| 276 | r.setFaceId(route.faceId); |
| 277 | r.setOrigin(route.origin); |
| 278 | r.setCost(route.cost); |
| 279 | r.setFlags(route.flags); |
| 280 | if (route.expires) { |
| 281 | r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now)); |
| 282 | } |
| 283 | item.addRoute(r); |
| 284 | } |
| 285 | context.append(item.wireEncode()); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 286 | } |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 287 | context.end(); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 291 | RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 292 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 293 | bool isSelfRegistration = (parameters.getFaceId() == 0); |
| 294 | if (isSelfRegistration) { |
| 295 | shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>(); |
| 296 | // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field", |
| 297 | // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD |
| 298 | // and is initialized synchronously with IncomingFaceId field enabled. |
| 299 | BOOST_ASSERT(incomingFaceIdTag != nullptr); |
| 300 | parameters.setFaceId(*incomingFaceIdTag); |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 301 | } |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 304 | ndn::mgmt::Authorization |
| 305 | RibManager::makeAuthorization(const std::string& verb) |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 306 | { |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 307 | return [this] (const Name& prefix, const Interest& interest, |
| 308 | const ndn::mgmt::ControlParameters* params, |
| 309 | const ndn::mgmt::AcceptContinuation& accept, |
| 310 | const ndn::mgmt::RejectContinuation& reject) { |
| 311 | BOOST_ASSERT(params != nullptr); |
| 312 | BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters)); |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 313 | BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 314 | |
Junxiao Shi | f4cfed1 | 2018-08-22 23:26:29 +0000 | [diff] [blame] | 315 | ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ? |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 316 | m_localhostValidator : m_localhopValidator; |
| 317 | validator.validate(interest, |
| 318 | bind([&interest, this, accept] { extractRequester(interest, accept); }), |
| 319 | bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); })); |
| 320 | }; |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 323 | std::ostream& |
| 324 | operator<<(std::ostream& os, RibManager::SlAnnounceResult res) |
| 325 | { |
| 326 | switch (res) { |
| 327 | case RibManager::SlAnnounceResult::OK: |
| 328 | return os << "OK"; |
| 329 | case RibManager::SlAnnounceResult::ERROR: |
| 330 | return os << "ERROR"; |
| 331 | case RibManager::SlAnnounceResult::VALIDATION_FAILURE: |
| 332 | return os << "VALIDATION_FAILURE"; |
| 333 | case RibManager::SlAnnounceResult::EXPIRED: |
| 334 | return os << "EXPIRED"; |
| 335 | case RibManager::SlAnnounceResult::NOT_FOUND: |
| 336 | return os << "NOT_FOUND"; |
| 337 | } |
| 338 | BOOST_ASSERT_MSG(false, "bad SlAnnounceResult"); |
| 339 | return os; |
| 340 | } |
| 341 | |
| 342 | RibManager::SlAnnounceResult |
| 343 | RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r) |
| 344 | { |
| 345 | switch (r) { |
| 346 | case RibUpdateResult::OK: |
| 347 | return SlAnnounceResult::OK; |
| 348 | case RibUpdateResult::ERROR: |
| 349 | return SlAnnounceResult::ERROR; |
| 350 | case RibUpdateResult::EXPIRED: |
| 351 | return SlAnnounceResult::EXPIRED; |
| 352 | default: |
| 353 | BOOST_ASSERT(false); |
| 354 | return SlAnnounceResult::ERROR; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void |
| 359 | RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId, |
| 360 | time::milliseconds maxLifetime, const SlAnnounceCallback& cb) |
| 361 | { |
| 362 | BOOST_ASSERT(pa.getData()); |
| 363 | |
| 364 | if (!m_isLocalhopEnabled) { |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 365 | NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 366 | ": localhop_security unconfigured"); |
| 367 | cb(SlAnnounceResult::VALIDATION_FAILURE); |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | m_localhopValidator.validate(*pa.getData(), |
| 372 | [=] (const Data&) { |
| 373 | Route route(pa, faceId); |
| 374 | route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime); |
| 375 | beginAddRoute(pa.getAnnouncedName(), route, nullopt, |
| 376 | [=] (RibUpdateResult ribRes) { |
| 377 | auto res = getSlAnnounceResultFromRibUpdateResult(ribRes); |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 378 | NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 379 | cb(res); |
| 380 | }); |
| 381 | }, |
| 382 | [=] (const Data&, ndn::security::v2::ValidationError err) { |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 383 | NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 384 | " validation error: " << err); |
| 385 | cb(SlAnnounceResult::VALIDATION_FAILURE); |
| 386 | } |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime, |
| 392 | const SlAnnounceCallback& cb) |
| 393 | { |
| 394 | Route routeQuery; |
| 395 | routeQuery.faceId = faceId; |
| 396 | routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN; |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 397 | Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery); |
| 398 | |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 399 | if (oldRoute == nullptr || !oldRoute->announcement) { |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 400 | NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found"); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 401 | return cb(SlAnnounceResult::NOT_FOUND); |
| 402 | } |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 403 | Name routeName = oldRoute->announcement->getAnnouncedName(); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 404 | |
| 405 | Route route = *oldRoute; |
| 406 | route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime); |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 407 | beginAddRoute(routeName, route, nullopt, |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 408 | [=] (RibUpdateResult ribRes) { |
| 409 | auto res = getSlAnnounceResultFromRibUpdateResult(ribRes); |
Teng Liang | a4e6ec3 | 2018-10-21 09:25:00 -0700 | [diff] [blame] | 410 | NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 411 | cb(res); |
| 412 | }); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const |
| 417 | { |
| 418 | shared_ptr<RibEntry> entry; |
| 419 | auto exactMatch = m_rib.find(name); |
| 420 | if (exactMatch != m_rib.end()) { |
| 421 | entry = exactMatch->second; |
| 422 | } |
| 423 | else { |
| 424 | entry = m_rib.findParent(name); |
| 425 | } |
| 426 | if (entry == nullptr) { |
| 427 | return cb(nullopt); |
| 428 | } |
| 429 | |
| 430 | auto pa = entry->getPrefixAnnouncement(); |
| 431 | pa.toData(m_keyChain); |
| 432 | cb(pa); |
| 433 | } |
| 434 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 435 | void |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 436 | RibManager::fetchActiveFaces() |
| 437 | { |
| 438 | NFD_LOG_DEBUG("Fetching active faces"); |
| 439 | |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 440 | m_nfdController.fetch<ndn::nfd::FaceDataset>( |
| 441 | bind(&RibManager::removeInvalidFaces, this, _1), |
| 442 | bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2), |
| 443 | ndn::nfd::CommandOptions()); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 447 | RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 448 | { |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 449 | NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 450 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
| 451 | } |
| 452 | |
| 453 | void |
| 454 | RibManager::onFaceDestroyedEvent(uint64_t faceId) |
| 455 | { |
| 456 | m_rib.beginRemoveFace(faceId); |
| 457 | m_registeredFaces.erase(faceId); |
| 458 | } |
| 459 | |
| 460 | void |
| 461 | RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait) |
| 462 | { |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 463 | m_activeFaceFetchEvent = m_scheduler.scheduleEvent(timeToWait, [this] { fetchActiveFaces(); }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 467 | RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 468 | { |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 469 | NFD_LOG_DEBUG("Checking for invalid face registrations"); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 470 | |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 471 | FaceIdSet activeFaceIds; |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 472 | for (const auto& faceStatus : activeFaces) { |
| 473 | activeFaceIds.insert(faceStatus.getFaceId()); |
Junxiao Shi | 78926c9 | 2015-02-28 22:56:06 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 476 | // Look for face IDs that were registered but not active to find missed |
| 477 | // face destroyed events |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 478 | for (auto faceId : m_registeredFaces) { |
Weiwei Liu | 6e21cdb | 2016-09-29 15:16:23 -0700 | [diff] [blame] | 479 | if (activeFaceIds.count(faceId) == 0) { |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 480 | NFD_LOG_DEBUG("Removing invalid face ID: " << faceId); |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 481 | m_scheduler.scheduleEvent(0_ns, [this, faceId] { this->onFaceDestroyedEvent(faceId); }); |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 482 | } |
Vince Lehman | 76c751c | 2014-11-18 17:36:38 -0600 | [diff] [blame] | 483 | } |
Vince Lehman | 26b215c | 2014-08-17 15:00:41 -0500 | [diff] [blame] | 484 | |
| 485 | // Reschedule the check for future clean up |
| 486 | scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL); |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 490 | RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification) |
Vince Lehman | cd613c5 | 2014-07-30 14:34:49 -0500 | [diff] [blame] | 491 | { |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 492 | NFD_LOG_TRACE("onNotification: " << notification); |
| 493 | |
| 494 | if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) { |
| 495 | NFD_LOG_DEBUG("Received notification for destroyed faceId: " << notification.getFaceId()); |
Davide Pesavento | e1bdc08 | 2018-10-11 21:20:23 -0400 | [diff] [blame] | 496 | m_scheduler.scheduleEvent(0_ns, [this, id = notification.getFaceId()] { onFaceDestroyedEvent(id); }); |
Yanbiao Li | cf0db02 | 2016-01-29 00:54:25 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 500 | } // namespace rib |
| 501 | } // namespace nfd |