blob: 1e881fa70b7397299abe0903b67d9f38c54b45d1 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi75306352018-02-01 21:59:44 +00002/*
Junxiao Shifeddc3c2019-01-17 19:06:00 +00003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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 Afanasyev3ecec502014-04-16 13:42:44 -070010 *
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/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
26#include "rib-manager.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000027
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
29#include "common/logger.hpp"
Davide Pesavento78ddcab2019-02-28 22:00:03 -050030#include "rib/rib.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040031#include "table/fib.hpp"
Nick Gordon9fcf1232017-03-10 22:30:20 +000032
Junxiao Shicbc8e942016-09-06 03:17:45 +000033#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000034#include <ndn-cxx/mgmt/nfd/face-status.hpp>
35#include <ndn-cxx/mgmt/nfd/rib-entry.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070036
37namespace nfd {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050038
39using rib::RibUpdate;
40using rib::Route;
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070041
Davide Pesaventoa3148082018-04-12 18:21:54 -040042NFD_LOG_INIT(RibManager);
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070043
Junxiao Shif4cfed12018-08-22 23:26:29 +000044static const std::string MGMT_MODULE_NAME = "rib";
45static const Name LOCALHOST_TOP_PREFIX = "/localhost/nfd";
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040046static const time::seconds ACTIVE_FACE_FETCH_INTERVAL = 5_min;
Vince Lehman26b215c2014-08-17 15:00:41 -050047
Yanbiao Lif48d0802018-06-01 03:00:02 -070048const Name RibManager::LOCALHOP_TOP_PREFIX = "/localhop/nfd";
49
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050050RibManager::RibManager(rib::Rib& rib, ndn::Face& face, ndn::KeyChain& keyChain,
Davide Pesavento0a71dd32019-03-17 20:36:18 -040051 ndn::nfd::Controller& nfdController, Dispatcher& dispatcher)
Davide Pesavento78ddcab2019-02-28 22:00:03 -050052 : ManagerBase(MGMT_MODULE_NAME, dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000053 , m_rib(rib)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000054 , m_keyChain(keyChain)
Junxiao Shif4cfed12018-08-22 23:26:29 +000055 , m_nfdController(nfdController)
56 , m_dispatcher(dispatcher)
Davide Pesavento9f8b10e2018-08-22 08:45:37 +000057 , m_faceMonitor(face)
58 , m_localhostValidator(face)
59 , m_localhopValidator(face)
Junxiao Shif4cfed12018-08-22 23:26:29 +000060 , m_isLocalhopEnabled(false)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070061{
Yanbiao Licf0db022016-01-29 00:54:25 -080062 registerCommandHandler<ndn::nfd::RibRegisterCommand>("register",
63 bind(&RibManager::registerEntry, this, _2, _3, _4, _5));
64 registerCommandHandler<ndn::nfd::RibUnregisterCommand>("unregister",
65 bind(&RibManager::unregisterEntry, this, _2, _3, _4, _5));
66
67 registerStatusDatasetHandler("list", bind(&RibManager::listEntries, this, _1, _2, _3));
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070068}
69
Junxiao Shif4cfed12018-08-22 23:26:29 +000070void
71RibManager::applyLocalhostConfig(const ConfigSection& section, const std::string& filename)
72{
73 m_localhostValidator.load(section, filename);
74}
75
76void
77RibManager::enableLocalhop(const ConfigSection& section, const std::string& filename)
78{
79 m_localhopValidator.load(section, filename);
80 m_isLocalhopEnabled = true;
81}
82
83void
84RibManager::disableLocalhop()
85{
86 m_isLocalhopEnabled = false;
87}
Vince Lehman26b215c2014-08-17 15:00:41 -050088
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070089void
90RibManager::registerWithNfd()
91{
Junxiao Shif4cfed12018-08-22 23:26:29 +000092 registerTopPrefix(LOCALHOST_TOP_PREFIX);
Alexander Afanasyevb3893c92014-05-15 01:49:54 -070093
Junxiao Shia3295742014-05-16 22:40:10 -070094 if (m_isLocalhopEnabled) {
Junxiao Shif4cfed12018-08-22 23:26:29 +000095 registerTopPrefix(LOCALHOP_TOP_PREFIX);
Junxiao Shia3295742014-05-16 22:40:10 -070096 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070097
Alexander Afanasyev89cf5e02014-04-17 12:08:57 -070098 NFD_LOG_INFO("Start monitoring face create/destroy events");
Junxiao Shifbf78342015-01-23 14:46:41 -070099 m_faceMonitor.onNotification.connect(bind(&RibManager::onNotification, this, _1));
Junxiao Shi15b12e72014-08-09 19:56:24 -0700100 m_faceMonitor.start();
Vince Lehmancd613c52014-07-30 14:34:49 -0500101
Vince Lehman26b215c2014-08-17 15:00:41 -0500102 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700103}
104
105void
Eric Newberryecc45cb2016-11-08 19:57:12 +0000106RibManager::enableLocalFields()
Yanbiao Licf0db022016-01-29 00:54:25 -0800107{
Eric Newberryecc45cb2016-11-08 19:57:12 +0000108 m_nfdController.start<ndn::nfd::FaceUpdateCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000109 ControlParameters().setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true),
Davide Pesavento19779d82019-02-14 13:40:04 -0500110 [] (const ControlParameters&) {
Junxiao Shi52009042018-09-10 12:33:56 +0000111 NFD_LOG_DEBUG("Local fields enabled");
112 },
113 [] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500114 NDN_THROW(Error("Couldn't enable local fields (" + to_string(res.getCode()) +
115 " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000116 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800117}
118
119void
Junxiao Shi52009042018-09-10 12:33:56 +0000120RibManager::beginAddRoute(const Name& name, Route route, optional<time::nanoseconds> expires,
121 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800122{
Junxiao Shi52009042018-09-10 12:33:56 +0000123 if (expires) {
Junxiao Shi52009042018-09-10 12:33:56 +0000124 route.expires = time::steady_clock::now() + *expires;
125 }
126 else if (route.expires) {
127 expires = *route.expires - time::steady_clock::now();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000128 }
129
130 if (expires && *expires <= 0_s) {
131 m_rib.onRouteExpiration(name, route);
132 return done(RibUpdateResult::EXPIRED);
Junxiao Shi52009042018-09-10 12:33:56 +0000133 }
134
135 NFD_LOG_INFO("Adding route " << name << " nexthop=" << route.faceId <<
136 " origin=" << route.origin << " cost=" << route.cost);
137
138 if (expires) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400139 auto event = getScheduler().schedule(*expires, [=] { m_rib.onRouteExpiration(name, route); });
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000140 route.setExpirationEvent(event);
Junxiao Shi52009042018-09-10 12:33:56 +0000141 NFD_LOG_TRACE("Scheduled unregistration at: " << *route.expires);
142 }
143
Junxiao Shi52009042018-09-10 12:33:56 +0000144 RibUpdate update;
145 update.setAction(RibUpdate::REGISTER)
146 .setName(name)
147 .setRoute(route);
148 beginRibUpdate(update, done);
Yanbiao Licf0db022016-01-29 00:54:25 -0800149}
150
151void
Junxiao Shi52009042018-09-10 12:33:56 +0000152RibManager::beginRemoveRoute(const Name& name, const Route& route,
153 const std::function<void(RibUpdateResult)>& done)
Yanbiao Licf0db022016-01-29 00:54:25 -0800154{
Junxiao Shi52009042018-09-10 12:33:56 +0000155 NFD_LOG_INFO("Removing route " << name << " nexthop=" << route.faceId <<
156 " origin=" << route.origin);
Yanbiao Licf0db022016-01-29 00:54:25 -0800157
Junxiao Shi52009042018-09-10 12:33:56 +0000158 RibUpdate update;
159 update.setAction(RibUpdate::UNREGISTER)
160 .setName(name)
161 .setRoute(route);
162 beginRibUpdate(update, done);
163}
164
165void
166RibManager::beginRibUpdate(const RibUpdate& update,
167 const std::function<void(RibUpdateResult)>& done)
168{
169 m_rib.beginApplyUpdate(update,
170 [=] {
171 NFD_LOG_DEBUG("RIB update succeeded for " << update);
172 done(RibUpdateResult::OK);
173 },
174 [=] (uint32_t code, const std::string& error) {
175 NFD_LOG_DEBUG("RIB update failed for " << update << " (" << code << " " << error << ")");
176
177 // Since the FIB rejected the update, clean up invalid routes
178 scheduleActiveFaceFetch(1_s);
179
180 done(RibUpdateResult::ERROR);
181 });
Yanbiao Licf0db022016-01-29 00:54:25 -0800182}
183
184void
Yanbiao Licf0db022016-01-29 00:54:25 -0800185RibManager::registerTopPrefix(const Name& topPrefix)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700186{
Junxiao Shi52009042018-09-10 12:33:56 +0000187 // add FIB nexthop
Yanbiao Licf0db022016-01-29 00:54:25 -0800188 m_nfdController.start<ndn::nfd::FibAddNextHopCommand>(
Junxiao Shi52009042018-09-10 12:33:56 +0000189 ControlParameters().setName(Name(topPrefix).append(MGMT_MODULE_NAME))
190 .setFaceId(0),
191 [=] (const ControlParameters& res) {
192 NFD_LOG_DEBUG("Successfully registered " << topPrefix << " with NFD");
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700193
Junxiao Shi52009042018-09-10 12:33:56 +0000194 // Routes must be inserted into the RIB so route flags can be applied
195 Route route;
196 route.faceId = res.getFaceId();
197 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
198 route.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
199
200 m_rib.insert(topPrefix, route);
Junxiao Shi52009042018-09-10 12:33:56 +0000201 },
202 [=] (const ControlResponse& res) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500203 NDN_THROW(Error("Cannot add FIB entry " + topPrefix.toUri() + " (" +
204 to_string(res.getCode()) + " " + res.getText() + ")"));
Junxiao Shi52009042018-09-10 12:33:56 +0000205 });
206
207 // add top prefix to the dispatcher without prefix registration
Junxiao Shif4cfed12018-08-22 23:26:29 +0000208 m_dispatcher.addTopPrefix(topPrefix, false);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700209}
210
211void
Yanbiao Licf0db022016-01-29 00:54:25 -0800212RibManager::registerEntry(const Name& topPrefix, const Interest& interest,
213 ControlParameters parameters,
214 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700215{
Davide Pesavento2cae8ca2019-04-18 20:48:05 -0400216 if (parameters.getName().size() > Fib::getMaxDepth()) {
217 done(ControlResponse(414, "Route prefix cannot exceed " + to_string(Fib::getMaxDepth()) +
Junxiao Shi75306352018-02-01 21:59:44 +0000218 " components"));
219 return;
220 }
221
Yanbiao Licf0db022016-01-29 00:54:25 -0800222 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600223
224 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800225 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700226
Vince Lehman218be0a2015-01-15 17:25:20 -0600227 Route route;
228 route.faceId = parameters.getFaceId();
229 route.origin = parameters.getOrigin();
230 route.cost = parameters.getCost();
231 route.flags = parameters.getFlags();
Syed Obaid3313a372014-07-01 01:31:33 -0500232
Junxiao Shi52009042018-09-10 12:33:56 +0000233 optional<time::nanoseconds> expires;
Alexander Afanasyevf67cf082014-07-18 16:47:29 -0700234 if (parameters.hasExpirationPeriod() &&
Nick Gordon9fcf1232017-03-10 22:30:20 +0000235 parameters.getExpirationPeriod() != time::milliseconds::max()) {
Junxiao Shi52009042018-09-10 12:33:56 +0000236 expires = time::duration_cast<time::nanoseconds>(parameters.getExpirationPeriod());
Vince Lehman76c751c2014-11-18 17:36:38 -0600237 }
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700238
Junxiao Shi52009042018-09-10 12:33:56 +0000239 beginAddRoute(parameters.getName(), std::move(route), expires, [] (RibUpdateResult) {});
Vince Lehman281ded72014-08-21 12:17:08 -0500240}
241
242void
Yanbiao Licf0db022016-01-29 00:54:25 -0800243RibManager::unregisterEntry(const Name& topPrefix, const Interest& interest,
244 ControlParameters parameters,
245 const ndn::mgmt::CommandContinuation& done)
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700246{
Yanbiao Licf0db022016-01-29 00:54:25 -0800247 setFaceForSelfRegistration(interest, parameters);
Vince Lehman76c751c2014-11-18 17:36:38 -0600248
249 // Respond since command is valid and authorized
Yanbiao Licf0db022016-01-29 00:54:25 -0800250 done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
Alexander Afanasyevfb1c8082014-07-17 15:16:15 -0700251
Vince Lehman218be0a2015-01-15 17:25:20 -0600252 Route route;
253 route.faceId = parameters.getFaceId();
254 route.origin = parameters.getOrigin();
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700255
Junxiao Shi52009042018-09-10 12:33:56 +0000256 beginRemoveRoute(parameters.getName(), route, [] (RibUpdateResult) {});
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700257}
258
259void
Yanbiao Licf0db022016-01-29 00:54:25 -0800260RibManager::listEntries(const Name& topPrefix, const Interest& interest,
261 ndn::mgmt::StatusDatasetContext& context)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700262{
Junxiao Shi3f21e582017-05-29 15:26:32 +0000263 auto now = time::steady_clock::now();
264 for (const auto& kv : m_rib) {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500265 const rib::RibEntry& entry = *kv.second;
Junxiao Shi3f21e582017-05-29 15:26:32 +0000266 ndn::nfd::RibEntry item;
267 item.setName(entry.getName());
268 for (const Route& route : entry.getRoutes()) {
269 ndn::nfd::Route r;
270 r.setFaceId(route.faceId);
271 r.setOrigin(route.origin);
272 r.setCost(route.cost);
273 r.setFlags(route.flags);
274 if (route.expires) {
275 r.setExpirationPeriod(time::duration_cast<time::milliseconds>(*route.expires - now));
276 }
277 item.addRoute(r);
278 }
279 context.append(item.wireEncode());
Vince Lehman76c751c2014-11-18 17:36:38 -0600280 }
Yanbiao Licf0db022016-01-29 00:54:25 -0800281 context.end();
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700282}
283
284void
Yanbiao Licf0db022016-01-29 00:54:25 -0800285RibManager::setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700286{
Yanbiao Licf0db022016-01-29 00:54:25 -0800287 bool isSelfRegistration = (parameters.getFaceId() == 0);
288 if (isSelfRegistration) {
289 shared_ptr<lp::IncomingFaceIdTag> incomingFaceIdTag = request.getTag<lp::IncomingFaceIdTag>();
290 // NDNLPv2 says "application MUST be prepared to receive a packet without IncomingFaceId field",
291 // but it's fine to assert IncomingFaceId is available, because InternalFace lives inside NFD
292 // and is initialized synchronously with IncomingFaceId field enabled.
293 BOOST_ASSERT(incomingFaceIdTag != nullptr);
294 parameters.setFaceId(*incomingFaceIdTag);
Vince Lehman76c751c2014-11-18 17:36:38 -0600295 }
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700296}
297
Junxiao Shi21738402016-08-19 19:48:00 +0000298ndn::mgmt::Authorization
299RibManager::makeAuthorization(const std::string& verb)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700300{
Junxiao Shi21738402016-08-19 19:48:00 +0000301 return [this] (const Name& prefix, const Interest& interest,
302 const ndn::mgmt::ControlParameters* params,
303 const ndn::mgmt::AcceptContinuation& accept,
304 const ndn::mgmt::RejectContinuation& reject) {
305 BOOST_ASSERT(params != nullptr);
306 BOOST_ASSERT(typeid(*params) == typeid(ndn::nfd::ControlParameters));
Junxiao Shif4cfed12018-08-22 23:26:29 +0000307 BOOST_ASSERT(prefix == LOCALHOST_TOP_PREFIX || prefix == LOCALHOP_TOP_PREFIX);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700308
Junxiao Shif4cfed12018-08-22 23:26:29 +0000309 ndn::ValidatorConfig& validator = prefix == LOCALHOST_TOP_PREFIX ?
Junxiao Shi21738402016-08-19 19:48:00 +0000310 m_localhostValidator : m_localhopValidator;
311 validator.validate(interest,
312 bind([&interest, this, accept] { extractRequester(interest, accept); }),
313 bind([reject] { reject(ndn::mgmt::RejectReply::STATUS403); }));
314 };
Vince Lehman26b215c2014-08-17 15:00:41 -0500315}
316
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000317std::ostream&
318operator<<(std::ostream& os, RibManager::SlAnnounceResult res)
319{
320 switch (res) {
321 case RibManager::SlAnnounceResult::OK:
322 return os << "OK";
323 case RibManager::SlAnnounceResult::ERROR:
324 return os << "ERROR";
325 case RibManager::SlAnnounceResult::VALIDATION_FAILURE:
326 return os << "VALIDATION_FAILURE";
327 case RibManager::SlAnnounceResult::EXPIRED:
328 return os << "EXPIRED";
329 case RibManager::SlAnnounceResult::NOT_FOUND:
330 return os << "NOT_FOUND";
331 }
332 BOOST_ASSERT_MSG(false, "bad SlAnnounceResult");
333 return os;
334}
335
336RibManager::SlAnnounceResult
337RibManager::getSlAnnounceResultFromRibUpdateResult(RibUpdateResult r)
338{
339 switch (r) {
340 case RibUpdateResult::OK:
341 return SlAnnounceResult::OK;
342 case RibUpdateResult::ERROR:
343 return SlAnnounceResult::ERROR;
344 case RibUpdateResult::EXPIRED:
345 return SlAnnounceResult::EXPIRED;
346 default:
347 BOOST_ASSERT(false);
348 return SlAnnounceResult::ERROR;
349 }
350}
351
352void
353RibManager::slAnnounce(const ndn::PrefixAnnouncement& pa, uint64_t faceId,
354 time::milliseconds maxLifetime, const SlAnnounceCallback& cb)
355{
356 BOOST_ASSERT(pa.getData());
357
358 if (!m_isLocalhopEnabled) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700359 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000360 ": localhop_security unconfigured");
361 cb(SlAnnounceResult::VALIDATION_FAILURE);
362 return;
363 }
364
365 m_localhopValidator.validate(*pa.getData(),
366 [=] (const Data&) {
367 Route route(pa, faceId);
368 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
369 beginAddRoute(pa.getAnnouncedName(), route, nullopt,
370 [=] (RibUpdateResult ribRes) {
371 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700372 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId << ": " << res);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000373 cb(res);
374 });
375 },
376 [=] (const Data&, ndn::security::v2::ValidationError err) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700377 NFD_LOG_INFO("slAnnounce " << pa.getAnnouncedName() << " " << faceId <<
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000378 " validation error: " << err);
379 cb(SlAnnounceResult::VALIDATION_FAILURE);
380 }
381 );
382}
383
384void
385RibManager::slRenew(const Name& name, uint64_t faceId, time::milliseconds maxLifetime,
386 const SlAnnounceCallback& cb)
387{
388 Route routeQuery;
389 routeQuery.faceId = faceId;
390 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
Teng Lianga4e6ec32018-10-21 09:25:00 -0700391 Route* oldRoute = m_rib.findLongestPrefix(name, routeQuery);
392
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000393 if (oldRoute == nullptr || !oldRoute->announcement) {
Teng Lianga4e6ec32018-10-21 09:25:00 -0700394 NFD_LOG_DEBUG("slRenew " << name << " " << faceId << ": not found");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000395 return cb(SlAnnounceResult::NOT_FOUND);
396 }
Teng Lianga4e6ec32018-10-21 09:25:00 -0700397 Name routeName = oldRoute->announcement->getAnnouncedName();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000398
399 Route route = *oldRoute;
400 route.expires = std::min(route.annExpires, time::steady_clock::now() + maxLifetime);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700401 beginAddRoute(routeName, route, nullopt,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000402 [=] (RibUpdateResult ribRes) {
403 auto res = getSlAnnounceResultFromRibUpdateResult(ribRes);
Teng Lianga4e6ec32018-10-21 09:25:00 -0700404 NFD_LOG_INFO("slRenew " << name << " " << faceId << ": " << res << " " << routeName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000405 cb(res);
406 });
407}
408
409void
410RibManager::slFindAnn(const Name& name, const SlFindAnnCallback& cb) const
411{
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500412 shared_ptr<rib::RibEntry> entry;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000413 auto exactMatch = m_rib.find(name);
414 if (exactMatch != m_rib.end()) {
415 entry = exactMatch->second;
416 }
417 else {
418 entry = m_rib.findParent(name);
419 }
420 if (entry == nullptr) {
421 return cb(nullopt);
422 }
423
424 auto pa = entry->getPrefixAnnouncement();
425 pa.toData(m_keyChain);
426 cb(pa);
427}
428
Vince Lehman26b215c2014-08-17 15:00:41 -0500429void
Vince Lehmancd613c52014-07-30 14:34:49 -0500430RibManager::fetchActiveFaces()
431{
432 NFD_LOG_DEBUG("Fetching active faces");
433
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700434 m_nfdController.fetch<ndn::nfd::FaceDataset>(
435 bind(&RibManager::removeInvalidFaces, this, _1),
436 bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
437 ndn::nfd::CommandOptions());
Vince Lehmancd613c52014-07-30 14:34:49 -0500438}
439
440void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700441RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
Vince Lehmancd613c52014-07-30 14:34:49 -0500442{
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700443 NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
Yanbiao Licf0db022016-01-29 00:54:25 -0800444 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
445}
446
447void
Yanbiao Licf0db022016-01-29 00:54:25 -0800448RibManager::scheduleActiveFaceFetch(const time::seconds& timeToWait)
449{
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400450 m_activeFaceFetchEvent = getScheduler().schedule(timeToWait, [this] { fetchActiveFaces(); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800451}
452
453void
Weiwei Liu6e21cdb2016-09-29 15:16:23 -0700454RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
Vince Lehmancd613c52014-07-30 14:34:49 -0500455{
Vince Lehman26b215c2014-08-17 15:00:41 -0500456 NFD_LOG_DEBUG("Checking for invalid face registrations");
Vince Lehmancd613c52014-07-30 14:34:49 -0500457
Junxiao Shi17a70012019-06-25 10:50:32 +0000458 std::set<uint64_t> activeFaceIds;
Davide Pesaventod396b612017-02-20 22:11:50 -0500459 for (const auto& faceStatus : activeFaces) {
460 activeFaceIds.insert(faceStatus.getFaceId());
Junxiao Shi78926c92015-02-28 22:56:06 -0700461 }
Junxiao Shi17a70012019-06-25 10:50:32 +0000462 getGlobalIoService().post([=] { m_rib.beginRemoveFailedFaces(activeFaceIds); });
Vince Lehman26b215c2014-08-17 15:00:41 -0500463
464 // Reschedule the check for future clean up
465 scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
Vince Lehmancd613c52014-07-30 14:34:49 -0500466}
467
468void
Davide Pesaventod396b612017-02-20 22:11:50 -0500469RibManager::onNotification(const ndn::nfd::FaceEventNotification& notification)
Vince Lehmancd613c52014-07-30 14:34:49 -0500470{
Yanbiao Licf0db022016-01-29 00:54:25 -0800471 NFD_LOG_TRACE("onNotification: " << notification);
472
473 if (notification.getKind() == ndn::nfd::FACE_EVENT_DESTROYED) {
Davide Pesavento0a71dd32019-03-17 20:36:18 -0400474 NFD_LOG_DEBUG("Received notification for destroyed FaceId " << notification.getFaceId());
Junxiao Shi17a70012019-06-25 10:50:32 +0000475 getGlobalIoService().post([this, id = notification.getFaceId()] { m_rib.beginRemoveFace(id); });
Yanbiao Licf0db022016-01-29 00:54:25 -0800476 }
477}
478
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700479} // namespace nfd