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